Home | History | Annotate | Line # | Download | only in ksh
tree.c revision 1.1
      1  1.1  jtc /*
      2  1.1  jtc  * command tree climbing
      3  1.1  jtc  */
      4  1.1  jtc 
      5  1.1  jtc #include "sh.h"
      6  1.1  jtc 
      7  1.1  jtc #define INDENT	4
      8  1.1  jtc 
      9  1.1  jtc #define tputc(c, shf)	shf_putchar(c, shf);
     10  1.1  jtc static void 	ptree ARGS((struct op *t, int indent, struct shf *f));
     11  1.1  jtc static void 	pioact ARGS((struct shf *f, int indent, struct ioword *iop));
     12  1.1  jtc static void	tputC ARGS((int c, struct shf *shf));
     13  1.1  jtc static void	tputS ARGS((char *wp, struct shf *shf));
     14  1.1  jtc static void	vfptreef ARGS((struct shf *shf, int indent, const char *fmt, va_list va));
     15  1.1  jtc static struct ioword **iocopy ARGS((struct ioword **iow, Area *ap));
     16  1.1  jtc static void     iofree ARGS((struct ioword **iow, Area *ap));
     17  1.1  jtc 
     18  1.1  jtc /*
     19  1.1  jtc  * print a command tree
     20  1.1  jtc  */
     21  1.1  jtc 
     22  1.1  jtc static void
     23  1.1  jtc ptree(t, indent, shf)
     24  1.1  jtc 	register struct op *t;
     25  1.1  jtc 	int indent;
     26  1.1  jtc 	register struct shf *shf;
     27  1.1  jtc {
     28  1.1  jtc 	register char **w;
     29  1.1  jtc 	struct ioword **ioact;
     30  1.1  jtc 	struct op *t1;
     31  1.1  jtc 
     32  1.1  jtc  Chain:
     33  1.1  jtc 	if (t == NULL)
     34  1.1  jtc 		return;
     35  1.1  jtc 	switch (t->type) {
     36  1.1  jtc 	  case TCOM:
     37  1.1  jtc 		if (t->vars)
     38  1.1  jtc 			for (w = t->vars; *w != NULL; )
     39  1.1  jtc 				fptreef(shf, indent, "%S ", *w++);
     40  1.1  jtc 		else
     41  1.1  jtc 			fptreef(shf, indent, "#no-vars# ");
     42  1.1  jtc 		if (t->args)
     43  1.1  jtc 			for (w = t->args; *w != NULL; )
     44  1.1  jtc 				fptreef(shf, indent, "%S ", *w++);
     45  1.1  jtc 		else
     46  1.1  jtc 			fptreef(shf, indent, "#no-args# ");
     47  1.1  jtc 		break;
     48  1.1  jtc 	  case TEXEC:
     49  1.1  jtc 		t = t->left;
     50  1.1  jtc 		goto Chain;
     51  1.1  jtc 	  case TPAREN:
     52  1.1  jtc 		fptreef(shf, indent + 2, "( %T) ", t->left);
     53  1.1  jtc 		break;
     54  1.1  jtc 	  case TPIPE:
     55  1.1  jtc 		fptreef(shf, indent, "%T| ", t->left);
     56  1.1  jtc 		t = t->right;
     57  1.1  jtc 		goto Chain;
     58  1.1  jtc 	  case TLIST:
     59  1.1  jtc 		fptreef(shf, indent, "%T%;", t->left);
     60  1.1  jtc 		t = t->right;
     61  1.1  jtc 		goto Chain;
     62  1.1  jtc 	  case TOR:
     63  1.1  jtc 	  case TAND:
     64  1.1  jtc 		fptreef(shf, indent, "%T%s %T",
     65  1.1  jtc 			t->left, (t->type==TOR) ? "||" : "&&", t->right);
     66  1.1  jtc 		break;
     67  1.1  jtc 	  case TBANG:
     68  1.1  jtc 		fptreef(shf, indent, "! ");
     69  1.1  jtc 		t = t->right;
     70  1.1  jtc 		goto Chain;
     71  1.1  jtc 	  case TDBRACKET:
     72  1.1  jtc 	  {
     73  1.1  jtc 		int i;
     74  1.1  jtc 
     75  1.1  jtc 		fptreef(shf, indent, "[[");
     76  1.1  jtc 		for (i = 0; t->args[i]; i++)
     77  1.1  jtc 			fptreef(shf, indent, " %S", t->args[i]);
     78  1.1  jtc 		fptreef(shf, indent, " ]] ");
     79  1.1  jtc 		break;
     80  1.1  jtc 	  }
     81  1.1  jtc #ifdef KSH
     82  1.1  jtc 	  case TSELECT:
     83  1.1  jtc 		fptreef(shf, indent, "select %s ", t->str);
     84  1.1  jtc 		/* fall through */
     85  1.1  jtc #endif /* KSH */
     86  1.1  jtc 	  case TFOR:
     87  1.1  jtc 		if (t->type == TFOR)
     88  1.1  jtc 			fptreef(shf, indent, "for %s ", t->str);
     89  1.1  jtc 		if (t->vars != NULL) {
     90  1.1  jtc 			fptreef(shf, indent, "in ");
     91  1.1  jtc 			for (w = t->vars; *w; )
     92  1.1  jtc 				fptreef(shf, indent, "%S ", *w++);
     93  1.1  jtc 			fptreef(shf, indent, "%;");
     94  1.1  jtc 		}
     95  1.1  jtc 		fptreef(shf, indent + INDENT, "do%N%T", t->left);
     96  1.1  jtc 		fptreef(shf, indent, "%;done ");
     97  1.1  jtc 		break;
     98  1.1  jtc 	  case TCASE:
     99  1.1  jtc 		fptreef(shf, indent, "case %S in", t->str);
    100  1.1  jtc 		for (t1 = t->left; t1 != NULL; t1 = t1->right) {
    101  1.1  jtc 			fptreef(shf, indent, "%N(");
    102  1.1  jtc 			for (w = t1->vars; *w != NULL; w++)
    103  1.1  jtc 				fptreef(shf, indent, "%S%c", *w,
    104  1.1  jtc 					(w[1] != NULL) ? '|' : ')');
    105  1.1  jtc 			fptreef(shf, indent + INDENT, "%;%T%N;;", t1->left);
    106  1.1  jtc 		}
    107  1.1  jtc 		fptreef(shf, indent, "%Nesac ");
    108  1.1  jtc 		break;
    109  1.1  jtc 	  case TIF:
    110  1.1  jtc 	  case TELIF:
    111  1.1  jtc 		/* 3 == strlen("if ") */
    112  1.1  jtc 		fptreef(shf, indent + 3, "if %T", t->left);
    113  1.1  jtc 		for (;;) {
    114  1.1  jtc 			t = t->right;
    115  1.1  jtc 			if (t->left != NULL) {
    116  1.1  jtc 				fptreef(shf, indent, "%;");
    117  1.1  jtc 				fptreef(shf, indent + INDENT, "then%N%T",
    118  1.1  jtc 					t->left);
    119  1.1  jtc 			}
    120  1.1  jtc 			if (t->right == NULL || t->right->type != TELIF)
    121  1.1  jtc 				break;
    122  1.1  jtc 			t = t->right;
    123  1.1  jtc 			fptreef(shf, indent, "%;");
    124  1.1  jtc 			/* 5 == strlen("elif ") */
    125  1.1  jtc 			fptreef(shf, indent + 5, "elif %T", t->left);
    126  1.1  jtc 		}
    127  1.1  jtc 		if (t->right != NULL) {
    128  1.1  jtc 			fptreef(shf, indent, "%;");
    129  1.1  jtc 			fptreef(shf, indent + INDENT, "else%;%T", t->right);
    130  1.1  jtc 		}
    131  1.1  jtc 		fptreef(shf, indent, "%;fi ");
    132  1.1  jtc 		break;
    133  1.1  jtc 	  case TWHILE:
    134  1.1  jtc 	  case TUNTIL:
    135  1.1  jtc 		/* 6 == strlen("while"/"until") */
    136  1.1  jtc 		fptreef(shf, indent + 6, "%s %T",
    137  1.1  jtc 			(t->type==TWHILE) ? "while" : "until",
    138  1.1  jtc 			t->left);
    139  1.1  jtc 		fptreef(shf, indent, "%;do");
    140  1.1  jtc 		fptreef(shf, indent + INDENT, "%;%T", t->right);
    141  1.1  jtc 		fptreef(shf, indent, "%;done ");
    142  1.1  jtc 		break;
    143  1.1  jtc 	  case TBRACE:
    144  1.1  jtc 		fptreef(shf, indent + INDENT, "{%;%T", t->left);
    145  1.1  jtc 		fptreef(shf, indent, "%;} ");
    146  1.1  jtc 		break;
    147  1.1  jtc 	  case TCOPROC:
    148  1.1  jtc 		fptreef(shf, indent, "%T|& ", t->left);
    149  1.1  jtc 		break;
    150  1.1  jtc 	  case TASYNC:
    151  1.1  jtc 		fptreef(shf, indent, "%T& ", t->left);
    152  1.1  jtc 		break;
    153  1.1  jtc 	  case TFUNCT:
    154  1.1  jtc 		fptreef(shf, indent, "function %s %T", t->str, t->left);
    155  1.1  jtc 		break;
    156  1.1  jtc 	  case TTIME:
    157  1.1  jtc 		fptreef(shf, indent, "time %T", t->left);
    158  1.1  jtc 		break;
    159  1.1  jtc 	  default:
    160  1.1  jtc 		fptreef(shf, indent, "<botch>");
    161  1.1  jtc 		break;
    162  1.1  jtc 	}
    163  1.1  jtc 	if ((ioact = t->ioact) != NULL) {
    164  1.1  jtc 		int	need_nl = 0;
    165  1.1  jtc 
    166  1.1  jtc 		while (*ioact != NULL)
    167  1.1  jtc 			pioact(shf, indent, *ioact++);
    168  1.1  jtc 		/* Print here documents after everything else... */
    169  1.1  jtc 		for (ioact = t->ioact; *ioact != NULL; ) {
    170  1.1  jtc 			struct ioword *iop = *ioact++;
    171  1.1  jtc 
    172  1.1  jtc 			/* name is 0 when tracing (set -x) */
    173  1.1  jtc 			if ((iop->flag & IOTYPE) == IOHERE && iop->name) {
    174  1.1  jtc 				struct shf *rshf;
    175  1.1  jtc 				char buf[1024];
    176  1.1  jtc 				int n;
    177  1.1  jtc 
    178  1.1  jtc 				tputc('\n', shf);
    179  1.1  jtc 				if ((rshf = shf_open(iop->name, O_RDONLY, 0, 0))) {
    180  1.1  jtc 					while ((n = shf_read(buf, sizeof(buf), rshf))
    181  1.1  jtc 										> 0)
    182  1.1  jtc 						shf_write(buf, n, shf);
    183  1.1  jtc 					shf_close(rshf);
    184  1.1  jtc 				} else
    185  1.1  jtc 					errorf("can't open %s - %s",
    186  1.1  jtc 						iop->name, strerror(errno));
    187  1.1  jtc 				fptreef(shf, indent, "%s", evalstr(iop->delim, 0));
    188  1.1  jtc 				need_nl = 1;
    189  1.1  jtc 			}
    190  1.1  jtc 		}
    191  1.1  jtc 		/* Last delimiter must be followed by a newline (this often
    192  1.1  jtc 		 * leads to an extra blank line, but its not worth worrying
    193  1.1  jtc 		 * about)
    194  1.1  jtc 		 */
    195  1.1  jtc 		if (need_nl)
    196  1.1  jtc 			tputc('\n', shf);
    197  1.1  jtc 	}
    198  1.1  jtc }
    199  1.1  jtc 
    200  1.1  jtc static void
    201  1.1  jtc pioact(shf, indent, iop)
    202  1.1  jtc 	register struct shf *shf;
    203  1.1  jtc 	int indent;
    204  1.1  jtc 	register struct ioword *iop;
    205  1.1  jtc {
    206  1.1  jtc 	int flag = iop->flag;
    207  1.1  jtc 	int type = flag & IOTYPE;
    208  1.1  jtc 	int expected;
    209  1.1  jtc 
    210  1.1  jtc 	expected = (type == IOREAD || type == IORDWR || type == IOHERE) ? 0
    211  1.1  jtc 		    : (type == IOCAT || type == IOWRITE) ? 1
    212  1.1  jtc 		    : (type == IODUP && (iop->unit == !(flag & IORDUP))) ?
    213  1.1  jtc 			iop->unit
    214  1.1  jtc 		    : iop->unit + 1;
    215  1.1  jtc 	if (iop->unit != expected)
    216  1.1  jtc 		tputc('0' + iop->unit, shf);
    217  1.1  jtc 
    218  1.1  jtc 	switch (type) {
    219  1.1  jtc 	case IOREAD:
    220  1.1  jtc 		fptreef(shf, indent, "< ");
    221  1.1  jtc 		break;
    222  1.1  jtc 	case IOHERE:
    223  1.1  jtc 		if (flag&IOSKIP)
    224  1.1  jtc 			fptreef(shf, indent, "<<- ");
    225  1.1  jtc 		else
    226  1.1  jtc 			fptreef(shf, indent, "<< ");
    227  1.1  jtc 		break;
    228  1.1  jtc 	case IOCAT:
    229  1.1  jtc 		fptreef(shf, indent, ">> ");
    230  1.1  jtc 		break;
    231  1.1  jtc 	case IOWRITE:
    232  1.1  jtc 		if (flag&IOCLOB)
    233  1.1  jtc 			fptreef(shf, indent, ">| ");
    234  1.1  jtc 		else
    235  1.1  jtc 			fptreef(shf, indent, "> ");
    236  1.1  jtc 		break;
    237  1.1  jtc 	case IORDWR:
    238  1.1  jtc 		fptreef(shf, indent, "<> ");
    239  1.1  jtc 		break;
    240  1.1  jtc 	case IODUP:
    241  1.1  jtc 		if (flag & IORDUP)
    242  1.1  jtc 			fptreef(shf, indent, "<&");
    243  1.1  jtc 		else
    244  1.1  jtc 			fptreef(shf, indent, ">&");
    245  1.1  jtc 		break;
    246  1.1  jtc 	}
    247  1.1  jtc 	/* name/delim are 0 when printing syntax errors */
    248  1.1  jtc 	if (type == IOHERE) {
    249  1.1  jtc 		if (iop->delim)
    250  1.1  jtc 			fptreef(shf, indent, "%S ", iop->delim);
    251  1.1  jtc 	} else if (iop->name)
    252  1.1  jtc 		fptreef(shf, indent, (iop->flag & IONAMEXP) ? "%s " : "%S ",
    253  1.1  jtc 			iop->name);
    254  1.1  jtc }
    255  1.1  jtc 
    256  1.1  jtc 
    257  1.1  jtc /*
    258  1.1  jtc  * variants of fputc, fputs for ptreef and snptreef
    259  1.1  jtc  */
    260  1.1  jtc 
    261  1.1  jtc static void
    262  1.1  jtc tputC(c, shf)
    263  1.1  jtc 	register int c;
    264  1.1  jtc 	register struct shf *shf;
    265  1.1  jtc {
    266  1.1  jtc 	if ((c&0x60) == 0) {		/* C0|C1 */
    267  1.1  jtc 		tputc((c&0x80) ? '$' : '^', shf);
    268  1.1  jtc 		tputc(((c&0x7F)|0x40), shf);
    269  1.1  jtc 	} else if ((c&0x7F) == 0x7F) {	/* DEL */
    270  1.1  jtc 		tputc((c&0x80) ? '$' : '^', shf);
    271  1.1  jtc 		tputc('?', shf);
    272  1.1  jtc 	} else
    273  1.1  jtc 		tputc(c, shf);
    274  1.1  jtc }
    275  1.1  jtc 
    276  1.1  jtc static void
    277  1.1  jtc tputS(wp, shf)
    278  1.1  jtc 	register char *wp;
    279  1.1  jtc 	register struct shf *shf;
    280  1.1  jtc {
    281  1.1  jtc 	register int c, quoted=0;
    282  1.1  jtc 
    283  1.1  jtc 	while (1)
    284  1.1  jtc 		switch ((c = *wp++)) {
    285  1.1  jtc 		  case EOS:
    286  1.1  jtc 			return;
    287  1.1  jtc 		  case CHAR:
    288  1.1  jtc 			tputC(*wp++, shf);
    289  1.1  jtc 			break;
    290  1.1  jtc 		  case QCHAR:
    291  1.1  jtc 			c = *wp++;
    292  1.1  jtc 			if (!quoted || (c == '"' || c == '`' || c == '$'))
    293  1.1  jtc 				tputc('\\', shf);
    294  1.1  jtc 			tputC(c, shf);
    295  1.1  jtc 			break;
    296  1.1  jtc 		  case COMSUB:
    297  1.1  jtc 			tputc('$', shf);
    298  1.1  jtc 			tputc('(', shf);
    299  1.1  jtc 			while (*wp != 0)
    300  1.1  jtc 				tputC(*wp++, shf);
    301  1.1  jtc 			tputc(')', shf);
    302  1.1  jtc 			break;
    303  1.1  jtc 		  case EXPRSUB:
    304  1.1  jtc 			tputc('$', shf);
    305  1.1  jtc 			tputc('(', shf);
    306  1.1  jtc 			tputc('(', shf);
    307  1.1  jtc 			while (*wp != 0)
    308  1.1  jtc 				tputC(*wp++, shf);
    309  1.1  jtc 			tputc(')', shf);
    310  1.1  jtc 			tputc(')', shf);
    311  1.1  jtc 			break;
    312  1.1  jtc 		  case OQUOTE:
    313  1.1  jtc 		  	quoted = 1;
    314  1.1  jtc 			tputc('"', shf);
    315  1.1  jtc 			break;
    316  1.1  jtc 		  case CQUOTE:
    317  1.1  jtc 			quoted = 0;
    318  1.1  jtc 			tputc('"', shf);
    319  1.1  jtc 			break;
    320  1.1  jtc 		  case OSUBST:
    321  1.1  jtc 			tputc('$', shf);
    322  1.1  jtc 			tputc('{', shf);
    323  1.1  jtc 			while ((c = *wp++) != 0)
    324  1.1  jtc 				tputC(c, shf);
    325  1.1  jtc 			break;
    326  1.1  jtc 		  case CSUBST:
    327  1.1  jtc 			tputc('}', shf);
    328  1.1  jtc 			break;
    329  1.1  jtc #ifdef KSH
    330  1.1  jtc 		  case OPAT:
    331  1.1  jtc 			tputc(*wp++, shf);
    332  1.1  jtc 			tputc('(', shf);
    333  1.1  jtc 			break;
    334  1.1  jtc 		  case SPAT:
    335  1.1  jtc 			tputc('|', shf);
    336  1.1  jtc 			break;
    337  1.1  jtc 		  case CPAT:
    338  1.1  jtc 			tputc(')', shf);
    339  1.1  jtc 			break;
    340  1.1  jtc #endif /* KSH */
    341  1.1  jtc 		}
    342  1.1  jtc }
    343  1.1  jtc 
    344  1.1  jtc /*
    345  1.1  jtc  * this is the _only_ way to reliably handle
    346  1.1  jtc  * variable args with an ANSI compiler
    347  1.1  jtc  */
    348  1.1  jtc /* VARARGS */
    349  1.1  jtc int
    350  1.1  jtc #ifdef HAVE_PROTOTYPES
    351  1.1  jtc fptreef(struct shf *shf, int indent, const char *fmt, ...)
    352  1.1  jtc #else
    353  1.1  jtc fptreef(shf, indent, fmt, va_alist)
    354  1.1  jtc   struct shf *shf;
    355  1.1  jtc   int indent;
    356  1.1  jtc   const char *fmt;
    357  1.1  jtc   va_dcl
    358  1.1  jtc #endif
    359  1.1  jtc {
    360  1.1  jtc   va_list	va;
    361  1.1  jtc 
    362  1.1  jtc   SH_VA_START(va, fmt);
    363  1.1  jtc 
    364  1.1  jtc   vfptreef(shf, indent, fmt, va);
    365  1.1  jtc   va_end(va);
    366  1.1  jtc   return 0;
    367  1.1  jtc }
    368  1.1  jtc 
    369  1.1  jtc /* VARARGS */
    370  1.1  jtc char *
    371  1.1  jtc #ifdef HAVE_PROTOTYPES
    372  1.1  jtc snptreef(char *s, int n, const char *fmt, ...)
    373  1.1  jtc #else
    374  1.1  jtc snptreef(s, n, fmt, va_alist)
    375  1.1  jtc   char *s;
    376  1.1  jtc   int n;
    377  1.1  jtc   const char *fmt;
    378  1.1  jtc   va_dcl
    379  1.1  jtc #endif
    380  1.1  jtc {
    381  1.1  jtc   va_list va;
    382  1.1  jtc   struct shf shf;
    383  1.1  jtc 
    384  1.1  jtc   shf_sopen(s, n, SHF_WR | (s ? 0 : SHF_DYNAMIC), &shf);
    385  1.1  jtc 
    386  1.1  jtc   SH_VA_START(va, fmt);
    387  1.1  jtc   vfptreef(&shf, 0, fmt, va);
    388  1.1  jtc   va_end(va);
    389  1.1  jtc 
    390  1.1  jtc   return shf_sclose(&shf); /* null terminates */
    391  1.1  jtc }
    392  1.1  jtc 
    393  1.1  jtc static void
    394  1.1  jtc vfptreef(shf, indent, fmt, va)
    395  1.1  jtc 	register struct shf *shf;
    396  1.1  jtc 	int indent;
    397  1.1  jtc 	const char *fmt;
    398  1.1  jtc 	register va_list va;
    399  1.1  jtc {
    400  1.1  jtc 	register int c;
    401  1.1  jtc 
    402  1.1  jtc 	while ((c = *fmt++))
    403  1.1  jtc 	    if (c == '%') {
    404  1.1  jtc 		register long n;
    405  1.1  jtc 		register char *p;
    406  1.1  jtc 		int neg;
    407  1.1  jtc 
    408  1.1  jtc 		switch ((c = *fmt++)) {
    409  1.1  jtc 		  case 'c':
    410  1.1  jtc 			tputc(va_arg(va, int), shf);
    411  1.1  jtc 			break;
    412  1.1  jtc 		  case 's':
    413  1.1  jtc 			p = va_arg(va, char *);
    414  1.1  jtc 			while (*p)
    415  1.1  jtc 				tputc(*p++, shf);
    416  1.1  jtc 			break;
    417  1.1  jtc 		  case 'S':	/* word */
    418  1.1  jtc 			p = va_arg(va, char *);
    419  1.1  jtc 			tputS(p, shf);
    420  1.1  jtc 			break;
    421  1.1  jtc 		  case 'd': case 'u': /* decimal */
    422  1.1  jtc 			n = (c == 'd') ? va_arg(va, int)
    423  1.1  jtc 				       : va_arg(va, unsigned int);
    424  1.1  jtc 			neg = c=='d' && n<0;
    425  1.1  jtc 			p = ulton((neg) ? -n : n, 10);
    426  1.1  jtc 			if (neg)
    427  1.1  jtc 				*--p = '-';
    428  1.1  jtc 			while (*p)
    429  1.1  jtc 				tputc(*p++, shf);
    430  1.1  jtc 			break;
    431  1.1  jtc 		  case 'T':	/* format tree */
    432  1.1  jtc 			ptree(va_arg(va, struct op *), indent, shf);
    433  1.1  jtc 			break;
    434  1.1  jtc 		  case ';':	/* newline or ; */
    435  1.1  jtc 		  case 'N':	/* newline or space */
    436  1.1  jtc 			if (shf->flags & SHF_STRING) {
    437  1.1  jtc 				if (c == ';')
    438  1.1  jtc 					tputc(';', shf);
    439  1.1  jtc 				tputc(' ', shf);
    440  1.1  jtc 			} else {
    441  1.1  jtc 				int i;
    442  1.1  jtc 
    443  1.1  jtc 				tputc('\n', shf);
    444  1.1  jtc 				for (i = indent; i >= 8; i -= 8)
    445  1.1  jtc 					tputc('\t', shf);
    446  1.1  jtc 				for (; i > 0; --i)
    447  1.1  jtc 					tputc(' ', shf);
    448  1.1  jtc 			}
    449  1.1  jtc 			break;
    450  1.1  jtc 		  case 'R':
    451  1.1  jtc 			pioact(shf, indent, va_arg(va, struct ioword *));
    452  1.1  jtc 			break;
    453  1.1  jtc 		  default:
    454  1.1  jtc 			tputc(c, shf);
    455  1.1  jtc 			break;
    456  1.1  jtc 		}
    457  1.1  jtc 	    } else
    458  1.1  jtc 		tputc(c, shf);
    459  1.1  jtc }
    460  1.1  jtc 
    461  1.1  jtc /*
    462  1.1  jtc  * copy tree (for function definition)
    463  1.1  jtc  */
    464  1.1  jtc 
    465  1.1  jtc struct op *
    466  1.1  jtc tcopy(t, ap)
    467  1.1  jtc 	register struct op *t;
    468  1.1  jtc 	Area *ap;
    469  1.1  jtc {
    470  1.1  jtc 	register struct op *r;
    471  1.1  jtc 	register char **tw, **rw;
    472  1.1  jtc 
    473  1.1  jtc 	if (t == NULL)
    474  1.1  jtc 		return NULL;
    475  1.1  jtc 
    476  1.1  jtc 	r = (struct op *) alloc(sizeof(struct op), ap);
    477  1.1  jtc 
    478  1.1  jtc 	r->type = t->type;
    479  1.1  jtc 	r->u.evalflags = t->u.evalflags;
    480  1.1  jtc 
    481  1.1  jtc 	r->str = t->type == TCASE ? wdcopy(t->str, ap) : str_save(t->str, ap);
    482  1.1  jtc 
    483  1.1  jtc 	if (t->vars == NULL)
    484  1.1  jtc 		r->vars = NULL;
    485  1.1  jtc 	else {
    486  1.1  jtc 		for (tw = t->vars; *tw++ != NULL; )
    487  1.1  jtc 			;
    488  1.1  jtc 		rw = r->vars = (char **)
    489  1.1  jtc 			alloc((int)(tw - t->vars) * sizeof(*tw), ap);
    490  1.1  jtc 		for (tw = t->vars; *tw != NULL; )
    491  1.1  jtc 			*rw++ = wdcopy(*tw++, ap);
    492  1.1  jtc 		*rw = NULL;
    493  1.1  jtc 	}
    494  1.1  jtc 
    495  1.1  jtc 	if (t->args == NULL)
    496  1.1  jtc 		r->args = NULL;
    497  1.1  jtc 	else {
    498  1.1  jtc 		for (tw = t->args; *tw++ != NULL; )
    499  1.1  jtc 			;
    500  1.1  jtc 		rw = r->args = (char **)
    501  1.1  jtc 			alloc((int)(tw - t->args) * sizeof(*tw), ap);
    502  1.1  jtc 		for (tw = t->args; *tw != NULL; )
    503  1.1  jtc 			*rw++ = wdcopy(*tw++, ap);
    504  1.1  jtc 		*rw = NULL;
    505  1.1  jtc 	}
    506  1.1  jtc 
    507  1.1  jtc 	r->ioact = (t->ioact == NULL) ? NULL : iocopy(t->ioact, ap);
    508  1.1  jtc 
    509  1.1  jtc 	r->left = tcopy(t->left, ap);
    510  1.1  jtc 	r->right = tcopy(t->right, ap);
    511  1.1  jtc 
    512  1.1  jtc 	return r;
    513  1.1  jtc }
    514  1.1  jtc 
    515  1.1  jtc char *
    516  1.1  jtc wdcopy(wp, ap)
    517  1.1  jtc 	const char *wp;
    518  1.1  jtc 	Area *ap;
    519  1.1  jtc {
    520  1.1  jtc 	size_t len = wdscan(wp, EOS) - wp;
    521  1.1  jtc 	return memcpy(alloc(len, ap), wp, len);
    522  1.1  jtc }
    523  1.1  jtc 
    524  1.1  jtc /* return the position of prefix c in wp plus 1 */
    525  1.1  jtc char *
    526  1.1  jtc wdscan(wp, c)
    527  1.1  jtc 	register const char *wp;
    528  1.1  jtc 	register int c;
    529  1.1  jtc {
    530  1.1  jtc 	register int nest = 0;
    531  1.1  jtc 
    532  1.1  jtc 	while (1)
    533  1.1  jtc 		switch (*wp++) {
    534  1.1  jtc 		  case EOS:
    535  1.1  jtc 			return (char *) wp;
    536  1.1  jtc 		  case CHAR:
    537  1.1  jtc 		  case QCHAR:
    538  1.1  jtc 			wp++;
    539  1.1  jtc 			break;
    540  1.1  jtc 		  case COMSUB:
    541  1.1  jtc 		  case EXPRSUB:
    542  1.1  jtc 			while (*wp++ != 0)
    543  1.1  jtc 				;
    544  1.1  jtc 			break;
    545  1.1  jtc 		  case OQUOTE:
    546  1.1  jtc 		  case CQUOTE:
    547  1.1  jtc 			break;
    548  1.1  jtc 		  case OSUBST:
    549  1.1  jtc 			nest++;
    550  1.1  jtc 			while (*wp++ != '\0')
    551  1.1  jtc 				;
    552  1.1  jtc 			break;
    553  1.1  jtc 		  case CSUBST:
    554  1.1  jtc 			if (c == CSUBST && nest == 0)
    555  1.1  jtc 				return (char *) wp;
    556  1.1  jtc 			nest--;
    557  1.1  jtc 			break;
    558  1.1  jtc #ifdef KSH
    559  1.1  jtc 		  case OPAT:
    560  1.1  jtc 			nest++;
    561  1.1  jtc 			wp++;
    562  1.1  jtc 			break;
    563  1.1  jtc 		  case SPAT:
    564  1.1  jtc 		  case CPAT:
    565  1.1  jtc 			if (c == wp[-1] && nest == 0)
    566  1.1  jtc 				return (char *) wp;
    567  1.1  jtc 			if (wp[-1] == CPAT)
    568  1.1  jtc 				nest--;
    569  1.1  jtc 			break;
    570  1.1  jtc #endif /* KSH */
    571  1.1  jtc 		}
    572  1.1  jtc }
    573  1.1  jtc 
    574  1.1  jtc static	struct ioword **
    575  1.1  jtc iocopy(iow, ap)
    576  1.1  jtc 	register struct ioword **iow;
    577  1.1  jtc 	Area *ap;
    578  1.1  jtc {
    579  1.1  jtc 	register struct ioword **ior;
    580  1.1  jtc 	register int i;
    581  1.1  jtc 
    582  1.1  jtc 	for (ior = iow; *ior++ != NULL; )
    583  1.1  jtc 		;
    584  1.1  jtc 	ior = (struct ioword **) alloc((int)(ior - iow) * sizeof(*ior), ap);
    585  1.1  jtc 
    586  1.1  jtc 	for (i = 0; iow[i] != NULL; i++) {
    587  1.1  jtc 		register struct ioword *p, *q;
    588  1.1  jtc 
    589  1.1  jtc 		p = iow[i];
    590  1.1  jtc 		q = (struct ioword *) alloc(sizeof(*p), ap);
    591  1.1  jtc 		ior[i] = q;
    592  1.1  jtc 		*q = *p;
    593  1.1  jtc 		if (p->name != (char *) 0)
    594  1.1  jtc 			q->name = wdcopy(p->name, ap);
    595  1.1  jtc 		if (p->delim != (char *) 0)
    596  1.1  jtc 			q->delim = wdcopy(p->delim, ap);
    597  1.1  jtc 	}
    598  1.1  jtc 	ior[i] = NULL;
    599  1.1  jtc 
    600  1.1  jtc 	return ior;
    601  1.1  jtc }
    602  1.1  jtc 
    603  1.1  jtc /*
    604  1.1  jtc  * free tree (for function definition)
    605  1.1  jtc  */
    606  1.1  jtc 
    607  1.1  jtc void
    608  1.1  jtc tfree(t, ap)
    609  1.1  jtc 	register struct op *t;
    610  1.1  jtc 	Area *ap;
    611  1.1  jtc {
    612  1.1  jtc 	register char **w;
    613  1.1  jtc 
    614  1.1  jtc 	if (t == NULL)
    615  1.1  jtc 		return;
    616  1.1  jtc 
    617  1.1  jtc 	if (t->str != NULL)
    618  1.1  jtc 		afree((void*)t->str, ap);
    619  1.1  jtc 
    620  1.1  jtc 	if (t->vars != NULL) {
    621  1.1  jtc 		for (w = t->vars; *w != NULL; w++)
    622  1.1  jtc 			afree((void*)*w, ap);
    623  1.1  jtc 		afree((void*)t->vars, ap);
    624  1.1  jtc 	}
    625  1.1  jtc 
    626  1.1  jtc 	if (t->args != NULL) {
    627  1.1  jtc 		for (w = t->args; *w != NULL; w++)
    628  1.1  jtc 			afree((void*)*w, ap);
    629  1.1  jtc 		afree((void*)t->args, ap);
    630  1.1  jtc 	}
    631  1.1  jtc 
    632  1.1  jtc 	if (t->ioact != NULL)
    633  1.1  jtc 		iofree(t->ioact, ap);
    634  1.1  jtc 
    635  1.1  jtc 	tfree(t->left, ap);
    636  1.1  jtc 	tfree(t->right, ap);
    637  1.1  jtc 
    638  1.1  jtc 	afree((void*)t, ap);
    639  1.1  jtc }
    640  1.1  jtc 
    641  1.1  jtc static	void
    642  1.1  jtc iofree(iow, ap)
    643  1.1  jtc 	struct ioword **iow;
    644  1.1  jtc 	Area *ap;
    645  1.1  jtc {
    646  1.1  jtc 	register struct ioword **iop;
    647  1.1  jtc 	register struct ioword *p;
    648  1.1  jtc 
    649  1.1  jtc 	for (iop = iow; (p = *iop++) != NULL; ) {
    650  1.1  jtc 		if (p->name != NULL)
    651  1.1  jtc 			afree((void*)p->name, ap);
    652  1.1  jtc 		afree((void*)p, ap);
    653  1.1  jtc 	}
    654  1.1  jtc }
    655