121929Sdist /* 221929Sdist * Copyright (c) 1980 Regents of the University of California. 322538Sedward * All rights reserved. The Berkeley Software License Agreement 421929Sdist * specifies the terms and conditions for redistribution. 521929Sdist */ 621929Sdist 717510Sedward #ifndef lint 8*28049Slepreau static char *sccsid = "@(#)err.c 5.3 (Berkeley) 05/13/86"; 922538Sedward #endif 101291Sbill 111291Sbill #include "sh.h" 121291Sbill #include <sys/ioctl.h> 131291Sbill 141291Sbill /* 151291Sbill * C Shell 161291Sbill */ 171291Sbill 181291Sbill bool errspl; /* Argument to error was spliced by seterr2 */ 191291Sbill char one[2] = { '1', 0 }; 201291Sbill char *onev[2] = { one, NOSTR }; 211291Sbill /* 221291Sbill * Print error string s with optional argument arg. 231291Sbill * This routine always resets or exits. The flag haderr 241291Sbill * is set so the routine who catches the unwind can propogate 251291Sbill * it if they want. 261291Sbill * 271291Sbill * Note that any open files at the point of error will eventually 281291Sbill * be closed in the routine process in sh.c which is the only 291291Sbill * place error unwinds are ever caught. 301291Sbill */ 3117510Sedward /*VARARGS1*/ 321291Sbill error(s, arg) 331291Sbill char *s; 341291Sbill { 351291Sbill register char **v; 361291Sbill register char *ep; 371291Sbill 381291Sbill /* 391291Sbill * Must flush before we print as we wish output before the error 401291Sbill * to go on (some form of) standard output, while output after 411291Sbill * goes on (some form of) diagnostic output. 421291Sbill * If didfds then output will go to 1/2 else to FSHOUT/FSHDIAG. 431291Sbill * See flush in sh.print.c. 441291Sbill */ 451291Sbill flush(); 461291Sbill haderr = 1; /* Now to diagnostic output */ 471291Sbill timflg = 0; /* This isn't otherwise reset */ 481291Sbill if (v = pargv) 491291Sbill pargv = 0, blkfree(v); 501291Sbill if (v = gargv) 511291Sbill gargv = 0, blkfree(v); 521291Sbill 531291Sbill /* 541291Sbill * A zero arguments causes no printing, else print 551291Sbill * an error diagnostic here. 561291Sbill */ 571291Sbill if (s) 581291Sbill printf(s, arg), printf(".\n"); 591291Sbill 601291Sbill didfds = 0; /* Forget about 0,1,2 */ 611291Sbill if ((ep = err) && errspl) { 621291Sbill errspl = 0; 631291Sbill xfree(ep); 641291Sbill } 651291Sbill errspl = 0; 661291Sbill 671291Sbill /* 68*28049Slepreau * Go away if -e or we are a child shell 69*28049Slepreau */ 70*28049Slepreau if (exiterr || child) 71*28049Slepreau exit(1); 72*28049Slepreau 73*28049Slepreau /* 741291Sbill * Reset the state of the input. 751291Sbill * This buffered seek to end of file will also 761291Sbill * clear the while/foreach stack. 771291Sbill */ 781291Sbill btoeof(); 791291Sbill 801291Sbill setq("status", onev, &shvhed); 811291Sbill if (tpgrp > 0) 8217510Sedward (void) ioctl(FSHTTY, TIOCSPGRP, (char *)&tpgrp); 831291Sbill reset(); /* Unwind */ 841291Sbill } 851291Sbill 861291Sbill /* 871291Sbill * Perror is the shells version of perror which should otherwise 881291Sbill * never be called. 891291Sbill */ 901291Sbill Perror(s) 911291Sbill char *s; 921291Sbill { 931291Sbill 941291Sbill /* 951291Sbill * Perror uses unit 2, thus if we didn't set up the fd's 961291Sbill * we must set up unit 2 now else the diagnostic will disappear 971291Sbill */ 981291Sbill if (!didfds) { 991291Sbill register int oerrno = errno; 1001291Sbill 10117510Sedward (void) dcopy(SHDIAG, 2); 1021291Sbill errno = oerrno; 1031291Sbill } 1041291Sbill perror(s); 1051291Sbill error(NOSTR); /* To exit or unwind */ 1061291Sbill } 1071291Sbill 1081291Sbill bferr(cp) 1091291Sbill char *cp; 1101291Sbill { 1111291Sbill 1121291Sbill flush(); 1131291Sbill haderr = 1; 1141291Sbill printf("%s: ", bname); 1151291Sbill error(cp); 1161291Sbill } 1171291Sbill 1181291Sbill /* 1191291Sbill * The parser and scanner set up errors for later by calling seterr, 1201291Sbill * which sets the variable err as a side effect; later to be tested, 1211291Sbill * e.g. in process. 1221291Sbill */ 1231291Sbill seterr(s) 1241291Sbill char *s; 1251291Sbill { 1261291Sbill 1271291Sbill if (err == 0) 1281291Sbill err = s, errspl = 0; 1291291Sbill } 1301291Sbill 1311291Sbill /* Set err to a splice of cp and dp, to be freed later in error() */ 1321291Sbill seterr2(cp, dp) 1331291Sbill char *cp, *dp; 1341291Sbill { 1351291Sbill 1361291Sbill if (err) 1371291Sbill return; 1381291Sbill err = strspl(cp, dp); 1391291Sbill errspl++; 1401291Sbill } 1411291Sbill 1421291Sbill /* Set err to a splice of cp with a string form of character d */ 1431291Sbill seterrc(cp, d) 1441291Sbill char *cp, d; 1451291Sbill { 1461291Sbill char chbuf[2]; 1471291Sbill 1481291Sbill chbuf[0] = d; 1491291Sbill chbuf[1] = 0; 1501291Sbill seterr2(cp, chbuf); 1511291Sbill } 152