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