1 /*-
2  * Copyright (c) 1980, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 5/31/93";
37 #else
38 static const char rcsid[] =
39   "$FreeBSD$";
40 #endif
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <unistd.h>
45 #if __STDC__
46 # include <stdarg.h>
47 #else
48 # include <varargs.h>
49 #endif
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 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 #if __STDC__
seterror(int id,...)294 seterror(int id, ...)
295 #else
296 seterror(id, va_alist)
297      int id;
298      va_dcl
299 #endif
300 {
301     if (seterr == 0) {
302 	char    berr[BUFSIZ];
303 	va_list va;
304 
305 #if __STDC__
306 	va_start(va, id);
307 #else
308 	va_start(va);
309 #endif
310 	if (id < 0 || id > sizeof(errorlist) / sizeof(errorlist[0]))
311 	    id = ERR_INVALID;
312 	vsnprintf(berr, sizeof(berr), errorlist[id], va);
313 	va_end(va);
314 
315 	seterr = strsave(berr);
316     }
317 }
318 
319 /*
320  * Print the error with the given id.
321  *
322  * Special ids:
323  *	ERR_SILENT: Print nothing.
324  *	ERR_OLD: Print the previously set error if one was there.
325  *	         otherwise return.
326  *	ERR_NAME: If this bit is set, print the name of the function
327  *		  in bname
328  *
329  * This routine always resets or exits.  The flag haderr
330  * is set so the routine who catches the unwind can propagate
331  * it if they want.
332  *
333  * Note that any open files at the point of error will eventually
334  * be closed in the routine process in sh.c which is the only
335  * place error unwinds are ever caught.
336  */
337 void
338 #if __STDC__
stderror(int id,...)339 stderror(int id, ...)
340 #else
341 stderror(id, va_alist)
342     int     id;
343     va_dcl
344 #endif
345 {
346     va_list va;
347     Char **v;
348     int     flags = id & ERR_FLAGS;
349 
350     id &= ~ERR_FLAGS;
351 
352     if ((flags & ERR_OLD) && seterr == NULL)
353 	return;
354 
355     if (id < 0 || id > sizeof(errorlist) / sizeof(errorlist[0]))
356 	id = ERR_INVALID;
357 
358     (void) fflush(cshout);
359     (void) fflush(csherr);
360     haderr = 1;			/* Now to diagnostic output */
361     timflg = 0;			/* This isn't otherwise reset */
362 
363 
364     if (!(flags & ERR_SILENT)) {
365 	if (flags & ERR_NAME)
366 	    (void) fprintf(csherr, "%s: ", bname);
367 	if ((flags & ERR_OLD))
368 	    /* Old error. */
369 	    (void) fprintf(csherr, "%s.\n", seterr);
370 	else {
371 #if __STDC__
372 	    va_start(va, id);
373 #else
374 	    va_start(va);
375 #endif
376 	    (void) vfprintf(csherr, errorlist[id], va);
377 	    va_end(va);
378 	    (void) fprintf(csherr, ".\n");
379 	}
380     }
381 
382     if (seterr) {
383 	xfree((ptr_t) seterr);
384 	seterr = NULL;
385     }
386 
387     if ((v = pargv) != NULL)
388 	pargv = 0, blkfree(v);
389     if ((v = gargv) != NULL)
390 	gargv = 0, blkfree(v);
391 
392     (void) fflush(cshout);
393     (void) fflush(csherr);
394     didfds = 0;			/* Forget about 0,1,2 */
395     /*
396      * Go away if -e or we are a child shell
397      */
398     if (exiterr || child)
399 	xexit(1);
400 
401     /*
402      * Reset the state of the input. This buffered seek to end of file will
403      * also clear the while/foreach stack.
404      */
405     btoeof();
406 
407     set(STRstatus, Strsave(STR1));
408     if (tpgrp > 0)
409 	(void) tcsetpgrp(FSHTTY, tpgrp);
410     reset();			/* Unwind */
411 }
412