xref: /openbsd/usr.bin/make/error.c (revision 09467b48)
1 /*	$OpenBSD: error.c,v 1.25 2015/09/27 16:58:16 guenther 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 			__attribute__((__format__ (printf, 3, 0)));
51 /*-
52  * Error --
53  *	Print an error message given its format.
54  */
55 void
56 Error(const 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 void
75 Fatal(const char *fmt, ...)
76 {
77 	va_list ap;
78 
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 		post_mortem();
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 void
100 Punt(const char *fmt, ...)
101 {
102 	if (fmt) {
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 
112 	Job_AbortAll();
113 	if (DEBUG(GRAPH2))
114 		post_mortem();
115 	exit(2);		/* Not 1, so -q can distinguish error */
116 }
117 
118 /*
119  * Finish --
120  *	Called when aborting due to errors in command or fatal signal
121  *
122  * Side Effects:
123  *	The program exits
124  */
125 void
126 Finish()
127 {
128 	Job_Wait();
129 	print_errors();
130 	if (DEBUG(GRAPH2))
131 		post_mortem();
132 	exit(2);		/* Not 1 so -q can distinguish error */
133 }
134 
135 
136 /*-
137  * ParseVErrorInternal	--
138  *	Error message abort function for parsing. Prints out the context
139  *	of the error (line number and file) as well as the message with
140  *	two optional arguments.
141  *
142  * Side Effects:
143  *	"fatals" is incremented if the level is PARSE_FATAL.
144  */
145 static void
146 ParseVErrorInternal(const Location *origin, int type, const char *fmt,
147     va_list ap)
148 {
149 	static bool first = true;
150 	fprintf(stderr, "*** %s",
151 	    type == PARSE_WARNING ? "Warning" : "Parse error");
152 	if (first) {
153 		fprintf(stderr, " in %s: ", Var_Value(".CURDIR"));
154 		first = false;
155 	} else
156 		fprintf(stderr, ": ");
157 	vfprintf(stderr, fmt, ap);
158 	va_end(ap);
159 	if (origin->fname)
160 	    	fprintf(stderr, " (%s:%lu)", origin->fname, origin->lineno);
161 	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 void
172 Parse_Error(int type, const char *fmt, ...)
173 {
174 	va_list ap;
175 	Location l;
176 
177 	va_start(ap, fmt);
178 	Parse_FillLocation(&l);
179 	ParseVErrorInternal(&l, type, fmt, ap);
180 	va_end(ap);
181 }
182