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