Merge commit 'crater/master'
[dragonfly.git] / contrib / tcsh-6 / sh.h
blobf955734ee242756ef34dbe6148d9c903c83663e6
1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.h,v 3.150 2009/06/25 21:27:37 christos Exp $ */
2 /*
3 * sh.h: Catch it all globals and includes file!
4 */
5 /*-
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
7 * All rights reserved.
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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 #ifndef _h_sh
34 #define _h_sh
36 #include "config.h"
38 #include <stddef.h>
39 #include <signal.h>
41 #ifdef HAVE_ICONV
42 # include <iconv.h>
43 #endif
45 #ifdef HAVE_STDINT_H
46 # include <stdint.h>
47 #endif
49 #ifdef HAVE_INTTYPES_H
50 # include <inttypes.h>
51 #endif
53 #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
54 typedef unsigned long intptr_t;
55 #endif
57 #ifndef EXTERN
58 # define EXTERN extern
59 #else /* !EXTERN */
60 # ifdef WINNT_NATIVE
61 # define IZERO = 0
62 # define IZERO_STRUCT = {0}
63 # endif /* WINNT_NATIVE */
64 #endif /* EXTERN */
66 #ifndef IZERO
67 # define IZERO
68 #endif /* IZERO */
69 #ifndef IZERO_STRUCT
70 # define IZERO_STRUCT
71 #endif /* IZERO_STRUCT */
73 #ifndef WINNT_NATIVE
74 # define INIT_ZERO
75 # define INIT_ZERO_STRUCT
76 # define force_read xread
77 #endif /*!WINNT_NATIVE */
79 * Sanity
81 #if defined(_POSIX_SOURCE) && !defined(POSIX)
82 # define POSIX
83 #endif
85 #if defined(POSIXJOBS) && !defined(BSDJOBS)
86 # define BSDJOBS
87 #endif
89 #ifdef SHORT_STRINGS
90 # ifdef WIDE_STRINGS
91 #include <wchar.h>
92 typedef wchar_t Char;
93 typedef unsigned long uChar;
94 typedef wint_t eChar; /* Can contain any Char value or CHAR_ERR */
95 #define CHAR_ERR WEOF /* Pretty please, use bit 31... */
96 #define normal_mbtowc(PWC, S, N) rt_mbtowc(PWC, S, N)
97 #define reset_mbtowc() IGNORE(mbtowc(NULL, NULL, 0))
98 # else
99 typedef short Char;
100 typedef unsigned short uChar;
101 typedef int eChar;
102 #define CHAR_ERR (-1)
103 #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
104 #define reset_mbtowc() ((void)0)
105 # endif
106 # define SAVE(a) (Strsave(str2short(a)))
107 #else
108 typedef char Char;
109 typedef unsigned char uChar;
110 typedef int eChar;
111 #define CHAR_ERR (-1)
112 #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
113 #define reset_mbtowc() ((void)0)
114 # define SAVE(a) (strsave(a))
115 #endif
117 /* Elide unused argument warnings */
118 #define USE(a) (void) (a)
119 #define IGNORE(a) ignore((intptr_t)a)
120 static inline void ignore(intptr_t a)
122 USE(a);
126 * Return true if the path is absolute
128 #if defined(WINNT_NATIVE)
129 # define ABSOLUTEP(p) ((p)[0] == '/' || \
130 (Isalpha((p)[0]) && (p)[1] == ':'))
131 #elif defined(__CYGWIN__)
132 # define ABSOLUTEP(p) ((p)[0] == '/' || \
133 (Isalpha((p)[0]) && (p)[1] == ':' && \
134 ((p)[2] == '\0' || (p)[2] == '/')))
135 #else /* !WINNT_NATIVE && !__CYGWIN__ */
136 # define ABSOLUTEP(p) (*(p) == '/')
137 #endif /* WINNT_NATIVE || __CYGWIN__ */
140 * Fundamental definitions which may vary from system to system.
142 * BUFSIZE The i/o buffering size; also limits word size
143 * MAILINTVL How often to mailcheck; more often is more expensive
145 #ifdef BUFSIZE
146 # if BUFSIZE < 4096
147 # undef BUFSIZE
148 # define BUFSIZE 4096 /* buffer size should be no less than this */
149 # endif
150 #else
151 # define BUFSIZE 4096
152 #endif /* BUFSIZE */
154 #define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
155 #define MAILINTVL 600 /* 10 minutes */
157 #ifndef INBUFSIZE
158 # define INBUFSIZE 2*BUFSIZE /* Num input characters on the command line */
159 #endif /* INBUFSIZE */
163 * What our builtin echo looks like
165 #define NONE_ECHO 0
166 #define BSD_ECHO 1
167 #define SYSV_ECHO 2
168 #define BOTH_ECHO (BSD_ECHO|SYSV_ECHO)
170 #ifndef ECHO_STYLE
171 # if SYSVREL > 0
172 # define ECHO_STYLE SYSV_ECHO
173 # else /* SYSVREL == 0 */
174 # define ECHO_STYLE BSD_ECHO
175 # endif /* SYSVREL */
176 #endif /* ECHO_STYLE */
179 * The shell moves std in/out/diag and the old std input away from units
180 * 0, 1, and 2 so that it is easy to set up these standards for invoked
181 * commands.
183 #define FSAFE 5 /* We keep the first 5 descriptors untouched */
184 #define FSHTTY 15 /* /dev/tty when manip pgrps */
185 #define FSHIN 16 /* Preferred desc for shell input */
186 #define FSHOUT 17 /* ... shell output */
187 #define FSHDIAG 18 /* ... shell diagnostics */
188 #define FOLDSTD 19 /* ... old std input */
190 #ifdef PROF
191 #define xexit(n) done(n)
192 #endif
194 #ifdef cray
195 # define word word_t /* sys/types.h defines word.. bad move! */
196 #endif
198 #include <sys/types.h>
200 #ifdef cray
201 # undef word
202 #endif
205 * Path separator in environment variables
207 #ifndef PATHSEP
208 # if defined(__EMX__) || defined(WINNT_NATIVE)
209 # define PATHSEP ';'
210 # else /* unix */
211 # define PATHSEP ':'
212 # endif /* __EMX__ || WINNT_NATIVE */
213 #endif /* !PATHSEP */
215 #if defined(__HP_CXD_SPP) && !defined(__hpux)
216 # include <sys/cnx_stat.h>
217 # define stat stat64
218 # define fstat fstat64
219 # define lstat lstat64
220 #endif /* __HP_CXD_SPP && !__hpux */
223 * This macro compares the st_dev field of struct stat. On aix on ibmESA
224 * st_dev is a structure, so comparison does not work.
226 #ifndef DEV_DEV_COMPARE
227 # define DEV_DEV_COMPARE(x,y) ((x) == (y))
228 #endif /* DEV_DEV_COMPARE */
230 #ifdef _SEQUENT_
231 # include <sys/procstats.h>
232 #endif /* _SEQUENT_ */
233 #if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
234 # include <sys/times.h>
235 #endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
237 #ifdef NLS
238 # include <locale.h>
239 #endif /* NLS */
242 #if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
243 # include <sys/param.h>
244 #endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
245 #include <sys/stat.h>
247 #if defined(BSDTIMES) || defined(BSDLIMIT)
248 # include <sys/time.h>
249 # if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
250 # include "/usr/ucbinclude/sys/resource.h"
251 # else
252 # ifdef convex
253 # define sysrusage cvxrusage
254 # include <sys/sysinfo.h>
255 # else
256 # define sysrusage rusage
257 # include <sys/resource.h>
258 # endif /* convex */
259 # endif /* SYSVREL>3 */
260 #endif /* BSDTIMES */
262 #ifndef WINNT_NATIVE
263 # ifndef POSIX
264 # ifdef TERMIO
265 # include <termio.h>
266 # else /* SGTTY */
267 # include <sgtty.h>
268 # endif /* TERMIO */
269 # else /* POSIX */
270 # ifndef _UWIN
271 # include <termios.h>
272 # else
273 # include <termio.h>
274 # endif /* _UWIN */
275 # if SYSVREL > 3
276 # undef TIOCGLTC /* we don't need those, since POSIX has them */
277 # undef TIOCSLTC
278 # undef CSWTCH
279 # define CSWTCH _POSIX_VDISABLE /* So job control works */
280 # endif /* SYSVREL > 3 */
281 # endif /* POSIX */
282 #endif /* WINNT_NATIVE */
284 #ifdef sonyrisc
285 # include <sys/ttold.h>
286 #endif /* sonyrisc */
288 #if defined(POSIX) && !defined(WINNT_NATIVE)
289 # include <unistd.h>
292 * the gcc+protoize version of <stdlib.h>
293 * redefines malloc(), so we define the following
294 * to avoid it.
296 # if defined(SYSMALLOC) || defined(linux) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX)
297 # define NO_FIX_MALLOC
298 # include <stdlib.h>
299 # else /* glibc */
300 # define _GNU_STDLIB_H
301 # define malloc __malloc
302 # define free __free
303 # define calloc __calloc
304 # define realloc __realloc
305 # include <stdlib.h>
306 # undef malloc
307 # undef free
308 # undef calloc
309 # undef realloc
310 # endif /* glibc || sgi */
311 #endif /* POSIX && !WINNT_NATIVE */
312 #include <limits.h>
314 #if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(linux) || defined(__GNU__) || defined(__GLIBC__)
315 # if !defined(pyr) && !defined(stellar)
316 # include <time.h>
317 # ifdef _MINIX
318 # define HZ CLOCKS_PER_SEC
319 # endif /* _MINIX */
320 # endif /* !pyr && !stellar */
321 #endif /* SYSVREL > 0 || _IBMR2 */
323 /* In the following ifdef the DECOSF1 has been commented so that later
324 * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
326 #if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
327 # if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
328 # include <sys/ioctl.h>
329 # endif
330 #endif
332 #if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
333 #undef CSWTCH
334 #define CSWTCH _POSIX_VDISABLE
335 #endif
337 #if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
338 # include <sys/filio.h>
339 #endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
341 #if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
342 # include <sys/file.h>
343 #endif /* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
345 #if !defined(O_RDONLY) || !defined(O_NDELAY)
346 # include <fcntl.h>
347 #endif
348 #ifndef O_LARGEFILE
349 # define O_LARGEFILE 0
350 #endif
352 #include <errno.h>
354 #include <setjmp.h>
356 #include <stdarg.h>
358 #ifdef HAVE_DIRENT_H
359 # include <dirent.h>
360 #else
361 # ifdef HAVE_NDIR_H
362 # include <ndir.h>
363 # else
364 # include <sys/dir.h>
365 # endif
366 # define dirent direct
367 #endif /* HAVE_DIRENT_H */
368 #ifndef HAVE_STRUCT_DIRENT_D_INO
369 # define d_ino d_fileno
370 #endif
371 #if defined(hpux) || defined(sgi) || defined(OREO)
372 # include <stdio.h> /* So the fgetpwent() prototypes work */
373 #endif /* hpux || sgi || OREO */
374 #ifndef WINNT_NATIVE
375 #include <pwd.h>
376 #include <grp.h>
377 #endif /* WINNT_NATIVE */
378 #ifdef HAVE_SHADOW_H
379 # include <shadow.h>
380 #endif /* HAVE_SHADOW_H */
381 #ifdef HAVE_AUTH_H
382 # include <auth.h>
383 #endif /* HAVE_AUTH_H */
384 #if defined(BSD) && !defined(POSIX)
385 # include <strings.h>
386 # define strchr(a, b) index(a, b)
387 # define strrchr(a, b) rindex(a, b)
388 #else
389 # include <string.h>
390 #endif /* BSD */
393 * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
394 * include it yet, so we include it here
396 #if defined(sgi) && SYSVREL > 3
397 # include <sys/cdefs.h>
398 #endif /* sgi && SYSVREL > 3 */
400 #ifdef REMOTEHOST
401 # ifdef ISC
402 # undef MAXHOSTNAMELEN /* Busted headers? */
403 # endif
405 # include <netinet/in.h>
406 # include <arpa/inet.h>
407 # include <sys/socket.h>
408 # if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
409 # if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
410 # if !defined(sgi)
411 # if !defined(__CYGWIN__)
412 # define INET6
413 # endif /* __CYGWIN__ */
414 # endif /* sgi */
415 # endif /* __APPLE__ */
416 # endif
417 # include <sys/uio.h> /* For struct iovec */
418 #endif /* REMOTEHOST */
420 #ifdef PURIFY
421 /* exit normally, allowing purify to trace leaks */
422 # define _exit exit
423 typedef int pret_t;
424 #else /* !PURIFY */
426 * If your compiler complains, then you can either
427 * throw it away and get gcc or, use the following define
428 * and get rid of the typedef.
429 * [The 4.2/3BSD vax compiler does not like that]
430 * Both MULTIFLOW and PCC compilers exhbit this bug. -- sterling@netcom.com
432 # if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__)
433 # define pret_t void
434 # else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */
435 typedef void pret_t;
436 # endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */
437 #endif /* PURIFY */
440 * ASCII vs. EBCDIC
442 #if 'Z' - 'A' == 25
443 # ifndef IS_ASCII
444 # define IS_ASCII
445 # endif
446 #endif
448 #include "sh.types.h"
450 #if !HAVE_DECL_GETPGRP
451 # ifndef GETPGRP_VOID
452 extern pid_t getpgrp (int);
453 # else
454 extern pid_t getpgrp (void);
455 # endif
456 #endif
458 #ifndef lint
459 typedef ptr_t memalign_t;
460 #else
461 typedef union {
462 char am_char, *am_char_p;
463 short am_short, *am_short_p;
464 int am_int, *am_int_p;
465 long am_long, *am_long_p;
466 float am_float, *am_float_p;
467 double am_double, *am_double_p;
468 } *memalign_t;
470 # define malloc lint_malloc
471 # define free lint_free
472 # define realloc lint_realloc
473 # define calloc lint_calloc
474 #endif
476 #ifdef SYSMALLOC
477 # define xmalloc(i) smalloc(i)
478 # define xrealloc(p, i) srealloc(p, i)
479 # define xcalloc(n, s) scalloc(n, s)
480 # define xfree sfree
481 #else
482 # define xmalloc(i) malloc(i)
483 # define xrealloc(p, i) realloc(p, i)
484 # define xcalloc(n, s) calloc(n, s)
485 # define xfree free
486 #endif /* SYSMALLOC */
487 #include "sh.char.h"
488 #include "sh.err.h"
489 #include "sh.dir.h"
490 #include "sh.proc.h"
492 #include "pathnames.h"
496 * C shell
498 * Bill Joy, UC Berkeley
499 * October, 1978; May 1980
501 * Jim Kulp, IIASA, Laxenburg Austria
502 * April, 1980
505 #ifdef HESIOD
506 # include <hesiod.h>
507 #endif /* HESIOD */
509 #ifdef REMOTEHOST
510 # include <netdb.h>
511 #endif /* REMOTEHOST */
513 #ifndef MAXHOSTNAMELEN
514 # ifdef HOST_NAME_MAX
515 # define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
516 # elif defined(SCO) && (SYSVREL > 3)
517 # include <sys/socket.h>
518 # else
519 # define MAXHOSTNAMELEN 256
520 # endif
521 #endif /* MAXHOSTNAMELEN */
525 #define eq(a, b) (Strcmp(a, b) == 0)
527 /* globone() flags */
528 #define G_ERROR 0 /* default action: error if multiple words */
529 #define G_IGNORE 1 /* ignore the rest of the words */
530 #define G_APPEND 2 /* make a sentence by cat'ing the words */
533 * Global flags
535 EXTERN int chkstop IZERO; /* Warned of stopped jobs... allow exit */
537 #if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
538 # define CLOSE_ON_EXEC
539 #else
540 EXTERN int didcch IZERO; /* Have closed unused fd's for child */
541 #endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
543 EXTERN int didfds IZERO; /* Have setup i/o fd's for child */
544 EXTERN int doneinp IZERO; /* EOF indicator after reset from readc */
545 EXTERN int exiterr IZERO; /* Exit if error or non-zero exit status */
546 EXTERN int child IZERO; /* Child shell ... errors cause exit */
547 EXTERN int haderr IZERO; /* Reset was because of an error */
548 EXTERN int intty IZERO; /* Input is a tty */
549 EXTERN int intact IZERO; /* We are interactive... therefore prompt */
550 EXTERN int justpr IZERO; /* Just print because of :p hist mod */
551 EXTERN int loginsh IZERO; /* We are a loginsh -> .login/.logout */
552 EXTERN int neednote IZERO; /* Need to pnotify() */
553 EXTERN int noexec IZERO; /* Don't execute, just syntax check */
554 EXTERN int pjobs IZERO; /* want to print jobs if interrupted */
555 EXTERN int setintr IZERO; /* Set interrupts on/off -> Wait intr... */
556 EXTERN int havhash IZERO; /* path hashing is available */
557 EXTERN int editing IZERO; /* doing filename expansion and line editing */
558 EXTERN int noediting IZERO; /* initial $term defaulted to noedit */
559 EXTERN int bslash_quote IZERO;/* PWP: tcsh-style quoting? (in sh.c) */
560 EXTERN int compat_expr IZERO;/* csh-style expressions? */
561 EXTERN int isoutatty IZERO; /* is SHOUT a tty */
562 EXTERN int isdiagatty IZERO;/* is SHDIAG a tty */
563 EXTERN int is1atty IZERO; /* is file descriptor 1 a tty (didfds mode) */
564 EXTERN int is2atty IZERO; /* is file descriptor 2 a tty (didfds mode) */
565 EXTERN int arun IZERO; /* Currently running multi-line-aliases */
566 EXTERN int implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
567 EXTERN int inheredoc IZERO; /* Currently parsing a heredoc */
568 /* We received a window change event */
569 EXTERN volatile sig_atomic_t windowchg IZERO;
570 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
571 EXTERN int dspmbyte_ls;
572 #endif
575 * Global i/o info
577 EXTERN Char *arginp IZERO; /* Argument input for sh -c and internal `xx` */
578 EXTERN int onelflg IZERO; /* 2 -> need line for -t, 1 -> exit on read */
579 extern Char *ffile; /* Name of shell file for $0 */
580 extern int dolzero; /* if $?0 should return true... */
582 extern char *seterr; /* Error message from scanner/parser */
583 #ifndef errno
584 extern int errno; /* Error from C library routines */
585 #endif
586 extern int exitset;
587 /* Temp name for << shell files in /tmp, for xfree() */
588 EXTERN Char *shtemp IZERO;
590 #ifdef BSDTIMES
591 EXTERN struct timeval time0; /* Time at which the shell started */
592 EXTERN struct sysrusage ru0;
593 #else
594 # ifdef _SEQUENT_
595 EXTERN timeval_t time0; /* time at which shell started */
596 EXTERN struct process_stats ru0;
597 # else /* _SEQUENT_ */
598 # ifndef POSIX
599 EXTERN time_t time0; /* time at which shell started */
600 # else /* POSIX */
601 EXTERN clock_t time0; /* time at which shell started */
602 EXTERN clock_t clk_tck;
603 # endif /* POSIX */
604 EXTERN struct tms shtimes; /* shell and child times for process timing */
605 # endif /* _SEQUENT_ */
606 EXTERN time_t seconds0;
607 #endif /* BSDTIMES */
609 #ifndef HZ
610 # define HZ 100 /* for division into seconds */
611 #endif
614 * Miscellany
616 EXTERN Char *doldol; /* Character pid for $$ */
617 EXTERN pid_t backpid; /* pid of the last background job */
620 * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
621 * cause pid's could be unsigned and that would break our -1 flag, and
622 * uid_t and gid_t are not defined in all the systems so I would have to
623 * make special cases for them. In the future...
625 EXTERN uid_t uid, euid; /* Invokers real and effective */
626 EXTERN gid_t gid, egid; /* User and group ids */
627 EXTERN pid_t opgrp, /* Initial pgrp and tty pgrp */
628 shpgrp, /* Pgrp of shell */
629 tpgrp; /* Terminal process group */
630 /* If tpgrp is -1, leave tty alone! */
632 EXTERN Char *Prompt; /* The actual printed prompt or NULL */
633 EXTERN Char *RPrompt; /* Right-hand side prompt or NULL */
636 * To be able to redirect i/o for builtins easily, the shell moves the i/o
637 * descriptors it uses away from 0,1,2.
638 * Ideally these should be in units which are closed across exec's
639 * (this saves work) but for version 6, this is not usually possible.
640 * The desired initial values for these descriptors are defined in
641 * sh.local.h.
643 EXTERN int SHIN IZERO; /* Current shell input (script) */
644 EXTERN int SHOUT IZERO; /* Shell output */
645 EXTERN int SHDIAG IZERO; /* Diagnostic output... shell errs go here */
646 EXTERN int OLDSTD IZERO; /* Old standard input (def for cmds) */
649 #if SYSVREL == 4 && defined(_UTS)
651 * From: fadden@uts.amdahl.com (Andy McFadden)
652 * we need sigsetjmp for UTS4, but not UTS2.1
654 # define SIGSETJMP
655 #endif
658 * Error control
660 * Errors in scanning and parsing set up an error message to be printed
661 * at the end and complete. Other errors always cause a reset.
662 * Because of source commands and .cshrc we need nested error catches.
665 #ifdef SIGSETJMP
666 typedef struct { sigjmp_buf j; } jmp_buf_t;
667 # define setexit() sigsetjmp(reslab.j)
668 # define _reset() siglongjmp(reslab.j, 1)
669 #else
670 typedef struct { jmp_buf j; } jmp_buf_t;
671 # define setexit() setjmp(reslab.j)
672 # define _reset() longjmp(reslab.j, 1)
673 #endif
675 #define getexit(a) (void) ((a) = reslab)
676 #define resexit(a) (void) (reslab = (a))
678 #define cpybin(a, b) (void) ((a) = (b))
680 extern jmp_buf_t reslab;
682 EXTERN Char *gointr; /* Label for an onintr transfer */
684 extern struct sigaction parintr; /* Parents interrupt catch */
685 extern struct sigaction parterm; /* Parents terminate catch */
688 * Lexical definitions.
690 * All lexical space is allocated dynamically.
691 * The eighth/sixteenth bit of characters is used to prevent recognition,
692 * and eventually stripped.
694 #define META 0200
695 #define ASCII 0177
696 #ifdef WIDE_STRINGS /* Implies SHORT_STRINGS */
697 /* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
698 # define QUOTE 0x40000000
699 # define TRIM 0x3FFFFFFF /* Mask to strip quote bit */
700 # define UNDER 0x08000000 /* Underline flag */
701 # define BOLD 0x04000000 /* Bold flag */
702 # define STANDOUT 0x02000000 /* Standout flag */
703 # define LITERAL 0x01000000 /* Literal character flag */
704 # define ATTRIBUTES 0x0F000000 /* The bits used for attributes */
705 # define INVALID_BYTE 0x00800000 /* Invalid character on input */
706 # ifdef SOLARIS2
707 # define CHAR 0x30FFFFFF /* Mask to mask out the character */
708 # else
709 # define CHAR 0x00FFFFFF /* Mask to mask out the character */
710 # endif
711 #elif defined (SHORT_STRINGS)
712 # define QUOTE ((Char) 0100000)/* 16nth char bit used for 'ing */
713 # define TRIM 0073777 /* Mask to strip quote/lit bit */
714 # define UNDER 0040000 /* Underline flag */
715 # define BOLD 0020000 /* Bold flag */
716 # define STANDOUT 0010000 /* Standout flag */
717 # define LITERAL 0004000 /* Literal character flag */
718 # define ATTRIBUTES 0074000 /* The bits used for attributes */
719 # define INVALID_BYTE 0
720 # define CHAR 0000377 /* Mask to mask out the character */
721 #else
722 # define QUOTE ((Char) 0200) /* Eighth char bit used for 'ing */
723 # define TRIM 0177 /* Mask to strip quote bit */
724 # define UNDER 0000000 /* No extra bits to do both */
725 # define BOLD 0000000 /* Bold flag */
726 # define STANDOUT META /* Standout flag */
727 # define LITERAL 0000000 /* Literal character flag */
728 # define ATTRIBUTES 0200 /* The bits used for attributes */
729 # define INVALID_BYTE 0
730 # define CHAR 0000177 /* Mask to mask out the character */
731 #endif
732 #define CHAR_DBWIDTH (LITERAL|(LITERAL-1))
734 EXTERN int AsciiOnly; /* If set only 7 bits expected in characters */
737 * Each level of input has a buffered input structure.
738 * There are one or more blocks of buffered input for each level,
739 * exactly one if the input is seekable and tell is available.
740 * In other cases, the shell buffers enough blocks to keep all loops
741 * in the buffer.
743 * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
744 * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
745 * fbuf indexing).
747 * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
748 * is no difference between byte and character offsets.
750 EXTERN struct Bin {
751 off_t Bfseekp; /* Seek pointer, generally != lseek() value */
752 off_t Bfbobp; /* Seekp of beginning of buffers */
753 off_t Bfeobp; /* Seekp of end of buffers */
754 int Bfblocks; /* Number of buffer blocks */
755 Char **Bfbuf; /* The array of buffer blocks */
756 #ifdef WIDE_STRINGS
757 /* Number of bytes in each character if (cantell) */
758 unsigned char Bfclens[BUFSIZE + 1];
759 #endif
760 } B;
763 * This structure allows us to seek inside aliases
765 struct Ain {
766 int type;
767 #define TCSH_I_SEEK 0 /* Invalid seek */
768 #define TCSH_A_SEEK 1 /* Alias seek */
769 #define TCSH_F_SEEK 2 /* File seek */
770 #define TCSH_E_SEEK 3 /* Eval seek */
771 union {
772 off_t _f_seek; /* A byte offset if (cantell) */
773 Char* _c_seek;
774 } fc;
775 #define f_seek fc._f_seek
776 #define c_seek fc._c_seek
777 Char **a_seek;
780 extern int aret; /* Type of last char returned */
781 #define SEEKEQ(a, b) ((a)->type == (b)->type && \
782 (a)->f_seek == (b)->f_seek && \
783 (a)->a_seek == (b)->a_seek)
785 #define fseekp B.Bfseekp
786 #define fbobp B.Bfbobp
787 #define feobp B.Bfeobp
788 #define fblocks B.Bfblocks
789 #define fbuf B.Bfbuf
790 #define fclens B.Bfclens
793 * The shell finds commands in loops by reseeking the input
794 * For whiles, in particular, it reseeks to the beginning of the
795 * line the while was on; hence the while placement restrictions.
797 EXTERN struct Ain lineloc;
799 EXTERN int cantell; /* Is current source tellable ? */
802 * Input lines are parsed into doubly linked circular
803 * lists of words of the following form.
805 struct wordent {
806 Char *word;
807 struct wordent *prev;
808 struct wordent *next;
812 * During word building, both in the initial lexical phase and
813 * when expanding $ variable substitutions, expansion by `!' and `$'
814 * must be inhibited when reading ahead in routines which are themselves
815 * processing `!' and `$' expansion or after characters such as `\' or in
816 * quotations. The following flags are passed to the getC routines
817 * telling them which of these substitutions are appropriate for the
818 * next character to be returned.
820 #define DODOL 1
821 #define DOEXCL 2
822 #define DOALL DODOL|DOEXCL
825 * Labuf implements a general buffer for lookahead during lexical operations.
826 * Text which is to be placed in the input stream can be stuck here.
827 * We stick parsed ahead $ constructs during initial input,
828 * process id's from `$$', and modified variable values (from qualifiers
829 * during expansion in sh.dol.c) here.
831 extern struct Strbuf labuf;
832 EXTERN size_t lap; /* N/A if == labuf.len, index into labuf.s otherwise */
835 * Parser structure
837 * Each command is parsed to a tree of command structures and
838 * flags are set bottom up during this process, to be propagated down
839 * as needed during the semantics/exeuction pass (sh.sem.c).
841 struct command {
842 unsigned char t_dtyp; /* Type of node */
843 #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
844 #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
845 #define NODE_PIPE 3 /* t_dlef | t_drit */
846 #define NODE_LIST 4 /* t_dlef ; t_drit */
847 #define NODE_OR 5 /* t_dlef || t_drit */
848 #define NODE_AND 6 /* t_dlef && t_drit */
849 unsigned char t_nice; /* Nice value */
850 #ifdef apollo
851 unsigned char t_systype; /* System environment */
852 #endif
853 unsigned long t_dflg; /* Flags, e.g. F_AMPERSAND|... */
854 /* save these when re-doing */
855 #ifndef apollo
856 #define F_SAVE (F_NICE|F_TIME|F_NOHUP|F_HUP)
857 #else
858 #define F_SAVE (F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
859 #endif
860 #define F_AMPERSAND (1<<0) /* executes in background */
861 #define F_APPEND (1<<1) /* output is redirected >> */
862 #define F_PIPEIN (1<<2) /* input is a pipe */
863 #define F_PIPEOUT (1<<3) /* output is a pipe */
864 #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
865 #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
866 /* spare */
867 #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
868 #define F_OVERWRITE (1<<8) /* output was ! */
869 #define F_READ (1<<9) /* input redirection is << */
870 #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
871 #define F_NICE (1<<11) /* t_nice is meaningful */
872 #define F_NOHUP (1<<12) /* nohup this command */
873 #define F_TIME (1<<13) /* time this command */
874 #define F_BACKQ (1<<14) /* command is in `` */
875 #define F_HUP (1<<15) /* hup this command */
876 #ifdef apollo
877 #define F_VER (1<<16) /* execute command under SYSTYPE */
878 #endif
879 union {
880 Char *T_dlef; /* Input redirect word */
881 struct command *T_dcar; /* Left part of list/pipe */
882 } L;
883 union {
884 Char *T_drit; /* Output redirect word */
885 struct command *T_dcdr; /* Right part of list/pipe */
886 } R;
887 #define t_dlef L.T_dlef
888 #define t_dcar L.T_dcar
889 #define t_drit R.T_drit
890 #define t_dcdr R.T_dcdr
891 Char **t_dcom; /* Command/argument vector */
892 struct command *t_dspr; /* Pointer to ()'d subtree */
897 * The keywords for the parser
899 #define TC_BREAK 0
900 #define TC_BRKSW 1
901 #define TC_CASE 2
902 #define TC_DEFAULT 3
903 #define TC_ELSE 4
904 #define TC_END 5
905 #define TC_ENDIF 6
906 #define TC_ENDSW 7
907 #define TC_EXIT 8
908 #define TC_FOREACH 9
909 #define TC_GOTO 10
910 #define TC_IF 11
911 #define TC_LABEL 12
912 #define TC_LET 13
913 #define TC_SET 14
914 #define TC_SWITCH 15
915 #define TC_TEST 16
916 #define TC_THEN 17
917 #define TC_WHILE 18
920 * These are declared here because they want to be
921 * initialized in sh.init.c (to allow them to be made readonly)
924 #if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
925 /* Avoid hpux ansi mode spurious warnings */
926 typedef void (*bfunc_t) ();
927 #else
928 typedef void (*bfunc_t) (Char **, struct command *);
929 #endif /* hpux && __STDC__ && !__GNUC__ */
931 extern const struct biltins {
932 const char *bname;
933 bfunc_t bfunct;
934 int minargs, maxargs;
935 } bfunc[];
936 extern int nbfunc;
937 #ifdef WINNT_NATIVE
938 extern struct biltins nt_bfunc[];
939 extern int nt_nbfunc;
940 #endif /* WINNT_NATIVE*/
941 extern int bequiet;
943 extern struct srch {
944 const char *s_name;
945 int s_value;
946 } srchn[];
947 extern int nsrchn;
950 * Structure defining the existing while/foreach loops at this
951 * source level. Loops are implemented by seeking back in the
952 * input. For foreach (fe), the word list is attached here.
954 EXTERN struct whyle {
955 struct Ain w_start; /* Point to restart loop */
956 struct Ain w_end; /* End of loop (0 if unknown) */
957 Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
958 Char *w_fename; /* Name for fe */
959 struct whyle *w_next; /* Next (more outer) loop */
960 } *whyles;
963 * Variable structure
965 * Aliases and variables are stored in AVL balanced binary trees.
967 EXTERN struct varent {
968 Char **vec; /* Array of words which is the value */
969 Char *v_name; /* Name of variable/alias */
970 int v_flags; /* Flags */
971 #define VAR_ALL -1
972 #define VAR_READONLY 1
973 #define VAR_READWRITE 2
974 #define VAR_NOGLOB 4
975 #define VAR_FIRST 32
976 #define VAR_LAST 64
977 struct varent *v_link[3]; /* The links, see below */
978 int v_bal; /* Balance factor */
979 } shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
981 #define v_left v_link[0]
982 #define v_right v_link[1]
983 #define v_parent v_link[2]
985 #define adrof(v) adrof1(v, &shvhed)
986 #define varval(v) value1(v, &shvhed)
989 * The following are for interfacing redo substitution in
990 * aliases to the lexical routines.
992 EXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
993 EXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
994 EXTERN Char **alvec IZERO_STRUCT,
995 *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
998 * Filename/command name expansion variables
1001 #ifdef __CYGWIN__
1002 # undef MAXPATHLEN
1003 #endif /* __CYGWIN__ */
1005 #ifndef MAXPATHLEN
1006 # ifdef PATH_MAX
1007 # define MAXPATHLEN PATH_MAX
1008 # else
1009 # define MAXPATHLEN 2048
1010 # endif
1011 #endif /* MAXPATHLEN */
1013 #ifndef HAVENOLIMIT
1015 * resource limits
1017 extern struct limits {
1018 int limconst;
1019 const char *limname;
1020 int limdiv;
1021 const char *limscale;
1022 } limits[];
1023 #endif /* !HAVENOLIMIT */
1026 * History list
1028 * Each history list entry contains an embedded wordlist
1029 * from the scanner, a number for the event, and a reference count
1030 * to aid in discarding old entries.
1032 * Essentially "invisible" entries are put on the history list
1033 * when history substitution includes modifiers, and thrown away
1034 * at the next discarding since their event numbers are very negative.
1036 EXTERN struct Hist {
1037 struct wordent Hlex;
1038 int Hnum;
1039 int Href;
1040 time_t Htime;
1041 Char *histline;
1042 struct Hist *Hnext;
1043 } Histlist IZERO_STRUCT;
1045 EXTERN struct wordent paraml; /* Current lexical word list */
1046 EXTERN int eventno; /* Next events number */
1047 EXTERN int lastev; /* Last event reference (default) */
1049 EXTERN Char HIST; /* history invocation character */
1050 EXTERN Char HISTSUB; /* auto-substitute character */
1051 EXTERN Char PRCH; /* Prompt symbol for regular users */
1052 EXTERN Char PRCHROOT; /* Prompt symbol for root */
1055 * For operating systems with single case filenames (OS/2)
1057 #ifdef CASE_INSENSITIVE
1058 # ifdef WIDE_STRINGS
1059 # define samecase(x) (towlower(x))
1060 # else
1061 # define samecase(x) (isupper((unsigned char)(x)) ? \
1062 tolower((unsigned char)(x)) : (x))
1063 # endif
1064 #else
1065 # define samecase(x) (x)
1066 #endif /* CASE_INSENSITIVE */
1069 * strings.h:
1071 #ifndef SHORT_STRINGS
1072 #define Strchr(a, b) strchr(a, b)
1073 #define Strrchr(a, b) strrchr(a, b)
1074 #define Strcat(a, b) strcat(a, b)
1075 #define Strncat(a, b, c) strncat(a, b, c)
1076 #define Strcpy(a, b) strcpy(a, b)
1077 #define Strncpy(a, b, c) strncpy(a, b, c)
1078 #define Strlen(a) strlen(a)
1079 #define Strcmp(a, b) strcmp(a, b)
1080 #define Strncmp(a, b, c) strncmp(a, b, c)
1081 #define Strcasecmp(a, b) strcasecmp(a, b)
1083 #define Strspl(a, b) strspl(a, b)
1084 #define Strnsave(a, b) strnsave(a, b)
1085 #define Strsave(a) strsave(a)
1086 #define Strend(a) strend(a)
1087 #define Strstr(a, b) strstr(a, b)
1089 #define str2short(a) (a)
1090 #define blk2short(a) saveblk(a)
1091 #define short2blk(a) saveblk(a)
1092 #define short2str(a) caching_strip(a)
1093 #else
1094 #ifdef WIDE_STRINGS
1095 #define Strchr(a, b) wcschr(a, b)
1096 #define Strrchr(a, b) wcsrchr(a, b)
1097 #define Strcat(a, b) wcscat(a, b)
1098 #define Strncat(a, b, c) wcsncat(a, b, c)
1099 #define Strcpy(a, b) wcscpy(a, b)
1100 #define Strncpy(a, b, c) wcsncpy(a, b, c)
1101 #define Strlen(a) wcslen(a)
1102 #define Strcmp(a, b) wcscmp(a, b)
1103 #define Strncmp(a, b, c) wcsncmp(a, b, c)
1104 #else
1105 #define Strchr(a, b) s_strchr(a, b)
1106 #define Strrchr(a, b) s_strrchr(a, b)
1107 #define Strcat(a, b) s_strcat(a, b)
1108 #define Strncat(a, b, c) s_strncat(a, b, c)
1109 #define Strcpy(a, b) s_strcpy(a, b)
1110 #define Strncpy(a, b, c) s_strncpy(a, b, c)
1111 #define Strlen(a) s_strlen(a)
1112 #define Strcmp(a, b) s_strcmp(a, b)
1113 #define Strncmp(a, b, c) s_strncmp(a, b, c)
1114 #endif
1115 #define Strcasecmp(a, b) s_strcasecmp(a, b)
1117 #define Strspl(a, b) s_strspl(a, b)
1118 #define Strnsave(a, b) s_strnsave(a, b)
1119 #define Strsave(a) s_strsave(a)
1120 #define Strend(a) s_strend(a)
1121 #define Strstr(a, b) s_strstr(a, b)
1122 #endif
1125 * setname is a macro to save space (see sh.err.c)
1127 EXTERN const char *bname;
1129 #define setname(a) (bname = (a))
1131 #ifdef VFORK
1132 EXTERN Char *Vsav;
1133 EXTERN Char *Vdp;
1134 EXTERN Char *Vexpath;
1135 EXTERN char **Vt;
1136 #endif /* VFORK */
1138 EXTERN Char **evalvec;
1139 EXTERN Char *evalp;
1141 extern struct mesg {
1142 const char *iname; /* name from /usr/include */
1143 const char *pname; /* print name */
1144 } mesg[];
1146 /* word_chars is set by default to WORD_CHARS but can be overridden by
1147 the worchars variable--if unset, reverts to WORD_CHARS */
1149 EXTERN Char *word_chars;
1151 #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
1153 EXTERN Char *STR_SHELLPATH;
1155 #ifdef _PATH_BSHELL
1156 EXTERN Char *STR_BSHELL;
1157 #endif
1158 EXTERN Char *STR_WORD_CHARS;
1159 EXTERN Char **STR_environ IZERO;
1161 extern int dont_free; /* Tell free that we are in danger if we free */
1163 extern Char *INVPTR;
1164 extern Char **INVPPTR;
1166 extern char *progname;
1167 extern int tcsh;
1168 extern int xlate_cr;
1169 extern int output_raw;
1170 extern int lbuffed;
1171 extern time_t Htime;
1172 extern int numeof;
1173 extern int insource;
1174 extern char linbuf[];
1175 extern char *linp;
1176 extern int nsig;
1177 #ifdef VFORK
1178 extern int use_fork;
1179 #endif
1180 extern int tellwhat;
1181 extern int NoNLSRebind;
1182 #if !HAVE_DECL_ENVIRON
1183 extern char **environ;
1184 #endif
1186 #include "tc.h"
1188 #ifndef WINNT_NATIVE
1189 # ifdef NLS_CATALOGS
1190 # if defined(linux) || defined(__GNU__) || defined(__GLIBC__)
1191 # include <locale.h>
1192 # ifdef notdef
1193 # include <localeinfo.h> /* Has this changed ? */
1194 # endif
1195 # include <features.h>
1196 # endif
1197 # ifdef SUNOS4
1198 /* Who stole my nl_types.h? :-(
1199 * All this stuff is in the man pages, but nowhere else?
1200 * This does not link right now...
1202 typedef void *nl_catd;
1203 extern const char * catgets (nl_catd, int, int, const char *);
1204 nl_catd catopen (const char *, int);
1205 int catclose (nl_catd);
1206 # else
1207 # ifdef __uxps__
1208 # define gettxt gettxt_ds
1209 # endif
1210 # include <nl_types.h>
1211 # ifdef __uxps__
1212 # undef gettxt
1213 # endif
1214 # endif
1215 # ifndef MCLoadBySet
1216 # define MCLoadBySet 0
1217 # endif
1218 EXTERN nl_catd catd;
1219 # if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
1220 # define CGETS(b, c, d) iconv_catgets(catd, b, c, d)
1221 # else
1222 # define CGETS(b, c, d) xcatgets(catd, b, c, d)
1223 # endif
1224 # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
1225 # else
1226 # define CGETS(b, c, d) d
1227 # define CSAVS(b, c, d) d
1228 # endif
1229 #else /* WINNT_NATIVE */
1230 # define CGETS(b, c, d) nt_cgets( b, c, d)
1231 # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
1232 #endif /* WINNT_NATIVE */
1234 #if defined(FILEC)
1235 extern int filec;
1236 #endif /* FILEC */
1238 #include "sh.decls.h"
1240 * Since on some machines characters are unsigned, and the signed
1241 * keyword is not universally implemented, we treat all characters
1242 * as unsigned and sign extend them where we need.
1244 #define SIGN_EXTEND_CHAR(a) (((a) & 0x80) ? ((a) | ~0x7f) : (a))
1247 * explanation for use by the "--help" option
1249 #define HELP_STRING "\
1250 -b file batch mode, read and execute commands from `file' \n\
1251 -c command run `command' from next argument \n\
1252 -d load directory stack from `~/.cshdirs' \n\
1253 -Dname[=value] define environment variable `name' to `value' (DomainOS only) \n\
1254 -e exit on any error \n\
1255 -f start faster by ignoring the start-up file \n\
1256 -F use fork() instead of vfork() when spawning (ConvexOS only) \n\
1257 -i interactive, even when input is not from a terminal \n\
1258 -l act as a login shell, must be the only option specified \n\
1259 -m load the start-up file, whether or not owned by effective user \n\
1260 -n file no execute mode, just check syntax of the following `file' \n\
1261 -q accept SIGQUIT for running under a debugger \n\
1262 -s read commands from standard input \n\
1263 -t read one line from standard input \n\
1264 -v echo commands after history substitution \n\
1265 -V like -v but including commands read from the start-up file \n\
1266 -x echo commands immediately before execution \n\
1267 -X like -x but including commands read from the start-up file \n\
1268 --help print this message and exit \n\
1269 --version print the version shell variable and exit \n\
1270 \nSee the tcsh(1) manual page for detailed information.\n"
1272 #include "tc.nls.h"
1274 #endif /* _h_sh */