/* print variable/alias value using necessary quotes * (POSIX says they should be suitable for re-entry...) * No trailing newline is printed. */ void print_value_quoted(const char *s) { const char *p; int inquote = 0; /* Test if any quotes are needed */ for (p = s; *p; p++) if (ctype(*p, C_QUOTE)) break; if (!*p) { shf_puts(s, shl_stdout); return; } for (p = s; *p; p++) { if (*p == '\'') { if (inquote) shf_putc('\'', shl_stdout); shf_putc('\\', shl_stdout); inquote = 0; } else if (!inquote) { shf_putc('\'', shl_stdout); inquote = 1; } shf_putc(*p, shl_stdout); } if (inquote) shf_putc('\'', shl_stdout); }
/* Close a string - if it was opened for writing, it is null terminated; * returns a pointer to the string and frees shf if it was allocated * (does not free string if it was allocated). */ char * shf_sclose(struct shf *shf) { unsigned char *s = shf->buf; /* null terminate */ if (shf->flags & SHF_WR) { shf->wnleft++; shf_putc('\0', shf); } if (shf->flags & SHF_ALLOCS) afree(shf, shf->areap); return (char *) s; }
int pprompt(const char *cp, int ntruncate) { char delimiter = 0; bool doprint = (ntruncate != -1); bool indelimit = false; int columns = 0, lines = 0; /* * Undocumented AT&T ksh feature: * If the second char in the prompt string is \r then the first * char is taken to be a non-printing delimiter and any chars * between two instances of the delimiter are not considered to * be part of the prompt length */ if (*cp && cp[1] == '\r') { delimiter = *cp; cp += 2; } for (; *cp; cp++) { if (indelimit && *cp != delimiter) ; else if (*cp == '\n' || *cp == '\r') { lines += columns / x_cols + ((*cp == '\n') ? 1 : 0); columns = 0; } else if (*cp == '\t') { columns = (columns | 7) + 1; } else if (*cp == '\b') { if (columns > 0) columns--; } else if (*cp == delimiter) indelimit = !indelimit; else if (UTFMODE && ((unsigned char)*cp > 0x7F)) { const char *cp2; columns += utf_widthadj(cp, &cp2); if (doprint && (indelimit || (ntruncate < (x_cols * lines + columns)))) shf_write(cp, cp2 - cp, shl_out); cp = cp2 - /* loop increment */ 1; continue; } else columns++; if (doprint && (*cp != delimiter) && (indelimit || (ntruncate < (x_cols * lines + columns)))) shf_putc(*cp, shl_out); } if (doprint) shf_flush(shl_out); return (x_cols * lines + columns); }
static void pioact(struct shf *shf, struct ioword *iop) { int flag = iop->flag; int type = flag & IOTYPE; int expected; expected = (type == IOREAD || type == IORDWR || type == IOHERE) ? 0 : (type == IOCAT || type == IOWRITE) ? 1 : (type == IODUP && (iop->unit == !(flag & IORDUP))) ? iop->unit : iop->unit + 1; if (iop->unit != expected) shf_fprintf(shf, "%d", iop->unit); switch (type) { case IOREAD: shf_putc('<', shf); break; case IOHERE: shf_puts("<<", shf); if (flag & IOSKIP) shf_putc('-', shf); break; case IOCAT: shf_puts(">>", shf); break; case IOWRITE: shf_putc('>', shf); if (flag & IOCLOB) shf_putc('|', shf); break; case IORDWR: shf_puts("<>", shf); break; case IODUP: shf_puts(flag & IORDUP ? "<&" : ">&", shf); break; } /* name/delim are NULL when printing syntax errors */ if (type == IOHERE) { if (iop->delim) wdvarput(shf, iop->delim, 0, WDS_TPUTS); if (iop->flag & IOHERESTR) shf_putc(' ', shf); } else if (iop->name) { if (iop->flag & IONAMEXP) print_value_quoted(shf, iop->name); else wdvarput(shf, iop->name, 0, WDS_TPUTS); shf_putc(' ', shf); } prevent_semicolon = false; }
void yyerror(const char *fmt, ...) { va_list va; /* pop aliases and re-reads */ while (source->type == SALIAS || source->type == SREREAD) source = source->next; /* zap pending input */ source->str = null; error_prefix(true); va_start(va, fmt); shf_vfprintf(shl_out, fmt, va); shf_putc('\n', shl_out); va_end(va); errorfz(); }
void change_xtrace(unsigned char newval, bool dosnapshot) { static bool in_xtrace; if (in_xtrace) return; if (!dosnapshot && newval == Flag(FXTRACE)) return; if (Flag(FXTRACE) == 2) { shf_putc('\n', shl_xtrace); Flag(FXTRACE) = 1; shf_flush(shl_xtrace); } if (!dosnapshot && Flag(FXTRACE) == 1) switch (newval) { case 1: return; case 2: goto changed_xtrace; } shf_flush(shl_xtrace); if (shl_xtrace->fd != 2) close(shl_xtrace->fd); if (!newval || (shl_xtrace->fd = savefd(2)) == -1) shl_xtrace->fd = 2; changed_xtrace: if ((Flag(FXTRACE) = newval) == 2) { in_xtrace = true; Flag(FXTRACE) = 0; shf_puts(substitute(str_val(global("PS4")), 0), shl_xtrace); Flag(FXTRACE) = 2; in_xtrace = false; } }
static void printoptions(bool verbose) { int i = 0; if (verbose) { int n = 0, len, octs = 0; struct options_info oi; /* verbose version */ shf_puts("Current option settings\n", shl_stdout); oi.opt_width = 0; while (i < (int)NELEM(options)) { if (options[i].name) { oi.opts[n++] = i; len = strlen(options[i].name); if (len > octs) octs = len; len = utf_mbswidth(options[i].name); if (len > oi.opt_width) oi.opt_width = len; } ++i; } print_columns(shl_stdout, n, options_fmt_entry, &oi, octs + 4, oi.opt_width + 4, true); } else { /* short version á la AT&T ksh93 */ shf_puts("set", shl_stdout); while (i < (int)NELEM(options)) { if (Flag(i) && options[i].name) shprintf(" -o %s", options[i].name); ++i; } shf_putc('\n', shl_stdout); } }
static void printoptions(bool verbose) { size_t i = 0; if (verbose) { size_t n = 0, len, octs = 0; struct options_info oi; /* verbose version */ shf_puts("Current option settings\n", shl_stdout); oi.opt_width = 0; while (i < NELEM(options)) { if ((len = strlen(OFN(i)))) { oi.opts[n++] = i; if (len > octs) octs = len; len = utf_mbswidth(OFN(i)); if ((int)len > oi.opt_width) oi.opt_width = (int)len; } ++i; } print_columns(shl_stdout, n, options_fmt_entry, &oi, octs + 4, oi.opt_width + 4, true); } else { /* short version like AT&T ksh93 */ shf_puts(Tset, shl_stdout); while (i < NELEM(options)) { if (Flag(i) && OFN(i)[0]) shprintf(" -o %s", OFN(i)); ++i; } shf_putc('\n', shl_stdout); } }
/* * Print things in columns and rows - func() is called to format * the i-th element */ void print_columns(struct shf *shf, unsigned int n, void (*func)(char *, size_t, unsigned int, const void *), const void *arg, size_t max_oct, size_t max_colz, bool prefcol) { unsigned int i, r, c, rows, cols, nspace, max_col; char *str; if (!n) return; if (max_colz > 2147483646) { #ifndef MKSH_SMALL internal_warningf("print_columns called with %s=%zu >= INT_MAX", "max_col", max_colz); #endif return; } max_col = (unsigned int)max_colz; if (max_oct > 2147483646) { #ifndef MKSH_SMALL internal_warningf("print_columns called with %s=%zu >= INT_MAX", "max_oct", max_oct); #endif return; } ++max_oct; str = alloc(max_oct, ATEMP); /* * We use (max_col + 2) to consider the separator space. * Note that no spaces are printed after the last column * to avoid problems with terminals that have auto-wrap, * but we need to also take this into account in x_cols. */ cols = (x_cols + 1) / (max_col + 2); /* if we can only print one column anyway, skip the goo */ if (cols < 2) { for (i = 0; i < n; ++i) { (*func)(str, max_oct, i, arg); shf_puts(str, shf); shf_putc('\n', shf); } goto out; } rows = (n + cols - 1) / cols; if (prefcol && cols > rows) { cols = rows; rows = (n + cols - 1) / cols; } nspace = (x_cols - max_col * cols) / cols; if (nspace < 2) nspace = 2; max_col = -max_col; for (r = 0; r < rows; r++) { for (c = 0; c < cols; c++) { if ((i = c * rows + r) >= n) break; (*func)(str, max_oct, i, arg); if (i + rows >= n) shf_puts(str, shf); else shf_fprintf(shf, "%*s%*s", max_col, str, nspace, null); } shf_putchar('\n', shf); } out: afree(str, ATEMP); }
/* * print variable/alias value using necessary quotes * (POSIX says they should be suitable for re-entry...) * No trailing newline is printed. */ void print_value_quoted(struct shf *shf, const char *s) { unsigned char c; const unsigned char *p = (const unsigned char *)s; bool inquote = true; /* first, check whether any quotes are needed */ while ((c = *p++) >= 32) if (ctype(c, C_QUOTE)) inquote = false; p = (const unsigned char *)s; if (c == 0) { if (inquote) { /* nope, use the shortcut */ shf_puts(s, shf); return; } /* otherwise, quote nicely via state machine */ while ((c = *p++) != 0) { if (c == '\'') { /* * multiple single quotes or any of them * at the beginning of a string look nicer * this way than when simply substituting */ if (inquote) { shf_putc('\'', shf); inquote = false; } shf_putc('\\', shf); } else if (!inquote) { shf_putc('\'', shf); inquote = true; } shf_putc(c, shf); } } else { unsigned int wc; size_t n; /* use $'...' quote format */ shf_putc('$', shf); shf_putc('\'', shf); while ((c = *p) != 0) { if (c >= 0xC2) { n = utf_mbtowc(&wc, (const char *)p); if (n != (size_t)-1) { p += n; shf_fprintf(shf, "\\u%04X", wc); continue; } } ++p; switch (c) { /* see unbksl() in this file for comments */ case 7: c = 'a'; if (0) /* FALLTHROUGH */ case '\b': c = 'b'; if (0) /* FALLTHROUGH */ case '\f': c = 'f'; if (0) /* FALLTHROUGH */ case '\n': c = 'n'; if (0) /* FALLTHROUGH */ case '\r': c = 'r'; if (0) /* FALLTHROUGH */ case '\t': c = 't'; if (0) /* FALLTHROUGH */ case 11: c = 'v'; if (0) /* FALLTHROUGH */ case '\033': /* take E not e because \e is \ in *roff */ c = 'E'; /* FALLTHROUGH */ case '\\': shf_putc('\\', shf); if (0) /* FALLTHROUGH */ default: if (c < 32 || c > 0x7E) { /* FALLTHROUGH */ case '\'': shf_fprintf(shf, "\\%03o", c); break; } shf_putc(c, shf); break; } } inquote = true; } if (inquote) shf_putc('\'', shf); }
static int comexec(struct op *t, struct tbl * volatile tp, const char **ap, volatile int flags, volatile int *xerrok) { int i; volatile int rv = 0; const char *cp; const char **lastp; /* Must be static (XXX but why?) */ static struct op texec; int type_flags; bool resetspec; int fcflags = FC_BI|FC_FUNC|FC_PATH; struct block *l_expand, *l_assign; int optc; const char *exec_argv0 = NULL; bool exec_clrenv = false; /* snag the last argument for $_ */ if (Flag(FTALKING) && *(lastp = ap)) { /* * XXX not the same as AT&T ksh, which only seems to set $_ * after a newline (but not in functions/dot scripts, but in * interactive and script) - perhaps save last arg here and * set it in shell()?. */ while (*++lastp) ; /* setstr() can't fail here */ setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp, KSH_RETURN_ERROR); } /** * Deal with the shell builtins builtin, exec and command since * they can be followed by other commands. This must be done before * we know if we should create a local block which must be done * before we can do a path search (in case the assignments change * PATH). * Odd cases: * FOO=bar exec >/dev/null FOO is kept but not exported * FOO=bar exec foobar FOO is exported * FOO=bar command exec >/dev/null FOO is neither kept nor exported * FOO=bar command FOO is neither kept nor exported * PATH=... foobar use new PATH in foobar search */ resetspec = false; while (tp && tp->type == CSHELL) { /* undo effects of command */ fcflags = FC_BI|FC_FUNC|FC_PATH; if (tp->val.f == c_builtin) { if ((cp = *++ap) == NULL || (!strcmp(cp, "--") && (cp = *++ap) == NULL)) { tp = NULL; break; } if ((tp = findcom(cp, FC_BI)) == NULL) errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin"); if (tp->type == CSHELL && (tp->val.f == c_cat #ifdef MKSH_PRINTF_BUILTIN || tp->val.f == c_printf #endif )) break; continue; } else if (tp->val.f == c_exec) { if (ap[1] == NULL) break; ksh_getopt_reset(&builtin_opt, GF_ERROR); while ((optc = ksh_getopt(ap, &builtin_opt, "a:c")) != -1) switch (optc) { case 'a': exec_argv0 = builtin_opt.optarg; break; case 'c': exec_clrenv = true; /* ensure we can actually do this */ resetspec = true; break; default: rv = 2; goto Leave; } ap += builtin_opt.optind; flags |= XEXEC; } else if (tp->val.f == c_command) { bool saw_p = false; /* * Ugly dealing with options in two places (here * and in c_command(), but such is life) */ ksh_getopt_reset(&builtin_opt, 0); while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p') saw_p = true; if (optc != -1) /* command -vV or something */ break; /* don't look for functions */ fcflags = FC_BI|FC_PATH; if (saw_p) { if (Flag(FRESTRICTED)) { warningf(true, "%s: %s", "command -p", "restricted"); rv = 1; goto Leave; } fcflags |= FC_DEFPATH; } ap += builtin_opt.optind; /* * POSIX says special builtins lose their status * if accessed using command. */ resetspec = true; if (!ap[0]) { /* ensure command with no args exits with 0 */ subst_exstat = 0; break; } } else if (tp->val.f == c_cat) { /* if we have any flags, do not use the builtin */ if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' && /* argument, begins with -, is not - or -- */ (ap[1][1] != '-' || ap[1][2] != '\0')) { struct tbl *ext_cat; ext_cat = findcom(Tcat, FC_PATH | FC_FUNC); if (ext_cat && (ext_cat->type != CTALIAS || (ext_cat->flag & ISSET))) tp = ext_cat; } break; #ifdef MKSH_PRINTF_BUILTIN } else if (tp->val.f == c_printf) { struct tbl *ext_printf; ext_printf = findcom(Tprintf, FC_PATH | FC_FUNC); if (ext_printf && (ext_printf->type != CTALIAS || (ext_printf->flag & ISSET))) tp = ext_printf; break; #endif } else if (tp->val.f == c_trap) { t->u.evalflags &= ~DOTCOMEXEC; break; } else break; tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC)); } if (t->u.evalflags & DOTCOMEXEC) flags |= XEXEC; l_expand = e->loc; if (!resetspec && (!ap[0] || (tp && (tp->flag & KEEPASN)))) type_flags = 0; else { /* create new variable/function block */ newblock(); /* ksh functions don't keep assignments, POSIX functions do. */ if (!resetspec && tp && tp->type == CFUNC && !(tp->flag & FKSH)) type_flags = EXPORT; else type_flags = LOCAL|LOCAL_COPY|EXPORT; } l_assign = e->loc; if (exec_clrenv) l_assign->flags |= BF_STOPENV; if (Flag(FEXPORT)) type_flags |= EXPORT; if (Flag(FXTRACE)) change_xtrace(2, false); for (i = 0; t->vars[i]; i++) { /* do NOT lookup in the new var/fn block just created */ e->loc = l_expand; cp = evalstr(t->vars[i], DOASNTILDE | DOSCALAR); e->loc = l_assign; if (Flag(FXTRACE)) { const char *ccp; ccp = skip_varname(cp, true); if (*ccp == '+') ++ccp; if (*ccp == '=') ++ccp; shf_write(cp, ccp - cp, shl_xtrace); print_value_quoted(shl_xtrace, ccp); shf_putc(' ', shl_xtrace); } /* but assign in there as usual */ typeset(cp, type_flags, 0, 0, 0); } if (Flag(FXTRACE)) { change_xtrace(2, false); if (ap[rv = 0]) { xtrace_ap_loop: print_value_quoted(shl_xtrace, ap[rv]); if (ap[++rv]) { shf_putc(' ', shl_xtrace); goto xtrace_ap_loop; } } change_xtrace(1, false); } if ((cp = *ap) == NULL) { rv = subst_exstat; goto Leave; } else if (!tp) { if (Flag(FRESTRICTED) && vstrchr(cp, '/')) { warningf(true, "%s: %s", cp, "restricted"); rv = 1; goto Leave; } tp = findcom(cp, fcflags); } switch (tp->type) { /* shell built-in */ case CSHELL: do_call_builtin: rv = call_builtin(tp, (const char **)ap, null, resetspec); if (resetspec && tp->val.f == c_shift) { l_expand->argc = l_assign->argc; l_expand->argv = l_assign->argv; } break; /* function call */ case CFUNC: { volatile uint32_t old_inuse; const char * volatile old_kshname; volatile uint8_t old_flags[FNFLAGS]; if (!(tp->flag & ISSET)) { struct tbl *ftp; if (!tp->u.fpath) { rv = (tp->u2.errnov == ENOENT) ? 127 : 126; warningf(true, "%s: %s %s: %s", cp, "can't find", "function definition file", cstrerror(tp->u2.errnov)); break; } if (include(tp->u.fpath, 0, NULL, false) < 0) { if (!strcmp(cp, Tcat)) { no_cat_in_FPATH: tp = findcom(Tcat, FC_BI); goto do_call_builtin; } #ifdef MKSH_PRINTF_BUILTIN if (!strcmp(cp, Tprintf)) { no_printf_in_FPATH: tp = findcom(Tprintf, FC_BI); goto do_call_builtin; } #endif warningf(true, "%s: %s %s %s: %s", cp, "can't open", "function definition file", tp->u.fpath, cstrerror(errno)); rv = 127; break; } if (!(ftp = findfunc(cp, hash(cp), false)) || !(ftp->flag & ISSET)) { if (!strcmp(cp, Tcat)) goto no_cat_in_FPATH; #ifdef MKSH_PRINTF_BUILTIN if (!strcmp(cp, Tprintf)) goto no_printf_in_FPATH; #endif warningf(true, "%s: %s %s", cp, "function not defined by", tp->u.fpath); rv = 127; break; } tp = ftp; } /* * ksh functions set $0 to function name, POSIX * functions leave $0 unchanged. */ old_kshname = kshname; if (tp->flag & FKSH) kshname = ap[0]; else ap[0] = kshname; e->loc->argv = ap; for (i = 0; *ap++ != NULL; i++) ; e->loc->argc = i - 1; /* * ksh-style functions handle getopts sanely, * Bourne/POSIX functions are insane... */ if (tp->flag & FKSH) { e->loc->flags |= BF_DOGETOPTS; e->loc->getopts_state = user_opt; getopts_reset(1); } for (type_flags = 0; type_flags < FNFLAGS; ++type_flags) old_flags[type_flags] = shell_flags[type_flags]; change_xtrace((Flag(FXTRACEREC) ? Flag(FXTRACE) : 0) | ((tp->flag & TRACE) ? 1 : 0), false); old_inuse = tp->flag & FINUSE; tp->flag |= FINUSE; e->type = E_FUNC; if (!(i = kshsetjmp(e->jbuf))) { execute(tp->val.t, flags & XERROK, NULL); i = LRETURN; } kshname = old_kshname; change_xtrace(old_flags[(int)FXTRACE], false); #ifndef MKSH_LEGACY_MODE if (tp->flag & FKSH) { /* Korn style functions restore Flags on return */ old_flags[(int)FXTRACE] = Flag(FXTRACE); for (type_flags = 0; type_flags < FNFLAGS; ++type_flags) shell_flags[type_flags] = old_flags[type_flags]; } #endif tp->flag = (tp->flag & ~FINUSE) | old_inuse; /* * Were we deleted while executing? If so, free the * execution tree. */ if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) { if (tp->flag & ALLOC) { tp->flag &= ~ALLOC; tfree(tp->val.t, tp->areap); } tp->flag = 0; } switch (i) { case LRETURN: case LERROR: rv = exstat & 0xFF; break; case LINTR: case LEXIT: case LLEAVE: case LSHELL: quitenv(NULL); unwind(i); /* NOTREACHED */ default: quitenv(NULL); internal_errorf("%s %d", "CFUNC", i); } break; } /* executable command */ case CEXEC: /* tracked alias */ case CTALIAS: if (!(tp->flag&ISSET)) { if (tp->u2.errnov == ENOENT) { rv = 127; warningf(true, "%s: %s", cp, "not found"); } else { rv = 126; warningf(true, "%s: %s: %s", cp, "can't execute", cstrerror(tp->u2.errnov)); } break; } /* set $_ to program's full path */ /* setstr() can't fail here */ setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0), tp->val.s, KSH_RETURN_ERROR); /* to fork, we set up a TEXEC node and call execute */ texec.type = TEXEC; /* for vistree/dumptree */ texec.left = t; texec.str = tp->val.s; texec.args = ap; /* in this case we do not fork, of course */ if (flags & XEXEC) { if (exec_argv0) texec.args[0] = exec_argv0; j_exit(); if (!(flags & XBGND) #ifndef MKSH_UNEMPLOYED || Flag(FMONITOR) #endif ) { setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG); setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG); } } rv = exchild(&texec, flags, xerrok, -1); break; } Leave: if (flags & XEXEC) { exstat = rv & 0xFF; unwind(LLEAVE); } return (rv); }
/* * print a command tree */ static void ptree(struct op *t, int indent, struct shf *shf) { const char **w; struct ioword **ioact; struct op *t1; int i; Chain: if (t == NULL) return; switch (t->type) { case TCOM: prevent_semicolon = false; /* * special-case 'var=<<EOF' (rough; see * exec.c:execute() for full code) */ if ( /* we have zero arguments, i.e. no programme to run */ t->args[0] == NULL && /* we have exactly one variable assignment */ t->vars[0] != NULL && t->vars[1] == NULL && /* we have exactly one I/O redirection */ t->ioact != NULL && t->ioact[0] != NULL && t->ioact[1] == NULL && /* of type "here document" (or "here string") */ (t->ioact[0]->flag & IOTYPE) == IOHERE) { fptreef(shf, indent, "%S", t->vars[0]); break; } if (t->vars) { w = (const char **)t->vars; while (*w) fptreef(shf, indent, "%S ", *w++); } else shf_puts("#no-vars# ", shf); if (t->args) { w = t->args; while (*w) fptreef(shf, indent, "%S ", *w++); } else shf_puts("#no-args# ", shf); break; case TEXEC: t = t->left; goto Chain; case TPAREN: fptreef(shf, indent + 2, "( %T) ", t->left); break; case TPIPE: fptreef(shf, indent, "%T| ", t->left); t = t->right; goto Chain; case TLIST: fptreef(shf, indent, "%T%;", t->left); t = t->right; goto Chain; case TOR: case TAND: fptreef(shf, indent, "%T%s %T", t->left, (t->type == TOR) ? "||" : "&&", t->right); break; case TBANG: shf_puts("! ", shf); prevent_semicolon = false; t = t->right; goto Chain; case TDBRACKET: w = t->args; shf_puts("[[", shf); while (*w) fptreef(shf, indent, " %S", *w++); shf_puts(" ]] ", shf); break; case TSELECT: case TFOR: fptreef(shf, indent, "%s %s ", (t->type == TFOR) ? "for" : Tselect, t->str); if (t->vars != NULL) { shf_puts("in ", shf); w = (const char **)t->vars; while (*w) fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%;"); } fptreef(shf, indent + INDENT, "do%N%T", t->left); fptreef(shf, indent, "%;done "); break; case TCASE: fptreef(shf, indent, "case %S in", t->str); for (t1 = t->left; t1 != NULL; t1 = t1->right) { fptreef(shf, indent, "%N("); w = (const char **)t1->vars; while (*w) { fptreef(shf, indent, "%S%c", *w, (w[1] != NULL) ? '|' : ')'); ++w; } fptreef(shf, indent + INDENT, "%N%T%N;%c", t1->left, t1->u.charflag); } fptreef(shf, indent, "%Nesac "); break; #ifdef DEBUG case TELIF: internal_errorf("TELIF in tree.c:ptree() unexpected"); /* FALLTHROUGH */ #endif case TIF: i = 2; t1 = t; goto process_TIF; do { t1 = t1->right; i = 0; fptreef(shf, indent, "%;"); process_TIF: /* 5 == strlen("elif ") */ fptreef(shf, indent + 5 - i, Telif_pT + i, t1->left); t1 = t1->right; if (t1->left != NULL) { fptreef(shf, indent, "%;"); fptreef(shf, indent + INDENT, "%s%N%T", "then", t1->left); } } while (t1->right && t1->right->type == TELIF); if (t1->right != NULL) { fptreef(shf, indent, "%;"); fptreef(shf, indent + INDENT, "%s%N%T", "else", t1->right); } fptreef(shf, indent, "%;fi "); break; case TWHILE: case TUNTIL: /* 6 == strlen("while "/"until ") */ fptreef(shf, indent + 6, "%s %T", (t->type == TWHILE) ? "while" : "until", t->left); fptreef(shf, indent, "%;"); fptreef(shf, indent + INDENT, "do%N%T", t->right); fptreef(shf, indent, "%;done "); break; case TBRACE: fptreef(shf, indent + INDENT, "{%N%T", t->left); fptreef(shf, indent, "%;} "); break; case TCOPROC: fptreef(shf, indent, "%T|& ", t->left); prevent_semicolon = true; break; case TASYNC: fptreef(shf, indent, "%T& ", t->left); prevent_semicolon = true; break; case TFUNCT: fpFUNCTf(shf, indent, tobool(t->u.ksh_func), t->str, t->left); break; case TTIME: fptreef(shf, indent, "%s %T", "time", t->left); break; default: shf_puts("<botch>", shf); prevent_semicolon = false; break; } if ((ioact = t->ioact) != NULL) { bool need_nl = false; while (*ioact != NULL) pioact(shf, *ioact++); /* Print here documents after everything else... */ ioact = t->ioact; while (*ioact != NULL) { struct ioword *iop = *ioact++; /* heredoc is NULL when tracing (set -x) */ if ((iop->flag & (IOTYPE | IOHERESTR)) == IOHERE && iop->heredoc) { shf_putc('\n', shf); shf_puts(iop->heredoc, shf); fptreef(shf, indent, "%s", iop->flag & IONDELIM ? "<<" : evalstr(iop->delim, 0)); need_nl = true; } } /* * Last delimiter must be followed by a newline (this * often leads to an extra blank line, but it's not * worth worrying about) */ if (need_nl) { shf_putc('\n', shf); prevent_semicolon = true; } } }
/* * print a command tree */ static void ptree(struct op *t, int indent, struct shf *shf) { const char **w; struct ioword **ioact; struct op *t1; int i; const char *ccp; Chain: if (t == NULL) return; switch (t->type) { case TCOM: prevent_semicolon = false; /* special-case 'var=<<EOF' (cf. exec.c:execute) */ if (t->args && /* we have zero arguments, i.e. no program to run */ t->args[0] == NULL && /* we have exactly one variable assignment */ t->vars[0] != NULL && t->vars[1] == NULL && /* we have exactly one I/O redirection */ t->ioact != NULL && t->ioact[0] != NULL && t->ioact[1] == NULL && /* of type "here document" (or "here string") */ (t->ioact[0]->ioflag & IOTYPE) == IOHERE && /* the variable assignment begins with a valid varname */ (ccp = skip_wdvarname(t->vars[0], true)) != t->vars[0] && /* and has no right-hand side (i.e. "varname=") */ ccp[0] == CHAR && ((ccp[1] == '=' && ccp[2] == EOS) || /* or "varname+=" */ (ccp[1] == '+' && ccp[2] == CHAR && ccp[3] == '=' && ccp[4] == EOS))) { fptreef(shf, indent, Tf_S, t->vars[0]); break; } if (t->vars) { w = (const char **)t->vars; while (*w) fptreef(shf, indent, Tf_S_, *w++); } else shf_puts("#no-vars# ", shf); if (t->args) { w = t->args; if (*w && **w == CHAR) { char *cp = wdstrip(*w++, WDS_TPUTS); if (valid_alias_name(cp)) shf_putc('\\', shf); shf_puts(cp, shf); shf_putc(' ', shf); afree(cp, ATEMP); } while (*w) fptreef(shf, indent, Tf_S_, *w++); } else shf_puts("#no-args# ", shf); break; case TEXEC: t = t->left; goto Chain; case TPAREN: fptreef(shf, indent + 2, "( %T) ", t->left); break; case TPIPE: fptreef(shf, indent, "%T| ", t->left); t = t->right; goto Chain; case TLIST: fptreef(shf, indent, "%T%;", t->left); t = t->right; goto Chain; case TOR: case TAND: fptreef(shf, indent, "%T%s %T", t->left, (t->type == TOR) ? "||" : "&&", t->right); break; case TBANG: shf_puts("! ", shf); prevent_semicolon = false; t = t->right; goto Chain; case TDBRACKET: w = t->args; shf_puts("[[", shf); while (*w) fptreef(shf, indent, Tf__S, *w++); shf_puts(" ]] ", shf); break; case TSELECT: case TFOR: fptreef(shf, indent, "%s %s ", (t->type == TFOR) ? "for" : Tselect, t->str); if (t->vars != NULL) { shf_puts("in ", shf); w = (const char **)t->vars; while (*w) fptreef(shf, indent, Tf_S_, *w++); fptreef(shf, indent, Tft_end); } fptreef(shf, indent + INDENT, "do%N%T", t->left); fptreef(shf, indent, "%;done "); break; case TCASE: fptreef(shf, indent, "case %S in", t->str); for (t1 = t->left; t1 != NULL; t1 = t1->right) { fptreef(shf, indent, "%N("); w = (const char **)t1->vars; while (*w) { fptreef(shf, indent, "%S%c", *w, (w[1] != NULL) ? '|' : ')'); ++w; } fptreef(shf, indent + INDENT, "%N%T%N;%c", t1->left, t1->u.charflag); } fptreef(shf, indent, "%Nesac "); break; case TELIF: internal_errorf(TELIF_unexpected); /* FALLTHROUGH */ case TIF: i = 2; t1 = t; goto process_TIF; do { t1 = t1->right; i = 0; fptreef(shf, indent, Tft_end); process_TIF: /* 5 == strlen("elif ") */ fptreef(shf, indent + 5 - i, Telif_pT + i, t1->left); t1 = t1->right; if (t1->left != NULL) { fptreef(shf, indent, Tft_end); fptreef(shf, indent + INDENT, "%s%N%T", "then", t1->left); } } while (t1->right && t1->right->type == TELIF); if (t1->right != NULL) { fptreef(shf, indent, Tft_end); fptreef(shf, indent + INDENT, "%s%N%T", "else", t1->right); } fptreef(shf, indent, "%;fi "); break; case TWHILE: case TUNTIL: /* 6 == strlen("while "/"until ") */ fptreef(shf, indent + 6, Tf_s_T, (t->type == TWHILE) ? "while" : "until", t->left); fptreef(shf, indent, Tft_end); fptreef(shf, indent + INDENT, "do%N%T", t->right); fptreef(shf, indent, "%;done "); break; case TBRACE: fptreef(shf, indent + INDENT, "{%N%T", t->left); fptreef(shf, indent, "%;} "); break; case TCOPROC: fptreef(shf, indent, "%T|& ", t->left); prevent_semicolon = true; break; case TASYNC: fptreef(shf, indent, "%T& ", t->left); prevent_semicolon = true; break; case TFUNCT: fpFUNCTf(shf, indent, tobool(t->u.ksh_func), t->str, t->left); break; case TTIME: fptreef(shf, indent, Tf_s_T, Ttime, t->left); break; default: shf_puts("<botch>", shf); prevent_semicolon = false; break; } if ((ioact = t->ioact) != NULL) { bool need_nl = false; while (*ioact != NULL) pioact(shf, *ioact++); /* Print here documents after everything else... */ ioact = t->ioact; while (*ioact != NULL) { struct ioword *iop = *ioact++; /* heredoc is NULL when tracing (set -x) */ if ((iop->ioflag & (IOTYPE | IOHERESTR)) == IOHERE && iop->heredoc) { shf_putc('\n', shf); shf_puts(iop->heredoc, shf); fptreef(shf, indent, Tf_s, evalstr(iop->delim, 0)); need_nl = true; } } /* * Last delimiter must be followed by a newline (this * often leads to an extra blank line, but it's not * worth worrying about) */ if (need_nl) { shf_putc('\n', shf); prevent_semicolon = true; } } }
/* variant of fputs for ptreef and wdstrip */ static const char * wdvarput(struct shf *shf, const char *wp, int quotelevel, int opmode) { int c; const char *cs; /*- * problems: * `...` -> $(...) * 'foo' -> "foo" * x${foo:-"hi"} -> x${foo:-hi} unless WDS_TPUTS * x${foo:-'hi'} -> x${foo:-hi} * could change encoding to: * OQUOTE ["'] ... CQUOTE ["'] * COMSUB [(`] ...\0 (handle $ ` \ and maybe " in `...` case) */ while (/* CONSTCOND */ 1) switch (*wp++) { case EOS: return (--wp); case ADELIM: if (ord(*wp) == ORD(/*{*/ '}')) { ++wp; goto wdvarput_csubst; } /* FALLTHROUGH */ case CHAR: c = ord(*wp++); shf_putc(c, shf); break; case QCHAR: c = ord(*wp++); if (opmode & WDS_TPUTS) switch (c) { case ORD('\n'): if (quotelevel == 0) { c = ORD('\''); shf_putc(c, shf); shf_putc(ORD('\n'), shf); } break; default: if (quotelevel == 0) /* FALLTHROUGH */ case ORD('"'): case ORD('`'): case ORD('$'): case ORD('\\'): shf_putc(ORD('\\'), shf); break; } shf_putc(c, shf); break; case COMASUB: case COMSUB: shf_puts("$(", shf); cs = ")"; if (ord(*wp) == ORD('(' /*)*/)) shf_putc(' ', shf); pSUB: while ((c = *wp++) != 0) shf_putc(c, shf); shf_puts(cs, shf); break; case FUNASUB: case FUNSUB: c = ORD(' '); if (0) /* FALLTHROUGH */ case VALSUB: c = ORD('|'); shf_putc('$', shf); shf_putc('{', shf); shf_putc(c, shf); cs = ";}"; goto pSUB; case EXPRSUB: shf_puts("$((", shf); cs = "))"; goto pSUB; case OQUOTE: if (opmode & WDS_TPUTS) { quotelevel++; shf_putc('"', shf); } break; case CQUOTE: if (opmode & WDS_TPUTS) { if (quotelevel) quotelevel--; shf_putc('"', shf); } break; case OSUBST: shf_putc('$', shf); if (ord(*wp++) == ORD('{')) shf_putc('{', shf); while ((c = *wp++) != 0) shf_putc(c, shf); wp = wdvarput(shf, wp, 0, opmode); break; case CSUBST: if (ord(*wp++) == ORD('}')) { wdvarput_csubst: shf_putc('}', shf); } return (wp); case OPAT: shf_putchar(*wp++, shf); shf_putc('(', shf); break; case SPAT: c = ORD('|'); if (0) /* FALLTHROUGH */ case CPAT: c = ORD(/*(*/ ')'); shf_putc(c, shf); break; } }
void x_puts(const char *s) { while (*s != 0) shf_putc(*s++, shl_out); }
void x_putc(int c) { shf_putc(c, shl_out); }
static int comexec(struct op *t, struct tbl * volatile tp, const char **ap, volatile int flags, volatile int *xerrok) { int i; volatile int rv = 0; const char *cp; const char **lastp; /* Must be static (XXX but why?) */ static struct op texec; int type_flags; bool keepasn_ok; int fcflags = FC_BI|FC_FUNC|FC_PATH; bool bourne_function_call = false; struct block *l_expand, *l_assign; /* * snag the last argument for $_ XXX not the same as AT&T ksh, * which only seems to set $_ after a newline (but not in * functions/dot scripts, but in interactive and script) - * perhaps save last arg here and set it in shell()?. */ if (Flag(FTALKING) && *(lastp = ap)) { while (*++lastp) ; /* setstr() can't fail here */ setstr(typeset("_", LOCAL, 0, INTEGER, 0), *--lastp, KSH_RETURN_ERROR); } /** * Deal with the shell builtins builtin, exec and command since * they can be followed by other commands. This must be done before * we know if we should create a local block which must be done * before we can do a path search (in case the assignments change * PATH). * Odd cases: * FOO=bar exec >/dev/null FOO is kept but not exported * FOO=bar exec foobar FOO is exported * FOO=bar command exec >/dev/null FOO is neither kept nor exported * FOO=bar command FOO is neither kept nor exported * PATH=... foobar use new PATH in foobar search */ keepasn_ok = true; while (tp && tp->type == CSHELL) { /* undo effects of command */ fcflags = FC_BI|FC_FUNC|FC_PATH; if (tp->val.f == c_builtin) { if ((cp = *++ap) == NULL || (!strcmp(cp, "--") && (cp = *++ap) == NULL)) { tp = NULL; break; } if ((tp = findcom(cp, FC_BI)) == NULL) errorf("%s: %s: %s", Tbuiltin, cp, "not a builtin"); continue; } else if (tp->val.f == c_exec) { if (ap[1] == NULL) break; ap++; flags |= XEXEC; } else if (tp->val.f == c_command) { int optc, saw_p = 0; /* * Ugly dealing with options in two places (here * and in c_command(), but such is life) */ ksh_getopt_reset(&builtin_opt, 0); while ((optc = ksh_getopt(ap, &builtin_opt, ":p")) == 'p') saw_p = 1; if (optc != EOF) /* command -vV or something */ break; /* don't look for functions */ fcflags = FC_BI|FC_PATH; if (saw_p) { if (Flag(FRESTRICTED)) { warningf(true, "%s: %s", "command -p", "restricted"); rv = 1; goto Leave; } fcflags |= FC_DEFPATH; } ap += builtin_opt.optind; /* * POSIX says special builtins lose their status * if accessed using command. */ keepasn_ok = false; if (!ap[0]) { /* ensure command with no args exits with 0 */ subst_exstat = 0; break; } #ifndef MKSH_NO_EXTERNAL_CAT } else if (tp->val.f == c_cat) { /* * if we have any flags, do not use the builtin * in theory, we could allow -u, but that would * mean to use ksh_getopt here and possibly ad- * ded complexity and more code and isn't worth * additional hassle (and the builtin must call * ksh_getopt already but can't come back here) */ if (ap[1] && ap[1][0] == '-' && ap[1][1] != '\0' && /* argument, begins with -, is not - or -- */ (ap[1][1] != '-' || ap[1][2] != '\0')) /* don't look for builtins or functions */ fcflags = FC_PATH; else /* go on, use the builtin */ break; #endif } else if (tp->val.f == c_trap) { t->u.evalflags &= ~DOTCOMEXEC; break; } else break; tp = findcom(ap[0], fcflags & (FC_BI|FC_FUNC)); } if (t->u.evalflags & DOTCOMEXEC) flags |= XEXEC; l_expand = e->loc; if (keepasn_ok && (!ap[0] || (tp && (tp->flag & KEEPASN)))) type_flags = 0; else { /* create new variable/function block */ newblock(); /* ksh functions don't keep assignments, POSIX functions do. */ if (keepasn_ok && tp && tp->type == CFUNC && !(tp->flag & FKSH)) { bourne_function_call = true; type_flags = EXPORT; } else type_flags = LOCAL|LOCAL_COPY|EXPORT; } l_assign = e->loc; if (Flag(FEXPORT)) type_flags |= EXPORT; if (Flag(FXTRACE)) change_xtrace(2, false); for (i = 0; t->vars[i]; i++) { /* do NOT lookup in the new var/fn block just created */ e->loc = l_expand; cp = evalstr(t->vars[i], DOASNTILDE | DOASNFIELD); e->loc = l_assign; if (Flag(FXTRACE)) { const char *ccp; ccp = skip_varname(cp, true); if (*ccp == '+') ++ccp; if (*ccp == '=') ++ccp; shf_write(cp, ccp - cp, shl_xtrace); print_value_quoted(shl_xtrace, ccp); shf_putc(' ', shl_xtrace); } /* but assign in there as usual */ typeset(cp, type_flags, 0, 0, 0); if (bourne_function_call && !(type_flags & EXPORT)) typeset(cp, LOCAL | LOCAL_COPY | EXPORT, 0, 0, 0); } if (Flag(FXTRACE)) { change_xtrace(2, false); if (ap[rv = 0]) { xtrace_ap_loop: print_value_quoted(shl_xtrace, ap[rv]); if (ap[++rv]) { shf_putc(' ', shl_xtrace); goto xtrace_ap_loop; } } change_xtrace(1, false); } if ((cp = *ap) == NULL) { rv = subst_exstat; goto Leave; } else if (!tp) { if (Flag(FRESTRICTED) && vstrchr(cp, '/')) { warningf(true, "%s: %s", cp, "restricted"); rv = 1; goto Leave; } tp = findcom(cp, fcflags); } switch (tp->type) { /* shell built-in */ case CSHELL: rv = call_builtin(tp, (const char **)ap, null); if (!keepasn_ok && tp->val.f == c_shift) { l_expand->argc = l_assign->argc; l_expand->argv = l_assign->argv; } break; /* function call */ case CFUNC: { volatile unsigned char old_xflag; volatile uint32_t old_inuse; const char * volatile old_kshname; if (!(tp->flag & ISSET)) { struct tbl *ftp; if (!tp->u.fpath) { rv = (tp->u2.errnov == ENOENT) ? 127 : 126; warningf(true, "%s: %s %s: %s", cp, "can't find", "function definition file", cstrerror(tp->u2.errnov)); break; } if (include(tp->u.fpath, 0, NULL, false) < 0) { warningf(true, "%s: %s %s %s: %s", cp, "can't open", "function definition file", tp->u.fpath, cstrerror(errno)); rv = 127; break; } if (!(ftp = findfunc(cp, hash(cp), false)) || !(ftp->flag & ISSET)) { warningf(true, "%s: %s %s", cp, "function not defined by", tp->u.fpath); rv = 127; break; } tp = ftp; } /* * ksh functions set $0 to function name, POSIX * functions leave $0 unchanged. */ old_kshname = kshname; if (tp->flag & FKSH) kshname = ap[0]; else ap[0] = kshname; e->loc->argv = ap; for (i = 0; *ap++ != NULL; i++) ; e->loc->argc = i - 1; /* * ksh-style functions handle getopts sanely, * Bourne/POSIX functions are insane... */ if (tp->flag & FKSH) { e->loc->flags |= BF_DOGETOPTS; e->loc->getopts_state = user_opt; getopts_reset(1); } old_xflag = Flag(FXTRACE) ? 1 : 0; change_xtrace((Flag(FXTRACEREC) ? old_xflag : 0) | ((tp->flag & TRACE) ? 1 : 0), false); old_inuse = tp->flag & FINUSE; tp->flag |= FINUSE; e->type = E_FUNC; if (!(i = kshsetjmp(e->jbuf))) { execute(tp->val.t, flags & XERROK, NULL); i = LRETURN; } kshname = old_kshname; change_xtrace(old_xflag, false); tp->flag = (tp->flag & ~FINUSE) | old_inuse; /* * Were we deleted while executing? If so, free the * execution tree. TODO: Unfortunately, the table entry * is never re-used until the lookup table is expanded. */ if ((tp->flag & (FDELETE|FINUSE)) == FDELETE) { if (tp->flag & ALLOC) { tp->flag &= ~ALLOC; tfree(tp->val.t, tp->areap); } tp->flag = 0; } switch (i) { case LRETURN: case LERROR: rv = exstat & 0xFF; break; case LINTR: case LEXIT: case LLEAVE: case LSHELL: quitenv(NULL); unwind(i); /* NOTREACHED */ default: quitenv(NULL); internal_errorf("%s %d", "CFUNC", i); } break; } /* executable command */ case CEXEC: /* tracked alias */ case CTALIAS: if (!(tp->flag&ISSET)) { if (tp->u2.errnov == ENOENT) { rv = 127; warningf(true, "%s: %s", cp, "not found"); } else { rv = 126; warningf(true, "%s: %s: %s", cp, "can't execute", cstrerror(tp->u2.errnov)); } break; } /* set $_ to programme's full path */ /* setstr() can't fail here */ setstr(typeset("_", LOCAL | EXPORT, 0, INTEGER, 0), tp->val.s, KSH_RETURN_ERROR); if (flags&XEXEC) { j_exit(); if (!(flags&XBGND) #ifndef MKSH_UNEMPLOYED || Flag(FMONITOR) #endif ) { setexecsig(&sigtraps[SIGINT], SS_RESTORE_ORIG); setexecsig(&sigtraps[SIGQUIT], SS_RESTORE_ORIG); } } /* to fork we set up a TEXEC node and call execute */ texec.type = TEXEC; /* for tprint */ texec.left = t; texec.str = tp->val.s; texec.args = ap; rv = exchild(&texec, flags, xerrok, -1); break; } Leave: if (flags & XEXEC) { exstat = rv & 0xFF; unwind(LLEAVE); } return (rv); }
int shf_vfprintf(struct shf *shf, const char *fmt, va_list args) { char c, *s; int tmp = 0; int field, precision; int len; int flags; unsigned long long llnum; /* %#o produces the longest output */ char numbuf[(BITS(long long) + 2) / 3 + 1]; /* this stuff for dealing with the buffer */ int nwritten = 0; if (!fmt) return 0; while ((c = *fmt++)) { if (c != '%') { shf_putc(c, shf); nwritten++; continue; } /* * This will accept flags/fields in any order - not * just the order specified in printf(3), but this is * the way _doprnt() seems to work (on bsd and sysV). * The only restriction is that the format character must * come last :-). */ flags = field = precision = 0; for ( ; (c = *fmt++) ; ) { switch (c) { case '#': flags |= FL_HASH; continue; case '+': flags |= FL_PLUS; continue; case '-': flags |= FL_RIGHT; continue; case ' ': flags |= FL_BLANK; continue; case '0': if (!(flags & FL_DOT)) flags |= FL_ZERO; continue; case '.': flags |= FL_DOT; precision = 0; continue; case '*': tmp = va_arg(args, int); if (flags & FL_DOT) precision = tmp; else if ((field = tmp) < 0) { field = -field; flags |= FL_RIGHT; } continue; case 'l': if (*fmt == 'l') { fmt++; flags |= FL_LLONG; } else flags |= FL_LONG; continue; case 'h': flags |= FL_SHORT; continue; } if (digit(c)) { tmp = c - '0'; while (c = *fmt++, digit(c)) tmp = tmp * 10 + c - '0'; --fmt; if (tmp < 0) /* overflow? */ tmp = 0; if (flags & FL_DOT) precision = tmp; else field = tmp; continue; } break; } if (precision < 0) precision = 0; if (!c) /* nasty format */ break; if (c >= 'A' && c <= 'Z') { flags |= FL_UPPER; c = c - 'A' + 'a'; } switch (c) { case 'p': /* pointer */ flags &= ~(FL_LLONG | FL_SHORT); flags |= FL_LONG; /* aaahhh... */ case 'd': case 'i': case 'o': case 'u': case 'x': flags |= FL_NUMBER; s = &numbuf[sizeof(numbuf)]; if (flags & FL_LLONG) llnum = va_arg(args, unsigned long long); else if (flags & FL_LONG) { if (c == 'd' || c == 'i') llnum = va_arg(args, long); else llnum = va_arg(args, unsigned long); } else { if (c == 'd' || c == 'i')
/* variant of fputs for ptreef and wdstrip */ static const char * wdvarput(struct shf *shf, const char *wp, int quotelevel, int opmode) { int c; const char *cs; /*- * problems: * `...` -> $(...) * 'foo' -> "foo" * x${foo:-"hi"} -> x${foo:-hi} unless WDS_TPUTS * x${foo:-'hi'} -> x${foo:-hi} unless WDS_KEEPQ * could change encoding to: * OQUOTE ["'] ... CQUOTE ["'] * COMSUB [(`] ...\0 (handle $ ` \ and maybe " in `...` case) */ while (/* CONSTCOND */ 1) switch (*wp++) { case EOS: return (--wp); case ADELIM: case CHAR: c = *wp++; if ((opmode & WDS_MAGIC) && (ISMAGIC(c) || c == '[' || c == '!' || c == '-' || c == ']' || c == '*' || c == '?')) shf_putc(MAGIC, shf); shf_putc(c, shf); break; case QCHAR: { bool doq; c = *wp++; doq = (c == '"' || c == '`' || c == '$' || c == '\\'); if (opmode & WDS_TPUTS) { if (quotelevel == 0) doq = true; } else { if (!(opmode & WDS_KEEPQ)) doq = false; } if (doq) shf_putc('\\', shf); shf_putc(c, shf); break; } case COMSUB: shf_puts("$(", shf); cs = ")"; pSUB: while ((c = *wp++) != 0) shf_putc(c, shf); shf_puts(cs, shf); break; case FUNSUB: c = ' '; if (0) /* FALLTHROUGH */ case VALSUB: c = '|'; shf_putc('$', shf); shf_putc('{', shf); shf_putc(c, shf); cs = ";}"; goto pSUB; case EXPRSUB: shf_puts("$((", shf); cs = "))"; goto pSUB; case OQUOTE: if (opmode & WDS_TPUTS) { quotelevel++; shf_putc('"', shf); } break; case CQUOTE: if (opmode & WDS_TPUTS) { if (quotelevel) quotelevel--; shf_putc('"', shf); } break; case OSUBST: shf_putc('$', shf); if (*wp++ == '{') shf_putc('{', shf); while ((c = *wp++) != 0) shf_putc(c, shf); wp = wdvarput(shf, wp, 0, opmode); break; case CSUBST: if (*wp++ == '}') shf_putc('}', shf); return (wp); case OPAT: if (opmode & WDS_MAGIC) { shf_putc(MAGIC, shf); shf_putchar(*wp++ | 0x80, shf); } else { shf_putchar(*wp++, shf); shf_putc('(', shf); } break; case SPAT: c = '|'; if (0) case CPAT: c = /*(*/ ')'; if (opmode & WDS_MAGIC) shf_putc(MAGIC, shf); shf_putc(c, shf); break; } }
static int dopprompt(const char *sp, int ntruncate, const char **spp, int doprint) { char strbuf[1024], tmpbuf[1024], *p, *str, nbuf[32], delimiter = '\0'; int len, c, n, totlen = 0, indelimit = 0, counting = 1, delimitthis; const char *cp = sp; struct tm *tm; time_t t; if (*cp && cp[1] == '\r') { delimiter = *cp; cp += 2; } while (*cp != 0) { delimitthis = 0; if (indelimit && *cp != delimiter) ; else if (*cp == '\n' || *cp == '\r') { totlen = 0; sp = cp + 1; } else if (*cp == '\t') { if (counting) totlen = (totlen | 7) + 1; } else if (*cp == delimiter) { indelimit = !indelimit; delimitthis = 1; } if (*cp == '\\') { cp++; if (!*cp) break; if (Flag(FSH)) snprintf(strbuf, sizeof strbuf, "\\%c", *cp); else switch (*cp) { case 'a': /* '\' 'a' bell */ strbuf[0] = '\007'; strbuf[1] = '\0'; break; case 'd': /* '\' 'd' Dow Mon DD */ time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, "%a %b %d", tm); break; case 'D': /* '\' 'D' '{' strftime format '}' */ p = strchr(cp + 2, '}'); if (cp[1] != '{' || p == NULL) { snprintf(strbuf, sizeof strbuf, "\\%c", *cp); break; } strlcpy(tmpbuf, cp + 2, sizeof tmpbuf); p = strchr(tmpbuf, '}'); if (p) *p = '\0'; time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, tmpbuf, tm); cp = strchr(cp + 2, '}'); break; case 'e': /* '\' 'e' escape */ strbuf[0] = '\033'; strbuf[1] = '\0'; break; case 'h': /* '\' 'h' shortened hostname */ gethostname(strbuf, sizeof strbuf); p = strchr(strbuf, '.'); if (p) *p = '\0'; break; case 'H': /* '\' 'H' full hostname */ gethostname(strbuf, sizeof strbuf); break; case 'j': /* '\' 'j' number of jobs */ snprintf(strbuf, sizeof strbuf, "%d", j_njobs()); break; case 'l': /* '\' 'l' basename of tty */ p = ttyname(0); if (p) p = basename(p); if (p) strlcpy(strbuf, p, sizeof strbuf); break; case 'n': /* '\' 'n' newline */ strbuf[0] = '\n'; strbuf[1] = '\0'; totlen = 0; /* reset for prompt re-print */ sp = cp + 1; break; case 'r': /* '\' 'r' return */ strbuf[0] = '\r'; strbuf[1] = '\0'; totlen = 0; /* reset for prompt re-print */ sp = cp + 1; break; case 's': /* '\' 's' basename $0 */ strlcpy(strbuf, kshname, sizeof strbuf); break; case 't': /* '\' 't' 24 hour HH:MM:SS */ time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, "%T", tm); break; case 'T': /* '\' 'T' 12 hour HH:MM:SS */ time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, "%l:%M:%S", tm); break; case '@': /* '\' '@' 12 hour am/pm format */ time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, "%r", tm); break; case 'A': /* '\' 'A' 24 hour HH:MM */ time(&t); tm = localtime(&t); strftime(strbuf, sizeof strbuf, "%R", tm); break; case 'u': /* '\' 'u' username */ p = getlogin(); if (p) strlcpy(strbuf, p, sizeof strbuf); else strbuf[0] = '\0'; break; case 'v': /* '\' 'v' version (short) */ p = strchr(ksh_version, ' '); if (p) p = strchr(p + 1, ' '); if (p) { p++; strlcpy(strbuf, p, sizeof strbuf); p = strchr(strbuf, ' '); if (p) *p = '\0'; } break; case 'V': /* '\' 'V' version (long) */ strlcpy(strbuf, ksh_version, sizeof strbuf); break; case 'w': /* '\' 'w' cwd */ p = str_val(global("PWD")); n = strlen(str_val(global("HOME"))); if (strcmp(p, "/") == 0) { strlcpy(strbuf, p, sizeof strbuf); } else if (strcmp(p, str_val(global("HOME"))) == 0) { strbuf[0] = '~'; strbuf[1] = '\0'; } else if (strncmp(p, str_val(global("HOME")), n) == 0 && p[n] == '/') { snprintf(strbuf, sizeof strbuf, "~/%s", str_val(global("PWD")) + n + 1); } else strlcpy(strbuf, p, sizeof strbuf); break; case 'W': /* '\' 'W' basename(cwd) */ p = str_val(global("PWD")); strlcpy(strbuf, basename(p), sizeof strbuf); break; case '!': /* '\' '!' history line number XXX busted */ snprintf(strbuf, sizeof strbuf, "%d", source->line + 1); break; case '#': /* '\' '#' command line number XXX busted */ snprintf(strbuf, sizeof strbuf, "%d", source->line + 1); break; case '$': /* '\' '$' $ or # XXX busted */ strbuf[0] = ksheuid ? '$' : '#'; strbuf[1] = '\0'; break; case '0': /* '\' '#' '#' ' #' octal numeric handling */ case '1': case '2': case '3': case '4': case '5': case '6': case '7': if ((cp[1] > '7' || cp[1] < '0') || (cp[2] > '7' || cp[2] < '0')) { snprintf(strbuf, sizeof strbuf, "\\%c", *cp); break; } n = cp[0] * 8 * 8 + cp[1] * 8 + cp[2]; snprintf(strbuf, sizeof strbuf, "%c", n); cp += 2; break; case '\\': /* '\' '\' */ strbuf[0] = '\\'; strbuf[1] = '\0'; break; case '[': /* '\' '[' .... stop counting */ strbuf[0] = '\0'; counting = 0; break; case ']': /* '\' ']' restart counting */ strbuf[0] = '\0'; counting = 1; break; default: snprintf(strbuf, sizeof strbuf, "\\%c", *cp); break; } cp++; str = strbuf; len = strlen(str); if (ntruncate) { if (ntruncate >= len) { ntruncate -= len; continue; } str += ntruncate; len -= ntruncate; ntruncate = 0; } if (doprint) shf_write(str, len, shl_out); if (counting && !indelimit && !delimitthis) totlen += len; continue; } else if (*cp != '!') c = *cp++; else if (*++cp == '!') c = *cp++; else { char *p; shf_snprintf(p = nbuf, sizeof(nbuf), "%d", source->line + 1); len = strlen(nbuf); if (ntruncate) { if (ntruncate >= len) { ntruncate -= len; continue; } p += ntruncate; len -= ntruncate; ntruncate = 0; } if (doprint) shf_write(p, len, shl_out); if (counting && !indelimit && !delimitthis) totlen += len; continue; } if (ntruncate) --ntruncate; else if (doprint) { shf_putc(c, shl_out); } if (counting && !indelimit && !delimitthis) totlen++; } if (doprint) shf_flush(shl_out); if (spp) *spp = sp; return (totlen); }
static void vfptreef(struct shf *shf, int indent, const char *fmt, va_list va) { int c; while ((c = *fmt++)) { if (c == '%') { switch ((c = *fmt++)) { case 'c': /* character (octet, probably) */ shf_putchar(va_arg(va, int), shf); break; case 's': /* string */ shf_puts(va_arg(va, char *), shf); break; case 'S': /* word */ wdvarput(shf, va_arg(va, char *), 0, WDS_TPUTS); break; case 'd': /* signed decimal */ shf_fprintf(shf, "%d", va_arg(va, int)); break; case 'u': /* unsigned decimal */ shf_fprintf(shf, "%u", va_arg(va, unsigned int)); break; case 'T': /* format tree */ ptree(va_arg(va, struct op *), indent, shf); goto dont_trash_prevent_semicolon; case ';': /* newline or ; */ case 'N': /* newline or space */ if (shf->flags & SHF_STRING) { if (c == ';' && !prevent_semicolon) shf_putc(';', shf); shf_putc(' ', shf); } else { int i; shf_putc('\n', shf); i = indent; while (i >= 8) { shf_putc('\t', shf); i -= 8; } while (i--) shf_putc(' ', shf); } break; case 'R': /* I/O redirection */ pioact(shf, va_arg(va, struct ioword *)); break; default: shf_putc(c, shf); break; } } else shf_putc(c, shf); prevent_semicolon = false; dont_trash_prevent_semicolon: ; }
int x_putc(int c) { return shf_putc(c, shl_out); }