xref: /openbsd-src/usr.bin/make/error.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: error.c,v 1.24 2012/10/18 17:54:43 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 #include "dump.h"
46 
47 int fatal_errors = 0;
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 	Job_Wait();
81 
82 	va_start(ap, fmt);
83 	(void)vfprintf(stderr, fmt, ap);
84 	va_end(ap);
85 	(void)fprintf(stderr, "\n");
86 
87 	if (DEBUG(GRAPH2))
88 		post_mortem();
89 	exit(2);		/* Not 1 so -q can distinguish error */
90 }
91 
92 /*
93  * Punt --
94  *	Major exception once jobs are being created. Kills all jobs, prints
95  *	a message and exits.
96  *
97  * Side Effects:
98  *	All children are killed indiscriminately and the program Lib_Exits
99  */
100 /* VARARGS */
101 void
102 Punt(char *fmt, ...)
103 {
104 	if (fmt) {
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 
114 	Job_AbortAll();
115 	if (DEBUG(GRAPH2))
116 		post_mortem();
117 	exit(2);		/* Not 1, so -q can distinguish error */
118 }
119 
120 /*
121  * Finish --
122  *	Called when aborting due to errors in command or fatal signal
123  *
124  * Side Effects:
125  *	The program exits
126  */
127 void
128 Finish()
129 {
130 	Job_Wait();
131 	print_errors();
132 	if (DEBUG(GRAPH2))
133 		post_mortem();
134 	exit(2);		/* Not 1 so -q can distinguish error */
135 }
136 
137 
138 /*-
139  * ParseVErrorInternal	--
140  *	Error message abort function for parsing. Prints out the context
141  *	of the error (line number and file) as well as the message with
142  *	two optional arguments.
143  *
144  * Side Effects:
145  *	"fatals" is incremented if the level is PARSE_FATAL.
146  */
147 /* VARARGS */
148 static void
149 ParseVErrorInternal(const Location *origin, int type, const char *fmt,
150     va_list ap)
151 {
152 	static bool first = true;
153 	fprintf(stderr, "*** %s",
154 	    type == PARSE_WARNING ? "Warning" : "Parse error");
155 	if (first) {
156 		fprintf(stderr, " in %s: ", Var_Value(".CURDIR"));
157 		first = false;
158 	} else
159 		fprintf(stderr, ": ");
160 	vfprintf(stderr, fmt, ap);
161 	va_end(ap);
162 	if (origin->fname)
163 	    	fprintf(stderr, " (%s:%lu)", origin->fname, origin->lineno);
164 	fprintf(stderr, "\n");
165 	if (type == PARSE_FATAL)
166 		fatal_errors ++;
167 }
168 
169 /*-
170  * Parse_Error	--
171  *	External interface to ParseVErrorInternal; uses the default filename
172  *	Line number.
173  */
174 /* VARARGS */
175 void
176 Parse_Error(int type, const char *fmt, ...)
177 {
178 	va_list ap;
179 	Location l;
180 
181 	va_start(ap, fmt);
182 	Parse_FillLocation(&l);
183 	ParseVErrorInternal(&l, type, fmt, ap);
184 	va_end(ap);
185 }
186