1*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: sh.h,v 1.35 2015/09/10 22:48:58 nicm Exp $ */
2*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: shf.h,v 1.6 2005/12/11 18:53:51 deraadt Exp $ */
3*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: table.h,v 1.8 2012/02/19 07:52:30 otto Exp $ */
4*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */
5*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: expand.h,v 1.7 2015/09/01 13:12:31 tedu Exp $ */
6*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: lex.h,v 1.13 2013/03/03 19:11:34 guenther Exp $ */
7*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: proto.h,v 1.35 2013/09/04 15:49:19 millert Exp $ */
8*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
9*7c356e86SAndroid Build Coastguard Worker /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
10*7c356e86SAndroid Build Coastguard Worker
11*7c356e86SAndroid Build Coastguard Worker /*-
12*7c356e86SAndroid Build Coastguard Worker * Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
13*7c356e86SAndroid Build Coastguard Worker * 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
14*7c356e86SAndroid Build Coastguard Worker * 2019, 2020
15*7c356e86SAndroid Build Coastguard Worker * mirabilos <[email protected]>
16*7c356e86SAndroid Build Coastguard Worker *
17*7c356e86SAndroid Build Coastguard Worker * Provided that these terms and disclaimer and all copyright notices
18*7c356e86SAndroid Build Coastguard Worker * are retained or reproduced in an accompanying document, permission
19*7c356e86SAndroid Build Coastguard Worker * is granted to deal in this work without restriction, including un‐
20*7c356e86SAndroid Build Coastguard Worker * limited rights to use, publicly perform, distribute, sell, modify,
21*7c356e86SAndroid Build Coastguard Worker * merge, give away, or sublicence.
22*7c356e86SAndroid Build Coastguard Worker *
23*7c356e86SAndroid Build Coastguard Worker * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
24*7c356e86SAndroid Build Coastguard Worker * the utmost extent permitted by applicable law, neither express nor
25*7c356e86SAndroid Build Coastguard Worker * implied; without malicious intent or gross negligence. In no event
26*7c356e86SAndroid Build Coastguard Worker * may a licensor, author or contributor be held liable for indirect,
27*7c356e86SAndroid Build Coastguard Worker * direct, other damage, loss, or other issues arising in any way out
28*7c356e86SAndroid Build Coastguard Worker * of dealing in the work, even if advised of the possibility of such
29*7c356e86SAndroid Build Coastguard Worker * damage or existence of a defect, except proven that it results out
30*7c356e86SAndroid Build Coastguard Worker * of said person’s immediate fault when using the work as intended.
31*7c356e86SAndroid Build Coastguard Worker */
32*7c356e86SAndroid Build Coastguard Worker
33*7c356e86SAndroid Build Coastguard Worker #ifdef __dietlibc__
34*7c356e86SAndroid Build Coastguard Worker /* XXX imake style */
35*7c356e86SAndroid Build Coastguard Worker #define _BSD_SOURCE /* live, BSD, live❣ */
36*7c356e86SAndroid Build Coastguard Worker #endif
37*7c356e86SAndroid Build Coastguard Worker
38*7c356e86SAndroid Build Coastguard Worker #if HAVE_SYS_PARAM_H
39*7c356e86SAndroid Build Coastguard Worker #include <sys/param.h>
40*7c356e86SAndroid Build Coastguard Worker #endif
41*7c356e86SAndroid Build Coastguard Worker #include <sys/types.h>
42*7c356e86SAndroid Build Coastguard Worker #if HAVE_BOTH_TIME_H
43*7c356e86SAndroid Build Coastguard Worker #include <sys/time.h>
44*7c356e86SAndroid Build Coastguard Worker #include <time.h>
45*7c356e86SAndroid Build Coastguard Worker #elif HAVE_SYS_TIME_H
46*7c356e86SAndroid Build Coastguard Worker #include <sys/time.h>
47*7c356e86SAndroid Build Coastguard Worker #elif HAVE_TIME_H
48*7c356e86SAndroid Build Coastguard Worker #include <time.h>
49*7c356e86SAndroid Build Coastguard Worker #endif
50*7c356e86SAndroid Build Coastguard Worker #include <sys/ioctl.h>
51*7c356e86SAndroid Build Coastguard Worker #if HAVE_SYS_SYSMACROS_H
52*7c356e86SAndroid Build Coastguard Worker #include <sys/sysmacros.h>
53*7c356e86SAndroid Build Coastguard Worker #endif
54*7c356e86SAndroid Build Coastguard Worker #if HAVE_SYS_MKDEV_H
55*7c356e86SAndroid Build Coastguard Worker #include <sys/mkdev.h>
56*7c356e86SAndroid Build Coastguard Worker #endif
57*7c356e86SAndroid Build Coastguard Worker #if HAVE_SYS_MMAN_H
58*7c356e86SAndroid Build Coastguard Worker #include <sys/mman.h>
59*7c356e86SAndroid Build Coastguard Worker #endif
60*7c356e86SAndroid Build Coastguard Worker #if HAVE_SYS_RESOURCE_H
61*7c356e86SAndroid Build Coastguard Worker #include <sys/resource.h>
62*7c356e86SAndroid Build Coastguard Worker #endif
63*7c356e86SAndroid Build Coastguard Worker #include <sys/stat.h>
64*7c356e86SAndroid Build Coastguard Worker #include <sys/wait.h>
65*7c356e86SAndroid Build Coastguard Worker #include <dirent.h>
66*7c356e86SAndroid Build Coastguard Worker #include <errno.h>
67*7c356e86SAndroid Build Coastguard Worker #include <fcntl.h>
68*7c356e86SAndroid Build Coastguard Worker #if HAVE_IO_H
69*7c356e86SAndroid Build Coastguard Worker #include <io.h>
70*7c356e86SAndroid Build Coastguard Worker #endif
71*7c356e86SAndroid Build Coastguard Worker #if HAVE_LIBGEN_H
72*7c356e86SAndroid Build Coastguard Worker #include <libgen.h>
73*7c356e86SAndroid Build Coastguard Worker #endif
74*7c356e86SAndroid Build Coastguard Worker #if HAVE_LIBUTIL_H
75*7c356e86SAndroid Build Coastguard Worker #include <libutil.h>
76*7c356e86SAndroid Build Coastguard Worker #endif
77*7c356e86SAndroid Build Coastguard Worker #include <limits.h>
78*7c356e86SAndroid Build Coastguard Worker #if HAVE_PATHS_H
79*7c356e86SAndroid Build Coastguard Worker #include <paths.h>
80*7c356e86SAndroid Build Coastguard Worker #endif
81*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NOPWNAM
82*7c356e86SAndroid Build Coastguard Worker #include <pwd.h>
83*7c356e86SAndroid Build Coastguard Worker #endif
84*7c356e86SAndroid Build Coastguard Worker #include <setjmp.h>
85*7c356e86SAndroid Build Coastguard Worker #include <signal.h>
86*7c356e86SAndroid Build Coastguard Worker #include <stdarg.h>
87*7c356e86SAndroid Build Coastguard Worker #include <stddef.h>
88*7c356e86SAndroid Build Coastguard Worker #if HAVE_STDINT_H
89*7c356e86SAndroid Build Coastguard Worker #include <stdint.h>
90*7c356e86SAndroid Build Coastguard Worker #endif
91*7c356e86SAndroid Build Coastguard Worker #include <stdio.h>
92*7c356e86SAndroid Build Coastguard Worker #include <stdlib.h>
93*7c356e86SAndroid Build Coastguard Worker #include <string.h>
94*7c356e86SAndroid Build Coastguard Worker #if HAVE_STRINGS_H
95*7c356e86SAndroid Build Coastguard Worker #include <strings.h>
96*7c356e86SAndroid Build Coastguard Worker #endif
97*7c356e86SAndroid Build Coastguard Worker #if HAVE_TERMIOS_H
98*7c356e86SAndroid Build Coastguard Worker #include <termios.h>
99*7c356e86SAndroid Build Coastguard Worker #else
100*7c356e86SAndroid Build Coastguard Worker /* shudder… */
101*7c356e86SAndroid Build Coastguard Worker #include <termio.h>
102*7c356e86SAndroid Build Coastguard Worker #endif
103*7c356e86SAndroid Build Coastguard Worker #ifdef _ISC_UNIX
104*7c356e86SAndroid Build Coastguard Worker /* XXX imake style */
105*7c356e86SAndroid Build Coastguard Worker #include <sys/sioctl.h>
106*7c356e86SAndroid Build Coastguard Worker #endif
107*7c356e86SAndroid Build Coastguard Worker #if HAVE_ULIMIT_H
108*7c356e86SAndroid Build Coastguard Worker #include <ulimit.h>
109*7c356e86SAndroid Build Coastguard Worker #endif
110*7c356e86SAndroid Build Coastguard Worker #include <unistd.h>
111*7c356e86SAndroid Build Coastguard Worker #if HAVE_VALUES_H
112*7c356e86SAndroid Build Coastguard Worker #include <values.h>
113*7c356e86SAndroid Build Coastguard Worker #endif
114*7c356e86SAndroid Build Coastguard Worker #ifdef MIRBSD_BOOTFLOPPY
115*7c356e86SAndroid Build Coastguard Worker #include <wchar.h>
116*7c356e86SAndroid Build Coastguard Worker #endif
117*7c356e86SAndroid Build Coastguard Worker
118*7c356e86SAndroid Build Coastguard Worker /* monkey-patch known-bad offsetof versions to quell a warning */
119*7c356e86SAndroid Build Coastguard Worker #if (defined(__KLIBC__) || defined(__dietlibc__)) && \
120*7c356e86SAndroid Build Coastguard Worker ((defined(__GNUC__) && (__GNUC__ > 3)) || defined(__NWCC__))
121*7c356e86SAndroid Build Coastguard Worker #undef offsetof
122*7c356e86SAndroid Build Coastguard Worker #define offsetof(s,e) __builtin_offsetof(s, e)
123*7c356e86SAndroid Build Coastguard Worker #endif
124*7c356e86SAndroid Build Coastguard Worker
125*7c356e86SAndroid Build Coastguard Worker #undef __attribute__
126*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_BOUNDED
127*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_BOUNDED(x,y,z) __attribute__((__bounded__(x, y, z)))
128*7c356e86SAndroid Build Coastguard Worker #else
129*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_BOUNDED(x,y,z) /* nothing */
130*7c356e86SAndroid Build Coastguard Worker #endif
131*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_FORMAT
132*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_FORMAT(x,y,z) __attribute__((__format__(x, y, z)))
133*7c356e86SAndroid Build Coastguard Worker #else
134*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_FORMAT(x,y,z) /* nothing */
135*7c356e86SAndroid Build Coastguard Worker #endif
136*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_NORETURN
137*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_NORETURN __attribute__((__noreturn__))
138*7c356e86SAndroid Build Coastguard Worker #else
139*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_NORETURN /* nothing */
140*7c356e86SAndroid Build Coastguard Worker #endif
141*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_PURE
142*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_PURE __attribute__((__pure__))
143*7c356e86SAndroid Build Coastguard Worker #else
144*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_PURE /* nothing */
145*7c356e86SAndroid Build Coastguard Worker #endif
146*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_UNUSED
147*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_UNUSED __attribute__((__unused__))
148*7c356e86SAndroid Build Coastguard Worker #else
149*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_UNUSED /* nothing */
150*7c356e86SAndroid Build Coastguard Worker #endif
151*7c356e86SAndroid Build Coastguard Worker #if HAVE_ATTRIBUTE_USED
152*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_USED __attribute__((__used__))
153*7c356e86SAndroid Build Coastguard Worker #else
154*7c356e86SAndroid Build Coastguard Worker #define MKSH_A_USED /* nothing */
155*7c356e86SAndroid Build Coastguard Worker #endif
156*7c356e86SAndroid Build Coastguard Worker
157*7c356e86SAndroid Build Coastguard Worker #if defined(MirBSD) && (MirBSD >= 0x09A1) && \
158*7c356e86SAndroid Build Coastguard Worker defined(__ELF__) && defined(__GNUC__) && \
159*7c356e86SAndroid Build Coastguard Worker !defined(__llvm__) && !defined(__NWCC__)
160*7c356e86SAndroid Build Coastguard Worker /*
161*7c356e86SAndroid Build Coastguard Worker * We got usable __IDSTRING __COPYRIGHT __RCSID __SCCSID macros
162*7c356e86SAndroid Build Coastguard Worker * which work for all cases; no need to redefine them using the
163*7c356e86SAndroid Build Coastguard Worker * "portable" macros from below when we might have the "better"
164*7c356e86SAndroid Build Coastguard Worker * gcc+ELF specific macros or other system dependent ones.
165*7c356e86SAndroid Build Coastguard Worker */
166*7c356e86SAndroid Build Coastguard Worker #else
167*7c356e86SAndroid Build Coastguard Worker #undef __IDSTRING
168*7c356e86SAndroid Build Coastguard Worker #undef __IDSTRING_CONCAT
169*7c356e86SAndroid Build Coastguard Worker #undef __IDSTRING_EXPAND
170*7c356e86SAndroid Build Coastguard Worker #undef __COPYRIGHT
171*7c356e86SAndroid Build Coastguard Worker #undef __RCSID
172*7c356e86SAndroid Build Coastguard Worker #undef __SCCSID
173*7c356e86SAndroid Build Coastguard Worker #define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p
174*7c356e86SAndroid Build Coastguard Worker #define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p)
175*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_DONT_EMIT_IDSTRING
176*7c356e86SAndroid Build Coastguard Worker #define __IDSTRING(prefix,string) /* nothing */
177*7c356e86SAndroid Build Coastguard Worker #elif defined(__ELF__) && defined(__GNUC__) && \
178*7c356e86SAndroid Build Coastguard Worker !(defined(__GNUC__) && defined(__mips16) && (__GNUC__ >= 8)) && \
179*7c356e86SAndroid Build Coastguard Worker !defined(__llvm__) && !defined(__NWCC__) && !defined(NO_ASM)
180*7c356e86SAndroid Build Coastguard Worker #define __IDSTRING(prefix,string) \
181*7c356e86SAndroid Build Coastguard Worker __asm__(".section .comment" \
182*7c356e86SAndroid Build Coastguard Worker "\n .ascii \"@(\"\"#)" #prefix ": \"" \
183*7c356e86SAndroid Build Coastguard Worker "\n .asciz \"" string "\"" \
184*7c356e86SAndroid Build Coastguard Worker "\n .previous")
185*7c356e86SAndroid Build Coastguard Worker #else
186*7c356e86SAndroid Build Coastguard Worker #define __IDSTRING(prefix,string) \
187*7c356e86SAndroid Build Coastguard Worker static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \
188*7c356e86SAndroid Build Coastguard Worker MKSH_A_USED = "@(""#)" #prefix ": " string
189*7c356e86SAndroid Build Coastguard Worker #endif
190*7c356e86SAndroid Build Coastguard Worker #define __COPYRIGHT(x) __IDSTRING(copyright,x)
191*7c356e86SAndroid Build Coastguard Worker #define __RCSID(x) __IDSTRING(rcsid,x)
192*7c356e86SAndroid Build Coastguard Worker #define __SCCSID(x) __IDSTRING(sccsid,x)
193*7c356e86SAndroid Build Coastguard Worker #endif
194*7c356e86SAndroid Build Coastguard Worker
195*7c356e86SAndroid Build Coastguard Worker #ifdef EXTERN
196*7c356e86SAndroid Build Coastguard Worker __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.904 2020/10/31 03:53:06 tg Exp $");
197*7c356e86SAndroid Build Coastguard Worker #endif
198*7c356e86SAndroid Build Coastguard Worker #define MKSH_VERSION "R59 2020/10/31"
199*7c356e86SAndroid Build Coastguard Worker
200*7c356e86SAndroid Build Coastguard Worker /* arithmetic types: C implementation */
201*7c356e86SAndroid Build Coastguard Worker #if !HAVE_CAN_INTTYPES
202*7c356e86SAndroid Build Coastguard Worker #if !HAVE_CAN_UCBINTS
203*7c356e86SAndroid Build Coastguard Worker typedef signed int int32_t;
204*7c356e86SAndroid Build Coastguard Worker typedef unsigned int uint32_t;
205*7c356e86SAndroid Build Coastguard Worker #else
206*7c356e86SAndroid Build Coastguard Worker typedef u_int32_t uint32_t;
207*7c356e86SAndroid Build Coastguard Worker #endif
208*7c356e86SAndroid Build Coastguard Worker #endif
209*7c356e86SAndroid Build Coastguard Worker
210*7c356e86SAndroid Build Coastguard Worker /* arithmetic types: shell arithmetics */
211*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_LEGACY_MODE
212*7c356e86SAndroid Build Coastguard Worker /*
213*7c356e86SAndroid Build Coastguard Worker * POSIX demands these to be the C environment's long type
214*7c356e86SAndroid Build Coastguard Worker */
215*7c356e86SAndroid Build Coastguard Worker typedef long mksh_ari_t;
216*7c356e86SAndroid Build Coastguard Worker typedef unsigned long mksh_uari_t;
217*7c356e86SAndroid Build Coastguard Worker #else
218*7c356e86SAndroid Build Coastguard Worker /*
219*7c356e86SAndroid Build Coastguard Worker * These types are exactly 32 bit wide; signed and unsigned
220*7c356e86SAndroid Build Coastguard Worker * integer wraparound, even across division and modulo, for
221*7c356e86SAndroid Build Coastguard Worker * any shell code using them, is guaranteed.
222*7c356e86SAndroid Build Coastguard Worker */
223*7c356e86SAndroid Build Coastguard Worker typedef int32_t mksh_ari_t;
224*7c356e86SAndroid Build Coastguard Worker typedef uint32_t mksh_uari_t;
225*7c356e86SAndroid Build Coastguard Worker #endif
226*7c356e86SAndroid Build Coastguard Worker
227*7c356e86SAndroid Build Coastguard Worker /* boolean type (no <stdbool.h> deliberately) */
228*7c356e86SAndroid Build Coastguard Worker typedef unsigned char mksh_bool;
229*7c356e86SAndroid Build Coastguard Worker #undef bool
230*7c356e86SAndroid Build Coastguard Worker /* false MUST equal the same 0 as written by static storage initialisation */
231*7c356e86SAndroid Build Coastguard Worker #undef false
232*7c356e86SAndroid Build Coastguard Worker #undef true
233*7c356e86SAndroid Build Coastguard Worker /* access macros for boolean type */
234*7c356e86SAndroid Build Coastguard Worker #define bool mksh_bool
235*7c356e86SAndroid Build Coastguard Worker /* values must have identity mapping between mksh_bool and short */
236*7c356e86SAndroid Build Coastguard Worker #define false 0
237*7c356e86SAndroid Build Coastguard Worker #define true 1
238*7c356e86SAndroid Build Coastguard Worker /* make any-type into bool or short */
239*7c356e86SAndroid Build Coastguard Worker #define tobool(cond) ((cond) ? true : false)
240*7c356e86SAndroid Build Coastguard Worker
241*7c356e86SAndroid Build Coastguard Worker /* char (octet) type: C implementation */
242*7c356e86SAndroid Build Coastguard Worker #if !HAVE_CAN_INT8TYPE
243*7c356e86SAndroid Build Coastguard Worker #if !HAVE_CAN_UCBINT8
244*7c356e86SAndroid Build Coastguard Worker typedef unsigned char uint8_t;
245*7c356e86SAndroid Build Coastguard Worker #else
246*7c356e86SAndroid Build Coastguard Worker typedef u_int8_t uint8_t;
247*7c356e86SAndroid Build Coastguard Worker #endif
248*7c356e86SAndroid Build Coastguard Worker #endif
249*7c356e86SAndroid Build Coastguard Worker
250*7c356e86SAndroid Build Coastguard Worker /* other standard types */
251*7c356e86SAndroid Build Coastguard Worker
252*7c356e86SAndroid Build Coastguard Worker #if !HAVE_SIG_T
253*7c356e86SAndroid Build Coastguard Worker #undef sig_t
254*7c356e86SAndroid Build Coastguard Worker typedef void (*sig_t)(int);
255*7c356e86SAndroid Build Coastguard Worker #endif
256*7c356e86SAndroid Build Coastguard Worker
257*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_TYPEDEF_SIG_ATOMIC_T
258*7c356e86SAndroid Build Coastguard Worker typedef MKSH_TYPEDEF_SIG_ATOMIC_T sig_atomic_t;
259*7c356e86SAndroid Build Coastguard Worker #endif
260*7c356e86SAndroid Build Coastguard Worker
261*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_TYPEDEF_SSIZE_T
262*7c356e86SAndroid Build Coastguard Worker typedef MKSH_TYPEDEF_SSIZE_T ssize_t;
263*7c356e86SAndroid Build Coastguard Worker #endif
264*7c356e86SAndroid Build Coastguard Worker
265*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
266*7c356e86SAndroid Build Coastguard Worker #define MKSH_SHF_NO_INLINE
267*7c356e86SAndroid Build Coastguard Worker #endif
268*7c356e86SAndroid Build Coastguard Worker
269*7c356e86SAndroid Build Coastguard Worker /* do not merge these conditionals as neatcc’s preprocessor is simple */
270*7c356e86SAndroid Build Coastguard Worker #ifdef __neatcc__
271*7c356e86SAndroid Build Coastguard Worker /* parsing of comma operator <,> in expressions broken */
272*7c356e86SAndroid Build Coastguard Worker #define MKSH_SHF_NO_INLINE
273*7c356e86SAndroid Build Coastguard Worker #endif
274*7c356e86SAndroid Build Coastguard Worker
275*7c356e86SAndroid Build Coastguard Worker /* un-do vendor damage */
276*7c356e86SAndroid Build Coastguard Worker
277*7c356e86SAndroid Build Coastguard Worker #undef BAD /* AIX defines that somewhere */
278*7c356e86SAndroid Build Coastguard Worker #undef PRINT /* LynxOS defines that somewhere */
279*7c356e86SAndroid Build Coastguard Worker #undef flock /* SCO UnixWare defines that to flock64 but ENOENT */
280*7c356e86SAndroid Build Coastguard Worker
281*7c356e86SAndroid Build Coastguard Worker
282*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_INCLUDES_ONLY
283*7c356e86SAndroid Build Coastguard Worker
284*7c356e86SAndroid Build Coastguard Worker /* compile-time assertions */
285*7c356e86SAndroid Build Coastguard Worker #define cta(name,expr) struct cta_ ## name { char t[(expr) ? 1 : -1]; }
286*7c356e86SAndroid Build Coastguard Worker
287*7c356e86SAndroid Build Coastguard Worker /* EBCDIC fun */
288*7c356e86SAndroid Build Coastguard Worker
289*7c356e86SAndroid Build Coastguard Worker /* see the large comment in shf.c for an EBCDIC primer */
290*7c356e86SAndroid Build Coastguard Worker
291*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_FOR_Z_OS) && defined(__MVS__) && defined(__IBMC__) && defined(__CHARSET_LIB)
292*7c356e86SAndroid Build Coastguard Worker # if !__CHARSET_LIB && !defined(MKSH_EBCDIC)
293*7c356e86SAndroid Build Coastguard Worker # error "Please compile with Build.sh -E for EBCDIC!"
294*7c356e86SAndroid Build Coastguard Worker # endif
295*7c356e86SAndroid Build Coastguard Worker # if __CHARSET_LIB && defined(MKSH_EBCDIC)
296*7c356e86SAndroid Build Coastguard Worker # error "Please compile without -E argument to Build.sh for ASCII!"
297*7c356e86SAndroid Build Coastguard Worker # endif
298*7c356e86SAndroid Build Coastguard Worker # if __CHARSET_LIB && !defined(_ENHANCED_ASCII_EXT)
299*7c356e86SAndroid Build Coastguard Worker /* go all-out on ASCII */
300*7c356e86SAndroid Build Coastguard Worker # define _ENHANCED_ASCII_EXT 0xFFFFFFFF
301*7c356e86SAndroid Build Coastguard Worker # endif
302*7c356e86SAndroid Build Coastguard Worker #endif
303*7c356e86SAndroid Build Coastguard Worker
304*7c356e86SAndroid Build Coastguard Worker /* extra types */
305*7c356e86SAndroid Build Coastguard Worker
306*7c356e86SAndroid Build Coastguard Worker /* getrusage does not exist on OS/2 kLIBC */
307*7c356e86SAndroid Build Coastguard Worker #if !HAVE_GETRUSAGE && !defined(__OS2__)
308*7c356e86SAndroid Build Coastguard Worker #undef rusage
309*7c356e86SAndroid Build Coastguard Worker #undef RUSAGE_SELF
310*7c356e86SAndroid Build Coastguard Worker #undef RUSAGE_CHILDREN
311*7c356e86SAndroid Build Coastguard Worker #define rusage mksh_rusage
312*7c356e86SAndroid Build Coastguard Worker #define RUSAGE_SELF 0
313*7c356e86SAndroid Build Coastguard Worker #define RUSAGE_CHILDREN -1
314*7c356e86SAndroid Build Coastguard Worker
315*7c356e86SAndroid Build Coastguard Worker struct rusage {
316*7c356e86SAndroid Build Coastguard Worker struct timeval ru_utime;
317*7c356e86SAndroid Build Coastguard Worker struct timeval ru_stime;
318*7c356e86SAndroid Build Coastguard Worker };
319*7c356e86SAndroid Build Coastguard Worker #endif
320*7c356e86SAndroid Build Coastguard Worker
321*7c356e86SAndroid Build Coastguard Worker /* extra macros */
322*7c356e86SAndroid Build Coastguard Worker
323*7c356e86SAndroid Build Coastguard Worker #ifndef timerclear
324*7c356e86SAndroid Build Coastguard Worker #define timerclear(tvp) \
325*7c356e86SAndroid Build Coastguard Worker do { \
326*7c356e86SAndroid Build Coastguard Worker (tvp)->tv_sec = (tvp)->tv_usec = 0; \
327*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
328*7c356e86SAndroid Build Coastguard Worker #endif
329*7c356e86SAndroid Build Coastguard Worker #ifndef timeradd
330*7c356e86SAndroid Build Coastguard Worker #define timeradd(tvp,uvp,vvp) \
331*7c356e86SAndroid Build Coastguard Worker do { \
332*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
333*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
334*7c356e86SAndroid Build Coastguard Worker if ((vvp)->tv_usec >= 1000000) { \
335*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_sec++; \
336*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_usec -= 1000000; \
337*7c356e86SAndroid Build Coastguard Worker } \
338*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
339*7c356e86SAndroid Build Coastguard Worker #endif
340*7c356e86SAndroid Build Coastguard Worker #ifndef timersub
341*7c356e86SAndroid Build Coastguard Worker #define timersub(tvp,uvp,vvp) \
342*7c356e86SAndroid Build Coastguard Worker do { \
343*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
344*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
345*7c356e86SAndroid Build Coastguard Worker if ((vvp)->tv_usec < 0) { \
346*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_sec--; \
347*7c356e86SAndroid Build Coastguard Worker (vvp)->tv_usec += 1000000; \
348*7c356e86SAndroid Build Coastguard Worker } \
349*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
350*7c356e86SAndroid Build Coastguard Worker #endif
351*7c356e86SAndroid Build Coastguard Worker
352*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH__NO_PATH_MAX
353*7c356e86SAndroid Build Coastguard Worker #undef PATH_MAX
354*7c356e86SAndroid Build Coastguard Worker #else
355*7c356e86SAndroid Build Coastguard Worker #ifndef PATH_MAX
356*7c356e86SAndroid Build Coastguard Worker #ifdef MAXPATHLEN
357*7c356e86SAndroid Build Coastguard Worker #define PATH_MAX MAXPATHLEN
358*7c356e86SAndroid Build Coastguard Worker #else
359*7c356e86SAndroid Build Coastguard Worker #define PATH_MAX 1024
360*7c356e86SAndroid Build Coastguard Worker #endif
361*7c356e86SAndroid Build Coastguard Worker #endif
362*7c356e86SAndroid Build Coastguard Worker #endif
363*7c356e86SAndroid Build Coastguard Worker #ifndef SIZE_MAX
364*7c356e86SAndroid Build Coastguard Worker #ifdef SIZE_T_MAX
365*7c356e86SAndroid Build Coastguard Worker #define SIZE_MAX SIZE_T_MAX
366*7c356e86SAndroid Build Coastguard Worker #else
367*7c356e86SAndroid Build Coastguard Worker #define SIZE_MAX ((size_t)-1)
368*7c356e86SAndroid Build Coastguard Worker #endif
369*7c356e86SAndroid Build Coastguard Worker #endif
370*7c356e86SAndroid Build Coastguard Worker #ifndef S_ISLNK
371*7c356e86SAndroid Build Coastguard Worker #define S_ISLNK(m) ((m & 0170000) == 0120000)
372*7c356e86SAndroid Build Coastguard Worker #endif
373*7c356e86SAndroid Build Coastguard Worker #ifndef S_ISSOCK
374*7c356e86SAndroid Build Coastguard Worker #define S_ISSOCK(m) ((m & 0170000) == 0140000)
375*7c356e86SAndroid Build Coastguard Worker #endif
376*7c356e86SAndroid Build Coastguard Worker #if !defined(S_ISCDF) && defined(S_CDF)
377*7c356e86SAndroid Build Coastguard Worker #define S_ISCDF(m) (S_ISDIR(m) && ((m) & S_CDF))
378*7c356e86SAndroid Build Coastguard Worker #endif
379*7c356e86SAndroid Build Coastguard Worker #ifndef DEFFILEMODE
380*7c356e86SAndroid Build Coastguard Worker #define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
381*7c356e86SAndroid Build Coastguard Worker #endif
382*7c356e86SAndroid Build Coastguard Worker
383*7c356e86SAndroid Build Coastguard Worker
384*7c356e86SAndroid Build Coastguard Worker /* determine ksh_NSIG: first, use the traditional definitions */
385*7c356e86SAndroid Build Coastguard Worker #undef ksh_NSIG
386*7c356e86SAndroid Build Coastguard Worker #if defined(NSIG)
387*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (NSIG)
388*7c356e86SAndroid Build Coastguard Worker #elif defined(_NSIG)
389*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (_NSIG)
390*7c356e86SAndroid Build Coastguard Worker #elif defined(SIGMAX)
391*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (SIGMAX + 1)
392*7c356e86SAndroid Build Coastguard Worker #elif defined(_SIGMAX)
393*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (_SIGMAX + 1)
394*7c356e86SAndroid Build Coastguard Worker #elif defined(NSIG_MAX)
395*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (NSIG_MAX)
396*7c356e86SAndroid Build Coastguard Worker #elif defined(MKSH_FOR_Z_OS)
397*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG 40
398*7c356e86SAndroid Build Coastguard Worker #else
399*7c356e86SAndroid Build Coastguard Worker # error Please have your platform define NSIG.
400*7c356e86SAndroid Build Coastguard Worker #endif
401*7c356e86SAndroid Build Coastguard Worker /* range-check them */
402*7c356e86SAndroid Build Coastguard Worker #if (ksh_NSIG < 1)
403*7c356e86SAndroid Build Coastguard Worker # error Your NSIG value is not positive.
404*7c356e86SAndroid Build Coastguard Worker #undef ksh_NSIG
405*7c356e86SAndroid Build Coastguard Worker #endif
406*7c356e86SAndroid Build Coastguard Worker /* second, see if the new POSIX definition is available */
407*7c356e86SAndroid Build Coastguard Worker #ifdef NSIG_MAX
408*7c356e86SAndroid Build Coastguard Worker #if (NSIG_MAX < 2)
409*7c356e86SAndroid Build Coastguard Worker /* and usable */
410*7c356e86SAndroid Build Coastguard Worker # error Your NSIG_MAX value is too small.
411*7c356e86SAndroid Build Coastguard Worker #undef NSIG_MAX
412*7c356e86SAndroid Build Coastguard Worker #elif (ksh_NSIG > NSIG_MAX)
413*7c356e86SAndroid Build Coastguard Worker /* and realistic */
414*7c356e86SAndroid Build Coastguard Worker # error Your NSIG value is larger than your NSIG_MAX value.
415*7c356e86SAndroid Build Coastguard Worker #undef NSIG_MAX
416*7c356e86SAndroid Build Coastguard Worker #else
417*7c356e86SAndroid Build Coastguard Worker /* since it’s usable, prefer it */
418*7c356e86SAndroid Build Coastguard Worker #undef ksh_NSIG
419*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG (NSIG_MAX)
420*7c356e86SAndroid Build Coastguard Worker #endif
421*7c356e86SAndroid Build Coastguard Worker /* if NSIG_MAX is now still defined, use sysconf(_SC_NSIG) at runtime */
422*7c356e86SAndroid Build Coastguard Worker #endif
423*7c356e86SAndroid Build Coastguard Worker /* third, for cpp without the error directive, default */
424*7c356e86SAndroid Build Coastguard Worker #ifndef ksh_NSIG
425*7c356e86SAndroid Build Coastguard Worker #define ksh_NSIG 64
426*7c356e86SAndroid Build Coastguard Worker #endif
427*7c356e86SAndroid Build Coastguard Worker
428*7c356e86SAndroid Build Coastguard Worker #define ksh_sigmask(sig) (((sig) < 1 || (sig) > 127) ? 255 : 128 + (sig))
429*7c356e86SAndroid Build Coastguard Worker
430*7c356e86SAndroid Build Coastguard Worker
431*7c356e86SAndroid Build Coastguard Worker /* OS-dependent additions (functions, variables, by OS) */
432*7c356e86SAndroid Build Coastguard Worker
433*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_EXE_EXT
434*7c356e86SAndroid Build Coastguard Worker #undef MKSH_EXE_EXT
435*7c356e86SAndroid Build Coastguard Worker #define MKSH_EXE_EXT ".exe"
436*7c356e86SAndroid Build Coastguard Worker #else
437*7c356e86SAndroid Build Coastguard Worker #define MKSH_EXE_EXT ""
438*7c356e86SAndroid Build Coastguard Worker #endif
439*7c356e86SAndroid Build Coastguard Worker
440*7c356e86SAndroid Build Coastguard Worker #ifdef __OS2__
441*7c356e86SAndroid Build Coastguard Worker #define MKSH_UNIXROOT "/@unixroot"
442*7c356e86SAndroid Build Coastguard Worker #else
443*7c356e86SAndroid Build Coastguard Worker #define MKSH_UNIXROOT ""
444*7c356e86SAndroid Build Coastguard Worker #endif
445*7c356e86SAndroid Build Coastguard Worker
446*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_DOSPATH
447*7c356e86SAndroid Build Coastguard Worker #ifndef __GNUC__
448*7c356e86SAndroid Build Coastguard Worker # error GCC extensions needed later on
449*7c356e86SAndroid Build Coastguard Worker #endif
450*7c356e86SAndroid Build Coastguard Worker #define MKSH_PATHSEPS ";"
451*7c356e86SAndroid Build Coastguard Worker #define MKSH_PATHSEPC ';'
452*7c356e86SAndroid Build Coastguard Worker #else
453*7c356e86SAndroid Build Coastguard Worker #define MKSH_PATHSEPS ":"
454*7c356e86SAndroid Build Coastguard Worker #define MKSH_PATHSEPC ':'
455*7c356e86SAndroid Build Coastguard Worker #endif
456*7c356e86SAndroid Build Coastguard Worker
457*7c356e86SAndroid Build Coastguard Worker #if !HAVE_FLOCK_DECL
458*7c356e86SAndroid Build Coastguard Worker extern int flock(int, int);
459*7c356e86SAndroid Build Coastguard Worker #endif
460*7c356e86SAndroid Build Coastguard Worker
461*7c356e86SAndroid Build Coastguard Worker #if !HAVE_GETTIMEOFDAY
462*7c356e86SAndroid Build Coastguard Worker #define mksh_TIME(tv) do { \
463*7c356e86SAndroid Build Coastguard Worker (tv).tv_usec = 0; \
464*7c356e86SAndroid Build Coastguard Worker (tv).tv_sec = time(NULL); \
465*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
466*7c356e86SAndroid Build Coastguard Worker #else
467*7c356e86SAndroid Build Coastguard Worker #define mksh_TIME(tv) gettimeofday(&(tv), NULL)
468*7c356e86SAndroid Build Coastguard Worker #endif
469*7c356e86SAndroid Build Coastguard Worker
470*7c356e86SAndroid Build Coastguard Worker #if !HAVE_GETRUSAGE
471*7c356e86SAndroid Build Coastguard Worker extern int getrusage(int, struct rusage *);
472*7c356e86SAndroid Build Coastguard Worker #endif
473*7c356e86SAndroid Build Coastguard Worker
474*7c356e86SAndroid Build Coastguard Worker #if !HAVE_MEMMOVE
475*7c356e86SAndroid Build Coastguard Worker /* we assume either memmove or bcopy exist, at the moment */
476*7c356e86SAndroid Build Coastguard Worker #define memmove(dst,src,len) bcopy((src), (dst), (len))
477*7c356e86SAndroid Build Coastguard Worker #endif
478*7c356e86SAndroid Build Coastguard Worker
479*7c356e86SAndroid Build Coastguard Worker #if !HAVE_REVOKE_DECL
480*7c356e86SAndroid Build Coastguard Worker extern int revoke(const char *);
481*7c356e86SAndroid Build Coastguard Worker #endif
482*7c356e86SAndroid Build Coastguard Worker
483*7c356e86SAndroid Build Coastguard Worker #if defined(DEBUG) || !HAVE_STRERROR
484*7c356e86SAndroid Build Coastguard Worker #undef strerror
485*7c356e86SAndroid Build Coastguard Worker #define strerror /* poisoned */ dontuse_strerror
486*7c356e86SAndroid Build Coastguard Worker #define cstrerror /* replaced */ cstrerror
487*7c356e86SAndroid Build Coastguard Worker extern const char *cstrerror(int);
488*7c356e86SAndroid Build Coastguard Worker #else
489*7c356e86SAndroid Build Coastguard Worker #define cstrerror(errnum) ((const char *)strerror(errnum))
490*7c356e86SAndroid Build Coastguard Worker #endif
491*7c356e86SAndroid Build Coastguard Worker
492*7c356e86SAndroid Build Coastguard Worker #if !HAVE_STRLCPY
493*7c356e86SAndroid Build Coastguard Worker size_t strlcpy(char *, const char *, size_t);
494*7c356e86SAndroid Build Coastguard Worker #endif
495*7c356e86SAndroid Build Coastguard Worker
496*7c356e86SAndroid Build Coastguard Worker #ifdef __INTERIX
497*7c356e86SAndroid Build Coastguard Worker /* XXX imake style */
498*7c356e86SAndroid Build Coastguard Worker #define makedev mkdev
499*7c356e86SAndroid Build Coastguard Worker extern int __cdecl seteuid(uid_t);
500*7c356e86SAndroid Build Coastguard Worker extern int __cdecl setegid(gid_t);
501*7c356e86SAndroid Build Coastguard Worker #endif
502*7c356e86SAndroid Build Coastguard Worker
503*7c356e86SAndroid Build Coastguard Worker #if defined(__COHERENT__)
504*7c356e86SAndroid Build Coastguard Worker #ifndef O_ACCMODE
505*7c356e86SAndroid Build Coastguard Worker /* this need not work everywhere, take care */
506*7c356e86SAndroid Build Coastguard Worker #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
507*7c356e86SAndroid Build Coastguard Worker #endif
508*7c356e86SAndroid Build Coastguard Worker #endif
509*7c356e86SAndroid Build Coastguard Worker
510*7c356e86SAndroid Build Coastguard Worker #ifndef O_BINARY
511*7c356e86SAndroid Build Coastguard Worker #define O_BINARY 0
512*7c356e86SAndroid Build Coastguard Worker #endif
513*7c356e86SAndroid Build Coastguard Worker
514*7c356e86SAndroid Build Coastguard Worker #undef O_MAYEXEC /* https://lwn.net/Articles/820658/ */
515*7c356e86SAndroid Build Coastguard Worker #define O_MAYEXEC 0
516*7c356e86SAndroid Build Coastguard Worker
517*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH__NO_SYMLINK
518*7c356e86SAndroid Build Coastguard Worker #undef S_ISLNK
519*7c356e86SAndroid Build Coastguard Worker #define S_ISLNK(m) (/* CONSTCOND */ 0)
520*7c356e86SAndroid Build Coastguard Worker #define mksh_lstat stat
521*7c356e86SAndroid Build Coastguard Worker #else
522*7c356e86SAndroid Build Coastguard Worker #define mksh_lstat lstat
523*7c356e86SAndroid Build Coastguard Worker #endif
524*7c356e86SAndroid Build Coastguard Worker
525*7c356e86SAndroid Build Coastguard Worker #if HAVE_TERMIOS_H
526*7c356e86SAndroid Build Coastguard Worker #define mksh_ttyst struct termios
527*7c356e86SAndroid Build Coastguard Worker #define mksh_tcget(fd,st) tcgetattr((fd), (st))
528*7c356e86SAndroid Build Coastguard Worker #define mksh_tcset(fd,st) tcsetattr((fd), TCSADRAIN, (st))
529*7c356e86SAndroid Build Coastguard Worker #else
530*7c356e86SAndroid Build Coastguard Worker #define mksh_ttyst struct termio
531*7c356e86SAndroid Build Coastguard Worker #define mksh_tcget(fd,st) ioctl((fd), TCGETA, (st))
532*7c356e86SAndroid Build Coastguard Worker #define mksh_tcset(fd,st) ioctl((fd), TCSETAW, (st))
533*7c356e86SAndroid Build Coastguard Worker #endif
534*7c356e86SAndroid Build Coastguard Worker
535*7c356e86SAndroid Build Coastguard Worker #ifndef ISTRIP
536*7c356e86SAndroid Build Coastguard Worker #define ISTRIP 0
537*7c356e86SAndroid Build Coastguard Worker #endif
538*7c356e86SAndroid Build Coastguard Worker
539*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_EBCDIC
540*7c356e86SAndroid Build Coastguard Worker #define KSH_BEL '\a'
541*7c356e86SAndroid Build Coastguard Worker #define KSH_ESC 047
542*7c356e86SAndroid Build Coastguard Worker #define KSH_ESC_STRING "\047"
543*7c356e86SAndroid Build Coastguard Worker #define KSH_VTAB '\v'
544*7c356e86SAndroid Build Coastguard Worker #else
545*7c356e86SAndroid Build Coastguard Worker /*
546*7c356e86SAndroid Build Coastguard Worker * According to the comments in pdksh, \007 seems to be more portable
547*7c356e86SAndroid Build Coastguard Worker * than \a (HP-UX cc, Ultrix cc, old pcc, etc.) so we avoid the escape
548*7c356e86SAndroid Build Coastguard Worker * sequence if ASCII can be assumed.
549*7c356e86SAndroid Build Coastguard Worker */
550*7c356e86SAndroid Build Coastguard Worker #define KSH_BEL 7
551*7c356e86SAndroid Build Coastguard Worker #define KSH_ESC 033
552*7c356e86SAndroid Build Coastguard Worker #define KSH_ESC_STRING "\033"
553*7c356e86SAndroid Build Coastguard Worker #define KSH_VTAB 11
554*7c356e86SAndroid Build Coastguard Worker #endif
555*7c356e86SAndroid Build Coastguard Worker
556*7c356e86SAndroid Build Coastguard Worker
557*7c356e86SAndroid Build Coastguard Worker /* some useful #defines */
558*7c356e86SAndroid Build Coastguard Worker #ifdef EXTERN
559*7c356e86SAndroid Build Coastguard Worker # define E_INIT(i) = i
560*7c356e86SAndroid Build Coastguard Worker #else
561*7c356e86SAndroid Build Coastguard Worker # define E_INIT(i)
562*7c356e86SAndroid Build Coastguard Worker # define EXTERN extern
563*7c356e86SAndroid Build Coastguard Worker # define EXTERN_DEFINED
564*7c356e86SAndroid Build Coastguard Worker #endif
565*7c356e86SAndroid Build Coastguard Worker
566*7c356e86SAndroid Build Coastguard Worker /* define bit in flag */
567*7c356e86SAndroid Build Coastguard Worker #define BIT(i) (1U << (i))
568*7c356e86SAndroid Build Coastguard Worker #define NELEM(a) (sizeof(a) / sizeof((a)[0]))
569*7c356e86SAndroid Build Coastguard Worker
570*7c356e86SAndroid Build Coastguard Worker /*
571*7c356e86SAndroid Build Coastguard Worker * Make MAGIC a char that might be printed to make bugs more obvious, but
572*7c356e86SAndroid Build Coastguard Worker * not a char that is used often. Also, can't use the high bit as it causes
573*7c356e86SAndroid Build Coastguard Worker * portability problems (calling strchr(x, 0x80 | 'x') is error prone).
574*7c356e86SAndroid Build Coastguard Worker *
575*7c356e86SAndroid Build Coastguard Worker * MAGIC can be followed by MAGIC (to escape the octet itself) or one of:
576*7c356e86SAndroid Build Coastguard Worker * ' !)*,-?[]{|}' 0x80|' !*+?@' (probably… hysteric raisins abound)
577*7c356e86SAndroid Build Coastguard Worker *
578*7c356e86SAndroid Build Coastguard Worker * The |0x80 is likely unsafe on EBCDIC :( though the listed chars are
579*7c356e86SAndroid Build Coastguard Worker * low-bit7 at least on cp1047 so YMMV
580*7c356e86SAndroid Build Coastguard Worker */
581*7c356e86SAndroid Build Coastguard Worker #define MAGIC KSH_BEL /* prefix for *?[!{,} during expand */
582*7c356e86SAndroid Build Coastguard Worker #define ISMAGIC(c) (ord(c) == ORD(MAGIC))
583*7c356e86SAndroid Build Coastguard Worker
584*7c356e86SAndroid Build Coastguard Worker EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */
585*7c356e86SAndroid Build Coastguard Worker
586*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_LEGACY_MODE
587*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_ISLEGACY "LEGACY"
588*7c356e86SAndroid Build Coastguard Worker #else
589*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_ISLEGACY "MIRBSD"
590*7c356e86SAndroid Build Coastguard Worker #endif
591*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_WITH_TEXTMODE
592*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_TEXTMODE " +TEXTMODE"
593*7c356e86SAndroid Build Coastguard Worker #else
594*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_TEXTMODE ""
595*7c356e86SAndroid Build Coastguard Worker #endif
596*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_EBCDIC
597*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_EBCDIC " +EBCDIC"
598*7c356e86SAndroid Build Coastguard Worker #else
599*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_EBCDIC ""
600*7c356e86SAndroid Build Coastguard Worker #endif
601*7c356e86SAndroid Build Coastguard Worker #ifndef KSH_VERSIONNAME_VENDOR_EXT
602*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSIONNAME_VENDOR_EXT ""
603*7c356e86SAndroid Build Coastguard Worker #endif
604*7c356e86SAndroid Build Coastguard Worker EXTERN const char initvsn[] E_INIT("KSH_VERSION=@(#)" KSH_VERSIONNAME_ISLEGACY \
605*7c356e86SAndroid Build Coastguard Worker " KSH " MKSH_VERSION KSH_VERSIONNAME_EBCDIC KSH_VERSIONNAME_TEXTMODE \
606*7c356e86SAndroid Build Coastguard Worker KSH_VERSIONNAME_VENDOR_EXT);
607*7c356e86SAndroid Build Coastguard Worker #define KSH_VERSION (initvsn + /* "KSH_VERSION=@(#)" */ 16)
608*7c356e86SAndroid Build Coastguard Worker
609*7c356e86SAndroid Build Coastguard Worker EXTERN const char digits_uc[] E_INIT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
610*7c356e86SAndroid Build Coastguard Worker EXTERN const char digits_lc[] E_INIT("0123456789abcdefghijklmnopqrstuvwxyz");
611*7c356e86SAndroid Build Coastguard Worker
612*7c356e86SAndroid Build Coastguard Worker /*
613*7c356e86SAndroid Build Coastguard Worker * Evil hack for const correctness due to API brokenness
614*7c356e86SAndroid Build Coastguard Worker */
615*7c356e86SAndroid Build Coastguard Worker union mksh_cchack {
616*7c356e86SAndroid Build Coastguard Worker char *rw;
617*7c356e86SAndroid Build Coastguard Worker const char *ro;
618*7c356e86SAndroid Build Coastguard Worker };
619*7c356e86SAndroid Build Coastguard Worker union mksh_ccphack {
620*7c356e86SAndroid Build Coastguard Worker char **rw;
621*7c356e86SAndroid Build Coastguard Worker const char **ro;
622*7c356e86SAndroid Build Coastguard Worker };
623*7c356e86SAndroid Build Coastguard Worker
624*7c356e86SAndroid Build Coastguard Worker /*
625*7c356e86SAndroid Build Coastguard Worker * Evil hack since casting uint to sint is implementation-defined
626*7c356e86SAndroid Build Coastguard Worker */
627*7c356e86SAndroid Build Coastguard Worker typedef union {
628*7c356e86SAndroid Build Coastguard Worker mksh_ari_t i;
629*7c356e86SAndroid Build Coastguard Worker mksh_uari_t u;
630*7c356e86SAndroid Build Coastguard Worker } mksh_ari_u;
631*7c356e86SAndroid Build Coastguard Worker
632*7c356e86SAndroid Build Coastguard Worker /* for const debugging */
633*7c356e86SAndroid Build Coastguard Worker #if defined(DEBUG) && defined(__GNUC__) && !defined(__ICC) && \
634*7c356e86SAndroid Build Coastguard Worker !defined(__INTEL_COMPILER) && !defined(__SUNPRO_C)
635*7c356e86SAndroid Build Coastguard Worker char *ucstrchr(char *, int);
636*7c356e86SAndroid Build Coastguard Worker char *ucstrstr(char *, const char *);
637*7c356e86SAndroid Build Coastguard Worker #undef strchr
638*7c356e86SAndroid Build Coastguard Worker #define strchr ucstrchr
639*7c356e86SAndroid Build Coastguard Worker #define strstr ucstrstr
640*7c356e86SAndroid Build Coastguard Worker #define cstrchr(s,c) ({ \
641*7c356e86SAndroid Build Coastguard Worker union mksh_cchack in, out; \
642*7c356e86SAndroid Build Coastguard Worker \
643*7c356e86SAndroid Build Coastguard Worker in.ro = (s); \
644*7c356e86SAndroid Build Coastguard Worker out.rw = ucstrchr(in.rw, (c)); \
645*7c356e86SAndroid Build Coastguard Worker (out.ro); \
646*7c356e86SAndroid Build Coastguard Worker })
647*7c356e86SAndroid Build Coastguard Worker #define cstrstr(b,l) ({ \
648*7c356e86SAndroid Build Coastguard Worker union mksh_cchack in, out; \
649*7c356e86SAndroid Build Coastguard Worker \
650*7c356e86SAndroid Build Coastguard Worker in.ro = (b); \
651*7c356e86SAndroid Build Coastguard Worker out.rw = ucstrstr(in.rw, (l)); \
652*7c356e86SAndroid Build Coastguard Worker (out.ro); \
653*7c356e86SAndroid Build Coastguard Worker })
654*7c356e86SAndroid Build Coastguard Worker #define vstrchr(s,c) (cstrchr((s), (c)) != NULL)
655*7c356e86SAndroid Build Coastguard Worker #define vstrstr(b,l) (cstrstr((b), (l)) != NULL)
656*7c356e86SAndroid Build Coastguard Worker #else /* !DEBUG, !gcc */
657*7c356e86SAndroid Build Coastguard Worker #define cstrchr(s,c) ((const char *)strchr((s), (c)))
658*7c356e86SAndroid Build Coastguard Worker #define cstrstr(s,c) ((const char *)strstr((s), (c)))
659*7c356e86SAndroid Build Coastguard Worker #define vstrchr(s,c) (strchr((s), (c)) != NULL)
660*7c356e86SAndroid Build Coastguard Worker #define vstrstr(b,l) (strstr((b), (l)) != NULL)
661*7c356e86SAndroid Build Coastguard Worker #endif
662*7c356e86SAndroid Build Coastguard Worker
663*7c356e86SAndroid Build Coastguard Worker #if defined(DEBUG) || defined(__COVERITY__)
664*7c356e86SAndroid Build Coastguard Worker #ifndef DEBUG_LEAKS
665*7c356e86SAndroid Build Coastguard Worker #define DEBUG_LEAKS
666*7c356e86SAndroid Build Coastguard Worker #endif
667*7c356e86SAndroid Build Coastguard Worker #endif
668*7c356e86SAndroid Build Coastguard Worker
669*7c356e86SAndroid Build Coastguard Worker #if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 593)
670*7c356e86SAndroid Build Coastguard Worker #error Must run Build.sh to compile this.
671*7c356e86SAndroid Build Coastguard Worker extern void thiswillneverbedefinedIhope(void);
672*7c356e86SAndroid Build Coastguard Worker int
im_sorry_dave(void)673*7c356e86SAndroid Build Coastguard Worker im_sorry_dave(void)
674*7c356e86SAndroid Build Coastguard Worker {
675*7c356e86SAndroid Build Coastguard Worker /* I’m sorry, Dave. I’m afraid I can’t do that. */
676*7c356e86SAndroid Build Coastguard Worker return (thiswillneverbedefinedIhope());
677*7c356e86SAndroid Build Coastguard Worker }
678*7c356e86SAndroid Build Coastguard Worker #endif
679*7c356e86SAndroid Build Coastguard Worker
680*7c356e86SAndroid Build Coastguard Worker /* use this ipv strchr(s, 0) but no side effects in s! */
681*7c356e86SAndroid Build Coastguard Worker #define strnul(s) ((s) + strlen((const void *)s))
682*7c356e86SAndroid Build Coastguard Worker
683*7c356e86SAndroid Build Coastguard Worker #define utf_ptradjx(src,dst) do { \
684*7c356e86SAndroid Build Coastguard Worker (dst) = (src) + utf_ptradj(src); \
685*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
686*7c356e86SAndroid Build Coastguard Worker
687*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
688*7c356e86SAndroid Build Coastguard Worker #define strdupx(d,s,ap) do { \
689*7c356e86SAndroid Build Coastguard Worker (d) = strdup_i((s), (ap)); \
690*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
691*7c356e86SAndroid Build Coastguard Worker #define strndupx(d,s,n,ap) do { \
692*7c356e86SAndroid Build Coastguard Worker (d) = strndup_i((s), (n), (ap)); \
693*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
694*7c356e86SAndroid Build Coastguard Worker #else
695*7c356e86SAndroid Build Coastguard Worker /* be careful to evaluate arguments only once! */
696*7c356e86SAndroid Build Coastguard Worker #define strdupx(d,s,ap) do { \
697*7c356e86SAndroid Build Coastguard Worker const char *strdup_src = (const void *)(s); \
698*7c356e86SAndroid Build Coastguard Worker char *strdup_dst = NULL; \
699*7c356e86SAndroid Build Coastguard Worker \
700*7c356e86SAndroid Build Coastguard Worker if (strdup_src != NULL) { \
701*7c356e86SAndroid Build Coastguard Worker size_t strdup_len = strlen(strdup_src) + 1; \
702*7c356e86SAndroid Build Coastguard Worker strdup_dst = alloc(strdup_len, (ap)); \
703*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst, strdup_src, strdup_len); \
704*7c356e86SAndroid Build Coastguard Worker } \
705*7c356e86SAndroid Build Coastguard Worker (d) = strdup_dst; \
706*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
707*7c356e86SAndroid Build Coastguard Worker #define strndupx(d,s,n,ap) do { \
708*7c356e86SAndroid Build Coastguard Worker const char *strdup_src = (const void *)(s); \
709*7c356e86SAndroid Build Coastguard Worker char *strdup_dst = NULL; \
710*7c356e86SAndroid Build Coastguard Worker \
711*7c356e86SAndroid Build Coastguard Worker if (strdup_src != NULL) { \
712*7c356e86SAndroid Build Coastguard Worker size_t strndup_len = (n); \
713*7c356e86SAndroid Build Coastguard Worker strdup_dst = alloc(strndup_len + 1, (ap)); \
714*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst, strdup_src, strndup_len); \
715*7c356e86SAndroid Build Coastguard Worker strdup_dst[strndup_len] = '\0'; \
716*7c356e86SAndroid Build Coastguard Worker } \
717*7c356e86SAndroid Build Coastguard Worker (d) = strdup_dst; \
718*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
719*7c356e86SAndroid Build Coastguard Worker #endif
720*7c356e86SAndroid Build Coastguard Worker #define strdup2x(d,s1,s2) do { \
721*7c356e86SAndroid Build Coastguard Worker const char *strdup_src = (const void *)(s1); \
722*7c356e86SAndroid Build Coastguard Worker const char *strdup_app = (const void *)(s2); \
723*7c356e86SAndroid Build Coastguard Worker size_t strndup_len = strlen(strdup_src); \
724*7c356e86SAndroid Build Coastguard Worker size_t strndup_ln2 = strlen(strdup_app) + 1; \
725*7c356e86SAndroid Build Coastguard Worker char *strdup_dst = alloc(strndup_len + strndup_ln2, ATEMP); \
726*7c356e86SAndroid Build Coastguard Worker \
727*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst, strdup_src, strndup_len); \
728*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst + strndup_len, strdup_app, strndup_ln2); \
729*7c356e86SAndroid Build Coastguard Worker (d) = strdup_dst; \
730*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
731*7c356e86SAndroid Build Coastguard Worker #define strpathx(d,s1,s2,cond) do { \
732*7c356e86SAndroid Build Coastguard Worker const char *strdup_src = (const void *)(s1); \
733*7c356e86SAndroid Build Coastguard Worker const char *strdup_app = (const void *)(s2); \
734*7c356e86SAndroid Build Coastguard Worker size_t strndup_len = strlen(strdup_src) + 1; \
735*7c356e86SAndroid Build Coastguard Worker size_t strndup_ln2 = ((cond) || *strdup_app) ? \
736*7c356e86SAndroid Build Coastguard Worker strlen(strdup_app) + 1 : 0; \
737*7c356e86SAndroid Build Coastguard Worker char *strdup_dst = alloc(strndup_len + strndup_ln2, ATEMP); \
738*7c356e86SAndroid Build Coastguard Worker \
739*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst, strdup_src, strndup_len); \
740*7c356e86SAndroid Build Coastguard Worker if (strndup_ln2) { \
741*7c356e86SAndroid Build Coastguard Worker strdup_dst[strndup_len - 1] = '/'; \
742*7c356e86SAndroid Build Coastguard Worker memcpy(strdup_dst + strndup_len, strdup_app, \
743*7c356e86SAndroid Build Coastguard Worker strndup_ln2); \
744*7c356e86SAndroid Build Coastguard Worker } \
745*7c356e86SAndroid Build Coastguard Worker (d) = strdup_dst; \
746*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
747*7c356e86SAndroid Build Coastguard Worker
748*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_SMALL
749*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NOPWNAM
750*7c356e86SAndroid Build Coastguard Worker #define MKSH_NOPWNAM /* defined */
751*7c356e86SAndroid Build Coastguard Worker #endif
752*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_S_NOVI
753*7c356e86SAndroid Build Coastguard Worker #define MKSH_S_NOVI 1
754*7c356e86SAndroid Build Coastguard Worker #endif
755*7c356e86SAndroid Build Coastguard Worker #endif
756*7c356e86SAndroid Build Coastguard Worker
757*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_S_NOVI
758*7c356e86SAndroid Build Coastguard Worker #define MKSH_S_NOVI 0
759*7c356e86SAndroid Build Coastguard Worker #endif
760*7c356e86SAndroid Build Coastguard Worker
761*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_NOPROSPECTOFWORK) && !defined(MKSH_UNEMPLOYED)
762*7c356e86SAndroid Build Coastguard Worker #define MKSH_UNEMPLOYED 1
763*7c356e86SAndroid Build Coastguard Worker #endif
764*7c356e86SAndroid Build Coastguard Worker
765*7c356e86SAndroid Build Coastguard Worker #define NUFILE 32 /* Number of user-accessible files */
766*7c356e86SAndroid Build Coastguard Worker #define FDBASE 10 /* First file usable by Shell */
767*7c356e86SAndroid Build Coastguard Worker
768*7c356e86SAndroid Build Coastguard Worker /*
769*7c356e86SAndroid Build Coastguard Worker * simple grouping allocator
770*7c356e86SAndroid Build Coastguard Worker */
771*7c356e86SAndroid Build Coastguard Worker
772*7c356e86SAndroid Build Coastguard Worker
773*7c356e86SAndroid Build Coastguard Worker /* 0. OS API: where to get memory from and how to free it (grouped) */
774*7c356e86SAndroid Build Coastguard Worker
775*7c356e86SAndroid Build Coastguard Worker /* malloc(3)/realloc(3) -> free(3) for use by the memory allocator */
776*7c356e86SAndroid Build Coastguard Worker #define malloc_osi(sz) malloc(sz)
777*7c356e86SAndroid Build Coastguard Worker #define realloc_osi(p,sz) realloc((p), (sz))
778*7c356e86SAndroid Build Coastguard Worker #define free_osimalloc(p) free(p)
779*7c356e86SAndroid Build Coastguard Worker
780*7c356e86SAndroid Build Coastguard Worker /* malloc(3)/realloc(3) -> free(3) for use by mksh code */
781*7c356e86SAndroid Build Coastguard Worker #define malloc_osfunc(sz) malloc(sz)
782*7c356e86SAndroid Build Coastguard Worker #define realloc_osfunc(p,sz) realloc((p), (sz))
783*7c356e86SAndroid Build Coastguard Worker #define free_osfunc(p) free(p)
784*7c356e86SAndroid Build Coastguard Worker
785*7c356e86SAndroid Build Coastguard Worker #if HAVE_MKNOD
786*7c356e86SAndroid Build Coastguard Worker /* setmode(3) -> free(3) */
787*7c356e86SAndroid Build Coastguard Worker #define free_ossetmode(p) free(p)
788*7c356e86SAndroid Build Coastguard Worker #endif
789*7c356e86SAndroid Build Coastguard Worker
790*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH__NO_PATH_MAX
791*7c356e86SAndroid Build Coastguard Worker /* GNU libc: get_current_dir_name(3) -> free(3) */
792*7c356e86SAndroid Build Coastguard Worker #define free_gnu_gcdn(p) free(p)
793*7c356e86SAndroid Build Coastguard Worker #endif
794*7c356e86SAndroid Build Coastguard Worker
795*7c356e86SAndroid Build Coastguard Worker
796*7c356e86SAndroid Build Coastguard Worker /* 1. internal structure */
797*7c356e86SAndroid Build Coastguard Worker struct lalloc_common {
798*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *next;
799*7c356e86SAndroid Build Coastguard Worker };
800*7c356e86SAndroid Build Coastguard Worker
801*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
802*7c356e86SAndroid Build Coastguard Worker struct lalloc_item {
803*7c356e86SAndroid Build Coastguard Worker struct lalloc_common *next;
804*7c356e86SAndroid Build Coastguard Worker size_t len;
805*7c356e86SAndroid Build Coastguard Worker char dummy[8192 - sizeof(struct lalloc_common *) - sizeof(size_t)];
806*7c356e86SAndroid Build Coastguard Worker };
807*7c356e86SAndroid Build Coastguard Worker #endif
808*7c356e86SAndroid Build Coastguard Worker
809*7c356e86SAndroid Build Coastguard Worker /* 2. sizes */
810*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
811*7c356e86SAndroid Build Coastguard Worker #define ALLOC_ITEM struct lalloc_item
812*7c356e86SAndroid Build Coastguard Worker #define ALLOC_OVERHEAD 0
813*7c356e86SAndroid Build Coastguard Worker #else
814*7c356e86SAndroid Build Coastguard Worker #define ALLOC_ITEM struct lalloc_common
815*7c356e86SAndroid Build Coastguard Worker #define ALLOC_OVERHEAD (sizeof(ALLOC_ITEM))
816*7c356e86SAndroid Build Coastguard Worker #endif
817*7c356e86SAndroid Build Coastguard Worker
818*7c356e86SAndroid Build Coastguard Worker /* 3. group structure */
819*7c356e86SAndroid Build Coastguard Worker typedef struct lalloc_common Area;
820*7c356e86SAndroid Build Coastguard Worker
821*7c356e86SAndroid Build Coastguard Worker
822*7c356e86SAndroid Build Coastguard Worker EXTERN Area aperm; /* permanent object space */
823*7c356e86SAndroid Build Coastguard Worker #define APERM &aperm
824*7c356e86SAndroid Build Coastguard Worker #define ATEMP &e->area
825*7c356e86SAndroid Build Coastguard Worker
826*7c356e86SAndroid Build Coastguard Worker /*
827*7c356e86SAndroid Build Coastguard Worker * flags (the order of these enums MUST match the order in misc.c(options[]))
828*7c356e86SAndroid Build Coastguard Worker */
829*7c356e86SAndroid Build Coastguard Worker enum sh_flag {
830*7c356e86SAndroid Build Coastguard Worker #define SHFLAGS_ENUMS
831*7c356e86SAndroid Build Coastguard Worker #include "sh_flags.gen"
832*7c356e86SAndroid Build Coastguard Worker FNFLAGS /* (place holder: how many flags are there) */
833*7c356e86SAndroid Build Coastguard Worker };
834*7c356e86SAndroid Build Coastguard Worker
835*7c356e86SAndroid Build Coastguard Worker #define Flag(f) (shell_flags[(int)(f)])
836*7c356e86SAndroid Build Coastguard Worker #define UTFMODE Flag(FUNNYCODE)
837*7c356e86SAndroid Build Coastguard Worker
838*7c356e86SAndroid Build Coastguard Worker /*
839*7c356e86SAndroid Build Coastguard Worker * parsing & execution environment
840*7c356e86SAndroid Build Coastguard Worker *
841*7c356e86SAndroid Build Coastguard Worker * note that kshlongjmp MUST NOT be passed 0 as second argument!
842*7c356e86SAndroid Build Coastguard Worker */
843*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_NO_SIGSETJMP
844*7c356e86SAndroid Build Coastguard Worker #define kshjmp_buf jmp_buf
845*7c356e86SAndroid Build Coastguard Worker #define kshsetjmp(jbuf) _setjmp(jbuf)
846*7c356e86SAndroid Build Coastguard Worker #define kshlongjmp _longjmp
847*7c356e86SAndroid Build Coastguard Worker #else
848*7c356e86SAndroid Build Coastguard Worker #define kshjmp_buf sigjmp_buf
849*7c356e86SAndroid Build Coastguard Worker #define kshsetjmp(jbuf) sigsetjmp((jbuf), 0)
850*7c356e86SAndroid Build Coastguard Worker #define kshlongjmp siglongjmp
851*7c356e86SAndroid Build Coastguard Worker #endif
852*7c356e86SAndroid Build Coastguard Worker
853*7c356e86SAndroid Build Coastguard Worker struct sretrace_info;
854*7c356e86SAndroid Build Coastguard Worker struct yyrecursive_state;
855*7c356e86SAndroid Build Coastguard Worker
856*7c356e86SAndroid Build Coastguard Worker EXTERN struct sretrace_info *retrace_info;
857*7c356e86SAndroid Build Coastguard Worker EXTERN unsigned int subshell_nesting_type;
858*7c356e86SAndroid Build Coastguard Worker
859*7c356e86SAndroid Build Coastguard Worker extern struct env {
860*7c356e86SAndroid Build Coastguard Worker ALLOC_ITEM alloc_INT; /* internal, do not touch */
861*7c356e86SAndroid Build Coastguard Worker Area area; /* temporary allocation area */
862*7c356e86SAndroid Build Coastguard Worker struct env *oenv; /* link to previous environment */
863*7c356e86SAndroid Build Coastguard Worker struct block *loc; /* local variables and functions */
864*7c356e86SAndroid Build Coastguard Worker short *savefd; /* original redirected fds */
865*7c356e86SAndroid Build Coastguard Worker struct temp *temps; /* temp files */
866*7c356e86SAndroid Build Coastguard Worker /* saved parser recursion state */
867*7c356e86SAndroid Build Coastguard Worker struct yyrecursive_state *yyrecursive_statep;
868*7c356e86SAndroid Build Coastguard Worker kshjmp_buf jbuf; /* long jump back to env creator */
869*7c356e86SAndroid Build Coastguard Worker uint8_t type; /* environment type - see below */
870*7c356e86SAndroid Build Coastguard Worker uint8_t flags; /* EF_* */
871*7c356e86SAndroid Build Coastguard Worker } *e;
872*7c356e86SAndroid Build Coastguard Worker
873*7c356e86SAndroid Build Coastguard Worker /* struct env.type values */
874*7c356e86SAndroid Build Coastguard Worker #define E_NONE 0 /* dummy environment */
875*7c356e86SAndroid Build Coastguard Worker #define E_PARSE 1 /* parsing command # */
876*7c356e86SAndroid Build Coastguard Worker #define E_FUNC 2 /* executing function # */
877*7c356e86SAndroid Build Coastguard Worker #define E_INCL 3 /* including a file via . # */
878*7c356e86SAndroid Build Coastguard Worker #define E_EXEC 4 /* executing command tree */
879*7c356e86SAndroid Build Coastguard Worker #define E_LOOP 5 /* executing for/while # */
880*7c356e86SAndroid Build Coastguard Worker #define E_ERRH 6 /* general error handler # */
881*7c356e86SAndroid Build Coastguard Worker #define E_GONE 7 /* hidden in child */
882*7c356e86SAndroid Build Coastguard Worker #define E_EVAL 8 /* running eval # */
883*7c356e86SAndroid Build Coastguard Worker /* # indicates env has valid jbuf (see unwind()) */
884*7c356e86SAndroid Build Coastguard Worker
885*7c356e86SAndroid Build Coastguard Worker /* struct env.flag values */
886*7c356e86SAndroid Build Coastguard Worker #define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
887*7c356e86SAndroid Build Coastguard Worker #define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
888*7c356e86SAndroid Build Coastguard Worker #define EF_IN_EVAL BIT(3) /* inside an eval */
889*7c356e86SAndroid Build Coastguard Worker
890*7c356e86SAndroid Build Coastguard Worker /* Do breaks/continues stop at env type e? */
891*7c356e86SAndroid Build Coastguard Worker #define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE || \
892*7c356e86SAndroid Build Coastguard Worker (t) == E_FUNC || (t) == E_INCL)
893*7c356e86SAndroid Build Coastguard Worker /* Do returns stop at env type e? */
894*7c356e86SAndroid Build Coastguard Worker #define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
895*7c356e86SAndroid Build Coastguard Worker
896*7c356e86SAndroid Build Coastguard Worker /* values for kshlongjmp(e->jbuf, i) */
897*7c356e86SAndroid Build Coastguard Worker /* note that i MUST NOT be zero */
898*7c356e86SAndroid Build Coastguard Worker #define LRETURN 1 /* return statement */
899*7c356e86SAndroid Build Coastguard Worker #define LEXIT 2 /* exit statement */
900*7c356e86SAndroid Build Coastguard Worker #define LERROR 3 /* errorf() called */
901*7c356e86SAndroid Build Coastguard Worker #define LERREXT 4 /* set -e caused */
902*7c356e86SAndroid Build Coastguard Worker #define LINTR 5 /* ^C noticed */
903*7c356e86SAndroid Build Coastguard Worker #define LBREAK 6 /* break statement */
904*7c356e86SAndroid Build Coastguard Worker #define LCONTIN 7 /* continue statement */
905*7c356e86SAndroid Build Coastguard Worker #define LSHELL 8 /* return to interactive shell() */
906*7c356e86SAndroid Build Coastguard Worker #define LAEXPR 9 /* error in arithmetic expression */
907*7c356e86SAndroid Build Coastguard Worker #define LLEAVE 10 /* untrappable exit/error */
908*7c356e86SAndroid Build Coastguard Worker
909*7c356e86SAndroid Build Coastguard Worker /* sort of shell global state */
910*7c356e86SAndroid Build Coastguard Worker EXTERN pid_t procpid; /* PID of executing process */
911*7c356e86SAndroid Build Coastguard Worker EXTERN int exstat; /* exit status */
912*7c356e86SAndroid Build Coastguard Worker EXTERN int subst_exstat; /* exit status of last $(..)/`..` */
913*7c356e86SAndroid Build Coastguard Worker EXTERN struct tbl *vp_pipest; /* global PIPESTATUS array */
914*7c356e86SAndroid Build Coastguard Worker EXTERN short trap_exstat; /* exit status before running a trap */
915*7c356e86SAndroid Build Coastguard Worker EXTERN uint8_t trap_nested; /* running nested traps */
916*7c356e86SAndroid Build Coastguard Worker EXTERN uint8_t shell_flags[FNFLAGS];
917*7c356e86SAndroid Build Coastguard Worker EXTERN uint8_t baseline_flags[FNFLAGS
918*7c356e86SAndroid Build Coastguard Worker #if !defined(MKSH_SMALL) || defined(DEBUG)
919*7c356e86SAndroid Build Coastguard Worker + 1
920*7c356e86SAndroid Build Coastguard Worker #endif
921*7c356e86SAndroid Build Coastguard Worker ];
922*7c356e86SAndroid Build Coastguard Worker EXTERN bool as_builtin; /* direct builtin call */
923*7c356e86SAndroid Build Coastguard Worker EXTERN const char *kshname; /* $0 */
924*7c356e86SAndroid Build Coastguard Worker EXTERN struct {
925*7c356e86SAndroid Build Coastguard Worker uid_t kshuid_v; /* real UID of shell at startup */
926*7c356e86SAndroid Build Coastguard Worker uid_t ksheuid_v; /* effective UID of shell */
927*7c356e86SAndroid Build Coastguard Worker gid_t kshgid_v; /* real GID of shell at startup */
928*7c356e86SAndroid Build Coastguard Worker gid_t kshegid_v; /* effective GID of shell */
929*7c356e86SAndroid Build Coastguard Worker pid_t kshpgrp_v; /* process group of shell */
930*7c356e86SAndroid Build Coastguard Worker pid_t kshppid_v; /* PID of parent of shell */
931*7c356e86SAndroid Build Coastguard Worker pid_t kshpid_v; /* $$, shell PID */
932*7c356e86SAndroid Build Coastguard Worker } rndsetupstate;
933*7c356e86SAndroid Build Coastguard Worker
934*7c356e86SAndroid Build Coastguard Worker #define kshpid rndsetupstate.kshpid_v
935*7c356e86SAndroid Build Coastguard Worker #define kshpgrp rndsetupstate.kshpgrp_v
936*7c356e86SAndroid Build Coastguard Worker #define kshuid rndsetupstate.kshuid_v
937*7c356e86SAndroid Build Coastguard Worker #define ksheuid rndsetupstate.ksheuid_v
938*7c356e86SAndroid Build Coastguard Worker #define kshgid rndsetupstate.kshgid_v
939*7c356e86SAndroid Build Coastguard Worker #define kshegid rndsetupstate.kshegid_v
940*7c356e86SAndroid Build Coastguard Worker #define kshppid rndsetupstate.kshppid_v
941*7c356e86SAndroid Build Coastguard Worker
942*7c356e86SAndroid Build Coastguard Worker
943*7c356e86SAndroid Build Coastguard Worker /* option processing */
944*7c356e86SAndroid Build Coastguard Worker #define OF_CMDLINE 0x01 /* command line */
945*7c356e86SAndroid Build Coastguard Worker #define OF_SET 0x02 /* set builtin */
946*7c356e86SAndroid Build Coastguard Worker #define OF_SPECIAL 0x04 /* a special variable changing */
947*7c356e86SAndroid Build Coastguard Worker #define OF_INTERNAL 0x08 /* set internally by shell */
948*7c356e86SAndroid Build Coastguard Worker #define OF_FIRSTTIME 0x10 /* as early as possible, once */
949*7c356e86SAndroid Build Coastguard Worker #define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
950*7c356e86SAndroid Build Coastguard Worker
951*7c356e86SAndroid Build Coastguard Worker /* null value for variable; comparison pointer for unset */
952*7c356e86SAndroid Build Coastguard Worker EXTERN char null[] E_INIT("");
953*7c356e86SAndroid Build Coastguard Worker
954*7c356e86SAndroid Build Coastguard Worker /* string pooling: do we rely on the compiler? */
955*7c356e86SAndroid Build Coastguard Worker #ifndef HAVE_STRING_POOLING
956*7c356e86SAndroid Build Coastguard Worker /* no, we use our own, saves quite some space */
957*7c356e86SAndroid Build Coastguard Worker #elif HAVE_STRING_POOLING == 2
958*7c356e86SAndroid Build Coastguard Worker /* “on demand” */
959*7c356e86SAndroid Build Coastguard Worker #ifdef __GNUC__
960*7c356e86SAndroid Build Coastguard Worker /* only for GCC 4 or later, older ones can get by without */
961*7c356e86SAndroid Build Coastguard Worker #if __GNUC__ < 4
962*7c356e86SAndroid Build Coastguard Worker #undef HAVE_STRING_POOLING
963*7c356e86SAndroid Build Coastguard Worker #endif
964*7c356e86SAndroid Build Coastguard Worker #else
965*7c356e86SAndroid Build Coastguard Worker /* not GCC, default to on */
966*7c356e86SAndroid Build Coastguard Worker #endif
967*7c356e86SAndroid Build Coastguard Worker #elif HAVE_STRING_POOLING == 0
968*7c356e86SAndroid Build Coastguard Worker /* default to on, unless explicitly set to 0 */
969*7c356e86SAndroid Build Coastguard Worker #undef HAVE_STRING_POOLING
970*7c356e86SAndroid Build Coastguard Worker #endif
971*7c356e86SAndroid Build Coastguard Worker
972*7c356e86SAndroid Build Coastguard Worker #ifndef HAVE_STRING_POOLING /* helpers for pooled strings */
973*7c356e86SAndroid Build Coastguard Worker EXTERN const char T4spaces[] E_INIT(" ");
974*7c356e86SAndroid Build Coastguard Worker #define T1space (Treal_sp2 + 5)
975*7c356e86SAndroid Build Coastguard Worker #define Tcolsp (Tf_sD_ + 2)
976*7c356e86SAndroid Build Coastguard Worker #define TC_IFSWS (TinitIFS + 4)
977*7c356e86SAndroid Build Coastguard Worker EXTERN const char TinitIFS[] E_INIT("IFS= \t\n");
978*7c356e86SAndroid Build Coastguard Worker EXTERN const char TFCEDIT_dollaru[] E_INIT("${FCEDIT:-/bin/ed} $_");
979*7c356e86SAndroid Build Coastguard Worker #define Tspdollaru (TFCEDIT_dollaru + 18)
980*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tsgdot[] E_INIT("*=.");
981*7c356e86SAndroid Build Coastguard Worker EXTERN const char Taugo[] E_INIT("augo");
982*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbracket[] E_INIT("[");
983*7c356e86SAndroid Build Coastguard Worker #define Tdot (Tsgdot + 2)
984*7c356e86SAndroid Build Coastguard Worker #define Talias (Tunalias + 2)
985*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbadnum[] E_INIT("bad number");
986*7c356e86SAndroid Build Coastguard Worker #define Tbadsubst (Tfg_badsubst + 10)
987*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbg[] E_INIT("bg");
988*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbad_bsize[] E_INIT("bad shf/buf/bsize");
989*7c356e86SAndroid Build Coastguard Worker #define Tbsize (Tbad_bsize + 12)
990*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbad_sig_ss[] E_INIT("%s: bad signal '%s'");
991*7c356e86SAndroid Build Coastguard Worker #define Tbad_sig_s (Tbad_sig_ss + 4)
992*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tsgbreak[] E_INIT("*=break");
993*7c356e86SAndroid Build Coastguard Worker #define Tbreak (Tsgbreak + 2)
994*7c356e86SAndroid Build Coastguard Worker EXTERN const char T__builtin[] E_INIT("-\\builtin");
995*7c356e86SAndroid Build Coastguard Worker #define T_builtin (T__builtin + 1)
996*7c356e86SAndroid Build Coastguard Worker #define Tbuiltin (T__builtin + 2)
997*7c356e86SAndroid Build Coastguard Worker EXTERN const char Toomem[] E_INIT("can't allocate %zu data bytes");
998*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tcant_cd[] E_INIT("restricted shell - can't cd");
999*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tcant_find[] E_INIT("can't find");
1000*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tcant_open[] E_INIT("can't open");
1001*7c356e86SAndroid Build Coastguard Worker #define Tbytes (Toomem + 24)
1002*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tbcat[] E_INIT("!cat");
1003*7c356e86SAndroid Build Coastguard Worker #define Tcat (Tbcat + 1)
1004*7c356e86SAndroid Build Coastguard Worker #define Tcd (Tcant_cd + 25)
1005*7c356e86SAndroid Build Coastguard Worker #define T_command (T_funny_command + 9)
1006*7c356e86SAndroid Build Coastguard Worker #define Tcommand (T_funny_command + 10)
1007*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tsgcontinue[] E_INIT("*=continue");
1008*7c356e86SAndroid Build Coastguard Worker #define Tcontinue (Tsgcontinue + 2)
1009*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tcreate[] E_INIT("create");
1010*7c356e86SAndroid Build Coastguard Worker EXTERN const char TELIF_unexpected[] E_INIT("TELIF unexpected");
1011*7c356e86SAndroid Build Coastguard Worker EXTERN const char TEXECSHELL[] E_INIT("EXECSHELL");
1012*7c356e86SAndroid Build Coastguard Worker EXTERN const char TENV[] E_INIT("ENV");
1013*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tdsgexport[] E_INIT("^*=export");
1014*7c356e86SAndroid Build Coastguard Worker #define Texport (Tdsgexport + 3)
1015*7c356e86SAndroid Build Coastguard Worker #ifdef __OS2__
1016*7c356e86SAndroid Build Coastguard Worker EXTERN const char Textproc[] E_INIT("extproc");
1017*7c356e86SAndroid Build Coastguard Worker #endif
1018*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tfalse[] E_INIT("false");
1019*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tfg[] E_INIT("fg");
1020*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tfg_badsubst[] E_INIT("fileglob: bad substitution");
1021*7c356e86SAndroid Build Coastguard Worker #define Tfile (Tfile_fd + 20)
1022*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tfile_fd[] E_INIT("function definition file");
1023*7c356e86SAndroid Build Coastguard Worker EXTERN const char TFPATH[] E_INIT("FPATH");
1024*7c356e86SAndroid Build Coastguard Worker EXTERN const char T_function[] E_INIT(" function");
1025*7c356e86SAndroid Build Coastguard Worker #define Tfunction (T_function + 1)
1026*7c356e86SAndroid Build Coastguard Worker EXTERN const char T_funny_command[] E_INIT("funny $()-command");
1027*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tgetopts[] E_INIT("getopts");
1028*7c356e86SAndroid Build Coastguard Worker #define Thistory (Tnot_in_history + 7)
1029*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tintovfl[] E_INIT("integer overflow %zu %c %zu prevented");
1030*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tinvname[] E_INIT("%s: invalid %s name");
1031*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tjobs[] E_INIT("jobs");
1032*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tjob_not_started[] E_INIT("job not started");
1033*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tmksh[] E_INIT("mksh");
1034*7c356e86SAndroid Build Coastguard Worker #define Tname (Tinvname + 15)
1035*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tno_args[] E_INIT("missing argument");
1036*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tno_OLDPWD[] E_INIT("no OLDPWD");
1037*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tnot_ident[] E_INIT("is not an identifier");
1038*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tnot_in_history[] E_INIT("not in history");
1039*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tnot_found_s[] E_INIT("%s not found");
1040*7c356e86SAndroid Build Coastguard Worker #define Tnot_found (Tnot_found_s + 3)
1041*7c356e86SAndroid Build Coastguard Worker #define Tnot_started (Tjob_not_started + 4)
1042*7c356e86SAndroid Build Coastguard Worker #define TOLDPWD (Tno_OLDPWD + 3)
1043*7c356e86SAndroid Build Coastguard Worker #define Topen (Tcant_open + 6)
1044*7c356e86SAndroid Build Coastguard Worker EXTERN const char To_o_reset[] E_INIT(" -o .reset");
1045*7c356e86SAndroid Build Coastguard Worker #define To_reset (To_o_reset + 4)
1046*7c356e86SAndroid Build Coastguard Worker #define TPATH (TFPATH + 1)
1047*7c356e86SAndroid Build Coastguard Worker #define Tpo (Tset_po + 4)
1048*7c356e86SAndroid Build Coastguard Worker #define Tpv (TpVv + 1)
1049*7c356e86SAndroid Build Coastguard Worker EXTERN const char TpVv[] E_INIT("Vpv");
1050*7c356e86SAndroid Build Coastguard Worker #define TPWD (Tno_OLDPWD + 6)
1051*7c356e86SAndroid Build Coastguard Worker #define Tread (Tshf_read + 4)
1052*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tdsgreadonly[] E_INIT("^*=readonly");
1053*7c356e86SAndroid Build Coastguard Worker #define Treadonly (Tdsgreadonly + 3)
1054*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tredirection_dup[] E_INIT("can't finish (dup) redirection");
1055*7c356e86SAndroid Build Coastguard Worker #define Tredirection (Tredirection_dup + 19)
1056*7c356e86SAndroid Build Coastguard Worker #define Treal_sp1 (Treal_sp2 + 1)
1057*7c356e86SAndroid Build Coastguard Worker EXTERN const char Treal_sp2[] E_INIT(" real ");
1058*7c356e86SAndroid Build Coastguard Worker EXTERN const char TREPLY[] E_INIT("REPLY");
1059*7c356e86SAndroid Build Coastguard Worker EXTERN const char Treq_arg[] E_INIT("requires an argument");
1060*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tselect[] E_INIT("select");
1061*7c356e86SAndroid Build Coastguard Worker #define Tset (Tf_parm + 18)
1062*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tset_po[] E_INIT("set +o");
1063*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tsghset[] E_INIT("*=#set");
1064*7c356e86SAndroid Build Coastguard Worker #define Tsh (Tmksh + 2)
1065*7c356e86SAndroid Build Coastguard Worker #define TSHELL (TEXECSHELL + 4)
1066*7c356e86SAndroid Build Coastguard Worker #define Tshell (Ttoo_many_files + 23)
1067*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tshf_read[] E_INIT("shf_read");
1068*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tshf_write[] E_INIT("shf_write");
1069*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tgsource[] E_INIT("=source");
1070*7c356e86SAndroid Build Coastguard Worker #define Tsource (Tgsource + 1)
1071*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tj_suspend[] E_INIT("j_suspend");
1072*7c356e86SAndroid Build Coastguard Worker #define Tsuspend (Tj_suspend + 2)
1073*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tsynerr[] E_INIT("syntax error");
1074*7c356e86SAndroid Build Coastguard Worker EXTERN const char Ttime[] E_INIT("time");
1075*7c356e86SAndroid Build Coastguard Worker EXTERN const char Ttoo_many_args[] E_INIT("too many arguments");
1076*7c356e86SAndroid Build Coastguard Worker EXTERN const char Ttoo_many_files[] E_INIT("too many open files in shell");
1077*7c356e86SAndroid Build Coastguard Worker EXTERN const char Ttrue[] E_INIT("true");
1078*7c356e86SAndroid Build Coastguard Worker EXTERN const char Ttty_fd_dupof[] E_INIT("dup of tty fd");
1079*7c356e86SAndroid Build Coastguard Worker #define Ttty_fd (Ttty_fd_dupof + 7)
1080*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tdgtypeset[] E_INIT("^=typeset");
1081*7c356e86SAndroid Build Coastguard Worker #define Ttypeset (Tdgtypeset + 2)
1082*7c356e86SAndroid Build Coastguard Worker #define Tugo (Taugo + 1)
1083*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tunalias[] E_INIT("unalias");
1084*7c356e86SAndroid Build Coastguard Worker #define Tunexpected (TELIF_unexpected + 6)
1085*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tunexpected_type[] E_INIT("%s: unexpected %s type %d");
1086*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tunknown_option[] E_INIT("unknown option");
1087*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tunwind[] E_INIT("unwind");
1088*7c356e86SAndroid Build Coastguard Worker #define Tuser_sp1 (Tuser_sp2 + 1)
1089*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tuser_sp2[] E_INIT(" user ");
1090*7c356e86SAndroid Build Coastguard Worker #define Twrite (Tshf_write + 4)
1091*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf__S[] E_INIT(" %S");
1092*7c356e86SAndroid Build Coastguard Worker #define Tf__d (Tunexpected_type + 22)
1093*7c356e86SAndroid Build Coastguard Worker #define Tf_ss (Tf__ss + 1)
1094*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf__ss[] E_INIT(" %s%s");
1095*7c356e86SAndroid Build Coastguard Worker #define Tf__sN (Tf_s_s_sN + 5)
1096*7c356e86SAndroid Build Coastguard Worker #define Tf_T (Tf_s_T + 3)
1097*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_dN[] E_INIT("%d\n");
1098*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_s_[] E_INIT("%s ");
1099*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_s_T[] E_INIT("%s %T");
1100*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_s_s_sN[] E_INIT("%s %s %s\n");
1101*7c356e86SAndroid Build Coastguard Worker #define Tf_s_s (Tf_sD_s_s + 4)
1102*7c356e86SAndroid Build Coastguard Worker #define Tf__s_s (Tf_sD_s_s + 3)
1103*7c356e86SAndroid Build Coastguard Worker #define Tf_s_sD_s (Tf_cant_ss_s + 6)
1104*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_optfoo[] E_INIT("%s%s-%c: %s");
1105*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sD_[] E_INIT("%s: ");
1106*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_szs[] E_INIT("%s: %zd %s");
1107*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_parm[] E_INIT("%s: parameter not set");
1108*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_coproc[] E_INIT("-p: %s");
1109*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_cant_s[] E_INIT("%s: can't %s");
1110*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_cant_ss_s[] E_INIT("can't %s %s: %s");
1111*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_heredoc[] E_INIT("here document '%s' unclosed");
1112*7c356e86SAndroid Build Coastguard Worker #if HAVE_MKNOD
1113*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_nonnum[] E_INIT("non-numeric %s %s '%s'");
1114*7c356e86SAndroid Build Coastguard Worker #endif
1115*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_S_[] E_INIT("%S ");
1116*7c356e86SAndroid Build Coastguard Worker #define Tf_S (Tf__S + 1)
1117*7c356e86SAndroid Build Coastguard Worker #define Tf_lu (Tf_toolarge + 17)
1118*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_toolarge[] E_INIT("%s %s too large: %lu");
1119*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_ldfailed[] E_INIT("%s %s(%d, %ld) failed: %s");
1120*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sD_s_sD_s[] E_INIT("%s: %s %s: %s");
1121*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_toomany[] E_INIT("too many %ss");
1122*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sd[] E_INIT("%s %d");
1123*7c356e86SAndroid Build Coastguard Worker #define Tf_s (Tf_temp + 28)
1124*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tft_end[] E_INIT("%;");
1125*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tft_R[] E_INIT("%R");
1126*7c356e86SAndroid Build Coastguard Worker #define Tf_d (Tunexpected_type + 23)
1127*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sD_s_qs[] E_INIT("%s: %s '%s'");
1128*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_ro[] E_INIT("read-only: %s");
1129*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_flags[] E_INIT("%s: flags 0x%X");
1130*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_temp[] E_INIT("can't %s temporary file %s: %s");
1131*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_ssfaileds[] E_INIT("%s: %s failed: %s");
1132*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sD_sD_s[] E_INIT("%s: %s: %s");
1133*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf__c_[] E_INIT("-%c ");
1134*7c356e86SAndroid Build Coastguard Worker EXTERN const char Tf_sD_s_s[] E_INIT("%s: %s %s");
1135*7c356e86SAndroid Build Coastguard Worker #define Tf_sN (Tf_s_s_sN + 6)
1136*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_s (Tf_temp + 24)
1137*7c356e86SAndroid Build Coastguard Worker EXTERN const char T_devtty[] E_INIT("/dev/tty");
1138*7c356e86SAndroid Build Coastguard Worker #else /* helpers for string pooling */
1139*7c356e86SAndroid Build Coastguard Worker #define T4spaces " "
1140*7c356e86SAndroid Build Coastguard Worker #define T1space " "
1141*7c356e86SAndroid Build Coastguard Worker #define Tcolsp ": "
1142*7c356e86SAndroid Build Coastguard Worker #define TC_IFSWS " \t\n"
1143*7c356e86SAndroid Build Coastguard Worker #define TinitIFS "IFS= \t\n"
1144*7c356e86SAndroid Build Coastguard Worker #define TFCEDIT_dollaru "${FCEDIT:-/bin/ed} $_"
1145*7c356e86SAndroid Build Coastguard Worker #define Tspdollaru " $_"
1146*7c356e86SAndroid Build Coastguard Worker #define Tsgdot "*=."
1147*7c356e86SAndroid Build Coastguard Worker #define Taugo "augo"
1148*7c356e86SAndroid Build Coastguard Worker #define Tbracket "["
1149*7c356e86SAndroid Build Coastguard Worker #define Tdot "."
1150*7c356e86SAndroid Build Coastguard Worker #define Talias "alias"
1151*7c356e86SAndroid Build Coastguard Worker #define Tbadnum "bad number"
1152*7c356e86SAndroid Build Coastguard Worker #define Tbadsubst "bad substitution"
1153*7c356e86SAndroid Build Coastguard Worker #define Tbg "bg"
1154*7c356e86SAndroid Build Coastguard Worker #define Tbad_bsize "bad shf/buf/bsize"
1155*7c356e86SAndroid Build Coastguard Worker #define Tbsize "bsize"
1156*7c356e86SAndroid Build Coastguard Worker #define Tbad_sig_ss "%s: bad signal '%s'"
1157*7c356e86SAndroid Build Coastguard Worker #define Tbad_sig_s "bad signal '%s'"
1158*7c356e86SAndroid Build Coastguard Worker #define Tsgbreak "*=break"
1159*7c356e86SAndroid Build Coastguard Worker #define Tbreak "break"
1160*7c356e86SAndroid Build Coastguard Worker #define T__builtin "-\\builtin"
1161*7c356e86SAndroid Build Coastguard Worker #define T_builtin "\\builtin"
1162*7c356e86SAndroid Build Coastguard Worker #define Tbuiltin "builtin"
1163*7c356e86SAndroid Build Coastguard Worker #define Toomem "can't allocate %zu data bytes"
1164*7c356e86SAndroid Build Coastguard Worker #define Tcant_cd "restricted shell - can't cd"
1165*7c356e86SAndroid Build Coastguard Worker #define Tcant_find "can't find"
1166*7c356e86SAndroid Build Coastguard Worker #define Tcant_open "can't open"
1167*7c356e86SAndroid Build Coastguard Worker #define Tbytes "bytes"
1168*7c356e86SAndroid Build Coastguard Worker #define Tbcat "!cat"
1169*7c356e86SAndroid Build Coastguard Worker #define Tcat "cat"
1170*7c356e86SAndroid Build Coastguard Worker #define Tcd "cd"
1171*7c356e86SAndroid Build Coastguard Worker #define T_command "-command"
1172*7c356e86SAndroid Build Coastguard Worker #define Tcommand "command"
1173*7c356e86SAndroid Build Coastguard Worker #define Tsgcontinue "*=continue"
1174*7c356e86SAndroid Build Coastguard Worker #define Tcontinue "continue"
1175*7c356e86SAndroid Build Coastguard Worker #define Tcreate "create"
1176*7c356e86SAndroid Build Coastguard Worker #define TELIF_unexpected "TELIF unexpected"
1177*7c356e86SAndroid Build Coastguard Worker #define TEXECSHELL "EXECSHELL"
1178*7c356e86SAndroid Build Coastguard Worker #define TENV "ENV"
1179*7c356e86SAndroid Build Coastguard Worker #define Tdsgexport "^*=export"
1180*7c356e86SAndroid Build Coastguard Worker #define Texport "export"
1181*7c356e86SAndroid Build Coastguard Worker #ifdef __OS2__
1182*7c356e86SAndroid Build Coastguard Worker #define Textproc "extproc"
1183*7c356e86SAndroid Build Coastguard Worker #endif
1184*7c356e86SAndroid Build Coastguard Worker #define Tfalse "false"
1185*7c356e86SAndroid Build Coastguard Worker #define Tfg "fg"
1186*7c356e86SAndroid Build Coastguard Worker #define Tfg_badsubst "fileglob: bad substitution"
1187*7c356e86SAndroid Build Coastguard Worker #define Tfile "file"
1188*7c356e86SAndroid Build Coastguard Worker #define Tfile_fd "function definition file"
1189*7c356e86SAndroid Build Coastguard Worker #define TFPATH "FPATH"
1190*7c356e86SAndroid Build Coastguard Worker #define T_function " function"
1191*7c356e86SAndroid Build Coastguard Worker #define Tfunction "function"
1192*7c356e86SAndroid Build Coastguard Worker #define T_funny_command "funny $()-command"
1193*7c356e86SAndroid Build Coastguard Worker #define Tgetopts "getopts"
1194*7c356e86SAndroid Build Coastguard Worker #define Thistory "history"
1195*7c356e86SAndroid Build Coastguard Worker #define Tintovfl "integer overflow %zu %c %zu prevented"
1196*7c356e86SAndroid Build Coastguard Worker #define Tinvname "%s: invalid %s name"
1197*7c356e86SAndroid Build Coastguard Worker #define Tjobs "jobs"
1198*7c356e86SAndroid Build Coastguard Worker #define Tjob_not_started "job not started"
1199*7c356e86SAndroid Build Coastguard Worker #define Tmksh "mksh"
1200*7c356e86SAndroid Build Coastguard Worker #define Tname "name"
1201*7c356e86SAndroid Build Coastguard Worker #define Tno_args "missing argument"
1202*7c356e86SAndroid Build Coastguard Worker #define Tno_OLDPWD "no OLDPWD"
1203*7c356e86SAndroid Build Coastguard Worker #define Tnot_ident "is not an identifier"
1204*7c356e86SAndroid Build Coastguard Worker #define Tnot_in_history "not in history"
1205*7c356e86SAndroid Build Coastguard Worker #define Tnot_found_s "%s not found"
1206*7c356e86SAndroid Build Coastguard Worker #define Tnot_found "not found"
1207*7c356e86SAndroid Build Coastguard Worker #define Tnot_started "not started"
1208*7c356e86SAndroid Build Coastguard Worker #define TOLDPWD "OLDPWD"
1209*7c356e86SAndroid Build Coastguard Worker #define Topen "open"
1210*7c356e86SAndroid Build Coastguard Worker #define To_o_reset " -o .reset"
1211*7c356e86SAndroid Build Coastguard Worker #define To_reset ".reset"
1212*7c356e86SAndroid Build Coastguard Worker #define TPATH "PATH"
1213*7c356e86SAndroid Build Coastguard Worker #define Tpo "+o"
1214*7c356e86SAndroid Build Coastguard Worker #define Tpv "pv"
1215*7c356e86SAndroid Build Coastguard Worker #define TpVv "Vpv"
1216*7c356e86SAndroid Build Coastguard Worker #define TPWD "PWD"
1217*7c356e86SAndroid Build Coastguard Worker #define Tread "read"
1218*7c356e86SAndroid Build Coastguard Worker #define Tdsgreadonly "^*=readonly"
1219*7c356e86SAndroid Build Coastguard Worker #define Treadonly "readonly"
1220*7c356e86SAndroid Build Coastguard Worker #define Tredirection_dup "can't finish (dup) redirection"
1221*7c356e86SAndroid Build Coastguard Worker #define Tredirection "redirection"
1222*7c356e86SAndroid Build Coastguard Worker #define Treal_sp1 "real "
1223*7c356e86SAndroid Build Coastguard Worker #define Treal_sp2 " real "
1224*7c356e86SAndroid Build Coastguard Worker #define TREPLY "REPLY"
1225*7c356e86SAndroid Build Coastguard Worker #define Treq_arg "requires an argument"
1226*7c356e86SAndroid Build Coastguard Worker #define Tselect "select"
1227*7c356e86SAndroid Build Coastguard Worker #define Tset "set"
1228*7c356e86SAndroid Build Coastguard Worker #define Tset_po "set +o"
1229*7c356e86SAndroid Build Coastguard Worker #define Tsghset "*=#set"
1230*7c356e86SAndroid Build Coastguard Worker #define Tsh "sh"
1231*7c356e86SAndroid Build Coastguard Worker #define TSHELL "SHELL"
1232*7c356e86SAndroid Build Coastguard Worker #define Tshell "shell"
1233*7c356e86SAndroid Build Coastguard Worker #define Tshf_read "shf_read"
1234*7c356e86SAndroid Build Coastguard Worker #define Tshf_write "shf_write"
1235*7c356e86SAndroid Build Coastguard Worker #define Tgsource "=source"
1236*7c356e86SAndroid Build Coastguard Worker #define Tsource "source"
1237*7c356e86SAndroid Build Coastguard Worker #define Tj_suspend "j_suspend"
1238*7c356e86SAndroid Build Coastguard Worker #define Tsuspend "suspend"
1239*7c356e86SAndroid Build Coastguard Worker #define Tsynerr "syntax error"
1240*7c356e86SAndroid Build Coastguard Worker #define Ttime "time"
1241*7c356e86SAndroid Build Coastguard Worker #define Ttoo_many_args "too many arguments"
1242*7c356e86SAndroid Build Coastguard Worker #define Ttoo_many_files "too many open files in shell"
1243*7c356e86SAndroid Build Coastguard Worker #define Ttrue "true"
1244*7c356e86SAndroid Build Coastguard Worker #define Ttty_fd_dupof "dup of tty fd"
1245*7c356e86SAndroid Build Coastguard Worker #define Ttty_fd "tty fd"
1246*7c356e86SAndroid Build Coastguard Worker #define Tdgtypeset "^=typeset"
1247*7c356e86SAndroid Build Coastguard Worker #define Ttypeset "typeset"
1248*7c356e86SAndroid Build Coastguard Worker #define Tugo "ugo"
1249*7c356e86SAndroid Build Coastguard Worker #define Tunalias "unalias"
1250*7c356e86SAndroid Build Coastguard Worker #define Tunexpected "unexpected"
1251*7c356e86SAndroid Build Coastguard Worker #define Tunexpected_type "%s: unexpected %s type %d"
1252*7c356e86SAndroid Build Coastguard Worker #define Tunknown_option "unknown option"
1253*7c356e86SAndroid Build Coastguard Worker #define Tunwind "unwind"
1254*7c356e86SAndroid Build Coastguard Worker #define Tuser_sp1 "user "
1255*7c356e86SAndroid Build Coastguard Worker #define Tuser_sp2 " user "
1256*7c356e86SAndroid Build Coastguard Worker #define Twrite "write"
1257*7c356e86SAndroid Build Coastguard Worker #define Tf__S " %S"
1258*7c356e86SAndroid Build Coastguard Worker #define Tf__d " %d"
1259*7c356e86SAndroid Build Coastguard Worker #define Tf_ss "%s%s"
1260*7c356e86SAndroid Build Coastguard Worker #define Tf__ss " %s%s"
1261*7c356e86SAndroid Build Coastguard Worker #define Tf__sN " %s\n"
1262*7c356e86SAndroid Build Coastguard Worker #define Tf_T "%T"
1263*7c356e86SAndroid Build Coastguard Worker #define Tf_dN "%d\n"
1264*7c356e86SAndroid Build Coastguard Worker #define Tf_s_ "%s "
1265*7c356e86SAndroid Build Coastguard Worker #define Tf_s_T "%s %T"
1266*7c356e86SAndroid Build Coastguard Worker #define Tf_s_s_sN "%s %s %s\n"
1267*7c356e86SAndroid Build Coastguard Worker #define Tf_s_s "%s %s"
1268*7c356e86SAndroid Build Coastguard Worker #define Tf__s_s " %s %s"
1269*7c356e86SAndroid Build Coastguard Worker #define Tf_s_sD_s "%s %s: %s"
1270*7c356e86SAndroid Build Coastguard Worker #define Tf_optfoo "%s%s-%c: %s"
1271*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_ "%s: "
1272*7c356e86SAndroid Build Coastguard Worker #define Tf_szs "%s: %zd %s"
1273*7c356e86SAndroid Build Coastguard Worker #define Tf_parm "%s: parameter not set"
1274*7c356e86SAndroid Build Coastguard Worker #define Tf_coproc "-p: %s"
1275*7c356e86SAndroid Build Coastguard Worker #define Tf_cant_s "%s: can't %s"
1276*7c356e86SAndroid Build Coastguard Worker #define Tf_cant_ss_s "can't %s %s: %s"
1277*7c356e86SAndroid Build Coastguard Worker #define Tf_heredoc "here document '%s' unclosed"
1278*7c356e86SAndroid Build Coastguard Worker #if HAVE_MKNOD
1279*7c356e86SAndroid Build Coastguard Worker #define Tf_nonnum "non-numeric %s %s '%s'"
1280*7c356e86SAndroid Build Coastguard Worker #endif
1281*7c356e86SAndroid Build Coastguard Worker #define Tf_S_ "%S "
1282*7c356e86SAndroid Build Coastguard Worker #define Tf_S "%S"
1283*7c356e86SAndroid Build Coastguard Worker #define Tf_lu "%lu"
1284*7c356e86SAndroid Build Coastguard Worker #define Tf_toolarge "%s %s too large: %lu"
1285*7c356e86SAndroid Build Coastguard Worker #define Tf_ldfailed "%s %s(%d, %ld) failed: %s"
1286*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_s_sD_s "%s: %s %s: %s"
1287*7c356e86SAndroid Build Coastguard Worker #define Tf_toomany "too many %ss"
1288*7c356e86SAndroid Build Coastguard Worker #define Tf_sd "%s %d"
1289*7c356e86SAndroid Build Coastguard Worker #define Tf_s "%s"
1290*7c356e86SAndroid Build Coastguard Worker #define Tft_end "%;"
1291*7c356e86SAndroid Build Coastguard Worker #define Tft_R "%R"
1292*7c356e86SAndroid Build Coastguard Worker #define Tf_d "%d"
1293*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_s_qs "%s: %s '%s'"
1294*7c356e86SAndroid Build Coastguard Worker #define Tf_ro "read-only: %s"
1295*7c356e86SAndroid Build Coastguard Worker #define Tf_flags "%s: flags 0x%X"
1296*7c356e86SAndroid Build Coastguard Worker #define Tf_temp "can't %s temporary file %s: %s"
1297*7c356e86SAndroid Build Coastguard Worker #define Tf_ssfaileds "%s: %s failed: %s"
1298*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_sD_s "%s: %s: %s"
1299*7c356e86SAndroid Build Coastguard Worker #define Tf__c_ "-%c "
1300*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_s_s "%s: %s %s"
1301*7c356e86SAndroid Build Coastguard Worker #define Tf_sN "%s\n"
1302*7c356e86SAndroid Build Coastguard Worker #define Tf_sD_s "%s: %s"
1303*7c356e86SAndroid Build Coastguard Worker #define T_devtty "/dev/tty"
1304*7c356e86SAndroid Build Coastguard Worker #endif /* end of string pooling */
1305*7c356e86SAndroid Build Coastguard Worker
1306*7c356e86SAndroid Build Coastguard Worker typedef uint8_t Temp_type;
1307*7c356e86SAndroid Build Coastguard Worker /* expanded heredoc */
1308*7c356e86SAndroid Build Coastguard Worker #define TT_HEREDOC_EXP 0
1309*7c356e86SAndroid Build Coastguard Worker /* temporary file used for history editing (fc -e) */
1310*7c356e86SAndroid Build Coastguard Worker #define TT_HIST_EDIT 1
1311*7c356e86SAndroid Build Coastguard Worker /* temporary file used during in-situ command substitution */
1312*7c356e86SAndroid Build Coastguard Worker #define TT_FUNSUB 2
1313*7c356e86SAndroid Build Coastguard Worker
1314*7c356e86SAndroid Build Coastguard Worker /* temp/heredoc files. The file is removed when the struct is freed. */
1315*7c356e86SAndroid Build Coastguard Worker struct temp {
1316*7c356e86SAndroid Build Coastguard Worker struct temp *next;
1317*7c356e86SAndroid Build Coastguard Worker struct shf *shf;
1318*7c356e86SAndroid Build Coastguard Worker /* pid of process parsed here-doc */
1319*7c356e86SAndroid Build Coastguard Worker pid_t pid;
1320*7c356e86SAndroid Build Coastguard Worker Temp_type type;
1321*7c356e86SAndroid Build Coastguard Worker /* actually longer: name (variable length) */
1322*7c356e86SAndroid Build Coastguard Worker char tffn[3];
1323*7c356e86SAndroid Build Coastguard Worker };
1324*7c356e86SAndroid Build Coastguard Worker
1325*7c356e86SAndroid Build Coastguard Worker /*
1326*7c356e86SAndroid Build Coastguard Worker * stdio and our IO routines
1327*7c356e86SAndroid Build Coastguard Worker */
1328*7c356e86SAndroid Build Coastguard Worker
1329*7c356e86SAndroid Build Coastguard Worker #define shl_xtrace (&shf_iob[0]) /* for set -x */
1330*7c356e86SAndroid Build Coastguard Worker #define shl_stdout (&shf_iob[1])
1331*7c356e86SAndroid Build Coastguard Worker #define shl_out (&shf_iob[2])
1332*7c356e86SAndroid Build Coastguard Worker #ifdef DF
1333*7c356e86SAndroid Build Coastguard Worker #define shl_dbg (&shf_iob[3]) /* for DF() */
1334*7c356e86SAndroid Build Coastguard Worker #endif
1335*7c356e86SAndroid Build Coastguard Worker EXTERN bool shl_stdout_ok;
1336*7c356e86SAndroid Build Coastguard Worker
1337*7c356e86SAndroid Build Coastguard Worker /*
1338*7c356e86SAndroid Build Coastguard Worker * trap handlers
1339*7c356e86SAndroid Build Coastguard Worker */
1340*7c356e86SAndroid Build Coastguard Worker typedef struct trap {
1341*7c356e86SAndroid Build Coastguard Worker const char *name; /* short name */
1342*7c356e86SAndroid Build Coastguard Worker const char *mess; /* descriptive name */
1343*7c356e86SAndroid Build Coastguard Worker char *trap; /* trap command */
1344*7c356e86SAndroid Build Coastguard Worker sig_t cursig; /* current handler (valid if TF_ORIG_* set) */
1345*7c356e86SAndroid Build Coastguard Worker sig_t shtrap; /* shell signal handler */
1346*7c356e86SAndroid Build Coastguard Worker int signal; /* signal number */
1347*7c356e86SAndroid Build Coastguard Worker int flags; /* TF_* */
1348*7c356e86SAndroid Build Coastguard Worker volatile sig_atomic_t set; /* trap pending */
1349*7c356e86SAndroid Build Coastguard Worker } Trap;
1350*7c356e86SAndroid Build Coastguard Worker
1351*7c356e86SAndroid Build Coastguard Worker /* values for Trap.flags */
1352*7c356e86SAndroid Build Coastguard Worker #define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
1353*7c356e86SAndroid Build Coastguard Worker #define TF_USER_SET BIT(1) /* user has (tried to) set trap */
1354*7c356e86SAndroid Build Coastguard Worker #define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
1355*7c356e86SAndroid Build Coastguard Worker #define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
1356*7c356e86SAndroid Build Coastguard Worker #define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
1357*7c356e86SAndroid Build Coastguard Worker #define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
1358*7c356e86SAndroid Build Coastguard Worker #define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
1359*7c356e86SAndroid Build Coastguard Worker #define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
1360*7c356e86SAndroid Build Coastguard Worker #define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
1361*7c356e86SAndroid Build Coastguard Worker #define TF_FATAL BIT(9) /* causes termination if not trapped */
1362*7c356e86SAndroid Build Coastguard Worker
1363*7c356e86SAndroid Build Coastguard Worker /* values for setsig()/setexecsig() flags argument */
1364*7c356e86SAndroid Build Coastguard Worker #define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
1365*7c356e86SAndroid Build Coastguard Worker #define SS_RESTORE_CURR 0 /* leave current handler in place */
1366*7c356e86SAndroid Build Coastguard Worker #define SS_RESTORE_ORIG 1 /* restore original handler */
1367*7c356e86SAndroid Build Coastguard Worker #define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
1368*7c356e86SAndroid Build Coastguard Worker #define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
1369*7c356e86SAndroid Build Coastguard Worker #define SS_FORCE BIT(3) /* set signal even if original signal ignored */
1370*7c356e86SAndroid Build Coastguard Worker #define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
1371*7c356e86SAndroid Build Coastguard Worker #define SS_SHTRAP BIT(5) /* trap for internal use (ALRM, CHLD, WINCH) */
1372*7c356e86SAndroid Build Coastguard Worker
1373*7c356e86SAndroid Build Coastguard Worker #define ksh_SIGEXIT 0 /* for trap EXIT */
1374*7c356e86SAndroid Build Coastguard Worker #define ksh_SIGERR ksh_NSIG /* for trap ERR */
1375*7c356e86SAndroid Build Coastguard Worker
1376*7c356e86SAndroid Build Coastguard Worker EXTERN volatile sig_atomic_t trap; /* traps pending? */
1377*7c356e86SAndroid Build Coastguard Worker EXTERN volatile sig_atomic_t intrsig; /* pending trap interrupts command */
1378*7c356e86SAndroid Build Coastguard Worker EXTERN volatile sig_atomic_t fatal_trap; /* received a fatal signal */
1379*7c356e86SAndroid Build Coastguard Worker extern Trap sigtraps[ksh_NSIG + 1];
1380*7c356e86SAndroid Build Coastguard Worker
1381*7c356e86SAndroid Build Coastguard Worker /* got_winch = 1 when we need to re-adjust the window size */
1382*7c356e86SAndroid Build Coastguard Worker #ifdef SIGWINCH
1383*7c356e86SAndroid Build Coastguard Worker EXTERN volatile sig_atomic_t got_winch E_INIT(1);
1384*7c356e86SAndroid Build Coastguard Worker #else
1385*7c356e86SAndroid Build Coastguard Worker #define got_winch true
1386*7c356e86SAndroid Build Coastguard Worker #endif
1387*7c356e86SAndroid Build Coastguard Worker
1388*7c356e86SAndroid Build Coastguard Worker /*
1389*7c356e86SAndroid Build Coastguard Worker * TMOUT support
1390*7c356e86SAndroid Build Coastguard Worker */
1391*7c356e86SAndroid Build Coastguard Worker /* values for ksh_tmout_state */
1392*7c356e86SAndroid Build Coastguard Worker enum tmout_enum {
1393*7c356e86SAndroid Build Coastguard Worker TMOUT_EXECUTING = 0, /* executing commands */
1394*7c356e86SAndroid Build Coastguard Worker TMOUT_READING, /* waiting for input */
1395*7c356e86SAndroid Build Coastguard Worker TMOUT_LEAVING /* have timed out */
1396*7c356e86SAndroid Build Coastguard Worker };
1397*7c356e86SAndroid Build Coastguard Worker EXTERN unsigned int ksh_tmout;
1398*7c356e86SAndroid Build Coastguard Worker EXTERN enum tmout_enum ksh_tmout_state;
1399*7c356e86SAndroid Build Coastguard Worker
1400*7c356e86SAndroid Build Coastguard Worker /* For "You have stopped jobs" message */
1401*7c356e86SAndroid Build Coastguard Worker EXTERN bool really_exit;
1402*7c356e86SAndroid Build Coastguard Worker
1403*7c356e86SAndroid Build Coastguard Worker /*
1404*7c356e86SAndroid Build Coastguard Worker * fast character classes
1405*7c356e86SAndroid Build Coastguard Worker */
1406*7c356e86SAndroid Build Coastguard Worker
1407*7c356e86SAndroid Build Coastguard Worker /* internal types, do not reference */
1408*7c356e86SAndroid Build Coastguard Worker
1409*7c356e86SAndroid Build Coastguard Worker /* initially empty — filled at runtime from $IFS */
1410*7c356e86SAndroid Build Coastguard Worker #define CiIFS BIT(0)
1411*7c356e86SAndroid Build Coastguard Worker #define CiCNTRL BIT(1) /* \x01‥\x08\x0E‥\x1F\x7F */
1412*7c356e86SAndroid Build Coastguard Worker #define CiUPPER BIT(2) /* A‥Z */
1413*7c356e86SAndroid Build Coastguard Worker #define CiLOWER BIT(3) /* a‥z */
1414*7c356e86SAndroid Build Coastguard Worker #define CiHEXLT BIT(4) /* A‥Fa‥f */
1415*7c356e86SAndroid Build Coastguard Worker #define CiOCTAL BIT(5) /* 0‥7 */
1416*7c356e86SAndroid Build Coastguard Worker #define CiQCL BIT(6) /* &();| */
1417*7c356e86SAndroid Build Coastguard Worker #define CiALIAS BIT(7) /* !,.@ */
1418*7c356e86SAndroid Build Coastguard Worker #define CiQCX BIT(8) /* *[\\ */
1419*7c356e86SAndroid Build Coastguard Worker #define CiVAR1 BIT(9) /* !*@ */
1420*7c356e86SAndroid Build Coastguard Worker #define CiQCM BIT(10) /* /^~ */
1421*7c356e86SAndroid Build Coastguard Worker #define CiDIGIT BIT(11) /* 89 */
1422*7c356e86SAndroid Build Coastguard Worker #define CiQC BIT(12) /* "' */
1423*7c356e86SAndroid Build Coastguard Worker #define CiSPX BIT(13) /* \x0B\x0C */
1424*7c356e86SAndroid Build Coastguard Worker #define CiCURLY BIT(14) /* {} */
1425*7c356e86SAndroid Build Coastguard Worker #define CiANGLE BIT(15) /* <> */
1426*7c356e86SAndroid Build Coastguard Worker #define CiNUL BIT(16) /* \x00 */
1427*7c356e86SAndroid Build Coastguard Worker #define CiTAB BIT(17) /* \x09 */
1428*7c356e86SAndroid Build Coastguard Worker #define CiNL BIT(18) /* \x0A */
1429*7c356e86SAndroid Build Coastguard Worker #define CiCR BIT(19) /* \x0D */
1430*7c356e86SAndroid Build Coastguard Worker #define CiSP BIT(20) /* \x20 */
1431*7c356e86SAndroid Build Coastguard Worker #define CiHASH BIT(21) /* # */
1432*7c356e86SAndroid Build Coastguard Worker #define CiSS BIT(22) /* $ */
1433*7c356e86SAndroid Build Coastguard Worker #define CiPERCT BIT(23) /* % */
1434*7c356e86SAndroid Build Coastguard Worker #define CiPLUS BIT(24) /* + */
1435*7c356e86SAndroid Build Coastguard Worker #define CiMINUS BIT(25) /* - */
1436*7c356e86SAndroid Build Coastguard Worker #define CiCOLON BIT(26) /* : */
1437*7c356e86SAndroid Build Coastguard Worker #define CiEQUAL BIT(27) /* = */
1438*7c356e86SAndroid Build Coastguard Worker #define CiQUEST BIT(28) /* ? */
1439*7c356e86SAndroid Build Coastguard Worker #define CiBRACK BIT(29) /* [] */
1440*7c356e86SAndroid Build Coastguard Worker #define CiUNDER BIT(30) /* _ */
1441*7c356e86SAndroid Build Coastguard Worker #define CiGRAVE BIT(31) /* ` */
1442*7c356e86SAndroid Build Coastguard Worker /* out of space, but one for *@ would make sense, possibly others */
1443*7c356e86SAndroid Build Coastguard Worker
1444*7c356e86SAndroid Build Coastguard Worker /* compile-time initialised, ASCII only */
1445*7c356e86SAndroid Build Coastguard Worker extern const uint32_t tpl_ctypes[128];
1446*7c356e86SAndroid Build Coastguard Worker /* run-time, contains C_IFS as well, full 2⁸ octet range */
1447*7c356e86SAndroid Build Coastguard Worker EXTERN uint32_t ksh_ctypes[256];
1448*7c356e86SAndroid Build Coastguard Worker /* first octet of $IFS, for concatenating "$*" */
1449*7c356e86SAndroid Build Coastguard Worker EXTERN char ifs0;
1450*7c356e86SAndroid Build Coastguard Worker
1451*7c356e86SAndroid Build Coastguard Worker /* external types */
1452*7c356e86SAndroid Build Coastguard Worker
1453*7c356e86SAndroid Build Coastguard Worker /* !%+,-.0‥9:@A‥Z[]_a‥z valid characters in alias names */
1454*7c356e86SAndroid Build Coastguard Worker #define C_ALIAS (CiALIAS | CiBRACK | CiCOLON | CiDIGIT | CiLOWER | CiMINUS | CiOCTAL | CiPERCT | CiPLUS | CiUNDER | CiUPPER)
1455*7c356e86SAndroid Build Coastguard Worker /* 0‥9A‥Za‥z alphanumerical */
1456*7c356e86SAndroid Build Coastguard Worker #define C_ALNUM (CiDIGIT | CiLOWER | CiOCTAL | CiUPPER)
1457*7c356e86SAndroid Build Coastguard Worker /* 0‥9A‥Z_a‥z alphanumerical plus underscore (“word character”) */
1458*7c356e86SAndroid Build Coastguard Worker #define C_ALNUX (CiDIGIT | CiLOWER | CiOCTAL | CiUNDER | CiUPPER)
1459*7c356e86SAndroid Build Coastguard Worker /* A‥Za‥z alphabetical (upper plus lower) */
1460*7c356e86SAndroid Build Coastguard Worker #define C_ALPHA (CiLOWER | CiUPPER)
1461*7c356e86SAndroid Build Coastguard Worker /* A‥Z_a‥z alphabetical plus underscore (identifier lead) */
1462*7c356e86SAndroid Build Coastguard Worker #define C_ALPHX (CiLOWER | CiUNDER | CiUPPER)
1463*7c356e86SAndroid Build Coastguard Worker /* \x01‥\x7F 7-bit ASCII except NUL */
1464*7c356e86SAndroid Build Coastguard Worker #define C_ASCII (CiALIAS | CiANGLE | CiBRACK | CiCNTRL | CiCOLON | CiCR | CiCURLY | CiDIGIT | CiEQUAL | CiGRAVE | CiHASH | CiLOWER | CiMINUS | CiNL | CiOCTAL | CiPERCT | CiPLUS | CiQC | CiQCL | CiQCM | CiQCX | CiQUEST | CiSP | CiSPX | CiSS | CiTAB | CiUNDER | CiUPPER)
1465*7c356e86SAndroid Build Coastguard Worker /* \x09\x20 tab and space */
1466*7c356e86SAndroid Build Coastguard Worker #define C_BLANK (CiSP | CiTAB)
1467*7c356e86SAndroid Build Coastguard Worker /* \x09\x20"' separator for completion */
1468*7c356e86SAndroid Build Coastguard Worker #define C_CFS (CiQC | CiSP | CiTAB)
1469*7c356e86SAndroid Build Coastguard Worker /* \x00‥\x1F\x7F POSIX control characters */
1470*7c356e86SAndroid Build Coastguard Worker #define C_CNTRL (CiCNTRL | CiCR | CiNL | CiNUL | CiSPX | CiTAB)
1471*7c356e86SAndroid Build Coastguard Worker /* 0‥9 decimal digits */
1472*7c356e86SAndroid Build Coastguard Worker #define C_DIGIT (CiDIGIT | CiOCTAL)
1473*7c356e86SAndroid Build Coastguard Worker /* &();`| editor x_locate_word() command */
1474*7c356e86SAndroid Build Coastguard Worker #define C_EDCMD (CiGRAVE | CiQCL)
1475*7c356e86SAndroid Build Coastguard Worker /* \x09\x0A\x20"&'():;<=>`| editor non-word characters */
1476*7c356e86SAndroid Build Coastguard Worker #define C_EDNWC (CiANGLE | CiCOLON | CiEQUAL | CiGRAVE | CiNL | CiQC | CiQCL | CiSP | CiTAB)
1477*7c356e86SAndroid Build Coastguard Worker /* "#$&'()*:;<=>?[\\`{|} editor quotes for tab completion */
1478*7c356e86SAndroid Build Coastguard Worker #define C_EDQ (CiANGLE | CiCOLON | CiCURLY | CiEQUAL | CiGRAVE | CiHASH | CiQC | CiQCL | CiQCX | CiQUEST | CiSS)
1479*7c356e86SAndroid Build Coastguard Worker /* !‥~ POSIX graphical (alphanumerical plus punctuation) */
1480*7c356e86SAndroid Build Coastguard Worker #define C_GRAPH (C_PUNCT | CiDIGIT | CiLOWER | CiOCTAL | CiUPPER)
1481*7c356e86SAndroid Build Coastguard Worker /* A‥Fa‥f hex letter */
1482*7c356e86SAndroid Build Coastguard Worker #define C_HEXLT CiHEXLT
1483*7c356e86SAndroid Build Coastguard Worker /* \x00 + $IFS IFS whitespace, IFS non-whitespace, NUL */
1484*7c356e86SAndroid Build Coastguard Worker #define C_IFS (CiIFS | CiNUL)
1485*7c356e86SAndroid Build Coastguard Worker /* \x09\x0A\x20 IFS whitespace */
1486*7c356e86SAndroid Build Coastguard Worker #define C_IFSWS (CiNL | CiSP | CiTAB)
1487*7c356e86SAndroid Build Coastguard Worker /* \x09\x0A\x20&();<>| (for the lexer) */
1488*7c356e86SAndroid Build Coastguard Worker #define C_LEX1 (CiANGLE | CiNL | CiQCL | CiSP | CiTAB)
1489*7c356e86SAndroid Build Coastguard Worker /* a‥z lowercase letters */
1490*7c356e86SAndroid Build Coastguard Worker #define C_LOWER CiLOWER
1491*7c356e86SAndroid Build Coastguard Worker /* not alnux or dollar separator for motion */
1492*7c356e86SAndroid Build Coastguard Worker #define C_MFS (CiALIAS | CiANGLE | CiBRACK | CiCNTRL | CiCOLON | CiCR | CiCURLY | CiEQUAL | CiGRAVE | CiHASH | CiMINUS | CiNL | CiNUL | CiPERCT | CiPLUS | CiQC | CiQCL | CiQCM | CiQCX | CiQUEST | CiSP | CiSPX | CiTAB)
1493*7c356e86SAndroid Build Coastguard Worker /* 0‥7 octal digit */
1494*7c356e86SAndroid Build Coastguard Worker #define C_OCTAL CiOCTAL
1495*7c356e86SAndroid Build Coastguard Worker /* !*+?@ pattern magical operator, except space */
1496*7c356e86SAndroid Build Coastguard Worker #define C_PATMO (CiPLUS | CiQUEST | CiVAR1)
1497*7c356e86SAndroid Build Coastguard Worker /* \x20‥~ POSIX printable characters (graph plus space) */
1498*7c356e86SAndroid Build Coastguard Worker #define C_PRINT (C_GRAPH | CiSP)
1499*7c356e86SAndroid Build Coastguard Worker /* !"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ POSIX punctuation */
1500*7c356e86SAndroid Build Coastguard Worker #define C_PUNCT (CiALIAS | CiANGLE | CiBRACK | CiCOLON | CiCURLY | CiEQUAL | CiGRAVE | CiHASH | CiMINUS | CiPERCT | CiPLUS | CiQC | CiQCL | CiQCM | CiQCX | CiQUEST | CiSS | CiUNDER)
1501*7c356e86SAndroid Build Coastguard Worker /* \x09\x0A"#$&'()*;<=>?[\\]`| characters requiring quoting, minus space */
1502*7c356e86SAndroid Build Coastguard Worker #define C_QUOTE (CiANGLE | CiBRACK | CiEQUAL | CiGRAVE | CiHASH | CiNL | CiQC | CiQCL | CiQCX | CiQUEST | CiSS | CiTAB)
1503*7c356e86SAndroid Build Coastguard Worker /* 0‥9A‥Fa‥f hexadecimal digit */
1504*7c356e86SAndroid Build Coastguard Worker #define C_SEDEC (CiDIGIT | CiHEXLT | CiOCTAL)
1505*7c356e86SAndroid Build Coastguard Worker /* \x09‥\x0D\x20 POSIX space class */
1506*7c356e86SAndroid Build Coastguard Worker #define C_SPACE (CiCR | CiNL | CiSP | CiSPX | CiTAB)
1507*7c356e86SAndroid Build Coastguard Worker /* +-=? substitution operations with word */
1508*7c356e86SAndroid Build Coastguard Worker #define C_SUB1 (CiEQUAL | CiMINUS | CiPLUS | CiQUEST)
1509*7c356e86SAndroid Build Coastguard Worker /* #% substitution operations with pattern */
1510*7c356e86SAndroid Build Coastguard Worker #define C_SUB2 (CiHASH | CiPERCT)
1511*7c356e86SAndroid Build Coastguard Worker /* A‥Z uppercase letters */
1512*7c356e86SAndroid Build Coastguard Worker #define C_UPPER CiUPPER
1513*7c356e86SAndroid Build Coastguard Worker /* !#$*-?@ substitution parameters, other than positional */
1514*7c356e86SAndroid Build Coastguard Worker #define C_VAR1 (CiHASH | CiMINUS | CiQUEST | CiSS | CiVAR1)
1515*7c356e86SAndroid Build Coastguard Worker
1516*7c356e86SAndroid Build Coastguard Worker /* individual chars you might like */
1517*7c356e86SAndroid Build Coastguard Worker #define C_ANGLE CiANGLE /* <> angle brackets */
1518*7c356e86SAndroid Build Coastguard Worker #define C_COLON CiCOLON /* : colon */
1519*7c356e86SAndroid Build Coastguard Worker #define C_CR CiCR /* \x0D ASCII carriage return */
1520*7c356e86SAndroid Build Coastguard Worker #define C_DOLAR CiSS /* $ dollar sign */
1521*7c356e86SAndroid Build Coastguard Worker #define C_EQUAL CiEQUAL /* = equals sign */
1522*7c356e86SAndroid Build Coastguard Worker #define C_GRAVE CiGRAVE /* ` accent gravis */
1523*7c356e86SAndroid Build Coastguard Worker #define C_HASH CiHASH /* # hash sign */
1524*7c356e86SAndroid Build Coastguard Worker #define C_LF CiNL /* \x0A ASCII line feed */
1525*7c356e86SAndroid Build Coastguard Worker #define C_MINUS CiMINUS /* - hyphen-minus */
1526*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_WITH_TEXTMODE
1527*7c356e86SAndroid Build Coastguard Worker #define C_NL (CiNL | CiCR) /* CR or LF under OS/2 TEXTMODE */
1528*7c356e86SAndroid Build Coastguard Worker #else
1529*7c356e86SAndroid Build Coastguard Worker #define C_NL CiNL /* LF only like under Unix */
1530*7c356e86SAndroid Build Coastguard Worker #endif
1531*7c356e86SAndroid Build Coastguard Worker #define C_NUL CiNUL /* \x00 ASCII NUL */
1532*7c356e86SAndroid Build Coastguard Worker #define C_PLUS CiPLUS /* + plus sign */
1533*7c356e86SAndroid Build Coastguard Worker #define C_QC CiQC /* "' quote characters */
1534*7c356e86SAndroid Build Coastguard Worker #define C_QUEST CiQUEST /* ? question mark */
1535*7c356e86SAndroid Build Coastguard Worker #define C_SPC CiSP /* \x20 ASCII space */
1536*7c356e86SAndroid Build Coastguard Worker #define C_TAB CiTAB /* \x09 ASCII horizontal tabulator */
1537*7c356e86SAndroid Build Coastguard Worker #define C_UNDER CiUNDER /* _ underscore */
1538*7c356e86SAndroid Build Coastguard Worker
1539*7c356e86SAndroid Build Coastguard Worker /* identity transform of octet */
1540*7c356e86SAndroid Build Coastguard Worker #if defined(DEBUG) && defined(__GNUC__) && !defined(__ICC) && \
1541*7c356e86SAndroid Build Coastguard Worker !defined(__INTEL_COMPILER) && !defined(__SUNPRO_C)
1542*7c356e86SAndroid Build Coastguard Worker extern unsigned int eek_ord;
1543*7c356e86SAndroid Build Coastguard Worker #define ORD(c) ((size_t)(c) > 0xFF ? eek_ord : \
1544*7c356e86SAndroid Build Coastguard Worker ((unsigned int)(unsigned char)(c)))
1545*7c356e86SAndroid Build Coastguard Worker #define ord(c) __builtin_choose_expr( \
1546*7c356e86SAndroid Build Coastguard Worker __builtin_types_compatible_p(__typeof__(c), char) || \
1547*7c356e86SAndroid Build Coastguard Worker __builtin_types_compatible_p(__typeof__(c), unsigned char), \
1548*7c356e86SAndroid Build Coastguard Worker ((unsigned int)(unsigned char)(c)), ({ \
1549*7c356e86SAndroid Build Coastguard Worker size_t ord_c = (c); \
1550*7c356e86SAndroid Build Coastguard Worker \
1551*7c356e86SAndroid Build Coastguard Worker if (ord_c > (size_t)0xFFU) \
1552*7c356e86SAndroid Build Coastguard Worker internal_errorf("%s:%d:ord(%zX)", \
1553*7c356e86SAndroid Build Coastguard Worker __FILE__, __LINE__, ord_c); \
1554*7c356e86SAndroid Build Coastguard Worker ((unsigned int)(unsigned char)(ord_c)); \
1555*7c356e86SAndroid Build Coastguard Worker }))
1556*7c356e86SAndroid Build Coastguard Worker #else
1557*7c356e86SAndroid Build Coastguard Worker #define ord(c) ((unsigned int)(unsigned char)(c))
1558*7c356e86SAndroid Build Coastguard Worker #define ORD(c) ord(c) /* may evaluate arguments twice */
1559*7c356e86SAndroid Build Coastguard Worker #endif
1560*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_EBCDIC) || defined(MKSH_FAUX_EBCDIC)
1561*7c356e86SAndroid Build Coastguard Worker EXTERN unsigned short ebcdic_map[256];
1562*7c356e86SAndroid Build Coastguard Worker EXTERN unsigned char ebcdic_rtt_toascii[256];
1563*7c356e86SAndroid Build Coastguard Worker EXTERN unsigned char ebcdic_rtt_fromascii[256];
1564*7c356e86SAndroid Build Coastguard Worker extern void ebcdic_init(void);
1565*7c356e86SAndroid Build Coastguard Worker /* one-way to-ascii-or-high conversion, for POSIX locale ordering */
1566*7c356e86SAndroid Build Coastguard Worker #define asciibetical(c) ((unsigned int)ebcdic_map[(unsigned char)(c)])
1567*7c356e86SAndroid Build Coastguard Worker /* two-way round-trip conversion, for general use */
1568*7c356e86SAndroid Build Coastguard Worker #define rtt2asc(c) ebcdic_rtt_toascii[(unsigned char)(c)]
1569*7c356e86SAndroid Build Coastguard Worker #define asc2rtt(c) ebcdic_rtt_fromascii[(unsigned char)(c)]
1570*7c356e86SAndroid Build Coastguard Worker /* case-independent char comparison */
1571*7c356e86SAndroid Build Coastguard Worker #define ksh_eq(c,u,l) (ord(c) == ord(u) || ord(c) == ord(l))
1572*7c356e86SAndroid Build Coastguard Worker #else
1573*7c356e86SAndroid Build Coastguard Worker #define asciibetical(c) ord(c)
1574*7c356e86SAndroid Build Coastguard Worker #define rtt2asc(c) ((unsigned char)(c))
1575*7c356e86SAndroid Build Coastguard Worker #define asc2rtt(c) ((unsigned char)(c))
1576*7c356e86SAndroid Build Coastguard Worker #define ksh_eq(c,u,l) ((ord(c) | 0x20) == ord(l))
1577*7c356e86SAndroid Build Coastguard Worker #endif
1578*7c356e86SAndroid Build Coastguard Worker /* control character foo */
1579*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_EBCDIC
1580*7c356e86SAndroid Build Coastguard Worker #define ksh_isctrl(c) (ord(c) < 0x40 || ord(c) == 0xFF)
1581*7c356e86SAndroid Build Coastguard Worker #else
1582*7c356e86SAndroid Build Coastguard Worker #define ksh_isctrl(c) ((ord(c) & 0x7F) < 0x20 || ord(c) == 0x7F)
1583*7c356e86SAndroid Build Coastguard Worker #endif
1584*7c356e86SAndroid Build Coastguard Worker /* new fast character classes */
1585*7c356e86SAndroid Build Coastguard Worker #define ctype(c,t) tobool(ksh_ctypes[ord(c)] & (t))
1586*7c356e86SAndroid Build Coastguard Worker #define cinttype(c,t) ((c) >= 0 && (c) <= 0xFF ? \
1587*7c356e86SAndroid Build Coastguard Worker tobool(ksh_ctypes[(unsigned char)(c)] & (t)) : false)
1588*7c356e86SAndroid Build Coastguard Worker /* helper functions */
1589*7c356e86SAndroid Build Coastguard Worker #define ksh_isdash(s) tobool(ord((s)[0]) == '-' && ord((s)[1]) == '\0')
1590*7c356e86SAndroid Build Coastguard Worker /* invariant distance even in EBCDIC */
1591*7c356e86SAndroid Build Coastguard Worker #define ksh_tolower(c) (ctype(c, C_UPPER) ? (c) - 'A' + 'a' : (c))
1592*7c356e86SAndroid Build Coastguard Worker #define ksh_toupper(c) (ctype(c, C_LOWER) ? (c) - 'a' + 'A' : (c))
1593*7c356e86SAndroid Build Coastguard Worker /* strictly speaking rtt2asc() here, but this works even in EBCDIC */
1594*7c356e86SAndroid Build Coastguard Worker #define ksh_numdig(c) (ord(c) - ORD('0'))
1595*7c356e86SAndroid Build Coastguard Worker #define ksh_numuc(c) (rtt2asc(c) - rtt2asc('A'))
1596*7c356e86SAndroid Build Coastguard Worker #define ksh_numlc(c) (rtt2asc(c) - rtt2asc('a'))
1597*7c356e86SAndroid Build Coastguard Worker #define ksh_toctrl(c) asc2rtt(ord(c) == ORD('?') ? 0x7F : rtt2asc(c) & 0x9F)
1598*7c356e86SAndroid Build Coastguard Worker #define ksh_unctrl(c) asc2rtt(rtt2asc(c) ^ 0x40U)
1599*7c356e86SAndroid Build Coastguard Worker
1600*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_SMALL
1601*7c356e86SAndroid Build Coastguard Worker #define SMALLP(x) /* nothing */
1602*7c356e86SAndroid Build Coastguard Worker #else
1603*7c356e86SAndroid Build Coastguard Worker #define SMALLP(x) , x
1604*7c356e86SAndroid Build Coastguard Worker #endif
1605*7c356e86SAndroid Build Coastguard Worker
1606*7c356e86SAndroid Build Coastguard Worker /* Argument parsing for built-in commands and getopts command */
1607*7c356e86SAndroid Build Coastguard Worker
1608*7c356e86SAndroid Build Coastguard Worker /* Values for Getopt.flags */
1609*7c356e86SAndroid Build Coastguard Worker #define GF_ERROR BIT(0) /* call errorf() if there is an error */
1610*7c356e86SAndroid Build Coastguard Worker #define GF_PLUSOPT BIT(1) /* allow +c as an option */
1611*7c356e86SAndroid Build Coastguard Worker #define GF_NONAME BIT(2) /* don't print argv[0] in errors */
1612*7c356e86SAndroid Build Coastguard Worker
1613*7c356e86SAndroid Build Coastguard Worker /* Values for Getopt.info */
1614*7c356e86SAndroid Build Coastguard Worker #define GI_MINUS BIT(0) /* an option started with -... */
1615*7c356e86SAndroid Build Coastguard Worker #define GI_PLUS BIT(1) /* an option started with +... */
1616*7c356e86SAndroid Build Coastguard Worker #define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
1617*7c356e86SAndroid Build Coastguard Worker
1618*7c356e86SAndroid Build Coastguard Worker /* in case some OS defines these */
1619*7c356e86SAndroid Build Coastguard Worker #undef optarg
1620*7c356e86SAndroid Build Coastguard Worker #undef optind
1621*7c356e86SAndroid Build Coastguard Worker
1622*7c356e86SAndroid Build Coastguard Worker typedef struct {
1623*7c356e86SAndroid Build Coastguard Worker const char *optarg;
1624*7c356e86SAndroid Build Coastguard Worker int optind;
1625*7c356e86SAndroid Build Coastguard Worker int uoptind; /* what user sees in $OPTIND */
1626*7c356e86SAndroid Build Coastguard Worker int flags; /* see GF_* */
1627*7c356e86SAndroid Build Coastguard Worker int info; /* see GI_* */
1628*7c356e86SAndroid Build Coastguard Worker unsigned int p; /* 0 or index into argv[optind - 1] */
1629*7c356e86SAndroid Build Coastguard Worker char buf[2]; /* for bad option OPTARG value */
1630*7c356e86SAndroid Build Coastguard Worker } Getopt;
1631*7c356e86SAndroid Build Coastguard Worker
1632*7c356e86SAndroid Build Coastguard Worker EXTERN Getopt builtin_opt; /* for shell builtin commands */
1633*7c356e86SAndroid Build Coastguard Worker EXTERN Getopt user_opt; /* parsing state for getopts builtin command */
1634*7c356e86SAndroid Build Coastguard Worker
1635*7c356e86SAndroid Build Coastguard Worker /* This for co-processes */
1636*7c356e86SAndroid Build Coastguard Worker
1637*7c356e86SAndroid Build Coastguard Worker /* something that won't (realisticly) wrap */
1638*7c356e86SAndroid Build Coastguard Worker typedef int Coproc_id;
1639*7c356e86SAndroid Build Coastguard Worker
1640*7c356e86SAndroid Build Coastguard Worker struct coproc {
1641*7c356e86SAndroid Build Coastguard Worker void *job; /* 0 or job of co-process using input pipe */
1642*7c356e86SAndroid Build Coastguard Worker int read; /* pipe from co-process's stdout */
1643*7c356e86SAndroid Build Coastguard Worker int readw; /* other side of read (saved temporarily) */
1644*7c356e86SAndroid Build Coastguard Worker int write; /* pipe to co-process's stdin */
1645*7c356e86SAndroid Build Coastguard Worker int njobs; /* number of live jobs using output pipe */
1646*7c356e86SAndroid Build Coastguard Worker Coproc_id id; /* id of current output pipe */
1647*7c356e86SAndroid Build Coastguard Worker };
1648*7c356e86SAndroid Build Coastguard Worker EXTERN struct coproc coproc;
1649*7c356e86SAndroid Build Coastguard Worker
1650*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NOPROSPECTOFWORK
1651*7c356e86SAndroid Build Coastguard Worker /* used in jobs.c and by coprocess stuff in exec.c and select() calls */
1652*7c356e86SAndroid Build Coastguard Worker EXTERN sigset_t sm_default, sm_sigchld;
1653*7c356e86SAndroid Build Coastguard Worker #endif
1654*7c356e86SAndroid Build Coastguard Worker
1655*7c356e86SAndroid Build Coastguard Worker /* name of called builtin function (used by error functions) */
1656*7c356e86SAndroid Build Coastguard Worker EXTERN const char *builtin_argv0;
1657*7c356e86SAndroid Build Coastguard Worker /* is called builtin a POSIX special builtin? (error functions only) */
1658*7c356e86SAndroid Build Coastguard Worker EXTERN bool builtin_spec;
1659*7c356e86SAndroid Build Coastguard Worker
1660*7c356e86SAndroid Build Coastguard Worker /* current working directory */
1661*7c356e86SAndroid Build Coastguard Worker EXTERN char *current_wd;
1662*7c356e86SAndroid Build Coastguard Worker
1663*7c356e86SAndroid Build Coastguard Worker /* input line size */
1664*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_SMALL
1665*7c356e86SAndroid Build Coastguard Worker #define LINE (4096 - ALLOC_OVERHEAD)
1666*7c356e86SAndroid Build Coastguard Worker #else
1667*7c356e86SAndroid Build Coastguard Worker #define LINE (16384 - ALLOC_OVERHEAD)
1668*7c356e86SAndroid Build Coastguard Worker #endif
1669*7c356e86SAndroid Build Coastguard Worker /* columns and lines of the tty */
1670*7c356e86SAndroid Build Coastguard Worker EXTERN mksh_ari_t x_cols E_INIT(80);
1671*7c356e86SAndroid Build Coastguard Worker EXTERN mksh_ari_t x_lins E_INIT(24);
1672*7c356e86SAndroid Build Coastguard Worker
1673*7c356e86SAndroid Build Coastguard Worker
1674*7c356e86SAndroid Build Coastguard Worker /* Determine the location of the system (common) profile */
1675*7c356e86SAndroid Build Coastguard Worker
1676*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_DEFAULT_PROFILEDIR
1677*7c356e86SAndroid Build Coastguard Worker #define MKSH_DEFAULT_PROFILEDIR MKSH_UNIXROOT "/etc"
1678*7c356e86SAndroid Build Coastguard Worker #endif
1679*7c356e86SAndroid Build Coastguard Worker
1680*7c356e86SAndroid Build Coastguard Worker #define MKSH_SYSTEM_PROFILE MKSH_DEFAULT_PROFILEDIR "/profile"
1681*7c356e86SAndroid Build Coastguard Worker #define MKSH_SUID_PROFILE MKSH_DEFAULT_PROFILEDIR "/suid_profile"
1682*7c356e86SAndroid Build Coastguard Worker
1683*7c356e86SAndroid Build Coastguard Worker
1684*7c356e86SAndroid Build Coastguard Worker /* Used by v_evaluate() and setstr() to control action when error occurs */
1685*7c356e86SAndroid Build Coastguard Worker #define KSH_UNWIND_ERROR 0 /* unwind the stack (kshlongjmp) */
1686*7c356e86SAndroid Build Coastguard Worker #define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */
1687*7c356e86SAndroid Build Coastguard Worker
1688*7c356e86SAndroid Build Coastguard Worker /*
1689*7c356e86SAndroid Build Coastguard Worker * Shell file I/O routines
1690*7c356e86SAndroid Build Coastguard Worker */
1691*7c356e86SAndroid Build Coastguard Worker
1692*7c356e86SAndroid Build Coastguard Worker #define SHF_BSIZE 512
1693*7c356e86SAndroid Build Coastguard Worker
1694*7c356e86SAndroid Build Coastguard Worker #define shf_fileno(shf) ((shf)->fd)
1695*7c356e86SAndroid Build Coastguard Worker #define shf_setfileno(shf,nfd) ((shf)->fd = (nfd))
1696*7c356e86SAndroid Build Coastguard Worker #define shf_getc_i(shf) ((shf)->rnleft > 0 ? \
1697*7c356e86SAndroid Build Coastguard Worker (shf)->rnleft--, (int)ord(*(shf)->rp++) : \
1698*7c356e86SAndroid Build Coastguard Worker shf_getchar(shf))
1699*7c356e86SAndroid Build Coastguard Worker #define shf_putc_i(c,shf) ((shf)->wnleft == 0 ? \
1700*7c356e86SAndroid Build Coastguard Worker shf_putchar((uint8_t)(c), (shf)) : \
1701*7c356e86SAndroid Build Coastguard Worker ((shf)->wnleft--, *(shf)->wp++ = (c)))
1702*7c356e86SAndroid Build Coastguard Worker #define shf_eof(shf) ((shf)->flags & SHF_EOF)
1703*7c356e86SAndroid Build Coastguard Worker #define shf_error(shf) ((shf)->flags & SHF_ERROR)
1704*7c356e86SAndroid Build Coastguard Worker #define shf_errno(shf) ((shf)->errnosv)
1705*7c356e86SAndroid Build Coastguard Worker #define shf_clearerr(shf) ((shf)->flags &= ~(SHF_EOF | SHF_ERROR))
1706*7c356e86SAndroid Build Coastguard Worker
1707*7c356e86SAndroid Build Coastguard Worker /* Flags passed to shf_*open() */
1708*7c356e86SAndroid Build Coastguard Worker #define SHF_RD 0x0001
1709*7c356e86SAndroid Build Coastguard Worker #define SHF_WR 0x0002
1710*7c356e86SAndroid Build Coastguard Worker #define SHF_RDWR (SHF_RD | SHF_WR)
1711*7c356e86SAndroid Build Coastguard Worker #define SHF_ACCMODE 0x0003 /* mask */
1712*7c356e86SAndroid Build Coastguard Worker #define SHF_GETFL 0x0004 /* use fcntl() to figure RD/WR flags */
1713*7c356e86SAndroid Build Coastguard Worker #define SHF_UNBUF 0x0008 /* unbuffered I/O */
1714*7c356e86SAndroid Build Coastguard Worker #define SHF_CLEXEC 0x0010 /* set close on exec flag */
1715*7c356e86SAndroid Build Coastguard Worker #define SHF_MAPHI 0x0020 /* make fd > FDBASE (and close orig)
1716*7c356e86SAndroid Build Coastguard Worker * (shf_open() only) */
1717*7c356e86SAndroid Build Coastguard Worker #define SHF_DYNAMIC 0x0040 /* string: increase buffer as needed */
1718*7c356e86SAndroid Build Coastguard Worker #define SHF_INTERRUPT 0x0080 /* EINTR in read/write causes error */
1719*7c356e86SAndroid Build Coastguard Worker /* Flags used internally */
1720*7c356e86SAndroid Build Coastguard Worker #define SHF_STRING 0x0100 /* a string, not a file */
1721*7c356e86SAndroid Build Coastguard Worker #define SHF_ALLOCS 0x0200 /* shf and shf->buf were alloc()ed */
1722*7c356e86SAndroid Build Coastguard Worker #define SHF_ALLOCB 0x0400 /* shf->buf was alloc()ed */
1723*7c356e86SAndroid Build Coastguard Worker #define SHF_ERROR 0x0800 /* read()/write() error */
1724*7c356e86SAndroid Build Coastguard Worker #define SHF_EOF 0x1000 /* read eof (sticky) */
1725*7c356e86SAndroid Build Coastguard Worker #define SHF_READING 0x2000 /* currently reading: rnleft,rp valid */
1726*7c356e86SAndroid Build Coastguard Worker #define SHF_WRITING 0x4000 /* currently writing: wnleft,wp valid */
1727*7c356e86SAndroid Build Coastguard Worker
1728*7c356e86SAndroid Build Coastguard Worker
1729*7c356e86SAndroid Build Coastguard Worker struct shf {
1730*7c356e86SAndroid Build Coastguard Worker Area *areap; /* area shf/buf were allocated in */
1731*7c356e86SAndroid Build Coastguard Worker unsigned char *rp; /* read: current position in buffer */
1732*7c356e86SAndroid Build Coastguard Worker unsigned char *wp; /* write: current position in buffer */
1733*7c356e86SAndroid Build Coastguard Worker unsigned char *buf; /* buffer */
1734*7c356e86SAndroid Build Coastguard Worker ssize_t bsize; /* actual size of buf */
1735*7c356e86SAndroid Build Coastguard Worker ssize_t rbsize; /* size of buffer (1 if SHF_UNBUF) */
1736*7c356e86SAndroid Build Coastguard Worker ssize_t rnleft; /* read: how much data left in buffer */
1737*7c356e86SAndroid Build Coastguard Worker ssize_t wbsize; /* size of buffer (0 if SHF_UNBUF) */
1738*7c356e86SAndroid Build Coastguard Worker ssize_t wnleft; /* write: how much space left in buffer */
1739*7c356e86SAndroid Build Coastguard Worker int flags; /* see SHF_* */
1740*7c356e86SAndroid Build Coastguard Worker int fd; /* file descriptor */
1741*7c356e86SAndroid Build Coastguard Worker int errnosv; /* saved value of errno after error */
1742*7c356e86SAndroid Build Coastguard Worker };
1743*7c356e86SAndroid Build Coastguard Worker
1744*7c356e86SAndroid Build Coastguard Worker extern struct shf shf_iob[];
1745*7c356e86SAndroid Build Coastguard Worker
1746*7c356e86SAndroid Build Coastguard Worker struct table {
1747*7c356e86SAndroid Build Coastguard Worker Area *areap; /* area to allocate entries */
1748*7c356e86SAndroid Build Coastguard Worker struct tbl **tbls; /* hashed table items */
1749*7c356e86SAndroid Build Coastguard Worker size_t nfree; /* free table entries */
1750*7c356e86SAndroid Build Coastguard Worker uint8_t tshift; /* table size (2^tshift) */
1751*7c356e86SAndroid Build Coastguard Worker };
1752*7c356e86SAndroid Build Coastguard Worker
1753*7c356e86SAndroid Build Coastguard Worker /* table item */
1754*7c356e86SAndroid Build Coastguard Worker struct tbl {
1755*7c356e86SAndroid Build Coastguard Worker /* Area to allocate from */
1756*7c356e86SAndroid Build Coastguard Worker Area *areap;
1757*7c356e86SAndroid Build Coastguard Worker /* value */
1758*7c356e86SAndroid Build Coastguard Worker union {
1759*7c356e86SAndroid Build Coastguard Worker char *s; /* string */
1760*7c356e86SAndroid Build Coastguard Worker mksh_ari_t i; /* integer */
1761*7c356e86SAndroid Build Coastguard Worker mksh_uari_t u; /* unsigned integer */
1762*7c356e86SAndroid Build Coastguard Worker int (*f)(const char **); /* built-in command */
1763*7c356e86SAndroid Build Coastguard Worker struct op *t; /* "function" tree */
1764*7c356e86SAndroid Build Coastguard Worker } val;
1765*7c356e86SAndroid Build Coastguard Worker union {
1766*7c356e86SAndroid Build Coastguard Worker struct tbl *array; /* array values */
1767*7c356e86SAndroid Build Coastguard Worker const char *fpath; /* temporary path to undef function */
1768*7c356e86SAndroid Build Coastguard Worker } u;
1769*7c356e86SAndroid Build Coastguard Worker union {
1770*7c356e86SAndroid Build Coastguard Worker int field; /* field with for -L/-R/-Z */
1771*7c356e86SAndroid Build Coastguard Worker int errnov; /* CEXEC/CTALIAS */
1772*7c356e86SAndroid Build Coastguard Worker } u2;
1773*7c356e86SAndroid Build Coastguard Worker union {
1774*7c356e86SAndroid Build Coastguard Worker uint32_t hval; /* hash(name) */
1775*7c356e86SAndroid Build Coastguard Worker uint32_t index; /* index for an array */
1776*7c356e86SAndroid Build Coastguard Worker } ua;
1777*7c356e86SAndroid Build Coastguard Worker /*
1778*7c356e86SAndroid Build Coastguard Worker * command type (see below), base (if INTEGER),
1779*7c356e86SAndroid Build Coastguard Worker * offset from val.s of value (if EXPORT)
1780*7c356e86SAndroid Build Coastguard Worker */
1781*7c356e86SAndroid Build Coastguard Worker int type;
1782*7c356e86SAndroid Build Coastguard Worker /* flags (see below) */
1783*7c356e86SAndroid Build Coastguard Worker uint32_t flag;
1784*7c356e86SAndroid Build Coastguard Worker
1785*7c356e86SAndroid Build Coastguard Worker /* actually longer: name (variable length) */
1786*7c356e86SAndroid Build Coastguard Worker char name[4];
1787*7c356e86SAndroid Build Coastguard Worker };
1788*7c356e86SAndroid Build Coastguard Worker
1789*7c356e86SAndroid Build Coastguard Worker EXTERN struct tbl *vtemp;
1790*7c356e86SAndroid Build Coastguard Worker /* set by isglobal(), global() and local() */
1791*7c356e86SAndroid Build Coastguard Worker EXTERN bool last_lookup_was_array;
1792*7c356e86SAndroid Build Coastguard Worker
1793*7c356e86SAndroid Build Coastguard Worker /* common flag bits */
1794*7c356e86SAndroid Build Coastguard Worker #define ALLOC BIT(0) /* val.s has been allocated */
1795*7c356e86SAndroid Build Coastguard Worker #define DEFINED BIT(1) /* is defined in block */
1796*7c356e86SAndroid Build Coastguard Worker #define ISSET BIT(2) /* has value, vp->val.[si] */
1797*7c356e86SAndroid Build Coastguard Worker #define EXPORT BIT(3) /* exported variable/function */
1798*7c356e86SAndroid Build Coastguard Worker #define TRACE BIT(4) /* var: user flagged, func: execution tracing */
1799*7c356e86SAndroid Build Coastguard Worker /* (start non-common flags at 8) */
1800*7c356e86SAndroid Build Coastguard Worker /* flag bits used for variables */
1801*7c356e86SAndroid Build Coastguard Worker #define SPECIAL BIT(8) /* PATH, IFS, SECONDS, etc */
1802*7c356e86SAndroid Build Coastguard Worker #define INTEGER BIT(9) /* val.i contains integer value */
1803*7c356e86SAndroid Build Coastguard Worker #define RDONLY BIT(10) /* read-only variable */
1804*7c356e86SAndroid Build Coastguard Worker #define LOCAL BIT(11) /* for local typeset() */
1805*7c356e86SAndroid Build Coastguard Worker #define ARRAY BIT(13) /* array */
1806*7c356e86SAndroid Build Coastguard Worker #define LJUST BIT(14) /* left justify */
1807*7c356e86SAndroid Build Coastguard Worker #define RJUST BIT(15) /* right justify */
1808*7c356e86SAndroid Build Coastguard Worker #define ZEROFIL BIT(16) /* 0 filled if RJUSTIFY, strip 0s if LJUSTIFY */
1809*7c356e86SAndroid Build Coastguard Worker #define LCASEV BIT(17) /* convert to lower case */
1810*7c356e86SAndroid Build Coastguard Worker #define UCASEV_AL BIT(18) /* convert to upper case / autoload function */
1811*7c356e86SAndroid Build Coastguard Worker #define INT_U BIT(19) /* unsigned integer */
1812*7c356e86SAndroid Build Coastguard Worker #define INT_L BIT(20) /* long integer (no-op but used as magic) */
1813*7c356e86SAndroid Build Coastguard Worker #define IMPORT BIT(21) /* flag to typeset(): no arrays, must have = */
1814*7c356e86SAndroid Build Coastguard Worker #define LOCAL_COPY BIT(22) /* with LOCAL - copy attrs from existing var */
1815*7c356e86SAndroid Build Coastguard Worker #define EXPRINEVAL BIT(23) /* contents currently being evaluated */
1816*7c356e86SAndroid Build Coastguard Worker #define EXPRLVALUE BIT(24) /* useable as lvalue (temp flag) */
1817*7c356e86SAndroid Build Coastguard Worker #define AINDEX BIT(25) /* array index >0 = ua.index filled in */
1818*7c356e86SAndroid Build Coastguard Worker #define ASSOC BIT(26) /* ARRAY ? associative : reference */
1819*7c356e86SAndroid Build Coastguard Worker /* flag bits used for taliases/builtins/aliases/keywords/functions */
1820*7c356e86SAndroid Build Coastguard Worker #define KEEPASN BIT(8) /* keep command assignments (eg, var=x cmd) */
1821*7c356e86SAndroid Build Coastguard Worker #define FINUSE BIT(9) /* function being executed */
1822*7c356e86SAndroid Build Coastguard Worker #define FDELETE BIT(10) /* function deleted while it was executing */
1823*7c356e86SAndroid Build Coastguard Worker #define FKSH BIT(11) /* function defined with function x (vs x()) */
1824*7c356e86SAndroid Build Coastguard Worker #define SPEC_BI BIT(12) /* a POSIX special builtin */
1825*7c356e86SAndroid Build Coastguard Worker #define LOWER_BI BIT(13) /* (with LOW_BI) override even w/o flags */
1826*7c356e86SAndroid Build Coastguard Worker #define LOW_BI BIT(14) /* external utility overrides built-in one */
1827*7c356e86SAndroid Build Coastguard Worker #define DECL_UTIL BIT(15) /* is declaration utility */
1828*7c356e86SAndroid Build Coastguard Worker #define DECL_FWDR BIT(16) /* is declaration utility forwarder */
1829*7c356e86SAndroid Build Coastguard Worker #define NEXTLOC_BI BIT(17) /* needs BF_RESETSPEC on e->loc */
1830*7c356e86SAndroid Build Coastguard Worker
1831*7c356e86SAndroid Build Coastguard Worker /*
1832*7c356e86SAndroid Build Coastguard Worker * Attributes that can be set by the user (used to decide if an unset
1833*7c356e86SAndroid Build Coastguard Worker * param should be repoted by set/typeset). Does not include ARRAY or
1834*7c356e86SAndroid Build Coastguard Worker * LOCAL.
1835*7c356e86SAndroid Build Coastguard Worker */
1836*7c356e86SAndroid Build Coastguard Worker #define USERATTRIB (EXPORT | INTEGER | RDONLY | LJUST | RJUST | ZEROFIL | \
1837*7c356e86SAndroid Build Coastguard Worker LCASEV | UCASEV_AL | INT_U | INT_L)
1838*7c356e86SAndroid Build Coastguard Worker
1839*7c356e86SAndroid Build Coastguard Worker #define arrayindex(vp) ((unsigned long)((vp)->flag & AINDEX ? \
1840*7c356e86SAndroid Build Coastguard Worker (vp)->ua.index : 0))
1841*7c356e86SAndroid Build Coastguard Worker
1842*7c356e86SAndroid Build Coastguard Worker enum namerefflag {
1843*7c356e86SAndroid Build Coastguard Worker SRF_NOP,
1844*7c356e86SAndroid Build Coastguard Worker SRF_ENABLE,
1845*7c356e86SAndroid Build Coastguard Worker SRF_DISABLE
1846*7c356e86SAndroid Build Coastguard Worker };
1847*7c356e86SAndroid Build Coastguard Worker
1848*7c356e86SAndroid Build Coastguard Worker /* command types */
1849*7c356e86SAndroid Build Coastguard Worker #define CNONE 0 /* undefined */
1850*7c356e86SAndroid Build Coastguard Worker #define CSHELL 1 /* built-in */
1851*7c356e86SAndroid Build Coastguard Worker #define CFUNC 2 /* function */
1852*7c356e86SAndroid Build Coastguard Worker #define CEXEC 4 /* executable command */
1853*7c356e86SAndroid Build Coastguard Worker #define CALIAS 5 /* alias */
1854*7c356e86SAndroid Build Coastguard Worker #define CKEYWD 6 /* keyword */
1855*7c356e86SAndroid Build Coastguard Worker #define CTALIAS 7 /* tracked alias */
1856*7c356e86SAndroid Build Coastguard Worker
1857*7c356e86SAndroid Build Coastguard Worker /* Flags for findcom()/comexec() */
1858*7c356e86SAndroid Build Coastguard Worker #define FC_SPECBI BIT(0) /* special builtin */
1859*7c356e86SAndroid Build Coastguard Worker #define FC_FUNC BIT(1) /* function */
1860*7c356e86SAndroid Build Coastguard Worker #define FC_NORMBI BIT(2) /* not special builtin */
1861*7c356e86SAndroid Build Coastguard Worker #define FC_BI (FC_SPECBI | FC_NORMBI)
1862*7c356e86SAndroid Build Coastguard Worker #define FC_PATH BIT(3) /* do path search */
1863*7c356e86SAndroid Build Coastguard Worker #define FC_DEFPATH BIT(4) /* use default path in path search */
1864*7c356e86SAndroid Build Coastguard Worker #define FC_WHENCE BIT(5) /* for use by command and whence */
1865*7c356e86SAndroid Build Coastguard Worker
1866*7c356e86SAndroid Build Coastguard Worker #define AF_ARGV_ALLOC 0x1 /* argv[] array allocated */
1867*7c356e86SAndroid Build Coastguard Worker #define AF_ARGS_ALLOCED 0x2 /* argument strings allocated */
1868*7c356e86SAndroid Build Coastguard Worker #define AI_ARGV(a,i) ((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip])
1869*7c356e86SAndroid Build Coastguard Worker #define AI_ARGC(a) ((a).ai_argc - (a).skip)
1870*7c356e86SAndroid Build Coastguard Worker
1871*7c356e86SAndroid Build Coastguard Worker /* Argument info. Used for $#, $* for shell, functions, includes, etc. */
1872*7c356e86SAndroid Build Coastguard Worker struct arg_info {
1873*7c356e86SAndroid Build Coastguard Worker const char **argv;
1874*7c356e86SAndroid Build Coastguard Worker int flags; /* AF_* */
1875*7c356e86SAndroid Build Coastguard Worker int ai_argc;
1876*7c356e86SAndroid Build Coastguard Worker int skip; /* first arg is argv[0], second is argv[1 + skip] */
1877*7c356e86SAndroid Build Coastguard Worker };
1878*7c356e86SAndroid Build Coastguard Worker
1879*7c356e86SAndroid Build Coastguard Worker /*
1880*7c356e86SAndroid Build Coastguard Worker * activation record for function blocks
1881*7c356e86SAndroid Build Coastguard Worker */
1882*7c356e86SAndroid Build Coastguard Worker struct block {
1883*7c356e86SAndroid Build Coastguard Worker Area area; /* area to allocate things */
1884*7c356e86SAndroid Build Coastguard Worker const char **argv;
1885*7c356e86SAndroid Build Coastguard Worker char *error; /* error handler */
1886*7c356e86SAndroid Build Coastguard Worker char *exit; /* exit handler */
1887*7c356e86SAndroid Build Coastguard Worker struct block *next; /* enclosing block */
1888*7c356e86SAndroid Build Coastguard Worker struct table vars; /* local variables */
1889*7c356e86SAndroid Build Coastguard Worker struct table funs; /* local functions */
1890*7c356e86SAndroid Build Coastguard Worker Getopt getopts_state;
1891*7c356e86SAndroid Build Coastguard Worker int argc;
1892*7c356e86SAndroid Build Coastguard Worker int flags; /* see BF_* */
1893*7c356e86SAndroid Build Coastguard Worker };
1894*7c356e86SAndroid Build Coastguard Worker
1895*7c356e86SAndroid Build Coastguard Worker /* Values for struct block.flags */
1896*7c356e86SAndroid Build Coastguard Worker #define BF_DOGETOPTS BIT(0) /* save/restore getopts state */
1897*7c356e86SAndroid Build Coastguard Worker #define BF_STOPENV BIT(1) /* do not export further */
1898*7c356e86SAndroid Build Coastguard Worker /* BF_RESETSPEC and NEXTLOC_BI must be numerically identical! */
1899*7c356e86SAndroid Build Coastguard Worker #define BF_RESETSPEC BIT(17) /* use ->next for set and shift */
1900*7c356e86SAndroid Build Coastguard Worker
1901*7c356e86SAndroid Build Coastguard Worker /*
1902*7c356e86SAndroid Build Coastguard Worker * Used by ktwalk() and ktnext() routines.
1903*7c356e86SAndroid Build Coastguard Worker */
1904*7c356e86SAndroid Build Coastguard Worker struct tstate {
1905*7c356e86SAndroid Build Coastguard Worker struct tbl **next;
1906*7c356e86SAndroid Build Coastguard Worker ssize_t left;
1907*7c356e86SAndroid Build Coastguard Worker };
1908*7c356e86SAndroid Build Coastguard Worker
1909*7c356e86SAndroid Build Coastguard Worker EXTERN struct table taliases; /* tracked aliases */
1910*7c356e86SAndroid Build Coastguard Worker EXTERN struct table builtins; /* built-in commands */
1911*7c356e86SAndroid Build Coastguard Worker EXTERN struct table aliases; /* aliases */
1912*7c356e86SAndroid Build Coastguard Worker EXTERN struct table keywords; /* keywords */
1913*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NOPWNAM
1914*7c356e86SAndroid Build Coastguard Worker EXTERN struct table homedirs; /* homedir() cache */
1915*7c356e86SAndroid Build Coastguard Worker #endif
1916*7c356e86SAndroid Build Coastguard Worker
1917*7c356e86SAndroid Build Coastguard Worker struct builtin {
1918*7c356e86SAndroid Build Coastguard Worker const char *name;
1919*7c356e86SAndroid Build Coastguard Worker int (*func)(const char **);
1920*7c356e86SAndroid Build Coastguard Worker };
1921*7c356e86SAndroid Build Coastguard Worker
1922*7c356e86SAndroid Build Coastguard Worker extern const struct builtin mkshbuiltins[];
1923*7c356e86SAndroid Build Coastguard Worker
1924*7c356e86SAndroid Build Coastguard Worker /* values for set_prompt() */
1925*7c356e86SAndroid Build Coastguard Worker #define PS1 0 /* command */
1926*7c356e86SAndroid Build Coastguard Worker #define PS2 1 /* command continuation */
1927*7c356e86SAndroid Build Coastguard Worker
1928*7c356e86SAndroid Build Coastguard Worker EXTERN char *path; /* copy of either PATH or def_path */
1929*7c356e86SAndroid Build Coastguard Worker EXTERN const char *def_path; /* path to use if PATH not set */
1930*7c356e86SAndroid Build Coastguard Worker EXTERN char *tmpdir; /* TMPDIR value */
1931*7c356e86SAndroid Build Coastguard Worker EXTERN const char *prompt;
1932*7c356e86SAndroid Build Coastguard Worker EXTERN uint8_t cur_prompt; /* PS1 or PS2 */
1933*7c356e86SAndroid Build Coastguard Worker EXTERN int current_lineno; /* LINENO value */
1934*7c356e86SAndroid Build Coastguard Worker
1935*7c356e86SAndroid Build Coastguard Worker /*
1936*7c356e86SAndroid Build Coastguard Worker * Description of a command or an operation on commands.
1937*7c356e86SAndroid Build Coastguard Worker */
1938*7c356e86SAndroid Build Coastguard Worker struct op {
1939*7c356e86SAndroid Build Coastguard Worker const char **args; /* arguments to a command */
1940*7c356e86SAndroid Build Coastguard Worker char **vars; /* variable assignments */
1941*7c356e86SAndroid Build Coastguard Worker struct ioword **ioact; /* IO actions (eg, < > >>) */
1942*7c356e86SAndroid Build Coastguard Worker struct op *left, *right; /* descendents */
1943*7c356e86SAndroid Build Coastguard Worker char *str; /* word for case; identifier for for,
1944*7c356e86SAndroid Build Coastguard Worker * select, and functions;
1945*7c356e86SAndroid Build Coastguard Worker * path to execute for TEXEC;
1946*7c356e86SAndroid Build Coastguard Worker * time hook for TCOM.
1947*7c356e86SAndroid Build Coastguard Worker */
1948*7c356e86SAndroid Build Coastguard Worker int lineno; /* TCOM/TFUNC: LINENO for this */
1949*7c356e86SAndroid Build Coastguard Worker short type; /* operation type, see below */
1950*7c356e86SAndroid Build Coastguard Worker /* WARNING: newtp(), tcopy() use evalflags = 0 to clear union */
1951*7c356e86SAndroid Build Coastguard Worker union {
1952*7c356e86SAndroid Build Coastguard Worker /* TCOM: arg expansion eval() flags */
1953*7c356e86SAndroid Build Coastguard Worker short evalflags;
1954*7c356e86SAndroid Build Coastguard Worker /* TFUNC: function x (vs x()) */
1955*7c356e86SAndroid Build Coastguard Worker short ksh_func;
1956*7c356e86SAndroid Build Coastguard Worker /* TPAT: termination character */
1957*7c356e86SAndroid Build Coastguard Worker char charflag;
1958*7c356e86SAndroid Build Coastguard Worker } u;
1959*7c356e86SAndroid Build Coastguard Worker };
1960*7c356e86SAndroid Build Coastguard Worker
1961*7c356e86SAndroid Build Coastguard Worker /* Tree.type values */
1962*7c356e86SAndroid Build Coastguard Worker #define TEOF 0
1963*7c356e86SAndroid Build Coastguard Worker #define TCOM 1 /* command */
1964*7c356e86SAndroid Build Coastguard Worker #define TPAREN 2 /* (c-list) */
1965*7c356e86SAndroid Build Coastguard Worker #define TPIPE 3 /* a | b */
1966*7c356e86SAndroid Build Coastguard Worker #define TLIST 4 /* a ; b */
1967*7c356e86SAndroid Build Coastguard Worker #define TOR 5 /* || */
1968*7c356e86SAndroid Build Coastguard Worker #define TAND 6 /* && */
1969*7c356e86SAndroid Build Coastguard Worker #define TBANG 7 /* ! */
1970*7c356e86SAndroid Build Coastguard Worker #define TDBRACKET 8 /* [[ .. ]] */
1971*7c356e86SAndroid Build Coastguard Worker #define TFOR 9
1972*7c356e86SAndroid Build Coastguard Worker #define TSELECT 10
1973*7c356e86SAndroid Build Coastguard Worker #define TCASE 11
1974*7c356e86SAndroid Build Coastguard Worker #define TIF 12
1975*7c356e86SAndroid Build Coastguard Worker #define TWHILE 13
1976*7c356e86SAndroid Build Coastguard Worker #define TUNTIL 14
1977*7c356e86SAndroid Build Coastguard Worker #define TELIF 15
1978*7c356e86SAndroid Build Coastguard Worker #define TPAT 16 /* pattern in case */
1979*7c356e86SAndroid Build Coastguard Worker #define TBRACE 17 /* {c-list} */
1980*7c356e86SAndroid Build Coastguard Worker #define TASYNC 18 /* c & */
1981*7c356e86SAndroid Build Coastguard Worker #define TFUNCT 19 /* function name { command; } */
1982*7c356e86SAndroid Build Coastguard Worker #define TTIME 20 /* time pipeline */
1983*7c356e86SAndroid Build Coastguard Worker #define TEXEC 21 /* fork/exec eval'd TCOM */
1984*7c356e86SAndroid Build Coastguard Worker #define TCOPROC 22 /* coprocess |& */
1985*7c356e86SAndroid Build Coastguard Worker
1986*7c356e86SAndroid Build Coastguard Worker /*
1987*7c356e86SAndroid Build Coastguard Worker * prefix codes for words in command tree
1988*7c356e86SAndroid Build Coastguard Worker */
1989*7c356e86SAndroid Build Coastguard Worker #define EOS 0 /* end of string */
1990*7c356e86SAndroid Build Coastguard Worker #define CHAR 1 /* unquoted character */
1991*7c356e86SAndroid Build Coastguard Worker #define QCHAR 2 /* quoted character */
1992*7c356e86SAndroid Build Coastguard Worker #define COMSUB 3 /* $() substitution (0 terminated) */
1993*7c356e86SAndroid Build Coastguard Worker #define EXPRSUB 4 /* $(()) substitution (0 terminated) */
1994*7c356e86SAndroid Build Coastguard Worker #define OQUOTE 5 /* opening " or ' */
1995*7c356e86SAndroid Build Coastguard Worker #define CQUOTE 6 /* closing " or ' */
1996*7c356e86SAndroid Build Coastguard Worker #define OSUBST 7 /* opening ${ subst (followed by { or X) */
1997*7c356e86SAndroid Build Coastguard Worker #define CSUBST 8 /* closing } of above (followed by } or X) */
1998*7c356e86SAndroid Build Coastguard Worker #define OPAT 9 /* open pattern: *(, @(, etc. */
1999*7c356e86SAndroid Build Coastguard Worker #define SPAT 10 /* separate pattern: | */
2000*7c356e86SAndroid Build Coastguard Worker #define CPAT 11 /* close pattern: ) */
2001*7c356e86SAndroid Build Coastguard Worker #define ADELIM 12 /* arbitrary delimiter: ${foo:2:3} ${foo/bar/baz} */
2002*7c356e86SAndroid Build Coastguard Worker #define FUNSUB 14 /* ${ foo;} substitution (NUL terminated) */
2003*7c356e86SAndroid Build Coastguard Worker #define VALSUB 15 /* ${|foo;} substitution (NUL terminated) */
2004*7c356e86SAndroid Build Coastguard Worker #define COMASUB 16 /* `…` substitution (COMSUB but expand aliases) */
2005*7c356e86SAndroid Build Coastguard Worker #define FUNASUB 17 /* function substitution but expand aliases */
2006*7c356e86SAndroid Build Coastguard Worker
2007*7c356e86SAndroid Build Coastguard Worker /*
2008*7c356e86SAndroid Build Coastguard Worker * IO redirection
2009*7c356e86SAndroid Build Coastguard Worker */
2010*7c356e86SAndroid Build Coastguard Worker struct ioword {
2011*7c356e86SAndroid Build Coastguard Worker char *ioname; /* filename (unused if heredoc) */
2012*7c356e86SAndroid Build Coastguard Worker char *delim; /* delimiter for <<, <<- */
2013*7c356e86SAndroid Build Coastguard Worker char *heredoc; /* content of heredoc */
2014*7c356e86SAndroid Build Coastguard Worker unsigned short ioflag; /* action (below) */
2015*7c356e86SAndroid Build Coastguard Worker short unit; /* unit (fd) affected */
2016*7c356e86SAndroid Build Coastguard Worker };
2017*7c356e86SAndroid Build Coastguard Worker
2018*7c356e86SAndroid Build Coastguard Worker /* ioword.flag - type of redirection */
2019*7c356e86SAndroid Build Coastguard Worker #define IOTYPE 0xF /* type: bits 0:3 */
2020*7c356e86SAndroid Build Coastguard Worker #define IOREAD 0x1 /* < */
2021*7c356e86SAndroid Build Coastguard Worker #define IOWRITE 0x2 /* > */
2022*7c356e86SAndroid Build Coastguard Worker #define IORDWR 0x3 /* <>: todo */
2023*7c356e86SAndroid Build Coastguard Worker #define IOHERE 0x4 /* << (here file) */
2024*7c356e86SAndroid Build Coastguard Worker #define IOCAT 0x5 /* >> */
2025*7c356e86SAndroid Build Coastguard Worker #define IODUP 0x6 /* <&/>& */
2026*7c356e86SAndroid Build Coastguard Worker #define IOEVAL BIT(4) /* expand in << */
2027*7c356e86SAndroid Build Coastguard Worker #define IOSKIP BIT(5) /* <<-, skip ^\t* */
2028*7c356e86SAndroid Build Coastguard Worker #define IOCLOB BIT(6) /* >|, override -o noclobber */
2029*7c356e86SAndroid Build Coastguard Worker #define IORDUP BIT(7) /* x<&y (as opposed to x>&y) */
2030*7c356e86SAndroid Build Coastguard Worker #define IODUPSELF BIT(8) /* x>&x (as opposed to x>&y) */
2031*7c356e86SAndroid Build Coastguard Worker #define IONAMEXP BIT(9) /* name has been expanded */
2032*7c356e86SAndroid Build Coastguard Worker #define IOBASH BIT(10) /* &> etc. */
2033*7c356e86SAndroid Build Coastguard Worker #define IOHERESTR BIT(11) /* <<< (here string) */
2034*7c356e86SAndroid Build Coastguard Worker #define IONDELIM BIT(12) /* null delimiter (<<) */
2035*7c356e86SAndroid Build Coastguard Worker
2036*7c356e86SAndroid Build Coastguard Worker /* execute/exchild flags */
2037*7c356e86SAndroid Build Coastguard Worker #define XEXEC BIT(0) /* execute without forking */
2038*7c356e86SAndroid Build Coastguard Worker #define XFORK BIT(1) /* fork before executing */
2039*7c356e86SAndroid Build Coastguard Worker #define XBGND BIT(2) /* command & */
2040*7c356e86SAndroid Build Coastguard Worker #define XPIPEI BIT(3) /* input is pipe */
2041*7c356e86SAndroid Build Coastguard Worker #define XPIPEO BIT(4) /* output is pipe */
2042*7c356e86SAndroid Build Coastguard Worker #define XXCOM BIT(5) /* `...` command */
2043*7c356e86SAndroid Build Coastguard Worker #define XPCLOSE BIT(6) /* exchild: close close_fd in parent */
2044*7c356e86SAndroid Build Coastguard Worker #define XCCLOSE BIT(7) /* exchild: close close_fd in child */
2045*7c356e86SAndroid Build Coastguard Worker #define XERROK BIT(8) /* non-zero exit ok (for set -e) */
2046*7c356e86SAndroid Build Coastguard Worker #define XCOPROC BIT(9) /* starting a co-process */
2047*7c356e86SAndroid Build Coastguard Worker #define XTIME BIT(10) /* timing TCOM command */
2048*7c356e86SAndroid Build Coastguard Worker #define XPIPEST BIT(11) /* want PIPESTATUS */
2049*7c356e86SAndroid Build Coastguard Worker
2050*7c356e86SAndroid Build Coastguard Worker /*
2051*7c356e86SAndroid Build Coastguard Worker * flags to control expansion of words (assumed by t->evalflags to fit
2052*7c356e86SAndroid Build Coastguard Worker * in a short)
2053*7c356e86SAndroid Build Coastguard Worker */
2054*7c356e86SAndroid Build Coastguard Worker #define DOBLANK BIT(0) /* perform blank interpretation */
2055*7c356e86SAndroid Build Coastguard Worker #define DOGLOB BIT(1) /* expand [?* */
2056*7c356e86SAndroid Build Coastguard Worker #define DOPAT BIT(2) /* quote *?[ */
2057*7c356e86SAndroid Build Coastguard Worker #define DOTILDE BIT(3) /* normal ~ expansion (first char) */
2058*7c356e86SAndroid Build Coastguard Worker #define DONTRUNCOMMAND BIT(4) /* do not run $(command) things */
2059*7c356e86SAndroid Build Coastguard Worker #define DOASNTILDE BIT(5) /* assignment ~ expansion (after =, :) */
2060*7c356e86SAndroid Build Coastguard Worker #define DOBRACE BIT(6) /* used by expand(): do brace expansion */
2061*7c356e86SAndroid Build Coastguard Worker #define DOMAGIC BIT(7) /* used by expand(): string contains MAGIC */
2062*7c356e86SAndroid Build Coastguard Worker #define DOTEMP BIT(8) /* dito: in word part of ${..[%#=?]..} */
2063*7c356e86SAndroid Build Coastguard Worker #define DOVACHECK BIT(9) /* var assign check (for typeset, set, etc) */
2064*7c356e86SAndroid Build Coastguard Worker #define DOMARKDIRS BIT(10) /* force markdirs behaviour */
2065*7c356e86SAndroid Build Coastguard Worker #define DOTCOMEXEC BIT(11) /* not an eval flag, used by sh -c hack */
2066*7c356e86SAndroid Build Coastguard Worker #define DOSCALAR BIT(12) /* change field handling to non-list context */
2067*7c356e86SAndroid Build Coastguard Worker #define DOHEREDOC BIT(13) /* change scalar handling to heredoc body */
2068*7c356e86SAndroid Build Coastguard Worker #define DOHERESTR BIT(14) /* append a newline char */
2069*7c356e86SAndroid Build Coastguard Worker #define DODBMAGIC BIT(15) /* add magic to expansions for [[ x = $y ]] */
2070*7c356e86SAndroid Build Coastguard Worker
2071*7c356e86SAndroid Build Coastguard Worker #define X_EXTRA 20 /* this many extra bytes in X string */
2072*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
2073*7c356e86SAndroid Build Coastguard Worker #define X_WASTE 15 /* allowed extra bytes to avoid shrinking, */
2074*7c356e86SAndroid Build Coastguard Worker #else
2075*7c356e86SAndroid Build Coastguard Worker #define X_WASTE 255 /* … must be 2ⁿ-1 */
2076*7c356e86SAndroid Build Coastguard Worker #endif
2077*7c356e86SAndroid Build Coastguard Worker
2078*7c356e86SAndroid Build Coastguard Worker typedef struct XString {
2079*7c356e86SAndroid Build Coastguard Worker /* beginning of string */
2080*7c356e86SAndroid Build Coastguard Worker char *beg;
2081*7c356e86SAndroid Build Coastguard Worker /* length of allocated area, minus safety margin */
2082*7c356e86SAndroid Build Coastguard Worker size_t len;
2083*7c356e86SAndroid Build Coastguard Worker /* end of string */
2084*7c356e86SAndroid Build Coastguard Worker char *end;
2085*7c356e86SAndroid Build Coastguard Worker /* memory area used */
2086*7c356e86SAndroid Build Coastguard Worker Area *areap;
2087*7c356e86SAndroid Build Coastguard Worker } XString;
2088*7c356e86SAndroid Build Coastguard Worker
2089*7c356e86SAndroid Build Coastguard Worker /* initialise expandable string */
2090*7c356e86SAndroid Build Coastguard Worker #define XinitN(xs,length,area) do { \
2091*7c356e86SAndroid Build Coastguard Worker (xs).len = (length); \
2092*7c356e86SAndroid Build Coastguard Worker (xs).areap = (area); \
2093*7c356e86SAndroid Build Coastguard Worker (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
2094*7c356e86SAndroid Build Coastguard Worker (xs).end = (xs).beg + (xs).len; \
2095*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
2096*7c356e86SAndroid Build Coastguard Worker #define Xinit(xs,xp,length,area) do { \
2097*7c356e86SAndroid Build Coastguard Worker XinitN((xs), (length), (area)); \
2098*7c356e86SAndroid Build Coastguard Worker (xp) = (xs).beg; \
2099*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
2100*7c356e86SAndroid Build Coastguard Worker
2101*7c356e86SAndroid Build Coastguard Worker /* stuff char into string */
2102*7c356e86SAndroid Build Coastguard Worker #define Xput(xs,xp,c) (*xp++ = (c))
2103*7c356e86SAndroid Build Coastguard Worker
2104*7c356e86SAndroid Build Coastguard Worker /* check if there are at least n bytes left */
2105*7c356e86SAndroid Build Coastguard Worker #define XcheckN(xs,xp,n) do { \
2106*7c356e86SAndroid Build Coastguard Worker ssize_t more = ((xp) + (n)) - (xs).end; \
2107*7c356e86SAndroid Build Coastguard Worker if (more > 0) \
2108*7c356e86SAndroid Build Coastguard Worker (xp) = Xcheck_grow(&(xs), (xp), (size_t)more); \
2109*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
2110*7c356e86SAndroid Build Coastguard Worker
2111*7c356e86SAndroid Build Coastguard Worker /* check for overflow, expand string */
2112*7c356e86SAndroid Build Coastguard Worker #define Xcheck(xs,xp) XcheckN((xs), (xp), 1)
2113*7c356e86SAndroid Build Coastguard Worker
2114*7c356e86SAndroid Build Coastguard Worker /* free string */
2115*7c356e86SAndroid Build Coastguard Worker #define Xfree(xs,xp) afree((xs).beg, (xs).areap)
2116*7c356e86SAndroid Build Coastguard Worker
2117*7c356e86SAndroid Build Coastguard Worker /* close, return string */
2118*7c356e86SAndroid Build Coastguard Worker #define Xclose(xs,xp) aresize((xs).beg, (xp) - (xs).beg, (xs).areap)
2119*7c356e86SAndroid Build Coastguard Worker
2120*7c356e86SAndroid Build Coastguard Worker /* beginning of string */
2121*7c356e86SAndroid Build Coastguard Worker #define Xstring(xs,xp) ((xs).beg)
2122*7c356e86SAndroid Build Coastguard Worker
2123*7c356e86SAndroid Build Coastguard Worker #define Xnleft(xs,xp) ((xs).end - (xp)) /* may be less than 0 */
2124*7c356e86SAndroid Build Coastguard Worker #define Xlength(xs,xp) ((xp) - (xs).beg)
2125*7c356e86SAndroid Build Coastguard Worker #define Xsize(xs,xp) ((xs).end - (xs).beg)
2126*7c356e86SAndroid Build Coastguard Worker #define Xsavepos(xs,xp) ((xp) - (xs).beg)
2127*7c356e86SAndroid Build Coastguard Worker #define Xrestpos(xs,xp,n) ((xs).beg + (n))
2128*7c356e86SAndroid Build Coastguard Worker
2129*7c356e86SAndroid Build Coastguard Worker char *Xcheck_grow(XString *, const char *, size_t);
2130*7c356e86SAndroid Build Coastguard Worker
2131*7c356e86SAndroid Build Coastguard Worker /*
2132*7c356e86SAndroid Build Coastguard Worker * expandable vector of generic pointers
2133*7c356e86SAndroid Build Coastguard Worker */
2134*7c356e86SAndroid Build Coastguard Worker
2135*7c356e86SAndroid Build Coastguard Worker typedef struct {
2136*7c356e86SAndroid Build Coastguard Worker /* beginning of allocated area */
2137*7c356e86SAndroid Build Coastguard Worker void **beg;
2138*7c356e86SAndroid Build Coastguard Worker /* currently used number of entries */
2139*7c356e86SAndroid Build Coastguard Worker size_t len;
2140*7c356e86SAndroid Build Coastguard Worker /* allocated number of entries */
2141*7c356e86SAndroid Build Coastguard Worker size_t siz;
2142*7c356e86SAndroid Build Coastguard Worker } XPtrV;
2143*7c356e86SAndroid Build Coastguard Worker
2144*7c356e86SAndroid Build Coastguard Worker #define XPinit(x,n) do { \
2145*7c356e86SAndroid Build Coastguard Worker (x).siz = (n); \
2146*7c356e86SAndroid Build Coastguard Worker (x).len = 0; \
2147*7c356e86SAndroid Build Coastguard Worker (x).beg = alloc2((x).siz, sizeof(void *), ATEMP); \
2148*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0) \
2149*7c356e86SAndroid Build Coastguard Worker
2150*7c356e86SAndroid Build Coastguard Worker #define XPput(x,p) do { \
2151*7c356e86SAndroid Build Coastguard Worker if ((x).len == (x).siz) { \
2152*7c356e86SAndroid Build Coastguard Worker (x).beg = aresize2((x).beg, (x).siz, \
2153*7c356e86SAndroid Build Coastguard Worker 2 * sizeof(void *), ATEMP); \
2154*7c356e86SAndroid Build Coastguard Worker (x).siz <<= 1; \
2155*7c356e86SAndroid Build Coastguard Worker } \
2156*7c356e86SAndroid Build Coastguard Worker (x).beg[(x).len++] = (p); \
2157*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
2158*7c356e86SAndroid Build Coastguard Worker
2159*7c356e86SAndroid Build Coastguard Worker #define XPptrv(x) ((x).beg)
2160*7c356e86SAndroid Build Coastguard Worker #define XPsize(x) ((x).len)
2161*7c356e86SAndroid Build Coastguard Worker #define XPclose(x) aresize2((x).beg, XPsize(x), sizeof(void *), ATEMP)
2162*7c356e86SAndroid Build Coastguard Worker #define XPfree(x) afree((x).beg, ATEMP)
2163*7c356e86SAndroid Build Coastguard Worker
2164*7c356e86SAndroid Build Coastguard Worker /* for print_columns */
2165*7c356e86SAndroid Build Coastguard Worker
2166*7c356e86SAndroid Build Coastguard Worker struct columnise_opts {
2167*7c356e86SAndroid Build Coastguard Worker struct shf *shf;
2168*7c356e86SAndroid Build Coastguard Worker char linesep;
2169*7c356e86SAndroid Build Coastguard Worker bool do_last;
2170*7c356e86SAndroid Build Coastguard Worker bool prefcol;
2171*7c356e86SAndroid Build Coastguard Worker };
2172*7c356e86SAndroid Build Coastguard Worker
2173*7c356e86SAndroid Build Coastguard Worker /*
2174*7c356e86SAndroid Build Coastguard Worker * Lexer internals
2175*7c356e86SAndroid Build Coastguard Worker */
2176*7c356e86SAndroid Build Coastguard Worker
2177*7c356e86SAndroid Build Coastguard Worker typedef struct source Source;
2178*7c356e86SAndroid Build Coastguard Worker struct source {
2179*7c356e86SAndroid Build Coastguard Worker /* input buffer */
2180*7c356e86SAndroid Build Coastguard Worker XString xs;
2181*7c356e86SAndroid Build Coastguard Worker /* memory area, also checked in reclaim() */
2182*7c356e86SAndroid Build Coastguard Worker Area *areap;
2183*7c356e86SAndroid Build Coastguard Worker /* stacked source */
2184*7c356e86SAndroid Build Coastguard Worker Source *next;
2185*7c356e86SAndroid Build Coastguard Worker /* input pointer */
2186*7c356e86SAndroid Build Coastguard Worker const char *str;
2187*7c356e86SAndroid Build Coastguard Worker /* start of current buffer */
2188*7c356e86SAndroid Build Coastguard Worker const char *start;
2189*7c356e86SAndroid Build Coastguard Worker /* input file name */
2190*7c356e86SAndroid Build Coastguard Worker const char *file;
2191*7c356e86SAndroid Build Coastguard Worker /* extra data */
2192*7c356e86SAndroid Build Coastguard Worker union {
2193*7c356e86SAndroid Build Coastguard Worker /* string[] */
2194*7c356e86SAndroid Build Coastguard Worker const char **strv;
2195*7c356e86SAndroid Build Coastguard Worker /* shell file */
2196*7c356e86SAndroid Build Coastguard Worker struct shf *shf;
2197*7c356e86SAndroid Build Coastguard Worker /* alias (SF_HASALIAS) */
2198*7c356e86SAndroid Build Coastguard Worker struct tbl *tblp;
2199*7c356e86SAndroid Build Coastguard Worker /* (also for SREREAD) */
2200*7c356e86SAndroid Build Coastguard Worker char *freeme;
2201*7c356e86SAndroid Build Coastguard Worker } u;
2202*7c356e86SAndroid Build Coastguard Worker /* flags */
2203*7c356e86SAndroid Build Coastguard Worker int flags;
2204*7c356e86SAndroid Build Coastguard Worker /* input type */
2205*7c356e86SAndroid Build Coastguard Worker int type;
2206*7c356e86SAndroid Build Coastguard Worker /* line number */
2207*7c356e86SAndroid Build Coastguard Worker int line;
2208*7c356e86SAndroid Build Coastguard Worker /* line the error occurred on (0 if not set) */
2209*7c356e86SAndroid Build Coastguard Worker int errline;
2210*7c356e86SAndroid Build Coastguard Worker /* buffer for ungetsc() (SREREAD) and alias (SALIAS) */
2211*7c356e86SAndroid Build Coastguard Worker char ugbuf[2];
2212*7c356e86SAndroid Build Coastguard Worker };
2213*7c356e86SAndroid Build Coastguard Worker
2214*7c356e86SAndroid Build Coastguard Worker /* Source.type values */
2215*7c356e86SAndroid Build Coastguard Worker #define SEOF 0 /* input EOF */
2216*7c356e86SAndroid Build Coastguard Worker #define SFILE 1 /* file input */
2217*7c356e86SAndroid Build Coastguard Worker #define SSTDIN 2 /* read stdin */
2218*7c356e86SAndroid Build Coastguard Worker #define SSTRING 3 /* string */
2219*7c356e86SAndroid Build Coastguard Worker #define SWSTR 4 /* string without \n */
2220*7c356e86SAndroid Build Coastguard Worker #define SWORDS 5 /* string[] */
2221*7c356e86SAndroid Build Coastguard Worker #define SWORDSEP 6 /* string[] separator */
2222*7c356e86SAndroid Build Coastguard Worker #define SALIAS 7 /* alias expansion */
2223*7c356e86SAndroid Build Coastguard Worker #define SREREAD 8 /* read ahead to be re-scanned */
2224*7c356e86SAndroid Build Coastguard Worker #define SSTRINGCMDLINE 9 /* string from "mksh -c ..." */
2225*7c356e86SAndroid Build Coastguard Worker
2226*7c356e86SAndroid Build Coastguard Worker /* Source.flags values */
2227*7c356e86SAndroid Build Coastguard Worker #define SF_ECHO BIT(0) /* echo input to shlout */
2228*7c356e86SAndroid Build Coastguard Worker #define SF_ALIAS BIT(1) /* faking space at end of alias */
2229*7c356e86SAndroid Build Coastguard Worker #define SF_ALIASEND BIT(2) /* faking space at end of alias */
2230*7c356e86SAndroid Build Coastguard Worker #define SF_TTY BIT(3) /* type == SSTDIN & it is a tty */
2231*7c356e86SAndroid Build Coastguard Worker #define SF_HASALIAS BIT(4) /* u.tblp valid (SALIAS, SEOF) */
2232*7c356e86SAndroid Build Coastguard Worker #define SF_MAYEXEC BIT(5) /* special sh -c optimisation hack */
2233*7c356e86SAndroid Build Coastguard Worker
2234*7c356e86SAndroid Build Coastguard Worker typedef union {
2235*7c356e86SAndroid Build Coastguard Worker int i;
2236*7c356e86SAndroid Build Coastguard Worker char *cp;
2237*7c356e86SAndroid Build Coastguard Worker char **wp;
2238*7c356e86SAndroid Build Coastguard Worker struct op *o;
2239*7c356e86SAndroid Build Coastguard Worker struct ioword *iop;
2240*7c356e86SAndroid Build Coastguard Worker } YYSTYPE;
2241*7c356e86SAndroid Build Coastguard Worker
2242*7c356e86SAndroid Build Coastguard Worker /* If something is added here, add it to tokentab[] in syn.c as well */
2243*7c356e86SAndroid Build Coastguard Worker #define LWORD 256
2244*7c356e86SAndroid Build Coastguard Worker #define LOGAND 257 /* && */
2245*7c356e86SAndroid Build Coastguard Worker #define LOGOR 258 /* || */
2246*7c356e86SAndroid Build Coastguard Worker #define BREAK 259 /* ;; */
2247*7c356e86SAndroid Build Coastguard Worker #define IF 260
2248*7c356e86SAndroid Build Coastguard Worker #define THEN 261
2249*7c356e86SAndroid Build Coastguard Worker #define ELSE 262
2250*7c356e86SAndroid Build Coastguard Worker #define ELIF 263
2251*7c356e86SAndroid Build Coastguard Worker #define FI 264
2252*7c356e86SAndroid Build Coastguard Worker #define CASE 265
2253*7c356e86SAndroid Build Coastguard Worker #define ESAC 266
2254*7c356e86SAndroid Build Coastguard Worker #define FOR 267
2255*7c356e86SAndroid Build Coastguard Worker #define SELECT 268
2256*7c356e86SAndroid Build Coastguard Worker #define WHILE 269
2257*7c356e86SAndroid Build Coastguard Worker #define UNTIL 270
2258*7c356e86SAndroid Build Coastguard Worker #define DO 271
2259*7c356e86SAndroid Build Coastguard Worker #define DONE 272
2260*7c356e86SAndroid Build Coastguard Worker #define IN 273
2261*7c356e86SAndroid Build Coastguard Worker #define FUNCTION 274
2262*7c356e86SAndroid Build Coastguard Worker #define TIME 275
2263*7c356e86SAndroid Build Coastguard Worker #define REDIR 276
2264*7c356e86SAndroid Build Coastguard Worker #define MDPAREN 277 /* (( )) */
2265*7c356e86SAndroid Build Coastguard Worker #define BANG 278 /* ! */
2266*7c356e86SAndroid Build Coastguard Worker #define DBRACKET 279 /* [[ .. ]] */
2267*7c356e86SAndroid Build Coastguard Worker #define COPROC 280 /* |& */
2268*7c356e86SAndroid Build Coastguard Worker #define BRKEV 281 /* ;| */
2269*7c356e86SAndroid Build Coastguard Worker #define BRKFT 282 /* ;& */
2270*7c356e86SAndroid Build Coastguard Worker #define YYERRCODE 300
2271*7c356e86SAndroid Build Coastguard Worker
2272*7c356e86SAndroid Build Coastguard Worker /* flags to yylex */
2273*7c356e86SAndroid Build Coastguard Worker #define CONTIN BIT(0) /* skip new lines to complete command */
2274*7c356e86SAndroid Build Coastguard Worker #define ONEWORD BIT(1) /* single word for substitute() */
2275*7c356e86SAndroid Build Coastguard Worker #define ALIAS BIT(2) /* recognise alias */
2276*7c356e86SAndroid Build Coastguard Worker #define KEYWORD BIT(3) /* recognise keywords */
2277*7c356e86SAndroid Build Coastguard Worker #define LETEXPR BIT(4) /* get expression inside (( )) */
2278*7c356e86SAndroid Build Coastguard Worker #define CMDASN BIT(5) /* parse x[1 & 2] as one word, for typeset */
2279*7c356e86SAndroid Build Coastguard Worker #define HEREDOC BIT(6) /* parsing a here document body */
2280*7c356e86SAndroid Build Coastguard Worker #define ESACONLY BIT(7) /* only accept esac keyword */
2281*7c356e86SAndroid Build Coastguard Worker #define CMDWORD BIT(8) /* parsing simple command (alias related) */
2282*7c356e86SAndroid Build Coastguard Worker #define HEREDELIM BIT(9) /* parsing <<,<<- delimiter */
2283*7c356e86SAndroid Build Coastguard Worker #define LQCHAR BIT(10) /* source string contains QCHAR */
2284*7c356e86SAndroid Build Coastguard Worker
2285*7c356e86SAndroid Build Coastguard Worker #define HERES 10 /* max number of << in line */
2286*7c356e86SAndroid Build Coastguard Worker
2287*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_EBCDIC
2288*7c356e86SAndroid Build Coastguard Worker #define CTRL_AT (0x00U)
2289*7c356e86SAndroid Build Coastguard Worker #define CTRL_A (0x01U)
2290*7c356e86SAndroid Build Coastguard Worker #define CTRL_B (0x02U)
2291*7c356e86SAndroid Build Coastguard Worker #define CTRL_C (0x03U)
2292*7c356e86SAndroid Build Coastguard Worker #define CTRL_D (0x37U)
2293*7c356e86SAndroid Build Coastguard Worker #define CTRL_E (0x2DU)
2294*7c356e86SAndroid Build Coastguard Worker #define CTRL_F (0x2EU)
2295*7c356e86SAndroid Build Coastguard Worker #define CTRL_G (0x2FU)
2296*7c356e86SAndroid Build Coastguard Worker #define CTRL_H (0x16U)
2297*7c356e86SAndroid Build Coastguard Worker #define CTRL_I (0x05U)
2298*7c356e86SAndroid Build Coastguard Worker #define CTRL_J (0x15U)
2299*7c356e86SAndroid Build Coastguard Worker #define CTRL_K (0x0BU)
2300*7c356e86SAndroid Build Coastguard Worker #define CTRL_L (0x0CU)
2301*7c356e86SAndroid Build Coastguard Worker #define CTRL_M (0x0DU)
2302*7c356e86SAndroid Build Coastguard Worker #define CTRL_N (0x0EU)
2303*7c356e86SAndroid Build Coastguard Worker #define CTRL_O (0x0FU)
2304*7c356e86SAndroid Build Coastguard Worker #define CTRL_P (0x10U)
2305*7c356e86SAndroid Build Coastguard Worker #define CTRL_Q (0x11U)
2306*7c356e86SAndroid Build Coastguard Worker #define CTRL_R (0x12U)
2307*7c356e86SAndroid Build Coastguard Worker #define CTRL_S (0x13U)
2308*7c356e86SAndroid Build Coastguard Worker #define CTRL_T (0x3CU)
2309*7c356e86SAndroid Build Coastguard Worker #define CTRL_U (0x3DU)
2310*7c356e86SAndroid Build Coastguard Worker #define CTRL_V (0x32U)
2311*7c356e86SAndroid Build Coastguard Worker #define CTRL_W (0x26U)
2312*7c356e86SAndroid Build Coastguard Worker #define CTRL_X (0x18U)
2313*7c356e86SAndroid Build Coastguard Worker #define CTRL_Y (0x19U)
2314*7c356e86SAndroid Build Coastguard Worker #define CTRL_Z (0x3FU)
2315*7c356e86SAndroid Build Coastguard Worker #define CTRL_BO (0x27U)
2316*7c356e86SAndroid Build Coastguard Worker #define CTRL_BK (0x1CU)
2317*7c356e86SAndroid Build Coastguard Worker #define CTRL_BC (0x1DU)
2318*7c356e86SAndroid Build Coastguard Worker #define CTRL_CA (0x1EU)
2319*7c356e86SAndroid Build Coastguard Worker #define CTRL_US (0x1FU)
2320*7c356e86SAndroid Build Coastguard Worker #define CTRL_QM (0x07U)
2321*7c356e86SAndroid Build Coastguard Worker #else
2322*7c356e86SAndroid Build Coastguard Worker #define CTRL_AT (0x00U)
2323*7c356e86SAndroid Build Coastguard Worker #define CTRL_A (0x01U)
2324*7c356e86SAndroid Build Coastguard Worker #define CTRL_B (0x02U)
2325*7c356e86SAndroid Build Coastguard Worker #define CTRL_C (0x03U)
2326*7c356e86SAndroid Build Coastguard Worker #define CTRL_D (0x04U)
2327*7c356e86SAndroid Build Coastguard Worker #define CTRL_E (0x05U)
2328*7c356e86SAndroid Build Coastguard Worker #define CTRL_F (0x06U)
2329*7c356e86SAndroid Build Coastguard Worker #define CTRL_G (0x07U)
2330*7c356e86SAndroid Build Coastguard Worker #define CTRL_H (0x08U)
2331*7c356e86SAndroid Build Coastguard Worker #define CTRL_I (0x09U)
2332*7c356e86SAndroid Build Coastguard Worker #define CTRL_J (0x0AU)
2333*7c356e86SAndroid Build Coastguard Worker #define CTRL_K (0x0BU)
2334*7c356e86SAndroid Build Coastguard Worker #define CTRL_L (0x0CU)
2335*7c356e86SAndroid Build Coastguard Worker #define CTRL_M (0x0DU)
2336*7c356e86SAndroid Build Coastguard Worker #define CTRL_N (0x0EU)
2337*7c356e86SAndroid Build Coastguard Worker #define CTRL_O (0x0FU)
2338*7c356e86SAndroid Build Coastguard Worker #define CTRL_P (0x10U)
2339*7c356e86SAndroid Build Coastguard Worker #define CTRL_Q (0x11U)
2340*7c356e86SAndroid Build Coastguard Worker #define CTRL_R (0x12U)
2341*7c356e86SAndroid Build Coastguard Worker #define CTRL_S (0x13U)
2342*7c356e86SAndroid Build Coastguard Worker #define CTRL_T (0x14U)
2343*7c356e86SAndroid Build Coastguard Worker #define CTRL_U (0x15U)
2344*7c356e86SAndroid Build Coastguard Worker #define CTRL_V (0x16U)
2345*7c356e86SAndroid Build Coastguard Worker #define CTRL_W (0x17U)
2346*7c356e86SAndroid Build Coastguard Worker #define CTRL_X (0x18U)
2347*7c356e86SAndroid Build Coastguard Worker #define CTRL_Y (0x19U)
2348*7c356e86SAndroid Build Coastguard Worker #define CTRL_Z (0x1AU)
2349*7c356e86SAndroid Build Coastguard Worker #define CTRL_BO (0x1BU)
2350*7c356e86SAndroid Build Coastguard Worker #define CTRL_BK (0x1CU)
2351*7c356e86SAndroid Build Coastguard Worker #define CTRL_BC (0x1DU)
2352*7c356e86SAndroid Build Coastguard Worker #define CTRL_CA (0x1EU)
2353*7c356e86SAndroid Build Coastguard Worker #define CTRL_US (0x1FU)
2354*7c356e86SAndroid Build Coastguard Worker #define CTRL_QM (0x7FU)
2355*7c356e86SAndroid Build Coastguard Worker #endif
2356*7c356e86SAndroid Build Coastguard Worker
2357*7c356e86SAndroid Build Coastguard Worker #define IDENT 64
2358*7c356e86SAndroid Build Coastguard Worker
2359*7c356e86SAndroid Build Coastguard Worker EXTERN Source *source; /* yyparse/yylex source */
2360*7c356e86SAndroid Build Coastguard Worker EXTERN YYSTYPE yylval; /* result from yylex */
2361*7c356e86SAndroid Build Coastguard Worker EXTERN struct ioword *heres[HERES], **herep;
2362*7c356e86SAndroid Build Coastguard Worker EXTERN char ident[IDENT + 1];
2363*7c356e86SAndroid Build Coastguard Worker
2364*7c356e86SAndroid Build Coastguard Worker EXTERN char **history; /* saved commands */
2365*7c356e86SAndroid Build Coastguard Worker EXTERN char **histptr; /* last history item */
2366*7c356e86SAndroid Build Coastguard Worker EXTERN mksh_ari_t histsize; /* history size */
2367*7c356e86SAndroid Build Coastguard Worker
2368*7c356e86SAndroid Build Coastguard Worker /* flags to histsave */
2369*7c356e86SAndroid Build Coastguard Worker #define HIST_FLUSH 0
2370*7c356e86SAndroid Build Coastguard Worker #define HIST_QUEUE 1
2371*7c356e86SAndroid Build Coastguard Worker #define HIST_APPEND 2
2372*7c356e86SAndroid Build Coastguard Worker #define HIST_STORE 3
2373*7c356e86SAndroid Build Coastguard Worker #define HIST_NOTE 4
2374*7c356e86SAndroid Build Coastguard Worker
2375*7c356e86SAndroid Build Coastguard Worker /* user and system time of last j_waitjed job */
2376*7c356e86SAndroid Build Coastguard Worker EXTERN struct timeval j_usrtime, j_systime;
2377*7c356e86SAndroid Build Coastguard Worker
2378*7c356e86SAndroid Build Coastguard Worker #define notok2mul(max,val,c) (((val) != 0) && ((c) != 0) && \
2379*7c356e86SAndroid Build Coastguard Worker (((max) / (c)) < (val)))
2380*7c356e86SAndroid Build Coastguard Worker #define notok2add(max,val,c) ((val) > ((max) - (c)))
2381*7c356e86SAndroid Build Coastguard Worker #define notoktomul(val,cnst) notok2mul(SIZE_MAX, (val), (cnst))
2382*7c356e86SAndroid Build Coastguard Worker #define notoktoadd(val,cnst) notok2add(SIZE_MAX, (val), (cnst))
2383*7c356e86SAndroid Build Coastguard Worker #define checkoktoadd(val,cnst) do { \
2384*7c356e86SAndroid Build Coastguard Worker if (notoktoadd((val), (cnst))) \
2385*7c356e86SAndroid Build Coastguard Worker internal_errorf(Tintovfl, (size_t)(val), \
2386*7c356e86SAndroid Build Coastguard Worker '+', (size_t)(cnst)); \
2387*7c356e86SAndroid Build Coastguard Worker } while (/* CONSTCOND */ 0)
2388*7c356e86SAndroid Build Coastguard Worker
2389*7c356e86SAndroid Build Coastguard Worker
2390*7c356e86SAndroid Build Coastguard Worker /* lalloc.c */
2391*7c356e86SAndroid Build Coastguard Worker void ainit(Area *);
2392*7c356e86SAndroid Build Coastguard Worker void afreeall(Area *);
2393*7c356e86SAndroid Build Coastguard Worker /* these cannot fail and can take NULL (not for ap) */
2394*7c356e86SAndroid Build Coastguard Worker #define alloc(n,ap) aresize(NULL, (n), (ap))
2395*7c356e86SAndroid Build Coastguard Worker #define alloc2(m,n,ap) aresize2(NULL, (m), (n), (ap))
2396*7c356e86SAndroid Build Coastguard Worker void *aresize(void *, size_t, Area *);
2397*7c356e86SAndroid Build Coastguard Worker void *aresize2(void *, size_t, size_t, Area *);
2398*7c356e86SAndroid Build Coastguard Worker void afree(void *, Area *); /* can take NULL */
2399*7c356e86SAndroid Build Coastguard Worker #define aresizeif(z,p,n,ap) (((p) == NULL) || ((z) < (n)) || \
2400*7c356e86SAndroid Build Coastguard Worker (((z) & ~X_WASTE) > ((n) & ~X_WASTE)) ? \
2401*7c356e86SAndroid Build Coastguard Worker aresize((p), (n), (ap)) : (p))
2402*7c356e86SAndroid Build Coastguard Worker /* edit.c */
2403*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NO_CMDLINE_EDITING
2404*7c356e86SAndroid Build Coastguard Worker int x_bind(const char * SMALLP(bool));
2405*7c356e86SAndroid Build Coastguard Worker int x_bind_check(void);
2406*7c356e86SAndroid Build Coastguard Worker int x_bind_list(void);
2407*7c356e86SAndroid Build Coastguard Worker int x_bind_showall(void);
2408*7c356e86SAndroid Build Coastguard Worker void x_init(void);
2409*7c356e86SAndroid Build Coastguard Worker #ifdef DEBUG_LEAKS
2410*7c356e86SAndroid Build Coastguard Worker void x_done(void);
2411*7c356e86SAndroid Build Coastguard Worker #endif
2412*7c356e86SAndroid Build Coastguard Worker int x_read(char *);
2413*7c356e86SAndroid Build Coastguard Worker #endif
2414*7c356e86SAndroid Build Coastguard Worker void x_mkraw(int, mksh_ttyst *, bool);
2415*7c356e86SAndroid Build Coastguard Worker void x_initterm(const char *);
2416*7c356e86SAndroid Build Coastguard Worker /* eval.c */
2417*7c356e86SAndroid Build Coastguard Worker char *substitute(const char *, int);
2418*7c356e86SAndroid Build Coastguard Worker char **eval(const char **, int);
2419*7c356e86SAndroid Build Coastguard Worker char *evalstr(const char *cp, int);
2420*7c356e86SAndroid Build Coastguard Worker char *evalonestr(const char *cp, int);
2421*7c356e86SAndroid Build Coastguard Worker char *debunk(char *, const char *, size_t);
2422*7c356e86SAndroid Build Coastguard Worker void expand(const char *, XPtrV *, int);
2423*7c356e86SAndroid Build Coastguard Worker int glob_str(char *, XPtrV *, bool);
2424*7c356e86SAndroid Build Coastguard Worker char *do_tilde(char *);
2425*7c356e86SAndroid Build Coastguard Worker /* exec.c */
2426*7c356e86SAndroid Build Coastguard Worker int execute(struct op * volatile, volatile int, volatile int * volatile);
2427*7c356e86SAndroid Build Coastguard Worker int c_builtin(const char **);
2428*7c356e86SAndroid Build Coastguard Worker struct tbl *get_builtin(const char *);
2429*7c356e86SAndroid Build Coastguard Worker struct tbl *findfunc(const char *, uint32_t, bool);
2430*7c356e86SAndroid Build Coastguard Worker int define(const char *, struct op *);
2431*7c356e86SAndroid Build Coastguard Worker const char *builtin(const char *, int (*)(const char **));
2432*7c356e86SAndroid Build Coastguard Worker struct tbl *findcom(const char *, int);
2433*7c356e86SAndroid Build Coastguard Worker void flushcom(bool);
2434*7c356e86SAndroid Build Coastguard Worker int search_access(const char *, int);
2435*7c356e86SAndroid Build Coastguard Worker const char *search_path(const char *, const char *, int, int *);
2436*7c356e86SAndroid Build Coastguard Worker void pr_menu(const char * const *);
2437*7c356e86SAndroid Build Coastguard Worker void pr_list(struct columnise_opts *, char * const *);
2438*7c356e86SAndroid Build Coastguard Worker int herein(struct ioword *, char **);
2439*7c356e86SAndroid Build Coastguard Worker /* expr.c */
2440*7c356e86SAndroid Build Coastguard Worker int evaluate(const char *, mksh_ari_t *, int, bool);
2441*7c356e86SAndroid Build Coastguard Worker int v_evaluate(struct tbl *, const char *, volatile int, bool);
2442*7c356e86SAndroid Build Coastguard Worker /* UTF-8 stuff */
2443*7c356e86SAndroid Build Coastguard Worker size_t utf_mbtowc(unsigned int *, const char *);
2444*7c356e86SAndroid Build Coastguard Worker size_t utf_wctomb(char *, unsigned int);
2445*7c356e86SAndroid Build Coastguard Worker int utf_widthadj(const char *, const char **);
2446*7c356e86SAndroid Build Coastguard Worker size_t utf_mbswidth(const char *) MKSH_A_PURE;
2447*7c356e86SAndroid Build Coastguard Worker const char *utf_skipcols(const char *, int, int *);
2448*7c356e86SAndroid Build Coastguard Worker size_t utf_ptradj(const char *) MKSH_A_PURE;
2449*7c356e86SAndroid Build Coastguard Worker #ifdef MIRBSD_BOOTFLOPPY
2450*7c356e86SAndroid Build Coastguard Worker #define utf_wcwidth(i) wcwidth((wchar_t)(i))
2451*7c356e86SAndroid Build Coastguard Worker #else
2452*7c356e86SAndroid Build Coastguard Worker int utf_wcwidth(unsigned int) MKSH_A_PURE;
2453*7c356e86SAndroid Build Coastguard Worker #endif
2454*7c356e86SAndroid Build Coastguard Worker int ksh_access(const char *, int);
2455*7c356e86SAndroid Build Coastguard Worker struct tbl *tempvar(const char *);
2456*7c356e86SAndroid Build Coastguard Worker /* funcs.c */
2457*7c356e86SAndroid Build Coastguard Worker int c_hash(const char **);
2458*7c356e86SAndroid Build Coastguard Worker int c_pwd(const char **);
2459*7c356e86SAndroid Build Coastguard Worker int c_print(const char **);
2460*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_PRINTF_BUILTIN
2461*7c356e86SAndroid Build Coastguard Worker int c_printf(const char **);
2462*7c356e86SAndroid Build Coastguard Worker #endif
2463*7c356e86SAndroid Build Coastguard Worker int c_whence(const char **);
2464*7c356e86SAndroid Build Coastguard Worker int c_command(const char **);
2465*7c356e86SAndroid Build Coastguard Worker int c_typeset(const char **);
2466*7c356e86SAndroid Build Coastguard Worker bool valid_alias_name(const char *);
2467*7c356e86SAndroid Build Coastguard Worker int c_alias(const char **);
2468*7c356e86SAndroid Build Coastguard Worker int c_unalias(const char **);
2469*7c356e86SAndroid Build Coastguard Worker int c_let(const char **);
2470*7c356e86SAndroid Build Coastguard Worker int c_jobs(const char **);
2471*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_UNEMPLOYED
2472*7c356e86SAndroid Build Coastguard Worker int c_fgbg(const char **);
2473*7c356e86SAndroid Build Coastguard Worker #endif
2474*7c356e86SAndroid Build Coastguard Worker int c_kill(const char **);
2475*7c356e86SAndroid Build Coastguard Worker void getopts_reset(int);
2476*7c356e86SAndroid Build Coastguard Worker int c_getopts(const char **);
2477*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_NO_CMDLINE_EDITING
2478*7c356e86SAndroid Build Coastguard Worker int c_bind(const char **);
2479*7c356e86SAndroid Build Coastguard Worker #endif
2480*7c356e86SAndroid Build Coastguard Worker int c_shift(const char **);
2481*7c356e86SAndroid Build Coastguard Worker int c_umask(const char **);
2482*7c356e86SAndroid Build Coastguard Worker int c_dot(const char **);
2483*7c356e86SAndroid Build Coastguard Worker int c_wait(const char **);
2484*7c356e86SAndroid Build Coastguard Worker int c_read(const char **);
2485*7c356e86SAndroid Build Coastguard Worker int c_eval(const char **);
2486*7c356e86SAndroid Build Coastguard Worker int c_trap(const char **);
2487*7c356e86SAndroid Build Coastguard Worker int c_brkcont(const char **);
2488*7c356e86SAndroid Build Coastguard Worker int c_exitreturn(const char **);
2489*7c356e86SAndroid Build Coastguard Worker int c_set(const char **);
2490*7c356e86SAndroid Build Coastguard Worker int c_unset(const char **);
2491*7c356e86SAndroid Build Coastguard Worker int c_ulimit(const char **);
2492*7c356e86SAndroid Build Coastguard Worker int c_times(const char **);
2493*7c356e86SAndroid Build Coastguard Worker int timex(struct op *, int, volatile int *);
2494*7c356e86SAndroid Build Coastguard Worker void timex_hook(struct op *, char ** volatile *);
2495*7c356e86SAndroid Build Coastguard Worker int c_exec(const char **);
2496*7c356e86SAndroid Build Coastguard Worker int c_test(const char **);
2497*7c356e86SAndroid Build Coastguard Worker #if HAVE_MKNOD
2498*7c356e86SAndroid Build Coastguard Worker int c_mknod(const char **);
2499*7c356e86SAndroid Build Coastguard Worker #endif
2500*7c356e86SAndroid Build Coastguard Worker int c_realpath(const char **);
2501*7c356e86SAndroid Build Coastguard Worker int c_rename(const char **);
2502*7c356e86SAndroid Build Coastguard Worker int c_cat(const char **);
2503*7c356e86SAndroid Build Coastguard Worker int c_sleep(const char **);
2504*7c356e86SAndroid Build Coastguard Worker /* histrap.c */
2505*7c356e86SAndroid Build Coastguard Worker void init_histvec(void);
2506*7c356e86SAndroid Build Coastguard Worker void hist_init(Source *);
2507*7c356e86SAndroid Build Coastguard Worker #if HAVE_PERSISTENT_HISTORY
2508*7c356e86SAndroid Build Coastguard Worker void hist_finish(void);
2509*7c356e86SAndroid Build Coastguard Worker #endif
2510*7c356e86SAndroid Build Coastguard Worker void histsave(int *, const char *, int, bool);
2511*7c356e86SAndroid Build Coastguard Worker #if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
2512*7c356e86SAndroid Build Coastguard Worker bool histsync(void);
2513*7c356e86SAndroid Build Coastguard Worker #endif
2514*7c356e86SAndroid Build Coastguard Worker int c_fc(const char **);
2515*7c356e86SAndroid Build Coastguard Worker void sethistsize(mksh_ari_t);
2516*7c356e86SAndroid Build Coastguard Worker #if HAVE_PERSISTENT_HISTORY
2517*7c356e86SAndroid Build Coastguard Worker void sethistfile(const char *);
2518*7c356e86SAndroid Build Coastguard Worker #endif
2519*7c356e86SAndroid Build Coastguard Worker #if !defined(MKSH_NO_CMDLINE_EDITING) && !MKSH_S_NOVI
2520*7c356e86SAndroid Build Coastguard Worker char **histpos(void) MKSH_A_PURE;
2521*7c356e86SAndroid Build Coastguard Worker int histnum(int);
2522*7c356e86SAndroid Build Coastguard Worker #endif
2523*7c356e86SAndroid Build Coastguard Worker int findhist(int, const char *, bool, bool) MKSH_A_PURE;
2524*7c356e86SAndroid Build Coastguard Worker char **hist_get_newest(bool);
2525*7c356e86SAndroid Build Coastguard Worker void inittraps(void);
2526*7c356e86SAndroid Build Coastguard Worker void alarm_init(void);
2527*7c356e86SAndroid Build Coastguard Worker Trap *gettrap(const char *, bool, bool);
2528*7c356e86SAndroid Build Coastguard Worker void trapsig(int);
2529*7c356e86SAndroid Build Coastguard Worker void intrcheck(void);
2530*7c356e86SAndroid Build Coastguard Worker int fatal_trap_check(void);
2531*7c356e86SAndroid Build Coastguard Worker int trap_pending(void);
2532*7c356e86SAndroid Build Coastguard Worker void runtraps(int intr);
2533*7c356e86SAndroid Build Coastguard Worker void runtrap(Trap *, bool);
2534*7c356e86SAndroid Build Coastguard Worker void cleartraps(void);
2535*7c356e86SAndroid Build Coastguard Worker void restoresigs(void);
2536*7c356e86SAndroid Build Coastguard Worker void settrap(Trap *, const char *);
2537*7c356e86SAndroid Build Coastguard Worker bool block_pipe(void);
2538*7c356e86SAndroid Build Coastguard Worker void restore_pipe(void);
2539*7c356e86SAndroid Build Coastguard Worker int setsig(Trap *, sig_t, int);
2540*7c356e86SAndroid Build Coastguard Worker void setexecsig(Trap *, int);
2541*7c356e86SAndroid Build Coastguard Worker #if HAVE_FLOCK || HAVE_LOCK_FCNTL
2542*7c356e86SAndroid Build Coastguard Worker void mksh_lockfd(int);
2543*7c356e86SAndroid Build Coastguard Worker void mksh_unlkfd(int);
2544*7c356e86SAndroid Build Coastguard Worker #endif
2545*7c356e86SAndroid Build Coastguard Worker /* jobs.c */
2546*7c356e86SAndroid Build Coastguard Worker void j_init(void);
2547*7c356e86SAndroid Build Coastguard Worker void j_exit(void);
2548*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_UNEMPLOYED
2549*7c356e86SAndroid Build Coastguard Worker void j_change(void);
2550*7c356e86SAndroid Build Coastguard Worker #endif
2551*7c356e86SAndroid Build Coastguard Worker int exchild(struct op *, int, volatile int *, int);
2552*7c356e86SAndroid Build Coastguard Worker void startlast(void);
2553*7c356e86SAndroid Build Coastguard Worker int waitlast(void);
2554*7c356e86SAndroid Build Coastguard Worker int waitfor(const char *, int *);
2555*7c356e86SAndroid Build Coastguard Worker int j_kill(const char *, int);
2556*7c356e86SAndroid Build Coastguard Worker #ifndef MKSH_UNEMPLOYED
2557*7c356e86SAndroid Build Coastguard Worker int j_resume(const char *, int);
2558*7c356e86SAndroid Build Coastguard Worker #endif
2559*7c356e86SAndroid Build Coastguard Worker #if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
2560*7c356e86SAndroid Build Coastguard Worker void j_suspend(void);
2561*7c356e86SAndroid Build Coastguard Worker #endif
2562*7c356e86SAndroid Build Coastguard Worker int j_jobs(const char *, int, int);
2563*7c356e86SAndroid Build Coastguard Worker void j_notify(void);
2564*7c356e86SAndroid Build Coastguard Worker pid_t j_async(void);
2565*7c356e86SAndroid Build Coastguard Worker int j_stopped_running(void);
2566*7c356e86SAndroid Build Coastguard Worker /* lex.c */
2567*7c356e86SAndroid Build Coastguard Worker int yylex(int);
2568*7c356e86SAndroid Build Coastguard Worker void yyskiputf8bom(void);
2569*7c356e86SAndroid Build Coastguard Worker void yyerror(const char *, ...)
2570*7c356e86SAndroid Build Coastguard Worker MKSH_A_NORETURN
2571*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2572*7c356e86SAndroid Build Coastguard Worker Source *pushs(int, Area *);
2573*7c356e86SAndroid Build Coastguard Worker void set_prompt(int, Source *);
2574*7c356e86SAndroid Build Coastguard Worker int pprompt(const char *, int);
2575*7c356e86SAndroid Build Coastguard Worker /* main.c */
2576*7c356e86SAndroid Build Coastguard Worker int include(const char *, int, const char **, bool);
2577*7c356e86SAndroid Build Coastguard Worker int command(const char *, int);
2578*7c356e86SAndroid Build Coastguard Worker int shell(Source * volatile, volatile int);
2579*7c356e86SAndroid Build Coastguard Worker /* argument MUST NOT be 0 */
2580*7c356e86SAndroid Build Coastguard Worker void unwind(int) MKSH_A_NORETURN;
2581*7c356e86SAndroid Build Coastguard Worker void newenv(int);
2582*7c356e86SAndroid Build Coastguard Worker void quitenv(struct shf *);
2583*7c356e86SAndroid Build Coastguard Worker void cleanup_parents_env(void);
2584*7c356e86SAndroid Build Coastguard Worker void cleanup_proc_env(void);
2585*7c356e86SAndroid Build Coastguard Worker void errorf(const char *, ...)
2586*7c356e86SAndroid Build Coastguard Worker MKSH_A_NORETURN
2587*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2588*7c356e86SAndroid Build Coastguard Worker void errorfx(int, const char *, ...)
2589*7c356e86SAndroid Build Coastguard Worker MKSH_A_NORETURN
2590*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 2, 3);
2591*7c356e86SAndroid Build Coastguard Worker void warningf(bool, const char *, ...)
2592*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 2, 3);
2593*7c356e86SAndroid Build Coastguard Worker void bi_errorf(const char *, ...)
2594*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2595*7c356e86SAndroid Build Coastguard Worker void maybe_errorf(int *, int, const char *, ...)
2596*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 3, 4);
2597*7c356e86SAndroid Build Coastguard Worker #define errorfz() errorf(NULL)
2598*7c356e86SAndroid Build Coastguard Worker #define errorfxz(rc) errorfx((rc), NULL)
2599*7c356e86SAndroid Build Coastguard Worker #define bi_errorfz() bi_errorf(NULL)
2600*7c356e86SAndroid Build Coastguard Worker void internal_errorf(const char *, ...)
2601*7c356e86SAndroid Build Coastguard Worker MKSH_A_NORETURN
2602*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2603*7c356e86SAndroid Build Coastguard Worker void internal_warningf(const char *, ...)
2604*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2605*7c356e86SAndroid Build Coastguard Worker void error_prefix(bool);
2606*7c356e86SAndroid Build Coastguard Worker void shellf(const char *, ...)
2607*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2608*7c356e86SAndroid Build Coastguard Worker void shprintf(const char *, ...)
2609*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2610*7c356e86SAndroid Build Coastguard Worker int can_seek(int);
2611*7c356e86SAndroid Build Coastguard Worker void initio(void);
2612*7c356e86SAndroid Build Coastguard Worker void recheck_ctype(void);
2613*7c356e86SAndroid Build Coastguard Worker int ksh_dup2(int, int, bool);
2614*7c356e86SAndroid Build Coastguard Worker short savefd(int);
2615*7c356e86SAndroid Build Coastguard Worker void restfd(int, int);
2616*7c356e86SAndroid Build Coastguard Worker void openpipe(int *);
2617*7c356e86SAndroid Build Coastguard Worker void closepipe(int *);
2618*7c356e86SAndroid Build Coastguard Worker int check_fd(const char *, int, const char **);
2619*7c356e86SAndroid Build Coastguard Worker void coproc_init(void);
2620*7c356e86SAndroid Build Coastguard Worker void coproc_read_close(int);
2621*7c356e86SAndroid Build Coastguard Worker void coproc_readw_close(int);
2622*7c356e86SAndroid Build Coastguard Worker void coproc_write_close(int);
2623*7c356e86SAndroid Build Coastguard Worker int coproc_getfd(int, const char **);
2624*7c356e86SAndroid Build Coastguard Worker void coproc_cleanup(int);
2625*7c356e86SAndroid Build Coastguard Worker struct temp *maketemp(Area *, Temp_type, struct temp **);
2626*7c356e86SAndroid Build Coastguard Worker void ktinit(Area *, struct table *, uint8_t);
2627*7c356e86SAndroid Build Coastguard Worker struct tbl *ktscan(struct table *, const char *, uint32_t, struct tbl ***);
2628*7c356e86SAndroid Build Coastguard Worker /* table, name (key) to search for, hash(n) */
2629*7c356e86SAndroid Build Coastguard Worker #define ktsearch(tp,s,h) ktscan((tp), (s), (h), NULL)
2630*7c356e86SAndroid Build Coastguard Worker struct tbl *ktenter(struct table *, const char *, uint32_t);
2631*7c356e86SAndroid Build Coastguard Worker #define ktdelete(p) do { p->flag = 0; } while (/* CONSTCOND */ 0)
2632*7c356e86SAndroid Build Coastguard Worker void ktwalk(struct tstate *, struct table *);
2633*7c356e86SAndroid Build Coastguard Worker struct tbl *ktnext(struct tstate *);
2634*7c356e86SAndroid Build Coastguard Worker struct tbl **ktsort(struct table *);
2635*7c356e86SAndroid Build Coastguard Worker #ifdef DF
2636*7c356e86SAndroid Build Coastguard Worker void DF(const char *, ...)
2637*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2638*7c356e86SAndroid Build Coastguard Worker #endif
2639*7c356e86SAndroid Build Coastguard Worker /* misc.c */
2640*7c356e86SAndroid Build Coastguard Worker size_t option(const char *) MKSH_A_PURE;
2641*7c356e86SAndroid Build Coastguard Worker char *getoptions(void);
2642*7c356e86SAndroid Build Coastguard Worker void change_flag(enum sh_flag, int, bool);
2643*7c356e86SAndroid Build Coastguard Worker void change_xtrace(unsigned char, bool);
2644*7c356e86SAndroid Build Coastguard Worker int parse_args(const char **, int, bool *);
2645*7c356e86SAndroid Build Coastguard Worker int getn(const char *, int *);
2646*7c356e86SAndroid Build Coastguard Worker int gmatchx(const char *, const char *, bool);
2647*7c356e86SAndroid Build Coastguard Worker bool has_globbing(const char *) MKSH_A_PURE;
2648*7c356e86SAndroid Build Coastguard Worker int ascstrcmp(const void *, const void *) MKSH_A_PURE;
2649*7c356e86SAndroid Build Coastguard Worker int ascpstrcmp(const void *, const void *) MKSH_A_PURE;
2650*7c356e86SAndroid Build Coastguard Worker void ksh_getopt_reset(Getopt *, int);
2651*7c356e86SAndroid Build Coastguard Worker int ksh_getopt(const char **, Getopt *, const char *);
2652*7c356e86SAndroid Build Coastguard Worker void print_value_quoted(struct shf *, const char *);
2653*7c356e86SAndroid Build Coastguard Worker char *quote_value(const char *);
2654*7c356e86SAndroid Build Coastguard Worker void print_columns(struct columnise_opts *, unsigned int,
2655*7c356e86SAndroid Build Coastguard Worker void (*)(char *, size_t, unsigned int, const void *),
2656*7c356e86SAndroid Build Coastguard Worker const void *, size_t, size_t);
2657*7c356e86SAndroid Build Coastguard Worker void strip_nuls(char *, size_t)
2658*7c356e86SAndroid Build Coastguard Worker MKSH_A_BOUNDED(__string__, 1, 2);
2659*7c356e86SAndroid Build Coastguard Worker ssize_t blocking_read(int, char *, size_t)
2660*7c356e86SAndroid Build Coastguard Worker MKSH_A_BOUNDED(__buffer__, 2, 3);
2661*7c356e86SAndroid Build Coastguard Worker int reset_nonblock(int);
2662*7c356e86SAndroid Build Coastguard Worker char *ksh_get_wd(void);
2663*7c356e86SAndroid Build Coastguard Worker char *do_realpath(const char *);
2664*7c356e86SAndroid Build Coastguard Worker void simplify_path(char *);
2665*7c356e86SAndroid Build Coastguard Worker void set_current_wd(const char *);
2666*7c356e86SAndroid Build Coastguard Worker int c_cd(const char **);
2667*7c356e86SAndroid Build Coastguard Worker #if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
2668*7c356e86SAndroid Build Coastguard Worker char *strdup_i(const char *, Area *);
2669*7c356e86SAndroid Build Coastguard Worker char *strndup_i(const char *, size_t, Area *);
2670*7c356e86SAndroid Build Coastguard Worker #endif
2671*7c356e86SAndroid Build Coastguard Worker int unbksl(bool, int (*)(void), void (*)(int));
2672*7c356e86SAndroid Build Coastguard Worker #ifdef __OS2__
2673*7c356e86SAndroid Build Coastguard Worker /* os2.c */
2674*7c356e86SAndroid Build Coastguard Worker void os2_init(int *, const char ***);
2675*7c356e86SAndroid Build Coastguard Worker void setextlibpath(const char *, const char *);
2676*7c356e86SAndroid Build Coastguard Worker int access_ex(int (*)(const char *, int), const char *, int);
2677*7c356e86SAndroid Build Coastguard Worker int stat_ex(int (*)(const char *, struct stat *), const char *, struct stat *);
2678*7c356e86SAndroid Build Coastguard Worker const char *real_exec_name(const char *);
2679*7c356e86SAndroid Build Coastguard Worker #endif
2680*7c356e86SAndroid Build Coastguard Worker /* shf.c */
2681*7c356e86SAndroid Build Coastguard Worker struct shf *shf_open(const char *, int, int, int);
2682*7c356e86SAndroid Build Coastguard Worker struct shf *shf_fdopen(int, int, struct shf *);
2683*7c356e86SAndroid Build Coastguard Worker struct shf *shf_reopen(int, int, struct shf *);
2684*7c356e86SAndroid Build Coastguard Worker struct shf *shf_sopen(char *, ssize_t, int, struct shf *);
2685*7c356e86SAndroid Build Coastguard Worker int shf_close(struct shf *);
2686*7c356e86SAndroid Build Coastguard Worker int shf_fdclose(struct shf *);
2687*7c356e86SAndroid Build Coastguard Worker char *shf_sclose(struct shf *);
2688*7c356e86SAndroid Build Coastguard Worker int shf_flush(struct shf *);
2689*7c356e86SAndroid Build Coastguard Worker ssize_t shf_read(char *, ssize_t, struct shf *);
2690*7c356e86SAndroid Build Coastguard Worker char *shf_getse(char *, ssize_t, struct shf *);
2691*7c356e86SAndroid Build Coastguard Worker int shf_getchar(struct shf *s);
2692*7c356e86SAndroid Build Coastguard Worker int shf_ungetc(int, struct shf *);
2693*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_SHF_NO_INLINE
2694*7c356e86SAndroid Build Coastguard Worker int shf_getc(struct shf *);
2695*7c356e86SAndroid Build Coastguard Worker int shf_putc(int, struct shf *);
2696*7c356e86SAndroid Build Coastguard Worker #else
2697*7c356e86SAndroid Build Coastguard Worker #define shf_getc shf_getc_i
2698*7c356e86SAndroid Build Coastguard Worker #define shf_putc shf_putc_i
2699*7c356e86SAndroid Build Coastguard Worker #endif
2700*7c356e86SAndroid Build Coastguard Worker int shf_putchar(int, struct shf *);
2701*7c356e86SAndroid Build Coastguard Worker ssize_t shf_puts(const char *, struct shf *);
2702*7c356e86SAndroid Build Coastguard Worker ssize_t shf_write(const char *, ssize_t, struct shf *);
2703*7c356e86SAndroid Build Coastguard Worker ssize_t shf_fprintf(struct shf *, const char *, ...)
2704*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 2, 3);
2705*7c356e86SAndroid Build Coastguard Worker ssize_t shf_snprintf(char *, ssize_t, const char *, ...)
2706*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 3, 4)
2707*7c356e86SAndroid Build Coastguard Worker MKSH_A_BOUNDED(__string__, 1, 2);
2708*7c356e86SAndroid Build Coastguard Worker char *shf_smprintf(const char *, ...)
2709*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 1, 2);
2710*7c356e86SAndroid Build Coastguard Worker ssize_t shf_vfprintf(struct shf *, const char *, va_list)
2711*7c356e86SAndroid Build Coastguard Worker MKSH_A_FORMAT(__printf__, 2, 0);
2712*7c356e86SAndroid Build Coastguard Worker void set_ifs(const char *);
2713*7c356e86SAndroid Build Coastguard Worker /* syn.c */
2714*7c356e86SAndroid Build Coastguard Worker void initkeywords(void);
2715*7c356e86SAndroid Build Coastguard Worker struct op *compile(Source *, bool, bool);
2716*7c356e86SAndroid Build Coastguard Worker bool parse_usec(const char *, struct timeval *);
2717*7c356e86SAndroid Build Coastguard Worker char *yyrecursive(int);
2718*7c356e86SAndroid Build Coastguard Worker void yyrecursive_pop(bool);
2719*7c356e86SAndroid Build Coastguard Worker /* tree.c */
2720*7c356e86SAndroid Build Coastguard Worker void fptreef(struct shf *, int, const char *, ...);
2721*7c356e86SAndroid Build Coastguard Worker char *snptreef(char *, ssize_t, const char *, ...);
2722*7c356e86SAndroid Build Coastguard Worker struct op *tcopy(struct op *, Area *);
2723*7c356e86SAndroid Build Coastguard Worker char *wdcopy(const char *, Area *);
2724*7c356e86SAndroid Build Coastguard Worker const char *wdscan(const char *, int);
2725*7c356e86SAndroid Build Coastguard Worker #define WDS_TPUTS BIT(0) /* tputS (dumpwdvar) mode */
2726*7c356e86SAndroid Build Coastguard Worker char *wdstrip(const char *, int);
2727*7c356e86SAndroid Build Coastguard Worker void tfree(struct op *, Area *);
2728*7c356e86SAndroid Build Coastguard Worker void dumpchar(struct shf *, unsigned char);
2729*7c356e86SAndroid Build Coastguard Worker void dumptree(struct shf *, struct op *);
2730*7c356e86SAndroid Build Coastguard Worker void dumpwdvar(struct shf *, const char *);
2731*7c356e86SAndroid Build Coastguard Worker void dumpioact(struct shf *shf, struct op *t);
2732*7c356e86SAndroid Build Coastguard Worker void vistree(char *, size_t, struct op *)
2733*7c356e86SAndroid Build Coastguard Worker MKSH_A_BOUNDED(__string__, 1, 2);
2734*7c356e86SAndroid Build Coastguard Worker void fpFUNCTf(struct shf *, int, bool, const char *, struct op *);
2735*7c356e86SAndroid Build Coastguard Worker /* var.c */
2736*7c356e86SAndroid Build Coastguard Worker void newblock(void);
2737*7c356e86SAndroid Build Coastguard Worker void popblock(void);
2738*7c356e86SAndroid Build Coastguard Worker void initvar(void);
2739*7c356e86SAndroid Build Coastguard Worker struct block *varsearch(struct block *, struct tbl **, const char *, uint32_t);
2740*7c356e86SAndroid Build Coastguard Worker struct tbl *global(const char *);
2741*7c356e86SAndroid Build Coastguard Worker struct tbl *isglobal(const char *, bool);
2742*7c356e86SAndroid Build Coastguard Worker struct tbl *local(const char *, bool);
2743*7c356e86SAndroid Build Coastguard Worker char *str_val(struct tbl *);
2744*7c356e86SAndroid Build Coastguard Worker int setstr(struct tbl *, const char *, int);
2745*7c356e86SAndroid Build Coastguard Worker struct tbl *setint_v(struct tbl *, struct tbl *, bool);
2746*7c356e86SAndroid Build Coastguard Worker void setint(struct tbl *, mksh_ari_t);
2747*7c356e86SAndroid Build Coastguard Worker void setint_n(struct tbl *, mksh_ari_t, int);
2748*7c356e86SAndroid Build Coastguard Worker struct tbl *typeset(const char *, uint32_t, uint32_t, int, int);
2749*7c356e86SAndroid Build Coastguard Worker void unset(struct tbl *, int);
2750*7c356e86SAndroid Build Coastguard Worker const char *skip_varname(const char *, bool) MKSH_A_PURE;
2751*7c356e86SAndroid Build Coastguard Worker const char *skip_wdvarname(const char *, bool) MKSH_A_PURE;
2752*7c356e86SAndroid Build Coastguard Worker int is_wdvarname(const char *, bool) MKSH_A_PURE;
2753*7c356e86SAndroid Build Coastguard Worker int is_wdvarassign(const char *) MKSH_A_PURE;
2754*7c356e86SAndroid Build Coastguard Worker struct tbl *arraysearch(struct tbl *, uint32_t);
2755*7c356e86SAndroid Build Coastguard Worker char **makenv(void);
2756*7c356e86SAndroid Build Coastguard Worker void change_winsz(void);
2757*7c356e86SAndroid Build Coastguard Worker size_t array_ref_len(const char *) MKSH_A_PURE;
2758*7c356e86SAndroid Build Coastguard Worker char *arrayname(const char *);
2759*7c356e86SAndroid Build Coastguard Worker mksh_uari_t set_array(const char *, bool, const char **);
2760*7c356e86SAndroid Build Coastguard Worker uint32_t hash(const void *) MKSH_A_PURE;
2761*7c356e86SAndroid Build Coastguard Worker uint32_t chvt_rndsetup(const void *, size_t) MKSH_A_PURE;
2762*7c356e86SAndroid Build Coastguard Worker mksh_ari_t rndget(void);
2763*7c356e86SAndroid Build Coastguard Worker void rndset(unsigned long);
2764*7c356e86SAndroid Build Coastguard Worker void rndpush(const void *);
2765*7c356e86SAndroid Build Coastguard Worker void record_match(const char *);
2766*7c356e86SAndroid Build Coastguard Worker
2767*7c356e86SAndroid Build Coastguard Worker enum Test_op {
2768*7c356e86SAndroid Build Coastguard Worker /* non-operator */
2769*7c356e86SAndroid Build Coastguard Worker TO_NONOP = 0,
2770*7c356e86SAndroid Build Coastguard Worker /* unary operators */
2771*7c356e86SAndroid Build Coastguard Worker TO_STNZE, TO_STZER, TO_ISSET, TO_OPTION,
2772*7c356e86SAndroid Build Coastguard Worker TO_FILAXST,
2773*7c356e86SAndroid Build Coastguard Worker TO_FILEXST,
2774*7c356e86SAndroid Build Coastguard Worker TO_FILREG, TO_FILBDEV, TO_FILCDEV, TO_FILSYM, TO_FILFIFO, TO_FILSOCK,
2775*7c356e86SAndroid Build Coastguard Worker TO_FILCDF, TO_FILID, TO_FILGID, TO_FILSETG, TO_FILSTCK, TO_FILUID,
2776*7c356e86SAndroid Build Coastguard Worker TO_FILRD, TO_FILGZ, TO_FILTT, TO_FILSETU, TO_FILWR, TO_FILEX,
2777*7c356e86SAndroid Build Coastguard Worker /* binary operators */
2778*7c356e86SAndroid Build Coastguard Worker TO_STEQL, TO_STNEQ, TO_STLT, TO_STGT, TO_INTEQ, TO_INTNE, TO_INTGT,
2779*7c356e86SAndroid Build Coastguard Worker TO_INTGE, TO_INTLT, TO_INTLE, TO_FILEQ, TO_FILNT, TO_FILOT,
2780*7c356e86SAndroid Build Coastguard Worker /* not an operator */
2781*7c356e86SAndroid Build Coastguard Worker TO_NONNULL /* !TO_NONOP */
2782*7c356e86SAndroid Build Coastguard Worker };
2783*7c356e86SAndroid Build Coastguard Worker typedef enum Test_op Test_op;
2784*7c356e86SAndroid Build Coastguard Worker
2785*7c356e86SAndroid Build Coastguard Worker /* Used by Test_env.isa() (order important - used to index *_tokens[] arrays) */
2786*7c356e86SAndroid Build Coastguard Worker enum Test_meta {
2787*7c356e86SAndroid Build Coastguard Worker TM_OR, /* -o or || */
2788*7c356e86SAndroid Build Coastguard Worker TM_AND, /* -a or && */
2789*7c356e86SAndroid Build Coastguard Worker TM_NOT, /* ! */
2790*7c356e86SAndroid Build Coastguard Worker TM_OPAREN, /* ( */
2791*7c356e86SAndroid Build Coastguard Worker TM_CPAREN, /* ) */
2792*7c356e86SAndroid Build Coastguard Worker TM_UNOP, /* unary operator */
2793*7c356e86SAndroid Build Coastguard Worker TM_BINOP, /* binary operator */
2794*7c356e86SAndroid Build Coastguard Worker TM_END /* end of input */
2795*7c356e86SAndroid Build Coastguard Worker };
2796*7c356e86SAndroid Build Coastguard Worker typedef enum Test_meta Test_meta;
2797*7c356e86SAndroid Build Coastguard Worker
2798*7c356e86SAndroid Build Coastguard Worker struct t_op {
2799*7c356e86SAndroid Build Coastguard Worker const char op_text[4];
2800*7c356e86SAndroid Build Coastguard Worker Test_op op_num;
2801*7c356e86SAndroid Build Coastguard Worker };
2802*7c356e86SAndroid Build Coastguard Worker
2803*7c356e86SAndroid Build Coastguard Worker /* for string reuse */
2804*7c356e86SAndroid Build Coastguard Worker extern const struct t_op u_ops[];
2805*7c356e86SAndroid Build Coastguard Worker extern const struct t_op b_ops[];
2806*7c356e86SAndroid Build Coastguard Worker /* ensure order with funcs.c */
2807*7c356e86SAndroid Build Coastguard Worker #define Tda (u_ops[0].op_text)
2808*7c356e86SAndroid Build Coastguard Worker #define Tdn (u_ops[12].op_text)
2809*7c356e86SAndroid Build Coastguard Worker #define Tdo (u_ops[14].op_text)
2810*7c356e86SAndroid Build Coastguard Worker #define Tdr (u_ops[16].op_text)
2811*7c356e86SAndroid Build Coastguard Worker #define Tdu (u_ops[20].op_text) /* "-u" */
2812*7c356e86SAndroid Build Coastguard Worker #define Tdx (u_ops[23].op_text)
2813*7c356e86SAndroid Build Coastguard Worker
2814*7c356e86SAndroid Build Coastguard Worker #define Tu (Tdu + 1) /* "u" */
2815*7c356e86SAndroid Build Coastguard Worker
2816*7c356e86SAndroid Build Coastguard Worker #define TEF_ERROR BIT(0) /* set if we've hit an error */
2817*7c356e86SAndroid Build Coastguard Worker #define TEF_DBRACKET BIT(1) /* set if [[ .. ]] test */
2818*7c356e86SAndroid Build Coastguard Worker
2819*7c356e86SAndroid Build Coastguard Worker typedef struct test_env {
2820*7c356e86SAndroid Build Coastguard Worker union {
2821*7c356e86SAndroid Build Coastguard Worker const char **wp; /* used by ptest_* */
2822*7c356e86SAndroid Build Coastguard Worker XPtrV *av; /* used by dbtestp_* */
2823*7c356e86SAndroid Build Coastguard Worker } pos;
2824*7c356e86SAndroid Build Coastguard Worker const char **wp_end; /* used by ptest_* */
2825*7c356e86SAndroid Build Coastguard Worker Test_op (*isa)(struct test_env *, Test_meta);
2826*7c356e86SAndroid Build Coastguard Worker const char *(*getopnd) (struct test_env *, Test_op, bool);
2827*7c356e86SAndroid Build Coastguard Worker int (*eval)(struct test_env *, Test_op, const char *, const char *, bool);
2828*7c356e86SAndroid Build Coastguard Worker void (*error)(struct test_env *, int, const char *);
2829*7c356e86SAndroid Build Coastguard Worker int flags; /* TEF_* */
2830*7c356e86SAndroid Build Coastguard Worker } Test_env;
2831*7c356e86SAndroid Build Coastguard Worker
2832*7c356e86SAndroid Build Coastguard Worker extern const char * const dbtest_tokens[];
2833*7c356e86SAndroid Build Coastguard Worker
2834*7c356e86SAndroid Build Coastguard Worker Test_op test_isop(Test_meta, const char *) MKSH_A_PURE;
2835*7c356e86SAndroid Build Coastguard Worker int test_eval(Test_env *, Test_op, const char *, const char *, bool);
2836*7c356e86SAndroid Build Coastguard Worker int test_parse(Test_env *);
2837*7c356e86SAndroid Build Coastguard Worker
2838*7c356e86SAndroid Build Coastguard Worker /* tty_fd is not opened O_BINARY, it's thus never read/written */
2839*7c356e86SAndroid Build Coastguard Worker EXTERN int tty_fd E_INIT(-1); /* dup'd tty file descriptor */
2840*7c356e86SAndroid Build Coastguard Worker EXTERN bool tty_devtty; /* true if tty_fd is from /dev/tty */
2841*7c356e86SAndroid Build Coastguard Worker EXTERN mksh_ttyst tty_state; /* saved tty state */
2842*7c356e86SAndroid Build Coastguard Worker EXTERN bool tty_hasstate; /* true if tty_state is valid */
2843*7c356e86SAndroid Build Coastguard Worker
2844*7c356e86SAndroid Build Coastguard Worker extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */
2845*7c356e86SAndroid Build Coastguard Worker
2846*7c356e86SAndroid Build Coastguard Worker #ifdef __OS2__
2847*7c356e86SAndroid Build Coastguard Worker #define binopen2(path,flags) __extension__({ \
2848*7c356e86SAndroid Build Coastguard Worker int binopen2_fd = open((path), (flags) | O_BINARY); \
2849*7c356e86SAndroid Build Coastguard Worker if (binopen2_fd >= 0) \
2850*7c356e86SAndroid Build Coastguard Worker setmode(binopen2_fd, O_BINARY); \
2851*7c356e86SAndroid Build Coastguard Worker (binopen2_fd); \
2852*7c356e86SAndroid Build Coastguard Worker })
2853*7c356e86SAndroid Build Coastguard Worker #define binopen3(path,flags,mode) __extension__({ \
2854*7c356e86SAndroid Build Coastguard Worker int binopen3_fd = open((path), (flags) | O_BINARY, (mode)); \
2855*7c356e86SAndroid Build Coastguard Worker if (binopen3_fd >= 0) \
2856*7c356e86SAndroid Build Coastguard Worker setmode(binopen3_fd, O_BINARY); \
2857*7c356e86SAndroid Build Coastguard Worker (binopen3_fd); \
2858*7c356e86SAndroid Build Coastguard Worker })
2859*7c356e86SAndroid Build Coastguard Worker #else
2860*7c356e86SAndroid Build Coastguard Worker #define binopen2(path,flags) open((path), (flags) | O_BINARY)
2861*7c356e86SAndroid Build Coastguard Worker #define binopen3(path,flags,mode) open((path), (flags) | O_BINARY, (mode))
2862*7c356e86SAndroid Build Coastguard Worker #endif
2863*7c356e86SAndroid Build Coastguard Worker
2864*7c356e86SAndroid Build Coastguard Worker #ifdef MKSH_DOSPATH
2865*7c356e86SAndroid Build Coastguard Worker #define mksh_drvltr(s) __extension__({ \
2866*7c356e86SAndroid Build Coastguard Worker const char *mksh_drvltr_s = (s); \
2867*7c356e86SAndroid Build Coastguard Worker (ctype(mksh_drvltr_s[0], C_ALPHA) && mksh_drvltr_s[1] == ':'); \
2868*7c356e86SAndroid Build Coastguard Worker })
2869*7c356e86SAndroid Build Coastguard Worker #define mksh_abspath(s) __extension__({ \
2870*7c356e86SAndroid Build Coastguard Worker const char *mksh_abspath_s = (s); \
2871*7c356e86SAndroid Build Coastguard Worker (mksh_cdirsep(mksh_abspath_s[0]) || \
2872*7c356e86SAndroid Build Coastguard Worker (mksh_drvltr(mksh_abspath_s) && \
2873*7c356e86SAndroid Build Coastguard Worker mksh_cdirsep(mksh_abspath_s[2]))); \
2874*7c356e86SAndroid Build Coastguard Worker })
2875*7c356e86SAndroid Build Coastguard Worker #define mksh_cdirsep(c) __extension__({ \
2876*7c356e86SAndroid Build Coastguard Worker char mksh_cdirsep_c = (c); \
2877*7c356e86SAndroid Build Coastguard Worker (mksh_cdirsep_c == '/' || mksh_cdirsep_c == '\\'); \
2878*7c356e86SAndroid Build Coastguard Worker })
2879*7c356e86SAndroid Build Coastguard Worker #define mksh_sdirsep(s) strpbrk((s), "/\\")
2880*7c356e86SAndroid Build Coastguard Worker #define mksh_vdirsep(s) __extension__({ \
2881*7c356e86SAndroid Build Coastguard Worker const char *mksh_vdirsep_s = (s); \
2882*7c356e86SAndroid Build Coastguard Worker (((mksh_drvltr(mksh_vdirsep_s) && \
2883*7c356e86SAndroid Build Coastguard Worker !mksh_cdirsep(mksh_vdirsep_s[2])) ? (!0) : \
2884*7c356e86SAndroid Build Coastguard Worker (mksh_sdirsep(mksh_vdirsep_s) != NULL)) && \
2885*7c356e86SAndroid Build Coastguard Worker (strcmp(mksh_vdirsep_s, T_builtin) != 0)); \
2886*7c356e86SAndroid Build Coastguard Worker })
2887*7c356e86SAndroid Build Coastguard Worker int getdrvwd(char **, unsigned int);
2888*7c356e86SAndroid Build Coastguard Worker #else
2889*7c356e86SAndroid Build Coastguard Worker #define mksh_abspath(s) (ord((s)[0]) == ORD('/'))
2890*7c356e86SAndroid Build Coastguard Worker #define mksh_cdirsep(c) (ord(c) == ORD('/'))
2891*7c356e86SAndroid Build Coastguard Worker #define mksh_sdirsep(s) strchr((s), '/')
2892*7c356e86SAndroid Build Coastguard Worker #define mksh_vdirsep(s) vstrchr((s), '/')
2893*7c356e86SAndroid Build Coastguard Worker #endif
2894*7c356e86SAndroid Build Coastguard Worker
2895*7c356e86SAndroid Build Coastguard Worker /* be sure not to interfere with anyone else's idea about EXTERN */
2896*7c356e86SAndroid Build Coastguard Worker #ifdef EXTERN_DEFINED
2897*7c356e86SAndroid Build Coastguard Worker # undef EXTERN_DEFINED
2898*7c356e86SAndroid Build Coastguard Worker # undef EXTERN
2899*7c356e86SAndroid Build Coastguard Worker #endif
2900*7c356e86SAndroid Build Coastguard Worker #undef E_INIT
2901*7c356e86SAndroid Build Coastguard Worker
2902*7c356e86SAndroid Build Coastguard Worker #endif /* !MKSH_INCLUDES_ONLY */
2903