int p_swap(l_lst **la, l_lst **lb, char c) { if (*lb && c == 'a') p_switch(la, lb); else if (*la && c == 'b') { if (*lb) p_switch(lb, la); else { (*lb)->start = (*la)->start; (*la)->start = (*la)->start->next; (*la)->start->prev = NULL; } } write(1, "p", 1); write(1, &c, 1); write(1, " ", 1); return (0); }
static_fn void p_switch(const struct regnod *reg) { if (level > 1) sfnputc(outfile, '\t', level - 1); p_arg(reg->regptr, ')', PRE); begin_line = 0; sfputc(outfile, '\t'); if (reg->regcom) p_tree(reg->regcom, 0); level++; if (reg->regflag) { p_keyword(";&", END); } else { p_keyword(";;", END); } if (reg->regnxt) p_switch(reg->regnxt); }
// Print script corresponding to shell tree <t>. static_fn void p_tree(const Shnode_t *t, int tflags) { char *cp = NULL; int save = end_line; int needbrace = (tflags & NEED_BRACE); int bracket = 0; tflags &= ~NEED_BRACE; if (tflags & NO_NEWLINE) { end_line = ' '; } else { end_line = '\n'; } int cmd_type = t->tre.tretyp & COMMSK; switch (cmd_type) { case TTIME: { if (t->tre.tretyp & COMSCAN) { p_keyword("!", BEGIN); } else { p_keyword("time", BEGIN); } if (t->par.partre) p_tree(t->par.partre, tflags); level--; break; } case TCOM: { if (begin_line && level > 0) sfnputc(outfile, '\t', level); begin_line = 0; p_comarg((struct comnod *)t); break; } case TSETIO: { if (t->tre.tretyp & FPCL) { tflags |= NEED_BRACE; } else { tflags = NO_NEWLINE | NEED_BRACE; } p_tree(t->fork.forktre, tflags); p_redirect(t->fork.forkio); break; } case TFORK: { if (needbrace) tflags |= NEED_BRACE; if (t->tre.tretyp & (FAMP | FCOOP)) { tflags = NEED_BRACE | NO_NEWLINE; end_line = ' '; } else if (t->fork.forkio) { tflags = NO_NEWLINE; } p_tree(t->fork.forktre, tflags); if (t->fork.forkio) p_redirect(t->fork.forkio); if (t->tre.tretyp & FCOOP) { sfputr(outfile, "|&", '\n'); begin_line = 1; } else if (t->tre.tretyp & FAMP) { sfputr(outfile, "&", '\n'); begin_line = 1; } break; } case TIF: { p_keyword("if", BEGIN); p_tree(t->if_.iftre, 0); p_keyword("then", MIDDLE); p_tree(t->if_.thtre, 0); if (t->if_.eltre) { p_keyword("else", MIDDLE); p_tree(t->if_.eltre, 0); } p_keyword("fi", END); break; } case TWH: { if (t->wh.whinc) { cp = "for"; } else if (t->tre.tretyp & COMSCAN) { cp = "until"; } else { cp = "while"; } p_keyword(cp, BEGIN); if (t->wh.whinc) { struct argnod *arg = (t->wh.whtre)->ar.arexpr; sfprintf(outfile, "(( %s; ", forinit); forinit = ""; sfputr(outfile, arg->argval, ';'); arg = (t->wh.whinc)->arexpr; sfprintf(outfile, " %s))\n", arg->argval); } else { p_tree(t->wh.whtre, 0); } t = t->wh.dotre; p_keyword("do", MIDDLE); p_tree(t, 0); p_keyword("done", END); break; } case TLST: { Shnode_t *tr = t->lst.lstrit; if (tr->tre.tretyp == TWH && tr->wh.whinc && t->lst.lstlef->tre.tretyp == TARITH) { // Arithmetic for statement. struct argnod *init = (t->lst.lstlef)->ar.arexpr; forinit = init->argval; p_tree(t->lst.lstrit, tflags); break; } if (needbrace) p_keyword("{", BEGIN); p_tree(t->lst.lstlef, 0); if (needbrace) tflags = 0; p_tree(t->lst.lstrit, tflags); if (needbrace) p_keyword("}", END); break; } case TAND: case TORF: case TFIL: { if (cmd_type == TAND) { cp = "&&"; } else if (cmd_type == TORF) { cp = "||"; } else { cp = "|"; } if (t->tre.tretyp & TTEST) { tflags |= NO_NEWLINE; if (!(tflags & NO_BRACKET)) { p_keyword("[[", BEGIN); tflags |= NO_BRACKET; bracket = 1; } } p_tree(t->lst.lstlef, NEED_BRACE | NO_NEWLINE | (tflags & NO_BRACKET)); if (tflags & FALTPIPE) { Shnode_t *tt = t->lst.lstrit; if (tt->tre.tretyp != TFIL || !(tt->lst.lstlef->tre.tretyp & FALTPIPE)) { sfputc(outfile, '\n'); return; } } sfputr(outfile, cp, here_doc ? '\n' : ' '); if (here_doc) { here_body(here_doc); here_doc = 0; } level++; p_tree(t->lst.lstrit, tflags | NEED_BRACE); if (bracket) p_keyword("]]", END); level--; break; } case TPAR: { p_keyword("(", BEGIN); p_tree(t->par.partre, 0); p_keyword(")", END); break; } case TARITH: { struct argnod *ap = t->ar.arexpr; if (begin_line && level) sfnputc(outfile, '\t', level); sfprintf(outfile, "(( %s ))%c", ap->argval, end_line); if (!(tflags & NO_NEWLINE)) begin_line = 1; break; } case TFOR: { cp = ((t->tre.tretyp & COMSCAN) ? "select" : "for"); p_keyword(cp, BEGIN); sfputr(outfile, t->for_.fornam, ' '); if (t->for_.forlst) { sfputr(outfile, "in", ' '); tflags = end_line; end_line = '\n'; p_comarg(t->for_.forlst); end_line = tflags; } else { sfputc(outfile, '\n'); } begin_line = 1; t = t->for_.fortre; p_keyword("do", MIDDLE); p_tree(t, 0); p_keyword("done", END); break; } case TSW: { p_keyword("case", BEGIN); p_arg(t->sw.swarg, ' ', 0); if (t->sw.swlst) { begin_line = 1; sfputr(outfile, "in", '\n'); tflags = end_line; end_line = '\n'; p_switch(t->sw.swlst); end_line = tflags; } p_keyword("esac", END); break; } case TFUN: { if (t->tre.tretyp & FPOSIX) { sfprintf(outfile, "%s", t->funct.functnam); p_keyword("()\n", BEGIN); } else { p_keyword("function", BEGIN); tflags = (t->funct.functargs ? ' ' : '\n'); sfputr(outfile, t->funct.functnam, tflags); if (t->funct.functargs) { tflags = end_line; end_line = '\n'; p_comarg(t->funct.functargs); end_line = tflags; } } begin_line = 1; p_keyword("{\n", MIDDLE); begin_line = 1; p_tree(t->funct.functtre, 0); p_keyword("}", END); break; } case TTST: { // new test compound command if (!(tflags & NO_BRACKET)) p_keyword("[[", BEGIN); if ((t->tre.tretyp & TPAREN) == TPAREN) { p_keyword("(", BEGIN); p_tree(t->lst.lstlef, NO_BRACKET | NO_NEWLINE); p_keyword(")", END); } else { int flags = (t->tre.tretyp) >> TSHIFT; if (t->tre.tretyp & TNEGATE) sfputr(outfile, "!", ' '); if (t->tre.tretyp & TUNARY) { un_op[1] = flags; sfputr(outfile, un_op, ' '); } else { cp = ((char *)(shtab_testops + (flags & 037) - 1)->sh_name); } p_arg(&(t->lst.lstlef->arg), ' ', 0); if (t->tre.tretyp & TBINARY) { assert(cp); sfputr(outfile, cp, ' '); p_arg(&(t->lst.lstrit->arg), ' ', 0); } } if (!(tflags & NO_BRACKET)) p_keyword("]]", END); } default: { break; } } while (begin_line && here_doc) { here_body(here_doc); here_doc = 0; } end_line = save; }