sh.h revision 83098
183098Smp/* $Header: /src/pub/tcsh/sh.h,v 3.96 2001/03/18 19:06:29 christos Exp $ */
259243Sobrien/*
359243Sobrien * sh.h: Catch it all globals and includes file!
459243Sobrien */
559243Sobrien/*-
659243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
759243Sobrien * All rights reserved.
859243Sobrien *
959243Sobrien * Redistribution and use in source and binary forms, with or without
1059243Sobrien * modification, are permitted provided that the following conditions
1159243Sobrien * are met:
1259243Sobrien * 1. Redistributions of source code must retain the above copyright
1359243Sobrien *    notice, this list of conditions and the following disclaimer.
1459243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1559243Sobrien *    notice, this list of conditions and the following disclaimer in the
1659243Sobrien *    documentation and/or other materials provided with the distribution.
1759243Sobrien * 3. All advertising materials mentioning features or use of this software
1859243Sobrien *    must display the following acknowledgement:
1959243Sobrien *	This product includes software developed by the University of
2059243Sobrien *	California, Berkeley and its contributors.
2159243Sobrien * 4. Neither the name of the University nor the names of its contributors
2259243Sobrien *    may be used to endorse or promote products derived from this software
2359243Sobrien *    without specific prior written permission.
2459243Sobrien *
2559243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2659243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2759243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2859243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2959243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3059243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3159243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3259243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3359243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3459243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3559243Sobrien * SUCH DAMAGE.
3659243Sobrien */
3759243Sobrien#ifndef _h_sh
3859243Sobrien#define _h_sh
3959243Sobrien
4059243Sobrien#include "config.h"
4159243Sobrien
4259243Sobrien#ifndef EXTERN
4359243Sobrien# define EXTERN extern
4459243Sobrien#else /* !EXTERN */
4569408Sache# ifdef WINNT_NATIVE
4659243Sobrien#  define IZERO = 0
4759243Sobrien#  define IZERO_STRUCT = {0}
4869408Sache# endif /* WINNT_NATIVE */
4959243Sobrien#endif /* EXTERN */
5059243Sobrien
5159243Sobrien#ifndef IZERO
5259243Sobrien# define IZERO
5359243Sobrien#endif /* IZERO */
5459243Sobrien#ifndef IZERO_STRUCT
5559243Sobrien# define IZERO_STRUCT
5659243Sobrien# endif /* IZERO_STRUCT */
5759243Sobrien
5869408Sache#ifndef WINNT_NATIVE
5959243Sobrien# define INIT_ZERO
6059243Sobrien# define INIT_ZERO_STRUCT
6159243Sobrien# define force_read read
6269408Sache#endif /*!WINNT_NATIVE */
6359243Sobrien/*
6459243Sobrien * Sanity
6559243Sobrien */
6659243Sobrien#if defined(_POSIX_SOURCE) && !defined(POSIX)
6759243Sobrien# define POSIX
6859243Sobrien#endif
6959243Sobrien
7059243Sobrien#if defined(POSIXJOBS) && !defined(BSDJOBS)
7159243Sobrien# define BSDJOBS
7259243Sobrien#endif
7359243Sobrien
7459243Sobrien#if defined(POSIXSIGS) && !defined(BSDSIGS)
7559243Sobrien# define BSDSIGS
7659243Sobrien#endif
7759243Sobrien
7859243Sobrien#ifdef SHORT_STRINGS
7959243Sobrientypedef short Char;
8059243Sobrientypedef unsigned short uChar;
8159243Sobrien# define SAVE(a) (Strsave(str2short(a)))
8259243Sobrien#else
8359243Sobrientypedef char Char;
8459243Sobrientypedef unsigned char uChar;
8559243Sobrien# define SAVE(a) (strsave(a))
8659243Sobrien#endif
8759243Sobrien
8859243Sobrien/* Elide unused argument warnings */
8959243Sobrien#define USE(a)	(void) (a)
9059243Sobrien/*
9159243Sobrien * If your compiler complains, then you can either
9259243Sobrien * throw it away and get gcc or, use the following define
9359243Sobrien * and get rid of the typedef.
9459243Sobrien * [The 4.2/3BSD vax compiler does not like that]
9559243Sobrien * Both MULTIFLOW and PCC compilers exhbit this bug.  -- sterling@netcom.com
9659243Sobrien */
9759243Sobrien#ifdef SIGVOID
9859243Sobrien# if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__)
9959243Sobrien#  define sigret_t void
10059243Sobrien# else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */
10159243Sobrientypedef void sigret_t;
10259243Sobrien# endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */
10359243Sobrien#else /* !SIGVOID */
10459243Sobrientypedef int sigret_t;
10559243Sobrien#endif /* SIGVOID */
10659243Sobrien
10759243Sobrien/*
10859243Sobrien * Return true if the path is absolute
10959243Sobrien */
11069408Sache#ifndef WINNT_NATIVE
11159243Sobrien# define ABSOLUTEP(p)	(*(p) == '/')
11269408Sache#else /* WINNT_NATIVE */
11359243Sobrien# define ABSOLUTEP(p)	((p)[0] == '/' || \
11459243Sobrien			 (Isalpha((p)[0]) && (p)[1] == ':' && (p)[2] == '/'))
11569408Sache#endif /* WINNT_NATIVE */
11659243Sobrien
11759243Sobrien/*
11859243Sobrien * Fundamental definitions which may vary from system to system.
11959243Sobrien *
12059243Sobrien *	BUFSIZE		The i/o buffering size; also limits word size
12159243Sobrien *	MAILINTVL	How often to mailcheck; more often is more expensive
12259243Sobrien */
12359243Sobrien#ifdef BUFSIZE
12461524Sobrien# if	   BUFSIZE < 4096
12559243Sobrien#  undef   BUFSIZE
12661524Sobrien#  define  BUFSIZE	4096	/* buffer size should be no less than this */
12759243Sobrien# endif
12859243Sobrien#else
12961524Sobrien# define   BUFSIZE	4096
13059243Sobrien#endif /* BUFSIZE */
13159243Sobrien
13259243Sobrien#define FORKSLEEP	10	/* delay loop on non-interactive fork failure */
13359243Sobrien#define	MAILINTVL	600	/* 10 minutes */
13459243Sobrien
13559243Sobrien#ifndef INBUFSIZE
13659243Sobrien# define INBUFSIZE    2*BUFSIZE /* Num input characters on the command line */
13759243Sobrien#endif /* INBUFSIZE */
13859243Sobrien
13959243Sobrien
14059243Sobrien/*
14159243Sobrien * What our builtin echo looks like
14259243Sobrien */
14359243Sobrien#define NONE_ECHO	0
14459243Sobrien#define BSD_ECHO	1
14559243Sobrien#define SYSV_ECHO	2
14659243Sobrien#define BOTH_ECHO	(BSD_ECHO|SYSV_ECHO)
14759243Sobrien
14859243Sobrien#ifndef ECHO_STYLE
14959243Sobrien# if SYSVREL > 0
15059243Sobrien#  define ECHO_STYLE SYSV_ECHO
15159243Sobrien# else /* SYSVREL == 0 */
15259243Sobrien#  define ECHO_STYLE BSD_ECHO
15359243Sobrien# endif /* SYSVREL */
15459243Sobrien#endif /* ECHO_STYLE */
15559243Sobrien
15659243Sobrien/*
15759243Sobrien * The shell moves std in/out/diag and the old std input away from units
15859243Sobrien * 0, 1, and 2 so that it is easy to set up these standards for invoked
15959243Sobrien * commands.
16059243Sobrien */
16159243Sobrien#define	FSHTTY	15		/* /dev/tty when manip pgrps */
16259243Sobrien#define	FSHIN	16		/* Preferred desc for shell input */
16359243Sobrien#define	FSHOUT	17		/* ... shell output */
16459243Sobrien#define	FSHDIAG	18		/* ... shell diagnostics */
16559243Sobrien#define	FOLDSTD	19		/* ... old std input */
16659243Sobrien
16759243Sobrien#ifdef PROF
16859243Sobrien#define	xexit(n)	done(n)
16959243Sobrien#endif
17059243Sobrien
17159243Sobrien#ifdef cray
17259243Sobrien# define word word_t           /* sys/types.h defines word.. bad move! */
17359243Sobrien#endif
17459243Sobrien
17559243Sobrien#include <sys/types.h>
17659243Sobrien
17759243Sobrien#ifdef cray
17859243Sobrien# undef word
17959243Sobrien#endif
18059243Sobrien
18159243Sobrien/*
18259243Sobrien * Path separator in environment variables
18359243Sobrien */
18459243Sobrien#ifndef PATHSEP
18569408Sache# if defined(__EMX__) || defined(WINNT_NATIVE)
18659243Sobrien#  define PATHSEP ';'
18759243Sobrien# else /* unix */
18859243Sobrien#  define PATHSEP ':'
18969408Sache# endif /* __EMX__ || WINNT_NATIVE */
19059243Sobrien#endif /* !PATHSEP */
19159243Sobrien
19283098Smp#if defined(__HP_CXD_SPP) && !defined(__hpux)
19359243Sobrien# include <sys/cnx_stat.h>
19459243Sobrien# define stat stat64
19559243Sobrien# define fstat fstat64
19659243Sobrien# define lstat lstat64
19783098Smp#endif /* __HP_CXD_SPP && !__hpux */
19859243Sobrien
19959243Sobrien/*
20059243Sobrien * This macro compares the st_dev field of struct stat. On aix on ibmESA
20159243Sobrien * st_dev is a structure, so comparison does not work.
20259243Sobrien */
20359243Sobrien#ifndef DEV_DEV_COMPARE
20459243Sobrien# define DEV_DEV_COMPARE(x,y)   ((x) == (y))
20559243Sobrien#endif /* DEV_DEV_COMPARE */
20659243Sobrien
20759243Sobrien#ifdef _SEQUENT_
20859243Sobrien# include <sys/procstats.h>
20959243Sobrien#endif /* _SEQUENT_ */
21069408Sache#if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
21159243Sobrien# include <sys/times.h>
21269408Sache#endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
21359243Sobrien
21459243Sobrien#ifdef NLS
21559243Sobrien# include <locale.h>
21659243Sobrien#endif /* NLS */
21759243Sobrien
21859243Sobrien
21969408Sache#if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
22059243Sobrien# include <sys/param.h>
22169408Sache#endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
22259243Sobrien#include <sys/stat.h>
22359243Sobrien
22459243Sobrien#if defined(BSDTIMES) || defined(BSDLIMIT)
22559243Sobrien# include <sys/time.h>
22669408Sache# if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
22759243Sobrien#  include "/usr/ucbinclude/sys/resource.h"
22859243Sobrien# else
22959243Sobrien#  ifdef convex
23059243Sobrien#   define sysrusage cvxrusage
23159243Sobrien#   include <sys/sysinfo.h>
23259243Sobrien#  else
23359243Sobrien#   define sysrusage rusage
23459243Sobrien#   include <sys/resource.h>
23559243Sobrien#  endif /* convex */
23659243Sobrien# endif /* SYSVREL>3 */
23759243Sobrien#endif /* BSDTIMES */
23859243Sobrien
23969408Sache#ifndef WINNT_NATIVE
24059243Sobrien# ifndef POSIX
24159243Sobrien#  ifdef TERMIO
24259243Sobrien#   include <termio.h>
24359243Sobrien#  else /* SGTTY */
24459243Sobrien#   include <sgtty.h>
24559243Sobrien#  endif /* TERMIO */
24659243Sobrien# else /* POSIX */
24759243Sobrien#  ifndef _UWIN
24859243Sobrien#   include <termios.h>
24959243Sobrien#  else
25059243Sobrien#   include <termio.h>
25159243Sobrien#  endif /* _UWIN */
25259243Sobrien#  if SYSVREL > 3
25359243Sobrien#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
25459243Sobrien#   undef TIOCSLTC
25559243Sobrien#   undef CSWTCH
25659243Sobrien#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
25759243Sobrien#  endif /* SYSVREL > 3 */
25859243Sobrien# endif /* POSIX */
25969408Sache#endif /* WINNT_NATIVE */
26059243Sobrien
26159243Sobrien#ifdef sonyrisc
26259243Sobrien# include <sys/ttold.h>
26359243Sobrien#endif /* sonyrisc */
26459243Sobrien
26569408Sache#if defined(POSIX) && !defined(WINNT_NATIVE)
26659243Sobrien/*
26759243Sobrien * We should be using setpgid and setpgid
26859243Sobrien * by now, but in some systems we use the
26959243Sobrien * old routines...
27059243Sobrien */
27159243Sobrien# define getpgrp __getpgrp
27259243Sobrien# define setpgrp __setpgrp
27359243Sobrien# include <unistd.h>
27459243Sobrien# undef getpgrp
27559243Sobrien# undef setpgrp
27659243Sobrien
27759243Sobrien/*
27859243Sobrien * the gcc+protoize version of <stdlib.h>
27959243Sobrien * redefines malloc(), so we define the following
28059243Sobrien * to avoid it.
28159243Sobrien */
28259243Sobrien# if defined(linux) || defined(sgi) || defined(_OSD_POSIX)
28359243Sobrien#  define NO_FIX_MALLOC
28459243Sobrien#  include <stdlib.h>
28559243Sobrien# else /* linux */
28659243Sobrien#  define _GNU_STDLIB_H
28759243Sobrien#  define malloc __malloc
28859243Sobrien#  define free __free
28959243Sobrien#  define calloc __calloc
29059243Sobrien#  define realloc __realloc
29159243Sobrien#  include <stdlib.h>
29259243Sobrien#  undef malloc
29359243Sobrien#  undef free
29459243Sobrien#  undef calloc
29559243Sobrien#  undef realloc
29659243Sobrien# endif /* linux || sgi */
29759243Sobrien# include <limits.h>
29869408Sache#endif /* POSIX && !WINNT_NATIVE */
29959243Sobrien
30083098Smp#if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(linux)
30159243Sobrien# if !defined(pyr) && !defined(stellar)
30259243Sobrien#  include <time.h>
30359243Sobrien#  ifdef _MINIX
30459243Sobrien#   define HZ CLOCKS_PER_SEC
30559243Sobrien#  endif /* _MINIX */
30659243Sobrien# endif /* !pyr && !stellar */
30759243Sobrien#endif /* SYSVREL > 0 ||  _IBMR2 */
30859243Sobrien
30959243Sobrien/* In the following ifdef the DECOSF1 has been commented so that later
31059243Sobrien * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
31159243Sobrien */
31259243Sobrien#if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
31369408Sache# if !defined(COHERENT) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
31459243Sobrien#  include <sys/ioctl.h>
31559243Sobrien# endif
31659243Sobrien#endif
31759243Sobrien
31859243Sobrien#if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
31959243Sobrien#undef CSWTCH
32059243Sobrien#define CSWTCH _POSIX_VDISABLE
32159243Sobrien#endif
32259243Sobrien
32369408Sache#if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
32459243Sobrien# include <sys/filio.h>
32559243Sobrien#endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
32659243Sobrien
32769408Sache#if !defined(_MINIX) && !defined(COHERENT) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
32859243Sobrien# include <sys/file.h>
32969408Sache#endif	/* !_MINIX && !COHERENT && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
33059243Sobrien
33159243Sobrien#if !defined(O_RDONLY) || !defined(O_NDELAY)
33259243Sobrien# include <fcntl.h>
33359243Sobrien#endif
33459243Sobrien
33559243Sobrien#include <errno.h>
33659243Sobrien
33759243Sobrien#include <setjmp.h>
33859243Sobrien
33959243Sobrien#if __STDC__ || defined(FUNCPROTO)
34059243Sobrien# include <stdarg.h>
34159243Sobrien#else
34259243Sobrien#ifdef	_MINIX
34359243Sobrien# include "mi.varargs.h"
34459243Sobrien#else
34559243Sobrien# include <varargs.h>
34659243Sobrien#endif	/* _MINIX */
34759243Sobrien#endif
34859243Sobrien
34959243Sobrien#ifdef DIRENT
35059243Sobrien# include <dirent.h>
35159243Sobrien#else
35259243Sobrien# ifdef hp9000s500
35359243Sobrien#  include <ndir.h>
35459243Sobrien# else
35559243Sobrien#  include <sys/dir.h>
35659243Sobrien# endif
35759243Sobrien# define dirent direct
35859243Sobrien#endif /* DIRENT */
35959243Sobrien#if defined(hpux) || defined(sgi) || defined(OREO) || defined(COHERENT)
36059243Sobrien# include <stdio.h>	/* So the fgetpwent() prototypes work */
36159243Sobrien#endif /* hpux || sgi || OREO || COHERENT */
36269408Sache#ifndef WINNT_NATIVE
36359243Sobrien#include <pwd.h>
36459243Sobrien#include <grp.h>
36569408Sache#endif /* WINNT_NATIVE */
36659243Sobrien#ifdef PW_SHADOW
36759243Sobrien# include <shadow.h>
36859243Sobrien#endif /* PW_SHADOW */
36959243Sobrien#ifdef PW_AUTH
37059243Sobrien# include <auth.h>
37159243Sobrien#endif /* PW_AUTH */
37259243Sobrien#if defined(BSD) && !defined(POSIX)
37359243Sobrien# include <strings.h>
37459243Sobrien# define strchr(a, b) index(a, b)
37559243Sobrien# define strrchr(a, b) rindex(a, b)
37659243Sobrien#else
37759243Sobrien# include <string.h>
37859243Sobrien#endif /* BSD */
37959243Sobrien
38059243Sobrien/*
38159243Sobrien * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
38259243Sobrien * include it yet, so we include it here
38359243Sobrien */
38459243Sobrien#if defined(sgi) && SYSVREL > 3
38559243Sobrien# include <sys/cdefs.h>
38659243Sobrien#endif /* sgi && SYSVREL > 3 */
38759243Sobrien
38859243Sobrien#ifdef REMOTEHOST
38959243Sobrien# ifdef ISC
39059243Sobrien#  undef MAXHOSTNAMELEN	/* Busted headers? */
39159243Sobrien# endif
39259243Sobrien
39359243Sobrien# include <netinet/in.h>
39459243Sobrien# include <arpa/inet.h>
39559243Sobrien# include <sys/socket.h>
39669408Sache# if defined(_SS_SIZE) || defined(_SS_MAXSIZE)
39769408Sache#  define INET6
39869408Sache# endif
39959243Sobrien# include <sys/uio.h>	/* For struct iovec */
40059243Sobrien#endif /* REMOTEHOST */
40159243Sobrien
40259243Sobrien/*
40359243Sobrien * ANSIisms... These must be *after* the system include and
40459243Sobrien * *before* our includes, so that BSDreno has time to define __P
40559243Sobrien */
40659243Sobrien#undef __P
40759243Sobrien#ifndef __P
40859243Sobrien# if __STDC__ || defined(FUNCPROTO)
40959243Sobrien#  ifndef FUNCPROTO
41059243Sobrien#   define FUNCPROTO
41159243Sobrien#  endif
41259243Sobrien#  define __P(a) a
41359243Sobrien# else
41459243Sobrien#  define __P(a) ()
41559243Sobrien#  if !__STDC__
41659243Sobrien#   define const
41759243Sobrien#   ifndef apollo
41859243Sobrien#    define volatile	/* Apollo 'c' extensions need this */
41959243Sobrien#   endif /* apollo */
42059243Sobrien#  endif
42159243Sobrien# endif
42259243Sobrien#endif
42359243Sobrien
42459243Sobrien
42559243Sobrien#ifdef PURIFY
42659243Sobrien/* exit normally, allowing purify to trace leaks */
42759243Sobrien# define _exit		exit
42859243Sobrientypedef  int		pret_t;
42959243Sobrien#else /* !PURIFY */
43059243Sobrien/*
43159243Sobrien * If your compiler complains, then you can either
43259243Sobrien * throw it away and get gcc or, use the following define
43359243Sobrien * and get rid of the typedef.
43459243Sobrien * [The 4.2/3BSD vax compiler does not like that]
43559243Sobrien * Both MULTIFLOW and PCC compilers exhbit this bug.  -- sterling@netcom.com
43659243Sobrien */
43759243Sobrien# if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__)
43859243Sobrien#  define pret_t void
43959243Sobrien# else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */
44059243Sobrientypedef void pret_t;
44159243Sobrien# endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */
44259243Sobrien#endif /* PURIFY */
44359243Sobrien
44459243Sobrientypedef int bool;
44559243Sobrien
44669408Sache/*
44769408Sache * ASCII vs. EBCDIC
44869408Sache */
44969408Sache#if 'Z' - 'A' == 25
45069408Sache# ifndef IS_ASCII
45169408Sache#  define IS_ASCII
45269408Sache# endif
45369408Sache#endif
45469408Sache
45559243Sobrien#include "sh.types.h"
45659243Sobrien
45769408Sache#ifndef WINNT_NATIVE
45859243Sobrien# ifndef POSIX
45959243Sobrienextern pid_t getpgrp __P((int));
46059243Sobrien# else /* POSIX */
46159243Sobrien#  if (defined(BSD) && !defined(BSD4_4)) || defined(SUNOS4) || defined(IRIS4D) || defined(DGUX)
46259243Sobrienextern pid_t getpgrp __P((int));
46359243Sobrien#  else /* !(BSD || SUNOS4 || IRIS4D || DGUX) */
46459243Sobrienextern pid_t getpgrp __P((void));
46559243Sobrien#  endif	/* BSD || SUNOS4 || IRISD || DGUX */
46659243Sobrien# endif /* POSIX */
46759243Sobrienextern pid_t setpgrp __P((pid_t, pid_t));
46869408Sache#endif /* !WINNT_NATIVE */
46959243Sobrien
47059243Sobrientypedef sigret_t (*signalfun_t) __P((int));
47159243Sobrien
47259243Sobrien#ifndef lint
47359243Sobrientypedef ptr_t memalign_t;
47459243Sobrien#else
47559243Sobrientypedef union {
47659243Sobrien    char    am_char, *am_char_p;
47759243Sobrien    short   am_short, *am_short_p;
47859243Sobrien    int     am_int, *am_int_p;
47959243Sobrien    long    am_long, *am_long_p;
48059243Sobrien    float   am_float, *am_float_p;
48159243Sobrien    double  am_double, *am_double_p;
48259243Sobrien}      *memalign_t;
48359243Sobrien
48459243Sobrien# define malloc		lint_malloc
48559243Sobrien# define free		lint_free
48659243Sobrien# define realloc	lint_realloc
48759243Sobrien# define calloc		lint_calloc
48859243Sobrien#endif
48959243Sobrien
49059243Sobrien#ifdef MDEBUG
49159243Sobrienextern memalign_t	DebugMalloc	__P((unsigned, char *, int));
49259243Sobrienextern memalign_t	DebugRealloc	__P((ptr_t, unsigned, char *, int));
49359243Sobrienextern memalign_t	DebugCalloc	__P((unsigned, unsigned, char *, int));
49459243Sobrienextern void		DebugFree	__P((ptr_t, char *, int));
49559243Sobrien# define xmalloc(i)  	DebugMalloc(i, __FILE__, __LINE__)
49659243Sobrien# define xrealloc(p, i)((p) ? DebugRealloc(p, i, __FILE__, __LINE__) : \
49759243Sobrien			      DebugMalloc(i, __FILE__, __LINE__))
49859243Sobrien# define xcalloc(n, s)	DebugCalloc(n, s, __FILE__, __LINE__)
49959243Sobrien# define xfree(p)    	if (p) DebugFree(p, __FILE__, __LINE__)
50059243Sobrien#else
50159243Sobrien# ifdef SYSMALLOC
50259243Sobrien#  define xmalloc(i)		smalloc(i)
50359243Sobrien#  define xrealloc(p, i)	srealloc(p, i)
50459243Sobrien#  define xcalloc(n, s)		scalloc(n, s)
50559243Sobrien#  define xfree(p)		sfree(p)
50659243Sobrien# else
50759243Sobrien#  define xmalloc(i)  		malloc(i)
50859243Sobrien#  define xrealloc(p, i)	realloc(p, i)
50959243Sobrien#  define xcalloc(n, s)		calloc(n, s)
51059243Sobrien#  define xfree(p)    		free(p)
51159243Sobrien# endif /* SYSMALLOC */
51259243Sobrien#endif /* MDEBUG */
51359243Sobrien#include "sh.char.h"
51459243Sobrien#include "sh.err.h"
51559243Sobrien#include "sh.dir.h"
51659243Sobrien#include "sh.proc.h"
51759243Sobrien
51859243Sobrien#include "pathnames.h"
51959243Sobrien
52059243Sobrien
52159243Sobrien/*
52259243Sobrien * C shell
52359243Sobrien *
52459243Sobrien * Bill Joy, UC Berkeley
52559243Sobrien * October, 1978; May 1980
52659243Sobrien *
52759243Sobrien * Jim Kulp, IIASA, Laxenburg Austria
52859243Sobrien * April, 1980
52959243Sobrien */
53059243Sobrien
53159243Sobrien#if !defined(MAXNAMLEN) && defined(_D_NAME_MAX)
53259243Sobrien# define MAXNAMLEN _D_NAME_MAX
53359243Sobrien#endif /* MAXNAMLEN */
53459243Sobrien
53559243Sobrien#ifdef HESIOD
53659243Sobrien# include <hesiod.h>
53759243Sobrien#endif /* HESIOD */
53859243Sobrien
53959243Sobrien#ifdef REMOTEHOST
54059243Sobrien# include <netdb.h>
54159243Sobrien#endif /* REMOTEHOST */
54259243Sobrien
54359243Sobrien#ifndef MAXHOSTNAMELEN
54459243Sobrien# if defined(SCO) && (SYSVREL > 3)
54559243Sobrien#  include <sys/socket.h>
54659243Sobrien# else
54783098Smp#  define MAXHOSTNAMELEN 256
54859243Sobrien# endif
54959243Sobrien#endif /* MAXHOSTNAMELEN */
55059243Sobrien
55159243Sobrien
55259243Sobrien
55359243Sobrien#define	eq(a, b)	(Strcmp(a, b) == 0)
55459243Sobrien
55559243Sobrien/* globone() flags */
55659243Sobrien#define G_ERROR		0	/* default action: error if multiple words */
55759243Sobrien#define G_IGNORE	1	/* ignore the rest of the words		   */
55859243Sobrien#define G_APPEND	2	/* make a sentence by cat'ing the words    */
55959243Sobrien
56059243Sobrien/*
56159243Sobrien * Global flags
56259243Sobrien */
56359243SobrienEXTERN bool    chkstop IZERO;	/* Warned of stopped jobs... allow exit */
56459243Sobrien
56559243Sobrien#if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
56659243Sobrien# define CLOSE_ON_EXEC
56759243Sobrien#else
56859243SobrienEXTERN bool    didcch IZERO;	/* Have closed unused fd's for child */
56959243Sobrien#endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
57059243Sobrien
57159243SobrienEXTERN bool    didfds IZERO;	/* Have setup i/o fd's for child */
57259243SobrienEXTERN bool    doneinp IZERO;	/* EOF indicator after reset from readc */
57359243SobrienEXTERN bool    exiterr IZERO;	/* Exit if error or non-zero exit status */
57459243SobrienEXTERN bool    child IZERO;	/* Child shell ... errors cause exit */
57559243SobrienEXTERN bool    haderr IZERO;	/* Reset was because of an error */
57659243SobrienEXTERN bool    intty IZERO;	/* Input is a tty */
57759243SobrienEXTERN bool    intact IZERO;	/* We are interactive... therefore prompt */
57859243SobrienEXTERN bool    justpr IZERO;	/* Just print because of :p hist mod */
57959243SobrienEXTERN bool    loginsh IZERO;	/* We are a loginsh -> .login/.logout */
58059243SobrienEXTERN bool    neednote IZERO;	/* Need to pnotify() */
58159243SobrienEXTERN bool    noexec IZERO;	/* Don't execute, just syntax check */
58259243SobrienEXTERN bool    pjobs IZERO;	/* want to print jobs if interrupted */
58359243SobrienEXTERN bool    setintr IZERO;	/* Set interrupts on/off -> Wait intr... */
58459243SobrienEXTERN bool    timflg IZERO;	/* Time the next waited for command */
58559243SobrienEXTERN bool    havhash IZERO;	/* path hashing is available */
58659243SobrienEXTERN bool    editing IZERO;	/* doing filename expansion and line editing */
58759243SobrienEXTERN bool    noediting IZERO;	/* initial $term defaulted to noedit */
58859243SobrienEXTERN bool    bslash_quote IZERO;/* PWP: tcsh-style quoting?  (in sh.c) */
58959243SobrienEXTERN bool    isoutatty IZERO;	/* is SHOUT a tty */
59059243SobrienEXTERN bool    isdiagatty IZERO;/* is SHDIAG a tty */
59159243SobrienEXTERN bool    is1atty IZERO;	/* is file descriptor 1 a tty (didfds mode) */
59259243SobrienEXTERN bool    is2atty IZERO;	/* is file descriptor 2 a tty (didfds mode) */
59359243SobrienEXTERN bool    arun IZERO;	/* Currently running multi-line-aliases */
59459243SobrienEXTERN int     implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
59559243SobrienEXTERN bool    inheredoc IZERO;	/* Currently parsing a heredoc */
59659243Sobrien
59759243Sobrien/*
59859243Sobrien * Global i/o info
59959243Sobrien */
60059243SobrienEXTERN Char   *arginp IZERO;	/* Argument input for sh -c and internal `xx` */
60159243SobrienEXTERN int     onelflg IZERO;	/* 2 -> need line for -t, 1 -> exit on read */
60259243Sobrienextern Char   *ffile;		/* Name of shell file for $0 */
60359243Sobrienextern bool    dolzero;		/* if $?0 should return true... */
60459243Sobrien
60559243Sobrienextern char *seterr;		/* Error message from scanner/parser */
60669408Sache#ifndef BSD4_4
60759243Sobrienextern int errno;		/* Error from C library routines */
60869408Sache#endif
60969408Sacheextern int exitset;
61059243SobrienEXTERN Char   *shtemp IZERO;	/* Temp name for << shell files in /tmp */
61159243Sobrien
61259243Sobrien#ifdef BSDTIMES
61359243SobrienEXTERN struct timeval time0;	/* Time at which the shell started */
61459243SobrienEXTERN struct sysrusage ru0;
61559243Sobrien#else
61659243Sobrien# ifdef _SEQUENT_
61759243SobrienEXTERN timeval_t time0;		/* time at which shell started */
61859243SobrienEXTERN struct process_stats ru0;
61959243Sobrien# else /* _SEQUENT_ */
62059243Sobrien#  ifndef POSIX
62159243SobrienEXTERN time_t  time0;		/* time at which shell started */
62259243Sobrien#  else	/* POSIX */
62359243SobrienEXTERN clock_t time0;		/* time at which shell started */
62459243SobrienEXTERN clock_t clk_tck;
62559243Sobrien#  endif /* POSIX */
62659243SobrienEXTERN struct tms shtimes;	/* shell and child times for process timing */
62759243Sobrien# endif /* _SEQUENT_ */
62859415SobrienEXTERN long seconds0;
62959243Sobrien#endif /* BSDTIMES */
63059243Sobrien
63159243Sobrien#ifndef HZ
63259243Sobrien# define HZ	100		/* for division into seconds */
63359243Sobrien#endif
63459243Sobrien
63559243Sobrien/*
63659243Sobrien * Miscellany
63759243Sobrien */
63859243SobrienEXTERN Char   *doldol;		/* Character pid for $$ */
63959243SobrienEXTERN int     backpid;		/* pid of the last background job */
64059243Sobrien
64159243Sobrien/*
64259243Sobrien * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
64359243Sobrien * cause pid's could be unsigned and that would break our -1 flag, and
64459243Sobrien * uid_t and gid_t are not defined in all the systems so I would have to
64559243Sobrien * make special cases for them. In the future...
64659243Sobrien */
64759243SobrienEXTERN int     uid, euid, 	/* Invokers real and effective */
64859243Sobrien	       gid, egid;	/* User and group ids */
64959243SobrienEXTERN int     opgrp,		/* Initial pgrp and tty pgrp */
65059243Sobrien               shpgrp,		/* Pgrp of shell */
65159243Sobrien               tpgrp;		/* Terminal process group */
65259243Sobrien				/* If tpgrp is -1, leave tty alone! */
65359243Sobrien
65459243SobrienEXTERN Char    PromptBuf[INBUFSIZE*2];	/* buffer for the actual printed prompt.
65559243Sobrien					 * this must be large enough to contain
65659243Sobrien					 * the input line and the prompt, in
65759243Sobrien					 * case a correction occurred...
65859243Sobrien					 */
65959243SobrienEXTERN Char    RPromptBuf[INBUFSIZE];	/* buffer for right-hand side prompt */
66059243Sobrien
66159243Sobrien/*
66259243Sobrien * To be able to redirect i/o for builtins easily, the shell moves the i/o
66359243Sobrien * descriptors it uses away from 0,1,2.
66459243Sobrien * Ideally these should be in units which are closed across exec's
66559243Sobrien * (this saves work) but for version 6, this is not usually possible.
66659243Sobrien * The desired initial values for these descriptors are defined in
66759243Sobrien * sh.local.h.
66859243Sobrien */
66959243SobrienEXTERN int   SHIN IZERO;	/* Current shell input (script) */
67059243SobrienEXTERN int   SHOUT IZERO;	/* Shell output */
67159243SobrienEXTERN int   SHDIAG IZERO;	/* Diagnostic output... shell errs go here */
67259243SobrienEXTERN int   OLDSTD IZERO;	/* Old standard input (def for cmds) */
67359243Sobrien
67459243Sobrien
67559243Sobrien#if SYSVREL == 4 && defined(_UTS)
67659243Sobrien/*
67759243Sobrien * From: fadden@uts.amdahl.com (Andy McFadden)
67859243Sobrien * we need sigsetjmp for UTS4, but not UTS2.1
67959243Sobrien */
68059243Sobrien# define SIGSETJMP
68159243Sobrien#endif
68259243Sobrien
68359243Sobrien/*
68459243Sobrien * Error control
68559243Sobrien *
68659243Sobrien * Errors in scanning and parsing set up an error message to be printed
68759243Sobrien * at the end and complete.  Other errors always cause a reset.
68859243Sobrien * Because of source commands and .cshrc we need nested error catches.
68959243Sobrien */
69059243Sobrien
69159243Sobrien#ifdef NO_STRUCT_ASSIGNMENT
69259243Sobrien
69359243Sobrien# ifdef SIGSETJMP
69459243Sobrien   typedef sigjmp_buf jmp_buf_t;
69559243Sobrien   /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */
69659243Sobrien#  define setexit()  sigsetjmp(reslab)
69759243Sobrien#  define reset()    siglongjmp(reslab, 1)
69859243Sobrien# else
69959243Sobrien   typedef jmp_buf jmp_buf_t;
70059243Sobrien   /* bugfix by Jak Kirman @ Brown U.: remove the (void) cast here, see sh.c */
70159243Sobrien#  define setexit()  setjmp(reslab)
70259243Sobrien#  define reset()    longjmp(reslab, 1)
70359243Sobrien# endif
70459243Sobrien# define getexit(a) (void) memmove((ptr_t)&(a), (ptr_t)&reslab, sizeof(reslab))
70559243Sobrien# define resexit(a) (void) memmove((ptr_t)&reslab, (ptr_t)&(a), sizeof(reslab))
70659243Sobrien
70759243Sobrien# define cpybin(a, b) (void) memmove((ptr_t)&(a), (ptr_t)&(b), sizeof(Bin))
70859243Sobrien
70959243Sobrien#else
71059243Sobrien
71159243Sobrien# ifdef SIGSETJMP
71259243Sobrien   typedef struct { sigjmp_buf j; } jmp_buf_t;
71359243Sobrien#  define setexit()  sigsetjmp(reslab.j)
71459243Sobrien#  define reset()    siglongjmp(reslab.j, 1)
71559243Sobrien# else
71659243Sobrien   typedef struct { jmp_buf j; } jmp_buf_t;
71759243Sobrien#  define setexit()  setjmp(reslab.j)
71859243Sobrien#  define reset()    longjmp(reslab.j, 1)
71959243Sobrien# endif
72059243Sobrien
72159243Sobrien# define getexit(a) (void) ((a) = reslab)
72259243Sobrien# define resexit(a) (void) (reslab = (a))
72359243Sobrien
72459243Sobrien# define cpybin(a, b) (void) ((a) = (b))
72559243Sobrien
72659243Sobrien#endif	/* NO_STRUCT_ASSIGNMENT */
72759243Sobrien
72859243Sobrienextern jmp_buf_t reslab;
72959243Sobrien
73059243SobrienEXTERN Char   *gointr;		/* Label for an onintr transfer */
73159243Sobrien
73259243Sobrienextern signalfun_t parintr;	/* Parents interrupt catch */
73359243Sobrienextern signalfun_t parterm;	/* Parents terminate catch */
73459243Sobrien
73559243Sobrien/*
73659243Sobrien * Lexical definitions.
73759243Sobrien *
73859243Sobrien * All lexical space is allocated dynamically.
73959243Sobrien * The eighth/sixteenth bit of characters is used to prevent recognition,
74059243Sobrien * and eventually stripped.
74159243Sobrien */
74259243Sobrien#define		META		0200
74359243Sobrien#define		ASCII		0177
74459243Sobrien#ifdef SHORT_STRINGS
74559243Sobrien# define	QUOTE 	((Char)	0100000)/* 16nth char bit used for 'ing */
74659243Sobrien# define	TRIM		0077777	/* Mask to strip quote bit */
74759243Sobrien# define	UNDER		0040000	/* Underline flag */
74859243Sobrien# define	BOLD		0020000	/* Bold flag */
74959243Sobrien# define	STANDOUT	0010000	/* Standout flag */
75059243Sobrien# define	LITERAL		0004000	/* Literal character flag */
75159243Sobrien# define	ATTRIBUTES	0074000	/* The bits used for attributes */
75259243Sobrien# define	CHAR		0000377	/* Mask to mask out the character */
75359243Sobrien#else
75459243Sobrien# define	QUOTE 	((Char)	0200)	/* Eighth char bit used for 'ing */
75559243Sobrien# define	TRIM		0177	/* Mask to strip quote bit */
75659243Sobrien# define	UNDER		0000000	/* No extra bits to do both */
75759243Sobrien# define	BOLD		0000000	/* Bold flag */
75859243Sobrien# define	STANDOUT	META	/* Standout flag */
75959243Sobrien# define	LITERAL		0000000	/* Literal character flag */
76059243Sobrien# define	ATTRIBUTES	0200	/* The bits used for attributes */
76159243Sobrien# define	CHAR		0000177	/* Mask to mask out the character */
76259243Sobrien#endif
76359243Sobrien
76459243SobrienEXTERN int     AsciiOnly;	/* If set only 7 bits expected in characters */
76559243Sobrien
76659243Sobrien/*
76759243Sobrien * Each level of input has a buffered input structure.
76859243Sobrien * There are one or more blocks of buffered input for each level,
76959243Sobrien * exactly one if the input is seekable and tell is available.
77059243Sobrien * In other cases, the shell buffers enough blocks to keep all loops
77159243Sobrien * in the buffer.
77259243Sobrien */
77359243SobrienEXTERN struct Bin {
77459243Sobrien    off_t   Bfseekp;		/* Seek pointer */
77559243Sobrien    off_t   Bfbobp;		/* Seekp of beginning of buffers */
77659243Sobrien    off_t   Bfeobp;		/* Seekp of end of buffers */
77759243Sobrien    int     Bfblocks;		/* Number of buffer blocks */
77859243Sobrien    Char  **Bfbuf;		/* The array of buffer blocks */
77959243Sobrien}       B;
78059243Sobrien
78159243Sobrien/*
78259243Sobrien * This structure allows us to seek inside aliases
78359243Sobrien */
78459243Sobrienstruct Ain {
78559243Sobrien    int type;
78669408Sache#define TCSH_I_SEEK 	 0		/* Invalid seek */
78769408Sache#define TCSH_A_SEEK	 1		/* Alias seek */
78869408Sache#define TCSH_F_SEEK	 2		/* File seek */
78969408Sache#define TCSH_E_SEEK	 3		/* Eval seek */
79059243Sobrien    union {
79159243Sobrien	off_t _f_seek;
79259243Sobrien	Char* _c_seek;
79359243Sobrien    } fc;
79459243Sobrien#define f_seek fc._f_seek
79559243Sobrien#define c_seek fc._c_seek
79659243Sobrien    Char **a_seek;
79759243Sobrien} ;
79859243Sobrien
79959243Sobrienextern int aret;		/* Type of last char returned */
80059243Sobrien#define SEEKEQ(a, b) ((a)->type == (b)->type && \
80159243Sobrien		      (a)->f_seek == (b)->f_seek && \
80259243Sobrien		      (a)->a_seek == (b)->a_seek)
80359243Sobrien
80459243Sobrien#define	fseekp	B.Bfseekp
80559243Sobrien#define	fbobp	B.Bfbobp
80659243Sobrien#define	feobp	B.Bfeobp
80759243Sobrien#define	fblocks	B.Bfblocks
80859243Sobrien#define	fbuf	B.Bfbuf
80959243Sobrien
81059243Sobrien/*
81159243Sobrien * The shell finds commands in loops by reseeking the input
81259243Sobrien * For whiles, in particular, it reseeks to the beginning of the
81359243Sobrien * line the while was on; hence the while placement restrictions.
81459243Sobrien */
81559243SobrienEXTERN struct Ain lineloc;
81659243Sobrien
81759243SobrienEXTERN bool    cantell;		/* Is current source tellable ? */
81859243Sobrien
81959243Sobrien/*
82059243Sobrien * Input lines are parsed into doubly linked circular
82159243Sobrien * lists of words of the following form.
82259243Sobrien */
82359243Sobrienstruct wordent {
82459243Sobrien    Char   *word;
82559243Sobrien    struct wordent *prev;
82659243Sobrien    struct wordent *next;
82759243Sobrien};
82859243Sobrien
82959243Sobrien/*
83059243Sobrien * During word building, both in the initial lexical phase and
83159243Sobrien * when expanding $ variable substitutions, expansion by `!' and `$'
83259243Sobrien * must be inhibited when reading ahead in routines which are themselves
83359243Sobrien * processing `!' and `$' expansion or after characters such as `\' or in
83459243Sobrien * quotations.  The following flags are passed to the getC routines
83559243Sobrien * telling them which of these substitutions are appropriate for the
83659243Sobrien * next character to be returned.
83759243Sobrien */
83859243Sobrien#define	DODOL	1
83959243Sobrien#define	DOEXCL	2
84059243Sobrien#define	DOALL	DODOL|DOEXCL
84159243Sobrien
84259243Sobrien/*
84359243Sobrien * Labuf implements a general buffer for lookahead during lexical operations.
84459243Sobrien * Text which is to be placed in the input stream can be stuck here.
84559243Sobrien * We stick parsed ahead $ constructs during initial input,
84659243Sobrien * process id's from `$$', and modified variable values (from qualifiers
84759243Sobrien * during expansion in sh.dol.c) here.
84859243Sobrien */
84959243SobrienEXTERN Char   *lap;
85059243Sobrien
85159243Sobrien/*
85259243Sobrien * Parser structure
85359243Sobrien *
85459243Sobrien * Each command is parsed to a tree of command structures and
85559243Sobrien * flags are set bottom up during this process, to be propagated down
85659243Sobrien * as needed during the semantics/exeuction pass (sh.sem.c).
85759243Sobrien */
85859243Sobrienstruct command {
85959243Sobrien    unsigned char   t_dtyp;	/* Type of node 		 */
86059243Sobrien#define	NODE_COMMAND	1	/* t_dcom <t_dlef >t_drit	 */
86159243Sobrien#define	NODE_PAREN	2	/* ( t_dspr ) <t_dlef >t_drit	 */
86259243Sobrien#define	NODE_PIPE	3	/* t_dlef | t_drit		 */
86359243Sobrien#define	NODE_LIST	4	/* t_dlef ; t_drit		 */
86459243Sobrien#define	NODE_OR		5	/* t_dlef || t_drit		 */
86559243Sobrien#define	NODE_AND	6	/* t_dlef && t_drit		 */
86659243Sobrien    unsigned char   t_nice;	/* Nice value			 */
86759243Sobrien#ifdef apollo
86859243Sobrien    unsigned char   t_systype;	/* System environment		 */
86959243Sobrien#endif
87059243Sobrien    unsigned long   t_dflg;	/* Flags, e.g. F_AMPERSAND|... 	 */
87159243Sobrien/* save these when re-doing 	 */
87259243Sobrien#ifndef apollo
87359243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP|F_HUP)
87459243Sobrien#else
87559243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
87659243Sobrien#endif
87759243Sobrien#define	F_AMPERSAND	(1<<0)	/* executes in background	 */
87859243Sobrien#define	F_APPEND	(1<<1)	/* output is redirected >>	 */
87959243Sobrien#define	F_PIPEIN	(1<<2)	/* input is a pipe		 */
88059243Sobrien#define	F_PIPEOUT	(1<<3)	/* output is a pipe		 */
88159243Sobrien#define	F_NOFORK	(1<<4)	/* don't fork, last ()ized cmd	 */
88259243Sobrien#define	F_NOINTERRUPT	(1<<5)	/* should be immune from intr's */
88359243Sobrien/* spare */
88459243Sobrien#define	F_STDERR	(1<<7)	/* redirect unit 2 with unit 1	 */
88559243Sobrien#define	F_OVERWRITE	(1<<8)	/* output was !			 */
88659243Sobrien#define	F_READ		(1<<9)	/* input redirection is <<	 */
88759243Sobrien#define	F_REPEAT	(1<<10)	/* reexec aft if, repeat,...	 */
88859243Sobrien#define	F_NICE		(1<<11)	/* t_nice is meaningful 	 */
88959243Sobrien#define	F_NOHUP		(1<<12)	/* nohup this command 		 */
89059243Sobrien#define	F_TIME		(1<<13)	/* time this command 		 */
89159243Sobrien#define F_BACKQ		(1<<14)	/* command is in ``		 */
89259243Sobrien#define F_HUP		(1<<15)	/* hup this command		 */
89359243Sobrien#ifdef apollo
89459243Sobrien#define F_VER		(1<<16)	/* execute command under SYSTYPE */
89559243Sobrien#endif
89659243Sobrien    union {
89759243Sobrien	Char   *T_dlef;		/* Input redirect word 		 */
89859243Sobrien	struct command *T_dcar;	/* Left part of list/pipe 	 */
89959243Sobrien    }       L;
90059243Sobrien    union {
90159243Sobrien	Char   *T_drit;		/* Output redirect word 	 */
90259243Sobrien	struct command *T_dcdr;	/* Right part of list/pipe 	 */
90359243Sobrien    }       R;
90459243Sobrien#define	t_dlef	L.T_dlef
90559243Sobrien#define	t_dcar	L.T_dcar
90659243Sobrien#define	t_drit	R.T_drit
90759243Sobrien#define	t_dcdr	R.T_dcdr
90859243Sobrien    Char  **t_dcom;		/* Command/argument vector 	 */
90959243Sobrien    struct command *t_dspr;	/* Pointer to ()'d subtree 	 */
91059243Sobrien};
91159243Sobrien
91259243Sobrien
91359243Sobrien/*
91459243Sobrien * The keywords for the parser
91559243Sobrien */
91659243Sobrien#define	TC_BREAK	0
91759243Sobrien#define	TC_BRKSW	1
91859243Sobrien#define	TC_CASE		2
91959243Sobrien#define	TC_DEFAULT 	3
92059243Sobrien#define	TC_ELSE		4
92159243Sobrien#define	TC_END		5
92259243Sobrien#define	TC_ENDIF	6
92359243Sobrien#define	TC_ENDSW	7
92459243Sobrien#define	TC_EXIT		8
92559243Sobrien#define	TC_FOREACH	9
92659243Sobrien#define	TC_GOTO		10
92759243Sobrien#define	TC_IF		11
92859243Sobrien#define	TC_LABEL	12
92959243Sobrien#define	TC_LET		13
93059243Sobrien#define	TC_SET		14
93159243Sobrien#define	TC_SWITCH	15
93259243Sobrien#define	TC_TEST		16
93359243Sobrien#define	TC_THEN		17
93459243Sobrien#define	TC_WHILE	18
93559243Sobrien
93659243Sobrien/*
93759243Sobrien * These are declared here because they want to be
93859243Sobrien * initialized in sh.init.c (to allow them to be made readonly)
93959243Sobrien */
94059243Sobrien
94159243Sobrien#if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
94259243Sobrien    /* Avoid hpux ansi mode spurious warnings */
94359243Sobrientypedef void (*bfunc_t) ();
94459243Sobrien#else
94559243Sobrientypedef void (*bfunc_t) __P((Char **, struct command *));
94659243Sobrien#endif /* hpux && __STDC__ && !__GNUC__ */
94759243Sobrien
94859243Sobrienextern struct biltins {
94959243Sobrien    char   *bname;
95059243Sobrien    bfunc_t bfunct;
95159243Sobrien    int     minargs, maxargs;
95259243Sobrien} bfunc[];
95359243Sobrienextern int nbfunc;
95469408Sache#ifdef WINNT_NATIVE
95559243Sobrienextern struct biltins  nt_bfunc[];
95659243Sobrienextern int nt_nbfunc;
95769408Sache#endif /* WINNT_NATIVE*/
95859243Sobrien
95959243Sobrienextern struct srch {
96059243Sobrien    char   *s_name;
96159243Sobrien    int     s_value;
96259243Sobrien}       srchn[];
96359243Sobrienextern int nsrchn;
96459243Sobrien
96559243Sobrien/*
96659243Sobrien * Structure defining the existing while/foreach loops at this
96759243Sobrien * source level.  Loops are implemented by seeking back in the
96859243Sobrien * input.  For foreach (fe), the word list is attached here.
96959243Sobrien */
97059243SobrienEXTERN struct whyle {
97159243Sobrien    struct Ain   w_start;	/* Point to restart loop */
97259243Sobrien    struct Ain   w_end;		/* End of loop (0 if unknown) */
97359243Sobrien    Char  **w_fe, **w_fe0;	/* Current/initial wordlist for fe */
97459243Sobrien    Char   *w_fename;		/* Name for fe */
97559243Sobrien    struct whyle *w_next;	/* Next (more outer) loop */
97659243Sobrien}      *whyles;
97759243Sobrien
97859243Sobrien/*
97959243Sobrien * Variable structure
98059243Sobrien *
98159243Sobrien * Aliases and variables are stored in AVL balanced binary trees.
98259243Sobrien */
98359243SobrienEXTERN struct varent {
98459243Sobrien    Char  **vec;		/* Array of words which is the value */
98559243Sobrien    Char   *v_name;		/* Name of variable/alias */
98659243Sobrien    int	    v_flags;		/* Flags */
98759243Sobrien#define VAR_ALL		-1
98859243Sobrien#define VAR_READONLY	1
98959243Sobrien#define VAR_READWRITE	2
99059243Sobrien#define VAR_NOGLOB	4
99159243Sobrien#define VAR_FIRST       32
99259243Sobrien#define VAR_LAST        64
99359243Sobrien    struct varent *v_link[3];	/* The links, see below */
99459243Sobrien    int     v_bal;		/* Balance factor */
99559243Sobrien}       shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
99659243Sobrien
99759243Sobrien#define v_left		v_link[0]
99859243Sobrien#define v_right		v_link[1]
99959243Sobrien#define v_parent	v_link[2]
100059243Sobrien
100159243Sobrien#define adrof(v)	adrof1(v, &shvhed)
100259243Sobrien#define varval(v)	value1(v, &shvhed)
100359243Sobrien
100459243Sobrien/*
100559243Sobrien * The following are for interfacing redo substitution in
100659243Sobrien * aliases to the lexical routines.
100759243Sobrien */
100859243SobrienEXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
100959243SobrienEXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
101059243SobrienEXTERN Char  **alvec IZERO_STRUCT,
101159243Sobrien	      *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
101259243Sobrien
101359243Sobrien/*
101459243Sobrien * Filename/command name expansion variables
101559243Sobrien */
101659243SobrienEXTERN int   gflag;		/* After tglob -> is globbing needed? */
101759243Sobrien
101859243Sobrien#define MAXVARLEN 30		/* Maximum number of char in a variable name */
101959243Sobrien
102059243Sobrien#ifndef MAXPATHLEN
102159243Sobrien# define MAXPATHLEN 2048
102259243Sobrien#endif /* MAXPATHLEN */
102359243Sobrien
102459243Sobrien#ifndef MAXNAMLEN
102559243Sobrien# define MAXNAMLEN 512
102659243Sobrien#endif /* MAXNAMLEN */
102759243Sobrien
102859243Sobrien#ifndef HAVENOLIMIT
102959243Sobrien/*
103059243Sobrien * resource limits
103159243Sobrien */
103259243Sobrienextern struct limits {
103359243Sobrien    int     limconst;
103459243Sobrien    char   *limname;
103559243Sobrien    int     limdiv;
103659243Sobrien    char   *limscale;
103759243Sobrien} limits[];
103859243Sobrien#endif /* !HAVENOLIMIT */
103959243Sobrien
104059243Sobrien/*
104159243Sobrien * Variables for filename expansion
104259243Sobrien */
104359243Sobrienextern Char **gargv;		/* Pointer to the (stack) arglist */
104459243Sobrienextern int    gargc;		/* Number args in gargv */
104559243Sobrien
104659243Sobrien/*
104759243Sobrien * Variables for command expansion.
104859243Sobrien */
104959243Sobrienextern Char **pargv;		/* Pointer to the argv list space */
105059243SobrienEXTERN Char  *pargs;		/* Pointer to start current word */
105159243SobrienEXTERN long   pnleft;		/* Number of chars left in pargs */
105259243SobrienEXTERN Char  *pargcp;		/* Current index into pargs */
105359243Sobrien
105459243Sobrien/*
105559243Sobrien * History list
105659243Sobrien *
105759243Sobrien * Each history list entry contains an embedded wordlist
105859243Sobrien * from the scanner, a number for the event, and a reference count
105959243Sobrien * to aid in discarding old entries.
106059243Sobrien *
106159243Sobrien * Essentially "invisible" entries are put on the history list
106259243Sobrien * when history substitution includes modifiers, and thrown away
106359243Sobrien * at the next discarding since their event numbers are very negative.
106459243Sobrien */
106559243SobrienEXTERN struct Hist {
106659243Sobrien    struct wordent Hlex;
106759243Sobrien    int     Hnum;
106859243Sobrien    int     Href;
106959243Sobrien    time_t  Htime;
107059243Sobrien    Char   *histline;
107159243Sobrien    struct Hist *Hnext;
107259243Sobrien}       Histlist IZERO_STRUCT;
107359243Sobrien
107459243SobrienEXTERN struct wordent paraml;	/* Current lexical word list */
107559243SobrienEXTERN int     eventno;		/* Next events number */
107659243SobrienEXTERN int     lastev;		/* Last event reference (default) */
107759243Sobrien
107859243SobrienEXTERN Char    HIST;		/* history invocation character */
107959243SobrienEXTERN Char    HISTSUB;		/* auto-substitute character */
108059243SobrienEXTERN Char    PRCH;		/* Prompt symbol for regular users */
108159243SobrienEXTERN Char    PRCHROOT;	/* Prompt symbol for root */
108259243Sobrien
108359243Sobrien/*
108459243Sobrien * For operating systems with single case filenames (OS/2)
108559243Sobrien */
108659243Sobrien#ifdef CASE_INSENSITIVE
108759243Sobrien# define samecase(x) (isupper((unsigned char)(x)) ? \
108859243Sobrien		      tolower((unsigned char)(x)) : (x))
108959243Sobrien#else
109059243Sobrien# define samecase(x) (x)
109159243Sobrien#endif /* CASE_INSENSITIVE */
109259243Sobrien
109359243Sobrien/*
109459243Sobrien * strings.h:
109559243Sobrien */
109659243Sobrien#ifndef SHORT_STRINGS
109759243Sobrien#define Strchr(a, b)  		strchr(a, b)
109859243Sobrien#define Strrchr(a, b)  		strrchr(a, b)
109959243Sobrien#define Strcat(a, b)  		strcat(a, b)
110059243Sobrien#define Strncat(a, b, c) 	strncat(a, b, c)
110159243Sobrien#define Strcpy(a, b)  		strcpy(a, b)
110259243Sobrien#define Strncpy(a, b, c) 	strncpy(a, b, c)
110359243Sobrien#define Strlen(a)		strlen(a)
110459243Sobrien#define Strcmp(a, b)		strcmp(a, b)
110559243Sobrien#define Strncmp(a, b, c)	strncmp(a, b, c)
110659243Sobrien
110759243Sobrien#define Strspl(a, b)		strspl(a, b)
110859243Sobrien#define Strsave(a)		strsave(a)
110959243Sobrien#define Strend(a)		strend(a)
111059243Sobrien#define Strstr(a, b)		strstr(a, b)
111159243Sobrien
111259243Sobrien#define str2short(a) 		(a)
111359243Sobrien#define blk2short(a) 		saveblk(a)
111459243Sobrien#define short2blk(a) 		saveblk(a)
111559243Sobrien#define short2str(a) 		strip(a)
111659243Sobrien#else
111759243Sobrien#define Strchr(a, b)		s_strchr(a, b)
111859243Sobrien#define Strrchr(a, b) 		s_strrchr(a, b)
111959243Sobrien#define Strcat(a, b)  		s_strcat(a, b)
112059243Sobrien#define Strncat(a, b, c) 	s_strncat(a, b, c)
112159243Sobrien#define Strcpy(a, b)  		s_strcpy(a, b)
112259243Sobrien#define Strncpy(a, b, c)	s_strncpy(a, b, c)
112359243Sobrien#define Strlen(a)		s_strlen(a)
112459243Sobrien#define Strcmp(a, b)		s_strcmp(a, b)
112559243Sobrien#define Strncmp(a, b, c)	s_strncmp(a, b, c)
112659243Sobrien
112759243Sobrien#define Strspl(a, b)		s_strspl(a, b)
112859243Sobrien#define Strsave(a)		s_strsave(a)
112959243Sobrien#define Strend(a)		s_strend(a)
113059243Sobrien#define Strstr(a, b)		s_strstr(a, b)
113159243Sobrien#endif
113259243Sobrien
113359243Sobrien/*
113459243Sobrien * setname is a macro to save space (see sh.err.c)
113559243Sobrien */
113659243SobrienEXTERN char   *bname;
113759243Sobrien
113859243Sobrien#define	setname(a)	(bname = (a))
113959243Sobrien
114059243Sobrien#ifdef VFORK
114159243SobrienEXTERN Char   *Vsav;
114259243SobrienEXTERN Char   *Vdp;
114359243SobrienEXTERN Char   *Vexpath;
114459243SobrienEXTERN char  **Vt;
114559243Sobrien#endif /* VFORK */
114659243Sobrien
114759243SobrienEXTERN Char  **evalvec;
114859243SobrienEXTERN Char   *evalp;
114959243Sobrien
115059243Sobrienextern struct mesg {
115159243Sobrien    char   *iname;		/* name from /usr/include */
115259243Sobrien    char   *pname;		/* print name */
115359243Sobrien}       mesg[];
115459243Sobrien
115559243Sobrien/* word_chars is set by default to WORD_CHARS but can be overridden by
115659243Sobrien   the worchars variable--if unset, reverts to WORD_CHARS */
115759243Sobrien
115859243SobrienEXTERN Char   *word_chars;
115959243Sobrien
116059243Sobrien#define WORD_CHARS "*?_-.[]~="	/* default chars besides alnums in words */
116159243Sobrien
116259243SobrienEXTERN Char   *STR_SHELLPATH;
116359243Sobrien
116459243Sobrien#ifdef _PATH_BSHELL
116559243SobrienEXTERN Char   *STR_BSHELL;
116659243Sobrien#endif
116759243SobrienEXTERN Char   *STR_WORD_CHARS;
116859243SobrienEXTERN Char  **STR_environ IZERO;
116959243Sobrien
117059243Sobrienextern int     dont_free;	/* Tell free that we are in danger if we free */
117159243Sobrien
117259243Sobrienextern Char    *INVPTR;
117359243Sobrienextern Char    **INVPPTR;
117459243Sobrien
117559243Sobrienextern char    *progname;
117659243Sobrienextern int	tcsh;
117759243Sobrien
117859243Sobrien#include "tc.h"
117959243Sobrien#include "sh.decls.h"
118059243Sobrien
118159243Sobrien/*
118259243Sobrien * To print system call errors...
118359243Sobrien */
118459243Sobrien#ifdef BSD4_4
118559243Sobrien# include <errno.h>
118659243Sobrien#else
118759243Sobrien# ifndef linux
118859243Sobrien#  ifdef NEEDstrerror
118959243Sobrienextern char *sys_errlist[];
119059243Sobrien#  endif
119159243Sobrienextern int errno, sys_nerr;
119259243Sobrien# endif /* !linux */
119359243Sobrien#endif
119459243Sobrien
119569408Sache#ifndef WINNT_NATIVE
119659243Sobrien# ifdef NLS_CATALOGS
119759243Sobrien#  ifdef linux
119859243Sobrien#   include <locale.h>
119959243Sobrien#   ifdef notdef
120059243Sobrien#    include <localeinfo.h>	/* Has this changed ? */
120159243Sobrien#   endif
120259243Sobrien#   include <features.h>
120359243Sobrien#  endif
120459243Sobrien#  ifdef SUNOS4
120559243Sobrien   /* Who stole my nl_types.h? :-(
120659243Sobrien    * All this stuff is in the man pages, but nowhere else?
120759243Sobrien    * This does not link right now...
120859243Sobrien    */
120959243Sobrien   typedef void *nl_catd;
121059243Sobrien   extern const char * catgets __P((nl_catd, int, int, const char *));
121159243Sobrien   nl_catd catopen __P((const char *, int));
121259243Sobrien   int catclose __P((nl_catd));
121359243Sobrien#  else
121459243Sobrien#   ifdef __uxps__
121559243Sobrien#    define gettxt gettxt_ds
121659243Sobrien#   endif
121759243Sobrien#   include <nl_types.h>
121859243Sobrien#   ifdef __uxps__
121959243Sobrien#    undef gettxt
122059243Sobrien#   endif
122159243Sobrien#  endif
122259243Sobrien#  ifndef MCLoadBySet
122359243Sobrien#   define MCLoadBySet 0
122459243Sobrien#  endif
122559243SobrienEXTERN nl_catd catd;
122659243Sobrien#  define CGETS(b, c, d)	catgets(catd, b, c, d)
122759243Sobrien#  define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
122859243Sobrien# else
122959243Sobrien#  define CGETS(b, c, d)	d
123059243Sobrien#  define CSAVS(b, c, d)	d
123159243Sobrien# endif
123269408Sache#else /* WINNT_NATIVE */
123359243Sobrien# define CGETS(b, c, d)	nt_cgets( b, c, d)
123459243Sobrien# define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
123569408Sache#endif /* WINNT_NATIVE */
123659243Sobrien
123759243Sobrien/*
123859243Sobrien * Since on some machines characters are unsigned, and the signed
123959243Sobrien * keyword is not universally implemented, we treat all characters
124059243Sobrien * as unsigned and sign extend them where we need.
124159243Sobrien */
124259243Sobrien#define SIGN_EXTEND_CHAR(a)	(((a) & 0x80) ? ((a) | ~0x7f) : (a))
124359243Sobrien
124459243Sobrien#endif /* _h_sh */
1245