c_ksh.c revision 1.1.1.3 1 /*
2 * built-in Korn commands: c_*
3 */
4
5 #include "sh.h"
6 #include "ksh_stat.h"
7 #include <ctype.h>
8
9 int
10 c_cd(wp)
11 char **wp;
12 {
13 int optc;
14 int physical = Flag(FPHYSICAL);
15 int cdnode; /* was a node from cdpath added in? */
16 int printpath = 0; /* print where we cd'd? */
17 int rval;
18 struct tbl *pwd_s, *oldpwd_s;
19 XString xs;
20 char *xp;
21 char *dir, *try, *pwd;
22 int phys_path;
23 char *cdpath;
24
25 while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != EOF)
26 switch (optc) {
27 case 'L':
28 physical = 0;
29 break;
30 case 'P':
31 physical = 1;
32 break;
33 case '?':
34 return 1;
35 }
36 wp += builtin_opt.optind;
37
38 if (Flag(FRESTRICTED)) {
39 bi_errorf("restricted shell - can't cd");
40 return 1;
41 }
42
43 pwd_s = global("PWD");
44 oldpwd_s = global("OLDPWD");
45
46 if (!wp[0]) {
47 /* No arguments - go home */
48 if ((dir = str_val(global("HOME"))) == null) {
49 bi_errorf("no home directory (HOME not set)");
50 return 1;
51 }
52 } else if (!wp[1]) {
53 /* One argument: - or dir */
54 dir = wp[0];
55 if (strcmp(dir, "-") == 0) {
56 dir = str_val(oldpwd_s);
57 if (dir == null) {
58 bi_errorf("no OLDPWD");
59 return 1;
60 }
61 printpath++;
62 }
63 } else if (!wp[2]) {
64 /* Two arguments - substitute arg1 in PWD for arg2 */
65 int ilen, olen, nlen, elen;
66 char *cp;
67
68 if (!current_wd[0]) {
69 bi_errorf("don't know current directory");
70 return 1;
71 }
72 /* substitue arg1 for arg2 in current path.
73 * if the first substitution fails because the cd fails
74 * we could try to find another substitution. For now
75 * we don't
76 */
77 if ((cp = strstr(current_wd, wp[0])) == (char *) 0) {
78 bi_errorf("bad substitution");
79 return 1;
80 }
81 ilen = cp - current_wd;
82 olen = strlen(wp[0]);
83 nlen = strlen(wp[1]);
84 elen = strlen(current_wd + ilen + olen) + 1;
85 dir = alloc(ilen + nlen + elen, ATEMP);
86 memcpy(dir, current_wd, ilen);
87 memcpy(dir + ilen, wp[1], nlen);
88 memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
89 printpath++;
90 } else {
91 bi_errorf("too many arguments");
92 return 1;
93 }
94
95 Xinit(xs, xp, PATH, ATEMP);
96 /* xp will have a bogus value after make_path() - set it to 0
97 * so that if it's used, it will cause a dump
98 */
99 xp = (char *) 0;
100
101 cdpath = str_val(global("CDPATH"));
102 do {
103 cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
104 #ifdef S_ISLNK
105 if (physical)
106 rval = chdir(try = Xstring(xs, xp) + phys_path);
107 else
108 #endif /* S_ISLNK */
109 {
110 simplify_path(Xstring(xs, xp));
111 rval = chdir(try = Xstring(xs, xp));
112 }
113 } while (rval < 0 && cdpath != (char *) 0);
114
115 if (rval < 0) {
116 if (cdnode)
117 bi_errorf("%s: bad directory", dir);
118 else
119 bi_errorf("%s - %s", try, strerror(errno));
120 return 1;
121 }
122
123 /* Clear out tracked aliases with relative paths */
124 flushcom(0);
125
126 /* Set OLDPWD */
127 if (current_wd[0])
128 setstr(oldpwd_s, current_wd);
129
130 if (!ISABSPATH(Xstring(xs, xp))) {
131 #ifdef OS2
132 /* simplify_path() doesn't know about os/2's drive contexts,
133 * so it can't set current_wd when changing to a:foo.
134 * Handle this by calling getcwd()...
135 */
136 pwd = ksh_get_wd((char *) 0, 0);
137 #else /* OS2 */
138 pwd = (char *) 0;
139 #endif /* OS2 */
140 } else
141 #ifdef S_ISLNK
142 if (!physical || !(pwd = get_phys_path(Xstring(xs, xp))))
143 #endif /* S_ISLNK */
144 pwd = Xstring(xs, xp);
145
146 /* Set PWD */
147 if (pwd) {
148 set_current_wd(pwd);
149 setstr(pwd_s, pwd);
150 } else {
151 set_current_wd(null);
152 pwd = Xstring(xs, xp);
153 /* XXX unset $PWD? */
154 }
155 if (printpath || cdnode)
156 shprintf("%s\n", pwd);
157
158 return 0;
159 }
160
161 int
162 c_pwd(wp)
163 char **wp;
164 {
165 int optc;
166 int physical = Flag(FPHYSICAL);
167 char *p;
168
169 while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != EOF)
170 switch (optc) {
171 case 'L':
172 physical = 0;
173 break;
174 case 'P':
175 physical = 1;
176 break;
177 case '?':
178 return 1;
179 }
180 wp += builtin_opt.optind;
181
182 if (wp[0]) {
183 bi_errorf("too many arguments");
184 return 1;
185 }
186 #ifdef S_ISLNK
187 p = current_wd[0] ? (physical ? get_phys_path(current_wd) : current_wd)
188 : (char *) 0;
189 #else /* S_ISLNK */
190 p = current_wd[0] ? current_wd : (char *) 0;
191 #endif /* S_ISLNK */
192 if (p && eaccess(p, R_OK) < 0)
193 p = (char *) 0;
194 if (!p) {
195 p = ksh_get_wd((char *) 0, 0);
196 if (!p) {
197 bi_errorf("can't get current directory - %s",
198 strerror(errno));
199 return 1;
200 }
201 }
202 shprintf("%s\n", p);
203 return 0;
204 }
205
206 int
207 c_print(wp)
208 char **wp;
209 {
210 #define PO_NL BIT(0) /* print newline */
211 #define PO_EXPAND BIT(1) /* expand backslash sequences */
212 #define PO_PMINUSMINUS BIT(2) /* print a -- argument */
213 #define PO_HIST BIT(3) /* print to history instead of stdout */
214 #define PO_COPROC BIT(4) /* printing to coprocess: block SIGPIPE */
215 #define PO_FSLASH BIT(5) /* swap slash for backslash (for os2 ) */
216 int fd = 1;
217 int flags = PO_EXPAND|PO_NL;
218 char *s;
219 const char *emsg;
220 XString xs;
221 char *xp;
222
223 if (wp[0][0] == 'e') { /* echo command */
224 int nflags = flags;
225
226 /* A compromise between sysV and BSD echo commands:
227 * escape sequences are enabled by default, and
228 * -n, -e and -E are recognized if they appear
229 * in arguments with no illegal options (ie, echo -nq
230 * will print -nq).
231 * Different from sysV echo since options are recognized,
232 * different from BSD echo since escape sequences are enabled
233 * by default.
234 */
235 wp += 1;
236 while ((s = *wp) && *s == '-' && s[1]) {
237 while (*++s)
238 if (*s == 'n')
239 nflags &= ~PO_NL;
240 else if (*s == 'e')
241 nflags |= PO_EXPAND;
242 else if (*s == 'E')
243 nflags &= ~PO_EXPAND;
244 else
245 /* bad option: don't use nflags, print
246 * argument
247 */
248 break;
249 if (*s)
250 break;
251 wp++;
252 flags = nflags;
253 }
254 } else {
255 int optc;
256 #if OS2
257 const char *options = "Rnpfrsu,"; /* added f flag */
258 #else
259 const char *options = "Rnprsu,";
260 #endif
261 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF)
262 switch (optc) {
263 case 'R': /* fake BSD echo command */
264 flags |= PO_PMINUSMINUS;
265 flags &= ~PO_EXPAND;
266 options = "ne";
267 break;
268 case 'e':
269 flags |= PO_EXPAND;
270 break;
271 #ifdef OS2
272 case 'f':
273 flags |= PO_FSLASH;
274 break;
275 #endif
276 case 'n':
277 flags &= ~PO_NL;
278 break;
279 #ifdef KSH
280 case 'p':
281 if ((fd = coproc_getfd(W_OK, &emsg)) < 0) {
282 bi_errorf("-p: %s", emsg);
283 return 1;
284 }
285 break;
286 #endif /* KSH */
287 case 'r':
288 flags &= ~PO_EXPAND;
289 break;
290 case 's':
291 flags |= PO_HIST;
292 break;
293 case 'u':
294 if (!*(s = builtin_opt.optarg))
295 fd = 0;
296 else if ((fd = check_fd(s, W_OK, &emsg)) < 0) {
297 bi_errorf("-u: %s: %s", s, emsg);
298 return 1;
299 }
300 break;
301 case '?':
302 return 1;
303 }
304 if (!(builtin_opt.info & GI_MINUSMINUS)) {
305 /* treat a lone - like -- */
306 if (wp[builtin_opt.optind]
307 && strcmp(wp[builtin_opt.optind], "-") == 0)
308 builtin_opt.optind++;
309 } else if (flags & PO_PMINUSMINUS)
310 builtin_opt.optind--;
311 wp += builtin_opt.optind;
312 }
313
314 Xinit(xs, xp, 128, ATEMP);
315
316 while (*wp != NULL) {
317 register int c;
318 s = *wp;
319 while ((c = *s++) != '\0') {
320 Xcheck(xs, xp);
321 #ifdef OS2
322 if ((flags & PO_FSLASH) && c == '\\')
323 if (*s == '\\')
324 *s++;
325 else
326 c = '/';
327 #endif /* OS2 */
328 if ((flags & PO_EXPAND) && c == '\\') {
329 int i;
330
331 switch ((c = *s++)) {
332 /* Oddly enough, \007 seems more portable than
333 * \a (due to HP-UX cc, Ultrix cc, old pcc's,
334 * etc.).
335 */
336 case 'a': c = '\007'; break;
337 case 'b': c = '\b'; break;
338 case 'c': flags &= ~PO_NL;
339 continue; /* AT&T brain damage */
340 case 'f': c = '\f'; break;
341 case 'n': c = '\n'; break;
342 case 'r': c = '\r'; break;
343 case 't': c = '\t'; break;
344 case 'v': c = 0x0B; break;
345 case '0':
346 /* Look for an octal number: can have
347 * three digits (not counting the
348 * leading 0). Truely burnt.
349 */
350 c = 0;
351 for (i = 0; i < 3; i++) {
352 if (*s >= '0' && *s <= '7')
353 c = c*8 + *s++ - '0';
354 else
355 break;
356 }
357 break;
358 case '\0': s--; c = '\\'; break;
359 case '\\': break;
360 default:
361 Xput(xs, xp, '\\');
362 }
363 }
364 Xput(xs, xp, c);
365 }
366 if (*++wp != NULL)
367 Xput(xs, xp, ' ');
368 }
369 if (flags & PO_NL)
370 Xput(xs, xp, '\n');
371
372 if (flags & PO_HIST) {
373 Xput(xs, xp, '\0');
374 source->line++;
375 histsave(source->line, Xstring(xs, xp), 1);
376 Xfree(xs, xp);
377 } else {
378 int n, len = Xlength(xs, xp);
379 #ifdef KSH
380 int UNINITIALIZED(opipe);
381
382 /* Ensure we aren't killed by a SIGPIPE while writing to
383 * a coprocess. at&t ksh doesn't seem to do this (seems
384 * to just check that the co-process is alive, which is
385 * not enough).
386 */
387 if (coproc.write >= 0 && coproc.write == fd) {
388 flags |= PO_COPROC;
389 opipe = block_pipe();
390 }
391 #endif /* KSH */
392 for (s = Xstring(xs, xp); len > 0; ) {
393 n = write(fd, s, len);
394 if (n < 0) {
395 #ifdef KSH
396 if (flags & PO_COPROC)
397 restore_pipe(opipe);
398 #endif /* KSH */
399 if (errno == EINTR) {
400 /* allow user to ^C out */
401 intrcheck();
402 #ifdef KSH
403 if (flags & PO_COPROC)
404 opipe = block_pipe();
405 #endif /* KSH */
406 continue;
407 }
408 #ifdef KSH
409 /* This doesn't really make sense - could
410 * break scripts (print -p generates
411 * error message).
412 *if (errno == EPIPE)
413 * coproc_write_close(fd);
414 */
415 #endif /* KSH */
416 return 1;
417 }
418 s += n;
419 len -= n;
420 }
421 #ifdef KSH
422 if (flags & PO_COPROC)
423 restore_pipe(opipe);
424 #endif /* KSH */
425 }
426
427 return 0;
428 }
429
430 int
431 c_whence(wp)
432 char **wp;
433 {
434 struct tbl *tp;
435 char *id;
436 int pflag = 0, vflag = 0, Vflag = 0;
437 int ret = 0;
438 int optc;
439 int iam_whence = wp[0][0] == 'w';
440 int fcflags;
441 const char *options = iam_whence ? "pv" : "pvV";
442
443 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF)
444 switch (optc) {
445 case 'p':
446 pflag = 1;
447 break;
448 case 'v':
449 vflag = 1;
450 break;
451 case 'V':
452 Vflag = 1;
453 break;
454 case '?':
455 return 1;
456 }
457 wp += builtin_opt.optind;
458
459
460 fcflags = FC_BI | FC_PATH | FC_FUNC;
461 if (!iam_whence) {
462 /* Note that -p on its own is deal with in comexec() */
463 if (pflag)
464 fcflags |= FC_DEFPATH;
465 /* Convert command options to whence options - note that
466 * command -pV uses a different path search than whence -v
467 * or whence -pv. This should be considered a feature.
468 */
469 vflag = Vflag;
470 }
471 if (pflag)
472 fcflags &= ~(FC_BI | FC_FUNC);
473
474 while ((vflag || ret == 0) && (id = *wp++) != NULL) {
475 tp = NULL;
476 if ((iam_whence || vflag) && !pflag)
477 tp = tsearch(&keywords, id, hash(id));
478 if (!tp && !pflag) {
479 tp = tsearch(&aliases, id, hash(id));
480 if (tp && !(tp->flag & ISSET))
481 tp = NULL;
482 }
483 if (!tp)
484 tp = findcom(id, fcflags);
485 if (vflag || (tp->type != CALIAS && tp->type != CEXEC
486 && tp->type != CTALIAS))
487 shprintf("%s", id);
488 switch (tp->type) {
489 case CKEYWD:
490 if (vflag)
491 shprintf(" is a reserved word");
492 break;
493 case CALIAS:
494 if (vflag)
495 shprintf(" is an %salias for ",
496 (tp->flag & EXPORT) ? "exported "
497 : null);
498 if (!iam_whence && !vflag)
499 shprintf("alias %s=", id);
500 print_value_quoted(tp->val.s);
501 break;
502 case CFUNC:
503 if (vflag) {
504 shprintf(" is a");
505 if (tp->flag & EXPORT)
506 shprintf("n exported");
507 if (tp->flag & TRACE)
508 shprintf(" traced");
509 if (!(tp->flag & ISSET)) {
510 shprintf(" undefined");
511 if (tp->u.fpath)
512 shprintf(" (autoload from %s)",
513 tp->u.fpath);
514 }
515 shprintf(" function");
516 }
517 break;
518 case CSHELL:
519 if (vflag)
520 shprintf(" is a%s shell builtin",
521 (tp->flag & SPEC_BI) ? " special" : null);
522 break;
523 case CTALIAS:
524 case CEXEC:
525 if (tp->flag & ISSET) {
526 if (vflag) {
527 shprintf(" is ");
528 if (tp->type == CTALIAS)
529 shprintf(
530 "a tracked %salias for ",
531 (tp->flag & EXPORT) ?
532 "exported "
533 : null);
534 }
535 shprintf("%s", tp->val.s);
536 } else {
537 if (vflag)
538 shprintf(" not found");
539 ret = 1;
540 }
541 break;
542 default:
543 shprintf("%s is *GOK*", id);
544 break;
545 }
546 if (vflag || !ret)
547 shprintf(newline);
548 }
549 return ret;
550 }
551
552 /* Deal with command -vV - command -p dealt with in comexec() */
553 int
554 c_command(wp)
555 char **wp;
556 {
557 /* Let c_whence do the work. Note that c_command() must be
558 * a distinct function from c_whence() (tested in comexec()).
559 */
560 return c_whence(wp);
561 }
562
563 /* typeset, export, and readonly */
564 int
565 c_typeset(wp)
566 char **wp;
567 {
568 struct block *l = e->loc;
569 struct tbl *vp, **p;
570 Tflag fset = 0, fclr = 0;
571 int thing = 0, func = 0, local = 0;
572 const char *options = "L#R#UZ#fi#lrtux"; /* see comment below */
573 char *fieldstr, *basestr;
574 int field, base;
575 int optc;
576 Tflag flag;
577 int pflag = 0;
578
579 switch (**wp) {
580 case 'e': /* export */
581 fset |= EXPORT;
582 options = "p";
583 break;
584 case 'r': /* readonly */
585 fset |= RDONLY;
586 options = "p";
587 break;
588 case 's': /* set */
589 /* called with 'typeset -' */
590 break;
591 case 't': /* typeset */
592 local = 1;
593 break;
594 }
595
596 fieldstr = basestr = (char *) 0;
597 builtin_opt.flags |= GF_PLUSOPT;
598 /* at&t ksh seems to have 0-9 as options, which are multiplied
599 * to get a number that is used with -L, -R, -Z or -i (eg, -1R2
600 * sets right justify in a field of 12). This allows options
601 * to be grouped in an order (eg, -Lu12), but disallows -i8 -L3 and
602 * does not allow the number to be specified as a seperate argument
603 * Here, the number must follow the RLZi option, but is optional
604 * (see the # kludge in ksh_getopt()).
605 */
606 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != EOF) {
607 flag = 0;
608 switch (optc) {
609 case 'L':
610 flag |= LJUST;
611 fieldstr = builtin_opt.optarg;
612 break;
613 case 'R':
614 flag |= RJUST;
615 fieldstr = builtin_opt.optarg;
616 break;
617 case 'U':
618 /* at&t ksh uses u, but this conflicts with
619 * upper/lower case. If this option is changed,
620 * need to change the -U below as well
621 */
622 flag |= INT_U;
623 break;
624 case 'Z':
625 flag |= ZEROFIL;
626 fieldstr = builtin_opt.optarg;
627 break;
628 case 'f':
629 func = 1;
630 break;
631 case 'i':
632 flag |= INTEGER;
633 basestr = builtin_opt.optarg;
634 break;
635 case 'l':
636 flag |= LCASEV;
637 break;
638 case 'p': /* posix export/readonly -p flag */
639 pflag = 1;
640 break;
641 case 'r':
642 flag |= RDONLY;
643 break;
644 case 't':
645 flag |= TRACE;
646 break;
647 case 'u':
648 flag |= UCASEV_AL; /* upper case / autoload */
649 break;
650 case 'x':
651 flag |= EXPORT;
652 break;
653 case '?':
654 return 1;
655 }
656 if (builtin_opt.info & GI_PLUS) {
657 fclr |= flag;
658 fset &= ~flag;
659 thing = '+';
660 } else {
661 fset |= flag;
662 fclr &= ~flag;
663 thing = '-';
664 }
665 }
666
667 field = 0;
668 if (fieldstr && !bi_getn(fieldstr, &field))
669 return 1;
670 base = 0;
671 if (basestr && !bi_getn(basestr, &base))
672 return 1;
673
674 if (!(builtin_opt.info & GI_MINUSMINUS) && wp[builtin_opt.optind]
675 && (wp[builtin_opt.optind][0] == '-'
676 || wp[builtin_opt.optind][0] == '+')
677 && wp[builtin_opt.optind][1] == '\0')
678 {
679 thing = wp[builtin_opt.optind][0];
680 builtin_opt.optind++;
681 }
682
683 if (func && ((fset|fclr) & ~(TRACE|UCASEV_AL|EXPORT))) {
684 bi_errorf("only -t, -u and -x options may be used with -f");
685 return 1;
686 }
687 if (wp[builtin_opt.optind]) {
688 /* Take care of exclusions */
689 /* setting these attributes clears the others, unless they
690 * are also set in this command
691 */
692 if (fset & (LJUST|RJUST|ZEROFIL|UCASEV_AL|LCASEV|INTEGER
693 |INT_U|INT_L))
694 fclr |= ~fset &
695 (LJUST|RJUST|ZEROFIL|UCASEV_AL|LCASEV|INTEGER
696 |INT_U|INT_L);
697 fclr &= ~fset; /* set wins */
698 if ((fset & (ZEROFIL|LJUST)) == ZEROFIL) {
699 fset |= RJUST;
700 fclr &= ~RJUST;
701 }
702 if (fset & LCASEV) /* LCASEV has priority */
703 fclr |= UCASEV_AL;
704 else if (fset & UCASEV_AL)
705 fclr |= LCASEV;
706 if (fset & LJUST) /* LJUST has priority */
707 fclr |= RJUST;
708 else if (fset & RJUST)
709 fclr |= LJUST;
710 if ((fset | fclr) & INTEGER) {
711 if (!(fset | fclr) & INT_U)
712 fclr |= INT_U;
713 if (!(fset | fclr) & INT_L)
714 fclr |= INT_L;
715 }
716 fset &= ~fclr; /* in case of something like -LR */
717 }
718
719 /* set variables and attributes */
720 if (wp[builtin_opt.optind]) {
721 int i;
722 int rval = 0;
723 struct tbl *f;
724
725 if (local && !func)
726 fset |= LOCAL;
727 for (i = builtin_opt.optind; wp[i]; i++) {
728 if (func) {
729 f = findfunc(wp[i], hash(wp[i]),
730 (fset&UCASEV_AL) ? TRUE : FALSE);
731 if (!f) {
732 /* at&t ksh does ++rval: bogus */
733 rval = 1;
734 continue;
735 }
736 if (fset | fclr) {
737 f->flag |= fset;
738 f->flag &= ~fclr;
739 } else
740 fptreef(shl_stdout, 0,
741 "function %s %T\n",
742 wp[i], f->val.t);
743 } else if (!typeset(wp[i], fset, fclr, field, base)) {
744 bi_errorf("%s: not identifier", wp[i]);
745 return 1;
746 }
747 }
748 return rval;
749 }
750
751 /* list variables and attributes */
752 flag = fset | fclr; /* no difference at this point.. */
753 if (func) {
754 for (l = e->loc; l; l = l->next) {
755 for (p = tsort(&l->funs); (vp = *p++); ) {
756 if (flag && (vp->flag & flag) == 0)
757 continue;
758 if (thing == '-')
759 fptreef(shl_stdout, 0, "function %s %T\n",
760 vp->name, vp->val.t);
761 else
762 shprintf("%s\n", vp->name);
763 }
764 }
765 } else {
766 for (l = e->loc; l; l = l->next) {
767 for (p = tsort(&l->vars); (vp = *p++); )
768 for (; vp; vp = vp->u.array) {
769 /* Report an unset param only if the user has
770 * explicitly given it some attribute (like export);
771 * otherwise, after "echo $FOO", we would report FOO...
772 */
773 if (!(vp->flag & ISSET) && !(vp->flag & USERATTRIB))
774 continue;
775 if (flag && (vp->flag & flag) == 0)
776 continue;
777 /* no arguments */
778 if (thing == 0 && flag == 0) {
779 /* at&t ksh prints things like export, integer,
780 * leftadj, zerofill, etc., but POSIX says must
781 * be suitable for re-entry...
782 */
783 shprintf("typeset ");
784 if ((vp->flag&INTEGER))
785 shprintf("-i ");
786 if ((vp->flag&EXPORT))
787 shprintf("-x ");
788 if ((vp->flag&RDONLY))
789 shprintf("-r ");
790 if ((vp->flag&TRACE))
791 shprintf("-t ");
792 if ((vp->flag&LJUST))
793 shprintf("-L%d ", vp->u2.field);
794 if ((vp->flag&RJUST))
795 shprintf("-R%d ", vp->u2.field);
796 if ((vp->flag&ZEROFIL))
797 shprintf("-Z ");
798 if ((vp->flag&LCASEV))
799 shprintf("-l ");
800 if ((vp->flag&UCASEV_AL))
801 shprintf("-u ");
802 if ((vp->flag&INT_U))
803 shprintf("-U ");
804 if (vp->flag&ARRAY)
805 shprintf("%s[%d]\n", vp->name,vp->index);
806 else
807 shprintf("%s\n", vp->name);
808 } else {
809 if (pflag)
810 shprintf("%s ",
811 (flag & EXPORT) ? "export" : "readonly");
812 if (vp->flag&ARRAY)
813 shprintf("%s[%d]", vp->name, vp->index);
814 else
815 shprintf("%s", vp->name);
816 if (thing == '-' && (vp->flag&ISSET)) {
817 char *s = str_val(vp);
818
819 shprintf("=");
820 /* at&t ksh can't have justified integers.. */
821 if ((vp->flag & (INTEGER|LJUST|RJUST))
822 == INTEGER)
823 shprintf("%s", s);
824 else
825 print_value_quoted(s);
826 }
827 shprintf(newline);
828 }
829 }
830 }
831 }
832 return 0;
833 }
834
835 int
836 c_alias(wp)
837 char **wp;
838 {
839 struct table *t = &aliases;
840 int rv = 0, rflag = 0, tflag, Uflag = 0;
841 Tflag xflag = 0;
842 int optc;
843
844 while ((optc = ksh_getopt(wp, &builtin_opt, "drtUx")) != EOF)
845 switch (optc) {
846 case 'd':
847 t = &homedirs;
848 break;
849 case 'r':
850 rflag = 1;
851 break;
852 case 't':
853 t = &taliases;
854 break;
855 case 'U': /* kludge for tracked alias initialization
856 * (don't do a path search, just make an entry)
857 */
858 Uflag = 1;
859 break;
860 case 'x':
861 xflag = EXPORT;
862 break;
863 case '?':
864 return 1;
865 }
866 wp += builtin_opt.optind;
867
868 tflag = t == &taliases;
869
870 /* "hash -r" means reset all the tracked aliases.. */
871 if (rflag) {
872 static const char *const args[] = {
873 "unalias", "-ta", (const char *) 0
874 };
875
876 if (!tflag || *wp) {
877 shprintf(
878 "alias: -r flag can only be used with -t and without arguments\n");
879 return 1;
880 }
881 ksh_getopt_reset(&builtin_opt, GF_ERROR);
882 return c_unalias((char **) args);
883 }
884
885 if (*wp == NULL) {
886 struct tbl *ap, **p;
887
888 for (p = tsort(t); (ap = *p++) != NULL; )
889 if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
890 shprintf("%s=", ap->name);
891 print_value_quoted(ap->val.s);
892 shprintf(newline);
893 }
894 }
895
896 for (; *wp != NULL; wp++) {
897 char *alias = *wp;
898 char *val = strchr(alias, '=');
899 char *newval;
900 struct tbl *ap;
901 int h;
902
903 if (val)
904 alias = str_nsave(alias, val++ - alias, ATEMP);
905 h = hash(alias);
906 if (val == NULL && !tflag && !xflag) {
907 ap = tsearch(t, alias, h);
908 if (ap != NULL && (ap->flag&ISSET)) {
909 shprintf("%s=", ap->name);
910 print_value_quoted(ap->val.s);
911 shprintf(newline);
912 } else {
913 shprintf("%s alias not found\n", alias);
914 rv = 1;
915 }
916 continue;
917 }
918 ap = tenter(t, alias, h);
919 ap->type = tflag ? CTALIAS : CALIAS;
920 /* Are we setting the value or just some flags? */
921 if ((val && !tflag) || (!val && tflag && !Uflag)) {
922 if (ap->flag&ALLOC) {
923 ap->flag &= ~(ALLOC|ISSET);
924 afree((void*)ap->val.s, APERM);
925 }
926 /* ignore values for -t (at&t ksh does this) */
927 newval = tflag ? search(alias, path, X_OK, (int *) 0)
928 : val;
929 if (newval) {
930 ap->val.s = str_save(newval, APERM);
931 ap->flag |= ALLOC|ISSET;
932 } else
933 ap->flag &= ~ISSET;
934 }
935 ap->flag |= DEFINED|xflag;
936 if (val)
937 afree(alias, ATEMP);
938 }
939
940 return rv;
941 }
942
943 int
944 c_unalias(wp)
945 char **wp;
946 {
947 register struct table *t = &aliases;
948 register struct tbl *ap;
949 int rv = 0, all = 0;
950 int optc;
951
952 while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != EOF)
953 switch (optc) {
954 case 'a':
955 all = 1;
956 break;
957 case 'd':
958 t = &homedirs;
959 break;
960 case 't':
961 t = &taliases;
962 break;
963 case '?':
964 return 1;
965 }
966 wp += builtin_opt.optind;
967
968 for (; *wp != NULL; wp++) {
969 ap = tsearch(t, *wp, hash(*wp));
970 if (ap == NULL) {
971 rv = 1; /* POSIX */
972 continue;
973 }
974 if (ap->flag&ALLOC) {
975 ap->flag &= ~(ALLOC|ISSET);
976 afree((void*)ap->val.s, APERM);
977 }
978 ap->flag &= ~(DEFINED|ISSET|EXPORT);
979 }
980
981 if (all) {
982 struct tstate ts;
983
984 for (twalk(&ts, t); (ap = tnext(&ts)); ) {
985 if (ap->flag&ALLOC) {
986 ap->flag &= ~(ALLOC|ISSET);
987 afree((void*)ap->val.s, APERM);
988 }
989 ap->flag &= ~(DEFINED|ISSET|EXPORT);
990 }
991 }
992
993 return rv;
994 }
995
996 #ifdef KSH
997 int
998 c_let(wp)
999 char **wp;
1000 {
1001 int rv = 1;
1002 long val;
1003
1004 if (wp[1] == (char *) 0) /* at&t ksh does this */
1005 bi_errorf("no arguments");
1006 else
1007 for (wp++; *wp; wp++)
1008 if (!evaluate(*wp, &val, TRUE)) {
1009 rv = 2; /* distinguish error from zero result */
1010 break;
1011 } else
1012 rv = val == 0;
1013 return rv;
1014 }
1015 #endif /* KSH */
1016
1017 int
1018 c_jobs(wp)
1019 char **wp;
1020 {
1021 int optc;
1022 int flag = 0;
1023 int nflag = 0;
1024 int rv = 0;
1025
1026 while ((optc = ksh_getopt(wp, &builtin_opt, "lpnz")) != EOF)
1027 switch (optc) {
1028 case 'l':
1029 flag = 1;
1030 break;
1031 case 'p':
1032 flag = 2;
1033 break;
1034 case 'n':
1035 nflag = 1;
1036 break;
1037 case 'z': /* debugging: print zombies */
1038 nflag = -1;
1039 break;
1040 case '?':
1041 return 1;
1042 }
1043 wp += builtin_opt.optind;
1044 if (!*wp)
1045 if (j_jobs((char *) 0, flag, nflag))
1046 rv = 1;
1047 else
1048 for (; *wp; wp++)
1049 if (j_jobs(*wp, flag, nflag))
1050 rv = 1;
1051 return rv;
1052 }
1053
1054 #ifdef JOBS
1055 int
1056 c_fgbg(wp)
1057 char **wp;
1058 {
1059 int bg = strcmp(*wp, "bg") == 0;
1060 int UNINITIALIZED(rv);
1061
1062 if (!Flag(FMONITOR)) {
1063 bi_errorf("job control not enabled");
1064 return 1;
1065 }
1066 if (ksh_getopt(wp, &builtin_opt, null) == '?')
1067 return 1;
1068 wp += builtin_opt.optind;
1069 if (*wp)
1070 for (; *wp; wp++)
1071 rv = j_resume(*wp, bg);
1072 else
1073 rv = j_resume("%%", bg);
1074 /* POSIX says fg shall return 0 (unless an error occurs).
1075 * at&t ksh returns the exit value of the job...
1076 */
1077 return (bg || Flag(FPOSIX)) ? 0 : rv;
1078 }
1079 #endif
1080
1081 struct kill_info {
1082 int num_width;
1083 int name_width;
1084 };
1085 static char *kill_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
1086
1087 /* format a single kill item */
1088 static char *
1089 kill_fmt_entry(arg, i, buf, buflen)
1090 void *arg;
1091 int i;
1092 char *buf;
1093 int buflen;
1094 {
1095 struct kill_info *ki = (struct kill_info *) arg;
1096
1097 i++;
1098 if (sigtraps[i].name)
1099 shf_snprintf(buf, buflen, "%*d %*s %s",
1100 ki->num_width, i,
1101 ki->name_width, sigtraps[i].name,
1102 sigtraps[i].mess);
1103 else
1104 shf_snprintf(buf, buflen, "%*d %*d %s",
1105 ki->num_width, i,
1106 ki->name_width, sigtraps[i].signal,
1107 sigtraps[i].mess);
1108 return buf;
1109 }
1110
1111
1112 int
1113 c_kill(wp)
1114 char **wp;
1115 {
1116 Trap *t = (Trap *) 0;
1117 char *p;
1118 int lflag = 0;
1119 int i, n, rv, sig;
1120
1121 /* assume old style options if -digits or -UPPERCASE */
1122 if ((p = wp[1]) && *p == '-' && (digit(p[1]) || isupper(p[1]))) {
1123 if (!(t = gettrap(p + 1))) {
1124 bi_errorf("bad signal `%s'", p + 1);
1125 return 1;
1126 }
1127 i = (wp[2] && strcmp(wp[2], "--") == 0) ? 3 : 2;
1128 } else {
1129 int optc;
1130
1131 while ((optc = ksh_getopt(wp, &builtin_opt, "ls:")) != EOF)
1132 switch (optc) {
1133 case 'l':
1134 lflag = 1;
1135 break;
1136 case 's':
1137 if (!(t = gettrap(builtin_opt.optarg))) {
1138 bi_errorf("bad signal `%s'",
1139 builtin_opt.optarg);
1140 return 1;
1141 }
1142 case '?':
1143 return 1;
1144 }
1145 i = builtin_opt.optind;
1146 }
1147 if ((lflag && t) || (!wp[i] && !lflag)) {
1148 shf_fprintf(shl_out,
1149 "Usage: kill [ -s signame | -signum | -signame ] {pid|job}...\n\
1150 kill -l [exit_status]\n"
1151 );
1152 bi_errorf(null);
1153 return 1;
1154 }
1155
1156 if (lflag) {
1157 if (wp[i]) {
1158 for (; wp[i]; i++) {
1159 if (!bi_getn(wp[i], &n))
1160 return 1;
1161 if (n > 128 && n < 128 + SIGNALS)
1162 n -= 128;
1163 if (n > 0 && n < SIGNALS && sigtraps[n].name)
1164 shprintf("%s\n", sigtraps[n].name);
1165 else
1166 shprintf("%d\n", n);
1167 }
1168 } else if (Flag(FPOSIX)) {
1169 p = null;
1170 for (i = 1; i < SIGNALS; i++, p = space)
1171 if (sigtraps[i].name)
1172 shprintf("%s%s", p, sigtraps[i].name);
1173 shprintf(newline);
1174 } else {
1175 int w, i;
1176 int mess_width;
1177 struct kill_info ki;
1178
1179 for (i = SIGNALS, ki.num_width = 1; i >= 10; i /= 10)
1180 ki.num_width++;
1181 ki.name_width = mess_width = 0;
1182 for (i = 0; i < SIGNALS; i++) {
1183 w = sigtraps[i].name ? strlen(sigtraps[i].name)
1184 : ki.num_width;
1185 if (w > ki.name_width)
1186 ki.name_width = w;
1187 w = strlen(sigtraps[i].mess);
1188 if (w > mess_width)
1189 mess_width = w;
1190 }
1191
1192 print_columns(shl_stdout, SIGNALS - 1,
1193 kill_fmt_entry, (void *) &ki,
1194 ki.num_width + ki.name_width + mess_width + 3);
1195 }
1196 return 0;
1197 }
1198 rv = 0;
1199 sig = t ? t->signal : SIGTERM;
1200 for (; (p = wp[i]); i++) {
1201 if (*p == '%') {
1202 if (j_kill(p, sig))
1203 rv = 1;
1204 } else if (!getn(p, &n)) {
1205 bi_errorf("%s: arguments must be jobs or process ids",
1206 p);
1207 rv = 1;
1208 } else {
1209 /* use killpg if < -1 since -1 does special things for
1210 * some non-killpg-endowed kills
1211 */
1212 if ((n < -1 ? killpg(-n, sig) : kill(n, sig)) < 0) {
1213 bi_errorf("%s: %s", p, strerror(errno));
1214 rv = 1;
1215 }
1216 }
1217 }
1218 return rv;
1219 }
1220
1221 static Getopt user_opt; /* parsing state for getopts builtin command */
1222 static int getopts_noset; /* stop OPTIND assign from resetting state */
1223
1224 void
1225 getopts_reset(val)
1226 int val;
1227 {
1228 if (!getopts_noset && val >= 1) {
1229 ksh_getopt_reset(&user_opt,
1230 GF_NONAME | (Flag(FPOSIX) ? 0 : GF_PLUSOPT));
1231 user_opt.optind = val;
1232 }
1233 }
1234
1235 int
1236 c_getopts(wp)
1237 char **wp;
1238 {
1239 int argc;
1240 const char *options;
1241 const char *var;
1242 int optc;
1243 char buf[3];
1244 struct tbl *vq;
1245
1246 if (ksh_getopt(wp, &builtin_opt, null) == '?')
1247 return 1;
1248 wp += builtin_opt.optind;
1249
1250 options = *wp++;
1251 if (!options) {
1252 bi_errorf("missing options argument");
1253 return 1;
1254 }
1255
1256 var = *wp++;
1257 if (!var) {
1258 bi_errorf("missing name argument");
1259 return 1;
1260 }
1261 if (!*var || *skip_varname(var, TRUE)) {
1262 bi_errorf("%s: is not an identifier", var);
1263 return 1;
1264 }
1265
1266 if (e->loc->next == (struct block *) 0) {
1267 internal_errorf(0, "c_getopts: no argv");
1268 return 1;
1269 }
1270 /* Which arguments are we parsing... */
1271 if (*wp == (char *) 0)
1272 wp = e->loc->next->argv;
1273 else
1274 *--wp = e->loc->next->argv[0];
1275
1276 /* Check that our saved state won't cause a core dump... */
1277 for (argc = 0; wp[argc]; argc++)
1278 ;
1279 if (user_opt.optind > argc
1280 || (user_opt.p != 0
1281 && user_opt.p > strlen(wp[user_opt.optind - 1])))
1282 {
1283 bi_errorf("arguments changed since last call");
1284 return 1;
1285 }
1286
1287 user_opt.optarg = (char *) 0;
1288 optc = ksh_getopt(wp, &user_opt, options);
1289
1290 if (optc >= 0 && optc != '?' && (user_opt.info & GI_PLUS)) {
1291 buf[0] = '+';
1292 buf[1] = optc;
1293 buf[2] = '\0';
1294 } else {
1295 /* POSIX says var is set to ? at end-of-options, at&t ksh
1296 * sets it to null - we go with POSIX...
1297 */
1298 buf[0] = optc < 0 ? '?' : optc;
1299 buf[1] = '\0';
1300 }
1301
1302 /* at&t ksh does not change OPTIND if it was an unknown option.
1303 * Scripts counting on this are prone to break... (ie, don't count
1304 * on this staying).
1305 */
1306 if (optc != '?') {
1307 getopts_noset = 1;
1308 setint(global("OPTIND"), (long) user_opt.optind);
1309 getopts_noset = 0;
1310 }
1311
1312 if (user_opt.optarg == (char *) 0)
1313 unset(global("OPTARG"), 0);
1314 else
1315 setstr(global("OPTARG"), user_opt.optarg);
1316
1317 vq = global(var);
1318 if (vq->flag & RDONLY) {
1319 bi_errorf("%s is readonly", var);
1320 return 1;
1321 }
1322 if (Flag(FEXPORT))
1323 typeset(var, EXPORT, 0, 0, 0);
1324 setstr(vq, buf);
1325
1326 return optc < 0 ? 1 : 0;
1327 }
1328
1329 #ifdef EMACS
1330 int
1331 c_bind(wp)
1332 char **wp;
1333 {
1334 int rv = 0, macro = 0, list = 0;
1335 register char *cp;
1336 int optc;
1337
1338 while ((optc = ksh_getopt(wp, &builtin_opt, "lm")) != EOF)
1339 switch (optc) {
1340 case 'l':
1341 list = 1;
1342 break;
1343 case 'm':
1344 macro = 1;
1345 break;
1346 case '?':
1347 return 1;
1348 }
1349 wp += builtin_opt.optind;
1350
1351 if (*wp == NULL) /* list all */
1352 rv = x_bind((char*)NULL, (char*)NULL, 0, list);
1353
1354 for (; *wp != NULL; wp++) {
1355 cp = strchr(*wp, '=');
1356 if (cp != NULL)
1357 *cp++ = '\0';
1358 if (x_bind(*wp, cp, macro, 0))
1359 rv = 1;
1360 }
1361
1362 return rv;
1363 }
1364 #endif
1365
1366 /* A leading = means assignments before command are kept;
1367 * a leading * means a POSIX special builtin;
1368 * a leading + means a POSIX regular builtin
1369 * (* and + should not be combined).
1370 */
1371 const struct builtin kshbuiltins [] = {
1372 {"+alias", c_alias}, /* no =: at&t manual wrong */
1373 {"+cd", c_cd},
1374 {"+command", c_command},
1375 {"echo", c_print},
1376 {"*=export", c_typeset},
1377 #ifdef HISTORY
1378 {"+fc", c_fc},
1379 #endif /* HISTORY */
1380 {"+getopts", c_getopts},
1381 {"+jobs", c_jobs},
1382 {"+kill", c_kill},
1383 #ifdef KSH
1384 {"let", c_let},
1385 #endif /* KSH */
1386 {"print", c_print},
1387 {"pwd", c_pwd},
1388 {"*=readonly", c_typeset},
1389 {"=typeset", c_typeset},
1390 {"+unalias", c_unalias},
1391 {"whence", c_whence},
1392 #ifdef JOBS
1393 {"+bg", c_fgbg},
1394 {"+fg", c_fgbg},
1395 #endif
1396 #ifdef EMACS
1397 {"bind", c_bind},
1398 #endif
1399 {NULL, NULL}
1400 };
1401