sh.err.c revision 231990
1231990Smp/* $Header: /p/tcsh/cvsroot/tcsh/sh.err.c,v 3.55 2011/02/25 23:58:34 christos Exp $ */
259243Sobrien/*
359243Sobrien * sh.err.c: Error printing routines.
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#define _h_sh_err		/* Don't redefine the errors	 */
3459243Sobrien#include "sh.h"
35167465Smp#include <assert.h>
3659243Sobrien
37231990SmpRCSID("$tcsh: sh.err.c,v 3.55 2011/02/25 23:58:34 christos Exp $")
3859243Sobrien
3959243Sobrien/*
4059243Sobrien * C Shell
4159243Sobrien */
4259243Sobrien
4359243Sobrien#ifdef lint
4459243Sobrien#undef va_arg
4559243Sobrien#define va_arg(a, b) (a ? (b) 0 : (b) 0)
4659243Sobrien#endif
4759243Sobrien
4859243Sobrienchar   *seterr = NULL;	/* Holds last error if there was one */
4959243Sobrien
5059243Sobrien#define ERR_FLAGS	0xf0000000
5159243Sobrien#define ERR_NAME	0x10000000
5259243Sobrien#define ERR_SILENT	0x20000000
5359243Sobrien#define ERR_OLD		0x40000000
54231990Smp#define ERR_INTERRUPT	0x80000000
5559243Sobrien
5659243Sobrien#define ERR_SYNTAX	0
5759243Sobrien#define ERR_NOTALLOWED	1
5859243Sobrien#define ERR_WTOOLONG	2
5959243Sobrien#define ERR_LTOOLONG	3
6059243Sobrien#define ERR_DOLZERO	4
6159243Sobrien#define ERR_INCBR	5
6259243Sobrien#define ERR_EXPORD	6
6359243Sobrien#define ERR_BADMOD	7
6459243Sobrien#define ERR_SUBSCRIPT	8
6559243Sobrien#define ERR_BADNUM	9
6659243Sobrien#define ERR_NOMORE	10
6759243Sobrien#define ERR_FILENAME	11
6859243Sobrien#define ERR_GLOB	12
6959243Sobrien#define ERR_COMMAND	13
7059243Sobrien#define ERR_TOOFEW	14
7159243Sobrien#define ERR_TOOMANY	15
7259243Sobrien#define ERR_DANGER	16
7359243Sobrien#define ERR_EMPTYIF	17
7459243Sobrien#define ERR_IMPRTHEN	18
7559243Sobrien#define ERR_NOPAREN	19
7659243Sobrien#define ERR_NOTFOUND	20
7759243Sobrien#define ERR_MASK	21
7859243Sobrien#define ERR_LIMIT	22
7959243Sobrien#define ERR_TOOLARGE	23
8059243Sobrien#define ERR_SCALEF	24
8159243Sobrien#define ERR_UNDVAR	25
8259243Sobrien#define ERR_DEEP	26
8359243Sobrien#define ERR_BADSIG	27
8459243Sobrien#define ERR_UNKSIG	28
8559243Sobrien#define ERR_VARBEGIN	29
8659243Sobrien#define ERR_VARTOOLONG	30
8759243Sobrien#define ERR_VARALNUM	31
8859243Sobrien#define ERR_JOBCONTROL	32
8959243Sobrien#define ERR_EXPRESSION	33
9059243Sobrien#define ERR_NOHOMEDIR	34
9159243Sobrien#define ERR_CANTCHANGE	35
9259243Sobrien#define ERR_NULLCOM	36
9359243Sobrien#define ERR_ASSIGN	37
9459243Sobrien#define ERR_UNKNOWNOP	38
9559243Sobrien#define ERR_AMBIG	39
9659243Sobrien#define ERR_EXISTS	40
9759243Sobrien#define ERR_ARGC	41
9859243Sobrien#define ERR_INTR	42
9959243Sobrien#define ERR_RANGE	43
10059243Sobrien#define ERR_OVERFLOW	44
10159243Sobrien#define ERR_NOSUCHJOB	45
10259243Sobrien#define ERR_TERMINAL	46
10359243Sobrien#define ERR_NOTWHILE	47
10459243Sobrien#define ERR_NOPROC	48
10559243Sobrien#define ERR_NOMATCH	49
10659243Sobrien#define ERR_MISSING	50
10759243Sobrien#define ERR_UNMATCHED	51
10859243Sobrien#define ERR_NOMEM	52
10959243Sobrien#define ERR_PIPE	53
11059243Sobrien#define ERR_SYSTEM	54
11159243Sobrien#define ERR_STRING	55
11259243Sobrien#define ERR_JOBS	56
11359243Sobrien#define ERR_JOBARGS	57
11459243Sobrien#define ERR_JOBCUR	58
11559243Sobrien#define ERR_JOBPREV	59
11659243Sobrien#define ERR_JOBPAT	60
11759243Sobrien#define ERR_NESTING	61
11859243Sobrien#define ERR_JOBCTRLSUB	62
11959243Sobrien#define ERR_SYNC	63
12059243Sobrien#define ERR_STOPPED	64
12159243Sobrien#define ERR_NODIR	65
12259243Sobrien#define ERR_EMPTY	66
12359243Sobrien#define ERR_BADDIR	67
12459243Sobrien#define ERR_DIRUS	68
12559243Sobrien#define ERR_HFLAG	69
12659243Sobrien#define ERR_NOTLOGIN	70
12759243Sobrien#define ERR_DIV0	71
12859243Sobrien#define ERR_MOD0	72
12959243Sobrien#define ERR_BADSCALE	73
13059243Sobrien#define ERR_SUSPLOG	74
13159243Sobrien#define ERR_UNKUSER	75
13259243Sobrien#define ERR_NOHOME	76
13359243Sobrien#define ERR_HISTUS	77
13459243Sobrien#define ERR_SPDOLLT	78
13559243Sobrien#define ERR_NEWLINE	79
13659243Sobrien#define ERR_SPSTAR	80
13759243Sobrien#define ERR_DIGIT	81
13859243Sobrien#define ERR_VARILL	82
13959243Sobrien#define ERR_NLINDEX	83
14059243Sobrien#define ERR_EXPOVFL	84
14159243Sobrien#define ERR_VARSYN	85
14259243Sobrien#define ERR_BADBANG	86
14359243Sobrien#define ERR_NOSUBST	87
14459243Sobrien#define ERR_BADSUBST	88
14559243Sobrien#define ERR_LHS		89
14659243Sobrien#define ERR_RHSLONG	90
14759243Sobrien#define ERR_BADBANGMOD	91
14859243Sobrien#define ERR_MODFAIL	92
14959243Sobrien#define ERR_SUBOVFL	93
15059243Sobrien#define ERR_BADBANGARG	94
15159243Sobrien#define ERR_NOSEARCH	95
15259243Sobrien#define ERR_NOEVENT	96
15359243Sobrien#define ERR_TOOMANYRP	97
15459243Sobrien#define ERR_TOOMANYLP	98
15559243Sobrien#define ERR_BADPLP	99
15659243Sobrien#define ERR_MISRED	100
15759243Sobrien#define ERR_OUTRED	101
15859243Sobrien#define ERR_REDPAR	102
15959243Sobrien#define ERR_INRED	103
16059243Sobrien#define ERR_BADPLPS	104
16159243Sobrien#define ERR_ALIASLOOP	105
16259243Sobrien#define ERR_NOWATCH	106
16359243Sobrien#define ERR_NOSCHED	107
16459243Sobrien#define ERR_SCHEDUSAGE	108
16559243Sobrien#define ERR_SCHEDEV	109
16659243Sobrien#define ERR_SCHEDCOM	110
16759243Sobrien#define ERR_SCHEDTIME	111
16859243Sobrien#define ERR_SCHEDREL	112
16959243Sobrien#define ERR_TCNOSTR	113
17059243Sobrien#define ERR_SETTCUS	114
17159243Sobrien#define ERR_TCCAP	115
17259243Sobrien#define ERR_TCPARM	116
17359243Sobrien#define ERR_TCARGS	117
17459243Sobrien#define ERR_TCNARGS	118
17559243Sobrien#define ERR_TCUSAGE	119
17659243Sobrien#define ERR_ARCH	120
17759243Sobrien#define ERR_HISTLOOP	121
17859243Sobrien#define ERR_FILEINQ	122
17959243Sobrien#define ERR_SELOVFL	123
18059243Sobrien#define ERR_TCSHUSAGE   124
18159243Sobrien#define ERR_COMPCOM	125
18259243Sobrien#define ERR_COMPINV	126
18359243Sobrien#define ERR_COMPMIS	127
18459243Sobrien#define ERR_COMPINC	128
18559243Sobrien#define ERR_MFLAG	129
18659243Sobrien#define ERR_ULIMUS	130
18759243Sobrien#define ERR_READONLY	131
18859243Sobrien#define ERR_BADJOB	132
18959243Sobrien#define ERR_INVALID	133
19059243Sobrien#define ERR_BADCOLORVAR	134
191231990Smp#define ERR_EOF		135
192231990Smp#define NO_ERRORS	136
19359243Sobrien
194145479Smpstatic const char *elst[NO_ERRORS] INIT_ZERO_STRUCT;
19559243Sobrien
19659243Sobrien/*
19759243Sobrien * Init the elst depending on the locale
19859243Sobrien */
19959243Sobrienvoid
200167465Smperrinit(void)
20159243Sobrien{
20259243Sobrien#ifdef NLS_CATALOGS
203167465Smp    size_t i;
20459243Sobrien
20559243Sobrien    for (i = 0; i < NO_ERRORS; i++)
206167465Smp	xfree((char *)(intptr_t)elst[i]);
207195609Smp#  if defined(__FreeBSD__) || defined(hpux) || defined(__MidnightBSD__)
20859243Sobrien#  define NLS_MAXSET 30
20959243Sobrien    for (i = 1; i <= NLS_MAXSET; i++)
21059243Sobrien	CGETS(i, 1, "" );
21159243Sobrien#  endif
21259243Sobrien#endif
21359243Sobrien
21459243Sobrien    elst[ERR_SYNTAX] = CSAVS(1, 1, "Syntax Error");
21559243Sobrien    elst[ERR_NOTALLOWED] = CSAVS(1, 2, "%s is not allowed");
21659243Sobrien    elst[ERR_WTOOLONG] = CSAVS(1, 3, "Word too long");
21759243Sobrien    elst[ERR_LTOOLONG] = CSAVS(1, 4, "$< line too long");
21859243Sobrien    elst[ERR_DOLZERO] = CSAVS(1, 5, "No file for $0");
21959243Sobrien    elst[ERR_INCBR] = CSAVS(1, 6, "Incomplete [] modifier");
22059243Sobrien    elst[ERR_EXPORD] = CSAVS(1, 7, "$ expansion must end before ]");
22159243Sobrien    elst[ERR_BADMOD] = CSAVS(1, 8, "Bad : modifier in $ (%c)");
22259243Sobrien    elst[ERR_SUBSCRIPT] = CSAVS(1, 9, "Subscript error");
22359243Sobrien    elst[ERR_BADNUM] = CSAVS(1, 10, "Badly formed number");
22459243Sobrien    elst[ERR_NOMORE] = CSAVS(1, 11, "No more words");
22559243Sobrien    elst[ERR_FILENAME] = CSAVS(1, 12, "Missing file name");
22659243Sobrien    elst[ERR_GLOB] = CSAVS(1, 13, "Internal glob error");
22759243Sobrien    elst[ERR_COMMAND] = CSAVS(1, 14, "Command not found");
22859243Sobrien    elst[ERR_TOOFEW] = CSAVS(1, 15, "Too few arguments");
22959243Sobrien    elst[ERR_TOOMANY] = CSAVS(1, 16, "Too many arguments");
23059243Sobrien    elst[ERR_DANGER] = CSAVS(1, 17, "Too dangerous to alias that");
23159243Sobrien    elst[ERR_EMPTYIF] = CSAVS(1, 18, "Empty if");
23259243Sobrien    elst[ERR_IMPRTHEN] = CSAVS(1, 19, "Improper then");
23359243Sobrien    elst[ERR_NOPAREN] = CSAVS(1, 20, "Words not parenthesized");
23459243Sobrien    elst[ERR_NOTFOUND] = CSAVS(1, 21, "%s not found");
23559243Sobrien    elst[ERR_MASK] = CSAVS(1, 22, "Improper mask");
23659243Sobrien    elst[ERR_LIMIT] = CSAVS(1, 23, "No such limit");
23759243Sobrien    elst[ERR_TOOLARGE] = CSAVS(1, 24, "Argument too large");
23859243Sobrien    elst[ERR_SCALEF] = CSAVS(1, 25, "Improper or unknown scale factor");
23959243Sobrien    elst[ERR_UNDVAR] = CSAVS(1, 26, "Undefined variable");
24059243Sobrien    elst[ERR_DEEP] = CSAVS(1, 27, "Directory stack not that deep");
24159243Sobrien    elst[ERR_BADSIG] = CSAVS(1, 28, "Bad signal number");
24259243Sobrien    elst[ERR_UNKSIG] = CSAVS(1, 29, "Unknown signal; kill -l lists signals");
24359243Sobrien    elst[ERR_VARBEGIN] = CSAVS(1, 30, "Variable name must begin with a letter");
24459243Sobrien    elst[ERR_VARTOOLONG] = CSAVS(1, 31, "Variable name too long");
24559243Sobrien    elst[ERR_VARALNUM] = CSAVS(1, 32,
24659243Sobrien	"Variable name must contain alphanumeric characters");
24759243Sobrien    elst[ERR_JOBCONTROL] = CSAVS(1, 33, "No job control in this shell");
24859243Sobrien    elst[ERR_EXPRESSION] = CSAVS(1, 34, "Expression Syntax");
24959243Sobrien    elst[ERR_NOHOMEDIR] = CSAVS(1, 35, "No home directory");
25059243Sobrien    elst[ERR_CANTCHANGE] = CSAVS(1, 36, "Can't change to home directory");
25159243Sobrien    elst[ERR_NULLCOM] = CSAVS(1, 37, "Invalid null command");
25259243Sobrien    elst[ERR_ASSIGN] = CSAVS(1, 38, "Assignment missing expression");
25359243Sobrien    elst[ERR_UNKNOWNOP] = CSAVS(1, 39, "Unknown operator");
25459243Sobrien    elst[ERR_AMBIG] = CSAVS(1, 40, "Ambiguous");
25559243Sobrien    elst[ERR_EXISTS] = CSAVS(1, 41, "%s: File exists");
25659243Sobrien    elst[ERR_ARGC] = CSAVS(1, 42, "Argument for -c ends in backslash");
25759243Sobrien    elst[ERR_INTR] = CSAVS(1, 43, "Interrupted");
25859243Sobrien    elst[ERR_RANGE] = CSAVS(1, 44, "Subscript out of range");
25959243Sobrien    elst[ERR_OVERFLOW] = CSAVS(1, 45, "Line overflow");
26059243Sobrien    elst[ERR_NOSUCHJOB] = CSAVS(1, 46, "No such job");
26159243Sobrien    elst[ERR_TERMINAL] = CSAVS(1, 47, "Can't from terminal");
26259243Sobrien    elst[ERR_NOTWHILE] = CSAVS(1, 48, "Not in while/foreach");
26359243Sobrien    elst[ERR_NOPROC] = CSAVS(1, 49, "No more processes");
26459243Sobrien    elst[ERR_NOMATCH] = CSAVS(1, 50, "No match");
26559243Sobrien    elst[ERR_MISSING] = CSAVS(1, 51, "Missing %c");
26659243Sobrien    elst[ERR_UNMATCHED] = CSAVS(1, 52, "Unmatched %c");
26759243Sobrien    elst[ERR_NOMEM] = CSAVS(1, 53, "Out of memory");
26859243Sobrien    elst[ERR_PIPE] = CSAVS(1, 54, "Can't make pipe");
26959243Sobrien    elst[ERR_SYSTEM] = CSAVS(1, 55, "%s: %s");
27059243Sobrien    elst[ERR_STRING] = CSAVS(1, 56, "%s");
27159243Sobrien    elst[ERR_JOBS] = CSAVS(1, 57, "Usage: jobs [ -l ]");
27259243Sobrien    elst[ERR_JOBARGS] = CSAVS(1, 58, "Arguments should be jobs or process id's");
27359243Sobrien    elst[ERR_JOBCUR] = CSAVS(1, 59, "No current job");
27459243Sobrien    elst[ERR_JOBPREV] = CSAVS(1, 60, "No previous job");
27559243Sobrien    elst[ERR_JOBPAT] = CSAVS(1, 61, "No job matches pattern");
27659243Sobrien    elst[ERR_NESTING] = CSAVS(1, 62, "Fork nesting > %d; maybe `...` loop");
27759243Sobrien    elst[ERR_JOBCTRLSUB] = CSAVS(1, 63, "No job control in subshells");
27859243Sobrien    elst[ERR_SYNC] = CSAVS(1, 64, "Sync fault: Process %d not found");
27959243Sobrien    elst[ERR_STOPPED] =
28059243Sobrien#ifdef SUSPENDED
28159243Sobrien	CSAVS(1, 65, "%sThere are suspended jobs");
28259243Sobrien#else
28359243Sobrien	CSAVS(1, 66, "%sThere are stopped jobs");
28459243Sobrien#endif /* SUSPENDED */
28559243Sobrien    elst[ERR_NODIR] = CSAVS(1, 67, "No other directory");
28659243Sobrien    elst[ERR_EMPTY] = CSAVS(1, 68, "Directory stack empty");
28759243Sobrien    elst[ERR_BADDIR] = CSAVS(1, 69, "Bad directory");
28859243Sobrien    elst[ERR_DIRUS] = CSAVS(1, 70, "Usage: %s [-%s]%s");
28959243Sobrien    elst[ERR_HFLAG] = CSAVS(1, 71, "No operand for -h flag");
29059243Sobrien    elst[ERR_NOTLOGIN] = CSAVS(1, 72, "Not a login shell");
29159243Sobrien    elst[ERR_DIV0] = CSAVS(1, 73, "Division by 0");
29259243Sobrien    elst[ERR_MOD0] = CSAVS(1, 74, "Mod by 0");
29359243Sobrien    elst[ERR_BADSCALE] = CSAVS(1, 75, "Bad scaling; did you mean \"%s\"?");
29459243Sobrien    elst[ERR_SUSPLOG] = CSAVS(1, 76, "Can't suspend a login shell (yet)");
29559243Sobrien    elst[ERR_UNKUSER] = CSAVS(1, 77, "Unknown user: %s");
29659243Sobrien    elst[ERR_NOHOME] = CSAVS(1, 78, "No $home variable set");
29759243Sobrien    elst[ERR_HISTUS] = CSAVS(1, 79,
29859243Sobrien	"Usage: history [-%s] [# number of events]");
29959243Sobrien    elst[ERR_SPDOLLT] = CSAVS(1, 80, "$, ! or < not allowed with $# or $?");
30059243Sobrien    elst[ERR_NEWLINE] = CSAVS(1, 81, "Newline in variable name");
30159243Sobrien    elst[ERR_SPSTAR] = CSAVS(1, 82, "* not allowed with $# or $?");
30259243Sobrien    elst[ERR_DIGIT] = CSAVS(1, 83, "$?<digit> or $#<digit> not allowed");
30359243Sobrien    elst[ERR_VARILL] = CSAVS(1, 84, "Illegal variable name");
30459243Sobrien    elst[ERR_NLINDEX] = CSAVS(1, 85, "Newline in variable index");
30559243Sobrien    elst[ERR_EXPOVFL] = CSAVS(1, 86, "Expansion buffer overflow");
30659243Sobrien    elst[ERR_VARSYN] = CSAVS(1, 87, "Variable syntax");
30759243Sobrien    elst[ERR_BADBANG] = CSAVS(1, 88, "Bad ! form");
30859243Sobrien    elst[ERR_NOSUBST] = CSAVS(1, 89, "No previous substitute");
30959243Sobrien    elst[ERR_BADSUBST] = CSAVS(1, 90, "Bad substitute");
31059243Sobrien    elst[ERR_LHS] = CSAVS(1, 91, "No previous left hand side");
31159243Sobrien    elst[ERR_RHSLONG] = CSAVS(1, 92, "Right hand side too long");
31259243Sobrien    elst[ERR_BADBANGMOD] = CSAVS(1, 93, "Bad ! modifier: %c");
31359243Sobrien    elst[ERR_MODFAIL] = CSAVS(1, 94, "Modifier failed");
31459243Sobrien    elst[ERR_SUBOVFL] = CSAVS(1, 95, "Substitution buffer overflow");
31559243Sobrien    elst[ERR_BADBANGARG] = CSAVS(1, 96, "Bad ! arg selector");
31659243Sobrien    elst[ERR_NOSEARCH] = CSAVS(1, 97, "No prev search");
31759243Sobrien    elst[ERR_NOEVENT] = CSAVS(1, 98, "%s: Event not found");
31859243Sobrien    elst[ERR_TOOMANYRP] = CSAVS(1, 99, "Too many )'s");
31959243Sobrien    elst[ERR_TOOMANYLP] = CSAVS(1, 100, "Too many ('s");
32059243Sobrien    elst[ERR_BADPLP] = CSAVS(1, 101, "Badly placed (");
32159243Sobrien    elst[ERR_MISRED] = CSAVS(1, 102, "Missing name for redirect");
32259243Sobrien    elst[ERR_OUTRED] = CSAVS(1, 103, "Ambiguous output redirect");
32359243Sobrien    elst[ERR_REDPAR] = CSAVS(1, 104, "Can't << within ()'s");
32459243Sobrien    elst[ERR_INRED] = CSAVS(1, 105, "Ambiguous input redirect");
32559243Sobrien    elst[ERR_BADPLPS] = CSAVS(1, 106, "Badly placed ()'s");
32659243Sobrien    elst[ERR_ALIASLOOP] = CSAVS(1, 107, "Alias loop");
32759243Sobrien    elst[ERR_NOWATCH] = CSAVS(1, 108, "No $watch variable set");
32859243Sobrien    elst[ERR_NOSCHED] = CSAVS(1, 109, "No scheduled events");
32959243Sobrien    elst[ERR_SCHEDUSAGE] = CSAVS(1, 110,
33059243Sobrien	"Usage: sched -<item#>.\nUsage: sched [+]hh:mm <command>");
33159243Sobrien    elst[ERR_SCHEDEV] = CSAVS(1, 111, "Not that many scheduled events");
33259243Sobrien    elst[ERR_SCHEDCOM] = CSAVS(1, 112, "No command to run");
33359243Sobrien    elst[ERR_SCHEDTIME] = CSAVS(1, 113, "Invalid time for event");
33459243Sobrien    elst[ERR_SCHEDREL] = CSAVS(1, 114, "Relative time inconsistent with am/pm");
33559243Sobrien    elst[ERR_TCNOSTR] = CSAVS(1, 115, "Out of termcap string space");
33659243Sobrien    elst[ERR_SETTCUS] = CSAVS(1, 116, "Usage: settc %s [yes|no]");
33759243Sobrien    elst[ERR_TCCAP] = CSAVS(1, 117, "Unknown capability `%s'");
33859243Sobrien    elst[ERR_TCPARM] = CSAVS(1, 118, "Unknown termcap parameter `%%%c'");
33959243Sobrien    elst[ERR_TCARGS] = CSAVS(1, 119, "Too many arguments for `%s' (%d)");
34059243Sobrien    elst[ERR_TCNARGS] = CSAVS(1, 120, "`%s' requires %d arguments");
34159243Sobrien    elst[ERR_TCUSAGE] = CSAVS(1, 121,
34259243Sobrien	"Usage: echotc [-v|-s] [<capability> [<args>]]");
34359243Sobrien    elst[ERR_ARCH] = CSAVS(1, 122, "%s: %s. Binary file not executable");
34459243Sobrien    elst[ERR_HISTLOOP] = CSAVS(1, 123, "!# History loop");
34559243Sobrien    elst[ERR_FILEINQ] = CSAVS(1, 124, "Malformed file inquiry");
34659243Sobrien    elst[ERR_SELOVFL] = CSAVS(1, 125, "Selector overflow");
34759243Sobrien#ifdef apollo
34859243Sobrien    elst[ERR_TCSHUSAGE] = CSAVS(1, 126,
34959243Sobrien"Unknown option: `-%s'\nUsage: %s [ -bcdefilmnqstvVxX -Dname[=value] ] [ argument ... ]");
35059243Sobrien#else /* !apollo */
35159243Sobrien# ifdef convex
35259243Sobrien    elst[ERR_TCSHUSAGE] = CSAVS(1, 127,
35359243Sobrien"Unknown option: `-%s'\nUsage: %s [ -bcdefFilmnqstvVxX ] [ argument ... ]");
35459243Sobrien# else /* rest */
35559243Sobrien    elst[ERR_TCSHUSAGE] = CSAVS(1, 128,
35659243Sobrien"Unknown option: `-%s'\nUsage: %s [ -bcdefilmnqstvVxX ] [ argument ... ]");
35759243Sobrien# endif /* convex */
35859243Sobrien#endif /* apollo */
35959243Sobrien    elst[ERR_COMPCOM] = CSAVS(1, 129, "\nInvalid completion: \"%s\"");
36059243Sobrien    elst[ERR_COMPINV] = CSAVS(1, 130, "\nInvalid %s: '%c'");
36159243Sobrien    elst[ERR_COMPMIS] = CSAVS(1, 131,
36259243Sobrien	"\nMissing separator '%c' after %s \"%s\"");
36359243Sobrien    elst[ERR_COMPINC] = CSAVS(1, 132, "\nIncomplete %s: \"%s\"");
36459243Sobrien    elst[ERR_MFLAG] = CSAVS(1, 133, "No operand for -m flag");
36559243Sobrien    elst[ERR_ULIMUS] = CSAVS(1, 134, "Usage: unlimit [-fh] [limits]");
36659243Sobrien    elst[ERR_READONLY] = CSAVS(1, 135, "$%S is read-only");
36759243Sobrien    elst[ERR_BADJOB] = CSAVS(1, 136, "No such job (badjob)");
36859243Sobrien    elst[ERR_BADCOLORVAR] = CSAVS(1, 137, "Unknown colorls variable `%c%c'");
369231990Smp    elst[ERR_EOF] = CSAVS(1, 138, "Unexpected end of file");
37059243Sobrien}
371167465Smp
372167465Smp/* Cleanup data. */
373167465Smpstruct cleanup_entry
374167465Smp{
375167465Smp    void *var;
376167465Smp    void (*fn) (void *);
377167465Smp#ifdef CLEANUP_DEBUG
378167465Smp    const char *file;
379167465Smp    size_t line;
380167465Smp#endif
381167465Smp};
382167465Smp
383167465Smpstatic struct cleanup_entry *cleanup_stack; /* = NULL; */
384167465Smpstatic size_t cleanup_sp; /* = 0; Next free entry */
385167465Smpstatic size_t cleanup_mark; /* = 0; Last entry to handle before unwinding */
386167465Smpstatic size_t cleanup_stack_size; /* = 0 */
387167465Smp
388167465Smp/* fn() will be run with all signals blocked, so it should not do anything
389167465Smp   risky. */
390167465Smpvoid
391167465Smpcleanup_push_internal(void *var, void (*fn) (void *)
392167465Smp#ifdef CLEANUP_DEBUG
393167465Smp    , const char *file, size_t line
394167465Smp#endif
395167465Smp)
396167465Smp{
397167465Smp    struct cleanup_entry *ce;
398167465Smp
399167465Smp    if (cleanup_sp == cleanup_stack_size) {
400167465Smp	if (cleanup_stack_size == 0)
401167465Smp	    cleanup_stack_size = 64; /* Arbitrary */
402167465Smp	else
403167465Smp	    cleanup_stack_size *= 2;
404167465Smp	cleanup_stack = xrealloc(cleanup_stack,
405167465Smp				 cleanup_stack_size * sizeof (*cleanup_stack));
406167465Smp    }
407167465Smp    ce = cleanup_stack + cleanup_sp;
408167465Smp    ce->var = var;
409167465Smp    ce->fn = fn;
410167465Smp#ifdef CLEANUP_DEBUG
411167465Smp    ce->file = file;
412167465Smp    ce->line = line;
413167465Smp#endif
414167465Smp    cleanup_sp++;
415167465Smp}
416167465Smp
417167465Smpstatic void
418167465Smpcleanup_ignore_fn(void *dummy)
419167465Smp{
420167465Smp    USE(dummy);
421167465Smp}
422167465Smp
423167465Smpvoid
424167465Smpcleanup_ignore(void *var)
425167465Smp{
426167465Smp    struct cleanup_entry *ce;
427167465Smp
428167465Smp    ce = cleanup_stack + cleanup_sp;
429167465Smp    while (ce != cleanup_stack) {
430167465Smp        ce--;
431167465Smp	if (ce->var == var) {
432167465Smp	    ce->fn = cleanup_ignore_fn;
433167465Smp	    return;
434167465Smp	}
435167465Smp    }
436167465Smp    abort();
437167465Smp}
438167465Smp
439167465Smpvoid
440167465Smpcleanup_until(void *last_var)
441167465Smp{
442167465Smp    while (cleanup_sp != 0) {
443167465Smp	struct cleanup_entry ce;
444167465Smp
445167465Smp	cleanup_sp--;
446167465Smp	ce = cleanup_stack[cleanup_sp];
447167465Smp	ce.fn(ce.var);
448167465Smp	if (ce.var == last_var)
449167465Smp	    return;
450167465Smp    }
451167465Smp    abort();
452167465Smp}
453167465Smp
454231990Smpint
455231990Smpcleanup_reset(void)
456231990Smp{
457231990Smp    return cleanup_sp > cleanup_mark;
458231990Smp}
459231990Smp
460167465Smpvoid
461167465Smpcleanup_until_mark(void)
462167465Smp{
463167465Smp    while (cleanup_sp > cleanup_mark) {
464167465Smp	struct cleanup_entry ce;
465167465Smp
466167465Smp	cleanup_sp--;
467167465Smp	ce = cleanup_stack[cleanup_sp];
468167465Smp	ce.fn(ce.var);
469167465Smp    }
470167465Smp}
471167465Smp
472167465Smpsize_t
473167465Smpcleanup_push_mark(void)
474167465Smp{
475167465Smp    size_t old_mark;
476167465Smp
477167465Smp    old_mark = cleanup_mark;
478167465Smp    cleanup_mark = cleanup_sp;
479167465Smp    return old_mark;
480167465Smp}
481167465Smp
482167465Smpvoid
483167465Smpcleanup_pop_mark(size_t mark)
484167465Smp{
485167465Smp    assert (mark <= cleanup_sp);
486167465Smp    cleanup_mark = mark;
487167465Smp}
488167465Smp
489167465Smpvoid
490167465Smpsigint_cleanup(void *xsa)
491167465Smp{
492167465Smp    const struct sigaction *sa;
493167465Smp
494167465Smp    sa = xsa;
495167465Smp    sigaction(SIGINT, sa, NULL);
496167465Smp}
497167465Smp
498167465Smpvoid
499167465Smpsigprocmask_cleanup(void *xmask)
500167465Smp{
501167465Smp    sigset_t *mask;
502167465Smp
503167465Smp    mask = xmask;
504167465Smp    sigprocmask(SIG_SETMASK, mask, NULL);
505167465Smp}
506167465Smp
507167465Smpvoid
508167465Smpopen_cleanup(void *xptr)
509167465Smp{
510167465Smp    int *ptr;
511167465Smp
512167465Smp    ptr = xptr;
513167465Smp    xclose(*ptr);
514167465Smp}
515167465Smp
516167465Smpvoid
517167465Smpopendir_cleanup(void *xdir)
518167465Smp{
519167465Smp    DIR *dir;
520167465Smp
521167465Smp    dir = xdir;
522167465Smp    xclosedir(dir);
523167465Smp}
524167465Smp
525167465Smpvoid
526167465Smpxfree_indirect(void *xptr)
527167465Smp{
528167465Smp    void **ptr; /* This is actually type punning :( */
529167465Smp
530167465Smp    ptr = xptr;
531167465Smp    xfree(*ptr);
532167465Smp}
533167465Smp
534167465Smpvoid
535167465Smpreset(void)
536167465Smp{
537167465Smp    cleanup_until_mark();
538167465Smp    _reset();
539231990Smp    abort();
540167465Smp}
541167465Smp
54259243Sobrien/*
54359243Sobrien * The parser and scanner set up errors for later by calling seterr,
54459243Sobrien * which sets the variable err as a side effect; later to be tested,
54559243Sobrien * e.g. in process.
54659243Sobrien */
54759243Sobrienvoid
54859243Sobrien/*VARARGS1*/
54959243Sobrienseterror(unsigned int id, ...)
55059243Sobrien{
55159243Sobrien    if (seterr == 0) {
55259243Sobrien	va_list va;
553167465Smp
55459243Sobrien	va_start(va, id);
55559243Sobrien	if (id >= sizeof(elst) / sizeof(elst[0]))
55659243Sobrien	    id = ERR_INVALID;
557167465Smp	seterr = xvasprintf(elst[id], va);
55859243Sobrien	va_end(va);
55959243Sobrien    }
56059243Sobrien}
56159243Sobrien
562231990Smpvoid
563231990Smpfixerror(void)
564231990Smp{
565231990Smp    didfds = 0;			/* Forget about 0,1,2 */
566231990Smp    /*
567231990Smp     * Go away if -e or we are a child shell
568231990Smp     */
569231990Smp    if (!exitset || exiterr || child)
570231990Smp	xexit(1);
571231990Smp
572231990Smp    /*
573231990Smp     * Reset the state of the input. This buffered seek to end of file will
574231990Smp     * also clear the while/foreach stack.
575231990Smp     */
576231990Smp    btoeof();
577231990Smp
578231990Smp    setcopy(STRstatus, STR1, VAR_READWRITE);/*FIXRESET*/
579231990Smp#ifdef BSDJOBS
580231990Smp    if (tpgrp > 0)
581231990Smp	(void) tcsetpgrp(FSHTTY, tpgrp);
582231990Smp#endif
583231990Smp}
584231990Smp
58559243Sobrien/*
58659243Sobrien * Print the error with the given id.
58759243Sobrien *
58859243Sobrien * Special ids:
58959243Sobrien *	ERR_SILENT: Print nothing.
590231990Smp *	ERR_OLD: Print the previously set error
59159243Sobrien *	ERR_NAME: If this bit is set, print the name of the function
59259243Sobrien *		  in bname
59359243Sobrien *
59459243Sobrien * This routine always resets or exits.  The flag haderr
59559243Sobrien * is set so the routine who catches the unwind can propogate
59659243Sobrien * it if they want.
59759243Sobrien *
59859243Sobrien * Note that any open files at the point of error will eventually
59959243Sobrien * be closed in the routine process in sh.c which is the only
60059243Sobrien * place error unwinds are ever caught.
60159243Sobrien */
60259243Sobrienvoid
60359243Sobrien/*VARARGS*/
60459243Sobrienstderror(unsigned int id, ...)
60559243Sobrien{
60659243Sobrien    va_list va;
60759243Sobrien    int flags;
60859243Sobrien
60959243Sobrien    va_start(va, id);
61059243Sobrien
61159243Sobrien    /*
61259243Sobrien     * Reset don't free flag for buggy os's
61359243Sobrien     */
61459243Sobrien    dont_free = 0;
61559243Sobrien
61659243Sobrien    flags = (int) id & ERR_FLAGS;
61759243Sobrien    id &= ~ERR_FLAGS;
61859243Sobrien
61959243Sobrien    /* Pyramid's OS/x has a subtle bug in <varargs.h> which prevents calling
62059243Sobrien     * va_end more than once in the same function. -- sterling@netcom.com
62159243Sobrien     */
622231990Smp    assert(!((flags & ERR_OLD) && seterr == NULL));
62359243Sobrien
624231990Smp    if (id >= sizeof(elst) / sizeof(elst[0]))
625231990Smp	id = ERR_INVALID;
62659243Sobrien
627231990Smp    if (!(flags & ERR_SILENT)) {
628231990Smp	/*
629231990Smp	 * Must flush before we print as we wish output before the error
630231990Smp	 * to go on (some form of) standard output, while output after
631231990Smp	 * goes on (some form of) diagnostic output. If didfds then output
632231990Smp	 * will go to 1/2 else to FSHOUT/FSHDIAG. See flush in sh.print.c.
633231990Smp	 */
634231990Smp	flush();/*FIXRESET*/
635231990Smp	haderr = 1;		/* Now to diagnostic output */
636231990Smp	if (flags & ERR_NAME)
637231990Smp	    xprintf("%s: ", bname);/*FIXRESET*/
638231990Smp	if ((flags & ERR_OLD)) {
639231990Smp	    /* Old error. */
640231990Smp	    xprintf("%s.\n", seterr);/*FIXRESET*/
641231990Smp	} else {
642231990Smp	    xvprintf(elst[id], va);/*FIXRESET*/
643231990Smp	    xprintf(".\n");/*FIXRESET*/
64459243Sobrien	}
64559243Sobrien    }
64659243Sobrien    va_end(va);
64759243Sobrien
64859243Sobrien    if (seterr) {
649167465Smp	xfree(seterr);
65059243Sobrien	seterr = NULL;
65159243Sobrien    }
65259243Sobrien
653231990Smp    fixerror();
65459243Sobrien
655231990Smp    reset();		/* Unwind */
65659243Sobrien}
657