1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien * sh.h: Catch it all globals and includes file!
3c80476e4SDavid E. O'Brien */
4c80476e4SDavid E. O'Brien /*-
5c80476e4SDavid E. O'Brien * Copyright (c) 1980, 1991 The Regents of the University of California.
6c80476e4SDavid E. O'Brien * All rights reserved.
7c80476e4SDavid E. O'Brien *
8c80476e4SDavid E. O'Brien * Redistribution and use in source and binary forms, with or without
9c80476e4SDavid E. O'Brien * modification, are permitted provided that the following conditions
10c80476e4SDavid E. O'Brien * are met:
11c80476e4SDavid E. O'Brien * 1. Redistributions of source code must retain the above copyright
12c80476e4SDavid E. O'Brien * notice, this list of conditions and the following disclaimer.
13c80476e4SDavid E. O'Brien * 2. Redistributions in binary form must reproduce the above copyright
14c80476e4SDavid E. O'Brien * notice, this list of conditions and the following disclaimer in the
15c80476e4SDavid E. O'Brien * documentation and/or other materials provided with the distribution.
1629301572SMark Peek * 3. Neither the name of the University nor the names of its contributors
17c80476e4SDavid E. O'Brien * may be used to endorse or promote products derived from this software
18c80476e4SDavid E. O'Brien * without specific prior written permission.
19c80476e4SDavid E. O'Brien *
20c80476e4SDavid E. O'Brien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c80476e4SDavid E. O'Brien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c80476e4SDavid E. O'Brien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c80476e4SDavid E. O'Brien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c80476e4SDavid E. O'Brien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c80476e4SDavid E. O'Brien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c80476e4SDavid E. O'Brien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c80476e4SDavid E. O'Brien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c80476e4SDavid E. O'Brien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c80476e4SDavid E. O'Brien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c80476e4SDavid E. O'Brien * SUCH DAMAGE.
31c80476e4SDavid E. O'Brien */
32c80476e4SDavid E. O'Brien #ifndef _h_sh
33c80476e4SDavid E. O'Brien #define _h_sh
34c80476e4SDavid E. O'Brien
35c80476e4SDavid E. O'Brien #include "config.h"
36c80476e4SDavid E. O'Brien
3723338178SMark Peek #include <stddef.h>
3845e5710bSMark Peek #include <signal.h>
3923338178SMark Peek
4023338178SMark Peek #ifdef HAVE_ICONV
4123338178SMark Peek # include <iconv.h>
4229301572SMark Peek #endif
4323338178SMark Peek
4423338178SMark Peek #ifdef HAVE_STDINT_H
4523338178SMark Peek # include <stdint.h>
4623338178SMark Peek #endif
4723338178SMark Peek
4823338178SMark Peek #ifdef HAVE_INTTYPES_H
4923338178SMark Peek # include <inttypes.h>
5023338178SMark Peek #endif
5123338178SMark Peek
5223338178SMark Peek #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
5323338178SMark Peek typedef unsigned long intptr_t;
5429301572SMark Peek #endif
5529301572SMark Peek
56c80476e4SDavid E. O'Brien #ifndef EXTERN
57c80476e4SDavid E. O'Brien # define EXTERN extern
58c80476e4SDavid E. O'Brien #else /* !EXTERN */
593b6eaa7bSAndrey A. Chernov # ifdef WINNT_NATIVE
60c80476e4SDavid E. O'Brien # define IZERO = 0
61c80476e4SDavid E. O'Brien # define IZERO_STRUCT = {0}
623b6eaa7bSAndrey A. Chernov # endif /* WINNT_NATIVE */
63c80476e4SDavid E. O'Brien #endif /* EXTERN */
64c80476e4SDavid E. O'Brien
65c80476e4SDavid E. O'Brien #ifndef IZERO
66c80476e4SDavid E. O'Brien # define IZERO
67c80476e4SDavid E. O'Brien #endif /* IZERO */
68c80476e4SDavid E. O'Brien #ifndef IZERO_STRUCT
69c80476e4SDavid E. O'Brien # define IZERO_STRUCT
70c80476e4SDavid E. O'Brien #endif /* IZERO_STRUCT */
71c80476e4SDavid E. O'Brien
723b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
73c80476e4SDavid E. O'Brien # define INIT_ZERO
74c80476e4SDavid E. O'Brien # define INIT_ZERO_STRUCT
7545e5710bSMark Peek # define force_read xread
763b6eaa7bSAndrey A. Chernov #endif /*!WINNT_NATIVE */
779ccc37e3SMark Peek
789ccc37e3SMark Peek #if defined(KANJI) && defined(WIDE_STRINGS) && defined(HAVE_NL_LANGINFO) && defined(CODESET)
799ccc37e3SMark Peek #define AUTOSET_KANJI
809ccc37e3SMark Peek #endif
81c80476e4SDavid E. O'Brien /*
82c80476e4SDavid E. O'Brien * Sanity
83c80476e4SDavid E. O'Brien */
84c80476e4SDavid E. O'Brien #if defined(_POSIX_SOURCE) && !defined(POSIX)
85c80476e4SDavid E. O'Brien # define POSIX
86c80476e4SDavid E. O'Brien #endif
87c80476e4SDavid E. O'Brien
88c80476e4SDavid E. O'Brien #if defined(POSIXJOBS) && !defined(BSDJOBS)
89c80476e4SDavid E. O'Brien # define BSDJOBS
90c80476e4SDavid E. O'Brien #endif
91c80476e4SDavid E. O'Brien
929ccc37e3SMark Peek #define TMP_TEMPLATE ".XXXXXX"
939ccc37e3SMark Peek
94c80476e4SDavid E. O'Brien #ifdef SHORT_STRINGS
9523338178SMark Peek # ifdef WIDE_STRINGS
9645e5710bSMark Peek #include <wchar.h>
979ccc37e3SMark Peek # ifdef UTF16_STRINGS
989ccc37e3SMark Peek typedef wint_t Char;
999ccc37e3SMark Peek # else
10023338178SMark Peek typedef wchar_t Char;
1019ccc37e3SMark Peek #endif
10223338178SMark Peek typedef unsigned long uChar;
10323338178SMark Peek typedef wint_t eChar; /* Can contain any Char value or CHAR_ERR */
10423338178SMark Peek #define CHAR_ERR WEOF /* Pretty please, use bit 31... */
10523338178SMark Peek #define normal_mbtowc(PWC, S, N) rt_mbtowc(PWC, S, N)
1069ccc37e3SMark Peek #define reset_mbtowc() TCSH_IGNORE(mbtowc(NULL, NULL, 0))
10723338178SMark Peek # else
108c80476e4SDavid E. O'Brien typedef short Char;
109c80476e4SDavid E. O'Brien typedef unsigned short uChar;
11023338178SMark Peek typedef int eChar;
11123338178SMark Peek #define CHAR_ERR (-1)
11223338178SMark Peek #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
11323338178SMark Peek #define reset_mbtowc() ((void)0)
11423338178SMark Peek # endif
115c80476e4SDavid E. O'Brien # define SAVE(a) (Strsave(str2short(a)))
116c80476e4SDavid E. O'Brien #else
117c80476e4SDavid E. O'Brien typedef char Char;
118c80476e4SDavid E. O'Brien typedef unsigned char uChar;
11923338178SMark Peek typedef int eChar;
12023338178SMark Peek #define CHAR_ERR (-1)
12123338178SMark Peek #define normal_mbtowc(PWC, S, N) ((void)(N), *(PWC) = (unsigned char)*(S), 1)
12223338178SMark Peek #define reset_mbtowc() ((void)0)
123c80476e4SDavid E. O'Brien # define SAVE(a) (strsave(a))
124c80476e4SDavid E. O'Brien #endif
125c80476e4SDavid E. O'Brien
1269ccc37e3SMark Peek #if !defined(__inline) && !defined(__GNUC__) && !defined(_MSC_VER)
1279ccc37e3SMark Peek #define __inline
1289ccc37e3SMark Peek #endif
12919d2e3deSDmitry Chagin #ifdef _MSC_VER
13019d2e3deSDmitry Chagin #define TCSH_PTRDIFF_T_FMT "I"
13119d2e3deSDmitry Chagin #else
13219d2e3deSDmitry Chagin #define TCSH_PTRDIFF_T_FMT "t"
13319d2e3deSDmitry Chagin #endif
134c80476e4SDavid E. O'Brien /* Elide unused argument warnings */
135c80476e4SDavid E. O'Brien #define USE(a) (void) (a)
1369ccc37e3SMark Peek #define TCSH_IGNORE(a) tcsh_ignore((intptr_t)a)
tcsh_ignore(intptr_t a)1379ccc37e3SMark Peek static __inline void tcsh_ignore(intptr_t a)
138a15e6f9aSMark Peek {
139a15e6f9aSMark Peek USE(a);
140a15e6f9aSMark Peek }
141c80476e4SDavid E. O'Brien
142c80476e4SDavid E. O'Brien /*
143c80476e4SDavid E. O'Brien * Return true if the path is absolute
144c80476e4SDavid E. O'Brien */
145b2d5d167SMark Peek #if defined(WINNT_NATIVE)
146c80476e4SDavid E. O'Brien # define ABSOLUTEP(p) ((p)[0] == '/' || \
14729301572SMark Peek (Isalpha((p)[0]) && (p)[1] == ':'))
148b2d5d167SMark Peek #elif defined(__CYGWIN__)
149b2d5d167SMark Peek # define ABSOLUTEP(p) ((p)[0] == '/' || \
150b2d5d167SMark Peek (Isalpha((p)[0]) && (p)[1] == ':' && \
151b2d5d167SMark Peek ((p)[2] == '\0' || (p)[2] == '/')))
15229301572SMark Peek #else /* !WINNT_NATIVE && !__CYGWIN__ */
15329301572SMark Peek # define ABSOLUTEP(p) (*(p) == '/')
15429301572SMark Peek #endif /* WINNT_NATIVE || __CYGWIN__ */
155c80476e4SDavid E. O'Brien
156c80476e4SDavid E. O'Brien /*
157c80476e4SDavid E. O'Brien * Fundamental definitions which may vary from system to system.
158c80476e4SDavid E. O'Brien *
159c80476e4SDavid E. O'Brien * BUFSIZE The i/o buffering size; also limits word size
160c80476e4SDavid E. O'Brien * MAILINTVL How often to mailcheck; more often is more expensive
161c80476e4SDavid E. O'Brien */
162c80476e4SDavid E. O'Brien #ifdef BUFSIZE
163b2be84b1SDavid E. O'Brien # if BUFSIZE < 4096
164c80476e4SDavid E. O'Brien # undef BUFSIZE
165b2be84b1SDavid E. O'Brien # define BUFSIZE 4096 /* buffer size should be no less than this */
166c80476e4SDavid E. O'Brien # endif
167c80476e4SDavid E. O'Brien #else
168b2be84b1SDavid E. O'Brien # define BUFSIZE 4096
169c80476e4SDavid E. O'Brien #endif /* BUFSIZE */
170c80476e4SDavid E. O'Brien
171c80476e4SDavid E. O'Brien #define FORKSLEEP 10 /* delay loop on non-interactive fork failure */
172c80476e4SDavid E. O'Brien #define MAILINTVL 600 /* 10 minutes */
173c80476e4SDavid E. O'Brien
174c80476e4SDavid E. O'Brien #ifndef INBUFSIZE
175c80476e4SDavid E. O'Brien # define INBUFSIZE 2*BUFSIZE /* Num input characters on the command line */
176c80476e4SDavid E. O'Brien #endif /* INBUFSIZE */
177c80476e4SDavid E. O'Brien
178c80476e4SDavid E. O'Brien
179c80476e4SDavid E. O'Brien /*
180c80476e4SDavid E. O'Brien * What our builtin echo looks like
181c80476e4SDavid E. O'Brien */
182c80476e4SDavid E. O'Brien #define NONE_ECHO 0
183c80476e4SDavid E. O'Brien #define BSD_ECHO 1
184c80476e4SDavid E. O'Brien #define SYSV_ECHO 2
185c80476e4SDavid E. O'Brien #define BOTH_ECHO (BSD_ECHO|SYSV_ECHO)
186c80476e4SDavid E. O'Brien
187c80476e4SDavid E. O'Brien #ifndef ECHO_STYLE
188c80476e4SDavid E. O'Brien # if SYSVREL > 0
189c80476e4SDavid E. O'Brien # define ECHO_STYLE SYSV_ECHO
190c80476e4SDavid E. O'Brien # else /* SYSVREL == 0 */
191c80476e4SDavid E. O'Brien # define ECHO_STYLE BSD_ECHO
192c80476e4SDavid E. O'Brien # endif /* SYSVREL */
193c80476e4SDavid E. O'Brien #endif /* ECHO_STYLE */
194c80476e4SDavid E. O'Brien
19519d2e3deSDmitry Chagin /* values for noclobber */
19619d2e3deSDmitry Chagin #define NOCLOBBER_DEFAULT 1
19719d2e3deSDmitry Chagin #define NOCLOBBER_NOTEMPTY 2
19819d2e3deSDmitry Chagin #define NOCLOBBER_ASK 4
19919d2e3deSDmitry Chagin
200c80476e4SDavid E. O'Brien /*
201c80476e4SDavid E. O'Brien * The shell moves std in/out/diag and the old std input away from units
202c80476e4SDavid E. O'Brien * 0, 1, and 2 so that it is easy to set up these standards for invoked
203c80476e4SDavid E. O'Brien * commands.
204c80476e4SDavid E. O'Brien */
20523338178SMark Peek #define FSAFE 5 /* We keep the first 5 descriptors untouched */
206c80476e4SDavid E. O'Brien #define FSHTTY 15 /* /dev/tty when manip pgrps */
207c80476e4SDavid E. O'Brien #define FSHIN 16 /* Preferred desc for shell input */
208c80476e4SDavid E. O'Brien #define FSHOUT 17 /* ... shell output */
209c80476e4SDavid E. O'Brien #define FSHDIAG 18 /* ... shell diagnostics */
210c80476e4SDavid E. O'Brien #define FOLDSTD 19 /* ... old std input */
211c80476e4SDavid E. O'Brien
212c80476e4SDavid E. O'Brien #ifdef PROF
213c80476e4SDavid E. O'Brien #define xexit(n) done(n)
214c80476e4SDavid E. O'Brien #endif
215c80476e4SDavid E. O'Brien
216c80476e4SDavid E. O'Brien #ifdef cray
217c80476e4SDavid E. O'Brien # define word word_t /* sys/types.h defines word.. bad move! */
218c80476e4SDavid E. O'Brien #endif
219c80476e4SDavid E. O'Brien
220c80476e4SDavid E. O'Brien #include <sys/types.h>
221c80476e4SDavid E. O'Brien
222c80476e4SDavid E. O'Brien #ifdef cray
223c80476e4SDavid E. O'Brien # undef word
224c80476e4SDavid E. O'Brien #endif
225c80476e4SDavid E. O'Brien
226c80476e4SDavid E. O'Brien /*
227c80476e4SDavid E. O'Brien * Path separator in environment variables
228c80476e4SDavid E. O'Brien */
229c80476e4SDavid E. O'Brien #ifndef PATHSEP
2303b6eaa7bSAndrey A. Chernov # if defined(__EMX__) || defined(WINNT_NATIVE)
231c80476e4SDavid E. O'Brien # define PATHSEP ';'
232c80476e4SDavid E. O'Brien # else /* unix */
233c80476e4SDavid E. O'Brien # define PATHSEP ':'
2343b6eaa7bSAndrey A. Chernov # endif /* __EMX__ || WINNT_NATIVE */
235c80476e4SDavid E. O'Brien #endif /* !PATHSEP */
236c80476e4SDavid E. O'Brien
2376767bd61SMark Peek #if defined(__HP_CXD_SPP) && !defined(__hpux)
238c80476e4SDavid E. O'Brien # include <sys/cnx_stat.h>
239c80476e4SDavid E. O'Brien # define stat stat64
240c80476e4SDavid E. O'Brien # define fstat fstat64
241c80476e4SDavid E. O'Brien # define lstat lstat64
2426767bd61SMark Peek #endif /* __HP_CXD_SPP && !__hpux */
243c80476e4SDavid E. O'Brien
2449ccc37e3SMark Peek #ifdef HAVE_LONG_LONG
2459ccc37e3SMark Peek typedef long long tcsh_number_t;
2469ccc37e3SMark Peek #else
2479ccc37e3SMark Peek typedef long tcsh_number_t;
2489ccc37e3SMark Peek #endif
249c80476e4SDavid E. O'Brien /*
250c80476e4SDavid E. O'Brien * This macro compares the st_dev field of struct stat. On aix on ibmESA
251c80476e4SDavid E. O'Brien * st_dev is a structure, so comparison does not work.
252c80476e4SDavid E. O'Brien */
253c80476e4SDavid E. O'Brien #ifndef DEV_DEV_COMPARE
254c80476e4SDavid E. O'Brien # define DEV_DEV_COMPARE(x,y) ((x) == (y))
255c80476e4SDavid E. O'Brien #endif /* DEV_DEV_COMPARE */
256c80476e4SDavid E. O'Brien
257c80476e4SDavid E. O'Brien #ifdef _SEQUENT_
258c80476e4SDavid E. O'Brien # include <sys/procstats.h>
259c80476e4SDavid E. O'Brien #endif /* _SEQUENT_ */
2603b6eaa7bSAndrey A. Chernov #if (defined(POSIX) || SYSVREL > 0) && !defined(WINNT_NATIVE)
261c80476e4SDavid E. O'Brien # include <sys/times.h>
2623b6eaa7bSAndrey A. Chernov #endif /* (POSIX || SYSVREL > 0) && !WINNT_NATIVE */
263c80476e4SDavid E. O'Brien
264c80476e4SDavid E. O'Brien #ifdef NLS
265c80476e4SDavid E. O'Brien # include <locale.h>
266c80476e4SDavid E. O'Brien #endif /* NLS */
267c80476e4SDavid E. O'Brien
2683b6eaa7bSAndrey A. Chernov #if !defined(_MINIX) && !defined(_VMS_POSIX) && !defined(WINNT_NATIVE) && !defined(__MVS__)
269c80476e4SDavid E. O'Brien # include <sys/param.h>
2703b6eaa7bSAndrey A. Chernov #endif /* !_MINIX && !_VMS_POSIX && !WINNT_NATIVE && !__MVS__ */
271c80476e4SDavid E. O'Brien #include <sys/stat.h>
272c80476e4SDavid E. O'Brien
273c80476e4SDavid E. O'Brien #if defined(BSDTIMES) || defined(BSDLIMIT)
274c80476e4SDavid E. O'Brien # include <sys/time.h>
2753b6eaa7bSAndrey A. Chernov # if SYSVREL>3 && !defined(SCO) && !defined(sgi) && !defined(SNI) && !defined(sun) && !(defined(__alpha) && defined(__osf__)) && !defined(_SX) && !defined(__MVS__)
276c80476e4SDavid E. O'Brien # include "/usr/ucbinclude/sys/resource.h"
277c80476e4SDavid E. O'Brien # else
278c80476e4SDavid E. O'Brien # ifdef convex
279c80476e4SDavid E. O'Brien # define sysrusage cvxrusage
280c80476e4SDavid E. O'Brien # include <sys/sysinfo.h>
281c80476e4SDavid E. O'Brien # else
282c80476e4SDavid E. O'Brien # define sysrusage rusage
283c80476e4SDavid E. O'Brien # include <sys/resource.h>
284c80476e4SDavid E. O'Brien # endif /* convex */
285c80476e4SDavid E. O'Brien # endif /* SYSVREL>3 */
286c80476e4SDavid E. O'Brien #endif /* BSDTIMES */
287c80476e4SDavid E. O'Brien
2883b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
289c80476e4SDavid E. O'Brien # ifndef POSIX
290c80476e4SDavid E. O'Brien # ifdef TERMIO
291c80476e4SDavid E. O'Brien # include <termio.h>
292c80476e4SDavid E. O'Brien # else /* SGTTY */
293c80476e4SDavid E. O'Brien # include <sgtty.h>
294c80476e4SDavid E. O'Brien # endif /* TERMIO */
295c80476e4SDavid E. O'Brien # else /* POSIX */
296c80476e4SDavid E. O'Brien # ifndef _UWIN
297c80476e4SDavid E. O'Brien # include <termios.h>
298c80476e4SDavid E. O'Brien # else
299c80476e4SDavid E. O'Brien # include <termio.h>
300c80476e4SDavid E. O'Brien # endif /* _UWIN */
3019ccc37e3SMark Peek # if SYSVREL > 3 || defined(__linux__)
302c80476e4SDavid E. O'Brien # undef TIOCGLTC /* we don't need those, since POSIX has them */
303c80476e4SDavid E. O'Brien # undef TIOCSLTC
304c80476e4SDavid E. O'Brien # undef CSWTCH
305c80476e4SDavid E. O'Brien # define CSWTCH _POSIX_VDISABLE /* So job control works */
306c80476e4SDavid E. O'Brien # endif /* SYSVREL > 3 */
307c80476e4SDavid E. O'Brien # endif /* POSIX */
3083b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
309c80476e4SDavid E. O'Brien
310c80476e4SDavid E. O'Brien #ifdef sonyrisc
311c80476e4SDavid E. O'Brien # include <sys/ttold.h>
312c80476e4SDavid E. O'Brien #endif /* sonyrisc */
313c80476e4SDavid E. O'Brien
3143b6eaa7bSAndrey A. Chernov #if defined(POSIX) && !defined(WINNT_NATIVE)
315c80476e4SDavid E. O'Brien # include <unistd.h>
316c80476e4SDavid E. O'Brien
317c80476e4SDavid E. O'Brien /*
318c80476e4SDavid E. O'Brien * the gcc+protoize version of <stdlib.h>
319c80476e4SDavid E. O'Brien * redefines malloc(), so we define the following
320c80476e4SDavid E. O'Brien * to avoid it.
321c80476e4SDavid E. O'Brien */
3229ccc37e3SMark Peek # if defined(SYSMALLOC) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || defined(sgi) || defined(_OSD_POSIX)
323c80476e4SDavid E. O'Brien # define NO_FIX_MALLOC
324c80476e4SDavid E. O'Brien # include <stdlib.h>
32523338178SMark Peek # else /* glibc */
326c80476e4SDavid E. O'Brien # define _GNU_STDLIB_H
327c80476e4SDavid E. O'Brien # define malloc __malloc
328c80476e4SDavid E. O'Brien # define free __free
329c80476e4SDavid E. O'Brien # define calloc __calloc
330c80476e4SDavid E. O'Brien # define realloc __realloc
331c80476e4SDavid E. O'Brien # include <stdlib.h>
332c80476e4SDavid E. O'Brien # undef malloc
333c80476e4SDavid E. O'Brien # undef free
334c80476e4SDavid E. O'Brien # undef calloc
335c80476e4SDavid E. O'Brien # undef realloc
33623338178SMark Peek # endif /* glibc || sgi */
3373b6eaa7bSAndrey A. Chernov #endif /* POSIX && !WINNT_NATIVE */
33845e5710bSMark Peek #include <limits.h>
339c80476e4SDavid E. O'Brien
3409ccc37e3SMark Peek #if SYSVREL > 0 || defined(_IBMR2) || defined(_MINIX) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
341c80476e4SDavid E. O'Brien # if !defined(pyr) && !defined(stellar)
342c80476e4SDavid E. O'Brien # include <time.h>
343c80476e4SDavid E. O'Brien # ifdef _MINIX
344c80476e4SDavid E. O'Brien # define HZ CLOCKS_PER_SEC
345c80476e4SDavid E. O'Brien # endif /* _MINIX */
346c80476e4SDavid E. O'Brien # endif /* !pyr && !stellar */
347c80476e4SDavid E. O'Brien #endif /* SYSVREL > 0 || _IBMR2 */
348c80476e4SDavid E. O'Brien
349c80476e4SDavid E. O'Brien /* In the following ifdef the DECOSF1 has been commented so that later
350c80476e4SDavid E. O'Brien * versions of DECOSF1 will get TIOCGWINSZ. This might break older versions...
351c80476e4SDavid E. O'Brien */
352c80476e4SDavid E. O'Brien #if !((defined(SUNOS4) || defined(_MINIX) /* || defined(DECOSF1) */) && defined(TERMIO))
35345e5710bSMark Peek # if !defined(_VMS_POSIX) && !defined(WINNT_NATIVE)
354c80476e4SDavid E. O'Brien # include <sys/ioctl.h>
3559ccc37e3SMark Peek # if SYSVREL > 3 || defined(__linux__)
3569ccc37e3SMark Peek # undef TIOCGLTC /* we don't need those, since POSIX has them */
3579ccc37e3SMark Peek # undef TIOCSLTC
3589ccc37e3SMark Peek # undef CSWTCH
3599ccc37e3SMark Peek # define CSWTCH _POSIX_VDISABLE /* So job control works */
3609ccc37e3SMark Peek # endif /* SYSVREL > 3 */
361c80476e4SDavid E. O'Brien # endif
362c80476e4SDavid E. O'Brien #endif
363c80476e4SDavid E. O'Brien
364c80476e4SDavid E. O'Brien #if (defined(__DGUX__) && defined(POSIX)) || defined(DGUX)
365c80476e4SDavid E. O'Brien #undef CSWTCH
366c80476e4SDavid E. O'Brien #define CSWTCH _POSIX_VDISABLE
367c80476e4SDavid E. O'Brien #endif
368c80476e4SDavid E. O'Brien
3693b6eaa7bSAndrey A. Chernov #if (!defined(FIOCLEX) && defined(SUNOS4)) || ((SYSVREL == 4) && !defined(_SEQUENT_) && !defined(SCO) && !defined(_SX)) && !defined(__MVS__)
370c80476e4SDavid E. O'Brien # include <sys/filio.h>
371c80476e4SDavid E. O'Brien #endif /* (!FIOCLEX && SUNOS4) || (SYSVREL == 4 && !_SEQUENT_ && !SCO && !_SX ) */
372c80476e4SDavid E. O'Brien
37345e5710bSMark Peek #if !defined(_MINIX) && !defined(supermax) && !defined(WINNT_NATIVE) && !defined(IRIS4D)
374c80476e4SDavid E. O'Brien # include <sys/file.h>
37545e5710bSMark Peek #endif /* !_MINIX && !supermax && !WINNT_NATIVE && !defined(IRIS4D) */
376c80476e4SDavid E. O'Brien
377c80476e4SDavid E. O'Brien #if !defined(O_RDONLY) || !defined(O_NDELAY)
378c80476e4SDavid E. O'Brien # include <fcntl.h>
379c80476e4SDavid E. O'Brien #endif
380c80476e4SDavid E. O'Brien
381c80476e4SDavid E. O'Brien #include <errno.h>
382c80476e4SDavid E. O'Brien
383c80476e4SDavid E. O'Brien #include <setjmp.h>
384c80476e4SDavid E. O'Brien
385c80476e4SDavid E. O'Brien #include <stdarg.h>
386c80476e4SDavid E. O'Brien
38723338178SMark Peek #ifdef HAVE_DIRENT_H
388c80476e4SDavid E. O'Brien # include <dirent.h>
389c80476e4SDavid E. O'Brien #else
39023338178SMark Peek # ifdef HAVE_NDIR_H
391c80476e4SDavid E. O'Brien # include <ndir.h>
392c80476e4SDavid E. O'Brien # else
393c80476e4SDavid E. O'Brien # include <sys/dir.h>
394c80476e4SDavid E. O'Brien # endif
395c80476e4SDavid E. O'Brien # define dirent direct
39623338178SMark Peek #endif /* HAVE_DIRENT_H */
39723338178SMark Peek #ifndef HAVE_STRUCT_DIRENT_D_INO
39823338178SMark Peek # define d_ino d_fileno
39923338178SMark Peek #endif
40045e5710bSMark Peek #if defined(hpux) || defined(sgi) || defined(OREO)
401c80476e4SDavid E. O'Brien # include <stdio.h> /* So the fgetpwent() prototypes work */
40245e5710bSMark Peek #endif /* hpux || sgi || OREO */
4033b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
404c80476e4SDavid E. O'Brien #include <pwd.h>
405c80476e4SDavid E. O'Brien #include <grp.h>
4063b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
40723338178SMark Peek #ifdef HAVE_SHADOW_H
408c80476e4SDavid E. O'Brien # include <shadow.h>
40923338178SMark Peek #endif /* HAVE_SHADOW_H */
41023338178SMark Peek #ifdef HAVE_AUTH_H
411c80476e4SDavid E. O'Brien # include <auth.h>
41223338178SMark Peek #endif /* HAVE_AUTH_H */
413c80476e4SDavid E. O'Brien #if defined(BSD) && !defined(POSIX)
414c80476e4SDavid E. O'Brien # include <strings.h>
415c80476e4SDavid E. O'Brien # define strchr(a, b) index(a, b)
416c80476e4SDavid E. O'Brien # define strrchr(a, b) rindex(a, b)
417c80476e4SDavid E. O'Brien #else
418c80476e4SDavid E. O'Brien # include <string.h>
419c80476e4SDavid E. O'Brien #endif /* BSD */
420c80476e4SDavid E. O'Brien
421c80476e4SDavid E. O'Brien /*
422c80476e4SDavid E. O'Brien * IRIX-5.0 has <sys/cdefs.h>, but most system include files do not
423c80476e4SDavid E. O'Brien * include it yet, so we include it here
424c80476e4SDavid E. O'Brien */
425c80476e4SDavid E. O'Brien #if defined(sgi) && SYSVREL > 3
426c80476e4SDavid E. O'Brien # include <sys/cdefs.h>
427c80476e4SDavid E. O'Brien #endif /* sgi && SYSVREL > 3 */
428c80476e4SDavid E. O'Brien
429c80476e4SDavid E. O'Brien #ifdef REMOTEHOST
430c80476e4SDavid E. O'Brien # ifdef ISC
431c80476e4SDavid E. O'Brien # undef MAXHOSTNAMELEN /* Busted headers? */
432c80476e4SDavid E. O'Brien # endif
433c80476e4SDavid E. O'Brien
434c80476e4SDavid E. O'Brien # include <netinet/in.h>
435c80476e4SDavid E. O'Brien # include <arpa/inet.h>
436c80476e4SDavid E. O'Brien # include <sys/socket.h>
43723338178SMark Peek # if (defined(_SS_SIZE) || defined(_SS_MAXSIZE)) && defined(HAVE_STRUCT_SOCKADDR_STORAGE_SS_FAMILY)
43829301572SMark Peek # if !defined(__APPLE__) /* Damnit, where is getnameinfo() folks? */
43929301572SMark Peek # if !defined(sgi)
4403b6eaa7bSAndrey A. Chernov # define INET6
44129301572SMark Peek # endif /* sgi */
44229301572SMark Peek # endif /* __APPLE__ */
4433b6eaa7bSAndrey A. Chernov # endif
444c80476e4SDavid E. O'Brien # include <sys/uio.h> /* For struct iovec */
445c80476e4SDavid E. O'Brien #endif /* REMOTEHOST */
446c80476e4SDavid E. O'Brien
447c80476e4SDavid E. O'Brien #ifdef PURIFY
448c80476e4SDavid E. O'Brien /* exit normally, allowing purify to trace leaks */
449c80476e4SDavid E. O'Brien # define _exit exit
45019d2e3deSDmitry Chagin #endif /* !PURIFY */
451c80476e4SDavid E. O'Brien
4523b6eaa7bSAndrey A. Chernov /*
4533b6eaa7bSAndrey A. Chernov * ASCII vs. EBCDIC
4543b6eaa7bSAndrey A. Chernov */
4553b6eaa7bSAndrey A. Chernov #if 'Z' - 'A' == 25
4563b6eaa7bSAndrey A. Chernov # ifndef IS_ASCII
4573b6eaa7bSAndrey A. Chernov # define IS_ASCII
4583b6eaa7bSAndrey A. Chernov # endif
4593b6eaa7bSAndrey A. Chernov #endif
4603b6eaa7bSAndrey A. Chernov
461c80476e4SDavid E. O'Brien #include "sh.types.h"
462c80476e4SDavid E. O'Brien
46345e5710bSMark Peek #if !HAVE_DECL_GETPGRP
46423338178SMark Peek # ifndef GETPGRP_VOID
46545e5710bSMark Peek extern pid_t getpgrp (int);
46623338178SMark Peek # else
46745e5710bSMark Peek extern pid_t getpgrp (void);
46823338178SMark Peek # endif
46923338178SMark Peek #endif
470c80476e4SDavid E. O'Brien
471c80476e4SDavid E. O'Brien #ifndef lint
472c80476e4SDavid E. O'Brien typedef ptr_t memalign_t;
473c80476e4SDavid E. O'Brien #else
474c80476e4SDavid E. O'Brien typedef union {
475c80476e4SDavid E. O'Brien char am_char, *am_char_p;
476c80476e4SDavid E. O'Brien short am_short, *am_short_p;
477c80476e4SDavid E. O'Brien int am_int, *am_int_p;
478c80476e4SDavid E. O'Brien long am_long, *am_long_p;
479c80476e4SDavid E. O'Brien float am_float, *am_float_p;
480c80476e4SDavid E. O'Brien double am_double, *am_double_p;
481c80476e4SDavid E. O'Brien } *memalign_t;
482c80476e4SDavid E. O'Brien
483c80476e4SDavid E. O'Brien # define malloc lint_malloc
484c80476e4SDavid E. O'Brien # define free lint_free
485c80476e4SDavid E. O'Brien # define realloc lint_realloc
486c80476e4SDavid E. O'Brien # define calloc lint_calloc
487c80476e4SDavid E. O'Brien #endif
488c80476e4SDavid E. O'Brien
489c80476e4SDavid E. O'Brien #ifdef SYSMALLOC
490c80476e4SDavid E. O'Brien # define xmalloc(i) smalloc(i)
491c80476e4SDavid E. O'Brien # define xrealloc(p, i) srealloc(p, i)
492c80476e4SDavid E. O'Brien # define xcalloc(n, s) scalloc(n, s)
49345e5710bSMark Peek # define xfree sfree
494c80476e4SDavid E. O'Brien #else
495c80476e4SDavid E. O'Brien # define xmalloc(i) malloc(i)
496c80476e4SDavid E. O'Brien # define xrealloc(p, i) realloc(p, i)
497c80476e4SDavid E. O'Brien # define xcalloc(n, s) calloc(n, s)
49845e5710bSMark Peek # define xfree free
499c80476e4SDavid E. O'Brien #endif /* SYSMALLOC */
500c80476e4SDavid E. O'Brien #include "sh.char.h"
501c80476e4SDavid E. O'Brien #include "sh.err.h"
502c80476e4SDavid E. O'Brien #include "sh.dir.h"
503c80476e4SDavid E. O'Brien #include "sh.proc.h"
504c80476e4SDavid E. O'Brien
505c80476e4SDavid E. O'Brien #include "pathnames.h"
506c80476e4SDavid E. O'Brien
507c80476e4SDavid E. O'Brien
508c80476e4SDavid E. O'Brien /*
509c80476e4SDavid E. O'Brien * C shell
510c80476e4SDavid E. O'Brien *
511c80476e4SDavid E. O'Brien * Bill Joy, UC Berkeley
512c80476e4SDavid E. O'Brien * October, 1978; May 1980
513c80476e4SDavid E. O'Brien *
514c80476e4SDavid E. O'Brien * Jim Kulp, IIASA, Laxenburg Austria
515c80476e4SDavid E. O'Brien * April, 1980
516c80476e4SDavid E. O'Brien */
517c80476e4SDavid E. O'Brien
518c80476e4SDavid E. O'Brien #ifdef HESIOD
519c80476e4SDavid E. O'Brien # include <hesiod.h>
520c80476e4SDavid E. O'Brien #endif /* HESIOD */
521c80476e4SDavid E. O'Brien
522c80476e4SDavid E. O'Brien #ifdef REMOTEHOST
523c80476e4SDavid E. O'Brien # include <netdb.h>
524c80476e4SDavid E. O'Brien #endif /* REMOTEHOST */
525c80476e4SDavid E. O'Brien
526c80476e4SDavid E. O'Brien #ifndef MAXHOSTNAMELEN
52745e5710bSMark Peek # ifdef HOST_NAME_MAX
52845e5710bSMark Peek # define MAXHOSTNAMELEN (HOST_NAME_MAX + 1)
52945e5710bSMark Peek # elif defined(SCO) && (SYSVREL > 3)
530c80476e4SDavid E. O'Brien # include <sys/socket.h>
531c80476e4SDavid E. O'Brien # else
5326767bd61SMark Peek # define MAXHOSTNAMELEN 256
533c80476e4SDavid E. O'Brien # endif
534c80476e4SDavid E. O'Brien #endif /* MAXHOSTNAMELEN */
535c80476e4SDavid E. O'Brien
536c80476e4SDavid E. O'Brien
537c80476e4SDavid E. O'Brien
538c80476e4SDavid E. O'Brien #define eq(a, b) (Strcmp(a, b) == 0)
539c80476e4SDavid E. O'Brien
540c80476e4SDavid E. O'Brien /* globone() flags */
541c80476e4SDavid E. O'Brien #define G_ERROR 0 /* default action: error if multiple words */
542c80476e4SDavid E. O'Brien #define G_IGNORE 1 /* ignore the rest of the words */
543c80476e4SDavid E. O'Brien #define G_APPEND 2 /* make a sentence by cat'ing the words */
544c80476e4SDavid E. O'Brien
545c80476e4SDavid E. O'Brien /*
546c80476e4SDavid E. O'Brien * Global flags
547c80476e4SDavid E. O'Brien */
54823338178SMark Peek EXTERN int chkstop IZERO; /* Warned of stopped jobs... allow exit */
549c80476e4SDavid E. O'Brien
550c80476e4SDavid E. O'Brien #if (defined(FIOCLEX) && defined(FIONCLEX)) || defined(F_SETFD)
551c80476e4SDavid E. O'Brien # define CLOSE_ON_EXEC
552c80476e4SDavid E. O'Brien #else
55323338178SMark Peek EXTERN int didcch IZERO; /* Have closed unused fd's for child */
554c80476e4SDavid E. O'Brien #endif /* (FIOCLEX && FIONCLEX) || F_SETFD */
555c80476e4SDavid E. O'Brien
55623338178SMark Peek EXTERN int didfds IZERO; /* Have setup i/o fd's for child */
55723338178SMark Peek EXTERN int doneinp IZERO; /* EOF indicator after reset from readc */
55823338178SMark Peek EXTERN int exiterr IZERO; /* Exit if error or non-zero exit status */
55923338178SMark Peek EXTERN int child IZERO; /* Child shell ... errors cause exit */
56023338178SMark Peek EXTERN int haderr IZERO; /* Reset was because of an error */
56123338178SMark Peek EXTERN int intty IZERO; /* Input is a tty */
56223338178SMark Peek EXTERN int intact IZERO; /* We are interactive... therefore prompt */
56323338178SMark Peek EXTERN int justpr IZERO; /* Just print because of :p hist mod */
56423338178SMark Peek EXTERN int loginsh IZERO; /* We are a loginsh -> .login/.logout */
56523338178SMark Peek EXTERN int neednote IZERO; /* Need to pnotify() */
56623338178SMark Peek EXTERN int noexec IZERO; /* Don't execute, just syntax check */
56723338178SMark Peek EXTERN int pjobs IZERO; /* want to print jobs if interrupted */
56823338178SMark Peek EXTERN int setintr IZERO; /* Set interrupts on/off -> Wait intr... */
56919d2e3deSDmitry Chagin EXTERN int handle_interrupt IZERO;/* Are we currently handling an interrupt? */
57023338178SMark Peek EXTERN int havhash IZERO; /* path hashing is available */
57123338178SMark Peek EXTERN int editing IZERO; /* doing filename expansion and line editing */
57223338178SMark Peek EXTERN int noediting IZERO; /* initial $term defaulted to noedit */
57323338178SMark Peek EXTERN int bslash_quote IZERO;/* PWP: tcsh-style quoting? (in sh.c) */
5749ccc37e3SMark Peek EXTERN int anyerror IZERO; /* propagate errors from pipelines/backq */
575a15e6f9aSMark Peek EXTERN int compat_expr IZERO;/* csh-style expressions? */
57623338178SMark Peek EXTERN int isoutatty IZERO; /* is SHOUT a tty */
57723338178SMark Peek EXTERN int isdiagatty IZERO;/* is SHDIAG a tty */
57823338178SMark Peek EXTERN int is1atty IZERO; /* is file descriptor 1 a tty (didfds mode) */
57923338178SMark Peek EXTERN int is2atty IZERO; /* is file descriptor 2 a tty (didfds mode) */
58023338178SMark Peek EXTERN int arun IZERO; /* Currently running multi-line-aliases */
581c80476e4SDavid E. O'Brien EXTERN int implicit_cd IZERO;/* implicit cd enabled?(1=enabled,2=verbose) */
58219d2e3deSDmitry Chagin EXTERN int cdtohome IZERO; /* cd without args goes home */
58323338178SMark Peek EXTERN int inheredoc IZERO; /* Currently parsing a heredoc */
58419d2e3deSDmitry Chagin EXTERN int no_clobber IZERO; /* no clobber enabled? 1=yes 2=notempty, 4=ask*/
58545e5710bSMark Peek /* We received a window change event */
58645e5710bSMark Peek EXTERN volatile sig_atomic_t windowchg IZERO;
58723338178SMark Peek #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
58823338178SMark Peek EXTERN int dspmbyte_ls;
58923338178SMark Peek #endif
590c80476e4SDavid E. O'Brien
591c80476e4SDavid E. O'Brien /*
592c80476e4SDavid E. O'Brien * Global i/o info
593c80476e4SDavid E. O'Brien */
594c80476e4SDavid E. O'Brien EXTERN Char *arginp IZERO; /* Argument input for sh -c and internal `xx` */
595c80476e4SDavid E. O'Brien EXTERN int onelflg IZERO; /* 2 -> need line for -t, 1 -> exit on read */
596c80476e4SDavid E. O'Brien extern Char *ffile; /* Name of shell file for $0 */
59723338178SMark Peek extern int dolzero; /* if $?0 should return true... */
598c80476e4SDavid E. O'Brien
599c80476e4SDavid E. O'Brien extern char *seterr; /* Error message from scanner/parser */
60023338178SMark Peek #ifndef errno
601c80476e4SDavid E. O'Brien extern int errno; /* Error from C library routines */
6023b6eaa7bSAndrey A. Chernov #endif
6033b6eaa7bSAndrey A. Chernov extern int exitset;
60445e5710bSMark Peek /* Temp name for << shell files in /tmp, for xfree() */
60545e5710bSMark Peek EXTERN Char *shtemp IZERO;
606c80476e4SDavid E. O'Brien
607c80476e4SDavid E. O'Brien #ifdef BSDTIMES
608c80476e4SDavid E. O'Brien EXTERN struct timeval time0; /* Time at which the shell started */
609c80476e4SDavid E. O'Brien EXTERN struct sysrusage ru0;
610c80476e4SDavid E. O'Brien #else
611c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
612c80476e4SDavid E. O'Brien EXTERN timeval_t time0; /* time at which shell started */
613c80476e4SDavid E. O'Brien EXTERN struct process_stats ru0;
614c80476e4SDavid E. O'Brien # else /* _SEQUENT_ */
615c80476e4SDavid E. O'Brien # ifndef POSIX
616c80476e4SDavid E. O'Brien EXTERN time_t time0; /* time at which shell started */
617c80476e4SDavid E. O'Brien # else /* POSIX */
618c80476e4SDavid E. O'Brien EXTERN clock_t time0; /* time at which shell started */
619c80476e4SDavid E. O'Brien EXTERN clock_t clk_tck;
620c80476e4SDavid E. O'Brien # endif /* POSIX */
621c80476e4SDavid E. O'Brien EXTERN struct tms shtimes; /* shell and child times for process timing */
622c80476e4SDavid E. O'Brien # endif /* _SEQUENT_ */
62345e5710bSMark Peek EXTERN time_t seconds0;
624c80476e4SDavid E. O'Brien #endif /* BSDTIMES */
625c80476e4SDavid E. O'Brien
626c80476e4SDavid E. O'Brien #ifndef HZ
627c80476e4SDavid E. O'Brien # define HZ 100 /* for division into seconds */
628c80476e4SDavid E. O'Brien #endif
629c80476e4SDavid E. O'Brien
630c80476e4SDavid E. O'Brien /*
631c80476e4SDavid E. O'Brien * Miscellany
632c80476e4SDavid E. O'Brien */
63319d2e3deSDmitry Chagin EXTERN pid_t mainpid; /* pid of the main shell ($$) */
634c80476e4SDavid E. O'Brien EXTERN Char *doldol; /* Character pid for $$ */
63545e5710bSMark Peek EXTERN pid_t backpid; /* pid of the last background job */
636c80476e4SDavid E. O'Brien
63719d2e3deSDmitry Chagin
638c80476e4SDavid E. O'Brien /*
639c80476e4SDavid E. O'Brien * Ideally these should be uid_t, gid_t, pid_t. I cannot do that right now
640c80476e4SDavid E. O'Brien * cause pid's could be unsigned and that would break our -1 flag, and
641c80476e4SDavid E. O'Brien * uid_t and gid_t are not defined in all the systems so I would have to
642c80476e4SDavid E. O'Brien * make special cases for them. In the future...
643c80476e4SDavid E. O'Brien */
64423338178SMark Peek EXTERN uid_t uid, euid; /* Invokers real and effective */
64523338178SMark Peek EXTERN gid_t gid, egid; /* User and group ids */
64623338178SMark Peek EXTERN pid_t opgrp, /* Initial pgrp and tty pgrp */
647c80476e4SDavid E. O'Brien shpgrp, /* Pgrp of shell */
648c80476e4SDavid E. O'Brien tpgrp; /* Terminal process group */
649c80476e4SDavid E. O'Brien /* If tpgrp is -1, leave tty alone! */
650c80476e4SDavid E. O'Brien
65145e5710bSMark Peek EXTERN Char *Prompt; /* The actual printed prompt or NULL */
65245e5710bSMark Peek EXTERN Char *RPrompt; /* Right-hand side prompt or NULL */
653c80476e4SDavid E. O'Brien
654c80476e4SDavid E. O'Brien /*
655c80476e4SDavid E. O'Brien * To be able to redirect i/o for builtins easily, the shell moves the i/o
656c80476e4SDavid E. O'Brien * descriptors it uses away from 0,1,2.
657c80476e4SDavid E. O'Brien * Ideally these should be in units which are closed across exec's
658c80476e4SDavid E. O'Brien * (this saves work) but for version 6, this is not usually possible.
659c80476e4SDavid E. O'Brien * The desired initial values for these descriptors are defined in
660c80476e4SDavid E. O'Brien * sh.local.h.
661c80476e4SDavid E. O'Brien */
662c80476e4SDavid E. O'Brien EXTERN int SHIN IZERO; /* Current shell input (script) */
663c80476e4SDavid E. O'Brien EXTERN int SHOUT IZERO; /* Shell output */
664c80476e4SDavid E. O'Brien EXTERN int SHDIAG IZERO; /* Diagnostic output... shell errs go here */
665c80476e4SDavid E. O'Brien EXTERN int OLDSTD IZERO; /* Old standard input (def for cmds) */
666c80476e4SDavid E. O'Brien
667c80476e4SDavid E. O'Brien
6689ccc37e3SMark Peek #if (SYSVREL == 4 && defined(_UTS)) || defined(__linux__)
669c80476e4SDavid E. O'Brien /*
670c80476e4SDavid E. O'Brien * From: fadden@uts.amdahl.com (Andy McFadden)
671c80476e4SDavid E. O'Brien * we need sigsetjmp for UTS4, but not UTS2.1
672c80476e4SDavid E. O'Brien */
673c80476e4SDavid E. O'Brien # define SIGSETJMP
674c80476e4SDavid E. O'Brien #endif
675c80476e4SDavid E. O'Brien
676c80476e4SDavid E. O'Brien /*
677c80476e4SDavid E. O'Brien * Error control
678c80476e4SDavid E. O'Brien *
679c80476e4SDavid E. O'Brien * Errors in scanning and parsing set up an error message to be printed
680c80476e4SDavid E. O'Brien * at the end and complete. Other errors always cause a reset.
681c80476e4SDavid E. O'Brien * Because of source commands and .cshrc we need nested error catches.
682c80476e4SDavid E. O'Brien */
683c80476e4SDavid E. O'Brien
684c80476e4SDavid E. O'Brien #ifdef SIGSETJMP
685d803a9d0SBrooks Davis typedef struct { const char *f; size_t l; sigjmp_buf j; } jmp_buf_t;
686d803a9d0SBrooks Davis # define tcsh_setjmp() sigsetjmp(reslab.j, 1)
687d803a9d0SBrooks Davis # define tcsh_longjmp() siglongjmp(reslab.j, 1)
688c80476e4SDavid E. O'Brien #else
689d803a9d0SBrooks Davis typedef struct { const char *f; size_t l; jmp_buf j; } jmp_buf_t;
690d803a9d0SBrooks Davis # define tcsh_setjmp() setjmp(reslab.j)
691d803a9d0SBrooks Davis # define tcsh_longjmp() longjmp(reslab.j, 1)
692d803a9d0SBrooks Davis #endif
693d803a9d0SBrooks Davis
694d803a9d0SBrooks Davis #define setexit() (reslab.f = __func__, \
695d803a9d0SBrooks Davis reslab.l = __LINE__, \
696d803a9d0SBrooks Davis tcsh_setjmp())
697d803a9d0SBrooks Davis #ifdef SETJMP_DEBUG
698d803a9d0SBrooks Davis # define _reset() xprintf("reset %s %zu\n", reslab.f, reslab.l), \
699d803a9d0SBrooks Davis flush(), tcsh_longjmp()
700d803a9d0SBrooks Davis #else
701d803a9d0SBrooks Davis # define _reset() tcsh_longjmp()
702c80476e4SDavid E. O'Brien #endif
703c80476e4SDavid E. O'Brien
704c80476e4SDavid E. O'Brien #define getexit(a) (void) ((a) = reslab)
705c80476e4SDavid E. O'Brien #define resexit(a) (void) (reslab = (a))
706c80476e4SDavid E. O'Brien
707c80476e4SDavid E. O'Brien #define cpybin(a, b) (void) ((a) = (b))
708c80476e4SDavid E. O'Brien
709c80476e4SDavid E. O'Brien extern jmp_buf_t reslab;
710c80476e4SDavid E. O'Brien
711c80476e4SDavid E. O'Brien EXTERN Char *gointr; /* Label for an onintr transfer */
712c80476e4SDavid E. O'Brien
71345e5710bSMark Peek extern struct sigaction parintr; /* Parents interrupt catch */
71445e5710bSMark Peek extern struct sigaction parterm; /* Parents terminate catch */
715c80476e4SDavid E. O'Brien
716c80476e4SDavid E. O'Brien /*
717c80476e4SDavid E. O'Brien * Lexical definitions.
718c80476e4SDavid E. O'Brien *
719c80476e4SDavid E. O'Brien * All lexical space is allocated dynamically.
720c80476e4SDavid E. O'Brien * The eighth/sixteenth bit of characters is used to prevent recognition,
721c80476e4SDavid E. O'Brien * and eventually stripped.
722c80476e4SDavid E. O'Brien */
723c80476e4SDavid E. O'Brien #define META 0200
724c80476e4SDavid E. O'Brien #define ASCII 0177
72523338178SMark Peek #ifdef WIDE_STRINGS /* Implies SHORT_STRINGS */
72623338178SMark Peek /* 31st char bit used for 'ing (not 32nd, we want all values nonnegative) */
72719d2e3deSDmitry Chagin /*
72819d2e3deSDmitry Chagin * Notice
72919d2e3deSDmitry Chagin *
73019d2e3deSDmitry Chagin * By fix for handling unicode name file, 32nd bit is used.
73119d2e3deSDmitry Chagin * We need use '&' instead of '> or <' when comparing with INVALID_BYTE etc..
73219d2e3deSDmitry Chagin * Cast to uChar is not recommended,
73319d2e3deSDmitry Chagin * becase Char is 4bytes but uChar is 8bytes on I32LP64. */
73419d2e3deSDmitry Chagin # define QUOTE 0x80000000
73519d2e3deSDmitry Chagin # define TRIM 0x7FFFFFFF /* Mask to strip quote bit */
736a15e6f9aSMark Peek # define UNDER 0x08000000 /* Underline flag */
737a15e6f9aSMark Peek # define BOLD 0x04000000 /* Bold flag */
738a15e6f9aSMark Peek # define STANDOUT 0x02000000 /* Standout flag */
739a15e6f9aSMark Peek # define LITERAL 0x01000000 /* Literal character flag */
740a15e6f9aSMark Peek # define ATTRIBUTES 0x0F000000 /* The bits used for attributes */
74119d2e3deSDmitry Chagin # define INVALID_BYTE 0xF0000000 /* Invalid character on input */
742a15e6f9aSMark Peek # ifdef SOLARIS2
743a15e6f9aSMark Peek # define CHAR 0x30FFFFFF /* Mask to mask out the character */
744a15e6f9aSMark Peek # else
745a15e6f9aSMark Peek # define CHAR 0x00FFFFFF /* Mask to mask out the character */
746a15e6f9aSMark Peek # endif
74723338178SMark Peek #elif defined (SHORT_STRINGS)
748c80476e4SDavid E. O'Brien # define QUOTE ((Char) 0100000)/* 16nth char bit used for 'ing */
74923338178SMark Peek # define TRIM 0073777 /* Mask to strip quote/lit bit */
750c80476e4SDavid E. O'Brien # define UNDER 0040000 /* Underline flag */
751c80476e4SDavid E. O'Brien # define BOLD 0020000 /* Bold flag */
752c80476e4SDavid E. O'Brien # define STANDOUT 0010000 /* Standout flag */
753c80476e4SDavid E. O'Brien # define LITERAL 0004000 /* Literal character flag */
754c80476e4SDavid E. O'Brien # define ATTRIBUTES 0074000 /* The bits used for attributes */
75523338178SMark Peek # define INVALID_BYTE 0
756c80476e4SDavid E. O'Brien # define CHAR 0000377 /* Mask to mask out the character */
757c80476e4SDavid E. O'Brien #else
758c80476e4SDavid E. O'Brien # define QUOTE ((Char) 0200) /* Eighth char bit used for 'ing */
759c80476e4SDavid E. O'Brien # define TRIM 0177 /* Mask to strip quote bit */
760c80476e4SDavid E. O'Brien # define UNDER 0000000 /* No extra bits to do both */
761c80476e4SDavid E. O'Brien # define BOLD 0000000 /* Bold flag */
762c80476e4SDavid E. O'Brien # define STANDOUT META /* Standout flag */
763c80476e4SDavid E. O'Brien # define LITERAL 0000000 /* Literal character flag */
764c80476e4SDavid E. O'Brien # define ATTRIBUTES 0200 /* The bits used for attributes */
76523338178SMark Peek # define INVALID_BYTE 0
766c80476e4SDavid E. O'Brien # define CHAR 0000177 /* Mask to mask out the character */
767c80476e4SDavid E. O'Brien #endif
76823338178SMark Peek #define CHAR_DBWIDTH (LITERAL|(LITERAL-1))
769c80476e4SDavid E. O'Brien
77019d2e3deSDmitry Chagin # define MAX_UTF32 0x7FFFFFFF /* max UTF32 is U+7FFFFFFF */
77119d2e3deSDmitry Chagin
772c80476e4SDavid E. O'Brien EXTERN int AsciiOnly; /* If set only 7 bits expected in characters */
773c80476e4SDavid E. O'Brien
774c80476e4SDavid E. O'Brien /*
775c80476e4SDavid E. O'Brien * Each level of input has a buffered input structure.
776c80476e4SDavid E. O'Brien * There are one or more blocks of buffered input for each level,
777c80476e4SDavid E. O'Brien * exactly one if the input is seekable and tell is available.
778c80476e4SDavid E. O'Brien * In other cases, the shell buffers enough blocks to keep all loops
779c80476e4SDavid E. O'Brien * in the buffer.
78045e5710bSMark Peek *
78145e5710bSMark Peek * If (WIDE_STRINGS && cantell), fbobp is always a byte offset, but
78245e5710bSMark Peek * (fseekp - fbobp) and (feobp - fbobp) are character offsets (usable for
78345e5710bSMark Peek * fbuf indexing).
78445e5710bSMark Peek *
78545e5710bSMark Peek * If (!cantell), all offsets are character offsets; if (!WIDE_STRINGS), there
78645e5710bSMark Peek * is no difference between byte and character offsets.
787c80476e4SDavid E. O'Brien */
788c80476e4SDavid E. O'Brien EXTERN struct Bin {
78923338178SMark Peek off_t Bfseekp; /* Seek pointer, generally != lseek() value */
790c80476e4SDavid E. O'Brien off_t Bfbobp; /* Seekp of beginning of buffers */
791c80476e4SDavid E. O'Brien off_t Bfeobp; /* Seekp of end of buffers */
792c80476e4SDavid E. O'Brien int Bfblocks; /* Number of buffer blocks */
793c80476e4SDavid E. O'Brien Char **Bfbuf; /* The array of buffer blocks */
79423338178SMark Peek #ifdef WIDE_STRINGS
79523338178SMark Peek /* Number of bytes in each character if (cantell) */
79623338178SMark Peek unsigned char Bfclens[BUFSIZE + 1];
79723338178SMark Peek #endif
798c80476e4SDavid E. O'Brien } B;
799c80476e4SDavid E. O'Brien
800c80476e4SDavid E. O'Brien /*
801c80476e4SDavid E. O'Brien * This structure allows us to seek inside aliases
802c80476e4SDavid E. O'Brien */
803c80476e4SDavid E. O'Brien struct Ain {
804c80476e4SDavid E. O'Brien int type;
8053b6eaa7bSAndrey A. Chernov #define TCSH_I_SEEK 0 /* Invalid seek */
8063b6eaa7bSAndrey A. Chernov #define TCSH_A_SEEK 1 /* Alias seek */
8073b6eaa7bSAndrey A. Chernov #define TCSH_F_SEEK 2 /* File seek */
8083b6eaa7bSAndrey A. Chernov #define TCSH_E_SEEK 3 /* Eval seek */
809c80476e4SDavid E. O'Brien union {
81045e5710bSMark Peek off_t _f_seek; /* A byte offset if (cantell) */
811c80476e4SDavid E. O'Brien Char* _c_seek;
812c80476e4SDavid E. O'Brien } fc;
813c80476e4SDavid E. O'Brien #define f_seek fc._f_seek
814c80476e4SDavid E. O'Brien #define c_seek fc._c_seek
815c80476e4SDavid E. O'Brien Char **a_seek;
816c80476e4SDavid E. O'Brien } ;
817c80476e4SDavid E. O'Brien
818c80476e4SDavid E. O'Brien extern int aret; /* Type of last char returned */
819c80476e4SDavid E. O'Brien #define SEEKEQ(a, b) ((a)->type == (b)->type && \
820c80476e4SDavid E. O'Brien (a)->f_seek == (b)->f_seek && \
821c80476e4SDavid E. O'Brien (a)->a_seek == (b)->a_seek)
822c80476e4SDavid E. O'Brien
823c80476e4SDavid E. O'Brien #define fseekp B.Bfseekp
824c80476e4SDavid E. O'Brien #define fbobp B.Bfbobp
825c80476e4SDavid E. O'Brien #define feobp B.Bfeobp
826c80476e4SDavid E. O'Brien #define fblocks B.Bfblocks
827c80476e4SDavid E. O'Brien #define fbuf B.Bfbuf
82823338178SMark Peek #define fclens B.Bfclens
829c80476e4SDavid E. O'Brien
830c80476e4SDavid E. O'Brien /*
831c80476e4SDavid E. O'Brien * The shell finds commands in loops by reseeking the input
832c80476e4SDavid E. O'Brien * For whiles, in particular, it reseeks to the beginning of the
833c80476e4SDavid E. O'Brien * line the while was on; hence the while placement restrictions.
834c80476e4SDavid E. O'Brien */
835c80476e4SDavid E. O'Brien EXTERN struct Ain lineloc;
836c80476e4SDavid E. O'Brien
83723338178SMark Peek EXTERN int cantell; /* Is current source tellable ? */
838c80476e4SDavid E. O'Brien
839c80476e4SDavid E. O'Brien /*
840c80476e4SDavid E. O'Brien * Input lines are parsed into doubly linked circular
841c80476e4SDavid E. O'Brien * lists of words of the following form.
842c80476e4SDavid E. O'Brien */
843c80476e4SDavid E. O'Brien struct wordent {
844c80476e4SDavid E. O'Brien Char *word;
845c80476e4SDavid E. O'Brien struct wordent *prev;
846c80476e4SDavid E. O'Brien struct wordent *next;
847c80476e4SDavid E. O'Brien };
848c80476e4SDavid E. O'Brien
849c80476e4SDavid E. O'Brien /*
850c80476e4SDavid E. O'Brien * During word building, both in the initial lexical phase and
851c80476e4SDavid E. O'Brien * when expanding $ variable substitutions, expansion by `!' and `$'
852c80476e4SDavid E. O'Brien * must be inhibited when reading ahead in routines which are themselves
853c80476e4SDavid E. O'Brien * processing `!' and `$' expansion or after characters such as `\' or in
854c80476e4SDavid E. O'Brien * quotations. The following flags are passed to the getC routines
855c80476e4SDavid E. O'Brien * telling them which of these substitutions are appropriate for the
856c80476e4SDavid E. O'Brien * next character to be returned.
857c80476e4SDavid E. O'Brien */
858c80476e4SDavid E. O'Brien #define DODOL 1
859c80476e4SDavid E. O'Brien #define DOEXCL 2
860c80476e4SDavid E. O'Brien #define DOALL DODOL|DOEXCL
861c80476e4SDavid E. O'Brien
862c80476e4SDavid E. O'Brien /*
863c80476e4SDavid E. O'Brien * Labuf implements a general buffer for lookahead during lexical operations.
864c80476e4SDavid E. O'Brien * Text which is to be placed in the input stream can be stuck here.
865c80476e4SDavid E. O'Brien * We stick parsed ahead $ constructs during initial input,
866c80476e4SDavid E. O'Brien * process id's from `$$', and modified variable values (from qualifiers
867c80476e4SDavid E. O'Brien * during expansion in sh.dol.c) here.
868c80476e4SDavid E. O'Brien */
86945e5710bSMark Peek extern struct Strbuf labuf;
87045e5710bSMark Peek EXTERN size_t lap; /* N/A if == labuf.len, index into labuf.s otherwise */
871c80476e4SDavid E. O'Brien
872c80476e4SDavid E. O'Brien /*
873c80476e4SDavid E. O'Brien * Parser structure
874c80476e4SDavid E. O'Brien *
875c80476e4SDavid E. O'Brien * Each command is parsed to a tree of command structures and
876c80476e4SDavid E. O'Brien * flags are set bottom up during this process, to be propagated down
877c80476e4SDavid E. O'Brien * as needed during the semantics/exeuction pass (sh.sem.c).
878c80476e4SDavid E. O'Brien */
879c80476e4SDavid E. O'Brien struct command {
880c80476e4SDavid E. O'Brien unsigned char t_dtyp; /* Type of node */
881c80476e4SDavid E. O'Brien #define NODE_COMMAND 1 /* t_dcom <t_dlef >t_drit */
882c80476e4SDavid E. O'Brien #define NODE_PAREN 2 /* ( t_dspr ) <t_dlef >t_drit */
883c80476e4SDavid E. O'Brien #define NODE_PIPE 3 /* t_dlef | t_drit */
884c80476e4SDavid E. O'Brien #define NODE_LIST 4 /* t_dlef ; t_drit */
885c80476e4SDavid E. O'Brien #define NODE_OR 5 /* t_dlef || t_drit */
886c80476e4SDavid E. O'Brien #define NODE_AND 6 /* t_dlef && t_drit */
887c80476e4SDavid E. O'Brien unsigned char t_nice; /* Nice value */
888c80476e4SDavid E. O'Brien #ifdef apollo
889c80476e4SDavid E. O'Brien unsigned char t_systype; /* System environment */
890c80476e4SDavid E. O'Brien #endif
891c80476e4SDavid E. O'Brien unsigned long t_dflg; /* Flags, e.g. F_AMPERSAND|... */
892c80476e4SDavid E. O'Brien /* save these when re-doing */
893c80476e4SDavid E. O'Brien #ifndef apollo
894c80476e4SDavid E. O'Brien #define F_SAVE (F_NICE|F_TIME|F_NOHUP|F_HUP)
895c80476e4SDavid E. O'Brien #else
896c80476e4SDavid E. O'Brien #define F_SAVE (F_NICE|F_TIME|F_NOHUP||F_HUP|F_VER)
897c80476e4SDavid E. O'Brien #endif
898c80476e4SDavid E. O'Brien #define F_AMPERSAND (1<<0) /* executes in background */
899c80476e4SDavid E. O'Brien #define F_APPEND (1<<1) /* output is redirected >> */
900c80476e4SDavid E. O'Brien #define F_PIPEIN (1<<2) /* input is a pipe */
901c80476e4SDavid E. O'Brien #define F_PIPEOUT (1<<3) /* output is a pipe */
902c80476e4SDavid E. O'Brien #define F_NOFORK (1<<4) /* don't fork, last ()ized cmd */
903c80476e4SDavid E. O'Brien #define F_NOINTERRUPT (1<<5) /* should be immune from intr's */
904c80476e4SDavid E. O'Brien /* spare */
905c80476e4SDavid E. O'Brien #define F_STDERR (1<<7) /* redirect unit 2 with unit 1 */
906c80476e4SDavid E. O'Brien #define F_OVERWRITE (1<<8) /* output was ! */
907c80476e4SDavid E. O'Brien #define F_READ (1<<9) /* input redirection is << */
908c80476e4SDavid E. O'Brien #define F_REPEAT (1<<10) /* reexec aft if, repeat,... */
909c80476e4SDavid E. O'Brien #define F_NICE (1<<11) /* t_nice is meaningful */
910c80476e4SDavid E. O'Brien #define F_NOHUP (1<<12) /* nohup this command */
911c80476e4SDavid E. O'Brien #define F_TIME (1<<13) /* time this command */
912c80476e4SDavid E. O'Brien #define F_BACKQ (1<<14) /* command is in `` */
913c80476e4SDavid E. O'Brien #define F_HUP (1<<15) /* hup this command */
914c80476e4SDavid E. O'Brien #ifdef apollo
915c80476e4SDavid E. O'Brien #define F_VER (1<<16) /* execute command under SYSTYPE */
916c80476e4SDavid E. O'Brien #endif
917c80476e4SDavid E. O'Brien union {
918c80476e4SDavid E. O'Brien Char *T_dlef; /* Input redirect word */
919c80476e4SDavid E. O'Brien struct command *T_dcar; /* Left part of list/pipe */
920c80476e4SDavid E. O'Brien } L;
921c80476e4SDavid E. O'Brien union {
922c80476e4SDavid E. O'Brien Char *T_drit; /* Output redirect word */
923c80476e4SDavid E. O'Brien struct command *T_dcdr; /* Right part of list/pipe */
924c80476e4SDavid E. O'Brien } R;
925c80476e4SDavid E. O'Brien #define t_dlef L.T_dlef
926c80476e4SDavid E. O'Brien #define t_dcar L.T_dcar
927c80476e4SDavid E. O'Brien #define t_drit R.T_drit
928c80476e4SDavid E. O'Brien #define t_dcdr R.T_dcdr
929c80476e4SDavid E. O'Brien Char **t_dcom; /* Command/argument vector */
930c80476e4SDavid E. O'Brien struct command *t_dspr; /* Pointer to ()'d subtree */
931c80476e4SDavid E. O'Brien };
932c80476e4SDavid E. O'Brien
933c80476e4SDavid E. O'Brien
934c80476e4SDavid E. O'Brien /*
935c80476e4SDavid E. O'Brien * The keywords for the parser
936c80476e4SDavid E. O'Brien */
937c80476e4SDavid E. O'Brien #define TC_BREAK 0
938c80476e4SDavid E. O'Brien #define TC_BRKSW 1
939c80476e4SDavid E. O'Brien #define TC_CASE 2
940c80476e4SDavid E. O'Brien #define TC_DEFAULT 3
941c80476e4SDavid E. O'Brien #define TC_ELSE 4
942c80476e4SDavid E. O'Brien #define TC_END 5
943c80476e4SDavid E. O'Brien #define TC_ENDIF 6
944c80476e4SDavid E. O'Brien #define TC_ENDSW 7
945c80476e4SDavid E. O'Brien #define TC_EXIT 8
946c80476e4SDavid E. O'Brien #define TC_FOREACH 9
947c80476e4SDavid E. O'Brien #define TC_GOTO 10
948c80476e4SDavid E. O'Brien #define TC_IF 11
949c80476e4SDavid E. O'Brien #define TC_LABEL 12
950c80476e4SDavid E. O'Brien #define TC_LET 13
951c80476e4SDavid E. O'Brien #define TC_SET 14
952c80476e4SDavid E. O'Brien #define TC_SWITCH 15
953c80476e4SDavid E. O'Brien #define TC_TEST 16
954c80476e4SDavid E. O'Brien #define TC_THEN 17
955c80476e4SDavid E. O'Brien #define TC_WHILE 18
956c80476e4SDavid E. O'Brien
957c80476e4SDavid E. O'Brien /*
958c80476e4SDavid E. O'Brien * These are declared here because they want to be
959c80476e4SDavid E. O'Brien * initialized in sh.init.c (to allow them to be made readonly)
960c80476e4SDavid E. O'Brien */
961c80476e4SDavid E. O'Brien
962c80476e4SDavid E. O'Brien #if defined(hpux) && defined(__STDC__) && !defined(__GNUC__)
963c80476e4SDavid E. O'Brien /* Avoid hpux ansi mode spurious warnings */
964c80476e4SDavid E. O'Brien typedef void (*bfunc_t) ();
965c80476e4SDavid E. O'Brien #else
96645e5710bSMark Peek typedef void (*bfunc_t) (Char **, struct command *);
967c80476e4SDavid E. O'Brien #endif /* hpux && __STDC__ && !__GNUC__ */
968c80476e4SDavid E. O'Brien
96945e5710bSMark Peek extern const struct biltins {
97023338178SMark Peek const char *bname;
971c80476e4SDavid E. O'Brien bfunc_t bfunct;
972c80476e4SDavid E. O'Brien int minargs, maxargs;
973c80476e4SDavid E. O'Brien } bfunc[];
974c80476e4SDavid E. O'Brien extern int nbfunc;
9753b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
976c80476e4SDavid E. O'Brien extern struct biltins nt_bfunc[];
977c80476e4SDavid E. O'Brien extern int nt_nbfunc;
9783b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE*/
97923338178SMark Peek extern int bequiet;
980c80476e4SDavid E. O'Brien
981c80476e4SDavid E. O'Brien extern struct srch {
98223338178SMark Peek const char *s_name;
983c80476e4SDavid E. O'Brien int s_value;
984c80476e4SDavid E. O'Brien } srchn[];
985c80476e4SDavid E. O'Brien extern int nsrchn;
986c80476e4SDavid E. O'Brien
987c80476e4SDavid E. O'Brien /*
988c80476e4SDavid E. O'Brien * Structure defining the existing while/foreach loops at this
989c80476e4SDavid E. O'Brien * source level. Loops are implemented by seeking back in the
990c80476e4SDavid E. O'Brien * input. For foreach (fe), the word list is attached here.
991c80476e4SDavid E. O'Brien */
992c80476e4SDavid E. O'Brien EXTERN struct whyle {
993c80476e4SDavid E. O'Brien struct Ain w_start; /* Point to restart loop */
994c80476e4SDavid E. O'Brien struct Ain w_end; /* End of loop (0 if unknown) */
995c80476e4SDavid E. O'Brien Char **w_fe, **w_fe0; /* Current/initial wordlist for fe */
996c80476e4SDavid E. O'Brien Char *w_fename; /* Name for fe */
997c80476e4SDavid E. O'Brien struct whyle *w_next; /* Next (more outer) loop */
998c80476e4SDavid E. O'Brien } *whyles;
999c80476e4SDavid E. O'Brien
1000c80476e4SDavid E. O'Brien /*
1001c80476e4SDavid E. O'Brien * Variable structure
1002c80476e4SDavid E. O'Brien *
1003c80476e4SDavid E. O'Brien * Aliases and variables are stored in AVL balanced binary trees.
1004c80476e4SDavid E. O'Brien */
1005c80476e4SDavid E. O'Brien EXTERN struct varent {
1006c80476e4SDavid E. O'Brien Char **vec; /* Array of words which is the value */
1007c80476e4SDavid E. O'Brien Char *v_name; /* Name of variable/alias */
1008c80476e4SDavid E. O'Brien int v_flags; /* Flags */
1009c80476e4SDavid E. O'Brien #define VAR_ALL -1
1010c80476e4SDavid E. O'Brien #define VAR_READONLY 1
1011c80476e4SDavid E. O'Brien #define VAR_READWRITE 2
1012c80476e4SDavid E. O'Brien #define VAR_NOGLOB 4
1013c80476e4SDavid E. O'Brien #define VAR_FIRST 32
1014c80476e4SDavid E. O'Brien #define VAR_LAST 64
1015c80476e4SDavid E. O'Brien struct varent *v_link[3]; /* The links, see below */
1016c80476e4SDavid E. O'Brien int v_bal; /* Balance factor */
1017c80476e4SDavid E. O'Brien } shvhed IZERO_STRUCT, aliases IZERO_STRUCT;
1018c80476e4SDavid E. O'Brien
1019c80476e4SDavid E. O'Brien #define v_left v_link[0]
1020c80476e4SDavid E. O'Brien #define v_right v_link[1]
1021c80476e4SDavid E. O'Brien #define v_parent v_link[2]
1022c80476e4SDavid E. O'Brien
1023c80476e4SDavid E. O'Brien #define adrof(v) adrof1(v, &shvhed)
1024c80476e4SDavid E. O'Brien #define varval(v) value1(v, &shvhed)
1025c80476e4SDavid E. O'Brien
1026c80476e4SDavid E. O'Brien /*
1027c80476e4SDavid E. O'Brien * The following are for interfacing redo substitution in
1028c80476e4SDavid E. O'Brien * aliases to the lexical routines.
1029c80476e4SDavid E. O'Brien */
1030c80476e4SDavid E. O'Brien EXTERN struct wordent *alhistp IZERO_STRUCT;/* Argument list (first) */
1031c80476e4SDavid E. O'Brien EXTERN struct wordent *alhistt IZERO_STRUCT;/* Node after last in arg list */
1032c80476e4SDavid E. O'Brien EXTERN Char **alvec IZERO_STRUCT,
1033c80476e4SDavid E. O'Brien *alvecp IZERO_STRUCT;/* The (remnants of) alias vector */
1034c80476e4SDavid E. O'Brien
1035c80476e4SDavid E. O'Brien /*
1036c80476e4SDavid E. O'Brien * Filename/command name expansion variables
1037c80476e4SDavid E. O'Brien */
1038c80476e4SDavid E. O'Brien
1039c80476e4SDavid E. O'Brien #ifndef MAXPATHLEN
104045e5710bSMark Peek # ifdef PATH_MAX
104145e5710bSMark Peek # define MAXPATHLEN PATH_MAX
104245e5710bSMark Peek # else
1043c80476e4SDavid E. O'Brien # define MAXPATHLEN 2048
104445e5710bSMark Peek # endif
1045c80476e4SDavid E. O'Brien #endif /* MAXPATHLEN */
1046c80476e4SDavid E. O'Brien
1047c80476e4SDavid E. O'Brien #ifndef HAVENOLIMIT
1048c80476e4SDavid E. O'Brien /*
1049c80476e4SDavid E. O'Brien * resource limits
1050c80476e4SDavid E. O'Brien */
1051c80476e4SDavid E. O'Brien extern struct limits {
1052c80476e4SDavid E. O'Brien int limconst;
105323338178SMark Peek const char *limname;
1054c80476e4SDavid E. O'Brien int limdiv;
105523338178SMark Peek const char *limscale;
1056c80476e4SDavid E. O'Brien } limits[];
1057c80476e4SDavid E. O'Brien #endif /* !HAVENOLIMIT */
1058c80476e4SDavid E. O'Brien
1059c80476e4SDavid E. O'Brien /*
1060c80476e4SDavid E. O'Brien * History list
1061c80476e4SDavid E. O'Brien *
1062c80476e4SDavid E. O'Brien * Each history list entry contains an embedded wordlist
1063c80476e4SDavid E. O'Brien * from the scanner, a number for the event, and a reference count
1064c80476e4SDavid E. O'Brien * to aid in discarding old entries.
1065c80476e4SDavid E. O'Brien *
1066c80476e4SDavid E. O'Brien * Essentially "invisible" entries are put on the history list
1067c80476e4SDavid E. O'Brien * when history substitution includes modifiers, and thrown away
1068c80476e4SDavid E. O'Brien * at the next discarding since their event numbers are very negative.
1069c80476e4SDavid E. O'Brien */
1070c80476e4SDavid E. O'Brien EXTERN struct Hist {
1071c80476e4SDavid E. O'Brien struct wordent Hlex;
10729ccc37e3SMark Peek int Hnum; /* eventno when inserted into history list */
1073c80476e4SDavid E. O'Brien int Href;
1074c80476e4SDavid E. O'Brien time_t Htime;
1075c80476e4SDavid E. O'Brien Char *histline;
10769ccc37e3SMark Peek struct Hist *Hnext, *Hprev; /* doubly linked list */
10779ccc37e3SMark Peek unsigned Hhash; /* hash value of command line */
1078c80476e4SDavid E. O'Brien } Histlist IZERO_STRUCT;
1079c80476e4SDavid E. O'Brien
108019d2e3deSDmitry Chagin extern struct wordent paraml; /* Current lexical word list */
1081c80476e4SDavid E. O'Brien EXTERN int eventno; /* Next events number */
1082c80476e4SDavid E. O'Brien EXTERN int lastev; /* Last event reference (default) */
1083c80476e4SDavid E. O'Brien
1084c80476e4SDavid E. O'Brien EXTERN Char HIST; /* history invocation character */
1085c80476e4SDavid E. O'Brien EXTERN Char HISTSUB; /* auto-substitute character */
1086c80476e4SDavid E. O'Brien EXTERN Char PRCH; /* Prompt symbol for regular users */
1087c80476e4SDavid E. O'Brien EXTERN Char PRCHROOT; /* Prompt symbol for root */
1088c80476e4SDavid E. O'Brien
1089c80476e4SDavid E. O'Brien /*
1090c80476e4SDavid E. O'Brien * For operating systems with single case filenames (OS/2)
1091c80476e4SDavid E. O'Brien */
1092c80476e4SDavid E. O'Brien #ifdef CASE_INSENSITIVE
109323338178SMark Peek # ifdef WIDE_STRINGS
109423338178SMark Peek # define samecase(x) (towlower(x))
109523338178SMark Peek # else
1096c80476e4SDavid E. O'Brien # define samecase(x) (isupper((unsigned char)(x)) ? \
1097c80476e4SDavid E. O'Brien tolower((unsigned char)(x)) : (x))
109823338178SMark Peek # endif
1099c80476e4SDavid E. O'Brien #else
1100c80476e4SDavid E. O'Brien # define samecase(x) (x)
1101c80476e4SDavid E. O'Brien #endif /* CASE_INSENSITIVE */
1102c80476e4SDavid E. O'Brien
1103c80476e4SDavid E. O'Brien /*
1104c80476e4SDavid E. O'Brien * strings.h:
1105c80476e4SDavid E. O'Brien */
1106c80476e4SDavid E. O'Brien #ifndef SHORT_STRINGS
1107c80476e4SDavid E. O'Brien #define Strchr(a, b) strchr(a, b)
1108c80476e4SDavid E. O'Brien #define Strrchr(a, b) strrchr(a, b)
1109c80476e4SDavid E. O'Brien #define Strcat(a, b) strcat(a, b)
1110c80476e4SDavid E. O'Brien #define Strncat(a, b, c) strncat(a, b, c)
1111c80476e4SDavid E. O'Brien #define Strcpy(a, b) strcpy(a, b)
1112c80476e4SDavid E. O'Brien #define Strncpy(a, b, c) strncpy(a, b, c)
1113c80476e4SDavid E. O'Brien #define Strlen(a) strlen(a)
1114c80476e4SDavid E. O'Brien #define Strcmp(a, b) strcmp(a, b)
1115c80476e4SDavid E. O'Brien #define Strncmp(a, b, c) strncmp(a, b, c)
1116b2d5d167SMark Peek #define Strcasecmp(a, b) strcasecmp(a, b)
1117c80476e4SDavid E. O'Brien
1118c80476e4SDavid E. O'Brien #define Strspl(a, b) strspl(a, b)
111945e5710bSMark Peek #define Strnsave(a, b) strnsave(a, b)
1120c80476e4SDavid E. O'Brien #define Strsave(a) strsave(a)
1121c80476e4SDavid E. O'Brien #define Strend(a) strend(a)
1122c80476e4SDavid E. O'Brien #define Strstr(a, b) strstr(a, b)
1123c80476e4SDavid E. O'Brien
1124c80476e4SDavid E. O'Brien #define str2short(a) (a)
1125c80476e4SDavid E. O'Brien #define blk2short(a) saveblk(a)
1126c80476e4SDavid E. O'Brien #define short2blk(a) saveblk(a)
112745e5710bSMark Peek #define short2str(a) caching_strip(a)
1128c80476e4SDavid E. O'Brien #else
11299ccc37e3SMark Peek #if defined(WIDE_STRINGS) && !defined(UTF16_STRINGS)
113023338178SMark Peek #define Strchr(a, b) wcschr(a, b)
113123338178SMark Peek #define Strrchr(a, b) wcsrchr(a, b)
113223338178SMark Peek #define Strcat(a, b) wcscat(a, b)
113323338178SMark Peek #define Strncat(a, b, c) wcsncat(a, b, c)
113423338178SMark Peek #define Strcpy(a, b) wcscpy(a, b)
113523338178SMark Peek #define Strncpy(a, b, c) wcsncpy(a, b, c)
113623338178SMark Peek #define Strlen(a) wcslen(a)
113723338178SMark Peek #define Strcmp(a, b) wcscmp(a, b)
113823338178SMark Peek #define Strncmp(a, b, c) wcsncmp(a, b, c)
113923338178SMark Peek #else
1140c80476e4SDavid E. O'Brien #define Strchr(a, b) s_strchr(a, b)
1141c80476e4SDavid E. O'Brien #define Strrchr(a, b) s_strrchr(a, b)
1142c80476e4SDavid E. O'Brien #define Strcat(a, b) s_strcat(a, b)
1143c80476e4SDavid E. O'Brien #define Strncat(a, b, c) s_strncat(a, b, c)
1144c80476e4SDavid E. O'Brien #define Strcpy(a, b) s_strcpy(a, b)
1145c80476e4SDavid E. O'Brien #define Strncpy(a, b, c) s_strncpy(a, b, c)
1146c80476e4SDavid E. O'Brien #define Strlen(a) s_strlen(a)
1147c80476e4SDavid E. O'Brien #define Strcmp(a, b) s_strcmp(a, b)
1148c80476e4SDavid E. O'Brien #define Strncmp(a, b, c) s_strncmp(a, b, c)
114923338178SMark Peek #endif
1150b2d5d167SMark Peek #define Strcasecmp(a, b) s_strcasecmp(a, b)
1151c80476e4SDavid E. O'Brien
1152c80476e4SDavid E. O'Brien #define Strspl(a, b) s_strspl(a, b)
115345e5710bSMark Peek #define Strnsave(a, b) s_strnsave(a, b)
1154c80476e4SDavid E. O'Brien #define Strsave(a) s_strsave(a)
1155c80476e4SDavid E. O'Brien #define Strend(a) s_strend(a)
1156c80476e4SDavid E. O'Brien #define Strstr(a, b) s_strstr(a, b)
1157c80476e4SDavid E. O'Brien #endif
1158c80476e4SDavid E. O'Brien
1159*5224c2a3SDmitry Chagin #define TCSH_MODIFIERS "ehlqrstuxQ"
1160*5224c2a3SDmitry Chagin
1161c80476e4SDavid E. O'Brien /*
1162c80476e4SDavid E. O'Brien * setname is a macro to save space (see sh.err.c)
1163c80476e4SDavid E. O'Brien */
116423338178SMark Peek EXTERN const char *bname;
1165c80476e4SDavid E. O'Brien
1166c80476e4SDavid E. O'Brien #define setname(a) (bname = (a))
1167c80476e4SDavid E. O'Brien
1168c80476e4SDavid E. O'Brien #ifdef VFORK
1169c80476e4SDavid E. O'Brien EXTERN Char *Vsav;
1170c80476e4SDavid E. O'Brien EXTERN Char *Vdp;
1171c80476e4SDavid E. O'Brien EXTERN Char *Vexpath;
1172c80476e4SDavid E. O'Brien EXTERN char **Vt;
1173c80476e4SDavid E. O'Brien #endif /* VFORK */
1174c80476e4SDavid E. O'Brien
1175c80476e4SDavid E. O'Brien EXTERN Char **evalvec;
1176c80476e4SDavid E. O'Brien EXTERN Char *evalp;
1177c80476e4SDavid E. O'Brien
1178c80476e4SDavid E. O'Brien extern struct mesg {
117923338178SMark Peek const char *iname; /* name from /usr/include */
118023338178SMark Peek const char *pname; /* print name */
1181c80476e4SDavid E. O'Brien } mesg[];
1182c80476e4SDavid E. O'Brien
118319d2e3deSDmitry Chagin /* word_chars is set by default to WORD_CHARS (or WORD_CHARS_VI) but can
118419d2e3deSDmitry Chagin be overridden by the wordchars variable--if unset, reverts to
118519d2e3deSDmitry Chagin WORD_CHARS (or WORD_CHARS_VI) */
1186c80476e4SDavid E. O'Brien
1187c80476e4SDavid E. O'Brien EXTERN Char *word_chars;
1188c80476e4SDavid E. O'Brien
1189c80476e4SDavid E. O'Brien #define WORD_CHARS "*?_-.[]~=" /* default chars besides alnums in words */
119019d2e3deSDmitry Chagin #define WORD_CHARS_VI "_" /* default chars besides alnums in words */
1191c80476e4SDavid E. O'Brien
1192c80476e4SDavid E. O'Brien EXTERN Char *STR_SHELLPATH;
1193c80476e4SDavid E. O'Brien
1194c80476e4SDavid E. O'Brien #ifdef _PATH_BSHELL
1195c80476e4SDavid E. O'Brien EXTERN Char *STR_BSHELL;
1196c80476e4SDavid E. O'Brien #endif
1197c80476e4SDavid E. O'Brien EXTERN Char *STR_WORD_CHARS;
119819d2e3deSDmitry Chagin EXTERN Char *STR_WORD_CHARS_VI;
1199c80476e4SDavid E. O'Brien EXTERN Char **STR_environ IZERO;
1200c80476e4SDavid E. O'Brien
1201c80476e4SDavid E. O'Brien extern int dont_free; /* Tell free that we are in danger if we free */
1202c80476e4SDavid E. O'Brien
1203c80476e4SDavid E. O'Brien extern Char *INVPTR;
1204c80476e4SDavid E. O'Brien extern Char **INVPPTR;
1205c80476e4SDavid E. O'Brien
1206c80476e4SDavid E. O'Brien extern char *progname;
1207c80476e4SDavid E. O'Brien extern int tcsh;
120823338178SMark Peek extern int xlate_cr;
120923338178SMark Peek extern int output_raw;
121023338178SMark Peek extern int lbuffed;
121123338178SMark Peek extern time_t Htime;
121223338178SMark Peek extern int numeof;
121323338178SMark Peek extern int insource;
121423338178SMark Peek extern char linbuf[];
121523338178SMark Peek extern char *linp;
121623338178SMark Peek extern int nsig;
121723338178SMark Peek #ifdef VFORK
121823338178SMark Peek extern int use_fork;
121923338178SMark Peek #endif
122023338178SMark Peek extern int tellwhat;
122123338178SMark Peek extern int NoNLSRebind;
122245e5710bSMark Peek #if !HAVE_DECL_ENVIRON
122345e5710bSMark Peek extern char **environ;
122445e5710bSMark Peek #endif
1225c80476e4SDavid E. O'Brien
1226c80476e4SDavid E. O'Brien #include "tc.h"
1227c80476e4SDavid E. O'Brien
12283b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
1229c80476e4SDavid E. O'Brien # ifdef NLS_CATALOGS
12309ccc37e3SMark Peek # ifdef HAVE_FEATURES_H
1231c80476e4SDavid E. O'Brien # include <features.h>
1232c80476e4SDavid E. O'Brien # endif
12339ccc37e3SMark Peek # ifdef HAVE_NL_LANGINFO
12349ccc37e3SMark Peek # include <langinfo.h>
12359ccc37e3SMark Peek # endif
1236c80476e4SDavid E. O'Brien # ifdef __uxps__
1237c80476e4SDavid E. O'Brien # define gettxt gettxt_ds
1238c80476e4SDavid E. O'Brien # endif
1239c80476e4SDavid E. O'Brien # include <nl_types.h>
1240c80476e4SDavid E. O'Brien # ifdef __uxps__
1241c80476e4SDavid E. O'Brien # undef gettxt
1242c80476e4SDavid E. O'Brien # endif
1243c80476e4SDavid E. O'Brien EXTERN nl_catd catd;
124445e5710bSMark Peek # if defined(HAVE_ICONV) && defined(HAVE_NL_LANGINFO)
124523338178SMark Peek # define CGETS(b, c, d) iconv_catgets(catd, b, c, d)
124623338178SMark Peek # else
124745e5710bSMark Peek # define CGETS(b, c, d) xcatgets(catd, b, c, d)
124823338178SMark Peek # endif
1249c80476e4SDavid E. O'Brien # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
1250c80476e4SDavid E. O'Brien # else
1251c80476e4SDavid E. O'Brien # define CGETS(b, c, d) d
1252c80476e4SDavid E. O'Brien # define CSAVS(b, c, d) d
1253c80476e4SDavid E. O'Brien # endif
12543b6eaa7bSAndrey A. Chernov #else /* WINNT_NATIVE */
1255c80476e4SDavid E. O'Brien # define CGETS(b, c, d) nt_cgets( b, c, d)
1256c80476e4SDavid E. O'Brien # define CSAVS(b, c, d) strsave(CGETS(b, c, d))
12573b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1258c80476e4SDavid E. O'Brien
1259b2d5d167SMark Peek #if defined(FILEC)
126023338178SMark Peek extern int filec;
1261b2d5d167SMark Peek #endif /* FILEC */
1262b2d5d167SMark Peek
126323338178SMark Peek #include "sh.decls.h"
1264c80476e4SDavid E. O'Brien /*
1265c80476e4SDavid E. O'Brien * Since on some machines characters are unsigned, and the signed
1266c80476e4SDavid E. O'Brien * keyword is not universally implemented, we treat all characters
1267c80476e4SDavid E. O'Brien * as unsigned and sign extend them where we need.
1268c80476e4SDavid E. O'Brien */
1269c80476e4SDavid E. O'Brien #define SIGN_EXTEND_CHAR(a) (((a) & 0x80) ? ((a) | ~0x7f) : (a))
1270c80476e4SDavid E. O'Brien
127123338178SMark Peek /*
127223338178SMark Peek * explanation for use by the "--help" option
127323338178SMark Peek */
127423338178SMark Peek #define HELP_STRING "\
127523338178SMark Peek -b file batch mode, read and execute commands from `file' \n\
127623338178SMark Peek -c command run `command' from next argument \n\
127723338178SMark Peek -d load directory stack from `~/.cshdirs' \n\
127823338178SMark Peek -Dname[=value] define environment variable `name' to `value' (DomainOS only) \n\
127923338178SMark Peek -e exit on any error \n\
128023338178SMark Peek -f start faster by ignoring the start-up file \n\
128123338178SMark Peek -F use fork() instead of vfork() when spawning (ConvexOS only) \n\
128223338178SMark Peek -i interactive, even when input is not from a terminal \n\
128323338178SMark Peek -l act as a login shell, must be the only option specified \n\
128423338178SMark Peek -m load the start-up file, whether or not owned by effective user \n\
128523338178SMark Peek -n file no execute mode, just check syntax of the following `file' \n\
128623338178SMark Peek -q accept SIGQUIT for running under a debugger \n\
128723338178SMark Peek -s read commands from standard input \n\
128823338178SMark Peek -t read one line from standard input \n\
128923338178SMark Peek -v echo commands after history substitution \n\
129023338178SMark Peek -V like -v but including commands read from the start-up file \n\
129123338178SMark Peek -x echo commands immediately before execution \n\
129223338178SMark Peek -X like -x but including commands read from the start-up file \n\
129323338178SMark Peek --help print this message and exit \n\
129423338178SMark Peek --version print the version shell variable and exit \n\
129523338178SMark Peek \nSee the tcsh(1) manual page for detailed information.\n"
129623338178SMark Peek
129723338178SMark Peek #include "tc.nls.h"
129823338178SMark Peek
1299c80476e4SDavid E. O'Brien #endif /* _h_sh */
1300