2012-06-28 22:01:01 +02:00
|
|
|
/* $OpenBSD: tree.c,v 1.20 2012/06/27 07:17:19 otto Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2012-02-06 18:42:24 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
|
|
|
|
* 2011, 2012
|
2009-05-16 18:59:42 +02:00
|
|
|
* Thorsten Glaser <tg@mirbsd.org>
|
|
|
|
*
|
|
|
|
* Provided that these terms and disclaimer and all copyright notices
|
|
|
|
* are retained or reproduced in an accompanying document, permission
|
|
|
|
* is granted to deal in this work without restriction, including un-
|
|
|
|
* limited rights to use, publicly perform, distribute, sell, modify,
|
|
|
|
* merge, give away, or sublicence.
|
|
|
|
*
|
|
|
|
* This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
|
|
|
|
* the utmost extent permitted by applicable law, neither express nor
|
|
|
|
* implied; without malicious intent or gross negligence. In no event
|
|
|
|
* may a licensor, author or contributor be held liable for indirect,
|
|
|
|
* direct, other damage, loss, or other issues arising in any way out
|
|
|
|
* of dealing in the work, even if advised of the possibility of such
|
|
|
|
* damage or existence of a defect, except proven that it results out
|
|
|
|
* of said person's immediate fault when using the work as intended.
|
|
|
|
*/
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
#include "sh.h"
|
|
|
|
|
2012-12-04 02:10:35 +01:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.67 2012/12/04 01:10:35 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
#define INDENT 8
|
2005-05-23 05:06:10 +02:00
|
|
|
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void ptree(struct op *, int, struct shf *);
|
|
|
|
static void pioact(struct shf *, int, struct ioword *);
|
2011-05-03 00:52:54 +02:00
|
|
|
static const char *wdvarput(struct shf *, const char *, int, int);
|
• remove strcasestr.c, use home-grown implementation¹, call it stricmp,
and have it return an API-correct const char *
• enhance and stylify comments
• a little KNF and simplifications
• #ifdef DEBUG: replace strchr and strstr with ucstrchr and ucstrstr
that take and return a non-const char *, and fix the violations
• new cstrchr, cstrstr (take and give const char *)
• new vstrchr, vstrstr (take const or not, give boolean value)
• new afreechk(x) = afreechv(x,x) = if (x1) afree(x2, ATEMP)
• new ksh_isdash(str) = (str != NULL) && !strcmp(str, "-")
• replace the only use of strrchr with inlined code to shrink
• minor man page fixes
• Minix 3 signames are autogenerated with gcc
• rename strlfun.c to strlcpy.c since we don't do strlcat(3) anyway,
only strlcpy(3), and shorten it
• dot.mkshrc: move MKSH=… down to the export line
to not disturb the PS1 visual impression ☺
• dot.mkshrc: Lstripcom(): optimise
• bump version
¹) side effect from creating API-correct cstrchr, cstrstr, etc.
uses goto so it must be better ☻
tested on mirbsd-current via both Makefile and Build.sh
2007-03-04 04:04:28 +01:00
|
|
|
static void vfptreef(struct shf *, int, const char *, va_list);
|
2008-12-13 18:02:18 +01:00
|
|
|
static struct ioword **iocopy(struct ioword **, Area *);
|
|
|
|
static void iofree(struct ioword **, Area *);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
/* "foo& ; bar" and "foo |& ; bar" are invalid */
|
2011-04-22 14:15:42 +02:00
|
|
|
static bool prevent_semicolon;
|
2011-03-06 18:08:14 +01:00
|
|
|
|
2012-12-04 02:10:35 +01:00
|
|
|
static const char Telif_pT[] = "elif %T";
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
/*
|
|
|
|
* print a command tree
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ptree(struct op *t, int indent, struct shf *shf)
|
|
|
|
{
|
2007-03-04 01:13:17 +01:00
|
|
|
const char **w;
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword **ioact;
|
|
|
|
struct op *t1;
|
2011-03-06 18:08:14 +01:00
|
|
|
int i;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
Chain:
|
|
|
|
if (t == NULL)
|
|
|
|
return;
|
|
|
|
switch (t->type) {
|
|
|
|
case TCOM:
|
2012-07-30 21:58:05 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
if (t->vars) {
|
|
|
|
w = (const char **)t->vars;
|
|
|
|
while (*w)
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%S ", *w++);
|
2011-03-06 18:08:14 +01:00
|
|
|
} else
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("#no-vars# ", shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
if (t->args) {
|
|
|
|
w = t->args;
|
|
|
|
while (*w)
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%S ", *w++);
|
2011-03-06 18:08:14 +01:00
|
|
|
} else
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("#no-args# ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
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",
|
2011-03-06 18:08:14 +01:00
|
|
|
t->left, (t->type == TOR) ? "||" : "&&", t->right);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case TBANG:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("! ", shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
t = t->right;
|
|
|
|
goto Chain;
|
2011-03-06 18:08:14 +01:00
|
|
|
case TDBRACKET:
|
|
|
|
w = t->args;
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("[[", shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
while (*w)
|
|
|
|
fptreef(shf, indent, " %S", *w++);
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(" ]] ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case TSELECT:
|
|
|
|
case TFOR:
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shf, indent, "%s %s ",
|
2011-09-07 17:24:22 +02:00
|
|
|
(t->type == TFOR) ? "for" : Tselect, t->str);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (t->vars != NULL) {
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("in ", shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
w = (const char **)t->vars;
|
|
|
|
while (*w)
|
2005-05-23 05:06:10 +02:00
|
|
|
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(");
|
2011-03-06 18:08:14 +01:00
|
|
|
w = (const char **)t1->vars;
|
|
|
|
while (*w) {
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%S%c", *w,
|
|
|
|
(w[1] != NULL) ? '|' : ')');
|
2011-03-06 18:08:14 +01:00
|
|
|
++w;
|
|
|
|
}
|
2011-05-29 04:18:57 +02:00
|
|
|
fptreef(shf, indent + INDENT, "%N%T%N;%c", t1->left,
|
|
|
|
t1->u.charflag);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
fptreef(shf, indent, "%Nesac ");
|
|
|
|
break;
|
2012-04-07 13:19:30 +02:00
|
|
|
#ifdef DEBUG
|
2005-05-23 05:06:10 +02:00
|
|
|
case TELIF:
|
2011-03-06 18:08:14 +01:00
|
|
|
internal_errorf("TELIF in tree.c:ptree() unexpected");
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
#endif
|
|
|
|
case TIF:
|
|
|
|
i = 2;
|
2011-12-30 00:36:25 +01:00
|
|
|
t1 = t;
|
2011-03-06 18:08:14 +01:00
|
|
|
goto process_TIF;
|
|
|
|
do {
|
2011-12-30 00:36:25 +01:00
|
|
|
t1 = t1->right;
|
2011-03-06 18:08:14 +01:00
|
|
|
i = 0;
|
|
|
|
fptreef(shf, indent, "%;");
|
|
|
|
process_TIF:
|
|
|
|
/* 5 == strlen("elif ") */
|
2012-12-04 02:10:35 +01:00
|
|
|
fptreef(shf, indent + 5 - i, Telif_pT + i, t1->left);
|
2011-12-30 00:36:25 +01:00
|
|
|
t1 = t1->right;
|
|
|
|
if (t1->left != NULL) {
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%;");
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shf, indent + INDENT, "%s%N%T",
|
2011-12-30 00:36:25 +01:00
|
|
|
"then", t1->left);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2011-12-30 00:36:25 +01:00
|
|
|
} while (t1->right && t1->right->type == TELIF);
|
|
|
|
if (t1->right != NULL) {
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%;");
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shf, indent + INDENT, "%s%N%T",
|
2011-12-30 00:36:25 +01:00
|
|
|
"else", t1->right);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
fptreef(shf, indent, "%;fi ");
|
|
|
|
break;
|
|
|
|
case TWHILE:
|
|
|
|
case TUNTIL:
|
2012-12-04 02:10:35 +01:00
|
|
|
/* 6 == strlen("while "/"until ") */
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent + 6, "%s %T",
|
2011-03-06 18:08:14 +01:00
|
|
|
(t->type == TWHILE) ? "while" : "until",
|
2005-05-23 05:06:10 +02:00
|
|
|
t->left);
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shf, indent, "%;");
|
|
|
|
fptreef(shf, indent + INDENT, "do%N%T", t->right);
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%;done ");
|
|
|
|
break;
|
|
|
|
case TBRACE:
|
2011-03-06 03:14:09 +01:00
|
|
|
fptreef(shf, indent + INDENT, "{%N%T", t->left);
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%;} ");
|
|
|
|
break;
|
|
|
|
case TCOPROC:
|
|
|
|
fptreef(shf, indent, "%T|& ", t->left);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case TASYNC:
|
|
|
|
fptreef(shf, indent, "%T& ", t->left);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case TFUNCT:
|
2011-04-09 17:14:55 +02:00
|
|
|
fpFUNCTf(shf, indent, tobool(t->u.ksh_func), t->str, t->left);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case TTIME:
|
2010-08-28 22:22:24 +02:00
|
|
|
fptreef(shf, indent, "%s %T", "time", t->left);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
default:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("<botch>", shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((ioact = t->ioact) != NULL) {
|
2011-03-06 18:08:14 +01:00
|
|
|
bool need_nl = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
while (*ioact != NULL)
|
|
|
|
pioact(shf, indent, *ioact++);
|
|
|
|
/* Print here documents after everything else... */
|
2011-03-06 18:08:14 +01:00
|
|
|
ioact = t->ioact;
|
|
|
|
while (*ioact != NULL) {
|
2005-05-23 05:06:10 +02:00
|
|
|
struct ioword *iop = *ioact++;
|
|
|
|
|
2011-05-07 02:24:35 +02:00
|
|
|
/* heredoc is NULL when tracing (set -x) */
|
2011-05-05 02:05:01 +02:00
|
|
|
if ((iop->flag & (IOTYPE | IOHERESTR)) == IOHERE &&
|
|
|
|
iop->heredoc) {
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\n', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_puts(iop->heredoc, shf);
|
|
|
|
fptreef(shf, indent, "%s",
|
2011-05-05 02:05:01 +02:00
|
|
|
iop->flag & IONDELIM ? "<<" :
|
2005-05-23 05:06:10 +02:00
|
|
|
evalstr(iop->delim, 0));
|
2011-03-06 18:08:14 +01:00
|
|
|
need_nl = true;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-06 18:08:14 +01:00
|
|
|
/*
|
|
|
|
* Last delimiter must be followed by a newline (this
|
|
|
|
* often leads to an extra blank line, but it's not
|
|
|
|
* worth worrying about)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
2012-07-30 21:58:05 +02:00
|
|
|
if (need_nl) {
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\n', shf);
|
2012-07-30 21:58:05 +02:00
|
|
|
prevent_semicolon = true;
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pioact(struct shf *shf, int indent, 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)
|
2008-07-09 23:32:45 +02:00
|
|
|
shf_fprintf(shf, "%d", iop->unit);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case IOREAD:
|
2011-03-06 18:08:14 +01:00
|
|
|
shf_puts("<", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IOHERE:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(flag & IOSKIP ? "<<-" : "<<", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IOCAT:
|
2011-03-06 18:08:14 +01:00
|
|
|
shf_puts(">>", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IOWRITE:
|
2011-03-06 18:08:14 +01:00
|
|
|
shf_puts(flag & IOCLOB ? ">|" : ">", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IORDWR:
|
2011-03-06 18:08:14 +01:00
|
|
|
shf_puts("<>", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IODUP:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(flag & IORDUP ? "<&" : ">&", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-03-06 18:08:14 +01:00
|
|
|
/* name/delim are NULL when printing syntax errors */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (type == IOHERE) {
|
|
|
|
if (iop->delim)
|
2012-07-30 21:58:05 +02:00
|
|
|
wdvarput(shf, iop->delim, 0, WDS_TPUTS);
|
|
|
|
if (iop->flag & IOHERESTR)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(' ', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (iop->name)
|
|
|
|
fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ",
|
|
|
|
iop->name);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = false;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2011-05-03 00:52:54 +02:00
|
|
|
/* variant of fputs for ptreef and wdstrip */
|
|
|
|
static const char *
|
|
|
|
wdvarput(struct shf *shf, const char *wp, int quotelevel, int opmode)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-05-03 00:52:54 +02:00
|
|
|
int c;
|
2012-07-30 23:37:17 +02:00
|
|
|
const char *cs;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
/*-
|
|
|
|
* problems:
|
2005-05-23 05:06:10 +02:00
|
|
|
* `...` -> $(...)
|
|
|
|
* 'foo' -> "foo"
|
2011-05-03 00:52:54 +02:00
|
|
|
* x${foo:-"hi"} -> x${foo:-hi} unless WDS_TPUTS
|
|
|
|
* x${foo:-'hi'} -> x${foo:-hi} unless WDS_KEEPQ
|
2005-05-23 05:06:10 +02:00
|
|
|
* could change encoding to:
|
|
|
|
* OQUOTE ["'] ... CQUOTE ["']
|
|
|
|
* COMSUB [(`] ...\0 (handle $ ` \ and maybe " in `...` case)
|
|
|
|
*/
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1)
|
2008-10-14 01:06:04 +02:00
|
|
|
switch (*wp++) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case EOS:
|
2011-05-05 00:38:27 +02:00
|
|
|
return (--wp);
|
2007-07-06 03:53:36 +02:00
|
|
|
case ADELIM:
|
2005-05-23 05:06:10 +02:00
|
|
|
case CHAR:
|
2011-05-03 00:52:54 +02:00
|
|
|
c = *wp++;
|
|
|
|
if ((opmode & WDS_MAGIC) &&
|
2012-03-23 21:07:13 +01:00
|
|
|
(ISMAGIC(c) || c == '[' || c == '!' ||
|
2011-05-03 00:52:54 +02:00
|
|
|
c == '-' || c == ']' || c == '*' || c == '?'))
|
|
|
|
shf_putc(MAGIC, shf);
|
|
|
|
shf_putc(c, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-05-03 00:52:54 +02:00
|
|
|
case QCHAR: {
|
|
|
|
bool doq;
|
|
|
|
|
2005-05-23 05:06:10 +02:00
|
|
|
c = *wp++;
|
2011-05-03 00:52:54 +02:00
|
|
|
doq = (c == '"' || c == '`' || c == '$' || c == '\\');
|
|
|
|
if (opmode & WDS_TPUTS) {
|
|
|
|
if (quotelevel == 0)
|
|
|
|
doq = true;
|
|
|
|
} else {
|
|
|
|
if (!(opmode & WDS_KEEPQ))
|
|
|
|
doq = false;
|
|
|
|
}
|
|
|
|
if (doq)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\\', shf);
|
|
|
|
shf_putc(c, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-05-03 00:52:54 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
case COMSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$(", shf);
|
2012-07-30 23:37:17 +02:00
|
|
|
cs = ")";
|
|
|
|
pSUB:
|
2011-03-08 19:49:51 +01:00
|
|
|
while ((c = *wp++) != 0)
|
|
|
|
shf_putc(c, shf);
|
2012-07-30 23:37:17 +02:00
|
|
|
shf_puts(cs, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2012-10-22 22:19:18 +02:00
|
|
|
case FUNSUB:
|
|
|
|
shf_puts("${ ", shf);
|
|
|
|
cs = ";}";
|
|
|
|
goto pSUB;
|
2005-05-23 05:06:10 +02:00
|
|
|
case EXPRSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$((", shf);
|
2012-07-30 23:37:17 +02:00
|
|
|
cs = "))";
|
|
|
|
goto pSUB;
|
2005-05-23 05:06:10 +02:00
|
|
|
case OQUOTE:
|
2011-05-03 00:52:54 +02:00
|
|
|
if (opmode & WDS_TPUTS) {
|
|
|
|
quotelevel++;
|
|
|
|
shf_putc('"', shf);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case CQUOTE:
|
2011-05-03 00:52:54 +02:00
|
|
|
if (opmode & WDS_TPUTS) {
|
|
|
|
if (quotelevel)
|
|
|
|
quotelevel--;
|
|
|
|
shf_putc('"', shf);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case OSUBST:
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('$', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (*wp++ == '{')
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('{', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
while ((c = *wp++) != 0)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(c, shf);
|
2011-05-03 00:52:54 +02:00
|
|
|
wp = wdvarput(shf, wp, 0, opmode);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case CSUBST:
|
|
|
|
if (*wp++ == '}')
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('}', shf);
|
2011-05-03 00:52:54 +02:00
|
|
|
return (wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
case OPAT:
|
2011-05-03 00:52:54 +02:00
|
|
|
if (opmode & WDS_MAGIC) {
|
|
|
|
shf_putc(MAGIC, shf);
|
|
|
|
shf_putchar(*wp++ | 0x80, shf);
|
|
|
|
} else {
|
|
|
|
shf_putchar(*wp++, shf);
|
|
|
|
shf_putc('(', shf);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case SPAT:
|
2011-05-03 00:52:54 +02:00
|
|
|
c = '|';
|
|
|
|
if (0)
|
2005-05-23 05:06:10 +02:00
|
|
|
case CPAT:
|
2011-05-03 00:52:54 +02:00
|
|
|
c = /*(*/ ')';
|
|
|
|
if (opmode & WDS_MAGIC)
|
|
|
|
shf_putc(MAGIC, shf);
|
|
|
|
shf_putc(c, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is the _only_ way to reliably handle
|
|
|
|
* variable args with an ANSI compiler
|
|
|
|
*/
|
|
|
|
/* VARARGS */
|
2011-03-06 18:08:14 +01:00
|
|
|
void
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(struct shf *shf, int indent, const char *fmt, ...)
|
|
|
|
{
|
2007-10-25 17:19:16 +02:00
|
|
|
va_list va;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
va_start(va, fmt);
|
|
|
|
vfptreef(shf, indent, fmt, va);
|
|
|
|
va_end(va);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS */
|
|
|
|
char *
|
2011-08-27 20:06:52 +02:00
|
|
|
snptreef(char *s, ssize_t n, const char *fmt, ...)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2007-10-25 17:19:16 +02:00
|
|
|
va_list va;
|
|
|
|
struct shf shf;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
shf_sopen(s, n, SHF_WR | (s ? 0 : SHF_DYNAMIC), &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
va_start(va, fmt);
|
|
|
|
vfptreef(&shf, 0, fmt, va);
|
|
|
|
va_end(va);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
/* shf_sclose NUL terminates */
|
|
|
|
return (shf_sclose(&shf));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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':
|
2011-03-06 18:08:14 +01:00
|
|
|
/* character (octet, probably) */
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putchar(va_arg(va, int), shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case 's':
|
2011-03-06 18:08:14 +01:00
|
|
|
/* string */
|
2006-11-10 01:09:27 +01:00
|
|
|
shf_puts(va_arg(va, char *), shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-03-06 18:08:14 +01:00
|
|
|
case 'S':
|
|
|
|
/* word */
|
2011-05-03 00:52:54 +02:00
|
|
|
wdvarput(shf, va_arg(va, char *), 0, WDS_TPUTS);
|
2006-11-10 01:09:27 +01:00
|
|
|
break;
|
2011-03-06 18:08:14 +01:00
|
|
|
case 'd':
|
|
|
|
/* signed decimal */
|
2006-11-10 01:09:27 +01:00
|
|
|
shf_fprintf(shf, "%d", va_arg(va, int));
|
|
|
|
break;
|
2011-03-06 18:08:14 +01:00
|
|
|
case 'u':
|
|
|
|
/* unsigned decimal */
|
• more unsigned → unsigned int
• more int → bool
• more regression tests: check if the utf8-hack flag is really disabled
at non-interactive startup, enabled at interactive startup, if the
current locale is a UTF-8 one
• make the mksh-local multibyte handling functions globally accessible,
change their names, syntax and semantics a little (XXX more work needed)
• optimise
• utf_wctomb: src → dst, as we’re writing to that char array (pasto?)
• edit.c:x_e_getmbc(): if the second byte of a 2- or 3-byte multibyte
sequence is invalid utf-8, ungetc it (not possible for the 3rd byte yet)
• edit.c:x_zotc3(): easier (and faster) handling of UTF-8
• implement, document and test for base-1 numbers: they just get the
ASCII (8-bit) or Unicode (UTF-8) value of the octet(s) after the ‘1#’,
or do the same as print \x## or \u#### (depending on the utf8-hack flag),
plus support the PUA assignment of EF80‥EFFF for the MirBSD encoding “hack”
(print doesn’t, as it has \x## and \u#### to distinguish, but we cannot use
base-0 numbers which I had planned to use for raw octets first, as they are
used internally): http://thread.gmane.org/gmane.os.miros.general/7938
• as an application example, add a hexdumper to the regression tests ☺
2008-04-20 00:15:06 +02:00
|
|
|
shf_fprintf(shf, "%u", va_arg(va, unsigned int));
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2011-03-06 18:08:14 +01:00
|
|
|
case 'T':
|
|
|
|
/* format tree */
|
2005-05-23 05:06:10 +02:00
|
|
|
ptree(va_arg(va, struct op *), indent, shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
goto dont_trash_prevent_semicolon;
|
|
|
|
case ';':
|
|
|
|
/* newline or ; */
|
|
|
|
case 'N':
|
|
|
|
/* newline or space */
|
2005-05-23 05:06:10 +02:00
|
|
|
if (shf->flags & SHF_STRING) {
|
2011-03-06 18:08:14 +01:00
|
|
|
if (c == ';' && !prevent_semicolon)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(';', shf);
|
|
|
|
shf_putc(' ', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\n', shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
i = indent;
|
|
|
|
while (i >= 8) {
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
i -= 8;
|
|
|
|
}
|
|
|
|
while (i--)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(' ', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'R':
|
2011-03-06 18:08:14 +01:00
|
|
|
/* I/O redirection */
|
2005-05-23 05:06:10 +02:00
|
|
|
pioact(shf, indent, va_arg(va, struct ioword *));
|
|
|
|
break;
|
|
|
|
default:
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(c, shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc(c, shf);
|
2011-03-06 18:08:14 +01:00
|
|
|
prevent_semicolon = false;
|
|
|
|
dont_trash_prevent_semicolon:
|
|
|
|
;
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy tree (for function definition)
|
|
|
|
*/
|
|
|
|
struct op *
|
2008-12-13 18:02:18 +01:00
|
|
|
tcopy(struct op *t, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct op *r;
|
2007-03-04 01:13:17 +01:00
|
|
|
const char **tw;
|
|
|
|
char **rw;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (t == NULL)
|
2009-06-08 22:06:50 +02:00
|
|
|
return (NULL);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
r = alloc(sizeof(struct op), ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
r->type = t->type;
|
|
|
|
r->u.evalflags = t->u.evalflags;
|
|
|
|
|
2008-10-28 15:32:43 +01:00
|
|
|
if (t->type == TCASE)
|
|
|
|
r->str = wdcopy(t->str, ap);
|
|
|
|
else
|
|
|
|
strdupx(r->str, t->str, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (t->vars == NULL)
|
|
|
|
r->vars = NULL;
|
|
|
|
else {
|
2011-03-06 18:08:14 +01:00
|
|
|
tw = (const char **)t->vars;
|
|
|
|
while (*tw)
|
|
|
|
++tw;
|
2010-09-14 23:26:19 +02:00
|
|
|
rw = r->vars = alloc2(tw - (const char **)t->vars + 1,
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(*tw), ap);
|
2011-03-06 18:08:14 +01:00
|
|
|
tw = (const char **)t->vars;
|
|
|
|
while (*tw)
|
2005-05-23 05:06:10 +02:00
|
|
|
*rw++ = wdcopy(*tw++, ap);
|
|
|
|
*rw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t->args == NULL)
|
|
|
|
r->args = NULL;
|
|
|
|
else {
|
2011-03-06 18:08:14 +01:00
|
|
|
tw = t->args;
|
|
|
|
while (*tw)
|
|
|
|
++tw;
|
2010-09-14 23:26:19 +02:00
|
|
|
r->args = (const char **)(rw = alloc2(tw - t->args + 1,
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(*tw), ap));
|
2011-03-06 18:08:14 +01:00
|
|
|
tw = t->args;
|
|
|
|
while (*tw)
|
2005-05-23 05:06:10 +02:00
|
|
|
*rw++ = wdcopy(*tw++, ap);
|
|
|
|
*rw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->ioact = (t->ioact == NULL) ? NULL : iocopy(t->ioact, ap);
|
|
|
|
|
|
|
|
r->left = tcopy(t->left, ap);
|
|
|
|
r->right = tcopy(t->right, ap);
|
|
|
|
r->lineno = t->lineno;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (r);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2008-12-13 18:02:18 +01:00
|
|
|
wdcopy(const char *wp, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
2011-03-06 18:08:14 +01:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = wdscan(wp, EOS) - wp;
|
2009-06-08 22:06:50 +02:00
|
|
|
return (memcpy(alloc(len, ap), wp, len));
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* return the position of prefix c in wp plus 1 */
|
2006-11-12 15:58:16 +01:00
|
|
|
const char *
|
2005-05-23 05:06:10 +02:00
|
|
|
wdscan(const char *wp, int c)
|
|
|
|
{
|
|
|
|
int nest = 0;
|
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1)
|
2005-05-23 05:06:10 +02:00
|
|
|
switch (*wp++) {
|
|
|
|
case EOS:
|
2006-11-12 15:58:16 +01:00
|
|
|
return (wp);
|
2007-07-06 03:53:36 +02:00
|
|
|
case ADELIM:
|
|
|
|
if (c == ADELIM)
|
|
|
|
return (wp + 1);
|
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case CHAR:
|
|
|
|
case QCHAR:
|
|
|
|
wp++;
|
|
|
|
break;
|
2011-03-12 22:41:15 +01:00
|
|
|
case COMSUB:
|
2012-10-22 22:19:18 +02:00
|
|
|
case FUNSUB:
|
2011-03-13 00:04:48 +01:00
|
|
|
case EXPRSUB:
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*wp++ != 0)
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
case OQUOTE:
|
|
|
|
case CQUOTE:
|
|
|
|
break;
|
|
|
|
case OSUBST:
|
|
|
|
nest++;
|
|
|
|
while (*wp++ != '\0')
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
case CSUBST:
|
|
|
|
wp++;
|
|
|
|
if (c == CSUBST && nest == 0)
|
2006-11-12 15:58:16 +01:00
|
|
|
return (wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
nest--;
|
|
|
|
break;
|
|
|
|
case OPAT:
|
|
|
|
nest++;
|
|
|
|
wp++;
|
|
|
|
break;
|
|
|
|
case SPAT:
|
|
|
|
case CPAT:
|
|
|
|
if (c == wp[-1] && nest == 0)
|
2006-11-12 15:58:16 +01:00
|
|
|
return (wp);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (wp[-1] == CPAT)
|
|
|
|
nest--;
|
|
|
|
break;
|
|
|
|
default:
|
2007-05-13 19:51:24 +02:00
|
|
|
internal_warningf(
|
2005-05-23 05:06:10 +02:00
|
|
|
"wdscan: unknown char 0x%x (carrying on)",
|
|
|
|
wp[-1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
/*
|
|
|
|
* return a copy of wp without any of the mark up characters and with
|
|
|
|
* quote characters (" ' \) stripped. (string is allocated from ATEMP)
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
char *
|
2011-05-03 00:52:54 +02:00
|
|
|
wdstrip(const char *wp, int opmode)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct shf shf;
|
|
|
|
|
|
|
|
shf_sopen(NULL, 32, SHF_WR | SHF_DYNAMIC, &shf);
|
2011-05-03 00:52:54 +02:00
|
|
|
wdvarput(&shf, wp, 0, opmode);
|
2011-03-12 22:41:15 +01:00
|
|
|
/* shf_sclose NUL terminates */
|
|
|
|
return (shf_sclose(&shf));
|
|
|
|
}
|
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
static struct ioword **
|
2008-12-13 18:02:18 +01:00
|
|
|
iocopy(struct ioword **iow, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct ioword **ior;
|
|
|
|
int i;
|
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
ior = iow;
|
|
|
|
while (*ior)
|
|
|
|
++ior;
|
2010-09-14 23:26:19 +02:00
|
|
|
ior = alloc2(ior - iow + 1, sizeof(struct ioword *), ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
for (i = 0; iow[i] != NULL; i++) {
|
|
|
|
struct ioword *p, *q;
|
|
|
|
|
|
|
|
p = iow[i];
|
2009-06-08 22:06:50 +02:00
|
|
|
q = alloc(sizeof(struct ioword), ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
ior[i] = q;
|
|
|
|
*q = *p;
|
|
|
|
if (p->name != NULL)
|
|
|
|
q->name = wdcopy(p->name, ap);
|
|
|
|
if (p->delim != NULL)
|
|
|
|
q->delim = wdcopy(p->delim, ap);
|
|
|
|
if (p->heredoc != NULL)
|
2008-10-28 15:32:43 +01:00
|
|
|
strdupx(q->heredoc, p->heredoc, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
ior[i] = NULL;
|
|
|
|
|
2009-06-08 22:06:50 +02:00
|
|
|
return (ior);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* free tree (for function definition)
|
|
|
|
*/
|
|
|
|
void
|
2008-12-13 18:02:18 +01:00
|
|
|
tfree(struct op *t, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
char **w;
|
|
|
|
|
|
|
|
if (t == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (t->str != NULL)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(t->str, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
if (t->vars != NULL) {
|
|
|
|
for (w = t->vars; *w != NULL; w++)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(*w, ap);
|
|
|
|
afree(t->vars, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (t->args != NULL) {
|
2011-03-06 18:08:14 +01:00
|
|
|
/*XXX we assume the caller is right */
|
2007-03-04 01:13:17 +01:00
|
|
|
union mksh_ccphack cw;
|
2011-03-06 18:08:14 +01:00
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
cw.ro = t->args;
|
|
|
|
for (w = cw.rw; *w != NULL; w++)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(*w, ap);
|
|
|
|
afree(t->args, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (t->ioact != NULL)
|
|
|
|
iofree(t->ioact, ap);
|
|
|
|
|
|
|
|
tfree(t->left, ap);
|
|
|
|
tfree(t->right, ap);
|
|
|
|
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(t, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
2007-03-04 01:13:17 +01:00
|
|
|
static void
|
2008-12-13 18:02:18 +01:00
|
|
|
iofree(struct ioword **iow, Area *ap)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct ioword **iop;
|
|
|
|
struct ioword *p;
|
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
iop = iow;
|
|
|
|
while ((p = *iop++) != NULL) {
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p->name != NULL)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(p->name, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p->delim != NULL)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(p->delim, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (p->heredoc != NULL)
|
2008-11-12 01:54:52 +01:00
|
|
|
afree(p->heredoc, ap);
|
|
|
|
afree(p, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2008-09-14 22:29:13 +02:00
|
|
|
afree(iow, ap);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
2010-08-28 22:22:24 +02:00
|
|
|
|
2011-03-06 18:08:14 +01:00
|
|
|
void
|
2010-08-28 22:22:24 +02:00
|
|
|
fpFUNCTf(struct shf *shf, int i, bool isksh, const char *k, struct op *v)
|
|
|
|
{
|
|
|
|
if (isksh)
|
2011-09-07 17:24:22 +02:00
|
|
|
fptreef(shf, i, "%s %s %T", Tfunction, k, v);
|
2010-08-28 22:22:24 +02:00
|
|
|
else
|
2011-03-06 18:08:14 +01:00
|
|
|
fptreef(shf, i, "%s() %T", k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* for jobs.c */
|
|
|
|
void
|
|
|
|
vistree(char *dst, size_t sz, struct op *t)
|
|
|
|
{
|
2012-02-06 18:42:24 +01:00
|
|
|
unsigned int c;
|
2011-03-06 18:08:14 +01:00
|
|
|
char *cp, *buf;
|
2012-02-06 18:42:24 +01:00
|
|
|
size_t n;
|
2011-03-06 18:08:14 +01:00
|
|
|
|
2012-02-06 18:42:24 +01:00
|
|
|
buf = alloc(sz + 8, ATEMP);
|
|
|
|
snptreef(buf, sz + 8, "%T", t);
|
2011-03-06 18:08:14 +01:00
|
|
|
cp = buf;
|
2012-02-06 18:42:24 +01:00
|
|
|
vist_loop:
|
|
|
|
if (UTFMODE && (n = utf_mbtowc(&c, cp)) != (size_t)-1) {
|
|
|
|
if (c == 0 || n >= sz)
|
|
|
|
/* NUL or not enough free space */
|
|
|
|
goto vist_out;
|
|
|
|
/* copy multibyte char */
|
|
|
|
sz -= n;
|
|
|
|
while (n--)
|
|
|
|
*dst++ = *cp++;
|
|
|
|
goto vist_loop;
|
2011-03-06 18:08:14 +01:00
|
|
|
}
|
2012-02-06 18:42:24 +01:00
|
|
|
if (--sz == 0 || (c = (unsigned char)(*cp++)) == 0)
|
|
|
|
/* NUL or not enough free space */
|
|
|
|
goto vist_out;
|
|
|
|
if ((c & 0x60) == 0 || (c & 0x7F) == 0x7F) {
|
|
|
|
/* C0 or C1 control character or DEL */
|
|
|
|
if (--sz == 0)
|
|
|
|
/* not enough free space for two chars */
|
|
|
|
goto vist_out;
|
|
|
|
*dst++ = (c & 0x80) ? '$' : '^';
|
|
|
|
c = (c & 0x7F) ^ 0x40;
|
|
|
|
} else if (UTFMODE && c > 0x7F) {
|
|
|
|
/* better not try to display broken multibyte chars */
|
|
|
|
c = '?';
|
|
|
|
}
|
|
|
|
*dst++ = c;
|
|
|
|
goto vist_loop;
|
|
|
|
|
|
|
|
vist_out:
|
2011-03-06 18:08:14 +01:00
|
|
|
*dst = '\0';
|
|
|
|
afree(buf, ATEMP);
|
2010-08-28 22:22:24 +02:00
|
|
|
}
|
2011-03-08 19:49:51 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2011-03-17 22:58:40 +01:00
|
|
|
void
|
2011-03-08 19:49:51 +01:00
|
|
|
dumpchar(struct shf *shf, int c)
|
|
|
|
{
|
|
|
|
if (((c & 0x60) == 0) || ((c & 0x7F) == 0x7F)) {
|
|
|
|
/* C0 or C1 control character or DEL */
|
|
|
|
shf_putc((c & 0x80) ? '$' : '^', shf);
|
|
|
|
c = (c & 0x7F) ^ 0x40;
|
|
|
|
}
|
|
|
|
shf_putc(c, shf);
|
|
|
|
}
|
|
|
|
|
2011-05-03 00:52:54 +02:00
|
|
|
/* see: wdvarput */
|
|
|
|
static const char *
|
2011-10-26 00:36:39 +02:00
|
|
|
dumpwdvar_i(struct shf *shf, const char *wp, int quotelevel)
|
2011-03-08 19:49:51 +01:00
|
|
|
{
|
2011-05-03 00:52:54 +02:00
|
|
|
int c;
|
2011-03-08 19:49:51 +01:00
|
|
|
|
2011-03-13 02:20:25 +01:00
|
|
|
while (/* CONSTCOND */ 1) {
|
2011-03-08 19:49:51 +01:00
|
|
|
switch(*wp++) {
|
|
|
|
case EOS:
|
|
|
|
shf_puts("EOS", shf);
|
2011-05-05 00:38:27 +02:00
|
|
|
return (--wp);
|
2011-03-08 19:49:51 +01:00
|
|
|
case ADELIM:
|
|
|
|
shf_puts("ADELIM=", shf);
|
2011-05-03 00:52:54 +02:00
|
|
|
if (0)
|
|
|
|
case CHAR:
|
|
|
|
shf_puts("CHAR=", shf);
|
2011-03-08 19:49:51 +01:00
|
|
|
dumpchar(shf, *wp++);
|
|
|
|
break;
|
|
|
|
case QCHAR:
|
|
|
|
shf_puts("QCHAR<", shf);
|
|
|
|
c = *wp++;
|
2011-05-03 00:52:54 +02:00
|
|
|
if (quotelevel == 0 ||
|
2011-03-08 19:49:51 +01:00
|
|
|
(c == '"' || c == '`' || c == '$' || c == '\\'))
|
|
|
|
shf_putc('\\', shf);
|
|
|
|
dumpchar(shf, c);
|
|
|
|
goto closeandout;
|
|
|
|
case COMSUB:
|
|
|
|
shf_puts("COMSUB<", shf);
|
2011-03-13 00:04:48 +01:00
|
|
|
dumpsub:
|
2011-03-08 19:49:51 +01:00
|
|
|
while ((c = *wp++) != 0)
|
|
|
|
dumpchar(shf, c);
|
|
|
|
closeandout:
|
|
|
|
shf_putc('>', shf);
|
|
|
|
break;
|
2012-10-22 22:19:18 +02:00
|
|
|
case FUNSUB:
|
|
|
|
shf_puts("FUNSUB<", shf);
|
|
|
|
goto dumpsub;
|
2011-03-08 19:49:51 +01:00
|
|
|
case EXPRSUB:
|
2011-03-13 00:04:48 +01:00
|
|
|
shf_puts("EXPRSUB<", shf);
|
|
|
|
goto dumpsub;
|
2011-03-08 19:49:51 +01:00
|
|
|
case OQUOTE:
|
|
|
|
shf_fprintf(shf, "OQUOTE{%d", ++quotelevel);
|
|
|
|
break;
|
|
|
|
case CQUOTE:
|
|
|
|
shf_fprintf(shf, "%d}CQUOTE", quotelevel);
|
|
|
|
if (quotelevel)
|
|
|
|
quotelevel--;
|
|
|
|
else
|
|
|
|
shf_puts("(err)", shf);
|
|
|
|
break;
|
|
|
|
case OSUBST:
|
|
|
|
shf_puts("OSUBST(", shf);
|
|
|
|
dumpchar(shf, *wp++);
|
|
|
|
shf_puts(")[", shf);
|
|
|
|
while ((c = *wp++) != 0)
|
|
|
|
dumpchar(shf, c);
|
2011-05-03 00:52:54 +02:00
|
|
|
shf_putc('|', shf);
|
2011-10-26 00:36:39 +02:00
|
|
|
wp = dumpwdvar_i(shf, wp, 0);
|
2011-03-08 19:49:51 +01:00
|
|
|
break;
|
|
|
|
case CSUBST:
|
|
|
|
shf_puts("]CSUBST(", shf);
|
|
|
|
dumpchar(shf, *wp++);
|
|
|
|
shf_putc(')', shf);
|
2011-05-03 00:52:54 +02:00
|
|
|
return (wp);
|
2011-03-08 19:49:51 +01:00
|
|
|
case OPAT:
|
|
|
|
shf_puts("OPAT=", shf);
|
|
|
|
dumpchar(shf, *wp++);
|
|
|
|
break;
|
|
|
|
case SPAT:
|
|
|
|
shf_puts("SPAT", shf);
|
|
|
|
break;
|
|
|
|
case CPAT:
|
|
|
|
shf_puts("CPAT", shf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
shf_fprintf(shf, "INVAL<%u>", (uint8_t)wp[-1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
shf_putc(' ', shf);
|
|
|
|
}
|
|
|
|
}
|
2011-05-03 00:52:54 +02:00
|
|
|
void
|
|
|
|
dumpwdvar(struct shf *shf, const char *wp)
|
|
|
|
{
|
2011-10-26 00:36:39 +02:00
|
|
|
dumpwdvar_i(shf, wp, 0);
|
2011-05-03 00:52:54 +02:00
|
|
|
}
|
2011-03-08 19:49:51 +01:00
|
|
|
|
2011-12-29 23:54:22 +01:00
|
|
|
void
|
|
|
|
dumpioact(struct shf *shf, struct op *t)
|
|
|
|
{
|
|
|
|
struct ioword **ioact, *iop;
|
|
|
|
|
|
|
|
if ((ioact = t->ioact) == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
shf_puts("{IOACT", shf);
|
|
|
|
while ((iop = *ioact++) != NULL) {
|
|
|
|
int type = iop->flag & IOTYPE;
|
|
|
|
#define DT(x) case x: shf_puts(#x, shf); break;
|
|
|
|
#define DB(x) if (iop->flag & x) shf_puts("|" #x, shf);
|
|
|
|
|
|
|
|
shf_putc(';', shf);
|
|
|
|
switch (type) {
|
|
|
|
DT(IOREAD)
|
|
|
|
DT(IOWRITE)
|
|
|
|
DT(IORDWR)
|
|
|
|
DT(IOHERE)
|
|
|
|
DT(IOCAT)
|
|
|
|
DT(IODUP)
|
|
|
|
default:
|
|
|
|
shf_fprintf(shf, "unk%d", type);
|
|
|
|
}
|
|
|
|
DB(IOEVAL)
|
|
|
|
DB(IOSKIP)
|
|
|
|
DB(IOCLOB)
|
|
|
|
DB(IORDUP)
|
|
|
|
DB(IONAMEXP)
|
|
|
|
DB(IOBASH)
|
|
|
|
DB(IOHERESTR)
|
|
|
|
DB(IONDELIM)
|
|
|
|
shf_fprintf(shf, ",unit=%d", iop->unit);
|
|
|
|
if (iop->delim) {
|
|
|
|
shf_puts(",delim<", shf);
|
|
|
|
dumpwdvar(shf, iop->delim);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
if (iop->name) {
|
|
|
|
if (iop->flag & IONAMEXP) {
|
|
|
|
shf_puts(",name=", shf);
|
|
|
|
print_value_quoted(shf, iop->name);
|
|
|
|
} else {
|
|
|
|
shf_puts(",name<", shf);
|
|
|
|
dumpwdvar(shf, iop->name);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iop->heredoc) {
|
|
|
|
shf_puts(",heredoc=", shf);
|
|
|
|
print_value_quoted(shf, iop->heredoc);
|
|
|
|
}
|
|
|
|
#undef DT
|
|
|
|
#undef DB
|
|
|
|
}
|
|
|
|
shf_putc('}', shf);
|
|
|
|
}
|
|
|
|
|
2011-03-08 19:49:51 +01:00
|
|
|
void
|
|
|
|
dumptree(struct shf *shf, struct op *t)
|
|
|
|
{
|
2012-05-04 20:45:03 +02:00
|
|
|
int i, j;
|
2011-03-08 19:49:51 +01:00
|
|
|
const char **w, *name;
|
|
|
|
struct op *t1;
|
2011-12-31 01:27:27 +01:00
|
|
|
static int nesting;
|
2011-03-08 19:49:51 +01:00
|
|
|
|
|
|
|
for (i = 0; i < nesting; ++i)
|
|
|
|
shf_putc('\t', shf);
|
|
|
|
++nesting;
|
|
|
|
shf_puts("{tree:" /*}*/, shf);
|
|
|
|
if (t == NULL) {
|
|
|
|
name = "(null)";
|
|
|
|
goto out;
|
|
|
|
}
|
2011-12-29 23:54:22 +01:00
|
|
|
dumpioact(shf, t);
|
2011-03-08 19:49:51 +01:00
|
|
|
switch (t->type) {
|
|
|
|
#define OPEN(x) case x: name = #x; shf_puts(" {" #x ":", shf); /*}*/
|
|
|
|
|
|
|
|
OPEN(TCOM)
|
|
|
|
if (t->vars) {
|
|
|
|
i = 0;
|
|
|
|
w = (const char **)t->vars;
|
|
|
|
while (*w) {
|
|
|
|
shf_putc('\n', shf);
|
2012-05-04 20:45:03 +02:00
|
|
|
for (j = 0; j < nesting; ++j)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
|
|
|
shf_fprintf(shf, " var%d<", i++);
|
|
|
|
dumpwdvar(shf, *w++);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
shf_puts(" #no-vars#", shf);
|
|
|
|
if (t->args) {
|
|
|
|
i = 0;
|
|
|
|
w = t->args;
|
|
|
|
while (*w) {
|
|
|
|
shf_putc('\n', shf);
|
2012-05-04 20:45:03 +02:00
|
|
|
for (j = 0; j < nesting; ++j)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
|
|
|
shf_fprintf(shf, " arg%d<", i++);
|
|
|
|
dumpwdvar(shf, *w++);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
shf_puts(" #no-args#", shf);
|
|
|
|
break;
|
|
|
|
OPEN(TEXEC)
|
|
|
|
dumpleftandout:
|
|
|
|
t = t->left;
|
|
|
|
dumpandout:
|
|
|
|
shf_putc('\n', shf);
|
|
|
|
dumptree(shf, t);
|
|
|
|
break;
|
|
|
|
OPEN(TPAREN)
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TPIPE)
|
|
|
|
dumpleftmidrightandout:
|
|
|
|
shf_putc('\n', shf);
|
|
|
|
dumptree(shf, t->left);
|
2011-04-09 17:05:44 +02:00
|
|
|
/* middumprightandout: (unused) */
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_fprintf(shf, "/%s:", name);
|
|
|
|
dumprightandout:
|
|
|
|
t = t->right;
|
|
|
|
goto dumpandout;
|
|
|
|
OPEN(TLIST)
|
|
|
|
goto dumpleftmidrightandout;
|
|
|
|
OPEN(TOR)
|
|
|
|
goto dumpleftmidrightandout;
|
|
|
|
OPEN(TAND)
|
|
|
|
goto dumpleftmidrightandout;
|
|
|
|
OPEN(TBANG)
|
|
|
|
goto dumprightandout;
|
|
|
|
OPEN(TDBRACKET)
|
|
|
|
i = 0;
|
|
|
|
w = t->args;
|
|
|
|
while (*w) {
|
|
|
|
shf_putc('\n', shf);
|
2012-05-04 20:45:03 +02:00
|
|
|
for (j = 0; j < nesting; ++j)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
|
|
|
shf_fprintf(shf, " arg%d<", i++);
|
|
|
|
dumpwdvar(shf, *w++);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
OPEN(TFOR)
|
|
|
|
dumpfor:
|
|
|
|
shf_fprintf(shf, " str<%s>", t->str);
|
|
|
|
if (t->vars != NULL) {
|
|
|
|
i = 0;
|
|
|
|
w = (const char **)t->vars;
|
|
|
|
while (*w) {
|
|
|
|
shf_putc('\n', shf);
|
2012-05-04 20:45:03 +02:00
|
|
|
for (j = 0; j < nesting; ++j)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
|
|
|
shf_fprintf(shf, " var%d<", i++);
|
|
|
|
dumpwdvar(shf, *w++);
|
|
|
|
shf_putc('>', shf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TSELECT)
|
|
|
|
goto dumpfor;
|
|
|
|
OPEN(TCASE)
|
|
|
|
shf_fprintf(shf, " str<%s>", t->str);
|
|
|
|
i = 0;
|
|
|
|
for (t1 = t->left; t1 != NULL; t1 = t1->right) {
|
|
|
|
shf_putc('\n', shf);
|
2012-05-04 20:45:03 +02:00
|
|
|
for (j = 0; j < nesting; ++j)
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\t', shf);
|
|
|
|
shf_fprintf(shf, " sub%d[(", i);
|
|
|
|
w = (const char **)t1->vars;
|
|
|
|
while (*w) {
|
|
|
|
dumpwdvar(shf, *w);
|
|
|
|
if (w[1] != NULL)
|
|
|
|
shf_putc('|', shf);
|
|
|
|
++w;
|
|
|
|
}
|
|
|
|
shf_putc(')', shf);
|
2011-12-29 23:54:22 +01:00
|
|
|
dumpioact(shf, t);
|
2011-03-08 19:49:51 +01:00
|
|
|
shf_putc('\n', shf);
|
|
|
|
dumptree(shf, t1->left);
|
2011-05-29 04:18:57 +02:00
|
|
|
shf_fprintf(shf, " ;%c/%d]", t1->u.charflag, i++);
|
2011-03-08 19:49:51 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
OPEN(TWHILE)
|
|
|
|
goto dumpleftmidrightandout;
|
|
|
|
OPEN(TUNTIL)
|
|
|
|
goto dumpleftmidrightandout;
|
|
|
|
OPEN(TBRACE)
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TCOPROC)
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TASYNC)
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TFUNCT)
|
|
|
|
shf_fprintf(shf, " str<%s> ksh<%s>", t->str,
|
|
|
|
t->u.ksh_func ? "yes" : "no");
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TTIME)
|
|
|
|
goto dumpleftandout;
|
|
|
|
OPEN(TIF)
|
|
|
|
dumpif:
|
|
|
|
shf_putc('\n', shf);
|
|
|
|
dumptree(shf, t->left);
|
|
|
|
t = t->right;
|
2011-12-29 23:54:22 +01:00
|
|
|
dumpioact(shf, t);
|
2011-03-08 19:49:51 +01:00
|
|
|
if (t->left != NULL) {
|
|
|
|
shf_puts(" /TTHEN:\n", shf);
|
|
|
|
dumptree(shf, t->left);
|
|
|
|
}
|
|
|
|
if (t->right && t->right->type == TELIF) {
|
|
|
|
shf_puts(" /TELIF:", shf);
|
|
|
|
t = t->right;
|
2011-12-29 23:54:22 +01:00
|
|
|
dumpioact(shf, t);
|
2011-03-08 19:49:51 +01:00
|
|
|
goto dumpif;
|
|
|
|
}
|
|
|
|
if (t->right != NULL) {
|
|
|
|
shf_puts(" /TELSE:\n", shf);
|
|
|
|
dumptree(shf, t->right);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
OPEN(TEOF)
|
|
|
|
dumpunexpected:
|
|
|
|
shf_puts("unexpected", shf);
|
|
|
|
break;
|
|
|
|
OPEN(TELIF)
|
|
|
|
goto dumpunexpected;
|
|
|
|
OPEN(TPAT)
|
|
|
|
goto dumpunexpected;
|
|
|
|
default:
|
|
|
|
name = "TINVALID";
|
|
|
|
shf_fprintf(shf, "{T<%d>:" /*}*/, t->type);
|
|
|
|
goto dumpunexpected;
|
|
|
|
|
|
|
|
#undef OPEN
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
shf_fprintf(shf, /*{*/ " /%s}\n", name);
|
|
|
|
--nesting;
|
|
|
|
}
|
|
|
|
#endif
|