Print this page
8485 Remove set but unused variables in usr/src/cmd
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/sh/xec.c
+++ new/usr/src/cmd/sh/xec.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 + * Copyright 2017 Gary Mills
23 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27
27 28 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 29 /* All Rights Reserved */
29 30
30 31 /*
31 32 *
32 33 * UNIX shell
33 34 *
34 35 */
35 36
36 37
37 38 #include "defs.h"
38 39 #include <errno.h>
39 40 #include "sym.h"
40 41 #include "hash.h"
41 42 #include <sys/types.h>
42 43 #include <sys/times.h>
43 44
44 45 pid_t parent;
45 46
46 47 void execprint(unsigned char **);
47 48
48 49 /* ======== command execution ======== */
49 50
50 51 /*VARARGS3*/
51 52 int
52 53 execute(argt, xflags, errorflg, pf1, pf2)
53 54 struct trenod *argt;
54 55 int xflags, errorflg;
55 56 int *pf1, *pf2;
56 57 {
57 58 /*
58 59 * `stakbot' is preserved by this routine
59 60 */
60 61 struct trenod *t;
61 62 unsigned char *sav = savstak();
62 63
63 64 sigchk();
64 65 if (!errorflg)
65 66 flags &= ~errflg;
66 67
67 68 if ((t = argt) && execbrk == 0) {
68 69 int treeflgs;
69 70 unsigned char **com;
70 71 int type;
71 72 short pos;
72 73
73 74 treeflgs = t->tretyp;
74 75 type = treeflgs & COMMSK;
75 76
76 77 switch (type)
77 78 {
78 79 case TFND:
79 80 {
80 81 struct fndnod *f = fndptr(t);
81 82 struct namnod *n = lookup(f->fndnam);
82 83
83 84 exitval = 0;
84 85
85 86 if (n->namflg & N_RDONLY)
86 87 failed(n->namid, wtfailed);
87 88
88 89 if (flags & rshflg && (n == &pathnod ||
89 90 eq(n->namid, "SHELL")))
90 91 failed(n->namid, restricted);
91 92 /*
92 93 * If function of same name is previously
93 94 * defined, it will no longer be used.
94 95 */
95 96 if (n->namflg & N_FUNCTN) {
96 97 freefunc(n);
97 98 } else {
98 99 free(n->namval);
99 100 free(n->namenv);
100 101
101 102 n->namval = 0;
102 103 n->namflg &= ~(N_EXPORT | N_ENVCHG);
103 104 }
104 105 /*
105 106 * If function is defined within function,
106 107 * we don't want to free it along with the
107 108 * free of the defining function. If we are
108 109 * in a loop, fndnod may be reused, so it
109 110 * should never be freed.
110 111 */
111 112 if (funcnt != 0 || loopcnt != 0)
112 113 f->fndref++;
113 114
114 115 /*
115 116 * We hang a fndnod on the namenv so that
116 117 * ref cnt(fndref) can be increased while
↓ open down ↓ |
84 lines elided |
↑ open up ↑ |
117 118 * running in the function.
118 119 */
119 120 n->namenv = (unsigned char *)f;
120 121 attrib(n, N_FUNCTN);
121 122 hash_func(n->namid);
122 123 break;
123 124 }
124 125
125 126 case TCOM:
126 127 {
127 - unsigned char *a1, *name;
128 + unsigned char *name;
128 129 int argn, internal;
129 130 struct argnod *schain = gchain;
130 131 struct ionod *io = t->treio;
131 132 short cmdhash;
132 133 short comtype;
133 134
134 135 exitval = 0;
135 136
136 137 gchain = 0;
137 138 argn = getarg(t);
138 139 com = scan(argn);
139 - a1 = com[1];
140 140 gchain = schain;
141 141
142 142 if (argn != 0)
143 143 cmdhash = pathlook(com[0], 1, comptr(t)->comset);
144 144
145 145 if (argn == 0 || (comtype = hashtype(cmdhash)) == BUILTIN) {
146 146 setlist(comptr(t)->comset, 0);
147 147 }
148 148
149 149 if (argn && (flags&noexec) == 0)
150 150 {
151 151
152 152 /* print command if execpr */
153 153 if (flags & execpr)
154 154 execprint(com);
155 155
156 156 if (comtype == NOTFOUND)
157 157 {
158 158 pos = hashdata(cmdhash);
159 159 if (pos == 1)
160 160 failure(*com, notfound);
161 161 else if (pos == 2)
162 162 failure(*com, badexec);
163 163 else
164 164 failure(*com, badperm);
165 165 break;
166 166 }
167 167
168 168 else if (comtype == PATH_COMMAND)
169 169 {
170 170 pos = -1;
171 171 }
172 172
173 173 else if (comtype & (COMMAND | REL_COMMAND))
174 174 {
175 175 pos = hashdata(cmdhash);
176 176 }
177 177
178 178 else if (comtype == BUILTIN) {
179 179 builtin(hashdata(cmdhash),argn,com,t);
180 180 freejobs();
181 181 break;
182 182 }
183 183 else if (comtype == FUNCTION)
184 184 {
185 185 struct dolnod *olddolh;
186 186 struct namnod *n, *opt;
187 187 struct fndnod *f;
188 188 short index;
189 189 unsigned char **olddolv = dolv;
190 190 int olddolc = dolc;
191 191
192 192 n = findnam(com[0]);
193 193 f = fndptr(n->namenv);
194 194 /* just in case */
195 195 if (f == NULL)
196 196 break;
197 197 /* save current positional parameters */
198 198 olddolh = (struct dolnod *)savargs(funcnt);
199 199 f->fndref++;
200 200 funcnt++;
201 201 index = initio(io, 1);
202 202 setargs(com);
203 203 execute(f->fndval, xflags,
204 204 errorflg, pf1, pf2);
205 205 execbrk = 0;
206 206 restore(index);
207 207 (void) restorargs(olddolh, funcnt);
208 208 dolv = olddolv;
209 209 dolc = olddolc;
210 210 funcnt--;
211 211 /*
212 212 * n->namenv may have been
213 213 * pointing different func.
214 214 * Therefore, we can't use
215 215 * freefunc(n).
216 216 */
217 217 freetree((struct trenod *)f);
218 218
219 219 break;
220 220 }
221 221 }
222 222 else if (t->treio == 0)
223 223 {
224 224 chktrap();
225 225 break;
226 226 }
227 227
228 228 }
229 229
230 230 case TFORK:
231 231 {
232 232 int monitor = 0;
233 233 int linked = 0;
234 234
235 235 exitval = 0;
236 236
237 237 if (!(xflags & XEC_EXECED) || treeflgs&(FPOU|FAMP))
238 238 {
239 239
240 240 int forkcnt = 1;
241 241
242 242 if (!(treeflgs&FPOU))
243 243 {
244 244 monitor = (!(xflags & XEC_NOSTOP)
245 245 && (flags&(monitorflg|jcflg|jcoff))
246 246 == (monitorflg|jcflg));
247 247 if (monitor) {
248 248 int savefd;
249 249 unsigned char *savebot;
250 250 savefd = setb(-1);
251 251 savebot = stakbot;
252 252 prcmd(t);
253 253 (void)setb(savefd);
254 254 allocjob(savebot, cwdget(), monitor);
255 255 } else
256 256 allocjob("", "", 0);
257 257
258 258 }
259 259
260 260 if (treeflgs & (FPOU|FAMP)) {
261 261 link_iodocs(iotemp);
262 262 linked = 1;
263 263 }
264 264
265 265 while ((parent = fork()) == -1)
266 266 {
267 267 /*
268 268 * FORKLIM is the max period between forks -
269 269 * power of 2 usually. Currently shell tries
270 270 * after 2,4,8,16, and 32 seconds and then quits
271 271 */
272 272
273 273 if ((forkcnt = (forkcnt * 2)) > FORKLIM)
274 274 {
275 275 switch (errno)
276 276 {
277 277 case ENOMEM:
278 278 deallocjob();
279 279 error(noswap);
280 280 break;
281 281 default:
282 282 deallocjob();
283 283 error(nofork);
284 284 break;
285 285 }
286 286 } else if (errno == EPERM) {
287 287 deallocjob();
288 288 error(eacces);
289 289 break;
290 290 }
291 291 sigchk();
292 292 sh_sleep(forkcnt);
293 293 }
294 294
295 295 if (parent) {
296 296 if (monitor)
297 297 setpgid(parent, 0);
298 298 if (treeflgs & FPIN)
299 299 closepipe(pf1);
300 300 if (!(treeflgs&FPOU)) {
301 301 postjob(parent,!(treeflgs&FAMP));
302 302 freejobs();
303 303 }
304 304 chktrap();
305 305 break;
306 306 }
307 307 mypid = getpid();
308 308 }
309 309
310 310 /*
311 311 * Forked process: assume it is not a subshell for
312 312 * now. If it is, the presence of a left parenthesis
313 313 * will trigger the jcoff flag to be turned off.
314 314 * When jcoff is turned on, monitoring is not going on
315 315 * and waitpid will not look for WUNTRACED.
316 316 */
317 317
318 318 flags |= (forked|jcoff);
319 319
320 320 fiotemp = 0;
321 321
322 322 if (linked == 1) {
323 323 swap_iodoc_nm(iotemp);
324 324 xflags |= XEC_LINKED;
325 325 } else if (!(xflags & XEC_LINKED))
326 326 iotemp = 0;
327 327 #ifdef ACCT
328 328 suspacct();
329 329 #endif
330 330 settmp();
331 331 oldsigs();
332 332
333 333 if (!(treeflgs & FPOU))
334 334 makejob(monitor, !(treeflgs & FAMP));
335 335
336 336 /*
337 337 * pipe in or out
338 338 */
339 339 if (treeflgs & FPIN)
340 340 {
341 341 renamef(pf1[INPIPE], 0);
342 342 close(pf1[OTPIPE]);
343 343 }
344 344
345 345 if (treeflgs & FPOU)
346 346 {
347 347 close(pf2[INPIPE]);
348 348 renamef(pf2[OTPIPE], 1);
349 349 }
350 350
351 351 /*
352 352 * io redirection
353 353 */
354 354 initio(t->treio, 0);
355 355
356 356 if (type == TFORK)
357 357 execute(forkptr(t)->forktre, xflags | XEC_EXECED, errorflg);
358 358 else if (com[0] != ENDARGS)
359 359 {
360 360 eflag = 0;
361 361 setlist(comptr(t)->comset, N_EXPORT);
362 362 rmtemp(0);
363 363 clearjobs();
364 364 execa(com, pos);
365 365 }
366 366 done(0);
367 367 }
368 368
369 369 case TPAR:
370 370 /* Forked process is subshell: may want job control */
371 371 flags &= ~jcoff;
372 372 clearjobs();
373 373 execute(parptr(t)->partre, xflags, errorflg);
374 374 done(0);
375 375
376 376 case TFIL:
377 377 {
378 378 int pv[2];
379 379
380 380 chkpipe(pv);
381 381 if (execute(lstptr(t)->lstlef, xflags & XEC_NOSTOP, errorflg, pf1, pv) == 0)
382 382 execute(lstptr(t)->lstrit, xflags, errorflg, pv, pf2);
383 383 else
384 384 closepipe(pv);
385 385 }
386 386 break;
387 387
388 388 case TLST:
389 389 execute(lstptr(t)->lstlef, xflags&XEC_NOSTOP, errorflg);
390 390 /* Update errorflg if set -e is invoked in the sub-sh*/
391 391 execute(lstptr(t)->lstrit, xflags, (errorflg | (eflag & errflg)));
392 392 break;
393 393
394 394 case TAND:
395 395 case TORF:
396 396 {
397 397 int xval;
398 398 xval = execute(lstptr(t)->lstlef, XEC_NOSTOP, 0);
399 399 if ((xval == 0) == (type == TAND))
400 400 execute(lstptr(t)->lstrit, xflags|XEC_NOSTOP, errorflg);
401 401 break;
402 402 }
403 403
404 404 case TFOR:
405 405 {
406 406 struct namnod *n = lookup(forptr(t)->fornam);
407 407 unsigned char **args;
408 408 struct dolnod *argsav = 0;
409 409
410 410 if (forptr(t)->forlst == 0)
411 411 {
412 412 args = dolv + 1;
413 413 argsav = useargs();
414 414 }
415 415 else
416 416 {
417 417 struct argnod *schain = gchain;
418 418
419 419 gchain = 0;
420 420 args = scan(getarg(forptr(t)->forlst));
421 421 gchain = schain;
422 422 }
423 423 loopcnt++;
424 424 while (*args != ENDARGS && execbrk == 0)
425 425 {
426 426 assign(n, *args++);
427 427 execute(forptr(t)->fortre, XEC_NOSTOP, errorflg);
428 428 if (breakcnt < 0)
429 429 execbrk = (++breakcnt != 0);
430 430 }
431 431 if (breakcnt > 0)
432 432 execbrk = (--breakcnt != 0);
433 433
434 434 loopcnt--;
435 435 if(argsav)
436 436 argfor = (struct dolnod *)freeargs(argsav);
437 437 }
438 438 break;
439 439
440 440 case TWH:
441 441 case TUN:
442 442 {
443 443 int i = 0;
444 444
445 445 loopcnt++;
446 446 while (execbrk == 0 && (execute(whptr(t)->whtre,
447 447 XEC_NOSTOP, 0) == 0) == (type == TWH) &&
448 448 (flags&noexec) == 0)
449 449 {
450 450 i = execute(whptr(t)->dotre, XEC_NOSTOP, errorflg);
451 451 if (breakcnt < 0)
452 452 execbrk = (++breakcnt != 0);
453 453 }
454 454 if (breakcnt > 0)
455 455 execbrk = (--breakcnt != 0);
456 456
457 457 loopcnt--;
458 458 exitval = i;
459 459 }
460 460 break;
461 461
462 462 case TIF:
463 463 if (execute(ifptr(t)->iftre, XEC_NOSTOP, 0) == 0)
464 464 execute(ifptr(t)->thtre, xflags|XEC_NOSTOP, errorflg);
465 465 else if (ifptr(t)->eltre)
466 466 execute(ifptr(t)->eltre, xflags|XEC_NOSTOP, errorflg);
467 467 else
468 468 exitval = 0; /* force zero exit for if-then-fi */
469 469 break;
470 470
471 471 case TSW:
472 472 {
473 473 unsigned char *r = mactrim(swptr(t)->swarg);
474 474 struct regnod *regp;
475 475
476 476 regp = swptr(t)->swlst;
477 477 while (regp)
478 478 {
479 479 struct argnod *rex = regp->regptr;
480 480
481 481 while (rex)
482 482 {
483 483 unsigned char *s;
484 484
485 485 if (gmatch(r, s = macro(rex->argval)) || (trim(s), eq(r, s)))
486 486 {
487 487 execute(regp->regcom, XEC_NOSTOP, errorflg);
488 488 regp = 0;
489 489 break;
490 490 }
491 491 else
492 492 rex = rex->argnxt;
493 493 }
494 494 if (regp)
495 495 regp = regp->regnxt;
496 496 }
497 497 }
498 498 break;
499 499 }
500 500 exitset();
501 501 }
502 502 sigchk();
503 503 tdystak(sav);
504 504 flags |= eflag;
505 505 return(exitval);
506 506 }
507 507
508 508 void
509 509 execexp(unsigned char *s, int f)
510 510 {
511 511 struct fileblk fb;
512 512
513 513 push(&fb);
514 514 if (s)
515 515 {
516 516 estabf(s);
517 517 fb.feval = (unsigned char **)(f);
518 518 }
519 519 else if (f >= 0)
520 520 initf(f);
521 521 execute(cmd(NL, NLFLG | MTFLG), 0, (int)(flags & errflg));
522 522 pop();
523 523 }
524 524
525 525 void
526 526 execprint(unsigned char **com)
527 527 {
528 528 int argn = 0;
529 529 unsigned char *s;
530 530
531 531 prs(_gettext(execpmsg));
532 532 while(com[argn] != ENDARGS)
533 533 {
534 534 s = com[argn++];
535 535 write(output, s, length(s) - 1);
536 536 blank();
537 537 }
538 538
539 539 newline();
540 540 }
↓ open down ↓ |
391 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX