1 /* $NetBSD: err.c,v 1.21 2013/07/16 17:47:43 christos Exp $ */
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)err.c 8.1 (Berkeley) 5/31/93";
37 __RCSID("$NetBSD: err.c,v 1.21 2013/07/16 17:47:43 christos Exp $");
41 #include <sys/types.h>
50 char *seterr
= NULL
; /* Holds last error if there was one */
52 #define ERR_FLAGS ((int)0xf0000000)
53 #define ERR_NAME 0x10000000
54 #define ERR_SILENT 0x20000000
55 #define ERR_OLD 0x40000000
57 static const char *errorlist
[] =
61 #define ERR_NOTALLOWED 1
63 #define ERR_WTOOLONG 2
65 #define ERR_LTOOLONG 3
69 #define ERR_DOLQUEST 5
70 "$? not allowed here",
72 "Incomplete [] modifier",
74 "$ expansion must end before ]",
76 "Bad : modifier in $ (%c)",
77 #define ERR_SUBSCRIPT 9
80 "Badly formed number",
83 #define ERR_FILENAME 12
86 "Internal glob error",
87 #define ERR_COMMAND 14
91 #define ERR_TOOMANY 16
94 "Too dangerous to alias that",
95 #define ERR_EMPTYIF 18
97 #define ERR_IMPRTHEN 19
99 #define ERR_NOPAREN 20
100 "Words not parenthesized",
101 #define ERR_NOTFOUND 21
107 #define ERR_TOOLARGE 24
108 "Argument too large",
109 #define ERR_SCALEF 25
110 "Improper or unknown scale factor",
111 #define ERR_UNDVAR 26
112 "Undefined variable",
114 "Directory stack not that deep",
115 #define ERR_BADSIG 28
117 #define ERR_UNKSIG 29
118 "Unknown signal; kill -l lists signals",
119 #define ERR_VARBEGIN 30
120 "Variable name must begin with a letter",
121 #define ERR_VARTOOLONG 31
122 "Variable name too long",
123 #define ERR_VARALNUM 32
124 "Variable name must contain alphanumeric characters",
125 #define ERR_JOBCONTROL 33
126 "No job control in this shell",
127 #define ERR_EXPRESSION 34
129 #define ERR_NOHOMEDIR 35
131 #define ERR_CANTCHANGE 36
132 "Can't change to home directory",
133 #define ERR_NULLCOM 37
134 "Invalid null command",
135 #define ERR_ASSIGN 38
136 "Assignment missing expression",
137 #define ERR_UNKNOWNOP 39
141 #define ERR_EXISTS 41
146 "Subscript out of range",
147 #define ERR_OVERFLOW 44
149 #define ERR_VARMOD 45
150 "Unknown variable modifier",
151 #define ERR_NOSUCHJOB 46
153 #define ERR_TERMINAL 47
154 "Can't from terminal",
155 #define ERR_NOTWHILE 48
156 "Not in while/foreach",
157 #define ERR_NOPROC 49
159 #define ERR_NOMATCH 50
161 #define ERR_MISSING 51
163 #define ERR_UNMATCHED 52
169 #define ERR_SYSTEM 55
171 #define ERR_STRING 56
174 "usage: jobs [ -l ]",
175 #define ERR_JOBARGS 58
176 "Arguments should be jobs or process id's",
177 #define ERR_JOBCUR 59
179 #define ERR_JOBPREV 60
181 #define ERR_JOBPAT 61
182 "No job matches pattern",
183 #define ERR_NESTING 62
184 "Fork nesting > %d; maybe `...` loop",
185 #define ERR_JOBCTRLSUB 63
186 "No job control in subshells",
187 #define ERR_BADPLPS 64
189 #define ERR_STOPPED 65
190 "%sThere are suspended jobs",
192 "No other directory",
194 "Directory stack empty",
195 #define ERR_BADDIR 68
198 "usage: %s [-lvn]%s",
200 "No operand for -h flag",
201 #define ERR_NOTLOGIN 71
207 #define ERR_BADSCALE 74
208 "Bad scaling; did you mean \"%s\"?",
209 #define ERR_SUSPLOG 75
210 "Can't suspend a login shell (yet)",
211 #define ERR_UNKUSER 76
213 #define ERR_NOHOME 77
214 "No $home variable set",
215 #define ERR_HISTUS 78
216 "usage: history [-rh] [# number of events]",
217 #define ERR_SPDOLLT 79
218 "$, ! or < not allowed with $# or $?",
219 #define ERR_NEWLINE 80
220 "Newline in variable name",
221 #define ERR_SPSTAR 81
222 "* not allowed with $# or $?",
224 "$?<digit> or $#<digit> not allowed",
225 #define ERR_VARILL 83
226 "Illegal variable name",
227 #define ERR_NLINDEX 84
228 "Newline in variable index",
229 #define ERR_EXPOVFL 85
230 "Expansion buffer overflow",
231 #define ERR_VARSYN 86
233 #define ERR_BADBANG 87
235 #define ERR_NOSUBST 88
236 "No previous substitute",
237 #define ERR_BADSUBST 89
240 "No previous left hand side",
241 #define ERR_RHSLONG 91
242 "Right hand side too long",
243 #define ERR_BADBANGMOD 92
244 "Bad ! modifier: %c",
245 #define ERR_MODFAIL 93
247 #define ERR_SUBOVFL 94
248 "Substitution buffer overflow",
249 #define ERR_BADBANGARG 95
250 "Bad ! arg selector",
251 #define ERR_NOSEARCH 96
253 #define ERR_NOEVENT 97
254 "%s: Event not found",
255 #define ERR_TOOMANYRP 98
257 #define ERR_TOOMANYLP 99
259 #define ERR_BADPLP 100
261 #define ERR_MISRED 101
262 "Missing name for redirect",
263 #define ERR_OUTRED 102
264 "Ambiguous output redirect",
265 #define ERR_REDPAR 103
266 "Can't << within ()'s",
267 #define ERR_INRED 104
268 "Ambiguous input redirect",
269 #define ERR_ALIASLOOP 105
271 #define ERR_HISTLOOP 106
274 "%s: %s. Wrong Architecture",
275 #define ERR_FILEINQ 108
276 "Malformed file inquiry",
277 #define ERR_SELOVFL 109
279 #define ERR_INVALID 110
284 * The parser and scanner set up errors for later by calling seterr,
285 * which sets the variable err as a side effect; later to be tested,
289 seterror(int id
, ...)
296 if (id
< 0 || id
>= (int)(sizeof(errorlist
) / sizeof(errorlist
[0])) - 1)
298 (void)vsnprintf(berr
, sizeof(berr
), errorlist
[id
], va
);
301 seterr
= strsave(berr
);
306 * Print the error with the given id.
309 * ERR_SILENT: Print nothing.
310 * ERR_OLD: Print the previously set error if one was there.
312 * ERR_NAME: If this bit is set, print the name of the function
315 * This routine always resets or exits. The flag haderr
316 * is set so the routine who catches the unwind can propogate
319 * Note that any open files at the point of error will eventually
320 * be closed in the routine process in sh.c which is the only
321 * place error unwinds are ever caught.
324 stderror(int id
, ...)
330 flags
= id
& ERR_FLAGS
;
333 if ((flags
& ERR_OLD
) && seterr
== NULL
)
336 if (id
< 0 || id
> (int)(sizeof(errorlist
) / sizeof(errorlist
[0])))
339 (void)fflush(cshout
);
340 (void)fflush(csherr
);
341 haderr
= 1; /* Now to diagnostic output */
342 timflg
= 0; /* This isn't otherwise reset */
345 if (!(flags
& ERR_SILENT
)) {
346 if (flags
& ERR_NAME
)
347 (void)fprintf(csherr
, "%s: ", bname
);
348 if ((flags
& ERR_OLD
))
350 (void)fprintf(csherr
, "%s.\n", seterr
);
353 (void)vfprintf(csherr
, errorlist
[id
], va
);
355 (void)fprintf(csherr
, ".\n");
360 xfree((ptr_t
) seterr
);
364 if ((v
= pargv
) != NULL
)
365 pargv
= 0, blkfree(v
);
366 if ((v
= gargv
) != NULL
)
367 gargv
= 0, blkfree(v
);
369 (void)fflush(cshout
);
370 (void)fflush(csherr
);
371 didfds
= 0; /* Forget about 0,1,2 */
373 * Go away if -e or we are a child shell
375 if (exiterr
|| child
)
379 * Reset the state of the input. This buffered seek to end of file will
380 * also clear the while/foreach stack.
384 set(STRstatus
, Strsave(STR1
));
386 (void)tcsetpgrp(FSHTTY
, tpgrp
);
387 reset(); /* Unwind */