2 * sh.h: Catch it all globals and includes file!
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 #ifdef HAVE_INTTYPES_H
49 # include <inttypes.h>
52 #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
53 typedef unsigned long intptr_t;
57 # define EXTERN extern
61 # define IZERO_STRUCT = {0}
62 # endif /* WINNT_NATIVE */
70 #endif /* IZERO_STRUCT */
74 # define INIT_ZERO_STRUCT
75 # define force_read xread
76 #endif /*!WINNT_NATIVE */
78 #if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
85 #if defined(_POSIX_SOURCE) && !defined(POSIX)
89 #if defined(POSIXJOBS) && !defined(BSDJOBS)
93 #define TMP_TEMPLATE ".XXXXXX"
102 typedef wchar_t Char
;
104 typedef unsigned long uChar
;
105 typedef wint_t eChar
; /* Can contain any Char value or CHAR_ERR */
106 #define CHAR_ERR WEOF /* Pretty please, use bit 31... */
107 #define normal_mbtowc(PWC, S, N) rt_mbtowc(PWC, S, N)
108 #define reset_mbtowc() TCSH_IGNORE(mbtowc(NULL, NULL, 0))
111 typedef unsigned short uChar
;
113 #define CHAR_ERR (-1)
114 #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
115 #define reset_mbtowc() ((void)0)
117 # define SAVE(a) (Strsave(str2short(a)))
121 typedef unsigned char uChar
;
123 #define CHAR_ERR (-1)
124 #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
125 #define reset_mbtowc() ((void)0)
126 # define SAVE(a) (strsave(a))
129 #if !defined(__inline) && !defined(__GNUC__) && !defined(_MSC_VER)
133 #define TCSH_PTRDIFF_T_FMT "I"
135 #define TCSH_PTRDIFF_T_FMT "t"
137 /* Elide unused argument warnings */
138 #define USE(a) (void) (a)
139 #define TCSH_IGNORE(a) tcsh_ignore((intptr_t)a)
140 static __inline
void tcsh_ignore(intptr_t a
)
146 * Return true if the path is absolute
148 #if defined(WINNT_NATIVE)
149 # define ABSOLUTEP(p) ((p)[0] == '/' || \
150 (Isalpha((p)[0]) && (p)[1] == ':'))
151 #elif defined(__CYGWIN__)
152 # define ABSOLUTEP(p) ((p)[0] == '/' || \
153 (Isalpha((p)[0]) && (p)[1] == ':' && \
154 ((p)[2] == '\0' || (p)[2] == '/')))
155 #else /* !WINNT_NATIVE && !__CYGWIN__ */
156 # define ABSOLUTEP(p) (*(p) == '/')
157 #endif /* WINNT_NATIVE || __CYGWIN__ */
160 * Fundamental definitions which may vary from system to system.
162 * BUFSIZE The i/o buffering size; also limits word size
163 * MAILINTVL How often to mailcheck; more often is more expensive
168 # define BUFSIZE 4096 /* buffer size should be no less than this */
171 # define BUFSIZE 4096
174 #define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
175 #define MAILINTVL 600 /* 10 minutes */
178 # define INBUFSIZE 2*BUFSIZE /* Num input characters on the command line */
179 #endif /* INBUFSIZE */
183 * What our builtin echo looks like
188 #define BOTH_ECHO (BSD_ECHO|SYSV_ECHO)
192 # define ECHO_STYLE SYSV_ECHO
193 # else /* SYSVREL == 0 */
194 # define ECHO_STYLE BSD_ECHO
195 # endif /* SYSVREL */
196 #endif /* ECHO_STYLE */
198 /* values for noclobber */
199 #define NOCLOBBER_DEFAULT 1
200 #define NOCLOBBER_NOTEMPTY 2
201 #define NOCLOBBER_ASK 4
204 * The shell moves std in/out/diag and the old std input away from units
205 * 0, 1, and 2 so that it is easy to set up these standards for invoked
208 #define FSAFE 5 /* We keep the first 5 descriptors untouched */
209 #define FSHTTY 15 /* /dev/tty when manip pgrps */
210 #define FSHIN 16 /* Preferred desc for shell input */
211 #define FSHOUT 17 /* ... shell output */
212 #define FSHDIAG 18 /* ... shell diagnostics */
213 #define FOLDSTD 19 /* ... old std input */
216 #define xexit(n) done(n)
220 # define word word_t /* sys/types.h defines word.. bad move! */
223 #include <sys/types.h>
230 * Path separator in environment variables
233 # if defined(__EMX__) || defined(WINNT_NATIVE)
237 # endif /* __EMX__ || WINNT_NATIVE */
238 #endif /* !PATHSEP */
240 #if defined(__HP_CXD_SPP) && !defined(__hpux)
241 # include <sys/cnx_stat.h>
243 # define fstat fstat64
244 # define lstat lstat64
245 #endif /* __HP_CXD_SPP && !__hpux */
247 #ifdef HAVE_LONG_LONG
248 typedef long long tcsh_number_t
;
250 typedef long tcsh_number_t
;
253 * This macro compares the st_dev field of struct stat. On aix on ibmESA
254 * st_dev is a structure, so comparison does not work.
256 #ifndef DEV_DEV_COMPARE
257 # define DEV_DEV_COMPARE(x,y) ((x) == (y))
258 #endif /* DEV_DEV_COMPARE */
261 # include <sys/procstats.h>
262 #endif /* _SEQUENT_ */
263 #if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
264 # include <sys/times.h>
265 #endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
271 #if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
272 # include <sys/param.h>
273 #endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
274 #include <sys/stat.h>
276 #if defined(BSDTIMES) || defined(BSDLIMIT)
277 # include <sys/time.h>
278 # if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
279 # include "/usr/ucbinclude/sys/resource.h"
282 # define sysrusage cvxrusage
283 # include <sys/sysinfo.h>
285 # define sysrusage rusage
286 # include <sys/resource.h>
288 # endif /* SYSVREL>3 */
289 #endif /* BSDTIMES */
300 # include <termios.h>
304 # if SYSVREL > 3 || defined(__linux__)
305 # undef TIOCGLTC /* we don't need those, since POSIX has them */
308 # define CSWTCH _POSIX_VDISABLE /* So job control works */
309 # endif /* SYSVREL > 3 */
311 #endif /* WINNT_NATIVE */
314 # include <sys/ttold.h>
315 #endif /* sonyrisc */
317 #if defined(POSIX) && !defined(WINNT_NATIVE)
321 * the gcc+protoize version of <stdlib.h>
322 * redefines malloc(), so we define the following
325 # if defined(SYSMALLOC) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX) || defined(__OpenBSD__)
326 # define NO_FIX_MALLOC
329 # define _GNU_STDLIB_H
330 # define malloc __malloc
332 # define calloc __calloc
333 # define realloc __realloc
339 # endif /* glibc || sgi */
340 #endif /* POSIX && !WINNT_NATIVE */
343 #if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
344 # if !defined(pyr) && !defined(stellar)
347 # define HZ CLOCKS_PER_SEC
349 # endif /* !pyr && !stellar */
350 #endif /* SYSVREL > 0 || _IBMR2 */
352 /* In the following ifdef the DECOSF1 has been commented so that later
353 * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
355 #if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
356 # if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
357 # include <sys/ioctl.h>
358 # if SYSVREL > 3 || defined(__linux__)
359 # undef TIOCGLTC /* we don't need those, since POSIX has them */
362 # define CSWTCH _POSIX_VDISABLE /* So job control works */
363 # endif /* SYSVREL > 3 */
367 #if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
369 #define CSWTCH _POSIX_VDISABLE
372 #if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
373 # include <sys/filio.h>
374 #endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
376 #if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
377 # include <sys/file.h>
378 #endif /* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
380 #if !defined(O_RDONLY) || !defined(O_NDELAY)
396 # include <sys/dir.h>
398 # define dirent direct
399 #endif /* HAVE_DIRENT_H */
400 #ifndef HAVE_STRUCT_DIRENT_D_INO
401 # define d_ino d_fileno
403 #if defined(hpux) || defined(sgi) || defined(OREO)
404 # include <stdio.h> /* So the fgetpwent() prototypes work */
405 #endif /* hpux || sgi || OREO */
409 #endif /* WINNT_NATIVE */
412 #endif /* HAVE_SHADOW_H */
415 #endif /* HAVE_AUTH_H */
416 #if defined(BSD) && !defined(POSIX)
417 # include <strings.h>
418 # define strchr(a, b) index(a, b)
419 # define strrchr(a, b) rindex(a, b)
425 * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
426 * include it yet, so we include it here
428 #if defined(sgi) && SYSVREL > 3
429 # include <sys/cdefs.h>
430 #endif /* sgi && SYSVREL > 3 */
434 # undef MAXHOSTNAMELEN /* Busted headers? */
437 # include <netinet/in.h>
438 # include <arpa/inet.h>
439 # include <sys/socket.h>
440 # if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
441 # if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
445 # endif /* __APPLE__ */
447 # include <sys/uio.h> /* For struct iovec */
448 #endif /* REMOTEHOST */
451 /* exit normally, allowing purify to trace leaks */
464 #include "sh.types.h"
466 #if !HAVE_DECL_GETPGRP
467 # ifndef GETPGRP_VOID
468 extern pid_t
getpgrp (int);
470 extern pid_t
getpgrp (void);
475 typedef ptr_t memalign_t
;
478 char am_char
, *am_char_p
;
479 short am_short
, *am_short_p
;
480 int am_int
, *am_int_p
;
481 long am_long
, *am_long_p
;
482 float am_float
, *am_float_p
;
483 double am_double
, *am_double_p
;
486 # define malloc lint_malloc
487 # define free lint_free
488 # define realloc lint_realloc
489 # define calloc lint_calloc
493 # define xmalloc(i) smalloc(i)
494 # define xrealloc(p, i) srealloc(p, i)
495 # define xcalloc(n, s) scalloc(n, s)
498 # define xmalloc(i) malloc(i)
499 # define xrealloc(p, i) realloc(p, i)
500 # define xcalloc(n, s) calloc(n, s)
502 #endif /* SYSMALLOC */
508 #include "pathnames.h"
514 * Bill Joy, UC Berkeley
515 * October, 1978; May 1980
517 * Jim Kulp, IIASA, Laxenburg Austria
527 #endif /* REMOTEHOST */
529 #ifndef MAXHOSTNAMELEN
530 # ifdef HOST_NAME_MAX
531 # define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
532 # elif defined(SCO) && (SYSVREL > 3)
533 # include <sys/socket.h>
535 # define MAXHOSTNAMELEN 256
537 #endif /* MAXHOSTNAMELEN */
541 #define eq(a, b) (Strcmp(a, b) == 0)
543 /* globone() flags */
544 #define G_ERROR 0 /* default action: error if multiple words */
545 #define G_IGNORE 1 /* ignore the rest of the words */
546 #define G_APPEND 2 /* make a sentence by cat'ing the words */
551 EXTERN
int chkstop IZERO
; /* Warned of stopped jobs... allow exit */
553 #if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
554 # define CLOSE_ON_EXEC
556 EXTERN
int didcch IZERO
; /* Have closed unused fd's for child */
557 #endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
559 EXTERN
int didfds IZERO
; /* Have setup i/o fd's for child */
560 EXTERN
int doneinp IZERO
; /* EOF indicator after reset from readc */
561 EXTERN
int exiterr IZERO
; /* Exit if error or non-zero exit status */
562 EXTERN
int child IZERO
; /* Child shell ... errors cause exit */
563 EXTERN
int haderr IZERO
; /* Reset was because of an error */
564 EXTERN
int intty IZERO
; /* Input is a tty */
565 EXTERN
int intact IZERO
; /* We are interactive... therefore prompt */
566 EXTERN
int justpr IZERO
; /* Just print because of :p hist mod */
567 EXTERN
int loginsh IZERO
; /* We are a loginsh -> .login/.logout */
568 EXTERN
int neednote IZERO
; /* Need to pnotify() */
569 EXTERN
int noexec IZERO
; /* Don't execute, just syntax check */
570 EXTERN
int pjobs IZERO
; /* want to print jobs if interrupted */
571 EXTERN
int setintr IZERO
; /* Set interrupts on/off -> Wait intr... */
572 EXTERN
int handle_interrupt IZERO
;/* Are we currently handling an interrupt? */
573 EXTERN
int havhash IZERO
; /* path hashing is available */
574 EXTERN
int editing IZERO
; /* doing filename expansion and line editing */
575 EXTERN
int noediting IZERO
; /* initial $term defaulted to noedit */
576 EXTERN
int bslash_quote IZERO
;/* PWP: tcsh-style quoting? (in sh.c) */
577 EXTERN
int anyerror IZERO
; /* propagate errors from pipelines/backq */
578 EXTERN
int compat_expr IZERO
;/* csh-style expressions? */
579 EXTERN
int isoutatty IZERO
; /* is SHOUT a tty */
580 EXTERN
int isdiagatty IZERO
;/* is SHDIAG a tty */
581 EXTERN
int is1atty IZERO
; /* is file descriptor 1 a tty (didfds mode) */
582 EXTERN
int is2atty IZERO
; /* is file descriptor 2 a tty (didfds mode) */
583 EXTERN
int arun IZERO
; /* Currently running multi-line-aliases */
584 EXTERN
int implicit_cd IZERO
;/* implicit cd enabled?(1=enabled,2=verbose) */
585 EXTERN
int cdtohome IZERO
; /* cd without args goes home */
586 EXTERN
int inheredoc IZERO
; /* Currently parsing a heredoc */
587 EXTERN
int no_clobber IZERO
; /* no clobber enabled? 1=yes 2=notempty, 4=ask*/
588 /* We received a window change event */
589 EXTERN
volatile sig_atomic_t windowchg IZERO
;
590 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
591 EXTERN
int dspmbyte_ls
;
597 EXTERN Char
*arginp IZERO
; /* Argument input for sh -c and internal `xx` */
598 EXTERN
int onelflg IZERO
; /* 2 -> need line for -t, 1 -> exit on read */
599 extern Char
*ffile
; /* Name of shell file for $0 */
600 extern int dolzero
; /* if $?0 should return true... */
602 extern char *seterr
; /* Error message from scanner/parser */
604 extern int errno
; /* Error from C library routines */
607 /* Temp name for << shell files in /tmp, for xfree() */
608 EXTERN Char
*shtemp IZERO
;
611 EXTERN
struct timeval time0
; /* Time at which the shell started */
612 EXTERN
struct sysrusage ru0
;
615 EXTERN timeval_t time0
; /* time at which shell started */
616 EXTERN
struct process_stats ru0
;
617 # else /* _SEQUENT_ */
619 EXTERN
time_t time0
; /* time at which shell started */
621 EXTERN
clock_t time0
; /* time at which shell started */
622 EXTERN
clock_t clk_tck
;
624 EXTERN
struct tms shtimes
; /* shell and child times for process timing */
625 # endif /* _SEQUENT_ */
626 EXTERN
time_t seconds0
;
627 #endif /* BSDTIMES */
630 # define HZ 100 /* for division into seconds */
636 EXTERN pid_t mainpid
; /* pid of the main shell ($$) */
637 EXTERN Char
*doldol
; /* Character pid for $$ */
638 EXTERN pid_t backpid
; /* pid of the last background job */
642 * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
643 * cause pid's could be unsigned and that would break our -1 flag, and
644 * uid_t and gid_t are not defined in all the systems so I would have to
645 * make special cases for them. In the future...
647 EXTERN uid_t uid
, euid
; /* Invokers real and effective */
648 EXTERN gid_t gid
, egid
; /* User and group ids */
649 EXTERN pid_t opgrp
, /* Initial pgrp and tty pgrp */
650 shpgrp
, /* Pgrp of shell */
651 tpgrp
; /* Terminal process group */
652 /* If tpgrp is -1, leave tty alone! */
654 EXTERN Char
*Prompt
; /* The actual printed prompt or NULL */
655 EXTERN Char
*RPrompt
; /* Right-hand side prompt or NULL */
658 * To be able to redirect i/o for builtins easily, the shell moves the i/o
659 * descriptors it uses away from 0,1,2.
660 * Ideally these should be in units which are closed across exec's
661 * (this saves work) but for version 6, this is not usually possible.
662 * The desired initial values for these descriptors are defined in
665 EXTERN
int SHIN IZERO
; /* Current shell input (script) */
666 EXTERN
int SHOUT IZERO
; /* Shell output */
667 EXTERN
int SHDIAG IZERO
; /* Diagnostic output... shell errs go here */
668 EXTERN
int OLDSTD IZERO
; /* Old standard input (def for cmds) */
671 #if (SYSVREL == 4 && defined(_UTS)) || defined(__linux__)
673 * From: fadden@uts.amdahl.com (Andy McFadden)
674 * we need sigsetjmp for UTS4, but not UTS2.1
682 * Errors in scanning and parsing set up an error message to be printed
683 * at the end and complete. Other errors always cause a reset.
684 * Because of source commands and .cshrc we need nested error catches.
688 typedef struct { const char *f
; size_t l
; sigjmp_buf j
; } jmp_buf_t
;
689 # define tcsh_setjmp() sigsetjmp(reslab.j, 1)
690 # define tcsh_longjmp() siglongjmp(reslab.j, 1)
692 typedef struct { const char *f
; size_t l
; jmp_buf j
; } jmp_buf_t
;
693 # define tcsh_setjmp() setjmp(reslab.j)
694 # define tcsh_longjmp() longjmp(reslab.j, 1)
697 #define setexit() (reslab.f = __func__, \
698 reslab.l = __LINE__, \
701 # define _reset() xprintf("reset %s %zu\n", reslab.f, reslab.l), \
702 flush(), tcsh_longjmp()
704 # define _reset() tcsh_longjmp()
707 #define getexit(a) (void) ((a) = reslab)
708 #define resexit(a) (void) (reslab = (a))
710 #define cpybin(a, b) (void) ((a) = (b))
712 extern jmp_buf_t reslab
;
714 EXTERN Char
*gointr
; /* Label for an onintr transfer */
716 extern struct sigaction parintr
; /* Parents interrupt catch */
717 extern struct sigaction parterm
; /* Parents terminate catch */
720 * Lexical definitions.
722 * All lexical space is allocated dynamically.
723 * The eighth/sixteenth bit of characters is used to prevent recognition,
724 * and eventually stripped.
728 #ifdef WIDE_STRINGS /* Implies SHORT_STRINGS */
729 /* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
733 * By fix for handling unicode name file, 32nd bit is used.
734 * We need use '&' instead of '> or <' when comparing with INVALID_BYTE etc..
735 * Cast to uChar is not recommended,
736 * becase Char is 4bytes but uChar is 8bytes on I32LP64. */
737 # define QUOTE 0x80000000
738 # define TRIM 0x7FFFFFFF /* Mask to strip quote bit */
739 # define UNDER 0x08000000 /* Underline flag */
740 # define BOLD 0x04000000 /* Bold flag */
741 # define STANDOUT 0x02000000 /* Standout flag */
742 # define LITERAL 0x01000000 /* Literal character flag */
743 # define ATTRIBUTES 0x0F000000 /* The bits used for attributes */
744 # define INVALID_BYTE 0xF0000000 /* Invalid character on input */
746 # define CHAR 0x30FFFFFF /* Mask to mask out the character */
748 # define CHAR 0x00FFFFFF /* Mask to mask out the character */
750 #elif defined (SHORT_STRINGS)
751 # define QUOTE ((Char) 0100000)/* 16nth char bit used for 'ing */
752 # define TRIM 0073777 /* Mask to strip quote/lit bit */
753 # define UNDER 0040000 /* Underline flag */
754 # define BOLD 0020000 /* Bold flag */
755 # define STANDOUT 0010000 /* Standout flag */
756 # define LITERAL 0004000 /* Literal character flag */
757 # define ATTRIBUTES 0074000 /* The bits used for attributes */
758 # define INVALID_BYTE 0
759 # define CHAR 0000377 /* Mask to mask out the character */
761 # define QUOTE ((Char) 0200) /* Eighth char bit used for 'ing */
762 # define TRIM 0177 /* Mask to strip quote bit */
763 # define UNDER 0000000 /* No extra bits to do both */
764 # define BOLD 0000000 /* Bold flag */
765 # define STANDOUT META /* Standout flag */
766 # define LITERAL 0000000 /* Literal character flag */
767 # define ATTRIBUTES 0200 /* The bits used for attributes */
768 # define INVALID_BYTE 0
769 # define CHAR 0000177 /* Mask to mask out the character */
771 #define CHAR_DBWIDTH (LITERAL|(LITERAL-1))
773 # define MAX_UTF32 0x7FFFFFFF /* max UTF32 is U+7FFFFFFF */
775 EXTERN
int AsciiOnly
; /* If set only 7 bits expected in characters */
778 * Each level of input has a buffered input structure.
779 * There are one or more blocks of buffered input for each level,
780 * exactly one if the input is seekable and tell is available.
781 * In other cases, the shell buffers enough blocks to keep all loops
784 * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
785 * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
788 * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
789 * is no difference between byte and character offsets.
792 off_t Bfseekp
; /* Seek pointer, generally != lseek() value */
793 off_t Bfbobp
; /* Seekp of beginning of buffers */
794 off_t Bfeobp
; /* Seekp of end of buffers */
795 int Bfblocks
; /* Number of buffer blocks */
796 Char
**Bfbuf
; /* The array of buffer blocks */
798 /* Number of bytes in each character if (cantell) */
799 unsigned char Bfclens
[BUFSIZE
+ 1];
804 * This structure allows us to seek inside aliases
808 #define TCSH_I_SEEK 0 /* Invalid seek */
809 #define TCSH_A_SEEK 1 /* Alias seek */
810 #define TCSH_F_SEEK 2 /* File seek */
811 #define TCSH_E_SEEK 3 /* Eval seek */
813 off_t _f_seek
; /* A byte offset if (cantell) */
816 #define f_seek fc._f_seek
817 #define c_seek fc._c_seek
821 extern int aret
; /* Type of last char returned */
822 #define SEEKEQ(a, b) ((a)->type == (b)->type && \
823 (a)->f_seek == (b)->f_seek && \
824 (a)->a_seek == (b)->a_seek)
826 #define fseekp B.Bfseekp
827 #define fbobp B.Bfbobp
828 #define feobp B.Bfeobp
829 #define fblocks B.Bfblocks
831 #define fclens B.Bfclens
834 * The shell finds commands in loops by reseeking the input
835 * For whiles, in particular, it reseeks to the beginning of the
836 * line the while was on; hence the while placement restrictions.
838 EXTERN
struct Ain lineloc
;
840 EXTERN
int cantell
; /* Is current source tellable ? */
843 * Input lines are parsed into doubly linked circular
844 * lists of words of the following form.
848 struct wordent
*prev
;
849 struct wordent
*next
;
853 * During word building, both in the initial lexical phase and
854 * when expanding $ variable substitutions, expansion by `!' and `$'
855 * must be inhibited when reading ahead in routines which are themselves
856 * processing `!' and `$' expansion or after characters such as `\' or in
857 * quotations. The following flags are passed to the getC routines
858 * telling them which of these substitutions are appropriate for the
859 * next character to be returned.
863 #define DOALL DODOL|DOEXCL
866 * Labuf implements a general buffer for lookahead during lexical operations.
867 * Text which is to be placed in the input stream can be stuck here.
868 * We stick parsed ahead $ constructs during initial input,
869 * process id's from `$$', and modified variable values (from qualifiers
870 * during expansion in sh.dol.c) here.
872 extern struct Strbuf labuf
;
873 EXTERN
size_t lap
; /* N/A if == labuf.len, index into labuf.s otherwise */
878 * Each command is parsed to a tree of command structures and
879 * flags are set bottom up during this process, to be propagated down
880 * as needed during the semantics/exeuction pass (sh.sem.c).
883 unsigned char t_dtyp
; /* Type of node */
884 #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
885 #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
886 #define NODE_PIPE 3 /* t_dlef | t_drit */
887 #define NODE_LIST 4 /* t_dlef ; t_drit */
888 #define NODE_OR 5 /* t_dlef || t_drit */
889 #define NODE_AND 6 /* t_dlef && t_drit */
890 unsigned char t_nice
; /* Nice value */
892 unsigned char t_systype
; /* System environment */
894 unsigned long t_dflg
; /* Flags, e.g. F_AMPERSAND|... */
895 /* save these when re-doing */
897 #define F_SAVE (F_NICE|F_TIME|F_NOHUP|F_HUP)
899 #define F_SAVE (F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
901 #define F_AMPERSAND (1<<0) /* executes in background */
902 #define F_APPEND (1<<1) /* output is redirected >> */
903 #define F_PIPEIN (1<<2) /* input is a pipe */
904 #define F_PIPEOUT (1<<3) /* output is a pipe */
905 #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
906 #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
908 #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
909 #define F_OVERWRITE (1<<8) /* output was ! */
910 #define F_READ (1<<9) /* input redirection is << */
911 #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
912 #define F_NICE (1<<11) /* t_nice is meaningful */
913 #define F_NOHUP (1<<12) /* nohup this command */
914 #define F_TIME (1<<13) /* time this command */
915 #define F_BACKQ (1<<14) /* command is in `` */
916 #define F_HUP (1<<15) /* hup this command */
918 #define F_VER (1<<16) /* execute command under SYSTYPE */
921 Char
*T_dlef
; /* Input redirect word */
922 struct command
*T_dcar
; /* Left part of list/pipe */
925 Char
*T_drit
; /* Output redirect word */
926 struct command
*T_dcdr
; /* Right part of list/pipe */
928 #define t_dlef L.T_dlef
929 #define t_dcar L.T_dcar
930 #define t_drit R.T_drit
931 #define t_dcdr R.T_dcdr
932 Char
**t_dcom
; /* Command/argument vector */
933 struct command
*t_dspr
; /* Pointer to ()'d subtree */
938 * The keywords for the parser
961 * These are declared here because they want to be
962 * initialized in sh.init.c (to allow them to be made readonly)
965 #if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
966 /* Avoid hpux ansi mode spurious warnings */
967 typedef void (*bfunc_t
) ();
969 typedef void (*bfunc_t
) (Char
**, struct command
*);
970 #endif /* hpux && __STDC__ && !__GNUC__ */
972 extern const struct biltins
{
975 int minargs
, maxargs
;
979 extern struct biltins nt_bfunc
[];
980 extern int nt_nbfunc
;
981 #endif /* WINNT_NATIVE*/
991 * Structure defining the existing while/foreach loops at this
992 * source level. Loops are implemented by seeking back in the
993 * input. For foreach (fe), the word list is attached here.
995 EXTERN
struct whyle
{
996 struct Ain w_start
; /* Point to restart loop */
997 struct Ain w_end
; /* End of loop (0 if unknown) */
998 Char
**w_fe
, **w_fe0
; /* Current/initial wordlist for fe */
999 Char
*w_fename
; /* Name for fe */
1000 struct whyle
*w_next
; /* Next (more outer) loop */
1004 * Variable structure
1006 * Aliases and variables are stored in AVL balanced binary trees.
1008 EXTERN
struct varent
{
1009 Char
**vec
; /* Array of words which is the value */
1010 Char
*v_name
; /* Name of variable/alias */
1011 int v_flags
; /* Flags */
1013 #define VAR_READONLY 1
1014 #define VAR_READWRITE 2
1015 #define VAR_NOGLOB 4
1016 #define VAR_NOERROR 8
1017 #define VAR_FIRST 32
1019 struct varent
*v_link
[3]; /* The links, see below */
1020 int v_bal
; /* Balance factor */
1021 } shvhed IZERO_STRUCT
, aliases IZERO_STRUCT
;
1023 #define v_left v_link[0]
1024 #define v_right v_link[1]
1025 #define v_parent v_link[2]
1027 #define adrof(v) adrof1(v, &shvhed)
1028 #define varval(v) value1(v, &shvhed)
1031 * The following are for interfacing redo substitution in
1032 * aliases to the lexical routines.
1034 #define HIST_PURGE -500000
1035 EXTERN
struct wordent
*alhistp IZERO_STRUCT
;/* Argument list (first) */
1036 EXTERN
struct wordent
*alhistt IZERO_STRUCT
;/* Node after last in arg list */
1037 EXTERN Char
**alvec IZERO_STRUCT
,
1038 *alvecp IZERO_STRUCT
;/* The (remnants of) alias vector */
1041 * Filename/command name expansion variables
1046 # define MAXPATHLEN PATH_MAX
1048 # define MAXPATHLEN 2048
1050 #endif /* MAXPATHLEN */
1056 extern struct limits
{
1058 const char *limname
;
1060 const char *limscale
;
1062 #endif /* !HAVENOLIMIT */
1067 * Each history list entry contains an embedded wordlist
1068 * from the scanner, a number for the event, and a reference count
1069 * to aid in discarding old entries.
1071 * Essentially "invisible" entries are put on the history list
1072 * when history substitution includes modifiers, and thrown away
1073 * at the next discarding since their event numbers are very negative.
1075 EXTERN
struct Hist
{
1076 struct wordent Hlex
;
1077 int Hnum
; /* eventno when inserted into history list */
1081 struct Hist
*Hnext
, *Hprev
; /* doubly linked list */
1082 unsigned Hhash
; /* hash value of command line */
1083 } Histlist IZERO_STRUCT
;
1085 extern struct wordent paraml
; /* Current lexical word list */
1086 EXTERN
int eventno
; /* Next events number */
1087 EXTERN
int lastev
; /* Last event reference (default) */
1089 EXTERN Char HIST
; /* history invocation character */
1090 EXTERN Char HISTSUB
; /* auto-substitute character */
1091 EXTERN Char PRCH
; /* Prompt symbol for regular users */
1092 EXTERN Char PRCHROOT
; /* Prompt symbol for root */
1095 * For operating systems with single case filenames (OS/2)
1097 #ifdef CASE_INSENSITIVE
1098 # ifdef WIDE_STRINGS
1099 # define samecase(x) (towlower(x))
1101 # define samecase(x) (isupper((unsigned char)(x)) ? \
1102 tolower((unsigned char)(x)) : (x))
1105 # define samecase(x) (x)
1106 #endif /* CASE_INSENSITIVE */
1111 #ifndef SHORT_STRINGS
1112 #define Strchr(a, b) strchr(a, b)
1113 #define Strrchr(a, b) strrchr(a, b)
1114 #define Strcat(a, b) strcat(a, b)
1115 #define Strncat(a, b, c) strncat(a, b, c)
1116 #define Strcpy(a, b) strcpy(a, b)
1117 #define Strncpy(a, b, c) strncpy(a, b, c)
1118 #define Strlen(a) strlen(a)
1119 #define Strcmp(a, b) strcmp(a, b)
1120 #define Strncmp(a, b, c) strncmp(a, b, c)
1121 #define Strcasecmp(a, b) strcasecmp(a, b)
1123 #define Strspl(a, b) strspl(a, b)
1124 #define Strnsave(a, b) strnsave(a, b)
1125 #define Strsave(a) strsave(a)
1126 #define Strend(a) strend(a)
1127 #define Strstr(a, b) strstr(a, b)
1129 #define str2short(a) ((void *)(intptr_t)(a))
1130 #define blk2short(a) saveblk(a)
1131 #define short2blk(a) saveblk(a)
1132 #define short2str(a) caching_strip(a)
1134 #if defined(WIDE_STRINGS) && !defined(UTF16_STRINGS)
1135 #define Strchr(a, b) wcschr(a, b)
1136 #define Strrchr(a, b) wcsrchr(a, b)
1137 #define Strcat(a, b) wcscat(a, b)
1138 #define Strncat(a, b, c) wcsncat(a, b, c)
1139 #define Strcpy(a, b) wcscpy(a, b)
1140 #define Strncpy(a, b, c) wcsncpy(a, b, c)
1141 #define Strlen(a) wcslen(a)
1142 #define Strcmp(a, b) wcscmp(a, b)
1143 #define Strncmp(a, b, c) wcsncmp(a, b, c)
1145 #define Strchr(a, b) s_strchr(a, b)
1146 #define Strrchr(a, b) s_strrchr(a, b)
1147 #define Strcat(a, b) s_strcat(a, b)
1148 #define Strncat(a, b, c) s_strncat(a, b, c)
1149 #define Strcpy(a, b) s_strcpy(a, b)
1150 #define Strncpy(a, b, c) s_strncpy(a, b, c)
1151 #define Strlen(a) s_strlen(a)
1152 #define Strcmp(a, b) s_strcmp(a, b)
1153 #define Strncmp(a, b, c) s_strncmp(a, b, c)
1155 #define Strcasecmp(a, b) s_strcasecmp(a, b)
1157 #define Strspl(a, b) s_strspl(a, b)
1158 #define Strnsave(a, b) s_strnsave(a, b)
1159 #define Strsave(a) s_strsave(a)
1160 #define Strend(a) s_strend(a)
1161 #define Strstr(a, b) s_strstr(a, b)
1164 #define TCSH_MODIFIERS "ehlqrstuxQ"
1167 * setname is a macro to save space (see sh.err.c)
1169 EXTERN
const char *bname
;
1171 #define setname(a) (bname = (a))
1176 EXTERN Char
*Vexpath
;
1180 EXTERN Char
**evalvec
;
1183 extern struct mesg
{
1184 const char *iname
; /* name from /usr/include */
1185 const char *pname
; /* print name */
1188 /* word_chars is set by default to WORD_CHARS (or WORD_CHARS_VI) but can
1189 be overridden by the wordchars variable--if unset, reverts to
1190 WORD_CHARS (or WORD_CHARS_VI) */
1192 EXTERN Char
*word_chars
;
1194 #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
1195 #define WORD_CHARS_VI "_" /* default chars besides alnums in words */
1197 EXTERN Char
*STR_SHELLPATH
;
1200 EXTERN Char
*STR_BSHELL
;
1202 EXTERN Char
*STR_WORD_CHARS
;
1203 EXTERN Char
*STR_WORD_CHARS_VI
;
1204 EXTERN Char
**STR_environ IZERO
;
1206 extern int dont_free
; /* Tell free that we are in danger if we free */
1208 extern Char
*INVPTR
;
1209 extern Char
**INVPPTR
;
1211 extern char *progname
;
1213 extern int xlate_cr
;
1214 extern int output_raw
;
1216 extern time_t Htime
;
1218 extern int insource
;
1219 extern char linbuf
[];
1223 extern int use_fork
;
1225 extern int tellwhat
;
1226 extern int NoNLSRebind
;
1227 #if !HAVE_DECL_ENVIRON
1228 extern char **environ
;
1233 #ifndef WINNT_NATIVE
1234 # ifdef NLS_CATALOGS
1235 # ifdef HAVE_FEATURES_H
1236 # include <features.h>
1238 # ifdef HAVE_NL_LANGINFO
1239 # include <langinfo.h>
1242 # define gettxt gettxt_ds
1244 # include <nl_types.h>
1248 EXTERN nl_catd catd
;
1249 # if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
1250 # define CGETS(b, c, d) iconv_catgets(catd, b, c, d)
1252 # define CGETS(b, c, d) xcatgets(catd, b, c, d)
1254 # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
1256 # define CGETS(b, c, d) d
1257 # define CSAVS(b, c, d) d
1259 #else /* WINNT_NATIVE */
1260 # define CGETS(b, c, d) nt_cgets( b, c, d)
1261 # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
1262 #endif /* WINNT_NATIVE */
1268 #include "sh.decls.h"
1270 * Since on some machines characters are unsigned, and the signed
1271 * keyword is not universally implemented, we treat all characters
1272 * as unsigned and sign extend them where we need.
1274 #define SIGN_EXTEND_CHAR(a) (((a) & 0x80) ? ((a) | ~0x7f) : (a))
1277 * explanation for use by the "--help" option
1279 #define HELP_STRING "\
1280 -b file batch mode, read and execute commands from `file' \n\
1281 -c command run `command' from next argument \n\
1282 -d load directory stack from `~/.cshdirs' \n\
1283 -Dname[=value] define environment variable `name' to `value' (DomainOS only) \n\
1284 -e exit on any error \n\
1285 -f start faster by ignoring the start-up file \n\
1286 -F use fork() instead of vfork() when spawning (ConvexOS only) \n\
1287 -i interactive, even when input is not from a terminal \n\
1288 -l act as a login shell, must be the only option specified \n\
1289 -m load the start-up file, whether or not owned by effective user \n\
1290 -n file no execute mode, just check syntax of the following `file' \n\
1291 -q accept SIGQUIT for running under a debugger \n\
1292 -s read commands from standard input \n\
1293 -t read one line from standard input \n\
1294 -v echo commands after history substitution \n\
1295 -V like -v but including commands read from the start-up file \n\
1296 -x echo commands immediately before execution \n\
1297 -X like -x but including commands read from the start-up file \n\
1298 --help print this message and exit \n\
1299 --version print the version shell variable and exit \n\
1300 \nSee the tcsh(1) manual page for detailed information.\n"
1304 #define TEXP_IGNORE 1 /* in ignore, it means to ignore value, just parse */
1305 #define TEXP_NOGLOB 2 /* in ignore, it means not to globone */