xref: /netbsd-src/bin/csh/err.c (revision 2fa7e14158d403140e8fb3c233c9e17417a69d87)
1*2fa7e141Sandvar /* $NetBSD: err.c,v 1.25 2022/04/08 10:17:52 andvar Exp $ */
249f0ad86Scgd 
361f28255Scgd /*-
4cee2bad8Smycroft  * Copyright (c) 1980, 1991, 1993
5cee2bad8Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
328ea378c6Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3449f0ad86Scgd #if 0
3549f0ad86Scgd static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 5/31/93";
3649f0ad86Scgd #else
37*2fa7e141Sandvar __RCSID("$NetBSD: err.c,v 1.25 2022/04/08 10:17:52 andvar Exp $");
3849f0ad86Scgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
4161f28255Scgd #include <sys/types.h>
42b771e65bSwiz 
4318158540Swiz #include <stdarg.h>
4461f28255Scgd #include <stdlib.h>
4561f28255Scgd #include <unistd.h>
46b771e65bSwiz 
4761f28255Scgd #include "csh.h"
4861f28255Scgd #include "extern.h"
4961f28255Scgd 
5061f28255Scgd char *seterr = NULL;	/* Holds last error if there was one */
5161f28255Scgd 
5237e39248Schristos #define ERR_FLAGS	((int)0xf0000000)
5361f28255Scgd #define ERR_NAME	0x10000000
5461f28255Scgd #define ERR_SILENT	0x20000000
5561f28255Scgd #define ERR_OLD		0x40000000
5661f28255Scgd 
57cdbd74daSmycroft static const char *errorlist[] =
5861f28255Scgd {
5961f28255Scgd #define ERR_SYNTAX	0
6061f28255Scgd     "Syntax Error",
6161f28255Scgd #define ERR_NOTALLOWED	1
6261f28255Scgd     "%s is not allowed",
6361f28255Scgd #define ERR_WTOOLONG	2
6461f28255Scgd     "Word too long",
6561f28255Scgd #define ERR_LTOOLONG	3
6661f28255Scgd     "$< line too long",
6761f28255Scgd #define ERR_DOLZERO	4
6861f28255Scgd     "No file for $0",
6961f28255Scgd #define ERR_DOLQUEST	5
7061f28255Scgd     "$? not allowed here",
7161f28255Scgd #define ERR_INCBR	6
7261f28255Scgd     "Incomplete [] modifier",
7361f28255Scgd #define ERR_EXPORD	7
7461f28255Scgd     "$ expansion must end before ]",
7561f28255Scgd #define ERR_BADMOD	8
7661f28255Scgd     "Bad : modifier in $ (%c)",
7761f28255Scgd #define ERR_SUBSCRIPT	9
7861f28255Scgd     "Subscript error",
7961f28255Scgd #define ERR_BADNUM	10
8061f28255Scgd     "Badly formed number",
8161f28255Scgd #define ERR_NOMORE	11
8261f28255Scgd     "No more words",
8361f28255Scgd #define ERR_FILENAME	12
8461f28255Scgd     "Missing file name",
8561f28255Scgd #define ERR_GLOB	13
8661f28255Scgd     "Internal glob error",
8761f28255Scgd #define ERR_COMMAND	14
8861f28255Scgd     "Command not found",
8961f28255Scgd #define ERR_TOOFEW	15
9061f28255Scgd     "Too few arguments",
9161f28255Scgd #define ERR_TOOMANY	16
9261f28255Scgd     "Too many arguments",
9361f28255Scgd #define ERR_DANGER	17
9461f28255Scgd     "Too dangerous to alias that",
9561f28255Scgd #define ERR_EMPTYIF	18
9661f28255Scgd     "Empty if",
9761f28255Scgd #define ERR_IMPRTHEN	19
9861f28255Scgd     "Improper then",
9961f28255Scgd #define ERR_NOPAREN	20
10061f28255Scgd     "Words not parenthesized",
10161f28255Scgd #define ERR_NOTFOUND	21
10261f28255Scgd     "%s not found",
10361f28255Scgd #define ERR_MASK	22
10461f28255Scgd     "Improper mask",
10561f28255Scgd #define ERR_LIMIT	23
10661f28255Scgd     "No such limit",
10761f28255Scgd #define ERR_TOOLARGE	24
10861f28255Scgd     "Argument too large",
10961f28255Scgd #define ERR_SCALEF	25
11061f28255Scgd     "Improper or unknown scale factor",
11161f28255Scgd #define ERR_UNDVAR	26
11261f28255Scgd     "Undefined variable",
11361f28255Scgd #define ERR_DEEP	27
11461f28255Scgd     "Directory stack not that deep",
11561f28255Scgd #define ERR_BADSIG	28
11661f28255Scgd     "Bad signal number",
11761f28255Scgd #define ERR_UNKSIG	29
11861f28255Scgd     "Unknown signal; kill -l lists signals",
11961f28255Scgd #define ERR_VARBEGIN	30
12061f28255Scgd     "Variable name must begin with a letter",
12161f28255Scgd #define ERR_VARTOOLONG	31
12261f28255Scgd     "Variable name too long",
12361f28255Scgd #define ERR_VARALNUM	32
12461f28255Scgd     "Variable name must contain alphanumeric characters",
12561f28255Scgd #define ERR_JOBCONTROL	33
12661f28255Scgd     "No job control in this shell",
12761f28255Scgd #define ERR_EXPRESSION	34
12861f28255Scgd     "Expression Syntax",
12961f28255Scgd #define ERR_NOHOMEDIR	35
13061f28255Scgd     "No home directory",
13161f28255Scgd #define ERR_CANTCHANGE	36
13261f28255Scgd     "Can't change to home directory",
13361f28255Scgd #define ERR_NULLCOM	37
13461f28255Scgd     "Invalid null command",
13561f28255Scgd #define ERR_ASSIGN	38
13661f28255Scgd     "Assignment missing expression",
13761f28255Scgd #define ERR_UNKNOWNOP	39
13861f28255Scgd     "Unknown operator",
13961f28255Scgd #define ERR_AMBIG	40
14061f28255Scgd     "Ambiguous",
14161f28255Scgd #define ERR_EXISTS	41
14261f28255Scgd     "%s: File exists",
14361f28255Scgd #define ERR_INTR	42
14461f28255Scgd     "Interrupted",
14561f28255Scgd #define ERR_RANGE	43
14661f28255Scgd     "Subscript out of range",
14761f28255Scgd #define ERR_OVERFLOW	44
14861f28255Scgd     "Line overflow",
14961f28255Scgd #define ERR_VARMOD	45
15061f28255Scgd     "Unknown variable modifier",
15161f28255Scgd #define ERR_NOSUCHJOB	46
15261f28255Scgd     "No such job",
15361f28255Scgd #define ERR_TERMINAL	47
15461f28255Scgd     "Can't from terminal",
15561f28255Scgd #define ERR_NOTWHILE	48
15661f28255Scgd     "Not in while/foreach",
15761f28255Scgd #define ERR_NOPROC	49
15861f28255Scgd     "No more processes",
15961f28255Scgd #define ERR_NOMATCH	50
16061f28255Scgd     "No match",
16161f28255Scgd #define ERR_MISSING	51
16261f28255Scgd     "Missing %c",
16361f28255Scgd #define ERR_UNMATCHED	52
16461f28255Scgd     "Unmatched %c",
16561f28255Scgd #define ERR_NOMEM	53
16661f28255Scgd     "Out of memory",
16761f28255Scgd #define ERR_PIPE	54
16861f28255Scgd     "Can't make pipe",
16961f28255Scgd #define ERR_SYSTEM	55
17061f28255Scgd     "%s: %s",
17161f28255Scgd #define ERR_STRING	56
17261f28255Scgd     "%s",
17361f28255Scgd #define ERR_JOBS	57
174a902e362Schristos     "usage: jobs [ -lZ ]",
17561f28255Scgd #define ERR_JOBARGS	58
17661f28255Scgd     "Arguments should be jobs or process id's",
17761f28255Scgd #define ERR_JOBCUR	59
17861f28255Scgd     "No current job",
17961f28255Scgd #define ERR_JOBPREV	60
18061f28255Scgd     "No previous job",
18161f28255Scgd #define ERR_JOBPAT	61
18261f28255Scgd     "No job matches pattern",
18361f28255Scgd #define ERR_NESTING	62
18461f28255Scgd     "Fork nesting > %d; maybe `...` loop",
18561f28255Scgd #define ERR_JOBCTRLSUB	63
18661f28255Scgd     "No job control in subshells",
18761f28255Scgd #define ERR_BADPLPS	64
18861f28255Scgd     "Badly placed ()'s",
18961f28255Scgd #define ERR_STOPPED	65
19061f28255Scgd     "%sThere are suspended jobs",
19161f28255Scgd #define ERR_NODIR	66
19261f28255Scgd     "No other directory",
19361f28255Scgd #define ERR_EMPTY	67
19461f28255Scgd     "Directory stack empty",
19561f28255Scgd #define ERR_BADDIR	68
19661f28255Scgd     "Bad directory",
19761f28255Scgd #define ERR_DIRUS	69
198b635f565Sjmmv     "usage: %s [-lvn]%s",
19961f28255Scgd #define ERR_HFLAG	70
20061f28255Scgd     "No operand for -h flag",
20161f28255Scgd #define ERR_NOTLOGIN	71
20261f28255Scgd     "Not a login shell",
20361f28255Scgd #define ERR_DIV0	72
20461f28255Scgd     "Division by 0",
20561f28255Scgd #define ERR_MOD0	73
20661f28255Scgd     "Mod by 0",
20761f28255Scgd #define ERR_BADSCALE	74
20861f28255Scgd     "Bad scaling; did you mean \"%s\"?",
20961f28255Scgd #define ERR_SUSPLOG	75
21061f28255Scgd     "Can't suspend a login shell (yet)",
21161f28255Scgd #define ERR_UNKUSER	76
21261f28255Scgd     "Unknown user: %s",
21361f28255Scgd #define ERR_NOHOME	77
21461f28255Scgd     "No $home variable set",
21561f28255Scgd #define ERR_HISTUS	78
216b635f565Sjmmv     "usage: history [-rh] [# number of events]",
21761f28255Scgd #define ERR_SPDOLLT	79
218cee2bad8Smycroft     "$, ! or < not allowed with $# or $?",
21961f28255Scgd #define ERR_NEWLINE	80
22061f28255Scgd     "Newline in variable name",
22161f28255Scgd #define ERR_SPSTAR	81
22261f28255Scgd     "* not allowed with $# or $?",
22361f28255Scgd #define ERR_DIGIT	82
22461f28255Scgd     "$?<digit> or $#<digit> not allowed",
22561f28255Scgd #define ERR_VARILL	83
22661f28255Scgd     "Illegal variable name",
22761f28255Scgd #define ERR_NLINDEX	84
22861f28255Scgd     "Newline in variable index",
22961f28255Scgd #define ERR_EXPOVFL	85
23061f28255Scgd     "Expansion buffer overflow",
23161f28255Scgd #define ERR_VARSYN	86
23261f28255Scgd     "Variable syntax",
23361f28255Scgd #define ERR_BADBANG	87
23461f28255Scgd     "Bad ! form",
23561f28255Scgd #define ERR_NOSUBST	88
23661f28255Scgd     "No previous substitute",
23761f28255Scgd #define ERR_BADSUBST	89
23861f28255Scgd     "Bad substitute",
23961f28255Scgd #define ERR_LHS		90
24061f28255Scgd     "No previous left hand side",
24161f28255Scgd #define ERR_RHSLONG	91
24261f28255Scgd     "Right hand side too long",
24361f28255Scgd #define ERR_BADBANGMOD	92
24461f28255Scgd     "Bad ! modifier: %c",
24561f28255Scgd #define ERR_MODFAIL	93
24661f28255Scgd     "Modifier failed",
24761f28255Scgd #define ERR_SUBOVFL	94
24861f28255Scgd     "Substitution buffer overflow",
24961f28255Scgd #define ERR_BADBANGARG	95
25061f28255Scgd     "Bad ! arg selector",
25161f28255Scgd #define ERR_NOSEARCH	96
25261f28255Scgd     "No prev search",
25361f28255Scgd #define ERR_NOEVENT	97
25461f28255Scgd     "%s: Event not found",
25561f28255Scgd #define ERR_TOOMANYRP	98
25661f28255Scgd     "Too many )'s",
25761f28255Scgd #define ERR_TOOMANYLP	99
25861f28255Scgd     "Too many ('s",
25961f28255Scgd #define ERR_BADPLP	100
26061f28255Scgd     "Badly placed (",
26161f28255Scgd #define ERR_MISRED	101
26261f28255Scgd     "Missing name for redirect",
26361f28255Scgd #define ERR_OUTRED	102
26461f28255Scgd     "Ambiguous output redirect",
26561f28255Scgd #define ERR_REDPAR	103
26661f28255Scgd     "Can't << within ()'s",
26761f28255Scgd #define ERR_INRED	104
26861f28255Scgd     "Ambiguous input redirect",
26961f28255Scgd #define ERR_ALIASLOOP	105
27061f28255Scgd     "Alias loop",
27161f28255Scgd #define ERR_HISTLOOP	106
27261f28255Scgd     "!# History loop",
27361f28255Scgd #define ERR_ARCH        107
27461f28255Scgd     "%s: %s. Wrong Architecture",
27561f28255Scgd #define ERR_FILEINQ	108
27661f28255Scgd     "Malformed file inquiry",
27761f28255Scgd #define ERR_SELOVFL	109
27861f28255Scgd     "Selector overflow",
27961f28255Scgd #define ERR_INVALID	110
28061f28255Scgd     "Invalid Error"
28161f28255Scgd };
28261f28255Scgd 
28361f28255Scgd /*
28461f28255Scgd  * The parser and scanner set up errors for later by calling seterr,
28561f28255Scgd  * which sets the variable err as a side effect; later to be tested,
28661f28255Scgd  * e.g. in process.
28761f28255Scgd  */
28861f28255Scgd void
seterror(int id,...)28961f28255Scgd seterror(int id, ...)
29061f28255Scgd {
29161f28255Scgd     if (seterr == 0) {
2920bf6fd0cSchristos 	char berr[BUFSIZE];
29361f28255Scgd 	va_list va;
29461f28255Scgd 
29561f28255Scgd 	va_start(va, id);
2969050ab5cSlukem 	if (id < 0 || id >= (int)(sizeof(errorlist) / sizeof(errorlist[0])) - 1)
29761f28255Scgd 	    id = ERR_INVALID;
2985924694dSmycroft 	(void)vsnprintf(berr, sizeof(berr), errorlist[id], va);
29961f28255Scgd 	va_end(va);
30061f28255Scgd 
30161f28255Scgd 	seterr = strsave(berr);
30261f28255Scgd     }
30361f28255Scgd }
30461f28255Scgd 
30561f28255Scgd /*
30661f28255Scgd  * Print the error with the given id.
30761f28255Scgd  *
30861f28255Scgd  * Special ids:
30961f28255Scgd  *	ERR_SILENT: Print nothing.
31061f28255Scgd  *	ERR_OLD: Print the previously set error if one was there.
31161f28255Scgd  *	         otherwise return.
31261f28255Scgd  *	ERR_NAME: If this bit is set, print the name of the function
31361f28255Scgd  *		  in bname
31461f28255Scgd  *
31561f28255Scgd  * This routine always resets or exits.  The flag haderr
316*2fa7e141Sandvar  * is set so the routine who catches the unwind can propagate
31761f28255Scgd  * it if they want.
31861f28255Scgd  *
31961f28255Scgd  * Note that any open files at the point of error will eventually
32061f28255Scgd  * be closed in the routine process in sh.c which is the only
32161f28255Scgd  * place error unwinds are ever caught.
32261f28255Scgd  */
32361f28255Scgd void
stderror(int id,...)32461f28255Scgd stderror(int id, ...)
32561f28255Scgd {
32661f28255Scgd     va_list va;
32776adbe2bStls     Char **v;
328b771e65bSwiz     int flags;
32961f28255Scgd 
330b771e65bSwiz     flags = id & ERR_FLAGS;
33161f28255Scgd     id &= ~ERR_FLAGS;
33261f28255Scgd 
33361f28255Scgd     if ((flags & ERR_OLD) && seterr == NULL)
334cdbd74daSmycroft 	abort();
33561f28255Scgd 
3369050ab5cSlukem     if (id < 0 || id > (int)(sizeof(errorlist) / sizeof(errorlist[0])))
33761f28255Scgd 	id = ERR_INVALID;
33861f28255Scgd 
339cee2bad8Smycroft     (void)fflush(cshout);
340cee2bad8Smycroft     (void)fflush(csherr);
34161f28255Scgd     haderr = 1;			/* Now to diagnostic output */
34261f28255Scgd     timflg = 0;			/* This isn't otherwise reset */
34361f28255Scgd 
34461f28255Scgd 
34561f28255Scgd     if (!(flags & ERR_SILENT)) {
34661f28255Scgd 	if (flags & ERR_NAME)
347cee2bad8Smycroft 	    (void)fprintf(csherr, "%s: ", bname);
34861f28255Scgd 	if ((flags & ERR_OLD))
34961f28255Scgd 	    /* Old error. */
350cee2bad8Smycroft 	    (void)fprintf(csherr, "%s.\n", seterr);
35161f28255Scgd 	else {
35261f28255Scgd 	    va_start(va, id);
353cee2bad8Smycroft 	    (void)vfprintf(csherr, errorlist[id], va);
35461f28255Scgd 	    va_end(va);
355cee2bad8Smycroft 	    (void)fprintf(csherr, ".\n");
35661f28255Scgd 	}
35761f28255Scgd     }
35861f28255Scgd 
35961f28255Scgd     if (seterr) {
3601767ce60Schristos 	free(seterr);
36161f28255Scgd 	seterr = NULL;
36261f28255Scgd     }
36361f28255Scgd 
364cee2bad8Smycroft     if ((v = pargv) != NULL)
36561f28255Scgd 	pargv = 0, blkfree(v);
366cee2bad8Smycroft     if ((v = gargv) != NULL)
36761f28255Scgd 	gargv = 0, blkfree(v);
36861f28255Scgd 
369cee2bad8Smycroft     (void)fflush(cshout);
370cee2bad8Smycroft     (void)fflush(csherr);
37161f28255Scgd     didfds = 0;			/* Forget about 0,1,2 */
37261f28255Scgd     /*
37361f28255Scgd      * Go away if -e or we are a child shell
37461f28255Scgd      */
375ee9e50eaSmycroft     if (exiterr || child)
37661f28255Scgd 	xexit(1);
37761f28255Scgd 
37861f28255Scgd     /*
37961f28255Scgd      * Reset the state of the input. This buffered seek to end of file will
38061f28255Scgd      * also clear the while/foreach stack.
38161f28255Scgd      */
38261f28255Scgd     btoeof();
38361f28255Scgd 
38461f28255Scgd     set(STRstatus, Strsave(STR1));
38561f28255Scgd     if (tpgrp > 0)
38661f28255Scgd 	(void)tcsetpgrp(FSHTTY, tpgrp);
38761f28255Scgd     reset();			/* Unwind */
38861f28255Scgd }
389