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