1232633Smp/* $Header: /p/tcsh/cvsroot/tcsh/sh.h,v 3.165 2011/04/14 18:25:25 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 */
78232633Smp
79232633Smp#if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
80232633Smp#define AUTOSET_KANJI
81232633Smp#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
93232633Smp#define TMP_TEMPLATE ".XXXXXX"
94232633Smp
9559243Sobrien#ifdef SHORT_STRINGS
96167465Smp# ifdef WIDE_STRINGS
97145479Smp#include <wchar.h>
98232633Smp#  ifdef UTF16_STRINGS
99232633Smptypedef wint_t Char;
100232633Smp#  else
101145479Smptypedef wchar_t Char;
102232633Smp#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)
107232633Smp#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
127232633Smp#if !defined(__inline) && !defined(__GNUC__) && !defined(_MSC_VER)
128232633Smp#define __inline
129232633Smp#endif
13059243Sobrien/* Elide unused argument warnings */
13159243Sobrien#define USE(a)	(void) (a)
132232633Smp#define TCSH_IGNORE(a)	tcsh_ignore((intptr_t)a)
133232633Smpstatic __inline void tcsh_ignore(intptr_t a)
134195609Smp{
135195609Smp    USE(a);
136195609Smp}
13759243Sobrien
13859243Sobrien/*
13959243Sobrien * Return true if the path is absolute
14059243Sobrien */
141131962Smp#if defined(WINNT_NATIVE)
142100616Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
143100616Smp    (Isalpha((p)[0]) && (p)[1] == ':'))
144131962Smp#elif defined(__CYGWIN__)
145131962Smp# define ABSOLUTEP(p)	((p)[0] == '/' || \
146131962Smp    (Isalpha((p)[0]) && (p)[1] == ':' && \
147131962Smp     ((p)[2] == '\0' || (p)[2] == '/')))
148100616Smp#else /* !WINNT_NATIVE && !__CYGWIN__ */
14959243Sobrien# define ABSOLUTEP(p)	(*(p) == '/')
150100616Smp#endif /* WINNT_NATIVE || __CYGWIN__ */
15159243Sobrien
15259243Sobrien/*
15359243Sobrien * Fundamental definitions which may vary from system to system.
15459243Sobrien *
15559243Sobrien *	BUFSIZE		The i/o buffering size; also limits word size
15659243Sobrien *	MAILINTVL	How often to mailcheck; more often is more expensive
15759243Sobrien */
15859243Sobrien#ifdef BUFSIZE
15961524Sobrien# if	   BUFSIZE < 4096
16059243Sobrien#  undef   BUFSIZE
16161524Sobrien#  define  BUFSIZE	4096	/* buffer size should be no less than this */
16259243Sobrien# endif
16359243Sobrien#else
16461524Sobrien# define   BUFSIZE	4096
16559243Sobrien#endif /* BUFSIZE */
16659243Sobrien
16759243Sobrien#define FORKSLEEP	10	/* delay loop on non-interactive fork failure */
16859243Sobrien#define	MAILINTVL	600	/* 10 minutes */
16959243Sobrien
17059243Sobrien#ifndef INBUFSIZE
17159243Sobrien# define INBUFSIZE    2*BUFSIZE /* Num input characters on the command line */
17259243Sobrien#endif /* INBUFSIZE */
17359243Sobrien
17459243Sobrien
17559243Sobrien/*
17659243Sobrien * What our builtin echo looks like
17759243Sobrien */
17859243Sobrien#define NONE_ECHO	0
17959243Sobrien#define BSD_ECHO	1
18059243Sobrien#define SYSV_ECHO	2
18159243Sobrien#define BOTH_ECHO	(BSD_ECHO|SYSV_ECHO)
18259243Sobrien
18359243Sobrien#ifndef ECHO_STYLE
18459243Sobrien# if SYSVREL > 0
18559243Sobrien#  define ECHO_STYLE SYSV_ECHO
18659243Sobrien# else /* SYSVREL == 0 */
18759243Sobrien#  define ECHO_STYLE BSD_ECHO
18859243Sobrien# endif /* SYSVREL */
18959243Sobrien#endif /* ECHO_STYLE */
19059243Sobrien
19159243Sobrien/*
19259243Sobrien * The shell moves std in/out/diag and the old std input away from units
19359243Sobrien * 0, 1, and 2 so that it is easy to set up these standards for invoked
19459243Sobrien * commands.
19559243Sobrien */
196145479Smp#define	FSAFE	5		/* We keep the first 5 descriptors untouched */
19759243Sobrien#define	FSHTTY	15		/* /dev/tty when manip pgrps */
19859243Sobrien#define	FSHIN	16		/* Preferred desc for shell input */
19959243Sobrien#define	FSHOUT	17		/* ... shell output */
20059243Sobrien#define	FSHDIAG	18		/* ... shell diagnostics */
20159243Sobrien#define	FOLDSTD	19		/* ... old std input */
20259243Sobrien
20359243Sobrien#ifdef PROF
20459243Sobrien#define	xexit(n)	done(n)
20559243Sobrien#endif
20659243Sobrien
20759243Sobrien#ifdef cray
20859243Sobrien# define word word_t           /* sys/types.h defines word.. bad move! */
20959243Sobrien#endif
21059243Sobrien
21159243Sobrien#include <sys/types.h>
21259243Sobrien
21359243Sobrien#ifdef cray
21459243Sobrien# undef word
21559243Sobrien#endif
21659243Sobrien
21759243Sobrien/*
21859243Sobrien * Path separator in environment variables
21959243Sobrien */
22059243Sobrien#ifndef PATHSEP
22169408Sache# if defined(__EMX__) || defined(WINNT_NATIVE)
22259243Sobrien#  define PATHSEP ';'
22359243Sobrien# else /* unix */
22459243Sobrien#  define PATHSEP ':'
22569408Sache# endif /* __EMX__ || WINNT_NATIVE */
22659243Sobrien#endif /* !PATHSEP */
22759243Sobrien
22883098Smp#if defined(__HP_CXD_SPP) && !defined(__hpux)
22959243Sobrien# include <sys/cnx_stat.h>
23059243Sobrien# define stat stat64
23159243Sobrien# define fstat fstat64
23259243Sobrien# define lstat lstat64
23383098Smp#endif /* __HP_CXD_SPP && !__hpux */
23459243Sobrien
235232633Smp#ifdef HAVE_LONG_LONG
236232633Smptypedef long long tcsh_number_t;
237232633Smp#else
238232633Smptypedef long tcsh_number_t;
239232633Smp#endif
24059243Sobrien/*
24159243Sobrien * This macro compares the st_dev field of struct stat. On aix on ibmESA
24259243Sobrien * st_dev is a structure, so comparison does not work.
24359243Sobrien */
24459243Sobrien#ifndef DEV_DEV_COMPARE
24559243Sobrien# define DEV_DEV_COMPARE(x,y)   ((x) == (y))
24659243Sobrien#endif /* DEV_DEV_COMPARE */
24759243Sobrien
24859243Sobrien#ifdef _SEQUENT_
24959243Sobrien# include <sys/procstats.h>
25059243Sobrien#endif /* _SEQUENT_ */
25169408Sache#if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
25259243Sobrien# include <sys/times.h>
25369408Sache#endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
25459243Sobrien
25559243Sobrien#ifdef NLS
25659243Sobrien# include <locale.h>
25759243Sobrien#endif /* NLS */
25859243Sobrien
25969408Sache#if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
26059243Sobrien# include <sys/param.h>
26169408Sache#endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
26259243Sobrien#include <sys/stat.h>
26359243Sobrien
26459243Sobrien#if defined(BSDTIMES) || defined(BSDLIMIT)
26559243Sobrien# include <sys/time.h>
26669408Sache# if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
26759243Sobrien#  include "/usr/ucbinclude/sys/resource.h"
26859243Sobrien# else
26959243Sobrien#  ifdef convex
27059243Sobrien#   define sysrusage cvxrusage
27159243Sobrien#   include <sys/sysinfo.h>
27259243Sobrien#  else
27359243Sobrien#   define sysrusage rusage
27459243Sobrien#   include <sys/resource.h>
27559243Sobrien#  endif /* convex */
27659243Sobrien# endif /* SYSVREL>3 */
27759243Sobrien#endif /* BSDTIMES */
27859243Sobrien
27969408Sache#ifndef WINNT_NATIVE
28059243Sobrien# ifndef POSIX
28159243Sobrien#  ifdef TERMIO
28259243Sobrien#   include <termio.h>
28359243Sobrien#  else /* SGTTY */
28459243Sobrien#   include <sgtty.h>
28559243Sobrien#  endif /* TERMIO */
28659243Sobrien# else /* POSIX */
28759243Sobrien#  ifndef _UWIN
28859243Sobrien#   include <termios.h>
28959243Sobrien#  else
29059243Sobrien#   include <termio.h>
29159243Sobrien#  endif /* _UWIN */
292232633Smp#  if SYSVREL > 3 || defined(__linux__)
29359243Sobrien#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
29459243Sobrien#   undef TIOCSLTC
29559243Sobrien#   undef CSWTCH
29659243Sobrien#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
29759243Sobrien#  endif /* SYSVREL > 3 */
29859243Sobrien# endif /* POSIX */
29969408Sache#endif /* WINNT_NATIVE */
30059243Sobrien
30159243Sobrien#ifdef sonyrisc
30259243Sobrien# include <sys/ttold.h>
30359243Sobrien#endif /* sonyrisc */
30459243Sobrien
30569408Sache#if defined(POSIX) && !defined(WINNT_NATIVE)
30659243Sobrien# include <unistd.h>
30759243Sobrien
30859243Sobrien/*
30959243Sobrien * the gcc+protoize version of <stdlib.h>
31059243Sobrien * redefines malloc(), so we define the following
31159243Sobrien * to avoid it.
31259243Sobrien */
313232633Smp# if defined(SYSMALLOC) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX)
31459243Sobrien#  define NO_FIX_MALLOC
31559243Sobrien#  include <stdlib.h>
316145479Smp# else /* glibc */
31759243Sobrien#  define _GNU_STDLIB_H
31859243Sobrien#  define malloc __malloc
31959243Sobrien#  define free __free
32059243Sobrien#  define calloc __calloc
32159243Sobrien#  define realloc __realloc
32259243Sobrien#  include <stdlib.h>
32359243Sobrien#  undef malloc
32459243Sobrien#  undef free
32559243Sobrien#  undef calloc
32659243Sobrien#  undef realloc
327145479Smp# endif /* glibc || sgi */
32869408Sache#endif /* POSIX && !WINNT_NATIVE */
329167465Smp#include <limits.h>
33059243Sobrien
331232633Smp#if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
33259243Sobrien# if !defined(pyr) && !defined(stellar)
33359243Sobrien#  include <time.h>
33459243Sobrien#  ifdef _MINIX
33559243Sobrien#   define HZ CLOCKS_PER_SEC
33659243Sobrien#  endif /* _MINIX */
33759243Sobrien# endif /* !pyr && !stellar */
33859243Sobrien#endif /* SYSVREL > 0 ||  _IBMR2 */
33959243Sobrien
34059243Sobrien/* In the following ifdef the DECOSF1 has been commented so that later
34159243Sobrien * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
34259243Sobrien */
34359243Sobrien#if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
344167465Smp# if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
34559243Sobrien#  include <sys/ioctl.h>
346232633Smp#  if SYSVREL > 3 || defined(__linux__)
347232633Smp#   undef TIOCGLTC	/* we don't need those, since POSIX has them */
348232633Smp#   undef TIOCSLTC
349232633Smp#   undef CSWTCH
350232633Smp#   define CSWTCH _POSIX_VDISABLE	/* So job control works */
351232633Smp#  endif /* SYSVREL > 3 */
35259243Sobrien# endif
35359243Sobrien#endif
35459243Sobrien
35559243Sobrien#if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
35659243Sobrien#undef CSWTCH
35759243Sobrien#define CSWTCH _POSIX_VDISABLE
35859243Sobrien#endif
35959243Sobrien
36069408Sache#if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
36159243Sobrien# include <sys/filio.h>
36259243Sobrien#endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
36359243Sobrien
364167465Smp#if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
36559243Sobrien# include <sys/file.h>
366167465Smp#endif	/* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
36759243Sobrien
36859243Sobrien#if !defined(O_RDONLY) || !defined(O_NDELAY)
36959243Sobrien# include <fcntl.h>
37059243Sobrien#endif
37159243Sobrien
37259243Sobrien#include <errno.h>
37359243Sobrien
37459243Sobrien#include <setjmp.h>
37559243Sobrien
376167465Smp#include <stdarg.h>
37759243Sobrien
378145479Smp#ifdef HAVE_DIRENT_H
37959243Sobrien# include <dirent.h>
38059243Sobrien#else
381145479Smp# ifdef HAVE_NDIR_H
38259243Sobrien#  include <ndir.h>
38359243Sobrien# else
38459243Sobrien#  include <sys/dir.h>
38559243Sobrien# endif
38659243Sobrien# define dirent direct
387145479Smp#endif /* HAVE_DIRENT_H */
388145479Smp#ifndef HAVE_STRUCT_DIRENT_D_INO
389145479Smp# define d_ino d_fileno
390145479Smp#endif
391167465Smp#if defined(hpux) || defined(sgi) || defined(OREO)
39259243Sobrien# include <stdio.h>	/* So the fgetpwent() prototypes work */
393167465Smp#endif /* hpux || sgi || OREO */
39469408Sache#ifndef WINNT_NATIVE
39559243Sobrien#include <pwd.h>
39659243Sobrien#include <grp.h>
39769408Sache#endif /* WINNT_NATIVE */
398145479Smp#ifdef HAVE_SHADOW_H
39959243Sobrien# include <shadow.h>
400145479Smp#endif /* HAVE_SHADOW_H */
401145479Smp#ifdef HAVE_AUTH_H
40259243Sobrien# include <auth.h>
403145479Smp#endif /* HAVE_AUTH_H */
40459243Sobrien#if defined(BSD) && !defined(POSIX)
40559243Sobrien# include <strings.h>
40659243Sobrien# define strchr(a, b) index(a, b)
40759243Sobrien# define strrchr(a, b) rindex(a, b)
40859243Sobrien#else
40959243Sobrien# include <string.h>
41059243Sobrien#endif /* BSD */
41159243Sobrien
41259243Sobrien/*
41359243Sobrien * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
41459243Sobrien * include it yet, so we include it here
41559243Sobrien */
41659243Sobrien#if defined(sgi) && SYSVREL > 3
41759243Sobrien# include <sys/cdefs.h>
41859243Sobrien#endif /* sgi && SYSVREL > 3 */
41959243Sobrien
42059243Sobrien#ifdef REMOTEHOST
42159243Sobrien# ifdef ISC
42259243Sobrien#  undef MAXHOSTNAMELEN	/* Busted headers? */
42359243Sobrien# endif
42459243Sobrien
42559243Sobrien# include <netinet/in.h>
42659243Sobrien# include <arpa/inet.h>
42759243Sobrien# include <sys/socket.h>
428145479Smp# if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
429100616Smp#  if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
430100616Smp#   if !defined(sgi)
431167465Smp#    if !defined(__CYGWIN__)
432167465Smp#     define INET6
433167465Smp#    endif /* __CYGWIN__ */
434100616Smp#   endif /* sgi */
435100616Smp#  endif /* __APPLE__ */
43669408Sache# endif
43759243Sobrien# include <sys/uio.h>	/* For struct iovec */
43859243Sobrien#endif /* REMOTEHOST */
43959243Sobrien
44059243Sobrien#ifdef PURIFY
44159243Sobrien/* exit normally, allowing purify to trace leaks */
44259243Sobrien# define _exit		exit
44359243Sobrientypedef  int		pret_t;
44459243Sobrien#else /* !PURIFY */
44559243Sobrien/*
44659243Sobrien * If your compiler complains, then you can either
44759243Sobrien * throw it away and get gcc or, use the following define
44859243Sobrien * and get rid of the typedef.
44959243Sobrien * [The 4.2/3BSD vax compiler does not like that]
45059243Sobrien * Both MULTIFLOW and PCC compilers exhbit this bug.  -- sterling@netcom.com
45159243Sobrien */
45259243Sobrien# if (defined(vax) || defined(uts) || defined(MULTIFLOW) || defined(PCC)) && !defined(__GNUC__)
45359243Sobrien#  define pret_t void
45459243Sobrien# else /* !((vax || uts || MULTIFLOW || PCC) && !__GNUC__) */
45559243Sobrientypedef void pret_t;
45659243Sobrien# endif /* (vax || uts || MULTIFLOW || PCC) && !__GNUC__ */
45759243Sobrien#endif /* PURIFY */
45859243Sobrien
45969408Sache/*
46069408Sache * ASCII vs. EBCDIC
46169408Sache */
46269408Sache#if 'Z' - 'A' == 25
46369408Sache# ifndef IS_ASCII
46469408Sache#  define IS_ASCII
46569408Sache# endif
46669408Sache#endif
46769408Sache
46859243Sobrien#include "sh.types.h"
46959243Sobrien
470167465Smp#if !HAVE_DECL_GETPGRP
471145479Smp# ifndef GETPGRP_VOID
472167465Smpextern pid_t getpgrp (int);
473145479Smp# else
474167465Smpextern pid_t getpgrp (void);
475145479Smp# endif
476145479Smp#endif
47759243Sobrien
47859243Sobrien#ifndef lint
47959243Sobrientypedef ptr_t memalign_t;
48059243Sobrien#else
48159243Sobrientypedef union {
48259243Sobrien    char    am_char, *am_char_p;
48359243Sobrien    short   am_short, *am_short_p;
48459243Sobrien    int     am_int, *am_int_p;
48559243Sobrien    long    am_long, *am_long_p;
48659243Sobrien    float   am_float, *am_float_p;
48759243Sobrien    double  am_double, *am_double_p;
48859243Sobrien}      *memalign_t;
48959243Sobrien
49059243Sobrien# define malloc		lint_malloc
49159243Sobrien# define free		lint_free
49259243Sobrien# define realloc	lint_realloc
49359243Sobrien# define calloc		lint_calloc
49459243Sobrien#endif
49559243Sobrien
496167465Smp#ifdef SYSMALLOC
497167465Smp# define xmalloc(i)	smalloc(i)
498167465Smp# define xrealloc(p, i)	srealloc(p, i)
499167465Smp# define xcalloc(n, s)	scalloc(n, s)
500167465Smp# define xfree		sfree
50159243Sobrien#else
502167465Smp# define xmalloc(i)  	malloc(i)
503167465Smp# define xrealloc(p, i)	realloc(p, i)
504167465Smp# define xcalloc(n, s)	calloc(n, s)
505167465Smp# define xfree	 	free
506167465Smp#endif /* SYSMALLOC */
50759243Sobrien#include "sh.char.h"
50859243Sobrien#include "sh.err.h"
50959243Sobrien#include "sh.dir.h"
51059243Sobrien#include "sh.proc.h"
51159243Sobrien
51259243Sobrien#include "pathnames.h"
51359243Sobrien
51459243Sobrien
51559243Sobrien/*
51659243Sobrien * C shell
51759243Sobrien *
51859243Sobrien * Bill Joy, UC Berkeley
51959243Sobrien * October, 1978; May 1980
52059243Sobrien *
52159243Sobrien * Jim Kulp, IIASA, Laxenburg Austria
52259243Sobrien * April, 1980
52359243Sobrien */
52459243Sobrien
52559243Sobrien#ifdef HESIOD
52659243Sobrien# include <hesiod.h>
52759243Sobrien#endif /* HESIOD */
52859243Sobrien
52959243Sobrien#ifdef REMOTEHOST
53059243Sobrien# include <netdb.h>
53159243Sobrien#endif /* REMOTEHOST */
53259243Sobrien
53359243Sobrien#ifndef MAXHOSTNAMELEN
534167465Smp# ifdef HOST_NAME_MAX
535167465Smp#  define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
536167465Smp# elif defined(SCO) && (SYSVREL > 3)
53759243Sobrien#  include <sys/socket.h>
53859243Sobrien# else
53983098Smp#  define MAXHOSTNAMELEN 256
54059243Sobrien# endif
54159243Sobrien#endif /* MAXHOSTNAMELEN */
54259243Sobrien
54359243Sobrien
54459243Sobrien
54559243Sobrien#define	eq(a, b)	(Strcmp(a, b) == 0)
54659243Sobrien
54759243Sobrien/* globone() flags */
54859243Sobrien#define G_ERROR		0	/* default action: error if multiple words */
54959243Sobrien#define G_IGNORE	1	/* ignore the rest of the words		   */
55059243Sobrien#define G_APPEND	2	/* make a sentence by cat'ing the words    */
55159243Sobrien
55259243Sobrien/*
55359243Sobrien * Global flags
55459243Sobrien */
555145479SmpEXTERN int    chkstop IZERO;	/* Warned of stopped jobs... allow exit */
55659243Sobrien
55759243Sobrien#if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
55859243Sobrien# define CLOSE_ON_EXEC
55959243Sobrien#else
560145479SmpEXTERN int    didcch IZERO;	/* Have closed unused fd's for child */
56159243Sobrien#endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
56259243Sobrien
563145479SmpEXTERN int    didfds IZERO;	/* Have setup i/o fd's for child */
564145479SmpEXTERN int    doneinp IZERO;	/* EOF indicator after reset from readc */
565145479SmpEXTERN int    exiterr IZERO;	/* Exit if error or non-zero exit status */
566145479SmpEXTERN int    child IZERO;	/* Child shell ... errors cause exit */
567145479SmpEXTERN int    haderr IZERO;	/* Reset was because of an error */
568145479SmpEXTERN int    intty IZERO;	/* Input is a tty */
569145479SmpEXTERN int    intact IZERO;	/* We are interactive... therefore prompt */
570145479SmpEXTERN int    justpr IZERO;	/* Just print because of :p hist mod */
571145479SmpEXTERN int    loginsh IZERO;	/* We are a loginsh -> .login/.logout */
572145479SmpEXTERN int    neednote IZERO;	/* Need to pnotify() */
573145479SmpEXTERN int    noexec IZERO;	/* Don't execute, just syntax check */
574145479SmpEXTERN int    pjobs IZERO;	/* want to print jobs if interrupted */
575145479SmpEXTERN int    setintr IZERO;	/* Set interrupts on/off -> Wait intr... */
576232633SmpEXTERN int    handle_intr IZERO;/* Are we currently handling an interrupt? */
577145479SmpEXTERN int    havhash IZERO;	/* path hashing is available */
578145479SmpEXTERN int    editing IZERO;	/* doing filename expansion and line editing */
579145479SmpEXTERN int    noediting IZERO;	/* initial $term defaulted to noedit */
580145479SmpEXTERN int    bslash_quote IZERO;/* PWP: tcsh-style quoting?  (in sh.c) */
581232633SmpEXTERN int    anyerror IZERO;	/* propagate errors from pipelines/backq */
582195609SmpEXTERN int    compat_expr IZERO;/* csh-style expressions? */
583145479SmpEXTERN int    isoutatty IZERO;	/* is SHOUT a tty */
584145479SmpEXTERN int    isdiagatty IZERO;/* is SHDIAG a tty */
585145479SmpEXTERN int    is1atty IZERO;	/* is file descriptor 1 a tty (didfds mode) */
586145479SmpEXTERN int    is2atty IZERO;	/* is file descriptor 2 a tty (didfds mode) */
587145479SmpEXTERN int    arun IZERO;	/* Currently running multi-line-aliases */
58859243SobrienEXTERN int     implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
589145479SmpEXTERN int    inheredoc IZERO;	/* Currently parsing a heredoc */
590167465Smp/* We received a window change event */
591167465SmpEXTERN volatile sig_atomic_t windowchg IZERO;
592145479Smp#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
593145479SmpEXTERN int    dspmbyte_ls;
594145479Smp#endif
59559243Sobrien
59659243Sobrien/*
59759243Sobrien * Global i/o info
59859243Sobrien */
59959243SobrienEXTERN Char   *arginp IZERO;	/* Argument input for sh -c and internal `xx` */
60059243SobrienEXTERN int     onelflg IZERO;	/* 2 -> need line for -t, 1 -> exit on read */
60159243Sobrienextern Char   *ffile;		/* Name of shell file for $0 */
602145479Smpextern int    dolzero;		/* if $?0 should return true... */
60359243Sobrien
60459243Sobrienextern char *seterr;		/* Error message from scanner/parser */
605145479Smp#ifndef errno
60659243Sobrienextern int errno;		/* Error from C library routines */
60769408Sache#endif
60869408Sacheextern int exitset;
609167465Smp/* Temp name for << shell files in /tmp, for xfree() */
610167465SmpEXTERN Char   *shtemp IZERO;
61159243Sobrien
61259243Sobrien#ifdef BSDTIMES
61359243SobrienEXTERN struct timeval time0;	/* Time at which the shell started */
61459243SobrienEXTERN struct sysrusage ru0;
61559243Sobrien#else
61659243Sobrien# ifdef _SEQUENT_
61759243SobrienEXTERN timeval_t time0;		/* time at which shell started */
61859243SobrienEXTERN struct process_stats ru0;
61959243Sobrien# else /* _SEQUENT_ */
62059243Sobrien#  ifndef POSIX
62159243SobrienEXTERN time_t  time0;		/* time at which shell started */
62259243Sobrien#  else	/* POSIX */
62359243SobrienEXTERN clock_t time0;		/* time at which shell started */
62459243SobrienEXTERN clock_t clk_tck;
62559243Sobrien#  endif /* POSIX */
62659243SobrienEXTERN struct tms shtimes;	/* shell and child times for process timing */
62759243Sobrien# endif /* _SEQUENT_ */
628167465SmpEXTERN time_t seconds0;
62959243Sobrien#endif /* BSDTIMES */
63059243Sobrien
63159243Sobrien#ifndef HZ
63259243Sobrien# define HZ	100		/* for division into seconds */
63359243Sobrien#endif
63459243Sobrien
63559243Sobrien/*
63659243Sobrien * Miscellany
63759243Sobrien */
63859243SobrienEXTERN Char   *doldol;		/* Character pid for $$ */
639167465SmpEXTERN pid_t   backpid;		/* pid of the last background job */
64059243Sobrien
64159243Sobrien/*
64259243Sobrien * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
64359243Sobrien * cause pid's could be unsigned and that would break our -1 flag, and
64459243Sobrien * uid_t and gid_t are not defined in all the systems so I would have to
64559243Sobrien * make special cases for them. In the future...
64659243Sobrien */
647145479SmpEXTERN uid_t   uid, euid; 	/* Invokers real and effective */
648145479SmpEXTERN gid_t   gid, egid;	/* User and group ids */
649145479SmpEXTERN pid_t   opgrp,		/* Initial pgrp and tty pgrp */
65059243Sobrien               shpgrp,		/* Pgrp of shell */
65159243Sobrien               tpgrp;		/* Terminal process group */
65259243Sobrien				/* If tpgrp is -1, leave tty alone! */
65359243Sobrien
654167465SmpEXTERN Char   *Prompt;		/* The actual printed prompt or NULL */
655167465SmpEXTERN Char   *RPrompt;		/* Right-hand side prompt or NULL */
65659243Sobrien
65759243Sobrien/*
65859243Sobrien * To be able to redirect i/o for builtins easily, the shell moves the i/o
65959243Sobrien * descriptors it uses away from 0,1,2.
66059243Sobrien * Ideally these should be in units which are closed across exec's
66159243Sobrien * (this saves work) but for version 6, this is not usually possible.
66259243Sobrien * The desired initial values for these descriptors are defined in
66359243Sobrien * sh.local.h.
66459243Sobrien */
66559243SobrienEXTERN int   SHIN IZERO;	/* Current shell input (script) */
66659243SobrienEXTERN int   SHOUT IZERO;	/* Shell output */
66759243SobrienEXTERN int   SHDIAG IZERO;	/* Diagnostic output... shell errs go here */
66859243SobrienEXTERN int   OLDSTD IZERO;	/* Old standard input (def for cmds) */
66959243Sobrien
67059243Sobrien
671232633Smp#if (SYSVREL == 4 && defined(_UTS)) || defined(__linux__)
67259243Sobrien/*
67359243Sobrien * From: fadden@uts.amdahl.com (Andy McFadden)
67459243Sobrien * we need sigsetjmp for UTS4, but not UTS2.1
67559243Sobrien */
67659243Sobrien# define SIGSETJMP
67759243Sobrien#endif
67859243Sobrien
67959243Sobrien/*
68059243Sobrien * Error control
68159243Sobrien *
68259243Sobrien * Errors in scanning and parsing set up an error message to be printed
68359243Sobrien * at the end and complete.  Other errors always cause a reset.
68459243Sobrien * Because of source commands and .cshrc we need nested error catches.
68559243Sobrien */
68659243Sobrien
687167465Smp#ifdef SIGSETJMP
688167465Smp   typedef struct { sigjmp_buf j; } jmp_buf_t;
689232633Smp# define setexit()  sigsetjmp(reslab.j, 1)
690167465Smp# define _reset()    siglongjmp(reslab.j, 1)
69159243Sobrien#else
69259243Sobrien   typedef struct { jmp_buf j; } jmp_buf_t;
693167465Smp# define setexit()  setjmp(reslab.j)
694167465Smp# define _reset()    longjmp(reslab.j, 1)
695167465Smp#endif
69659243Sobrien
697167465Smp#define getexit(a) (void) ((a) = reslab)
698167465Smp#define resexit(a) (void) (reslab = (a))
69959243Sobrien
700167465Smp#define cpybin(a, b) (void) ((a) = (b))
70159243Sobrien
70259243Sobrienextern jmp_buf_t reslab;
70359243Sobrien
70459243SobrienEXTERN Char   *gointr;		/* Label for an onintr transfer */
70559243Sobrien
706167465Smpextern struct sigaction parintr;	/* Parents interrupt catch */
707167465Smpextern struct sigaction parterm;	/* Parents terminate catch */
70859243Sobrien
70959243Sobrien/*
71059243Sobrien * Lexical definitions.
71159243Sobrien *
71259243Sobrien * All lexical space is allocated dynamically.
71359243Sobrien * The eighth/sixteenth bit of characters is used to prevent recognition,
71459243Sobrien * and eventually stripped.
71559243Sobrien */
71659243Sobrien#define		META		0200
71759243Sobrien#define		ASCII		0177
718145479Smp#ifdef WIDE_STRINGS		/* Implies SHORT_STRINGS */
719145479Smp/* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
720145479Smp# define	QUOTE		0x40000000
721145479Smp# define	TRIM		0x3FFFFFFF /* Mask to strip quote bit */
722195609Smp# define	UNDER		0x08000000 /* Underline flag */
723195609Smp# define	BOLD		0x04000000 /* Bold flag */
724195609Smp# define	STANDOUT	0x02000000 /* Standout flag */
725195609Smp# define	LITERAL		0x01000000 /* Literal character flag */
726195609Smp# define	ATTRIBUTES	0x0F000000 /* The bits used for attributes */
727195609Smp# define	INVALID_BYTE	0x00800000 /* Invalid character on input */
728195609Smp# ifdef SOLARIS2
729195609Smp#  define	CHAR		0x30FFFFFF /* Mask to mask out the character */
730195609Smp# else
731195609Smp#  define	CHAR		0x00FFFFFF /* Mask to mask out the character */
732195609Smp# endif
733145479Smp#elif defined (SHORT_STRINGS)
73459243Sobrien# define	QUOTE 	((Char)	0100000)/* 16nth char bit used for 'ing */
735145479Smp# define	TRIM		0073777	/* Mask to strip quote/lit bit */
73659243Sobrien# define	UNDER		0040000	/* Underline flag */
73759243Sobrien# define	BOLD		0020000	/* Bold flag */
73859243Sobrien# define	STANDOUT	0010000	/* Standout flag */
73959243Sobrien# define	LITERAL		0004000	/* Literal character flag */
74059243Sobrien# define	ATTRIBUTES	0074000	/* The bits used for attributes */
741145479Smp# define	INVALID_BYTE	0
74259243Sobrien# define	CHAR		0000377	/* Mask to mask out the character */
74359243Sobrien#else
74459243Sobrien# define	QUOTE 	((Char)	0200)	/* Eighth char bit used for 'ing */
74559243Sobrien# define	TRIM		0177	/* Mask to strip quote bit */
74659243Sobrien# define	UNDER		0000000	/* No extra bits to do both */
74759243Sobrien# define	BOLD		0000000	/* Bold flag */
74859243Sobrien# define	STANDOUT	META	/* Standout flag */
74959243Sobrien# define	LITERAL		0000000	/* Literal character flag */
75059243Sobrien# define	ATTRIBUTES	0200	/* The bits used for attributes */
751145479Smp# define	INVALID_BYTE	0
75259243Sobrien# define	CHAR		0000177	/* Mask to mask out the character */
753167465Smp#endif
754145479Smp#define		CHAR_DBWIDTH	(LITERAL|(LITERAL-1))
75559243Sobrien
75659243SobrienEXTERN int     AsciiOnly;	/* If set only 7 bits expected in characters */
75759243Sobrien
75859243Sobrien/*
75959243Sobrien * Each level of input has a buffered input structure.
76059243Sobrien * There are one or more blocks of buffered input for each level,
76159243Sobrien * exactly one if the input is seekable and tell is available.
76259243Sobrien * In other cases, the shell buffers enough blocks to keep all loops
76359243Sobrien * in the buffer.
764167465Smp *
765167465Smp * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
766167465Smp * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
767167465Smp * fbuf indexing).
768167465Smp *
769167465Smp * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
770167465Smp * is no difference between byte and character offsets.
77159243Sobrien */
77259243SobrienEXTERN struct Bin {
773145479Smp    off_t   Bfseekp;		/* Seek pointer, generally != lseek() value */
77459243Sobrien    off_t   Bfbobp;		/* Seekp of beginning of buffers */
77559243Sobrien    off_t   Bfeobp;		/* Seekp of end of buffers */
77659243Sobrien    int     Bfblocks;		/* Number of buffer blocks */
77759243Sobrien    Char  **Bfbuf;		/* The array of buffer blocks */
778145479Smp#ifdef WIDE_STRINGS
779145479Smp    /* Number of bytes in each character if (cantell) */
780145479Smp    unsigned char Bfclens[BUFSIZE + 1];
781145479Smp#endif
78259243Sobrien}       B;
78359243Sobrien
78459243Sobrien/*
78559243Sobrien * This structure allows us to seek inside aliases
78659243Sobrien */
78759243Sobrienstruct Ain {
78859243Sobrien    int type;
78969408Sache#define TCSH_I_SEEK 	 0		/* Invalid seek */
79069408Sache#define TCSH_A_SEEK	 1		/* Alias seek */
79169408Sache#define TCSH_F_SEEK	 2		/* File seek */
79269408Sache#define TCSH_E_SEEK	 3		/* Eval seek */
79359243Sobrien    union {
794167465Smp	off_t _f_seek;		/* A byte offset if (cantell) */
79559243Sobrien	Char* _c_seek;
79659243Sobrien    } fc;
79759243Sobrien#define f_seek fc._f_seek
79859243Sobrien#define c_seek fc._c_seek
79959243Sobrien    Char **a_seek;
80059243Sobrien} ;
80159243Sobrien
80259243Sobrienextern int aret;		/* Type of last char returned */
80359243Sobrien#define SEEKEQ(a, b) ((a)->type == (b)->type && \
80459243Sobrien		      (a)->f_seek == (b)->f_seek && \
80559243Sobrien		      (a)->a_seek == (b)->a_seek)
80659243Sobrien
80759243Sobrien#define	fseekp	B.Bfseekp
80859243Sobrien#define	fbobp	B.Bfbobp
80959243Sobrien#define	feobp	B.Bfeobp
81059243Sobrien#define	fblocks	B.Bfblocks
81159243Sobrien#define	fbuf	B.Bfbuf
812145479Smp#define fclens  B.Bfclens
81359243Sobrien
81459243Sobrien/*
81559243Sobrien * The shell finds commands in loops by reseeking the input
81659243Sobrien * For whiles, in particular, it reseeks to the beginning of the
81759243Sobrien * line the while was on; hence the while placement restrictions.
81859243Sobrien */
81959243SobrienEXTERN struct Ain lineloc;
82059243Sobrien
821145479SmpEXTERN int    cantell;		/* Is current source tellable ? */
82259243Sobrien
82359243Sobrien/*
82459243Sobrien * Input lines are parsed into doubly linked circular
82559243Sobrien * lists of words of the following form.
82659243Sobrien */
82759243Sobrienstruct wordent {
82859243Sobrien    Char   *word;
82959243Sobrien    struct wordent *prev;
83059243Sobrien    struct wordent *next;
83159243Sobrien};
83259243Sobrien
83359243Sobrien/*
83459243Sobrien * During word building, both in the initial lexical phase and
83559243Sobrien * when expanding $ variable substitutions, expansion by `!' and `$'
83659243Sobrien * must be inhibited when reading ahead in routines which are themselves
83759243Sobrien * processing `!' and `$' expansion or after characters such as `\' or in
83859243Sobrien * quotations.  The following flags are passed to the getC routines
83959243Sobrien * telling them which of these substitutions are appropriate for the
84059243Sobrien * next character to be returned.
84159243Sobrien */
84259243Sobrien#define	DODOL	1
84359243Sobrien#define	DOEXCL	2
84459243Sobrien#define	DOALL	DODOL|DOEXCL
84559243Sobrien
84659243Sobrien/*
84759243Sobrien * Labuf implements a general buffer for lookahead during lexical operations.
84859243Sobrien * Text which is to be placed in the input stream can be stuck here.
84959243Sobrien * We stick parsed ahead $ constructs during initial input,
85059243Sobrien * process id's from `$$', and modified variable values (from qualifiers
85159243Sobrien * during expansion in sh.dol.c) here.
85259243Sobrien */
853167465Smpextern struct Strbuf labuf;
854167465SmpEXTERN size_t lap; /* N/A if == labuf.len, index into labuf.s otherwise */
85559243Sobrien
85659243Sobrien/*
85759243Sobrien * Parser structure
85859243Sobrien *
85959243Sobrien * Each command is parsed to a tree of command structures and
86059243Sobrien * flags are set bottom up during this process, to be propagated down
86159243Sobrien * as needed during the semantics/exeuction pass (sh.sem.c).
86259243Sobrien */
86359243Sobrienstruct command {
86459243Sobrien    unsigned char   t_dtyp;	/* Type of node 		 */
86559243Sobrien#define	NODE_COMMAND	1	/* t_dcom <t_dlef >t_drit	 */
86659243Sobrien#define	NODE_PAREN	2	/* ( t_dspr ) <t_dlef >t_drit	 */
86759243Sobrien#define	NODE_PIPE	3	/* t_dlef | t_drit		 */
86859243Sobrien#define	NODE_LIST	4	/* t_dlef ; t_drit		 */
86959243Sobrien#define	NODE_OR		5	/* t_dlef || t_drit		 */
87059243Sobrien#define	NODE_AND	6	/* t_dlef && t_drit		 */
87159243Sobrien    unsigned char   t_nice;	/* Nice value			 */
87259243Sobrien#ifdef apollo
87359243Sobrien    unsigned char   t_systype;	/* System environment		 */
87459243Sobrien#endif
87559243Sobrien    unsigned long   t_dflg;	/* Flags, e.g. F_AMPERSAND|... 	 */
87659243Sobrien/* save these when re-doing 	 */
87759243Sobrien#ifndef apollo
87859243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP|F_HUP)
87959243Sobrien#else
88059243Sobrien#define	F_SAVE	(F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
88159243Sobrien#endif
88259243Sobrien#define	F_AMPERSAND	(1<<0)	/* executes in background	 */
88359243Sobrien#define	F_APPEND	(1<<1)	/* output is redirected >>	 */
88459243Sobrien#define	F_PIPEIN	(1<<2)	/* input is a pipe		 */
88559243Sobrien#define	F_PIPEOUT	(1<<3)	/* output is a pipe		 */
88659243Sobrien#define	F_NOFORK	(1<<4)	/* don't fork, last ()ized cmd	 */
88759243Sobrien#define	F_NOINTERRUPT	(1<<5)	/* should be immune from intr's */
88859243Sobrien/* spare */
88959243Sobrien#define	F_STDERR	(1<<7)	/* redirect unit 2 with unit 1	 */
89059243Sobrien#define	F_OVERWRITE	(1<<8)	/* output was !			 */
89159243Sobrien#define	F_READ		(1<<9)	/* input redirection is <<	 */
89259243Sobrien#define	F_REPEAT	(1<<10)	/* reexec aft if, repeat,...	 */
89359243Sobrien#define	F_NICE		(1<<11)	/* t_nice is meaningful 	 */
89459243Sobrien#define	F_NOHUP		(1<<12)	/* nohup this command 		 */
89559243Sobrien#define	F_TIME		(1<<13)	/* time this command 		 */
89659243Sobrien#define F_BACKQ		(1<<14)	/* command is in ``		 */
89759243Sobrien#define F_HUP		(1<<15)	/* hup this command		 */
89859243Sobrien#ifdef apollo
89959243Sobrien#define F_VER		(1<<16)	/* execute command under SYSTYPE */
90059243Sobrien#endif
90159243Sobrien    union {
90259243Sobrien	Char   *T_dlef;		/* Input redirect word 		 */
90359243Sobrien	struct command *T_dcar;	/* Left part of list/pipe 	 */
90459243Sobrien    }       L;
90559243Sobrien    union {
90659243Sobrien	Char   *T_drit;		/* Output redirect word 	 */
90759243Sobrien	struct command *T_dcdr;	/* Right part of list/pipe 	 */
90859243Sobrien    }       R;
90959243Sobrien#define	t_dlef	L.T_dlef
91059243Sobrien#define	t_dcar	L.T_dcar
91159243Sobrien#define	t_drit	R.T_drit
91259243Sobrien#define	t_dcdr	R.T_dcdr
91359243Sobrien    Char  **t_dcom;		/* Command/argument vector 	 */
91459243Sobrien    struct command *t_dspr;	/* Pointer to ()'d subtree 	 */
91559243Sobrien};
91659243Sobrien
91759243Sobrien
91859243Sobrien/*
91959243Sobrien * The keywords for the parser
92059243Sobrien */
92159243Sobrien#define	TC_BREAK	0
92259243Sobrien#define	TC_BRKSW	1
92359243Sobrien#define	TC_CASE		2
92459243Sobrien#define	TC_DEFAULT 	3
92559243Sobrien#define	TC_ELSE		4
92659243Sobrien#define	TC_END		5
92759243Sobrien#define	TC_ENDIF	6
92859243Sobrien#define	TC_ENDSW	7
92959243Sobrien#define	TC_EXIT		8
93059243Sobrien#define	TC_FOREACH	9
93159243Sobrien#define	TC_GOTO		10
93259243Sobrien#define	TC_IF		11
93359243Sobrien#define	TC_LABEL	12
93459243Sobrien#define	TC_LET		13
93559243Sobrien#define	TC_SET		14
93659243Sobrien#define	TC_SWITCH	15
93759243Sobrien#define	TC_TEST		16
93859243Sobrien#define	TC_THEN		17
93959243Sobrien#define	TC_WHILE	18
94059243Sobrien
94159243Sobrien/*
94259243Sobrien * These are declared here because they want to be
94359243Sobrien * initialized in sh.init.c (to allow them to be made readonly)
94459243Sobrien */
94559243Sobrien
94659243Sobrien#if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
94759243Sobrien    /* Avoid hpux ansi mode spurious warnings */
94859243Sobrientypedef void (*bfunc_t) ();
94959243Sobrien#else
950167465Smptypedef void (*bfunc_t) (Char **, struct command *);
95159243Sobrien#endif /* hpux && __STDC__ && !__GNUC__ */
95259243Sobrien
953167465Smpextern const struct biltins {
954145479Smp    const char   *bname;
95559243Sobrien    bfunc_t bfunct;
95659243Sobrien    int     minargs, maxargs;
95759243Sobrien} bfunc[];
95859243Sobrienextern int nbfunc;
95969408Sache#ifdef WINNT_NATIVE
96059243Sobrienextern struct biltins  nt_bfunc[];
96159243Sobrienextern int nt_nbfunc;
96269408Sache#endif /* WINNT_NATIVE*/
963145479Smpextern int bequiet;
96459243Sobrien
96559243Sobrienextern struct srch {
966145479Smp    const char *s_name;
967145479Smp    int  s_value;
968145479Smp} srchn[];
96959243Sobrienextern int nsrchn;
97059243Sobrien
97159243Sobrien/*
97259243Sobrien * Structure defining the existing while/foreach loops at this
97359243Sobrien * source level.  Loops are implemented by seeking back in the
97459243Sobrien * input.  For foreach (fe), the word list is attached here.
97559243Sobrien */
97659243SobrienEXTERN struct whyle {
97759243Sobrien    struct Ain   w_start;	/* Point to restart loop */
97859243Sobrien    struct Ain   w_end;		/* End of loop (0 if unknown) */
97959243Sobrien    Char  **w_fe, **w_fe0;	/* Current/initial wordlist for fe */
98059243Sobrien    Char   *w_fename;		/* Name for fe */
98159243Sobrien    struct whyle *w_next;	/* Next (more outer) loop */
98259243Sobrien}      *whyles;
98359243Sobrien
98459243Sobrien/*
98559243Sobrien * Variable structure
98659243Sobrien *
98759243Sobrien * Aliases and variables are stored in AVL balanced binary trees.
98859243Sobrien */
98959243SobrienEXTERN struct varent {
99059243Sobrien    Char  **vec;		/* Array of words which is the value */
99159243Sobrien    Char   *v_name;		/* Name of variable/alias */
99259243Sobrien    int	    v_flags;		/* Flags */
99359243Sobrien#define VAR_ALL		-1
99459243Sobrien#define VAR_READONLY	1
99559243Sobrien#define VAR_READWRITE	2
99659243Sobrien#define VAR_NOGLOB	4
99759243Sobrien#define VAR_FIRST       32
99859243Sobrien#define VAR_LAST        64
99959243Sobrien    struct varent *v_link[3];	/* The links, see below */
100059243Sobrien    int     v_bal;		/* Balance factor */
100159243Sobrien}       shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
100259243Sobrien
100359243Sobrien#define v_left		v_link[0]
100459243Sobrien#define v_right		v_link[1]
100559243Sobrien#define v_parent	v_link[2]
100659243Sobrien
100759243Sobrien#define adrof(v)	adrof1(v, &shvhed)
100859243Sobrien#define varval(v)	value1(v, &shvhed)
100959243Sobrien
101059243Sobrien/*
101159243Sobrien * The following are for interfacing redo substitution in
101259243Sobrien * aliases to the lexical routines.
101359243Sobrien */
101459243SobrienEXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
101559243SobrienEXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
101659243SobrienEXTERN Char  **alvec IZERO_STRUCT,
101759243Sobrien	      *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
101859243Sobrien
101959243Sobrien/*
102059243Sobrien * Filename/command name expansion variables
102159243Sobrien */
102259243Sobrien
1023131962Smp#ifdef __CYGWIN__
1024131962Smp# undef MAXPATHLEN
1025131962Smp#endif /* __CYGWIN__ */
1026131962Smp
102759243Sobrien#ifndef MAXPATHLEN
1028167465Smp# ifdef PATH_MAX
1029167465Smp#  define MAXPATHLEN PATH_MAX
1030167465Smp# else
1031167465Smp#  define MAXPATHLEN 2048
1032167465Smp# endif
103359243Sobrien#endif /* MAXPATHLEN */
103459243Sobrien
103559243Sobrien#ifndef HAVENOLIMIT
103659243Sobrien/*
103759243Sobrien * resource limits
103859243Sobrien */
103959243Sobrienextern struct limits {
1040145479Smp    int         limconst;
1041145479Smp    const char *limname;
1042145479Smp    int         limdiv;
1043145479Smp    const char *limscale;
104459243Sobrien} limits[];
104559243Sobrien#endif /* !HAVENOLIMIT */
104659243Sobrien
104759243Sobrien/*
104859243Sobrien * History list
104959243Sobrien *
105059243Sobrien * Each history list entry contains an embedded wordlist
105159243Sobrien * from the scanner, a number for the event, and a reference count
105259243Sobrien * to aid in discarding old entries.
105359243Sobrien *
105459243Sobrien * Essentially "invisible" entries are put on the history list
105559243Sobrien * when history substitution includes modifiers, and thrown away
105659243Sobrien * at the next discarding since their event numbers are very negative.
105759243Sobrien */
105859243SobrienEXTERN struct Hist {
105959243Sobrien    struct wordent Hlex;
1060232633Smp    int     Hnum;		 /* eventno when inserted into history list  */
106159243Sobrien    int     Href;
106259243Sobrien    time_t  Htime;
106359243Sobrien    Char   *histline;
1064232633Smp    struct Hist *Hnext, *Hprev;         /* doubly linked list */
1065232633Smp    unsigned Hhash;                     /* hash value of command line */
106659243Sobrien}       Histlist IZERO_STRUCT;
106759243Sobrien
106859243SobrienEXTERN struct wordent paraml;	/* Current lexical word list */
106959243SobrienEXTERN int     eventno;		/* Next events number */
107059243SobrienEXTERN int     lastev;		/* Last event reference (default) */
107159243Sobrien
107259243SobrienEXTERN Char    HIST;		/* history invocation character */
107359243SobrienEXTERN Char    HISTSUB;		/* auto-substitute character */
107459243SobrienEXTERN Char    PRCH;		/* Prompt symbol for regular users */
107559243SobrienEXTERN Char    PRCHROOT;	/* Prompt symbol for root */
107659243Sobrien
107759243Sobrien/*
107859243Sobrien * For operating systems with single case filenames (OS/2)
107959243Sobrien */
108059243Sobrien#ifdef CASE_INSENSITIVE
1081145479Smp# ifdef WIDE_STRINGS
1082145479Smp#  define samecase(x) (towlower(x))
1083145479Smp# else
1084145479Smp#  define samecase(x) (isupper((unsigned char)(x)) ? \
1085145479Smp		       tolower((unsigned char)(x)) : (x))
1086145479Smp# endif
108759243Sobrien#else
108859243Sobrien# define samecase(x) (x)
108959243Sobrien#endif /* CASE_INSENSITIVE */
109059243Sobrien
109159243Sobrien/*
109259243Sobrien * strings.h:
109359243Sobrien */
109459243Sobrien#ifndef SHORT_STRINGS
109559243Sobrien#define Strchr(a, b)  		strchr(a, b)
109659243Sobrien#define Strrchr(a, b)  		strrchr(a, b)
109759243Sobrien#define Strcat(a, b)  		strcat(a, b)
109859243Sobrien#define Strncat(a, b, c) 	strncat(a, b, c)
109959243Sobrien#define Strcpy(a, b)  		strcpy(a, b)
110059243Sobrien#define Strncpy(a, b, c) 	strncpy(a, b, c)
110159243Sobrien#define Strlen(a)		strlen(a)
110259243Sobrien#define Strcmp(a, b)		strcmp(a, b)
110359243Sobrien#define Strncmp(a, b, c)	strncmp(a, b, c)
1104131962Smp#define Strcasecmp(a, b)	strcasecmp(a, b)
110559243Sobrien
110659243Sobrien#define Strspl(a, b)		strspl(a, b)
1107167465Smp#define Strnsave(a, b)		strnsave(a, b)
110859243Sobrien#define Strsave(a)		strsave(a)
110959243Sobrien#define Strend(a)		strend(a)
111059243Sobrien#define Strstr(a, b)		strstr(a, b)
111159243Sobrien
111259243Sobrien#define str2short(a) 		(a)
111359243Sobrien#define blk2short(a) 		saveblk(a)
111459243Sobrien#define short2blk(a) 		saveblk(a)
1115167465Smp#define short2str(a) 		caching_strip(a)
111659243Sobrien#else
1117232633Smp#if defined(WIDE_STRINGS) && !defined(UTF16_STRINGS)
1118145479Smp#define Strchr(a, b)		wcschr(a, b)
1119145479Smp#define Strrchr(a, b)		wcsrchr(a, b)
1120145479Smp#define Strcat(a, b)  		wcscat(a, b)
1121145479Smp#define Strncat(a, b, c) 	wcsncat(a, b, c)
1122145479Smp#define Strcpy(a, b)  		wcscpy(a, b)
1123145479Smp#define Strncpy(a, b, c)	wcsncpy(a, b, c)
1124145479Smp#define Strlen(a)		wcslen(a)
1125145479Smp#define Strcmp(a, b)		wcscmp(a, b)
1126145479Smp#define Strncmp(a, b, c)	wcsncmp(a, b, c)
1127145479Smp#else
112859243Sobrien#define Strchr(a, b)		s_strchr(a, b)
112959243Sobrien#define Strrchr(a, b) 		s_strrchr(a, b)
113059243Sobrien#define Strcat(a, b)  		s_strcat(a, b)
113159243Sobrien#define Strncat(a, b, c) 	s_strncat(a, b, c)
113259243Sobrien#define Strcpy(a, b)  		s_strcpy(a, b)
113359243Sobrien#define Strncpy(a, b, c)	s_strncpy(a, b, c)
113459243Sobrien#define Strlen(a)		s_strlen(a)
113559243Sobrien#define Strcmp(a, b)		s_strcmp(a, b)
113659243Sobrien#define Strncmp(a, b, c)	s_strncmp(a, b, c)
1137145479Smp#endif
1138131962Smp#define Strcasecmp(a, b)	s_strcasecmp(a, b)
113959243Sobrien
114059243Sobrien#define Strspl(a, b)		s_strspl(a, b)
1141167465Smp#define Strnsave(a, b)		s_strnsave(a, b)
114259243Sobrien#define Strsave(a)		s_strsave(a)
114359243Sobrien#define Strend(a)		s_strend(a)
114459243Sobrien#define Strstr(a, b)		s_strstr(a, b)
114559243Sobrien#endif
114659243Sobrien
114759243Sobrien/*
114859243Sobrien * setname is a macro to save space (see sh.err.c)
114959243Sobrien */
1150145479SmpEXTERN const char   *bname;
115159243Sobrien
115259243Sobrien#define	setname(a)	(bname = (a))
115359243Sobrien
115459243Sobrien#ifdef VFORK
115559243SobrienEXTERN Char   *Vsav;
115659243SobrienEXTERN Char   *Vdp;
115759243SobrienEXTERN Char   *Vexpath;
115859243SobrienEXTERN char  **Vt;
115959243Sobrien#endif /* VFORK */
116059243Sobrien
116159243SobrienEXTERN Char  **evalvec;
116259243SobrienEXTERN Char   *evalp;
116359243Sobrien
116459243Sobrienextern struct mesg {
1165145479Smp    const char   *iname;	/* name from /usr/include */
1166145479Smp    const char *pname;		/* print name */
1167145479Smp} mesg[];
116859243Sobrien
116959243Sobrien/* word_chars is set by default to WORD_CHARS but can be overridden by
117059243Sobrien   the worchars variable--if unset, reverts to WORD_CHARS */
117159243Sobrien
117259243SobrienEXTERN Char   *word_chars;
117359243Sobrien
117459243Sobrien#define WORD_CHARS "*?_-.[]~="	/* default chars besides alnums in words */
117559243Sobrien
117659243SobrienEXTERN Char   *STR_SHELLPATH;
117759243Sobrien
117859243Sobrien#ifdef _PATH_BSHELL
117959243SobrienEXTERN Char   *STR_BSHELL;
118059243Sobrien#endif
118159243SobrienEXTERN Char   *STR_WORD_CHARS;
118259243SobrienEXTERN Char  **STR_environ IZERO;
118359243Sobrien
118459243Sobrienextern int     dont_free;	/* Tell free that we are in danger if we free */
118559243Sobrien
118659243Sobrienextern Char    *INVPTR;
118759243Sobrienextern Char    **INVPPTR;
118859243Sobrien
118959243Sobrienextern char    *progname;
119059243Sobrienextern int	tcsh;
1191145479Smpextern int	xlate_cr;
1192145479Smpextern int	output_raw;
1193145479Smpextern int	lbuffed;
1194145479Smpextern time_t	Htime;
1195145479Smpextern int	numeof;
1196145479Smpextern int 	insource;
1197145479Smpextern char	linbuf[];
1198145479Smpextern char 	*linp;
1199145479Smpextern int	nsig;
1200145479Smp#ifdef VFORK
1201145479Smpextern int	use_fork;
1202145479Smp#endif
1203145479Smpextern int	tellwhat;
1204145479Smpextern int	NoNLSRebind;
1205167465Smp#if !HAVE_DECL_ENVIRON
1206167465Smpextern char   **environ;
1207167465Smp#endif
120859243Sobrien
120959243Sobrien#include "tc.h"
121059243Sobrien
121169408Sache#ifndef WINNT_NATIVE
121259243Sobrien# ifdef NLS_CATALOGS
1213232633Smp#  ifdef HAVE_FEATURES_H
121459243Sobrien#   include <features.h>
121559243Sobrien#  endif
1216232633Smp#  ifdef HAVE_NL_LANGINFO
1217232633Smp#   include <langinfo.h>
121859243Sobrien#  endif
1219232633Smp#  ifdef __uxps__
1220232633Smp#   define gettxt gettxt_ds
122159243Sobrien#  endif
1222232633Smp#  include <nl_types.h>
1223232633Smp#  ifdef __uxps__
1224232633Smp#   undef gettxt
1225232633Smp#  endif
122659243SobrienEXTERN nl_catd catd;
1227167465Smp#  if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
1228145479Smp#   define CGETS(b, c, d)	iconv_catgets(catd, b, c, d)
1229145479Smp#  else
1230167465Smp#   define CGETS(b, c, d)	xcatgets(catd, b, c, d)
1231145479Smp#  endif
123259243Sobrien#  define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
123359243Sobrien# else
123459243Sobrien#  define CGETS(b, c, d)	d
123559243Sobrien#  define CSAVS(b, c, d)	d
123659243Sobrien# endif
123769408Sache#else /* WINNT_NATIVE */
123859243Sobrien# define CGETS(b, c, d)	nt_cgets( b, c, d)
123959243Sobrien# define CSAVS(b, c, d)	strsave(CGETS(b, c, d))
124069408Sache#endif /* WINNT_NATIVE */
124159243Sobrien
1242131962Smp#if defined(FILEC)
1243145479Smpextern int    filec;
1244131962Smp#endif /* FILEC */
1245131962Smp
1246145479Smp#include "sh.decls.h"
124759243Sobrien/*
124859243Sobrien * Since on some machines characters are unsigned, and the signed
124959243Sobrien * keyword is not universally implemented, we treat all characters
125059243Sobrien * as unsigned and sign extend them where we need.
125159243Sobrien */
125259243Sobrien#define SIGN_EXTEND_CHAR(a)	(((a) & 0x80) ? ((a) | ~0x7f) : (a))
125359243Sobrien
1254145479Smp/*
1255145479Smp * explanation for use by the "--help" option
1256145479Smp */
1257145479Smp#define HELP_STRING "\
1258145479Smp-b file		batch mode, read and execute commands from `file' \n\
1259145479Smp-c command	run `command' from next argument \n\
1260145479Smp-d		load directory stack from `~/.cshdirs' \n\
1261145479Smp-Dname[=value]	define environment variable `name' to `value' (DomainOS only) \n\
1262145479Smp-e		exit on any error \n\
1263145479Smp-f		start faster by ignoring the start-up file \n\
1264145479Smp-F		use fork() instead of vfork() when spawning (ConvexOS only) \n\
1265145479Smp-i		interactive, even when input is not from a terminal \n\
1266145479Smp-l		act as a login shell, must be the only option specified \n\
1267145479Smp-m		load the start-up file, whether or not owned by effective user \n\
1268145479Smp-n file		no execute mode, just check syntax of the following `file' \n\
1269145479Smp-q		accept SIGQUIT for running under a debugger \n\
1270145479Smp-s		read commands from standard input \n\
1271145479Smp-t		read one line from standard input \n\
1272145479Smp-v		echo commands after history substitution \n\
1273145479Smp-V		like -v but including commands read from the start-up file \n\
1274145479Smp-x		echo commands immediately before execution \n\
1275145479Smp-X		like -x but including commands read from the start-up file \n\
1276145479Smp--help		print this message and exit \n\
1277145479Smp--version	print the version shell variable and exit \n\
1278145479Smp\nSee the tcsh(1) manual page for detailed information.\n"
1279145479Smp
1280145479Smp#include "tc.nls.h"
1281145479Smp
128259243Sobrien#endif /* _h_sh */
1283