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