xref: /openbsd/usr.bin/make/error.c (revision 8932bfb7)
1 /*	$OpenBSD: error.c,v 1.19 2010/07/19 19:46:44 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 
41 #include "lowparse.h"
42 
43 int fatal_errors = 0;
44 bool supervise_jobs = false;
45 
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 Error(char *fmt, ...)
54 {
55 	va_list ap;
56 
57 	va_start(ap, fmt);
58 	(void)vfprintf(stderr, fmt, ap);
59 	va_end(ap);
60 	(void)fprintf(stderr, "\n");
61 }
62 
63 /*-
64  * Fatal --
65  *	Produce a Fatal error message. If jobs are running, waits for them
66  *	to finish.
67  *
68  * Side Effects:
69  *	The program exits
70  */
71 /* VARARGS */
72 void
73 Fatal(char *fmt, ...)
74 {
75 	va_list ap;
76 
77 	if (supervise_jobs)
78 		Job_Wait();
79 
80 	va_start(ap, fmt);
81 	(void)vfprintf(stderr, fmt, ap);
82 	va_end(ap);
83 	(void)fprintf(stderr, "\n");
84 
85 	if (DEBUG(GRAPH2))
86 		Targ_PrintGraph(2);
87 	exit(2);		/* Not 1 so -q can distinguish error */
88 }
89 
90 /*
91  * Punt --
92  *	Major exception once jobs are being created. Kills all jobs, prints
93  *	a message and exits.
94  *
95  * Side Effects:
96  *	All children are killed indiscriminately and the program Lib_Exits
97  */
98 /* VARARGS */
99 void
100 Punt(char *fmt, ...)
101 {
102 	va_list ap;
103 
104 	va_start(ap, fmt);
105 	(void)fprintf(stderr, "make: ");
106 	(void)vfprintf(stderr, fmt, ap);
107 	va_end(ap);
108 	(void)fprintf(stderr, "\n");
109 
110 	Job_AbortAll();
111 	if (DEBUG(GRAPH2))
112 		Targ_PrintGraph(2);
113 	exit(2);		/* Not 1, so -q can distinguish error */
114 }
115 
116 /*
117  * Finish --
118  *	Called when aborting due to errors in child shell to signal
119  *	abnormal exit.
120  *
121  * Side Effects:
122  *	The program exits
123  */
124 void
125 Finish(int errors) /* number of errors encountered in Make_Make */
126 {
127 	Job_Wait();
128 	if (errors != 0) {
129 		Error("Stop in %s:", Var_Value(".CURDIR"));
130 	}
131 	print_errors();
132 	if (DEBUG(GRAPH2))
133 		Targ_PrintGraph(2);
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 char *cfname, unsigned long clineno, int type,
150 	const char *fmt, va_list ap)
151 {
152 	if (cfname)
153 	    (void)fprintf(stderr, "\"%s\", line %lu: ", cfname, clineno);
154 	if (type == PARSE_WARNING)
155 		(void)fprintf(stderr, "warning: ");
156 	(void)vfprintf(stderr, fmt, ap);
157 	va_end(ap);
158 	(void)fprintf(stderr, "\n");
159 	if (type == PARSE_FATAL)
160 		fatal_errors ++;
161 }
162 
163 /*-
164  * Parse_Error	--
165  *	External interface to ParseVErrorInternal; uses the default filename
166  *	Line number.
167  */
168 /* VARARGS */
169 void
170 Parse_Error(int type, const char *fmt, ...)
171 {
172 	va_list ap;
173 
174 	va_start(ap, fmt);
175 	ParseVErrorInternal(Parse_Getfilename(), Parse_Getlineno(), type,
176 	    fmt, ap);
177 	va_end(ap);
178 }
179 
180