xref: /openbsd-src/usr.bin/make/error.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: error.c,v 1.8 2001/07/18 14:49:13 espie Exp $ */
3 
4 /*
5  * Copyright (c) 2001 Marc Espie.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifdef __STDC__
29 #include <stdarg.h>
30 #else
31 #include <varargs.h>
32 #endif
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 
37 #include "config.h"
38 #include "defines.h"
39 #include "error.h"
40 #include "job.h"
41 #include "targ.h"
42 
43 #include "lowparse.h"
44 
45 int	    fatal_errors = 0;
46 static void ParseVErrorInternal(const char *, unsigned long, int, const char *, va_list);
47 /*-
48  * Error --
49  *	Print an error message given its format.
50  */
51 /* VARARGS */
52 void
53 #ifdef __STDC__
54 Error(char *fmt, ...)
55 #else
56 Error(va_alist)
57 	va_dcl
58 #endif
59 {
60 	va_list ap;
61 #ifdef __STDC__
62 	va_start(ap, fmt);
63 #else
64 	char *fmt;
65 
66 	va_start(ap);
67 	fmt = va_arg(ap, char *);
68 #endif
69 	(void)vfprintf(stderr, fmt, ap);
70 	va_end(ap);
71 	(void)fprintf(stderr, "\n");
72 }
73 
74 /*-
75  * Fatal --
76  *	Produce a Fatal error message. If jobs are running, waits for them
77  *	to finish.
78  *
79  * Side Effects:
80  *	The program exits
81  */
82 /* VARARGS */
83 void
84 #ifdef __STDC__
85 Fatal(char *fmt, ...)
86 #else
87 Fatal(va_alist)
88 	va_dcl
89 #endif
90 {
91 	va_list ap;
92 #ifdef __STDC__
93 	va_start(ap, fmt);
94 #else
95 	char *fmt;
96 
97 	va_start(ap);
98 	fmt = va_arg(ap, char *);
99 #endif
100 	Job_Wait();
101 
102 	(void)vfprintf(stderr, fmt, ap);
103 	va_end(ap);
104 	(void)fprintf(stderr, "\n");
105 
106 	if (DEBUG(GRAPH2))
107 		Targ_PrintGraph(2);
108 	exit(2);		/* Not 1 so -q can distinguish error */
109 }
110 
111 /*
112  * Punt --
113  *	Major exception once jobs are being created. Kills all jobs, prints
114  *	a message and exits.
115  *
116  * Side Effects:
117  *	All children are killed indiscriminately and the program Lib_Exits
118  */
119 /* VARARGS */
120 void
121 #ifdef __STDC__
122 Punt(char *fmt, ...)
123 #else
124 Punt(va_alist)
125 	va_dcl
126 #endif
127 {
128 	va_list ap;
129 #ifdef __STDC__
130 	va_start(ap, fmt);
131 #else
132 	char *fmt;
133 
134 	va_start(ap);
135 	fmt = va_arg(ap, char *);
136 #endif
137 
138 	(void)fprintf(stderr, "make: ");
139 	(void)vfprintf(stderr, fmt, ap);
140 	va_end(ap);
141 	(void)fprintf(stderr, "\n");
142 
143 	DieHorribly();
144 }
145 
146 /*-
147  * DieHorribly --
148  *	Exit without giving a message.
149  *
150  * Side Effects:
151  *	A big one...
152  */
153 void
154 DieHorribly()
155 {
156 	Job_AbortAll();
157 	if (DEBUG(GRAPH2))
158 		Targ_PrintGraph(2);
159 	exit(2);		/* Not 1, so -q can distinguish error */
160 }
161 
162 /*
163  * Finish --
164  *	Called when aborting due to errors in child shell to signal
165  *	abnormal exit.
166  *
167  * Side Effects:
168  *	The program exits
169  */
170 void
171 Finish(errors)
172 	int errors;	/* number of errors encountered in Make_Make */
173 {
174 	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
175 }
176 
177 
178 /*-
179  * ParseVErrorInternal	--
180  *	Error message abort function for parsing. Prints out the context
181  *	of the error (line number and file) as well as the message with
182  *	two optional arguments.
183  *
184  * Side Effects:
185  *	"fatals" is incremented if the level is PARSE_FATAL.
186  */
187 /* VARARGS */
188 static void
189 #ifdef __STDC__
190 ParseVErrorInternal(const char *cfname, unsigned long clineno, int type,
191 	const char *fmt, va_list ap)
192 #else
193 ParseVErrorInternal(va_alist)
194 	va_dcl
195 #endif
196 {
197 	if (cfname)
198 	    (void)fprintf(stderr, "\"%s\", line %lu: ", cfname, clineno);
199 	if (type == PARSE_WARNING)
200 		(void)fprintf(stderr, "warning: ");
201 	(void)vfprintf(stderr, fmt, ap);
202 	va_end(ap);
203 	(void)fprintf(stderr, "\n");
204 	if (type == PARSE_FATAL)
205 		fatal_errors ++;
206 }
207 
208 /*-
209  * Parse_Error	--
210  *	External interface to ParseVErrorInternal; uses the default filename
211  *	Line number.
212  */
213 /* VARARGS */
214 void
215 #ifdef __STDC__
216 Parse_Error(int type, const char *fmt, ...)
217 #else
218 Parse_Error(va_alist)
219 	va_dcl
220 #endif
221 {
222 	va_list ap;
223 #ifdef __STDC__
224 	va_start(ap, fmt);
225 #else
226 	int type;		/* Error type (PARSE_WARNING, PARSE_FATAL) */
227 	char *fmt;
228 
229 	va_start(ap);
230 	type = va_arg(ap, int);
231 	fmt = va_arg(ap, char *);
232 #endif
233 
234 	ParseVErrorInternal(Parse_Getfilename(), Parse_Getlineno(), type, fmt, ap);
235 }
236 
237