clean up, optimise, comment code

This commit is contained in:
tg 2008-02-26 21:08:33 +00:00
parent 5ea53a15c7
commit 0f1501326b
2 changed files with 33 additions and 41 deletions

8
lex.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.51 2008/02/26 20:43:10 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/lex.c,v 1.52 2008/02/26 21:08:33 tg Exp $");
/* /*
* states while lexing word * states while lexing word
@ -683,15 +683,18 @@ yylex(int cf)
*wp++ = QCHAR; *wp++ = QCHAR;
*wp++ = c; *wp++ = c;
} }
/* invoke quoting mode */
Xstring(ws, wp)[0] = QCHAR; Xstring(ws, wp)[0] = QCHAR;
} else if (c == '\'') { } else if (c == '\'') {
PUSH_STATE(SSQUOTE); PUSH_STATE(SSQUOTE);
*wp++ = OQUOTE; *wp++ = OQUOTE;
ignore_backslash_newline++; ignore_backslash_newline++;
/* invoke quoting mode */
Xstring(ws, wp)[0] = QCHAR; Xstring(ws, wp)[0] = QCHAR;
} else if (c == '"') { } else if (c == '"') {
state = statep->ls_state = SHEREDQUOTE; state = statep->ls_state = SHEREDQUOTE;
*wp++ = OQUOTE; *wp++ = OQUOTE;
/* just don't IFS split; no quoting mode */
} else { } else {
*wp++ = CHAR; *wp++ = CHAR;
*wp++ = c; *wp++ = c;
@ -730,6 +733,7 @@ yylex(int cf)
if (c == '"') { if (c == '"') {
*wp++ = CQUOTE; *wp++ = CQUOTE;
state = statep->ls_state = state = statep->ls_state =
/* dp[1] == '<' means here string */
Xstring(ws, wp)[1] == '<' ? Xstring(ws, wp)[1] == '<' ?
SHERESTRING : SHEREDELIM; SHERESTRING : SHEREDELIM;
} else { } else {
@ -918,6 +922,7 @@ gethere(bool iseof)
for (p = heres; p < herep; p++) for (p = heres; p < herep; p++)
if (iseof && (*p)->delim[1] != '<') if (iseof && (*p)->delim[1] != '<')
/* only here strings at EOF */
return; return;
else else
readhere(*p); readhere(*p);
@ -940,6 +945,7 @@ readhere(struct ioword *iop)
int xpos; int xpos;
if (iop->delim[1] == '<') { if (iop->delim[1] == '<') {
/* process the here string */
xp = iop->heredoc = evalstr(iop->delim, DOBLANK); xp = iop->heredoc = evalstr(iop->delim, DOBLANK);
c = strlen(xp) - 1; c = strlen(xp) - 1;
memmove(xp, xp + 1, c); memmove(xp, xp + 1, c);

66
tree.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.13 2008/02/26 20:43:11 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/tree.c,v 1.14 2008/02/26 21:08:33 tg Exp $");
#define INDENT 4 #define INDENT 4
@ -34,12 +34,12 @@ ptree(struct op *t, int indent, struct shf *shf)
for (w = (const char **)t->vars; *w != NULL; ) for (w = (const char **)t->vars; *w != NULL; )
fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%S ", *w++);
else else
fptreef(shf, indent, "#no-vars# "); shf_puts("#no-vars# ", shf);
if (t->args) if (t->args)
for (w = t->args; *w != NULL; ) for (w = t->args; *w != NULL; )
fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%S ", *w++);
else else
fptreef(shf, indent, "#no-args# "); shf_puts("#no-args# ", shf);
break; break;
case TEXEC: case TEXEC:
t = t->left; t = t->left;
@ -61,17 +61,17 @@ ptree(struct op *t, int indent, struct shf *shf)
t->left, (t->type==TOR) ? "||" : "&&", t->right); t->left, (t->type==TOR) ? "||" : "&&", t->right);
break; break;
case TBANG: case TBANG:
fptreef(shf, indent, "! "); shf_puts("! ", shf);
t = t->right; t = t->right;
goto Chain; goto Chain;
case TDBRACKET: case TDBRACKET:
{ {
int i; int i;
fptreef(shf, indent, "[["); shf_puts("[[", shf);
for (i = 0; t->args[i]; i++) for (i = 0; t->args[i]; i++)
fptreef(shf, indent, " %S", t->args[i]); fptreef(shf, indent, " %S", t->args[i]);
fptreef(shf, indent, " ]] "); shf_puts(" ]] ", shf);
break; break;
} }
case TSELECT: case TSELECT:
@ -81,7 +81,7 @@ ptree(struct op *t, int indent, struct shf *shf)
if (t->type == TFOR) if (t->type == TFOR)
fptreef(shf, indent, "for %s ", t->str); fptreef(shf, indent, "for %s ", t->str);
if (t->vars != NULL) { if (t->vars != NULL) {
fptreef(shf, indent, "in "); shf_puts("in ", shf);
for (w = (const char **)t->vars; *w; ) for (w = (const char **)t->vars; *w; )
fptreef(shf, indent, "%S ", *w++); fptreef(shf, indent, "%S ", *w++);
fptreef(shf, indent, "%;"); fptreef(shf, indent, "%;");
@ -153,7 +153,7 @@ ptree(struct op *t, int indent, struct shf *shf)
fptreef(shf, indent, "time %T", t->left); fptreef(shf, indent, "time %T", t->left);
break; break;
default: default:
fptreef(shf, indent, "<botch>"); shf_puts("<botch>", shf);
break; break;
} }
if ((ioact = t->ioact) != NULL) { if ((ioact = t->ioact) != NULL) {
@ -167,6 +167,7 @@ ptree(struct op *t, int indent, struct shf *shf)
/* heredoc is 0 when tracing (set -x) */ /* heredoc is 0 when tracing (set -x) */
if ((iop->flag & IOTYPE) == IOHERE && iop->heredoc && if ((iop->flag & IOTYPE) == IOHERE && iop->heredoc &&
/* iop->delim[1] == '<' means here string */
(!iop->delim || iop->delim[1] != '<')) { (!iop->delim || iop->delim[1] != '<')) {
tputc('\n', shf); tputc('\n', shf);
shf_puts(iop->heredoc, shf); shf_puts(iop->heredoc, shf);
@ -200,39 +201,32 @@ pioact(struct shf *shf, int indent, struct ioword *iop)
switch (type) { switch (type) {
case IOREAD: case IOREAD:
fptreef(shf, indent, "< "); shf_puts("< ", shf);
break; break;
case IOHERE: case IOHERE:
if (flag&IOSKIP) shf_puts(flag & IOSKIP ? "<<-" : "<<", shf);
fptreef(shf, indent, "<<-");
else
fptreef(shf, indent, "<<");
break; break;
case IOCAT: case IOCAT:
fptreef(shf, indent, ">> "); shf_puts(">> ", shf);
break; break;
case IOWRITE: case IOWRITE:
if (flag&IOCLOB) shf_puts(flag & IOCLOB ? ">| " : "> ", shf);
fptreef(shf, indent, ">| ");
else
fptreef(shf, indent, "> ");
break; break;
case IORDWR: case IORDWR:
fptreef(shf, indent, "<> "); shf_puts("<> ", shf);
break; break;
case IODUP: case IODUP:
if (flag & IORDUP) shf_puts(flag & IORDUP ? "<&" : ">&", shf);
fptreef(shf, indent, "<&");
else
fptreef(shf, indent, ">&");
break; break;
} }
/* name/delim are 0 when printing syntax errors */ /* name/delim are 0 when printing syntax errors */
if (type == IOHERE) { if (type == IOHERE) {
if ((flag & IOSKIP) || (iop->delim[1] != '<'))
tputc(' ', shf);
if (iop->delim) if (iop->delim)
fptreef(shf, indent, "%S ", iop->delim); fptreef(shf, indent, "%s%S ",
/* here string */ iop->delim[1] == '<' ? "" : " ",
iop->delim);
else
tputc(' ', shf);
} else if (iop->name) } else if (iop->name)
fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ", fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ",
iop->name); iop->name);
@ -282,21 +276,17 @@ tputS(char *wp, struct shf *shf)
tputC(c, shf); tputC(c, shf);
break; break;
case COMSUB: case COMSUB:
tputc('$', shf); shf_puts("$(", shf);
tputc('(', shf);
while (*wp != 0) while (*wp != 0)
tputC(*wp++, shf); tputC(*wp++, shf);
tputc(')', shf); tputc(')', shf);
wp++; wp++;
break; break;
case EXPRSUB: case EXPRSUB:
tputc('$', shf); shf_puts("$((", shf);
tputc('(', shf);
tputc('(', shf);
while (*wp != 0) while (*wp != 0)
tputC(*wp++, shf); tputC(*wp++, shf);
tputc(')', shf); shf_puts("))", shf);
tputc(')', shf);
wp++; wp++;
break; break;
case OQUOTE: case OQUOTE:
@ -560,20 +550,16 @@ wdstrip(const char *wp)
shf_putchar(*wp++, &shf); shf_putchar(*wp++, &shf);
break; break;
case COMSUB: case COMSUB:
shf_putchar('$', &shf); shf_puts("$(", &shf);
shf_putchar('(', &shf);
while (*wp != 0) while (*wp != 0)
shf_putchar(*wp++, &shf); shf_putchar(*wp++, &shf);
shf_putchar(')', &shf); shf_putchar(')', &shf);
break; break;
case EXPRSUB: case EXPRSUB:
shf_putchar('$', &shf); shf_puts("$((", &shf);
shf_putchar('(', &shf);
shf_putchar('(', &shf);
while (*wp != 0) while (*wp != 0)
shf_putchar(*wp++, &shf); shf_putchar(*wp++, &shf);
shf_putchar(')', &shf); shf_puts("))", &shf);
shf_putchar(')', &shf);
break; break;
case OQUOTE: case OQUOTE:
break; break;