xref: /openbsd-src/usr.bin/make/error.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: error.c,v 1.16 2008/01/12 13:08:59 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 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 
35 #include "config.h"
36 #include "defines.h"
37 #include "error.h"
38 #include "job.h"
39 #include "targ.h"
40 #include "var.h"
41 
42 #include "lowparse.h"
43 
44 int fatal_errors = 0;
45 bool supervise_jobs = false;
46 
47 static void ParseVErrorInternal(const char *, unsigned long, int, const char *, va_list);
48 /*-
49  * Error --
50  *	Print an error message given its format.
51  */
52 /* VARARGS */
53 void
54 Error(char *fmt, ...)
55 {
56 	va_list ap;
57 
58 	va_start(ap, fmt);
59 	(void)vfprintf(stderr, fmt, ap);
60 	va_end(ap);
61 	(void)fprintf(stderr, "\n");
62 }
63 
64 /*-
65  * Fatal --
66  *	Produce a Fatal error message. If jobs are running, waits for them
67  *	to finish.
68  *
69  * Side Effects:
70  *	The program exits
71  */
72 /* VARARGS */
73 void
74 Fatal(char *fmt, ...)
75 {
76 	va_list ap;
77 
78 	if (supervise_jobs)
79 		Job_Wait();
80 
81 	va_start(ap, fmt);
82 	(void)vfprintf(stderr, fmt, ap);
83 	va_end(ap);
84 	(void)fprintf(stderr, "\n");
85 
86 	if (DEBUG(GRAPH2))
87 		Targ_PrintGraph(2);
88 	exit(2);		/* Not 1 so -q can distinguish error */
89 }
90 
91 /*
92  * Punt --
93  *	Major exception once jobs are being created. Kills all jobs, prints
94  *	a message and exits.
95  *
96  * Side Effects:
97  *	All children are killed indiscriminately and the program Lib_Exits
98  */
99 /* VARARGS */
100 void
101 Punt(char *fmt, ...)
102 {
103 	va_list ap;
104 
105 	va_start(ap, fmt);
106 	(void)fprintf(stderr, "make: ");
107 	(void)vfprintf(stderr, fmt, ap);
108 	va_end(ap);
109 	(void)fprintf(stderr, "\n");
110 
111 	DieHorribly();
112 }
113 
114 /*-
115  * DieHorribly --
116  *	Exit without giving a message.
117  *
118  * Side Effects:
119  *	A big one...
120  */
121 void
122 DieHorribly(void)
123 {
124 	Job_AbortAll();
125 	if (DEBUG(GRAPH2))
126 		Targ_PrintGraph(2);
127 	exit(2);		/* Not 1, so -q can distinguish error */
128 }
129 
130 /*
131  * Finish --
132  *	Called when aborting due to errors in child shell to signal
133  *	abnormal exit.
134  *
135  * Side Effects:
136  *	The program exits
137  */
138 void
139 Finish(int errors) /* number of errors encountered in Make_Make */
140 {
141 	Job_Wait();
142 	if (errors != 0) {
143 		Error("Stop in %s:", Var_Value(".CURDIR"));
144 	}
145 	print_errors();
146 	if (DEBUG(GRAPH2))
147 		Targ_PrintGraph(2);
148 	exit(2);		/* Not 1 so -q can distinguish error */
149 }
150 
151 
152 /*-
153  * ParseVErrorInternal	--
154  *	Error message abort function for parsing. Prints out the context
155  *	of the error (line number and file) as well as the message with
156  *	two optional arguments.
157  *
158  * Side Effects:
159  *	"fatals" is incremented if the level is PARSE_FATAL.
160  */
161 /* VARARGS */
162 static void
163 ParseVErrorInternal(const char *cfname, unsigned long clineno, int type,
164 	const char *fmt, va_list ap)
165 {
166 	if (cfname)
167 	    (void)fprintf(stderr, "\"%s\", line %lu: ", cfname, clineno);
168 	if (type == PARSE_WARNING)
169 		(void)fprintf(stderr, "warning: ");
170 	(void)vfprintf(stderr, fmt, ap);
171 	va_end(ap);
172 	(void)fprintf(stderr, "\n");
173 	if (type == PARSE_FATAL)
174 		fatal_errors ++;
175 }
176 
177 /*-
178  * Parse_Error	--
179  *	External interface to ParseVErrorInternal; uses the default filename
180  *	Line number.
181  */
182 /* VARARGS */
183 void
184 Parse_Error(int type, const char *fmt, ...)
185 {
186 	va_list ap;
187 
188 	va_start(ap, fmt);
189 	ParseVErrorInternal(Parse_Getfilename(), Parse_Getlineno(), type,
190 	    fmt, ap);
191 	va_end(ap);
192 }
193 
194