reenabled swaptest. quake should now load data and start on big endian architectures...
[AROS-Contrib.git] / gnu / abc-shell / c_ksh.c
blob52e0d867c7184d6ff11ab4e14870f453320b2490
1 /*
2 * built-in Korn commands: c_*
3 */
5 #include <sys/stat.h>
6 #include <ctype.h>
7 #include "sh.h"
9 int
10 c_cd(char **wp)
12 int optc;
13 int physical = Flag(FPHYSICAL);
14 int cdnode; /* was a node from cdpath added in? */
15 int printpath = 0; /* print where we cd'd? */
16 int rval;
17 struct tbl *pwd_s, *oldpwd_s;
18 XString xs;
19 char *xp;
20 char *dir, *try, *pwd;
21 int phys_path;
22 char *cdpath;
23 char *fdir = NULL;
25 while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
26 switch (optc) {
27 case 'L':
28 physical = 0;
29 break;
30 case 'P':
31 physical = 1;
32 break;
33 case '?':
34 return 1;
36 wp += builtin_opt.optind;
38 pwd_s = global("PWD");
39 oldpwd_s = global("OLDPWD");
41 if (!wp[0]) {
42 /* No arguments - go home */
43 if ((dir = str_val(global("HOME"))) == null) {
44 bi_errorf("no home directory (HOME not set)");
45 return 1;
47 } else if (!wp[1]) {
48 /* One argument: - or dir */
49 dir = wp[0];
50 if (strcmp(dir, "-") == 0) {
51 dir = str_val(oldpwd_s);
52 // dir = convert_path_multi(str_val(oldpwd_s));
53 if (dir == null) {
54 bi_errorf("no OLDPWD");
55 return 1;
57 printpath++;
59 } else if (!wp[2]) {
60 /* Two arguments - substitute arg1 in PWD for arg2 */
61 int ilen, olen, nlen, elen;
62 char *cp;
64 if (!current_wd[0]) {
65 bi_errorf("don't know current directory");
66 return 1;
68 /* substitue arg1 for arg2 in current path.
69 * if the first substitution fails because the cd fails
70 * we could try to find another substitution. For now
71 * we don't
73 if ((cp = strstr(current_wd, wp[0])) == (char *) 0) {
74 bi_errorf("bad substitution");
75 return 1;
77 ilen = cp - current_wd;
78 olen = strlen(wp[0]);
79 nlen = strlen(wp[1]);
80 elen = strlen(current_wd + ilen + olen) + 1;
81 fdir = dir = alloc(ilen + nlen + elen, ATEMP);
82 memcpy(dir, current_wd, ilen);
83 memcpy(dir + ilen, wp[1], nlen);
84 memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
85 printpath++;
87 } else {
88 bi_errorf("too many arguments");
89 return 1;
92 Xinit(xs, xp, PATH, ATEMP);
93 /* xp will have a bogus value after make_path() - set it to 0
94 * so that if it's used, it will cause a dump
97 xp = (char *) 0;
98 (void)xp; // Should be unused.
100 cdpath = str_val(global("CDPATH"));
101 do {
102 cdnode = make_path(current_wd, dir, &cdpath, &xs, &phys_path);
103 if (physical)
104 rval = chdir(try = Xstring(xs, xp) + phys_path);
105 else {
106 simplify_path(Xstring(xs, xp));
107 rval = chdir(try = Xstring(xs, xp));
109 } while (rval < 0 && cdpath != (char *) 0);
111 if (rval < 0) {
112 if (cdnode)
113 bi_errorf("%s: bad directory", dir);
114 else
115 bi_errorf("%s - %s", try, strerror(errno));
116 if (fdir)
117 afree(fdir, ATEMP);
118 return 1;
121 /* Clear out tracked aliases with relative paths */
123 flushcom(0);
125 /* Set OLDPWD (note: unsetting OLDPWD does not disable this
126 * setting in at&t ksh)
128 if (current_wd[0])
129 /* Ignore failure (happens if readonly or integer) */
130 setstr(oldpwd_s, current_wd, KSH_RETURN_ERROR);
132 if (!ISABSPATH(Xstring(xs, xp))) {
133 pwd = (char *) 0;
134 } else
135 if (!physical || !(pwd = get_phys_path(Xstring(xs, xp))))
136 // pwd = Xstring(xs, xp);
137 pwd = convert_path_multi(Xstring(xs, xp));
139 /* Set PWD */
140 if (pwd) {
141 char *ptmp = pwd;
142 set_current_wd(ptmp);
143 /* Ignore failure (happens if readonly or integer) */
144 setstr(pwd_s, ptmp, KSH_RETURN_ERROR);
145 } else {
146 set_current_wd(null);
147 pwd = Xstring(xs, xp);
148 // pwd = convert_path_multi(Xstring(xs, xp));
149 /* XXX unset $PWD? */
151 if (printpath || cdnode)
152 shprintf("%s\n", pwd);
153 if (fdir)
154 afree(fdir, ATEMP);
155 return 0;
159 c_pwd(char **wp)
161 int optc;
162 int physical = Flag(FPHYSICAL);
163 char *p, *freep = NULL;
165 while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
166 switch (optc) {
167 case 'L':
168 physical = 0;
169 break;
170 case 'P':
171 physical = 1;
172 break;
173 case '?':
174 return 1;
176 wp += builtin_opt.optind;
178 if (wp[0]) {
179 bi_errorf("too many arguments");
180 return 1;
182 p = current_wd[0] ? (physical ? get_phys_path(current_wd) : current_wd) :
183 (char *) 0;
184 if (p && access(p, R_OK) < 0)
185 p = (char *) 0;
186 if (!p) {
187 freep = p = ksh_get_wd((char *) 0, 0);
188 if (!p) {
189 bi_errorf("can't get current directory - %s",
190 strerror(errno));
191 return 1;
194 shprintf("%s\n", p);
195 if (freep)
196 afree(freep, ATEMP);
197 return 0;
201 c_print(char **wp)
203 #define PO_NL BIT(0) /* print newline */
204 #define PO_EXPAND BIT(1) /* expand backslash sequences */
205 #define PO_PMINUSMINUS BIT(2) /* print a -- argument */
206 #define PO_HIST BIT(3) /* print to history instead of stdout */
207 #define PO_COPROC BIT(4) /* printing to coprocess: block SIGPIPE */
209 int fd = 1;
210 int flags = PO_EXPAND|PO_NL;
211 char *s;
212 const char *emsg;
213 XString xs;
214 char *xp;
215 if (wp[0][0] == 'e') { /* echo command */
216 int nflags = flags;
218 /* A compromise between sysV and BSD echo commands:
219 * escape sequences are enabled by default, and
220 * -n, -e and -E are recognized if they appear
221 * in arguments with no illegal options (ie, echo -nq
222 * will print -nq).
223 * Different from sysV echo since options are recognized,
224 * different from BSD echo since escape sequences are enabled
225 * by default.
227 wp += 1;
228 while ((s = *wp) && *s == '-' && s[1]) {
229 while (*++s)
230 if (*s == 'n')
231 nflags &= ~PO_NL;
232 else if (*s == 'e')
233 nflags |= PO_EXPAND;
234 else if (*s == 'E')
235 nflags &= ~PO_EXPAND;
236 else
237 /* bad option: don't use nflags, print
238 * argument
240 break;
241 if (*s)
242 break;
243 wp++;
244 flags = nflags;
246 } else {
247 int optc;
248 const char *options = "Rnprsu,";
249 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
250 switch (optc) {
251 case 'R': /* fake BSD echo command */
252 flags |= PO_PMINUSMINUS;
253 flags &= ~PO_EXPAND;
254 options = "ne";
255 break;
256 case 'e':
257 flags |= PO_EXPAND;
258 break;
259 case 'n':
260 flags &= ~PO_NL;
261 break;
262 case 'p':
263 if ((fd = coproc_getfd(W_OK, &emsg)) < 0) {
264 bi_errorf("-p: %s", emsg);
265 return 1;
267 break;
268 case 'r':
269 flags &= ~PO_EXPAND;
270 break;
271 case 's':
272 flags |= PO_HIST;
273 break;
274 case 'u':
275 if (!*(s = builtin_opt.optarg))
276 fd = 0;
277 else if ((fd = check_fd(s, W_OK, &emsg)) < 0) {
278 bi_errorf("-u: %s: %s", s, emsg);
279 return 1;
281 break;
282 case '?':
283 return 1;
285 if (!(builtin_opt.info & GI_MINUSMINUS)) {
286 /* treat a lone - like -- */
287 if (wp[builtin_opt.optind] &&
288 strcmp(wp[builtin_opt.optind], "-") == 0)
289 builtin_opt.optind++;
290 } else if (flags & PO_PMINUSMINUS)
291 builtin_opt.optind--;
292 wp += builtin_opt.optind;
295 Xinit(xs, xp, 128, ATEMP);
297 while (*wp != NULL) {
298 int c;
299 s = *wp;
300 while ((c = *s++) != '\0') {
301 Xcheck(xs, xp);
302 if ((flags & PO_EXPAND) && c == '\\') {
303 int i;
305 switch ((c = *s++)) {
306 /* Oddly enough, \007 seems more portable than
307 * \a (due to HP-UX cc, Ultrix cc, old pcc's,
308 * etc.).
310 case 'a': c = '\007'; break;
311 case 'b': c = '\b'; break;
312 case 'c': flags &= ~PO_NL;
313 continue; /* AT&T brain damage */
314 case 'f': c = '\f'; break;
315 case 'n': c = '\n'; break;
316 case 'r': c = '\r'; break;
317 case 't': c = '\t'; break;
318 case 'v': c = 0x0B; break;
319 case '0':
320 /* Look for an octal number: can have
321 * three digits (not counting the
322 * leading 0). Truly burnt.
324 c = 0;
325 for (i = 0; i < 3; i++) {
326 if (*s >= '0' && *s <= '7')
327 c = c*8 + *s++ - '0';
328 else
329 break;
331 break;
332 case '\0': s--; c = '\\'; break;
333 case '\\': break;
334 default:
335 Xput(xs, xp, '\\');
338 Xput(xs, xp, c);
340 if (*++wp != NULL)
341 Xput(xs, xp, ' ');
343 if (flags & PO_NL)
344 Xput(xs, xp, '\n');
346 if (flags & PO_HIST) {
347 Xput(xs, xp, '\0');
348 source->line++;
349 histsave(source->line, Xstring(xs, xp), 1);
350 Xfree(xs, xp);
351 } else {
352 int n, len = Xlength(xs, xp);
353 int opipe = 0;
355 /* Ensure we aren't killed by a SIGPIPE while writing to
356 * a coprocess. at&t ksh doesn't seem to do this (seems
357 * to just check that the co-process is alive, which is
358 * not enough).
360 if (coproc.write >= 0 && coproc.write == fd) {
361 flags |= PO_COPROC;
362 opipe = block_pipe();
364 for (s = Xstring(xs, xp); len > 0; ) {
365 n = write(fd, s, len);
366 if (n < 0) {
367 if (flags & PO_COPROC)
368 restore_pipe(opipe);
369 if (errno == EINTR) {
370 /* allow user to ^C out */
371 intrcheck();
372 if (flags & PO_COPROC)
373 opipe = block_pipe();
374 continue;
376 /* This doesn't really make sense - could
377 * break scripts (print -p generates
378 * error message).
379 *if (errno == EPIPE)
380 * coproc_write_close(fd);
382 return 1;
384 s += n;
385 len -= n;
387 if (flags & PO_COPROC)
388 restore_pipe(opipe);
391 return 0;
395 c_whence(char **wp)
397 struct tbl *tp;
398 char *id;
399 int pflag = 0, vflag = 0, Vflag = 0;
400 int ret = 0;
401 int optc;
402 int iam_whence = wp[0][0] == 'w';
403 int fcflags;
404 const char *options = iam_whence ? "pv" : "pvV";
406 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1)
407 switch (optc) {
408 case 'p':
409 pflag = 1;
410 break;
411 case 'v':
412 vflag = 1;
413 break;
414 case 'V':
415 Vflag = 1;
416 break;
417 case '?':
418 return 1;
420 wp += builtin_opt.optind;
423 fcflags = FC_BI | FC_PATH | FC_FUNC;
424 if (!iam_whence) {
425 /* Note that -p on its own is deal with in comexec() */
426 if (pflag)
427 fcflags |= FC_DEFPATH;
428 /* Convert command options to whence options - note that
429 * command -pV uses a different path search than whence -v
430 * or whence -pv. This should be considered a feature.
432 vflag = Vflag;
434 if (pflag)
435 fcflags &= ~(FC_BI | FC_FUNC);
437 while ((vflag || ret == 0) && (id = *wp++) != NULL) {
438 tp = NULL;
439 if ((iam_whence || vflag) && !pflag)
440 tp = ktsearch(&keywords, id, hash(id));
441 if (!tp && !pflag) {
442 tp = ktsearch(aliases, id, hash(id));
443 if (tp && !(tp->flag & ISSET))
444 tp = NULL;
446 if (!tp)
447 tp = findcom(id, fcflags);
448 if (vflag || (tp->type != CALIAS && tp->type != CEXEC &&
449 tp->type != CTALIAS))
450 shprintf("%s", id);
451 switch (tp->type) {
452 case CKEYWD:
453 if (vflag)
454 shprintf(" is a reserved word");
455 break;
456 case CALIAS:
457 if (vflag)
458 shprintf(" is an %salias for ",
459 (tp->flag & EXPORTV) ? "exported " :
460 null);
461 if (!iam_whence && !vflag)
462 shprintf("alias %s=", id);
463 print_value_quoted(tp->val.s);
464 break;
465 case CFUNC:
466 if (vflag) {
467 shprintf(" is a");
468 if (tp->flag & EXPORTV)
469 shprintf("n exported");
470 if (tp->flag & TRACE)
471 shprintf(" traced");
472 if (!(tp->flag & ISSET)) {
473 shprintf(" undefined");
474 if (tp->u.fpath)
475 shprintf(" (autoload from %s)",
476 tp->u.fpath);
478 shprintf(" function");
480 break;
481 case CSHELL:
482 if (vflag)
483 shprintf(" is a%s shell builtin",
484 (tp->flag & SPEC_BI) ? " special" : null);
485 break;
486 case CTALIAS:
487 case CEXEC:
488 if (tp->flag & ISSET) {
489 if (vflag) {
490 shprintf(" is ");
491 if (tp->type == CTALIAS)
492 shprintf("a tracked %salias for ",
493 (tp->flag & EXPORTV) ?
494 "exported " : null);
496 shprintf("%s", tp->val.s);
497 } else {
498 if (vflag)
499 shprintf(" not found");
500 ret = 1;
502 break;
503 default:
504 shprintf("%s is *GOK*", id);
505 break;
507 if (vflag || !ret)
508 shprintf(newline);
510 return ret;
513 /* Deal with command -vV - command -p dealt with in comexec() */
515 c_command(char **wp)
517 /* Let c_whence do the work. Note that c_command() must be
518 * a distinct function from c_whence() (tested in comexec()).
520 return c_whence(wp);
523 /* typeset, export, and readonly */
525 c_typeset(char **wp)
527 struct block *l = e->loc;
528 struct tbl *vp, **p;
529 Tflag fset = 0, fclr = 0;
530 int thing = 0, func = 0, local = 0;
531 const char *options = "L#R#UZ#fi#lprtux"; /* see comment below */
532 char *fieldstr, *basestr;
533 int field, base;
534 int optc;
535 Tflag flag;
536 int pflag = 0;
538 switch (**wp) {
539 case 'e': /* export */
540 fset |= EXPORTV;
541 options = "p";
542 break;
543 case 'r': /* readonly */
544 fset |= RDONLY;
545 options = "p";
546 break;
547 case 's': /* set */
548 /* called with 'typeset -' */
549 break;
550 case 't': /* typeset */
551 local = 1;
552 break;
555 fieldstr = basestr = (char *) 0;
556 builtin_opt.flags |= GF_PLUSOPT;
557 /* at&t ksh seems to have 0-9 as options, which are multiplied
558 * to get a number that is used with -L, -R, -Z or -i (eg, -1R2
559 * sets right justify in a field of 12). This allows options
560 * to be grouped in an order (eg, -Lu12), but disallows -i8 -L3 and
561 * does not allow the number to be specified as a separate argument
562 * Here, the number must follow the RLZi option, but is optional
563 * (see the # kludge in ksh_getopt()).
565 while ((optc = ksh_getopt(wp, &builtin_opt, options)) != -1) {
566 flag = 0;
567 switch (optc) {
568 case 'L':
569 flag = LJUST;
570 fieldstr = builtin_opt.optarg;
571 break;
572 case 'R':
573 flag = RJUST;
574 fieldstr = builtin_opt.optarg;
575 break;
576 case 'U':
577 /* at&t ksh uses u, but this conflicts with
578 * upper/lower case. If this option is changed,
579 * need to change the -U below as well
581 flag = INT_U;
582 break;
583 case 'Z':
584 flag = ZEROFIL;
585 fieldstr = builtin_opt.optarg;
586 break;
587 case 'f':
588 func = 1;
589 break;
590 case 'i':
591 flag = INTEGER;
592 basestr = builtin_opt.optarg;
593 break;
594 case 'l':
595 flag = LCASEV;
596 break;
597 case 'p':
598 /* posix export/readonly -p flag.
599 * typset -p is the same as typeset (in pdksh);
600 * here for compatability with ksh93.
602 pflag = 1;
603 break;
604 case 'r':
605 flag = RDONLY;
606 break;
607 case 't':
608 flag = TRACE;
609 break;
610 case 'u':
611 flag = UCASEV_AL; /* upper case / autoload */
612 break;
613 case 'x':
614 flag = EXPORTV;
615 break;
616 case '?':
617 return 1;
619 if (builtin_opt.info & GI_PLUS) {
620 fclr |= flag;
621 fset &= ~flag;
622 thing = '+';
623 } else {
624 fset |= flag;
625 fclr &= ~flag;
626 thing = '-';
630 field = 0;
631 if (fieldstr && !bi_getn(fieldstr, &field))
632 return 1;
633 base = 0;
634 if (basestr && !bi_getn(basestr, &base))
635 return 1;
637 if (!(builtin_opt.info & GI_MINUSMINUS) && wp[builtin_opt.optind] &&
638 (wp[builtin_opt.optind][0] == '-' ||
639 wp[builtin_opt.optind][0] == '+') &&
640 wp[builtin_opt.optind][1] == '\0')
642 thing = wp[builtin_opt.optind][0];
643 builtin_opt.optind++;
646 if (func && ((fset|fclr) & ~(TRACE|UCASEV_AL|EXPORTV))) {
647 bi_errorf("only -t, -u and -x options may be used with -f");
648 return 1;
650 if (wp[builtin_opt.optind]) {
651 /* Take care of exclusions.
652 * At this point, flags in fset are cleared in fclr and vise
653 * versa. This property should be preserved.
655 if (fset & LCASEV) /* LCASEV has priority over UCASEV_AL */
656 fset &= ~UCASEV_AL;
657 if (fset & LJUST) /* LJUST has priority over RJUST */
658 fset &= ~RJUST;
659 if ((fset & (ZEROFIL|LJUST)) == ZEROFIL) { /* -Z implies -ZR */
660 fset |= RJUST;
661 fclr &= ~RJUST;
663 /* Setting these attributes clears the others, unless they
664 * are also set in this command
666 if (fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL | LCASEV |
667 INTEGER | INT_U | INT_L))
668 fclr |= ~fset & (LJUST | RJUST | ZEROFIL | UCASEV_AL | LCASEV |
669 INTEGER | INT_U | INT_L);
672 /* set variables and attributes */
673 if (wp[builtin_opt.optind]) {
674 int i;
675 int rval = 0;
676 struct tbl *f;
678 if (local && !func)
679 fset |= LOCAL;
680 for (i = builtin_opt.optind; wp[i]; i++) {
681 if (func) {
682 f = findfunc(wp[i], hash(wp[i]),
683 (fset&UCASEV_AL) ? true : false);
684 if (!f) {
685 /* at&t ksh does ++rval: bogus */
686 rval = 1;
687 continue;
689 if (fset | fclr) {
690 f->flag |= fset;
691 f->flag &= ~fclr;
692 } else
693 fptreef(shl_stdout, 0,
694 f->flag & FKSH ?
695 "function %s %T\n" :
696 "%s() %T\n", wp[i], f->val.t);
697 } else if (!typeset(wp[i], fset, fclr, field, base)) {
698 bi_errorf("%s: not identifier", wp[i]);
699 return 1;
702 return rval;
705 /* list variables and attributes */
706 flag = fset | fclr; /* no difference at this point.. */
707 if (func) {
708 for (l = e->loc; l; l = l->next) {
709 for (p = ktsort(&l->funs); (vp = *p++); ) {
710 if (flag && (vp->flag & flag) == 0)
711 continue;
712 if (thing == '-')
713 fptreef(shl_stdout, 0, vp->flag & FKSH ?
714 "function %s %T\n" : "%s() %T\n",
715 vp->name, vp->val.t);
716 else
717 shprintf("%s\n", vp->name);
720 } else {
721 for (l = e->loc; l; l = l->next) {
722 for (p = ktsort(&l->vars); (vp = *p++); ) {
723 struct tbl *tvp;
724 int any_set = 0;
726 * See if the parameter is set (for arrays, if any
727 * element is set).
729 for (tvp = vp; tvp; tvp = tvp->u.array)
730 if (tvp->flag & ISSET) {
731 any_set = 1;
732 break;
735 * Check attributes - note that all array elements
736 * have (should have?) the same attributes, so checking
737 * the first is sufficient.
739 * Report an unset param only if the user has
740 * explicitly given it some attribute (like export);
741 * otherwise, after "echo $FOO", we would report FOO...
743 if (!any_set && !(vp->flag & USERATTRIB))
744 continue;
745 if (flag && (vp->flag & flag) == 0)
746 continue;
747 for (; vp; vp = vp->u.array) {
748 /* Ignore array elements that aren't set unless there
749 * are no set elements, in which case the first is
750 * reported on
752 if ((vp->flag&ARRAY) && any_set && !(vp->flag & ISSET))
753 continue;
754 /* no arguments */
755 if (thing == 0 && flag == 0) {
756 /* at&t ksh prints things like export, integer,
757 * leftadj, zerofill, etc., but POSIX says must
758 * be suitable for re-entry...
760 shprintf("typeset ");
761 if ((vp->flag&INTEGER))
762 shprintf("-i ");
763 if ((vp->flag&EXPORTV))
764 shprintf("-x ");
765 if ((vp->flag&RDONLY))
766 shprintf("-r ");
767 if ((vp->flag&TRACE))
768 shprintf("-t ");
769 if ((vp->flag&LJUST))
770 shprintf("-L%d ", vp->u2.field);
771 if ((vp->flag&RJUST))
772 shprintf("-R%d ", vp->u2.field);
773 if ((vp->flag&ZEROFIL))
774 shprintf("-Z ");
775 if ((vp->flag&LCASEV))
776 shprintf("-l ");
777 if ((vp->flag&UCASEV_AL))
778 shprintf("-u ");
779 if ((vp->flag&INT_U))
780 shprintf("-U ");
781 shprintf("%s\n", vp->name);
782 if (vp->flag&ARRAY)
783 break;
784 } else {
785 if (pflag)
786 shprintf("%s ",
787 (flag & EXPORTV) ? "export" : "readonly");
788 if ((vp->flag&ARRAY) && any_set)
789 shprintf("%s[%d]", vp->name, vp->index);
790 else
791 shprintf("%s", vp->name);
792 if (thing == '-' && (vp->flag&ISSET)) {
793 char *s = str_val(vp);
794 shprintf("=");
795 /* at&t ksh can't have justified integers.. */
796 if ((vp->flag & (INTEGER|LJUST|RJUST))
797 == INTEGER)
798 shprintf("%s", s);
799 else
800 print_value_quoted(s);
802 shprintf(newline);
804 /* Only report first `element' of an array with
805 * no set elements.
807 if (!any_set)
808 break;
813 return 0;
817 c_alias(char **wp)
819 struct table *t = aliases;
820 int rv = 0, rflag = 0, tflag, Uflag = 0, pflag = 0;
821 int prefix = 0;
822 Tflag xflag = 0;
823 int optc;
825 builtin_opt.flags |= GF_PLUSOPT;
826 while ((optc = ksh_getopt(wp, &builtin_opt, "dprtUx")) != -1) {
827 prefix = builtin_opt.info & GI_PLUS ? '+' : '-';
828 switch (optc) {
829 case 'd':
830 t = homedirs;
831 break;
832 case 'p':
833 pflag = 1;
834 break;
835 case 'r':
836 rflag = 1;
837 break;
838 case 't':
839 t = taliases;
840 break;
841 case 'U':
842 /* kludge for tracked alias initialization
843 * (don't do a path search, just make an entry)
845 Uflag = 1;
846 break;
847 case 'x':
848 xflag = EXPORTV;
849 break;
850 case '?':
851 return 1;
854 wp += builtin_opt.optind;
856 if (!(builtin_opt.info & GI_MINUSMINUS) && *wp
857 && (wp[0][0] == '-' || wp[0][0] == '+') && wp[0][1] == '\0')
859 prefix = wp[0][0];
860 wp++;
863 tflag = t == taliases;
865 /* "hash -r" means reset all the tracked aliases.. */
866 if (rflag) {
867 static const char *const args[] = {
868 "unalias", "-ta", (const char *) 0
871 if (!tflag || *wp) {
872 shprintf("alias: -r flag can only be used with -t"
873 "and without arguments\n");
874 return 1;
876 ksh_getopt_reset(&builtin_opt, GF_ERROR);
877 return c_unalias((char **) args);
881 if (*wp == NULL) {
882 struct tbl *ap, **p;
884 for (p = ktsort(t); (ap = *p++) != NULL; )
885 if ((ap->flag & (ISSET|xflag)) == (ISSET|xflag)) {
886 if (pflag)
887 shf_puts("alias ", shl_stdout);
888 shf_puts(ap->name, shl_stdout);
889 if (prefix != '+') {
890 shf_putc('=', shl_stdout);
891 print_value_quoted(ap->val.s);
893 shprintf(newline);
897 for (; *wp != NULL; wp++) {
898 char *alias = *wp;
899 char *val = strchr(alias, '=');
900 char *newval;
901 struct tbl *ap;
902 int h;
904 if (val)
905 alias = str_nsave(alias, val++ - alias, ATEMP);
906 h = hash(alias);
907 if (val == NULL && !tflag && !xflag) {
908 ap = ktsearch(t, alias, h);
909 if (ap != NULL && (ap->flag&ISSET)) {
910 if (pflag)
911 shf_puts("alias ", shl_stdout);
912 shf_puts(ap->name, shl_stdout);
913 if (prefix != '+') {
914 shf_putc('=', shl_stdout);
915 print_value_quoted(ap->val.s);
917 shprintf(newline);
918 } else {
919 shprintf("%s alias not found\n", alias);
920 rv = 1;
922 continue;
924 ap = ktenter(t, alias, h);
925 ap->type = tflag ? CTALIAS : CALIAS;
926 /* Are we setting the value or just some flags? */
927 if ((val && !tflag) || (!val && tflag && !Uflag)) {
928 if (ap->flag&ALLOC) {
929 ap->flag &= ~(ALLOC|ISSET);
930 afree((void*)ap->val.s, APERM);
932 /* ignore values for -t (at&t ksh does this) */
933 newval = tflag ? search(alias, path, X_OK, (int *) 0) :
934 val;
935 if (newval) {
936 ap->val.s = str_save(newval, APERM);
937 ap->flag |= ALLOC|ISSET;
938 } else
939 ap->flag &= ~ISSET;
941 ap->flag |= DEFINED;
942 if (prefix == '+')
943 ap->flag &= ~xflag;
944 else
945 ap->flag |= xflag;
946 if (val)
947 afree(alias, ATEMP);
950 return rv;
954 c_unalias(char **wp)
956 struct table *t = aliases;
957 struct tbl *ap;
958 int rv = 0, all = 0;
959 int optc;
961 while ((optc = ksh_getopt(wp, &builtin_opt, "adt")) != -1)
962 switch (optc) {
963 case 'a':
964 all = 1;
965 break;
966 case 'd':
967 t = homedirs;
968 break;
969 case 't':
970 t = taliases;
971 break;
972 case '?':
973 return 1;
975 wp += builtin_opt.optind;
977 for (; *wp != NULL; wp++) {
978 ap = ktsearch(t, *wp, hash(*wp));
979 if (ap == NULL) {
980 rv = 1; /* POSIX */
981 continue;
983 if (ap->flag&ALLOC) {
984 ap->flag &= ~(ALLOC|ISSET);
985 afree((void*)ap->val.s, APERM);
987 ap->flag &= ~(DEFINED|ISSET|EXPORTV);
990 if (all) {
991 struct tstate ts;
993 for (ktwalk(&ts, t); (ap = ktnext(&ts)); ) {
994 if (ap->flag&ALLOC) {
995 ap->flag &= ~(ALLOC|ISSET);
996 afree((void*)ap->val.s, APERM);
998 ap->flag &= ~(DEFINED|ISSET|EXPORTV);
1002 return rv;
1006 c_let(char **wp)
1008 int rv = 1;
1009 long val;
1011 if (wp[1] == (char *) 0) /* at&t ksh does this */
1012 bi_errorf("no arguments");
1013 else
1014 for (wp++; *wp; wp++)
1015 if (!evaluate(*wp, &val, KSH_RETURN_ERROR, true)) {
1016 rv = 2; /* distinguish error from zero result */
1017 break;
1018 } else
1019 rv = val == 0;
1020 return rv;
1024 c_jobs(char **wp)
1026 int optc;
1027 int flag = 0;
1028 int nflag = 0;
1029 int rv = 0;
1031 while ((optc = ksh_getopt(wp, &builtin_opt, "lpnz")) != -1)
1032 switch (optc) {
1033 case 'l':
1034 flag = 1;
1035 break;
1036 case 'p':
1037 flag = 2;
1038 break;
1039 case 'n':
1040 nflag = 1;
1041 break;
1042 case 'z': /* debugging: print zombies */
1043 nflag = -1;
1044 break;
1045 case '?':
1046 return 1;
1048 wp += builtin_opt.optind;
1049 if (!*wp) {
1050 if (j_jobs((char *) 0, flag, nflag))
1051 rv = 1;
1052 } else {
1053 for (; *wp; wp++)
1054 if (j_jobs(*wp, flag, nflag))
1055 rv = 1;
1057 return rv;
1060 struct kill_info {
1061 int num_width;
1062 int name_width;
1064 static char *kill_fmt_entry(void *arg, int i, char *buf, int buflen);
1066 /* format a single kill item */
1067 static char *
1068 kill_fmt_entry(void *arg, int i, char *buf, int buflen)
1070 struct kill_info *ki = (struct kill_info *) arg;
1072 i++;
1073 if (sigtraps[i].name)
1074 shf_snprintf(buf, buflen, "%*d %*s %s",
1075 ki->num_width, i,
1076 ki->name_width, sigtraps[i].name,
1077 sigtraps[i].mess);
1078 else
1079 shf_snprintf(buf, buflen, "%*d %*d %s",
1080 ki->num_width, i,
1081 ki->name_width, sigtraps[i].signal,
1082 sigtraps[i].mess);
1083 return buf;
1088 c_kill(char **wp)
1090 Trap *t = (Trap *) 0;
1091 char *p;
1092 int lflag = 0;
1093 int i, n, rv, sig;
1095 /* assume old style options if -digits or -UPPERCASE */
1096 if ((p = wp[1]) && *p == '-' && (digit(p[1]) || isupper(p[1]))) {
1097 if (!(t = gettrap(p + 1, true))) {
1098 bi_errorf("bad signal `%s'", p + 1);
1099 return 1;
1101 i = (wp[2] && strcmp(wp[2], "--") == 0) ? 3 : 2;
1102 } else {
1103 int optc;
1105 while ((optc = ksh_getopt(wp, &builtin_opt, "ls:")) != -1)
1106 switch (optc) {
1107 case 'l':
1108 lflag = 1;
1109 break;
1110 case 's':
1111 if (!(t = gettrap(builtin_opt.optarg, true))) {
1112 bi_errorf("bad signal `%s'",
1113 builtin_opt.optarg);
1114 return 1;
1116 break;
1117 case '?':
1118 return 1;
1120 i = builtin_opt.optind;
1122 if ((lflag && t) || (!wp[i] && !lflag)) {
1123 shf_fprintf(shl_out,
1124 "Usage: kill [ -s signame | -signum | -signame ] {pid|job}...\n"
1125 " kill -l [exit_status]\n");
1126 bi_errorf(null);
1127 return 1;
1130 if (lflag) {
1131 if (wp[i]) {
1132 for (; wp[i]; i++) {
1133 if (!bi_getn(wp[i], &n))
1134 return 1;
1135 if (n > 128 && n < 128 + NSIG)
1136 n -= 128;
1137 if (n > 0 && n < NSIG && sigtraps[n].name)
1138 shprintf("%s\n", sigtraps[n].name);
1139 else
1140 shprintf("%d\n", n);
1142 } else if (Flag(FPOSIX)) {
1143 p = null;
1144 for (i = 1; i < NSIG; i++, p = space)
1145 if (sigtraps[i].name)
1146 shprintf("%s%s", p, sigtraps[i].name);
1147 shprintf(newline);
1148 } else {
1149 int w, i;
1150 int mess_width;
1151 struct kill_info ki;
1153 for (i = NSIG, ki.num_width = 1; i >= 10; i /= 10)
1154 ki.num_width++;
1155 ki.name_width = mess_width = 0;
1156 for (i = 0; i < NSIG; i++) {
1157 w = sigtraps[i].name ? strlen(sigtraps[i].name) :
1158 ki.num_width;
1159 if (w > ki.name_width)
1160 ki.name_width = w;
1161 w = strlen(sigtraps[i].mess);
1162 if (w > mess_width)
1163 mess_width = w;
1166 print_columns(shl_stdout, NSIG - 1,
1167 kill_fmt_entry, (void *) &ki,
1168 ki.num_width + ki.name_width + mess_width + 3, 1);
1170 return 0;
1172 rv = 0;
1173 sig = t ? t->signal : SIGTERM;
1174 for (; (p = wp[i]); i++) {
1175 if (*p == '%') {
1176 if (j_kill(p, sig))
1177 rv = 1;
1178 } else if (!getn(p, &n)) {
1179 bi_errorf("%s: arguments must be jobs or process IDs",
1181 rv = 1;
1182 } else {
1183 /* use killpg if < -1 since -1 does special things for
1184 * some non-killpg-endowed kills
1186 if ((n < -1 ? killpg(-n, sig) : kill(n, sig)) < 0) {
1187 bi_errorf("%s: %s", p, strerror(errno));
1188 rv = 1;
1192 return rv;
1195 void
1196 getopts_reset(int val)
1198 if (val >= 1) {
1199 ksh_getopt_reset(&user_opt,
1200 GF_NONAME | (Flag(FPOSIX) ? 0 : GF_PLUSOPT));
1201 user_opt.optind = user_opt.uoptind = val;
1206 c_getopts(char **wp)
1208 int argc;
1209 const char *options;
1210 const char *var;
1211 int optc;
1212 int ret;
1213 char buf[3];
1214 struct tbl *vq, *voptarg;
1216 if (ksh_getopt(wp, &builtin_opt, null) == '?')
1217 return 1;
1218 wp += builtin_opt.optind;
1220 options = *wp++;
1221 if (!options) {
1222 bi_errorf("missing options argument");
1223 return 1;
1226 var = *wp++;
1227 if (!var) {
1228 bi_errorf("missing name argument");
1229 return 1;
1231 if (!*var || *skip_varname(var, true)) {
1232 bi_errorf("%s: is not an identifier", var);
1233 return 1;
1236 if (e->loc->next == (struct block *) 0) {
1237 internal_errorf(0, "c_getopts: no argv");
1238 return 1;
1240 /* Which arguments are we parsing... */
1241 if (*wp == (char *) 0)
1242 wp = e->loc->next->argv;
1243 else
1244 *--wp = e->loc->next->argv[0];
1246 /* Check that our saved state won't cause a core dump... */
1247 for (argc = 0; wp[argc]; argc++)
1249 if (user_opt.optind > argc ||
1250 (user_opt.p != 0 &&
1251 user_opt.p > strlen(wp[user_opt.optind - 1])))
1253 bi_errorf("arguments changed since last call");
1254 return 1;
1257 user_opt.optarg = (char *) 0;
1258 optc = ksh_getopt(wp, &user_opt, options);
1260 if (optc >= 0 && optc != '?' && (user_opt.info & GI_PLUS)) {
1261 buf[0] = '+';
1262 buf[1] = optc;
1263 buf[2] = '\0';
1264 } else {
1265 /* POSIX says var is set to ? at end-of-options, at&t ksh
1266 * sets it to null - we go with POSIX...
1268 buf[0] = optc < 0 ? '?' : optc;
1269 buf[1] = '\0';
1272 /* at&t ksh does not change OPTIND if it was an unknown option.
1273 * Scripts counting on this are prone to break... (ie, don't count
1274 * on this staying).
1276 if (optc != '?') {
1277 user_opt.uoptind = user_opt.optind;
1280 voptarg = global("OPTARG");
1281 voptarg->flag &= ~RDONLY; /* at&t ksh clears ro and int */
1282 /* Paranoia: ensure no bizarre results. */
1283 if (voptarg->flag & INTEGER)
1284 typeset("OPTARG", 0, INTEGER, 0, 0);
1285 if (user_opt.optarg == (char *) 0)
1286 unset(voptarg, 0);
1287 else
1288 /* This can't fail (have cleared readonly/integer) */
1289 setstr(voptarg, user_opt.optarg, KSH_RETURN_ERROR);
1291 ret = 0;
1293 vq = global(var);
1294 /* Error message already printed (integer, readonly) */
1295 if (!setstr(vq, buf, KSH_RETURN_ERROR))
1296 ret = 1;
1297 if (Flag(FEXPORT))
1298 typeset(var, EXPORTV, 0, 0, 0);
1300 return optc < 0 ? 1 : ret;
1303 /* contact command - to ensure that people know that they are able to contact me - hnl_dk */
1306 c_contact(char **wp)
1308 shprintf(" .............................................................................\n");
1309 shprintf(" : :\n");
1310 shprintf(" : _____.___. _______ .____. :\n");
1311 shprintf("_____\\__ : |____________ \\ ___/___.____| |_______________ _____ :\n");
1312 shprintf("\\ __ __ ___/___ /__\\___ : |__ : __ / /___/_ /_____\n");
1313 shprintf(" \\ \\| |/ . |/ /// (_\\ ___/ |/ /_ /_\\ /_\\ \\\n");
1314 shprintf(" \\_______._______/|________///____________. |____._________/____________________\\\n");
1315 shprintf(" / |____|\n");
1316 shprintf(" : : spot\n");
1317 shprintf(" : This is abc-shell "ABC_VERSION", the AmigaOS4 POSIX shell :\n");
1318 shprintf(" : the project page of abc-shell: http://sf.net/projects/abc-shell :\n");
1319 shprintf(" : you are welcome to contact me (Henning Nielsen Lund) at hnl_dk@amigaos.dk :\n");
1320 shprintf(" : :\n");
1321 shprintf(" :...........................................................................:\n");
1322 return 0;
1325 /* A leading = means assignments before command are kept;
1326 * a leading * means a POSIX special builtin;
1327 * a leading + means a POSIX regular builtin
1328 * (* and + should not be combined).
1330 const struct builtin kshbuiltins [] = {
1331 {"+alias", c_alias}, /* no =: at&t manual wrong */
1332 {"+cd", c_cd},
1333 {"+command", c_command},
1334 {"echo", c_print},
1335 {"*=export", c_typeset},
1336 #ifdef HISTORY
1337 {"+fc", c_fc},
1338 #endif /* HISTORY */
1339 {"+getopts", c_getopts},
1340 {"+jobs", c_jobs},
1341 {"+kill", c_kill},
1342 {"let", c_let},
1343 {"print", c_print},
1344 {"pwd", c_pwd},
1345 {"*=readonly", c_typeset},
1346 {"=typeset", c_typeset},
1347 {"+unalias", c_unalias},
1348 {"whence", c_whence},
1349 {"contact", c_contact}, /* special abc-shell builtin */
1350 {NULL, NULL}