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