2008-09-14 22:29:13 +02:00
|
|
|
/* $OpenBSD: tree.c,v 1.19 2008/08/11 21:50:35 jaredy Exp $ */
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2009-05-16 18:59:42 +02:00
|
|
|
/*-
|
2010-01-29 10:34:31 +01:00
|
|
|
* Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
|
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"
|
|
|
|
|
2010-08-28 22:22:24 +02:00
|
|
|
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.31 2010/08/28 20:22:24 tg Exp $");
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
#define INDENT 4
|
|
|
|
|
2009-04-07 21:13:11 +02:00
|
|
|
#define tputc(c, shf) shf_putchar(c, shf);
|
• 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 *);
|
|
|
|
static void tputC(int, struct shf *);
|
|
|
|
static void tputS(char *, struct shf *);
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
Chain:
|
|
|
|
if (t == NULL)
|
|
|
|
return;
|
|
|
|
switch (t->type) {
|
|
|
|
case TCOM:
|
|
|
|
if (t->vars)
|
2007-03-04 01:13:17 +01:00
|
|
|
for (w = (const char **)t->vars; *w != NULL; )
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%S ", *w++);
|
|
|
|
else
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("#no-vars# ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
if (t->args)
|
|
|
|
for (w = t->args; *w != NULL; )
|
|
|
|
fptreef(shf, indent, "%S ", *w++);
|
|
|
|
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",
|
|
|
|
t->left, (t->type==TOR) ? "||" : "&&", t->right);
|
|
|
|
break;
|
|
|
|
case TBANG:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("! ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
t = t->right;
|
|
|
|
goto Chain;
|
2009-06-10 20:12:51 +02:00
|
|
|
case TDBRACKET: {
|
2005-05-23 05:06:10 +02:00
|
|
|
int i;
|
|
|
|
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("[[", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
for (i = 0; t->args[i]; i++)
|
|
|
|
fptreef(shf, indent, " %S", t->args[i]);
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(" ]] ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
2009-06-10 20:12:51 +02:00
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
case TSELECT:
|
|
|
|
fptreef(shf, indent, "select %s ", t->str);
|
2010-01-29 10:34:31 +01:00
|
|
|
/* FALLTHROUGH */
|
2005-05-23 05:06:10 +02:00
|
|
|
case TFOR:
|
|
|
|
if (t->type == TFOR)
|
|
|
|
fptreef(shf, indent, "for %s ", t->str);
|
|
|
|
if (t->vars != NULL) {
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("in ", shf);
|
2007-03-04 01:13:17 +01:00
|
|
|
for (w = (const char **)t->vars; *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(");
|
2007-03-04 01:13:17 +01:00
|
|
|
for (w = (const char **)t1->vars; *w != NULL; w++)
|
2005-05-23 05:06:10 +02:00
|
|
|
fptreef(shf, indent, "%S%c", *w,
|
|
|
|
(w[1] != NULL) ? '|' : ')');
|
|
|
|
fptreef(shf, indent + INDENT, "%;%T%N;;", t1->left);
|
|
|
|
}
|
|
|
|
fptreef(shf, indent, "%Nesac ");
|
|
|
|
break;
|
|
|
|
case TIF:
|
|
|
|
case TELIF:
|
|
|
|
/* 3 == strlen("if ") */
|
|
|
|
fptreef(shf, indent + 3, "if %T", t->left);
|
|
|
|
for (;;) {
|
|
|
|
t = t->right;
|
|
|
|
if (t->left != NULL) {
|
|
|
|
fptreef(shf, indent, "%;");
|
|
|
|
fptreef(shf, indent + INDENT, "then%N%T",
|
|
|
|
t->left);
|
|
|
|
}
|
|
|
|
if (t->right == NULL || t->right->type != TELIF)
|
|
|
|
break;
|
|
|
|
t = t->right;
|
|
|
|
fptreef(shf, indent, "%;");
|
|
|
|
/* 5 == strlen("elif ") */
|
|
|
|
fptreef(shf, indent + 5, "elif %T", t->left);
|
|
|
|
}
|
|
|
|
if (t->right != NULL) {
|
|
|
|
fptreef(shf, indent, "%;");
|
|
|
|
fptreef(shf, indent + INDENT, "else%;%T", t->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, "%;do");
|
|
|
|
fptreef(shf, indent + INDENT, "%;%T", t->right);
|
|
|
|
fptreef(shf, indent, "%;done ");
|
|
|
|
break;
|
|
|
|
case TBRACE:
|
|
|
|
fptreef(shf, indent + INDENT, "{%;%T", t->left);
|
|
|
|
fptreef(shf, indent, "%;} ");
|
|
|
|
break;
|
|
|
|
case TCOPROC:
|
|
|
|
fptreef(shf, indent, "%T|& ", t->left);
|
|
|
|
break;
|
|
|
|
case TASYNC:
|
|
|
|
fptreef(shf, indent, "%T& ", t->left);
|
|
|
|
break;
|
|
|
|
case TFUNCT:
|
2010-08-28 22:22:24 +02:00
|
|
|
fpFUNCTf(shf, indent, 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);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((ioact = t->ioact) != NULL) {
|
|
|
|
int need_nl = 0;
|
|
|
|
|
|
|
|
while (*ioact != NULL)
|
|
|
|
pioact(shf, indent, *ioact++);
|
|
|
|
/* Print here documents after everything else... */
|
|
|
|
for (ioact = t->ioact; *ioact != NULL; ) {
|
|
|
|
struct ioword *iop = *ioact++;
|
|
|
|
|
|
|
|
/* heredoc is 0 when tracing (set -x) */
|
2008-02-26 21:43:11 +01:00
|
|
|
if ((iop->flag & IOTYPE) == IOHERE && iop->heredoc &&
|
2008-02-26 22:08:33 +01:00
|
|
|
/* iop->delim[1] == '<' means here string */
|
2008-02-26 21:43:11 +01:00
|
|
|
(!iop->delim || iop->delim[1] != '<')) {
|
2005-05-23 05:06:10 +02:00
|
|
|
tputc('\n', shf);
|
|
|
|
shf_puts(iop->heredoc, shf);
|
|
|
|
fptreef(shf, indent, "%s",
|
|
|
|
evalstr(iop->delim, 0));
|
|
|
|
need_nl = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Last delimiter must be followed by a newline (this often
|
|
|
|
* leads to an extra blank line, but its not worth worrying
|
|
|
|
* about)
|
|
|
|
*/
|
|
|
|
if (need_nl)
|
|
|
|
tputc('\n', shf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2008-02-26 22:08:33 +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:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(">> ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IOWRITE:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts(flag & IOCLOB ? ">| " : "> ", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case IORDWR:
|
2008-02-26 22:08:33 +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;
|
|
|
|
}
|
|
|
|
/* name/delim are 0 when printing syntax errors */
|
|
|
|
if (type == IOHERE) {
|
|
|
|
if (iop->delim)
|
2008-02-26 22:08:33 +01:00
|
|
|
fptreef(shf, indent, "%s%S ",
|
|
|
|
/* here string */ iop->delim[1] == '<' ? "" : " ",
|
|
|
|
iop->delim);
|
|
|
|
else
|
|
|
|
tputc(' ', shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
} else if (iop->name)
|
|
|
|
fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ",
|
|
|
|
iop->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* variants of fputc, fputs for ptreef and snptreef
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
tputC(int c, struct shf *shf)
|
|
|
|
{
|
|
|
|
if ((c&0x60) == 0) { /* C0|C1 */
|
|
|
|
tputc((c&0x80) ? '$' : '^', shf);
|
|
|
|
tputc(((c&0x7F)|0x40), shf);
|
|
|
|
} else if ((c&0x7F) == 0x7F) { /* DEL */
|
|
|
|
tputc((c&0x80) ? '$' : '^', shf);
|
|
|
|
tputc('?', shf);
|
|
|
|
} else
|
|
|
|
tputc(c, shf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tputS(char *wp, struct shf *shf)
|
|
|
|
{
|
2010-02-25 21:18:19 +01:00
|
|
|
int c, quotelevel = 0;
|
2005-05-23 05:06:10 +02:00
|
|
|
|
|
|
|
/* problems:
|
|
|
|
* `...` -> $(...)
|
|
|
|
* 'foo' -> "foo"
|
|
|
|
* could change encoding to:
|
|
|
|
* OQUOTE ["'] ... CQUOTE ["']
|
|
|
|
* COMSUB [(`] ...\0 (handle $ ` \ and maybe " in `...` case)
|
|
|
|
*/
|
|
|
|
while (1)
|
2008-10-14 01:06:04 +02:00
|
|
|
switch (*wp++) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case EOS:
|
|
|
|
return;
|
2007-07-06 03:53:36 +02:00
|
|
|
case ADELIM:
|
2005-05-23 05:06:10 +02:00
|
|
|
case CHAR:
|
|
|
|
tputC(*wp++, shf);
|
|
|
|
break;
|
|
|
|
case QCHAR:
|
|
|
|
c = *wp++;
|
2010-02-25 21:18:19 +01:00
|
|
|
if (!quotelevel || (c == '"' || c == '`' || c == '$'))
|
2005-05-23 05:06:10 +02:00
|
|
|
tputc('\\', shf);
|
|
|
|
tputC(c, shf);
|
|
|
|
break;
|
|
|
|
case COMSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$(", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*wp != 0)
|
|
|
|
tputC(*wp++, shf);
|
|
|
|
tputc(')', shf);
|
|
|
|
wp++;
|
|
|
|
break;
|
|
|
|
case EXPRSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$((", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*wp != 0)
|
|
|
|
tputC(*wp++, shf);
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("))", shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
wp++;
|
|
|
|
break;
|
|
|
|
case OQUOTE:
|
2010-02-25 21:18:19 +01:00
|
|
|
quotelevel++;
|
2005-05-23 05:06:10 +02:00
|
|
|
tputc('"', shf);
|
|
|
|
break;
|
|
|
|
case CQUOTE:
|
2010-02-25 21:18:19 +01:00
|
|
|
if (quotelevel)
|
|
|
|
quotelevel--;
|
2005-05-23 05:06:10 +02:00
|
|
|
tputc('"', shf);
|
|
|
|
break;
|
|
|
|
case OSUBST:
|
|
|
|
tputc('$', shf);
|
|
|
|
if (*wp++ == '{')
|
|
|
|
tputc('{', shf);
|
|
|
|
while ((c = *wp++) != 0)
|
|
|
|
tputC(c, shf);
|
|
|
|
break;
|
|
|
|
case CSUBST:
|
|
|
|
if (*wp++ == '}')
|
|
|
|
tputc('}', shf);
|
|
|
|
break;
|
|
|
|
case OPAT:
|
|
|
|
tputc(*wp++, shf);
|
|
|
|
tputc('(', shf);
|
|
|
|
break;
|
|
|
|
case SPAT:
|
|
|
|
tputc('|', shf);
|
|
|
|
break;
|
|
|
|
case CPAT:
|
|
|
|
tputc(')', shf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is the _only_ way to reliably handle
|
|
|
|
* variable args with an ANSI compiler
|
|
|
|
*/
|
|
|
|
/* VARARGS */
|
|
|
|
int
|
|
|
|
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);
|
2005-05-23 05:06:10 +02:00
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
vfptreef(shf, indent, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
return (0);
|
2005-05-23 05:06:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS */
|
|
|
|
char *
|
|
|
|
snptreef(char *s, int n, const char *fmt, ...)
|
|
|
|
{
|
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
|
|
|
|
2007-10-25 17:19:16 +02:00
|
|
|
return (shf_sclose(&shf)); /* null terminates */
|
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':
|
|
|
|
tputc(va_arg(va, int), shf);
|
|
|
|
break;
|
|
|
|
case 's':
|
2006-11-10 01:09:27 +01:00
|
|
|
shf_puts(va_arg(va, char *), shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case 'S': /* word */
|
2006-11-10 01:09:27 +01:00
|
|
|
tputS(va_arg(va, char *), shf);
|
|
|
|
break;
|
|
|
|
case 'd': /* decimal */
|
|
|
|
shf_fprintf(shf, "%d", va_arg(va, int));
|
|
|
|
break;
|
|
|
|
case 'u': /* 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;
|
|
|
|
case 'T': /* format tree */
|
|
|
|
ptree(va_arg(va, struct op *), indent, shf);
|
|
|
|
break;
|
|
|
|
case ';': /* newline or ; */
|
|
|
|
case 'N': /* newline or space */
|
|
|
|
if (shf->flags & SHF_STRING) {
|
|
|
|
if (c == ';')
|
|
|
|
tputc(';', shf);
|
|
|
|
tputc(' ', shf);
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
tputc('\n', shf);
|
|
|
|
for (i = indent; i >= 8; i -= 8)
|
|
|
|
tputc('\t', shf);
|
|
|
|
for (; i > 0; --i)
|
|
|
|
tputc(' ', shf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
pioact(shf, indent, va_arg(va, struct ioword *));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
tputc(c, shf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
tputc(c, shf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 {
|
2007-03-04 01:13:17 +01:00
|
|
|
for (tw = (const char **)t->vars; *tw++ != NULL; )
|
2005-05-23 05:06:10 +02:00
|
|
|
;
|
2008-12-13 18:02:18 +01:00
|
|
|
rw = r->vars = alloc((tw - (const char **)t->vars + 1) *
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(*tw), ap);
|
2007-03-04 01:13:17 +01:00
|
|
|
for (tw = (const char **)t->vars; *tw != NULL; )
|
2005-05-23 05:06:10 +02:00
|
|
|
*rw++ = wdcopy(*tw++, ap);
|
|
|
|
*rw = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t->args == NULL)
|
|
|
|
r->args = NULL;
|
|
|
|
else {
|
|
|
|
for (tw = t->args; *tw++ != NULL; )
|
|
|
|
;
|
2008-12-13 18:02:18 +01:00
|
|
|
r->args = (const char **)(rw = alloc((tw - t->args + 1) *
|
2009-06-08 22:06:50 +02:00
|
|
|
sizeof(*tw), ap));
|
2005-05-23 05:06:10 +02:00
|
|
|
for (tw = t->args; *tw != NULL; )
|
|
|
|
*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
|
|
|
{
|
|
|
|
size_t 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;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
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;
|
|
|
|
case COMSUB:
|
|
|
|
case EXPRSUB:
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return a copy of wp without any of the mark up characters and
|
|
|
|
* with quote characters (" ' \) stripped.
|
|
|
|
* (string is allocated from ATEMP)
|
|
|
|
*/
|
|
|
|
char *
|
2008-03-01 22:10:26 +01:00
|
|
|
wdstrip(const char *wp, bool keepq, bool make_magic)
|
2005-05-23 05:06:10 +02:00
|
|
|
{
|
|
|
|
struct shf shf;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
shf_sopen(NULL, 32, SHF_WR | SHF_DYNAMIC, &shf);
|
|
|
|
|
|
|
|
/* problems:
|
|
|
|
* `...` -> $(...)
|
|
|
|
* x${foo:-"hi"} -> x${foo:-hi}
|
2008-03-01 22:10:26 +01:00
|
|
|
* x${foo:-'hi'} -> x${foo:-hi} unless keepq
|
2005-05-23 05:06:10 +02:00
|
|
|
*/
|
|
|
|
while (1)
|
2008-10-14 01:06:04 +02:00
|
|
|
switch (*wp++) {
|
2005-05-23 05:06:10 +02:00
|
|
|
case EOS:
|
2009-06-08 22:06:50 +02:00
|
|
|
return (shf_sclose(&shf)); /* null terminates */
|
2007-07-06 03:53:36 +02:00
|
|
|
case ADELIM:
|
2005-05-23 05:06:10 +02:00
|
|
|
case CHAR:
|
2008-03-01 22:10:26 +01:00
|
|
|
c = *wp++;
|
|
|
|
if (make_magic && (ISMAGIC(c) || c == '[' || c == NOT ||
|
|
|
|
c == '-' || c == ']' || c == '*' || c == '?'))
|
|
|
|
shf_putchar(MAGIC, &shf);
|
|
|
|
shf_putchar(c, &shf);
|
|
|
|
break;
|
2005-05-23 05:06:10 +02:00
|
|
|
case QCHAR:
|
2008-03-01 22:10:26 +01:00
|
|
|
c = *wp++;
|
|
|
|
if (keepq && (c == '"' || c == '`' || c == '$' || c == '\\'))
|
|
|
|
shf_putchar('\\', &shf);
|
|
|
|
shf_putchar(c, &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case COMSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$(", &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*wp != 0)
|
|
|
|
shf_putchar(*wp++, &shf);
|
|
|
|
shf_putchar(')', &shf);
|
|
|
|
break;
|
|
|
|
case EXPRSUB:
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("$((", &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
while (*wp != 0)
|
|
|
|
shf_putchar(*wp++, &shf);
|
2008-02-26 22:08:33 +01:00
|
|
|
shf_puts("))", &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case OQUOTE:
|
|
|
|
break;
|
|
|
|
case CQUOTE:
|
|
|
|
break;
|
|
|
|
case OSUBST:
|
|
|
|
shf_putchar('$', &shf);
|
|
|
|
if (*wp++ == '{')
|
|
|
|
shf_putchar('{', &shf);
|
|
|
|
while ((c = *wp++) != 0)
|
|
|
|
shf_putchar(c, &shf);
|
|
|
|
break;
|
|
|
|
case CSUBST:
|
|
|
|
if (*wp++ == '}')
|
|
|
|
shf_putchar('}', &shf);
|
|
|
|
break;
|
|
|
|
case OPAT:
|
2008-03-01 22:10:26 +01:00
|
|
|
if (make_magic) {
|
|
|
|
shf_putchar(MAGIC, &shf);
|
|
|
|
shf_putchar(*wp++ | 0x80, &shf);
|
|
|
|
} else {
|
|
|
|
shf_putchar(*wp++, &shf);
|
|
|
|
shf_putchar('(', &shf);
|
|
|
|
}
|
2005-05-23 05:06:10 +02:00
|
|
|
break;
|
|
|
|
case SPAT:
|
2008-03-01 22:10:26 +01:00
|
|
|
if (make_magic)
|
|
|
|
shf_putchar(MAGIC, &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_putchar('|', &shf);
|
|
|
|
break;
|
|
|
|
case CPAT:
|
2008-03-01 22:10:26 +01:00
|
|
|
if (make_magic)
|
|
|
|
shf_putchar(MAGIC, &shf);
|
2005-05-23 05:06:10 +02:00
|
|
|
shf_putchar(')', &shf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (ior = iow; *ior++ != NULL; )
|
|
|
|
;
|
2009-06-08 22:06:50 +02:00
|
|
|
ior = alloc((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) {
|
2007-03-04 01:13:17 +01:00
|
|
|
union mksh_ccphack cw;
|
|
|
|
/* XXX we assume the caller is right */
|
|
|
|
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;
|
|
|
|
|
|
|
|
for (iop = iow; (p = *iop++) != NULL; ) {
|
|
|
|
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
|
|
|
|
|
|
|
int
|
|
|
|
fpFUNCTf(struct shf *shf, int i, bool isksh, const char *k, struct op *v)
|
|
|
|
{
|
|
|
|
if (isksh)
|
|
|
|
return (fptreef(shf, i, "%s %s %T", T_function, k, v));
|
|
|
|
else
|
|
|
|
return (fptreef(shf, i, "%s() %T", k, v));
|
|
|
|
}
|