blob: a974bbdc015a703720f66b2024f0fd722101d4ad [file] [log] [blame]
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001/* $OpenBSD: sh.h,v 1.35 2015/09/10 22:48:58 nicm Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002/* $OpenBSD: shf.h,v 1.6 2005/12/11 18:53:51 deraadt Exp $ */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00003/* $OpenBSD: table.h,v 1.8 2012/02/19 07:52:30 otto Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07004/* $OpenBSD: tree.h,v 1.10 2005/03/28 21:28:22 deraadt Exp $ */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08005/* $OpenBSD: expand.h,v 1.7 2015/09/01 13:12:31 tedu Exp $ */
Thorsten Glaser811a5752013-07-25 14:24:45 +00006/* $OpenBSD: lex.h,v 1.13 2013/03/03 19:11:34 guenther Exp $ */
Elliott Hughes737fdce2014-08-07 12:59:26 -07007/* $OpenBSD: proto.h,v 1.35 2013/09/04 15:49:19 millert Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07008/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
9/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
10
11/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000012 * Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Elliott Hughesdd4abe02018-02-05 15:55:19 -080013 * 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
Elliott Hughesfc0307d2016-02-02 15:26:47 -080014 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070015 *
16 * Provided that these terms and disclaimer and all copyright notices
17 * are retained or reproduced in an accompanying document, permission
Geremy Condra03ebf062011-10-12 18:17:24 -070018 * is granted to deal in this work without restriction, including un‐
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070019 * limited rights to use, publicly perform, distribute, sell, modify,
20 * merge, give away, or sublicence.
21 *
Geremy Condra03ebf062011-10-12 18:17:24 -070022 * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070023 * the utmost extent permitted by applicable law, neither express nor
24 * implied; without malicious intent or gross negligence. In no event
25 * may a licensor, author or contributor be held liable for indirect,
26 * direct, other damage, loss, or other issues arising in any way out
27 * of dealing in the work, even if advised of the possibility of such
28 * damage or existence of a defect, except proven that it results out
Geremy Condra03ebf062011-10-12 18:17:24 -070029 * of said person’s immediate fault when using the work as intended.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070030 */
31
32#ifdef __dietlibc__
33/* XXX imake style */
Geremy Condra03ebf062011-10-12 18:17:24 -070034#define _BSD_SOURCE /* live, BSD, live❣ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070035#endif
36
37#if HAVE_SYS_PARAM_H
38#include <sys/param.h>
39#endif
40#include <sys/types.h>
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000041#if HAVE_BOTH_TIME_H
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070042#include <sys/time.h>
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000043#include <time.h>
44#elif HAVE_SYS_TIME_H
45#include <sys/time.h>
46#elif HAVE_TIME_H
47#include <time.h>
48#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070049#include <sys/ioctl.h>
50#if HAVE_SYS_SYSMACROS_H
51#include <sys/sysmacros.h>
52#endif
53#if HAVE_SYS_MKDEV_H
54#include <sys/mkdev.h>
55#endif
56#if HAVE_SYS_MMAN_H
57#include <sys/mman.h>
58#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000059#if HAVE_SYS_RESOURCE_H
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070060#include <sys/resource.h>
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000061#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070062#include <sys/stat.h>
63#include <sys/wait.h>
64#include <dirent.h>
65#include <errno.h>
66#include <fcntl.h>
Elliott Hughes96b43632015-07-17 11:39:41 -070067#if HAVE_IO_H
68#include <io.h>
69#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070070#if HAVE_LIBGEN_H
71#include <libgen.h>
72#endif
73#if HAVE_LIBUTIL_H
74#include <libutil.h>
75#endif
76#include <limits.h>
77#if HAVE_PATHS_H
78#include <paths.h>
79#endif
80#include <pwd.h>
81#include <setjmp.h>
82#include <signal.h>
83#include <stdarg.h>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070084#include <stddef.h>
85#if HAVE_STDINT_H
86#include <stdint.h>
87#endif
88#include <stdio.h>
89#include <stdlib.h>
90#include <string.h>
91#if HAVE_STRINGS_H
92#include <strings.h>
93#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000094#if HAVE_TERMIOS_H
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070095#include <termios.h>
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000096#else
97/* shudder… */
98#include <termio.h>
99#endif
100#ifdef _ISC_UNIX
101/* XXX imake style */
102#include <sys/sioctl.h>
103#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700104#if HAVE_ULIMIT_H
105#include <ulimit.h>
106#endif
107#include <unistd.h>
108#if HAVE_VALUES_H
109#include <values.h>
110#endif
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800111#ifdef MIRBSD_BOOTFLOPPY
112#include <wchar.h>
113#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700114
Elliott Hughes23925bb2017-09-22 16:04:20 -0700115/* monkey-patch known-bad offsetof versions to quell a warning */
116#if (defined(__KLIBC__) || defined(__dietlibc__)) && \
117 ((defined(__GNUC__) && (__GNUC__ > 3)) || defined(__NWCC__))
118#undef offsetof
119#define offsetof(s, e) __builtin_offsetof(s, e)
120#endif
121
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700122#undef __attribute__
123#if HAVE_ATTRIBUTE_BOUNDED
Elliott Hughes737fdce2014-08-07 12:59:26 -0700124#define MKSH_A_BOUNDED(x,y,z) __attribute__((__bounded__(x, y, z)))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700125#else
126#define MKSH_A_BOUNDED(x,y,z) /* nothing */
127#endif
128#if HAVE_ATTRIBUTE_FORMAT
Elliott Hughes737fdce2014-08-07 12:59:26 -0700129#define MKSH_A_FORMAT(x,y,z) __attribute__((__format__(x, y, z)))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700130#else
131#define MKSH_A_FORMAT(x,y,z) /* nothing */
132#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700133#if HAVE_ATTRIBUTE_NORETURN
Geremy Condra03ebf062011-10-12 18:17:24 -0700134#define MKSH_A_NORETURN __attribute__((__noreturn__))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700135#else
136#define MKSH_A_NORETURN /* nothing */
137#endif
Elliott Hughes737fdce2014-08-07 12:59:26 -0700138#if HAVE_ATTRIBUTE_PURE
139#define MKSH_A_PURE __attribute__((__pure__))
140#else
141#define MKSH_A_PURE /* nothing */
142#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700143#if HAVE_ATTRIBUTE_UNUSED
Geremy Condra03ebf062011-10-12 18:17:24 -0700144#define MKSH_A_UNUSED __attribute__((__unused__))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700145#else
146#define MKSH_A_UNUSED /* nothing */
147#endif
148#if HAVE_ATTRIBUTE_USED
Geremy Condra03ebf062011-10-12 18:17:24 -0700149#define MKSH_A_USED __attribute__((__used__))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700150#else
151#define MKSH_A_USED /* nothing */
152#endif
153
154#if defined(MirBSD) && (MirBSD >= 0x09A1) && \
155 defined(__ELF__) && defined(__GNUC__) && \
156 !defined(__llvm__) && !defined(__NWCC__)
157/*
158 * We got usable __IDSTRING __COPYRIGHT __RCSID __SCCSID macros
159 * which work for all cases; no need to redefine them using the
160 * "portable" macros from below when we might have the "better"
161 * gcc+ELF specific macros or other system dependent ones.
162 */
163#else
164#undef __IDSTRING
165#undef __IDSTRING_CONCAT
166#undef __IDSTRING_EXPAND
167#undef __COPYRIGHT
168#undef __RCSID
169#undef __SCCSID
170#define __IDSTRING_CONCAT(l,p) __LINTED__ ## l ## _ ## p
171#define __IDSTRING_EXPAND(l,p) __IDSTRING_CONCAT(l,p)
Geremy Condra03ebf062011-10-12 18:17:24 -0700172#ifdef MKSH_DONT_EMIT_IDSTRING
173#define __IDSTRING(prefix, string) /* nothing */
174#else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700175#define __IDSTRING(prefix, string) \
176 static const char __IDSTRING_EXPAND(__LINE__,prefix) [] \
177 MKSH_A_USED = "@(""#)" #prefix ": " string
Geremy Condra03ebf062011-10-12 18:17:24 -0700178#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700179#define __COPYRIGHT(x) __IDSTRING(copyright,x)
180#define __RCSID(x) __IDSTRING(rcsid,x)
181#define __SCCSID(x) __IDSTRING(sccsid,x)
182#endif
183
184#ifdef EXTERN
Elliott Hughes47086262019-03-26 12:34:31 -0700185__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.870 2019/03/01 16:18:14 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700186#endif
Elliott Hughes47086262019-03-26 12:34:31 -0700187#define MKSH_VERSION "R57 2019/03/01"
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000188
189/* arithmetic types: C implementation */
190#if !HAVE_CAN_INTTYPES
191#if !HAVE_CAN_UCBINTS
192typedef signed int int32_t;
193typedef unsigned int uint32_t;
194#else
195typedef u_int32_t uint32_t;
196#endif
197#endif
198
199/* arithmetic types: shell arithmetics */
200#ifdef MKSH_LEGACY_MODE
201/*
202 * POSIX demands these to be the C environment's long type
203 */
204typedef long mksh_ari_t;
205typedef unsigned long mksh_uari_t;
206#else
207/*
208 * These types are exactly 32 bit wide; signed and unsigned
209 * integer wraparound, even across division and modulo, for
210 * any shell code using them, is guaranteed.
211 */
212typedef int32_t mksh_ari_t;
213typedef uint32_t mksh_uari_t;
214#endif
215
216/* boolean type (no <stdbool.h> deliberately) */
217typedef unsigned char mksh_bool;
218#undef bool
219/* false MUST equal the same 0 as written by static storage initialisation */
220#undef false
221#undef true
222/* access macros for boolean type */
223#define bool mksh_bool
224/* values must have identity mapping between mksh_bool and short */
225#define false 0
226#define true 1
227/* make any-type into bool or short */
228#define tobool(cond) ((cond) ? true : false)
229
230/* char (octet) type: C implementation */
231#if !HAVE_CAN_INT8TYPE
232#if !HAVE_CAN_UCBINT8
233typedef unsigned char uint8_t;
234#else
235typedef u_int8_t uint8_t;
236#endif
237#endif
238
239/* other standard types */
240
241#if !HAVE_RLIM_T
242typedef unsigned long rlim_t;
243#endif
244
245#if !HAVE_SIG_T
246#undef sig_t
247typedef void (*sig_t)(int);
248#endif
249
250#ifdef MKSH_TYPEDEF_SIG_ATOMIC_T
251typedef MKSH_TYPEDEF_SIG_ATOMIC_T sig_atomic_t;
252#endif
253
254#ifdef MKSH_TYPEDEF_SSIZE_T
255typedef MKSH_TYPEDEF_SSIZE_T ssize_t;
256#endif
257
258/* un-do vendor damage */
259
260#undef BAD /* AIX defines that somewhere */
261#undef PRINT /* LynxOS defines that somewhere */
262#undef flock /* SCO UnixWare defines that to flock64 but ENOENT */
263
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700264
265#ifndef MKSH_INCLUDES_ONLY
266
Elliott Hughes23925bb2017-09-22 16:04:20 -0700267/* EBCDIC fun */
268
269/* see the large comment in shf.c for an EBCDIC primer */
270
271#if defined(MKSH_FOR_Z_OS) && defined(__MVS__) && defined(__IBMC__) && defined(__CHARSET_LIB)
272# if !__CHARSET_LIB && !defined(MKSH_EBCDIC)
273# error "Please compile with Build.sh -E for EBCDIC!"
274# endif
275# if __CHARSET_LIB && defined(MKSH_EBCDIC)
276# error "Please compile without -E argument to Build.sh for ASCII!"
277# endif
278# if __CHARSET_LIB && !defined(_ENHANCED_ASCII_EXT)
279 /* go all-out on ASCII */
280# define _ENHANCED_ASCII_EXT 0xFFFFFFFF
281# endif
282#endif
283
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700284/* extra types */
285
Elliott Hughes96b43632015-07-17 11:39:41 -0700286/* getrusage does not exist on OS/2 kLIBC */
287#if !HAVE_GETRUSAGE && !defined(__OS2__)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700288#undef rusage
289#undef RUSAGE_SELF
290#undef RUSAGE_CHILDREN
291#define rusage mksh_rusage
Geremy Condra03ebf062011-10-12 18:17:24 -0700292#define RUSAGE_SELF 0
293#define RUSAGE_CHILDREN -1
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700294
295struct rusage {
296 struct timeval ru_utime;
297 struct timeval ru_stime;
298};
299#endif
300
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700301/* extra macros */
302
303#ifndef timerclear
304#define timerclear(tvp) \
305 do { \
306 (tvp)->tv_sec = (tvp)->tv_usec = 0; \
307 } while (/* CONSTCOND */ 0)
308#endif
309#ifndef timeradd
310#define timeradd(tvp, uvp, vvp) \
311 do { \
312 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
313 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
314 if ((vvp)->tv_usec >= 1000000) { \
315 (vvp)->tv_sec++; \
316 (vvp)->tv_usec -= 1000000; \
317 } \
318 } while (/* CONSTCOND */ 0)
319#endif
320#ifndef timersub
321#define timersub(tvp, uvp, vvp) \
322 do { \
323 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
324 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
325 if ((vvp)->tv_usec < 0) { \
326 (vvp)->tv_sec--; \
327 (vvp)->tv_usec += 1000000; \
328 } \
329 } while (/* CONSTCOND */ 0)
330#endif
331
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000332#ifdef MKSH__NO_PATH_MAX
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700333#undef PATH_MAX
334#else
335#ifndef PATH_MAX
Elliott Hughes50012062015-03-10 22:22:24 -0700336#ifdef MAXPATHLEN
337#define PATH_MAX MAXPATHLEN
338#else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700339#define PATH_MAX 1024
340#endif
341#endif
Elliott Hughes50012062015-03-10 22:22:24 -0700342#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700343#ifndef SIZE_MAX
344#ifdef SIZE_T_MAX
345#define SIZE_MAX SIZE_T_MAX
346#else
347#define SIZE_MAX ((size_t)-1)
348#endif
349#endif
350#ifndef S_ISLNK
351#define S_ISLNK(m) ((m & 0170000) == 0120000)
352#endif
353#ifndef S_ISSOCK
354#define S_ISSOCK(m) ((m & 0170000) == 0140000)
355#endif
Geremy Condra03ebf062011-10-12 18:17:24 -0700356#if !defined(S_ISCDF) && defined(S_CDF)
357#define S_ISCDF(m) (S_ISDIR(m) && ((m) & S_CDF))
358#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700359#ifndef DEFFILEMODE
360#define DEFFILEMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
361#endif
362
Elliott Hughes96b43632015-07-17 11:39:41 -0700363
364/* determine ksh_NSIG: first, use the traditional definitions */
365#undef ksh_NSIG
366#if defined(NSIG)
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800367#define ksh_NSIG (NSIG)
Elliott Hughes96b43632015-07-17 11:39:41 -0700368#elif defined(_NSIG)
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800369#define ksh_NSIG (_NSIG)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700370#elif defined(SIGMAX)
Elliott Hughes96b43632015-07-17 11:39:41 -0700371#define ksh_NSIG (SIGMAX + 1)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000372#elif defined(_SIGMAX)
Elliott Hughes96b43632015-07-17 11:39:41 -0700373#define ksh_NSIG (_SIGMAX + 1)
374#elif defined(NSIG_MAX)
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800375#define ksh_NSIG (NSIG_MAX)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700376#elif defined(MKSH_FOR_Z_OS)
377#define ksh_NSIG 40
Elliott Hughes737fdce2014-08-07 12:59:26 -0700378#else
379# error Please have your platform define NSIG.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700380#endif
Elliott Hughes96b43632015-07-17 11:39:41 -0700381/* range-check them */
382#if (ksh_NSIG < 1)
Elliott Hughes737fdce2014-08-07 12:59:26 -0700383# error Your NSIG value is not positive.
Elliott Hughes96b43632015-07-17 11:39:41 -0700384#undef ksh_NSIG
385#endif
386/* second, see if the new POSIX definition is available */
387#ifdef NSIG_MAX
388#if (NSIG_MAX < 2)
389/* and usable */
390# error Your NSIG_MAX value is too small.
391#undef NSIG_MAX
392#elif (ksh_NSIG > NSIG_MAX)
393/* and realistic */
394# error Your NSIG value is larger than your NSIG_MAX value.
395#undef NSIG_MAX
396#else
397/* since it’s usable, prefer it */
398#undef ksh_NSIG
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800399#define ksh_NSIG (NSIG_MAX)
Elliott Hughes96b43632015-07-17 11:39:41 -0700400#endif
401/* if NSIG_MAX is now still defined, use sysconf(_SC_NSIG) at runtime */
402#endif
403/* third, for cpp without the error directive, default */
404#ifndef ksh_NSIG
405#define ksh_NSIG 64
Elliott Hughes737fdce2014-08-07 12:59:26 -0700406#endif
407
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800408#define ksh_sigmask(sig) (((sig) < 1 || (sig) > 127) ? 255 : 128 + (sig))
409
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700410
411/* OS-dependent additions (functions, variables, by OS) */
412
Elliott Hughes96b43632015-07-17 11:39:41 -0700413#ifdef MKSH_EXE_EXT
414#undef MKSH_EXE_EXT
415#define MKSH_EXE_EXT ".exe"
416#else
417#define MKSH_EXE_EXT ""
418#endif
419
420#ifdef __OS2__
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700421#define MKSH_UNIXROOT "/@unixroot"
422#else
423#define MKSH_UNIXROOT ""
424#endif
425
426#ifdef MKSH_DOSPATH
427#ifndef __GNUC__
428# error GCC extensions needed later on
429#endif
Elliott Hughes96b43632015-07-17 11:39:41 -0700430#define MKSH_PATHSEPS ";"
431#define MKSH_PATHSEPC ';'
Elliott Hughes96b43632015-07-17 11:39:41 -0700432#else
433#define MKSH_PATHSEPS ":"
434#define MKSH_PATHSEPC ':'
Elliott Hughes96b43632015-07-17 11:39:41 -0700435#endif
436
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700437#if !HAVE_FLOCK_DECL
438extern int flock(int, int);
439#endif
440
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000441#if !HAVE_GETTIMEOFDAY
442#define mksh_TIME(tv) do { \
443 (tv).tv_usec = 0; \
444 (tv).tv_sec = time(NULL); \
445} while (/* CONSTCOND */ 0)
446#else
447#define mksh_TIME(tv) gettimeofday(&(tv), NULL)
448#endif
449
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700450#if !HAVE_GETRUSAGE
451extern int getrusage(int, struct rusage *);
452#endif
453
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000454#if !HAVE_MEMMOVE
455/* we assume either memmove or bcopy exist, at the moment */
456#define memmove(dst, src, len) bcopy((src), (dst), (len))
457#endif
458
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700459#if !HAVE_REVOKE_DECL
460extern int revoke(const char *);
461#endif
462
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000463#if defined(DEBUG) || !HAVE_STRERROR
Thorsten Glaser811a5752013-07-25 14:24:45 +0000464#undef strerror
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000465#define strerror /* poisoned */ dontuse_strerror
466#define cstrerror /* replaced */ cstrerror
467extern const char *cstrerror(int);
468#else
469#define cstrerror(errnum) ((const char *)strerror(errnum))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700470#endif
471
472#if !HAVE_STRLCPY
473size_t strlcpy(char *, const char *, size_t);
474#endif
475
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700476#ifdef __INTERIX
477/* XXX imake style */
478#define makedev mkdev
479extern int __cdecl seteuid(uid_t);
480extern int __cdecl setegid(gid_t);
481#endif
482
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000483#if defined(__COHERENT__)
484#ifndef O_ACCMODE
485/* this need not work everywhere, take care */
486#define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
487#endif
488#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700489
Elliott Hughes737fdce2014-08-07 12:59:26 -0700490#ifndef O_BINARY
491#define O_BINARY 0
492#endif
493
Elliott Hughes47086262019-03-26 12:34:31 -0700494#ifndef O_MAYEXEC
495#define O_MAYEXEC 0
496#endif
497
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000498#ifdef MKSH__NO_SYMLINK
499#undef S_ISLNK
500#define S_ISLNK(m) (/* CONSTCOND */ 0)
501#define mksh_lstat stat
502#else
503#define mksh_lstat lstat
504#endif
505
506#if HAVE_TERMIOS_H
507#define mksh_ttyst struct termios
508#define mksh_tcget(fd,st) tcgetattr((fd), (st))
509#define mksh_tcset(fd,st) tcsetattr((fd), TCSADRAIN, (st))
510#else
511#define mksh_ttyst struct termio
512#define mksh_tcget(fd,st) ioctl((fd), TCGETA, (st))
513#define mksh_tcset(fd,st) ioctl((fd), TCSETAW, (st))
514#endif
515
Elliott Hughes737fdce2014-08-07 12:59:26 -0700516#ifndef ISTRIP
517#define ISTRIP 0
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700518#endif
519
Elliott Hughes23925bb2017-09-22 16:04:20 -0700520#ifdef MKSH_EBCDIC
521#define KSH_BEL '\a'
522#define KSH_ESC 047
523#define KSH_ESC_STRING "\047"
524#define KSH_VTAB '\v'
525#else
526/*
527 * According to the comments in pdksh, \007 seems to be more portable
528 * than \a (HP-UX cc, Ultrix cc, old pcc, etc.) so we avoid the escape
529 * sequence if ASCII can be assumed.
530 */
531#define KSH_BEL 7
532#define KSH_ESC 033
533#define KSH_ESC_STRING "\033"
534#define KSH_VTAB 11
535#endif
536
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700537
538/* some useful #defines */
539#ifdef EXTERN
Geremy Condra03ebf062011-10-12 18:17:24 -0700540# define E_INIT(i) = i
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700541#else
Geremy Condra03ebf062011-10-12 18:17:24 -0700542# define E_INIT(i)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700543# define EXTERN extern
544# define EXTERN_DEFINED
545#endif
546
Geremy Condra03ebf062011-10-12 18:17:24 -0700547/* define bit in flag */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700548#define BIT(i) (1U << (i))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700549#define NELEM(a) (sizeof(a) / sizeof((a)[0]))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700550
Geremy Condra03ebf062011-10-12 18:17:24 -0700551/*
552 * Make MAGIC a char that might be printed to make bugs more obvious, but
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700553 * not a char that is used often. Also, can't use the high bit as it causes
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000554 * portability problems (calling strchr(x, 0x80 | 'x') is error prone).
Elliott Hughes23925bb2017-09-22 16:04:20 -0700555 *
556 * MAGIC can be followed by MAGIC (to escape the octet itself) or one of:
557 * ' !)*,-?[]{|}' 0x80|' !*+?@' (probably… hysteric raisins abound)
558 *
559 * The |0x80 is likely unsafe on EBCDIC :( though the listed chars are
560 * low-bit7 at least on cp1047 so YMMV
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700561 */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700562#define MAGIC KSH_BEL /* prefix for *?[!{,} during expand */
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800563#define ISMAGIC(c) (ord(c) == ORD(MAGIC))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700564
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700565EXTERN const char *safe_prompt; /* safe prompt if PS1 substitution fails */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000566
567#ifdef MKSH_LEGACY_MODE
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700568#define KSH_VERSIONNAME_ISLEGACY "LEGACY"
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000569#else
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700570#define KSH_VERSIONNAME_ISLEGACY "MIRBSD"
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000571#endif
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700572#ifdef MKSH_WITH_TEXTMODE
573#define KSH_VERSIONNAME_TEXTMODE " +TEXTMODE"
574#else
575#define KSH_VERSIONNAME_TEXTMODE ""
576#endif
Elliott Hughes23925bb2017-09-22 16:04:20 -0700577#ifdef MKSH_EBCDIC
578#define KSH_VERSIONNAME_EBCDIC " +EBCDIC"
579#else
580#define KSH_VERSIONNAME_EBCDIC ""
581#endif
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700582#ifndef KSH_VERSIONNAME_VENDOR_EXT
583#define KSH_VERSIONNAME_VENDOR_EXT ""
584#endif
585EXTERN const char initvsn[] E_INIT("KSH_VERSION=@(#)" KSH_VERSIONNAME_ISLEGACY \
Elliott Hughes23925bb2017-09-22 16:04:20 -0700586 " KSH " MKSH_VERSION KSH_VERSIONNAME_EBCDIC KSH_VERSIONNAME_TEXTMODE \
587 KSH_VERSIONNAME_VENDOR_EXT);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700588#define KSH_VERSION (initvsn + /* "KSH_VERSION=@(#)" */ 16)
589
Geremy Condra03ebf062011-10-12 18:17:24 -0700590EXTERN const char digits_uc[] E_INIT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
591EXTERN const char digits_lc[] E_INIT("0123456789abcdefghijklmnopqrstuvwxyz");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700592
593/*
594 * Evil hack for const correctness due to API brokenness
595 */
596union mksh_cchack {
597 char *rw;
598 const char *ro;
599};
600union mksh_ccphack {
601 char **rw;
602 const char **ro;
603};
604
Thorsten Glaser811a5752013-07-25 14:24:45 +0000605/*
606 * Evil hack since casting uint to sint is implementation-defined
607 */
608typedef union {
609 mksh_ari_t i;
610 mksh_uari_t u;
611} mksh_ari_u;
612
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700613/* for const debugging */
614#if defined(DEBUG) && defined(__GNUC__) && !defined(__ICC) && \
615 !defined(__INTEL_COMPILER) && !defined(__SUNPRO_C)
616char *ucstrchr(char *, int);
617char *ucstrstr(char *, const char *);
618#undef strchr
619#define strchr ucstrchr
620#define strstr ucstrstr
621#define cstrchr(s,c) ({ \
622 union mksh_cchack in, out; \
623 \
624 in.ro = (s); \
625 out.rw = ucstrchr(in.rw, (c)); \
626 (out.ro); \
627})
628#define cstrstr(b,l) ({ \
629 union mksh_cchack in, out; \
630 \
631 in.ro = (b); \
632 out.rw = ucstrstr(in.rw, (l)); \
633 (out.ro); \
634})
635#define vstrchr(s,c) (cstrchr((s), (c)) != NULL)
636#define vstrstr(b,l) (cstrstr((b), (l)) != NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700637#else /* !DEBUG, !gcc */
638#define cstrchr(s,c) ((const char *)strchr((s), (c)))
639#define cstrstr(s,c) ((const char *)strstr((s), (c)))
640#define vstrchr(s,c) (strchr((s), (c)) != NULL)
641#define vstrstr(b,l) (strstr((b), (l)) != NULL)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000642#endif
643
644#if defined(DEBUG) || defined(__COVERITY__)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000645#ifndef DEBUG_LEAKS
646#define DEBUG_LEAKS
647#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000648#endif
649
Elliott Hughes47086262019-03-26 12:34:31 -0700650#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 571)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000651#error Must run Build.sh to compile this.
Thorsten Glaser811a5752013-07-25 14:24:45 +0000652extern void thiswillneverbedefinedIhope(void);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000653int
654im_sorry_dave(void)
655{
656 /* I’m sorry, Dave. I’m afraid I can’t do that. */
657 return (thiswillneverbedefinedIhope());
658}
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700659#endif
660
661/* use this ipv strchr(s, 0) but no side effects in s! */
Elliott Hughes23925bb2017-09-22 16:04:20 -0700662#define strnul(s) ((s) + strlen((const void *)s))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700663
664#define utf_ptradjx(src, dst) do { \
665 (dst) = (src) + utf_ptradj(src); \
666} while (/* CONSTCOND */ 0)
667
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000668#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
669#define strdupx(d, s, ap) do { \
670 (d) = strdup_i((s), (ap)); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700671} while (/* CONSTCOND */ 0)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000672#define strndupx(d, s, n, ap) do { \
673 (d) = strndup_i((s), (n), (ap)); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700674} while (/* CONSTCOND */ 0)
675#else
676/* be careful to evaluate arguments only once! */
677#define strdupx(d, s, ap) do { \
Elliott Hughes23925bb2017-09-22 16:04:20 -0700678 const char *strdup_src = (const void *)(s); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700679 char *strdup_dst = NULL; \
680 \
681 if (strdup_src != NULL) { \
682 size_t strdup_len = strlen(strdup_src) + 1; \
683 strdup_dst = alloc(strdup_len, (ap)); \
684 memcpy(strdup_dst, strdup_src, strdup_len); \
685 } \
686 (d) = strdup_dst; \
687} while (/* CONSTCOND */ 0)
688#define strndupx(d, s, n, ap) do { \
Elliott Hughes23925bb2017-09-22 16:04:20 -0700689 const char *strdup_src = (const void *)(s); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700690 char *strdup_dst = NULL; \
691 \
692 if (strdup_src != NULL) { \
693 size_t strndup_len = (n); \
694 strdup_dst = alloc(strndup_len + 1, (ap)); \
695 memcpy(strdup_dst, strdup_src, strndup_len); \
696 strdup_dst[strndup_len] = '\0'; \
697 } \
698 (d) = strdup_dst; \
699} while (/* CONSTCOND */ 0)
700#endif
701
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700702#ifdef MKSH_SMALL
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700703#ifndef MKSH_NOPWNAM
704#define MKSH_NOPWNAM /* defined */
705#endif
706#ifndef MKSH_S_NOVI
707#define MKSH_S_NOVI 1
708#endif
709#endif
710
711#ifndef MKSH_S_NOVI
712#define MKSH_S_NOVI 0
713#endif
714
Geremy Condra03ebf062011-10-12 18:17:24 -0700715#if defined(MKSH_NOPROSPECTOFWORK) && !defined(MKSH_UNEMPLOYED)
716#define MKSH_UNEMPLOYED 1
717#endif
718
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000719#define NUFILE 32 /* Number of user-accessible files */
720#define FDBASE 10 /* First file usable by Shell */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000721
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700722/*
723 * simple grouping allocator
724 */
725
Geremy Condra03ebf062011-10-12 18:17:24 -0700726
727/* 0. OS API: where to get memory from and how to free it (grouped) */
728
729/* malloc(3)/realloc(3) -> free(3) for use by the memory allocator */
730#define malloc_osi(sz) malloc(sz)
731#define realloc_osi(p,sz) realloc((p), (sz))
732#define free_osimalloc(p) free(p)
733
734/* malloc(3)/realloc(3) -> free(3) for use by mksh code */
735#define malloc_osfunc(sz) malloc(sz)
736#define realloc_osfunc(p,sz) realloc((p), (sz))
737#define free_osfunc(p) free(p)
738
739#if HAVE_MKNOD
740/* setmode(3) -> free(3) */
741#define free_ossetmode(p) free(p)
742#endif
743
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000744#ifdef MKSH__NO_PATH_MAX
Geremy Condra03ebf062011-10-12 18:17:24 -0700745/* GNU libc: get_current_dir_name(3) -> free(3) */
746#define free_gnu_gcdn(p) free(p)
747#endif
748
749
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700750/* 1. internal structure */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700751struct lalloc_common {
752 struct lalloc_common *next;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700753};
754
Elliott Hughes77740fc2016-08-12 15:06:53 -0700755#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
756struct lalloc_item {
757 struct lalloc_common *next;
758 size_t len;
759 char dummy[8192 - sizeof(struct lalloc_common *) - sizeof(size_t)];
760};
761#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700762
Elliott Hughes77740fc2016-08-12 15:06:53 -0700763/* 2. sizes */
764#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
765#define ALLOC_ITEM struct lalloc_item
766#define ALLOC_OVERHEAD 0
767#else
768#define ALLOC_ITEM struct lalloc_common
769#define ALLOC_OVERHEAD (sizeof(ALLOC_ITEM))
770#endif
771
772/* 3. group structure */
773typedef struct lalloc_common Area;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700774
775
776EXTERN Area aperm; /* permanent object space */
777#define APERM &aperm
778#define ATEMP &e->area
779
780/*
781 * flags (the order of these enums MUST match the order in misc.c(options[]))
782 */
783enum sh_flag {
784#define SHFLAGS_ENUMS
Elliott Hughes737fdce2014-08-07 12:59:26 -0700785#include "sh_flags.gen"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700786 FNFLAGS /* (place holder: how many flags are there) */
787};
788
Geremy Condra03ebf062011-10-12 18:17:24 -0700789#define Flag(f) (shell_flags[(int)(f)])
Elliott Hughes47086262019-03-26 12:34:31 -0700790#define UTFMODE Flag(FUNNYCODE)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700791
792/*
793 * parsing & execution environment
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000794 *
795 * note that kshlongjmp MUST NOT be passed 0 as second argument!
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700796 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000797#ifdef MKSH_NO_SIGSETJMP
798#define kshjmp_buf jmp_buf
799#define kshsetjmp(jbuf) _setjmp(jbuf)
800#define kshlongjmp _longjmp
801#else
802#define kshjmp_buf sigjmp_buf
803#define kshsetjmp(jbuf) sigsetjmp((jbuf), 0)
804#define kshlongjmp siglongjmp
805#endif
806
807struct sretrace_info;
808struct yyrecursive_state;
809
Elliott Hughes23925bb2017-09-22 16:04:20 -0700810EXTERN struct sretrace_info *retrace_info;
Elliott Hughesdd4abe02018-02-05 15:55:19 -0800811EXTERN unsigned int subshell_nesting_type;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000812
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700813extern struct env {
Geremy Condra03ebf062011-10-12 18:17:24 -0700814 ALLOC_ITEM alloc_INT; /* internal, do not touch */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700815 Area area; /* temporary allocation area */
816 struct env *oenv; /* link to previous environment */
817 struct block *loc; /* local variables and functions */
818 short *savefd; /* original redirected fds */
819 struct temp *temps; /* temp files */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000820 /* saved parser recursion state */
821 struct yyrecursive_state *yyrecursive_statep;
822 kshjmp_buf jbuf; /* long jump back to env creator */
823 uint8_t type; /* environment type - see below */
824 uint8_t flags; /* EF_* */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700825} *e;
826
827/* struct env.type values */
828#define E_NONE 0 /* dummy environment */
829#define E_PARSE 1 /* parsing command # */
830#define E_FUNC 2 /* executing function # */
831#define E_INCL 3 /* including a file via . # */
832#define E_EXEC 4 /* executing command tree */
833#define E_LOOP 5 /* executing for/while # */
834#define E_ERRH 6 /* general error handler # */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000835#define E_GONE 7 /* hidden in child */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700836#define E_EVAL 8 /* running eval # */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700837/* # indicates env has valid jbuf (see unwind()) */
838
839/* struct env.flag values */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700840#define EF_BRKCONT_PASS BIT(1) /* set if E_LOOP must pass break/continue on */
841#define EF_FAKE_SIGDIE BIT(2) /* hack to get info from unwind to quitenv */
842
843/* Do breaks/continues stop at env type e? */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000844#define STOP_BRKCONT(t) ((t) == E_NONE || (t) == E_PARSE || \
845 (t) == E_FUNC || (t) == E_INCL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700846/* Do returns stop at env type e? */
847#define STOP_RETURN(t) ((t) == E_FUNC || (t) == E_INCL)
848
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000849/* values for kshlongjmp(e->jbuf, i) */
850/* note that i MUST NOT be zero */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700851#define LRETURN 1 /* return statement */
852#define LEXIT 2 /* exit statement */
853#define LERROR 3 /* errorf() called */
854#define LLEAVE 4 /* untrappable exit/error */
855#define LINTR 5 /* ^C noticed */
856#define LBREAK 6 /* break statement */
857#define LCONTIN 7 /* continue statement */
858#define LSHELL 8 /* return to interactive shell() */
859#define LAEXPR 9 /* error in arithmetic expression */
860
Geremy Condra03ebf062011-10-12 18:17:24 -0700861/* sort of shell global state */
862EXTERN pid_t procpid; /* PID of executing process */
863EXTERN int exstat; /* exit status */
864EXTERN int subst_exstat; /* exit status of last $(..)/`..` */
865EXTERN struct tbl *vp_pipest; /* global PIPESTATUS array */
866EXTERN short trap_exstat; /* exit status before running a trap */
867EXTERN uint8_t trap_nested; /* running nested traps */
868EXTERN uint8_t shell_flags[FNFLAGS];
869EXTERN const char *kshname; /* $0 */
870EXTERN struct {
871 uid_t kshuid_v; /* real UID of shell */
872 uid_t ksheuid_v; /* effective UID of shell */
873 gid_t kshgid_v; /* real GID of shell */
874 gid_t kshegid_v; /* effective GID of shell */
875 pid_t kshpgrp_v; /* process group of shell */
876 pid_t kshppid_v; /* PID of parent of shell */
877 pid_t kshpid_v; /* $$, shell PID */
878} rndsetupstate;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700879
Geremy Condra03ebf062011-10-12 18:17:24 -0700880#define kshpid rndsetupstate.kshpid_v
881#define kshpgrp rndsetupstate.kshpgrp_v
882#define kshuid rndsetupstate.kshuid_v
883#define ksheuid rndsetupstate.ksheuid_v
884#define kshgid rndsetupstate.kshgid_v
885#define kshegid rndsetupstate.kshegid_v
886#define kshppid rndsetupstate.kshppid_v
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700887
888
889/* option processing */
890#define OF_CMDLINE 0x01 /* command line */
891#define OF_SET 0x02 /* set builtin */
892#define OF_SPECIAL 0x04 /* a special variable changing */
893#define OF_INTERNAL 0x08 /* set internally by shell */
894#define OF_FIRSTTIME 0x10 /* as early as possible, once */
895#define OF_ANY (OF_CMDLINE | OF_SET | OF_SPECIAL | OF_INTERNAL)
896
Geremy Condra03ebf062011-10-12 18:17:24 -0700897/* null value for variable; comparison pointer for unset */
898EXTERN char null[] E_INIT("");
Elliott Hughes77740fc2016-08-12 15:06:53 -0700899
900/* string pooling: do we rely on the compiler? */
901#ifndef HAVE_STRING_POOLING
902/* no, we use our own, saves quite some space */
903#elif HAVE_STRING_POOLING == 2
904/* “on demand” */
905#ifdef __GNUC__
906/* only for GCC 4 or later, older ones can get by without */
907#if __GNUC__ < 4
908#undef HAVE_STRING_POOLING
Geremy Condra03ebf062011-10-12 18:17:24 -0700909#endif
Elliott Hughes77740fc2016-08-12 15:06:53 -0700910#else
911/* not GCC, default to on */
912#endif
913#elif HAVE_STRING_POOLING == 0
914/* default to on, unless explicitly set to 0 */
915#undef HAVE_STRING_POOLING
916#endif
917
918#ifndef HAVE_STRING_POOLING /* helpers for pooled strings */
919EXTERN const char T4spaces[] E_INIT(" ");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700920#define T1space (Treal_sp2 + 5)
921#define Tcolsp (Tf_sD_ + 2)
Elliott Hughes23925bb2017-09-22 16:04:20 -0700922#define TC_IFSWS (TinitIFS + 4)
923EXTERN const char TinitIFS[] E_INIT("IFS= \t\n");
Elliott Hughes77740fc2016-08-12 15:06:53 -0700924EXTERN const char TFCEDIT_dollaru[] E_INIT("${FCEDIT:-/bin/ed} $_");
925#define Tspdollaru (TFCEDIT_dollaru + 18)
926EXTERN const char Tsgdot[] E_INIT("*=.");
927EXTERN const char Taugo[] E_INIT("augo");
928EXTERN const char Tbracket[] E_INIT("[");
929#define Tdot (Tsgdot + 2)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700930#define Talias (Tunalias + 2)
931EXTERN const char Tbadnum[] E_INIT("bad number");
932#define Tbadsubst (Tfg_badsubst + 10)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700933EXTERN const char Tbg[] E_INIT("bg");
934EXTERN const char Tbad_bsize[] E_INIT("bad shf/buf/bsize");
935#define Tbsize (Tbad_bsize + 12)
936EXTERN const char Tbad_sig_ss[] E_INIT("%s: bad signal '%s'");
937#define Tbad_sig_s (Tbad_sig_ss + 4)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700938EXTERN const char Tsgbreak[] E_INIT("*=break");
939#define Tbreak (Tsgbreak + 2)
940EXTERN const char T__builtin[] E_INIT("-\\builtin");
941#define T_builtin (T__builtin + 1)
942#define Tbuiltin (T__builtin + 2)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700943EXTERN const char Toomem[] E_INIT("can't allocate %zu data bytes");
944EXTERN const char Tcant_cd[] E_INIT("restricted shell - can't cd");
945EXTERN const char Tcant_find[] E_INIT("can't find");
946EXTERN const char Tcant_open[] E_INIT("can't open");
947#define Tbytes (Toomem + 24)
948EXTERN const char Tbcat[] E_INIT("!cat");
949#define Tcat (Tbcat + 1)
950#define Tcd (Tcant_cd + 25)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700951#define T_command (T_funny_command + 9)
952#define Tcommand (T_funny_command + 10)
953EXTERN const char Tsgcontinue[] E_INIT("*=continue");
954#define Tcontinue (Tsgcontinue + 2)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700955EXTERN const char Tcreate[] E_INIT("create");
956EXTERN const char TELIF_unexpected[] E_INIT("TELIF unexpected");
957EXTERN const char TEXECSHELL[] E_INIT("EXECSHELL");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700958EXTERN const char Tdsgexport[] E_INIT("^*=export");
959#define Texport (Tdsgexport + 3)
Elliott Hughes96b43632015-07-17 11:39:41 -0700960#ifdef __OS2__
961EXTERN const char Textproc[] E_INIT("extproc");
962#endif
Elliott Hughes77740fc2016-08-12 15:06:53 -0700963EXTERN const char Tfalse[] E_INIT("false");
964EXTERN const char Tfg[] E_INIT("fg");
965EXTERN const char Tfg_badsubst[] E_INIT("fileglob: bad substitution");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700966#define Tfile (Tfile_fd + 20)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700967EXTERN const char Tfile_fd[] E_INIT("function definition file");
968EXTERN const char TFPATH[] E_INIT("FPATH");
Geremy Condra03ebf062011-10-12 18:17:24 -0700969EXTERN const char T_function[] E_INIT(" function");
Elliott Hughes77740fc2016-08-12 15:06:53 -0700970#define Tfunction (T_function + 1)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700971EXTERN const char T_funny_command[] E_INIT("funny $()-command");
Elliott Hughes77740fc2016-08-12 15:06:53 -0700972EXTERN const char Tgetopts[] E_INIT("getopts");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700973#define Thistory (Tnot_in_history + 7)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700974EXTERN const char Tintovfl[] E_INIT("integer overflow %zu %c %zu prevented");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700975EXTERN const char Tinvname[] E_INIT("%s: invalid %s name");
Elliott Hughes77740fc2016-08-12 15:06:53 -0700976EXTERN const char Tjobs[] E_INIT("jobs");
977EXTERN const char Tjob_not_started[] E_INIT("job not started");
978EXTERN const char Tmksh[] E_INIT("mksh");
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700979#define Tname (Tinvname + 15)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700980EXTERN const char Tno_args[] E_INIT("missing argument");
981EXTERN const char Tno_OLDPWD[] E_INIT("no OLDPWD");
982EXTERN const char Tnot_ident[] E_INIT("is not an identifier");
983EXTERN const char Tnot_in_history[] E_INIT("not in history");
984EXTERN const char Tnot_found_s[] E_INIT("%s not found");
985#define Tnot_found (Tnot_found_s + 3)
986#define Tnot_started (Tjob_not_started + 4)
987#define TOLDPWD (Tno_OLDPWD + 3)
988#define Topen (Tcant_open + 6)
989#define TPATH (TFPATH + 1)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700990#define Tpv (TpVv + 1)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700991EXTERN const char TpVv[] E_INIT("Vpv");
992#define TPWD (Tno_OLDPWD + 6)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700993#define Tread (Tshf_read + 4)
994EXTERN const char Tdsgreadonly[] E_INIT("^*=readonly");
995#define Treadonly (Tdsgreadonly + 3)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700996EXTERN const char Tredirection_dup[] E_INIT("can't finish (dup) redirection");
997#define Tredirection (Tredirection_dup + 19)
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700998#define Treal_sp1 (Treal_sp2 + 1)
Elliott Hughes77740fc2016-08-12 15:06:53 -0700999EXTERN const char Treal_sp2[] E_INIT(" real ");
1000EXTERN const char Treq_arg[] E_INIT("requires an argument");
1001EXTERN const char Tselect[] E_INIT("select");
1002EXTERN const char Tsgset[] E_INIT("*=set");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001003#define Tset (Tf_parm + 18)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001004#define Tsh (Tmksh + 2)
1005#define TSHELL (TEXECSHELL + 4)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001006#define Tshell (Ttoo_many_files + 23)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001007EXTERN const char Tshf_read[] E_INIT("shf_read");
1008EXTERN const char Tshf_write[] E_INIT("shf_write");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001009EXTERN const char Tgsource[] E_INIT("=source");
1010#define Tsource (Tgsource + 1)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001011EXTERN const char Tj_suspend[] E_INIT("j_suspend");
1012#define Tsuspend (Tj_suspend + 2)
1013EXTERN const char Tsynerr[] E_INIT("syntax error");
1014EXTERN const char Ttime[] E_INIT("time");
1015EXTERN const char Ttoo_many_args[] E_INIT("too many arguments");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001016EXTERN const char Ttoo_many_files[] E_INIT("too many open files in shell");
Elliott Hughes77740fc2016-08-12 15:06:53 -07001017EXTERN const char Ttrue[] E_INIT("true");
1018EXTERN const char Ttty_fd_dupof[] E_INIT("dup of tty fd");
1019#define Ttty_fd (Ttty_fd_dupof + 7)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001020EXTERN const char Tdgtypeset[] E_INIT("^=typeset");
1021#define Ttypeset (Tdgtypeset + 2)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001022#define Tugo (Taugo + 1)
1023EXTERN const char Tunalias[] E_INIT("unalias");
1024#define Tunexpected (TELIF_unexpected + 6)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001025EXTERN const char Tunexpected_type[] E_INIT("%s: unexpected %s type %d");
Elliott Hughes77740fc2016-08-12 15:06:53 -07001026EXTERN const char Tunknown_option[] E_INIT("unknown option");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001027EXTERN const char Tunwind[] E_INIT("unwind");
1028#define Tuser_sp1 (Tuser_sp2 + 1)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001029EXTERN const char Tuser_sp2[] E_INIT(" user ");
1030#define Twrite (Tshf_write + 4)
1031EXTERN const char Tf__S[] E_INIT(" %S");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001032#define Tf__d (Tunexpected_type + 22)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001033EXTERN const char Tf__ss[] E_INIT(" %s%s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001034#define Tf__sN (Tf_s_s_sN + 5)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001035EXTERN const char Tf_sSs[] E_INIT("%s/%s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001036#define Tf_T (Tf_s_T + 3)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001037EXTERN const char Tf_dN[] E_INIT("%d\n");
1038EXTERN const char Tf_s_[] E_INIT("%s ");
1039EXTERN const char Tf_s_T[] E_INIT("%s %T");
1040EXTERN const char Tf_s_s_sN[] E_INIT("%s %s %s\n");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001041#define Tf_s_s (Tf_sD_s_s + 4)
1042#define Tf_s_sD_s (Tf_cant_ss_s + 6)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001043EXTERN const char Tf_optfoo[] E_INIT("%s%s-%c: %s");
1044EXTERN const char Tf_sD_[] E_INIT("%s: ");
1045EXTERN const char Tf_szs[] E_INIT("%s: %zd %s");
1046EXTERN const char Tf_parm[] E_INIT("%s: parameter not set");
1047EXTERN const char Tf_coproc[] E_INIT("-p: %s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001048EXTERN const char Tf_cant_s[] E_INIT("%s: can't %s");
1049EXTERN const char Tf_cant_ss_s[] E_INIT("can't %s %s: %s");
1050EXTERN const char Tf_heredoc[] E_INIT("here document '%s' unclosed");
Elliott Hughes77740fc2016-08-12 15:06:53 -07001051#if HAVE_MKNOD
1052EXTERN const char Tf_nonnum[] E_INIT("non-numeric %s %s '%s'");
1053#endif
1054EXTERN const char Tf_S_[] E_INIT("%S ");
1055#define Tf_S (Tf__S + 1)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001056#define Tf_lu (Tf_toolarge + 17)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001057EXTERN const char Tf_toolarge[] E_INIT("%s %s too large: %lu");
1058EXTERN const char Tf_ldfailed[] E_INIT("%s %s(%d, %ld) failed: %s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001059#define Tf_ss (Tf_sss + 2)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001060EXTERN const char Tf_sss[] E_INIT("%s%s%s");
1061EXTERN const char Tf_sD_s_sD_s[] E_INIT("%s: %s %s: %s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001062EXTERN const char Tf_toomany[] E_INIT("too many %ss");
Elliott Hughes77740fc2016-08-12 15:06:53 -07001063EXTERN const char Tf_sd[] E_INIT("%s %d");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001064#define Tf_s (Tf_temp + 28)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001065EXTERN const char Tft_end[] E_INIT("%;");
1066EXTERN const char Tft_R[] E_INIT("%R");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001067#define Tf_d (Tunexpected_type + 23)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001068EXTERN const char Tf_sD_s_qs[] E_INIT("%s: %s '%s'");
1069EXTERN const char Tf_ro[] E_INIT("read-only: %s");
1070EXTERN const char Tf_flags[] E_INIT("%s: flags 0x%X");
1071EXTERN const char Tf_temp[] E_INIT("can't %s temporary file %s: %s");
1072EXTERN const char Tf_ssfaileds[] E_INIT("%s: %s failed: %s");
1073EXTERN const char Tf_sD_sD_s[] E_INIT("%s: %s: %s");
1074EXTERN const char Tf__c_[] E_INIT("-%c ");
1075EXTERN const char Tf_sD_s_s[] E_INIT("%s: %s %s");
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001076#define Tf_sN (Tf_s_s_sN + 6)
1077#define Tf_sD_s (Tf_temp + 24)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001078EXTERN const char T_devtty[] E_INIT("/dev/tty");
1079#else /* helpers for string pooling */
1080#define T4spaces " "
1081#define T1space " "
1082#define Tcolsp ": "
Elliott Hughes77740fc2016-08-12 15:06:53 -07001083#define TC_IFSWS " \t\n"
Elliott Hughes23925bb2017-09-22 16:04:20 -07001084#define TinitIFS "IFS= \t\n"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001085#define TFCEDIT_dollaru "${FCEDIT:-/bin/ed} $_"
1086#define Tspdollaru " $_"
1087#define Tsgdot "*=."
1088#define Taugo "augo"
1089#define Tbracket "["
1090#define Tdot "."
1091#define Talias "alias"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001092#define Tbadnum "bad number"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001093#define Tbadsubst "bad substitution"
1094#define Tbg "bg"
1095#define Tbad_bsize "bad shf/buf/bsize"
1096#define Tbsize "bsize"
1097#define Tbad_sig_ss "%s: bad signal '%s'"
1098#define Tbad_sig_s "bad signal '%s'"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001099#define Tsgbreak "*=break"
1100#define Tbreak "break"
1101#define T__builtin "-\\builtin"
1102#define T_builtin "\\builtin"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001103#define Tbuiltin "builtin"
1104#define Toomem "can't allocate %zu data bytes"
1105#define Tcant_cd "restricted shell - can't cd"
1106#define Tcant_find "can't find"
1107#define Tcant_open "can't open"
1108#define Tbytes "bytes"
1109#define Tbcat "!cat"
1110#define Tcat "cat"
1111#define Tcd "cd"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001112#define T_command "-command"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001113#define Tcommand "command"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001114#define Tsgcontinue "*=continue"
1115#define Tcontinue "continue"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001116#define Tcreate "create"
1117#define TELIF_unexpected "TELIF unexpected"
1118#define TEXECSHELL "EXECSHELL"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001119#define Tdsgexport "^*=export"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001120#define Texport "export"
1121#ifdef __OS2__
1122#define Textproc "extproc"
1123#endif
1124#define Tfalse "false"
1125#define Tfg "fg"
1126#define Tfg_badsubst "fileglob: bad substitution"
1127#define Tfile "file"
1128#define Tfile_fd "function definition file"
1129#define TFPATH "FPATH"
1130#define T_function " function"
1131#define Tfunction "function"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001132#define T_funny_command "funny $()-command"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001133#define Tgetopts "getopts"
1134#define Thistory "history"
1135#define Tintovfl "integer overflow %zu %c %zu prevented"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001136#define Tinvname "%s: invalid %s name"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001137#define Tjobs "jobs"
1138#define Tjob_not_started "job not started"
1139#define Tmksh "mksh"
1140#define Tname "name"
1141#define Tno_args "missing argument"
1142#define Tno_OLDPWD "no OLDPWD"
1143#define Tnot_ident "is not an identifier"
1144#define Tnot_in_history "not in history"
1145#define Tnot_found_s "%s not found"
1146#define Tnot_found "not found"
1147#define Tnot_started "not started"
1148#define TOLDPWD "OLDPWD"
1149#define Topen "open"
1150#define TPATH "PATH"
1151#define Tpv "pv"
1152#define TpVv "Vpv"
1153#define TPWD "PWD"
1154#define Tread "read"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001155#define Tdsgreadonly "^*=readonly"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001156#define Treadonly "readonly"
1157#define Tredirection_dup "can't finish (dup) redirection"
1158#define Tredirection "redirection"
1159#define Treal_sp1 "real "
1160#define Treal_sp2 " real "
1161#define Treq_arg "requires an argument"
1162#define Tselect "select"
1163#define Tsgset "*=set"
1164#define Tset "set"
1165#define Tsh "sh"
1166#define TSHELL "SHELL"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001167#define Tshell "shell"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001168#define Tshf_read "shf_read"
1169#define Tshf_write "shf_write"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001170#define Tgsource "=source"
1171#define Tsource "source"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001172#define Tj_suspend "j_suspend"
1173#define Tsuspend "suspend"
1174#define Tsynerr "syntax error"
1175#define Ttime "time"
1176#define Ttoo_many_args "too many arguments"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001177#define Ttoo_many_files "too many open files in shell"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001178#define Ttrue "true"
1179#define Ttty_fd_dupof "dup of tty fd"
1180#define Ttty_fd "tty fd"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001181#define Tdgtypeset "^=typeset"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001182#define Ttypeset "typeset"
1183#define Tugo "ugo"
1184#define Tunalias "unalias"
1185#define Tunexpected "unexpected"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001186#define Tunexpected_type "%s: unexpected %s type %d"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001187#define Tunknown_option "unknown option"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001188#define Tunwind "unwind"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001189#define Tuser_sp1 "user "
1190#define Tuser_sp2 " user "
1191#define Twrite "write"
1192#define Tf__S " %S"
1193#define Tf__d " %d"
1194#define Tf__ss " %s%s"
1195#define Tf__sN " %s\n"
1196#define Tf_sSs "%s/%s"
1197#define Tf_T "%T"
1198#define Tf_dN "%d\n"
1199#define Tf_s_ "%s "
1200#define Tf_s_T "%s %T"
1201#define Tf_s_s_sN "%s %s %s\n"
1202#define Tf_s_s "%s %s"
1203#define Tf_s_sD_s "%s %s: %s"
1204#define Tf_optfoo "%s%s-%c: %s"
1205#define Tf_sD_ "%s: "
1206#define Tf_szs "%s: %zd %s"
1207#define Tf_parm "%s: parameter not set"
1208#define Tf_coproc "-p: %s"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001209#define Tf_cant_s "%s: can't %s"
1210#define Tf_cant_ss_s "can't %s %s: %s"
1211#define Tf_heredoc "here document '%s' unclosed"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001212#if HAVE_MKNOD
1213#define Tf_nonnum "non-numeric %s %s '%s'"
1214#endif
1215#define Tf_S_ "%S "
1216#define Tf_S "%S"
1217#define Tf_lu "%lu"
1218#define Tf_toolarge "%s %s too large: %lu"
1219#define Tf_ldfailed "%s %s(%d, %ld) failed: %s"
1220#define Tf_ss "%s%s"
1221#define Tf_sss "%s%s%s"
1222#define Tf_sD_s_sD_s "%s: %s %s: %s"
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001223#define Tf_toomany "too many %ss"
Elliott Hughes77740fc2016-08-12 15:06:53 -07001224#define Tf_sd "%s %d"
1225#define Tf_s "%s"
1226#define Tft_end "%;"
1227#define Tft_R "%R"
1228#define Tf_d "%d"
1229#define Tf_sD_s_qs "%s: %s '%s'"
1230#define Tf_ro "read-only: %s"
1231#define Tf_flags "%s: flags 0x%X"
1232#define Tf_temp "can't %s temporary file %s: %s"
1233#define Tf_ssfaileds "%s: %s failed: %s"
1234#define Tf_sD_sD_s "%s: %s: %s"
1235#define Tf__c_ "-%c "
1236#define Tf_sD_s_s "%s: %s %s"
1237#define Tf_sN "%s\n"
1238#define Tf_sD_s "%s: %s"
1239#define T_devtty "/dev/tty"
1240#endif /* end of string pooling */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001241
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001242typedef uint8_t Temp_type;
1243/* expanded heredoc */
1244#define TT_HEREDOC_EXP 0
1245/* temporary file used for history editing (fc -e) */
1246#define TT_HIST_EDIT 1
1247/* temporary file used during in-situ command substitution */
1248#define TT_FUNSUB 2
1249
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001250/* temp/heredoc files. The file is removed when the struct is freed. */
1251struct temp {
1252 struct temp *next;
1253 struct shf *shf;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001254 /* pid of process parsed here-doc */
1255 pid_t pid;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001256 Temp_type type;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001257 /* actually longer: name (variable length) */
1258 char tffn[3];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001259};
1260
1261/*
1262 * stdio and our IO routines
1263 */
1264
Thorsten Glaser811a5752013-07-25 14:24:45 +00001265#define shl_xtrace (&shf_iob[0]) /* for set -x */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001266#define shl_stdout (&shf_iob[1])
1267#define shl_out (&shf_iob[2])
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001268#ifdef DF
1269#define shl_dbg (&shf_iob[3]) /* for DF() */
1270#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07001271EXTERN bool shl_stdout_ok;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001272
1273/*
1274 * trap handlers
1275 */
1276typedef struct trap {
1277 const char *name; /* short name */
1278 const char *mess; /* descriptive name */
1279 char *trap; /* trap command */
1280 sig_t cursig; /* current handler (valid if TF_ORIG_* set) */
1281 sig_t shtrap; /* shell signal handler */
1282 int signal; /* signal number */
1283 int flags; /* TF_* */
1284 volatile sig_atomic_t set; /* trap pending */
1285} Trap;
1286
1287/* values for Trap.flags */
1288#define TF_SHELL_USES BIT(0) /* shell uses signal, user can't change */
1289#define TF_USER_SET BIT(1) /* user has (tried to) set trap */
1290#define TF_ORIG_IGN BIT(2) /* original action was SIG_IGN */
1291#define TF_ORIG_DFL BIT(3) /* original action was SIG_DFL */
1292#define TF_EXEC_IGN BIT(4) /* restore SIG_IGN just before exec */
1293#define TF_EXEC_DFL BIT(5) /* restore SIG_DFL just before exec */
1294#define TF_DFL_INTR BIT(6) /* when received, default action is LINTR */
1295#define TF_TTY_INTR BIT(7) /* tty generated signal (see j_waitj) */
1296#define TF_CHANGED BIT(8) /* used by runtrap() to detect trap changes */
1297#define TF_FATAL BIT(9) /* causes termination if not trapped */
1298
1299/* values for setsig()/setexecsig() flags argument */
1300#define SS_RESTORE_MASK 0x3 /* how to restore a signal before an exec() */
1301#define SS_RESTORE_CURR 0 /* leave current handler in place */
1302#define SS_RESTORE_ORIG 1 /* restore original handler */
1303#define SS_RESTORE_DFL 2 /* restore to SIG_DFL */
1304#define SS_RESTORE_IGN 3 /* restore to SIG_IGN */
1305#define SS_FORCE BIT(3) /* set signal even if original signal ignored */
1306#define SS_USER BIT(4) /* user is doing the set (ie, trap command) */
1307#define SS_SHTRAP BIT(5) /* trap for internal use (ALRM, CHLD, WINCH) */
1308
Elliott Hughes96b43632015-07-17 11:39:41 -07001309#define ksh_SIGEXIT 0 /* for trap EXIT */
1310#define ksh_SIGERR ksh_NSIG /* for trap ERR */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001311
1312EXTERN volatile sig_atomic_t trap; /* traps pending? */
1313EXTERN volatile sig_atomic_t intrsig; /* pending trap interrupts command */
Geremy Condra03ebf062011-10-12 18:17:24 -07001314EXTERN volatile sig_atomic_t fatal_trap; /* received a fatal signal */
Elliott Hughes96b43632015-07-17 11:39:41 -07001315extern Trap sigtraps[ksh_NSIG + 1];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001316
1317/* got_winch = 1 when we need to re-adjust the window size */
1318#ifdef SIGWINCH
Geremy Condra03ebf062011-10-12 18:17:24 -07001319EXTERN volatile sig_atomic_t got_winch E_INIT(1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001320#else
1321#define got_winch true
1322#endif
1323
1324/*
1325 * TMOUT support
1326 */
1327/* values for ksh_tmout_state */
1328enum tmout_enum {
1329 TMOUT_EXECUTING = 0, /* executing commands */
1330 TMOUT_READING, /* waiting for input */
1331 TMOUT_LEAVING /* have timed out */
1332};
1333EXTERN unsigned int ksh_tmout;
Elliott Hughes23925bb2017-09-22 16:04:20 -07001334EXTERN enum tmout_enum ksh_tmout_state;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001335
1336/* For "You have stopped jobs" message */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001337EXTERN bool really_exit;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001338
1339/*
1340 * fast character classes
1341 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001342
Elliott Hughes23925bb2017-09-22 16:04:20 -07001343/* internal types, do not reference */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001344
Elliott Hughes23925bb2017-09-22 16:04:20 -07001345/* initially empty — filled at runtime from $IFS */
1346#define CiIFS BIT(0)
1347#define CiCNTRL BIT(1) /* \x01‥\x08\x0E‥\x1F\x7F */
1348#define CiUPPER BIT(2) /* A‥Z */
1349#define CiLOWER BIT(3) /* a‥z */
1350#define CiHEXLT BIT(4) /* A‥Fa‥f */
1351#define CiOCTAL BIT(5) /* 0‥7 */
1352#define CiQCL BIT(6) /* &();| */
1353#define CiALIAS BIT(7) /* !,.@ */
1354#define CiQCX BIT(8) /* *[\\ */
1355#define CiVAR1 BIT(9) /* !*@ */
1356#define CiQCM BIT(10) /* /^~ */
1357#define CiDIGIT BIT(11) /* 89 */
1358#define CiQC BIT(12) /* "' */
1359#define CiSPX BIT(13) /* \x0B\x0C */
1360#define CiCURLY BIT(14) /* {} */
1361#define CiANGLE BIT(15) /* <> */
1362#define CiNUL BIT(16) /* \x00 */
1363#define CiTAB BIT(17) /* \x09 */
1364#define CiNL BIT(18) /* \x0A */
1365#define CiCR BIT(19) /* \x0D */
1366#define CiSP BIT(20) /* \x20 */
1367#define CiHASH BIT(21) /* # */
1368#define CiSS BIT(22) /* $ */
1369#define CiPERCT BIT(23) /* % */
1370#define CiPLUS BIT(24) /* + */
1371#define CiMINUS BIT(25) /* - */
1372#define CiCOLON BIT(26) /* : */
1373#define CiEQUAL BIT(27) /* = */
1374#define CiQUEST BIT(28) /* ? */
1375#define CiBRACK BIT(29) /* ] */
1376#define CiUNDER BIT(30) /* _ */
1377#define CiGRAVE BIT(31) /* ` */
1378/* out of space, but one for *@ would make sense, possibly others */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001379
Elliott Hughes23925bb2017-09-22 16:04:20 -07001380/* compile-time initialised, ASCII only */
1381extern const uint32_t tpl_ctypes[128];
1382/* run-time, contains C_IFS as well, full 2⁸ octet range */
1383EXTERN uint32_t ksh_ctypes[256];
1384/* first octet of $IFS, for concatenating "$*" */
1385EXTERN char ifs0;
1386
1387/* external types */
1388
1389/* !%,-.0‥9:@A‥Z[]_a‥z valid characters in alias names */
1390#define C_ALIAS (CiALIAS | CiBRACK | CiCOLON | CiDIGIT | CiLOWER | CiMINUS | CiOCTAL | CiPERCT | CiUNDER | CiUPPER)
1391/* 0‥9A‥Za‥z alphanumerical */
1392#define C_ALNUM (CiDIGIT | CiLOWER | CiOCTAL | CiUPPER)
1393/* 0‥9A‥Z_a‥z alphanumerical plus underscore (“word character”) */
1394#define C_ALNUX (CiDIGIT | CiLOWER | CiOCTAL | CiUNDER | CiUPPER)
1395/* A‥Za‥z alphabetical (upper plus lower) */
1396#define C_ALPHA (CiLOWER | CiUPPER)
1397/* A‥Z_a‥z alphabetical plus underscore (identifier lead) */
1398#define C_ALPHX (CiLOWER | CiUNDER | CiUPPER)
1399/* \x01‥\x7F 7-bit ASCII except NUL */
1400#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)
1401/* \x09\x20 tab and space */
1402#define C_BLANK (CiSP | CiTAB)
1403/* \x09\x20"' separator for completion */
1404#define C_CFS (CiQC | CiSP | CiTAB)
1405/* \x00‥\x1F\x7F POSIX control characters */
1406#define C_CNTRL (CiCNTRL | CiCR | CiNL | CiNUL | CiSPX | CiTAB)
1407/* 0‥9 decimal digits */
1408#define C_DIGIT (CiDIGIT | CiOCTAL)
1409/* &();`| editor x_locate_word() command */
1410#define C_EDCMD (CiGRAVE | CiQCL)
1411/* \x09\x0A\x20"&'():;<=>`| editor non-word characters */
1412#define C_EDNWC (CiANGLE | CiCOLON | CiEQUAL | CiGRAVE | CiNL | CiQC | CiQCL | CiSP | CiTAB)
1413/* "#$&'()*:;<=>?[\\`{|} editor quotes for tab completion */
1414#define C_EDQ (CiANGLE | CiCOLON | CiCURLY | CiEQUAL | CiGRAVE | CiHASH | CiQC | CiQCL | CiQCX | CiQUEST | CiSS)
1415/* !‥~ POSIX graphical (alphanumerical plus punctuation) */
1416#define C_GRAPH (C_PUNCT | CiDIGIT | CiLOWER | CiOCTAL | CiUPPER)
1417/* A‥Fa‥f hex letter */
1418#define C_HEXLT CiHEXLT
1419/* \x00 + $IFS IFS whitespace, IFS non-whitespace, NUL */
1420#define C_IFS (CiIFS | CiNUL)
1421/* \x09\x0A\x20 IFS whitespace */
1422#define C_IFSWS (CiNL | CiSP | CiTAB)
1423/* \x09\x0A\x20&();<>| (for the lexer) */
1424#define C_LEX1 (CiANGLE | CiNL | CiQCL | CiSP | CiTAB)
1425/* a‥z lowercase letters */
1426#define C_LOWER CiLOWER
1427/* not alnux or dollar separator for motion */
1428#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)
1429/* 0‥7 octal digit */
1430#define C_OCTAL CiOCTAL
1431/* !*+?@ pattern magical operator, except space */
1432#define C_PATMO (CiPLUS | CiQUEST | CiVAR1)
1433/* \x20‥~ POSIX printable characters (graph plus space) */
1434#define C_PRINT (C_GRAPH | CiSP)
1435/* !"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ POSIX punctuation */
1436#define C_PUNCT (CiALIAS | CiANGLE | CiBRACK | CiCOLON | CiCURLY | CiEQUAL | CiGRAVE | CiHASH | CiMINUS | CiPERCT | CiPLUS | CiQC | CiQCL | CiQCM | CiQCX | CiQUEST | CiSS | CiUNDER)
1437/* \x09\x0A"#$&'()*;<=>?[\\]`| characters requiring quoting, minus space */
1438#define C_QUOTE (CiANGLE | CiBRACK | CiEQUAL | CiGRAVE | CiHASH | CiNL | CiQC | CiQCL | CiQCX | CiQUEST | CiSS | CiTAB)
1439/* 0‥9A‥Fa‥f hexadecimal digit */
1440#define C_SEDEC (CiDIGIT | CiHEXLT | CiOCTAL)
1441/* \x09‥\x0D\x20 POSIX space class */
1442#define C_SPACE (CiCR | CiNL | CiSP | CiSPX | CiTAB)
1443/* +-=? substitution operations with word */
1444#define C_SUB1 (CiEQUAL | CiMINUS | CiPLUS | CiQUEST)
1445/* #% substitution operations with pattern */
1446#define C_SUB2 (CiHASH | CiPERCT)
1447/* A‥Z uppercase letters */
1448#define C_UPPER CiUPPER
1449/* !#$*-?@ substitution parameters, other than positional */
1450#define C_VAR1 (CiHASH | CiMINUS | CiQUEST | CiSS | CiVAR1)
1451
1452/* individual chars you might like */
1453#define C_ANGLE CiANGLE /* <> angle brackets */
1454#define C_COLON CiCOLON /* : colon */
1455#define C_CR CiCR /* \x0D ASCII carriage return */
1456#define C_DOLAR CiSS /* $ dollar sign */
1457#define C_EQUAL CiEQUAL /* = equals sign */
1458#define C_GRAVE CiGRAVE /* ` accent gravis */
1459#define C_HASH CiHASH /* # hash sign */
1460#define C_LF CiNL /* \x0A ASCII line feed */
1461#define C_MINUS CiMINUS /* - hyphen-minus */
1462#ifdef MKSH_WITH_TEXTMODE
1463#define C_NL (CiNL | CiCR) /* CR or LF under OS/2 TEXTMODE */
1464#else
1465#define C_NL CiNL /* LF only like under Unix */
1466#endif
1467#define C_NUL CiNUL /* \x00 ASCII NUL */
1468#define C_PLUS CiPLUS /* + plus sign */
1469#define C_QC CiQC /* "' quote characters */
1470#define C_QUEST CiQUEST /* ? question mark */
1471#define C_SPC CiSP /* \x20 ASCII space */
1472#define C_TAB CiTAB /* \x09 ASCII horizontal tabulator */
1473#define C_UNDER CiUNDER /* _ underscore */
1474
1475/* identity transform of octet */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001476#if defined(DEBUG) && defined(__GNUC__) && !defined(__ICC) && \
1477 !defined(__INTEL_COMPILER) && !defined(__SUNPRO_C)
1478extern unsigned int eek_ord;
1479#define ORD(c) ((size_t)(c) > 0xFF ? eek_ord : \
1480 ((unsigned int)(unsigned char)(c)))
1481#define ord(c) __builtin_choose_expr( \
1482 __builtin_types_compatible_p(__typeof__(c), char) || \
1483 __builtin_types_compatible_p(__typeof__(c), unsigned char), \
1484 ((unsigned int)(unsigned char)(c)), ({ \
1485 size_t ord_c = (c); \
1486 \
1487 if (ord_c > (size_t)0xFFU) \
1488 internal_errorf("%s:%d:ord(%zX)", \
1489 __FILE__, __LINE__, ord_c); \
1490 ((unsigned int)(unsigned char)(ord_c)); \
1491}))
1492#else
1493#define ord(c) ((unsigned int)(unsigned char)(c))
1494#define ORD(c) ord(c) /* may evaluate arguments twice */
1495#endif
Elliott Hughes23925bb2017-09-22 16:04:20 -07001496#if defined(MKSH_EBCDIC) || defined(MKSH_FAUX_EBCDIC)
1497EXTERN unsigned short ebcdic_map[256];
1498EXTERN unsigned char ebcdic_rtt_toascii[256];
1499EXTERN unsigned char ebcdic_rtt_fromascii[256];
1500extern void ebcdic_init(void);
1501/* one-way to-ascii-or-high conversion, for POSIX locale ordering */
1502#define asciibetical(c) ((unsigned int)ebcdic_map[(unsigned char)(c)])
1503/* two-way round-trip conversion, for general use */
1504#define rtt2asc(c) ebcdic_rtt_toascii[(unsigned char)(c)]
1505#define asc2rtt(c) ebcdic_rtt_fromascii[(unsigned char)(c)]
1506/* case-independent char comparison */
1507#define ksh_eq(c,u,l) (ord(c) == ord(u) || ord(c) == ord(l))
1508#else
1509#define asciibetical(c) ord(c)
1510#define rtt2asc(c) ((unsigned char)(c))
1511#define asc2rtt(c) ((unsigned char)(c))
1512#define ksh_eq(c,u,l) ((ord(c) | 0x20) == ord(l))
1513#endif
1514/* control character foo */
1515#ifdef MKSH_EBCDIC
1516#define ksh_isctrl(c) (ord(c) < 0x40 || ord(c) == 0xFF)
1517#else
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001518#define ksh_isctrl(c) ((ord(c) & 0x7F) < 0x20 || ord(c) == 0x7F)
Elliott Hughes23925bb2017-09-22 16:04:20 -07001519#endif
1520/* new fast character classes */
1521#define ctype(c,t) tobool(ksh_ctypes[ord(c)] & (t))
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001522#define cinttype(c,t) ((c) >= 0 && (c) <= 0xFF ? \
1523 tobool(ksh_ctypes[(unsigned char)(c)] & (t)) : false)
Elliott Hughes23925bb2017-09-22 16:04:20 -07001524/* helper functions */
1525#define ksh_isdash(s) tobool(ord((s)[0]) == '-' && ord((s)[1]) == '\0')
1526/* invariant distance even in EBCDIC */
1527#define ksh_tolower(c) (ctype(c, C_UPPER) ? (c) - 'A' + 'a' : (c))
1528#define ksh_toupper(c) (ctype(c, C_LOWER) ? (c) - 'a' + 'A' : (c))
1529/* strictly speaking rtt2asc() here, but this works even in EBCDIC */
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001530#define ksh_numdig(c) (ord(c) - ORD('0'))
Elliott Hughes23925bb2017-09-22 16:04:20 -07001531#define ksh_numuc(c) (rtt2asc(c) - rtt2asc('A'))
1532#define ksh_numlc(c) (rtt2asc(c) - rtt2asc('a'))
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001533#define ksh_toctrl(c) asc2rtt(ord(c) == ORD('?') ? 0x7F : rtt2asc(c) & 0x9F)
Elliott Hughes23925bb2017-09-22 16:04:20 -07001534#define ksh_unctrl(c) asc2rtt(rtt2asc(c) ^ 0x40U)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001535
1536/* Argument parsing for built-in commands and getopts command */
1537
1538/* Values for Getopt.flags */
1539#define GF_ERROR BIT(0) /* call errorf() if there is an error */
1540#define GF_PLUSOPT BIT(1) /* allow +c as an option */
1541#define GF_NONAME BIT(2) /* don't print argv[0] in errors */
1542
1543/* Values for Getopt.info */
1544#define GI_MINUS BIT(0) /* an option started with -... */
1545#define GI_PLUS BIT(1) /* an option started with +... */
1546#define GI_MINUSMINUS BIT(2) /* arguments were ended with -- */
1547
Geremy Condra03ebf062011-10-12 18:17:24 -07001548/* in case some OS defines these */
1549#undef optarg
1550#undef optind
1551
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001552typedef struct {
Geremy Condra03ebf062011-10-12 18:17:24 -07001553 const char *optarg;
1554 int optind;
1555 int uoptind; /* what user sees in $OPTIND */
1556 int flags; /* see GF_* */
1557 int info; /* see GI_* */
1558 unsigned int p; /* 0 or index into argv[optind - 1] */
1559 char buf[2]; /* for bad option OPTARG value */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001560} Getopt;
1561
1562EXTERN Getopt builtin_opt; /* for shell builtin commands */
1563EXTERN Getopt user_opt; /* parsing state for getopts builtin command */
1564
1565/* This for co-processes */
1566
Geremy Condra03ebf062011-10-12 18:17:24 -07001567/* something that won't (realisticly) wrap */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001568typedef int Coproc_id;
Geremy Condra03ebf062011-10-12 18:17:24 -07001569
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001570struct coproc {
1571 void *job; /* 0 or job of co-process using input pipe */
1572 int read; /* pipe from co-process's stdout */
1573 int readw; /* other side of read (saved temporarily) */
1574 int write; /* pipe to co-process's stdin */
1575 int njobs; /* number of live jobs using output pipe */
1576 Coproc_id id; /* id of current output pipe */
1577};
1578EXTERN struct coproc coproc;
1579
Geremy Condra03ebf062011-10-12 18:17:24 -07001580#ifndef MKSH_NOPROSPECTOFWORK
1581/* used in jobs.c and by coprocess stuff in exec.c and select() calls */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001582EXTERN sigset_t sm_default, sm_sigchld;
Geremy Condra03ebf062011-10-12 18:17:24 -07001583#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001584
1585/* name of called builtin function (used by error functions) */
1586EXTERN const char *builtin_argv0;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001587/* is called builtin a POSIX special builtin? (error functions only) */
Elliott Hughes50012062015-03-10 22:22:24 -07001588EXTERN bool builtin_spec;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001589
Geremy Condra03ebf062011-10-12 18:17:24 -07001590/* current working directory */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001591EXTERN char *current_wd;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001592
Thorsten Glaser811a5752013-07-25 14:24:45 +00001593/* input line size */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001594#ifdef MKSH_SMALL
1595#define LINE (4096 - ALLOC_OVERHEAD)
1596#else
1597#define LINE (16384 - ALLOC_OVERHEAD)
1598#endif
1599/* columns and lines of the tty */
1600EXTERN mksh_ari_t x_cols E_INIT(80);
1601EXTERN mksh_ari_t x_lins E_INIT(24);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001602
Geremy Condra03ebf062011-10-12 18:17:24 -07001603
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001604/* Determine the location of the system (common) profile */
Geremy Condra03ebf062011-10-12 18:17:24 -07001605
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001606#ifndef MKSH_DEFAULT_PROFILEDIR
Elliott Hughes966dd552016-12-08 15:56:04 -08001607#define MKSH_DEFAULT_PROFILEDIR MKSH_UNIXROOT "/etc"
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001608#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07001609
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001610#define MKSH_SYSTEM_PROFILE MKSH_DEFAULT_PROFILEDIR "/profile"
1611#define MKSH_SUID_PROFILE MKSH_DEFAULT_PROFILEDIR "/suid_profile"
Geremy Condra03ebf062011-10-12 18:17:24 -07001612
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001613
1614/* Used by v_evaluate() and setstr() to control action when error occurs */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001615#define KSH_UNWIND_ERROR 0 /* unwind the stack (kshlongjmp) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001616#define KSH_RETURN_ERROR 1 /* return 1/0 for success/failure */
1617
1618/*
1619 * Shell file I/O routines
1620 */
1621
Geremy Condra03ebf062011-10-12 18:17:24 -07001622#define SHF_BSIZE 512
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001623
Geremy Condra03ebf062011-10-12 18:17:24 -07001624#define shf_fileno(shf) ((shf)->fd)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001625#define shf_setfileno(shf,nfd) ((shf)->fd = (nfd))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001626#define shf_getc_i(shf) ((shf)->rnleft > 0 ? \
Elliott Hughesdd4abe02018-02-05 15:55:19 -08001627 (shf)->rnleft--, (int)ord(*(shf)->rp++) : \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001628 shf_getchar(shf))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001629#define shf_putc_i(c, shf) ((shf)->wnleft == 0 ? \
Elliott Hughes77740fc2016-08-12 15:06:53 -07001630 shf_putchar((uint8_t)(c), (shf)) : \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001631 ((shf)->wnleft--, *(shf)->wp++ = (c)))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001632#define shf_eof(shf) ((shf)->flags & SHF_EOF)
1633#define shf_error(shf) ((shf)->flags & SHF_ERROR)
Geremy Condra03ebf062011-10-12 18:17:24 -07001634#define shf_errno(shf) ((shf)->errnosv)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001635#define shf_clearerr(shf) ((shf)->flags &= ~(SHF_EOF | SHF_ERROR))
1636
1637/* Flags passed to shf_*open() */
1638#define SHF_RD 0x0001
1639#define SHF_WR 0x0002
1640#define SHF_RDWR (SHF_RD|SHF_WR)
1641#define SHF_ACCMODE 0x0003 /* mask */
1642#define SHF_GETFL 0x0004 /* use fcntl() to figure RD/WR flags */
1643#define SHF_UNBUF 0x0008 /* unbuffered I/O */
1644#define SHF_CLEXEC 0x0010 /* set close on exec flag */
1645#define SHF_MAPHI 0x0020 /* make fd > FDBASE (and close orig)
1646 * (shf_open() only) */
1647#define SHF_DYNAMIC 0x0040 /* string: increase buffer as needed */
1648#define SHF_INTERRUPT 0x0080 /* EINTR in read/write causes error */
1649/* Flags used internally */
1650#define SHF_STRING 0x0100 /* a string, not a file */
1651#define SHF_ALLOCS 0x0200 /* shf and shf->buf were alloc()ed */
1652#define SHF_ALLOCB 0x0400 /* shf->buf was alloc()ed */
1653#define SHF_ERROR 0x0800 /* read()/write() error */
1654#define SHF_EOF 0x1000 /* read eof (sticky) */
1655#define SHF_READING 0x2000 /* currently reading: rnleft,rp valid */
1656#define SHF_WRITING 0x4000 /* currently writing: wnleft,wp valid */
1657
1658
1659struct shf {
1660 Area *areap; /* area shf/buf were allocated in */
1661 unsigned char *rp; /* read: current position in buffer */
1662 unsigned char *wp; /* write: current position in buffer */
1663 unsigned char *buf; /* buffer */
Geremy Condra03ebf062011-10-12 18:17:24 -07001664 ssize_t bsize; /* actual size of buf */
1665 ssize_t rbsize; /* size of buffer (1 if SHF_UNBUF) */
1666 ssize_t rnleft; /* read: how much data left in buffer */
1667 ssize_t wbsize; /* size of buffer (0 if SHF_UNBUF) */
1668 ssize_t wnleft; /* write: how much space left in buffer */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001669 int flags; /* see SHF_* */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001670 int fd; /* file descriptor */
Geremy Condra03ebf062011-10-12 18:17:24 -07001671 int errnosv; /* saved value of errno after error */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001672};
1673
1674extern struct shf shf_iob[];
1675
1676struct table {
1677 Area *areap; /* area to allocate entries */
1678 struct tbl **tbls; /* hashed table items */
Geremy Condra03ebf062011-10-12 18:17:24 -07001679 size_t nfree; /* free table entries */
1680 uint8_t tshift; /* table size (2^tshift) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001681};
1682
Geremy Condra03ebf062011-10-12 18:17:24 -07001683/* table item */
1684struct tbl {
1685 /* Area to allocate from */
1686 Area *areap;
1687 /* value */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001688 union {
Geremy Condra03ebf062011-10-12 18:17:24 -07001689 char *s; /* string */
1690 mksh_ari_t i; /* integer */
1691 mksh_uari_t u; /* unsigned integer */
1692 int (*f)(const char **); /* built-in command */
1693 struct op *t; /* "function" tree */
1694 } val;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001695 union {
1696 struct tbl *array; /* array values */
1697 const char *fpath; /* temporary path to undef function */
1698 } u;
1699 union {
Geremy Condra03ebf062011-10-12 18:17:24 -07001700 int field; /* field with for -L/-R/-Z */
1701 int errnov; /* CEXEC/CTALIAS */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001702 } u2;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001703 union {
1704 uint32_t hval; /* hash(name) */
1705 uint32_t index; /* index for an array */
1706 } ua;
Geremy Condra03ebf062011-10-12 18:17:24 -07001707 /*
1708 * command type (see below), base (if INTEGER),
1709 * offset from val.s of value (if EXPORT)
1710 */
1711 int type;
1712 /* flags (see below) */
1713 uint32_t flag;
1714
1715 /* actually longer: name (variable length) */
1716 char name[4];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001717};
1718
Elliott Hughes77740fc2016-08-12 15:06:53 -07001719EXTERN struct tbl *vtemp;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001720/* set by isglobal(), global() and local() */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001721EXTERN bool last_lookup_was_array;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001722
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001723/* common flag bits */
1724#define ALLOC BIT(0) /* val.s has been allocated */
1725#define DEFINED BIT(1) /* is defined in block */
1726#define ISSET BIT(2) /* has value, vp->val.[si] */
1727#define EXPORT BIT(3) /* exported variable/function */
1728#define TRACE BIT(4) /* var: user flagged, func: execution tracing */
1729/* (start non-common flags at 8) */
1730/* flag bits used for variables */
1731#define SPECIAL BIT(8) /* PATH, IFS, SECONDS, etc */
1732#define INTEGER BIT(9) /* val.i contains integer value */
1733#define RDONLY BIT(10) /* read-only variable */
1734#define LOCAL BIT(11) /* for local typeset() */
1735#define ARRAY BIT(13) /* array */
1736#define LJUST BIT(14) /* left justify */
1737#define RJUST BIT(15) /* right justify */
1738#define ZEROFIL BIT(16) /* 0 filled if RJUSTIFY, strip 0s if LJUSTIFY */
1739#define LCASEV BIT(17) /* convert to lower case */
1740#define UCASEV_AL BIT(18) /* convert to upper case / autoload function */
1741#define INT_U BIT(19) /* unsigned integer */
Geremy Condra03ebf062011-10-12 18:17:24 -07001742#define INT_L BIT(20) /* long integer (no-op but used as magic) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001743#define IMPORT BIT(21) /* flag to typeset(): no arrays, must have = */
1744#define LOCAL_COPY BIT(22) /* with LOCAL - copy attrs from existing var */
1745#define EXPRINEVAL BIT(23) /* contents currently being evaluated */
1746#define EXPRLVALUE BIT(24) /* useable as lvalue (temp flag) */
1747#define AINDEX BIT(25) /* array index >0 = ua.index filled in */
1748#define ASSOC BIT(26) /* ARRAY ? associative : reference */
1749/* flag bits used for taliases/builtins/aliases/keywords/functions */
1750#define KEEPASN BIT(8) /* keep command assignments (eg, var=x cmd) */
1751#define FINUSE BIT(9) /* function being executed */
1752#define FDELETE BIT(10) /* function deleted while it was executing */
1753#define FKSH BIT(11) /* function defined with function x (vs x()) */
1754#define SPEC_BI BIT(12) /* a POSIX special builtin */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001755#define LOWER_BI BIT(13) /* (with LOW_BI) override even w/o flags */
1756#define LOW_BI BIT(14) /* external utility overrides built-in one */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001757#define DECL_UTIL BIT(15) /* is declaration utility */
1758#define DECL_FWDR BIT(16) /* is declaration utility forwarder */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001759
Geremy Condra03ebf062011-10-12 18:17:24 -07001760/*
1761 * Attributes that can be set by the user (used to decide if an unset
1762 * param should be repoted by set/typeset). Does not include ARRAY or
1763 * LOCAL.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001764 */
1765#define USERATTRIB (EXPORT|INTEGER|RDONLY|LJUST|RJUST|ZEROFIL|\
1766 LCASEV|UCASEV_AL|INT_U|INT_L)
1767
1768#define arrayindex(vp) ((unsigned long)((vp)->flag & AINDEX ? \
1769 (vp)->ua.index : 0))
1770
Elliott Hughes737fdce2014-08-07 12:59:26 -07001771enum namerefflag {
Geremy Condra03ebf062011-10-12 18:17:24 -07001772 SRF_NOP,
1773 SRF_ENABLE,
1774 SRF_DISABLE
Elliott Hughes737fdce2014-08-07 12:59:26 -07001775};
Geremy Condra03ebf062011-10-12 18:17:24 -07001776
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001777/* command types */
1778#define CNONE 0 /* undefined */
1779#define CSHELL 1 /* built-in */
1780#define CFUNC 2 /* function */
1781#define CEXEC 4 /* executable command */
1782#define CALIAS 5 /* alias */
1783#define CKEYWD 6 /* keyword */
1784#define CTALIAS 7 /* tracked alias */
1785
1786/* Flags for findcom()/comexec() */
1787#define FC_SPECBI BIT(0) /* special builtin */
Thorsten Glaser811a5752013-07-25 14:24:45 +00001788#define FC_FUNC BIT(1) /* function */
1789#define FC_NORMBI BIT(2) /* not special builtin */
1790#define FC_BI (FC_SPECBI | FC_NORMBI)
1791#define FC_PATH BIT(3) /* do path search */
1792#define FC_DEFPATH BIT(4) /* use default path in path search */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001793#define FC_WHENCE BIT(5) /* for use by command and whence */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001794
1795#define AF_ARGV_ALLOC 0x1 /* argv[] array allocated */
1796#define AF_ARGS_ALLOCED 0x2 /* argument strings allocated */
1797#define AI_ARGV(a, i) ((i) == 0 ? (a).argv[0] : (a).argv[(i) - (a).skip])
Geremy Condra03ebf062011-10-12 18:17:24 -07001798#define AI_ARGC(a) ((a).ai_argc - (a).skip)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001799
1800/* Argument info. Used for $#, $* for shell, functions, includes, etc. */
1801struct arg_info {
1802 const char **argv;
1803 int flags; /* AF_* */
Geremy Condra03ebf062011-10-12 18:17:24 -07001804 int ai_argc;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001805 int skip; /* first arg is argv[0], second is argv[1 + skip] */
1806};
1807
1808/*
1809 * activation record for function blocks
1810 */
1811struct block {
1812 Area area; /* area to allocate things */
1813 const char **argv;
1814 char *error; /* error handler */
1815 char *exit; /* exit handler */
1816 struct block *next; /* enclosing block */
1817 struct table vars; /* local variables */
1818 struct table funs; /* local functions */
1819 Getopt getopts_state;
1820 int argc;
1821 int flags; /* see BF_* */
1822};
1823
1824/* Values for struct block.flags */
1825#define BF_DOGETOPTS BIT(0) /* save/restore getopts state */
Elliott Hughes50012062015-03-10 22:22:24 -07001826#define BF_STOPENV BIT(1) /* do not export further */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001827
1828/*
1829 * Used by ktwalk() and ktnext() routines.
1830 */
1831struct tstate {
1832 struct tbl **next;
1833 ssize_t left;
1834};
1835
1836EXTERN struct table taliases; /* tracked aliases */
1837EXTERN struct table builtins; /* built-in commands */
1838EXTERN struct table aliases; /* aliases */
1839EXTERN struct table keywords; /* keywords */
1840#ifndef MKSH_NOPWNAM
1841EXTERN struct table homedirs; /* homedir() cache */
1842#endif
1843
1844struct builtin {
1845 const char *name;
1846 int (*func)(const char **);
1847};
1848
1849extern const struct builtin mkshbuiltins[];
1850
1851/* values for set_prompt() */
1852#define PS1 0 /* command */
1853#define PS2 1 /* command continuation */
1854
1855EXTERN char *path; /* copy of either PATH or def_path */
1856EXTERN const char *def_path; /* path to use if PATH not set */
1857EXTERN char *tmpdir; /* TMPDIR value */
1858EXTERN const char *prompt;
Elliott Hughesb27ce952015-04-21 13:39:18 -07001859EXTERN uint8_t cur_prompt; /* PS1 or PS2 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001860EXTERN int current_lineno; /* LINENO value */
1861
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001862/*
1863 * Description of a command or an operation on commands.
1864 */
1865struct op {
1866 const char **args; /* arguments to a command */
1867 char **vars; /* variable assignments */
1868 struct ioword **ioact; /* IO actions (eg, < > >>) */
1869 struct op *left, *right; /* descendents */
1870 char *str; /* word for case; identifier for for,
1871 * select, and functions;
1872 * path to execute for TEXEC;
1873 * time hook for TCOM.
1874 */
1875 int lineno; /* TCOM/TFUNC: LINENO for this */
1876 short type; /* operation type, see below */
Geremy Condra03ebf062011-10-12 18:17:24 -07001877 /* WARNING: newtp(), tcopy() use evalflags = 0 to clear union */
1878 union {
1879 /* TCOM: arg expansion eval() flags */
1880 short evalflags;
1881 /* TFUNC: function x (vs x()) */
1882 short ksh_func;
1883 /* TPAT: termination character */
1884 char charflag;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001885 } u;
1886};
1887
1888/* Tree.type values */
1889#define TEOF 0
1890#define TCOM 1 /* command */
1891#define TPAREN 2 /* (c-list) */
1892#define TPIPE 3 /* a | b */
1893#define TLIST 4 /* a ; b */
1894#define TOR 5 /* || */
1895#define TAND 6 /* && */
1896#define TBANG 7 /* ! */
1897#define TDBRACKET 8 /* [[ .. ]] */
1898#define TFOR 9
1899#define TSELECT 10
1900#define TCASE 11
1901#define TIF 12
1902#define TWHILE 13
1903#define TUNTIL 14
1904#define TELIF 15
1905#define TPAT 16 /* pattern in case */
1906#define TBRACE 17 /* {c-list} */
1907#define TASYNC 18 /* c & */
1908#define TFUNCT 19 /* function name { command; } */
1909#define TTIME 20 /* time pipeline */
1910#define TEXEC 21 /* fork/exec eval'd TCOM */
1911#define TCOPROC 22 /* coprocess |& */
1912
1913/*
1914 * prefix codes for words in command tree
1915 */
1916#define EOS 0 /* end of string */
1917#define CHAR 1 /* unquoted character */
1918#define QCHAR 2 /* quoted character */
1919#define COMSUB 3 /* $() substitution (0 terminated) */
1920#define EXPRSUB 4 /* $(()) substitution (0 terminated) */
1921#define OQUOTE 5 /* opening " or ' */
1922#define CQUOTE 6 /* closing " or ' */
1923#define OSUBST 7 /* opening ${ subst (followed by { or X) */
1924#define CSUBST 8 /* closing } of above (followed by } or X) */
1925#define OPAT 9 /* open pattern: *(, @(, etc. */
1926#define SPAT 10 /* separate pattern: | */
1927#define CPAT 11 /* close pattern: ) */
1928#define ADELIM 12 /* arbitrary delimiter: ${foo:2:3} ${foo/bar/baz} */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001929#define FUNSUB 14 /* ${ foo;} substitution (NUL terminated) */
Thorsten Glaser811a5752013-07-25 14:24:45 +00001930#define VALSUB 15 /* ${|foo;} substitution (NUL terminated) */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001931#define COMASUB 16 /* `…` substitution (COMSUB but expand aliases) */
1932#define FUNASUB 17 /* function substitution but expand aliases */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001933
1934/*
1935 * IO redirection
1936 */
1937struct ioword {
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001938 char *ioname; /* filename (unused if heredoc) */
Elliott Hughesb27ce952015-04-21 13:39:18 -07001939 char *delim; /* delimiter for <<, <<- */
1940 char *heredoc; /* content of heredoc */
1941 unsigned short ioflag; /* action (below) */
1942 short unit; /* unit (fd) affected */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001943};
1944
1945/* ioword.flag - type of redirection */
Geremy Condra03ebf062011-10-12 18:17:24 -07001946#define IOTYPE 0xF /* type: bits 0:3 */
1947#define IOREAD 0x1 /* < */
1948#define IOWRITE 0x2 /* > */
1949#define IORDWR 0x3 /* <>: todo */
1950#define IOHERE 0x4 /* << (here file) */
1951#define IOCAT 0x5 /* >> */
1952#define IODUP 0x6 /* <&/>& */
1953#define IOEVAL BIT(4) /* expand in << */
1954#define IOSKIP BIT(5) /* <<-, skip ^\t* */
1955#define IOCLOB BIT(6) /* >|, override -o noclobber */
1956#define IORDUP BIT(7) /* x<&y (as opposed to x>&y) */
Elliott Hughes47086262019-03-26 12:34:31 -07001957#define IODUPSELF BIT(8) /* x>&x (as opposed to x>&y) */
1958#define IONAMEXP BIT(9) /* name has been expanded */
1959#define IOBASH BIT(10) /* &> etc. */
1960#define IOHERESTR BIT(11) /* <<< (here string) */
1961#define IONDELIM BIT(12) /* null delimiter (<<) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001962
1963/* execute/exchild flags */
1964#define XEXEC BIT(0) /* execute without forking */
1965#define XFORK BIT(1) /* fork before executing */
1966#define XBGND BIT(2) /* command & */
1967#define XPIPEI BIT(3) /* input is pipe */
1968#define XPIPEO BIT(4) /* output is pipe */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001969#define XXCOM BIT(5) /* `...` command */
1970#define XPCLOSE BIT(6) /* exchild: close close_fd in parent */
1971#define XCCLOSE BIT(7) /* exchild: close close_fd in child */
1972#define XERROK BIT(8) /* non-zero exit ok (for set -e) */
1973#define XCOPROC BIT(9) /* starting a co-process */
1974#define XTIME BIT(10) /* timing TCOM command */
Geremy Condra03ebf062011-10-12 18:17:24 -07001975#define XPIPEST BIT(11) /* want PIPESTATUS */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001976
1977/*
1978 * flags to control expansion of words (assumed by t->evalflags to fit
1979 * in a short)
1980 */
1981#define DOBLANK BIT(0) /* perform blank interpretation */
1982#define DOGLOB BIT(1) /* expand [?* */
1983#define DOPAT BIT(2) /* quote *?[ */
1984#define DOTILDE BIT(3) /* normal ~ expansion (first char) */
1985#define DONTRUNCOMMAND BIT(4) /* do not run $(command) things */
1986#define DOASNTILDE BIT(5) /* assignment ~ expansion (after =, :) */
Geremy Condra03ebf062011-10-12 18:17:24 -07001987#define DOBRACE BIT(6) /* used by expand(): do brace expansion */
1988#define DOMAGIC BIT(7) /* used by expand(): string contains MAGIC */
1989#define DOTEMP BIT(8) /* dito: in word part of ${..[%#=?]..} */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001990#define DOVACHECK BIT(9) /* var assign check (for typeset, set, etc) */
1991#define DOMARKDIRS BIT(10) /* force markdirs behaviour */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001992#define DOTCOMEXEC BIT(11) /* not an eval flag, used by sh -c hack */
Elliott Hughes50012062015-03-10 22:22:24 -07001993#define DOSCALAR BIT(12) /* change field handling to non-list context */
1994#define DOHEREDOC BIT(13) /* change scalar handling to heredoc body */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001995#define DOHERESTR BIT(14) /* append a newline char */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001996
Geremy Condra03ebf062011-10-12 18:17:24 -07001997#define X_EXTRA 20 /* this many extra bytes in X string */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001998
1999typedef struct XString {
Elliott Hughes966dd552016-12-08 15:56:04 -08002000 /* beginning of string */
Elliott Hughes77740fc2016-08-12 15:06:53 -07002001 char *beg;
2002 /* length of allocated area, minus safety margin */
2003 size_t len;
2004 /* end of string */
2005 char *end;
2006 /* memory area used */
2007 Area *areap;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002008} XString;
2009
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002010/* initialise expandable string */
2011#define XinitN(xs, length, area) do { \
2012 (xs).len = (length); \
2013 (xs).areap = (area); \
2014 (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
2015 (xs).end = (xs).beg + (xs).len; \
2016} while (/* CONSTCOND */ 0)
2017#define Xinit(xs, xp, length, area) do { \
2018 XinitN((xs), (length), (area)); \
2019 (xp) = (xs).beg; \
2020} while (/* CONSTCOND */ 0)
2021
2022/* stuff char into string */
2023#define Xput(xs, xp, c) (*xp++ = (c))
2024
2025/* check if there are at least n bytes left */
2026#define XcheckN(xs, xp, n) do { \
Geremy Condra03ebf062011-10-12 18:17:24 -07002027 ssize_t more = ((xp) + (n)) - (xs).end; \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002028 if (more > 0) \
Elliott Hughes737fdce2014-08-07 12:59:26 -07002029 (xp) = Xcheck_grow(&(xs), (xp), (size_t)more); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002030} while (/* CONSTCOND */ 0)
2031
2032/* check for overflow, expand string */
2033#define Xcheck(xs, xp) XcheckN((xs), (xp), 1)
2034
2035/* free string */
2036#define Xfree(xs, xp) afree((xs).beg, (xs).areap)
2037
2038/* close, return string */
2039#define Xclose(xs, xp) aresize((xs).beg, (xp) - (xs).beg, (xs).areap)
2040
Elliott Hughes966dd552016-12-08 15:56:04 -08002041/* beginning of string */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002042#define Xstring(xs, xp) ((xs).beg)
2043
2044#define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */
2045#define Xlength(xs, xp) ((xp) - (xs).beg)
2046#define Xsize(xs, xp) ((xs).end - (xs).beg)
2047#define Xsavepos(xs, xp) ((xp) - (xs).beg)
2048#define Xrestpos(xs, xp, n) ((xs).beg + (n))
2049
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002050char *Xcheck_grow(XString *, const char *, size_t);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002051
2052/*
2053 * expandable vector of generic pointers
2054 */
2055
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002056typedef struct {
Elliott Hughes966dd552016-12-08 15:56:04 -08002057 /* beginning of allocated area */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002058 void **beg;
2059 /* currently used number of entries */
2060 size_t len;
2061 /* allocated number of entries */
2062 size_t siz;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002063} XPtrV;
2064
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002065#define XPinit(x, n) do { \
2066 (x).siz = (n); \
2067 (x).len = 0; \
2068 (x).beg = alloc2((x).siz, sizeof(void *), ATEMP); \
2069} while (/* CONSTCOND */ 0) \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002070
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002071#define XPput(x, p) do { \
2072 if ((x).len == (x).siz) { \
2073 (x).beg = aresize2((x).beg, (x).siz, \
2074 2 * sizeof(void *), ATEMP); \
2075 (x).siz <<= 1; \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002076 } \
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002077 (x).beg[(x).len++] = (p); \
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002078} while (/* CONSTCOND */ 0)
2079
2080#define XPptrv(x) ((x).beg)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002081#define XPsize(x) ((x).len)
Geremy Condra03ebf062011-10-12 18:17:24 -07002082#define XPclose(x) aresize2((x).beg, XPsize(x), sizeof(void *), ATEMP)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002083#define XPfree(x) afree((x).beg, ATEMP)
2084
Elliott Hughes966dd552016-12-08 15:56:04 -08002085/* for print_columns */
2086
2087struct columnise_opts {
2088 struct shf *shf;
2089 char linesep;
2090 bool do_last;
2091 bool prefcol;
2092};
2093
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002094/*
2095 * Lexer internals
2096 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002097
2098typedef struct source Source;
2099struct source {
Elliott Hughes77740fc2016-08-12 15:06:53 -07002100 /* input buffer */
2101 XString xs;
2102 /* memory area, also checked in reclaim() */
2103 Area *areap;
2104 /* stacked source */
2105 Source *next;
2106 /* input pointer */
2107 const char *str;
2108 /* start of current buffer */
2109 const char *start;
2110 /* input file name */
2111 const char *file;
2112 /* extra data */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002113 union {
Elliott Hughes77740fc2016-08-12 15:06:53 -07002114 /* string[] */
2115 const char **strv;
2116 /* shell file */
2117 struct shf *shf;
2118 /* alias (SF_HASALIAS) */
2119 struct tbl *tblp;
2120 /* (also for SREREAD) */
2121 char *freeme;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002122 } u;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002123 /* flags */
2124 int flags;
2125 /* input type */
2126 int type;
2127 /* line number */
2128 int line;
2129 /* line the error occurred on (0 if not set) */
2130 int errline;
2131 /* buffer for ungetsc() (SREREAD) and alias (SALIAS) */
2132 char ugbuf[2];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002133};
2134
2135/* Source.type values */
2136#define SEOF 0 /* input EOF */
2137#define SFILE 1 /* file input */
2138#define SSTDIN 2 /* read stdin */
2139#define SSTRING 3 /* string */
2140#define SWSTR 4 /* string without \n */
2141#define SWORDS 5 /* string[] */
2142#define SWORDSEP 6 /* string[] separator */
2143#define SALIAS 7 /* alias expansion */
2144#define SREREAD 8 /* read ahead to be re-scanned */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002145#define SSTRINGCMDLINE 9 /* string from "mksh -c ..." */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002146
2147/* Source.flags values */
2148#define SF_ECHO BIT(0) /* echo input to shlout */
2149#define SF_ALIAS BIT(1) /* faking space at end of alias */
2150#define SF_ALIASEND BIT(2) /* faking space at end of alias */
2151#define SF_TTY BIT(3) /* type == SSTDIN & it is a tty */
Geremy Condra03ebf062011-10-12 18:17:24 -07002152#define SF_HASALIAS BIT(4) /* u.tblp valid (SALIAS, SEOF) */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002153#define SF_MAYEXEC BIT(5) /* special sh -c optimisation hack */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002154
2155typedef union {
2156 int i;
2157 char *cp;
2158 char **wp;
2159 struct op *o;
2160 struct ioword *iop;
2161} YYSTYPE;
2162
2163/* If something is added here, add it to tokentab[] in syn.c as well */
2164#define LWORD 256
2165#define LOGAND 257 /* && */
2166#define LOGOR 258 /* || */
2167#define BREAK 259 /* ;; */
2168#define IF 260
2169#define THEN 261
2170#define ELSE 262
2171#define ELIF 263
2172#define FI 264
2173#define CASE 265
2174#define ESAC 266
2175#define FOR 267
2176#define SELECT 268
2177#define WHILE 269
2178#define UNTIL 270
2179#define DO 271
2180#define DONE 272
2181#define IN 273
2182#define FUNCTION 274
2183#define TIME 275
2184#define REDIR 276
2185#define MDPAREN 277 /* (( )) */
2186#define BANG 278 /* ! */
2187#define DBRACKET 279 /* [[ .. ]] */
2188#define COPROC 280 /* |& */
Geremy Condra03ebf062011-10-12 18:17:24 -07002189#define BRKEV 281 /* ;| */
2190#define BRKFT 282 /* ;& */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002191#define YYERRCODE 300
2192
2193/* flags to yylex */
2194#define CONTIN BIT(0) /* skip new lines to complete command */
2195#define ONEWORD BIT(1) /* single word for substitute() */
2196#define ALIAS BIT(2) /* recognise alias */
2197#define KEYWORD BIT(3) /* recognise keywords */
2198#define LETEXPR BIT(4) /* get expression inside (( )) */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002199#define CMDASN BIT(5) /* parse x[1 & 2] as one word, for typeset */
2200#define HEREDOC BIT(6) /* parsing a here document body */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002201#define ESACONLY BIT(7) /* only accept esac keyword */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002202#define CMDWORD BIT(8) /* parsing simple command (alias related) */
2203#define HEREDELIM BIT(9) /* parsing <<,<<- delimiter */
2204#define LQCHAR BIT(10) /* source string contains QCHAR */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002205
Geremy Condra03ebf062011-10-12 18:17:24 -07002206#define HERES 10 /* max number of << in line */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002207
Elliott Hughes23925bb2017-09-22 16:04:20 -07002208#ifdef MKSH_EBCDIC
2209#define CTRL_AT (0x00U)
2210#define CTRL_A (0x01U)
2211#define CTRL_B (0x02U)
2212#define CTRL_C (0x03U)
2213#define CTRL_D (0x37U)
2214#define CTRL_E (0x2DU)
2215#define CTRL_F (0x2EU)
2216#define CTRL_G (0x2FU)
2217#define CTRL_H (0x16U)
2218#define CTRL_I (0x05U)
2219#define CTRL_J (0x15U)
2220#define CTRL_K (0x0BU)
2221#define CTRL_L (0x0CU)
2222#define CTRL_M (0x0DU)
2223#define CTRL_N (0x0EU)
2224#define CTRL_O (0x0FU)
2225#define CTRL_P (0x10U)
2226#define CTRL_Q (0x11U)
2227#define CTRL_R (0x12U)
2228#define CTRL_S (0x13U)
2229#define CTRL_T (0x3CU)
2230#define CTRL_U (0x3DU)
2231#define CTRL_V (0x32U)
2232#define CTRL_W (0x26U)
2233#define CTRL_X (0x18U)
2234#define CTRL_Y (0x19U)
2235#define CTRL_Z (0x3FU)
2236#define CTRL_BO (0x27U)
2237#define CTRL_BK (0x1CU)
2238#define CTRL_BC (0x1DU)
2239#define CTRL_CA (0x1EU)
2240#define CTRL_US (0x1FU)
2241#define CTRL_QM (0x07U)
2242#else
2243#define CTRL_AT (0x00U)
2244#define CTRL_A (0x01U)
2245#define CTRL_B (0x02U)
2246#define CTRL_C (0x03U)
2247#define CTRL_D (0x04U)
2248#define CTRL_E (0x05U)
2249#define CTRL_F (0x06U)
2250#define CTRL_G (0x07U)
2251#define CTRL_H (0x08U)
2252#define CTRL_I (0x09U)
2253#define CTRL_J (0x0AU)
2254#define CTRL_K (0x0BU)
2255#define CTRL_L (0x0CU)
2256#define CTRL_M (0x0DU)
2257#define CTRL_N (0x0EU)
2258#define CTRL_O (0x0FU)
2259#define CTRL_P (0x10U)
2260#define CTRL_Q (0x11U)
2261#define CTRL_R (0x12U)
2262#define CTRL_S (0x13U)
2263#define CTRL_T (0x14U)
2264#define CTRL_U (0x15U)
2265#define CTRL_V (0x16U)
2266#define CTRL_W (0x17U)
2267#define CTRL_X (0x18U)
2268#define CTRL_Y (0x19U)
2269#define CTRL_Z (0x1AU)
2270#define CTRL_BO (0x1BU)
2271#define CTRL_BK (0x1CU)
2272#define CTRL_BC (0x1DU)
2273#define CTRL_CA (0x1EU)
2274#define CTRL_US (0x1FU)
2275#define CTRL_QM (0x7FU)
2276#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002277
Elliott Hughes23925bb2017-09-22 16:04:20 -07002278#define IDENT 64
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002279
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002280EXTERN Source *source; /* yyparse/yylex source */
Geremy Condra03ebf062011-10-12 18:17:24 -07002281EXTERN YYSTYPE yylval; /* result from yylex */
2282EXTERN struct ioword *heres[HERES], **herep;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002283EXTERN char ident[IDENT + 1];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002284
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002285EXTERN char **history; /* saved commands */
2286EXTERN char **histptr; /* last history item */
2287EXTERN mksh_ari_t histsize; /* history size */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002288
Elliott Hughes96b43632015-07-17 11:39:41 -07002289/* flags to histsave */
2290#define HIST_FLUSH 0
2291#define HIST_QUEUE 1
2292#define HIST_APPEND 2
2293#define HIST_STORE 3
2294#define HIST_NOTE 4
2295
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002296/* user and system time of last j_waitjed job */
2297EXTERN struct timeval j_usrtime, j_systime;
2298
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002299#define notok2mul(max, val, c) (((val) != 0) && ((c) != 0) && \
2300 (((max) / (c)) < (val)))
2301#define notok2add(max, val, c) ((val) > ((max) - (c)))
2302#define notoktomul(val, cnst) notok2mul(SIZE_MAX, (val), (cnst))
2303#define notoktoadd(val, cnst) notok2add(SIZE_MAX, (val), (cnst))
Geremy Condra03ebf062011-10-12 18:17:24 -07002304#define checkoktoadd(val, cnst) do { \
2305 if (notoktoadd((val), (cnst))) \
2306 internal_errorf(Tintovfl, (size_t)(val), \
2307 '+', (size_t)(cnst)); \
2308} while (/* CONSTCOND */ 0)
2309
2310
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002311/* lalloc.c */
2312void ainit(Area *);
2313void afreeall(Area *);
2314/* these cannot fail and can take NULL (not for ap) */
Geremy Condra03ebf062011-10-12 18:17:24 -07002315#define alloc(n, ap) aresize(NULL, (n), (ap))
2316#define alloc2(m, n, ap) aresize2(NULL, (m), (n), (ap))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002317void *aresize(void *, size_t, Area *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002318void *aresize2(void *, size_t, size_t, Area *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002319void afree(void *, Area *); /* can take NULL */
2320/* edit.c */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002321#ifndef MKSH_NO_CMDLINE_EDITING
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002322#ifndef MKSH_SMALL
2323int x_bind(const char *, const char *, bool, bool);
2324#else
2325int x_bind(const char *, const char *, bool);
2326#endif
2327void x_init(void);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002328#ifdef DEBUG_LEAKS
2329void x_done(void);
2330#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +00002331int x_read(char *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002332#endif
2333void x_mkraw(int, mksh_ttyst *, bool);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002334void x_initterm(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002335/* eval.c */
2336char *substitute(const char *, int);
2337char **eval(const char **, int);
2338char *evalstr(const char *cp, int);
2339char *evalonestr(const char *cp, int);
2340char *debunk(char *, const char *, size_t);
2341void expand(const char *, XPtrV *, int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002342int glob_str(char *, XPtrV *, bool);
Elliott Hughes50012062015-03-10 22:22:24 -07002343char *do_tilde(char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002344/* exec.c */
2345int execute(struct op * volatile, volatile int, volatile int * volatile);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002346int c_builtin(const char **);
2347struct tbl *get_builtin(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002348struct tbl *findfunc(const char *, uint32_t, bool);
2349int define(const char *, struct op *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002350const char *builtin(const char *, int (*)(const char **));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002351struct tbl *findcom(const char *, int);
Geremy Condra03ebf062011-10-12 18:17:24 -07002352void flushcom(bool);
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002353int search_access(const char *, int);
Geremy Condra03ebf062011-10-12 18:17:24 -07002354const char *search_path(const char *, const char *, int, int *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002355void pr_menu(const char * const *);
Elliott Hughes966dd552016-12-08 15:56:04 -08002356void pr_list(struct columnise_opts *, char * const *);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002357int herein(struct ioword *, char **);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002358/* expr.c */
2359int evaluate(const char *, mksh_ari_t *, int, bool);
2360int v_evaluate(struct tbl *, const char *, volatile int, bool);
2361/* UTF-8 stuff */
2362size_t utf_mbtowc(unsigned int *, const char *);
2363size_t utf_wctomb(char *, unsigned int);
2364int utf_widthadj(const char *, const char **);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002365size_t utf_mbswidth(const char *) MKSH_A_PURE;
Elliott Hughes77740fc2016-08-12 15:06:53 -07002366const char *utf_skipcols(const char *, int, int *);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002367size_t utf_ptradj(const char *) MKSH_A_PURE;
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002368#ifdef MIRBSD_BOOTFLOPPY
2369#define utf_wcwidth(i) wcwidth((wchar_t)(i))
2370#else
Elliott Hughes737fdce2014-08-07 12:59:26 -07002371int utf_wcwidth(unsigned int) MKSH_A_PURE;
Elliott Hughesfc0307d2016-02-02 15:26:47 -08002372#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07002373int ksh_access(const char *, int);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002374struct tbl *tempvar(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002375/* funcs.c */
2376int c_hash(const char **);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002377int c_pwd(const char **);
2378int c_print(const char **);
2379#ifdef MKSH_PRINTF_BUILTIN
2380int c_printf(const char **);
2381#endif
2382int c_whence(const char **);
2383int c_command(const char **);
2384int c_typeset(const char **);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002385bool valid_alias_name(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002386int c_alias(const char **);
2387int c_unalias(const char **);
2388int c_let(const char **);
2389int c_jobs(const char **);
2390#ifndef MKSH_UNEMPLOYED
2391int c_fgbg(const char **);
2392#endif
2393int c_kill(const char **);
2394void getopts_reset(int);
2395int c_getopts(const char **);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002396#ifndef MKSH_NO_CMDLINE_EDITING
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002397int c_bind(const char **);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002398#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002399int c_shift(const char **);
2400int c_umask(const char **);
2401int c_dot(const char **);
2402int c_wait(const char **);
2403int c_read(const char **);
2404int c_eval(const char **);
2405int c_trap(const char **);
2406int c_brkcont(const char **);
2407int c_exitreturn(const char **);
2408int c_set(const char **);
2409int c_unset(const char **);
2410int c_ulimit(const char **);
2411int c_times(const char **);
2412int timex(struct op *, int, volatile int *);
2413void timex_hook(struct op *, char ** volatile *);
2414int c_exec(const char **);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002415int c_test(const char **);
2416#if HAVE_MKNOD
2417int c_mknod(const char **);
2418#endif
2419int c_realpath(const char **);
2420int c_rename(const char **);
Geremy Condra03ebf062011-10-12 18:17:24 -07002421int c_cat(const char **);
2422int c_sleep(const char **);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002423/* histrap.c */
2424void init_histvec(void);
2425void hist_init(Source *);
2426#if HAVE_PERSISTENT_HISTORY
2427void hist_finish(void);
2428#endif
Elliott Hughes96b43632015-07-17 11:39:41 -07002429void histsave(int *, const char *, int, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002430#if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY
2431bool histsync(void);
2432#endif
2433int c_fc(const char **);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002434void sethistsize(mksh_ari_t);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002435#if HAVE_PERSISTENT_HISTORY
2436void sethistfile(const char *);
2437#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002438#if !defined(MKSH_NO_CMDLINE_EDITING) && !MKSH_S_NOVI
Elliott Hughes737fdce2014-08-07 12:59:26 -07002439char **histpos(void) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002440int histnum(int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002441#endif
Elliott Hughes737fdce2014-08-07 12:59:26 -07002442int findhist(int, int, const char *, bool) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002443char **hist_get_newest(bool);
2444void inittraps(void);
2445void alarm_init(void);
Elliott Hughes96b43632015-07-17 11:39:41 -07002446Trap *gettrap(const char *, bool, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002447void trapsig(int);
2448void intrcheck(void);
2449int fatal_trap_check(void);
2450int trap_pending(void);
2451void runtraps(int intr);
Geremy Condra03ebf062011-10-12 18:17:24 -07002452void runtrap(Trap *, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002453void cleartraps(void);
2454void restoresigs(void);
2455void settrap(Trap *, const char *);
Elliott Hughes966dd552016-12-08 15:56:04 -08002456bool block_pipe(void);
2457void restore_pipe(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002458int setsig(Trap *, sig_t, int);
2459void setexecsig(Trap *, int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002460#if HAVE_FLOCK || HAVE_LOCK_FCNTL
2461void mksh_lockfd(int);
2462void mksh_unlkfd(int);
2463#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002464/* jobs.c */
2465void j_init(void);
2466void j_exit(void);
2467#ifndef MKSH_UNEMPLOYED
2468void j_change(void);
2469#endif
2470int exchild(struct op *, int, volatile int *, int);
2471void startlast(void);
2472int waitlast(void);
2473int waitfor(const char *, int *);
2474int j_kill(const char *, int);
2475#ifndef MKSH_UNEMPLOYED
2476int j_resume(const char *, int);
2477#endif
Elliott Hughes737fdce2014-08-07 12:59:26 -07002478#if !defined(MKSH_UNEMPLOYED) && HAVE_GETSID
2479void j_suspend(void);
2480#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002481int j_jobs(const char *, int, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002482void j_notify(void);
2483pid_t j_async(void);
2484int j_stopped_running(void);
2485/* lex.c */
2486int yylex(int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002487void yyskiputf8bom(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002488void yyerror(const char *, ...)
2489 MKSH_A_NORETURN
Geremy Condra03ebf062011-10-12 18:17:24 -07002490 MKSH_A_FORMAT(__printf__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002491Source *pushs(int, Area *);
2492void set_prompt(int, Source *);
Thorsten Glaser811a5752013-07-25 14:24:45 +00002493int pprompt(const char *, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002494/* main.c */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002495int include(const char *, int, const char **, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002496int command(const char *, int);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002497int shell(Source * volatile, volatile int);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002498/* argument MUST NOT be 0 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002499void unwind(int) MKSH_A_NORETURN;
2500void newenv(int);
2501void quitenv(struct shf *);
2502void cleanup_parents_env(void);
2503void cleanup_proc_env(void);
2504void errorf(const char *, ...)
2505 MKSH_A_NORETURN
Geremy Condra03ebf062011-10-12 18:17:24 -07002506 MKSH_A_FORMAT(__printf__, 1, 2);
2507void errorfx(int, const char *, ...)
2508 MKSH_A_NORETURN
2509 MKSH_A_FORMAT(__printf__, 2, 3);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002510void warningf(bool, const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002511 MKSH_A_FORMAT(__printf__, 2, 3);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002512void bi_errorf(const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002513 MKSH_A_FORMAT(__printf__, 1, 2);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002514#define errorfz() errorf(NULL)
2515#define errorfxz(rc) errorfx((rc), NULL)
2516#define bi_errorfz() bi_errorf(NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002517void internal_errorf(const char *, ...)
2518 MKSH_A_NORETURN
Geremy Condra03ebf062011-10-12 18:17:24 -07002519 MKSH_A_FORMAT(__printf__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002520void internal_warningf(const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002521 MKSH_A_FORMAT(__printf__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002522void error_prefix(bool);
2523void shellf(const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002524 MKSH_A_FORMAT(__printf__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002525void shprintf(const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002526 MKSH_A_FORMAT(__printf__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002527int can_seek(int);
2528void initio(void);
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002529void recheck_ctype(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002530int ksh_dup2(int, int, bool);
2531short savefd(int);
2532void restfd(int, int);
2533void openpipe(int *);
2534void closepipe(int *);
2535int check_fd(const char *, int, const char **);
2536void coproc_init(void);
2537void coproc_read_close(int);
2538void coproc_readw_close(int);
2539void coproc_write_close(int);
2540int coproc_getfd(int, const char **);
2541void coproc_cleanup(int);
2542struct temp *maketemp(Area *, Temp_type, struct temp **);
Geremy Condra03ebf062011-10-12 18:17:24 -07002543void ktinit(Area *, struct table *, uint8_t);
2544struct tbl *ktscan(struct table *, const char *, uint32_t, struct tbl ***);
2545/* table, name (key) to search for, hash(n) */
2546#define ktsearch(tp, s, h) ktscan((tp), (s), (h), NULL)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002547struct tbl *ktenter(struct table *, const char *, uint32_t);
2548#define ktdelete(p) do { p->flag = 0; } while (/* CONSTCOND */ 0)
2549void ktwalk(struct tstate *, struct table *);
2550struct tbl *ktnext(struct tstate *);
2551struct tbl **ktsort(struct table *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002552#ifdef DF
2553void DF(const char *, ...)
2554 MKSH_A_FORMAT(__printf__, 1, 2);
2555#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002556/* misc.c */
Elliott Hughes737fdce2014-08-07 12:59:26 -07002557size_t option(const char *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002558char *getoptions(void);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002559void change_flag(enum sh_flag, int, bool);
Thorsten Glaser811a5752013-07-25 14:24:45 +00002560void change_xtrace(unsigned char, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002561int parse_args(const char **, int, bool *);
2562int getn(const char *, int *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002563int gmatchx(const char *, const char *, bool);
Elliott Hughes23925bb2017-09-22 16:04:20 -07002564bool has_globbing(const char *) MKSH_A_PURE;
2565int ascstrcmp(const void *, const void *) MKSH_A_PURE;
2566int ascpstrcmp(const void *, const void *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002567void ksh_getopt_reset(Getopt *, int);
2568int ksh_getopt(const char **, Getopt *, const char *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002569void print_value_quoted(struct shf *, const char *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002570char *quote_value(const char *);
Elliott Hughes966dd552016-12-08 15:56:04 -08002571void print_columns(struct columnise_opts *, unsigned int,
Elliott Hughes96b43632015-07-17 11:39:41 -07002572 void (*)(char *, size_t, unsigned int, const void *),
Elliott Hughes966dd552016-12-08 15:56:04 -08002573 const void *, size_t, size_t);
Elliott Hughesb27ce952015-04-21 13:39:18 -07002574void strip_nuls(char *, size_t)
2575 MKSH_A_BOUNDED(__string__, 1, 2);
Geremy Condra03ebf062011-10-12 18:17:24 -07002576ssize_t blocking_read(int, char *, size_t)
2577 MKSH_A_BOUNDED(__buffer__, 2, 3);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002578int reset_nonblock(int);
Geremy Condra03ebf062011-10-12 18:17:24 -07002579char *ksh_get_wd(void);
2580char *do_realpath(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002581void simplify_path(char *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002582void set_current_wd(const char *);
2583int c_cd(const char **);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002584#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
2585char *strdup_i(const char *, Area *);
2586char *strndup_i(const char *, size_t, Area *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002587#endif
2588int unbksl(bool, int (*)(void), void (*)(int));
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002589#ifdef __OS2__
2590/* os2.c */
2591void os2_init(int *, const char ***);
2592void setextlibpath(const char *, const char *);
2593int access_ex(int (*)(const char *, int), const char *, int);
2594int stat_ex(const char *, struct stat *);
2595const char *real_exec_name(const char *);
2596#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002597/* shf.c */
2598struct shf *shf_open(const char *, int, int, int);
2599struct shf *shf_fdopen(int, int, struct shf *);
2600struct shf *shf_reopen(int, int, struct shf *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002601struct shf *shf_sopen(char *, ssize_t, int, struct shf *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002602int shf_close(struct shf *);
2603int shf_fdclose(struct shf *);
2604char *shf_sclose(struct shf *);
2605int shf_flush(struct shf *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002606ssize_t shf_read(char *, ssize_t, struct shf *);
2607char *shf_getse(char *, ssize_t, struct shf *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002608int shf_getchar(struct shf *s);
2609int shf_ungetc(int, struct shf *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002610#if defined(MKSH_SMALL) && !defined(MKSH_SMALL_BUT_FAST)
Geremy Condra03ebf062011-10-12 18:17:24 -07002611int shf_getc(struct shf *);
2612int shf_putc(int, struct shf *);
2613#else
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002614#define shf_getc shf_getc_i
2615#define shf_putc shf_putc_i
Geremy Condra03ebf062011-10-12 18:17:24 -07002616#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002617int shf_putchar(int, struct shf *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002618ssize_t shf_puts(const char *, struct shf *);
2619ssize_t shf_write(const char *, ssize_t, struct shf *);
2620ssize_t shf_fprintf(struct shf *, const char *, ...)
2621 MKSH_A_FORMAT(__printf__, 2, 3);
2622ssize_t shf_snprintf(char *, ssize_t, const char *, ...)
2623 MKSH_A_FORMAT(__printf__, 3, 4)
2624 MKSH_A_BOUNDED(__string__, 1, 2);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002625char *shf_smprintf(const char *, ...)
Geremy Condra03ebf062011-10-12 18:17:24 -07002626 MKSH_A_FORMAT(__printf__, 1, 2);
2627ssize_t shf_vfprintf(struct shf *, const char *, va_list)
2628 MKSH_A_FORMAT(__printf__, 2, 0);
Elliott Hughes23925bb2017-09-22 16:04:20 -07002629void set_ifs(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002630/* syn.c */
2631void initkeywords(void);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002632struct op *compile(Source *, bool, bool);
Geremy Condra03ebf062011-10-12 18:17:24 -07002633bool parse_usec(const char *, struct timeval *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002634char *yyrecursive(int);
2635void yyrecursive_pop(bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002636/* tree.c */
Geremy Condra03ebf062011-10-12 18:17:24 -07002637void fptreef(struct shf *, int, const char *, ...);
2638char *snptreef(char *, ssize_t, const char *, ...);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002639struct op *tcopy(struct op *, Area *);
2640char *wdcopy(const char *, Area *);
2641const char *wdscan(const char *, int);
Geremy Condra03ebf062011-10-12 18:17:24 -07002642#define WDS_TPUTS BIT(0) /* tputS (dumpwdvar) mode */
Geremy Condra03ebf062011-10-12 18:17:24 -07002643char *wdstrip(const char *, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002644void tfree(struct op *, Area *);
Elliott Hughes47086262019-03-26 12:34:31 -07002645void dumpchar(struct shf *, unsigned char);
Geremy Condra03ebf062011-10-12 18:17:24 -07002646void dumptree(struct shf *, struct op *);
2647void dumpwdvar(struct shf *, const char *);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002648void dumpioact(struct shf *shf, struct op *t);
Geremy Condra03ebf062011-10-12 18:17:24 -07002649void vistree(char *, size_t, struct op *)
2650 MKSH_A_BOUNDED(__string__, 1, 2);
2651void fpFUNCTf(struct shf *, int, bool, const char *, struct op *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002652/* var.c */
2653void newblock(void);
2654void popblock(void);
2655void initvar(void);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002656struct block *varsearch(struct block *, struct tbl **, const char *, uint32_t);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002657struct tbl *global(const char *);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002658struct tbl *isglobal(const char *, bool);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002659struct tbl *local(const char *, bool);
2660char *str_val(struct tbl *);
2661int setstr(struct tbl *, const char *, int);
2662struct tbl *setint_v(struct tbl *, struct tbl *, bool);
2663void setint(struct tbl *, mksh_ari_t);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002664void setint_n(struct tbl *, mksh_ari_t, int);
2665struct tbl *typeset(const char *, uint32_t, uint32_t, int, int);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002666void unset(struct tbl *, int);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002667const char *skip_varname(const char *, bool) MKSH_A_PURE;
2668const char *skip_wdvarname(const char *, bool) MKSH_A_PURE;
2669int is_wdvarname(const char *, bool) MKSH_A_PURE;
2670int is_wdvarassign(const char *) MKSH_A_PURE;
Geremy Condra03ebf062011-10-12 18:17:24 -07002671struct tbl *arraysearch(struct tbl *, uint32_t);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002672char **makenv(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002673void change_winsz(void);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002674size_t array_ref_len(const char *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002675char *arrayname(const char *);
2676mksh_uari_t set_array(const char *, bool, const char **);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002677uint32_t hash(const void *) MKSH_A_PURE;
2678uint32_t chvt_rndsetup(const void *, size_t) MKSH_A_PURE;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002679mksh_ari_t rndget(void);
Thorsten Glaser811a5752013-07-25 14:24:45 +00002680void rndset(unsigned long);
Elliott Hughes737fdce2014-08-07 12:59:26 -07002681void rndpush(const void *);
Elliott Hughes77740fc2016-08-12 15:06:53 -07002682void record_match(const char *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002683
2684enum Test_op {
Geremy Condra03ebf062011-10-12 18:17:24 -07002685 /* non-operator */
2686 TO_NONOP = 0,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002687 /* unary operators */
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002688 TO_STNZE, TO_STZER, TO_ISSET, TO_OPTION,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002689 TO_FILAXST,
2690 TO_FILEXST,
2691 TO_FILREG, TO_FILBDEV, TO_FILCDEV, TO_FILSYM, TO_FILFIFO, TO_FILSOCK,
2692 TO_FILCDF, TO_FILID, TO_FILGID, TO_FILSETG, TO_FILSTCK, TO_FILUID,
2693 TO_FILRD, TO_FILGZ, TO_FILTT, TO_FILSETU, TO_FILWR, TO_FILEX,
2694 /* binary operators */
2695 TO_STEQL, TO_STNEQ, TO_STLT, TO_STGT, TO_INTEQ, TO_INTNE, TO_INTGT,
2696 TO_INTGE, TO_INTLT, TO_INTLE, TO_FILEQ, TO_FILNT, TO_FILOT,
2697 /* not an operator */
2698 TO_NONNULL /* !TO_NONOP */
2699};
2700typedef enum Test_op Test_op;
2701
2702/* Used by Test_env.isa() (order important - used to index *_tokens[] arrays) */
2703enum Test_meta {
2704 TM_OR, /* -o or || */
2705 TM_AND, /* -a or && */
2706 TM_NOT, /* ! */
2707 TM_OPAREN, /* ( */
2708 TM_CPAREN, /* ) */
2709 TM_UNOP, /* unary operator */
2710 TM_BINOP, /* binary operator */
2711 TM_END /* end of input */
2712};
2713typedef enum Test_meta Test_meta;
2714
2715#define TEF_ERROR BIT(0) /* set if we've hit an error */
2716#define TEF_DBRACKET BIT(1) /* set if [[ .. ]] test */
2717
2718typedef struct test_env {
2719 union {
Geremy Condra03ebf062011-10-12 18:17:24 -07002720 const char **wp; /* used by ptest_* */
2721 XPtrV *av; /* used by dbtestp_* */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002722 } pos;
Geremy Condra03ebf062011-10-12 18:17:24 -07002723 const char **wp_end; /* used by ptest_* */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002724 Test_op (*isa)(struct test_env *, Test_meta);
2725 const char *(*getopnd) (struct test_env *, Test_op, bool);
2726 int (*eval)(struct test_env *, Test_op, const char *, const char *, bool);
2727 void (*error)(struct test_env *, int, const char *);
Geremy Condra03ebf062011-10-12 18:17:24 -07002728 int flags; /* TEF_* */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002729} Test_env;
2730
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002731extern const char * const dbtest_tokens[];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002732
Elliott Hughes737fdce2014-08-07 12:59:26 -07002733Test_op test_isop(Test_meta, const char *) MKSH_A_PURE;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002734int test_eval(Test_env *, Test_op, const char *, const char *, bool);
2735int test_parse(Test_env *);
2736
Elliott Hughes737fdce2014-08-07 12:59:26 -07002737/* tty_fd is not opened O_BINARY, it's thus never read/written */
Geremy Condra03ebf062011-10-12 18:17:24 -07002738EXTERN int tty_fd E_INIT(-1); /* dup'd tty file descriptor */
2739EXTERN bool tty_devtty; /* true if tty_fd is from /dev/tty */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002740EXTERN mksh_ttyst tty_state; /* saved tty state */
2741EXTERN bool tty_hasstate; /* true if tty_state is valid */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002742
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00002743extern int tty_init_fd(void); /* initialise tty_fd, tty_devtty */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002744
Elliott Hughes96b43632015-07-17 11:39:41 -07002745#ifdef __OS2__
Elliott Hughes96b43632015-07-17 11:39:41 -07002746#define binopen2(path,flags) __extension__({ \
2747 int binopen2_fd = open((path), (flags) | O_BINARY); \
2748 if (binopen2_fd >= 0) \
2749 setmode(binopen2_fd, O_BINARY); \
2750 (binopen2_fd); \
2751})
2752#define binopen3(path,flags,mode) __extension__({ \
2753 int binopen3_fd = open((path), (flags) | O_BINARY, (mode)); \
2754 if (binopen3_fd >= 0) \
2755 setmode(binopen3_fd, O_BINARY); \
2756 (binopen3_fd); \
2757})
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002758#else
2759#define binopen2(path,flags) open((path), (flags) | O_BINARY)
2760#define binopen3(path,flags,mode) open((path), (flags) | O_BINARY, (mode))
2761#endif
2762
2763#ifdef MKSH_DOSPATH
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002764#define mksh_drvltr(s) __extension__({ \
2765 const char *mksh_drvltr_s = (s); \
2766 (ctype(mksh_drvltr_s[0], C_ALPHA) && mksh_drvltr_s[1] == ':'); \
2767})
Elliott Hughes96b43632015-07-17 11:39:41 -07002768#define mksh_abspath(s) __extension__({ \
2769 const char *mksh_abspath_s = (s); \
Elliott Hughes966dd552016-12-08 15:56:04 -08002770 (mksh_cdirsep(mksh_abspath_s[0]) || \
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002771 (mksh_drvltr(mksh_abspath_s) && \
2772 mksh_cdirsep(mksh_abspath_s[2]))); \
Elliott Hughes96b43632015-07-17 11:39:41 -07002773})
Elliott Hughes966dd552016-12-08 15:56:04 -08002774#define mksh_cdirsep(c) __extension__({ \
2775 char mksh_cdirsep_c = (c); \
2776 (mksh_cdirsep_c == '/' || mksh_cdirsep_c == '\\'); \
2777})
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002778#define mksh_sdirsep(s) strpbrk((s), "/\\")
2779#define mksh_vdirsep(s) __extension__({ \
2780 const char *mksh_vdirsep_s = (s); \
2781 (((mksh_drvltr(mksh_vdirsep_s) && \
2782 !mksh_cdirsep(mksh_vdirsep_s[2])) ? (!0) : \
2783 (mksh_sdirsep(mksh_vdirsep_s) != NULL)) && \
2784 (strcmp(mksh_vdirsep_s, T_builtin) != 0)); \
Elliott Hughesa3c3f962017-04-12 16:52:30 -07002785})
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002786int getdrvwd(char **, unsigned int);
Elliott Hughes96b43632015-07-17 11:39:41 -07002787#else
Elliott Hughesdd4abe02018-02-05 15:55:19 -08002788#define mksh_abspath(s) (ord((s)[0]) == ORD('/'))
2789#define mksh_cdirsep(c) (ord(c) == ORD('/'))
Elliott Hughes966dd552016-12-08 15:56:04 -08002790#define mksh_sdirsep(s) strchr((s), '/')
2791#define mksh_vdirsep(s) vstrchr((s), '/')
Elliott Hughes96b43632015-07-17 11:39:41 -07002792#endif
2793
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002794/* be sure not to interfere with anyone else's idea about EXTERN */
2795#ifdef EXTERN_DEFINED
2796# undef EXTERN_DEFINED
2797# undef EXTERN
2798#endif
Geremy Condra03ebf062011-10-12 18:17:24 -07002799#undef E_INIT
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07002800
2801#endif /* !MKSH_INCLUDES_ONLY */