c_ksh.c revision 1.1.1.2 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 if (flag && (vp->flag & flag) == 0)
770 continue;
771 /* no arguments */
772 if (thing == 0 && flag == 0) {
773 /* at&t ksh prints things like export, integer,
774 * leftadj, zerofill, etc., but POSIX says must
775 * be suitable for re-entry...
776 */
777 shprintf("typeset ");
778 if ((vp->flag&INTEGER))
779 shprintf("-i ");
780 if ((vp->flag&EXPORT))
781 shprintf("-x ");
782 if ((vp->flag&RDONLY))
783 shprintf("-r ");
784 if ((vp->flag&TRACE))
785 shprintf("-t ");
786 if ((vp->flag&LJUST))
787 shprintf("-L%d ", vp->u2.field);
788 if ((vp->flag&RJUST))
789 shprintf("-R%d ", vp->u2.field);
790 if ((vp->flag&ZEROFIL))
791 shprintf("-Z ");
792 if ((vp->flag&LCASEV))
793 shprintf("-l ");
794 if ((vp->flag&UCASEV_AL))
795 shprintf("-u ");
796 if ((vp->flag&INT_U))
797 shprintf("-U ");
798 if (vp->flag&ARRAY)
799 shprintf("%s[%d]\n", vp->name,vp->index);
800 else
801 shprintf("%s\n", vp->name);
802 } else {
803 if (pflag)
804 shprintf("%s ",
805 (flag & EXPORT) ? "export" : "readonly");
806 if (vp->flag&ARRAY)
807 shprintf("%s[%d]", vp->name, vp->index);
808 else
809 shprintf("%s", vp->name);
810 if (thing == '-' && (vp->flag&ISSET)) {
811 char *s = str_val(vp);
812
813 shprintf("=");
814 /* at&t ksh can't have justified integers.. */
815 if ((vp->flag & (INTEGER|LJUST|RJUST))
816 == INTEGER)
817 shprintf("%s", s);
818 else
819 print_value_quoted(s);
820 }
821 shprintf(newline);
822 }
823 }
824 }
825 }
826 return 0;
827 }
828
829 int
830 c_alias(wp)
831 char **wp;
832 {
833 struct table *t = &aliases;
834 int rv = 0, rflag = 0, tflag, Uflag = 0;
835 Tflag xflag = 0;
836 int optc;
837
838 while ((optc = ksh_getopt(wp, &builtin_opt, "drtUx")) != EOF)
839 switch (optc) {
840 case 'd':
841 t = &homedirs;
842 break;
843 case 'r':
844 rflag = 1;
845 break;
846 case 't':
847 t = &taliases;
848 break;
849 case 'U': /* kludge for tracked alias initialization
850 * (don't do a path search, just make an entry)
851 */
852 Uflag = 1;
853 break;
854 case 'x':
855 xflag = EXPORT;
856 break;
857 case '?':
858 return 1;
859 }
860 wp += builtin_opt.optind;
861
862 tflag = t == &taliases;
863
864 /* "hash -r" means reset all the tracked aliases.. */
865 if (rflag) {
866 static const char *const args[] = {
867 "unalias", "-ta", (const char *) 0
868 };
869
870 if (!tflag || *wp) {
871 shprintf(
872 "alias: -r flag can only be used with -t and without arguments\n");
873 return 1;
874 }
875 ksh_getopt_reset(&builtin_opt, GF_ERROR);
876 return c_unalias((char **) args);
877 }
878
879 if (*wp == NULL) {
880 struct tbl *ap, **p;
881
882 for (p = tsort(t); (ap = *p++) != NULL; )
883 if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
884 shprintf("%s=", ap->name);
885 print_value_quoted(ap->val.s);
886 shprintf(newline);
887 }
888 }
889
890 for (; *wp != NULL; wp++) {
891 char *alias = *wp;
892 char *val = strchr(alias, '=');
893 char *newval;
894 struct tbl *ap;
895 int h;
896
897 if (val)
898 alias = str_nsave(alias, val++ - alias, ATEMP);
899 h = hash(alias);
900 if (val == NULL && !tflag && !xflag) {
901 ap = tsearch(t, alias, h);
902 if (ap != NULL && (ap->flag&ISSET)) {
903 shprintf("%s=", ap->name);
904 print_value_quoted(ap->val.s);
905 shprintf(newline);
906 } else {
907 shprintf("%s alias not found\n", alias);
908 rv = 1;
909 }
910 continue;
911 }
912 ap = tenter(t, alias, h);
913 ap->type = tflag ? CTALIAS : CALIAS;
914 /* Are we setting the value or just some flags? */
915 if ((val && !tflag) || (!val && tflag && !Uflag)) {
916 if (ap->flag&ALLOC) {
917 ap->flag &= ~(ALLOC|ISSET);
918 afree((void*)ap->val.s, APERM);
919 }
920 /* ignore values for -t (at&t ksh does this) */
921 newval = tflag ? search(alias, path, X_OK, (int *) 0)
922 : val;
923 if (newval) {
924 ap->val.s = str_save(newval, APERM);
925 ap->flag |= ALLOC|ISSET;
926 } else
927 ap->flag &= ~ISSET;
928 }
929 ap->flag |= DEFINED|xflag;
930 if (val)
931 afree(alias, ATEMP);
932 }
933
934 return rv;
935 }
936
937 int
938 c_unalias(wp)
939 char **wp;
940 {
941 register struct table *t = &aliases;
942 register struct tbl *ap;
943 int rv = 0, all = 0;
944 int optc;
945
946 while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != EOF)
947 switch (optc) {
948 case 'a':
949 all = 1;
950 break;
951 case 'd':
952 t = &homedirs;
953 break;
954 case 't':
955 t = &taliases;
956 break;
957 case '?':
958 return 1;
959 }
960 wp += builtin_opt.optind;
961
962 for (; *wp != NULL; wp++) {
963 ap = tsearch(t, *wp, hash(*wp));
964 if (ap == NULL) {
965 rv = 1; /* POSIX */
966 continue;
967 }
968 if (ap->flag&ALLOC) {
969 ap->flag &= ~(ALLOC|ISSET);
970 afree((void*)ap->val.s, APERM);
971 }
972 ap->flag &= ~(DEFINED|ISSET|EXPORT);
973 }
974
975 if (all) {
976 struct tstate ts;
977
978 for (twalk(&ts, t); (ap = tnext(&ts)); ) {
979 if (ap->flag&ALLOC) {
980 ap->flag &= ~(ALLOC|ISSET);
981 afree((void*)ap->val.s, APERM);
982 }
983 ap->flag &= ~(DEFINED|ISSET|EXPORT);
984 }
985 }
986
987 return rv;
988 }
989
990 #ifdef KSH
991 int
992 c_let(wp)
993 char **wp;
994 {
995 int rv = 1;
996 long val;
997
998 if (wp[1] == (char *) 0) /* at&t ksh does this */
999 bi_errorf("no arguments");
1000 else
1001 for (wp++; *wp; wp++)
1002 if (!evaluate(*wp, &val, TRUE)) {
1003 rv = 2; /* distinguish error from zero result */
1004 break;
1005 } else
1006 rv = val == 0;
1007 return rv;
1008 }
1009 #endif /* KSH */
1010
1011 int
1012 c_jobs(wp)
1013 char **wp;
1014 {
1015 int optc;
1016 int flag = 0;
1017 int nflag = 0;
1018 int rv = 0;
1019
1020 while ((optc = ksh_getopt(wp, &builtin_opt, "lpnz")) != EOF)
1021 switch (optc) {
1022 case 'l':
1023 flag = 1;
1024 break;
1025 case 'p':
1026 flag = 2;
1027 break;
1028 case 'n':
1029 nflag = 1;
1030 break;
1031 case 'z': /* debugging: print zombies */
1032 nflag = -1;
1033 break;
1034 case '?':
1035 return 1;
1036 }
1037 wp += builtin_opt.optind;
1038 if (!*wp)
1039 if (j_jobs((char *) 0, flag, nflag))
1040 rv = 1;
1041 else
1042 for (; *wp; wp++)
1043 if (j_jobs(*wp, flag, nflag))
1044 rv = 1;
1045 return rv;
1046 }
1047
1048 #ifdef JOBS
1049 int
1050 c_fgbg(wp)
1051 char **wp;
1052 {
1053 int bg = strcmp(*wp, "bg") == 0;
1054 int UNINITIALIZED(rv);
1055
1056 if (!Flag(FMONITOR)) {
1057 bi_errorf("job control not enabled");
1058 return 1;
1059 }
1060 if (ksh_getopt(wp, &builtin_opt, null) == '?')
1061 return 1;
1062 wp += builtin_opt.optind;
1063 if (*wp)
1064 for (; *wp; wp++)
1065 rv = j_resume(*wp, bg);
1066 else
1067 rv = j_resume("%%", bg);
1068 /* POSIX says fg shall return 0 (unless an error occurs).
1069 * at&t ksh returns the exit value of the job...
1070 */
1071 return (bg || Flag(FPOSIX)) ? 0 : rv;
1072 }
1073 #endif
1074
1075 struct kill_info {
1076 int num_width;
1077 int name_width;
1078 };
1079 static char *kill_fmt_entry ARGS((void *arg, int i, char *buf, int buflen));
1080
1081 /* format a single kill item */
1082 static char *
1083 kill_fmt_entry(arg, i, buf, buflen)
1084 void *arg;
1085 int i;
1086 char *buf;
1087 int buflen;
1088 {
1089 struct kill_info *ki = (struct kill_info *) arg;
1090
1091 i++;
1092 if (sigtraps[i].name)
1093 shf_snprintf(buf, buflen, "%*d %*s %s",
1094 ki->num_width, i,
1095 ki->name_width, sigtraps[i].name,
1096 sigtraps[i].mess);
1097 else
1098 shf_snprintf(buf, buflen, "%*d %*d %s",
1099 ki->num_width, i,
1100 ki->name_width, sigtraps[i].signal,
1101 sigtraps[i].mess);
1102 return buf;
1103 }
1104
1105
1106 int
1107 c_kill(wp)
1108 char **wp;
1109 {
1110 Trap *t = (Trap *) 0;
1111 char *p;
1112 int lflag = 0;
1113 int i, n, rv, sig;
1114
1115 /* assume old style options if -digits or -UPPERCASE */
1116 if ((p = wp[1]) && *p == '-' && (digit(p[1]) || isupper(p[1]))) {
1117 if (!(t = gettrap(p + 1))) {
1118 bi_errorf("bad signal `%s'", p + 1);
1119 return 1;
1120 }
1121 i = (wp[2] && strcmp(wp[2], "--") == 0) ? 3 : 2;
1122 } else {
1123 int optc;
1124
1125 while ((optc = ksh_getopt(wp, &builtin_opt, "ls:")) != EOF)
1126 switch (optc) {
1127 case 'l':
1128 lflag = 1;
1129 break;
1130 case 's':
1131 if (!(t = gettrap(builtin_opt.optarg))) {
1132 bi_errorf("bad signal `%s'",
1133 builtin_opt.optarg);
1134 return 1;
1135 }
1136 case '?':
1137 return 1;
1138 }
1139 i = builtin_opt.optind;
1140 }
1141 if ((lflag && t) || (!wp[i] && !lflag)) {
1142 shf_fprintf(shl_out,
1143 "Usage: kill [ -s signame | -signum | -signame ] {pid|job}...\n\
1144 kill -l [exit_status]\n"
1145 );
1146 bi_errorf(null);
1147 return 1;
1148 }
1149
1150 if (lflag) {
1151 if (wp[i]) {
1152 for (; wp[i]; i++) {
1153 if (!bi_getn(wp[i], &n))
1154 return 1;
1155 if (n > 128 && n < 128 + SIGNALS)
1156 n -= 128;
1157 if (n > 0 && n < SIGNALS && sigtraps[n].name)
1158 shprintf("%s\n", sigtraps[n].name);
1159 else
1160 shprintf("%d\n", n);
1161 }
1162 } else if (Flag(FPOSIX)) {
1163 p = null;
1164 for (i = 1; i < SIGNALS; i++, p = space)
1165 if (sigtraps[i].name)
1166 shprintf("%s%s", p, sigtraps[i].name);
1167 shprintf(newline);
1168 } else {
1169 int w, i;
1170 int mess_width;
1171 struct kill_info ki;
1172
1173 for (i = SIGNALS, ki.num_width = 1; i >= 10; i /= 10)
1174 ki.num_width++;
1175 ki.name_width = mess_width = 0;
1176 for (i = 0; i < SIGNALS; i++) {
1177 w = sigtraps[i].name ? strlen(sigtraps[i].name)
1178 : ki.num_width;
1179 if (w > ki.name_width)
1180 ki.name_width = w;
1181 w = strlen(sigtraps[i].mess);
1182 if (w > mess_width)
1183 mess_width = w;
1184 }
1185
1186 print_columns(shl_stdout, SIGNALS - 1,
1187 kill_fmt_entry, (void *) &ki,
1188 ki.num_width + ki.name_width + mess_width + 3);
1189 }
1190 return 0;
1191 }
1192 rv = 0;
1193 sig = t ? t->signal : SIGTERM;
1194 for (; (p = wp[i]); i++) {
1195 if (*p == '%') {
1196 if (j_kill(p, sig))
1197 rv = 1;
1198 } else if (!getn(p, &n)) {
1199 bi_errorf("%s: arguments must be jobs or process ids",
1200 p);
1201 rv = 1;
1202 } else {
1203 /* use killpg if < -1 since -1 does special things for
1204 * some non-killpg-endowed kills
1205 */
1206 if ((n < -1 ? killpg(-n, sig) : kill(n, sig)) < 0) {
1207 bi_errorf("%s: %s", p, strerror(errno));
1208 rv = 1;
1209 }
1210 }
1211 }
1212 return rv;
1213 }
1214
1215 static Getopt user_opt; /* parsing state for getopts builtin command */
1216 static int getopts_noset; /* stop OPTIND assign from resetting state */
1217
1218 void
1219 getopts_reset(val)
1220 int val;
1221 {
1222 if (!getopts_noset && val >= 1) {
1223 ksh_getopt_reset(&user_opt,
1224 GF_NONAME | (Flag(FPOSIX) ? 0 : GF_PLUSOPT));
1225 user_opt.optind = val;
1226 }
1227 }
1228
1229 int
1230 c_getopts(wp)
1231 char **wp;
1232 {
1233 int argc;
1234 const char *options;
1235 const char *var;
1236 int optc;
1237 char buf[3];
1238 struct tbl *vq;
1239
1240 if (ksh_getopt(wp, &builtin_opt, null) == '?')
1241 return 1;
1242 wp += builtin_opt.optind;
1243
1244 options = *wp++;
1245 if (!options) {
1246 bi_errorf("missing options argument");
1247 return 1;
1248 }
1249
1250 var = *wp++;
1251 if (!var) {
1252 bi_errorf("missing name argument");
1253 return 1;
1254 }
1255 if (!*var || *skip_varname(var, TRUE)) {
1256 bi_errorf("%s: is not an identifier", var);
1257 return 1;
1258 }
1259
1260 if (e->loc->next == (struct block *) 0) {
1261 internal_errorf(0, "c_getopts: no argv");
1262 return 1;
1263 }
1264 /* Which arguments are we parsing... */
1265 if (*wp == (char *) 0)
1266 wp = e->loc->next->argv;
1267 else
1268 *--wp = e->loc->next->argv[0];
1269
1270 /* Check that our saved state won't cause a core dump... */
1271 for (argc = 0; wp[argc]; argc++)
1272 ;
1273 if (user_opt.optind > argc
1274 || (user_opt.p != 0
1275 && user_opt.p > strlen(wp[user_opt.optind - 1])))
1276 {
1277 bi_errorf("arguments changed since last call");
1278 return 1;
1279 }
1280
1281 user_opt.optarg = (char *) 0;
1282 optc = ksh_getopt(wp, &user_opt, options);
1283
1284 if (optc >= 0 && optc != '?' && (user_opt.info & GI_PLUS)) {
1285 buf[0] = '+';
1286 buf[1] = optc;
1287 buf[2] = '\0';
1288 } else {
1289 /* POSIX says var is set to ? at end-of-options, at&t ksh
1290 * sets it to null - we go with POSIX...
1291 */
1292 buf[0] = optc < 0 ? '?' : optc;
1293 buf[1] = '\0';
1294 }
1295
1296 /* at&t ksh does not change OPTIND if it was an unknown option.
1297 * Scripts counting on this are prone to break... (ie, don't count
1298 * on this staying).
1299 */
1300 if (optc != '?') {
1301 getopts_noset = 1;
1302 setint(global("OPTIND"), (long) user_opt.optind);
1303 getopts_noset = 0;
1304 }
1305
1306 if (user_opt.optarg == (char *) 0)
1307 unset(global("OPTARG"), 0);
1308 else
1309 setstr(global("OPTARG"), user_opt.optarg);
1310
1311 vq = global(var);
1312 if (vq->flag & RDONLY) {
1313 bi_errorf("%s is readonly", var);
1314 return 1;
1315 }
1316 if (Flag(FEXPORT))
1317 typeset(var, EXPORT, 0, 0, 0);
1318 setstr(vq, buf);
1319
1320 return optc < 0 ? 1 : 0;
1321 }
1322
1323 #ifdef EMACS
1324 int
1325 c_bind(wp)
1326 char **wp;
1327 {
1328 int rv = 0, macro = 0, list = 0;
1329 register char *cp;
1330 int optc;
1331
1332 while ((optc = ksh_getopt(wp, &builtin_opt, "lm")) != EOF)
1333 switch (optc) {
1334 case 'l':
1335 list = 1;
1336 break;
1337 case 'm':
1338 macro = 1;
1339 break;
1340 case '?':
1341 return 1;
1342 }
1343 wp += builtin_opt.optind;
1344
1345 if (*wp == NULL) /* list all */
1346 rv = x_bind((char*)NULL, (char*)NULL, 0, list);
1347
1348 for (; *wp != NULL; wp++) {
1349 cp = strchr(*wp, '=');
1350 if (cp != NULL)
1351 *cp++ = '\0';
1352 if (x_bind(*wp, cp, macro, 0))
1353 rv = 1;
1354 }
1355
1356 return rv;
1357 }
1358 #endif
1359
1360 /* A leading = means assignments before command are kept;
1361 * a leading * means a POSIX special builtin;
1362 * a leading + means a POSIX regular builtin
1363 * (* and + should not be combined).
1364 */
1365 const struct builtin kshbuiltins [] = {
1366 {"+alias", c_alias}, /* no =: at&t manual wrong */
1367 {"+cd", c_cd},
1368 {"+command", c_command},
1369 {"echo", c_print},
1370 {"*=export", c_typeset},
1371 #ifdef HISTORY
1372 {"+fc", c_fc},
1373 #endif /* HISTORY */
1374 {"+getopts", c_getopts},
1375 {"+jobs", c_jobs},
1376 {"+kill", c_kill},
1377 #ifdef KSH
1378 {"let", c_let},
1379 #endif /* KSH */
1380 {"print", c_print},
1381 {"pwd", c_pwd},
1382 {"*=readonly", c_typeset},
1383 {"=typeset", c_typeset},
1384 {"+unalias", c_unalias},
1385 {"whence", c_whence},
1386 #ifdef JOBS
1387 {"+bg", c_fgbg},
1388 {"+fg", c_fgbg},
1389 #endif
1390 #ifdef EMACS
1391 {"bind", c_bind},
1392 #endif
1393 {NULL, NULL}
1394 };
1395