blob: 1286b07177e2d875845f0553f2d3716d8a64ce21 [file] [log] [blame]
Elliott Hughesfc0307d2016-02-02 15:26:47 -08001/* $OpenBSD: main.c,v 1.57 2015/09/10 22:48:58 nicm Exp $ */
Elliott Hughes56b517d2014-10-06 11:30:44 -07002/* $OpenBSD: tty.c,v 1.10 2014/08/10 02:44:26 guenther Exp $ */
Elliott Hughesfc0307d2016-02-02 15:26:47 -08003/* $OpenBSD: io.c,v 1.26 2015/09/11 08:00:27 guenther Exp $ */
4/* $OpenBSD: table.c,v 1.16 2015/09/01 13:12:31 tedu Exp $ */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07005
6/*-
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00007 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
Elliott Hughesa3c3f962017-04-12 16:52:30 -07008 * 2011, 2012, 2013, 2014, 2015, 2016, 2017
Elliott Hughesfc0307d2016-02-02 15:26:47 -08009 * mirabilos <m@mirbsd.org>
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070010 *
11 * Provided that these terms and disclaimer and all copyright notices
12 * are retained or reproduced in an accompanying document, permission
13 * is granted to deal in this work without restriction, including un-
14 * limited rights to use, publicly perform, distribute, sell, modify,
15 * merge, give away, or sublicence.
16 *
17 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
18 * the utmost extent permitted by applicable law, neither express nor
19 * implied; without malicious intent or gross negligence. In no event
20 * may a licensor, author or contributor be held liable for indirect,
21 * direct, other damage, loss, or other issues arising in any way out
22 * of dealing in the work, even if advised of the possibility of such
23 * damage or existence of a defect, except proven that it results out
24 * of said person's immediate fault when using the work as intended.
25 */
26
Geremy Condra03ebf062011-10-12 18:17:24 -070027#define EXTERN
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070028#include "sh.h"
29
30#if HAVE_LANGINFO_CODESET
31#include <langinfo.h>
32#endif
33#if HAVE_SETLOCALE_CTYPE
34#include <locale.h>
35#endif
36
Elliott Hughesa3c3f962017-04-12 16:52:30 -070037__RCSID("$MirOS: src/bin/mksh/main.c,v 1.332 2017/04/12 16:01:45 tg Exp $");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070038
39extern char **environ;
40
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070041#ifndef MKSHRC_PATH
42#define MKSHRC_PATH "~/.mkshrc"
43#endif
44
45#ifndef MKSH_DEFAULT_TMPDIR
Elliott Hughes96b43632015-07-17 11:39:41 -070046#define MKSH_DEFAULT_TMPDIR MKSH_UNIXROOT "/tmp"
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070047#endif
48
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000049static uint8_t isuc(const char *);
50static int main_init(int, const char *[], Source **, struct block **);
Geremy Condra03ebf062011-10-12 18:17:24 -070051void chvt_reinit(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070052static void reclaim(void);
53static void remove_temps(struct temp *);
Geremy Condra03ebf062011-10-12 18:17:24 -070054static mksh_uari_t rndsetup(void);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070055#ifdef SIGWINCH
56static void x_sigwinch(int);
57#endif
58
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070059static const char initsubs[] =
Elliott Hughes966dd552016-12-08 15:56:04 -080060 "${PS2=> }"
61 "${PS3=#? }"
62 "${PS4=+ }"
63 "${SECONDS=0}"
64 "${TMOUT=0}"
65 "${EPOCHREALTIME=}";
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070066
67static const char *initcoms[] = {
Geremy Condra03ebf062011-10-12 18:17:24 -070068 Ttypeset, "-r", initvsn, NULL,
Elliott Hughes77740fc2016-08-12 15:06:53 -070069 Ttypeset, "-x", "HOME", TPATH, TSHELL, NULL,
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000070 Ttypeset, "-i10", "COLUMNS", "LINES", "SECONDS", "TMOUT", NULL,
Geremy Condra03ebf062011-10-12 18:17:24 -070071 Talias,
Elliott Hughesa3c3f962017-04-12 16:52:30 -070072 "integer=\\\\builtin typeset -i",
73 "local=\\\\builtin typeset",
Geremy Condra03ebf062011-10-12 18:17:24 -070074 /* not "alias -t --": hash -r needs to work */
Elliott Hughesa3c3f962017-04-12 16:52:30 -070075 "hash=\\\\builtin alias -t",
76 "type=\\\\builtin whence -v",
77 "autoload=\\\\builtin typeset -fu",
78 "functions=\\\\builtin typeset -f",
79 "history=\\\\builtin fc -l",
80 "nameref=\\\\builtin typeset -n",
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070081 "nohup=nohup ",
Elliott Hughesa3c3f962017-04-12 16:52:30 -070082 "r=\\\\builtin fc -e -",
83 "login=\\\\builtin exec login",
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070084 NULL,
85 /* this is what AT&T ksh seems to track, with the addition of emacs */
Geremy Condra03ebf062011-10-12 18:17:24 -070086 Talias, "-tU",
Elliott Hughes96b43632015-07-17 11:39:41 -070087 Tcat, "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
Elliott Hughes77740fc2016-08-12 15:06:53 -070088 "make", "mv", "pr", "rm", "sed", Tsh, "vi", "who", NULL,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070089 NULL
90};
91
Geremy Condra03ebf062011-10-12 18:17:24 -070092static const char *restr_com[] = {
Elliott Hughes77740fc2016-08-12 15:06:53 -070093 Ttypeset, "-r", TPATH, "ENV", TSHELL, NULL
Geremy Condra03ebf062011-10-12 18:17:24 -070094};
95
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +000096static bool initio_done;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -070097
Geremy Condra03ebf062011-10-12 18:17:24 -070098/* top-level parsing and execution environment */
99static struct env env;
100struct env *e = &env;
101
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700102/* compile-time assertions */
103#define cta(name, expr) struct cta_ ## name { char t[(expr) ? 1 : -1]; }
104
105/* this one should be defined by the standard */
106cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
107 (sizeof(unsigned char) == 1));
108cta(char_is_8_bits, ((CHAR_BIT) == 8) && ((int)(unsigned char)0xFF == 0xFF) &&
109 ((int)(unsigned char)0x100 == 0) && ((int)(unsigned char)(int)-1 == 0xFF));
110/* the next assertion is probably not really needed */
111cta(short_is_2_char, sizeof(short) == 2);
112cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
113/* the next assertion is probably not really needed */
114cta(int_is_4_char, sizeof(int) == 4);
115cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
116
117cta(long_ge_int, sizeof(long) >= sizeof(int));
118cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
119
120#ifndef MKSH_LEGACY_MODE
121/* the next assertion is probably not really needed */
122cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
123/* but this is */
124cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
125/* the next assertion is probably not really needed */
126cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
127/* but the next three are; we REQUIRE unsigned integer wraparound */
128cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
129cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
130cta(uari_wrap_32_bit,
131 (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
132 (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
133#endif
134/* these are always required */
135cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
136cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
137/* we require these to have the precisely same size and assume 2s complement */
138cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
139
140cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
141cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
142cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
143/* our formatting routines assume this */
144cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
145cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
146
Geremy Condra03ebf062011-10-12 18:17:24 -0700147static mksh_uari_t
148rndsetup(void)
149{
150 register uint32_t h;
151 struct {
152 ALLOC_ITEM alloc_INT;
153 void *dataptr, *stkptr, *mallocptr;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000154#if defined(__GLIBC__) && (__GLIBC__ >= 2)
Geremy Condra03ebf062011-10-12 18:17:24 -0700155 sigjmp_buf jbuf;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000156#endif
Geremy Condra03ebf062011-10-12 18:17:24 -0700157 struct timeval tv;
Geremy Condra03ebf062011-10-12 18:17:24 -0700158 } *bufptr;
159 char *cp;
160
Elliott Hughes77740fc2016-08-12 15:06:53 -0700161 cp = alloc(sizeof(*bufptr) - sizeof(ALLOC_ITEM), APERM);
Elliott Hughes966dd552016-12-08 15:56:04 -0800162 /* clear the allocated space, for valgrind and to avoid UB */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700163 memset(cp, 0, sizeof(*bufptr) - sizeof(ALLOC_ITEM));
Geremy Condra03ebf062011-10-12 18:17:24 -0700164 /* undo what alloc() did to the malloc result address */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700165 bufptr = (void *)(cp - sizeof(ALLOC_ITEM));
Geremy Condra03ebf062011-10-12 18:17:24 -0700166 /* PIE or something similar provides us with deltas here */
167 bufptr->dataptr = &rndsetupstate;
168 /* ASLR in at least Windows, Linux, some BSDs */
169 bufptr->stkptr = &bufptr;
170 /* randomised malloc in BSD (and possibly others) */
171 bufptr->mallocptr = bufptr;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000172#if defined(__GLIBC__) && (__GLIBC__ >= 2)
Geremy Condra03ebf062011-10-12 18:17:24 -0700173 /* glibc pointer guard */
174 sigsetjmp(bufptr->jbuf, 1);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000175#endif
176 /* introduce variation (and yes, second arg MBZ for portability) */
177 mksh_TIME(bufptr->tv);
Geremy Condra03ebf062011-10-12 18:17:24 -0700178
Elliott Hughes77740fc2016-08-12 15:06:53 -0700179#ifdef MKSH_ALLOC_CATCH_UNDERRUNS
180 mprotect(((char *)bufptr) + 4096, 4096, PROT_READ | PROT_WRITE);
181#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +0000182 h = chvt_rndsetup(bufptr, sizeof(*bufptr));
183
184 afree(cp, APERM);
185 return ((mksh_uari_t)h);
186}
187
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700188void
189chvt_reinit(void)
190{
191 kshpid = procpid = getpid();
192 ksheuid = geteuid();
193 kshpgrp = getpgrp();
194 kshppid = getppid();
195}
196
Geremy Condra03ebf062011-10-12 18:17:24 -0700197static const char *empty_argv[] = {
Elliott Hughes77740fc2016-08-12 15:06:53 -0700198 Tmksh, NULL
Geremy Condra03ebf062011-10-12 18:17:24 -0700199};
200
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000201static uint8_t
202isuc(const char *cx) {
203 char *cp, *x;
204 uint8_t rv = 0;
205
206 if (!cx || !*cx)
207 return (0);
208
209 /* uppercase a string duplicate */
210 strdupx(x, cx, ATEMP);
211 cp = x;
212 while ((*cp = ksh_toupper(*cp)))
213 ++cp;
214
215 /* check for UTF-8 */
216 if (strstr(x, "UTF-8") || strstr(x, "UTF8"))
217 rv = 1;
218
219 /* free copy and out */
220 afree(x, ATEMP);
221 return (rv);
222}
223
224static int
225main_init(int argc, const char *argv[], Source **sp, struct block **lp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700226{
227 int argi, i;
Geremy Condra03ebf062011-10-12 18:17:24 -0700228 Source *s = NULL;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700229 struct block *l;
Elliott Hughes56b517d2014-10-06 11:30:44 -0700230 unsigned char restricted_shell, errexit, utf_flag;
Geremy Condra03ebf062011-10-12 18:17:24 -0700231 char *cp;
232 const char *ccp, **wp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700233 struct tbl *vp;
234 struct stat s_stdin;
235#if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
Geremy Condra03ebf062011-10-12 18:17:24 -0700236 ssize_t k;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700237#endif
238
Elliott Hughes96b43632015-07-17 11:39:41 -0700239#ifdef __OS2__
240 for (i = 0; i < 3; ++i)
241 if (!isatty(i))
242 setmode(i, O_BINARY);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700243
244 os2_init(&argc, &argv);
Elliott Hughes96b43632015-07-17 11:39:41 -0700245#endif
246
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700247 /* do things like getpgrp() et al. */
248 chvt_reinit();
249
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800250 /* make sure argv[] is sane, for weird OSes */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700251 if (!*argv) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700252 argv = empty_argv;
253 argc = 1;
254 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700255 kshname = argv[0];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700256
Geremy Condra03ebf062011-10-12 18:17:24 -0700257 /* initialise permanent Area */
258 ainit(&aperm);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700259 /* max. name length: -2147483648 = 11 (+ NUL) */
260 vtemp = alloc(offsetof(struct tbl, name[0]) + 12, APERM);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700261
262 /* set up base environment */
Geremy Condra03ebf062011-10-12 18:17:24 -0700263 env.type = E_NONE;
264 ainit(&env.area);
265 /* set up global l->vars and l->funs */
266 newblock();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700267
268 /* Do this first so output routines (eg, errorf, shellf) can work */
269 initio();
270
Geremy Condra03ebf062011-10-12 18:17:24 -0700271 /* determine the basename (without '-' or path) of the executable */
272 ccp = kshname;
Elliott Hughes966dd552016-12-08 15:56:04 -0800273 goto begin_parsing_kshname;
Geremy Condra03ebf062011-10-12 18:17:24 -0700274 while ((i = ccp[argi++])) {
Elliott Hughes966dd552016-12-08 15:56:04 -0800275 if (mksh_cdirsep(i)) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700276 ccp += argi;
Elliott Hughes966dd552016-12-08 15:56:04 -0800277 begin_parsing_kshname:
Geremy Condra03ebf062011-10-12 18:17:24 -0700278 argi = 0;
279 if (*ccp == '-')
280 ++ccp;
281 }
282 }
283 if (!*ccp)
284 ccp = empty_argv[0];
285
Elliott Hughes737fdce2014-08-07 12:59:26 -0700286 /*
287 * Turn on nohup by default. (AT&T ksh does not have a nohup
288 * option - it always sends the hup).
289 */
290 Flag(FNOHUP) = 1;
291
292 /*
293 * Turn on brace expansion by default. AT&T kshs that have
294 * alternation always have it on.
295 */
296 Flag(FBRACEEXPAND) = 1;
297
298 /*
299 * Turn on "set -x" inheritance by default.
300 */
301 Flag(FXTRACEREC) = 1;
302
Geremy Condra03ebf062011-10-12 18:17:24 -0700303 /* define built-in commands and see if we were called as one */
304 ktinit(APERM, &builtins,
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800305 /* currently up to 54 builtins: 75% of 128 = 2^7 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000306 7);
Geremy Condra03ebf062011-10-12 18:17:24 -0700307 for (i = 0; mkshbuiltins[i].name != NULL; i++)
308 if (!strcmp(ccp, builtin(mkshbuiltins[i].name,
309 mkshbuiltins[i].func)))
310 Flag(FAS_BUILTIN) = 1;
311
312 if (!Flag(FAS_BUILTIN)) {
313 /* check for -T option early */
314 argi = parse_args(argv, OF_FIRSTTIME, NULL);
315 if (argi < 0)
316 return (1);
317
Thorsten Glaser811a5752013-07-25 14:24:45 +0000318#if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)
319 /* are we called as -sh or /bin/sh or so? */
Elliott Hughes96b43632015-07-17 11:39:41 -0700320 if (!strcmp(ccp, "sh" MKSH_EXE_EXT)) {
Thorsten Glaser811a5752013-07-25 14:24:45 +0000321 /* either also turns off braceexpand */
322#ifdef MKSH_BINSHPOSIX
323 /* enable better POSIX conformance */
324 change_flag(FPOSIX, OF_FIRSTTIME, true);
325#endif
Geremy Condra03ebf062011-10-12 18:17:24 -0700326#ifdef MKSH_BINSHREDUCED
Thorsten Glaser811a5752013-07-25 14:24:45 +0000327 /* enable kludge/compat mode */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000328 change_flag(FSH, OF_FIRSTTIME, true);
Geremy Condra03ebf062011-10-12 18:17:24 -0700329#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +0000330 }
331#endif
Geremy Condra03ebf062011-10-12 18:17:24 -0700332 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700333
334 initvar();
335
336 initctypes();
337
338 inittraps();
339
340 coproc_init();
341
342 /* set up variable and command dictionaries */
Geremy Condra03ebf062011-10-12 18:17:24 -0700343 ktinit(APERM, &taliases, 0);
344 ktinit(APERM, &aliases, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700345#ifndef MKSH_NOPWNAM
Geremy Condra03ebf062011-10-12 18:17:24 -0700346 ktinit(APERM, &homedirs, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700347#endif
348
349 /* define shell keywords */
350 initkeywords();
351
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700352 init_histvec();
353
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000354 /* initialise tty size before importing environment */
355 change_winsz();
356
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700357#ifdef _PATH_DEFPATH
358 def_path = _PATH_DEFPATH;
359#else
360#ifdef _CS_PATH
Geremy Condra03ebf062011-10-12 18:17:24 -0700361 if ((k = confstr(_CS_PATH, NULL, 0)) > 0 &&
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700362 confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
363 def_path = cp;
364 else
365#endif
366 /*
367 * this is uniform across all OSes unless it
Elliott Hughes77740fc2016-08-12 15:06:53 -0700368 * breaks somewhere hard; don't try to optimise,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700369 * e.g. add stuff for Interix or remove /usr
370 * for HURD, because e.g. Debian GNU/HURD is
371 * "keeping a regular /usr"; this is supposed
372 * to be a sane 'basic' default PATH
373 */
Elliott Hughes96b43632015-07-17 11:39:41 -0700374 def_path = MKSH_UNIXROOT "/bin" MKSH_PATHSEPS
375 MKSH_UNIXROOT "/usr/bin" MKSH_PATHSEPS
376 MKSH_UNIXROOT "/sbin" MKSH_PATHSEPS
377 MKSH_UNIXROOT "/usr/sbin";
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700378#endif
379
Geremy Condra03ebf062011-10-12 18:17:24 -0700380 /*
381 * Set PATH to def_path (will set the path global variable).
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700382 * (import of environment below will probably change this setting).
383 */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700384 vp = global(TPATH);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700385 /* setstr can't fail here */
386 setstr(vp, def_path, KSH_RETURN_ERROR);
387
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000388#ifndef MKSH_NO_CMDLINE_EDITING
Geremy Condra03ebf062011-10-12 18:17:24 -0700389 /*
390 * Set edit mode to emacs by default, may be overridden
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700391 * by the environment or the user. Also, we want tab completion
Geremy Condra03ebf062011-10-12 18:17:24 -0700392 * on in vi by default.
393 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000394 change_flag(FEMACS, OF_SPECIAL, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700395#if !MKSH_S_NOVI
396 Flag(FVITABCOMPLETE) = 1;
397#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000398#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700399
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700400 /* import environment */
Elliott Hughes737fdce2014-08-07 12:59:26 -0700401 if (environ != NULL) {
402 wp = (const char **)environ;
403 while (*wp != NULL) {
404 rndpush(*wp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700405 typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
Elliott Hughes737fdce2014-08-07 12:59:26 -0700406 ++wp;
407 }
408 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700409
Sandeep Patilc2036112017-04-12 18:02:50 -0700410 /* override default PATH regardless of environment */
411#ifdef MKSH_DEFPATH_OVERRIDE
412 vp = global(TPATH);
413 setstr(vp, MKSH_DEFPATH_OVERRIDE, KSH_RETURN_ERROR);
414#endif
415
Geremy Condra03ebf062011-10-12 18:17:24 -0700416 /* for security */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700417 typeset("IFS= \t\n", 0, 0, 0, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700418
419 /* assign default shell variable values */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700420 typeset("PATHSEP=" MKSH_PATHSEPS, 0, 0, 0, 0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700421 substitute(initsubs, 0);
422
423 /* Figure out the current working directory and set $PWD */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700424 vp = global(TPWD);
Geremy Condra03ebf062011-10-12 18:17:24 -0700425 cp = str_val(vp);
426 /* Try to use existing $PWD if it is valid */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700427 set_current_wd((mksh_abspath(cp) && test_eval(NULL, TO_FILEQ, cp,
428 Tdot, true)) ? cp : NULL);
Geremy Condra03ebf062011-10-12 18:17:24 -0700429 if (current_wd[0])
430 simplify_path(current_wd);
431 /* Only set pwd if we know where we are or if it had a bogus value */
432 if (current_wd[0] || *cp)
433 /* setstr can't fail here */
434 setstr(vp, current_wd, KSH_RETURN_ERROR);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700435
436 for (wp = initcoms; *wp != NULL; wp++) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700437 c_builtin(wp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700438 while (*wp != NULL)
439 wp++;
440 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000441 setint_n(global("OPTIND"), 1, 10);
Geremy Condra03ebf062011-10-12 18:17:24 -0700442
443 kshuid = getuid();
444 kshgid = getgid();
445 kshegid = getegid();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700446
447 safe_prompt = ksheuid ? "$ " : "# ";
448 vp = global("PS1");
449 /* Set PS1 if unset or we are root and prompt doesn't contain a # */
450 if (!(vp->flag & ISSET) ||
451 (!ksheuid && !strchr(str_val(vp), '#')))
452 /* setstr can't fail here */
453 setstr(vp, safe_prompt, KSH_RETURN_ERROR);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000454 setint_n((vp = global("BASHPID")), 0, 10);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700455 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000456 setint_n((vp = global("PGRP")), (mksh_uari_t)kshpgrp, 10);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700457 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000458 setint_n((vp = global("PPID")), (mksh_uari_t)kshppid, 10);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700459 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000460 setint_n((vp = global("USER_ID")), (mksh_uari_t)ksheuid, 10);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700461 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000462 setint_n((vp = global("KSHUID")), (mksh_uari_t)kshuid, 10);
Geremy Condra03ebf062011-10-12 18:17:24 -0700463 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000464 setint_n((vp = global("KSHEGID")), (mksh_uari_t)kshegid, 10);
Geremy Condra03ebf062011-10-12 18:17:24 -0700465 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000466 setint_n((vp = global("KSHGID")), (mksh_uari_t)kshgid, 10);
Geremy Condra03ebf062011-10-12 18:17:24 -0700467 vp->flag |= INT_U;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000468 setint_n((vp = global("RANDOM")), rndsetup(), 10);
469 vp->flag |= INT_U;
470 setint_n((vp_pipest = global("PIPESTATUS")), 0, 10);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700471
472 /* Set this before parsing arguments */
Elliott Hughesfc0307d2016-02-02 15:26:47 -0800473 Flag(FPRIVILEGED) = (kshuid != ksheuid || kshgid != kshegid) ? 2 : 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700474
475 /* this to note if monitor is set on command line (see below) */
476#ifndef MKSH_UNEMPLOYED
477 Flag(FMONITOR) = 127;
478#endif
479 /* this to note if utf-8 mode is set on command line (see below) */
480 UTFMODE = 2;
481
Geremy Condra03ebf062011-10-12 18:17:24 -0700482 if (!Flag(FAS_BUILTIN)) {
483 argi = parse_args(argv, OF_CMDLINE, NULL);
484 if (argi < 0)
485 return (1);
486 }
487
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700488 /* process this later only, default to off (hysterical raisins) */
489 utf_flag = UTFMODE;
490 UTFMODE = 0;
491
Geremy Condra03ebf062011-10-12 18:17:24 -0700492 if (Flag(FAS_BUILTIN)) {
493 /* auto-detect from environment variables, always */
494 utf_flag = 3;
495 } else if (Flag(FCOMMAND)) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000496 s = pushs(SSTRINGCMDLINE, ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700497 if (!(s->start = s->str = argv[argi++]))
Elliott Hughes77740fc2016-08-12 15:06:53 -0700498 errorf(Tf_optfoo, "", "", 'c', Treq_arg);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000499 while (*s->str) {
500 if (*s->str != ' ' && ctype(*s->str, C_QUOTE))
501 break;
502 s->str++;
503 }
504 if (!*s->str)
505 s->flags |= SF_MAYEXEC;
506 s->str = s->start;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700507#ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
508 /* compatibility to MidnightBSD 0.1 /bin/sh (kludge) */
509 if (Flag(FSH) && argv[argi] && !strcmp(argv[argi], "--"))
510 ++argi;
511#endif
512 if (argv[argi])
513 kshname = argv[argi++];
514 } else if (argi < argc && !Flag(FSTDIN)) {
515 s = pushs(SFILE, ATEMP);
Elliott Hughes96b43632015-07-17 11:39:41 -0700516#ifdef __OS2__
517 /*
518 * A bug in OS/2 extproc (like shebang) handling makes
519 * it not pass the full pathname of a script, so we need
520 * to search for it. This changes the behaviour of a
521 * simple "mksh foo", but can't be helped.
522 */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700523 s->file = argv[argi++];
524 if (search_access(s->file, X_OK) != 0)
525 s->file = search_path(s->file, path, X_OK, NULL);
Elliott Hughes96b43632015-07-17 11:39:41 -0700526 if (!s->file || !*s->file)
527 s->file = argv[argi - 1];
528#else
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700529 s->file = argv[argi++];
Elliott Hughes96b43632015-07-17 11:39:41 -0700530#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700531 s->u.shf = shf_open(s->file, O_RDONLY, 0,
532 SHF_MAPHI | SHF_CLEXEC);
533 if (s->u.shf == NULL) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700534 shl_stdout_ok = false;
Elliott Hughes77740fc2016-08-12 15:06:53 -0700535 warningf(true, Tf_sD_s, s->file, cstrerror(errno));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700536 /* mandated by SUSv4 */
537 exstat = 127;
538 unwind(LERROR);
539 }
540 kshname = s->file;
541 } else {
542 Flag(FSTDIN) = 1;
543 s = pushs(SSTDIN, ATEMP);
544 s->file = "<stdin>";
545 s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
546 NULL);
547 if (isatty(0) && isatty(2)) {
548 Flag(FTALKING) = Flag(FTALKING_I) = 1;
549 /* The following only if isatty(0) */
550 s->flags |= SF_TTY;
551 s->u.shf->flags |= SHF_INTERRUPT;
552 s->file = NULL;
553 }
554 }
555
556 /* this bizarreness is mandated by POSIX */
557 if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
558 Flag(FTALKING))
559 reset_nonblock(0);
560
561 /* initialise job control */
562 j_init();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000563 /* do this after j_init() which calls tty_init_state() */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700564 if (Flag(FTALKING)) {
565 if (utf_flag == 2) {
566#ifndef MKSH_ASSUME_UTF8
Geremy Condra03ebf062011-10-12 18:17:24 -0700567 /* auto-detect from locale or environment */
568 utf_flag = 4;
Elliott Hughes737fdce2014-08-07 12:59:26 -0700569#else /* this may not be an #elif */
570#if MKSH_ASSUME_UTF8
Geremy Condra03ebf062011-10-12 18:17:24 -0700571 utf_flag = 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700572#else
Geremy Condra03ebf062011-10-12 18:17:24 -0700573 /* always disable UTF-8 (for interactive) */
574 utf_flag = 0;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700575#endif
Elliott Hughes737fdce2014-08-07 12:59:26 -0700576#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700577 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000578#ifndef MKSH_NO_CMDLINE_EDITING
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700579 x_init();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000580#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700581 }
582
583#ifdef SIGWINCH
584 sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
585 setsig(&sigtraps[SIGWINCH], x_sigwinch,
586 SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
587#endif
588
589 l = e->loc;
Geremy Condra03ebf062011-10-12 18:17:24 -0700590 if (Flag(FAS_BUILTIN)) {
591 l->argc = argc;
592 l->argv = argv;
593 l->argv[0] = ccp;
594 } else {
595 l->argc = argc - argi;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000596 /*
597 * allocate a new array because otherwise, when we modify
598 * it in-place, ps(1) output changes; the meaning of argc
599 * here is slightly different as it excludes kshname, and
600 * we add a trailing NULL sentinel as well
601 */
602 l->argv = alloc2(l->argc + 2, sizeof(void *), APERM);
Geremy Condra03ebf062011-10-12 18:17:24 -0700603 l->argv[0] = kshname;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000604 memcpy(&l->argv[1], &argv[argi], l->argc * sizeof(void *));
605 l->argv[l->argc + 1] = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -0700606 getopts_reset(1);
607 }
608
609 /* divine the initial state of the utf8-mode Flag */
Geremy Condra03ebf062011-10-12 18:17:24 -0700610 ccp = null;
611 switch (utf_flag) {
612
613 /* auto-detect from locale or environment */
614 case 4:
615#if HAVE_SETLOCALE_CTYPE
616 ccp = setlocale(LC_CTYPE, "");
617#if HAVE_LANGINFO_CODESET
618 if (!isuc(ccp))
619 ccp = nl_langinfo(CODESET);
620#endif
621 if (!isuc(ccp))
622 ccp = null;
Geremy Condra03ebf062011-10-12 18:17:24 -0700623#endif
Elliott Hughes966dd552016-12-08 15:56:04 -0800624 /* FALLTHROUGH */
Geremy Condra03ebf062011-10-12 18:17:24 -0700625
626 /* auto-detect from environment */
627 case 3:
628 /* these were imported from environ earlier */
629 if (ccp == null)
630 ccp = str_val(global("LC_ALL"));
631 if (ccp == null)
632 ccp = str_val(global("LC_CTYPE"));
633 if (ccp == null)
634 ccp = str_val(global("LANG"));
635 UTFMODE = isuc(ccp);
636 break;
637
638 /* not set on command line, not FTALKING */
639 case 2:
640 /* unknown values */
641 default:
642 utf_flag = 0;
643 /* FALLTHROUGH */
644
645 /* known values */
646 case 1:
647 case 0:
648 UTFMODE = utf_flag;
649 break;
650 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700651
652 /* Disable during .profile/ENV reading */
Elliott Hughes56b517d2014-10-06 11:30:44 -0700653 restricted_shell = Flag(FRESTRICTED);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700654 Flag(FRESTRICTED) = 0;
655 errexit = Flag(FERREXIT);
656 Flag(FERREXIT) = 0;
657
Geremy Condra03ebf062011-10-12 18:17:24 -0700658 /*
659 * Do this before profile/$ENV so that if it causes problems in them,
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700660 * user will know why things broke.
661 */
662 if (!current_wd[0] && Flag(FTALKING))
Geremy Condra03ebf062011-10-12 18:17:24 -0700663 warningf(false, "can't determine current directory");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700664
Elliott Hughes737fdce2014-08-07 12:59:26 -0700665 if (Flag(FLOGIN))
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000666 include(MKSH_SYSTEM_PROFILE, 0, NULL, true);
Elliott Hughes737fdce2014-08-07 12:59:26 -0700667 if (!Flag(FPRIVILEGED)) {
668 if (Flag(FLOGIN))
669 include(substitute("$HOME/.profile", 0), 0, NULL, true);
670 if (Flag(FTALKING)) {
671 cp = substitute(substitute("${ENV:-" MKSHRC_PATH "}",
672 0), DOTILDE);
673 if (cp[0] != '\0')
674 include(cp, 0, NULL, true);
675 }
676 } else {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000677 include(MKSH_SUID_PROFILE, 0, NULL, true);
Elliott Hughes737fdce2014-08-07 12:59:26 -0700678 /* turn off -p if not set explicitly */
679 if (Flag(FPRIVILEGED) != 1)
680 change_flag(FPRIVILEGED, OF_INTERNAL, false);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700681 }
682
Elliott Hughes56b517d2014-10-06 11:30:44 -0700683 if (restricted_shell) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700684 c_builtin(restr_com);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700685 /* After typeset command... */
686 Flag(FRESTRICTED) = 1;
687 }
688 Flag(FERREXIT) = errexit;
689
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000690 if (Flag(FTALKING) && s)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700691 hist_init(s);
Geremy Condra03ebf062011-10-12 18:17:24 -0700692 else
693 /* set after ENV */
694 Flag(FTRACKALL) = 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700695
Geremy Condra03ebf062011-10-12 18:17:24 -0700696 alarm_init();
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700697
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000698 *sp = s;
699 *lp = l;
Geremy Condra03ebf062011-10-12 18:17:24 -0700700 return (0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700701}
702
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000703/* this indirection barrier reduces stack usage during normal operation */
704
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700705int
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000706main(int argc, const char *argv[])
707{
708 int rv;
709 Source *s;
710 struct block *l;
711
712 if ((rv = main_init(argc, argv, &s, &l)) == 0) {
713 if (Flag(FAS_BUILTIN)) {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700714 rv = c_builtin(l->argv);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000715 } else {
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700716 shell(s, 0);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000717 /* NOTREACHED */
718 }
719 }
720 return (rv);
721}
722
723int
724include(const char *name, int argc, const char **argv, bool intr_ok)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700725{
726 Source *volatile s = NULL;
727 struct shf *shf;
728 const char **volatile old_argv;
729 volatile int old_argc;
730 int i;
731
732 shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
733 if (shf == NULL)
734 return (-1);
735
736 if (argv) {
737 old_argv = e->loc->argv;
738 old_argc = e->loc->argc;
739 } else {
740 old_argv = NULL;
741 old_argc = 0;
742 }
743 newenv(E_INCL);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000744 if ((i = kshsetjmp(e->jbuf))) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700745 quitenv(s ? s->u.shf : NULL);
746 if (old_argv) {
747 e->loc->argv = old_argv;
748 e->loc->argc = old_argc;
749 }
750 switch (i) {
751 case LRETURN:
752 case LERROR:
Geremy Condra03ebf062011-10-12 18:17:24 -0700753 /* see below */
754 return (exstat & 0xFF);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700755 case LINTR:
Geremy Condra03ebf062011-10-12 18:17:24 -0700756 /*
757 * intr_ok is set if we are including .profile or $ENV.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700758 * If user ^Cs out, we don't want to kill the shell...
759 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000760 if (intr_ok && ((exstat & 0xFF) - 128) != SIGTERM)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700761 return (1);
762 /* FALLTHROUGH */
763 case LEXIT:
764 case LLEAVE:
765 case LSHELL:
766 unwind(i);
767 /* NOTREACHED */
768 default:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700769 internal_errorf(Tunexpected_type, Tunwind, Tsource, i);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700770 /* NOTREACHED */
771 }
772 }
773 if (argv) {
774 e->loc->argv = argv;
775 e->loc->argc = argc;
776 }
777 s = pushs(SFILE, ATEMP);
778 s->u.shf = shf;
779 strdupx(s->file, name, ATEMP);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700780 i = shell(s, 1);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700781 quitenv(s->u.shf);
782 if (old_argv) {
783 e->loc->argv = old_argv;
784 e->loc->argc = old_argc;
785 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700786 /* & 0xff to ensure value not -1 */
787 return (i & 0xFF);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700788}
789
790/* spawn a command into a shell optionally keeping track of the line number */
791int
792command(const char *comm, int line)
793{
Elliott Hughes77740fc2016-08-12 15:06:53 -0700794 Source *s, *sold = source;
795 int rv;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700796
797 s = pushs(SSTRING, ATEMP);
798 s->start = s->str = comm;
799 s->line = line;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700800 rv = shell(s, 1);
Elliott Hughes77740fc2016-08-12 15:06:53 -0700801 source = sold;
802 return (rv);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700803}
804
805/*
806 * run the commands from the input source, returning status.
807 */
808int
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700809shell(Source * volatile s, volatile int level)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700810{
811 struct op *t;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000812 volatile bool wastty = tobool(s->flags & SF_TTY);
813 volatile uint8_t attempts = 13;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700814 volatile bool interactive = (level == 0) && Flag(FTALKING);
Geremy Condra03ebf062011-10-12 18:17:24 -0700815 volatile bool sfirst = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700816 Source *volatile old_source = source;
817 int i;
818
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700819 newenv(level == 2 ? E_EVAL : E_PARSE);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700820 if (interactive)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000821 really_exit = false;
822 switch ((i = kshsetjmp(e->jbuf))) {
823 case 0:
824 break;
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700825 case LBREAK:
826 case LCONTIN:
827 if (level != 2) {
828 source = old_source;
829 quitenv(NULL);
830 internal_errorf(Tf_cant_s, Tshell,
831 i == LBREAK ? Tbreak : Tcontinue);
832 /* NOTREACHED */
833 }
834 /* assert: interactive == false */
835 /* FALLTHROUGH */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000836 case LINTR:
837 /* we get here if SIGINT not caught or ignored */
838 case LERROR:
839 case LSHELL:
840 if (interactive) {
841 if (i == LINTR)
842 shellf("\n");
843 /*
844 * Reset any eof that was read as part of a
845 * multiline command.
846 */
847 if (Flag(FIGNOREEOF) && s->type == SEOF && wastty)
848 s->type = SSTDIN;
849 /*
850 * Used by exit command to get back to
851 * top level shell. Kind of strange since
852 * interactive is set if we are reading from
853 * a tty, but to have stopped jobs, one only
854 * needs FMONITOR set (not FTALKING/SF_TTY)...
855 */
856 /* toss any input we have so far */
857 yyrecursive_pop(true);
858 s->start = s->str = null;
859 retrace_info = NULL;
860 herep = heres;
861 break;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700862 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000863 /* FALLTHROUGH */
864 case LEXIT:
865 case LLEAVE:
866 case LRETURN:
867 source = old_source;
868 quitenv(NULL);
869 /* keep on going */
870 unwind(i);
871 /* NOTREACHED */
872 default:
873 source = old_source;
874 quitenv(NULL);
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700875 internal_errorf(Tunexpected_type, Tunwind, Tshell, i);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000876 /* NOTREACHED */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700877 }
Geremy Condra03ebf062011-10-12 18:17:24 -0700878 while (/* CONSTCOND */ 1) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700879 if (trap)
880 runtraps(0);
881
882 if (s->next == NULL) {
883 if (Flag(FVERBOSE))
884 s->flags |= SF_ECHO;
885 else
886 s->flags &= ~SF_ECHO;
887 }
888 if (interactive) {
889 j_notify();
890 set_prompt(PS1, s);
891 }
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700892 t = compile(s, sfirst, true);
Elliott Hughes96b43632015-07-17 11:39:41 -0700893 if (interactive)
894 histsave(&s->line, NULL, HIST_FLUSH, true);
Geremy Condra03ebf062011-10-12 18:17:24 -0700895 sfirst = false;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000896 if (!t)
897 goto source_no_tree;
898 if (t->type == TEOF) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700899 if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700900 shellf("Use 'exit' to leave mksh\n");
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700901 s->type = SSTDIN;
902 } else if (wastty && !really_exit &&
903 j_stopped_running()) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000904 really_exit = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700905 s->type = SSTDIN;
906 } else {
Geremy Condra03ebf062011-10-12 18:17:24 -0700907 /*
908 * this for POSIX which says EXIT traps
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700909 * shall be taken in the environment
910 * immediately after the last command
911 * executed.
912 */
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700913 if (level == 0)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700914 unwind(LEXIT);
915 break;
916 }
Elliott Hughes737fdce2014-08-07 12:59:26 -0700917 } else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000918 t->u.evalflags |= DOTCOMEXEC;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000919 if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
920 exstat = execute(t, 0, NULL) & 0xFF;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700921
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000922 if (t->type != TEOF && interactive && really_exit)
923 really_exit = false;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700924
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000925 source_no_tree:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700926 reclaim();
927 }
928 quitenv(NULL);
929 source = old_source;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000930 return (exstat & 0xFF);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700931}
932
933/* return to closest error handler or shell(), exit if none found */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000934/* note: i MUST NOT be 0 */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700935void
936unwind(int i)
937{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000938 /*
939 * This is a kludge. We need to restore everything that was
940 * changed in the new environment, see cid 1005090337C7A669439
941 * and 10050903386452ACBF1, but fail to even save things most of
942 * the time. funcs.c:c_eval() changes FERREXIT temporarily to 0,
943 * which needs to be restored thus (related to Debian #696823).
944 * We did not save the shell flags, so we use a special or'd
945 * value here... this is mostly to clean up behind *other*
946 * callers of unwind(LERROR) here; exec.c has the regular case.
947 */
948 if (Flag(FERREXIT) & 0x80) {
949 /* GNU bash does not run this trapsig */
950 trapsig(ksh_SIGERR);
951 Flag(FERREXIT) &= ~0x80;
952 }
953
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700954 /* ordering for EXIT vs ERR is a bit odd (this is what AT&T ksh does) */
Thorsten Glaser811a5752013-07-25 14:24:45 +0000955 if (i == LEXIT || ((i == LERROR || i == LINTR) &&
956 sigtraps[ksh_SIGEXIT].trap &&
957 (!Flag(FTALKING) || Flag(FERREXIT)))) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700958 ++trap_nested;
959 runtrap(&sigtraps[ksh_SIGEXIT], trap_nested == 1);
960 --trap_nested;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700961 i = LLEAVE;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000962 } else if (Flag(FERREXIT) == 1 && (i == LERROR || i == LINTR)) {
Geremy Condra03ebf062011-10-12 18:17:24 -0700963 ++trap_nested;
964 runtrap(&sigtraps[ksh_SIGERR], trap_nested == 1);
965 --trap_nested;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700966 i = LLEAVE;
967 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000968
Geremy Condra03ebf062011-10-12 18:17:24 -0700969 while (/* CONSTCOND */ 1) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700970 switch (e->type) {
971 case E_PARSE:
972 case E_FUNC:
973 case E_INCL:
974 case E_LOOP:
975 case E_ERRH:
Elliott Hughesa3c3f962017-04-12 16:52:30 -0700976 case E_EVAL:
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +0000977 kshlongjmp(e->jbuf, i);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -0700978 /* NOTREACHED */
979 case E_NONE:
980 if (i == LINTR)
981 e->flags |= EF_FAKE_SIGDIE;
982 /* FALLTHROUGH */
983 default:
984 quitenv(NULL);
985 }
986 }
987}
988
989void
990newenv(int type)
991{
992 struct env *ep;
993 char *cp;
994
995 /*
996 * struct env includes ALLOC_ITEM for alignment constraints
997 * so first get the actually used memory, then assign it
998 */
Elliott Hughes77740fc2016-08-12 15:06:53 -0700999 cp = alloc(sizeof(struct env) - sizeof(ALLOC_ITEM), ATEMP);
Geremy Condra03ebf062011-10-12 18:17:24 -07001000 /* undo what alloc() did to the malloc result address */
Elliott Hughes77740fc2016-08-12 15:06:53 -07001001 ep = (void *)(cp - sizeof(ALLOC_ITEM));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001002 /* initialise public members of struct env (not the ALLOC_ITEM) */
1003 ainit(&ep->area);
1004 ep->oenv = e;
1005 ep->loc = e->loc;
1006 ep->savefd = NULL;
1007 ep->temps = NULL;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001008 ep->yyrecursive_statep = NULL;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001009 ep->type = type;
1010 ep->flags = 0;
1011 /* jump buffer is invalid because flags == 0 */
1012 e = ep;
1013}
1014
1015void
1016quitenv(struct shf *shf)
1017{
1018 struct env *ep = e;
1019 char *cp;
1020 int fd;
1021
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001022 yyrecursive_pop(true);
1023 while (ep->oenv && ep->oenv->loc != ep->loc)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001024 popblock();
1025 if (ep->savefd != NULL) {
1026 for (fd = 0; fd < NUFILE; fd++)
1027 /* if ep->savefd[fd] < 0, means fd was closed */
1028 if (ep->savefd[fd])
1029 restfd(fd, ep->savefd[fd]);
Geremy Condra03ebf062011-10-12 18:17:24 -07001030 if (ep->savefd[2])
1031 /* Clear any write errors */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001032 shf_reopen(2, SHF_WR, shl_out);
1033 }
Geremy Condra03ebf062011-10-12 18:17:24 -07001034 /*
1035 * Bottom of the stack.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001036 * Either main shell is exiting or cleanup_parents_env() was called.
1037 */
1038 if (ep->oenv == NULL) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001039#ifdef DEBUG_LEAKS
1040 int i;
1041#endif
1042
Geremy Condra03ebf062011-10-12 18:17:24 -07001043 if (ep->type == E_NONE) {
1044 /* Main shell exiting? */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001045#if HAVE_PERSISTENT_HISTORY
1046 if (Flag(FTALKING))
1047 hist_finish();
1048#endif
1049 j_exit();
1050 if (ep->flags & EF_FAKE_SIGDIE) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001051 int sig = (exstat & 0xFF) - 128;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001052
Geremy Condra03ebf062011-10-12 18:17:24 -07001053 /*
1054 * ham up our death a bit (AT&T ksh
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001055 * only seems to do this for SIGTERM)
1056 * Don't do it for SIGQUIT, since we'd
1057 * dump a core..
1058 */
1059 if ((sig == SIGINT || sig == SIGTERM) &&
1060 (kshpgrp == kshpid)) {
1061 setsig(&sigtraps[sig], SIG_DFL,
1062 SS_RESTORE_CURR | SS_FORCE);
1063 kill(0, sig);
1064 }
1065 }
1066 }
1067 if (shf)
1068 shf_close(shf);
1069 reclaim();
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001070#ifdef DEBUG_LEAKS
1071#ifndef MKSH_NO_CMDLINE_EDITING
1072 x_done();
1073#endif
Thorsten Glaser811a5752013-07-25 14:24:45 +00001074#ifndef MKSH_NOPROSPECTOFWORK
1075 /* block at least SIGCHLD during/after afreeall */
1076 sigprocmask(SIG_BLOCK, &sm_sigchld, NULL);
1077#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001078 afreeall(APERM);
1079 for (fd = 3; fd < NUFILE; fd++)
1080 if ((i = fcntl(fd, F_GETFD, 0)) != -1 &&
1081 (i & FD_CLOEXEC))
1082 close(fd);
1083 close(2);
1084 close(1);
1085 close(0);
1086#endif
1087 exit(exstat & 0xFF);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001088 }
1089 if (shf)
1090 shf_close(shf);
1091 reclaim();
1092
1093 e = e->oenv;
1094
1095 /* free the struct env - tricky due to the ALLOC_ITEM inside */
1096 cp = (void *)ep;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001097 afree(cp + sizeof(ALLOC_ITEM), ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001098}
1099
1100/* Called after a fork to cleanup stuff left over from parents environment */
1101void
1102cleanup_parents_env(void)
1103{
1104 struct env *ep;
1105 int fd;
1106
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001107 /*
1108 * Don't clean up temporary files - parent will probably need them.
1109 * Also, can't easily reclaim memory since variables, etc. could be
1110 * anywhere.
1111 */
1112
1113 /* close all file descriptors hiding in savefd */
1114 for (ep = e; ep; ep = ep->oenv) {
1115 if (ep->savefd) {
1116 for (fd = 0; fd < NUFILE; fd++)
1117 if (ep->savefd[fd] > 0)
1118 close(ep->savefd[fd]);
1119 afree(ep->savefd, &ep->area);
1120 ep->savefd = NULL;
1121 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001122#ifdef DEBUG_LEAKS
1123 if (ep->type != E_NONE)
1124 ep->type = E_GONE;
1125#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001126 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001127#ifndef DEBUG_LEAKS
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001128 e->oenv = NULL;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001129#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001130}
1131
1132/* Called just before an execve cleanup stuff temporary files */
1133void
1134cleanup_proc_env(void)
1135{
1136 struct env *ep;
1137
1138 for (ep = e; ep; ep = ep->oenv)
1139 remove_temps(ep->temps);
1140}
1141
1142/* remove temp files and free ATEMP Area */
1143static void
1144reclaim(void)
1145{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001146 struct block *l;
1147
1148 while ((l = e->loc) && (!e->oenv || e->oenv->loc != l)) {
1149 e->loc = l->next;
1150 afreeall(&l->area);
1151 }
1152
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001153 remove_temps(e->temps);
1154 e->temps = NULL;
Elliott Hughes77740fc2016-08-12 15:06:53 -07001155
1156 /*
1157 * if the memory backing source is reclaimed, things
1158 * will end up badly when a function expecting it to
1159 * be valid is run; a NULL pointer is easily debugged
1160 */
1161 if (source && source->areap == &e->area)
1162 source = NULL;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001163 afreeall(&e->area);
1164}
1165
1166static void
1167remove_temps(struct temp *tp)
1168{
Elliott Hughes77740fc2016-08-12 15:06:53 -07001169 while (tp) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001170 if (tp->pid == procpid)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001171 unlink(tp->tffn);
Elliott Hughes77740fc2016-08-12 15:06:53 -07001172 tp = tp->next;
1173 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001174}
1175
Geremy Condra03ebf062011-10-12 18:17:24 -07001176/*
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001177 * Initialise tty_fd. Used for tracking the size of the terminal,
1178 * saving/resetting tty modes upon forground job completion, and
1179 * for setting up the tty process group. Return values:
1180 * 0 = got controlling tty
1181 * 1 = got terminal but no controlling tty
1182 * 2 = cannot find a terminal
1183 * 3 = cannot dup fd
1184 * 4 = cannot make fd close-on-exec
1185 * An existing tty_fd is cached if no "better" one could be found,
1186 * i.e. if tty_devtty was already set or the new would not set it.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001187 */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001188int
1189tty_init_fd(void)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001190{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001191 int fd, rv, eno = 0;
1192 bool do_close = false, is_devtty = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001193
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001194 if (tty_devtty) {
1195 /* already got a tty which is /dev/tty */
1196 return (0);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001197 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001198
1199#ifdef _UWIN
Geremy Condra03ebf062011-10-12 18:17:24 -07001200 /*XXX imake style */
1201 if (isatty(3)) {
1202 /* fd 3 on UWIN _is_ /dev/tty (or our controlling tty) */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001203 fd = 3;
1204 goto got_fd;
1205 }
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001206#endif
Elliott Hughes77740fc2016-08-12 15:06:53 -07001207 if ((fd = open(T_devtty, O_RDWR, 0)) >= 0) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001208 do_close = true;
1209 goto got_fd;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001210 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001211 eno = errno;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001212
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001213 if (tty_fd >= 0) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001214 /* already got a non-devtty one */
1215 rv = 1;
1216 goto out;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001217 }
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001218 is_devtty = false;
1219
1220 if (isatty((fd = 0)) || isatty((fd = 2)))
1221 goto got_fd;
1222 /* cannot find one */
1223 rv = 2;
1224 /* assert: do_close == false */
1225 goto out;
1226
1227 got_fd:
1228 if ((rv = fcntl(fd, F_DUPFD, FDBASE)) < 0) {
1229 eno = errno;
1230 rv = 3;
1231 goto out;
1232 }
1233 if (fcntl(rv, F_SETFD, FD_CLOEXEC) < 0) {
1234 eno = errno;
1235 close(rv);
1236 rv = 4;
1237 goto out;
1238 }
1239 tty_fd = rv;
1240 tty_devtty = is_devtty;
1241 rv = eno = 0;
1242 out:
1243 if (do_close)
1244 close(fd);
1245 errno = eno;
1246 return (rv);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001247}
1248
1249/* A shell error occurred (eg, syntax error, etc.) */
Geremy Condra03ebf062011-10-12 18:17:24 -07001250
1251#define VWARNINGF_ERRORPREFIX 1
1252#define VWARNINGF_FILELINE 2
1253#define VWARNINGF_BUILTIN 4
1254#define VWARNINGF_INTERNAL 8
1255
1256static void vwarningf(unsigned int, const char *, va_list)
1257 MKSH_A_FORMAT(__printf__, 2, 0);
1258
1259static void
1260vwarningf(unsigned int flags, const char *fmt, va_list ap)
1261{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001262 if (fmt) {
Geremy Condra03ebf062011-10-12 18:17:24 -07001263 if (flags & VWARNINGF_INTERNAL)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001264 shf_fprintf(shl_out, Tf_sD_, "internal error");
Geremy Condra03ebf062011-10-12 18:17:24 -07001265 if (flags & VWARNINGF_ERRORPREFIX)
1266 error_prefix(tobool(flags & VWARNINGF_FILELINE));
1267 if ((flags & VWARNINGF_BUILTIN) &&
1268 /* not set when main() calls parse_args() */
1269 builtin_argv0 && builtin_argv0 != kshname)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001270 shf_fprintf(shl_out, Tf_sD_, builtin_argv0);
Geremy Condra03ebf062011-10-12 18:17:24 -07001271 shf_vfprintf(shl_out, fmt, ap);
1272 shf_putchar('\n', shl_out);
1273 }
1274 shf_flush(shl_out);
1275}
1276
1277void
1278errorfx(int rc, const char *fmt, ...)
1279{
1280 va_list va;
1281
1282 exstat = rc;
1283
1284 /* debugging: note that stdout not valid */
1285 shl_stdout_ok = false;
1286
1287 va_start(va, fmt);
1288 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1289 va_end(va);
1290 unwind(LERROR);
1291}
1292
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001293void
1294errorf(const char *fmt, ...)
1295{
1296 va_list va;
1297
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001298 exstat = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07001299
1300 /* debugging: note that stdout not valid */
1301 shl_stdout_ok = false;
1302
1303 va_start(va, fmt);
1304 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1305 va_end(va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001306 unwind(LERROR);
1307}
1308
1309/* like errorf(), but no unwind is done */
1310void
1311warningf(bool fileline, const char *fmt, ...)
1312{
1313 va_list va;
1314
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001315 va_start(va, fmt);
Geremy Condra03ebf062011-10-12 18:17:24 -07001316 vwarningf(VWARNINGF_ERRORPREFIX | (fileline ? VWARNINGF_FILELINE : 0),
1317 fmt, va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001318 va_end(va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001319}
1320
Geremy Condra03ebf062011-10-12 18:17:24 -07001321/*
1322 * Used by built-in utilities to prefix shell and utility name to message
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001323 * (also unwinds environments for special builtins).
1324 */
1325void
1326bi_errorf(const char *fmt, ...)
1327{
1328 va_list va;
1329
Geremy Condra03ebf062011-10-12 18:17:24 -07001330 /* debugging: note that stdout not valid */
1331 shl_stdout_ok = false;
1332
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001333 exstat = 1;
Geremy Condra03ebf062011-10-12 18:17:24 -07001334
1335 va_start(va, fmt);
1336 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE |
1337 VWARNINGF_BUILTIN, fmt, va);
1338 va_end(va);
1339
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001340 /* POSIX special builtins cause non-interactive shells to exit */
Elliott Hughes50012062015-03-10 22:22:24 -07001341 if (builtin_spec) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001342 builtin_argv0 = NULL;
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001343 /* may not want to use LERROR here */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001344 unwind(LERROR);
1345 }
1346}
1347
1348/* Called when something that shouldn't happen does */
1349void
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001350internal_errorf(const char *fmt, ...)
1351{
1352 va_list va;
1353
1354 va_start(va, fmt);
Geremy Condra03ebf062011-10-12 18:17:24 -07001355 vwarningf(VWARNINGF_INTERNAL, fmt, va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001356 va_end(va);
1357 unwind(LERROR);
1358}
1359
1360void
1361internal_warningf(const char *fmt, ...)
1362{
1363 va_list va;
1364
1365 va_start(va, fmt);
Geremy Condra03ebf062011-10-12 18:17:24 -07001366 vwarningf(VWARNINGF_INTERNAL, fmt, va);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001367 va_end(va);
1368}
1369
1370/* used by error reporting functions to print "ksh: .kshrc[25]: " */
1371void
1372error_prefix(bool fileline)
1373{
1374 /* Avoid foo: foo[2]: ... */
1375 if (!fileline || !source || !source->file ||
1376 strcmp(source->file, kshname) != 0)
Elliott Hughes77740fc2016-08-12 15:06:53 -07001377 shf_fprintf(shl_out, Tf_sD_, kshname + (*kshname == '-'));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001378 if (fileline && source && source->file != NULL) {
Elliott Hughesb27ce952015-04-21 13:39:18 -07001379 shf_fprintf(shl_out, "%s[%lu]: ", source->file,
1380 (unsigned long)(source->errline ?
1381 source->errline : source->line));
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001382 source->errline = 0;
1383 }
1384}
1385
1386/* printf to shl_out (stderr) with flush */
1387void
1388shellf(const char *fmt, ...)
1389{
1390 va_list va;
1391
Geremy Condra03ebf062011-10-12 18:17:24 -07001392 if (!initio_done)
1393 /* shl_out may not be set up yet... */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001394 return;
1395 va_start(va, fmt);
1396 shf_vfprintf(shl_out, fmt, va);
1397 va_end(va);
1398 shf_flush(shl_out);
1399}
1400
1401/* printf to shl_stdout (stdout) */
1402void
1403shprintf(const char *fmt, ...)
1404{
1405 va_list va;
1406
1407 if (!shl_stdout_ok)
1408 internal_errorf("shl_stdout not valid");
1409 va_start(va, fmt);
1410 shf_vfprintf(shl_stdout, fmt, va);
1411 va_end(va);
1412}
1413
1414/* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
1415int
1416can_seek(int fd)
1417{
1418 struct stat statb;
1419
1420 return (fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
1421 SHF_UNBUF : 0);
1422}
1423
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001424#ifdef DF
1425int shl_dbg_fd;
1426#define NSHF_IOB 4
1427#else
1428#define NSHF_IOB 3
1429#endif
1430struct shf shf_iob[NSHF_IOB];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001431
1432void
1433initio(void)
1434{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001435#ifdef DF
1436 const char *lfp;
1437#endif
1438
Geremy Condra03ebf062011-10-12 18:17:24 -07001439 /* force buffer allocation */
1440 shf_fdopen(1, SHF_WR, shl_stdout);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001441 shf_fdopen(2, SHF_WR, shl_out);
Thorsten Glaser811a5752013-07-25 14:24:45 +00001442 shf_fdopen(2, SHF_WR, shl_xtrace);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001443#ifdef DF
1444 if ((lfp = getenv("SDMKSH_PATH")) == NULL) {
Elliott Hughes96b43632015-07-17 11:39:41 -07001445 if ((lfp = getenv("HOME")) == NULL || !mksh_abspath(lfp))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001446 errorf("can't get home directory");
Elliott Hughes77740fc2016-08-12 15:06:53 -07001447 lfp = shf_smprintf(Tf_sSs, lfp, "mksh-dbg.txt");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001448 }
1449
1450 if ((shl_dbg_fd = open(lfp, O_WRONLY | O_APPEND | O_CREAT, 0600)) < 0)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001451 errorf("can't open debug output file %s", lfp);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001452 if (shl_dbg_fd < FDBASE) {
1453 int nfd;
1454
1455 nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
1456 close(shl_dbg_fd);
1457 if ((shl_dbg_fd = nfd) == -1)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001458 errorf("can't dup debug output file");
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001459 }
1460 fcntl(shl_dbg_fd, F_SETFD, FD_CLOEXEC);
1461 shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
1462 DF("=== open ===");
1463#endif
1464 initio_done = true;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001465}
1466
1467/* A dup2() with error checking */
1468int
1469ksh_dup2(int ofd, int nfd, bool errok)
1470{
1471 int rv;
1472
1473 if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001474 errorf(Ttoo_many_files);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001475
1476#ifdef __ultrix
Geremy Condra03ebf062011-10-12 18:17:24 -07001477 /*XXX imake style */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001478 if (rv >= 0)
1479 fcntl(nfd, F_SETFD, 0);
1480#endif
1481
1482 return (rv);
1483}
1484
1485/*
Geremy Condra03ebf062011-10-12 18:17:24 -07001486 * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10),
1487 * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001488 */
1489short
1490savefd(int fd)
1491{
1492 int nfd = fd;
1493
1494 if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
Elliott Hughes96b43632015-07-17 11:39:41 -07001495 (errno == EBADF || errno == EPERM))
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001496 return (-1);
1497 if (nfd < 0 || nfd > SHRT_MAX)
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001498 errorf(Ttoo_many_files);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001499 fcntl(nfd, F_SETFD, FD_CLOEXEC);
1500 return ((short)nfd);
1501}
1502
1503void
1504restfd(int fd, int ofd)
1505{
1506 if (fd == 2)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001507 shf_flush(&shf_iob[/* fd */ 2]);
Geremy Condra03ebf062011-10-12 18:17:24 -07001508 if (ofd < 0)
1509 /* original fd closed */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001510 close(fd);
1511 else if (fd != ofd) {
Geremy Condra03ebf062011-10-12 18:17:24 -07001512 /*XXX: what to do if this dup fails? */
1513 ksh_dup2(ofd, fd, true);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001514 close(ofd);
1515 }
1516}
1517
1518void
1519openpipe(int *pv)
1520{
1521 int lpv[2];
1522
1523 if (pipe(lpv) < 0)
1524 errorf("can't create pipe - try again");
1525 pv[0] = savefd(lpv[0]);
1526 if (pv[0] != lpv[0])
1527 close(lpv[0]);
1528 pv[1] = savefd(lpv[1]);
1529 if (pv[1] != lpv[1])
1530 close(lpv[1]);
Elliott Hughesa3c3f962017-04-12 16:52:30 -07001531#ifdef __OS2__
1532 setmode(pv[0], O_BINARY);
1533 setmode(pv[1], O_BINARY);
1534#endif
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001535}
1536
1537void
1538closepipe(int *pv)
1539{
1540 close(pv[0]);
1541 close(pv[1]);
1542}
1543
Geremy Condra03ebf062011-10-12 18:17:24 -07001544/*
1545 * Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001546 * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
1547 */
1548int
1549check_fd(const char *name, int mode, const char **emsgp)
1550{
Elliott Hughes77740fc2016-08-12 15:06:53 -07001551 int fd, fl;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001552
Elliott Hughes77740fc2016-08-12 15:06:53 -07001553 if (!name[0] || name[1])
1554 goto illegal_fd_name;
1555 if (name[0] == 'p')
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001556 return (coproc_getfd(mode, emsgp));
Elliott Hughes77740fc2016-08-12 15:06:53 -07001557 if (!ksh_isdigit(name[0])) {
1558 illegal_fd_name:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001559 if (emsgp)
1560 *emsgp = "illegal file descriptor name";
1561 return (-1);
1562 }
Elliott Hughes77740fc2016-08-12 15:06:53 -07001563
1564 if ((fl = fcntl((fd = ksh_numdig(name[0])), F_GETFL, 0)) < 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001565 if (emsgp)
1566 *emsgp = "bad file descriptor";
1567 return (-1);
1568 }
1569 fl &= O_ACCMODE;
Geremy Condra03ebf062011-10-12 18:17:24 -07001570 /*
1571 * X_OK is a kludge to disable this check for dups (x<&1):
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001572 * historical shells never did this check (XXX don't know what
1573 * POSIX has to say).
1574 */
1575 if (!(mode & X_OK) && fl != O_RDWR && (
1576 ((mode & R_OK) && fl != O_RDONLY) ||
1577 ((mode & W_OK) && fl != O_WRONLY))) {
1578 if (emsgp)
1579 *emsgp = (fl == O_WRONLY) ?
1580 "fd not open for reading" :
1581 "fd not open for writing";
1582 return (-1);
1583 }
1584 return (fd);
1585}
1586
1587/* Called once from main */
1588void
1589coproc_init(void)
1590{
1591 coproc.read = coproc.readw = coproc.write = -1;
1592 coproc.njobs = 0;
1593 coproc.id = 0;
1594}
1595
1596/* Called by c_read() when eof is read - close fd if it is the co-process fd */
1597void
1598coproc_read_close(int fd)
1599{
1600 if (coproc.read >= 0 && fd == coproc.read) {
1601 coproc_readw_close(fd);
1602 close(coproc.read);
1603 coproc.read = -1;
1604 }
1605}
1606
Geremy Condra03ebf062011-10-12 18:17:24 -07001607/*
1608 * Called by c_read() and by iosetup() to close the other side of the
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001609 * read pipe, so reads will actually terminate.
1610 */
1611void
1612coproc_readw_close(int fd)
1613{
1614 if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
1615 close(coproc.readw);
1616 coproc.readw = -1;
1617 }
1618}
1619
Geremy Condra03ebf062011-10-12 18:17:24 -07001620/*
1621 * Called by c_print when a write to a fd fails with EPIPE and by iosetup
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001622 * when co-process input is dup'd
1623 */
1624void
1625coproc_write_close(int fd)
1626{
1627 if (coproc.write >= 0 && fd == coproc.write) {
1628 close(coproc.write);
1629 coproc.write = -1;
1630 }
1631}
1632
Geremy Condra03ebf062011-10-12 18:17:24 -07001633/*
1634 * Called to check for existence of/value of the co-process file descriptor.
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001635 * (Used by check_fd() and by c_read/c_print to deal with -p option).
1636 */
1637int
1638coproc_getfd(int mode, const char **emsgp)
1639{
1640 int fd = (mode & R_OK) ? coproc.read : coproc.write;
1641
1642 if (fd >= 0)
1643 return (fd);
1644 if (emsgp)
1645 *emsgp = "no coprocess";
1646 return (-1);
1647}
1648
Geremy Condra03ebf062011-10-12 18:17:24 -07001649/*
1650 * called to close file descriptors related to the coprocess (if any)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001651 * Should be called with SIGCHLD blocked.
1652 */
1653void
1654coproc_cleanup(int reuse)
1655{
1656 /* This to allow co-processes to share output pipe */
1657 if (!reuse || coproc.readw < 0 || coproc.read < 0) {
1658 if (coproc.read >= 0) {
1659 close(coproc.read);
1660 coproc.read = -1;
1661 }
1662 if (coproc.readw >= 0) {
1663 close(coproc.readw);
1664 coproc.readw = -1;
1665 }
1666 }
1667 if (coproc.write >= 0) {
1668 close(coproc.write);
1669 coproc.write = -1;
1670 }
1671}
1672
1673struct temp *
1674maketemp(Area *ap, Temp_type type, struct temp **tlist)
1675{
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001676 char *cp;
Geremy Condra03ebf062011-10-12 18:17:24 -07001677 size_t len;
Thorsten Glaser811a5752013-07-25 14:24:45 +00001678 int i, j;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001679 struct temp *tp;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001680 const char *dir;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001681 struct stat sb;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001682
1683 dir = tmpdir ? tmpdir : MKSH_DEFAULT_TMPDIR;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001684 /* add "/shXXXXXX.tmp" plus NUL */
1685 len = strlen(dir);
1686 checkoktoadd(len, offsetof(struct temp, tffn[0]) + 14);
1687 tp = alloc(offsetof(struct temp, tffn[0]) + 14 + len, ap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001688
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001689 tp->shf = NULL;
1690 tp->pid = procpid;
1691 tp->type = type;
1692
1693 if (stat(dir, &sb) || !S_ISDIR(sb.st_mode)) {
1694 tp->tffn[0] = '\0';
1695 goto maketemp_out;
1696 }
1697
1698 cp = (void *)tp;
1699 cp += offsetof(struct temp, tffn[0]);
1700 memcpy(cp, dir, len);
1701 cp += len;
1702 memcpy(cp, "/shXXXXXX.tmp", 14);
1703 /* point to the first of six Xes */
1704 cp += 3;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001705
1706 /* cyclically attempt to open a temporary file */
Elliott Hughes96b43632015-07-17 11:39:41 -07001707 do {
1708 /* generate random part of filename */
1709 len = 0;
1710 do {
1711 cp[len++] = digits_lc[rndget() % 36];
1712 } while (len < 6);
1713
1714 /* check if this one works */
1715 if ((i = binopen3(tp->tffn, O_CREAT | O_EXCL | O_RDWR,
1716 0600)) < 0 && errno != EEXIST)
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001717 goto maketemp_out;
Elliott Hughes96b43632015-07-17 11:39:41 -07001718 } while (i < 0);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001719
1720 if (type == TT_FUNSUB) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001721 /* map us high and mark as close-on-exec */
Thorsten Glaser811a5752013-07-25 14:24:45 +00001722 if ((j = savefd(i)) != i) {
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001723 close(i);
Thorsten Glaser811a5752013-07-25 14:24:45 +00001724 i = j;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001725 }
Thorsten Glaser811a5752013-07-25 14:24:45 +00001726
1727 /* operation mode for the shf */
1728 j = SHF_RD;
1729 } else
1730 j = SHF_WR;
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001731
1732 /* shf_fdopen cannot fail, so no fd leak */
Thorsten Glaser811a5752013-07-25 14:24:45 +00001733 tp->shf = shf_fdopen(i, j, NULL);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001734
1735 maketemp_out:
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001736 tp->next = *tlist;
1737 *tlist = tp;
1738 return (tp);
1739}
1740
1741/*
1742 * We use a similar collision resolution algorithm as Python 2.5.4
1743 * but with a slightly tweaked implementation written from scratch.
1744 */
1745
Geremy Condra03ebf062011-10-12 18:17:24 -07001746#define INIT_TBLSHIFT 3 /* initial table shift (2^3 = 8) */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001747#define PERTURB_SHIFT 5 /* see Python 2.5.4 Objects/dictobject.c */
1748
Geremy Condra03ebf062011-10-12 18:17:24 -07001749static void tgrow(struct table *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001750static int tnamecmp(const void *, const void *);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001751
1752static void
Geremy Condra03ebf062011-10-12 18:17:24 -07001753tgrow(struct table *tp)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001754{
Geremy Condra03ebf062011-10-12 18:17:24 -07001755 size_t i, j, osize, mask, perturb;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001756 struct tbl *tblp, **pp;
1757 struct tbl **ntblp, **otblp = tp->tbls;
1758
Geremy Condra03ebf062011-10-12 18:17:24 -07001759 if (tp->tshift > 29)
1760 internal_errorf("hash table size limit reached");
1761
1762 /* calculate old size, new shift and new size */
1763 osize = (size_t)1 << (tp->tshift++);
1764 i = osize << 1;
1765
1766 ntblp = alloc2(i, sizeof(struct tbl *), tp->areap);
1767 /* multiplication cannot overflow: alloc2 checked that */
1768 memset(ntblp, 0, i * sizeof(struct tbl *));
1769
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001770 /* table can get very full when reaching its size limit */
1771 tp->nfree = (tp->tshift == 30) ? 0x3FFF0000UL :
1772 /* but otherwise, only 75% */
1773 ((i * 3) / 4);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001774 tp->tbls = ntblp;
1775 if (otblp == NULL)
1776 return;
Geremy Condra03ebf062011-10-12 18:17:24 -07001777
1778 mask = i - 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001779 for (i = 0; i < osize; i++)
1780 if ((tblp = otblp[i]) != NULL) {
1781 if ((tblp->flag & DEFINED)) {
1782 /* search for free hash table slot */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001783 j = perturb = tblp->ua.hval;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001784 goto find_first_empty_slot;
1785 find_next_empty_slot:
1786 j = (j << 2) + j + perturb + 1;
1787 perturb >>= PERTURB_SHIFT;
1788 find_first_empty_slot:
Geremy Condra03ebf062011-10-12 18:17:24 -07001789 pp = &ntblp[j & mask];
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001790 if (*pp != NULL)
1791 goto find_next_empty_slot;
1792 /* found an empty hash table slot */
1793 *pp = tblp;
1794 tp->nfree--;
1795 } else if (!(tblp->flag & FINUSE)) {
1796 afree(tblp, tp->areap);
1797 }
1798 }
1799 afree(otblp, tp->areap);
1800}
1801
1802void
Geremy Condra03ebf062011-10-12 18:17:24 -07001803ktinit(Area *ap, struct table *tp, uint8_t initshift)
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001804{
1805 tp->areap = ap;
1806 tp->tbls = NULL;
Geremy Condra03ebf062011-10-12 18:17:24 -07001807 tp->tshift = ((initshift > INIT_TBLSHIFT) ?
1808 initshift : INIT_TBLSHIFT) - 1;
1809 tgrow(tp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001810}
1811
1812/* table, name (key) to search for, hash(name), rv pointer to tbl ptr */
Geremy Condra03ebf062011-10-12 18:17:24 -07001813struct tbl *
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001814ktscan(struct table *tp, const char *name, uint32_t h, struct tbl ***ppp)
1815{
1816 size_t j, perturb, mask;
1817 struct tbl **pp, *p;
1818
Geremy Condra03ebf062011-10-12 18:17:24 -07001819 mask = ((size_t)1 << (tp->tshift)) - 1;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001820 /* search for hash table slot matching name */
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001821 j = perturb = h;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001822 goto find_first_slot;
1823 find_next_slot:
1824 j = (j << 2) + j + perturb + 1;
1825 perturb >>= PERTURB_SHIFT;
1826 find_first_slot:
1827 pp = &tp->tbls[j & mask];
1828 if ((p = *pp) != NULL && (p->ua.hval != h || !(p->flag & DEFINED) ||
1829 strcmp(p->name, name)))
1830 goto find_next_slot;
1831 /* p == NULL if not found, correct found entry otherwise */
1832 if (ppp)
1833 *ppp = pp;
1834 return (p);
1835}
1836
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001837/* table, name (key) to enter, hash(n) */
1838struct tbl *
1839ktenter(struct table *tp, const char *n, uint32_t h)
1840{
1841 struct tbl **pp, *p;
Geremy Condra03ebf062011-10-12 18:17:24 -07001842 size_t len;
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001843
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001844 Search:
1845 if ((p = ktscan(tp, n, h, &pp)))
1846 return (p);
1847
Geremy Condra03ebf062011-10-12 18:17:24 -07001848 if (tp->nfree == 0) {
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001849 /* too full */
Geremy Condra03ebf062011-10-12 18:17:24 -07001850 tgrow(tp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001851 goto Search;
1852 }
1853
1854 /* create new tbl entry */
Geremy Condra03ebf062011-10-12 18:17:24 -07001855 len = strlen(n);
1856 checkoktoadd(len, offsetof(struct tbl, name[0]) + 1);
1857 p = alloc(offsetof(struct tbl, name[0]) + ++len, tp->areap);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001858 p->flag = 0;
1859 p->type = 0;
1860 p->areap = tp->areap;
1861 p->ua.hval = h;
1862 p->u2.field = 0;
1863 p->u.array = NULL;
1864 memcpy(p->name, n, len);
1865
1866 /* enter in tp->tbls */
1867 tp->nfree--;
1868 *pp = p;
1869 return (p);
1870}
1871
1872void
1873ktwalk(struct tstate *ts, struct table *tp)
1874{
Geremy Condra03ebf062011-10-12 18:17:24 -07001875 ts->left = (size_t)1 << (tp->tshift);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001876 ts->next = tp->tbls;
1877}
1878
1879struct tbl *
1880ktnext(struct tstate *ts)
1881{
1882 while (--ts->left >= 0) {
1883 struct tbl *p = *ts->next++;
1884 if (p != NULL && (p->flag & DEFINED))
1885 return (p);
1886 }
1887 return (NULL);
1888}
1889
1890static int
1891tnamecmp(const void *p1, const void *p2)
1892{
1893 const struct tbl *a = *((const struct tbl * const *)p1);
1894 const struct tbl *b = *((const struct tbl * const *)p2);
1895
1896 return (strcmp(a->name, b->name));
1897}
1898
1899struct tbl **
1900ktsort(struct table *tp)
1901{
1902 size_t i;
1903 struct tbl **p, **sp, **dp;
1904
Geremy Condra03ebf062011-10-12 18:17:24 -07001905 /*
1906 * since the table is never entirely full, no need to reserve
1907 * additional space for the trailing NULL appended below
1908 */
1909 i = (size_t)1 << (tp->tshift);
1910 p = alloc2(i, sizeof(struct tbl *), ATEMP);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001911 sp = tp->tbls; /* source */
1912 dp = p; /* dest */
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001913 while (i--)
1914 if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
1915 ((*dp)->flag & ARRAY)))
1916 dp++;
Geremy Condra03ebf062011-10-12 18:17:24 -07001917 qsort(p, (i = dp - p), sizeof(struct tbl *), tnamecmp);
Jean-Baptiste Queru5155f1c2011-06-16 10:05:28 -07001918 p[i] = NULL;
1919 return (p);
1920}
1921
1922#ifdef SIGWINCH
1923static void
1924x_sigwinch(int sig MKSH_A_UNUSED)
1925{
1926 /* this runs inside interrupt context, with errno saved */
1927
1928 got_winch = 1;
1929}
1930#endif
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001931
1932#ifdef DF
1933void
1934DF(const char *fmt, ...)
1935{
1936 va_list args;
1937 struct timeval tv;
1938 mirtime_mjd mjd;
1939
1940 mksh_lockfd(shl_dbg_fd);
1941 mksh_TIME(tv);
1942 timet2mjd(&mjd, tv.tv_sec);
1943 shf_fprintf(shl_dbg, "[%02u:%02u:%02u (%u) %u.%06u] ",
1944 (unsigned)mjd.sec / 3600, ((unsigned)mjd.sec / 60) % 60,
1945 (unsigned)mjd.sec % 60, (unsigned)getpid(),
1946 (unsigned)tv.tv_sec, (unsigned)tv.tv_usec);
1947 va_start(args, fmt);
1948 shf_vfprintf(shl_dbg, fmt, args);
1949 va_end(args);
1950 shf_putc('\n', shl_dbg);
1951 shf_flush(shl_dbg);
1952 mksh_unlkfd(shl_dbg_fd);
1953}
1954#endif
1955
1956void
1957x_mkraw(int fd, mksh_ttyst *ocb, bool forread)
1958{
1959 mksh_ttyst cb;
1960
1961 if (ocb)
1962 mksh_tcget(fd, ocb);
1963 else
1964 ocb = &tty_state;
1965
1966 cb = *ocb;
1967 if (forread) {
Elliott Hughes737fdce2014-08-07 12:59:26 -07001968 cb.c_iflag &= ~(ISTRIP);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001969 cb.c_lflag &= ~(ICANON) | ECHO;
1970 } else {
Elliott Hughes737fdce2014-08-07 12:59:26 -07001971 cb.c_iflag &= ~(INLCR | ICRNL | ISTRIP);
Thorsten Glaserc2dc5de2013-02-18 23:02:51 +00001972 cb.c_lflag &= ~(ISIG | ICANON | ECHO);
1973 }
1974#if defined(VLNEXT) && defined(_POSIX_VDISABLE)
1975 /* OSF/1 processes lnext when ~icanon */
1976 cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
1977#endif
1978 /* SunOS 4.1.x & OSF/1 processes discard(flush) when ~icanon */
1979#if defined(VDISCARD) && defined(_POSIX_VDISABLE)
1980 cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
1981#endif
1982 cb.c_cc[VTIME] = 0;
1983 cb.c_cc[VMIN] = 1;
1984
1985 mksh_tcset(fd, &cb);
1986}