159243Sobrien/*
259243Sobrien * sh.h: Catch it all globals and includes file!
359243Sobrien */
459243Sobrien/*-
559243Sobrien * Copyright (c) 1980, 1991 The Regents of the University of California.
659243Sobrien * All rights reserved.
759243Sobrien *
859243Sobrien * Redistribution and use in source and binary forms, with or without
959243Sobrien * modification, are permitted provided that the following conditions
1059243Sobrien * are met:
1159243Sobrien * 1. Redistributions of source code must retain the above copyright
1259243Sobrien *    notice, this list of conditions and the following disclaimer.
1359243Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1459243Sobrien *    notice, this list of conditions and the following disclaimer in the
1559243Sobrien *    documentation and/or other materials provided with the distribution.
16100616Smp * 3. Neither the name of the University nor the names of its contributors
1759243Sobrien *    may be used to endorse or promote products derived from this software
1859243Sobrien *    without specific prior written permission.
1959243Sobrien *
2059243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2159243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2259243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2359243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2459243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2559243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2659243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2759243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2859243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2959243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3059243Sobrien * SUCH DAMAGE.
3159243Sobrien */
3259243Sobrien#ifndef _h_sh
3359243Sobrien#define _h_sh
3459243Sobrien
3559243Sobrien#include "config.h"
3659243Sobrien
37145479Smp#include <stddef.h>
38167465Smp#include <signal.h>
39145479Smp
40145479Smp#ifdef HAVE_ICONV
41145479Smp# include <iconv.h>
42100616Smp#endif
43145479Smp
44145479Smp#ifdef HAVE_STDINT_H
45145479Smp# include <stdint.h>
46100616Smp#endif
47100616Smp
48145479Smp#ifdef HAVE_INTTYPES_H
49145479Smp# include <inttypes.h>
50145479Smp#endif
51145479Smp
52145479Smp#if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
53145479Smptypedef unsigned long intptr_t;
54145479Smp#endif
55145479Smp
5659243Sobrien#ifndef EXTERN
5759243Sobrien# define EXTERN extern
5859243Sobrien#else /* !EXTERN */
5969408Sache# ifdef WINNT_NATIVE
6059243Sobrien#  define IZERO = 0
6159243Sobrien#  define IZERO_STRUCT = {0}
6269408Sache# endif /* WINNT_NATIVE */
6359243Sobrien#endif /* EXTERN */
6459243Sobrien
6559243Sobrien#ifndef IZERO
6659243Sobrien# define IZERO
6759243Sobrien#endif /* IZERO */
6859243Sobrien#ifndef IZERO_STRUCT
6959243Sobrien# define IZERO_STRUCT
70145479Smp#endif /* IZERO_STRUCT */
7159243Sobrien
7269408Sache#ifndef WINNT_NATIVE
7359243Sobrien# define INIT_ZERO
7459243Sobrien# define INIT_ZERO_STRUCT
75167465Smp# define force_read xread
7669408Sache#endif /*!WINNT_NATIVE */
77231990Smp
78231990Smp#if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
79231990Smp#define AUTOSET_KANJI
80231990Smp#endif
8159243Sobrien/*
8259243Sobrien * Sanity
8359243Sobrien */
8459243Sobrien#if defined(_POSIX_SOURCE) && !defined(POSIX)
8559243Sobrien# define POSIX
8659243Sobrien#endif
8759243Sobrien
8859243Sobrien#if defined(POSIXJOBS) && !defined(BSDJOBS)
8959243Sobrien# define BSDJOBS
9059243Sobrien#endif
9159243Sobrien
92231990Smp#define TMP_TEMPLATE ".XXXXXX"
93231990Smp
9459243Sobrien#ifdef SHORT_STRINGS
95167465Smp# ifdef WIDE_STRINGS
96145479Smp#include <wchar.h>
97231990Smp#  ifdef UTF16_STRINGS
98231990Smptypedef wint_t Char;
99231990Smp#  else
100145479Smptypedef wchar_t Char;
101231990Smp#endif
102145479Smptypedef unsigned long uChar;
103145479Smptypedef wint_t eChar; /* Can contain any Char value or CHAR_ERR */
104145479Smp#define CHAR_ERR WEOF /* Pretty please, use bit 31... */
105145479Smp#define normal_mbtowc(PWC, S, N) rt_mbtowc(PWC, S, N)
106231990Smp#define reset_mbtowc() TCSH_IGNORE(mbtowc(NULL, NULL, 0))
107145479Smp# else
10859243Sobrientypedef short Char;
10959243Sobrientypedef unsigned short uChar;
110145479Smptypedef int eChar;
111145479Smp#define CHAR_ERR (-1)
112145479Smp#define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
113145479Smp#define reset_mbtowc() ((void)0)
114145479Smp# endif
11559243Sobrien# define SAVE(a) (Strsave(str2short(a)))
11659243Sobrien#else
11759243Sobrientypedef char Char;
11859243Sobrientypedef unsigned char uChar;
119145479Smptypedef int eChar;
120145479Smp#define CHAR_ERR (-1)
121145479Smp#define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
122145479Smp#define reset_mbtowc() ((void)0)
12359243Sobrien# define SAVE(a) (strsave(a))
124145479Smp#endif
12559243Sobrien
126231990Smp#if !defined(__inline) && !defined(__GNUC__) && !defined(_MSC_VER)
127231990Smp#define __inline
128231990Smp#endif
129316957Sdchagin#ifdef _MSC_VER
130316957Sdchagin#define TCSH_PTRDIFF_T_FMT "I"
131316957Sdchagin#else
132316957Sdchagin#define TCSH_PTRDIFF_T_FMT "t"
133316957Sdchagin#endif
13459243Sobrien/* Elide unused argument warnings */
13559243Sobrien#define USE(a)	(void) (a)
136231990Smp#define TCSH_IGNORE(a)	tcsh_ignore((intptr_t)a)
137231990Smpstatic __inline void tcsh_ignore(intptr_t a)
138195609Smp{
139195609Smp    USE(a);
140195609Smp}
14159243Sobrien
14259243Sobrien/*
14359243Sobrien * Return true if the path is absolute
14459243Sobrien */
145131962Smp#if defined(WINNT_NATIVE)
146100616Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
147100616Smp    (Isalpha((p)[0]) && (p)[1] == ':'))
148131962Smp#elif defined(__CYGWIN__)
149131962Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
150131962Smp    (Isalpha((p)[0]) && (p)[1] == ':' && \
151131962Smp     ((p)[2] == '\0' || (p)[2] == '/')))
152100616Smp#else /* !WINNT_NATIVE && !__CYGWIN__ */
15359243Sobrien# define ABSOLUTEP(p)	(*(p) == '/')
154100616Smp#endif /* WINNT_NATIVE || __CYGWIN__ */
15559243Sobrien
15659243Sobrien/*
15759243Sobrien * Fundamental definitions which may vary from system to system.
15859243Sobrien *
15959243Sobrien *	BUFSIZE		The i/o buffering size; also limits word size
16059243Sobrien *	MAILINTVL	How often to mailcheck; more often is more expensive
16159243Sobrien */
16259243Sobrien#ifdef BUFSIZE
16361524Sobrien# if	   BUFSIZE < 4096
16459243Sobrien#  undef   BUFSIZE
16561524Sobrien#  define  BUFSIZE	4096	/* buffer size should be no less than this */
16659243Sobrien# endif
16759243Sobrien#else
16861524Sobrien# define   BUFSIZE	4096
16959243Sobrien#endif /* BUFSIZE */
17059243Sobrien
17159243Sobrien#define FORKSLEEP	10	/* delay loop on non-interactive fork failure */
17259243Sobrien#define	MAILINTVL	600	/* 10 minutes */
17359243Sobrien
17459243Sobrien#ifndef INBUFSIZE
17559243Sobrien# define INBUFSIZE    2*BUFSIZE /* Num input characters on the command line */
17659243Sobrien#endif /* INBUFSIZE */
17759243Sobrien
17859243Sobrien
17959243Sobrien/*
18059243Sobrien * What our builtin echo looks like
18159243Sobrien */
18259243Sobrien#define NONE_ECHO	0
18359243Sobrien#define BSD_ECHO	1
18459243Sobrien#define SYSV_ECHO	2
18559243Sobrien#define BOTH_ECHO	(BSD_ECHO|SYSV_ECHO)
18659243Sobrien
18759243Sobrien#ifndef ECHO_STYLE
18859243Sobrien# if SYSVREL > 0
18959243Sobrien#  define ECHO_STYLE SYSV_ECHO
19059243Sobrien# else /* SYSVREL == 0 */
19159243Sobrien#  define ECHO_STYLE BSD_ECHO
19259243Sobrien# endif /* SYSVREL */
19359243Sobrien#endif /* ECHO_STYLE */
19459243Sobrien
195316957Sdchagin/* values for noclobber */
196316957Sdchagin#define NOCLOBBER_DEFAULT  1
197316957Sdchagin#define NOCLOBBER_NOTEMPTY 2
198316957Sdchagin#define NOCLOBBER_ASK      4
199316957Sdchagin
20059243Sobrien/*
20159243Sobrien * The shell moves std in/out/diag and the old std input away from units
20259243Sobrien * 0, 1, and 2 so that it is easy to set up these standards for invoked
20359243Sobrien * commands.
20459243Sobrien */
205145479Smp#define	FSAFE	5		/* We keep the first 5 descriptors untouched */
20659243Sobrien#define	FSHTTY	15		/* /dev/tty when manip pgrps */
20759243Sobrien#define	FSHIN	16		/* Preferred desc for shell input */
20859243Sobrien#define	FSHOUT	17		/* ... shell output */
20959243Sobrien#define	FSHDIAG	18		/* ... shell diagnostics */
21059243Sobrien#define	FOLDSTD	19		/* ... old std input */
21159243Sobrien
21259243Sobrien#ifdef PROF
21359243Sobrien#define	xexit(n)	done(n)
21459243Sobrien#endif
21559243Sobrien
21659243Sobrien#ifdef cray
21759243Sobrien# define word word_t           /* sys/types.h defines word.. bad move! */
21859243Sobrien#endif
21959243Sobrien
22059243Sobrien#include <sys/types.h>
22159243Sobrien
22259243Sobrien#ifdef cray
22359243Sobrien# undef word
22459243Sobrien#endif
22559243Sobrien
22659243Sobrien/*
22759243Sobrien * Path separator in environment variables
22859243Sobrien */
22959243Sobrien#ifndef PATHSEP
23069408Sache# if defined(__EMX__) || defined(WINNT_NATIVE)
23159243Sobrien#  define PATHSEP ';'
23259243Sobrien# else /* unix */
23359243Sobrien#  define PATHSEP ':'
23469408Sache# endif /* __EMX__ || WINNT_NATIVE */
23559243Sobrien#endif /* !PATHSEP */
23659243Sobrien
23783098Smp#if defined(__HP_CXD_SPP) && !defined(__hpux)
23859243Sobrien# include <sys/cnx_stat.h>
23959243Sobrien# define stat stat64
24059243Sobrien# define fstat fstat64
24159243Sobrien# define lstat lstat64
24283098Smp#endif /* __HP_CXD_SPP && !__hpux */
24359243Sobrien
244231990Smp#ifdef HAVE_LONG_LONG
245231990Smptypedef long long tcsh_number_t;
246231990Smp#else
247231990Smptypedef long tcsh_number_t;
248231990Smp#endif
24959243Sobrien/*
25059243Sobrien * This macro compares the st_dev field of struct stat. On aix on ibmESA
25159243Sobrien * st_dev is a structure, so comparison does not work.
25259243Sobrien */
25359243Sobrien#ifndef DEV_DEV_COMPARE
25459243Sobrien# define DEV_DEV_COMPARE(x,y)   ((x) == (y))
25559243Sobrien#endif /* DEV_DEV_COMPARE */
25659243Sobrien
25759243Sobrien#ifdef _SEQUENT_
25859243Sobrien# include <sys/procstats.h>
25959243Sobrien#endif /* _SEQUENT_ */
26069408Sache#if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
26159243Sobrien# include <sys/times.h>
26269408Sache#endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
26359243Sobrien
26459243Sobrien#ifdef NLS
26559243Sobrien# include <locale.h>
26659243Sobrien#endif /* NLS */
26759243Sobrien
26869408Sache#if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
26959243Sobrien# include <sys/param.h>
27069408Sache#endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
27159243Sobrien#include <sys/stat.h>
27259243Sobrien
27359243Sobrien#if defined(BSDTIMES) || defined(BSDLIMIT)
27459243Sobrien# include <sys/time.h>
27569408Sache# if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
27659243Sobrien#  include "/usr/ucbinclude/sys/resource.h"
27759243Sobrien# else
27859243Sobrien#  ifdef convex
27959243Sobrien#   define sysrusage cvxrusage
28059243Sobrien#   include <sys/sysinfo.h>
28159243Sobrien#  else
28259243Sobrien#   define sysrusage rusage
28359243Sobrien#   include <sys/resource.h>
28459243Sobrien#  endif /* convex */
28559243Sobrien# endif /* SYSVREL>3 */
28659243Sobrien#endif /* BSDTIMES */
28759243Sobrien
28869408Sache#ifndef WINNT_NATIVE
28959243Sobrien# ifndef POSIX
29059243Sobrien#  ifdef TERMIO
29159243Sobrien#   include <termio.h>
29259243Sobrien#  else /* SGTTY */
29359243Sobrien#   include <sgtty.h>
29459243Sobrien#  endif /* TERMIO */
29559243Sobrien# else /* POSIX */
29659243Sobrien#  ifndef _UWIN
29759243Sobrien#   include <termios.h>
29859243Sobrien#  else
29959243Sobrien#   include <termio.h>
30059243Sobrien#  endif /* _UWIN */
301231990Smp#  if SYSVREL > 3 || defined(__linux__)
30259243Sobrien#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
30359243Sobrien#   undef TIOCSLTC
30459243Sobrien#   undef CSWTCH
30559243Sobrien#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
30659243Sobrien#  endif /* SYSVREL > 3 */
30759243Sobrien# endif /* POSIX */
30869408Sache#endif /* WINNT_NATIVE */
30959243Sobrien
31059243Sobrien#ifdef sonyrisc
31159243Sobrien# include <sys/ttold.h>
31259243Sobrien#endif /* sonyrisc */
31359243Sobrien
31469408Sache#if defined(POSIX) && !defined(WINNT_NATIVE)
31559243Sobrien# include <unistd.h>
31659243Sobrien
31759243Sobrien/*
31859243Sobrien * the gcc+protoize version of <stdlib.h>
31959243Sobrien * redefines malloc(), so we define the following
32059243Sobrien * to avoid it.
32159243Sobrien */
322231990Smp# if defined(SYSMALLOC) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX)
32359243Sobrien#  define NO_FIX_MALLOC
32459243Sobrien#  include <stdlib.h>
325145479Smp# else /* glibc */
32659243Sobrien#  define _GNU_STDLIB_H
32759243Sobrien#  define malloc __malloc
32859243Sobrien#  define free __free
32959243Sobrien#  define calloc __calloc
33059243Sobrien#  define realloc __realloc
33159243Sobrien#  include <stdlib.h>
33259243Sobrien#  undef malloc
33359243Sobrien#  undef free
33459243Sobrien#  undef calloc
33559243Sobrien#  undef realloc
336145479Smp# endif /* glibc || sgi */
33769408Sache#endif /* POSIX && !WINNT_NATIVE */
338167465Smp#include <limits.h>
33959243Sobrien
340231990Smp#if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
34159243Sobrien# if !defined(pyr) && !defined(stellar)
34259243Sobrien#  include <time.h>
34359243Sobrien#  ifdef _MINIX
34459243Sobrien#   define HZ CLOCKS_PER_SEC
34559243Sobrien#  endif /* _MINIX */
34659243Sobrien# endif /* !pyr && !stellar */
34759243Sobrien#endif /* SYSVREL > 0 ||  _IBMR2 */
34859243Sobrien
34959243Sobrien/* In the following ifdef the DECOSF1 has been commented so that later
35059243Sobrien * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
35159243Sobrien */
35259243Sobrien#if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
353167465Smp# if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
35459243Sobrien#  include <sys/ioctl.h>
355231990Smp#  if SYSVREL > 3 || defined(__linux__)
356231990Smp#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
357231990Smp#   undef TIOCSLTC
358231990Smp#   undef CSWTCH
359231990Smp#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
360231990Smp#  endif /* SYSVREL > 3 */
36159243Sobrien# endif
36259243Sobrien#endif
36359243Sobrien
36459243Sobrien#if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
36559243Sobrien#undef CSWTCH
36659243Sobrien#define CSWTCH _POSIX_VDISABLE
36759243Sobrien#endif
36859243Sobrien
36969408Sache#if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
37059243Sobrien# include <sys/filio.h>
37159243Sobrien#endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
37259243Sobrien
373167465Smp#if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
37459243Sobrien# include <sys/file.h>
375167465Smp#endif	/* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
37659243Sobrien
37759243Sobrien#if !defined(O_RDONLY) || !defined(O_NDELAY)
37859243Sobrien# include <fcntl.h>
37959243Sobrien#endif
38059243Sobrien
38159243Sobrien#include <errno.h>
38259243Sobrien
38359243Sobrien#include <setjmp.h>
38459243Sobrien
385167465Smp#include <stdarg.h>
38659243Sobrien
387145479Smp#ifdef HAVE_DIRENT_H
38859243Sobrien# include <dirent.h>
38959243Sobrien#else
390145479Smp# ifdef HAVE_NDIR_H
39159243Sobrien#  include <ndir.h>
39259243Sobrien# else
39359243Sobrien#  include <sys/dir.h>
39459243Sobrien# endif
39559243Sobrien# define dirent direct
396145479Smp#endif /* HAVE_DIRENT_H */
397145479Smp#ifndef HAVE_STRUCT_DIRENT_D_INO
398145479Smp# define d_ino d_fileno
399145479Smp#endif
400167465Smp#if defined(hpux) || defined(sgi) || defined(OREO)
40159243Sobrien# include <stdio.h>	/* So the fgetpwent() prototypes work */
402167465Smp#endif /* hpux || sgi || OREO */
40369408Sache#ifndef WINNT_NATIVE
40459243Sobrien#include <pwd.h>
40559243Sobrien#include <grp.h>
40669408Sache#endif /* WINNT_NATIVE */
407145479Smp#ifdef HAVE_SHADOW_H
40859243Sobrien# include <shadow.h>
409145479Smp#endif /* HAVE_SHADOW_H */
410145479Smp#ifdef HAVE_AUTH_H
41159243Sobrien# include <auth.h>
412145479Smp#endif /* HAVE_AUTH_H */
41359243Sobrien#if defined(BSD) && !defined(POSIX)
41459243Sobrien# include <strings.h>
41559243Sobrien# define strchr(a, b) index(a, b)
41659243Sobrien# define strrchr(a, b) rindex(a, b)
41759243Sobrien#else
41859243Sobrien# include <string.h>
41959243Sobrien#endif /* BSD */
42059243Sobrien
42159243Sobrien/*
42259243Sobrien * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
42359243Sobrien * include it yet, so we include it here
42459243Sobrien */
42559243Sobrien#if defined(sgi) && SYSVREL > 3
42659243Sobrien# include <sys/cdefs.h>
42759243Sobrien#endif /* sgi && SYSVREL > 3 */
42859243Sobrien
42959243Sobrien#ifdef REMOTEHOST
43059243Sobrien# ifdef ISC
43159243Sobrien#  undef MAXHOSTNAMELEN	/* Busted headers? */
43259243Sobrien# endif
43359243Sobrien
43459243Sobrien# include <netinet/in.h>
43559243Sobrien# include <arpa/inet.h>
43659243Sobrien# include <sys/socket.h>
437145479Smp# if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
438100616Smp#  if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
439100616Smp#   if !defined(sgi)
440316957Sdchagin#    define INET6
441100616Smp#   endif /* sgi */
442100616Smp#  endif /* __APPLE__ */
44369408Sache# endif
44459243Sobrien# include <sys/uio.h>	/* For struct iovec */
44559243Sobrien#endif /* REMOTEHOST */
44659243Sobrien
44759243Sobrien#ifdef PURIFY
44859243Sobrien/* exit normally, allowing purify to trace leaks */
44959243Sobrien# define _exit		exit
450316957Sdchagin#endif /* !PURIFY */
45159243Sobrien
45269408Sache/*
45369408Sache * ASCII vs. EBCDIC
45469408Sache */
45569408Sache#if 'Z' - 'A' == 25
45669408Sache# ifndef IS_ASCII
45769408Sache#  define IS_ASCII
45869408Sache# endif
45969408Sache#endif
46069408Sache
46159243Sobrien#include "sh.types.h"
46259243Sobrien
463167465Smp#if !HAVE_DECL_GETPGRP
464145479Smp# ifndef GETPGRP_VOID
465167465Smpextern pid_t getpgrp (int);
466145479Smp# else
467167465Smpextern pid_t getpgrp (void);
468145479Smp# endif
469145479Smp#endif
47059243Sobrien
47159243Sobrien#ifndef lint
47259243Sobrientypedef ptr_t memalign_t;
47359243Sobrien#else
47459243Sobrientypedef union {
47559243Sobrien    char    am_char, *am_char_p;
47659243Sobrien    short   am_short, *am_short_p;
47759243Sobrien    int     am_int, *am_int_p;
47859243Sobrien    long    am_long, *am_long_p;
47959243Sobrien    float   am_float, *am_float_p;
48059243Sobrien    double  am_double, *am_double_p;
48159243Sobrien}      *memalign_t;
48259243Sobrien
48359243Sobrien# define malloc		lint_malloc
48459243Sobrien# define free		lint_free
48559243Sobrien# define realloc	lint_realloc
48659243Sobrien# define calloc		lint_calloc
48759243Sobrien#endif
48859243Sobrien
489167465Smp#ifdef SYSMALLOC
490167465Smp# define xmalloc(i)	smalloc(i)
491167465Smp# define xrealloc(p, i)	srealloc(p, i)
492167465Smp# define xcalloc(n, s)	scalloc(n, s)
493167465Smp# define xfree		sfree
49459243Sobrien#else
495167465Smp# define xmalloc(i)  	malloc(i)
496167465Smp# define xrealloc(p, i)	realloc(p, i)
497167465Smp# define xcalloc(n, s)	calloc(n, s)
498167465Smp# define xfree	 	free
499167465Smp#endif /* SYSMALLOC */
50059243Sobrien#include "sh.char.h"
50159243Sobrien#include "sh.err.h"
50259243Sobrien#include "sh.dir.h"
50359243Sobrien#include "sh.proc.h"
50459243Sobrien
50559243Sobrien#include "pathnames.h"
50659243Sobrien
50759243Sobrien
50859243Sobrien/*
50959243Sobrien * C shell
51059243Sobrien *
51159243Sobrien * Bill Joy, UC Berkeley
51259243Sobrien * October, 1978; May 1980
51359243Sobrien *
51459243Sobrien * Jim Kulp, IIASA, Laxenburg Austria
51559243Sobrien * April, 1980
51659243Sobrien */
51759243Sobrien
51859243Sobrien#ifdef HESIOD
51959243Sobrien# include <hesiod.h>
52059243Sobrien#endif /* HESIOD */
52159243Sobrien
52259243Sobrien#ifdef REMOTEHOST
52359243Sobrien# include <netdb.h>
52459243Sobrien#endif /* REMOTEHOST */
52559243Sobrien
52659243Sobrien#ifndef MAXHOSTNAMELEN
527167465Smp# ifdef HOST_NAME_MAX
528167465Smp#  define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
529167465Smp# elif defined(SCO) && (SYSVREL > 3)
53059243Sobrien#  include <sys/socket.h>
53159243Sobrien# else
53283098Smp#  define MAXHOSTNAMELEN 256
53359243Sobrien# endif
53459243Sobrien#endif /* MAXHOSTNAMELEN */
53559243Sobrien
53659243Sobrien
53759243Sobrien
53859243Sobrien#define	eq(a, b)	(Strcmp(a, b) == 0)
53959243Sobrien
54059243Sobrien/* globone() flags */
54159243Sobrien#define G_ERROR		0	/* default action: error if multiple words */
54259243Sobrien#define G_IGNORE	1	/* ignore the rest of the words		   */
54359243Sobrien#define G_APPEND	2	/* make a sentence by cat'ing the words    */
54459243Sobrien
54559243Sobrien/*
54659243Sobrien * Global flags
54759243Sobrien */
548145479SmpEXTERN int    chkstop IZERO;	/* Warned of stopped jobs... allow exit */
54959243Sobrien
55059243Sobrien#if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
55159243Sobrien# define CLOSE_ON_EXEC
55259243Sobrien#else
553145479SmpEXTERN int    didcch IZERO;	/* Have closed unused fd's for child */
55459243Sobrien#endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
55559243Sobrien
556145479SmpEXTERN int    didfds IZERO;	/* Have setup i/o fd's for child */
557145479SmpEXTERN int    doneinp IZERO;	/* EOF indicator after reset from readc */
558145479SmpEXTERN int    exiterr IZERO;	/* Exit if error or non-zero exit status */
559145479SmpEXTERN int    child IZERO;	/* Child shell ... errors cause exit */
560145479SmpEXTERN int    haderr IZERO;	/* Reset was because of an error */
561145479SmpEXTERN int    intty IZERO;	/* Input is a tty */
562145479SmpEXTERN int    intact IZERO;	/* We are interactive... therefore prompt */
563145479SmpEXTERN int    justpr IZERO;	/* Just print because of :p hist mod */
564145479SmpEXTERN int    loginsh IZERO;	/* We are a loginsh -> .login/.logout */
565145479SmpEXTERN int    neednote IZERO;	/* Need to pnotify() */
566145479SmpEXTERN int    noexec IZERO;	/* Don't execute, just syntax check */
567145479SmpEXTERN int    pjobs IZERO;	/* want to print jobs if interrupted */
568145479SmpEXTERN int    setintr IZERO;	/* Set interrupts on/off -> Wait intr... */
569316957SdchaginEXTERN int    handle_interrupt IZERO;/* Are we currently handling an interrupt? */
570145479SmpEXTERN int    havhash IZERO;	/* path hashing is available */
571145479SmpEXTERN int    editing IZERO;	/* doing filename expansion and line editing */
572145479SmpEXTERN int    noediting IZERO;	/* initial $term defaulted to noedit */
573145479SmpEXTERN int    bslash_quote IZERO;/* PWP: tcsh-style quoting?  (in sh.c) */
574231990SmpEXTERN int    anyerror IZERO;	/* propagate errors from pipelines/backq */
575195609SmpEXTERN int    compat_expr IZERO;/* csh-style expressions? */
576145479SmpEXTERN int    isoutatty IZERO;	/* is SHOUT a tty */
577145479SmpEXTERN int    isdiagatty IZERO;/* is SHDIAG a tty */
578145479SmpEXTERN int    is1atty IZERO;	/* is file descriptor 1 a tty (didfds mode) */
579145479SmpEXTERN int    is2atty IZERO;	/* is file descriptor 2 a tty (didfds mode) */
580145479SmpEXTERN int    arun IZERO;	/* Currently running multi-line-aliases */
581316957SdchaginEXTERN int    implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
582316957SdchaginEXTERN int    cdtohome IZERO;	/* cd without args goes home */
583145479SmpEXTERN int    inheredoc IZERO;	/* Currently parsing a heredoc */
584316957SdchaginEXTERN int    no_clobber IZERO;	/* no clobber enabled? 1=yes 2=notempty, 4=ask*/
585167465Smp/* We received a window change event */
586167465SmpEXTERN volatile sig_atomic_t windowchg IZERO;
587145479Smp#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
588145479SmpEXTERN int    dspmbyte_ls;
589145479Smp#endif
59059243Sobrien
59159243Sobrien/*
59259243Sobrien * Global i/o info
59359243Sobrien */
59459243SobrienEXTERN Char   *arginp IZERO;	/* Argument input for sh -c and internal `xx` */
59559243SobrienEXTERN int     onelflg IZERO;	/* 2 -> need line for -t, 1 -> exit on read */
59659243Sobrienextern Char   *ffile;		/* Name of shell file for $0 */
597145479Smpextern int    dolzero;		/* if $?0 should return true... */
59859243Sobrien
59959243Sobrienextern char *seterr;		/* Error message from scanner/parser */
600145479Smp#ifndef errno
60159243Sobrienextern int errno;		/* Error from C library routines */
60269408Sache#endif
60369408Sacheextern int exitset;
604167465Smp/* Temp name for << shell files in /tmp, for xfree() */
605167465SmpEXTERN Char   *shtemp IZERO;
60659243Sobrien
60759243Sobrien#ifdef BSDTIMES
60859243SobrienEXTERN struct timeval time0;	/* Time at which the shell started */
60959243SobrienEXTERN struct sysrusage ru0;
61059243Sobrien#else
61159243Sobrien# ifdef _SEQUENT_
61259243SobrienEXTERN timeval_t time0;		/* time at which shell started */
61359243SobrienEXTERN struct process_stats ru0;
61459243Sobrien# else /* _SEQUENT_ */
61559243Sobrien#  ifndef POSIX
61659243SobrienEXTERN time_t  time0;		/* time at which shell started */
61759243Sobrien#  else	/* POSIX */
61859243SobrienEXTERN clock_t time0;		/* time at which shell started */
61959243SobrienEXTERN clock_t clk_tck;
62059243Sobrien#  endif /* POSIX */
62159243SobrienEXTERN struct tms shtimes;	/* shell and child times for process timing */
62259243Sobrien# endif /* _SEQUENT_ */
623167465SmpEXTERN time_t seconds0;
62459243Sobrien#endif /* BSDTIMES */
62559243Sobrien
62659243Sobrien#ifndef HZ
62759243Sobrien# define HZ	100		/* for division into seconds */
62859243Sobrien#endif
62959243Sobrien
63059243Sobrien/*
63159243Sobrien * Miscellany
63259243Sobrien */
633316957SdchaginEXTERN pid_t   mainpid;		/* pid of the main shell ($$) */
63459243SobrienEXTERN Char   *doldol;		/* Character pid for $$ */
635167465SmpEXTERN pid_t   backpid;		/* pid of the last background job */
63659243Sobrien
637316957Sdchagin
63859243Sobrien/*
63959243Sobrien * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
64059243Sobrien * cause pid's could be unsigned and that would break our -1 flag, and
64159243Sobrien * uid_t and gid_t are not defined in all the systems so I would have to
64259243Sobrien * make special cases for them. In the future...
64359243Sobrien */
644145479SmpEXTERN uid_t   uid, euid; 	/* Invokers real and effective */
645145479SmpEXTERN gid_t   gid, egid;	/* User and group ids */
646145479SmpEXTERN pid_t   opgrp,		/* Initial pgrp and tty pgrp */
64759243Sobrien               shpgrp,		/* Pgrp of shell */
64859243Sobrien               tpgrp;		/* Terminal process group */
64959243Sobrien				/* If tpgrp is -1, leave tty alone! */
65059243Sobrien
651167465SmpEXTERN Char   *Prompt;		/* The actual printed prompt or NULL */
652167465SmpEXTERN Char   *RPrompt;		/* Right-hand side prompt or NULL */
65359243Sobrien
65459243Sobrien/*
65559243Sobrien * To be able to redirect i/o for builtins easily, the shell moves the i/o
65659243Sobrien * descriptors it uses away from 0,1,2.
65759243Sobrien * Ideally these should be in units which are closed across exec's
65859243Sobrien * (this saves work) but for version 6, this is not usually possible.
65959243Sobrien * The desired initial values for these descriptors are defined in
66059243Sobrien * sh.local.h.
66159243Sobrien */
66259243SobrienEXTERN int   SHIN IZERO;	/* Current shell input (script) */
66359243SobrienEXTERN int   SHOUT IZERO;	/* Shell output */
66459243SobrienEXTERN int   SHDIAG IZERO;	/* Diagnostic output... shell errs go here */
66559243SobrienEXTERN int   OLDSTD IZERO;	/* Old standard input (def for cmds) */
66659243Sobrien
66759243Sobrien
668231990Smp#if (SYSVREL == 4 && defined(_UTS)) || defined(__linux__)
66959243Sobrien/*
67059243Sobrien * From: fadden@uts.amdahl.com (Andy McFadden)
67159243Sobrien * we need sigsetjmp for UTS4, but not UTS2.1
67259243Sobrien */
67359243Sobrien# define SIGSETJMP
67459243Sobrien#endif
67559243Sobrien
67659243Sobrien/*
67759243Sobrien * Error control
67859243Sobrien *
67959243Sobrien * Errors in scanning and parsing set up an error message to be printed
68059243Sobrien * at the end and complete.  Other errors always cause a reset.
68159243Sobrien * Because of source commands and .cshrc we need nested error catches.
68259243Sobrien */
68359243Sobrien
684167465Smp#ifdef SIGSETJMP
685354195Sbrooks   typedef struct { const char *f; size_t l; sigjmp_buf j; } jmp_buf_t;
686354195Sbrooks# define tcsh_setjmp() sigsetjmp(reslab.j, 1)
687354195Sbrooks# define tcsh_longjmp()   siglongjmp(reslab.j, 1)
688354195Sbrooks# define setexit()  (reslab.f = __func__, \
689354195Sbrooks		    reslab.l = __LINE__, \
690354195Sbrooks		    sigsetjmp(reslab.j, 1))
691354195Sbrooks# define _reset()   siglongjmp(reslab.j, 1)
69259243Sobrien#else
693354195Sbrooks   typedef struct { const char *f; size_t l; jmp_buf j; } jmp_buf_t;
694354195Sbrooks# define tcsh_setjmp() setjmp(reslab.j)
695354195Sbrooks# define tcsh_longjmp()   longjmp(reslab.j, 1)
696167465Smp#endif
69759243Sobrien
698354195Sbrooks#define setexit()  (reslab.f = __func__, \
699354195Sbrooks		    reslab.l = __LINE__, \
700354195Sbrooks		    tcsh_setjmp())
701354195Sbrooks#ifdef SETJMP_DEBUG
702354195Sbrooks# define _reset()   xprintf("reset %s %zu\n", reslab.f, reslab.l), \
703354195Sbrooks		    flush(), tcsh_longjmp()
704354195Sbrooks#else
705354195Sbrooks# define _reset()   tcsh_longjmp()
706354195Sbrooks#endif
707354195Sbrooks
708167465Smp#define getexit(a) (void) ((a) = reslab)
709167465Smp#define resexit(a) (void) (reslab = (a))
71059243Sobrien
711167465Smp#define cpybin(a, b) (void) ((a) = (b))
71259243Sobrien
71359243Sobrienextern jmp_buf_t reslab;
71459243Sobrien
71559243SobrienEXTERN Char   *gointr;		/* Label for an onintr transfer */
71659243Sobrien
717167465Smpextern struct sigaction parintr;	/* Parents interrupt catch */
718167465Smpextern struct sigaction parterm;	/* Parents terminate catch */
71959243Sobrien
72059243Sobrien/*
72159243Sobrien * Lexical definitions.
72259243Sobrien *
72359243Sobrien * All lexical space is allocated dynamically.
72459243Sobrien * The eighth/sixteenth bit of characters is used to prevent recognition,
72559243Sobrien * and eventually stripped.
72659243Sobrien */
72759243Sobrien#define		META		0200
72859243Sobrien#define		ASCII		0177
729145479Smp#ifdef WIDE_STRINGS		/* Implies SHORT_STRINGS */
730145479Smp/* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
731316957Sdchagin/*
732316957Sdchagin * Notice
733316957Sdchagin *
734316957Sdchagin * By fix for handling unicode name file, 32nd bit is used.
735316957Sdchagin * We need use '&' instead of '> or <' when comparing with INVALID_BYTE etc..
736316957Sdchagin * Cast to uChar is not recommended,
737316957Sdchagin *  becase Char is 4bytes but uChar is 8bytes on I32LP64. */
738316957Sdchagin# define	QUOTE		0x80000000
739316957Sdchagin# define	TRIM		0x7FFFFFFF /* Mask to strip quote bit */
740195609Smp# define	UNDER		0x08000000 /* Underline flag */
741195609Smp# define	BOLD		0x04000000 /* Bold flag */
742195609Smp# define	STANDOUT	0x02000000 /* Standout flag */
743195609Smp# define	LITERAL		0x01000000 /* Literal character flag */
744195609Smp# define	ATTRIBUTES	0x0F000000 /* The bits used for attributes */
745316957Sdchagin# define	INVALID_BYTE	0xF0000000 /* Invalid character on input */
746195609Smp# ifdef SOLARIS2
747195609Smp#  define	CHAR		0x30FFFFFF /* Mask to mask out the character */
748195609Smp# else
749195609Smp#  define	CHAR		0x00FFFFFF /* Mask to mask out the character */
750195609Smp# endif
751145479Smp#elif defined (SHORT_STRINGS)
75259243Sobrien# define	QUOTE 	((Char)	0100000)/* 16nth char bit used for 'ing */
753145479Smp# define	TRIM		0073777	/* Mask to strip quote/lit bit */
75459243Sobrien# define	UNDER		0040000	/* Underline flag */
75559243Sobrien# define	BOLD		0020000	/* Bold flag */
75659243Sobrien# define	STANDOUT	0010000	/* Standout flag */
75759243Sobrien# define	LITERAL		0004000	/* Literal character flag */
75859243Sobrien# define	ATTRIBUTES	0074000	/* The bits used for attributes */
759145479Smp# define	INVALID_BYTE	0
76059243Sobrien# define	CHAR		0000377	/* Mask to mask out the character */
76159243Sobrien#else
76259243Sobrien# define	QUOTE 	((Char)	0200)	/* Eighth char bit used for 'ing */
76359243Sobrien# define	TRIM		0177	/* Mask to strip quote bit */
76459243Sobrien# define	UNDER		0000000	/* No extra bits to do both */
76559243Sobrien# define	BOLD		0000000	/* Bold flag */
76659243Sobrien# define	STANDOUT	META	/* Standout flag */
76759243Sobrien# define	LITERAL		0000000	/* Literal character flag */
76859243Sobrien# define	ATTRIBUTES	0200	/* The bits used for attributes */
769145479Smp# define	INVALID_BYTE	0
77059243Sobrien# define	CHAR		0000177	/* Mask to mask out the character */
771167465Smp#endif
772145479Smp#define		CHAR_DBWIDTH	(LITERAL|(LITERAL-1))
77359243Sobrien
774316957Sdchagin# define 	MAX_UTF32	0x7FFFFFFF	/* max UTF32 is U+7FFFFFFF */
775316957Sdchagin
77659243SobrienEXTERN int     AsciiOnly;	/* If set only 7 bits expected in characters */
77759243Sobrien
77859243Sobrien/*
77959243Sobrien * Each level of input has a buffered input structure.
78059243Sobrien * There are one or more blocks of buffered input for each level,
78159243Sobrien * exactly one if the input is seekable and tell is available.
78259243Sobrien * In other cases, the shell buffers enough blocks to keep all loops
78359243Sobrien * in the buffer.
784167465Smp *
785167465Smp * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
786167465Smp * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
787167465Smp * fbuf indexing).
788167465Smp *
789167465Smp * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
790167465Smp * is no difference between byte and character offsets.
79159243Sobrien */
79259243SobrienEXTERN struct Bin {
793145479Smp    off_t   Bfseekp;		/* Seek pointer, generally != lseek() value */
79459243Sobrien    off_t   Bfbobp;		/* Seekp of beginning of buffers */
79559243Sobrien    off_t   Bfeobp;		/* Seekp of end of buffers */
79659243Sobrien    int     Bfblocks;		/* Number of buffer blocks */
79759243Sobrien    Char  **Bfbuf;		/* The array of buffer blocks */
798145479Smp#ifdef WIDE_STRINGS
799145479Smp    /* Number of bytes in each character if (cantell) */
800145479Smp    unsigned char Bfclens[BUFSIZE + 1];
801145479Smp#endif
80259243Sobrien}       B;
80359243Sobrien
80459243Sobrien/*
80559243Sobrien * This structure allows us to seek inside aliases
80659243Sobrien */
80759243Sobrienstruct Ain {
80859243Sobrien    int type;
80969408Sache#define TCSH_I_SEEK 	 0		/* Invalid seek */
81069408Sache#define TCSH_A_SEEK	 1		/* Alias seek */
81169408Sache#define TCSH_F_SEEK	 2		/* File seek */
81269408Sache#define TCSH_E_SEEK	 3		/* Eval seek */
81359243Sobrien    union {
814167465Smp	off_t _f_seek;		/* A byte offset if (cantell) */
81559243Sobrien	Char* _c_seek;
81659243Sobrien    } fc;
81759243Sobrien#define f_seek fc._f_seek
81859243Sobrien#define c_seek fc._c_seek
81959243Sobrien    Char **a_seek;
82059243Sobrien} ;
82159243Sobrien
82259243Sobrienextern int aret;		/* Type of last char returned */
82359243Sobrien#define SEEKEQ(a, b) ((a)->type == (b)->type && \
82459243Sobrien		      (a)->f_seek == (b)->f_seek && \
82559243Sobrien		      (a)->a_seek == (b)->a_seek)
82659243Sobrien
82759243Sobrien#define	fseekp	B.Bfseekp
82859243Sobrien#define	fbobp	B.Bfbobp
82959243Sobrien#define	feobp	B.Bfeobp
83059243Sobrien#define	fblocks	B.Bfblocks
83159243Sobrien#define	fbuf	B.Bfbuf
832145479Smp#define fclens  B.Bfclens
83359243Sobrien
83459243Sobrien/*
83559243Sobrien * The shell finds commands in loops by reseeking the input
83659243Sobrien * For whiles, in particular, it reseeks to the beginning of the
83759243Sobrien * line the while was on; hence the while placement restrictions.
83859243Sobrien */
83959243SobrienEXTERN struct Ain lineloc;
84059243Sobrien
841145479SmpEXTERN int    cantell;		/* Is current source tellable ? */
84259243Sobrien
84359243Sobrien/*
84459243Sobrien * Input lines are parsed into doubly linked circular
84559243Sobrien * lists of words of the following form.
84659243Sobrien */
84759243Sobrienstruct wordent {
84859243Sobrien    Char   *word;
84959243Sobrien    struct wordent *prev;
85059243Sobrien    struct wordent *next;
85159243Sobrien};
85259243Sobrien
85359243Sobrien/*
85459243Sobrien * During word building, both in the initial lexical phase and
85559243Sobrien * when expanding $ variable substitutions, expansion by `!' and `$'
85659243Sobrien * must be inhibited when reading ahead in routines which are themselves
85759243Sobrien * processing `!' and `$' expansion or after characters such as `\' or in
85859243Sobrien * quotations.  The following flags are passed to the getC routines
85959243Sobrien * telling them which of these substitutions are appropriate for the
86059243Sobrien * next character to be returned.
86159243Sobrien */
86259243Sobrien#define	DODOL	1
86359243Sobrien#define	DOEXCL	2
86459243Sobrien#define	DOALL	DODOL|DOEXCL
86559243Sobrien
86659243Sobrien/*
86759243Sobrien * Labuf implements a general buffer for lookahead during lexical operations.
86859243Sobrien * Text which is to be placed in the input stream can be stuck here.
86959243Sobrien * We stick parsed ahead $ constructs during initial input,
87059243Sobrien * process id's from `$$', and modified variable values (from qualifiers
87159243Sobrien * during expansion in sh.dol.c) here.
87259243Sobrien */
873167465Smpextern struct Strbuf labuf;
874167465SmpEXTERN size_t lap; /* N/A if == labuf.len, index into labuf.s otherwise */
87559243Sobrien
87659243Sobrien/*
87759243Sobrien * Parser structure
87859243Sobrien *
87959243Sobrien * Each command is parsed to a tree of command structures and
88059243Sobrien * flags are set bottom up during this process, to be propagated down
88159243Sobrien * as needed during the semantics/exeuction pass (sh.sem.c).
88259243Sobrien */
88359243Sobrienstruct command {
88459243Sobrien    unsigned char   t_dtyp;	/* Type of node 		 */
88559243Sobrien#define	NODE_COMMAND	1	/* t_dcom <t_dlef >t_drit	 */
88659243Sobrien#define	NODE_PAREN	2	/* ( t_dspr ) <t_dlef >t_drit	 */
88759243Sobrien#define	NODE_PIPE	3	/* t_dlef | t_drit		 */
88859243Sobrien#define	NODE_LIST	4	/* t_dlef ; t_drit		 */
88959243Sobrien#define	NODE_OR		5	/* t_dlef || t_drit		 */
89059243Sobrien#define	NODE_AND	6	/* t_dlef && t_drit		 */
89159243Sobrien    unsigned char   t_nice;	/* Nice value			 */
89259243Sobrien#ifdef apollo
89359243Sobrien    unsigned char   t_systype;	/* System environment		 */
89459243Sobrien#endif
89559243Sobrien    unsigned long   t_dflg;	/* Flags, e.g. F_AMPERSAND|... 	 */
89659243Sobrien/* save these when re-doing 	 */
89759243Sobrien#ifndef apollo
89859243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP|F_HUP)
89959243Sobrien#else
90059243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
90159243Sobrien#endif
90259243Sobrien#define	F_AMPERSAND	(1<<0)	/* executes in background	 */
90359243Sobrien#define	F_APPEND	(1<<1)	/* output is redirected >>	 */
90459243Sobrien#define	F_PIPEIN	(1<<2)	/* input is a pipe		 */
90559243Sobrien#define	F_PIPEOUT	(1<<3)	/* output is a pipe		 */
90659243Sobrien#define	F_NOFORK	(1<<4)	/* don't fork, last ()ized cmd	 */
90759243Sobrien#define	F_NOINTERRUPT	(1<<5)	/* should be immune from intr's */
90859243Sobrien/* spare */
90959243Sobrien#define	F_STDERR	(1<<7)	/* redirect unit 2 with unit 1	 */
91059243Sobrien#define	F_OVERWRITE	(1<<8)	/* output was !			 */
91159243Sobrien#define	F_READ		(1<<9)	/* input redirection is <<	 */
91259243Sobrien#define	F_REPEAT	(1<<10)	/* reexec aft if, repeat,...	 */
91359243Sobrien#define	F_NICE		(1<<11)	/* t_nice is meaningful 	 */
91459243Sobrien#define	F_NOHUP		(1<<12)	/* nohup this command 		 */
91559243Sobrien#define	F_TIME		(1<<13)	/* time this command 		 */
91659243Sobrien#define F_BACKQ		(1<<14)	/* command is in ``		 */
91759243Sobrien#define F_HUP		(1<<15)	/* hup this command		 */
91859243Sobrien#ifdef apollo
91959243Sobrien#define F_VER		(1<<16)	/* execute command under SYSTYPE */
92059243Sobrien#endif
92159243Sobrien    union {
92259243Sobrien	Char   *T_dlef;		/* Input redirect word 		 */
92359243Sobrien	struct command *T_dcar;	/* Left part of list/pipe 	 */
92459243Sobrien    }       L;
92559243Sobrien    union {
92659243Sobrien	Char   *T_drit;		/* Output redirect word 	 */
92759243Sobrien	struct command *T_dcdr;	/* Right part of list/pipe 	 */
92859243Sobrien    }       R;
92959243Sobrien#define	t_dlef	L.T_dlef
93059243Sobrien#define	t_dcar	L.T_dcar
93159243Sobrien#define	t_drit	R.T_drit
93259243Sobrien#define	t_dcdr	R.T_dcdr
93359243Sobrien    Char  **t_dcom;		/* Command/argument vector 	 */
93459243Sobrien    struct command *t_dspr;	/* Pointer to ()'d subtree 	 */
93559243Sobrien};
93659243Sobrien
93759243Sobrien
93859243Sobrien/*
93959243Sobrien * The keywords for the parser
94059243Sobrien */
94159243Sobrien#define	TC_BREAK	0
94259243Sobrien#define	TC_BRKSW	1
94359243Sobrien#define	TC_CASE		2
94459243Sobrien#define	TC_DEFAULT 	3
94559243Sobrien#define	TC_ELSE		4
94659243Sobrien#define	TC_END		5
94759243Sobrien#define	TC_ENDIF	6
94859243Sobrien#define	TC_ENDSW	7
94959243Sobrien#define	TC_EXIT		8
95059243Sobrien#define	TC_FOREACH	9
95159243Sobrien#define	TC_GOTO		10
95259243Sobrien#define	TC_IF		11
95359243Sobrien#define	TC_LABEL	12
95459243Sobrien#define	TC_LET		13
95559243Sobrien#define	TC_SET		14
95659243Sobrien#define	TC_SWITCH	15
95759243Sobrien#define	TC_TEST		16
95859243Sobrien#define	TC_THEN		17
95959243Sobrien#define	TC_WHILE	18
96059243Sobrien
96159243Sobrien/*
96259243Sobrien * These are declared here because they want to be
96359243Sobrien * initialized in sh.init.c (to allow them to be made readonly)
96459243Sobrien */
96559243Sobrien
96659243Sobrien#if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
96759243Sobrien    /* Avoid hpux ansi mode spurious warnings */
96859243Sobrientypedef void (*bfunc_t) ();
96959243Sobrien#else
970167465Smptypedef void (*bfunc_t) (Char **, struct command *);
97159243Sobrien#endif /* hpux && __STDC__ && !__GNUC__ */
97259243Sobrien
973167465Smpextern const struct biltins {
974145479Smp    const char   *bname;
97559243Sobrien    bfunc_t bfunct;
97659243Sobrien    int     minargs, maxargs;
97759243Sobrien} bfunc[];
97859243Sobrienextern int nbfunc;
97969408Sache#ifdef WINNT_NATIVE
98059243Sobrienextern struct biltins  nt_bfunc[];
98159243Sobrienextern int nt_nbfunc;
98269408Sache#endif /* WINNT_NATIVE*/
983145479Smpextern int bequiet;
98459243Sobrien
98559243Sobrienextern struct srch {
986145479Smp    const char *s_name;
987145479Smp    int  s_value;
988145479Smp} srchn[];
98959243Sobrienextern int nsrchn;
99059243Sobrien
99159243Sobrien/*
99259243Sobrien * Structure defining the existing while/foreach loops at this
99359243Sobrien * source level.  Loops are implemented by seeking back in the
99459243Sobrien * input.  For foreach (fe), the word list is attached here.
99559243Sobrien */
99659243SobrienEXTERN struct whyle {
99759243Sobrien    struct Ain   w_start;	/* Point to restart loop */
99859243Sobrien    struct Ain   w_end;		/* End of loop (0 if unknown) */
99959243Sobrien    Char  **w_fe, **w_fe0;	/* Current/initial wordlist for fe */
100059243Sobrien    Char   *w_fename;		/* Name for fe */
100159243Sobrien    struct whyle *w_next;	/* Next (more outer) loop */
100259243Sobrien}      *whyles;
100359243Sobrien
100459243Sobrien/*
100559243Sobrien * Variable structure
100659243Sobrien *
100759243Sobrien * Aliases and variables are stored in AVL balanced binary trees.
100859243Sobrien */
100959243SobrienEXTERN struct varent {
101059243Sobrien    Char  **vec;		/* Array of words which is the value */
101159243Sobrien    Char   *v_name;		/* Name of variable/alias */
101259243Sobrien    int	    v_flags;		/* Flags */
101359243Sobrien#define VAR_ALL		-1
101459243Sobrien#define VAR_READONLY	1
101559243Sobrien#define VAR_READWRITE	2
101659243Sobrien#define VAR_NOGLOB	4
101759243Sobrien#define VAR_FIRST       32
101859243Sobrien#define VAR_LAST        64
101959243Sobrien    struct varent *v_link[3];	/* The links, see below */
102059243Sobrien    int     v_bal;		/* Balance factor */
102159243Sobrien}       shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
102259243Sobrien
102359243Sobrien#define v_left		v_link[0]
102459243Sobrien#define v_right		v_link[1]
102559243Sobrien#define v_parent	v_link[2]
102659243Sobrien
102759243Sobrien#define adrof(v)	adrof1(v, &shvhed)
102859243Sobrien#define varval(v)	value1(v, &shvhed)
102959243Sobrien
103059243Sobrien/*
103159243Sobrien * The following are for interfacing redo substitution in
103259243Sobrien * aliases to the lexical routines.
103359243Sobrien */
103459243SobrienEXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
103559243SobrienEXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
103659243SobrienEXTERN Char  **alvec IZERO_STRUCT,
103759243Sobrien	      *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
103859243Sobrien
103959243Sobrien/*
104059243Sobrien * Filename/command name expansion variables
104159243Sobrien */
104259243Sobrien
104359243Sobrien#ifndef MAXPATHLEN
1044167465Smp# ifdef PATH_MAX
1045167465Smp#  define MAXPATHLEN PATH_MAX
1046167465Smp# else
1047167465Smp#  define MAXPATHLEN 2048
1048167465Smp# endif
104959243Sobrien#endif /* MAXPATHLEN */
105059243Sobrien
105159243Sobrien#ifndef HAVENOLIMIT
105259243Sobrien/*
105359243Sobrien * resource limits
105459243Sobrien */
105559243Sobrienextern struct limits {
1056145479Smp    int         limconst;
1057145479Smp    const char *limname;
1058145479Smp    int         limdiv;
1059145479Smp    const char *limscale;
106059243Sobrien} limits[];
106159243Sobrien#endif /* !HAVENOLIMIT */
106259243Sobrien
106359243Sobrien/*
106459243Sobrien * History list
106559243Sobrien *
106659243Sobrien * Each history list entry contains an embedded wordlist
106759243Sobrien * from the scanner, a number for the event, and a reference count
106859243Sobrien * to aid in discarding old entries.
106959243Sobrien *
107059243Sobrien * Essentially "invisible" entries are put on the history list
107159243Sobrien * when history substitution includes modifiers, and thrown away
107259243Sobrien * at the next discarding since their event numbers are very negative.
107359243Sobrien */
107459243SobrienEXTERN struct Hist {
107559243Sobrien    struct wordent Hlex;
1076231990Smp    int     Hnum;		 /* eventno when inserted into history list  */
107759243Sobrien    int     Href;
107859243Sobrien    time_t  Htime;
107959243Sobrien    Char   *histline;
1080231990Smp    struct Hist *Hnext, *Hprev;         /* doubly linked list */
1081231990Smp    unsigned Hhash;                     /* hash value of command line */
108259243Sobrien}       Histlist IZERO_STRUCT;
108359243Sobrien
1084316957Sdchaginextern struct wordent paraml;	/* Current lexical word list */
108559243SobrienEXTERN int     eventno;		/* Next events number */
108659243SobrienEXTERN int     lastev;		/* Last event reference (default) */
108759243Sobrien
108859243SobrienEXTERN Char    HIST;		/* history invocation character */
108959243SobrienEXTERN Char    HISTSUB;		/* auto-substitute character */
109059243SobrienEXTERN Char    PRCH;		/* Prompt symbol for regular users */
109159243SobrienEXTERN Char    PRCHROOT;	/* Prompt symbol for root */
109259243Sobrien
109359243Sobrien/*
109459243Sobrien * For operating systems with single case filenames (OS/2)
109559243Sobrien */
109659243Sobrien#ifdef CASE_INSENSITIVE
1097145479Smp# ifdef WIDE_STRINGS
1098145479Smp#  define samecase(x) (towlower(x))
1099145479Smp# else
1100145479Smp#  define samecase(x) (isupper((unsigned char)(x)) ? \
1101145479Smp		       tolower((unsigned char)(x)) : (x))
1102145479Smp# endif
110359243Sobrien#else
110459243Sobrien# define samecase(x) (x)
110559243Sobrien#endif /* CASE_INSENSITIVE */
110659243Sobrien
110759243Sobrien/*
110859243Sobrien * strings.h:
110959243Sobrien */
111059243Sobrien#ifndef SHORT_STRINGS
111159243Sobrien#define Strchr(a, b)  		strchr(a, b)
111259243Sobrien#define Strrchr(a, b)  		strrchr(a, b)
111359243Sobrien#define Strcat(a, b)  		strcat(a, b)
111459243Sobrien#define Strncat(a, b, c) 	strncat(a, b, c)
111559243Sobrien#define Strcpy(a, b)  		strcpy(a, b)
111659243Sobrien#define Strncpy(a, b, c) 	strncpy(a, b, c)
111759243Sobrien#define Strlen(a)		strlen(a)
111859243Sobrien#define Strcmp(a, b)		strcmp(a, b)
111959243Sobrien#define Strncmp(a, b, c)	strncmp(a, b, c)
1120131962Smp#define Strcasecmp(a, b)	strcasecmp(a, b)
112159243Sobrien
112259243Sobrien#define Strspl(a, b)		strspl(a, b)
1123167465Smp#define Strnsave(a, b)		strnsave(a, b)
112459243Sobrien#define Strsave(a)		strsave(a)
112559243Sobrien#define Strend(a)		strend(a)
112659243Sobrien#define Strstr(a, b)		strstr(a, b)
112759243Sobrien
112859243Sobrien#define str2short(a) 		(a)
112959243Sobrien#define blk2short(a) 		saveblk(a)
113059243Sobrien#define short2blk(a) 		saveblk(a)
1131167465Smp#define short2str(a) 		caching_strip(a)
113259243Sobrien#else
1133231990Smp#if defined(WIDE_STRINGS) && !defined(UTF16_STRINGS)
1134145479Smp#define Strchr(a, b)		wcschr(a, b)
1135145479Smp#define Strrchr(a, b)		wcsrchr(a, b)
1136145479Smp#define Strcat(a, b)  		wcscat(a, b)
1137145479Smp#define Strncat(a, b, c) 	wcsncat(a, b, c)
1138145479Smp#define Strcpy(a, b)  		wcscpy(a, b)
1139145479Smp#define Strncpy(a, b, c)	wcsncpy(a, b, c)
1140145479Smp#define Strlen(a)		wcslen(a)
1141145479Smp#define Strcmp(a, b)		wcscmp(a, b)
1142145479Smp#define Strncmp(a, b, c)	wcsncmp(a, b, c)
1143145479Smp#else
114459243Sobrien#define Strchr(a, b)		s_strchr(a, b)
114559243Sobrien#define Strrchr(a, b) 		s_strrchr(a, b)
114659243Sobrien#define Strcat(a, b)  		s_strcat(a, b)
114759243Sobrien#define Strncat(a, b, c) 	s_strncat(a, b, c)
114859243Sobrien#define Strcpy(a, b)  		s_strcpy(a, b)
114959243Sobrien#define Strncpy(a, b, c)	s_strncpy(a, b, c)
115059243Sobrien#define Strlen(a)		s_strlen(a)
115159243Sobrien#define Strcmp(a, b)		s_strcmp(a, b)
115259243Sobrien#define Strncmp(a, b, c)	s_strncmp(a, b, c)
1153145479Smp#endif
1154131962Smp#define Strcasecmp(a, b)	s_strcasecmp(a, b)
115559243Sobrien
115659243Sobrien#define Strspl(a, b)		s_strspl(a, b)
1157167465Smp#define Strnsave(a, b)		s_strnsave(a, b)
115859243Sobrien#define Strsave(a)		s_strsave(a)
115959243Sobrien#define Strend(a)		s_strend(a)
116059243Sobrien#define Strstr(a, b)		s_strstr(a, b)
116159243Sobrien#endif
116259243Sobrien
116359243Sobrien/*
116459243Sobrien * setname is a macro to save space (see sh.err.c)
116559243Sobrien */
1166145479SmpEXTERN const char   *bname;
116759243Sobrien
116859243Sobrien#define	setname(a)	(bname = (a))
116959243Sobrien
117059243Sobrien#ifdef VFORK
117159243SobrienEXTERN Char   *Vsav;
117259243SobrienEXTERN Char   *Vdp;
117359243SobrienEXTERN Char   *Vexpath;
117459243SobrienEXTERN char  **Vt;
117559243Sobrien#endif /* VFORK */
117659243Sobrien
117759243SobrienEXTERN Char  **evalvec;
117859243SobrienEXTERN Char   *evalp;
117959243Sobrien
118059243Sobrienextern struct mesg {
1181145479Smp    const char   *iname;	/* name from /usr/include */
1182145479Smp    const char *pname;		/* print name */
1183145479Smp} mesg[];
118459243Sobrien
1185316957Sdchagin/* word_chars is set by default to WORD_CHARS (or WORD_CHARS_VI) but can
1186316957Sdchagin   be overridden by the wordchars variable--if unset, reverts to
1187316957Sdchagin   WORD_CHARS (or WORD_CHARS_VI) */
118859243Sobrien
118959243SobrienEXTERN Char   *word_chars;
119059243Sobrien
119159243Sobrien#define WORD_CHARS "*?_-.[]~="	/* default chars besides alnums in words */
1192316957Sdchagin#define WORD_CHARS_VI "_"	/* default chars besides alnums in words */
119359243Sobrien
119459243SobrienEXTERN Char   *STR_SHELLPATH;
119559243Sobrien
119659243Sobrien#ifdef _PATH_BSHELL
119759243SobrienEXTERN Char   *STR_BSHELL;
119859243Sobrien#endif
119959243SobrienEXTERN Char   *STR_WORD_CHARS;
1200316957SdchaginEXTERN Char   *STR_WORD_CHARS_VI;
120159243SobrienEXTERN Char  **STR_environ IZERO;
120259243Sobrien
120359243Sobrienextern int     dont_free;	/* Tell free that we are in danger if we free */
120459243Sobrien
120559243Sobrienextern Char    *INVPTR;
120659243Sobrienextern Char    **INVPPTR;
120759243Sobrien
120859243Sobrienextern char    *progname;
120959243Sobrienextern int	tcsh;
1210145479Smpextern int	xlate_cr;
1211145479Smpextern int	output_raw;
1212145479Smpextern int	lbuffed;
1213145479Smpextern time_t	Htime;
1214145479Smpextern int	numeof;
1215145479Smpextern int 	insource;
1216145479Smpextern char	linbuf[];
1217145479Smpextern char 	*linp;
1218145479Smpextern int	nsig;
1219145479Smp#ifdef VFORK
1220145479Smpextern int	use_fork;
1221145479Smp#endif
1222145479Smpextern int	tellwhat;
1223145479Smpextern int	NoNLSRebind;
1224167465Smp#if !HAVE_DECL_ENVIRON
1225167465Smpextern char   **environ;
1226167465Smp#endif
122759243Sobrien
122859243Sobrien#include "tc.h"
122959243Sobrien
123069408Sache#ifndef WINNT_NATIVE
123159243Sobrien# ifdef NLS_CATALOGS
1232231990Smp#  ifdef HAVE_FEATURES_H
123359243Sobrien#   include <features.h>
123459243Sobrien#  endif
1235231990Smp#  ifdef HAVE_NL_LANGINFO
1236231990Smp#   include <langinfo.h>
123759243Sobrien#  endif
1238231990Smp#  ifdef __uxps__
1239231990Smp#   define gettxt gettxt_ds
124059243Sobrien#  endif
1241231990Smp#  include <nl_types.h>
1242231990Smp#  ifdef __uxps__
1243231990Smp#   undef gettxt
1244231990Smp#  endif
124559243SobrienEXTERN nl_catd catd;
1246167465Smp#  if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
1247145479Smp#   define CGETS(b, c, d)	iconv_catgets(catd, b, c, d)
1248145479Smp#  else
1249167465Smp#   define CGETS(b, c, d)	xcatgets(catd, b, c, d)
1250145479Smp#  endif
125159243Sobrien#  define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
125259243Sobrien# else
125359243Sobrien#  define CGETS(b, c, d)	d
125459243Sobrien#  define CSAVS(b, c, d)	d
125559243Sobrien# endif
125669408Sache#else /* WINNT_NATIVE */
125759243Sobrien# define CGETS(b, c, d)	nt_cgets( b, c, d)
125859243Sobrien# define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
125969408Sache#endif /* WINNT_NATIVE */
126059243Sobrien
1261131962Smp#if defined(FILEC)
1262145479Smpextern int    filec;
1263131962Smp#endif /* FILEC */
1264131962Smp
1265145479Smp#include "sh.decls.h"
126659243Sobrien/*
126759243Sobrien * Since on some machines characters are unsigned, and the signed
126859243Sobrien * keyword is not universally implemented, we treat all characters
126959243Sobrien * as unsigned and sign extend them where we need.
127059243Sobrien */
127159243Sobrien#define SIGN_EXTEND_CHAR(a)	(((a) & 0x80) ? ((a) | ~0x7f) : (a))
127259243Sobrien
1273145479Smp/*
1274145479Smp * explanation for use by the "--help" option
1275145479Smp */
1276145479Smp#define HELP_STRING "\
1277145479Smp-b file		batch mode, read and execute commands from `file' \n\
1278145479Smp-c command	run `command' from next argument \n\
1279145479Smp-d		load directory stack from `~/.cshdirs' \n\
1280145479Smp-Dname[=value]	define environment variable `name' to `value' (DomainOS only) \n\
1281145479Smp-e		exit on any error \n\
1282145479Smp-f		start faster by ignoring the start-up file \n\
1283145479Smp-F		use fork() instead of vfork() when spawning (ConvexOS only) \n\
1284145479Smp-i		interactive, even when input is not from a terminal \n\
1285145479Smp-l		act as a login shell, must be the only option specified \n\
1286145479Smp-m		load the start-up file, whether or not owned by effective user \n\
1287145479Smp-n file		no execute mode, just check syntax of the following `file' \n\
1288145479Smp-q		accept SIGQUIT for running under a debugger \n\
1289145479Smp-s		read commands from standard input \n\
1290145479Smp-t		read one line from standard input \n\
1291145479Smp-v		echo commands after history substitution \n\
1292145479Smp-V		like -v but including commands read from the start-up file \n\
1293145479Smp-x		echo commands immediately before execution \n\
1294145479Smp-X		like -x but including commands read from the start-up file \n\
1295145479Smp--help		print this message and exit \n\
1296145479Smp--version	print the version shell variable and exit \n\
1297145479Smp\nSee the tcsh(1) manual page for detailed information.\n"
1298145479Smp
1299145479Smp#include "tc.nls.h"
1300145479Smp
130159243Sobrien#endif /* _h_sh */
1302