1316958Sdchagin/* $Header: /p/tcsh/cvsroot/tcsh/sh.h,v 3.178 2016/09/12 16:33:54 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.
17100616Smp * 3. Neither the name of the University nor the names of its contributors
1859243Sobrien *    may be used to endorse or promote products derived from this software
1959243Sobrien *    without specific prior written permission.
2059243Sobrien *
2159243Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2259243Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2359243Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2459243Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2559243Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2659243Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2759243Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2859243Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2959243Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3059243Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3159243Sobrien * SUCH DAMAGE.
3259243Sobrien */
3359243Sobrien#ifndef _h_sh
3459243Sobrien#define _h_sh
3559243Sobrien
3659243Sobrien#include "config.h"
3759243Sobrien
38145479Smp#include <stddef.h>
39167465Smp#include <signal.h>
40145479Smp
41145479Smp#ifdef HAVE_ICONV
42145479Smp# include <iconv.h>
43100616Smp#endif
44145479Smp
45145479Smp#ifdef HAVE_STDINT_H
46145479Smp# include <stdint.h>
47100616Smp#endif
48100616Smp
49145479Smp#ifdef HAVE_INTTYPES_H
50145479Smp# include <inttypes.h>
51145479Smp#endif
52145479Smp
53145479Smp#if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
54145479Smptypedef unsigned long intptr_t;
55145479Smp#endif
56145479Smp
5759243Sobrien#ifndef EXTERN
5859243Sobrien# define EXTERN extern
5959243Sobrien#else /* !EXTERN */
6069408Sache# ifdef WINNT_NATIVE
6159243Sobrien#  define IZERO = 0
6259243Sobrien#  define IZERO_STRUCT = {0}
6369408Sache# endif /* WINNT_NATIVE */
6459243Sobrien#endif /* EXTERN */
6559243Sobrien
6659243Sobrien#ifndef IZERO
6759243Sobrien# define IZERO
6859243Sobrien#endif /* IZERO */
6959243Sobrien#ifndef IZERO_STRUCT
7059243Sobrien# define IZERO_STRUCT
71145479Smp#endif /* IZERO_STRUCT */
7259243Sobrien
7369408Sache#ifndef WINNT_NATIVE
7459243Sobrien# define INIT_ZERO
7559243Sobrien# define INIT_ZERO_STRUCT
76167465Smp# define force_read xread
7769408Sache#endif /*!WINNT_NATIVE */
78231990Smp
79231990Smp#if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
80231990Smp#define AUTOSET_KANJI
81231990Smp#endif
8259243Sobrien/*
8359243Sobrien * Sanity
8459243Sobrien */
8559243Sobrien#if defined(_POSIX_SOURCE) && !defined(POSIX)
8659243Sobrien# define POSIX
8759243Sobrien#endif
8859243Sobrien
8959243Sobrien#if defined(POSIXJOBS) && !defined(BSDJOBS)
9059243Sobrien# define BSDJOBS
9159243Sobrien#endif
9259243Sobrien
93231990Smp#define TMP_TEMPLATE ".XXXXXX"
94231990Smp
9559243Sobrien#ifdef SHORT_STRINGS
96167465Smp# ifdef WIDE_STRINGS
97145479Smp#include <wchar.h>
98231990Smp#  ifdef UTF16_STRINGS
99231990Smptypedef wint_t Char;
100231990Smp#  else
101145479Smptypedef wchar_t Char;
102231990Smp#endif
103145479Smptypedef unsigned long uChar;
104145479Smptypedef wint_t eChar; /* Can contain any Char value or CHAR_ERR */
105145479Smp#define CHAR_ERR WEOF /* Pretty please, use bit 31... */
106145479Smp#define normal_mbtowc(PWC, S, N) rt_mbtowc(PWC, S, N)
107231990Smp#define reset_mbtowc() TCSH_IGNORE(mbtowc(NULL, NULL, 0))
108145479Smp# else
10959243Sobrientypedef short Char;
11059243Sobrientypedef unsigned short uChar;
111145479Smptypedef int eChar;
112145479Smp#define CHAR_ERR (-1)
113145479Smp#define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
114145479Smp#define reset_mbtowc() ((void)0)
115145479Smp# endif
11659243Sobrien# define SAVE(a) (Strsave(str2short(a)))
11759243Sobrien#else
11859243Sobrientypedef char Char;
11959243Sobrientypedef unsigned char uChar;
120145479Smptypedef int eChar;
121145479Smp#define CHAR_ERR (-1)
122145479Smp#define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
123145479Smp#define reset_mbtowc() ((void)0)
12459243Sobrien# define SAVE(a) (strsave(a))
125145479Smp#endif
12659243Sobrien
127231990Smp#if !defined(__inline) && !defined(__GNUC__) && !defined(_MSC_VER)
128231990Smp#define __inline
129231990Smp#endif
130316958Sdchagin#ifdef _MSC_VER
131316958Sdchagin#define TCSH_PTRDIFF_T_FMT "I"
132316958Sdchagin#else
133316958Sdchagin#define TCSH_PTRDIFF_T_FMT "t"
134316958Sdchagin#endif
13559243Sobrien/* Elide unused argument warnings */
13659243Sobrien#define USE(a)	(void) (a)
137231990Smp#define TCSH_IGNORE(a)	tcsh_ignore((intptr_t)a)
138231990Smpstatic __inline void tcsh_ignore(intptr_t a)
139195609Smp{
140195609Smp    USE(a);
141195609Smp}
14259243Sobrien
14359243Sobrien/*
14459243Sobrien * Return true if the path is absolute
14559243Sobrien */
146131962Smp#if defined(WINNT_NATIVE)
147100616Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
148100616Smp    (Isalpha((p)[0]) && (p)[1] == ':'))
149131962Smp#elif defined(__CYGWIN__)
150131962Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
151131962Smp    (Isalpha((p)[0]) && (p)[1] == ':' && \
152131962Smp     ((p)[2] == '\0' || (p)[2] == '/')))
153100616Smp#else /* !WINNT_NATIVE && !__CYGWIN__ */
15459243Sobrien# define ABSOLUTEP(p)	(*(p) == '/')
155100616Smp#endif /* WINNT_NATIVE || __CYGWIN__ */
15659243Sobrien
15759243Sobrien/*
15859243Sobrien * Fundamental definitions which may vary from system to system.
15959243Sobrien *
16059243Sobrien *	BUFSIZE		The i/o buffering size; also limits word size
16159243Sobrien *	MAILINTVL	How often to mailcheck; more often is more expensive
16259243Sobrien */
16359243Sobrien#ifdef BUFSIZE
16461524Sobrien# if	   BUFSIZE < 4096
16559243Sobrien#  undef   BUFSIZE
16661524Sobrien#  define  BUFSIZE	4096	/* buffer size should be no less than this */
16759243Sobrien# endif
16859243Sobrien#else
16961524Sobrien# define   BUFSIZE	4096
17059243Sobrien#endif /* BUFSIZE */
17159243Sobrien
17259243Sobrien#define FORKSLEEP	10	/* delay loop on non-interactive fork failure */
17359243Sobrien#define	MAILINTVL	600	/* 10 minutes */
17459243Sobrien
17559243Sobrien#ifndef INBUFSIZE
17659243Sobrien# define INBUFSIZE    2*BUFSIZE /* Num input characters on the command line */
17759243Sobrien#endif /* INBUFSIZE */
17859243Sobrien
17959243Sobrien
18059243Sobrien/*
18159243Sobrien * What our builtin echo looks like
18259243Sobrien */
18359243Sobrien#define NONE_ECHO	0
18459243Sobrien#define BSD_ECHO	1
18559243Sobrien#define SYSV_ECHO	2
18659243Sobrien#define BOTH_ECHO	(BSD_ECHO|SYSV_ECHO)
18759243Sobrien
18859243Sobrien#ifndef ECHO_STYLE
18959243Sobrien# if SYSVREL > 0
19059243Sobrien#  define ECHO_STYLE SYSV_ECHO
19159243Sobrien# else /* SYSVREL == 0 */
19259243Sobrien#  define ECHO_STYLE BSD_ECHO
19359243Sobrien# endif /* SYSVREL */
19459243Sobrien#endif /* ECHO_STYLE */
19559243Sobrien
196316958Sdchagin/* values for noclobber */
197316958Sdchagin#define NOCLOBBER_DEFAULT  1
198316958Sdchagin#define NOCLOBBER_NOTEMPTY 2
199316958Sdchagin#define NOCLOBBER_ASK      4
200316958Sdchagin
20159243Sobrien/*
20259243Sobrien * The shell moves std in/out/diag and the old std input away from units
20359243Sobrien * 0, 1, and 2 so that it is easy to set up these standards for invoked
20459243Sobrien * commands.
20559243Sobrien */
206145479Smp#define	FSAFE	5		/* We keep the first 5 descriptors untouched */
20759243Sobrien#define	FSHTTY	15		/* /dev/tty when manip pgrps */
20859243Sobrien#define	FSHIN	16		/* Preferred desc for shell input */
20959243Sobrien#define	FSHOUT	17		/* ... shell output */
21059243Sobrien#define	FSHDIAG	18		/* ... shell diagnostics */
21159243Sobrien#define	FOLDSTD	19		/* ... old std input */
21259243Sobrien
21359243Sobrien#ifdef PROF
21459243Sobrien#define	xexit(n)	done(n)
21559243Sobrien#endif
21659243Sobrien
21759243Sobrien#ifdef cray
21859243Sobrien# define word word_t           /* sys/types.h defines word.. bad move! */
21959243Sobrien#endif
22059243Sobrien
22159243Sobrien#include <sys/types.h>
22259243Sobrien
22359243Sobrien#ifdef cray
22459243Sobrien# undef word
22559243Sobrien#endif
22659243Sobrien
22759243Sobrien/*
22859243Sobrien * Path separator in environment variables
22959243Sobrien */
23059243Sobrien#ifndef PATHSEP
23169408Sache# if defined(__EMX__) || defined(WINNT_NATIVE)
23259243Sobrien#  define PATHSEP ';'
23359243Sobrien# else /* unix */
23459243Sobrien#  define PATHSEP ':'
23569408Sache# endif /* __EMX__ || WINNT_NATIVE */
23659243Sobrien#endif /* !PATHSEP */
23759243Sobrien
23883098Smp#if defined(__HP_CXD_SPP) && !defined(__hpux)
23959243Sobrien# include <sys/cnx_stat.h>
24059243Sobrien# define stat stat64
24159243Sobrien# define fstat fstat64
24259243Sobrien# define lstat lstat64
24383098Smp#endif /* __HP_CXD_SPP && !__hpux */
24459243Sobrien
245231990Smp#ifdef HAVE_LONG_LONG
246231990Smptypedef long long tcsh_number_t;
247231990Smp#else
248231990Smptypedef long tcsh_number_t;
249231990Smp#endif
25059243Sobrien/*
25159243Sobrien * This macro compares the st_dev field of struct stat. On aix on ibmESA
25259243Sobrien * st_dev is a structure, so comparison does not work.
25359243Sobrien */
25459243Sobrien#ifndef DEV_DEV_COMPARE
25559243Sobrien# define DEV_DEV_COMPARE(x,y)   ((x) == (y))
25659243Sobrien#endif /* DEV_DEV_COMPARE */
25759243Sobrien
25859243Sobrien#ifdef _SEQUENT_
25959243Sobrien# include <sys/procstats.h>
26059243Sobrien#endif /* _SEQUENT_ */
26169408Sache#if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
26259243Sobrien# include <sys/times.h>
26369408Sache#endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
26459243Sobrien
26559243Sobrien#ifdef NLS
26659243Sobrien# include <locale.h>
26759243Sobrien#endif /* NLS */
26859243Sobrien
26969408Sache#if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
27059243Sobrien# include <sys/param.h>
27169408Sache#endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
27259243Sobrien#include <sys/stat.h>
27359243Sobrien
27459243Sobrien#if defined(BSDTIMES) || defined(BSDLIMIT)
27559243Sobrien# include <sys/time.h>
27669408Sache# if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
27759243Sobrien#  include "/usr/ucbinclude/sys/resource.h"
27859243Sobrien# else
27959243Sobrien#  ifdef convex
28059243Sobrien#   define sysrusage cvxrusage
28159243Sobrien#   include <sys/sysinfo.h>
28259243Sobrien#  else
28359243Sobrien#   define sysrusage rusage
28459243Sobrien#   include <sys/resource.h>
28559243Sobrien#  endif /* convex */
28659243Sobrien# endif /* SYSVREL>3 */
28759243Sobrien#endif /* BSDTIMES */
28859243Sobrien
28969408Sache#ifndef WINNT_NATIVE
29059243Sobrien# ifndef POSIX
29159243Sobrien#  ifdef TERMIO
29259243Sobrien#   include <termio.h>
29359243Sobrien#  else /* SGTTY */
29459243Sobrien#   include <sgtty.h>
29559243Sobrien#  endif /* TERMIO */
29659243Sobrien# else /* POSIX */
29759243Sobrien#  ifndef _UWIN
29859243Sobrien#   include <termios.h>
29959243Sobrien#  else
30059243Sobrien#   include <termio.h>
30159243Sobrien#  endif /* _UWIN */
302231990Smp#  if SYSVREL > 3 || defined(__linux__)
30359243Sobrien#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
30459243Sobrien#   undef TIOCSLTC
30559243Sobrien#   undef CSWTCH
30659243Sobrien#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
30759243Sobrien#  endif /* SYSVREL > 3 */
30859243Sobrien# endif /* POSIX */
30969408Sache#endif /* WINNT_NATIVE */
31059243Sobrien
31159243Sobrien#ifdef sonyrisc
31259243Sobrien# include <sys/ttold.h>
31359243Sobrien#endif /* sonyrisc */
31459243Sobrien
31569408Sache#if defined(POSIX) && !defined(WINNT_NATIVE)
31659243Sobrien# include <unistd.h>
31759243Sobrien
31859243Sobrien/*
31959243Sobrien * the gcc+protoize version of <stdlib.h>
32059243Sobrien * redefines malloc(), so we define the following
32159243Sobrien * to avoid it.
32259243Sobrien */
323231990Smp# if defined(SYSMALLOC) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX)
32459243Sobrien#  define NO_FIX_MALLOC
32559243Sobrien#  include <stdlib.h>
326145479Smp# else /* glibc */
32759243Sobrien#  define _GNU_STDLIB_H
32859243Sobrien#  define malloc __malloc
32959243Sobrien#  define free __free
33059243Sobrien#  define calloc __calloc
33159243Sobrien#  define realloc __realloc
33259243Sobrien#  include <stdlib.h>
33359243Sobrien#  undef malloc
33459243Sobrien#  undef free
33559243Sobrien#  undef calloc
33659243Sobrien#  undef realloc
337145479Smp# endif /* glibc || sgi */
33869408Sache#endif /* POSIX && !WINNT_NATIVE */
339167465Smp#include <limits.h>
34059243Sobrien
341231990Smp#if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
34259243Sobrien# if !defined(pyr) && !defined(stellar)
34359243Sobrien#  include <time.h>
34459243Sobrien#  ifdef _MINIX
34559243Sobrien#   define HZ CLOCKS_PER_SEC
34659243Sobrien#  endif /* _MINIX */
34759243Sobrien# endif /* !pyr && !stellar */
34859243Sobrien#endif /* SYSVREL > 0 ||  _IBMR2 */
34959243Sobrien
35059243Sobrien/* In the following ifdef the DECOSF1 has been commented so that later
35159243Sobrien * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
35259243Sobrien */
35359243Sobrien#if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
354167465Smp# if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
35559243Sobrien#  include <sys/ioctl.h>
356231990Smp#  if SYSVREL > 3 || defined(__linux__)
357231990Smp#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
358231990Smp#   undef TIOCSLTC
359231990Smp#   undef CSWTCH
360231990Smp#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
361231990Smp#  endif /* SYSVREL > 3 */
36259243Sobrien# endif
36359243Sobrien#endif
36459243Sobrien
36559243Sobrien#if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
36659243Sobrien#undef CSWTCH
36759243Sobrien#define CSWTCH _POSIX_VDISABLE
36859243Sobrien#endif
36959243Sobrien
37069408Sache#if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
37159243Sobrien# include <sys/filio.h>
37259243Sobrien#endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
37359243Sobrien
374167465Smp#if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
37559243Sobrien# include <sys/file.h>
376167465Smp#endif	/* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
37759243Sobrien
37859243Sobrien#if !defined(O_RDONLY) || !defined(O_NDELAY)
37959243Sobrien# include <fcntl.h>
38059243Sobrien#endif
38159243Sobrien
38259243Sobrien#include <errno.h>
38359243Sobrien
38459243Sobrien#include <setjmp.h>
38559243Sobrien
386167465Smp#include <stdarg.h>
38759243Sobrien
388145479Smp#ifdef HAVE_DIRENT_H
38959243Sobrien# include <dirent.h>
39059243Sobrien#else
391145479Smp# ifdef HAVE_NDIR_H
39259243Sobrien#  include <ndir.h>
39359243Sobrien# else
39459243Sobrien#  include <sys/dir.h>
39559243Sobrien# endif
39659243Sobrien# define dirent direct
397145479Smp#endif /* HAVE_DIRENT_H */
398145479Smp#ifndef HAVE_STRUCT_DIRENT_D_INO
399145479Smp# define d_ino d_fileno
400145479Smp#endif
401167465Smp#if defined(hpux) || defined(sgi) || defined(OREO)
40259243Sobrien# include <stdio.h>	/* So the fgetpwent() prototypes work */
403167465Smp#endif /* hpux || sgi || OREO */
40469408Sache#ifndef WINNT_NATIVE
40559243Sobrien#include <pwd.h>
40659243Sobrien#include <grp.h>
40769408Sache#endif /* WINNT_NATIVE */
408145479Smp#ifdef HAVE_SHADOW_H
40959243Sobrien# include <shadow.h>
410145479Smp#endif /* HAVE_SHADOW_H */
411145479Smp#ifdef HAVE_AUTH_H
41259243Sobrien# include <auth.h>
413145479Smp#endif /* HAVE_AUTH_H */
41459243Sobrien#if defined(BSD) && !defined(POSIX)
41559243Sobrien# include <strings.h>
41659243Sobrien# define strchr(a, b) index(a, b)
41759243Sobrien# define strrchr(a, b) rindex(a, b)
41859243Sobrien#else
41959243Sobrien# include <string.h>
42059243Sobrien#endif /* BSD */
42159243Sobrien
42259243Sobrien/*
42359243Sobrien * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
42459243Sobrien * include it yet, so we include it here
42559243Sobrien */
42659243Sobrien#if defined(sgi) && SYSVREL > 3
42759243Sobrien# include <sys/cdefs.h>
42859243Sobrien#endif /* sgi && SYSVREL > 3 */
42959243Sobrien
43059243Sobrien#ifdef REMOTEHOST
43159243Sobrien# ifdef ISC
43259243Sobrien#  undef MAXHOSTNAMELEN	/* Busted headers? */
43359243Sobrien# endif
43459243Sobrien
43559243Sobrien# include <netinet/in.h>
43659243Sobrien# include <arpa/inet.h>
43759243Sobrien# include <sys/socket.h>
438145479Smp# if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
439100616Smp#  if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
440100616Smp#   if !defined(sgi)
441316958Sdchagin#    define INET6
442100616Smp#   endif /* sgi */
443100616Smp#  endif /* __APPLE__ */
44469408Sache# endif
44559243Sobrien# include <sys/uio.h>	/* For struct iovec */
44659243Sobrien#endif /* REMOTEHOST */
44759243Sobrien
44859243Sobrien#ifdef PURIFY
44959243Sobrien/* exit normally, allowing purify to trace leaks */
45059243Sobrien# define _exit		exit
451316958Sdchagin#endif /* !PURIFY */
45259243Sobrien
45369408Sache/*
45469408Sache * ASCII vs. EBCDIC
45569408Sache */
45669408Sache#if 'Z' - 'A' == 25
45769408Sache# ifndef IS_ASCII
45869408Sache#  define IS_ASCII
45969408Sache# endif
46069408Sache#endif
46169408Sache
46259243Sobrien#include "sh.types.h"
46359243Sobrien
464167465Smp#if !HAVE_DECL_GETPGRP
465145479Smp# ifndef GETPGRP_VOID
466167465Smpextern pid_t getpgrp (int);
467145479Smp# else
468167465Smpextern pid_t getpgrp (void);
469145479Smp# endif
470145479Smp#endif
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
490167465Smp#ifdef SYSMALLOC
491167465Smp# define xmalloc(i)	smalloc(i)
492167465Smp# define xrealloc(p, i)	srealloc(p, i)
493167465Smp# define xcalloc(n, s)	scalloc(n, s)
494167465Smp# define xfree		sfree
49559243Sobrien#else
496167465Smp# define xmalloc(i)  	malloc(i)
497167465Smp# define xrealloc(p, i)	realloc(p, i)
498167465Smp# define xcalloc(n, s)	calloc(n, s)
499167465Smp# define xfree	 	free
500167465Smp#endif /* SYSMALLOC */
50159243Sobrien#include "sh.char.h"
50259243Sobrien#include "sh.err.h"
50359243Sobrien#include "sh.dir.h"
50459243Sobrien#include "sh.proc.h"
50559243Sobrien
50659243Sobrien#include "pathnames.h"
50759243Sobrien
50859243Sobrien
50959243Sobrien/*
51059243Sobrien * C shell
51159243Sobrien *
51259243Sobrien * Bill Joy, UC Berkeley
51359243Sobrien * October, 1978; May 1980
51459243Sobrien *
51559243Sobrien * Jim Kulp, IIASA, Laxenburg Austria
51659243Sobrien * April, 1980
51759243Sobrien */
51859243Sobrien
51959243Sobrien#ifdef HESIOD
52059243Sobrien# include <hesiod.h>
52159243Sobrien#endif /* HESIOD */
52259243Sobrien
52359243Sobrien#ifdef REMOTEHOST
52459243Sobrien# include <netdb.h>
52559243Sobrien#endif /* REMOTEHOST */
52659243Sobrien
52759243Sobrien#ifndef MAXHOSTNAMELEN
528167465Smp# ifdef HOST_NAME_MAX
529167465Smp#  define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
530167465Smp# elif defined(SCO) && (SYSVREL > 3)
53159243Sobrien#  include <sys/socket.h>
53259243Sobrien# else
53383098Smp#  define MAXHOSTNAMELEN 256
53459243Sobrien# endif
53559243Sobrien#endif /* MAXHOSTNAMELEN */
53659243Sobrien
53759243Sobrien
53859243Sobrien
53959243Sobrien#define	eq(a, b)	(Strcmp(a, b) == 0)
54059243Sobrien
54159243Sobrien/* globone() flags */
54259243Sobrien#define G_ERROR		0	/* default action: error if multiple words */
54359243Sobrien#define G_IGNORE	1	/* ignore the rest of the words		   */
54459243Sobrien#define G_APPEND	2	/* make a sentence by cat'ing the words    */
54559243Sobrien
54659243Sobrien/*
54759243Sobrien * Global flags
54859243Sobrien */
549145479SmpEXTERN int    chkstop IZERO;	/* Warned of stopped jobs... allow exit */
55059243Sobrien
55159243Sobrien#if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
55259243Sobrien# define CLOSE_ON_EXEC
55359243Sobrien#else
554145479SmpEXTERN int    didcch IZERO;	/* Have closed unused fd's for child */
55559243Sobrien#endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
55659243Sobrien
557145479SmpEXTERN int    didfds IZERO;	/* Have setup i/o fd's for child */
558145479SmpEXTERN int    doneinp IZERO;	/* EOF indicator after reset from readc */
559145479SmpEXTERN int    exiterr IZERO;	/* Exit if error or non-zero exit status */
560145479SmpEXTERN int    child IZERO;	/* Child shell ... errors cause exit */
561145479SmpEXTERN int    haderr IZERO;	/* Reset was because of an error */
562145479SmpEXTERN int    intty IZERO;	/* Input is a tty */
563145479SmpEXTERN int    intact IZERO;	/* We are interactive... therefore prompt */
564145479SmpEXTERN int    justpr IZERO;	/* Just print because of :p hist mod */
565145479SmpEXTERN int    loginsh IZERO;	/* We are a loginsh -> .login/.logout */
566145479SmpEXTERN int    neednote IZERO;	/* Need to pnotify() */
567145479SmpEXTERN int    noexec IZERO;	/* Don't execute, just syntax check */
568145479SmpEXTERN int    pjobs IZERO;	/* want to print jobs if interrupted */
569145479SmpEXTERN int    setintr IZERO;	/* Set interrupts on/off -> Wait intr... */
570316958SdchaginEXTERN int    handle_interrupt IZERO;/* Are we currently handling an interrupt? */
571145479SmpEXTERN int    havhash IZERO;	/* path hashing is available */
572145479SmpEXTERN int    editing IZERO;	/* doing filename expansion and line editing */
573145479SmpEXTERN int    noediting IZERO;	/* initial $term defaulted to noedit */
574145479SmpEXTERN int    bslash_quote IZERO;/* PWP: tcsh-style quoting?  (in sh.c) */
575231990SmpEXTERN int    anyerror IZERO;	/* propagate errors from pipelines/backq */
576195609SmpEXTERN int    compat_expr IZERO;/* csh-style expressions? */
577145479SmpEXTERN int    isoutatty IZERO;	/* is SHOUT a tty */
578145479SmpEXTERN int    isdiagatty IZERO;/* is SHDIAG a tty */
579145479SmpEXTERN int    is1atty IZERO;	/* is file descriptor 1 a tty (didfds mode) */
580145479SmpEXTERN int    is2atty IZERO;	/* is file descriptor 2 a tty (didfds mode) */
581145479SmpEXTERN int    arun IZERO;	/* Currently running multi-line-aliases */
582316958SdchaginEXTERN int    implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
583316958SdchaginEXTERN int    cdtohome IZERO;	/* cd without args goes home */
584145479SmpEXTERN int    inheredoc IZERO;	/* Currently parsing a heredoc */
585316958SdchaginEXTERN int    no_clobber IZERO;	/* no clobber enabled? 1=yes 2=notempty, 4=ask*/
586167465Smp/* We received a window change event */
587167465SmpEXTERN volatile sig_atomic_t windowchg IZERO;
588145479Smp#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
589145479SmpEXTERN int    dspmbyte_ls;
590145479Smp#endif
59159243Sobrien
59259243Sobrien/*
59359243Sobrien * Global i/o info
59459243Sobrien */
59559243SobrienEXTERN Char   *arginp IZERO;	/* Argument input for sh -c and internal `xx` */
59659243SobrienEXTERN int     onelflg IZERO;	/* 2 -> need line for -t, 1 -> exit on read */
59759243Sobrienextern Char   *ffile;		/* Name of shell file for $0 */
598145479Smpextern int    dolzero;		/* if $?0 should return true... */
59959243Sobrien
60059243Sobrienextern char *seterr;		/* Error message from scanner/parser */
601145479Smp#ifndef errno
60259243Sobrienextern int errno;		/* Error from C library routines */
60369408Sache#endif
60469408Sacheextern int exitset;
605167465Smp/* Temp name for << shell files in /tmp, for xfree() */
606167465SmpEXTERN Char   *shtemp IZERO;
60759243Sobrien
60859243Sobrien#ifdef BSDTIMES
60959243SobrienEXTERN struct timeval time0;	/* Time at which the shell started */
61059243SobrienEXTERN struct sysrusage ru0;
61159243Sobrien#else
61259243Sobrien# ifdef _SEQUENT_
61359243SobrienEXTERN timeval_t time0;		/* time at which shell started */
61459243SobrienEXTERN struct process_stats ru0;
61559243Sobrien# else /* _SEQUENT_ */
61659243Sobrien#  ifndef POSIX
61759243SobrienEXTERN time_t  time0;		/* time at which shell started */
61859243Sobrien#  else	/* POSIX */
61959243SobrienEXTERN clock_t time0;		/* time at which shell started */
62059243SobrienEXTERN clock_t clk_tck;
62159243Sobrien#  endif /* POSIX */
62259243SobrienEXTERN struct tms shtimes;	/* shell and child times for process timing */
62359243Sobrien# endif /* _SEQUENT_ */
624167465SmpEXTERN time_t seconds0;
62559243Sobrien#endif /* BSDTIMES */
62659243Sobrien
62759243Sobrien#ifndef HZ
62859243Sobrien# define HZ	100		/* for division into seconds */
62959243Sobrien#endif
63059243Sobrien
63159243Sobrien/*
63259243Sobrien * Miscellany
63359243Sobrien */
634316958SdchaginEXTERN pid_t   mainpid;		/* pid of the main shell ($$) */
63559243SobrienEXTERN Char   *doldol;		/* Character pid for $$ */
636167465SmpEXTERN pid_t   backpid;		/* pid of the last background job */
63759243Sobrien
638316958Sdchagin
63959243Sobrien/*
64059243Sobrien * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
64159243Sobrien * cause pid's could be unsigned and that would break our -1 flag, and
64259243Sobrien * uid_t and gid_t are not defined in all the systems so I would have to
64359243Sobrien * make special cases for them. In the future...
64459243Sobrien */
645145479SmpEXTERN uid_t   uid, euid; 	/* Invokers real and effective */
646145479SmpEXTERN gid_t   gid, egid;	/* User and group ids */
647145479SmpEXTERN pid_t   opgrp,		/* Initial pgrp and tty pgrp */
64859243Sobrien               shpgrp,		/* Pgrp of shell */
64959243Sobrien               tpgrp;		/* Terminal process group */
65059243Sobrien				/* If tpgrp is -1, leave tty alone! */
65159243Sobrien
652167465SmpEXTERN Char   *Prompt;		/* The actual printed prompt or NULL */
653167465SmpEXTERN Char   *RPrompt;		/* Right-hand side prompt or NULL */
65459243Sobrien
65559243Sobrien/*
65659243Sobrien * To be able to redirect i/o for builtins easily, the shell moves the i/o
65759243Sobrien * descriptors it uses away from 0,1,2.
65859243Sobrien * Ideally these should be in units which are closed across exec's
65959243Sobrien * (this saves work) but for version 6, this is not usually possible.
66059243Sobrien * The desired initial values for these descriptors are defined in
66159243Sobrien * sh.local.h.
66259243Sobrien */
66359243SobrienEXTERN int   SHIN IZERO;	/* Current shell input (script) */
66459243SobrienEXTERN int   SHOUT IZERO;	/* Shell output */
66559243SobrienEXTERN int   SHDIAG IZERO;	/* Diagnostic output... shell errs go here */
66659243SobrienEXTERN int   OLDSTD IZERO;	/* Old standard input (def for cmds) */
66759243Sobrien
66859243Sobrien
669231990Smp#if (SYSVREL == 4 && defined(_UTS)) || defined(__linux__)
67059243Sobrien/*
67159243Sobrien * From: fadden@uts.amdahl.com (Andy McFadden)
67259243Sobrien * we need sigsetjmp for UTS4, but not UTS2.1
67359243Sobrien */
67459243Sobrien# define SIGSETJMP
67559243Sobrien#endif
67659243Sobrien
67759243Sobrien/*
67859243Sobrien * Error control
67959243Sobrien *
68059243Sobrien * Errors in scanning and parsing set up an error message to be printed
68159243Sobrien * at the end and complete.  Other errors always cause a reset.
68259243Sobrien * Because of source commands and .cshrc we need nested error catches.
68359243Sobrien */
68459243Sobrien
685167465Smp#ifdef SIGSETJMP
686167465Smp   typedef struct { sigjmp_buf j; } jmp_buf_t;
687231990Smp# define setexit()  sigsetjmp(reslab.j, 1)
688167465Smp# define _reset()    siglongjmp(reslab.j, 1)
68959243Sobrien#else
69059243Sobrien   typedef struct { jmp_buf j; } jmp_buf_t;
691167465Smp# define setexit()  setjmp(reslab.j)
692167465Smp# define _reset()    longjmp(reslab.j, 1)
693167465Smp#endif
69459243Sobrien
695167465Smp#define getexit(a) (void) ((a) = reslab)
696167465Smp#define resexit(a) (void) (reslab = (a))
69759243Sobrien
698167465Smp#define cpybin(a, b) (void) ((a) = (b))
69959243Sobrien
70059243Sobrienextern jmp_buf_t reslab;
70159243Sobrien
70259243SobrienEXTERN Char   *gointr;		/* Label for an onintr transfer */
70359243Sobrien
704167465Smpextern struct sigaction parintr;	/* Parents interrupt catch */
705167465Smpextern struct sigaction parterm;	/* Parents terminate catch */
70659243Sobrien
70759243Sobrien/*
70859243Sobrien * Lexical definitions.
70959243Sobrien *
71059243Sobrien * All lexical space is allocated dynamically.
71159243Sobrien * The eighth/sixteenth bit of characters is used to prevent recognition,
71259243Sobrien * and eventually stripped.
71359243Sobrien */
71459243Sobrien#define		META		0200
71559243Sobrien#define		ASCII		0177
716145479Smp#ifdef WIDE_STRINGS		/* Implies SHORT_STRINGS */
717145479Smp/* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
718316958Sdchagin/*
719316958Sdchagin * Notice
720316958Sdchagin *
721316958Sdchagin * By fix for handling unicode name file, 32nd bit is used.
722316958Sdchagin * We need use '&' instead of '> or <' when comparing with INVALID_BYTE etc..
723316958Sdchagin * Cast to uChar is not recommended,
724316958Sdchagin *  becase Char is 4bytes but uChar is 8bytes on I32LP64. */
725316958Sdchagin# define	QUOTE		0x80000000
726316958Sdchagin# define	TRIM		0x7FFFFFFF /* Mask to strip quote bit */
727195609Smp# define	UNDER		0x08000000 /* Underline flag */
728195609Smp# define	BOLD		0x04000000 /* Bold flag */
729195609Smp# define	STANDOUT	0x02000000 /* Standout flag */
730195609Smp# define	LITERAL		0x01000000 /* Literal character flag */
731195609Smp# define	ATTRIBUTES	0x0F000000 /* The bits used for attributes */
732316958Sdchagin# define	INVALID_BYTE	0xF0000000 /* Invalid character on input */
733195609Smp# ifdef SOLARIS2
734195609Smp#  define	CHAR		0x30FFFFFF /* Mask to mask out the character */
735195609Smp# else
736195609Smp#  define	CHAR		0x00FFFFFF /* Mask to mask out the character */
737195609Smp# endif
738145479Smp#elif defined (SHORT_STRINGS)
73959243Sobrien# define	QUOTE 	((Char)	0100000)/* 16nth char bit used for 'ing */
740145479Smp# define	TRIM		0073777	/* Mask to strip quote/lit bit */
74159243Sobrien# define	UNDER		0040000	/* Underline flag */
74259243Sobrien# define	BOLD		0020000	/* Bold flag */
74359243Sobrien# define	STANDOUT	0010000	/* Standout flag */
74459243Sobrien# define	LITERAL		0004000	/* Literal character flag */
74559243Sobrien# define	ATTRIBUTES	0074000	/* The bits used for attributes */
746145479Smp# define	INVALID_BYTE	0
74759243Sobrien# define	CHAR		0000377	/* Mask to mask out the character */
74859243Sobrien#else
74959243Sobrien# define	QUOTE 	((Char)	0200)	/* Eighth char bit used for 'ing */
75059243Sobrien# define	TRIM		0177	/* Mask to strip quote bit */
75159243Sobrien# define	UNDER		0000000	/* No extra bits to do both */
75259243Sobrien# define	BOLD		0000000	/* Bold flag */
75359243Sobrien# define	STANDOUT	META	/* Standout flag */
75459243Sobrien# define	LITERAL		0000000	/* Literal character flag */
75559243Sobrien# define	ATTRIBUTES	0200	/* The bits used for attributes */
756145479Smp# define	INVALID_BYTE	0
75759243Sobrien# define	CHAR		0000177	/* Mask to mask out the character */
758167465Smp#endif
759145479Smp#define		CHAR_DBWIDTH	(LITERAL|(LITERAL-1))
76059243Sobrien
761316958Sdchagin# define 	MAX_UTF32	0x7FFFFFFF	/* max UTF32 is U+7FFFFFFF */
762316958Sdchagin
76359243SobrienEXTERN int     AsciiOnly;	/* If set only 7 bits expected in characters */
76459243Sobrien
76559243Sobrien/*
76659243Sobrien * Each level of input has a buffered input structure.
76759243Sobrien * There are one or more blocks of buffered input for each level,
76859243Sobrien * exactly one if the input is seekable and tell is available.
76959243Sobrien * In other cases, the shell buffers enough blocks to keep all loops
77059243Sobrien * in the buffer.
771167465Smp *
772167465Smp * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
773167465Smp * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
774167465Smp * fbuf indexing).
775167465Smp *
776167465Smp * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
777167465Smp * is no difference between byte and character offsets.
77859243Sobrien */
77959243SobrienEXTERN struct Bin {
780145479Smp    off_t   Bfseekp;		/* Seek pointer, generally != lseek() value */
78159243Sobrien    off_t   Bfbobp;		/* Seekp of beginning of buffers */
78259243Sobrien    off_t   Bfeobp;		/* Seekp of end of buffers */
78359243Sobrien    int     Bfblocks;		/* Number of buffer blocks */
78459243Sobrien    Char  **Bfbuf;		/* The array of buffer blocks */
785145479Smp#ifdef WIDE_STRINGS
786145479Smp    /* Number of bytes in each character if (cantell) */
787145479Smp    unsigned char Bfclens[BUFSIZE + 1];
788145479Smp#endif
78959243Sobrien}       B;
79059243Sobrien
79159243Sobrien/*
79259243Sobrien * This structure allows us to seek inside aliases
79359243Sobrien */
79459243Sobrienstruct Ain {
79559243Sobrien    int type;
79669408Sache#define TCSH_I_SEEK 	 0		/* Invalid seek */
79769408Sache#define TCSH_A_SEEK	 1		/* Alias seek */
79869408Sache#define TCSH_F_SEEK	 2		/* File seek */
79969408Sache#define TCSH_E_SEEK	 3		/* Eval seek */
80059243Sobrien    union {
801167465Smp	off_t _f_seek;		/* A byte offset if (cantell) */
80259243Sobrien	Char* _c_seek;
80359243Sobrien    } fc;
80459243Sobrien#define f_seek fc._f_seek
80559243Sobrien#define c_seek fc._c_seek
80659243Sobrien    Char **a_seek;
80759243Sobrien} ;
80859243Sobrien
80959243Sobrienextern int aret;		/* Type of last char returned */
81059243Sobrien#define SEEKEQ(a, b) ((a)->type == (b)->type && \
81159243Sobrien		      (a)->f_seek == (b)->f_seek && \
81259243Sobrien		      (a)->a_seek == (b)->a_seek)
81359243Sobrien
81459243Sobrien#define	fseekp	B.Bfseekp
81559243Sobrien#define	fbobp	B.Bfbobp
81659243Sobrien#define	feobp	B.Bfeobp
81759243Sobrien#define	fblocks	B.Bfblocks
81859243Sobrien#define	fbuf	B.Bfbuf
819145479Smp#define fclens  B.Bfclens
82059243Sobrien
82159243Sobrien/*
82259243Sobrien * The shell finds commands in loops by reseeking the input
82359243Sobrien * For whiles, in particular, it reseeks to the beginning of the
82459243Sobrien * line the while was on; hence the while placement restrictions.
82559243Sobrien */
82659243SobrienEXTERN struct Ain lineloc;
82759243Sobrien
828145479SmpEXTERN int    cantell;		/* Is current source tellable ? */
82959243Sobrien
83059243Sobrien/*
83159243Sobrien * Input lines are parsed into doubly linked circular
83259243Sobrien * lists of words of the following form.
83359243Sobrien */
83459243Sobrienstruct wordent {
83559243Sobrien    Char   *word;
83659243Sobrien    struct wordent *prev;
83759243Sobrien    struct wordent *next;
83859243Sobrien};
83959243Sobrien
84059243Sobrien/*
84159243Sobrien * During word building, both in the initial lexical phase and
84259243Sobrien * when expanding $ variable substitutions, expansion by `!' and `$'
84359243Sobrien * must be inhibited when reading ahead in routines which are themselves
84459243Sobrien * processing `!' and `$' expansion or after characters such as `\' or in
84559243Sobrien * quotations.  The following flags are passed to the getC routines
84659243Sobrien * telling them which of these substitutions are appropriate for the
84759243Sobrien * next character to be returned.
84859243Sobrien */
84959243Sobrien#define	DODOL	1
85059243Sobrien#define	DOEXCL	2
85159243Sobrien#define	DOALL	DODOL|DOEXCL
85259243Sobrien
85359243Sobrien/*
85459243Sobrien * Labuf implements a general buffer for lookahead during lexical operations.
85559243Sobrien * Text which is to be placed in the input stream can be stuck here.
85659243Sobrien * We stick parsed ahead $ constructs during initial input,
85759243Sobrien * process id's from `$$', and modified variable values (from qualifiers
85859243Sobrien * during expansion in sh.dol.c) here.
85959243Sobrien */
860167465Smpextern struct Strbuf labuf;
861167465SmpEXTERN size_t lap; /* N/A if == labuf.len, index into labuf.s otherwise */
86259243Sobrien
86359243Sobrien/*
86459243Sobrien * Parser structure
86559243Sobrien *
86659243Sobrien * Each command is parsed to a tree of command structures and
86759243Sobrien * flags are set bottom up during this process, to be propagated down
86859243Sobrien * as needed during the semantics/exeuction pass (sh.sem.c).
86959243Sobrien */
87059243Sobrienstruct command {
87159243Sobrien    unsigned char   t_dtyp;	/* Type of node 		 */
87259243Sobrien#define	NODE_COMMAND	1	/* t_dcom <t_dlef >t_drit	 */
87359243Sobrien#define	NODE_PAREN	2	/* ( t_dspr ) <t_dlef >t_drit	 */
87459243Sobrien#define	NODE_PIPE	3	/* t_dlef | t_drit		 */
87559243Sobrien#define	NODE_LIST	4	/* t_dlef ; t_drit		 */
87659243Sobrien#define	NODE_OR		5	/* t_dlef || t_drit		 */
87759243Sobrien#define	NODE_AND	6	/* t_dlef && t_drit		 */
87859243Sobrien    unsigned char   t_nice;	/* Nice value			 */
87959243Sobrien#ifdef apollo
88059243Sobrien    unsigned char   t_systype;	/* System environment		 */
88159243Sobrien#endif
88259243Sobrien    unsigned long   t_dflg;	/* Flags, e.g. F_AMPERSAND|... 	 */
88359243Sobrien/* save these when re-doing 	 */
88459243Sobrien#ifndef apollo
88559243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP|F_HUP)
88659243Sobrien#else
88759243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
88859243Sobrien#endif
88959243Sobrien#define	F_AMPERSAND	(1<<0)	/* executes in background	 */
89059243Sobrien#define	F_APPEND	(1<<1)	/* output is redirected >>	 */
89159243Sobrien#define	F_PIPEIN	(1<<2)	/* input is a pipe		 */
89259243Sobrien#define	F_PIPEOUT	(1<<3)	/* output is a pipe		 */
89359243Sobrien#define	F_NOFORK	(1<<4)	/* don't fork, last ()ized cmd	 */
89459243Sobrien#define	F_NOINTERRUPT	(1<<5)	/* should be immune from intr's */
89559243Sobrien/* spare */
89659243Sobrien#define	F_STDERR	(1<<7)	/* redirect unit 2 with unit 1	 */
89759243Sobrien#define	F_OVERWRITE	(1<<8)	/* output was !			 */
89859243Sobrien#define	F_READ		(1<<9)	/* input redirection is <<	 */
89959243Sobrien#define	F_REPEAT	(1<<10)	/* reexec aft if, repeat,...	 */
90059243Sobrien#define	F_NICE		(1<<11)	/* t_nice is meaningful 	 */
90159243Sobrien#define	F_NOHUP		(1<<12)	/* nohup this command 		 */
90259243Sobrien#define	F_TIME		(1<<13)	/* time this command 		 */
90359243Sobrien#define F_BACKQ		(1<<14)	/* command is in ``		 */
90459243Sobrien#define F_HUP		(1<<15)	/* hup this command		 */
90559243Sobrien#ifdef apollo
90659243Sobrien#define F_VER		(1<<16)	/* execute command under SYSTYPE */
90759243Sobrien#endif
90859243Sobrien    union {
90959243Sobrien	Char   *T_dlef;		/* Input redirect word 		 */
91059243Sobrien	struct command *T_dcar;	/* Left part of list/pipe 	 */
91159243Sobrien    }       L;
91259243Sobrien    union {
91359243Sobrien	Char   *T_drit;		/* Output redirect word 	 */
91459243Sobrien	struct command *T_dcdr;	/* Right part of list/pipe 	 */
91559243Sobrien    }       R;
91659243Sobrien#define	t_dlef	L.T_dlef
91759243Sobrien#define	t_dcar	L.T_dcar
91859243Sobrien#define	t_drit	R.T_drit
91959243Sobrien#define	t_dcdr	R.T_dcdr
92059243Sobrien    Char  **t_dcom;		/* Command/argument vector 	 */
92159243Sobrien    struct command *t_dspr;	/* Pointer to ()'d subtree 	 */
92259243Sobrien};
92359243Sobrien
92459243Sobrien
92559243Sobrien/*
92659243Sobrien * The keywords for the parser
92759243Sobrien */
92859243Sobrien#define	TC_BREAK	0
92959243Sobrien#define	TC_BRKSW	1
93059243Sobrien#define	TC_CASE		2
93159243Sobrien#define	TC_DEFAULT 	3
93259243Sobrien#define	TC_ELSE		4
93359243Sobrien#define	TC_END		5
93459243Sobrien#define	TC_ENDIF	6
93559243Sobrien#define	TC_ENDSW	7
93659243Sobrien#define	TC_EXIT		8
93759243Sobrien#define	TC_FOREACH	9
93859243Sobrien#define	TC_GOTO		10
93959243Sobrien#define	TC_IF		11
94059243Sobrien#define	TC_LABEL	12
94159243Sobrien#define	TC_LET		13
94259243Sobrien#define	TC_SET		14
94359243Sobrien#define	TC_SWITCH	15
94459243Sobrien#define	TC_TEST		16
94559243Sobrien#define	TC_THEN		17
94659243Sobrien#define	TC_WHILE	18
94759243Sobrien
94859243Sobrien/*
94959243Sobrien * These are declared here because they want to be
95059243Sobrien * initialized in sh.init.c (to allow them to be made readonly)
95159243Sobrien */
95259243Sobrien
95359243Sobrien#if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
95459243Sobrien    /* Avoid hpux ansi mode spurious warnings */
95559243Sobrientypedef void (*bfunc_t) ();
95659243Sobrien#else
957167465Smptypedef void (*bfunc_t) (Char **, struct command *);
95859243Sobrien#endif /* hpux && __STDC__ && !__GNUC__ */
95959243Sobrien
960167465Smpextern const struct biltins {
961145479Smp    const char   *bname;
96259243Sobrien    bfunc_t bfunct;
96359243Sobrien    int     minargs, maxargs;
96459243Sobrien} bfunc[];
96559243Sobrienextern int nbfunc;
96669408Sache#ifdef WINNT_NATIVE
96759243Sobrienextern struct biltins  nt_bfunc[];
96859243Sobrienextern int nt_nbfunc;
96969408Sache#endif /* WINNT_NATIVE*/
970145479Smpextern int bequiet;
97159243Sobrien
97259243Sobrienextern struct srch {
973145479Smp    const char *s_name;
974145479Smp    int  s_value;
975145479Smp} srchn[];
97659243Sobrienextern int nsrchn;
97759243Sobrien
97859243Sobrien/*
97959243Sobrien * Structure defining the existing while/foreach loops at this
98059243Sobrien * source level.  Loops are implemented by seeking back in the
98159243Sobrien * input.  For foreach (fe), the word list is attached here.
98259243Sobrien */
98359243SobrienEXTERN struct whyle {
98459243Sobrien    struct Ain   w_start;	/* Point to restart loop */
98559243Sobrien    struct Ain   w_end;		/* End of loop (0 if unknown) */
98659243Sobrien    Char  **w_fe, **w_fe0;	/* Current/initial wordlist for fe */
98759243Sobrien    Char   *w_fename;		/* Name for fe */
98859243Sobrien    struct whyle *w_next;	/* Next (more outer) loop */
98959243Sobrien}      *whyles;
99059243Sobrien
99159243Sobrien/*
99259243Sobrien * Variable structure
99359243Sobrien *
99459243Sobrien * Aliases and variables are stored in AVL balanced binary trees.
99559243Sobrien */
99659243SobrienEXTERN struct varent {
99759243Sobrien    Char  **vec;		/* Array of words which is the value */
99859243Sobrien    Char   *v_name;		/* Name of variable/alias */
99959243Sobrien    int	    v_flags;		/* Flags */
100059243Sobrien#define VAR_ALL		-1
100159243Sobrien#define VAR_READONLY	1
100259243Sobrien#define VAR_READWRITE	2
100359243Sobrien#define VAR_NOGLOB	4
100459243Sobrien#define VAR_FIRST       32
100559243Sobrien#define VAR_LAST        64
100659243Sobrien    struct varent *v_link[3];	/* The links, see below */
100759243Sobrien    int     v_bal;		/* Balance factor */
100859243Sobrien}       shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
100959243Sobrien
101059243Sobrien#define v_left		v_link[0]
101159243Sobrien#define v_right		v_link[1]
101259243Sobrien#define v_parent	v_link[2]
101359243Sobrien
101459243Sobrien#define adrof(v)	adrof1(v, &shvhed)
101559243Sobrien#define varval(v)	value1(v, &shvhed)
101659243Sobrien
101759243Sobrien/*
101859243Sobrien * The following are for interfacing redo substitution in
101959243Sobrien * aliases to the lexical routines.
102059243Sobrien */
102159243SobrienEXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
102259243SobrienEXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
102359243SobrienEXTERN Char  **alvec IZERO_STRUCT,
102459243Sobrien	      *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
102559243Sobrien
102659243Sobrien/*
102759243Sobrien * Filename/command name expansion variables
102859243Sobrien */
102959243Sobrien
103059243Sobrien#ifndef MAXPATHLEN
1031167465Smp# ifdef PATH_MAX
1032167465Smp#  define MAXPATHLEN PATH_MAX
1033167465Smp# else
1034167465Smp#  define MAXPATHLEN 2048
1035167465Smp# endif
103659243Sobrien#endif /* MAXPATHLEN */
103759243Sobrien
103859243Sobrien#ifndef HAVENOLIMIT
103959243Sobrien/*
104059243Sobrien * resource limits
104159243Sobrien */
104259243Sobrienextern struct limits {
1043145479Smp    int         limconst;
1044145479Smp    const char *limname;
1045145479Smp    int         limdiv;
1046145479Smp    const char *limscale;
104759243Sobrien} limits[];
104859243Sobrien#endif /* !HAVENOLIMIT */
104959243Sobrien
105059243Sobrien/*
105159243Sobrien * History list
105259243Sobrien *
105359243Sobrien * Each history list entry contains an embedded wordlist
105459243Sobrien * from the scanner, a number for the event, and a reference count
105559243Sobrien * to aid in discarding old entries.
105659243Sobrien *
105759243Sobrien * Essentially "invisible" entries are put on the history list
105859243Sobrien * when history substitution includes modifiers, and thrown away
105959243Sobrien * at the next discarding since their event numbers are very negative.
106059243Sobrien */
106159243SobrienEXTERN struct Hist {
106259243Sobrien    struct wordent Hlex;
1063231990Smp    int     Hnum;		 /* eventno when inserted into history list  */
106459243Sobrien    int     Href;
106559243Sobrien    time_t  Htime;
106659243Sobrien    Char   *histline;
1067231990Smp    struct Hist *Hnext, *Hprev;         /* doubly linked list */
1068231990Smp    unsigned Hhash;                     /* hash value of command line */
106959243Sobrien}       Histlist IZERO_STRUCT;
107059243Sobrien
1071316958Sdchaginextern struct wordent paraml;	/* Current lexical word list */
107259243SobrienEXTERN int     eventno;		/* Next events number */
107359243SobrienEXTERN int     lastev;		/* Last event reference (default) */
107459243Sobrien
107559243SobrienEXTERN Char    HIST;		/* history invocation character */
107659243SobrienEXTERN Char    HISTSUB;		/* auto-substitute character */
107759243SobrienEXTERN Char    PRCH;		/* Prompt symbol for regular users */
107859243SobrienEXTERN Char    PRCHROOT;	/* Prompt symbol for root */
107959243Sobrien
108059243Sobrien/*
108159243Sobrien * For operating systems with single case filenames (OS/2)
108259243Sobrien */
108359243Sobrien#ifdef CASE_INSENSITIVE
1084145479Smp# ifdef WIDE_STRINGS
1085145479Smp#  define samecase(x) (towlower(x))
1086145479Smp# else
1087145479Smp#  define samecase(x) (isupper((unsigned char)(x)) ? \
1088145479Smp		       tolower((unsigned char)(x)) : (x))
1089145479Smp# endif
109059243Sobrien#else
109159243Sobrien# define samecase(x) (x)
109259243Sobrien#endif /* CASE_INSENSITIVE */
109359243Sobrien
109459243Sobrien/*
109559243Sobrien * strings.h:
109659243Sobrien */
109759243Sobrien#ifndef SHORT_STRINGS
109859243Sobrien#define Strchr(a, b)  		strchr(a, b)
109959243Sobrien#define Strrchr(a, b)  		strrchr(a, b)
110059243Sobrien#define Strcat(a, b)  		strcat(a, b)
110159243Sobrien#define Strncat(a, b, c) 	strncat(a, b, c)
110259243Sobrien#define Strcpy(a, b)  		strcpy(a, b)
110359243Sobrien#define Strncpy(a, b, c) 	strncpy(a, b, c)
110459243Sobrien#define Strlen(a)		strlen(a)
110559243Sobrien#define Strcmp(a, b)		strcmp(a, b)
110659243Sobrien#define Strncmp(a, b, c)	strncmp(a, b, c)
1107131962Smp#define Strcasecmp(a, b)	strcasecmp(a, b)
110859243Sobrien
110959243Sobrien#define Strspl(a, b)		strspl(a, b)
1110167465Smp#define Strnsave(a, b)		strnsave(a, b)
111159243Sobrien#define Strsave(a)		strsave(a)
111259243Sobrien#define Strend(a)		strend(a)
111359243Sobrien#define Strstr(a, b)		strstr(a, b)
111459243Sobrien
111559243Sobrien#define str2short(a) 		(a)
111659243Sobrien#define blk2short(a) 		saveblk(a)
111759243Sobrien#define short2blk(a) 		saveblk(a)
1118167465Smp#define short2str(a) 		caching_strip(a)
111959243Sobrien#else
1120231990Smp#if defined(WIDE_STRINGS) && !defined(UTF16_STRINGS)
1121145479Smp#define Strchr(a, b)		wcschr(a, b)
1122145479Smp#define Strrchr(a, b)		wcsrchr(a, b)
1123145479Smp#define Strcat(a, b)  		wcscat(a, b)
1124145479Smp#define Strncat(a, b, c) 	wcsncat(a, b, c)
1125145479Smp#define Strcpy(a, b)  		wcscpy(a, b)
1126145479Smp#define Strncpy(a, b, c)	wcsncpy(a, b, c)
1127145479Smp#define Strlen(a)		wcslen(a)
1128145479Smp#define Strcmp(a, b)		wcscmp(a, b)
1129145479Smp#define Strncmp(a, b, c)	wcsncmp(a, b, c)
1130145479Smp#else
113159243Sobrien#define Strchr(a, b)		s_strchr(a, b)
113259243Sobrien#define Strrchr(a, b) 		s_strrchr(a, b)
113359243Sobrien#define Strcat(a, b)  		s_strcat(a, b)
113459243Sobrien#define Strncat(a, b, c) 	s_strncat(a, b, c)
113559243Sobrien#define Strcpy(a, b)  		s_strcpy(a, b)
113659243Sobrien#define Strncpy(a, b, c)	s_strncpy(a, b, c)
113759243Sobrien#define Strlen(a)		s_strlen(a)
113859243Sobrien#define Strcmp(a, b)		s_strcmp(a, b)
113959243Sobrien#define Strncmp(a, b, c)	s_strncmp(a, b, c)
1140145479Smp#endif
1141131962Smp#define Strcasecmp(a, b)	s_strcasecmp(a, b)
114259243Sobrien
114359243Sobrien#define Strspl(a, b)		s_strspl(a, b)
1144167465Smp#define Strnsave(a, b)		s_strnsave(a, b)
114559243Sobrien#define Strsave(a)		s_strsave(a)
114659243Sobrien#define Strend(a)		s_strend(a)
114759243Sobrien#define Strstr(a, b)		s_strstr(a, b)
114859243Sobrien#endif
114959243Sobrien
115059243Sobrien/*
115159243Sobrien * setname is a macro to save space (see sh.err.c)
115259243Sobrien */
1153145479SmpEXTERN const char   *bname;
115459243Sobrien
115559243Sobrien#define	setname(a)	(bname = (a))
115659243Sobrien
115759243Sobrien#ifdef VFORK
115859243SobrienEXTERN Char   *Vsav;
115959243SobrienEXTERN Char   *Vdp;
116059243SobrienEXTERN Char   *Vexpath;
116159243SobrienEXTERN char  **Vt;
116259243Sobrien#endif /* VFORK */
116359243Sobrien
116459243SobrienEXTERN Char  **evalvec;
116559243SobrienEXTERN Char   *evalp;
116659243Sobrien
116759243Sobrienextern struct mesg {
1168145479Smp    const char   *iname;	/* name from /usr/include */
1169145479Smp    const char *pname;		/* print name */
1170145479Smp} mesg[];
117159243Sobrien
1172316958Sdchagin/* word_chars is set by default to WORD_CHARS (or WORD_CHARS_VI) but can
1173316958Sdchagin   be overridden by the wordchars variable--if unset, reverts to
1174316958Sdchagin   WORD_CHARS (or WORD_CHARS_VI) */
117559243Sobrien
117659243SobrienEXTERN Char   *word_chars;
117759243Sobrien
117859243Sobrien#define WORD_CHARS "*?_-.[]~="	/* default chars besides alnums in words */
1179316958Sdchagin#define WORD_CHARS_VI "_"	/* default chars besides alnums in words */
118059243Sobrien
118159243SobrienEXTERN Char   *STR_SHELLPATH;
118259243Sobrien
118359243Sobrien#ifdef _PATH_BSHELL
118459243SobrienEXTERN Char   *STR_BSHELL;
118559243Sobrien#endif
118659243SobrienEXTERN Char   *STR_WORD_CHARS;
1187316958SdchaginEXTERN Char   *STR_WORD_CHARS_VI;
118859243SobrienEXTERN Char  **STR_environ IZERO;
118959243Sobrien
119059243Sobrienextern int     dont_free;	/* Tell free that we are in danger if we free */
119159243Sobrien
119259243Sobrienextern Char    *INVPTR;
119359243Sobrienextern Char    **INVPPTR;
119459243Sobrien
119559243Sobrienextern char    *progname;
119659243Sobrienextern int	tcsh;
1197145479Smpextern int	xlate_cr;
1198145479Smpextern int	output_raw;
1199145479Smpextern int	lbuffed;
1200145479Smpextern time_t	Htime;
1201145479Smpextern int	numeof;
1202145479Smpextern int 	insource;
1203145479Smpextern char	linbuf[];
1204145479Smpextern char 	*linp;
1205145479Smpextern int	nsig;
1206145479Smp#ifdef VFORK
1207145479Smpextern int	use_fork;
1208145479Smp#endif
1209145479Smpextern int	tellwhat;
1210145479Smpextern int	NoNLSRebind;
1211167465Smp#if !HAVE_DECL_ENVIRON
1212167465Smpextern char   **environ;
1213167465Smp#endif
121459243Sobrien
121559243Sobrien#include "tc.h"
121659243Sobrien
121769408Sache#ifndef WINNT_NATIVE
121859243Sobrien# ifdef NLS_CATALOGS
1219231990Smp#  ifdef HAVE_FEATURES_H
122059243Sobrien#   include <features.h>
122159243Sobrien#  endif
1222231990Smp#  ifdef HAVE_NL_LANGINFO
1223231990Smp#   include <langinfo.h>
122459243Sobrien#  endif
1225231990Smp#  ifdef __uxps__
1226231990Smp#   define gettxt gettxt_ds
122759243Sobrien#  endif
1228231990Smp#  include <nl_types.h>
1229231990Smp#  ifdef __uxps__
1230231990Smp#   undef gettxt
1231231990Smp#  endif
123259243SobrienEXTERN nl_catd catd;
1233167465Smp#  if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
1234145479Smp#   define CGETS(b, c, d)	iconv_catgets(catd, b, c, d)
1235145479Smp#  else
1236167465Smp#   define CGETS(b, c, d)	xcatgets(catd, b, c, d)
1237145479Smp#  endif
123859243Sobrien#  define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
123959243Sobrien# else
124059243Sobrien#  define CGETS(b, c, d)	d
124159243Sobrien#  define CSAVS(b, c, d)	d
124259243Sobrien# endif
124369408Sache#else /* WINNT_NATIVE */
124459243Sobrien# define CGETS(b, c, d)	nt_cgets( b, c, d)
124559243Sobrien# define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
124669408Sache#endif /* WINNT_NATIVE */
124759243Sobrien
1248131962Smp#if defined(FILEC)
1249145479Smpextern int    filec;
1250131962Smp#endif /* FILEC */
1251131962Smp
1252145479Smp#include "sh.decls.h"
125359243Sobrien/*
125459243Sobrien * Since on some machines characters are unsigned, and the signed
125559243Sobrien * keyword is not universally implemented, we treat all characters
125659243Sobrien * as unsigned and sign extend them where we need.
125759243Sobrien */
125859243Sobrien#define SIGN_EXTEND_CHAR(a)	(((a) & 0x80) ? ((a) | ~0x7f) : (a))
125959243Sobrien
1260145479Smp/*
1261145479Smp * explanation for use by the "--help" option
1262145479Smp */
1263145479Smp#define HELP_STRING "\
1264145479Smp-b file		batch mode, read and execute commands from `file' \n\
1265145479Smp-c command	run `command' from next argument \n\
1266145479Smp-d		load directory stack from `~/.cshdirs' \n\
1267145479Smp-Dname[=value]	define environment variable `name' to `value' (DomainOS only) \n\
1268145479Smp-e		exit on any error \n\
1269145479Smp-f		start faster by ignoring the start-up file \n\
1270145479Smp-F		use fork() instead of vfork() when spawning (ConvexOS only) \n\
1271145479Smp-i		interactive, even when input is not from a terminal \n\
1272145479Smp-l		act as a login shell, must be the only option specified \n\
1273145479Smp-m		load the start-up file, whether or not owned by effective user \n\
1274145479Smp-n file		no execute mode, just check syntax of the following `file' \n\
1275145479Smp-q		accept SIGQUIT for running under a debugger \n\
1276145479Smp-s		read commands from standard input \n\
1277145479Smp-t		read one line from standard input \n\
1278145479Smp-v		echo commands after history substitution \n\
1279145479Smp-V		like -v but including commands read from the start-up file \n\
1280145479Smp-x		echo commands immediately before execution \n\
1281145479Smp-X		like -x but including commands read from the start-up file \n\
1282145479Smp--help		print this message and exit \n\
1283145479Smp--version	print the version shell variable and exit \n\
1284145479Smp\nSee the tcsh(1) manual page for detailed information.\n"
1285145479Smp
1286145479Smp#include "tc.nls.h"
1287145479Smp
128859243Sobrien#endif /* _h_sh */
1289