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