home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / tcsh-6.01 / part05 / sh.err.c < prev    next >
C/C++ Source or Header  |  1991-12-19  |  12KB  |  470 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/sh.err.c,v 3.7 1991/12/19 22:34:14 christos Exp $ */
  2. /*
  3.  * sh.err.c: Error printing routines. 
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #define _h_tc_err        /* Don't redefine the errors     */
  38. #include "sh.h"
  39.  
  40. RCSID("$Id: sh.err.c,v 3.7 1991/12/19 22:34:14 christos Exp $")
  41.  
  42. /*
  43.  * C Shell
  44.  */
  45.  
  46. #ifdef lint
  47. #undef va_arg
  48. #define va_arg(a, b) (a ? (b) 0 : (b) 0)
  49. #endif
  50.  
  51. char   *seterr = NULL;    /* Holds last error if there was one */
  52.  
  53. #define ERR_FLAGS    0xf0000000
  54. #define ERR_NAME    0x10000000
  55. #define ERR_SILENT    0x20000000
  56. #define ERR_OLD        0x40000000
  57.  
  58. static char *errorlist[] = 
  59. {
  60. #define ERR_SYNTAX    0
  61.     "Syntax Error",
  62. #define ERR_NOTALLOWED    1
  63.     "%s is not allowed",
  64. #define ERR_WTOOLONG    2
  65.     "Word too long",
  66. #define ERR_LTOOLONG    3
  67.     "$< line too long",
  68. #define ERR_DOLZERO    4
  69.     "No file for $0",
  70. #define ERR_DOLQUEST    5
  71.     "$? not allowed here",
  72. #define ERR_INCBR    6
  73.     "Incomplete [] modifier",
  74. #define ERR_EXPORD    7
  75.     "$ expansion must end before ]",
  76. #define ERR_BADMOD    8
  77.     "Bad : modifier in $ (%c)",
  78. #define ERR_SUBSCRIPT    9
  79.     "Subscript error",
  80. #define ERR_BADNUM    10
  81.     "Badly formed number",
  82. #define ERR_NOMORE    11
  83.     "No more words",
  84. #define ERR_FILENAME    12
  85.     "Missing file name",
  86. #define ERR_GLOB    13
  87.     "Internal glob error",
  88. #define ERR_COMMAND    14
  89.     "Command not found",
  90. #define ERR_TOOFEW    15
  91.     "Too few arguments",
  92. #define ERR_TOOMANY    16
  93.     "Too many arguments",
  94. #define ERR_DANGER    17
  95.     "Too dangerous to alias that",
  96. #define ERR_EMPTYIF    18
  97.     "Empty if",
  98. #define ERR_IMPRTHEN    19
  99.     "Improper then",
  100. #define ERR_NOPAREN    20
  101.     "Words not parenthesized",
  102. #define ERR_NOTFOUND    21
  103.     "%s not found",
  104. #define ERR_MASK    22
  105.     "Improper mask",
  106. #define ERR_LIMIT    23
  107.     "No such limit",
  108. #define ERR_TOOLARGE    24
  109.     "Argument too large",
  110. #define ERR_SCALEF    25
  111.     "Improper or unknown scale factor",
  112. #define ERR_UNDVAR    26
  113.     "Undefined variable",
  114. #define ERR_DEEP    27
  115.     "Directory stack not that deep",
  116. #define ERR_BADSIG    28
  117.     "Bad signal number",
  118. #define ERR_UNKSIG    29
  119.     "Unknown signal; kill -l lists signals",
  120. #define ERR_VARBEGIN    30
  121.     "Variable name must begin with a letter",
  122. #define ERR_VARTOOLONG    31
  123.     "Variable name too long",
  124. #define ERR_VARALNUM    32
  125.     "Variable name must contain alphanumeric characters",
  126. #define ERR_JOBCONTROL    33
  127.     "No job control in this shell",
  128. #define ERR_EXPRESSION    34
  129.     "Expression Syntax",
  130. #define ERR_NOHOMEDIR    35
  131.     "No home directory",
  132. #define ERR_CANTCHANGE    36
  133.     "Can't change to home directory",
  134. #define ERR_NULLCOM    37
  135.     "Invalid null command",
  136. #define ERR_ASSIGN    38
  137.     "Assignment missing expression",
  138. #define ERR_UNKNOWNOP    39
  139.     "Unknown operator",
  140. #define ERR_AMBIG    40
  141.     "Ambiguous",
  142. #define ERR_EXISTS    41
  143.     "%s: File exists",
  144. #define ERR_ARGC    42
  145.     "Argument for -c ends in backslash",
  146. #define ERR_INTR    43
  147.     "Interrupted",
  148. #define ERR_RANGE    44
  149.     "Subscript out of range",
  150. #define ERR_OVERFLOW    45
  151.     "Line overflow",
  152. #define ERR_NOSUCHJOB    46
  153.     "No such job",
  154. #define ERR_TERMINAL    47
  155.     "Can't from terminal",
  156. #define ERR_NOTWHILE    48
  157.     "Not in while/foreach",
  158. #define ERR_NOPROC    49
  159.     "No more processes",
  160. #define ERR_NOMATCH    50
  161.     "No match",
  162. #define ERR_MISSING    51
  163.     "Missing %c",
  164. #define ERR_UNMATCHED    52
  165.     "Unmatched %c",
  166. #define ERR_NOMEM    53
  167.     "Out of memory",
  168. #define ERR_PIPE    54
  169.     "Can't make pipe",
  170. #define ERR_SYSTEM    55
  171.     "%s: %s",
  172. #define ERR_STRING    56
  173.     "%s",
  174. #define ERR_JOBS    57
  175.     "Usage: jobs [ -l ]",
  176. #define ERR_JOBARGS    58
  177.     "Arguments should be jobs or process id's",
  178. #define ERR_JOBCUR    59
  179.     "No current job",
  180. #define ERR_JOBPREV    60
  181.     "No previous job",
  182. #define ERR_JOBPAT    61
  183.     "No job matches pattern",
  184. #define ERR_NESTING    62
  185.     "Fork nesting > %d; maybe `...` loop",
  186. #define ERR_JOBCTRLSUB    63
  187.     "No job control in subshells",
  188. #define ERR_SYNC    64
  189.     "Sunc fault: Process %d not found",
  190. #define ERR_STOPPED    65
  191. #ifdef SUSPENDED
  192.     "%sThere are suspended jobs",
  193. #else
  194.     "%sThere are stopped jobs",
  195. #endif                /* SUSPENDED */
  196. #define ERR_NODIR    66
  197.     "No other directory",
  198. #define ERR_EMPTY    67
  199.     "Directory stack empty",
  200. #define ERR_BADDIR    68
  201.     "Bad directory",
  202. #define ERR_DIRUS    69
  203.     "Usage: %s [-lvn]%s",
  204. #define ERR_HFLAG    70
  205.     "No operand for -h flag",
  206. #define ERR_NOTLOGIN    71
  207.     "Not a login shell",
  208. #define ERR_DIV0    72
  209.     "Division by 0",
  210. #define ERR_MOD0    73
  211.     "Mod by 0",
  212. #define ERR_BADSCALE    74
  213.     "Bad scaling; did you mean \"%s\"?",
  214. #define ERR_SUSPLOG    75
  215.     "Can't suspend a login shell (yet)",
  216. #define ERR_UNKUSER    76
  217.     "Unknown user: %s",
  218. #define ERR_NOHOME    77
  219.     "No $home variable set",
  220. #define ERR_HISTUS    78
  221.     "Usage: history [-rht] [# number of events]",
  222. #define ERR_SPDOLLT    79
  223.     "$, ! or < not allowed with $# or $?",
  224. #define ERR_NEWLINE    80
  225.     "Newline in variable name",
  226. #define ERR_SPSTAR    81
  227.     "* not allowed with $# or $?",
  228. #define ERR_DIGIT    82
  229.     "$?<digit> or $#<digit> not allowed",
  230. #define ERR_VARILL    83
  231.     "Illegal variable name",
  232. #define ERR_NLINDEX    84
  233.     "Newline in variable index",
  234. #define ERR_EXPOVFL    85
  235.     "Expansion buffer overflow",
  236. #define ERR_VARSYN    86
  237.     "Variable syntax",
  238. #define ERR_BADBANG    87
  239.     "Bad ! form",
  240. #define ERR_NOSUBST    88
  241.     "No previous substitute",
  242. #define ERR_BADSUBST    89
  243.     "Bad substitute",
  244. #define ERR_LHS        90
  245.     "No previous left hand side",
  246. #define ERR_RHSLONG    91
  247.     "Right hand side too long",
  248. #define ERR_BADBANGMOD    92
  249.     "Bad ! modifier: %c",
  250. #define ERR_MODFAIL    93
  251.     "Modifier failed",
  252. #define ERR_SUBOVFL    94
  253.     "Substitution buffer overflow",
  254. #define ERR_BADBANGARG    95
  255.     "Bad ! arg selector",
  256. #define ERR_NOSEARCH    96
  257.     "No prev search",
  258. #define ERR_NOEVENT    97
  259.     "%s: Event not found",
  260. #define ERR_TOOMANYRP    98
  261.     "Too many )'s",
  262. #define ERR_TOOMANYLP    99
  263.     "Too many ('s",
  264. #define ERR_BADPLP    100
  265.     "Badly placed (",
  266. #define ERR_MISRED    101
  267.     "Missing name for redirect",
  268. #define ERR_OUTRED    102
  269.     "Ambiguous output redirect",
  270. #define ERR_REDPAR    103
  271.     "Can't << within ()'s",
  272. #define ERR_INRED    104
  273.     "Ambiguous input redirect",
  274. #define ERR_BADPLPS    105
  275.     "Badly placed ()'s",
  276. #define ERR_VARMOD    106
  277.     "Unknown variable modifier",
  278. #define ERR_ALIASLOOP    107
  279.     "Alias loop",
  280. #define ERR_NOWATCH    108
  281.     "No $watch variable set",
  282. #define ERR_NOSCHED    109
  283.     "No scheduled events",
  284. #define ERR_SCHEDUSAGE    110
  285.     "Usage: sched -<item#>.\nUsage: sched [+]hh:mm <command>",
  286. #define ERR_SCHEDEV    111
  287.     "Not that many scheduled events",
  288. #define ERR_SCHEDCOM    112
  289.     "No command to run",
  290. #define ERR_SCHEDTIME    113
  291.     "Invalid time for event",
  292. #define ERR_SCHEDREL    114
  293.     "Relative time inconsistent with am/pm",
  294. #define ERR_TCNOSTR    115
  295.     "Out of termcap string space",
  296. #define ERR_SETTCUS    116
  297.     "Usage: settc %s [yes|no]",
  298. #define ERR_TCCAP    117
  299.     "Unknown capability `%s'",
  300. #define ERR_TCPARM    118
  301.     "Unknown termcap parameter `%%%c'",
  302. #define ERR_TCARGS    119
  303.     "Too many arguments for `%s' (%d)",
  304. #define ERR_TCNARGS    120
  305.     "`%s' requires %d arguments",
  306. #define ERR_TCUSAGE    121
  307.     "Usage: echotc [-v|-s] [<capability> [<args>]]",
  308. #define ERR_ARCH    122
  309.     "%s: %s. Wrong Architecture",
  310. #define ERR_HISTLOOP    123
  311.     "!# History loop",
  312. #define ERR_FILEINQ    124
  313.     "Malformed file inquiry",
  314. #define ERR_SELOVFL    125
  315.     "Selector overflow",
  316. #define ERR_TCSHUSAGE   126
  317. #ifdef apollo
  318.     "Unknown option: -%s\nUsage: tcsh [ -bcdefilmnqstvVxX -Dname[=value] ] [ argument ... ]",
  319. #else /* !apollo */
  320. # if defined(__convex__) || defined(convex)
  321.     "Unknown option: -%s\nUsage: tcsh [ -bcdefFilmnqstvVxX ] [ argument ... ]",
  322. # else /* rest */
  323.     "Unknown option: -%s\nUsage: tcsh [ -bcdefilmnqstvVxX ] [ argument ... ]",
  324. # endif /* __convex__ || convex */
  325. #endif /* apollo */
  326. #define ERR_INVALID    127
  327.     "Invalid Error"
  328. };
  329.  
  330. /*
  331.  * The parser and scanner set up errors for later by calling seterr,
  332.  * which sets the variable err as a side effect; later to be tested,
  333.  * e.g. in process.
  334.  */
  335. void
  336. /*VARARGS1*/
  337. #if __STDC__
  338. seterror(unsigned int id, ...)
  339. #else
  340. seterror(va_alist)
  341.     va_dcl
  342. #endif
  343. {
  344.  
  345.     if (seterr == 0) {
  346.     va_list va;
  347.     char    berr[BUFSIZE];
  348. #if __STDC__
  349.     va_start(va, id);
  350. #else
  351.     unsigned int id;
  352.     va_start(va);
  353.     id = va_arg(va, unsigned int);
  354. #endif
  355.  
  356.     if (id >= sizeof(errorlist) / sizeof(errorlist[0]))
  357.         id = ERR_INVALID;
  358.     xvsprintf(berr, errorlist[id], va);
  359.     va_end(va);
  360.  
  361.     seterr = strsave(berr);
  362.     }
  363. }
  364.  
  365. /*
  366.  * Print the error with the given id.
  367.  *
  368.  * Special ids:
  369.  *    ERR_SILENT: Print nothing.
  370.  *    ERR_OLD: Print the previously set error if one was there.
  371.  *             otherwise return.
  372.  *    ERR_NAME: If this bit is set, print the name of the function
  373.  *          in bname
  374.  *
  375.  * This routine always resets or exits.  The flag haderr
  376.  * is set so the routine who catches the unwind can propogate
  377.  * it if they want.
  378.  *
  379.  * Note that any open files at the point of error will eventually
  380.  * be closed in the routine process in sh.c which is the only
  381.  * place error unwinds are ever caught.
  382.  */
  383. void
  384. /*VARARGS*/
  385. #if __STDC__
  386. stderror(unsigned int id, ...)
  387. #else
  388. stderror(va_alist)
  389.     va_dcl
  390. #endif
  391. {
  392.     va_list va;
  393.     register Char **v;
  394.     int     flags;
  395.  
  396. #if __STDC__
  397.     va_start(va, id);
  398. #else
  399.     unsigned int id;
  400.  
  401.     va_start(va);
  402.     id = va_arg(va, unsigned int);
  403. #endif
  404.  
  405.     flags = id & ERR_FLAGS;
  406.     id &= ~ERR_FLAGS;
  407.  
  408.     if ((flags & ERR_OLD) && seterr == NULL) {
  409.     va_end(va);
  410.     return;
  411.     }
  412.  
  413.     if (id >= sizeof(errorlist) / sizeof(errorlist[0]))
  414.     id = ERR_INVALID;
  415.  
  416.     /*
  417.      * Must flush before we print as we wish output before the error to go on
  418.      * (some form of) standard output, while output after goes on (some form
  419.      * of) diagnostic output. If didfds then output will go to 1/2 else to
  420.      * FSHOUT/FSHDIAG. See flush in sh.print.c.
  421.      */
  422.     flush();
  423.     haderr = 1;            /* Now to diagnostic output */
  424.     timflg = 0;            /* This isn't otherwise reset */
  425.  
  426.  
  427.     if (!(flags & ERR_SILENT)) {
  428.     if (flags & ERR_NAME)
  429.         xprintf("%s: ", bname);
  430.     if ((flags & ERR_OLD))
  431.         /* Old error. */
  432.         xprintf("%s.\n", seterr);
  433.     else {
  434.         xvprintf(errorlist[id], va);
  435.         xprintf(".\n");
  436.     }
  437.     }
  438.     va_end(va);
  439.  
  440.     if (seterr) {
  441.     xfree((ptr_t) seterr);
  442.     seterr = NULL;
  443.     }
  444.  
  445.     if (v = pargv)
  446.     pargv = 0, blkfree(v);
  447.     if (v = gargv)
  448.     gargv = 0, blkfree(v);
  449.  
  450.     didfds = 0;            /* Forget about 0,1,2 */
  451.     /*
  452.      * Go away if -e or we are a child shell
  453.      */
  454.     if (exiterr || child)
  455.     xexit(1);
  456.  
  457.     /*
  458.      * Reset the state of the input. This buffered seek to end of file will
  459.      * also clear the while/foreach stack.
  460.      */
  461.     btoeof();
  462.  
  463.     set(STRstatus, Strsave(STR1));
  464. #ifdef BSDJOBS
  465.     if (tpgrp > 0)
  466.     (void) tcsetpgrp(FSHTTY, tpgrp);
  467. #endif
  468.     reset();            /* Unwind */
  469. }
  470.