1 /* $OpenBSD: main.c,v 1.57 2015/09/10 22:48:58 nicm Exp $ */
2 /* $OpenBSD: tty.c,v 1.10 2014/08/10 02:44:26 guenther Exp $ */
3 /* $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 $ */
5
6 /*-
7 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
8 * 2011, 2012, 2013, 2014, 2015, 2016, 2017
9 * mirabilos <m@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.332 2017/04/12 16:01:45 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 MKSH_UNIXROOT "/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 initsubs[] =
60 "${PS2=> }"
61 "${PS3=#? }"
62 "${PS4=+ }"
63 "${SECONDS=0}"
64 "${TMOUT=0}"
65 "${EPOCHREALTIME=}";
66
67 static const char *initcoms[] = {
68 Ttypeset, "-r", initvsn, NULL,
69 Ttypeset, "-x", "HOME", TPATH, TSHELL, NULL,
70 Ttypeset, "-i10", "COLUMNS", "LINES", "SECONDS", "TMOUT", NULL,
71 Talias,
72 "integer=\\\\builtin typeset -i",
73 "local=\\\\builtin typeset",
74 /* not "alias -t --": hash -r needs to work */
75 "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",
81 "nohup=nohup ",
82 "r=\\\\builtin fc -e -",
83 "login=\\\\builtin exec login",
84 NULL,
85 /* this is what AT&T ksh seems to track, with the addition of emacs */
86 Talias, "-tU",
87 Tcat, "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
88 "make", "mv", "pr", "rm", "sed", Tsh, "vi", "who", NULL,
89 NULL
90 };
91
92 static const char *restr_com[] = {
93 Ttypeset, "-r", TPATH, "ENV", TSHELL, NULL
94 };
95
96 static bool initio_done;
97
98 /* top-level parsing and execution environment */
99 static struct env env;
100 struct env *e = &env;
101
102 /* 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 */
106 cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
107 (sizeof(unsigned char) == 1));
108 cta(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 */
111 cta(short_is_2_char, sizeof(short) == 2);
112 cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
113 /* the next assertion is probably not really needed */
114 cta(int_is_4_char, sizeof(int) == 4);
115 cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
116
117 cta(long_ge_int, sizeof(long) >= sizeof(int));
118 cta(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 */
122 cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
123 /* but this is */
124 cta(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 */
126 cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
127 /* but the next three are; we REQUIRE unsigned integer wraparound */
128 cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
129 cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
130 cta(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 */
135 cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
136 cta(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 */
138 cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
139
140 cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
141 cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
142 cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
143 /* our formatting routines assume this */
144 cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
145 cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
146
147 static mksh_uari_t
rndsetup(void)148 rndsetup(void)
149 {
150 register uint32_t h;
151 struct {
152 ALLOC_ITEM alloc_INT;
153 void *dataptr, *stkptr, *mallocptr;
154 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
155 sigjmp_buf jbuf;
156 #endif
157 struct timeval tv;
158 } *bufptr;
159 char *cp;
160
161 cp = alloc(sizeof(*bufptr) - sizeof(ALLOC_ITEM), APERM);
162 /* clear the allocated space, for valgrind and to avoid UB */
163 memset(cp, 0, sizeof(*bufptr) - sizeof(ALLOC_ITEM));
164 /* undo what alloc() did to the malloc result address */
165 bufptr = (void *)(cp - sizeof(ALLOC_ITEM));
166 /* 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;
172 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
173 /* glibc pointer guard */
174 sigsetjmp(bufptr->jbuf, 1);
175 #endif
176 /* introduce variation (and yes, second arg MBZ for portability) */
177 mksh_TIME(bufptr->tv);
178
179 #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
180 mprotect(((char *)bufptr) + 4096, 4096, PROT_READ | PROT_WRITE);
181 #endif
182 h = chvt_rndsetup(bufptr, sizeof(*bufptr));
183
184 afree(cp, APERM);
185 return ((mksh_uari_t)h);
186 }
187
188 void
chvt_reinit(void)189 chvt_reinit(void)
190 {
191 kshpid = procpid = getpid();
192 ksheuid = geteuid();
193 kshpgrp = getpgrp();
194 kshppid = getppid();
195 }
196
197 static const char *empty_argv[] = {
198 Tmksh, NULL
199 };
200
201 static uint8_t
isuc(const char * cx)202 isuc(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
224 static int
main_init(int argc,const char * argv[],Source ** sp,struct block ** lp)225 main_init(int argc, const char *argv[], Source **sp, struct block **lp)
226 {
227 int argi, i;
228 Source *s = NULL;
229 struct block *l;
230 unsigned char restricted_shell, errexit, utf_flag;
231 char *cp;
232 const char *ccp, **wp;
233 struct tbl *vp;
234 struct stat s_stdin;
235 #if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
236 ssize_t k;
237 #endif
238
239 #ifdef __OS2__
240 for (i = 0; i < 3; ++i)
241 if (!isatty(i))
242 setmode(i, O_BINARY);
243
244 os2_init(&argc, &argv);
245 #endif
246
247 /* do things like getpgrp() et al. */
248 chvt_reinit();
249
250 /* make sure argv[] is sane, for weird OSes */
251 if (!*argv) {
252 argv = empty_argv;
253 argc = 1;
254 }
255 kshname = argv[0];
256
257 /* initialise permanent Area */
258 ainit(&aperm);
259 /* max. name length: -2147483648 = 11 (+ NUL) */
260 vtemp = alloc(offsetof(struct tbl, name[0]) + 12, APERM);
261
262 /* set up base environment */
263 env.type = E_NONE;
264 ainit(&env.area);
265 /* set up global l->vars and l->funs */
266 newblock();
267
268 /* Do this first so output routines (eg, errorf, shellf) can work */
269 initio();
270
271 /* determine the basename (without '-' or path) of the executable */
272 ccp = kshname;
273 goto begin_parsing_kshname;
274 while ((i = ccp[argi++])) {
275 if (mksh_cdirsep(i)) {
276 ccp += argi;
277 begin_parsing_kshname:
278 argi = 0;
279 if (*ccp == '-')
280 ++ccp;
281 }
282 }
283 if (!*ccp)
284 ccp = empty_argv[0];
285
286 /*
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
303 /* define built-in commands and see if we were called as one */
304 ktinit(APERM, &builtins,
305 /* currently up to 54 builtins: 75% of 128 = 2^7 */
306 7);
307 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
318 #if defined(MKSH_BINSHPOSIX) || defined(MKSH_BINSHREDUCED)
319 /* are we called as -sh or /bin/sh or so? */
320 if (!strcmp(ccp, "sh" MKSH_EXE_EXT)) {
321 /* either also turns off braceexpand */
322 #ifdef MKSH_BINSHPOSIX
323 /* enable better POSIX conformance */
324 change_flag(FPOSIX, OF_FIRSTTIME, true);
325 #endif
326 #ifdef MKSH_BINSHREDUCED
327 /* enable kludge/compat mode */
328 change_flag(FSH, OF_FIRSTTIME, true);
329 #endif
330 }
331 #endif
332 }
333
334 initvar();
335
336 initctypes();
337
338 inittraps();
339
340 coproc_init();
341
342 /* set up variable and command dictionaries */
343 ktinit(APERM, &taliases, 0);
344 ktinit(APERM, &aliases, 0);
345 #ifndef MKSH_NOPWNAM
346 ktinit(APERM, &homedirs, 0);
347 #endif
348
349 /* define shell keywords */
350 initkeywords();
351
352 init_histvec();
353
354 /* initialise tty size before importing environment */
355 change_winsz();
356
357 #ifdef _PATH_DEFPATH
358 def_path = _PATH_DEFPATH;
359 #else
360 #ifdef _CS_PATH
361 if ((k = confstr(_CS_PATH, NULL, 0)) > 0 &&
362 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
368 * breaks somewhere hard; don't try to optimise,
369 * 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 */
374 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";
378 #endif
379
380 /*
381 * Set PATH to def_path (will set the path global variable).
382 * (import of environment below will probably change this setting).
383 */
384 vp = global(TPATH);
385 /* setstr can't fail here */
386 setstr(vp, def_path, KSH_RETURN_ERROR);
387
388 #ifndef MKSH_NO_CMDLINE_EDITING
389 /*
390 * Set edit mode to emacs by default, may be overridden
391 * by the environment or the user. Also, we want tab completion
392 * on in vi by default.
393 */
394 change_flag(FEMACS, OF_SPECIAL, true);
395 #if !MKSH_S_NOVI
396 Flag(FVITABCOMPLETE) = 1;
397 #endif
398 #endif
399
400 /* import environment */
401 if (environ != NULL) {
402 wp = (const char **)environ;
403 while (*wp != NULL) {
404 rndpush(*wp);
405 typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
406 ++wp;
407 }
408 }
409
410 /* 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
416 /* for security */
417 typeset("IFS= \t\n", 0, 0, 0, 0);
418
419 /* assign default shell variable values */
420 typeset("PATHSEP=" MKSH_PATHSEPS, 0, 0, 0, 0);
421 substitute(initsubs, 0);
422
423 /* Figure out the current working directory and set $PWD */
424 vp = global(TPWD);
425 cp = str_val(vp);
426 /* Try to use existing $PWD if it is valid */
427 set_current_wd((mksh_abspath(cp) && test_eval(NULL, TO_FILEQ, cp,
428 Tdot, true)) ? cp : NULL);
429 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);
435
436 for (wp = initcoms; *wp != NULL; wp++) {
437 c_builtin(wp);
438 while (*wp != NULL)
439 wp++;
440 }
441 setint_n(global("OPTIND"), 1, 10);
442
443 kshuid = getuid();
444 kshgid = getgid();
445 kshegid = getegid();
446
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);
454 setint_n((vp = global("BASHPID")), 0, 10);
455 vp->flag |= INT_U;
456 setint_n((vp = global("PGRP")), (mksh_uari_t)kshpgrp, 10);
457 vp->flag |= INT_U;
458 setint_n((vp = global("PPID")), (mksh_uari_t)kshppid, 10);
459 vp->flag |= INT_U;
460 setint_n((vp = global("USER_ID")), (mksh_uari_t)ksheuid, 10);
461 vp->flag |= INT_U;
462 setint_n((vp = global("KSHUID")), (mksh_uari_t)kshuid, 10);
463 vp->flag |= INT_U;
464 setint_n((vp = global("KSHEGID")), (mksh_uari_t)kshegid, 10);
465 vp->flag |= INT_U;
466 setint_n((vp = global("KSHGID")), (mksh_uari_t)kshgid, 10);
467 vp->flag |= INT_U;
468 setint_n((vp = global("RANDOM")), rndsetup(), 10);
469 vp->flag |= INT_U;
470 setint_n((vp_pipest = global("PIPESTATUS")), 0, 10);
471
472 /* Set this before parsing arguments */
473 Flag(FPRIVILEGED) = (kshuid != ksheuid || kshgid != kshegid) ? 2 : 0;
474
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
482 if (!Flag(FAS_BUILTIN)) {
483 argi = parse_args(argv, OF_CMDLINE, NULL);
484 if (argi < 0)
485 return (1);
486 }
487
488 /* process this later only, default to off (hysterical raisins) */
489 utf_flag = UTFMODE;
490 UTFMODE = 0;
491
492 if (Flag(FAS_BUILTIN)) {
493 /* auto-detect from environment variables, always */
494 utf_flag = 3;
495 } else if (Flag(FCOMMAND)) {
496 s = pushs(SSTRINGCMDLINE, ATEMP);
497 if (!(s->start = s->str = argv[argi++]))
498 errorf(Tf_optfoo, "", "", 'c', Treq_arg);
499 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;
507 #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);
516 #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 */
523 s->file = argv[argi++];
524 if (search_access(s->file, X_OK) != 0)
525 s->file = search_path(s->file, path, X_OK, NULL);
526 if (!s->file || !*s->file)
527 s->file = argv[argi - 1];
528 #else
529 s->file = argv[argi++];
530 #endif
531 s->u.shf = shf_open(s->file, O_RDONLY, 0,
532 SHF_MAPHI | SHF_CLEXEC);
533 if (s->u.shf == NULL) {
534 shl_stdout_ok = false;
535 warningf(true, Tf_sD_s, s->file, cstrerror(errno));
536 /* 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();
563 /* do this after j_init() which calls tty_init_state() */
564 if (Flag(FTALKING)) {
565 if (utf_flag == 2) {
566 #ifndef MKSH_ASSUME_UTF8
567 /* auto-detect from locale or environment */
568 utf_flag = 4;
569 #else /* this may not be an #elif */
570 #if MKSH_ASSUME_UTF8
571 utf_flag = 1;
572 #else
573 /* always disable UTF-8 (for interactive) */
574 utf_flag = 0;
575 #endif
576 #endif
577 }
578 #ifndef MKSH_NO_CMDLINE_EDITING
579 x_init();
580 #endif
581 }
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;
590 if (Flag(FAS_BUILTIN)) {
591 l->argc = argc;
592 l->argv = argv;
593 l->argv[0] = ccp;
594 } else {
595 l->argc = argc - argi;
596 /*
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);
603 l->argv[0] = kshname;
604 memcpy(&l->argv[1], &argv[argi], l->argc * sizeof(void *));
605 l->argv[l->argc + 1] = NULL;
606 getopts_reset(1);
607 }
608
609 /* divine the initial state of the utf8-mode Flag */
610 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;
623 #endif
624 /* FALLTHROUGH */
625
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 }
651
652 /* Disable during .profile/ENV reading */
653 restricted_shell = Flag(FRESTRICTED);
654 Flag(FRESTRICTED) = 0;
655 errexit = Flag(FERREXIT);
656 Flag(FERREXIT) = 0;
657
658 /*
659 * Do this before profile/$ENV so that if it causes problems in them,
660 * user will know why things broke.
661 */
662 if (!current_wd[0] && Flag(FTALKING))
663 warningf(false, "can't determine current directory");
664
665 if (Flag(FLOGIN))
666 include(MKSH_SYSTEM_PROFILE, 0, NULL, true);
667 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 {
677 include(MKSH_SUID_PROFILE, 0, NULL, true);
678 /* turn off -p if not set explicitly */
679 if (Flag(FPRIVILEGED) != 1)
680 change_flag(FPRIVILEGED, OF_INTERNAL, false);
681 }
682
683 if (restricted_shell) {
684 c_builtin(restr_com);
685 /* After typeset command... */
686 Flag(FRESTRICTED) = 1;
687 }
688 Flag(FERREXIT) = errexit;
689
690 if (Flag(FTALKING) && s)
691 hist_init(s);
692 else
693 /* set after ENV */
694 Flag(FTRACKALL) = 1;
695
696 alarm_init();
697
698 *sp = s;
699 *lp = l;
700 return (0);
701 }
702
703 /* this indirection barrier reduces stack usage during normal operation */
704
705 int
main(int argc,const char * argv[])706 main(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)) {
714 rv = c_builtin(l->argv);
715 } else {
716 shell(s, 0);
717 /* NOTREACHED */
718 }
719 }
720 return (rv);
721 }
722
723 int
include(const char * name,int argc,const char ** argv,bool intr_ok)724 include(const char *name, int argc, const char **argv, bool intr_ok)
725 {
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);
744 if ((i = kshsetjmp(e->jbuf))) {
745 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:
753 /* see below */
754 return (exstat & 0xFF);
755 case LINTR:
756 /*
757 * intr_ok is set if we are including .profile or $ENV.
758 * If user ^Cs out, we don't want to kill the shell...
759 */
760 if (intr_ok && ((exstat & 0xFF) - 128) != SIGTERM)
761 return (1);
762 /* FALLTHROUGH */
763 case LEXIT:
764 case LLEAVE:
765 case LSHELL:
766 unwind(i);
767 /* NOTREACHED */
768 default:
769 internal_errorf(Tunexpected_type, Tunwind, Tsource, i);
770 /* 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);
780 i = shell(s, 1);
781 quitenv(s->u.shf);
782 if (old_argv) {
783 e->loc->argv = old_argv;
784 e->loc->argc = old_argc;
785 }
786 /* & 0xff to ensure value not -1 */
787 return (i & 0xFF);
788 }
789
790 /* spawn a command into a shell optionally keeping track of the line number */
791 int
command(const char * comm,int line)792 command(const char *comm, int line)
793 {
794 Source *s, *sold = source;
795 int rv;
796
797 s = pushs(SSTRING, ATEMP);
798 s->start = s->str = comm;
799 s->line = line;
800 rv = shell(s, 1);
801 source = sold;
802 return (rv);
803 }
804
805 /*
806 * run the commands from the input source, returning status.
807 */
808 int
shell(Source * volatile s,volatile int level)809 shell(Source * volatile s, volatile int level)
810 {
811 struct op *t;
812 volatile bool wastty = tobool(s->flags & SF_TTY);
813 volatile uint8_t attempts = 13;
814 volatile bool interactive = (level == 0) && Flag(FTALKING);
815 volatile bool sfirst = true;
816 Source *volatile old_source = source;
817 int i;
818
819 newenv(level == 2 ? E_EVAL : E_PARSE);
820 if (interactive)
821 really_exit = false;
822 switch ((i = kshsetjmp(e->jbuf))) {
823 case 0:
824 break;
825 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 */
836 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;
862 }
863 /* 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);
875 internal_errorf(Tunexpected_type, Tunwind, Tshell, i);
876 /* NOTREACHED */
877 }
878 while (/* CONSTCOND */ 1) {
879 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 }
892 t = compile(s, sfirst, true);
893 if (interactive)
894 histsave(&s->line, NULL, HIST_FLUSH, true);
895 sfirst = false;
896 if (!t)
897 goto source_no_tree;
898 if (t->type == TEOF) {
899 if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
900 shellf("Use 'exit' to leave mksh\n");
901 s->type = SSTDIN;
902 } else if (wastty && !really_exit &&
903 j_stopped_running()) {
904 really_exit = true;
905 s->type = SSTDIN;
906 } else {
907 /*
908 * this for POSIX which says EXIT traps
909 * shall be taken in the environment
910 * immediately after the last command
911 * executed.
912 */
913 if (level == 0)
914 unwind(LEXIT);
915 break;
916 }
917 } else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
918 t->u.evalflags |= DOTCOMEXEC;
919 if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
920 exstat = execute(t, 0, NULL) & 0xFF;
921
922 if (t->type != TEOF && interactive && really_exit)
923 really_exit = false;
924
925 source_no_tree:
926 reclaim();
927 }
928 quitenv(NULL);
929 source = old_source;
930 return (exstat & 0xFF);
931 }
932
933 /* return to closest error handler or shell(), exit if none found */
934 /* note: i MUST NOT be 0 */
935 void
unwind(int i)936 unwind(int i)
937 {
938 /*
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
954 /* ordering for EXIT vs ERR is a bit odd (this is what AT&T ksh does) */
955 if (i == LEXIT || ((i == LERROR || i == LINTR) &&
956 sigtraps[ksh_SIGEXIT].trap &&
957 (!Flag(FTALKING) || Flag(FERREXIT)))) {
958 ++trap_nested;
959 runtrap(&sigtraps[ksh_SIGEXIT], trap_nested == 1);
960 --trap_nested;
961 i = LLEAVE;
962 } else if (Flag(FERREXIT) == 1 && (i == LERROR || i == LINTR)) {
963 ++trap_nested;
964 runtrap(&sigtraps[ksh_SIGERR], trap_nested == 1);
965 --trap_nested;
966 i = LLEAVE;
967 }
968
969 while (/* CONSTCOND */ 1) {
970 switch (e->type) {
971 case E_PARSE:
972 case E_FUNC:
973 case E_INCL:
974 case E_LOOP:
975 case E_ERRH:
976 case E_EVAL:
977 kshlongjmp(e->jbuf, i);
978 /* 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
989 void
newenv(int type)990 newenv(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 */
999 cp = alloc(sizeof(struct env) - sizeof(ALLOC_ITEM), ATEMP);
1000 /* undo what alloc() did to the malloc result address */
1001 ep = (void *)(cp - sizeof(ALLOC_ITEM));
1002 /* 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;
1008 ep->yyrecursive_statep = NULL;
1009 ep->type = type;
1010 ep->flags = 0;
1011 /* jump buffer is invalid because flags == 0 */
1012 e = ep;
1013 }
1014
1015 void
quitenv(struct shf * shf)1016 quitenv(struct shf *shf)
1017 {
1018 struct env *ep = e;
1019 char *cp;
1020 int fd;
1021
1022 yyrecursive_pop(true);
1023 while (ep->oenv && ep->oenv->loc != ep->loc)
1024 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]);
1030 if (ep->savefd[2])
1031 /* Clear any write errors */
1032 shf_reopen(2, SHF_WR, shl_out);
1033 }
1034 /*
1035 * Bottom of the stack.
1036 * Either main shell is exiting or cleanup_parents_env() was called.
1037 */
1038 if (ep->oenv == NULL) {
1039 #ifdef DEBUG_LEAKS
1040 int i;
1041 #endif
1042
1043 if (ep->type == E_NONE) {
1044 /* Main shell exiting? */
1045 #if HAVE_PERSISTENT_HISTORY
1046 if (Flag(FTALKING))
1047 hist_finish();
1048 #endif
1049 j_exit();
1050 if (ep->flags & EF_FAKE_SIGDIE) {
1051 int sig = (exstat & 0xFF) - 128;
1052
1053 /*
1054 * ham up our death a bit (AT&T ksh
1055 * 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();
1070 #ifdef DEBUG_LEAKS
1071 #ifndef MKSH_NO_CMDLINE_EDITING
1072 x_done();
1073 #endif
1074 #ifndef MKSH_NOPROSPECTOFWORK
1075 /* block at least SIGCHLD during/after afreeall */
1076 sigprocmask(SIG_BLOCK, &sm_sigchld, NULL);
1077 #endif
1078 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);
1088 }
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;
1097 afree(cp + sizeof(ALLOC_ITEM), ATEMP);
1098 }
1099
1100 /* Called after a fork to cleanup stuff left over from parents environment */
1101 void
cleanup_parents_env(void)1102 cleanup_parents_env(void)
1103 {
1104 struct env *ep;
1105 int fd;
1106
1107 /*
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 }
1122 #ifdef DEBUG_LEAKS
1123 if (ep->type != E_NONE)
1124 ep->type = E_GONE;
1125 #endif
1126 }
1127 #ifndef DEBUG_LEAKS
1128 e->oenv = NULL;
1129 #endif
1130 }
1131
1132 /* Called just before an execve cleanup stuff temporary files */
1133 void
cleanup_proc_env(void)1134 cleanup_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 */
1143 static void
reclaim(void)1144 reclaim(void)
1145 {
1146 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
1153 remove_temps(e->temps);
1154 e->temps = NULL;
1155
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;
1163 afreeall(&e->area);
1164 }
1165
1166 static void
remove_temps(struct temp * tp)1167 remove_temps(struct temp *tp)
1168 {
1169 while (tp) {
1170 if (tp->pid == procpid)
1171 unlink(tp->tffn);
1172 tp = tp->next;
1173 }
1174 }
1175
1176 /*
1177 * 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.
1187 */
1188 int
tty_init_fd(void)1189 tty_init_fd(void)
1190 {
1191 int fd, rv, eno = 0;
1192 bool do_close = false, is_devtty = true;
1193
1194 if (tty_devtty) {
1195 /* already got a tty which is /dev/tty */
1196 return (0);
1197 }
1198
1199 #ifdef _UWIN
1200 /*XXX imake style */
1201 if (isatty(3)) {
1202 /* fd 3 on UWIN _is_ /dev/tty (or our controlling tty) */
1203 fd = 3;
1204 goto got_fd;
1205 }
1206 #endif
1207 if ((fd = open(T_devtty, O_RDWR, 0)) >= 0) {
1208 do_close = true;
1209 goto got_fd;
1210 }
1211 eno = errno;
1212
1213 if (tty_fd >= 0) {
1214 /* already got a non-devtty one */
1215 rv = 1;
1216 goto out;
1217 }
1218 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);
1247 }
1248
1249 /* A shell error occurred (eg, syntax error, etc.) */
1250
1251 #define VWARNINGF_ERRORPREFIX 1
1252 #define VWARNINGF_FILELINE 2
1253 #define VWARNINGF_BUILTIN 4
1254 #define VWARNINGF_INTERNAL 8
1255
1256 static void vwarningf(unsigned int, const char *, va_list)
1257 MKSH_A_FORMAT(__printf__, 2, 0);
1258
1259 static void
vwarningf(unsigned int flags,const char * fmt,va_list ap)1260 vwarningf(unsigned int flags, const char *fmt, va_list ap)
1261 {
1262 if (fmt) {
1263 if (flags & VWARNINGF_INTERNAL)
1264 shf_fprintf(shl_out, Tf_sD_, "internal error");
1265 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)
1270 shf_fprintf(shl_out, Tf_sD_, builtin_argv0);
1271 shf_vfprintf(shl_out, fmt, ap);
1272 shf_putchar('\n', shl_out);
1273 }
1274 shf_flush(shl_out);
1275 }
1276
1277 void
errorfx(int rc,const char * fmt,...)1278 errorfx(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
1293 void
errorf(const char * fmt,...)1294 errorf(const char *fmt, ...)
1295 {
1296 va_list va;
1297
1298 exstat = 1;
1299
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);
1306 unwind(LERROR);
1307 }
1308
1309 /* like errorf(), but no unwind is done */
1310 void
warningf(bool fileline,const char * fmt,...)1311 warningf(bool fileline, const char *fmt, ...)
1312 {
1313 va_list va;
1314
1315 va_start(va, fmt);
1316 vwarningf(VWARNINGF_ERRORPREFIX | (fileline ? VWARNINGF_FILELINE : 0),
1317 fmt, va);
1318 va_end(va);
1319 }
1320
1321 /*
1322 * Used by built-in utilities to prefix shell and utility name to message
1323 * (also unwinds environments for special builtins).
1324 */
1325 void
bi_errorf(const char * fmt,...)1326 bi_errorf(const char *fmt, ...)
1327 {
1328 va_list va;
1329
1330 /* debugging: note that stdout not valid */
1331 shl_stdout_ok = false;
1332
1333 exstat = 1;
1334
1335 va_start(va, fmt);
1336 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE |
1337 VWARNINGF_BUILTIN, fmt, va);
1338 va_end(va);
1339
1340 /* POSIX special builtins cause non-interactive shells to exit */
1341 if (builtin_spec) {
1342 builtin_argv0 = NULL;
1343 /* may not want to use LERROR here */
1344 unwind(LERROR);
1345 }
1346 }
1347
1348 /* Called when something that shouldn't happen does */
1349 void
internal_errorf(const char * fmt,...)1350 internal_errorf(const char *fmt, ...)
1351 {
1352 va_list va;
1353
1354 va_start(va, fmt);
1355 vwarningf(VWARNINGF_INTERNAL, fmt, va);
1356 va_end(va);
1357 unwind(LERROR);
1358 }
1359
1360 void
internal_warningf(const char * fmt,...)1361 internal_warningf(const char *fmt, ...)
1362 {
1363 va_list va;
1364
1365 va_start(va, fmt);
1366 vwarningf(VWARNINGF_INTERNAL, fmt, va);
1367 va_end(va);
1368 }
1369
1370 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
1371 void
error_prefix(bool fileline)1372 error_prefix(bool fileline)
1373 {
1374 /* Avoid foo: foo[2]: ... */
1375 if (!fileline || !source || !source->file ||
1376 strcmp(source->file, kshname) != 0)
1377 shf_fprintf(shl_out, Tf_sD_, kshname + (*kshname == '-'));
1378 if (fileline && source && source->file != NULL) {
1379 shf_fprintf(shl_out, "%s[%lu]: ", source->file,
1380 (unsigned long)(source->errline ?
1381 source->errline : source->line));
1382 source->errline = 0;
1383 }
1384 }
1385
1386 /* printf to shl_out (stderr) with flush */
1387 void
shellf(const char * fmt,...)1388 shellf(const char *fmt, ...)
1389 {
1390 va_list va;
1391
1392 if (!initio_done)
1393 /* shl_out may not be set up yet... */
1394 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) */
1402 void
shprintf(const char * fmt,...)1403 shprintf(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) */
1415 int
can_seek(int fd)1416 can_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
1424 #ifdef DF
1425 int shl_dbg_fd;
1426 #define NSHF_IOB 4
1427 #else
1428 #define NSHF_IOB 3
1429 #endif
1430 struct shf shf_iob[NSHF_IOB];
1431
1432 void
initio(void)1433 initio(void)
1434 {
1435 #ifdef DF
1436 const char *lfp;
1437 #endif
1438
1439 /* force buffer allocation */
1440 shf_fdopen(1, SHF_WR, shl_stdout);
1441 shf_fdopen(2, SHF_WR, shl_out);
1442 shf_fdopen(2, SHF_WR, shl_xtrace);
1443 #ifdef DF
1444 if ((lfp = getenv("SDMKSH_PATH")) == NULL) {
1445 if ((lfp = getenv("HOME")) == NULL || !mksh_abspath(lfp))
1446 errorf("can't get home directory");
1447 lfp = shf_smprintf(Tf_sSs, lfp, "mksh-dbg.txt");
1448 }
1449
1450 if ((shl_dbg_fd = open(lfp, O_WRONLY | O_APPEND | O_CREAT, 0600)) < 0)
1451 errorf("can't open debug output file %s", lfp);
1452 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)
1458 errorf("can't dup debug output file");
1459 }
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;
1465 }
1466
1467 /* A dup2() with error checking */
1468 int
ksh_dup2(int ofd,int nfd,bool errok)1469 ksh_dup2(int ofd, int nfd, bool errok)
1470 {
1471 int rv;
1472
1473 if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
1474 errorf(Ttoo_many_files);
1475
1476 #ifdef __ultrix
1477 /*XXX imake style */
1478 if (rv >= 0)
1479 fcntl(nfd, F_SETFD, 0);
1480 #endif
1481
1482 return (rv);
1483 }
1484
1485 /*
1486 * 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.
1488 */
1489 short
savefd(int fd)1490 savefd(int fd)
1491 {
1492 int nfd = fd;
1493
1494 if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
1495 (errno == EBADF || errno == EPERM))
1496 return (-1);
1497 if (nfd < 0 || nfd > SHRT_MAX)
1498 errorf(Ttoo_many_files);
1499 fcntl(nfd, F_SETFD, FD_CLOEXEC);
1500 return ((short)nfd);
1501 }
1502
1503 void
restfd(int fd,int ofd)1504 restfd(int fd, int ofd)
1505 {
1506 if (fd == 2)
1507 shf_flush(&shf_iob[/* fd */ 2]);
1508 if (ofd < 0)
1509 /* original fd closed */
1510 close(fd);
1511 else if (fd != ofd) {
1512 /*XXX: what to do if this dup fails? */
1513 ksh_dup2(ofd, fd, true);
1514 close(ofd);
1515 }
1516 }
1517
1518 void
openpipe(int * pv)1519 openpipe(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]);
1531 #ifdef __OS2__
1532 setmode(pv[0], O_BINARY);
1533 setmode(pv[1], O_BINARY);
1534 #endif
1535 }
1536
1537 void
closepipe(int * pv)1538 closepipe(int *pv)
1539 {
1540 close(pv[0]);
1541 close(pv[1]);
1542 }
1543
1544 /*
1545 * Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
1546 * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
1547 */
1548 int
check_fd(const char * name,int mode,const char ** emsgp)1549 check_fd(const char *name, int mode, const char **emsgp)
1550 {
1551 int fd, fl;
1552
1553 if (!name[0] || name[1])
1554 goto illegal_fd_name;
1555 if (name[0] == 'p')
1556 return (coproc_getfd(mode, emsgp));
1557 if (!ksh_isdigit(name[0])) {
1558 illegal_fd_name:
1559 if (emsgp)
1560 *emsgp = "illegal file descriptor name";
1561 return (-1);
1562 }
1563
1564 if ((fl = fcntl((fd = ksh_numdig(name[0])), F_GETFL, 0)) < 0) {
1565 if (emsgp)
1566 *emsgp = "bad file descriptor";
1567 return (-1);
1568 }
1569 fl &= O_ACCMODE;
1570 /*
1571 * X_OK is a kludge to disable this check for dups (x<&1):
1572 * 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 */
1588 void
coproc_init(void)1589 coproc_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 */
1597 void
coproc_read_close(int fd)1598 coproc_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
1607 /*
1608 * Called by c_read() and by iosetup() to close the other side of the
1609 * read pipe, so reads will actually terminate.
1610 */
1611 void
coproc_readw_close(int fd)1612 coproc_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
1620 /*
1621 * Called by c_print when a write to a fd fails with EPIPE and by iosetup
1622 * when co-process input is dup'd
1623 */
1624 void
coproc_write_close(int fd)1625 coproc_write_close(int fd)
1626 {
1627 if (coproc.write >= 0 && fd == coproc.write) {
1628 close(coproc.write);
1629 coproc.write = -1;
1630 }
1631 }
1632
1633 /*
1634 * Called to check for existence of/value of the co-process file descriptor.
1635 * (Used by check_fd() and by c_read/c_print to deal with -p option).
1636 */
1637 int
coproc_getfd(int mode,const char ** emsgp)1638 coproc_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
1649 /*
1650 * called to close file descriptors related to the coprocess (if any)
1651 * Should be called with SIGCHLD blocked.
1652 */
1653 void
coproc_cleanup(int reuse)1654 coproc_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
1673 struct temp *
maketemp(Area * ap,Temp_type type,struct temp ** tlist)1674 maketemp(Area *ap, Temp_type type, struct temp **tlist)
1675 {
1676 char *cp;
1677 size_t len;
1678 int i, j;
1679 struct temp *tp;
1680 const char *dir;
1681 struct stat sb;
1682
1683 dir = tmpdir ? tmpdir : MKSH_DEFAULT_TMPDIR;
1684 /* 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);
1688
1689 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;
1705
1706 /* cyclically attempt to open a temporary file */
1707 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)
1717 goto maketemp_out;
1718 } while (i < 0);
1719
1720 if (type == TT_FUNSUB) {
1721 /* map us high and mark as close-on-exec */
1722 if ((j = savefd(i)) != i) {
1723 close(i);
1724 i = j;
1725 }
1726
1727 /* operation mode for the shf */
1728 j = SHF_RD;
1729 } else
1730 j = SHF_WR;
1731
1732 /* shf_fdopen cannot fail, so no fd leak */
1733 tp->shf = shf_fdopen(i, j, NULL);
1734
1735 maketemp_out:
1736 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
1746 #define INIT_TBLSHIFT 3 /* initial table shift (2^3 = 8) */
1747 #define PERTURB_SHIFT 5 /* see Python 2.5.4 Objects/dictobject.c */
1748
1749 static void tgrow(struct table *);
1750 static int tnamecmp(const void *, const void *);
1751
1752 static void
tgrow(struct table * tp)1753 tgrow(struct table *tp)
1754 {
1755 size_t i, j, osize, mask, perturb;
1756 struct tbl *tblp, **pp;
1757 struct tbl **ntblp, **otblp = tp->tbls;
1758
1759 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
1770 /* 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);
1774 tp->tbls = ntblp;
1775 if (otblp == NULL)
1776 return;
1777
1778 mask = i - 1;
1779 for (i = 0; i < osize; i++)
1780 if ((tblp = otblp[i]) != NULL) {
1781 if ((tblp->flag & DEFINED)) {
1782 /* search for free hash table slot */
1783 j = perturb = tblp->ua.hval;
1784 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:
1789 pp = &ntblp[j & mask];
1790 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
1802 void
ktinit(Area * ap,struct table * tp,uint8_t initshift)1803 ktinit(Area *ap, struct table *tp, uint8_t initshift)
1804 {
1805 tp->areap = ap;
1806 tp->tbls = NULL;
1807 tp->tshift = ((initshift > INIT_TBLSHIFT) ?
1808 initshift : INIT_TBLSHIFT) - 1;
1809 tgrow(tp);
1810 }
1811
1812 /* table, name (key) to search for, hash(name), rv pointer to tbl ptr */
1813 struct tbl *
ktscan(struct table * tp,const char * name,uint32_t h,struct tbl *** ppp)1814 ktscan(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
1819 mask = ((size_t)1 << (tp->tshift)) - 1;
1820 /* search for hash table slot matching name */
1821 j = perturb = h;
1822 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
1837 /* table, name (key) to enter, hash(n) */
1838 struct tbl *
ktenter(struct table * tp,const char * n,uint32_t h)1839 ktenter(struct table *tp, const char *n, uint32_t h)
1840 {
1841 struct tbl **pp, *p;
1842 size_t len;
1843
1844 Search:
1845 if ((p = ktscan(tp, n, h, &pp)))
1846 return (p);
1847
1848 if (tp->nfree == 0) {
1849 /* too full */
1850 tgrow(tp);
1851 goto Search;
1852 }
1853
1854 /* create new tbl entry */
1855 len = strlen(n);
1856 checkoktoadd(len, offsetof(struct tbl, name[0]) + 1);
1857 p = alloc(offsetof(struct tbl, name[0]) + ++len, tp->areap);
1858 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
1872 void
ktwalk(struct tstate * ts,struct table * tp)1873 ktwalk(struct tstate *ts, struct table *tp)
1874 {
1875 ts->left = (size_t)1 << (tp->tshift);
1876 ts->next = tp->tbls;
1877 }
1878
1879 struct tbl *
ktnext(struct tstate * ts)1880 ktnext(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
1890 static int
tnamecmp(const void * p1,const void * p2)1891 tnamecmp(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
1899 struct tbl **
ktsort(struct table * tp)1900 ktsort(struct table *tp)
1901 {
1902 size_t i;
1903 struct tbl **p, **sp, **dp;
1904
1905 /*
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);
1911 sp = tp->tbls; /* source */
1912 dp = p; /* dest */
1913 while (i--)
1914 if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
1915 ((*dp)->flag & ARRAY)))
1916 dp++;
1917 qsort(p, (i = dp - p), sizeof(struct tbl *), tnamecmp);
1918 p[i] = NULL;
1919 return (p);
1920 }
1921
1922 #ifdef SIGWINCH
1923 static void
x_sigwinch(int sig MKSH_A_UNUSED)1924 x_sigwinch(int sig MKSH_A_UNUSED)
1925 {
1926 /* this runs inside interrupt context, with errno saved */
1927
1928 got_winch = 1;
1929 }
1930 #endif
1931
1932 #ifdef DF
1933 void
DF(const char * fmt,...)1934 DF(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
1956 void
x_mkraw(int fd,mksh_ttyst * ocb,bool forread)1957 x_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) {
1968 cb.c_iflag &= ~(ISTRIP);
1969 cb.c_lflag &= ~(ICANON) | ECHO;
1970 } else {
1971 cb.c_iflag &= ~(INLCR | ICRNL | ISTRIP);
1972 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 }
1987