Simplify the print routines by using LST_FOREACH instead of Lst_ForEach
[dragonfly.git] / usr.bin / make / util.c
CommitLineData
edf6dd37 1/*-
84de9e23
MD
2 * Copyright (c) 2002 Juli Mallett. All rights reserved.
3 * Copyright (c) 1988, 1989, 1990, 1993
b83dbac0 4 * The Regents of the University of California. All rights reserved.
84de9e23
MD
5 * Copyright (c) 1989 by Berkeley Softworks
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
b83dbac0
MO
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
84de9e23
MD
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
1de703da 26 *
92b94568
MO
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
1901cbd2 39 * $FreeBSD: src/usr.bin/make/util.c,v 1.16 2005/02/04 13:23:39 harti Exp $
151b14f6 40 * $DragonFly: src/usr.bin/make/util.c,v 1.19 2005/03/12 11:31:30 okumoto Exp $
92b94568
MO
41 */
42
43/*-
44 * util.c --
45 * General utilitarian routines for make(1).
984263bc
MD
46 */
47
9863ce62 48#include <sys/types.h>
edf6dd37 49#include <sys/stat.h>
84de9e23 50#include <err.h>
984263bc 51#include <errno.h>
84de9e23 52#include <stdarg.h>
9863ce62
MO
53#include <stdlib.h>
54#include <string.h>
84de9e23
MD
55#include <unistd.h>
56
9863ce62 57#include "globals.h"
84de9e23 58#include "job.h"
9863ce62
MO
59#include "targ.h"
60#include "util.h"
84de9e23 61
8d399e34 62static void enomem(void) __dead2;
3f9df635 63
84de9e23
MD
64/*-
65 * Debug --
66 * Print a debugging message given its format.
67 *
68 * Results:
69 * None.
70 *
71 * Side Effects:
72 * The message is printed.
73 */
74/* VARARGS */
75void
76Debug(const char *fmt, ...)
77{
78 va_list ap;
984263bc 79
84de9e23 80 va_start(ap, fmt);
fbfaa208 81 vfprintf(stderr, fmt, ap);
84de9e23 82 va_end(ap);
fbfaa208 83 fflush(stderr);
84de9e23 84}
984263bc 85
9508d822
MO
86/*-
87 * Print a debugging message given its format and append the current
88 * errno description. Terminate with a newline.
89 */
90/* VARARGS */
91void
92DebugM(const char *fmt, ...)
93{
94 va_list ap;
95 int e = errno;
96
97 va_start(ap, fmt);
98 vfprintf(stderr, fmt, ap);
99 fprintf(stderr, ": %s\n", strerror(e));
100 va_end(ap);
101 fflush(stderr);
102}
103
84de9e23
MD
104/*-
105 * Error --
106 * Print an error message given its format.
107 *
108 * Results:
109 * None.
110 *
111 * Side Effects:
112 * The message is printed.
113 */
114/* VARARGS */
115void
116Error(const char *fmt, ...)
984263bc 117{
84de9e23 118 va_list ap;
b83dbac0 119
84de9e23 120 va_start(ap, fmt);
fbfaa208 121 vfprintf(stderr, fmt, ap);
84de9e23 122 va_end(ap);
fbfaa208
MO
123 fprintf(stderr, "\n");
124 fflush(stderr);
984263bc 125}
984263bc 126
84de9e23
MD
127/*-
128 * Fatal --
129 * Produce a Fatal error message. If jobs are running, waits for them
130 * to finish.
131 *
132 * Results:
133 * None
984263bc 134 *
84de9e23
MD
135 * Side Effects:
136 * The program exits
984263bc 137 */
84de9e23
MD
138/* VARARGS */
139void
140Fatal(const char *fmt, ...)
984263bc 141{
84de9e23 142 va_list ap;
b83dbac0 143
84de9e23
MD
144 va_start(ap, fmt);
145 if (jobsRunning)
146 Job_Wait();
147
fbfaa208 148 vfprintf(stderr, fmt, ap);
84de9e23 149 va_end(ap);
fbfaa208
MO
150 fprintf(stderr, "\n");
151 fflush(stderr);
84de9e23
MD
152
153 if (DEBUG(GRAPH2))
154 Targ_PrintGraph(2);
155 exit(2); /* Not 1 so -q can distinguish error */
984263bc
MD
156}
157
84de9e23
MD
158/*
159 * Punt --
160 * Major exception once jobs are being created. Kills all jobs, prints
161 * a message and exits.
162 *
163 * Results:
164 * None
165 *
166 * Side Effects:
167 * All children are killed indiscriminately and the program Lib_Exits
168 */
169/* VARARGS */
170void
171Punt(const char *fmt, ...)
984263bc 172{
84de9e23 173 va_list ap;
984263bc 174
b83dbac0 175 va_start(ap, fmt);
fbfaa208
MO
176 fprintf(stderr, "make: ");
177 vfprintf(stderr, fmt, ap);
84de9e23 178 va_end(ap);
fbfaa208
MO
179 fprintf(stderr, "\n");
180 fflush(stderr);
984263bc 181
84de9e23 182 DieHorribly();
984263bc 183}
984263bc 184
84de9e23
MD
185/*-
186 * DieHorribly --
187 * Exit without giving a message.
188 *
189 * Results:
190 * None
191 *
192 * Side Effects:
193 * A big one...
194 */
195void
196DieHorribly(void)
984263bc 197{
84de9e23
MD
198 if (jobsRunning)
199 Job_AbortAll();
200 if (DEBUG(GRAPH2))
201 Targ_PrintGraph(2);
202 exit(2); /* Not 1, so -q can distinguish error */
984263bc
MD
203}
204
84de9e23
MD
205/*
206 * Finish --
207 * Called when aborting due to errors in child shell to signal
454224cf 208 * abnormal exit, with the number of errors encountered in Make_Make.
84de9e23
MD
209 *
210 * Results:
211 * None
212 *
213 * Side Effects:
214 * The program exits
215 */
984263bc 216void
454224cf 217Finish(int errors)
984263bc 218{
fbfaa208 219
84de9e23 220 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
984263bc
MD
221}
222
84de9e23
MD
223/*
224 * emalloc --
225 * malloc, but die on error.
226 */
227void *
454224cf 228emalloc(size_t len)
984263bc 229{
84de9e23
MD
230 void *p;
231
232 if ((p = malloc(len)) == NULL)
233 enomem();
fbfaa208 234 return (p);
984263bc
MD
235}
236
84de9e23
MD
237/*
238 * estrdup --
239 * strdup, but die on error.
240 */
241char *
454224cf 242estrdup(const char *str)
984263bc 243{
84de9e23 244 char *p;
984263bc 245
84de9e23
MD
246 if ((p = strdup(str)) == NULL)
247 enomem();
fbfaa208 248 return (p);
984263bc
MD
249}
250
84de9e23
MD
251/*
252 * erealloc --
253 * realloc, but die on error.
984263bc 254 */
84de9e23 255void *
454224cf 256erealloc(void *ptr, size_t size)
984263bc 257{
fbfaa208 258
84de9e23
MD
259 if ((ptr = realloc(ptr, size)) == NULL)
260 enomem();
fbfaa208 261 return (ptr);
84de9e23 262}
984263bc 263
84de9e23
MD
264/*
265 * enomem --
266 * die when out of memory.
267 */
3f9df635 268static void
454224cf 269enomem(void)
984263bc 270{
84de9e23
MD
271 err(2, NULL);
272}
984263bc 273
84de9e23
MD
274/*
275 * enunlink --
276 * Remove a file carefully, avoiding directories.
277 */
984263bc 278int
454224cf 279eunlink(const char *file)
984263bc 280{
84de9e23 281 struct stat st;
984263bc 282
84de9e23 283 if (lstat(file, &st) == -1)
fbfaa208 284 return (-1);
984263bc 285
84de9e23
MD
286 if (S_ISDIR(st.st_mode)) {
287 errno = EISDIR;
fbfaa208 288 return (-1);
84de9e23 289 }
fbfaa208 290 return (unlink(file));
84de9e23 291}
984263bc 292
151b14f6
MO
293/*
294 * Convert a flag word to a printable thing and print it
295 */
296void
297print_flags(FILE *fp, const struct flag2str *tab, u_int flags)
298{
299 int first = 1;
300
301 fprintf(fp, "(");
302 while (tab->str != NULL) {
303 if (flags & tab->flag) {
304 if (!first)
305 fprintf(fp, "|");
306 first = 0;
307 fprintf(fp, "%s", tab->str);
308 }
309 tab++;
310 }
311 fprintf(fp, ")");
312}