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 $ */
7 * Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
8 * 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
9 * mirabilos <m@mirbsd.org>
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.
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.
30 #if HAVE_LANGINFO_CODESET
33 #if HAVE_SETLOCALE_CTYPE
37 __RCSID("$MirOS: src/bin/mksh/main.c,v 1.347 2018/01/13 21:45:07 tg Exp $");
40 #define MKSHRC_PATH "~/.mkshrc"
43 #ifndef MKSH_DEFAULT_TMPDIR
44 #define MKSH_DEFAULT_TMPDIR MKSH_UNIXROOT "/tmp"
47 static uint8_t isuc(const char *);
48 static int main_init(int, const char *[], Source **, struct block **);
49 void chvt_reinit(void);
50 static void reclaim(void);
51 static void remove_temps(struct temp *);
52 static mksh_uari_t rndsetup(void);
53 static void init_environ(void);
55 static void x_sigwinch(int);
58 static const char initsubs[] =
66 static const char *initcoms[] = {
67 Ttypeset, "-r", initvsn, NULL,
68 Ttypeset, "-x", "HOME", TPATH, TSHELL, NULL,
69 Ttypeset, "-i10", "COLUMNS", "LINES", "SECONDS", "TMOUT", NULL,
71 "integer=\\\\builtin typeset -i",
72 "local=\\\\builtin typeset",
73 /* not "alias -t --": hash -r needs to work */
74 "hash=\\\\builtin alias -t",
75 "type=\\\\builtin whence -v",
76 "autoload=\\\\builtin typeset -fu",
77 "functions=\\\\builtin typeset -f",
78 "history=\\\\builtin fc -l",
79 "nameref=\\\\builtin typeset -n",
81 "r=\\\\builtin fc -e -",
82 "login=\\\\builtin exec login",
84 /* this is what AT&T ksh seems to track, with the addition of emacs */
86 Tcat, "cc", "chmod", "cp", "date", "ed", "emacs", "grep", "ls",
87 "make", "mv", "pr", "rm", "sed", Tsh, "vi", "who", NULL,
91 static const char *restr_com[] = {
92 Ttypeset, "-r", TPATH, "ENV", TSHELL, NULL
95 static bool initio_done;
97 /* top-level parsing and execution environment */
98 static struct env env;
101 /* compile-time assertions */
102 #define cta(name, expr) struct cta_ ## name { char t[(expr) ? 1 : -1]; }
104 /* this one should be defined by the standard */
105 cta(char_is_1_char, (sizeof(char) == 1) && (sizeof(signed char) == 1) &&
106 (sizeof(unsigned char) == 1));
107 cta(char_is_8_bits, ((CHAR_BIT) == 8) && ((int)(unsigned char)0xFF == 0xFF) &&
108 ((int)(unsigned char)0x100 == 0) && ((int)(unsigned char)(int)-1 == 0xFF));
109 /* the next assertion is probably not really needed */
110 cta(short_is_2_char, sizeof(short) == 2);
111 cta(short_size_no_matter_of_signedness, sizeof(short) == sizeof(unsigned short));
112 /* the next assertion is probably not really needed */
113 cta(int_is_4_char, sizeof(int) == 4);
114 cta(int_size_no_matter_of_signedness, sizeof(int) == sizeof(unsigned int));
116 cta(long_ge_int, sizeof(long) >= sizeof(int));
117 cta(long_size_no_matter_of_signedness, sizeof(long) == sizeof(unsigned long));
119 #ifndef MKSH_LEGACY_MODE
120 /* the next assertion is probably not really needed */
121 cta(ari_is_4_char, sizeof(mksh_ari_t) == 4);
123 cta(ari_has_31_bit, 0 < (mksh_ari_t)(((((mksh_ari_t)1 << 15) << 15) - 1) * 2 + 1));
124 /* the next assertion is probably not really needed */
125 cta(uari_is_4_char, sizeof(mksh_uari_t) == 4);
126 /* but the next three are; we REQUIRE unsigned integer wraparound */
127 cta(uari_has_31_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 2 + 1));
128 cta(uari_has_32_bit, 0 < (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3));
129 cta(uari_wrap_32_bit,
130 (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 3) >
131 (mksh_uari_t)(((((mksh_uari_t)1 << 15) << 15) - 1) * 4 + 4));
133 /* these are always required */
134 cta(ari_is_signed, (mksh_ari_t)-1 < (mksh_ari_t)0);
135 cta(uari_is_unsigned, (mksh_uari_t)-1 > (mksh_uari_t)0);
136 /* we require these to have the precisely same size and assume 2s complement */
137 cta(ari_size_no_matter_of_signedness, sizeof(mksh_ari_t) == sizeof(mksh_uari_t));
139 cta(sizet_size_no_matter_of_signedness, sizeof(ssize_t) == sizeof(size_t));
140 cta(sizet_voidptr_same_size, sizeof(size_t) == sizeof(void *));
141 cta(sizet_funcptr_same_size, sizeof(size_t) == sizeof(void (*)(void)));
142 /* our formatting routines assume this */
143 cta(ptr_fits_in_long, sizeof(size_t) <= sizeof(long));
144 cta(ari_fits_in_long, sizeof(mksh_ari_t) <= sizeof(long));
151 ALLOC_ITEM alloc_INT;
152 void *dataptr, *stkptr, *mallocptr;
153 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
160 cp = alloc(sizeof(*bufptr) - sizeof(ALLOC_ITEM), APERM);
161 /* clear the allocated space, for valgrind and to avoid UB */
162 memset(cp, 0, sizeof(*bufptr) - sizeof(ALLOC_ITEM));
163 /* undo what alloc() did to the malloc result address */
164 bufptr = (void *)(cp - sizeof(ALLOC_ITEM));
165 /* PIE or something similar provides us with deltas here */
166 bufptr->dataptr = &rndsetupstate;
167 /* ASLR in at least Windows, Linux, some BSDs */
168 bufptr->stkptr = &bufptr;
169 /* randomised malloc in BSD (and possibly others) */
170 bufptr->mallocptr = bufptr;
171 #if defined(__GLIBC__) && (__GLIBC__ >= 2)
172 /* glibc pointer guard */
173 sigsetjmp(bufptr->jbuf, 1);
175 /* introduce variation (and yes, second arg MBZ for portability) */
176 mksh_TIME(bufptr->tv);
178 #ifdef MKSH_ALLOC_CATCH_UNDERRUNS
179 mprotect(((char *)bufptr) + 4096, 4096, PROT_READ | PROT_WRITE);
181 h = chvt_rndsetup(bufptr, sizeof(*bufptr));
184 return ((mksh_uari_t)h);
190 kshpid = procpid = getpid();
196 static const char *empty_argv[] = {
201 isuc(const char *cx) {
208 /* uppercase a string duplicate */
209 strdupx(x, cx, ATEMP);
211 while ((*cp = ksh_toupper(*cp)))
214 /* check for UTF-8 */
215 if (strstr(x, "UTF-8") || strstr(x, "UTF8"))
218 /* free copy and out */
224 main_init(int argc, const char *argv[], Source **sp, struct block **lp)
229 unsigned char restricted_shell, errexit, utf_flag;
231 const char *ccp, **wp;
234 #if !defined(_PATH_DEFPATH) && defined(_CS_PATH)
238 #if defined(MKSH_EBCDIC) || defined(MKSH_FAUX_EBCDIC)
244 os2_init(&argc, &argv);
247 /* do things like getpgrp() et al. */
250 /* make sure argv[] is sane, for weird OSes */
257 /* initialise permanent Area */
259 /* max. name length: -2147483648 = 11 (+ NUL) */
260 vtemp = alloc(offsetof(struct tbl, name[0]) + 12, APERM);
262 /* set up base environment */
265 /* set up global l->vars and l->funs */
268 /* Do this first so output routines (eg, errorf, shellf) can work */
271 /* determine the basename (without '-' or path) of the executable */
273 goto begin_parsing_kshname;
274 while ((i = ccp[argi++])) {
275 if (mksh_cdirsep(i)) {
277 begin_parsing_kshname:
287 * Turn on nohup by default. (AT&T ksh does not have a nohup
288 * option - it always sends the hup).
293 * Turn on brace expansion by default. AT&T kshs that have
294 * alternation always have it on.
296 Flag(FBRACEEXPAND) = 1;
299 * Turn on "set -x" inheritance by default.
301 Flag(FXTRACEREC) = 1;
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 */
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;
312 if (!Flag(FAS_BUILTIN)) {
313 /* check for -T option early */
314 argi = parse_args(argv, OF_FIRSTTIME, NULL);
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);
326 #ifdef MKSH_BINSHREDUCED
327 /* enable kludge/compat mode */
328 change_flag(FSH, OF_FIRSTTIME, true);
340 /* set up variable and command dictionaries */
341 ktinit(APERM, &taliases, 0);
342 ktinit(APERM, &aliases, 0);
344 ktinit(APERM, &homedirs, 0);
347 /* define shell keywords */
352 /* initialise tty size before importing environment */
356 def_path = _PATH_DEFPATH;
359 if ((k = confstr(_CS_PATH, NULL, 0)) > 0 &&
360 confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
365 * this is uniform across all OSes unless it
366 * breaks somewhere hard; don't try to optimise,
367 * e.g. add stuff for Interix or remove /usr
368 * for HURD, because e.g. Debian GNU/HURD is
369 * "keeping a regular /usr"; this is supposed
370 * to be a sane 'basic' default PATH
372 def_path = MKSH_UNIXROOT "/bin" MKSH_PATHSEPS
373 MKSH_UNIXROOT "/usr/bin" MKSH_PATHSEPS
374 MKSH_UNIXROOT "/sbin" MKSH_PATHSEPS
375 MKSH_UNIXROOT "/usr/sbin";
379 * Set PATH to def_path (will set the path global variable).
380 * (import of environment below will probably change this setting).
383 /* setstr can't fail here */
384 setstr(vp, def_path, KSH_RETURN_ERROR);
386 #ifndef MKSH_NO_CMDLINE_EDITING
388 * Set edit mode to emacs by default, may be overridden
389 * by the environment or the user. Also, we want tab completion
390 * on in vi by default.
392 change_flag(FEMACS, OF_SPECIAL, true);
394 Flag(FVITABCOMPLETE) = 1;
398 /* import environment */
401 /* override default PATH regardless of environment */
402 #ifdef MKSH_DEFPATH_OVERRIDE
404 setstr(vp, MKSH_DEFPATH_OVERRIDE, KSH_RETURN_ERROR);
408 typeset(TinitIFS, 0, 0, 0, 0);
410 /* assign default shell variable values */
411 typeset("PATHSEP=" MKSH_PATHSEPS, 0, 0, 0, 0);
412 substitute(initsubs, 0);
414 /* Figure out the current working directory and set $PWD */
417 /* Try to use existing $PWD if it is valid */
418 set_current_wd((mksh_abspath(cp) && test_eval(NULL, TO_FILEQ, cp,
419 Tdot, true)) ? cp : NULL);
421 simplify_path(current_wd);
422 /* Only set pwd if we know where we are or if it had a bogus value */
423 if (current_wd[0] || *cp)
424 /* setstr can't fail here */
425 setstr(vp, current_wd, KSH_RETURN_ERROR);
427 for (wp = initcoms; *wp != NULL; wp++) {
432 setint_n(global("OPTIND"), 1, 10);
438 safe_prompt = ksheuid ? "$ " : "# ";
440 /* Set PS1 if unset or we are root and prompt doesn't contain a # */
441 if (!(vp->flag & ISSET) ||
442 (!ksheuid && !strchr(str_val(vp), '#')))
443 /* setstr can't fail here */
444 setstr(vp, safe_prompt, KSH_RETURN_ERROR);
445 setint_n((vp = global("BASHPID")), 0, 10);
447 setint_n((vp = global("PGRP")), (mksh_uari_t)kshpgrp, 10);
449 setint_n((vp = global("PPID")), (mksh_uari_t)kshppid, 10);
451 setint_n((vp = global("USER_ID")), (mksh_uari_t)ksheuid, 10);
453 setint_n((vp = global("KSHUID")), (mksh_uari_t)kshuid, 10);
455 setint_n((vp = global("KSHEGID")), (mksh_uari_t)kshegid, 10);
457 setint_n((vp = global("KSHGID")), (mksh_uari_t)kshgid, 10);
459 setint_n((vp = global("RANDOM")), rndsetup(), 10);
461 setint_n((vp_pipest = global("PIPESTATUS")), 0, 10);
463 /* Set this before parsing arguments */
464 Flag(FPRIVILEGED) = (kshuid != ksheuid || kshgid != kshegid) ? 2 : 0;
466 /* this to note if monitor is set on command line (see below) */
467 #ifndef MKSH_UNEMPLOYED
468 Flag(FMONITOR) = 127;
470 /* this to note if utf-8 mode is set on command line (see below) */
473 if (!Flag(FAS_BUILTIN)) {
474 argi = parse_args(argv, OF_CMDLINE, NULL);
479 /* process this later only, default to off (hysterical raisins) */
483 if (Flag(FAS_BUILTIN)) {
484 /* auto-detect from environment variables, always */
486 } else if (Flag(FCOMMAND)) {
487 s = pushs(SSTRINGCMDLINE, ATEMP);
488 if (!(s->start = s->str = argv[argi++]))
489 errorf(Tf_optfoo, "", "", 'c', Treq_arg);
491 if (ctype(*s->str, C_QUOTE))
496 s->flags |= SF_MAYEXEC;
498 #ifdef MKSH_MIDNIGHTBSD01ASH_COMPAT
499 /* compatibility to MidnightBSD 0.1 /bin/sh (kludge) */
500 if (Flag(FSH) && argv[argi] && !strcmp(argv[argi], "--"))
504 kshname = argv[argi++];
505 } else if (argi < argc && !Flag(FSTDIN)) {
506 s = pushs(SFILE, ATEMP);
509 * A bug in OS/2 extproc (like shebang) handling makes
510 * it not pass the full pathname of a script, so we need
511 * to search for it. This changes the behaviour of a
512 * simple "mksh foo", but can't be helped.
514 s->file = argv[argi++];
515 if (search_access(s->file, X_OK) != 0)
516 s->file = search_path(s->file, path, X_OK, NULL);
517 if (!s->file || !*s->file)
518 s->file = argv[argi - 1];
520 s->file = argv[argi++];
522 s->u.shf = shf_open(s->file, O_RDONLY, 0,
523 SHF_MAPHI | SHF_CLEXEC);
524 if (s->u.shf == NULL) {
525 shl_stdout_ok = false;
526 warningf(true, Tf_sD_s, s->file, cstrerror(errno));
527 /* mandated by SUSv4 */
534 s = pushs(SSTDIN, ATEMP);
536 s->u.shf = shf_fdopen(0, SHF_RD | can_seek(0),
538 if (isatty(0) && isatty(2)) {
539 Flag(FTALKING) = Flag(FTALKING_I) = 1;
540 /* The following only if isatty(0) */
542 s->u.shf->flags |= SHF_INTERRUPT;
547 /* this bizarreness is mandated by POSIX */
548 if (fstat(0, &s_stdin) >= 0 && S_ISCHR(s_stdin.st_mode) &&
552 /* initialise job control */
554 /* do this after j_init() which calls tty_init_state() */
555 if (Flag(FTALKING)) {
557 #ifndef MKSH_ASSUME_UTF8
558 /* auto-detect from locale or environment */
560 #else /* this may not be an #elif */
564 /* always disable UTF-8 (for interactive) */
569 #ifndef MKSH_NO_CMDLINE_EDITING
575 sigtraps[SIGWINCH].flags |= TF_SHELL_USES;
576 setsig(&sigtraps[SIGWINCH], x_sigwinch,
577 SS_RESTORE_ORIG|SS_FORCE|SS_SHTRAP);
581 if (Flag(FAS_BUILTIN)) {
586 l->argc = argc - argi;
588 * allocate a new array because otherwise, when we modify
589 * it in-place, ps(1) output changes; the meaning of argc
590 * here is slightly different as it excludes kshname, and
591 * we add a trailing NULL sentinel as well
593 l->argv = alloc2(l->argc + 2, sizeof(void *), APERM);
594 l->argv[0] = kshname;
595 memcpy(&l->argv[1], &argv[argi], l->argc * sizeof(void *));
596 l->argv[l->argc + 1] = NULL;
600 /* divine the initial state of the utf8-mode Flag */
604 /* auto-detect from locale or environment */
606 #if HAVE_SETLOCALE_CTYPE
607 ccp = setlocale(LC_CTYPE, "");
608 #if HAVE_LANGINFO_CODESET
610 ccp = nl_langinfo(CODESET);
617 /* auto-detect from environment */
619 /* these were imported from environ earlier */
621 ccp = str_val(global("LC_ALL"));
623 ccp = str_val(global("LC_CTYPE"));
625 ccp = str_val(global("LANG"));
629 /* not set on command line, not FTALKING */
643 /* Disable during .profile/ENV reading */
644 restricted_shell = Flag(FRESTRICTED);
645 Flag(FRESTRICTED) = 0;
646 errexit = Flag(FERREXIT);
650 * Do this before profile/$ENV so that if it causes problems in them,
651 * user will know why things broke.
653 if (!current_wd[0] && Flag(FTALKING))
654 warningf(false, "can't determine current directory");
657 include(MKSH_SYSTEM_PROFILE, 0, NULL, true);
658 if (!Flag(FPRIVILEGED)) {
660 include(substitute("$HOME/.profile", 0), 0, NULL, true);
661 if (Flag(FTALKING)) {
662 cp = substitute("${ENV:-" MKSHRC_PATH "}", DOTILDE);
664 include(cp, 0, NULL, true);
667 include(MKSH_SUID_PROFILE, 0, NULL, true);
668 /* turn off -p if not set explicitly */
669 if (Flag(FPRIVILEGED) != 1)
670 change_flag(FPRIVILEGED, OF_INTERNAL, false);
673 if (restricted_shell) {
674 c_builtin(restr_com);
675 /* After typeset command... */
676 Flag(FRESTRICTED) = 1;
678 Flag(FERREXIT) = errexit;
680 if (Flag(FTALKING) && s)
693 /* this indirection barrier reduces stack usage during normal operation */
696 main(int argc, const char *argv[])
702 if ((rv = main_init(argc, argv, &s, &l)) == 0) {
703 if (Flag(FAS_BUILTIN)) {
704 rv = c_builtin(l->argv);
714 include(const char *name, int argc, const char **argv, bool intr_ok)
716 Source *volatile s = NULL;
718 const char **volatile old_argv;
719 volatile int old_argc;
722 shf = shf_open(name, O_RDONLY, 0, SHF_MAPHI | SHF_CLEXEC);
727 old_argv = e->loc->argv;
728 old_argc = e->loc->argc;
734 if ((i = kshsetjmp(e->jbuf))) {
735 quitenv(s ? s->u.shf : NULL);
737 e->loc->argv = old_argv;
738 e->loc->argc = old_argc;
744 return (exstat & 0xFF);
747 * intr_ok is set if we are including .profile or $ENV.
748 * If user ^Cs out, we don't want to kill the shell...
750 if (intr_ok && ((exstat & 0xFF) - 128) != SIGTERM)
759 internal_errorf(Tunexpected_type, Tunwind, Tsource, i);
767 s = pushs(SFILE, ATEMP);
769 strdupx(s->file, name, ATEMP);
773 e->loc->argv = old_argv;
774 e->loc->argc = old_argc;
776 /* & 0xff to ensure value not -1 */
780 /* spawn a command into a shell optionally keeping track of the line number */
782 command(const char *comm, int line)
784 Source *s, *sold = source;
787 s = pushs(SSTRING, ATEMP);
788 s->start = s->str = comm;
796 * run the commands from the input source, returning status.
799 shell(Source * volatile s, volatile int level)
802 volatile bool wastty = tobool(s->flags & SF_TTY);
803 volatile uint8_t attempts = 13;
804 volatile bool interactive = (level == 0) && Flag(FTALKING);
805 volatile bool sfirst = true;
806 Source *volatile old_source = source;
809 newenv(level == 2 ? E_EVAL : E_PARSE);
812 switch ((i = kshsetjmp(e->jbuf))) {
820 internal_errorf(Tf_cant_s, Tshell,
821 i == LBREAK ? Tbreak : Tcontinue);
824 /* assert: interactive == false */
827 /* we get here if SIGINT not caught or ignored */
834 * Reset any eof that was read as part of a
837 if (Flag(FIGNOREEOF) && s->type == SEOF && wastty)
840 * Used by exit command to get back to
841 * top level shell. Kind of strange since
842 * interactive is set if we are reading from
843 * a tty, but to have stopped jobs, one only
844 * needs FMONITOR set (not FTALKING/SF_TTY)...
846 /* toss any input we have so far */
847 yyrecursive_pop(true);
848 s->start = s->str = null;
865 internal_errorf(Tunexpected_type, Tunwind, Tshell, i);
868 while (/* CONSTCOND */ 1) {
872 if (s->next == NULL) {
876 s->flags &= ~SF_ECHO;
882 t = compile(s, sfirst, true);
884 histsave(&s->line, NULL, HIST_FLUSH, true);
888 if (t->type == TEOF) {
889 if (wastty && Flag(FIGNOREEOF) && --attempts > 0) {
890 shellf("Use 'exit' to leave mksh\n");
892 } else if (wastty && !really_exit &&
893 j_stopped_running()) {
898 * this for POSIX which says EXIT traps
899 * shall be taken in the environment
900 * immediately after the last command
907 } else if ((s->flags & SF_MAYEXEC) && t->type == TCOM)
908 t->u.evalflags |= DOTCOMEXEC;
909 if (!Flag(FNOEXEC) || (s->flags & SF_TTY))
910 exstat = execute(t, 0, NULL) & 0xFF;
912 if (t->type != TEOF && interactive && really_exit)
920 return (exstat & 0xFF);
923 /* return to closest error handler or shell(), exit if none found */
924 /* note: i MUST NOT be 0 */
929 * This is a kludge. We need to restore everything that was
930 * changed in the new environment, see cid 1005090337C7A669439
931 * and 10050903386452ACBF1, but fail to even save things most of
932 * the time. funcs.c:c_eval() changes FERREXIT temporarily to 0,
933 * which needs to be restored thus (related to Debian #696823).
934 * We did not save the shell flags, so we use a special or'd
935 * value here... this is mostly to clean up behind *other*
936 * callers of unwind(LERROR) here; exec.c has the regular case.
938 if (Flag(FERREXIT) & 0x80) {
939 /* GNU bash does not run this trapsig */
941 Flag(FERREXIT) &= ~0x80;
944 /* ordering for EXIT vs ERR is a bit odd (this is what AT&T ksh does) */
945 if (i == LEXIT || ((i == LERROR || i == LINTR) &&
946 sigtraps[ksh_SIGEXIT].trap &&
947 (!Flag(FTALKING) || Flag(FERREXIT)))) {
949 runtrap(&sigtraps[ksh_SIGEXIT], trap_nested == 1);
952 } else if (Flag(FERREXIT) == 1 && (i == LERROR || i == LINTR)) {
954 runtrap(&sigtraps[ksh_SIGERR], trap_nested == 1);
959 while (/* CONSTCOND */ 1) {
967 kshlongjmp(e->jbuf, i);
971 e->flags |= EF_FAKE_SIGDIE;
986 * struct env includes ALLOC_ITEM for alignment constraints
987 * so first get the actually used memory, then assign it
989 cp = alloc(sizeof(struct env) - sizeof(ALLOC_ITEM), ATEMP);
990 /* undo what alloc() did to the malloc result address */
991 ep = (void *)(cp - sizeof(ALLOC_ITEM));
992 /* initialise public members of struct env (not the ALLOC_ITEM) */
998 ep->yyrecursive_statep = NULL;
1001 /* jump buffer is invalid because flags == 0 */
1006 quitenv(struct shf *shf)
1012 yyrecursive_pop(true);
1013 while (ep->oenv && ep->oenv->loc != ep->loc)
1015 if (ep->savefd != NULL) {
1016 for (fd = 0; fd < NUFILE; fd++)
1017 /* if ep->savefd[fd] < 0, means fd was closed */
1019 restfd(fd, ep->savefd[fd]);
1021 /* Clear any write errors */
1022 shf_reopen(2, SHF_WR, shl_out);
1025 * Bottom of the stack.
1026 * Either main shell is exiting or cleanup_parents_env() was called.
1028 if (ep->oenv == NULL) {
1033 if (ep->type == E_NONE) {
1034 /* Main shell exiting? */
1035 #if HAVE_PERSISTENT_HISTORY
1040 if (ep->flags & EF_FAKE_SIGDIE) {
1041 int sig = (exstat & 0xFF) - 128;
1044 * ham up our death a bit (AT&T ksh
1045 * only seems to do this for SIGTERM)
1046 * Don't do it for SIGQUIT, since we'd
1049 if ((sig == SIGINT || sig == SIGTERM) &&
1050 (kshpgrp == kshpid)) {
1051 setsig(&sigtraps[sig], SIG_DFL,
1052 SS_RESTORE_CURR | SS_FORCE);
1061 #ifndef MKSH_NO_CMDLINE_EDITING
1064 #ifndef MKSH_NOPROSPECTOFWORK
1065 /* block at least SIGCHLD during/after afreeall */
1066 sigprocmask(SIG_BLOCK, &sm_sigchld, NULL);
1069 for (fd = 3; fd < NUFILE; fd++)
1070 if ((i = fcntl(fd, F_GETFD, 0)) != -1 &&
1077 exit(exstat & 0xFF);
1085 /* free the struct env - tricky due to the ALLOC_ITEM inside */
1087 afree(cp + sizeof(ALLOC_ITEM), ATEMP);
1090 /* Called after a fork to cleanup stuff left over from parents environment */
1092 cleanup_parents_env(void)
1098 * Don't clean up temporary files - parent will probably need them.
1099 * Also, can't easily reclaim memory since variables, etc. could be
1103 /* close all file descriptors hiding in savefd */
1104 for (ep = e; ep; ep = ep->oenv) {
1106 for (fd = 0; fd < NUFILE; fd++)
1107 if (ep->savefd[fd] > 0)
1108 close(ep->savefd[fd]);
1109 afree(ep->savefd, &ep->area);
1113 if (ep->type != E_NONE)
1122 /* Called just before an execve cleanup stuff temporary files */
1124 cleanup_proc_env(void)
1128 for (ep = e; ep; ep = ep->oenv)
1129 remove_temps(ep->temps);
1132 /* remove temp files and free ATEMP Area */
1138 while ((l = e->loc) && (!e->oenv || e->oenv->loc != l)) {
1143 remove_temps(e->temps);
1147 * if the memory backing source is reclaimed, things
1148 * will end up badly when a function expecting it to
1149 * be valid is run; a NULL pointer is easily debugged
1151 if (source && source->areap == &e->area)
1157 remove_temps(struct temp *tp)
1160 if (tp->pid == procpid)
1167 * Initialise tty_fd. Used for tracking the size of the terminal,
1168 * saving/resetting tty modes upon forground job completion, and
1169 * for setting up the tty process group. Return values:
1170 * 0 = got controlling tty
1171 * 1 = got terminal but no controlling tty
1172 * 2 = cannot find a terminal
1174 * 4 = cannot make fd close-on-exec
1175 * An existing tty_fd is cached if no "better" one could be found,
1176 * i.e. if tty_devtty was already set or the new would not set it.
1181 int fd, rv, eno = 0;
1182 bool do_close = false, is_devtty = true;
1185 /* already got a tty which is /dev/tty */
1190 /*XXX imake style */
1192 /* fd 3 on UWIN _is_ /dev/tty (or our controlling tty) */
1197 if ((fd = open(T_devtty, O_RDWR, 0)) >= 0) {
1204 /* already got a non-devtty one */
1210 if (isatty((fd = 0)) || isatty((fd = 2)))
1212 /* cannot find one */
1214 /* assert: do_close == false */
1218 if ((rv = fcntl(fd, F_DUPFD, FDBASE)) < 0) {
1223 if (fcntl(rv, F_SETFD, FD_CLOEXEC) < 0) {
1230 tty_devtty = is_devtty;
1239 /* A shell error occurred (eg, syntax error, etc.) */
1241 #define VWARNINGF_ERRORPREFIX 1
1242 #define VWARNINGF_FILELINE 2
1243 #define VWARNINGF_BUILTIN 4
1244 #define VWARNINGF_INTERNAL 8
1246 static void vwarningf(unsigned int, const char *, va_list)
1247 MKSH_A_FORMAT(__printf__, 2, 0);
1250 vwarningf(unsigned int flags, const char *fmt, va_list ap)
1253 if (flags & VWARNINGF_INTERNAL)
1254 shf_fprintf(shl_out, Tf_sD_, "internal error");
1255 if (flags & VWARNINGF_ERRORPREFIX)
1256 error_prefix(tobool(flags & VWARNINGF_FILELINE));
1257 if ((flags & VWARNINGF_BUILTIN) &&
1258 /* not set when main() calls parse_args() */
1259 builtin_argv0 && builtin_argv0 != kshname)
1260 shf_fprintf(shl_out, Tf_sD_, builtin_argv0);
1261 shf_vfprintf(shl_out, fmt, ap);
1262 shf_putchar('\n', shl_out);
1268 errorfx(int rc, const char *fmt, ...)
1274 /* debugging: note that stdout not valid */
1275 shl_stdout_ok = false;
1278 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1284 errorf(const char *fmt, ...)
1290 /* debugging: note that stdout not valid */
1291 shl_stdout_ok = false;
1294 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE, fmt, va);
1299 /* like errorf(), but no unwind is done */
1301 warningf(bool fileline, const char *fmt, ...)
1306 vwarningf(VWARNINGF_ERRORPREFIX | (fileline ? VWARNINGF_FILELINE : 0),
1312 * Used by built-in utilities to prefix shell and utility name to message
1313 * (also unwinds environments for special builtins).
1316 bi_errorf(const char *fmt, ...)
1320 /* debugging: note that stdout not valid */
1321 shl_stdout_ok = false;
1326 vwarningf(VWARNINGF_ERRORPREFIX | VWARNINGF_FILELINE |
1327 VWARNINGF_BUILTIN, fmt, va);
1330 /* POSIX special builtins cause non-interactive shells to exit */
1332 builtin_argv0 = NULL;
1333 /* may not want to use LERROR here */
1338 /* Called when something that shouldn't happen does */
1340 internal_errorf(const char *fmt, ...)
1345 vwarningf(VWARNINGF_INTERNAL, fmt, va);
1351 internal_warningf(const char *fmt, ...)
1356 vwarningf(VWARNINGF_INTERNAL, fmt, va);
1360 /* used by error reporting functions to print "ksh: .kshrc[25]: " */
1362 error_prefix(bool fileline)
1364 /* Avoid foo: foo[2]: ... */
1365 if (!fileline || !source || !source->file ||
1366 strcmp(source->file, kshname) != 0)
1367 shf_fprintf(shl_out, Tf_sD_, kshname + (*kshname == '-'));
1368 if (fileline && source && source->file != NULL) {
1369 shf_fprintf(shl_out, "%s[%lu]: ", source->file,
1370 (unsigned long)(source->errline ?
1371 source->errline : source->line));
1372 source->errline = 0;
1376 /* printf to shl_out (stderr) with flush */
1378 shellf(const char *fmt, ...)
1383 /* shl_out may not be set up yet... */
1386 shf_vfprintf(shl_out, fmt, va);
1391 /* printf to shl_stdout (stdout) */
1393 shprintf(const char *fmt, ...)
1398 internal_errorf("shl_stdout not valid");
1400 shf_vfprintf(shl_stdout, fmt, va);
1404 /* test if we can seek backwards fd (returns 0 or SHF_UNBUF) */
1410 return (fstat(fd, &statb) == 0 && !S_ISREG(statb.st_mode) ?
1420 struct shf shf_iob[NSHF_IOB];
1429 /* force buffer allocation */
1430 shf_fdopen(1, SHF_WR, shl_stdout);
1431 shf_fdopen(2, SHF_WR, shl_out);
1432 shf_fdopen(2, SHF_WR, shl_xtrace);
1434 if ((lfp = getenv("SDMKSH_PATH")) == NULL) {
1435 if ((lfp = getenv("HOME")) == NULL || !mksh_abspath(lfp))
1436 errorf("can't get home directory");
1437 lfp = shf_smprintf(Tf_sSs, lfp, "mksh-dbg.txt");
1440 if ((shl_dbg_fd = open(lfp, O_WRONLY | O_APPEND | O_CREAT, 0600)) < 0)
1441 errorf("can't open debug output file %s", lfp);
1442 if (shl_dbg_fd < FDBASE) {
1445 nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
1447 if ((shl_dbg_fd = nfd) == -1)
1448 errorf("can't dup debug output file");
1450 fcntl(shl_dbg_fd, F_SETFD, FD_CLOEXEC);
1451 shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
1457 /* A dup2() with error checking */
1459 ksh_dup2(int ofd, int nfd, bool errok)
1463 if (((rv = dup2(ofd, nfd)) < 0) && !errok && (errno != EBADF))
1464 errorf(Ttoo_many_files);
1467 /*XXX imake style */
1469 fcntl(nfd, F_SETFD, 0);
1476 * Move fd from user space (0 <= fd < 10) to shell space (fd >= 10),
1477 * set close-on-exec flag. See FDBASE in sh.h, maybe 24 not 10 here.
1484 if (fd < FDBASE && (nfd = fcntl(fd, F_DUPFD, FDBASE)) < 0 &&
1485 (errno == EBADF || errno == EPERM))
1487 if (nfd < 0 || nfd > SHRT_MAX)
1488 errorf(Ttoo_many_files);
1489 fcntl(nfd, F_SETFD, FD_CLOEXEC);
1490 return ((short)nfd);
1494 restfd(int fd, int ofd)
1497 shf_flush(&shf_iob[/* fd */ 2]);
1499 /* original fd closed */
1501 else if (fd != ofd) {
1502 /*XXX: what to do if this dup fails? */
1503 ksh_dup2(ofd, fd, true);
1514 errorf("can't create pipe - try again");
1515 pv[0] = savefd(lpv[0]);
1516 if (pv[0] != lpv[0])
1518 pv[1] = savefd(lpv[1]);
1519 if (pv[1] != lpv[1])
1522 setmode(pv[0], O_BINARY);
1523 setmode(pv[1], O_BINARY);
1535 * Called by iosetup() (deals with 2>&4, etc.), c_read, c_print to turn
1536 * a string (the X in 2>&X, read -uX, print -uX) into a file descriptor.
1539 check_fd(const char *name, int mode, const char **emsgp)
1543 if (!name[0] || name[1])
1544 goto illegal_fd_name;
1546 return (coproc_getfd(mode, emsgp));
1547 if (!ctype(name[0], C_DIGIT)) {
1550 *emsgp = "illegal file descriptor name";
1554 if ((fl = fcntl((fd = ksh_numdig(name[0])), F_GETFL, 0)) < 0) {
1556 *emsgp = "bad file descriptor";
1561 * X_OK is a kludge to disable this check for dups (x<&1):
1562 * historical shells never did this check (XXX don't know what
1563 * POSIX has to say).
1565 if (!(mode & X_OK) && fl != O_RDWR && (
1566 ((mode & R_OK) && fl != O_RDONLY) ||
1567 ((mode & W_OK) && fl != O_WRONLY))) {
1569 *emsgp = (fl == O_WRONLY) ?
1570 "fd not open for reading" :
1571 "fd not open for writing";
1577 /* Called once from main */
1581 coproc.read = coproc.readw = coproc.write = -1;
1586 /* Called by c_read() when eof is read - close fd if it is the co-process fd */
1588 coproc_read_close(int fd)
1590 if (coproc.read >= 0 && fd == coproc.read) {
1591 coproc_readw_close(fd);
1598 * Called by c_read() and by iosetup() to close the other side of the
1599 * read pipe, so reads will actually terminate.
1602 coproc_readw_close(int fd)
1604 if (coproc.readw >= 0 && coproc.read >= 0 && fd == coproc.read) {
1605 close(coproc.readw);
1611 * Called by c_print when a write to a fd fails with EPIPE and by iosetup
1612 * when co-process input is dup'd
1615 coproc_write_close(int fd)
1617 if (coproc.write >= 0 && fd == coproc.write) {
1618 close(coproc.write);
1624 * Called to check for existence of/value of the co-process file descriptor.
1625 * (Used by check_fd() and by c_read/c_print to deal with -p option).
1628 coproc_getfd(int mode, const char **emsgp)
1630 int fd = (mode & R_OK) ? coproc.read : coproc.write;
1635 *emsgp = "no coprocess";
1640 * called to close file descriptors related to the coprocess (if any)
1641 * Should be called with SIGCHLD blocked.
1644 coproc_cleanup(int reuse)
1646 /* This to allow co-processes to share output pipe */
1647 if (!reuse || coproc.readw < 0 || coproc.read < 0) {
1648 if (coproc.read >= 0) {
1652 if (coproc.readw >= 0) {
1653 close(coproc.readw);
1657 if (coproc.write >= 0) {
1658 close(coproc.write);
1664 maketemp(Area *ap, Temp_type type, struct temp **tlist)
1673 dir = tmpdir ? tmpdir : MKSH_DEFAULT_TMPDIR;
1674 /* add "/shXXXXXX.tmp" plus NUL */
1676 checkoktoadd(len, offsetof(struct temp, tffn[0]) + 14);
1677 tp = alloc(offsetof(struct temp, tffn[0]) + 14 + len, ap);
1683 if (stat(dir, &sb) || !S_ISDIR(sb.st_mode)) {
1689 cp += offsetof(struct temp, tffn[0]);
1690 memcpy(cp, dir, len);
1692 memcpy(cp, "/shXXXXXX.tmp", 14);
1693 /* point to the first of six Xes */
1696 /* cyclically attempt to open a temporary file */
1698 /* generate random part of filename */
1701 cp[len++] = digits_lc[rndget() % 36];
1704 /* check if this one works */
1705 if ((i = binopen3(tp->tffn, O_CREAT | O_EXCL | O_RDWR,
1706 0600)) < 0 && errno != EEXIST)
1710 if (type == TT_FUNSUB) {
1711 /* map us high and mark as close-on-exec */
1712 if ((j = savefd(i)) != i) {
1717 /* operation mode for the shf */
1722 /* shf_fdopen cannot fail, so no fd leak */
1723 tp->shf = shf_fdopen(i, j, NULL);
1732 * We use a similar collision resolution algorithm as Python 2.5.4
1733 * but with a slightly tweaked implementation written from scratch.
1736 #define INIT_TBLSHIFT 3 /* initial table shift (2^3 = 8) */
1737 #define PERTURB_SHIFT 5 /* see Python 2.5.4 Objects/dictobject.c */
1739 static void tgrow(struct table *);
1740 static int tnamecmp(const void *, const void *);
1743 tgrow(struct table *tp)
1745 size_t i, j, osize, mask, perturb;
1746 struct tbl *tblp, **pp;
1747 struct tbl **ntblp, **otblp = tp->tbls;
1749 if (tp->tshift > 29)
1750 internal_errorf("hash table size limit reached");
1752 /* calculate old size, new shift and new size */
1753 osize = (size_t)1 << (tp->tshift++);
1756 ntblp = alloc2(i, sizeof(struct tbl *), tp->areap);
1757 /* multiplication cannot overflow: alloc2 checked that */
1758 memset(ntblp, 0, i * sizeof(struct tbl *));
1760 /* table can get very full when reaching its size limit */
1761 tp->nfree = (tp->tshift == 30) ? 0x3FFF0000UL :
1762 /* but otherwise, only 75% */
1769 for (i = 0; i < osize; i++)
1770 if ((tblp = otblp[i]) != NULL) {
1771 if ((tblp->flag & DEFINED)) {
1772 /* search for free hash table slot */
1773 j = perturb = tblp->ua.hval;
1774 goto find_first_empty_slot;
1775 find_next_empty_slot:
1776 j = (j << 2) + j + perturb + 1;
1777 perturb >>= PERTURB_SHIFT;
1778 find_first_empty_slot:
1779 pp = &ntblp[j & mask];
1781 goto find_next_empty_slot;
1782 /* found an empty hash table slot */
1785 } else if (!(tblp->flag & FINUSE)) {
1786 afree(tblp, tp->areap);
1789 afree(otblp, tp->areap);
1793 ktinit(Area *ap, struct table *tp, uint8_t initshift)
1797 tp->tshift = ((initshift > INIT_TBLSHIFT) ?
1798 initshift : INIT_TBLSHIFT) - 1;
1802 /* table, name (key) to search for, hash(name), rv pointer to tbl ptr */
1804 ktscan(struct table *tp, const char *name, uint32_t h, struct tbl ***ppp)
1806 size_t j, perturb, mask;
1807 struct tbl **pp, *p;
1809 mask = ((size_t)1 << (tp->tshift)) - 1;
1810 /* search for hash table slot matching name */
1812 goto find_first_slot;
1814 j = (j << 2) + j + perturb + 1;
1815 perturb >>= PERTURB_SHIFT;
1817 pp = &tp->tbls[j & mask];
1818 if ((p = *pp) != NULL && (p->ua.hval != h || !(p->flag & DEFINED) ||
1819 strcmp(p->name, name)))
1820 goto find_next_slot;
1821 /* p == NULL if not found, correct found entry otherwise */
1827 /* table, name (key) to enter, hash(n) */
1829 ktenter(struct table *tp, const char *n, uint32_t h)
1831 struct tbl **pp, *p;
1835 if ((p = ktscan(tp, n, h, &pp)))
1838 if (tp->nfree == 0) {
1844 /* create new tbl entry */
1846 checkoktoadd(len, offsetof(struct tbl, name[0]) + 1);
1847 p = alloc(offsetof(struct tbl, name[0]) + ++len, tp->areap);
1850 p->areap = tp->areap;
1854 memcpy(p->name, n, len);
1856 /* enter in tp->tbls */
1863 ktwalk(struct tstate *ts, struct table *tp)
1865 ts->left = (size_t)1 << (tp->tshift);
1866 ts->next = tp->tbls;
1870 ktnext(struct tstate *ts)
1872 while (--ts->left >= 0) {
1873 struct tbl *p = *ts->next++;
1874 if (p != NULL && (p->flag & DEFINED))
1881 tnamecmp(const void *p1, const void *p2)
1883 const struct tbl *a = *((const struct tbl * const *)p1);
1884 const struct tbl *b = *((const struct tbl * const *)p2);
1886 return (ascstrcmp(a->name, b->name));
1890 ktsort(struct table *tp)
1893 struct tbl **p, **sp, **dp;
1896 * since the table is never entirely full, no need to reserve
1897 * additional space for the trailing NULL appended below
1899 i = (size_t)1 << (tp->tshift);
1900 p = alloc2(i, sizeof(struct tbl *), ATEMP);
1901 sp = tp->tbls; /* source */
1904 if ((*dp = *sp++) != NULL && (((*dp)->flag & DEFINED) ||
1905 ((*dp)->flag & ARRAY)))
1907 qsort(p, (i = dp - p), sizeof(struct tbl *), tnamecmp);
1914 x_sigwinch(int sig MKSH_A_UNUSED)
1916 /* this runs inside interrupt context, with errno saved */
1924 DF(const char *fmt, ...)
1930 mksh_lockfd(shl_dbg_fd);
1932 timet2mjd(&mjd, tv.tv_sec);
1933 shf_fprintf(shl_dbg, "[%02u:%02u:%02u (%u) %u.%06u] ",
1934 (unsigned)mjd.sec / 3600, ((unsigned)mjd.sec / 60) % 60,
1935 (unsigned)mjd.sec % 60, (unsigned)getpid(),
1936 (unsigned)tv.tv_sec, (unsigned)tv.tv_usec);
1937 va_start(args, fmt);
1938 shf_vfprintf(shl_dbg, fmt, args);
1940 shf_putc('\n', shl_dbg);
1942 mksh_unlkfd(shl_dbg_fd);
1947 x_mkraw(int fd, mksh_ttyst *ocb, bool forread)
1952 mksh_tcget(fd, ocb);
1958 cb.c_iflag &= ~(ISTRIP);
1959 cb.c_lflag &= ~(ICANON) | ECHO;
1961 cb.c_iflag &= ~(INLCR | ICRNL | ISTRIP);
1962 cb.c_lflag &= ~(ISIG | ICANON | ECHO);
1964 #if defined(VLNEXT) && defined(_POSIX_VDISABLE)
1965 /* OSF/1 processes lnext when ~icanon */
1966 cb.c_cc[VLNEXT] = _POSIX_VDISABLE;
1968 /* SunOS 4.1.x & OSF/1 processes discard(flush) when ~icanon */
1969 #if defined(VDISCARD) && defined(_POSIX_VDISABLE)
1970 cb.c_cc[VDISCARD] = _POSIX_VDISABLE;
1975 mksh_tcset(fd, &cb);
1987 struct dirent *dent;
1989 if ((dirp = opendir(MKSH_ENVDIR)) == NULL) {
1990 warningf(false, "cannot read environment from %s: %s",
1991 MKSH_ENVDIR, cstrerror(errno));
1994 XinitN(xs, 256, ATEMP);
1997 if ((dent = readdir(dirp)) != NULL) {
1998 if (skip_varname(dent->d_name, true)[0] == '\0') {
1999 xp = shf_smprintf(Tf_sSs, MKSH_ENVDIR, dent->d_name);
2000 if (!(shf = shf_open(xp, O_RDONLY, 0, 0))) {
2002 "cannot read environment %s from %s: %s",
2003 dent->d_name, MKSH_ENVDIR,
2008 n = strlen(dent->d_name);
2009 xp = Xstring(xs, xp);
2010 XcheckN(xs, xp, n + 32);
2011 memcpy(xp, dent->d_name, n);
2014 while ((n = shf_read(xp, Xnleft(xs, xp), shf)) > 0) {
2016 if (Xnleft(xs, xp) <= 0)
2017 XcheckN(xs, xp, Xlength(xs, xp));
2021 "cannot read environment %s from %s: %s",
2022 dent->d_name, MKSH_ENVDIR,
2023 cstrerror(shf_errno(shf)));
2026 xp = Xstring(xs, xp);
2028 typeset(xp, IMPORT | EXPORT, 0, 0, 0);
2034 warningf(false, "cannot read environment from %s: %s",
2035 MKSH_ENVDIR, cstrerror(errno));
2040 extern char **environ;
2047 if (environ == NULL)
2050 wp = (const char **)environ;
2051 while (*wp != NULL) {
2053 typeset(*wp, IMPORT | EXPORT, 0, 0, 0);
2059 #ifdef MKSH_EARLY_LOCALE_TRACKING
2065 ccp = str_val(global("LC_ALL"));
2067 ccp = str_val(global("LC_CTYPE"));
2069 ccp = str_val(global("LANG"));
2070 UTFMODE = isuc(ccp);
2071 #if HAVE_SETLOCALE_CTYPE
2072 ccp = setlocale(LC_CTYPE, ccp);
2073 #if HAVE_LANGINFO_CODESET
2075 ccp = nl_langinfo(CODESET);
2082 warningf(true, "early locale tracking enabled UTF-8 mode while in POSIX mode, you are now noncompliant");