Home | History | Annotate | Line # | Download | only in ksh
sh.h revision 1.1.1.2
      1      1.1  jtc /*
      2      1.1  jtc  * Public Domain Bourne/Korn shell
      3      1.1  jtc  */
      4      1.1  jtc 
      5      1.1  jtc /* $Id: sh.h,v 1.1.1.2 1996/10/09 15:12:52 jtc Exp $ */
      6      1.1  jtc 
      7      1.1  jtc #include "config.h"	/* system and option configuration info */
      8      1.1  jtc 
      9      1.1  jtc #ifdef HAVE_PROTOTYPES
     10      1.1  jtc # define	ARGS(args)	args	/* prototype declaration */
     11      1.1  jtc #else
     12      1.1  jtc # define	ARGS(args)	()	/* K&R declaration */
     13      1.1  jtc #endif
     14      1.1  jtc 
     15      1.1  jtc 
     16      1.1  jtc /* Start of common headers */
     17      1.1  jtc 
     18      1.1  jtc #include <stdio.h>
     19      1.1  jtc #include <sys/types.h>
     20      1.1  jtc #include <setjmp.h>
     21      1.1  jtc #ifdef HAVE_STDDEF_H
     22      1.1  jtc # include <stddef.h>
     23      1.1  jtc #endif
     24      1.1  jtc 
     25      1.1  jtc #ifdef HAVE_STDLIB_H
     26      1.1  jtc # include <stdlib.h>
     27      1.1  jtc #else
     28      1.1  jtc /* just a useful subset of what stdlib.h would have */
     29      1.1  jtc extern char * getenv  ARGS((const char *));
     30      1.1  jtc extern void * malloc  ARGS((size_t));
     31      1.1  jtc extern int    free    ARGS((void *));
     32      1.1  jtc extern int    exit    ARGS((int));
     33      1.1  jtc extern int    rand    ARGS((void));
     34      1.1  jtc extern void   srand   ARGS((unsigned int));
     35      1.1  jtc extern int    atoi    ARGS((const char *));
     36      1.1  jtc #endif /* HAVE_STDLIB_H */
     37      1.1  jtc 
     38      1.1  jtc #ifdef HAVE_UNISTD_H
     39      1.1  jtc # include <unistd.h>
     40      1.1  jtc #else
     41      1.1  jtc /* just a useful subset of what unistd.h would have */
     42      1.1  jtc extern int access ARGS((const char *, int));
     43      1.1  jtc extern int open ARGS((const char *, int, ...));
     44      1.1  jtc extern int creat ARGS((const char *, mode_t));
     45      1.1  jtc extern int read ARGS((int, char *, unsigned));
     46      1.1  jtc extern int write ARGS((int, const char *, unsigned));
     47      1.1  jtc extern off_t lseek ARGS((int, off_t, int));
     48      1.1  jtc extern int close ARGS((int));
     49      1.1  jtc extern int pipe ARGS((int []));
     50      1.1  jtc extern int dup2 ARGS((int, int));
     51      1.1  jtc extern int unlink ARGS((const char *));
     52      1.1  jtc extern int fork ARGS((void));
     53      1.1  jtc extern int execve ARGS((const char *, char * const[], char * const[]));
     54      1.1  jtc extern int chdir ARGS((const char *));
     55      1.1  jtc extern int kill ARGS((pid_t, int));
     56      1.1  jtc extern char *getcwd();	/* no ARGS here - differs on different machines */
     57      1.1  jtc extern int geteuid ARGS((void));
     58      1.1  jtc extern int readlink ARGS((const char *, char *, int));
     59      1.1  jtc extern int getegid ARGS((void));
     60      1.1  jtc extern int getpid ARGS((void));
     61      1.1  jtc extern int getppid ARGS((void));
     62      1.1  jtc extern unsigned int sleep ARGS((unsigned int));
     63      1.1  jtc extern int isatty ARGS((int));
     64      1.1  jtc # ifdef POSIX_PGRP
     65      1.1  jtc extern int getpgrp ARGS((void));
     66      1.1  jtc extern int setpgid ARGS((pid_t, pid_t));
     67      1.1  jtc # endif /* POSIX_PGRP */
     68      1.1  jtc # ifdef BSD_PGRP
     69      1.1  jtc extern int getpgrp ARGS((pid_t));
     70      1.1  jtc extern int setpgrp ARGS((pid_t, pid_t));
     71      1.1  jtc # endif /* BSD_PGRP */
     72      1.1  jtc # ifdef SYSV_PGRP
     73      1.1  jtc extern int getpgrp ARGS((void));
     74      1.1  jtc extern int setpgrp ARGS((void));
     75      1.1  jtc # endif /* SYSV_PGRP */
     76      1.1  jtc #endif /* HAVE_UNISTD_H */
     77      1.1  jtc 
     78      1.1  jtc #ifdef HAVE_STRING_H
     79      1.1  jtc # include <string.h>
     80      1.1  jtc #else
     81      1.1  jtc # include <strings.h>
     82      1.1  jtc # define strchr index
     83      1.1  jtc # define strrchr rindex
     84      1.1  jtc #endif /* HAVE_STRING_H */
     85      1.1  jtc #ifndef HAVE_STRSTR
     86      1.1  jtc char *strstr ARGS((const char *s, const char *p));
     87      1.1  jtc #endif /* HAVE_STRSTR */
     88      1.1  jtc #ifndef HAVE_STRCASECMP
     89      1.1  jtc int strcasecmp ARGS((const char *s1, const char *s2));
     90      1.1  jtc int strncasecmp ARGS((const char *s1, const char *s2, int n));
     91      1.1  jtc #endif /* HAVE_STRCASECMP */
     92      1.1  jtc 
     93      1.1  jtc #ifdef HAVE_MEMORY_H
     94      1.1  jtc # include <memory.h>
     95      1.1  jtc #endif
     96      1.1  jtc #ifndef HAVE_MEMSET
     97      1.1  jtc # define memcpy(d, s, n)	bcopy(s, d, n)
     98      1.1  jtc # define memcmp(s1, s2, n)	bcmp(s1, s2, n)
     99      1.1  jtc void *memset ARGS((void *d, int c, size_t n));
    100      1.1  jtc #endif /* HAVE_MEMSET */
    101      1.1  jtc #ifndef HAVE_MEMMOVE
    102      1.1  jtc # ifdef HAVE_BCOPY
    103      1.1  jtc #  define memmove(d, s, n)	bcopy(s, d, n)
    104      1.1  jtc # else
    105      1.1  jtc void *memmove ARGS((void *d, const void *s, size_t n));
    106      1.1  jtc # endif
    107      1.1  jtc #endif /* HAVE_MEMMOVE */
    108      1.1  jtc 
    109      1.1  jtc #ifdef HAVE_PROTOTYPES
    110      1.1  jtc # include <stdarg.h>
    111      1.1  jtc # define SH_VA_START(va, argn) va_start(va, argn)
    112      1.1  jtc #else
    113      1.1  jtc # include <varargs.h>
    114      1.1  jtc # define SH_VA_START(va, argn) va_start(va)
    115      1.1  jtc #endif /* HAVE_PROTOTYPES */
    116      1.1  jtc 
    117      1.1  jtc #include <errno.h>
    118      1.1  jtc extern int errno;
    119      1.1  jtc 
    120      1.1  jtc #ifdef HAVE_FCNTL_H
    121      1.1  jtc # include <fcntl.h>
    122      1.1  jtc #else
    123      1.1  jtc # include <sys/file.h>
    124      1.1  jtc #endif /* HAVE_FCNTL_H */
    125      1.1  jtc #ifndef O_ACCMODE
    126      1.1  jtc # define O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
    127      1.1  jtc #endif /* !O_ACCMODE */
    128      1.1  jtc 
    129      1.1  jtc #ifndef F_OK 	/* access() arguments */
    130      1.1  jtc # define F_OK 0
    131      1.1  jtc # define X_OK 1
    132      1.1  jtc # define W_OK 2
    133      1.1  jtc # define R_OK 4
    134      1.1  jtc #endif /* !F_OK */
    135      1.1  jtc 
    136      1.1  jtc #ifndef SEEK_SET
    137      1.1  jtc # ifdef L_SET
    138      1.1  jtc #  define SEEK_SET L_SET
    139      1.1  jtc #  define SEEK_CUR L_INCR
    140      1.1  jtc #  define SEEK_END L_XTND
    141      1.1  jtc # else /* L_SET */
    142      1.1  jtc #  define SEEK_SET 0
    143      1.1  jtc #  define SEEK_CUR 1
    144      1.1  jtc #  define SEEK_END 2
    145      1.1  jtc # endif /* L_SET */
    146      1.1  jtc #endif /* !SEEK_SET */
    147      1.1  jtc 
    148      1.1  jtc /* Some machines (eg, FreeBSD 1.1.5) define CLK_TCK in limits.h
    149      1.1  jtc  * (ksh_limval.h assumes limits has been included, if available)
    150      1.1  jtc  */
    151      1.1  jtc #ifdef HAVE_LIMITS_H
    152      1.1  jtc # include <limits.h>
    153      1.1  jtc #endif /* HAVE_LIMITS_H */
    154      1.1  jtc 
    155      1.1  jtc #include <signal.h>
    156      1.1  jtc #ifdef	NSIG
    157      1.1  jtc # define SIGNALS	NSIG
    158      1.1  jtc #else
    159      1.1  jtc # ifdef	_MINIX
    160      1.1  jtc #  define SIGNALS	(_NSIG+1) /* _NSIG is # of signals used, excluding 0. */
    161      1.1  jtc # else
    162      1.1  jtc #  ifdef _SIGMAX	/* QNX */
    163      1.1  jtc #   define SIGNALS	_SIGMAX
    164      1.1  jtc #  else /* _SIGMAX */
    165      1.1  jtc #   define SIGNALS	32
    166      1.1  jtc #  endif /* _SIGMAX */
    167      1.1  jtc # endif	/* _MINIX */
    168      1.1  jtc #endif	/* NSIG */
    169      1.1  jtc #ifndef SIGCHLD
    170      1.1  jtc # define SIGCHLD SIGCLD
    171      1.1  jtc #endif
    172      1.1  jtc /* struct sigaction.sa_flags is set to KSH_SA_FLAGS.  Used to ensure
    173      1.1  jtc  * system calls are interrupted
    174      1.1  jtc  */
    175      1.1  jtc #ifdef SA_INTERRUPT
    176      1.1  jtc # define KSH_SA_FLAGS	SA_INTERRUPT
    177      1.1  jtc #else /* SA_INTERRUPT */
    178      1.1  jtc # define KSH_SA_FLAGS	0
    179      1.1  jtc #endif /* SA_INTERRUPT */
    180      1.1  jtc 
    181      1.1  jtc typedef	RETSIGTYPE (*handler_t) ARGS((int));	/* signal handler */
    182      1.1  jtc 
    183      1.1  jtc #ifdef USE_FAKE_SIGACT
    184      1.1  jtc # include "sigact.h"			/* use sjg's fake sigaction() */
    185      1.1  jtc #endif
    186      1.1  jtc 
    187      1.1  jtc #ifdef HAVE_PATHS_H
    188      1.1  jtc # include <paths.h>
    189      1.1  jtc #endif /* HAVE_PATHS_H */
    190      1.1  jtc #ifdef _PATH_DEFPATH
    191      1.1  jtc # define DEFAULT__PATH _PATH_DEFPATH
    192      1.1  jtc #else /* _PATH_DEFPATH */
    193      1.1  jtc # define DEFAULT__PATH DEFAULT_PATH
    194      1.1  jtc #endif /* _PATH_DEFPATH */
    195      1.1  jtc 
    196      1.1  jtc #ifndef offsetof
    197      1.1  jtc # define offsetof(type,id) ((size_t)&((type*)NULL)->id)
    198      1.1  jtc #endif
    199      1.1  jtc 
    200      1.1  jtc #ifndef HAVE_KILLPG
    201      1.1  jtc # define killpg(p, s)	kill(-(p), (s))
    202      1.1  jtc #endif /* !HAVE_KILLPG */
    203      1.1  jtc 
    204      1.1  jtc /* Special cases for execve(2) */
    205      1.1  jtc #ifdef OS2
    206      1.1  jtc extern int ksh_execve(char *cmd, char **args, char **env);
    207      1.1  jtc #else /* OS2 */
    208      1.1  jtc # if defined(OS_ISC) && defined(_POSIX_SOURCE)
    209      1.1  jtc /* Kludge for ISC 3.2 (and other versions?) so programs will run correctly.  */
    210      1.1  jtc #  define ksh_execve(p, av, ev) do { \
    211      1.1  jtc 					__setostype(0); \
    212      1.1  jtc 					execve(p, av, ev); \
    213      1.1  jtc 					__setostype(1); \
    214      1.1  jtc 				} while (0)
    215      1.1  jtc # else /* OS_ISC && _POSIX */
    216      1.1  jtc #  define ksh_execve(p, av, ev)	execve(p, av, ev)
    217      1.1  jtc # endif /* OS_ISC && _POSIX */
    218      1.1  jtc #endif /* OS2 */
    219      1.1  jtc 
    220      1.1  jtc /* this is a hang-over from older versions of the os2 port */
    221      1.1  jtc #define ksh_dupbase(fd, base) fcntl(fd, F_DUPFD, base)
    222      1.1  jtc 
    223      1.1  jtc #ifdef HAVE_SIGSETJMP
    224      1.1  jtc # define ksh_sigsetjmp(env,sm)	sigsetjmp((env), (sm))
    225      1.1  jtc # define ksh_siglongjmp(env,v)	siglongjmp((env), (v))
    226      1.1  jtc # define ksh_jmp_buf		sigjmp_buf
    227      1.1  jtc #else /* HAVE_SIGSETJMP */
    228      1.1  jtc # ifdef HAVE__SETJMP
    229      1.1  jtc #  define ksh_sigsetjmp(env,sm)	_setjmp(env)
    230      1.1  jtc #  define ksh_siglongjmp(env,v)	_longjmp((env), (v))
    231      1.1  jtc # else /* HAVE__SETJMP */
    232      1.1  jtc #  define ksh_sigsetjmp(env,sm)	setjmp(env)
    233      1.1  jtc #  define ksh_siglongjmp(env,v)	longjmp((env), (v))
    234      1.1  jtc # endif /* HAVE__SETJMP */
    235      1.1  jtc # define ksh_jmp_buf		jmp_buf
    236      1.1  jtc #endif /* HAVE_SIGSETJMP */
    237      1.1  jtc 
    238      1.1  jtc /* Find a integer type that is at least 32 bits (or die) - SIZEOF_* defined
    239      1.1  jtc  * by autoconf (assumes an 8 bit byte, but I'm not concerned)
    240      1.1  jtc  */
    241      1.1  jtc #if SIZEOF_INT >= 4
    242      1.1  jtc # define INT32	int
    243      1.1  jtc #else /* SIZEOF_INT */
    244      1.1  jtc # if SIZEOF_LONG >= 4
    245      1.1  jtc #  define INT32	long
    246      1.1  jtc # else /* SIZEOF_LONG */
    247      1.1  jtc    #error cannot find 32 bit type...
    248      1.1  jtc # endif /* SIZEOF_LONG */
    249      1.1  jtc #endif /* SIZEOF_INT */
    250      1.1  jtc 
    251      1.1  jtc /* end of common headers */
    252      1.1  jtc 
    253      1.1  jtc /* Stop gcc and lint from complaining about possibly uninitialized variables */
    254      1.1  jtc #if defined(__GNUC__) || defined(lint)
    255      1.1  jtc # define UNINITIALIZED(var)	var = 0
    256      1.1  jtc #else
    257      1.1  jtc # define UNINITIALIZED(var)	var
    258      1.1  jtc #endif /* GNUC || lint */
    259      1.1  jtc 
    260      1.1  jtc /* some useful #defines */
    261      1.1  jtc #ifdef EXTERN
    262      1.1  jtc # define I__(i) = i
    263      1.1  jtc #else
    264      1.1  jtc # define I__(i)
    265      1.1  jtc # define EXTERN extern
    266      1.1  jtc # define EXTERN_DEFINED
    267      1.1  jtc #endif
    268      1.1  jtc 
    269      1.1  jtc #ifndef EXECSHELL
    270      1.1  jtc /* shell to exec scripts (see also $SHELL initialization in main.c) */
    271      1.1  jtc # ifdef OS2
    272      1.1  jtc #  define EXECSHELL	"c:\\os2\\cmd.exe"
    273      1.1  jtc #  define EXECSHELL_STR	"OS2_SHELL"
    274      1.1  jtc # else /* OS2 */
    275      1.1  jtc #  define EXECSHELL	"/bin/sh"
    276      1.1  jtc #  define EXECSHELL_STR	"EXECSHELL"
    277      1.1  jtc # endif /* OS2 */
    278      1.1  jtc #endif
    279      1.1  jtc 
    280      1.1  jtc /* ISABSPATH() means path is fully and completely specified,
    281      1.1  jtc  * ISROOTEDPATH() means a .. as the first component is a no-op,
    282      1.1  jtc  * ISRELPATH() means $PWD can be tacked on to get an absolute path.
    283      1.1  jtc  *
    284      1.1  jtc  * OS	Path		ISABSPATH	ISROOTEDPATH	ISRELPATH
    285      1.1  jtc  * unix	/foo		yes		yes		no
    286      1.1  jtc  * unix	foo		no		no		yes
    287      1.1  jtc  * unix	../foo		no		no		yes
    288      1.1  jtc  * os2	a:/foo		yes		yes		no
    289      1.1  jtc  * os2	a:foo		no		no		no
    290      1.1  jtc  * os2	/foo		no		yes		no
    291      1.1  jtc  * os2	foo		no		no		yes
    292      1.1  jtc  * os2	../foo		no		no		yes
    293      1.1  jtc  */
    294      1.1  jtc #ifdef OS2
    295      1.1  jtc # define PATHSEP        ';'
    296      1.1  jtc # define DIRSEP         '/'	/* even though \ is native */
    297      1.1  jtc # define DIRSEPSTR      "\\"
    298      1.1  jtc # define ISDIRSEP(c)    ((c) == '\\' || (c) == '/')
    299      1.1  jtc # define ISABSPATH(s)	(((s)[0] && (s)[1] == ':' && ISDIRSEP((s)[2])))
    300      1.1  jtc # define ISROOTEDPATH(s) (ISDIRSEP((s)[0]) || ISABSPATH(s))
    301      1.1  jtc # define ISRELPATH(s)	(!(s)[0] || ((s)[1] != ':' && !ISDIRSEP((s)[0])))
    302      1.1  jtc # define FILECHCONV(c)	(isascii(c) && isupper(c) ? tolower(c) : c)
    303      1.1  jtc # define FILECMP(s1, s2) stricmp(s1, s2)
    304      1.1  jtc # define FILENCMP(s1, s2, n) strnicmp(s1, s2, n)
    305      1.1  jtc extern char *ksh_strchr_dirsep(const char *path);
    306      1.1  jtc extern char *ksh_strrchr_dirsep(const char *path);
    307      1.1  jtc # define chdir          _chdir2
    308      1.1  jtc # define getcwd         _getcwd2
    309      1.1  jtc #else
    310      1.1  jtc # define PATHSEP        ':'
    311      1.1  jtc # define DIRSEP         '/'
    312      1.1  jtc # define DIRSEPSTR      "/"
    313      1.1  jtc # define ISDIRSEP(c)    ((c) == '/')
    314      1.1  jtc # define ISABSPATH(s)	ISDIRSEP((s)[0])
    315      1.1  jtc # define ISROOTEDPATH(s) ISABSPATH(s)
    316      1.1  jtc # define ISRELPATH(s)	(!ISABSPATH(s))
    317      1.1  jtc # define FILECHCONV(c)	c
    318      1.1  jtc # define FILECMP(s1, s2) strcmp(s1, s2)
    319      1.1  jtc # define FILENCMP(s1, s2, n) strncmp(s1, s2, n)
    320      1.1  jtc # define ksh_strchr_dirsep(p)   strchr(p, DIRSEP)
    321      1.1  jtc # define ksh_strrchr_dirsep(p)  strrchr(p, DIRSEP)
    322      1.1  jtc #endif
    323      1.1  jtc 
    324      1.1  jtc typedef int bool_t;
    325      1.1  jtc #define	FALSE	0
    326      1.1  jtc #define	TRUE	1
    327      1.1  jtc 
    328      1.1  jtc #define	NELEM(a) (sizeof(a) / sizeof((a)[0]))
    329      1.1  jtc #define	sizeofN(type, n) (sizeof(type) * (n))
    330      1.1  jtc #define	BIT(i)	(1<<(i))	/* define bit in flag */
    331      1.1  jtc 
    332      1.1  jtc /* Table flag type - needs > 16 and < 32 bits */
    333      1.1  jtc typedef INT32 Tflag;
    334      1.1  jtc 
    335      1.1  jtc #define	NUFILE	10		/* Number of user-accessible files */
    336      1.1  jtc #define	FDBASE	10		/* First file usable by Shell */
    337      1.1  jtc 
    338      1.1  jtc /* you're not going to run setuid shell scripts, are you? */
    339      1.1  jtc #define	eaccess(path, mode)	access(path, mode)
    340      1.1  jtc 
    341      1.1  jtc /* Make MAGIC a char that might be printed to make bugs more obvious, but
    342      1.1  jtc  * not a char that is used often.  Also, can't use the high bit as it causes
    343      1.1  jtc  * portability problems (calling strchr(x, 0x80|'x') is error prone).
    344      1.1  jtc  */
    345      1.1  jtc #define	MAGIC		(7)/* prefix for *?[!{,} during expand */
    346      1.1  jtc #define ISMAGIC(c)	((unsigned char)(c) == MAGIC)
    347      1.1  jtc #define	NOT		'!'	/* might use ^ (ie, [!...] vs [^..]) */
    348      1.1  jtc 
    349      1.1  jtc #define	LINE	1024		/* input line size */
    350      1.1  jtc #define	PATH	1024		/* pathname size (todo: PATH_MAX/pathconf()) */
    351      1.1  jtc #define ARRAYMAX 1023		/* max array index */
    352      1.1  jtc 
    353      1.1  jtc EXTERN	const char *kshname;	/* $0 */
    354      1.1  jtc EXTERN	pid_t	kshpid;		/* $$, shell pid */
    355      1.1  jtc EXTERN	pid_t	procpid;	/* pid of executing process */
    356      1.1  jtc EXTERN	int	exstat;		/* exit status */
    357      1.1  jtc EXTERN	int	subst_exstat;	/* exit status of last $(..)/`..` */
    358      1.1  jtc EXTERN	const char *safe_prompt; /* safe prompt if PS1 substitution fails */
    359      1.1  jtc 
    360      1.1  jtc 
    361      1.1  jtc /*
    362      1.1  jtc  * Area-based allocation built on malloc/free
    363      1.1  jtc  */
    364      1.1  jtc 
    365      1.1  jtc typedef struct Area {
    366      1.1  jtc 	struct Block *freelist;	/* free list */
    367      1.1  jtc } Area;
    368      1.1  jtc 
    369      1.1  jtc EXTERN	Area	aperm;		/* permanent object space */
    370      1.1  jtc #define	APERM	&aperm
    371      1.1  jtc #define	ATEMP	&e->area
    372      1.1  jtc 
    373      1.1  jtc #ifdef MEM_DEBUG
    374      1.1  jtc # include "chmem.h" /* a debugging front end for malloc et. al. */
    375      1.1  jtc #endif /* MEM_DEBUG */
    376      1.1  jtc 
    377      1.1  jtc 
    378      1.1  jtc /*
    379      1.1  jtc  * parsing & execution environment
    380      1.1  jtc  */
    381      1.1  jtc EXTERN	struct env {
    382      1.1  jtc 	short	type;			/* enviroment type - see below */
    383      1.1  jtc 	short	flags;			/* EF_* */
    384      1.1  jtc 	Area	area;			/* temporary allocation area */
    385      1.1  jtc 	struct	block *loc;		/* local variables and functions */
    386      1.1  jtc 	short  *savefd;			/* original redirected fd's */
    387      1.1  jtc 	struct	env *oenv;		/* link to previous enviroment */
    388      1.1  jtc 	ksh_jmp_buf jbuf;		/* long jump back to env creator */
    389      1.1  jtc 	struct temp *temps;		/* temp files */
    390      1.1  jtc } *e;
    391      1.1  jtc 
    392      1.1  jtc /* struct env.type values */
    393      1.1  jtc #define	E_NONE	0		/* dummy enviroment */
    394      1.1  jtc #define	E_PARSE	1		/* parsing command # */
    395      1.1  jtc #define	E_FUNC	2		/* executing function # */
    396      1.1  jtc #define	E_INCL	3		/* including a file via . # */
    397      1.1  jtc #define	E_EXEC	4		/* executing command tree */
    398      1.1  jtc #define	E_LOOP	5		/* executing for/while # */
    399      1.1  jtc #define	E_ERRH	6		/* general error handler # */
    400      1.1  jtc /* # indicates env has valid jbuf (see unwind()) */
    401      1.1  jtc 
    402      1.1  jtc /* struct env.flag values */
    403      1.1  jtc #define EF_FUNC_PARSE	BIT(0)	/* function being parsed */
    404      1.1  jtc #define EF_BRKCONT_PASS	BIT(1)	/* set if E_LOOP must pass break/continue on */
    405      1.1  jtc 
    406      1.1  jtc /* Do breaks/continues stop at env type e? */
    407      1.1  jtc #define STOP_BRKCONT(t)	((t) == E_NONE || (t) == E_PARSE \
    408      1.1  jtc 			 || (t) == E_FUNC || (t) == E_INCL)
    409      1.1  jtc /* Do returns stop at env type e? */
    410      1.1  jtc #define STOP_RETURN(t)	((t) == E_FUNC || (t) == E_INCL)
    411      1.1  jtc 
    412      1.1  jtc /* values for ksh_siglongjmp(e->jbuf, 0) */
    413      1.1  jtc #define LRETURN	1		/* return statement */
    414      1.1  jtc #define	LEXIT	2		/* exit statement */
    415      1.1  jtc #define LERROR	3		/* errorf() called */
    416      1.1  jtc #define LLEAVE	4		/* untrappable exit/error */
    417      1.1  jtc #define LINTR	5		/* ^C noticed */
    418      1.1  jtc #define	LBREAK	6		/* break statement */
    419      1.1  jtc #define	LCONTIN	7		/* continue statement */
    420      1.1  jtc #define LSHELL	8		/* return to interactive shell() */
    421      1.1  jtc #define LAEXPR	9		/* error in arithmetic expression */
    422      1.1  jtc 
    423      1.1  jtc 
    424      1.1  jtc /* option processing */
    425      1.1  jtc #define OF_CMDLINE	0x01	/* command line */
    426      1.1  jtc #define OF_SET		0x02	/* set builtin */
    427      1.1  jtc #define OF_SPECIAL	0x04	/* a special variable changing */
    428      1.1  jtc #define OF_ANY		(OF_CMDLINE | OF_SET | OF_SPECIAL)
    429      1.1  jtc 
    430      1.1  jtc struct option {
    431      1.1  jtc     const char	*name;	/* long name of option */
    432      1.1  jtc     char	c;	/* character flag (if any) */
    433      1.1  jtc     short	flags;	/* OF_* */
    434      1.1  jtc };
    435      1.1  jtc extern const struct option options[];
    436      1.1  jtc 
    437      1.1  jtc /*
    438      1.1  jtc  * flags (the order of these enums MUST match the order in misc.c(options[]))
    439      1.1  jtc  */
    440      1.1  jtc enum sh_flag {
    441      1.1  jtc 	FEXPORT = 0,	/* -a: export all */
    442      1.1  jtc #ifdef BRACE_EXPAND
    443      1.1  jtc 	FBRACEEXPAND,	/* enable {} globbing */
    444      1.1  jtc #endif
    445      1.1  jtc 	FBGNICE,	/* bgnice */
    446      1.1  jtc 	FCOMMAND,	/* -c: (invocation) execute specified command */
    447      1.1  jtc #ifdef EMACS
    448      1.1  jtc 	FEMACS,		/* emacs command editing */
    449      1.1  jtc #endif
    450      1.1  jtc 	FERREXIT,	/* -e: quit on error */
    451      1.1  jtc #ifdef EMACS
    452      1.1  jtc 	FGMACS,		/* gmacs command editing */
    453      1.1  jtc #endif
    454      1.1  jtc 	FIGNOREEOF,	/* eof does not exit */
    455      1.1  jtc 	FTALKING,	/* -i: interactive */
    456      1.1  jtc 	FKEYWORD,	/* -k: name=value anywere */
    457      1.1  jtc 	FLOGIN,		/* -l: a login shell */
    458      1.1  jtc 	FMARKDIRS,	/* mark dirs with / in file name completion */
    459      1.1  jtc 	FMONITOR,	/* -m: job control monitoring */
    460      1.1  jtc 	FNOCLOBBER,	/* -C: don't overwrite existing files */
    461      1.1  jtc 	FNOEXEC,	/* -n: don't execute any commands */
    462      1.1  jtc 	FNOGLOB,	/* -f: don't do file globbing */
    463      1.1  jtc 	FNOHUP,		/* -H: don't kill running jobs when login shell exits */
    464      1.1  jtc 	FNOLOG,		/* don't save functions in history (ignored) */
    465      1.1  jtc #ifdef	JOBS
    466      1.1  jtc 	FNOTIFY,	/* -b: asynchronous job completion notification */
    467      1.1  jtc #endif
    468      1.1  jtc 	FNOUNSET,	/* -u: using an unset var is an error */
    469      1.1  jtc 	FPHYSICAL,	/* -o physical: don't do logical cd's/pwd's */
    470      1.1  jtc 	FPOSIX,		/* -o posix: be posixly correct */
    471      1.1  jtc 	FPRIVILEGED,	/* -p: use suid_profile */
    472      1.1  jtc 	FRESTRICTED,	/* -r: restricted shell */
    473      1.1  jtc 	FSTDIN,		/* -s: (invocation) parse stdin */
    474      1.1  jtc 	FTRACKALL,	/* -h: create tracked aliases for all commands */
    475      1.1  jtc 	FVERBOSE,	/* -v: echo input */
    476      1.1  jtc #ifdef VI
    477      1.1  jtc 	FVI,		/* vi command editing */
    478      1.1  jtc 	FVIRAW,		/* always read in raw mode (ignored) */
    479      1.1  jtc 	FVISHOW8,	/* display chars with 8th bit set as is (versus M-) */
    480      1.1  jtc 	FVITABCOMPLETE,	/* enable tab as file name completion char */
    481      1.1  jtc 	FVIESCCOMPLETE,	/* enable ESC as file name completion in command mode */
    482      1.1  jtc #endif
    483      1.1  jtc 	FXTRACE,	/* -x: execution trace */
    484      1.1  jtc 	FNFLAGS /* (place holder: how many flags are there) */
    485      1.1  jtc };
    486      1.1  jtc 
    487      1.1  jtc #define Flag(f)	(shell_flags[(int) (f)])
    488      1.1  jtc 
    489      1.1  jtc EXTERN	char shell_flags [FNFLAGS];
    490      1.1  jtc 
    491      1.1  jtc EXTERN	char	null [] I__("");	/* null value for variable */
    492      1.1  jtc EXTERN	char	space [] I__(" ");
    493      1.1  jtc EXTERN	char	newline [] I__("\n");
    494      1.1  jtc EXTERN	char	slash [] I__("/");
    495      1.1  jtc 
    496      1.1  jtc /* temp/here files. the file is removed when the struct is freed */
    497      1.1  jtc struct temp {
    498      1.1  jtc 	struct temp	*next;
    499      1.1  jtc 	struct shf	*shf;
    500      1.1  jtc 	int		pid;		/* pid of process parsed here-doc */
    501      1.1  jtc 	char		*name;
    502      1.1  jtc };
    503      1.1  jtc 
    504      1.1  jtc /* here documents in functions are treated specially (the get removed when
    505      1.1  jtc  * shell exis) */
    506      1.1  jtc EXTERN struct temp	*func_heredocs;
    507      1.1  jtc 
    508      1.1  jtc /*
    509      1.1  jtc  * stdio and our IO routines
    510      1.1  jtc  */
    511      1.1  jtc 
    512      1.1  jtc #define shl_spare	(&shf_iob[0])	/* for c_read()/c_print() */
    513      1.1  jtc #define shl_stdout	(&shf_iob[1])
    514      1.1  jtc #define shl_out		(&shf_iob[2])
    515      1.1  jtc EXTERN int shl_stdout_ok;
    516      1.1  jtc 
    517      1.1  jtc /*
    518      1.1  jtc  * trap handlers
    519      1.1  jtc  */
    520      1.1  jtc typedef struct trap {
    521      1.1  jtc 	int	signal;		/* signal number */
    522      1.1  jtc 	const char *name;	/* short name */
    523      1.1  jtc 	const char *mess;	/* descriptive name */
    524      1.1  jtc 	char   *trap;		/* trap command */
    525      1.1  jtc 	int	volatile set;	/* trap pending */
    526      1.1  jtc 	int	flags;		/* TF_* */
    527      1.1  jtc 	handler_t cursig;	/* current handler (valid if TF_ORIG_* set) */
    528      1.1  jtc 	handler_t shtrap;	/* shell signal handler */
    529      1.1  jtc } Trap;
    530      1.1  jtc 
    531      1.1  jtc /* values for Trap.flags */
    532      1.1  jtc #define TF_SHELL_USES	BIT(0)	/* shell uses signal, user can't change */
    533      1.1  jtc #define TF_USER_SET	BIT(1)	/* user has (tried to) set trap */
    534      1.1  jtc #define TF_ORIG_IGN	BIT(2)	/* original action was SIG_IGN */
    535      1.1  jtc #define TF_ORIG_DFL	BIT(3)	/* original action was SIG_DFL */
    536      1.1  jtc #define TF_EXEC_IGN	BIT(4)	/* restore SIG_IGN just before exec */
    537      1.1  jtc #define TF_EXEC_DFL	BIT(5)	/* restore SIG_DFL just before exec */
    538      1.1  jtc #define TF_DFL_INTR	BIT(6)	/* when received, default action is LINTR */
    539      1.1  jtc #define TF_TTY_INTR	BIT(7)	/* tty generated signal (see j_waitj) */
    540      1.1  jtc #define TF_CHANGED	BIT(8)	/* used by runtrap() to detect trap changes */
    541      1.1  jtc #define TF_FATAL	BIT(9)	/* causes termination if not trapped */
    542      1.1  jtc 
    543      1.1  jtc /* values for setsig()/setexecsig() flags argument */
    544      1.1  jtc #define SS_RESTORE_MASK	0x3	/* how to restore a signal before an exec() */
    545      1.1  jtc #define SS_RESTORE_CURR	0	/* leave current handler in place */
    546      1.1  jtc #define SS_RESTORE_ORIG	1	/* restore original handler */
    547      1.1  jtc #define SS_RESTORE_DFL	2	/* restore to SIG_DFL */
    548      1.1  jtc #define SS_RESTORE_IGN	3	/* restore to SIG_IGN */
    549      1.1  jtc #define SS_FORCE	BIT(3)	/* set signal even if original signal ignored */
    550      1.1  jtc #define SS_USER		BIT(4)	/* user is doing the set (ie, trap command) */
    551      1.1  jtc #define SS_SHTRAP	BIT(5)	/* trap for internal use (CHLD,ALRM,WINCH) */
    552      1.1  jtc 
    553      1.1  jtc #define SIGEXIT_	0	/* for trap EXIT */
    554      1.1  jtc #define SIGERR_		SIGNALS	/* for trap ERR */
    555      1.1  jtc 
    556      1.1  jtc EXTERN	int volatile trap;	/* traps pending? */
    557      1.1  jtc EXTERN	int volatile intrsig;	/* pending trap interrupts executing command */
    558      1.1  jtc EXTERN	int volatile fatal_trap;/* received a fatal signal */
    559      1.1  jtc #ifndef FROM_TRAP_C
    560      1.1  jtc /* Kludge to avoid bogus re-declaration of sigtraps[] error on AIX 3.2.5 */
    561      1.1  jtc extern	Trap	sigtraps[SIGNALS+1];
    562      1.1  jtc #endif /* !FROM_TRAP_C */
    563      1.1  jtc 
    564      1.1  jtc 
    565      1.1  jtc #ifdef KSH
    566      1.1  jtc /*
    567      1.1  jtc  * TMOUT support
    568      1.1  jtc  */
    569      1.1  jtc /* values for ksh_tmout_state */
    570      1.1  jtc enum tmout_enum {
    571      1.1  jtc 		TMOUT_EXECUTING	= 0,	/* executing commands */
    572      1.1  jtc 		TMOUT_READING,		/* waiting for input */
    573      1.1  jtc 		TMOUT_LEAVING		/* have timed out */
    574      1.1  jtc 	};
    575      1.1  jtc EXTERN unsigned int ksh_tmout;
    576      1.1  jtc EXTERN enum tmout_enum ksh_tmout_state I__(TMOUT_EXECUTING);
    577      1.1  jtc #endif /* KSH */
    578      1.1  jtc 
    579      1.1  jtc 
    580      1.1  jtc /* For "You have stopped jobs" message */
    581      1.1  jtc EXTERN int really_exit;
    582      1.1  jtc 
    583      1.1  jtc 
    584      1.1  jtc /*
    585      1.1  jtc  * fast character classes
    586      1.1  jtc  */
    587      1.1  jtc #define	C_ALPHA	 BIT(0)		/* a-z_A-Z */
    588      1.1  jtc #define	C_DIGIT	 BIT(1)		/* 0-9 */
    589      1.1  jtc #define	C_LEX1	 BIT(2)		/* \0 \t\n|&;<>() */
    590      1.1  jtc #define	C_VAR1	 BIT(3)		/* *@#!$-? */
    591      1.1  jtc #define	C_IFSWS	 BIT(4)		/* \t \n (IFS white space) */
    592      1.1  jtc #define	C_SUBOP1 BIT(5)		/* "=-+?" */
    593      1.1  jtc #define	C_SUBOP2 BIT(6)		/* "#%" */
    594      1.1  jtc #define	C_IFS	 BIT(7)		/* $IFS */
    595      1.1  jtc #define	C_QUOTE	 BIT(8)		/*  \n\t"#$&'()*;<>?[\`| (needing quoting) */
    596      1.1  jtc 
    597      1.1  jtc extern	short ctypes [];
    598      1.1  jtc 
    599      1.1  jtc #define	ctype(c, t)	!!(ctypes[(unsigned char)(c)]&(t))
    600      1.1  jtc #define	letter(c)	ctype(c, C_ALPHA)
    601      1.1  jtc #define	digit(c)	ctype(c, C_DIGIT)
    602      1.1  jtc #define	letnum(c)	ctype(c, C_ALPHA|C_DIGIT)
    603      1.1  jtc 
    604      1.1  jtc EXTERN int ifs0 I__(' ');	/* for "$*" */
    605      1.1  jtc 
    606      1.1  jtc 
    607      1.1  jtc /* Argument parsing for built-in commands and getopts command */
    608      1.1  jtc 
    609      1.1  jtc /* Values for Getopt.flags */
    610      1.1  jtc #define GF_ERROR	BIT(0)	/* call errorf() if there is an error */
    611      1.1  jtc #define GF_PLUSOPT	BIT(1)	/* allow +c as an option */
    612      1.1  jtc #define GF_NONAME	BIT(2)	/* don't print argv[0] in errors */
    613      1.1  jtc 
    614      1.1  jtc /* Values for Getopt.info */
    615      1.1  jtc #define GI_MINUS	BIT(0)	/* an option started with -... */
    616      1.1  jtc #define GI_PLUS		BIT(1)	/* an option started with +... */
    617      1.1  jtc #define GI_MINUSMINUS	BIT(2)	/* arguments were ended with -- */
    618      1.1  jtc 
    619      1.1  jtc typedef struct {
    620      1.1  jtc 	int		optind;
    621      1.1  jtc 	char		*optarg;
    622      1.1  jtc 	int		flags;	/* see GF_* */
    623      1.1  jtc 	int		info;	/* see GI_* */
    624      1.1  jtc 	unsigned int	p;	/* 0 or index into argv[optind - 1] */
    625      1.1  jtc 	char		buf[2];	/* for bad option OPTARG value */
    626      1.1  jtc } Getopt;
    627      1.1  jtc 
    628      1.1  jtc EXTERN Getopt builtin_opt;	/* for shell builtin commands */
    629      1.1  jtc 
    630      1.1  jtc 
    631      1.1  jtc #ifdef KSH
    632      1.1  jtc /* This for co-processes */
    633      1.1  jtc 
    634      1.1  jtc typedef INT32 Coproc_id; /* something that won't (realisticly) wrap */
    635      1.1  jtc struct coproc {
    636      1.1  jtc 	int	read;		/* pipe from co-process's stdout */
    637      1.1  jtc 	int	readw;		/* other side of read (saved temporarily) */
    638      1.1  jtc 	int	write;		/* pipe to co-process's stdin */
    639      1.1  jtc 	Coproc_id id;		/* id of current output pipe */
    640      1.1  jtc 	int	njobs;		/* number of live jobs using output pipe */
    641      1.1  jtc 	void    *job;           /* 0 or job of co-process using input pipe */
    642      1.1  jtc };
    643      1.1  jtc EXTERN struct coproc coproc;
    644      1.1  jtc #endif /* KSH */
    645      1.1  jtc 
    646      1.1  jtc /* Used in jobs.c and by coprocess stuff in exec.c */
    647      1.1  jtc #ifdef JOB_SIGS
    648      1.1  jtc EXTERN sigset_t		sm_default, sm_sigchld;
    649      1.1  jtc #endif /* JOB_SIGS */
    650      1.1  jtc 
    651      1.1  jtc extern const char ksh_version[];
    652      1.1  jtc 
    653      1.1  jtc /* name of called builtin function (used by error functions) */
    654      1.1  jtc EXTERN char	*builtin_argv0;
    655      1.1  jtc EXTERN Tflag	builtin_flag;	/* flags of called builtin (SPEC_BI, etc.) */
    656      1.1  jtc 
    657      1.1  jtc /* current working directory, and size of memory allocated for same */
    658      1.1  jtc EXTERN char	*current_wd;
    659      1.1  jtc EXTERN int	current_wd_size;
    660      1.1  jtc 
    661      1.1  jtc #ifdef EDIT
    662      1.1  jtc /* Minimium required space to work with on a line - if the prompt leaves less
    663      1.1  jtc  * space than this on a line, the prompt is truncated.
    664      1.1  jtc  */
    665      1.1  jtc # define MIN_EDIT_SPACE	7
    666      1.1  jtc /* Minimium allowed value for x_cols: 2 for prompt, 3 for " < " at end of line
    667      1.1  jtc  */
    668      1.1  jtc # define MIN_COLS	(2 + MIN_EDIT_SPACE + 3)
    669      1.1  jtc EXTERN	int	x_cols I__(80);	/* tty columns */
    670      1.1  jtc #else
    671      1.1  jtc # define x_cols 80		/* for pr_menu(exec.c) */
    672      1.1  jtc #endif
    673      1.1  jtc 
    674      1.1  jtc 
    675      1.1  jtc /* These to avoid bracket matching problems */
    676      1.1  jtc #define OPAREN	'('
    677      1.1  jtc #define CPAREN	')'
    678      1.1  jtc #define OBRACK	'['
    679      1.1  jtc #define CBRACK	']'
    680      1.1  jtc #define OBRACE	'{'
    681      1.1  jtc #define CBRACE	'}'
    682      1.1  jtc 
    683  1.1.1.2  jtc /* Determine the location of the system (common) profile */
    684  1.1.1.2  jtc #ifndef KSH_SYSTEM_PROFILE
    685  1.1.1.2  jtc # ifdef __NeXT
    686  1.1.1.2  jtc #  define KSH_SYSTEM_PROFILE "/etc/profile.std"
    687  1.1.1.2  jtc # else /* __NeXT */
    688  1.1.1.2  jtc #  define KSH_SYSTEM_PROFILE "/etc/profile"
    689  1.1.1.2  jtc # endif /* __NeXT */
    690  1.1.1.2  jtc #endif /* KSH_SYSTEM_PROFILE */
    691      1.1  jtc 
    692      1.1  jtc #include "shf.h"
    693      1.1  jtc #include "table.h"
    694      1.1  jtc #include "tree.h"
    695      1.1  jtc #include "expand.h"
    696      1.1  jtc #include "lex.h"
    697      1.1  jtc #include "proto.h"
    698      1.1  jtc 
    699      1.1  jtc /* be sure not to interfere with anyone else's idea about EXTERN */
    700      1.1  jtc #ifdef EXTERN_DEFINED
    701      1.1  jtc # undef EXTERN_DEFINED
    702      1.1  jtc # undef EXTERN
    703      1.1  jtc #endif
    704      1.1  jtc #undef I__
    705