1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23 /* All Rights Reserved */ 24 25 26 /* 27 * Copyright (c) 1996, by Sun Microsystems, Inc. 28 * All rights reserved. 29 */ 30 31 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.4.1.3 */ 32 /* 33 * UNIX shell 34 */ 35 36 #include "defs.h" 37 38 freefunc(n) 39 struct namnod *n; 40 { 41 freetree((struct trenod *)(n->namenv)); 42 } 43 44 45 freetree(t) 46 register struct trenod *t; 47 { 48 if (t) 49 { 50 register int type; 51 52 if (t->tretyp & CNTMSK) 53 { 54 t->tretyp--; 55 return; 56 } 57 58 type = t->tretyp & COMMSK; 59 60 switch (type) 61 { 62 case TFND: 63 free(fndptr(t)->fndnam); 64 freetree(fndptr(t)->fndval); 65 break; 66 67 case TCOM: 68 freeio(comptr(t)->comio); 69 free_arg(comptr(t)->comarg); 70 free_arg(comptr(t)->comset); 71 break; 72 73 case TFORK: 74 freeio(forkptr(t)->forkio); 75 freetree(forkptr(t)->forktre); 76 break; 77 78 case TPAR: 79 freetree(parptr(t)->partre); 80 break; 81 82 case TFIL: 83 case TLST: 84 case TAND: 85 case TORF: 86 freetree(lstptr(t)->lstlef); 87 freetree(lstptr(t)->lstrit); 88 break; 89 90 case TFOR: 91 { 92 struct fornod *f = (struct fornod *)t; 93 94 free(f->fornam); 95 freetree(f->fortre); 96 if (f->forlst) 97 { 98 freeio(f->forlst->comio); 99 free_arg(f->forlst->comarg); 100 free_arg(f->forlst->comset); 101 free(f->forlst); 102 } 103 } 104 break; 105 106 case TWH: 107 case TUN: 108 freetree(whptr(t)->whtre); 109 freetree(whptr(t)->dotre); 110 break; 111 112 case TIF: 113 freetree(ifptr(t)->iftre); 114 freetree(ifptr(t)->thtre); 115 freetree(ifptr(t)->eltre); 116 break; 117 118 case TSW: 119 free(swptr(t)->swarg); 120 freereg(swptr(t)->swlst); 121 break; 122 } 123 free(t); 124 } 125 } 126 127 free_arg(argp) 128 register struct argnod *argp; 129 { 130 register struct argnod *sav; 131 132 while (argp) 133 { 134 sav = argp->argnxt; 135 free(argp); 136 argp = sav; 137 } 138 } 139 140 141 freeio(iop) 142 register struct ionod *iop; 143 { 144 register struct ionod *sav; 145 146 while (iop) 147 { 148 if (iop->iofile & IODOC) 149 { 150 151 #ifdef DEBUG 152 prs("unlinking "); 153 prs(iop->ioname); 154 newline(); 155 #endif 156 157 unlink(iop->ioname); 158 159 if (fiotemp == iop) 160 fiotemp = iop->iolst; 161 else 162 { 163 struct ionod *fiop = fiotemp; 164 165 while (fiop->iolst != iop) 166 fiop = fiop->iolst; 167 168 fiop->iolst = iop->iolst; 169 } 170 } 171 free(iop->ioname); 172 free(iop->iolink); 173 sav = iop->ionxt; 174 free(iop); 175 iop = sav; 176 } 177 } 178 179 180 freereg(regp) 181 register struct regnod *regp; 182 { 183 register struct regnod *sav; 184 185 while (regp) 186 { 187 free_arg(regp->regptr); 188 freetree(regp->regcom); 189 sav = regp->regnxt; 190 free(regp); 191 regp = sav; 192 } 193 } 194 195 196 static nonl = 0; 197 198 prbgnlst() 199 { 200 if (nonl) 201 prc_buff(SPACE); 202 else 203 prc_buff(NL); 204 } 205 206 prendlst() 207 { 208 if (nonl) { 209 prc_buff(';'); 210 prc_buff(SPACE); 211 } 212 else 213 prc_buff(NL); 214 } 215 216 prcmd(t) 217 { 218 nonl++; 219 prf(t); 220 nonl = 0; 221 } 222 223 prf(t) 224 register struct trenod *t; 225 { 226 sigchk(); 227 228 if (t) 229 { 230 register int type; 231 232 type = t->tretyp & COMMSK; 233 234 switch(type) 235 { 236 case TFND: 237 { 238 register struct fndnod *f = (struct fndnod *)t; 239 240 prs_buff(f->fndnam); 241 prs_buff("(){"); 242 prbgnlst(); 243 prf(f->fndval); 244 prbgnlst(); 245 prs_buff("}"); 246 break; 247 } 248 249 case TCOM: 250 if (comptr(t)->comset) { 251 prarg(comptr(t)->comset); 252 prc_buff(SPACE); 253 } 254 prarg(comptr(t)->comarg); 255 prio(comptr(t)->comio); 256 break; 257 258 case TFORK: 259 prf(forkptr(t)->forktre); 260 prio(forkptr(t)->forkio); 261 if (forkptr(t)->forktyp & FAMP) 262 prs_buff(" &"); 263 break; 264 265 case TPAR: 266 prs_buff("("); 267 prf(parptr(t)->partre); 268 prs_buff(")"); 269 break; 270 271 case TFIL: 272 prf(lstptr(t)->lstlef); 273 prs_buff(" | "); 274 prf(lstptr(t)->lstrit); 275 break; 276 277 case TLST: 278 prf(lstptr(t)->lstlef); 279 prendlst(); 280 prf(lstptr(t)->lstrit); 281 break; 282 283 case TAND: 284 prf(lstptr(t)->lstlef); 285 prs_buff(" && "); 286 prf(lstptr(t)->lstrit); 287 break; 288 289 case TORF: 290 prf(lstptr(t)->lstlef); 291 prs_buff(" || "); 292 prf(lstptr(t)->lstrit); 293 break; 294 295 case TFOR: 296 { 297 register struct argnod *arg; 298 register struct fornod *f = (struct fornod *)t; 299 300 prs_buff("for "); 301 prs_buff(f->fornam); 302 303 if (f->forlst) 304 { 305 arg = f->forlst->comarg; 306 prs_buff(" in"); 307 308 while(arg != ENDARGS) 309 { 310 prc_buff(SPACE); 311 prs_buff(arg->argval); 312 arg = arg->argnxt; 313 } 314 } 315 316 prendlst(); 317 prs_buff("do"); 318 prbgnlst(); 319 prf(f->fortre); 320 prendlst(); 321 prs_buff("done"); 322 } 323 break; 324 325 case TWH: 326 case TUN: 327 if (type == TWH) 328 prs_buff("while "); 329 else 330 prs_buff("until "); 331 prf(whptr(t)->whtre); 332 prendlst(); 333 prs_buff("do"); 334 prbgnlst(); 335 prf(whptr(t)->dotre); 336 prendlst(); 337 prs_buff("done"); 338 break; 339 340 case TIF: 341 { 342 struct ifnod *f = (struct ifnod *)t; 343 344 prs_buff("if "); 345 prf(f->iftre); 346 prendlst(); 347 prs_buff("then"); 348 prendlst(); 349 prf(f->thtre); 350 351 if (f->eltre) 352 { 353 prendlst(); 354 prs_buff("else"); 355 prendlst(); 356 prf(f->eltre); 357 } 358 359 prendlst(); 360 prs_buff("fi"); 361 break; 362 } 363 364 case TSW: 365 { 366 register struct regnod *swl; 367 368 prs_buff("case "); 369 prs_buff(swptr(t)->swarg); 370 371 swl = swptr(t)->swlst; 372 while(swl) 373 { 374 struct argnod *arg = swl->regptr; 375 376 if (arg) 377 { 378 prs_buff(arg->argval); 379 arg = arg->argnxt; 380 } 381 382 while(arg) 383 { 384 prs_buff(" | "); 385 prs_buff(arg->argval); 386 arg = arg->argnxt; 387 } 388 389 prs_buff(")"); 390 prf(swl->regcom); 391 prs_buff(";;"); 392 swl = swl->regnxt; 393 } 394 } 395 break; 396 } 397 } 398 399 sigchk(); 400 } 401 402 prarg(argp) 403 register struct argnod *argp; 404 { 405 while (argp) 406 { 407 prs_buff(argp->argval); 408 argp=argp->argnxt; 409 if (argp) 410 prc_buff(SPACE); 411 } 412 } 413 414 415 prio(iop) 416 register struct ionod *iop; 417 { 418 register int iof; 419 register unsigned char *ion; 420 421 while (iop) 422 { 423 iof = iop->iofile; 424 ion = (unsigned char *) iop->ioname; 425 426 if (*ion) 427 { 428 prc_buff(SPACE); 429 430 prn_buff(iof & IOUFD); 431 432 if (iof & IODOC) 433 prs_buff("<<"); 434 else if (iof & IOMOV) 435 { 436 if (iof & IOPUT) 437 prs_buff(">&"); 438 else 439 prs_buff("<&"); 440 441 } 442 else if ((iof & IOPUT) == 0) 443 prc_buff('<'); 444 else if (iof & IOAPP) 445 prs_buff(">>"); 446 else 447 prc_buff('>'); 448 449 prs_buff(ion); 450 } 451 iop = iop->ionxt; 452 } 453 } 454