1 /* $Header: /src/pub/tcsh/tc.func.c,v 3.94 2000/11/12 02:18:06 christos Exp $ */
3 * tc.func.c: New tcsh builtins.
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: tc.func.c,v 3.94 2000/11/12 02:18:06 christos Exp $")
42 #include "ed.defns.h" /* for the function names */
47 #endif /* WINNT_NATIVE */
52 #include <afs/kautils.h>
53 long ka_UserAuthenticateGeneral();
63 extern time_t t_period;
64 extern int just_signaled;
65 static bool precmd_active = 0;
66 static bool postcmd_active = 0;
67 static bool periodic_active = 0;
68 static bool cwdcmd_active = 0; /* PWP: for cwd_cmd */
69 static bool beepcmd_active = 0;
70 static signalfun_t alm_fun = NULL;
72 static void auto_logout __P((int));
73 static char *xgetpass __P((char *));
74 static void auto_lock __P((int));
76 static void insert __P((struct wordent *, bool));
77 static void insert_we __P((struct wordent *, struct wordent *));
78 static int inlist __P((Char *, Char *));
81 static int tildecompare __P((struct tildecache *, struct tildecache *));
82 static Char *gethomedir __P((Char *));
84 static sigret_t palarm __P((int));
85 static void getremotehost __P((void));
86 #endif /* REMOTEHOST */
93 * expand_lex: Take the given lex and put an expanded version of it in
94 * the string buf. First guy in lex list is ignored; last guy is ^J
95 * which we ignore. Only take lex'es from position 'from' to position
98 * Note: csh sometimes sets bit 8 in characters which causes all kinds
99 * of problems if we don't mask it here. Note: excl's in lexes have been
100 * un-back-slashed and must be re-back-slashed
102 * (PWP: NOTE: this returns a pointer to the END of the string expanded
103 * (in other words, where the NUL is).)
105 /* PWP: this is a combination of the old sprlex() and the expand_lex from
106 the magic-space stuff */
109 expand_lex(buf, bufsiz, sp0, from, to)
115 register struct wordent *sp;
116 register Char *s, *d, *e;
117 register Char prev_c;
121 * Make sure we have enough space to expand into. E.g. we may have
122 * "a|b" turn to "a | b" (from 3 to 5 characters) which is the worst
123 * case scenario (even "a>&! b" turns into "a > & ! b", i.e. 6 to 9
124 * characters -- am I missing any other cases?).
131 e = &buf[bufsiz]; /* for bounds checking */
134 return (buf); /* null lex */
135 if ((sp = sp0->next) == sp0)
136 return (buf); /* nada */
137 if (sp == (sp0 = sp0->prev))
138 return (buf); /* nada */
140 for (i = 0; i < NCARGS; i++) {
141 if ((i >= from) && (i <= to)) { /* if in range */
142 for (s = sp->word; *s && d < e; s++) {
144 * bugfix by Michael Bloom: anything but the current history
145 * character {(PWP) and backslash} seem to be dealt with
149 && (((*s & TRIM) == HIST) ||
150 (((*s & TRIM) == '\'') && (prev_c != '\\')) ||
151 (((*s & TRIM) == '\"') && (prev_c != '\\')) ||
152 (((*s & TRIM) == '\\') && (prev_c != '\\')))) {
167 d--; /* get rid of trailing space */
173 sprlex(buf, bufsiz, sp0)
180 cp = expand_lex(buf, bufsiz, sp0, 0, NCARGS);
187 Itoa(n, s, min_digits, attributes)
190 int min_digits, attributes;
193 * The array size here is derived from
195 * which is guaranteed to be enough for a decimal
196 * representation. We add 1 because integer divide
202 Char buf[CHAR_BIT * sizeof(int) / 3 + 1];
204 unsigned int un; /* handle most negative # too */
205 int pad = (min_digits != 0);
207 if (sizeof(buf) - 1 < min_digits)
208 min_digits = sizeof(buf) - 1;
218 *p++ = un % 10 + '0';
220 } while ((pad && --min_digits > 0) || un != 0);
223 *s++ = *--p | attributes;
238 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
239 extern bool dspmbyte_ls;
242 extern bool color_context_ls;
243 #endif /* COLOR_LS_F */
247 (void) t_search(STRNULL, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
255 stderror(ERR_NAME | ERR_NOMATCH);
258 v = gargv = saveblk(v);
260 for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
264 * We cannot process a flag therefore we let ls do it right.
266 static Char STRls[] = {'l', 's', '\0'};
267 static Char STRmCF[] = {'-', 'C', 'F', '\0', '\0' };
270 struct wordent cmd, *nextword, *lastword;
278 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
280 (void) sighold(SIGINT);
283 xfree((ptr_t) seterr);
290 /* Look at listflags, to add -A to the flags, to get a path
291 of ls if necessary */
292 if ((vp = adrof(STRlistflags)) != NULL && vp->vec[0] != STRNULL) {
293 if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
295 for (cp = vp->vec[0]; *cp; cp++)
313 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
314 nextword->word = Strsave(lspath);
315 lastword->next = nextword;
316 nextword->prev = lastword;
318 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
319 nextword->word = Strsave(STRmCF);
320 lastword->next = nextword;
321 nextword->prev = lastword;
322 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
325 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
326 nextword->word = Strsave(STRmmliteral);
327 lastword->next = nextword;
328 nextword->prev = lastword;
332 if (color_context_ls) {
334 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
335 nextword->word = Strsave(STRmmcolormauto);
336 lastword->next = nextword;
337 nextword->prev = lastword;
339 #endif /* COLOR_LS_F */
341 for (cp = *v; cp; cp = *++v) {
342 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
343 nextword->word = Strsave(cp);
344 lastword->next = nextword;
345 nextword->prev = lastword;
348 lastword->next = &cmd;
351 /* build a syntax tree for the command. */
352 t = syntax(cmd.next, &cmd, 0);
355 /* expand aliases like process() does */
357 /* execute the parse tree. */
358 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL);
359 /* done. free the lex list and parse tree. */
360 freelex(&cmd), freesyn(t);
363 (void) sigsetmask(omask);
365 (void) sigrelse(SIGINT);
369 Char *dp, *tmp, buf[MAXPATHLEN];
371 for (k = 0, i = 0; v[k] != NULL; k++) {
372 tmp = dnormalize(v[k], symlinks == SYM_IGNORE);
373 dp = &tmp[Strlen(tmp) - 1];
374 if (*dp == '/' && dp != tmp)
379 if (stat(short2str(tmp), &st) == -1) {
383 print_by_column(STRNULL, &v[i], k - i, FALSE);
385 xprintf("%S: %s.\n", tmp, strerror(errno));
388 else if (S_ISDIR(st.st_mode)) {
394 print_by_column(STRNULL, &v[i], k - i, FALSE);
396 if (k != 0 && v[1] != NULL)
398 xprintf("%S:\n", tmp);
399 for (cp = tmp, dp = buf; *cp; *dp++ = (*cp++ | QUOTE))
403 (dp[-1] != (Char) (':' | QUOTE)) &&
404 #endif /* WINNT_NATIVE */
405 (dp[-1] != (Char) ('/' | QUOTE)))
410 (void) t_search(buf, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
418 print_by_column(STRNULL, &v[i], k - i, FALSE);
428 static char *defaulttell = "ALL";
429 extern bool GotTermCaps;
442 * Avoid a compiler bug on hpux 9.05
443 * Writing the following as func(a ? b : c) breaks
446 TellTC(short2str(v[1]));
473 (void) strcpy(tv[0], short2str(v[1]));
474 (void) strcpy(tv[1], short2str(v[2]));
478 /* The dowhich() is by:
479 * Andreas Luik <luik@isaak.isa.de>
480 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
491 struct wordent lexp[3];
495 lexp[0].next = &lexp[1];
496 lexp[1].next = &lexp[2];
497 lexp[2].next = &lexp[0];
499 lexp[0].prev = &lexp[2];
500 lexp[1].prev = &lexp[0];
501 lexp[2].prev = &lexp[1];
503 lexp[0].word = STRNULL;
504 lexp[2].word = STRret;
506 if ((vp = adrof1(cmd, &aliases)) != NULL) {
508 xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd);
513 blkexpand(vp->vec, str);
517 rv = tellmewhat(lexp, str);
534 * We don't want to glob dowhich args because we lose quoteing
535 * E.g. which \ls if ls is aliased will not work correctly if
542 stderror(ERR_NAME | ERR_NOMATCH);
545 v = gargv = saveblk(v);
551 rv &= cmd_expand(*v, NULL);
554 set(STRstatus, Strsave(STR1), VAR_READWRITE);
557 /* Again look at the comment above; since we don't glob, we don't free */
559 blkfree(gargv), gargv = 0;
563 /* PWP: a hack to start up your stopped editor on a single keystroke */
564 /* jbs - fixed hack so it worked :-) 3/28/89 */
569 register struct process *pp, *retp;
570 register char *ep, *vp, *cp, *p;
571 int epl, vpl, pstatus;
573 if ((ep = getenv("EDITOR")) != NULL) { /* if we have a value */
574 if ((p = strrchr(ep, '/')) != NULL) /* if it has a path */
575 ep = p + 1; /* then we want only the last part */
580 if ((vp = getenv("VISUAL")) != NULL) { /* if we have a value */
581 if ((p = strrchr(vp, '/')) != NULL) /* and it has a path */
582 vp = p + 1; /* then we want only the last part */
587 for (vpl = 0; vp[vpl] && !Isspace(vp[vpl]); vpl++)
589 for (epl = 0; ep[epl] && !Isspace(ep[epl]); epl++)
592 if (pcurrent == NULL) /* see if we have any jobs */
593 return NULL; /* nope */
596 for (pp = proclist.p_next; pp; pp = pp->p_next)
597 if (pp->p_procid == pp->p_jobid) {
600 * Only foreground an edit session if it is suspended. Some GUI
601 * editors have may be happily running in a separate window, no
602 * point in foregrounding these if they're already running - webb
604 pstatus = (int) (pp->p_flags & PALLSTATES);
605 if (pstatus != PINTERRUPTED && pstatus != PSTOPPED &&
606 pstatus != PSIGNALED)
609 p = short2str(pp->p_command);
610 /* get the first word */
611 for (cp = p; *cp && !isspace((unsigned char) *cp); cp++)
615 if ((cp = strrchr(p, '/')) != NULL) /* and it has a path */
616 cp = cp + 1; /* then we want only the last part */
618 cp = p; /* else we get all of it */
620 /* if we find either in the current name, fg it */
621 if (strncmp(ep, cp, (size_t) epl) == 0 ||
622 strncmp(vp, cp, (size_t) vpl) == 0) {
625 * If there is a choice, then choose the current process if
626 * available, or the previous process otherwise, or else
627 * anything will do - Robert Webb (robertw@mulga.cs.mu.oz.au).
631 else if (retp == NULL || pp == pprevious)
636 return retp; /* Will be NULL if we didn't find a job */
641 register struct process *pp;
653 omask = sigblock(sigmask(SIGINT));
655 (void) sighold(SIGINT);
657 oGettingInput = GettingInput;
660 ohaderr = haderr; /* we need to ignore setting of haderr due to
661 * process getting stopped by a signal */
662 if (setexit() == 0) { /* come back here after pjwait */
664 (void) alarm(0); /* No autologout */
665 if (!pstart(pp, 1)) {
667 stderror(ERR_BADJOB, pp->p_command, strerror(errno));
671 setalarm(1); /* Autologout back on */
674 GettingInput = oGettingInput;
677 (void) sigsetmask(omask);
679 (void) sigrelse(SIGINT);
688 static char pass[PASSMAX + 1];
692 sigint = (signalfun_t) sigset(SIGINT, SIG_IGN);
693 (void) Rawmode(); /* Make sure, cause we want echo off */
694 if ((fd = open("/dev/tty", O_RDWR)) == -1)
697 xprintf("%s", prm); flush();
699 if (read(fd, &pass[i], 1) < 1 || pass[i] == '\n')
709 (void) sigset(SIGINT, sigint);
715 * Ask the user for his login password to continue working
716 * On systems that have a shadow password, this will only
717 * work for root, but what can we do?
719 * If we fail to get the password, then we log the user out
733 extern char *crypt __P((const char *, const char *));
735 extern char *crypt __P(());
740 #if defined(PW_AUTH) && !defined(XCRYPT)
742 struct authorization *apw;
743 extern char *crypt16 __P((const char *, const char *));
745 # define XCRYPT(a, b) crypt16(a, b)
747 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
748 (apw = getauthuid(euid)) != NULL) /* enhanced ultrix passwd */
749 srpp = apw->a_password;
751 #endif /* PW_AUTH && !XCRYPT */
753 #if defined(PW_SHADOW) && !defined(XCRYPT)
757 # define XCRYPT(a, b) crypt(a, b)
759 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
760 (spw = getspnam(pw->pw_name)) != NULL) /* shadowed passwd */
763 #endif /* PW_SHADOW && !XCRYPT */
767 #define XCRYPT(a, b) crypt(a, b)
769 #if !defined(__MVS__)
770 if ((pw = getpwuid(euid)) != NULL) /* effective user passwd */
771 srpp = pw->pw_passwd;
782 setalarm(0); /* Not for locking any more */
784 (void) sigsetmask(sigblock(0) & ~(sigmask(SIGALRM)));
786 (void) sigrelse(SIGALRM);
789 for (i = 0; i < 5; i++) {
796 if ((safs = varval(STRafsuser)) != STRNULL)
797 afsname = short2str(safs);
799 if ((afsname = getenv("AFSUSER")) == NULL)
800 afsname = pw->pw_name;
802 pp = xgetpass("Password:");
804 crpp = XCRYPT(pp, srpp);
805 if ((strcmp(crpp, srpp) == 0)
807 || (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION,
818 (void) memset(pp, 0, PASSMAX);
819 if (GettingInput && !just_signaled) {
828 xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name);
830 #endif /* NO_CRYPT */
841 xprintf("auto-logout\n");
842 /* Don't leave the tty in raw mode */
846 set(STRlogout, Strsave(STRautomatic), VAR_READWRITE);
851 GettingInput = FALSE; /* make flush() work to write hist files. Huber*/
862 (void) sigset(SIGALRM, alrmcatch);
863 #endif /* UNRELSIGS */
870 #endif /* !SIGVOID */
874 * Karl Kleinpaste, 21oct1983.
875 * Added precmd(), which checks for the alias
876 * precmd in aliases. If it's there, the alias
877 * is executed as a command. This is done
878 * after mailchk() and just before print-
879 * ing the prompt. Useful for things like printing
880 * one's current directory just before each command.
888 omask = sigblock(sigmask(SIGINT));
890 (void) sighold(SIGINT);
892 if (precmd_active) { /* an error must have been caught */
893 aliasrun(2, STRunalias, STRprecmd);
894 xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
898 if (!whyles && adrof1(STRprecmd, &aliases))
899 aliasrun(1, STRprecmd, NULL);
903 (void) sigsetmask(omask);
905 (void) sigrelse(SIGINT);
915 omask = sigblock(sigmask(SIGINT));
917 (void) sighold(SIGINT);
919 if (postcmd_active) { /* an error must have been caught */
920 aliasrun(2, STRunalias, STRpostcmd);
921 xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
925 if (!whyles && adrof1(STRpostcmd, &aliases))
926 aliasrun(1, STRpostcmd, NULL);
930 (void) sigsetmask(omask);
932 (void) sigrelse(SIGINT);
937 * Paul Placeway 11/24/87 Added cwd_cmd by hacking precmd() into
938 * submission... Run every time $cwd is set (after it is set). Useful
939 * for putting your machine and cwd (or anything else) in an xterm title
948 omask = sigblock(sigmask(SIGINT));
950 (void) sighold(SIGINT);
952 if (cwdcmd_active) { /* an error must have been caught */
953 aliasrun(2, STRunalias, STRcwdcmd);
954 xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
958 if (!whyles && adrof1(STRcwdcmd, &aliases))
959 aliasrun(1, STRcwdcmd, NULL);
963 (void) sigsetmask(omask);
965 (void) sigrelse(SIGINT);
970 * Joachim Hoenig 07/16/91 Added beep_cmd, run every time tcsh wishes
971 * to beep the terminal bell. Useful for playing nice sounds instead.
979 omask = sigblock(sigmask(SIGINT));
981 (void) sighold(SIGINT);
983 if (beepcmd_active) { /* an error must have been caught */
984 aliasrun(2, STRunalias, STRbeepcmd);
985 xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
989 if (!whyles && adrof1(STRbeepcmd, &aliases))
990 aliasrun(1, STRbeepcmd, NULL);
994 (void) sigsetmask(omask);
996 (void) sigrelse(SIGINT);
1002 * Karl Kleinpaste, 18 Jan 1984.
1003 * Added period_cmd(), which executes the alias "periodic" every
1004 * $tperiod minutes. Useful for occasional checking of msgs and such.
1014 omask = sigblock(sigmask(SIGINT));
1015 #else /* !BSDSIGS */
1016 (void) sighold(SIGINT);
1017 #endif /* BSDSIGS */
1018 if (periodic_active) { /* an error must have been caught */
1019 aliasrun(2, STRunalias, STRperiodic);
1020 xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
1023 periodic_active = 1;
1024 if (!whyles && adrof1(STRperiodic, &aliases)) {
1025 vp = varval(STRtperiod);
1026 if (vp == STRNULL) {
1027 aliasrun(1, STRperiodic, NULL);
1030 interval = getn(vp);
1032 if (t - t_period >= interval * 60) {
1034 aliasrun(1, STRperiodic, NULL);
1038 periodic_active = 0;
1040 (void) sigsetmask(omask);
1041 #else /* !BSDSIGS */
1042 (void) sigrelse(SIGINT);
1043 #endif /* BSDSIGS */
1047 * Karl Kleinpaste, 21oct1983.
1048 * Set up a one-word alias command, for use for special things.
1049 * This code is based on the mainline of process().
1052 aliasrun(cnt, s1, s2)
1056 struct wordent w, *new1, *new2; /* for holding alias name */
1057 struct command *t = NULL;
1063 xfree((ptr_t) seterr);
1064 seterr = NULL; /* don't repeatedly print err msg. */
1067 new1 = (struct wordent *) xcalloc(1, sizeof w);
1068 new1->word = Strsave(s1);
1070 /* build a lex list with one word. */
1071 w.next = w.prev = new1;
1072 new1->next = new1->prev = &w;
1075 /* build a lex list with two words. */
1076 new2 = (struct wordent *) xcalloc(1, sizeof w);
1077 new2->word = Strsave(s2);
1078 w.next = new2->prev = new1;
1079 new1->next = w.prev = new2;
1080 new1->prev = new2->next = &w;
1083 /* Save the old status */
1084 status = getn(varval(STRstatus));
1086 /* expand aliases like process() does. */
1088 /* build a syntax tree for the command. */
1089 t = syntax(w.next, &w, 0);
1096 /* catch any errors here */
1098 /* execute the parse tree. */
1100 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1101 * was execute(t, tpgrp);
1103 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL);
1104 /* done. free the lex list and parse tree. */
1105 freelex(&w), freesyn(t);
1109 * Either precmd, or cwdcmd, or periodic had an error. Call it again so
1110 * that it is removed
1118 * XXX: On the other hand, just interrupting them causes an error too.
1119 * So if we hit ^C in the middle of cwdcmd or periodic the alias gets
1120 * removed. We don't want that. Note that we want to remove precmd
1121 * though, cause that could lead into an infinite loop. This should be
1122 * fixed correctly, but then haderr should give us the whole exit
1123 * status not just true or false.
1125 else if (cwdcmd_active)
1127 else if (beepcmd_active)
1129 else if (periodic_active)
1133 /* reset the error catcher to the old place */
1137 /* Restore status */
1138 set(STRstatus, putn(status), VAR_READWRITE);
1147 unsigned alrm_time = 0, logout_time, lock_time;
1148 time_t cl, nl, sched_dif;
1150 if ((vp = adrof(STRautologout)) != NULL) {
1151 if ((cp = vp->vec[0]) != 0) {
1152 if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1153 alrm_time = logout_time;
1154 alm_fun = auto_logout;
1157 if ((cp = vp->vec[1]) != 0) {
1158 if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1160 if (alrm_time == 0 || lock_time < alrm_time) {
1161 alrm_time = lock_time;
1162 alm_fun = auto_lock;
1165 else /* lock_time always < alrm_time */
1167 alrm_time -= lock_time;
1171 if ((nl = sched_next()) != -1) {
1173 sched_dif = nl > cl ? nl - cl : 0;
1174 if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) {
1175 alrm_time = ((unsigned) sched_dif) + 1;
1176 alm_fun = sched_run;
1179 (void) alarm(alrm_time); /* Autologout ON */
1182 #undef RMDEBUG /* For now... */
1188 struct wordent *we, *args;
1189 register struct wordent *tmp, *del;
1192 static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'};
1194 #endif /* RMDEBUG */
1197 int ask, doit, star = 0, silent = 0;
1199 if (!adrof(STRrmstar))
1202 tag = varval(STRrmdebug);
1203 #endif /* RMDEBUG */
1205 while (*we->word == ';' && we != cp)
1210 xprintf(CGETS(22, 7, "parsing command line\n"));
1211 #endif /* RMDEBUG */
1212 if (!Strcmp(we->word, STRrm)) {
1214 ask = (*args->word != '-');
1215 while (*args->word == '-' && !silent) { /* check options */
1216 for (charac = (args->word + 1); *charac && !silent; charac++)
1217 silent = (*charac == 'i' || *charac == 'f');
1220 ask = (ask || (!ask && !silent));
1222 for (; !star && *args->word != ';'
1223 && args != cp; args = args->next)
1224 if (!Strcmp(args->word, STRstar))
1227 xprintf(CGETS(22, 8,
1228 "Do you really want to delete all files? [n/y] "));
1230 (void) force_read(SHIN, &c, 1);
1232 * Perhaps we should use the yesexpr from the
1235 doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL);
1236 while (c != '\n' && force_read(SHIN, &c, 1) == 1)
1239 /* remove the command instead */
1242 xprintf(CGETS(22, 9,
1243 "skipping deletion of files!\n"));
1244 #endif /* RMDEBUG */
1246 *tmp->word != '\n' &&
1247 *tmp->word != ';' && tmp != cp;) {
1248 tmp->prev->next = tmp->next;
1249 tmp->next->prev = tmp->prev;
1250 xfree((ptr_t) tmp->word);
1255 if (*tmp->word == ';') {
1256 tmp->prev->next = tmp->next;
1257 tmp->next->prev = tmp->prev;
1258 xfree((ptr_t) tmp->word);
1267 *we->word != ';' && we != cp;
1270 if (*we->word == ';')
1275 xprintf(CGETS(22, 10, "command line now is:\n"));
1276 for (we = cp->next; we != cp; we = we->next)
1277 xprintf("%S ", we->word);
1279 #endif /* RMDEBUG */
1284 /* Check if command is in continue list
1285 and do a "aliasing" if it exists as a job in background */
1287 #undef CNDEBUG /* For now */
1293 register struct process *pp, *np;
1294 Char *cmd, *continue_list, *continue_args_list;
1298 static Char STRcndebug[] =
1299 {'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'};
1300 #endif /* CNDEBUG */
1301 bool in_cont_list, in_cont_arg_list;
1305 tag = varval(STRcndebug);
1306 #endif /* CNDEBUG */
1307 continue_list = varval(STRcontinue);
1308 continue_args_list = varval(STRcontinue_args);
1309 if (*continue_list == '\0' && *continue_args_list == '\0')
1313 while (*we->word == ';' && we != cp)
1318 xprintf(CGETS(22, 11, "parsing command line\n"));
1319 #endif /* CNDEBUG */
1321 in_cont_list = inlist(continue_list, cmd);
1322 in_cont_arg_list = inlist(continue_args_list, cmd);
1323 if (in_cont_list || in_cont_arg_list) {
1326 xprintf(CGETS(22, 12, "in one of the lists\n"));
1327 #endif /* CNDEBUG */
1329 for (pp = proclist.p_next; pp; pp = pp->p_next) {
1330 if (prefix(cmd, pp->p_command)) {
1338 insert(we, in_cont_arg_list);
1342 *we->word != ';' && we != cp;
1345 if (*we->word == ';')
1350 xprintf(CGETS(22, 13, "command line now is:\n"));
1351 for (we = cp->next; we != cp; we = we->next)
1352 xprintf("%S ", we->word);
1354 #endif /* CNDEBUG */
1358 /* The actual "aliasing" of for backgrounds() is done here
1359 with the aid of insert_we(). */
1361 insert(pl, file_args)
1365 struct wordent *now, *last;
1366 Char *cmd, *bcmd, *cp1, *cp2;
1368 Char *pause = STRunderpause;
1369 int p_len = (int) Strlen(pause);
1371 cmd_len = (int) Strlen(pl->word);
1372 cmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 1) * sizeof(Char)));
1373 (void) Strcpy(cmd, pl->word);
1374 /* Do insertions at beginning, first replace command word */
1378 xfree((ptr_t) now->word);
1379 now->word = (Char *) xcalloc(1, (size_t) (5 * sizeof(Char)));
1380 (void) Strcpy(now->word, STRecho);
1382 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1383 now->word = (Char *) xcalloc(1, (size_t) (6 * sizeof(Char)));
1384 (void) Strcpy(now->word, STRbackqpwd);
1387 for (last = now; *last->word != '\n' && *last->word != ';';
1391 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1392 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1393 (void) Strcpy(now->word, STRgt);
1394 insert_we(now, last->prev);
1396 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1397 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1398 (void) Strcpy(now->word, STRbang);
1399 insert_we(now, last->prev);
1401 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1402 now->word = (Char *) xcalloc(1, (size_t) cmd_len + p_len + 4);
1408 while ((*cp1++ = *cp2++) != '\0')
1412 while ((*cp1++ = *cp2++) != '\0')
1414 insert_we(now, last->prev);
1416 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1417 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1418 (void) Strcpy(now->word, STRsemi);
1419 insert_we(now, last->prev);
1420 bcmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 2) * sizeof(Char)));
1424 while ((*cp1++ = *cp2++) != '\0')
1426 now = (struct wordent *) xcalloc(1, (size_t) (sizeof(struct wordent)));
1428 insert_we(now, last->prev);
1431 struct wordent *del;
1434 xfree((ptr_t) now->word);
1435 now->word = (Char *) xcalloc(1,
1436 (size_t) ((cmd_len + 2) * sizeof(Char)));
1440 while ((*cp1++ = *cp2++) != '\0')
1442 for (now = now->next;
1443 *now->word != '\n' && *now->word != ';' && now != pl;) {
1444 now->prev->next = now->next;
1445 now->next->prev = now->prev;
1446 xfree((ptr_t) now->word);
1455 insert_we(new, where)
1456 struct wordent *new, *where;
1460 new->next = where->next;
1462 new->next->prev = new;
1469 register Char *l, *n;
1478 if (*n == '\0' && (*l == ' ' || *l == '\0'))
1484 while (*l && *l != ' ')
1485 l++; /* skip to blank */
1486 while (*l && *l == ' ')
1487 l++; /* and find first nonblank character */
1494 #endif /* BSDJOBS */
1498 * Implement a small cache for tilde names. This is used primarily
1499 * to expand tilde names to directories, but also
1500 * we can find users from their home directories for the tilde
1501 * prompt, on machines where yp lookup is slow this can be a big win...
1502 * As with any cache this can run out of sync, rehash can sync it again.
1504 static struct tildecache {
1512 static int tsize = TILINCR;
1515 tildecompare(p1, p2)
1516 struct tildecache *p1, *p2;
1518 return Strcmp(p1->user, p2->user);
1525 register struct passwd *pp;
1527 char **res, **res1, *cp;
1531 pp = getpwnam(short2str(us));
1536 return Strsave(str2short(pp->pw_dir));
1538 res = hes_resolve(short2str(us), "filsys");
1541 extern char *strtok();
1544 * Look at the first token to determine how to interpret
1546 * Yes, strtok is evil (it's not thread-safe), but it's also
1549 cp = strtok(*res, " ");
1550 if (strcmp(cp, "AFS") == 0) {
1551 /* next token is AFS pathname.. */
1552 cp = strtok(NULL, " ");
1554 rp = Strsave(str2short(cp));
1555 } else if (strcmp(cp, "NFS") == 0) {
1557 if ((strtok(NULL, " ")) && /* skip remote pathname */
1558 (strtok(NULL, " ")) && /* skip host */
1559 (strtok(NULL, " ")) && /* skip mode */
1560 (cp = strtok(NULL, " "))) {
1561 rp = Strsave(str2short(cp));
1565 for (res1 = res; *res1; res1++)
1577 struct tildecache *bp1, *bp2, *bp;
1580 /* Ignore NIS special names */
1581 if (*us == '+' || *us == '-')
1585 tcache = (struct tildecache *) xmalloc((size_t) (TILINCR *
1586 sizeof(struct tildecache)));
1590 for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) {
1593 bp = bp1 + ((bp2 - bp1) >> 1);
1594 if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0)
1602 * Not in the cache, try to get it from the passwd file
1604 hd = gethomedir(us);
1611 tcache[tlength].user = Strsave(us);
1612 tcache[tlength].home = hd;
1613 tcache[tlength++].hlen = (int) Strlen(hd);
1615 qsort((ptr_t) tcache, (size_t) tlength, sizeof(struct tildecache),
1616 (int (*) __P((const void *, const void *))) tildecompare);
1618 if (tlength == tsize) {
1620 tcache = (struct tildecache *) xrealloc((ptr_t) tcache,
1622 sizeof(struct tildecache)));
1628 * Return the username if the directory path passed contains a
1629 * user's home directory in the tilde cache, otherwise return NULL
1630 * hm points to the place where the path became different.
1631 * Special case: Our own home directory.
1632 * If we are passed a null pointer, then we flush the cache.
1642 for (i = 0; i < tlength; i++) {
1643 xfree((ptr_t) tcache[i].home);
1644 xfree((ptr_t) tcache[i].user);
1646 xfree((ptr_t) tcache);
1652 if (((h = varval(STRhome)) != STRNULL) &&
1653 (Strncmp(p = *hm, h, (size_t) (j = (int) Strlen(h))) == 0) &&
1654 (p[j] == '/' || p[j] == '\0')) {
1658 for (i = 0; i < tlength; i++)
1659 if ((Strncmp(p = *hm, tcache[i].home, (size_t)
1660 (j = tcache[i].hlen)) == 0) && (p[j] == '/' || p[j] == '\0')) {
1662 return tcache[i].user;
1669 * PWP: read a bunch of aliases out of a file QUICKLY. The format
1670 * is almost the same as the result of saying "alias > FILE", except
1671 * that saying "aliases > FILE" does not expand non-letters to printable
1683 Char buf[BUFSIZE], line[BUFSIZE];
1684 char tbuf[BUFSIZE + 1], *tmp;
1685 extern bool output_raw; /* PWP: in sh.print.c */
1691 plist(&aliases, VAR_ALL);
1696 gflag = 0, tglob(v);
1700 stderror(ERR_NAME | ERR_NOMATCH);
1703 v = gargv = saveblk(v);
1707 if ((fd = open(tmp = short2str(*v), O_RDONLY)) < 0)
1708 stderror(ERR_NAME | ERR_SYSTEM, tmp, strerror(errno));
1711 if (setexit() == 0) {
1720 if ((n = read(fd, tbuf, BUFSIZE)) <= 0) {
1722 stderror(ERR_SYSTEM, progname, strerror(errno));
1726 for (i = 0; i < n; i++)
1727 buf[i] = (Char) tbuf[i];
1731 if ((*lp++ = *p++) == '\n') {
1736 for (lp = line; *lp; lp++) {
1741 vec = (Char **) xmalloc((size_t)
1742 (2 * sizeof(Char **)));
1743 vec[0] = Strsave(lp);
1745 setq(strip(line), vec, &aliases, VAR_READWRITE);
1756 blkfree(gargv), gargv = 0;
1759 #endif /* OBSOLETE */
1763 * set the shell-level var to 1 or apply change to it.
1771 if ((cp = getenv("SHLVL")) != NULL) {
1779 if (adrof(STRshlvl) != NULL)
1781 Unsetenv(STRKSHLVL);
1786 (void) Itoa(val, buff, 0, 0);
1787 set(STRshlvl, Strsave(buff), VAR_READWRITE);
1788 tsetenv(STRKSHLVL, buff);
1792 set(STRshlvl, SAVE("1"), VAR_READWRITE);
1793 tsetenv(STRKSHLVL, str2short("1"));
1799 * Try to recover from a read error
1806 case -1: /* Make sure that the code is reachable */
1811 #endif /* EWOULDBLOCK */
1813 #if defined(POSIX) && defined(EAGAIN)
1814 # if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN
1817 # endif /* !EWOULDBLOCK || EWOULDBLOCK != EAGAIN */
1818 #endif /* POSIX && EAGAIN */
1824 * Great! we have on suns 3 flavors and 5 names...
1825 * I hope that will cover everything.
1826 * I added some more defines... many systems have different defines.
1827 * Rather than dealing with getting the right includes, we'll just
1828 * cover all the known possibilities here. -- sterling@netcom.com
1831 # define O_NONBLOCK 0
1832 # endif /* O_NONBLOCK */
1835 # endif /* O_NDELAY */
1841 # endif /* _FNBIO */
1844 # endif /* FNONBIO */
1846 # define FNONBLOCK 0
1847 # endif /* FNONBLOCK */
1849 # define _FNONBLOCK 0
1850 # endif /* _FNONBLOCK */
1853 # endif /* FNDELAY */
1856 # endif /* _FNDELAY */
1857 # ifndef FNDLEAY /* Some linux versions have this typo */
1859 # endif /* FNDLEAY */
1860 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
1863 e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK|
1864 FNDELAY|_FNDELAY|FNDLEAY); /* whew! */
1865 if (fcntl(fd, F_SETFL, e) == -1)
1869 # endif /* F_SETFL */
1873 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
1877 # endif /* FIONBIO */
1879 #endif /* FDRETRY */
1899 #ifdef SHORT_STRINGS
1900 /* This strips the quote bit as a side effect */
1901 char *sa = strsave(short2str(a));
1902 char *sb = strsave(short2str(b));
1904 char *sa = strip(strsave(a));
1905 char *sb = strip(strsave(b));
1906 #endif /* SHORT_STRINGS */
1908 #if defined(NLS) && !defined(NOSTRCOLL)
1909 errno = 0; /* strcoll sets errno, another brain-damage */
1911 rv = strcoll(sa, sb);
1914 * We should be checking for errno != 0, but some systems
1915 * forget to reset errno to 0. So we only check for the
1916 * only documented valid errno value for strcoll [EINVAL]
1918 if (errno == EINVAL) {
1921 stderror(ERR_SYSTEM, "strcoll", strerror(errno));
1924 rv = strcmp(sa, sb);
1925 #endif /* NLS && !NOSTRCOLL */
1935 * From: peter@zeus.dialix.oz.au (Peter Wemm)
1936 * If exec() fails look first for a #! [word] [word] ....
1937 * If it is, splice the header into the argument list and retry.
1939 #define HACKBUFSZ 1024 /* Max chars in #! vector */
1940 #define HACKVECSZ 128 /* Max words in #! vector */
1946 unsigned char lbuf[HACKBUFSZ];
1947 char *sargv[HACKVECSZ];
1948 unsigned char *p, *ws;
1951 int fw = 0; /* found at least one word */
1953 #endif /* WINNT_NATIVE */
1955 if (read(fd, (char *) lbuf, HACKBUFSZ) <= 0)
1958 ws = 0; /* word started = 0 */
1960 for (p = lbuf; p < &lbuf[HACKBUFSZ]; )
1966 #endif /* WINNT_NATIVE */
1967 if (ws) { /* a blank after a word.. save it */
1969 #ifndef WINNT_NATIVE
1970 if (sargc < HACKVECSZ - 1)
1971 sargv[sargc++] = ws;
1973 #else /* WINNT_NATIVE */
1974 if (sargc < HACKVECSZ - 1) {
1975 sargv[sargc] = first_word ? NULL: hb_subst(ws);
1976 if (sargv[sargc] == NULL)
1983 #endif /* WINNT_NATIVE */
1988 case '\0': /* Whoa!! what the hell happened */
1991 case '\n': /* The end of the line. */
1995 #endif /* WINNT_NATIVE */
1996 ws) { /* terminate the last word */
1998 #ifndef WINNT_NATIVE
1999 if (sargc < HACKVECSZ - 1)
2000 sargv[sargc++] = ws;
2001 #else /* WINNT_NATIVE */
2002 if (sargc < HACKVECSZ - 1) { /* deal with the 1-word case */
2003 sargv[sargc] = first_word? NULL : hb_subst(ws);
2004 if (sargv[sargc] == NULL)
2008 #endif /* !WINNT_NATIVE */
2010 sargv[sargc] = NULL;
2013 *vp = blk2short(sargv);
2020 if (!ws) /* Start a new word? */
2027 #endif /* HASHBANG */
2038 (void) sigset(snum, SIG_IGN);
2039 #endif /* UNRELSIGS */
2052 const char *host = NULL;
2054 struct sockaddr_storage saddr;
2055 int len = sizeof(struct sockaddr_storage);
2056 static char hbuf[NI_MAXHOST];
2059 struct sockaddr_in saddr;
2060 int len = sizeof(struct sockaddr_in);
2062 #if defined(UTHOST) && !defined(HAVENOUTMP)
2066 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1) {
2071 int flag = NI_NUMERICHOST;
2074 #ifdef NI_WITHSCOPEID
2075 flag |= NI_WITHSCOPEID;
2077 getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf),
2082 if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr),
2087 host = inet_ntoa(saddr.sin_addr);
2090 #if defined(UTHOST) && !defined(HAVENOUTMP)
2093 char *name = utmphost();
2094 /* Avoid empty names and local X displays */
2095 if (name != NULL && *name != '\0' && *name != ':') {
2096 struct in_addr addr;
2098 /* Look for host:display.screen */
2100 * There is conflict with IPv6 address and X DISPLAY. So,
2101 * we assume there is no IPv6 address in utmp and don't
2104 if ((sptr = strchr(name, ':')) != NULL)
2106 /* Leave IPv4 address as is */
2108 * we use inet_addr here, not inet_aton because many systems
2109 * have not caught up yet.
2111 addr.s_addr = inet_addr(name);
2112 if (addr.s_addr != (unsigned long)~0)
2118 char dbuf[MAXHOSTNAMELEN], cbuf[MAXHOSTNAMELEN];
2119 struct addrinfo hints, *res = NULL;
2121 memset(&hints, 0, sizeof(hints));
2122 hints.ai_family = PF_UNSPEC;
2123 hints.ai_socktype = SOCK_STREAM;
2124 hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
2125 #if defined(UTHOST) && !defined(HAVENOUTMP)
2126 if (strlen(name) < utmphostsize())
2131 if (getaddrinfo(name, NULL, &hints, &res) != 0)
2133 } else if (gethostname(dbuf, sizeof(dbuf) - 1) == 0 &&
2134 (domain = strchr(dbuf, '.')) != NULL) {
2135 for (s = strchr(name, '.');
2136 s != NULL; s = strchr(s + 1, '.')) {
2137 if (*(s + 1) != '\0' &&
2138 (ptr = strstr(domain, s)) != NULL) {
2140 if (len + strlen(ptr) >= sizeof(cbuf))
2142 strncpy(cbuf, name, len);
2143 strcpy(cbuf + len, ptr);
2144 if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
2151 if (res->ai_canonname != NULL) {
2152 strncpy(hbuf, res->ai_canonname, sizeof(hbuf));
2158 if ((hp = gethostbyname(name)) == NULL) {
2159 /* Try again eliminating the trailing domain */
2160 if ((ptr = strchr(name, '.')) != NULL) {
2162 if ((hp = gethostbyname(name)) != NULL)
2177 tsetenv(STRREMOTEHOST, str2short(host));
2179 #if defined(UTHOST) && !defined(HAVENOUTMP)
2187 * From: <lesv@ppvku.ericsson.se> (Lennart Svensson)
2192 /* Don't get stuck if the resolver does not work! */
2193 signalfun_t osig = sigset(SIGALRM, palarm);
2206 (void) sigset(SIGALRM, osig);
2209 /* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */
2214 #endif /* REMOTEHOST */