1 /* $Header: /src/pub/tcsh/tc.func.c,v 3.105 2002/07/12 13:16:18 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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: tc.func.c,v 3.105 2002/07/12 13:16:18 christos Exp $")
38 #include "ed.defns.h" /* for the function names */
43 #endif /* WINNT_NATIVE */
48 #include <afs/kautils.h>
49 long ka_UserAuthenticateGeneral();
59 extern time_t t_period;
60 extern int just_signaled;
61 static bool precmd_active = 0;
62 static bool jobcmd_active = 0; /* GrP */
63 static bool postcmd_active = 0;
64 static bool periodic_active = 0;
65 static bool cwdcmd_active = 0; /* PWP: for cwd_cmd */
66 static bool beepcmd_active = 0;
67 static signalfun_t alm_fun = NULL;
69 static void auto_logout __P((int));
70 static char *xgetpass __P((char *));
71 static void auto_lock __P((int));
73 static void insert __P((struct wordent *, bool));
74 static void insert_we __P((struct wordent *, struct wordent *));
75 static int inlist __P((Char *, Char *));
78 static int tildecompare __P((struct tildecache *, struct tildecache *));
79 static Char *gethomedir __P((Char *));
81 static sigret_t palarm __P((int));
82 static void getremotehost __P((void));
83 #endif /* REMOTEHOST */
90 * expand_lex: Take the given lex and put an expanded version of it in
91 * the string buf. First guy in lex list is ignored; last guy is ^J
92 * which we ignore. Only take lex'es from position 'from' to position
95 * Note: csh sometimes sets bit 8 in characters which causes all kinds
96 * of problems if we don't mask it here. Note: excl's in lexes have been
97 * un-back-slashed and must be re-back-slashed
99 * (PWP: NOTE: this returns a pointer to the END of the string expanded
100 * (in other words, where the NUL is).)
102 /* PWP: this is a combination of the old sprlex() and the expand_lex from
103 the magic-space stuff */
106 expand_lex(buf, bufsiz, sp0, from, to)
112 register struct wordent *sp;
113 register Char *s, *d, *e;
114 register Char prev_c;
118 * Make sure we have enough space to expand into. E.g. we may have
119 * "a|b" turn to "a | b" (from 3 to 5 characters) which is the worst
120 * case scenario (even "a>&! b" turns into "a > & ! b", i.e. 6 to 9
121 * characters -- am I missing any other cases?).
128 e = &buf[bufsiz]; /* for bounds checking */
131 return (buf); /* null lex */
132 if ((sp = sp0->next) == sp0)
133 return (buf); /* nada */
134 if (sp == (sp0 = sp0->prev))
135 return (buf); /* nada */
137 for (i = 0; i < NCARGS; i++) {
138 if ((i >= from) && (i <= to)) { /* if in range */
139 for (s = sp->word; *s && d < e; s++) {
141 * bugfix by Michael Bloom: anything but the current history
142 * character {(PWP) and backslash} seem to be dealt with
146 && (((*s & TRIM) == HIST) ||
147 (((*s & TRIM) == '\'') && (prev_c != '\\')) ||
148 (((*s & TRIM) == '\"') && (prev_c != '\\')) ||
149 (((*s & TRIM) == '\\') && (prev_c != '\\')))) {
164 d--; /* get rid of trailing space */
170 sprlex(buf, bufsiz, sp0)
177 cp = expand_lex(buf, bufsiz, sp0, 0, NCARGS);
184 Itoa(n, s, min_digits, attributes)
187 int min_digits, attributes;
190 * The array size here is derived from
192 * which is guaranteed to be enough for a decimal
193 * representation. We add 1 because integer divide
199 Char buf[CHAR_BIT * sizeof(int) / 3 + 1];
201 unsigned int un; /* handle most negative # too */
202 int pad = (min_digits != 0);
204 if (sizeof(buf) - 1 < min_digits)
205 min_digits = sizeof(buf) - 1;
215 *p++ = un % 10 + '0';
217 } while ((pad && --min_digits > 0) || un != 0);
220 *s++ = *--p | attributes;
235 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
236 extern bool dspmbyte_ls;
239 extern bool color_context_ls;
240 #endif /* COLOR_LS_F */
244 (void) t_search(STRNULL, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
252 stderror(ERR_NAME | ERR_NOMATCH);
255 v = gargv = saveblk(v);
257 for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
261 * We cannot process a flag therefore we let ls do it right.
263 static Char STRls[] = {'l', 's', '\0'};
264 static Char STRmCF[] = {'-', 'C', 'F', '\0', '\0' };
267 struct wordent cmd, *nextword, *lastword;
275 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
277 (void) sighold(SIGINT);
280 xfree((ptr_t) seterr);
287 /* Look at listflags, to add -A to the flags, to get a path
288 of ls if necessary */
289 if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL &&
290 vp->vec[0] != STRNULL) {
291 if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
293 for (cp = vp->vec[0]; *cp; cp++)
311 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
312 nextword->word = Strsave(lspath);
313 lastword->next = nextword;
314 nextword->prev = lastword;
316 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
317 nextword->word = Strsave(STRmCF);
318 lastword->next = nextword;
319 nextword->prev = lastword;
320 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
323 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
324 nextword->word = Strsave(STRmmliteral);
325 lastword->next = nextword;
326 nextword->prev = lastword;
330 if (color_context_ls) {
332 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
333 nextword->word = Strsave(STRmmcolormauto);
334 lastword->next = nextword;
335 nextword->prev = lastword;
337 #endif /* COLOR_LS_F */
339 for (cp = *v; cp; cp = *++v) {
340 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
341 nextword->word = quote(Strsave(cp));
342 lastword->next = nextword;
343 nextword->prev = lastword;
346 lastword->next = &cmd;
349 /* build a syntax tree for the command. */
350 t = syntax(cmd.next, &cmd, 0);
353 /* expand aliases like process() does */
355 /* execute the parse tree. */
356 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE);
357 /* done. free the lex list and parse tree. */
358 freelex(&cmd), freesyn(t);
361 (void) sigsetmask(omask);
363 (void) sigrelse(SIGINT);
367 Char *dp, *tmp, buf[MAXPATHLEN];
369 for (k = 0, i = 0; v[k] != NULL; k++) {
370 tmp = dnormalize(v[k], symlinks == SYM_IGNORE);
371 dp = &tmp[Strlen(tmp) - 1];
372 if (*dp == '/' && dp != tmp)
377 if (stat(short2str(tmp), &st) == -1) {
381 print_by_column(STRNULL, &v[i], k - i, FALSE);
383 xprintf("%S: %s.\n", tmp, strerror(errno));
386 else if (S_ISDIR(st.st_mode)) {
392 print_by_column(STRNULL, &v[i], k - i, FALSE);
394 if (k != 0 && v[1] != NULL)
396 xprintf("%S:\n", tmp);
397 for (cp = tmp, dp = buf; *cp; *dp++ = (*cp++ | QUOTE))
401 (dp[-1] != (Char) (':' | QUOTE)) &&
402 #endif /* WINNT_NATIVE */
403 (dp[-1] != (Char) ('/' | QUOTE)))
408 (void) t_search(buf, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
416 print_by_column(STRNULL, &v[i], k - i, FALSE);
426 static char *defaulttell = "ALL";
427 extern bool GotTermCaps;
440 * Avoid a compiler bug on hpux 9.05
441 * Writing the following as func(a ? b : c) breaks
444 TellTC(short2str(v[1]));
471 (void) strcpy(tv[0], short2str(v[1]));
472 (void) strcpy(tv[1], short2str(v[2]));
476 /* The dowhich() is by:
477 * Andreas Luik <luik@isaak.isa.de>
478 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
489 struct wordent lexp[3];
493 lexp[0].next = &lexp[1];
494 lexp[1].next = &lexp[2];
495 lexp[2].next = &lexp[0];
497 lexp[0].prev = &lexp[2];
498 lexp[1].prev = &lexp[0];
499 lexp[2].prev = &lexp[1];
501 lexp[0].word = STRNULL;
502 lexp[2].word = STRret;
504 if ((vp = adrof1(cmd, &aliases)) != NULL && vp->vec != NULL) {
506 xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd);
511 blkexpand(vp->vec, str);
515 rv = tellmewhat(lexp, str);
532 * We don't want to glob dowhich args because we lose quoteing
533 * E.g. which \ls if ls is aliased will not work correctly if
540 stderror(ERR_NAME | ERR_NOMATCH);
543 v = gargv = saveblk(v);
549 rv &= cmd_expand(*v, NULL);
552 set(STRstatus, Strsave(STR1), VAR_READWRITE);
555 /* Again look at the comment above; since we don't glob, we don't free */
557 blkfree(gargv), gargv = 0;
561 /* PWP: a hack to start up your stopped editor on a single keystroke */
562 /* jbs - fixed hack so it worked :-) 3/28/89 */
567 register struct process *pp, *retp;
568 register char *ep, *vp, *cp, *p;
569 int epl, vpl, pstatus;
571 if ((ep = getenv("EDITOR")) != NULL) { /* if we have a value */
572 if ((p = strrchr(ep, '/')) != NULL) /* if it has a path */
573 ep = p + 1; /* then we want only the last part */
578 if ((vp = getenv("VISUAL")) != NULL) { /* if we have a value */
579 if ((p = strrchr(vp, '/')) != NULL) /* and it has a path */
580 vp = p + 1; /* then we want only the last part */
585 for (vpl = 0; vp[vpl] && !Isspace(vp[vpl]); vpl++)
587 for (epl = 0; ep[epl] && !Isspace(ep[epl]); epl++)
590 if (pcurrent == NULL) /* see if we have any jobs */
591 return NULL; /* nope */
594 for (pp = proclist.p_next; pp; pp = pp->p_next)
595 if (pp->p_procid == pp->p_jobid) {
598 * Only foreground an edit session if it is suspended. Some GUI
599 * editors have may be happily running in a separate window, no
600 * point in foregrounding these if they're already running - webb
602 pstatus = (int) (pp->p_flags & PALLSTATES);
603 if (pstatus != PINTERRUPTED && pstatus != PSTOPPED &&
604 pstatus != PSIGNALED)
607 p = short2str(pp->p_command);
608 /* get the first word */
609 for (cp = p; *cp && !isspace((unsigned char) *cp); cp++)
613 if ((cp = strrchr(p, '/')) != NULL) /* and it has a path */
614 cp = cp + 1; /* then we want only the last part */
616 cp = p; /* else we get all of it */
618 /* if we find either in the current name, fg it */
619 if (strncmp(ep, cp, (size_t) epl) == 0 ||
620 strncmp(vp, cp, (size_t) vpl) == 0) {
623 * If there is a choice, then choose the current process if
624 * available, or the previous process otherwise, or else
625 * anything will do - Robert Webb (robertw@mulga.cs.mu.oz.au).
629 else if (retp == NULL || pp == pprevious)
634 return retp; /* Will be NULL if we didn't find a job */
639 register struct process *pp;
651 omask = sigblock(sigmask(SIGINT));
653 (void) sighold(SIGINT);
655 oGettingInput = GettingInput;
658 ohaderr = haderr; /* we need to ignore setting of haderr due to
659 * process getting stopped by a signal */
660 if (setexit() == 0) { /* come back here after pjwait */
662 (void) alarm(0); /* No autologout */
663 if (!pstart(pp, 1)) {
665 stderror(ERR_BADJOB, pp->p_command, strerror(errno));
669 setalarm(1); /* Autologout back on */
672 GettingInput = oGettingInput;
675 (void) sigsetmask(omask);
677 (void) sigrelse(SIGINT);
686 static char pass[PASSMAX + 1];
690 sigint = (signalfun_t) sigset(SIGINT, SIG_IGN);
691 (void) Rawmode(); /* Make sure, cause we want echo off */
692 if ((fd = open("/dev/tty", O_RDWR)) == -1)
695 xprintf("%s", prm); flush();
697 if (read(fd, &pass[i], 1) < 1 || pass[i] == '\n')
707 (void) sigset(SIGINT, sigint);
713 * Ask the user for his login password to continue working
714 * On systems that have a shadow password, this will only
715 * work for root, but what can we do?
717 * If we fail to get the password, then we log the user out
731 extern char *crypt __P((const char *, const char *));
733 extern char *crypt __P(());
738 #if defined(PW_AUTH) && !defined(XCRYPT)
740 struct authorization *apw;
741 extern char *crypt16 __P((const char *, const char *));
743 # define XCRYPT(a, b) crypt16(a, b)
745 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
746 (apw = getauthuid(euid)) != NULL) /* enhanced ultrix passwd */
747 srpp = apw->a_password;
749 #endif /* PW_AUTH && !XCRYPT */
751 #if defined(PW_SHADOW) && !defined(XCRYPT)
755 # define XCRYPT(a, b) crypt(a, b)
757 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
758 (spw = getspnam(pw->pw_name)) != NULL) /* shadowed passwd */
761 #endif /* PW_SHADOW && !XCRYPT */
765 #define XCRYPT(a, b) crypt(a, b)
767 #if !defined(__MVS__)
768 if ((pw = getpwuid(euid)) != NULL) /* effective user passwd */
769 srpp = pw->pw_passwd;
780 setalarm(0); /* Not for locking any more */
782 (void) sigsetmask(sigblock(0) & ~(sigmask(SIGALRM)));
784 (void) sigrelse(SIGALRM);
787 for (i = 0; i < 5; i++) {
794 if ((safs = varval(STRafsuser)) != STRNULL)
795 afsname = short2str(safs);
797 if ((afsname = getenv("AFSUSER")) == NULL)
798 afsname = pw->pw_name;
800 pp = xgetpass("Password:");
802 crpp = XCRYPT(pp, srpp);
803 if ((strcmp(crpp, srpp) == 0)
805 || (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION,
816 (void) memset(pp, 0, PASSMAX);
817 if (GettingInput && !just_signaled) {
826 xprintf(CGETS(22, 2, "\nIncorrect passwd for %s\n"), pw->pw_name);
828 #endif /* NO_CRYPT */
839 xprintf("auto-logout\n");
840 /* Don't leave the tty in raw mode */
844 set(STRlogout, Strsave(STRautomatic), VAR_READWRITE);
849 GettingInput = FALSE; /* make flush() work to write hist files. Huber*/
860 (void) sigset(SIGALRM, alrmcatch);
861 #endif /* UNRELSIGS */
868 #endif /* !SIGVOID */
872 * Karl Kleinpaste, 21oct1983.
873 * Added precmd(), which checks for the alias
874 * precmd in aliases. If it's there, the alias
875 * is executed as a command. This is done
876 * after mailchk() and just before print-
877 * ing the prompt. Useful for things like printing
878 * one's current directory just before each command.
886 omask = sigblock(sigmask(SIGINT));
888 (void) sighold(SIGINT);
890 if (precmd_active) { /* an error must have been caught */
891 aliasrun(2, STRunalias, STRprecmd);
892 xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
896 if (!whyles && adrof1(STRprecmd, &aliases))
897 aliasrun(1, STRprecmd, NULL);
901 (void) sigsetmask(omask);
903 (void) sigrelse(SIGINT);
913 omask = sigblock(sigmask(SIGINT));
915 (void) sighold(SIGINT);
917 if (postcmd_active) { /* an error must have been caught */
918 aliasrun(2, STRunalias, STRpostcmd);
919 xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
923 if (!whyles && adrof1(STRpostcmd, &aliases))
924 aliasrun(1, STRpostcmd, NULL);
928 (void) sigsetmask(omask);
930 (void) sigrelse(SIGINT);
935 * Paul Placeway 11/24/87 Added cwd_cmd by hacking precmd() into
936 * submission... Run every time $cwd is set (after it is set). Useful
937 * for putting your machine and cwd (or anything else) in an xterm title
946 omask = sigblock(sigmask(SIGINT));
948 (void) sighold(SIGINT);
950 if (cwdcmd_active) { /* an error must have been caught */
951 aliasrun(2, STRunalias, STRcwdcmd);
952 xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
956 if (!whyles && adrof1(STRcwdcmd, &aliases))
957 aliasrun(1, STRcwdcmd, NULL);
961 (void) sigsetmask(omask);
963 (void) sigrelse(SIGINT);
968 * Joachim Hoenig 07/16/91 Added beep_cmd, run every time tcsh wishes
969 * to beep the terminal bell. Useful for playing nice sounds instead.
977 omask = sigblock(sigmask(SIGINT));
979 (void) sighold(SIGINT);
981 if (beepcmd_active) { /* an error must have been caught */
982 aliasrun(2, STRunalias, STRbeepcmd);
983 xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
987 if (!whyles && adrof1(STRbeepcmd, &aliases))
988 aliasrun(1, STRbeepcmd, NULL);
992 (void) sigsetmask(omask);
994 (void) sigrelse(SIGINT);
1000 * Karl Kleinpaste, 18 Jan 1984.
1001 * Added period_cmd(), which executes the alias "periodic" every
1002 * $tperiod minutes. Useful for occasional checking of msgs and such.
1012 omask = sigblock(sigmask(SIGINT));
1013 #else /* !BSDSIGS */
1014 (void) sighold(SIGINT);
1015 #endif /* BSDSIGS */
1016 if (periodic_active) { /* an error must have been caught */
1017 aliasrun(2, STRunalias, STRperiodic);
1018 xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
1021 periodic_active = 1;
1022 if (!whyles && adrof1(STRperiodic, &aliases)) {
1023 vp = varval(STRtperiod);
1024 if (vp == STRNULL) {
1025 aliasrun(1, STRperiodic, NULL);
1028 interval = getn(vp);
1030 if (t - t_period >= interval * 60) {
1032 aliasrun(1, STRperiodic, NULL);
1036 periodic_active = 0;
1038 (void) sigsetmask(omask);
1039 #else /* !BSDSIGS */
1040 (void) sigrelse(SIGINT);
1041 #endif /* BSDSIGS */
1046 * GrP Greg Parker May 2001
1047 * Added job_cmd(), which is run every time a job is started or
1048 * foregrounded. The command is passed a single argument, the string
1049 * used to start the job originally. With precmd, useful for setting
1051 * Cloned from cwd_cmd().
1060 omask = sigblock(sigmask(SIGINT));
1061 #else /* !BSDSIGS */
1062 (void) sighold(SIGINT);
1063 #endif /* BSDSIGS */
1064 if (jobcmd_active) { /* an error must have been caught */
1065 aliasrun(2, STRunalias, STRjobcmd);
1066 xprintf(CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n"));
1070 if (!whyles && adrof1(STRjobcmd, &aliases)) {
1071 struct process *pp = pcurrjob; /* put things back after the hook */
1072 aliasrun(2, STRjobcmd, args);
1078 (void) sigsetmask(omask);
1079 #else /* !BSDSIGS */
1080 (void) sigrelse(SIGINT);
1081 #endif /* BSDSIGS */
1086 * Karl Kleinpaste, 21oct1983.
1087 * Set up a one-word alias command, for use for special things.
1088 * This code is based on the mainline of process().
1091 aliasrun(cnt, s1, s2)
1095 struct wordent w, *new1, *new2; /* for holding alias name */
1096 struct command *t = NULL;
1102 xfree((ptr_t) seterr);
1103 seterr = NULL; /* don't repeatedly print err msg. */
1106 new1 = (struct wordent *) xcalloc(1, sizeof w);
1107 new1->word = Strsave(s1);
1109 /* build a lex list with one word. */
1110 w.next = w.prev = new1;
1111 new1->next = new1->prev = &w;
1114 /* build a lex list with two words. */
1115 new2 = (struct wordent *) xcalloc(1, sizeof w);
1116 new2->word = Strsave(s2);
1117 w.next = new2->prev = new1;
1118 new1->next = w.prev = new2;
1119 new1->prev = new2->next = &w;
1122 /* Save the old status */
1123 status = getn(varval(STRstatus));
1125 /* expand aliases like process() does. */
1127 /* build a syntax tree for the command. */
1128 t = syntax(w.next, &w, 0);
1135 /* catch any errors here */
1137 /* execute the parse tree. */
1139 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1140 * was execute(t, tpgrp);
1142 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
1143 /* done. free the lex list and parse tree. */
1144 freelex(&w), freesyn(t);
1148 * Either precmd, or cwdcmd, or periodic had an error. Call it again so
1149 * that it is removed
1157 * XXX: On the other hand, just interrupting them causes an error too.
1158 * So if we hit ^C in the middle of cwdcmd or periodic the alias gets
1159 * removed. We don't want that. Note that we want to remove precmd
1160 * though, cause that could lead into an infinite loop. This should be
1161 * fixed correctly, but then haderr should give us the whole exit
1162 * status not just true or false.
1164 else if (cwdcmd_active)
1166 else if (beepcmd_active)
1168 else if (periodic_active)
1172 /* reset the error catcher to the old place */
1176 /* Restore status */
1177 set(STRstatus, putn(status), VAR_READWRITE);
1186 unsigned alrm_time = 0, logout_time, lock_time;
1187 time_t cl, nl, sched_dif;
1189 if ((vp = adrof(STRautologout)) != NULL && vp->vec != NULL) {
1190 if ((cp = vp->vec[0]) != 0) {
1191 if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1192 alrm_time = logout_time;
1193 alm_fun = auto_logout;
1196 if ((cp = vp->vec[1]) != 0) {
1197 if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1199 if (alrm_time == 0 || lock_time < alrm_time) {
1200 alrm_time = lock_time;
1201 alm_fun = auto_lock;
1204 else /* lock_time always < alrm_time */
1206 alrm_time -= lock_time;
1210 if ((nl = sched_next()) != -1) {
1212 sched_dif = nl > cl ? nl - cl : 0;
1213 if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) {
1214 alrm_time = ((unsigned) sched_dif) + 1;
1215 alm_fun = sched_run;
1218 (void) alarm(alrm_time); /* Autologout ON */
1221 #undef RMDEBUG /* For now... */
1227 struct wordent *we, *args;
1228 register struct wordent *tmp, *del;
1231 static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'};
1233 #endif /* RMDEBUG */
1236 int ask, doit, star = 0, silent = 0;
1238 if (!adrof(STRrmstar))
1241 tag = varval(STRrmdebug);
1242 #endif /* RMDEBUG */
1244 while (*we->word == ';' && we != cp)
1249 xprintf(CGETS(22, 7, "parsing command line\n"));
1250 #endif /* RMDEBUG */
1251 if (!Strcmp(we->word, STRrm)) {
1253 ask = (*args->word != '-');
1254 while (*args->word == '-' && !silent) { /* check options */
1255 for (charac = (args->word + 1); *charac && !silent; charac++)
1256 silent = (*charac == 'i' || *charac == 'f');
1259 ask = (ask || (!ask && !silent));
1261 for (; !star && *args->word != ';'
1262 && args != cp; args = args->next)
1263 if (!Strcmp(args->word, STRstar))
1266 xprintf(CGETS(22, 8,
1267 "Do you really want to delete all files? [n/y] "));
1269 (void) force_read(SHIN, &c, 1);
1271 * Perhaps we should use the yesexpr from the
1274 doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL);
1275 while (c != '\n' && force_read(SHIN, &c, 1) == 1)
1278 /* remove the command instead */
1281 xprintf(CGETS(22, 9,
1282 "skipping deletion of files!\n"));
1283 #endif /* RMDEBUG */
1285 *tmp->word != '\n' &&
1286 *tmp->word != ';' && tmp != cp;) {
1287 tmp->prev->next = tmp->next;
1288 tmp->next->prev = tmp->prev;
1289 xfree((ptr_t) tmp->word);
1294 if (*tmp->word == ';') {
1295 tmp->prev->next = tmp->next;
1296 tmp->next->prev = tmp->prev;
1297 xfree((ptr_t) tmp->word);
1309 *we->word != ';' && we != cp;
1312 if (*we->word == ';')
1317 xprintf(CGETS(22, 10, "command line now is:\n"));
1318 for (we = cp->next; we != cp; we = we->next)
1319 xprintf("%S ", we->word);
1321 #endif /* RMDEBUG */
1326 /* Check if command is in continue list
1327 and do a "aliasing" if it exists as a job in background */
1329 #undef CNDEBUG /* For now */
1335 register struct process *pp, *np;
1336 Char *cmd, *continue_list, *continue_args_list;
1340 static Char STRcndebug[] =
1341 {'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'};
1342 #endif /* CNDEBUG */
1343 bool in_cont_list, in_cont_arg_list;
1347 tag = varval(STRcndebug);
1348 #endif /* CNDEBUG */
1349 continue_list = varval(STRcontinue);
1350 continue_args_list = varval(STRcontinue_args);
1351 if (*continue_list == '\0' && *continue_args_list == '\0')
1355 while (*we->word == ';' && we != cp)
1360 xprintf(CGETS(22, 11, "parsing command line\n"));
1361 #endif /* CNDEBUG */
1363 in_cont_list = inlist(continue_list, cmd);
1364 in_cont_arg_list = inlist(continue_args_list, cmd);
1365 if (in_cont_list || in_cont_arg_list) {
1368 xprintf(CGETS(22, 12, "in one of the lists\n"));
1369 #endif /* CNDEBUG */
1371 for (pp = proclist.p_next; pp; pp = pp->p_next) {
1372 if (prefix(cmd, pp->p_command)) {
1380 insert(we, in_cont_arg_list);
1384 *we->word != ';' && we != cp;
1387 if (*we->word == ';')
1392 xprintf(CGETS(22, 13, "command line now is:\n"));
1393 for (we = cp->next; we != cp; we = we->next)
1394 xprintf("%S ", we->word);
1396 #endif /* CNDEBUG */
1400 /* The actual "aliasing" of for backgrounds() is done here
1401 with the aid of insert_we(). */
1403 insert(pl, file_args)
1407 struct wordent *now, *last;
1408 Char *cmd, *bcmd, *cp1, *cp2;
1410 Char *pause = STRunderpause;
1411 int p_len = (int) Strlen(pause);
1413 cmd_len = (int) Strlen(pl->word);
1414 cmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 1) * sizeof(Char)));
1415 (void) Strcpy(cmd, pl->word);
1416 /* Do insertions at beginning, first replace command word */
1420 xfree((ptr_t) now->word);
1421 now->word = (Char *) xcalloc(1, (size_t) (5 * sizeof(Char)));
1422 (void) Strcpy(now->word, STRecho);
1424 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1425 now->word = (Char *) xcalloc(1, (size_t) (6 * sizeof(Char)));
1426 (void) Strcpy(now->word, STRbackqpwd);
1429 for (last = now; *last->word != '\n' && *last->word != ';';
1433 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1434 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1435 (void) Strcpy(now->word, STRgt);
1436 insert_we(now, last->prev);
1438 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1439 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1440 (void) Strcpy(now->word, STRbang);
1441 insert_we(now, last->prev);
1443 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1444 now->word = (Char *) xcalloc(1, (size_t) cmd_len + p_len + 4);
1450 while ((*cp1++ = *cp2++) != '\0')
1454 while ((*cp1++ = *cp2++) != '\0')
1456 insert_we(now, last->prev);
1458 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1459 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1460 (void) Strcpy(now->word, STRsemi);
1461 insert_we(now, last->prev);
1462 bcmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 2) * sizeof(Char)));
1466 while ((*cp1++ = *cp2++) != '\0')
1468 now = (struct wordent *) xcalloc(1, (size_t) (sizeof(struct wordent)));
1470 insert_we(now, last->prev);
1473 struct wordent *del;
1476 xfree((ptr_t) now->word);
1477 now->word = (Char *) xcalloc(1,
1478 (size_t) ((cmd_len + 2) * sizeof(Char)));
1482 while ((*cp1++ = *cp2++) != '\0')
1484 for (now = now->next;
1485 *now->word != '\n' && *now->word != ';' && now != pl;) {
1486 now->prev->next = now->next;
1487 now->next->prev = now->prev;
1488 xfree((ptr_t) now->word);
1497 insert_we(new, where)
1498 struct wordent *new, *where;
1502 new->next = where->next;
1504 new->next->prev = new;
1511 register Char *l, *n;
1520 if (*n == '\0' && (*l == ' ' || *l == '\0'))
1526 while (*l && *l != ' ')
1527 l++; /* skip to blank */
1528 while (*l && *l == ' ')
1529 l++; /* and find first nonblank character */
1536 #endif /* BSDJOBS */
1540 * Implement a small cache for tilde names. This is used primarily
1541 * to expand tilde names to directories, but also
1542 * we can find users from their home directories for the tilde
1543 * prompt, on machines where yp lookup is slow this can be a big win...
1544 * As with any cache this can run out of sync, rehash can sync it again.
1546 static struct tildecache {
1554 static int tsize = TILINCR;
1557 tildecompare(p1, p2)
1558 struct tildecache *p1, *p2;
1560 return Strcmp(p1->user, p2->user);
1567 register struct passwd *pp;
1569 char **res, **res1, *cp;
1573 pp = getpwnam(short2str(us));
1579 /* Don't return if root */
1580 if (pp->pw_dir[0] == '/' && pp->pw_dir[1] == '\0')
1584 return Strsave(str2short(pp->pw_dir));
1587 res = hes_resolve(short2str(us), "filsys");
1590 if ((*res) != NULL) {
1592 * Look at the first token to determine how to interpret
1594 * Yes, strtok is evil (it's not thread-safe), but it's also
1597 cp = strtok(*res, " ");
1598 if (strcmp(cp, "AFS") == 0) {
1599 /* next token is AFS pathname.. */
1600 cp = strtok(NULL, " ");
1602 rp = Strsave(str2short(cp));
1603 } else if (strcmp(cp, "NFS") == 0) {
1605 if ((strtok(NULL, " ")) && /* skip remote pathname */
1606 (strtok(NULL, " ")) && /* skip host */
1607 (strtok(NULL, " ")) && /* skip mode */
1608 (cp = strtok(NULL, " "))) {
1609 rp = Strsave(str2short(cp));
1613 for (res1 = res; *res1; res1++)
1616 /* Don't return if root */
1617 if (rp != NULL && rp[0] == '/' && rp[1] == '\0') {
1632 struct tildecache *bp1, *bp2, *bp;
1635 /* Ignore NIS special names */
1636 if (*us == '+' || *us == '-')
1640 tcache = (struct tildecache *) xmalloc((size_t) (TILINCR *
1641 sizeof(struct tildecache)));
1645 for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) {
1648 bp = bp1 + ((bp2 - bp1) >> 1);
1649 if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0)
1657 * Not in the cache, try to get it from the passwd file
1659 hd = gethomedir(us);
1666 tcache[tlength].user = Strsave(us);
1667 tcache[tlength].home = hd;
1668 tcache[tlength++].hlen = (int) Strlen(hd);
1670 qsort((ptr_t) tcache, (size_t) tlength, sizeof(struct tildecache),
1671 (int (*) __P((const void *, const void *))) tildecompare);
1673 if (tlength == tsize) {
1675 tcache = (struct tildecache *) xrealloc((ptr_t) tcache,
1677 sizeof(struct tildecache)));
1683 * Return the username if the directory path passed contains a
1684 * user's home directory in the tilde cache, otherwise return NULL
1685 * hm points to the place where the path became different.
1686 * Special case: Our own home directory.
1687 * If we are passed a null pointer, then we flush the cache.
1697 for (i = 0; i < tlength; i++) {
1698 xfree((ptr_t) tcache[i].home);
1699 xfree((ptr_t) tcache[i].user);
1701 xfree((ptr_t) tcache);
1707 if (((h = varval(STRhome)) != STRNULL) &&
1708 (Strncmp(p = *hm, h, (size_t) (j = (int) Strlen(h))) == 0) &&
1709 (p[j] == '/' || p[j] == '\0')) {
1713 for (i = 0; i < tlength; i++)
1714 if ((Strncmp(p = *hm, tcache[i].home, (size_t)
1715 (j = tcache[i].hlen)) == 0) && (p[j] == '/' || p[j] == '\0')) {
1717 return tcache[i].user;
1724 * PWP: read a bunch of aliases out of a file QUICKLY. The format
1725 * is almost the same as the result of saying "alias > FILE", except
1726 * that saying "aliases > FILE" does not expand non-letters to printable
1738 Char buf[BUFSIZE], line[BUFSIZE];
1739 char tbuf[BUFSIZE + 1], *tmp;
1740 extern bool output_raw; /* PWP: in sh.print.c */
1746 plist(&aliases, VAR_ALL);
1751 gflag = 0, tglob(v);
1755 stderror(ERR_NAME | ERR_NOMATCH);
1758 v = gargv = saveblk(v);
1762 if ((fd = open(tmp = short2str(*v), O_RDONLY)) < 0)
1763 stderror(ERR_NAME | ERR_SYSTEM, tmp, strerror(errno));
1766 if (setexit() == 0) {
1775 if ((n = read(fd, tbuf, BUFSIZE)) <= 0) {
1777 stderror(ERR_SYSTEM, progname, strerror(errno));
1781 for (i = 0; i < n; i++)
1782 buf[i] = (Char) tbuf[i];
1786 if ((*lp++ = *p++) == '\n') {
1791 for (lp = line; *lp; lp++) {
1796 vec = (Char **) xmalloc((size_t)
1797 (2 * sizeof(Char **)));
1798 vec[0] = Strsave(lp);
1800 setq(strip(line), vec, &aliases, VAR_READWRITE);
1811 blkfree(gargv), gargv = 0;
1814 #endif /* OBSOLETE */
1818 * set the shell-level var to 1 or apply change to it.
1826 if ((cp = getenv("SHLVL")) != NULL) {
1834 if (adrof(STRshlvl) != NULL)
1836 Unsetenv(STRKSHLVL);
1841 (void) Itoa(val, buff, 0, 0);
1842 set(STRshlvl, Strsave(buff), VAR_READWRITE);
1843 tsetenv(STRKSHLVL, buff);
1847 set(STRshlvl, SAVE("1"), VAR_READWRITE);
1848 tsetenv(STRKSHLVL, str2short("1"));
1854 * Try to recover from a read error
1861 case -1: /* Make sure that the code is reachable */
1866 #endif /* EWOULDBLOCK */
1868 #if defined(POSIX) && defined(EAGAIN)
1869 # if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN
1872 # endif /* !EWOULDBLOCK || EWOULDBLOCK != EAGAIN */
1873 #endif /* POSIX && EAGAIN */
1879 * Great! we have on suns 3 flavors and 5 names...
1880 * I hope that will cover everything.
1881 * I added some more defines... many systems have different defines.
1882 * Rather than dealing with getting the right includes, we'll just
1883 * cover all the known possibilities here. -- sterling@netcom.com
1886 # define O_NONBLOCK 0
1887 # endif /* O_NONBLOCK */
1890 # endif /* O_NDELAY */
1896 # endif /* _FNBIO */
1899 # endif /* FNONBIO */
1901 # define FNONBLOCK 0
1902 # endif /* FNONBLOCK */
1904 # define _FNONBLOCK 0
1905 # endif /* _FNONBLOCK */
1908 # endif /* FNDELAY */
1911 # endif /* _FNDELAY */
1912 # ifndef FNDLEAY /* Some linux versions have this typo */
1914 # endif /* FNDLEAY */
1915 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
1918 e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK|
1919 FNDELAY|_FNDELAY|FNDLEAY); /* whew! */
1920 if (fcntl(fd, F_SETFL, e) == -1)
1924 # endif /* F_SETFL */
1928 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
1932 # endif /* FIONBIO */
1934 #endif /* FDRETRY */
1954 #ifdef SHORT_STRINGS
1955 /* This strips the quote bit as a side effect */
1956 char *sa = strsave(short2str(a));
1957 char *sb = strsave(short2str(b));
1959 char *sa = strip(strsave(a));
1960 char *sb = strip(strsave(b));
1961 #endif /* SHORT_STRINGS */
1963 #if defined(NLS) && !defined(NOSTRCOLL)
1964 errno = 0; /* strcoll sets errno, another brain-damage */
1966 rv = strcoll(sa, sb);
1969 * We should be checking for errno != 0, but some systems
1970 * forget to reset errno to 0. So we only check for the
1971 * only documented valid errno value for strcoll [EINVAL]
1973 if (errno == EINVAL) {
1976 stderror(ERR_SYSTEM, "strcoll", strerror(errno));
1979 rv = strcmp(sa, sb);
1980 #endif /* NLS && !NOSTRCOLL */
1990 * From: peter@zeus.dialix.oz.au (Peter Wemm)
1991 * If exec() fails look first for a #! [word] [word] ....
1992 * If it is, splice the header into the argument list and retry.
1994 #define HACKBUFSZ 1024 /* Max chars in #! vector */
1995 #define HACKVECSZ 128 /* Max words in #! vector */
2001 unsigned char lbuf[HACKBUFSZ];
2002 char *sargv[HACKVECSZ];
2003 unsigned char *p, *ws;
2006 int fw = 0; /* found at least one word */
2008 #endif /* WINNT_NATIVE */
2010 if (read(fd, (char *) lbuf, HACKBUFSZ) <= 0)
2013 ws = 0; /* word started = 0 */
2015 for (p = lbuf; p < &lbuf[HACKBUFSZ]; )
2021 #endif /* WINNT_NATIVE */
2022 if (ws) { /* a blank after a word.. save it */
2024 #ifndef WINNT_NATIVE
2025 if (sargc < HACKVECSZ - 1)
2026 sargv[sargc++] = ws;
2028 #else /* WINNT_NATIVE */
2029 if (sargc < HACKVECSZ - 1) {
2030 sargv[sargc] = first_word ? NULL: hb_subst(ws);
2031 if (sargv[sargc] == NULL)
2038 #endif /* WINNT_NATIVE */
2043 case '\0': /* Whoa!! what the hell happened */
2046 case '\n': /* The end of the line. */
2050 #endif /* WINNT_NATIVE */
2051 ws) { /* terminate the last word */
2053 #ifndef WINNT_NATIVE
2054 if (sargc < HACKVECSZ - 1)
2055 sargv[sargc++] = ws;
2056 #else /* WINNT_NATIVE */
2057 if (sargc < HACKVECSZ - 1) { /* deal with the 1-word case */
2058 sargv[sargc] = first_word? NULL : hb_subst(ws);
2059 if (sargv[sargc] == NULL)
2063 #endif /* !WINNT_NATIVE */
2065 sargv[sargc] = NULL;
2068 *vp = blk2short(sargv);
2075 if (!ws) /* Start a new word? */
2082 #endif /* HASHBANG */
2093 (void) sigset(snum, SIG_IGN);
2094 #endif /* UNRELSIGS */
2107 const char *host = NULL;
2109 struct sockaddr_storage saddr;
2110 socklen_t len = sizeof(struct sockaddr_storage);
2111 static char hbuf[NI_MAXHOST];
2114 struct sockaddr_in saddr;
2115 int len = sizeof(struct sockaddr_in);
2117 #if defined(UTHOST) && !defined(HAVENOUTMP)
2122 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
2123 (saddr.ss_family == AF_INET6 || saddr.ss_family == AF_INET)) {
2124 int flag = NI_NUMERICHOST;
2126 #ifdef NI_WITHSCOPEID
2127 flag |= NI_WITHSCOPEID;
2129 getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf),
2133 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
2134 saddr.sin_family == AF_INET) {
2136 if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr),
2141 host = inet_ntoa(saddr.sin_addr);
2144 #if defined(UTHOST) && !defined(HAVENOUTMP)
2147 char *name = utmphost();
2148 /* Avoid empty names and local X displays */
2149 if (name != NULL && *name != '\0' && *name != ':') {
2150 struct in_addr addr;
2152 /* Look for host:display.screen */
2154 * There is conflict with IPv6 address and X DISPLAY. So,
2155 * we assume there is no IPv6 address in utmp and don't
2158 if ((sptr = strchr(name, ':')) != NULL)
2160 /* Leave IPv4 address as is */
2162 * we use inet_addr here, not inet_aton because many systems
2163 * have not caught up yet.
2165 addr.s_addr = inet_addr(name);
2166 if (addr.s_addr != (unsigned long)~0)
2172 char dbuf[MAXHOSTNAMELEN], cbuf[MAXHOSTNAMELEN];
2173 struct addrinfo hints, *res = NULL;
2175 memset(&hints, 0, sizeof(hints));
2176 hints.ai_family = PF_UNSPEC;
2177 hints.ai_socktype = SOCK_STREAM;
2178 hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
2179 #if defined(UTHOST) && !defined(HAVENOUTMP)
2180 if (strlen(name) < utmphostsize())
2185 if (getaddrinfo(name, NULL, &hints, &res) != 0)
2187 } else if (gethostname(dbuf, sizeof(dbuf) - 1) == 0 &&
2188 (domain = strchr(dbuf, '.')) != NULL) {
2189 for (s = strchr(name, '.');
2190 s != NULL; s = strchr(s + 1, '.')) {
2191 if (*(s + 1) != '\0' &&
2192 (ptr = strstr(domain, s)) != NULL) {
2194 if (len + strlen(ptr) >= sizeof(cbuf))
2196 strncpy(cbuf, name, len);
2197 strcpy(cbuf + len, ptr);
2198 if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
2205 if (res->ai_canonname != NULL) {
2206 strncpy(hbuf, res->ai_canonname, sizeof(hbuf));
2212 if ((hp = gethostbyname(name)) == NULL) {
2213 /* Try again eliminating the trailing domain */
2214 if ((ptr = strchr(name, '.')) != NULL) {
2216 if ((hp = gethostbyname(name)) != NULL)
2231 tsetenv(STRREMOTEHOST, str2short(host));
2233 #if defined(UTHOST) && !defined(HAVENOUTMP)
2241 * From: <lesv@ppvku.ericsson.se> (Lennart Svensson)
2246 /* Don't get stuck if the resolver does not work! */
2247 signalfun_t osig = sigset(SIGALRM, palarm);
2260 (void) sigset(SIGALRM, osig);
2263 /* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */
2268 #endif /* REMOTEHOST */