xref: /openbsd-src/bin/csh/error.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: error.c,v 1.8 2008/05/17 23:31:52 sobrado Exp $	*/
2 /*	$NetBSD: err.c,v 1.6 1995/03/21 09:02:47 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1980, 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
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  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 5/31/93";
36 #else
37 static char rcsid[] = "$OpenBSD: error.c,v 1.8 2008/05/17 23:31:52 sobrado Exp $";
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <stdarg.h>
45 
46 #include "csh.h"
47 #include "extern.h"
48 
49 char   *seterr = NULL;	/* Holds last error if there was one */
50 
51 #define ERR_FLAGS	0xf0000000
52 #define ERR_NAME	0x10000000
53 #define ERR_SILENT	0x20000000
54 #define ERR_OLD		0x40000000
55 
56 static char *errorlist[] =
57 {
58 #define ERR_SYNTAX	0
59     "Syntax Error",
60 #define ERR_NOTALLOWED	1
61     "%s is not allowed",
62 #define ERR_WTOOLONG	2
63     "Word too long",
64 #define ERR_LTOOLONG	3
65     "$< line too long",
66 #define ERR_DOLZERO	4
67     "No file for $0",
68 #define ERR_DOLQUEST	5
69     "$? not allowed here",
70 #define ERR_INCBR	6
71     "Incomplete [] modifier",
72 #define ERR_EXPORD	7
73     "$ expansion must end before ]",
74 #define ERR_BADMOD	8
75     "Bad : modifier in $ (%c)",
76 #define ERR_SUBSCRIPT	9
77     "Subscript error",
78 #define ERR_BADNUM	10
79     "Badly formed number",
80 #define ERR_NOMORE	11
81     "No more words",
82 #define ERR_FILENAME	12
83     "Missing file name",
84 #define ERR_GLOB	13
85     "Internal glob error",
86 #define ERR_COMMAND	14
87     "Command not found",
88 #define ERR_TOOFEW	15
89     "Too few arguments",
90 #define ERR_TOOMANY	16
91     "Too many arguments",
92 #define ERR_DANGER	17
93     "Too dangerous to alias that",
94 #define ERR_EMPTYIF	18
95     "Empty if",
96 #define ERR_IMPRTHEN	19
97     "Improper then",
98 #define ERR_NOPAREN	20
99     "Words not parenthesized",
100 #define ERR_NOTFOUND	21
101     "%s not found",
102 #define ERR_MASK	22
103     "Improper mask",
104 #define ERR_LIMIT	23
105     "No such limit",
106 #define ERR_TOOLARGE	24
107     "Argument too large",
108 #define ERR_SCALEF	25
109     "Improper or unknown scale factor",
110 #define ERR_UNDVAR	26
111     "Undefined variable",
112 #define ERR_DEEP	27
113     "Directory stack not that deep",
114 #define ERR_BADSIG	28
115     "Bad signal number",
116 #define ERR_UNKSIG	29
117     "Unknown signal; kill -l lists signals",
118 #define ERR_VARBEGIN	30
119     "Variable name must begin with a letter",
120 #define ERR_VARTOOLONG	31
121     "Variable name too long",
122 #define ERR_VARALNUM	32
123     "Variable name must contain alphanumeric characters",
124 #define ERR_JOBCONTROL	33
125     "No job control in this shell",
126 #define ERR_EXPRESSION	34
127     "Expression Syntax",
128 #define ERR_NOHOMEDIR	35
129     "No home directory",
130 #define ERR_CANTCHANGE	36
131     "Can't change to home directory",
132 #define ERR_NULLCOM	37
133     "Invalid null command",
134 #define ERR_ASSIGN	38
135     "Assignment missing expression",
136 #define ERR_UNKNOWNOP	39
137     "Unknown operator",
138 #define ERR_AMBIG	40
139     "Ambiguous",
140 #define ERR_EXISTS	41
141     "%s: File exists",
142 #define ERR_INTR	42
143     "Interrupted",
144 #define ERR_RANGE	43
145     "Subscript out of range",
146 #define ERR_OVERFLOW	44
147     "Line overflow",
148 #define ERR_VARMOD	45
149     "Unknown variable modifier",
150 #define ERR_NOSUCHJOB	46
151     "No such job",
152 #define ERR_TERMINAL	47
153     "Can't from terminal",
154 #define ERR_NOTWHILE	48
155     "Not in while/foreach",
156 #define ERR_NOPROC	49
157     "No more processes",
158 #define ERR_NOMATCH	50
159     "No match",
160 #define ERR_MISSING	51
161     "Missing %c",
162 #define ERR_UNMATCHED	52
163     "Unmatched %c",
164 #define ERR_NOMEM	53
165     "Out of memory",
166 #define ERR_PIPE	54
167     "Can't make pipe",
168 #define ERR_SYSTEM	55
169     "%s: %s",
170 #define ERR_STRING	56
171     "%s",
172 #define ERR_JOBS	57
173     "usage: jobs [-l]",
174 #define ERR_JOBARGS	58
175     "Arguments should be jobs or process id's",
176 #define ERR_JOBCUR	59
177     "No current job",
178 #define ERR_JOBPREV	60
179     "No previous job",
180 #define ERR_JOBPAT	61
181     "No job matches pattern",
182 #define ERR_NESTING	62
183     "Fork nesting > %d; maybe `...` loop",
184 #define ERR_JOBCTRLSUB	63
185     "No job control in subshells",
186 #define ERR_BADPLPS	64
187     "Badly placed ()'s",
188 #define ERR_STOPPED	65
189     "%sThere are suspended jobs",
190 #define ERR_NODIR	66
191     "No other directory",
192 #define ERR_EMPTY	67
193     "Directory stack empty",
194 #define ERR_BADDIR	68
195     "Bad directory",
196 #define ERR_DIRUS	69
197     "usage: %s [-lnv]%s",
198 #define ERR_HFLAG	70
199     "No operand for -h flag",
200 #define ERR_NOTLOGIN	71
201     "Not a login shell",
202 #define ERR_DIV0	72
203     "Division by 0",
204 #define ERR_MOD0	73
205     "Mod by 0",
206 #define ERR_BADSCALE	74
207     "Bad scaling; did you mean \"%s\"?",
208 #define ERR_SUSPLOG	75
209     "Can't suspend a login shell (yet)",
210 #define ERR_UNKUSER	76
211     "Unknown user: %s",
212 #define ERR_NOHOME	77
213     "No $home variable set",
214 #define ERR_HISTUS	78
215     "usage: history [-hr] [n]",
216 #define ERR_SPDOLLT	79
217     "$, ! or < not allowed with $# or $?",
218 #define ERR_NEWLINE	80
219     "Newline in variable name",
220 #define ERR_SPSTAR	81
221     "* not allowed with $# or $?",
222 #define ERR_DIGIT	82
223     "$?<digit> or $#<digit> not allowed",
224 #define ERR_VARILL	83
225     "Illegal variable name",
226 #define ERR_NLINDEX	84
227     "Newline in variable index",
228 #define ERR_EXPOVFL	85
229     "Expansion buffer overflow",
230 #define ERR_VARSYN	86
231     "Variable syntax",
232 #define ERR_BADBANG	87
233     "Bad ! form",
234 #define ERR_NOSUBST	88
235     "No previous substitute",
236 #define ERR_BADSUBST	89
237     "Bad substitute",
238 #define ERR_LHS		90
239     "No previous left hand side",
240 #define ERR_RHSLONG	91
241     "Right hand side too long",
242 #define ERR_BADBANGMOD	92
243     "Bad ! modifier: %c",
244 #define ERR_MODFAIL	93
245     "Modifier failed",
246 #define ERR_SUBOVFL	94
247     "Substitution buffer overflow",
248 #define ERR_BADBANGARG	95
249     "Bad ! arg selector",
250 #define ERR_NOSEARCH	96
251     "No prev search",
252 #define ERR_NOEVENT	97
253     "%s: Event not found",
254 #define ERR_TOOMANYRP	98
255     "Too many )'s",
256 #define ERR_TOOMANYLP	99
257     "Too many ('s",
258 #define ERR_BADPLP	100
259     "Badly placed (",
260 #define ERR_MISRED	101
261     "Missing name for redirect",
262 #define ERR_OUTRED	102
263     "Ambiguous output redirect",
264 #define ERR_REDPAR	103
265     "Can't << within ()'s",
266 #define ERR_INRED	104
267     "Ambiguous input redirect",
268 #define ERR_ALIASLOOP	105
269     "Alias loop",
270 #define ERR_HISTLOOP	106
271     "!# History loop",
272 #define ERR_ARCH	107
273     "%s: %s. Wrong Architecture",
274 #define ERR_FILEINQ	108
275     "Malformed file inquiry",
276 #define ERR_SELOVFL	109
277     "Selector overflow",
278 #define ERR_INVALID	110
279     "Invalid Error"
280 };
281 
282 /*
283  * The parser and scanner set up errors for later by calling seterr,
284  * which sets the variable err as a side effect; later to be tested,
285  * e.g. in process.
286  */
287 void
288 seterror(int id, ...)
289 {
290     if (seterr == 0) {
291 	char    berr[BUFSIZ];
292 	va_list va;
293 
294 	va_start(va, id);
295 	if (id < 0 || id >= sizeof(errorlist) / sizeof(errorlist[0]))
296 	    id = ERR_INVALID;
297 	vsnprintf(berr, sizeof(berr), errorlist[id], va);
298 	va_end(va);
299 
300 	seterr = strsave(berr);
301     }
302 }
303 
304 /*
305  * Print the error with the given id.
306  *
307  * Special ids:
308  *	ERR_SILENT: Print nothing.
309  *	ERR_OLD: Print the previously set error if one was there.
310  *		 otherwise return.
311  *	ERR_NAME: If this bit is set, print the name of the function
312  *		  in bname
313  *
314  * This routine always resets or exits.  The flag haderr
315  * is set so the routine who catches the unwind can propagate
316  * it if they want.
317  *
318  * Note that any open files at the point of error will eventually
319  * be closed in the routine process in sh.c which is the only
320  * place error unwinds are ever caught.
321  */
322 void
323 stderror(int id, ...)
324 {
325     va_list va;
326     Char **v;
327     int     flags = id & ERR_FLAGS;
328 
329     id &= ~ERR_FLAGS;
330 
331     if ((flags & ERR_OLD) && seterr == NULL)
332 	return;
333 
334     if (id < 0 || id > sizeof(errorlist) / sizeof(errorlist[0]))
335 	id = ERR_INVALID;
336 
337     (void) fflush(cshout);
338     (void) fflush(csherr);
339     haderr = 1;			/* Now to diagnostic output */
340     timflg = 0;			/* This isn't otherwise reset */
341 
342 
343     if (!(flags & ERR_SILENT)) {
344 	if (flags & ERR_NAME)
345 	    (void) fprintf(csherr, "%s: ", bname);
346 	if ((flags & ERR_OLD))
347 	    /* Old error. */
348 	    (void) fprintf(csherr, "%s.\n", seterr);
349 	else {
350 	    va_start(va, id);
351 	    (void) vfprintf(csherr, errorlist[id], va);
352 	    va_end(va);
353 	    (void) fprintf(csherr, ".\n");
354 	}
355     }
356 
357     if (seterr) {
358 	xfree((ptr_t) seterr);
359 	seterr = NULL;
360     }
361 
362     if ((v = pargv) != NULL)
363 	pargv = 0, blkfree(v);
364     if ((v = gargv) != NULL)
365 	gargv = 0, blkfree(v);
366 
367     (void) fflush(cshout);
368     (void) fflush(csherr);
369     didfds = 0;			/* Forget about 0,1,2 */
370     /*
371      * Go away if -e or we are a child shell
372      */
373     if (!exitset || exiterr || child)
374 	xexit(1);
375 
376     /*
377      * Reset the state of the input. This buffered seek to end of file will
378      * also clear the while/foreach stack.
379      */
380     btoeof();
381 
382     set(STRstatus, Strsave(STR1));
383     if (tpgrp > 0)
384 	(void) tcsetpgrp(FSHTTY, tpgrp);
385     reset();			/* Unwind */
386 }
387