2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
7 /* All Rights Reserved */
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley Software License Agreement
12 * specifies the terms and conditions for redistribution.
15 #pragma ident "%Z%%M% %I% %E% SMI"
22 * #include <sys/ioctl.h>
25 #include "sh.tconst.h"
31 bool errspl
; /* Argument to error was spliced by seterr2 */
32 tchar one
[2] = { '1', 0 };
33 tchar
*onev
[2] = { one
, NOSTR
};
35 * contains DIR * for last opendir_(), its left open if an error
36 * longjmp (reset) occurs before it gets closed via closedir.
37 * if its not null in the error handler, then closedir it.
42 * Print error string s with optional argument arg.
43 * This routine always resets or exits. The flag haderr
44 * is set so the routine who catches the unwind can propogate
47 * Note that any open files at the point of error will eventually
48 * be closed in the routine process in sh.c which is the only
49 * place error unwinds are ever caught.
60 * Must flush before we print as we wish output before the error
61 * to go on (some form of) standard output, while output after
62 * goes on (some form of) diagnostic output.
63 * If didfds then output will go to 1/2 else to FSHOUT/FSHDIAG.
64 * See flush in sh.print.c.
67 haderr
= 1; /* Now to diagnostic output */
68 timflg
= 0; /* This isn't otherwise reset */
70 pargv
= 0, blkfree(v
);
72 gargv
= 0, blkfree(v
);
75 * A zero arguments causes no printing, else print
76 * an error diagnostic here.
79 printf(s
, a1
, a2
), printf("\n");
83 didfds
= 0; /* Forget about 0,1,2 */
84 if ((ep
= err
) && errspl
) {
96 * Go away if -e or we are a child shell
98 if (exiterr
|| child
) {
103 * Reset the state of the input.
104 * This buffered seek to end of file will also
105 * clear the while/foreach stack.
109 setq(S_status
, onev
, &shvhed
);
111 (void) ioctl(FSHTTY
, TIOCSPGRP
, (char *)&tpgrp
);
112 reset(); /* Unwind */
116 * Perror is the shells version of perror which should otherwise
125 * Perror uses unit 2, thus if we didn't set up the fd's
126 * we must set up unit 2 now else the diagnostic will disappear
131 (void) dcopy(SHDIAG
, 2);
136 error(NULL
); /* To exit or unwind */
145 if( bname
) printf("%t: ", bname
);
146 error("%s", gettext(cp
));
150 * The parser and scanner set up errors for later by calling seterr,
151 * which sets the variable err as a side effect; later to be tested,
162 /* Set err to a splice of cp and dp, to be freed later in error() */
164 seterr2(tchar
*cp
, char *dp
)
172 /* Concatinate cp and dp in the allocated space. */
175 err
= (char *)xalloc(strlen(chbuf
)+strlen(gdp
)+1);
179 errspl
++;/* Remember to xfree(err). */
182 /* Set err to a splice of cp with a string form of character d */
184 seterrc(char *cp
, tchar d
)
186 char chbuf
[MB_LEN_MAX
+1];
188 /* don't overwrite an existing error message */
194 wchar_t wcd
=(wchar_t)(d
&TRIM
);
197 i
= wctomb(chbuf
, wcd
); /* chbuf holds d in multibyte representation. */
198 chbuf
[(i
>0)?i
:0] = (char) 0;
201 chbuf
[0]=(char)(d
&TRIM
); chbuf
[1]=(char)0;
205 /* Concatinate cp and d in the allocated space. */
206 err
= (char *)xalloc(strlen(cp
)+strlen(chbuf
)+1);
210 errspl
++; /* Remember to xfree(err). */