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