10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*9369SNobutomo.Nakano@Sun.COM * Common Development and Distribution License (the "License"). 6*9369SNobutomo.Nakano@Sun.COM * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21527Schin 22527Schin /* 23*9369SNobutomo.Nakano@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24527Schin * Use is subject to license terms. 25527Schin */ 26527Schin 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * UNIX shell 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include "defs.h" 350Sstevel@tonic-gate 36527Schin static void free_arg(struct argnod *); 37527Schin static void freeio(struct ionod *); 38527Schin static void freereg(struct regnod *); 39527Schin static void prarg(struct argnod *argp); 40527Schin static void prio(struct ionod *iop); 41527Schin 42527Schin void freefunc(struct namnod * n)43527Schinfreefunc(struct namnod *n) 440Sstevel@tonic-gate { 450Sstevel@tonic-gate freetree((struct trenod *)(n->namenv)); 460Sstevel@tonic-gate } 470Sstevel@tonic-gate 48*9369SNobutomo.Nakano@Sun.COM void freetree(struct trenod * t)49527Schinfreetree(struct trenod *t) 500Sstevel@tonic-gate { 510Sstevel@tonic-gate if (t) 520Sstevel@tonic-gate { 53527Schin int type; 540Sstevel@tonic-gate 550Sstevel@tonic-gate type = t->tretyp & COMMSK; 560Sstevel@tonic-gate 570Sstevel@tonic-gate switch (type) 580Sstevel@tonic-gate { 59*9369SNobutomo.Nakano@Sun.COM case TFND: { 60*9369SNobutomo.Nakano@Sun.COM struct fndnod *f = fndptr(t); 61*9369SNobutomo.Nakano@Sun.COM 62*9369SNobutomo.Nakano@Sun.COM if (f->fndref > 0) { 63*9369SNobutomo.Nakano@Sun.COM f->fndref--; 64*9369SNobutomo.Nakano@Sun.COM return; 65*9369SNobutomo.Nakano@Sun.COM } 66*9369SNobutomo.Nakano@Sun.COM free(f->fndnam); 67*9369SNobutomo.Nakano@Sun.COM freetree(f->fndval); 680Sstevel@tonic-gate break; 69*9369SNobutomo.Nakano@Sun.COM } 700Sstevel@tonic-gate 710Sstevel@tonic-gate case TCOM: 720Sstevel@tonic-gate freeio(comptr(t)->comio); 730Sstevel@tonic-gate free_arg(comptr(t)->comarg); 740Sstevel@tonic-gate free_arg(comptr(t)->comset); 750Sstevel@tonic-gate break; 760Sstevel@tonic-gate 770Sstevel@tonic-gate case TFORK: 780Sstevel@tonic-gate freeio(forkptr(t)->forkio); 790Sstevel@tonic-gate freetree(forkptr(t)->forktre); 800Sstevel@tonic-gate break; 810Sstevel@tonic-gate 820Sstevel@tonic-gate case TPAR: 830Sstevel@tonic-gate freetree(parptr(t)->partre); 840Sstevel@tonic-gate break; 850Sstevel@tonic-gate 860Sstevel@tonic-gate case TFIL: 870Sstevel@tonic-gate case TLST: 880Sstevel@tonic-gate case TAND: 890Sstevel@tonic-gate case TORF: 900Sstevel@tonic-gate freetree(lstptr(t)->lstlef); 910Sstevel@tonic-gate freetree(lstptr(t)->lstrit); 920Sstevel@tonic-gate break; 930Sstevel@tonic-gate 940Sstevel@tonic-gate case TFOR: 950Sstevel@tonic-gate { 960Sstevel@tonic-gate struct fornod *f = (struct fornod *)t; 970Sstevel@tonic-gate 980Sstevel@tonic-gate free(f->fornam); 990Sstevel@tonic-gate freetree(f->fortre); 1000Sstevel@tonic-gate if (f->forlst) 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate freeio(f->forlst->comio); 1030Sstevel@tonic-gate free_arg(f->forlst->comarg); 1040Sstevel@tonic-gate free_arg(f->forlst->comset); 1050Sstevel@tonic-gate free(f->forlst); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate break; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate case TWH: 1110Sstevel@tonic-gate case TUN: 1120Sstevel@tonic-gate freetree(whptr(t)->whtre); 1130Sstevel@tonic-gate freetree(whptr(t)->dotre); 1140Sstevel@tonic-gate break; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate case TIF: 1170Sstevel@tonic-gate freetree(ifptr(t)->iftre); 1180Sstevel@tonic-gate freetree(ifptr(t)->thtre); 1190Sstevel@tonic-gate freetree(ifptr(t)->eltre); 1200Sstevel@tonic-gate break; 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate case TSW: 1230Sstevel@tonic-gate free(swptr(t)->swarg); 1240Sstevel@tonic-gate freereg(swptr(t)->swlst); 1250Sstevel@tonic-gate break; 1260Sstevel@tonic-gate } 1270Sstevel@tonic-gate free(t); 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 131527Schin static void free_arg(struct argnod * argp)132527Schinfree_arg(struct argnod *argp) 1330Sstevel@tonic-gate { 134527Schin struct argnod *sav; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate while (argp) 1370Sstevel@tonic-gate { 1380Sstevel@tonic-gate sav = argp->argnxt; 1390Sstevel@tonic-gate free(argp); 1400Sstevel@tonic-gate argp = sav; 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 144527Schin void freeio(struct ionod * iop)145527Schinfreeio(struct ionod *iop) 1460Sstevel@tonic-gate { 147527Schin struct ionod *sav; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate while (iop) 1500Sstevel@tonic-gate { 1510Sstevel@tonic-gate if (iop->iofile & IODOC) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate #ifdef DEBUG 1550Sstevel@tonic-gate prs("unlinking "); 1560Sstevel@tonic-gate prs(iop->ioname); 1570Sstevel@tonic-gate newline(); 1580Sstevel@tonic-gate #endif 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate unlink(iop->ioname); 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate if (fiotemp == iop) 1630Sstevel@tonic-gate fiotemp = iop->iolst; 1640Sstevel@tonic-gate else 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate struct ionod *fiop = fiotemp; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate while (fiop->iolst != iop) 1690Sstevel@tonic-gate fiop = fiop->iolst; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate fiop->iolst = iop->iolst; 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate free(iop->ioname); 1750Sstevel@tonic-gate free(iop->iolink); 1760Sstevel@tonic-gate sav = iop->ionxt; 1770Sstevel@tonic-gate free(iop); 1780Sstevel@tonic-gate iop = sav; 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate } 1810Sstevel@tonic-gate 182527Schin static void freereg(struct regnod * regp)183527Schinfreereg(struct regnod *regp) 1840Sstevel@tonic-gate { 185527Schin struct regnod *sav; 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate while (regp) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate free_arg(regp->regptr); 1900Sstevel@tonic-gate freetree(regp->regcom); 1910Sstevel@tonic-gate sav = regp->regnxt; 1920Sstevel@tonic-gate free(regp); 1930Sstevel@tonic-gate regp = sav; 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate 198527Schin static int nonl = 0; 1990Sstevel@tonic-gate 200527Schin void prbgnlst(void)201527Schinprbgnlst(void) 2020Sstevel@tonic-gate { 2030Sstevel@tonic-gate if (nonl) 2040Sstevel@tonic-gate prc_buff(SPACE); 2050Sstevel@tonic-gate else 2060Sstevel@tonic-gate prc_buff(NL); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 209527Schin void prendlst(void)210527Schinprendlst(void) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate if (nonl) { 2130Sstevel@tonic-gate prc_buff(';'); 2140Sstevel@tonic-gate prc_buff(SPACE); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate else 2170Sstevel@tonic-gate prc_buff(NL); 2180Sstevel@tonic-gate } 2190Sstevel@tonic-gate 220527Schin void prcmd(struct trenod * t)221527Schinprcmd(struct trenod *t) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate nonl++; 2240Sstevel@tonic-gate prf(t); 2250Sstevel@tonic-gate nonl = 0; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 228527Schin void prf(struct trenod * t)229527Schinprf(struct trenod *t) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate sigchk(); 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (t) 2340Sstevel@tonic-gate { 235527Schin int type; 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate type = t->tretyp & COMMSK; 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate switch(type) 2400Sstevel@tonic-gate { 2410Sstevel@tonic-gate case TFND: 2420Sstevel@tonic-gate { 243527Schin struct fndnod *f = (struct fndnod *)t; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate prs_buff(f->fndnam); 2460Sstevel@tonic-gate prs_buff("(){"); 2470Sstevel@tonic-gate prbgnlst(); 2480Sstevel@tonic-gate prf(f->fndval); 2490Sstevel@tonic-gate prbgnlst(); 2500Sstevel@tonic-gate prs_buff("}"); 2510Sstevel@tonic-gate break; 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate case TCOM: 2550Sstevel@tonic-gate if (comptr(t)->comset) { 2560Sstevel@tonic-gate prarg(comptr(t)->comset); 2570Sstevel@tonic-gate prc_buff(SPACE); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate prarg(comptr(t)->comarg); 2600Sstevel@tonic-gate prio(comptr(t)->comio); 2610Sstevel@tonic-gate break; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate case TFORK: 2640Sstevel@tonic-gate prf(forkptr(t)->forktre); 2650Sstevel@tonic-gate prio(forkptr(t)->forkio); 2660Sstevel@tonic-gate if (forkptr(t)->forktyp & FAMP) 2670Sstevel@tonic-gate prs_buff(" &"); 2680Sstevel@tonic-gate break; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate case TPAR: 2710Sstevel@tonic-gate prs_buff("("); 2720Sstevel@tonic-gate prf(parptr(t)->partre); 2730Sstevel@tonic-gate prs_buff(")"); 2740Sstevel@tonic-gate break; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate case TFIL: 2770Sstevel@tonic-gate prf(lstptr(t)->lstlef); 2780Sstevel@tonic-gate prs_buff(" | "); 2790Sstevel@tonic-gate prf(lstptr(t)->lstrit); 2800Sstevel@tonic-gate break; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate case TLST: 2830Sstevel@tonic-gate prf(lstptr(t)->lstlef); 2840Sstevel@tonic-gate prendlst(); 2850Sstevel@tonic-gate prf(lstptr(t)->lstrit); 2860Sstevel@tonic-gate break; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate case TAND: 2890Sstevel@tonic-gate prf(lstptr(t)->lstlef); 2900Sstevel@tonic-gate prs_buff(" && "); 2910Sstevel@tonic-gate prf(lstptr(t)->lstrit); 2920Sstevel@tonic-gate break; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate case TORF: 2950Sstevel@tonic-gate prf(lstptr(t)->lstlef); 2960Sstevel@tonic-gate prs_buff(" || "); 2970Sstevel@tonic-gate prf(lstptr(t)->lstrit); 2980Sstevel@tonic-gate break; 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate case TFOR: 3010Sstevel@tonic-gate { 302527Schin struct argnod *arg; 303527Schin struct fornod *f = (struct fornod *)t; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate prs_buff("for "); 3060Sstevel@tonic-gate prs_buff(f->fornam); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (f->forlst) 3090Sstevel@tonic-gate { 3100Sstevel@tonic-gate arg = f->forlst->comarg; 3110Sstevel@tonic-gate prs_buff(" in"); 3120Sstevel@tonic-gate 3130Sstevel@tonic-gate while(arg != ENDARGS) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate prc_buff(SPACE); 3160Sstevel@tonic-gate prs_buff(arg->argval); 3170Sstevel@tonic-gate arg = arg->argnxt; 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate prendlst(); 3220Sstevel@tonic-gate prs_buff("do"); 3230Sstevel@tonic-gate prbgnlst(); 3240Sstevel@tonic-gate prf(f->fortre); 3250Sstevel@tonic-gate prendlst(); 3260Sstevel@tonic-gate prs_buff("done"); 3270Sstevel@tonic-gate } 3280Sstevel@tonic-gate break; 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate case TWH: 3310Sstevel@tonic-gate case TUN: 3320Sstevel@tonic-gate if (type == TWH) 3330Sstevel@tonic-gate prs_buff("while "); 3340Sstevel@tonic-gate else 3350Sstevel@tonic-gate prs_buff("until "); 3360Sstevel@tonic-gate prf(whptr(t)->whtre); 3370Sstevel@tonic-gate prendlst(); 3380Sstevel@tonic-gate prs_buff("do"); 3390Sstevel@tonic-gate prbgnlst(); 3400Sstevel@tonic-gate prf(whptr(t)->dotre); 3410Sstevel@tonic-gate prendlst(); 3420Sstevel@tonic-gate prs_buff("done"); 3430Sstevel@tonic-gate break; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate case TIF: 3460Sstevel@tonic-gate { 3470Sstevel@tonic-gate struct ifnod *f = (struct ifnod *)t; 3480Sstevel@tonic-gate 3490Sstevel@tonic-gate prs_buff("if "); 3500Sstevel@tonic-gate prf(f->iftre); 3510Sstevel@tonic-gate prendlst(); 3520Sstevel@tonic-gate prs_buff("then"); 3530Sstevel@tonic-gate prendlst(); 3540Sstevel@tonic-gate prf(f->thtre); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate if (f->eltre) 3570Sstevel@tonic-gate { 3580Sstevel@tonic-gate prendlst(); 3590Sstevel@tonic-gate prs_buff("else"); 3600Sstevel@tonic-gate prendlst(); 3610Sstevel@tonic-gate prf(f->eltre); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate prendlst(); 3650Sstevel@tonic-gate prs_buff("fi"); 3660Sstevel@tonic-gate break; 3670Sstevel@tonic-gate } 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate case TSW: 3700Sstevel@tonic-gate { 371527Schin struct regnod *swl; 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate prs_buff("case "); 3740Sstevel@tonic-gate prs_buff(swptr(t)->swarg); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate swl = swptr(t)->swlst; 3770Sstevel@tonic-gate while(swl) 3780Sstevel@tonic-gate { 3790Sstevel@tonic-gate struct argnod *arg = swl->regptr; 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate if (arg) 3820Sstevel@tonic-gate { 3830Sstevel@tonic-gate prs_buff(arg->argval); 3840Sstevel@tonic-gate arg = arg->argnxt; 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate while(arg) 3880Sstevel@tonic-gate { 3890Sstevel@tonic-gate prs_buff(" | "); 3900Sstevel@tonic-gate prs_buff(arg->argval); 3910Sstevel@tonic-gate arg = arg->argnxt; 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate prs_buff(")"); 3950Sstevel@tonic-gate prf(swl->regcom); 3960Sstevel@tonic-gate prs_buff(";;"); 3970Sstevel@tonic-gate swl = swl->regnxt; 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate break; 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate sigchk(); 4050Sstevel@tonic-gate } 4060Sstevel@tonic-gate 407527Schin static void prarg(struct argnod * argp)408527Schinprarg(struct argnod *argp) 4090Sstevel@tonic-gate { 4100Sstevel@tonic-gate while (argp) 4110Sstevel@tonic-gate { 4120Sstevel@tonic-gate prs_buff(argp->argval); 4130Sstevel@tonic-gate argp=argp->argnxt; 4140Sstevel@tonic-gate if (argp) 4150Sstevel@tonic-gate prc_buff(SPACE); 4160Sstevel@tonic-gate } 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 419527Schin static void prio(struct ionod * iop)420527Schinprio(struct ionod *iop) 4210Sstevel@tonic-gate { 422527Schin int iof; 423527Schin unsigned char *ion; 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate while (iop) 4260Sstevel@tonic-gate { 4270Sstevel@tonic-gate iof = iop->iofile; 4280Sstevel@tonic-gate ion = (unsigned char *) iop->ioname; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate if (*ion) 4310Sstevel@tonic-gate { 4320Sstevel@tonic-gate prc_buff(SPACE); 4330Sstevel@tonic-gate 4340Sstevel@tonic-gate prn_buff(iof & IOUFD); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate if (iof & IODOC) 4370Sstevel@tonic-gate prs_buff("<<"); 4380Sstevel@tonic-gate else if (iof & IOMOV) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate if (iof & IOPUT) 4410Sstevel@tonic-gate prs_buff(">&"); 4420Sstevel@tonic-gate else 4430Sstevel@tonic-gate prs_buff("<&"); 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate else if ((iof & IOPUT) == 0) 4470Sstevel@tonic-gate prc_buff('<'); 4480Sstevel@tonic-gate else if (iof & IOAPP) 4490Sstevel@tonic-gate prs_buff(">>"); 4500Sstevel@tonic-gate else 4510Sstevel@tonic-gate prc_buff('>'); 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate prs_buff(ion); 4540Sstevel@tonic-gate } 4550Sstevel@tonic-gate iop = iop->ionxt; 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate } 458