OSDN Git Service

59d0b1e0dfc21a78c041edb989fdc5d4bd56ff3c
[android-x86/external-mksh.git] / src / main.c
1 /*      $OpenBSD: main.c,v 1.55 2015/02/09 09:09:30 jsg Exp $   */
2 /*      $OpenBSD: tty.c,v 1.10 2014/08/10 02:44:26 guenther Exp $       */
3 /*      $OpenBSD: io.c,v 1.25 2014/08/11 20:28:47 guenther Exp $        */
4 /*      $OpenBSD: table.c,v 1.15 2012/02/19 07:52:30 otto Exp $ */
5
6 /*-
7  * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
8  *               2011, 2012, 2013, 2014
9  *      Thorsten Glaser <tg@mirbsd.org>
10  *
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
27 #define EXTERN
28 #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
37 __RCSID("$MirOS: src/bin/mksh/main.c,v 1.285.2.2 2015/03/01 15:43:01 tg Exp $");
38
39 extern char **environ;
40
41 #ifndef MKSHRC_PATH
42 #define MKSHRC_PATH     "~/.mkshrc"
43 #endif
44
45 #ifndef MKSH_DEFAULT_TMPDIR
46 #define MKSH_DEFAULT_TMPDIR     "/tmp"
47 #endif
48
49 static uint8_t isuc(const char *);
50 static int main_init(int, const char *[], Source **, struct block **);
51 void chvt_reinit(void);
52 static void reclaim(void);
53 static void remove_temps(struct temp *);
54 static mksh_uari_t rndsetup(void);
55 #ifdef SIGWINCH
56 static void x_sigwinch(int);
57 #endif
58
59 static const char initifs[] = "IFS= \t\n";
60
61 static const char initsubs[] =
62     "${PS2=> } ${PS3=#? } ${PS4=+ } ${SECONDS=0} ${TMOUT=0} ${EPOCHREALTIME=}";
63
64 static const char *initcoms[] = {
65         Ttypeset, "-r", initvsn, NULL,
66         Ttypeset, "-x", "HOME", "PATH", "SHELL", NULL,
67         Ttypeset, "-i10", "COLUMNS", "LINES", "SECONDS", "TMOUT", NULL,
68         Talias,
69         "integer=typeset -i",
70         Tlocal_typeset,
71         /* not "alias -t --": hash -r needs to work */
72         "hash=alias -t",
73         "type=whence -v",
74 #if !defined(ANDROID) && !defined(MKSH_UNEMPLOYED)
75         /* not in Android for political reasons */
76         /* not in ARGE mksh due to no job control */
77         "stop=kill -STOP",
78 #endif
79         "autoload=typeset -fu",
80         "functions=typeset -f",
81         "history=fc -l",
82         "nameref=typeset -n",
83         "nohup=nohup ",
84         Tr_fc_e_dash,
85         "source=PATH=$PATH:. command .",
86         "login=exec login",
87         NULL,
88          /* this is what AT&T ksh seems to track, with the addition of emacs */
89         Talias, "-tU",
90         "cat", "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
91         "make", "mv", "pr", "rm", "sed", "sh", "vi", "who", NULL,
92         NULL
93 };
94
95 static const char *restr_com[] = {
96         Ttypeset, "-r", "PATH", "ENV", "SHELL", NULL
97 };
98
99 static bool initio_done;
100
101 /* top-level parsing and execution environment */
102 static struct env env;
103 struct env *e = &env;
104
105 static mksh_uari_t
106 rndsetup(void)
107 {
108         register uint32_t h;
109         struct {
110                 ALLOC_ITEM alloc_INT;
111                 void *dataptr, *stkptr, *mallocptr;
112 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
113                 sigjmp_buf jbuf;
114 #endif
115                 struct timeval tv;
116         } *bufptr;
117         char *cp;
118
119         cp = alloc(sizeof(*bufptr) - ALLOC_SIZE, APERM);
120 #ifdef DEBUG
121         /* clear the allocated space, for valgrind */
122         memset(cp, 0, sizeof(*bufptr) - ALLOC_SIZE);
123 #endif
124         /* undo what alloc() did to the malloc result address */
125         bufptr = (void *)(cp - ALLOC_SIZE);
126         /* PIE or something similar provides us with deltas here */
127         bufptr->dataptr = &rndsetupstate;
128         /* ASLR in at least Windows, Linux, some BSDs */
129         bufptr->stkptr = &bufptr;
130         /* randomised malloc in BSD (and possibly others) */
131         bufptr->mallocptr = bufptr;
132 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
133         /* glibc pointer guard */
134         sigsetjmp(bufptr->jbuf, 1);
135 #endif
136         /* introduce variation (and yes, second arg MBZ for portability) */
137         mksh_TIME(bufptr->tv);
138
139         h = chvt_rndsetup(bufptr, sizeof(*bufptr));
140
141         afree(cp, APERM);
142         return ((mksh_uari_t)h);
143 }
144
145 void
146 chvt_reinit(void)
147 {
148         kshpid = procpid = getpid();
149         ksheuid = geteuid();
150         kshpgrp = getpgrp();
151         kshppid = getppid();
152 }
153
154 static const char *empty_argv[] = {
155         "mksh", NULL
156 };
157
158 static uint8_t
159 isuc(const char *cx) {
160         char *cp, *x;
161         uint8_t rv = 0;
162
163         if (!cx || !*cx)
164                 return (0);
165
166         /* uppercase a string duplicate */
167         strdupx(x, cx, ATEMP);
168         cp = x;
169         while ((*cp = ksh_toupper(*cp)))
170                 ++cp;
171
172         /* check for UTF-8 */
173         if (strstr(x, "UTF-8") || strstr(x, "UTF8"))
174                 rv = 1;
175
176         /* free copy and out */
177         afree(x, ATEMP);
178         return (rv);
179 }
180
181 static int
182 main_init(int argc, const char *argv[], Source **sp, struct block **lp)
183 {
184         int argi, i;
185         Source *s = NULL;
186         struct block *l;
187         unsigned char restricted_shell, errexit, utf_flag;
188         char *cp;
189         const char *ccp, **wp;
190         struct tbl *vp;
191         struct stat s_stdin;
192 #if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
193         ssize_t k;
194 #endif
195
196         /* do things like getpgrp() et al. */
197         chvt_reinit();
198
199         /* make sure argv[] is sane */
200         if (!*argv) {
201                 argv = empty_argv;
202                 argc = 1;
203         }
204         kshname = argv[0];
205
206         /* initialise permanent Area */
207         ainit(&aperm);
208
209         /* set up base environment */
210         env.type = E_NONE;
211         ainit(&env.area);
212         /* set up global l->vars and l->funs */
213         newblock();
214
215         /* Do this first so output routines (eg, errorf, shellf) can work */
216         initio();
217
218         /* determine the basename (without '-' or path) of the executable */
219         ccp = kshname;
220         goto begin_parse_kshname;
221         while ((i = ccp[argi++])) {
222                 if (i == '/') {
223                         ccp += argi;
224  begin_parse_kshname:
225                         argi = 0;
226                         if (*ccp == '-')
227                                 ++ccp;
228                 }
229         }
230         if (!*ccp)
231                 ccp = empty_argv[0];
232
233         /*
234          * Turn on nohup by default. (AT&T ksh does not have a nohup
235          * option - it always sends the hup).
236          */
237         Flag(FNOHUP) = 1;
238
239         /*
240          * Turn on brace expansion by default. AT&T kshs that have
241          * alternation always have it on.
242          */
243         Flag(FBRACEEXPAND) = 1;
244
245         /*
246          * Turn on "set -x" inheritance by default.
247          */
248         Flag(FXTRACEREC) = 1;
249
250         /* define built-in commands and see if we were called as one */
251         ktinit(APERM, &builtins,
252             /* currently up to 51 builtins: 75% of 128 = 2^7 */
253             7);
254         for (i = 0; mkshbuiltins[i].name != NULL; i++)
255                 if (!strcmp(ccp, builtin(mkshbuiltins[i].name,
256                     mkshbuiltins[i].func)))
257                         Flag(FAS_BUILTIN) = 1;
258
259         if (!Flag(FAS_BUILTIN)) {
260                 /* check for -T option early */
261                 argi = parse_args(argv, OF_FIRSTTIME, NULL);
262                 if (argi < 0)
263                         return (1);
264
265 #if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)
266                 /* are we called as -sh or /bin/sh or so? */
267                 if (!strcmp(ccp, "sh")) {
268                         /* either also turns off braceexpand */
269 #ifdef MKSH_BINSHPOSIX
270                         /* enable better POSIX conformance */
271                         change_flag(FPOSIX, OF_FIRSTTIME, true);
272 #endif
273 #ifdef MKSH_BINSHREDUCED
274                         /* enable kludge/compat mode */
275                         change_flag(FSH, OF_FIRSTTIME, true);
276 #endif
277                 }
278 #endif
279         }
280
281         initvar();
282
283         initctypes();
284
285         inittraps();
286
287         coproc_init();
288
289         /* set up variable and command dictionaries */
290         ktinit(APERM, &taliases, 0);
291         ktinit(APERM, &aliases, 0);
292 #ifndef MKSH_NOPWNAM
293         ktinit(APERM, &homedirs, 0);
294 #endif
295
296         /* define shell keywords */
297         initkeywords();
298
299         init_histvec();
300
301         /* initialise tty size before importing environment */
302         change_winsz();
303
304 #ifdef _PATH_DEFPATH
305         def_path = _PATH_DEFPATH;
306 #else
307 #ifdef _CS_PATH
308         if ((k = confstr(_CS_PATH, NULL, 0)) > 0 &&
309             confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
310                 def_path = cp;
311         else
312 #endif
313                 /*
314                  * this is uniform across all OSes unless it
315                  * breaks somewhere; don't try to optimise,
316                  * e.g. add stuff for Interix or remove /usr
317                  * for HURD, because e.g. Debian GNU/HURD is
318                  * "keeping a regular /usr"; this is supposed
319                  * to be a sane 'basic' default PATH
320                  */
321                 def_path = "/bin:/usr/bin:/sbin:/usr/sbin";
322 #endif
323
324         /*
325          * Set PATH to def_path (will set the path global variable).
326          * (import of environment below will probably change this setting).
327          */
328         vp = global("PATH");
329         /* setstr can't fail here */
330         setstr(vp, def_path, KSH_RETURN_ERROR);
331
332 #ifndef MKSH_NO_CMDLINE_EDITING
333         /*
334          * Set edit mode to emacs by default, may be overridden
335          * by the environment or the user. Also, we want tab completion
336          * on in vi by default.
337          */
338         change_flag(FEMACS, OF_SPECIAL, true);
339 #if !MKSH_S_NOVI
340         Flag(FVITABCOMPLETE) = 1;
341 #endif
342 #endif
343
344         /* import environment */
345         if (environ != NULL) {
346                 wp = (const char **)environ;
347                 while (*wp != NULL) {
348                         rndpush(*wp);
349                         typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
350                         ++wp;
351                 }
352         }
353
354         /* for security */
355         typeset(initifs, 0, 0, 0, 0);
356
357         /* assign default shell variable values */
358         substitute(initsubs, 0);
359
360         /* Figure out the current working directory and set $PWD */
361         vp = global("PWD");
362         cp = str_val(vp);
363         /* Try to use existing $PWD if it is valid */
364         set_current_wd((cp[0] == '/' && test_eval(NULL, TO_FILEQ, cp, ".",
365             true)) ? cp : NULL);
366         if (current_wd[0])
367                 simplify_path(current_wd);
368         /* Only set pwd if we know where we are or if it had a bogus value */
369         if (current_wd[0] || *cp)
370                 /* setstr can't fail here */
371                 setstr(vp, current_wd, KSH_RETURN_ERROR);
372
373         for (wp = initcoms; *wp != NULL; wp++) {
374                 shcomexec(wp);
375                 while (*wp != NULL)
376                         wp++;
377         }
378         setint_n(global("OPTIND"), 1, 10);
379
380         kshuid = getuid();
381         kshgid = getgid();
382         kshegid = getegid();
383
384         safe_prompt = ksheuid ? "$ " : "# ";
385         vp = global("PS1");
386         /* Set PS1 if unset or we are root and prompt doesn't contain a # */
387         if (!(vp->flag & ISSET) ||
388             (!ksheuid && !strchr(str_val(vp), '#')))
389                 /* setstr can't fail here */
390                 setstr(vp, safe_prompt, KSH_RETURN_ERROR);
391         setint_n((vp = global("BASHPID")), 0, 10);
392         vp->flag |= INT_U;
393         setint_n((vp = global("PGRP")), (mksh_uari_t)kshpgrp, 10);
394         vp->flag |= INT_U;
395         setint_n((vp = global("PPID")), (mksh_uari_t)kshppid, 10);
396         vp->flag |= INT_U;
397         setint_n((vp = global("USER_ID")), (mksh_uari_t)ksheuid, 10);
398         vp->flag |= INT_U;
399         setint_n((vp = global("KSHUID")), (mksh_uari_t)kshuid, 10);
400         vp->flag |= INT_U;
401         setint_n((vp = global("KSHEGID")), (mksh_uari_t)kshegid, 10);
402         vp->flag |= INT_U;
403         setint_n((vp = global("KSHGID")), (mksh_uari_t)kshgid, 10);
404         vp->flag |= INT_U;
405         setint_n((vp = global("RANDOM")), rndsetup(), 10);
406         vp->flag |= INT_U;
407         setint_n((vp_pipest = global("PIPESTATUS")), 0, 10);
408
409         /* Set this before parsing arguments */
410         Flag(FPRIVILEGED) = (
411 #if HAVE_ISSETUGID
412             issetugid() ||
413 #endif
414             kshuid != ksheuid || kshgid != kshegid) ? 2 : 0;
415
416         /* this to note if monitor is set on command line (see below) */
417 #ifndef MKSH_UNEMPLOYED
418         Flag(FMONITOR) = 127;
419 #endif
420         /* this to note if utf-8 mode is set on command line (see below) */
421         UTFMODE = 2;
422
423         if (!Flag(FAS_BUILTIN)) {
424                 argi = parse_args(argv, OF_CMDLINE, NULL);
425                 if (argi < 0)
426                         return (1);
427         }
428
429         /* process this later only, default to off (hysterical raisins) */
430         utf_flag = UTFMODE;
431         UTFMODE = 0;
432
433         if (Flag(FAS_BUILTIN)) {
434                 /* auto-detect from environment variables, always */
435                 utf_flag = 3;
436         } else if (Flag(FCOMMAND)) {
437                 s = pushs(SSTRINGCMDLINE, ATEMP);
438                 if (!(s->start = s->str = argv[argi++]))
439                         errorf("%s %s", "-c", "requires an argument");
440                 while (*s->str) {
441                         if (*s->str != ' ' && ctype(*s->str, C_QUOTE))
442                                 break;
443                         s->str++;
444                 }
445                 if (!*s->str)
446                         s->flags |= SF_MAYEXEC;
447                 s->str = s->start;
448 #ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
449                 /* compatibility to MidnightBSD 0.1 /bin/sh (kludge) */
450                 if (Flag(FSH) && argv[argi] && !strcmp(argv[argi], "--"))
451                         ++argi;
452 #endif
453                 if (argv[argi])
454                         kshname = argv[argi++];
455         } else if (argi < argc && !Flag(FSTDIN)) {
456                 s = pushs(SFILE, ATEMP);
457                 s->file = argv[argi++];
458                 s->u.shf = shf_open(s->file, O_RDONLY, 0,
459                     SHF_MAPHI | SHF_CLEXEC);
460                 if (s->u.shf == NULL) {
461                         shl_stdout_ok = false;
462                         warningf(true, "%s: %s", s->file, cstrerror(errno));
463                         /* mandated by SUSv4 */
464                         exstat = 127;
465                         unwind(LERROR);
466                 }
467                 kshname = s->file;
468         } else {
469                 Flag(FSTDIN) = 1;
470                 s = pushs(SSTDIN, ATEMP);
471                 s->file = "<stdin>";
472                 s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
473                     NULL);
474                 if (isatty(0) && isatty(2)) {
475                         Flag(FTALKING) = Flag(FTALKING_I) = 1;
476                         /* The following only if isatty(0) */
477                         s->flags |= SF_TTY;
478                         s->u.shf->flags |= SHF_INTERRUPT;
479                         s->file = NULL;
480                 }
481         }
482
483         /* this bizarreness is mandated by POSIX */
484         if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
485             Flag(FTALKING))
486                 reset_nonblock(0);
487
488         /* initialise job control */
489         j_init();
490         /* do this after j_init() which calls tty_init_state() */
491         if (Flag(FTALKING)) {
492                 if (utf_flag == 2) {
493 #ifndef MKSH_ASSUME_UTF8
494                         /* auto-detect from locale or environment */
495                         utf_flag = 4;
496 #else /* this may not be an #elif */
497 #if MKSH_ASSUME_UTF8
498                         utf_flag = 1;
499 #else
500                         /* always disable UTF-8 (for interactive) */
501                         utf_flag = 0;
502 #endif
503 #endif
504                 }
505 #ifndef MKSH_NO_CMDLINE_EDITING
506                 x_init();
507 #endif
508         }
509
510 #ifdef SIGWINCH
511         sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
512         setsig(&sigtraps[SIGWINCH], x_sigwinch,
513             SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
514 #endif
515
516         l = e->loc;
517         if (Flag(FAS_BUILTIN)) {
518                 l->argc = argc;
519                 l->argv = argv;
520                 l->argv[0] = ccp;
521         } else {
522                 l->argc = argc - argi;
523                 /*
524                  * allocate a new array because otherwise, when we modify
525                  * it in-place, ps(1) output changes; the meaning of argc
526                  * here is slightly different as it excludes kshname, and
527                  * we add a trailing NULL sentinel as well
528                  */
529                 l->argv = alloc2(l->argc + 2, sizeof(void *), APERM);
530                 l->argv[0] = kshname;
531                 memcpy(&l->argv[1], &argv[argi], l->argc * sizeof(void *));
532                 l->argv[l->argc + 1] = NULL;
533                 getopts_reset(1);
534         }
535
536         /* divine the initial state of the utf8-mode Flag */
537         ccp = null;
538         switch (utf_flag) {
539
540         /* auto-detect from locale or environment */
541         case 4:
542 #if HAVE_SETLOCALE_CTYPE
543                 ccp = setlocale(LC_CTYPE, "");
544 #if HAVE_LANGINFO_CODESET
545                 if (!isuc(ccp))
546                         ccp = nl_langinfo(CODESET);
547 #endif
548                 if (!isuc(ccp))
549                         ccp = null;
550                 /* FALLTHROUGH */
551 #endif
552
553         /* auto-detect from environment */
554         case 3:
555                 /* these were imported from environ earlier */
556                 if (ccp == null)
557                         ccp = str_val(global("LC_ALL"));
558                 if (ccp == null)
559                         ccp = str_val(global("LC_CTYPE"));
560                 if (ccp == null)
561                         ccp = str_val(global("LANG"));
562                 UTFMODE = isuc(ccp);
563                 break;
564
565         /* not set on command line, not FTALKING */
566         case 2:
567         /* unknown values */
568         default:
569                 utf_flag = 0;
570                 /* FALLTHROUGH */
571
572         /* known values */
573         case 1:
574         case 0:
575                 UTFMODE = utf_flag;
576                 break;
577         }
578
579         /* Disable during .profile/ENV reading */
580         restricted_shell = Flag(FRESTRICTED);
581         Flag(FRESTRICTED) = 0;
582         errexit = Flag(FERREXIT);
583         Flag(FERREXIT) = 0;
584
585         /*
586          * Do this before profile/$ENV so that if it causes problems in them,
587          * user will know why things broke.
588          */
589         if (!current_wd[0] && Flag(FTALKING))
590                 warningf(false, "can't determine current directory");
591
592         if (Flag(FLOGIN))
593                 include(MKSH_SYSTEM_PROFILE, 0, NULL, true);
594         if (!Flag(FPRIVILEGED)) {
595                 if (Flag(FLOGIN))
596                         include(substitute("$HOME/.profile", 0), 0, NULL, true);
597                 if (Flag(FTALKING)) {
598                         cp = substitute(substitute("${ENV:-" MKSHRC_PATH "}",
599                             0), DOTILDE);
600                         if (cp[0] != '\0')
601                                 include(cp, 0, NULL, true);
602                 }
603         } else {
604                 include(MKSH_SUID_PROFILE, 0, NULL, true);
605                 /* turn off -p if not set explicitly */
606                 if (Flag(FPRIVILEGED) != 1)
607                         change_flag(FPRIVILEGED, OF_INTERNAL, false);
608         }
609
610         if (restricted_shell) {
611                 shcomexec(restr_com);
612                 /* After typeset command... */
613                 Flag(FRESTRICTED) = 1;
614         }
615         Flag(FERREXIT) = errexit;
616
617         if (Flag(FTALKING) && s)
618                 hist_init(s);
619         else
620                 /* set after ENV */
621                 Flag(FTRACKALL) = 1;
622
623         alarm_init();
624
625         *sp = s;
626         *lp = l;
627         return (0);
628 }
629
630 /* this indirection barrier reduces stack usage during normal operation */
631
632 int
633 main(int argc, const char *argv[])
634 {
635         int rv;
636         Source *s;
637         struct block *l;
638
639         if ((rv = main_init(argc, argv, &s, &l)) == 0) {
640                 if (Flag(FAS_BUILTIN)) {
641                         rv = shcomexec(l->argv);
642                 } else {
643                         shell(s, true);
644                         /* NOTREACHED */
645                 }
646         }
647         return (rv);
648 }
649
650 int
651 include(const char *name, int argc, const char **argv, bool intr_ok)
652 {
653         Source *volatile s = NULL;
654         struct shf *shf;
655         const char **volatile old_argv;
656         volatile int old_argc;
657         int i;
658
659         shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
660         if (shf == NULL)
661                 return (-1);
662
663         if (argv) {
664                 old_argv = e->loc->argv;
665                 old_argc = e->loc->argc;
666         } else {
667                 old_argv = NULL;
668                 old_argc = 0;
669         }
670         newenv(E_INCL);
671         if ((i = kshsetjmp(e->jbuf))) {
672                 quitenv(s ? s->u.shf : NULL);
673                 if (old_argv) {
674                         e->loc->argv = old_argv;
675                         e->loc->argc = old_argc;
676                 }
677                 switch (i) {
678                 case LRETURN:
679                 case LERROR:
680                         /* see below */
681                         return (exstat & 0xFF);
682                 case LINTR:
683                         /*
684                          * intr_ok is set if we are including .profile or $ENV.
685                          * If user ^Cs out, we don't want to kill the shell...
686                          */
687                         if (intr_ok && ((exstat & 0xFF) - 128) != SIGTERM)
688                                 return (1);
689                         /* FALLTHROUGH */
690                 case LEXIT:
691                 case LLEAVE:
692                 case LSHELL:
693                         unwind(i);
694                         /* NOTREACHED */
695                 default:
696                         internal_errorf("%s %d", "include", i);
697                         /* NOTREACHED */
698                 }
699         }
700         if (argv) {
701                 e->loc->argv = argv;
702                 e->loc->argc = argc;
703         }
704         s = pushs(SFILE, ATEMP);
705         s->u.shf = shf;
706         strdupx(s->file, name, ATEMP);
707         i = shell(s, false);
708         quitenv(s->u.shf);
709         if (old_argv) {
710                 e->loc->argv = old_argv;
711                 e->loc->argc = old_argc;
712         }
713         /* & 0xff to ensure value not -1 */
714         return (i & 0xFF);
715 }
716
717 /* spawn a command into a shell optionally keeping track of the line number */
718 int
719 command(const char *comm, int line)
720 {
721         Source *s;
722
723         s = pushs(SSTRING, ATEMP);
724         s->start = s->str = comm;
725         s->line = line;
726         return (shell(s, false));
727 }
728
729 /*
730  * run the commands from the input source, returning status.
731  */
732 int
733 shell(Source * volatile s, volatile bool toplevel)
734 {
735         struct op *t;
736         volatile bool wastty = tobool(s->flags & SF_TTY);
737         volatile uint8_t attempts = 13;
738         volatile bool interactive = Flag(FTALKING) && toplevel;
739         volatile bool sfirst = true;
740         Source *volatile old_source = source;
741         int i;
742
743         newenv(E_PARSE);
744         if (interactive)
745                 really_exit = false;
746         switch ((i = kshsetjmp(e->jbuf))) {
747         case 0:
748                 break;
749         case LINTR:
750                 /* we get here if SIGINT not caught or ignored */
751         case LERROR:
752         case LSHELL:
753                 if (interactive) {
754                         if (i == LINTR)
755                                 shellf("\n");
756                         /*
757                          * Reset any eof that was read as part of a
758                          * multiline command.
759                          */
760                         if (Flag(FIGNOREEOF) && s->type == SEOF && wastty)
761                                 s->type = SSTDIN;
762                         /*
763                          * Used by exit command to get back to
764                          * top level shell. Kind of strange since
765                          * interactive is set if we are reading from
766                          * a tty, but to have stopped jobs, one only
767                          * needs FMONITOR set (not FTALKING/SF_TTY)...
768                          */
769                         /* toss any input we have so far */
770                         yyrecursive_pop(true);
771                         s->start = s->str = null;
772                         retrace_info = NULL;
773                         herep = heres;
774                         break;
775                 }
776                 /* FALLTHROUGH */
777         case LEXIT:
778         case LLEAVE:
779         case LRETURN:
780                 source = old_source;
781                 quitenv(NULL);
782                 /* keep on going */
783                 unwind(i);
784                 /* NOTREACHED */
785         default:
786                 source = old_source;
787                 quitenv(NULL);
788                 internal_errorf("%s %d", "shell", i);
789                 /* NOTREACHED */
790         }
791         while (/* CONSTCOND */ 1) {
792                 if (trap)
793                         runtraps(0);
794
795                 if (s->next == NULL) {
796                         if (Flag(FVERBOSE))
797                                 s->flags |= SF_ECHO;
798                         else
799                                 s->flags &= ~SF_ECHO;
800                 }
801                 if (interactive) {
802                         j_notify();
803                         set_prompt(PS1, s);
804                 }
805                 t = compile(s, sfirst);
806                 sfirst = false;
807                 if (!t)
808                         goto source_no_tree;
809                 if (t->type == TEOF) {
810                         if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
811                                 shellf("Use 'exit' to leave mksh\n");
812                                 s->type = SSTDIN;
813                         } else if (wastty && !really_exit &&
814                             j_stopped_running()) {
815                                 really_exit = true;
816                                 s->type = SSTDIN;
817                         } else {
818                                 /*
819                                  * this for POSIX which says EXIT traps
820                                  * shall be taken in the environment
821                                  * immediately after the last command
822                                  * executed.
823                                  */
824                                 if (toplevel)
825                                         unwind(LEXIT);
826                                 break;
827                         }
828                 } else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
829                         t->u.evalflags |= DOTCOMEXEC;
830                 if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
831                         exstat = execute(t, 0, NULL) & 0xFF;
832
833                 if (t->type != TEOF && interactive && really_exit)
834                         really_exit = false;
835
836  source_no_tree:
837                 reclaim();
838         }
839         quitenv(NULL);
840         source = old_source;
841         return (exstat & 0xFF);
842 }
843
844 /* return to closest error handler or shell(), exit if none found */
845 /* note: i MUST NOT be 0 */
846 void
847 unwind(int i)
848 {
849         /*
850          * This is a kludge. We need to restore everything that was
851          * changed in the new environment, see cid 1005090337C7A669439
852          * and 10050903386452ACBF1, but fail to even save things most of
853          * the time. funcs.c:c_eval() changes FERREXIT temporarily to 0,
854          * which needs to be restored thus (related to Debian #696823).
855          * We did not save the shell flags, so we use a special or'd
856          * value here... this is mostly to clean up behind *other*
857          * callers of unwind(LERROR) here; exec.c has the regular case.
858          */
859         if (Flag(FERREXIT) & 0x80) {
860                 /* GNU bash does not run this trapsig */
861                 trapsig(ksh_SIGERR);
862                 Flag(FERREXIT) &= ~0x80;
863         }
864
865         /* ordering for EXIT vs ERR is a bit odd (this is what AT&T ksh does) */
866         if (i == LEXIT || ((i == LERROR || i == LINTR) &&
867             sigtraps[ksh_SIGEXIT].trap &&
868             (!Flag(FTALKING) || Flag(FERREXIT)))) {
869                 ++trap_nested;
870                 runtrap(&sigtraps[ksh_SIGEXIT], trap_nested == 1);
871                 --trap_nested;
872                 i = LLEAVE;
873         } else if (Flag(FERREXIT) == 1 && (i == LERROR || i == LINTR)) {
874                 ++trap_nested;
875                 runtrap(&sigtraps[ksh_SIGERR], trap_nested == 1);
876                 --trap_nested;
877                 i = LLEAVE;
878         }
879
880         while (/* CONSTCOND */ 1) {
881                 switch (e->type) {
882                 case E_PARSE:
883                 case E_FUNC:
884                 case E_INCL:
885                 case E_LOOP:
886                 case E_ERRH:
887                         kshlongjmp(e->jbuf, i);
888                         /* NOTREACHED */
889                 case E_NONE:
890                         if (i == LINTR)
891                                 e->flags |= EF_FAKE_SIGDIE;
892                         /* FALLTHROUGH */
893                 default:
894                         quitenv(NULL);
895                         /*
896                          * quitenv() may have reclaimed the memory
897                          * used by source which will end badly when
898                          * we jump to a function that expects it to
899                          * be valid
900                          */
901                         source = NULL;
902                 }
903         }
904 }
905
906 void
907 newenv(int type)
908 {
909         struct env *ep;
910         char *cp;
911
912         /*
913          * struct env includes ALLOC_ITEM for alignment constraints
914          * so first get the actually used memory, then assign it
915          */
916         cp = alloc(sizeof(struct env) - ALLOC_SIZE, ATEMP);
917         /* undo what alloc() did to the malloc result address */
918         ep = (void *)(cp - ALLOC_SIZE);
919         /* initialise public members of struct env (not the ALLOC_ITEM) */
920         ainit(&ep->area);
921         ep->oenv = e;
922         ep->loc = e->loc;
923         ep->savefd = NULL;
924         ep->temps = NULL;
925         ep->yyrecursive_statep = NULL;
926         ep->type = type;
927         ep->flags = 0;
928         /* jump buffer is invalid because flags == 0 */
929         e = ep;
930 }
931
932 void
933 quitenv(struct shf *shf)
934 {
935         struct env *ep = e;
936         char *cp;
937         int fd;
938
939         yyrecursive_pop(true);
940         while (ep->oenv && ep->oenv->loc != ep->loc)
941                 popblock();
942         if (ep->savefd != NULL) {
943                 for (fd = 0; fd < NUFILE; fd++)
944                         /* if ep->savefd[fd] < 0, means fd was closed */
945                         if (ep->savefd[fd])
946                                 restfd(fd, ep->savefd[fd]);
947                 if (ep->savefd[2])
948                         /* Clear any write errors */
949                         shf_reopen(2, SHF_WR, shl_out);
950         }
951         /*
952          * Bottom of the stack.
953          * Either main shell is exiting or cleanup_parents_env() was called.
954          */
955         if (ep->oenv == NULL) {
956 #ifdef DEBUG_LEAKS
957                 int i;
958 #endif
959
960                 if (ep->type == E_NONE) {
961                         /* Main shell exiting? */
962 #if HAVE_PERSISTENT_HISTORY
963                         if (Flag(FTALKING))
964                                 hist_finish();
965 #endif
966                         j_exit();
967                         if (ep->flags & EF_FAKE_SIGDIE) {
968                                 int sig = (exstat & 0xFF) - 128;
969
970                                 /*
971                                  * ham up our death a bit (AT&T ksh
972                                  * only seems to do this for SIGTERM)
973                                  * Don't do it for SIGQUIT, since we'd
974                                  * dump a core..
975                                  */
976                                 if ((sig == SIGINT || sig == SIGTERM) &&
977                                     (kshpgrp == kshpid)) {
978                                         setsig(&sigtraps[sig], SIG_DFL,
979                                             SS_RESTORE_CURR | SS_FORCE);
980                                         kill(0, sig);
981                                 }
982                         }
983                 }
984                 if (shf)
985                         shf_close(shf);
986                 reclaim();
987 #ifdef DEBUG_LEAKS
988 #ifndef MKSH_NO_CMDLINE_EDITING
989                 x_done();
990 #endif
991 #ifndef MKSH_NOPROSPECTOFWORK
992                 /* block at least SIGCHLD during/after afreeall */
993                 sigprocmask(SIG_BLOCK, &sm_sigchld, NULL);
994 #endif
995                 afreeall(APERM);
996                 for (fd = 3; fd < NUFILE; fd++)
997                         if ((i = fcntl(fd, F_GETFD, 0)) != -1 &&
998                             (i & FD_CLOEXEC))
999                                 close(fd);
1000                 close(2);
1001                 close(1);
1002                 close(0);
1003 #endif
1004                 exit(exstat & 0xFF);
1005         }
1006         if (shf)
1007                 shf_close(shf);
1008         reclaim();
1009
1010         e = e->oenv;
1011
1012         /* free the struct env - tricky due to the ALLOC_ITEM inside */
1013         cp = (void *)ep;
1014         afree(cp + ALLOC_SIZE, ATEMP);
1015 }
1016
1017 /* Called after a fork to cleanup stuff left over from parents environment */
1018 void
1019 cleanup_parents_env(void)
1020 {
1021         struct env *ep;
1022         int fd;
1023
1024         /*
1025          * Don't clean up temporary files - parent will probably need them.
1026          * Also, can't easily reclaim memory since variables, etc. could be
1027          * anywhere.
1028          */
1029
1030         /* close all file descriptors hiding in savefd */
1031         for (ep = e; ep; ep = ep->oenv) {
1032                 if (ep->savefd) {
1033                         for (fd = 0; fd < NUFILE; fd++)
1034                                 if (ep->savefd[fd] > 0)
1035                                         close(ep->savefd[fd]);
1036                         afree(ep->savefd, &ep->area);
1037                         ep->savefd = NULL;
1038                 }
1039 #ifdef DEBUG_LEAKS
1040                 if (ep->type != E_NONE)
1041                         ep->type = E_GONE;
1042 #endif
1043         }
1044 #ifndef DEBUG_LEAKS
1045         e->oenv = NULL;
1046 #endif
1047 }
1048
1049 /* Called just before an execve cleanup stuff temporary files */
1050 void
1051 cleanup_proc_env(void)
1052 {
1053         struct env *ep;
1054
1055         for (ep = e; ep; ep = ep->oenv)
1056                 remove_temps(ep->temps);
1057 }
1058
1059 /* remove temp files and free ATEMP Area */
1060 static void
1061 reclaim(void)
1062 {
1063         struct block *l;
1064
1065         while ((l = e->loc) && (!e->oenv || e->oenv->loc != l)) {
1066                 e->loc = l->next;
1067                 afreeall(&l->area);
1068         }
1069
1070         remove_temps(e->temps);
1071         e->temps = NULL;
1072         afreeall(&e->area);
1073 }
1074
1075 static void
1076 remove_temps(struct temp *tp)
1077 {
1078         for (; tp != NULL; tp = tp->next)
1079                 if (tp->pid == procpid)
1080                         unlink(tp->tffn);
1081 }
1082
1083 /*
1084  * Initialise tty_fd. Used for tracking the size of the terminal,
1085  * saving/resetting tty modes upon forground job completion, and
1086  * for setting up the tty process group. Return values:
1087  *      0 = got controlling tty
1088  *      1 = got terminal but no controlling tty
1089  *      2 = cannot find a terminal
1090  *      3 = cannot dup fd
1091  *      4 = cannot make fd close-on-exec
1092  * An existing tty_fd is cached if no "better" one could be found,
1093  * i.e. if tty_devtty was already set or the new would not set it.
1094  */
1095 int
1096 tty_init_fd(void)
1097 {
1098         int fd, rv, eno = 0;
1099         bool do_close = false, is_devtty = true;
1100
1101         if (tty_devtty) {
1102                 /* already got a tty which is /dev/tty */
1103                 return (0);
1104         }
1105
1106 #ifdef _UWIN
1107         /*XXX imake style */
1108         if (isatty(3)) {
1109                 /* fd 3 on UWIN _is_ /dev/tty (or our controlling tty) */
1110                 fd = 3;
1111                 goto got_fd;
1112         }
1113 #endif
1114         if ((fd = open("/dev/tty", O_RDWR, 0)) >= 0) {
1115                 do_close = true;
1116                 goto got_fd;
1117         }
1118         eno = errno;
1119
1120         if (tty_fd >= 0) {
1121                 /* already got a non-devtty one */
1122                 rv = 1;
1123                 goto out;
1124         }
1125         is_devtty = false;
1126
1127         if (isatty((fd = 0)) || isatty((fd = 2)))
1128                 goto got_fd;
1129         /* cannot find one */
1130         rv = 2;
1131         /* assert: do_close == false */
1132         goto out;
1133
1134  got_fd:
1135         if ((rv = fcntl(fd, F_DUPFD, FDBASE)) < 0) {
1136                 eno = errno;
1137                 rv = 3;
1138                 goto out;
1139         }
1140         if (fcntl(rv, F_SETFD, FD_CLOEXEC) < 0) {
1141                 eno = errno;
1142                 close(rv);
1143                 rv = 4;
1144                 goto out;
1145         }
1146         tty_fd = rv;
1147         tty_devtty = is_devtty;
1148         rv = eno = 0;
1149  out:
1150         if (do_close)
1151                 close(fd);
1152         errno = eno;
1153         return (rv);
1154 }
1155
1156 /* A shell error occurred (eg, syntax error, etc.) */
1157
1158 #define VWARNINGF_ERRORPREFIX   1
1159 #define VWARNINGF_FILELINE      2
1160 #define VWARNINGF_BUILTIN       4
1161 #define VWARNINGF_INTERNAL      8
1162
1163 static void vwarningf(unsigned int, const char *, va_list)
1164     MKSH_A_FORMAT(__printf__, 2, 0);
1165
1166 static void
1167 vwarningf(unsigned int flags, const char *fmt, va_list ap)
1168 {
1169         if (fmt) {
1170                 if (flags & VWARNINGF_INTERNAL)
1171                         shf_fprintf(shl_out, "internal error: ");
1172                 if (flags & VWARNINGF_ERRORPREFIX)
1173                         error_prefix(tobool(flags & VWARNINGF_FILELINE));
1174                 if ((flags & VWARNINGF_BUILTIN) &&
1175                     /* not set when main() calls parse_args() */
1176                     builtin_argv0 && builtin_argv0 != kshname)
1177                         shf_fprintf(shl_out, "%s: ", builtin_argv0);
1178                 shf_vfprintf(shl_out, fmt, ap);
1179                 shf_putchar('\n', shl_out);
1180         }
1181         shf_flush(shl_out);
1182 }
1183
1184 void
1185 errorfx(int rc, const char *fmt, ...)
1186 {
1187         va_list va;
1188
1189         exstat = rc;
1190
1191         /* debugging: note that stdout not valid */
1192         shl_stdout_ok = false;
1193
1194         va_start(va, fmt);
1195         vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1196         va_end(va);
1197         unwind(LERROR);
1198 }
1199
1200 void
1201 errorf(const char *fmt, ...)
1202 {
1203         va_list va;
1204
1205         exstat = 1;
1206
1207         /* debugging: note that stdout not valid */
1208         shl_stdout_ok = false;
1209
1210         va_start(va, fmt);
1211         vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1212         va_end(va);
1213         unwind(LERROR);
1214 }
1215
1216 /* like errorf(), but no unwind is done */
1217 void
1218 warningf(bool fileline, const char *fmt, ...)
1219 {
1220         va_list va;
1221
1222         va_start(va, fmt);
1223         vwarningf(VWARNINGF_ERRORPREFIX | (fileline ? VWARNINGF_FILELINE : 0),
1224             fmt, va);
1225         va_end(va);
1226 }
1227
1228 /*
1229  * Used by built-in utilities to prefix shell and utility name to message
1230  * (also unwinds environments for special builtins).
1231  */
1232 void
1233 bi_errorf(const char *fmt, ...)
1234 {
1235         va_list va;
1236
1237         /* debugging: note that stdout not valid */
1238         shl_stdout_ok = false;
1239
1240         exstat = 1;
1241
1242         va_start(va, fmt);
1243         vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE |
1244             VWARNINGF_BUILTIN, fmt, va);
1245         va_end(va);
1246
1247         /*
1248          * POSIX special builtins and ksh special builtins cause
1249          * non-interactive shells to exit.
1250          * XXX odd use of KEEPASN; also may not want LERROR here
1251          */
1252         if (builtin_spec) {
1253                 builtin_argv0 = NULL;
1254                 unwind(LERROR);
1255         }
1256 }
1257
1258 /* Called when something that shouldn't happen does */
1259 void
1260 internal_errorf(const char *fmt, ...)
1261 {
1262         va_list va;
1263
1264         va_start(va, fmt);
1265         vwarningf(VWARNINGF_INTERNAL, fmt, va);
1266         va_end(va);
1267         unwind(LERROR);
1268 }
1269
1270 void
1271 internal_warningf(const char *fmt, ...)
1272 {
1273         va_list va;
1274
1275         va_start(va, fmt);
1276         vwarningf(VWARNINGF_INTERNAL, fmt, va);
1277         va_end(va);
1278 }
1279
1280 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
1281 void
1282 error_prefix(bool fileline)
1283 {
1284         /* Avoid foo: foo[2]: ... */
1285         if (!fileline || !source || !source->file ||
1286             strcmp(source->file, kshname) != 0)
1287                 shf_fprintf(shl_out, "%s: ", kshname + (*kshname == '-'));
1288         if (fileline && source && source->file != NULL) {
1289                 shf_fprintf(shl_out, "%s[%d]: ", source->file,
1290                     source->errline > 0 ? source->errline : source->line);
1291                 source->errline = 0;
1292         }
1293 }
1294
1295 /* printf to shl_out (stderr) with flush */
1296 void
1297 shellf(const char *fmt, ...)
1298 {
1299         va_list va;
1300
1301         if (!initio_done)
1302                 /* shl_out may not be set up yet... */
1303                 return;
1304         va_start(va, fmt);
1305         shf_vfprintf(shl_out, fmt, va);
1306         va_end(va);
1307         shf_flush(shl_out);
1308 }
1309
1310 /* printf to shl_stdout (stdout) */
1311 void
1312 shprintf(const char *fmt, ...)
1313 {
1314         va_list va;
1315
1316         if (!shl_stdout_ok)
1317                 internal_errorf("shl_stdout not valid");
1318         va_start(va, fmt);
1319         shf_vfprintf(shl_stdout, fmt, va);
1320         va_end(va);
1321 }
1322
1323 /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
1324 int
1325 can_seek(int fd)
1326 {
1327         struct stat statb;
1328
1329         return (fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
1330             SHF_UNBUF : 0);
1331 }
1332
1333 #ifdef DF
1334 int shl_dbg_fd;
1335 #define NSHF_IOB 4
1336 #else
1337 #define NSHF_IOB 3
1338 #endif
1339 struct shf shf_iob[NSHF_IOB];
1340
1341 void
1342 initio(void)
1343 {
1344 #ifdef DF
1345         const char *lfp;
1346 #endif
1347
1348         /* force buffer allocation */
1349         shf_fdopen(1, SHF_WR, shl_stdout);
1350         shf_fdopen(2, SHF_WR, shl_out);
1351         shf_fdopen(2, SHF_WR, shl_xtrace);
1352 #ifdef DF
1353         if ((lfp = getenv("SDMKSH_PATH")) == NULL) {
1354                 if ((lfp = getenv("HOME")) == NULL || *lfp != '/')
1355                         errorf("cannot get home directory");
1356                 lfp = shf_smprintf("%s/mksh-dbg.txt", lfp);
1357         }
1358
1359         if ((shl_dbg_fd = open(lfp, O_WRONLY | O_APPEND | O_CREAT, 0600)) < 0)
1360                 errorf("cannot open debug output file %s", lfp);
1361         if (shl_dbg_fd < FDBASE) {
1362                 int nfd;
1363
1364                 nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
1365                 close(shl_dbg_fd);
1366                 if ((shl_dbg_fd = nfd) == -1)
1367                         errorf("cannot dup debug output file");
1368         }
1369         fcntl(shl_dbg_fd, F_SETFD, FD_CLOEXEC);
1370         shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
1371         DF("=== open ===");
1372 #endif
1373         initio_done = true;
1374 }
1375
1376 /* A dup2() with error checking */
1377 int
1378 ksh_dup2(int ofd, int nfd, bool errok)
1379 {
1380         int rv;
1381
1382         if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
1383                 errorf("too many files open in shell");
1384
1385 #ifdef __ultrix
1386         /*XXX imake style */
1387         if (rv >= 0)
1388                 fcntl(nfd, F_SETFD, 0);
1389 #endif
1390
1391         return (rv);
1392 }
1393
1394 /*
1395  * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10),
1396  * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here.
1397  */
1398 short
1399 savefd(int fd)
1400 {
1401         int nfd = fd;
1402
1403         if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
1404             errno == EBADF)
1405                 return (-1);
1406         if (nfd < 0 || nfd > SHRT_MAX)
1407                 errorf("too many files open in shell");
1408         fcntl(nfd, F_SETFD, FD_CLOEXEC);
1409         return ((short)nfd);
1410 }
1411
1412 void
1413 restfd(int fd, int ofd)
1414 {
1415         if (fd == 2)
1416                 shf_flush(&shf_iob[/* fd */ 2]);
1417         if (ofd < 0)
1418                 /* original fd closed */
1419                 close(fd);
1420         else if (fd != ofd) {
1421                 /*XXX: what to do if this dup fails? */
1422                 ksh_dup2(ofd, fd, true);
1423                 close(ofd);
1424         }
1425 }
1426
1427 void
1428 openpipe(int *pv)
1429 {
1430         int lpv[2];
1431
1432         if (pipe(lpv) < 0)
1433                 errorf("can't create pipe - try again");
1434         pv[0] = savefd(lpv[0]);
1435         if (pv[0] != lpv[0])
1436                 close(lpv[0]);
1437         pv[1] = savefd(lpv[1]);
1438         if (pv[1] != lpv[1])
1439                 close(lpv[1]);
1440 }
1441
1442 void
1443 closepipe(int *pv)
1444 {
1445         close(pv[0]);
1446         close(pv[1]);
1447 }
1448
1449 /*
1450  * Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
1451  * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
1452  */
1453 int
1454 check_fd(const char *name, int mode, const char **emsgp)
1455 {
1456         int fd, fl;
1457
1458         if (name[0] == 'p' && !name[1])
1459                 return (coproc_getfd(mode, emsgp));
1460         for (fd = 0; ksh_isdigit(*name); ++name)
1461                 fd = (fd * 10) + *name - '0';
1462         if (*name || fd >= FDBASE) {
1463                 if (emsgp)
1464                         *emsgp = "illegal file descriptor name";
1465                 return (-1);
1466         }
1467         if ((fl = fcntl(fd, F_GETFL, 0)) < 0) {
1468                 if (emsgp)
1469                         *emsgp = "bad file descriptor";
1470                 return (-1);
1471         }
1472         fl &= O_ACCMODE;
1473         /*
1474          * X_OK is a kludge to disable this check for dups (x<&1):
1475          * historical shells never did this check (XXX don't know what
1476          * POSIX has to say).
1477          */
1478         if (!(mode & X_OK) && fl != O_RDWR && (
1479             ((mode & R_OK) && fl != O_RDONLY) ||
1480             ((mode & W_OK) && fl != O_WRONLY))) {
1481                 if (emsgp)
1482                         *emsgp = (fl == O_WRONLY) ?
1483                             "fd not open for reading" :
1484                             "fd not open for writing";
1485                 return (-1);
1486         }
1487         return (fd);
1488 }
1489
1490 /* Called once from main */
1491 void
1492 coproc_init(void)
1493 {
1494         coproc.read = coproc.readw = coproc.write = -1;
1495         coproc.njobs = 0;
1496         coproc.id = 0;
1497 }
1498
1499 /* Called by c_read() when eof is read - close fd if it is the co-process fd */
1500 void
1501 coproc_read_close(int fd)
1502 {
1503         if (coproc.read >= 0 && fd == coproc.read) {
1504                 coproc_readw_close(fd);
1505                 close(coproc.read);
1506                 coproc.read = -1;
1507         }
1508 }
1509
1510 /*
1511  * Called by c_read() and by iosetup() to close the other side of the
1512  * read pipe, so reads will actually terminate.
1513  */
1514 void
1515 coproc_readw_close(int fd)
1516 {
1517         if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
1518                 close(coproc.readw);
1519                 coproc.readw = -1;
1520         }
1521 }
1522
1523 /*
1524  * Called by c_print when a write to a fd fails with EPIPE and by iosetup
1525  * when co-process input is dup'd
1526  */
1527 void
1528 coproc_write_close(int fd)
1529 {
1530         if (coproc.write >= 0 && fd == coproc.write) {
1531                 close(coproc.write);
1532                 coproc.write = -1;
1533         }
1534 }
1535
1536 /*
1537  * Called to check for existence of/value of the co-process file descriptor.
1538  * (Used by check_fd() and by c_read/c_print to deal with -p option).
1539  */
1540 int
1541 coproc_getfd(int mode, const char **emsgp)
1542 {
1543         int fd = (mode & R_OK) ? coproc.read : coproc.write;
1544
1545         if (fd >= 0)
1546                 return (fd);
1547         if (emsgp)
1548                 *emsgp = "no coprocess";
1549         return (-1);
1550 }
1551
1552 /*
1553  * called to close file descriptors related to the coprocess (if any)
1554  * Should be called with SIGCHLD blocked.
1555  */
1556 void
1557 coproc_cleanup(int reuse)
1558 {
1559         /* This to allow co-processes to share output pipe */
1560         if (!reuse || coproc.readw < 0 || coproc.read < 0) {
1561                 if (coproc.read >= 0) {
1562                         close(coproc.read);
1563                         coproc.read = -1;
1564                 }
1565                 if (coproc.readw >= 0) {
1566                         close(coproc.readw);
1567                         coproc.readw = -1;
1568                 }
1569         }
1570         if (coproc.write >= 0) {
1571                 close(coproc.write);
1572                 coproc.write = -1;
1573         }
1574 }
1575
1576 struct temp *
1577 maketemp(Area *ap, Temp_type type, struct temp **tlist)
1578 {
1579         char *cp;
1580         size_t len;
1581         int i, j;
1582         struct temp *tp;
1583         const char *dir;
1584         struct stat sb;
1585
1586         dir = tmpdir ? tmpdir : MKSH_DEFAULT_TMPDIR;
1587         /* add "/shXXXXXX.tmp" plus NUL */
1588         len = strlen(dir);
1589         checkoktoadd(len, offsetof(struct temp, tffn[0]) + 14);
1590         tp = alloc(offsetof(struct temp, tffn[0]) + 14 + len, ap);
1591
1592         tp->shf = NULL;
1593         tp->pid = procpid;
1594         tp->type = type;
1595
1596         if (stat(dir, &sb) || !S_ISDIR(sb.st_mode)) {
1597                 tp->tffn[0] = '\0';
1598                 goto maketemp_out;
1599         }
1600
1601         cp = (void *)tp;
1602         cp += offsetof(struct temp, tffn[0]);
1603         memcpy(cp, dir, len);
1604         cp += len;
1605         memcpy(cp, "/shXXXXXX.tmp", 14);
1606         /* point to the first of six Xes */
1607         cp += 3;
1608         /* generate random part of filename */
1609         len = -1;
1610         do {
1611                 i = rndget() % 36;
1612                 cp[++len] = i < 26 ? 'a' + i : '0' + i - 26;
1613         } while (len < 5);
1614
1615         /* cyclically attempt to open a temporary file */
1616         while ((i = open(tp->tffn, O_CREAT | O_EXCL | O_RDWR | O_BINARY,
1617             0600)) < 0) {
1618                 if (errno != EEXIST)
1619                         goto maketemp_out;
1620                 /* count down from z to a then from 9 to 0 */
1621                 while (cp[len] == '0')
1622                         if (!len--)
1623                                 goto maketemp_out;
1624                 if (cp[len] == 'a')
1625                         cp[len] = '9';
1626                 else
1627                         --cp[len];
1628                 /* do another cycle */
1629         }
1630
1631         if (type == TT_FUNSUB) {
1632                 /* map us high and mark as close-on-exec */
1633                 if ((j = savefd(i)) != i) {
1634                         close(i);
1635                         i = j;
1636                 }
1637
1638                 /* operation mode for the shf */
1639                 j = SHF_RD;
1640         } else
1641                 j = SHF_WR;
1642
1643         /* shf_fdopen cannot fail, so no fd leak */
1644         tp->shf = shf_fdopen(i, j, NULL);
1645
1646  maketemp_out:
1647         tp->next = *tlist;
1648         *tlist = tp;
1649         return (tp);
1650 }
1651
1652 /*
1653  * We use a similar collision resolution algorithm as Python 2.5.4
1654  * but with a slightly tweaked implementation written from scratch.
1655  */
1656
1657 #define INIT_TBLSHIFT   3       /* initial table shift (2^3 = 8) */
1658 #define PERTURB_SHIFT   5       /* see Python 2.5.4 Objects/dictobject.c */
1659
1660 static void tgrow(struct table *);
1661 static int tnamecmp(const void *, const void *);
1662
1663 static void
1664 tgrow(struct table *tp)
1665 {
1666         size_t i, j, osize, mask, perturb;
1667         struct tbl *tblp, **pp;
1668         struct tbl **ntblp, **otblp = tp->tbls;
1669
1670         if (tp->tshift > 29)
1671                 internal_errorf("hash table size limit reached");
1672
1673         /* calculate old size, new shift and new size */
1674         osize = (size_t)1 << (tp->tshift++);
1675         i = osize << 1;
1676
1677         ntblp = alloc2(i, sizeof(struct tbl *), tp->areap);
1678         /* multiplication cannot overflow: alloc2 checked that */
1679         memset(ntblp, 0, i * sizeof(struct tbl *));
1680
1681         /* table can get very full when reaching its size limit */
1682         tp->nfree = (tp->tshift == 30) ? 0x3FFF0000UL :
1683             /* but otherwise, only 75% */
1684             ((i * 3) / 4);
1685         tp->tbls = ntblp;
1686         if (otblp == NULL)
1687                 return;
1688
1689         mask = i - 1;
1690         for (i = 0; i < osize; i++)
1691                 if ((tblp = otblp[i]) != NULL) {
1692                         if ((tblp->flag & DEFINED)) {
1693                                 /* search for free hash table slot */
1694                                 j = perturb = tblp->ua.hval;
1695                                 goto find_first_empty_slot;
1696  find_next_empty_slot:
1697                                 j = (j << 2) + j + perturb + 1;
1698                                 perturb >>= PERTURB_SHIFT;
1699  find_first_empty_slot:
1700                                 pp = &ntblp[j & mask];
1701                                 if (*pp != NULL)
1702                                         goto find_next_empty_slot;
1703                                 /* found an empty hash table slot */
1704                                 *pp = tblp;
1705                                 tp->nfree--;
1706                         } else if (!(tblp->flag & FINUSE)) {
1707                                 afree(tblp, tp->areap);
1708                         }
1709                 }
1710         afree(otblp, tp->areap);
1711 }
1712
1713 void
1714 ktinit(Area *ap, struct table *tp, uint8_t initshift)
1715 {
1716         tp->areap = ap;
1717         tp->tbls = NULL;
1718         tp->tshift = ((initshift > INIT_TBLSHIFT) ?
1719             initshift : INIT_TBLSHIFT) - 1;
1720         tgrow(tp);
1721 }
1722
1723 /* table, name (key) to search for, hash(name), rv pointer to tbl ptr */
1724 struct tbl *
1725 ktscan(struct table *tp, const char *name, uint32_t h, struct tbl ***ppp)
1726 {
1727         size_t j, perturb, mask;
1728         struct tbl **pp, *p;
1729
1730         mask = ((size_t)1 << (tp->tshift)) - 1;
1731         /* search for hash table slot matching name */
1732         j = perturb = h;
1733         goto find_first_slot;
1734  find_next_slot:
1735         j = (j << 2) + j + perturb + 1;
1736         perturb >>= PERTURB_SHIFT;
1737  find_first_slot:
1738         pp = &tp->tbls[j & mask];
1739         if ((p = *pp) != NULL && (p->ua.hval != h || !(p->flag & DEFINED) ||
1740             strcmp(p->name, name)))
1741                 goto find_next_slot;
1742         /* p == NULL if not found, correct found entry otherwise */
1743         if (ppp)
1744                 *ppp = pp;
1745         return (p);
1746 }
1747
1748 /* table, name (key) to enter, hash(n) */
1749 struct tbl *
1750 ktenter(struct table *tp, const char *n, uint32_t h)
1751 {
1752         struct tbl **pp, *p;
1753         size_t len;
1754
1755  Search:
1756         if ((p = ktscan(tp, n, h, &pp)))
1757                 return (p);
1758
1759         if (tp->nfree == 0) {
1760                 /* too full */
1761                 tgrow(tp);
1762                 goto Search;
1763         }
1764
1765         /* create new tbl entry */
1766         len = strlen(n);
1767         checkoktoadd(len, offsetof(struct tbl, name[0]) + 1);
1768         p = alloc(offsetof(struct tbl, name[0]) + ++len, tp->areap);
1769         p->flag = 0;
1770         p->type = 0;
1771         p->areap = tp->areap;
1772         p->ua.hval = h;
1773         p->u2.field = 0;
1774         p->u.array = NULL;
1775         memcpy(p->name, n, len);
1776
1777         /* enter in tp->tbls */
1778         tp->nfree--;
1779         *pp = p;
1780         return (p);
1781 }
1782
1783 void
1784 ktwalk(struct tstate *ts, struct table *tp)
1785 {
1786         ts->left = (size_t)1 << (tp->tshift);
1787         ts->next = tp->tbls;
1788 }
1789
1790 struct tbl *
1791 ktnext(struct tstate *ts)
1792 {
1793         while (--ts->left >= 0) {
1794                 struct tbl *p = *ts->next++;
1795                 if (p != NULL && (p->flag & DEFINED))
1796                         return (p);
1797         }
1798         return (NULL);
1799 }
1800
1801 static int
1802 tnamecmp(const void *p1, const void *p2)
1803 {
1804         const struct tbl *a = *((const struct tbl * const *)p1);
1805         const struct tbl *b = *((const struct tbl * const *)p2);
1806
1807         return (strcmp(a->name, b->name));
1808 }
1809
1810 struct tbl **
1811 ktsort(struct table *tp)
1812 {
1813         size_t i;
1814         struct tbl **p, **sp, **dp;
1815
1816         /*
1817          * since the table is never entirely full, no need to reserve
1818          * additional space for the trailing NULL appended below
1819          */
1820         i = (size_t)1 << (tp->tshift);
1821         p = alloc2(i, sizeof(struct tbl *), ATEMP);
1822         sp = tp->tbls;          /* source */
1823         dp = p;                 /* dest */
1824         while (i--)
1825                 if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
1826                     ((*dp)->flag & ARRAY)))
1827                         dp++;
1828         qsort(p, (i = dp - p), sizeof(struct tbl *), tnamecmp);
1829         p[i] = NULL;
1830         return (p);
1831 }
1832
1833 #ifdef SIGWINCH
1834 static void
1835 x_sigwinch(int sig MKSH_A_UNUSED)
1836 {
1837         /* this runs inside interrupt context, with errno saved */
1838
1839         got_winch = 1;
1840 }
1841 #endif
1842
1843 #ifdef DF
1844 void
1845 DF(const char *fmt, ...)
1846 {
1847         va_list args;
1848         struct timeval tv;
1849         mirtime_mjd mjd;
1850
1851         mksh_lockfd(shl_dbg_fd);
1852         mksh_TIME(tv);
1853         timet2mjd(&mjd, tv.tv_sec);
1854         shf_fprintf(shl_dbg, "[%02u:%02u:%02u (%u) %u.%06u] ",
1855             (unsigned)mjd.sec / 3600, ((unsigned)mjd.sec / 60) % 60,
1856             (unsigned)mjd.sec % 60, (unsigned)getpid(),
1857             (unsigned)tv.tv_sec, (unsigned)tv.tv_usec);
1858         va_start(args, fmt);
1859         shf_vfprintf(shl_dbg, fmt, args);
1860         va_end(args);
1861         shf_putc('\n', shl_dbg);
1862         shf_flush(shl_dbg);
1863         mksh_unlkfd(shl_dbg_fd);
1864 }
1865 #endif
1866
1867 void
1868 x_mkraw(int fd, mksh_ttyst *ocb, bool forread)
1869 {
1870         mksh_ttyst cb;
1871
1872         if (ocb)
1873                 mksh_tcget(fd, ocb);
1874         else
1875                 ocb = &tty_state;
1876
1877         cb = *ocb;
1878         if (forread) {
1879                 cb.c_iflag &= ~(ISTRIP);
1880                 cb.c_lflag &= ~(ICANON) | ECHO;
1881         } else {
1882                 cb.c_iflag &= ~(INLCR | ICRNL | ISTRIP);
1883                 cb.c_lflag &= ~(ISIG | ICANON | ECHO);
1884         }
1885 #if defined(VLNEXT) && defined(_POSIX_VDISABLE)
1886         /* OSF/1 processes lnext when ~icanon */
1887         cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
1888 #endif
1889         /* SunOS 4.1.x & OSF/1 processes discard(flush) when ~icanon */
1890 #if defined(VDISCARD) && defined(_POSIX_VDISABLE)
1891         cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
1892 #endif
1893         cb.c_cc[VTIME] = 0;
1894         cb.c_cc[VMIN] = 1;
1895
1896         mksh_tcset(fd, &cb);
1897 }