1 /* $Header: /src/pub/tcsh/tc.func.c,v 3.119 2005/03/06 03:57:10 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.119 2005/03/06 03:57:10 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 int precmd_active = 0;
62 static int jobcmd_active = 0; /* GrP */
63 static int postcmd_active = 0;
64 static int periodic_active = 0;
65 static int cwdcmd_active = 0; /* PWP: for cwd_cmd */
66 static int beepcmd_active = 0;
67 static signalfun_t alm_fun = NULL;
69 static void auto_logout __P((int));
70 static char *xgetpass __P((const char *));
71 static void auto_lock __P((int));
73 static void insert __P((struct wordent *, int));
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 RETSIGTYPE 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)
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++) {
142 int l = NLSSize(s, -1);
154 * bugfix by Michael Bloom: anything but the current history
155 * character {(PWP) and backslash} seem to be dealt with
159 && (((*s & TRIM) == HIST) ||
160 (((*s & TRIM) == '\'') && (prev_c != '\\')) ||
161 (((*s & TRIM) == '\"') && (prev_c != '\\')) ||
162 (((*s & TRIM) == '\\') && (prev_c != '\\')))) {
177 d--; /* get rid of trailing space */
183 sprlex(buf, bufsiz, sp0)
190 cp = expand_lex(buf, bufsiz, sp0, 0, NCARGS);
197 Itoa(n, s, min_digits, attributes)
200 int min_digits, attributes;
203 * The array size here is derived from
205 * which is guaranteed to be enough for a decimal
206 * representation. We add 1 because integer divide
212 Char buf[CHAR_BIT * sizeof(int) / 3 + 1];
214 unsigned int un; /* handle most negative # too */
215 int pad = (min_digits != 0);
217 if ((int)(sizeof(buf) - 1) < min_digits)
218 min_digits = sizeof(buf) - 1;
228 *p++ = un % 10 + '0';
230 } while ((pad && --min_digits > 0) || un != 0);
233 *s++ = *--p | attributes;
251 (void) t_search(STRNULL, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
259 stderror(ERR_NAME | ERR_NOMATCH);
262 v = gargv = saveblk(v);
264 for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
268 * We cannot process a flag therefore we let ls do it right.
272 struct wordent cmd, *nextword, *lastword;
280 omask = sigblock(sigmask(SIGINT)) & ~sigmask(SIGINT);
282 (void) sighold(SIGINT);
285 xfree((ptr_t) seterr);
292 /* Look at listflags, to add -A to the flags, to get a path
293 of ls if necessary */
294 if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL &&
295 vp->vec[0] != STRNULL) {
296 if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
298 for (cp = vp->vec[0]; *cp; cp++)
316 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
317 nextword->word = Strsave(lspath);
318 lastword->next = nextword;
319 nextword->prev = lastword;
321 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
322 nextword->word = Strsave(STRmCF);
323 lastword->next = nextword;
324 nextword->prev = lastword;
325 #if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
328 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
329 nextword->word = Strsave(STRmmliteral);
330 lastword->next = nextword;
331 nextword->prev = lastword;
335 if (color_context_ls) {
337 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
338 nextword->word = Strsave(STRmmcolormauto);
339 lastword->next = nextword;
340 nextword->prev = lastword;
342 #endif /* COLOR_LS_F */
344 for (cp = *v; cp; cp = *++v) {
345 nextword = (struct wordent *) xcalloc(1, sizeof cmd);
346 nextword->word = quote(Strsave(cp));
347 lastword->next = nextword;
348 nextword->prev = lastword;
351 lastword->next = &cmd;
354 /* build a syntax tree for the command. */
355 t = syntax(cmd.next, &cmd, 0);
358 /* expand aliases like process() does */
360 /* execute the parse tree. */
361 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, FALSE);
362 /* done. free the lex list and parse tree. */
363 freelex(&cmd), freesyn(t);
366 (void) sigsetmask(omask);
368 (void) sigrelse(SIGINT);
372 Char *dp, *tmp, buf[MAXPATHLEN];
374 for (k = 0, i = 0; v[k] != NULL; k++) {
375 tmp = dnormalize(v[k], symlinks == SYM_IGNORE);
376 dp = &tmp[Strlen(tmp) - 1];
377 if (*dp == '/' && dp != tmp)
382 if (stat(short2str(tmp), &st) == -1) {
386 print_by_column(STRNULL, &v[i], k - i, FALSE);
388 xprintf("%S: %s.\n", tmp, strerror(errno));
391 else if (S_ISDIR(st.st_mode)) {
397 print_by_column(STRNULL, &v[i], k - i, FALSE);
399 if (k != 0 && v[1] != NULL)
401 xprintf("%S:\n", tmp);
402 for (cp = tmp, dp = buf; *cp; *dp++ = (*cp++ | QUOTE))
406 (dp[-1] != (Char) (':' | QUOTE)) &&
407 #endif /* WINNT_NATIVE */
408 (dp[-1] != (Char) ('/' | QUOTE)))
413 (void) t_search(buf, NULL, LIST, 0, TW_ZERO, 0, STRNULL, 0);
421 print_by_column(STRNULL, &v[i], k - i, FALSE);
431 extern int GotTermCaps;
470 (void) strcpy(tv[0], short2str(v[1]));
471 (void) strcpy(tv[1], short2str(v[2]));
475 /* The dowhich() is by:
476 * Andreas Luik <luik@isaak.isa.de>
477 * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung
488 struct wordent lexp[3];
492 lexp[0].next = &lexp[1];
493 lexp[1].next = &lexp[2];
494 lexp[2].next = &lexp[0];
496 lexp[0].prev = &lexp[2];
497 lexp[1].prev = &lexp[0];
498 lexp[2].prev = &lexp[1];
500 lexp[0].word = STRNULL;
501 lexp[2].word = STRret;
503 if ((vp = adrof1(cmd, &aliases)) != NULL && vp->vec != NULL) {
505 xprintf(CGETS(22, 1, "%S: \t aliased to "), cmd);
510 blkexpand(vp->vec, str);
514 rv = tellmewhat(lexp, str);
531 * We don't want to glob dowhich args because we lose quoteing
532 * E.g. which \ls if ls is aliased will not work correctly if
539 stderror(ERR_NAME | ERR_NOMATCH);
542 v = gargv = saveblk(v);
548 rv &= cmd_expand(*v, NULL);
551 set(STRstatus, Strsave(STR1), VAR_READWRITE);
554 /* Again look at the comment above; since we don't glob, we don't free */
556 blkfree(gargv), gargv = 0;
560 /* PWP: a hack to start up your stopped editor on a single keystroke */
561 /* jbs - fixed hack so it worked :-) 3/28/89 */
566 struct process *pp, *retp;
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((unsigned char)vp[vpl]); vpl++)
587 for (epl = 0; ep[epl] && !isspace((unsigned char)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 */
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|O_LARGEFILE)) == -1)
695 xprintf("%s", prm); flush();
697 if (read(fd, &pass[i], 1) < 1 || pass[i] == '\n')
707 (void) sigset(SIGINT, sigint);
714 extern char *crypt __P(());
722 * Ask the user for his login password to continue working
723 * On systems that have a shadow password, this will only
724 * work for root, but what can we do?
726 * If we fail to get the password, then we log the user out
742 #if defined(HAVE_AUTH_H)
744 struct authorization *apw;
745 extern char *crypt16 __P((const char *, const char *));
747 # define XCRYPT(a, b) crypt16(a, b)
749 if ((pw = getpwuid(euid)) != NULL && /* effective user passwd */
750 (apw = getauthuid(euid)) != NULL) /* enhanced ultrix passwd */
751 srpp = apw->a_password;
753 #elif defined(HAVE_SHADOW_H)
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 */
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*/
861 (void) sigset(SIGALRM, alrmcatch);
862 #endif /* UNRELSIGS */
870 * Karl Kleinpaste, 21oct1983.
871 * Added precmd(), which checks for the alias
872 * precmd in aliases. If it's there, the alias
873 * is executed as a command. This is done
874 * after mailchk() and just before print-
875 * ing the prompt. Useful for things like printing
876 * one's current directory just before each command.
884 omask = sigblock(sigmask(SIGINT));
886 (void) sighold(SIGINT);
888 if (precmd_active) { /* an error must have been caught */
889 aliasrun(2, STRunalias, STRprecmd);
890 xprintf(CGETS(22, 3, "Faulty alias 'precmd' removed.\n"));
894 if (!whyles && adrof1(STRprecmd, &aliases))
895 aliasrun(1, STRprecmd, NULL);
899 (void) sigsetmask(omask);
901 (void) sigrelse(SIGINT);
911 omask = sigblock(sigmask(SIGINT));
913 (void) sighold(SIGINT);
915 if (postcmd_active) { /* an error must have been caught */
916 aliasrun(2, STRunalias, STRpostcmd);
917 xprintf(CGETS(22, 3, "Faulty alias 'postcmd' removed.\n"));
921 if (!whyles && adrof1(STRpostcmd, &aliases))
922 aliasrun(1, STRpostcmd, NULL);
926 (void) sigsetmask(omask);
928 (void) sigrelse(SIGINT);
933 * Paul Placeway 11/24/87 Added cwd_cmd by hacking precmd() into
934 * submission... Run every time $cwd is set (after it is set). Useful
935 * for putting your machine and cwd (or anything else) in an xterm title
944 omask = sigblock(sigmask(SIGINT));
946 (void) sighold(SIGINT);
948 if (cwdcmd_active) { /* an error must have been caught */
949 aliasrun(2, STRunalias, STRcwdcmd);
950 xprintf(CGETS(22, 4, "Faulty alias 'cwdcmd' removed.\n"));
954 if (!whyles && adrof1(STRcwdcmd, &aliases))
955 aliasrun(1, STRcwdcmd, NULL);
959 (void) sigsetmask(omask);
961 (void) sigrelse(SIGINT);
966 * Joachim Hoenig 07/16/91 Added beep_cmd, run every time tcsh wishes
967 * to beep the terminal bell. Useful for playing nice sounds instead.
975 omask = sigblock(sigmask(SIGINT));
977 (void) sighold(SIGINT);
979 if (beepcmd_active) { /* an error must have been caught */
980 aliasrun(2, STRunalias, STRbeepcmd);
981 xprintf(CGETS(22, 5, "Faulty alias 'beepcmd' removed.\n"));
985 if (!whyles && adrof1(STRbeepcmd, &aliases))
986 aliasrun(1, STRbeepcmd, NULL);
990 (void) sigsetmask(omask);
992 (void) sigrelse(SIGINT);
998 * Karl Kleinpaste, 18 Jan 1984.
999 * Added period_cmd(), which executes the alias "periodic" every
1000 * $tperiod minutes. Useful for occasional checking of msgs and such.
1010 omask = sigblock(sigmask(SIGINT));
1011 #else /* !BSDSIGS */
1012 (void) sighold(SIGINT);
1013 #endif /* BSDSIGS */
1014 if (periodic_active) { /* an error must have been caught */
1015 aliasrun(2, STRunalias, STRperiodic);
1016 xprintf(CGETS(22, 6, "Faulty alias 'periodic' removed.\n"));
1019 periodic_active = 1;
1020 if (!whyles && adrof1(STRperiodic, &aliases)) {
1021 vp = varval(STRtperiod);
1022 if (vp == STRNULL) {
1023 aliasrun(1, STRperiodic, NULL);
1026 interval = getn(vp);
1028 if (t - t_period >= interval * 60) {
1030 aliasrun(1, STRperiodic, NULL);
1034 periodic_active = 0;
1036 (void) sigsetmask(omask);
1037 #else /* !BSDSIGS */
1038 (void) sigrelse(SIGINT);
1039 #endif /* BSDSIGS */
1044 * GrP Greg Parker May 2001
1045 * Added job_cmd(), which is run every time a job is started or
1046 * foregrounded. The command is passed a single argument, the string
1047 * used to start the job originally. With precmd, useful for setting
1049 * Cloned from cwd_cmd().
1058 omask = sigblock(sigmask(SIGINT));
1059 #else /* !BSDSIGS */
1060 (void) sighold(SIGINT);
1061 #endif /* BSDSIGS */
1062 if (jobcmd_active) { /* an error must have been caught */
1063 aliasrun(2, STRunalias, STRjobcmd);
1064 xprintf(CGETS(22, 14, "Faulty alias 'jobcmd' removed.\n"));
1068 if (!whyles && adrof1(STRjobcmd, &aliases)) {
1069 struct process *pp = pcurrjob; /* put things back after the hook */
1070 aliasrun(2, STRjobcmd, args);
1076 (void) sigsetmask(omask);
1077 #else /* !BSDSIGS */
1078 (void) sigrelse(SIGINT);
1079 #endif /* BSDSIGS */
1084 * Karl Kleinpaste, 21oct1983.
1085 * Set up a one-word alias command, for use for special things.
1086 * This code is based on the mainline of process().
1089 aliasrun(cnt, s1, s2)
1093 struct wordent w, *new1, *new2; /* for holding alias name */
1094 struct command *t = NULL;
1100 xfree((ptr_t) seterr);
1101 seterr = NULL; /* don't repeatedly print err msg. */
1104 new1 = (struct wordent *) xcalloc(1, sizeof w);
1105 new1->word = Strsave(s1);
1107 /* build a lex list with one word. */
1108 w.next = w.prev = new1;
1109 new1->next = new1->prev = &w;
1112 /* build a lex list with two words. */
1113 new2 = (struct wordent *) xcalloc(1, sizeof w);
1114 new2->word = Strsave(s2);
1115 w.next = new2->prev = new1;
1116 new1->next = w.prev = new2;
1117 new1->prev = new2->next = &w;
1120 /* Save the old status */
1121 status = getn(varval(STRstatus));
1123 /* expand aliases like process() does. */
1125 /* build a syntax tree for the command. */
1126 t = syntax(w.next, &w, 0);
1133 /* catch any errors here */
1135 /* execute the parse tree. */
1137 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1138 * was execute(t, tpgrp);
1140 execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
1141 /* done. free the lex list and parse tree. */
1142 freelex(&w), freesyn(t);
1146 * Either precmd, or cwdcmd, or periodic had an error. Call it again so
1147 * that it is removed
1155 * XXX: On the other hand, just interrupting them causes an error too.
1156 * So if we hit ^C in the middle of cwdcmd or periodic the alias gets
1157 * removed. We don't want that. Note that we want to remove precmd
1158 * though, cause that could lead into an infinite loop. This should be
1159 * fixed correctly, but then haderr should give us the whole exit
1160 * status not just true or false.
1162 else if (cwdcmd_active)
1164 else if (beepcmd_active)
1166 else if (periodic_active)
1170 /* reset the error catcher to the old place */
1174 /* Restore status */
1175 set(STRstatus, putn(status), VAR_READWRITE);
1184 unsigned alrm_time = 0, logout_time, lock_time;
1185 time_t cl, nl, sched_dif;
1187 if ((vp = adrof(STRautologout)) != NULL && vp->vec != NULL) {
1188 if ((cp = vp->vec[0]) != 0) {
1189 if ((logout_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1192 * Solaris alarm(2) uses a timer based in clock ticks
1193 * internally so it multiplies our value with CLK_TCK...
1194 * Of course that can overflow leading to unexpected
1195 * results, so we clip it here. Grr. Where is that
1198 if (logout_time >= 0x7fffffff / CLK_TCK)
1199 logout_time = 0x7fffffff / CLK_TCK;
1200 #endif /* SOLARIS2 */
1201 alrm_time = logout_time;
1202 alm_fun = auto_logout;
1205 if ((cp = vp->vec[1]) != 0) {
1206 if ((lock_time = (unsigned) atoi(short2str(cp)) * 60) > 0) {
1208 if (alrm_time == 0 || lock_time < alrm_time) {
1209 alrm_time = lock_time;
1210 alm_fun = auto_lock;
1213 else /* lock_time always < alrm_time */
1215 alrm_time -= lock_time;
1219 if ((nl = sched_next()) != -1) {
1221 sched_dif = nl > cl ? nl - cl : 0;
1222 if ((alrm_time == 0) || ((unsigned) sched_dif < alrm_time)) {
1223 alrm_time = ((unsigned) sched_dif) + 1;
1224 alm_fun = sched_run;
1227 (void) alarm(alrm_time); /* Autologout ON */
1230 #undef RMDEBUG /* For now... */
1236 struct wordent *we, *args;
1237 struct wordent *tmp, *del;
1240 static Char STRrmdebug[] = {'r', 'm', 'd', 'e', 'b', 'u', 'g', '\0'};
1242 #endif /* RMDEBUG */
1245 int ask, doit, star = 0, silent = 0;
1247 if (!adrof(STRrmstar))
1250 tag = varval(STRrmdebug);
1251 #endif /* RMDEBUG */
1253 while (*we->word == ';' && we != cp)
1258 xprintf(CGETS(22, 7, "parsing command line\n"));
1259 #endif /* RMDEBUG */
1260 if (!Strcmp(we->word, STRrm)) {
1262 ask = (*args->word != '-');
1263 while (*args->word == '-' && !silent) { /* check options */
1264 for (charac = (args->word + 1); *charac && !silent; charac++)
1265 silent = (*charac == 'i' || *charac == 'f');
1268 ask = (ask || (!ask && !silent));
1270 for (; !star && *args->word != ';'
1271 && args != cp; args = args->next)
1272 if (!Strcmp(args->word, STRstar))
1275 xprintf(CGETS(22, 8,
1276 "Do you really want to delete all files? [n/y] "));
1278 (void) force_read(SHIN, &c, 1);
1280 * Perhaps we should use the yesexpr from the
1283 doit = (strchr(CGETS(22, 14, "Yy"), c) != NULL);
1284 while (c != '\n' && force_read(SHIN, &c, 1) == 1)
1287 /* remove the command instead */
1290 xprintf(CGETS(22, 9,
1291 "skipping deletion of files!\n"));
1292 #endif /* RMDEBUG */
1294 *tmp->word != '\n' &&
1295 *tmp->word != ';' && tmp != cp;) {
1296 tmp->prev->next = tmp->next;
1297 tmp->next->prev = tmp->prev;
1298 xfree((ptr_t) tmp->word);
1303 if (*tmp->word == ';') {
1304 tmp->prev->next = tmp->next;
1305 tmp->next->prev = tmp->prev;
1306 xfree((ptr_t) tmp->word);
1318 *we->word != ';' && we != cp;
1321 if (*we->word == ';')
1326 xprintf(CGETS(22, 10, "command line now is:\n"));
1327 for (we = cp->next; we != cp; we = we->next)
1328 xprintf("%S ", we->word);
1330 #endif /* RMDEBUG */
1335 /* Check if command is in continue list
1336 and do a "aliasing" if it exists as a job in background */
1338 #undef CNDEBUG /* For now */
1344 struct process *pp, *np;
1345 Char *cmd, *continue_list, *continue_args_list;
1349 static Char STRcndebug[] =
1350 {'c', 'n', 'd', 'e', 'b', 'u', 'g', '\0'};
1351 #endif /* CNDEBUG */
1352 int in_cont_list, in_cont_arg_list;
1356 tag = varval(STRcndebug);
1357 #endif /* CNDEBUG */
1358 continue_list = varval(STRcontinue);
1359 continue_args_list = varval(STRcontinue_args);
1360 if (*continue_list == '\0' && *continue_args_list == '\0')
1364 while (*we->word == ';' && we != cp)
1369 xprintf(CGETS(22, 11, "parsing command line\n"));
1370 #endif /* CNDEBUG */
1372 in_cont_list = inlist(continue_list, cmd);
1373 in_cont_arg_list = inlist(continue_args_list, cmd);
1374 if (in_cont_list || in_cont_arg_list) {
1377 xprintf(CGETS(22, 12, "in one of the lists\n"));
1378 #endif /* CNDEBUG */
1380 for (pp = proclist.p_next; pp; pp = pp->p_next) {
1381 if (prefix(cmd, pp->p_command)) {
1389 insert(we, in_cont_arg_list);
1393 *we->word != ';' && we != cp;
1396 if (*we->word == ';')
1401 xprintf(CGETS(22, 13, "command line now is:\n"));
1402 for (we = cp->next; we != cp; we = we->next)
1403 xprintf("%S ", we->word);
1405 #endif /* CNDEBUG */
1409 /* The actual "aliasing" of for backgrounds() is done here
1410 with the aid of insert_we(). */
1412 insert(pl, file_args)
1416 struct wordent *now, *last;
1417 Char *cmd, *bcmd, *cp1, *cp2;
1419 Char *upause = STRunderpause;
1420 int p_len = (int) Strlen(upause);
1422 cmd_len = (int) Strlen(pl->word);
1423 cmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 1) * sizeof(Char)));
1424 (void) Strcpy(cmd, pl->word);
1425 /* Do insertions at beginning, first replace command word */
1429 xfree((ptr_t) now->word);
1430 now->word = (Char *) xcalloc(1, (size_t) (5 * sizeof(Char)));
1431 (void) Strcpy(now->word, STRecho);
1433 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1434 now->word = (Char *) xcalloc(1, (size_t) (6 * sizeof(Char)));
1435 (void) Strcpy(now->word, STRbackqpwd);
1438 for (last = now; *last->word != '\n' && *last->word != ';';
1442 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1443 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1444 (void) Strcpy(now->word, STRgt);
1445 insert_we(now, last->prev);
1447 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1448 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1449 (void) Strcpy(now->word, STRbang);
1450 insert_we(now, last->prev);
1452 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1453 now->word = (Char *) xcalloc(1, (size_t) cmd_len + p_len + 4);
1459 while ((*cp1++ = *cp2++) != '\0')
1463 while ((*cp1++ = *cp2++) != '\0')
1465 insert_we(now, last->prev);
1467 now = (struct wordent *) xcalloc(1, (size_t) sizeof(struct wordent));
1468 now->word = (Char *) xcalloc(1, (size_t) (2 * sizeof(Char)));
1469 (void) Strcpy(now->word, STRsemi);
1470 insert_we(now, last->prev);
1471 bcmd = (Char *) xcalloc(1, (size_t) ((cmd_len + 2) * sizeof(Char)));
1475 while ((*cp1++ = *cp2++) != '\0')
1477 now = (struct wordent *) xcalloc(1, (size_t) (sizeof(struct wordent)));
1479 insert_we(now, last->prev);
1482 struct wordent *del;
1485 xfree((ptr_t) now->word);
1486 now->word = (Char *) xcalloc(1,
1487 (size_t) ((cmd_len + 2) * sizeof(Char)));
1491 while ((*cp1++ = *cp2++) != '\0')
1493 for (now = now->next;
1494 *now->word != '\n' && *now->word != ';' && now != pl;) {
1495 now->prev->next = now->next;
1496 now->next->prev = now->prev;
1497 xfree((ptr_t) now->word);
1506 insert_we(new, where)
1507 struct wordent *new, *where;
1511 new->next = where->next;
1513 new->next->prev = new;
1529 if (*n == '\0' && (*l == ' ' || *l == '\0'))
1535 while (*l && *l != ' ')
1536 l++; /* skip to blank */
1537 while (*l && *l == ' ')
1538 l++; /* and find first nonblank character */
1545 #endif /* BSDJOBS */
1549 * Implement a small cache for tilde names. This is used primarily
1550 * to expand tilde names to directories, but also
1551 * we can find users from their home directories for the tilde
1552 * prompt, on machines where yp lookup is slow this can be a big win...
1553 * As with any cache this can run out of sync, rehash can sync it again.
1555 static struct tildecache {
1563 static int tsize = TILINCR;
1566 tildecompare(p1, p2)
1567 struct tildecache *p1, *p2;
1569 return Strcmp(p1->user, p2->user);
1578 char **res, **res1, *cp;
1582 pp = getpwnam(short2str(us));
1588 /* Don't return if root */
1589 if (pp->pw_dir[0] == '/' && pp->pw_dir[1] == '\0')
1593 return Strsave(str2short(pp->pw_dir));
1596 res = hes_resolve(short2str(us), "filsys");
1599 if ((*res) != NULL) {
1601 * Look at the first token to determine how to interpret
1603 * Yes, strtok is evil (it's not thread-safe), but it's also
1606 cp = strtok(*res, " ");
1607 if (strcmp(cp, "AFS") == 0) {
1608 /* next token is AFS pathname.. */
1609 cp = strtok(NULL, " ");
1611 rp = Strsave(str2short(cp));
1612 } else if (strcmp(cp, "NFS") == 0) {
1614 if ((strtok(NULL, " ")) && /* skip remote pathname */
1615 (strtok(NULL, " ")) && /* skip host */
1616 (strtok(NULL, " ")) && /* skip mode */
1617 (cp = strtok(NULL, " "))) {
1618 rp = Strsave(str2short(cp));
1622 for (res1 = res; *res1; res1++)
1625 /* Don't return if root */
1626 if (rp != NULL && rp[0] == '/' && rp[1] == '\0') {
1641 struct tildecache *bp1, *bp2, *bp;
1644 /* Ignore NIS special names */
1645 if (*us == '+' || *us == '-')
1649 tcache = (struct tildecache *) xmalloc((size_t) (TILINCR *
1650 sizeof(struct tildecache)));
1654 for (bp1 = tcache, bp2 = tcache + tlength; bp1 < bp2;) {
1657 bp = bp1 + ((bp2 - bp1) >> 1);
1658 if ((i = *us - *bp->user) == 0 && (i = Strcmp(us, bp->user)) == 0)
1666 * Not in the cache, try to get it from the passwd file
1668 hd = gethomedir(us);
1675 tcache[tlength].user = Strsave(us);
1676 tcache[tlength].home = hd;
1677 tcache[tlength++].hlen = (int) Strlen(hd);
1679 qsort((ptr_t) tcache, (size_t) tlength, sizeof(struct tildecache),
1680 (int (*) __P((const void *, const void *))) tildecompare);
1682 if (tlength == tsize) {
1684 tcache = (struct tildecache *) xrealloc((ptr_t) tcache,
1686 sizeof(struct tildecache)));
1692 * Return the username if the directory path passed contains a
1693 * user's home directory in the tilde cache, otherwise return NULL
1694 * hm points to the place where the path became different.
1695 * Special case: Our own home directory.
1696 * If we are passed a null pointer, then we flush the cache.
1706 for (i = 0; i < tlength; i++) {
1707 xfree((ptr_t) tcache[i].home);
1708 xfree((ptr_t) tcache[i].user);
1710 xfree((ptr_t) tcache);
1716 if (((h = varval(STRhome)) != STRNULL) &&
1717 (Strncmp(p = *hm, h, (size_t) (j = (int) Strlen(h))) == 0) &&
1718 (p[j] == '/' || p[j] == '\0')) {
1722 for (i = 0; i < tlength; i++)
1723 if ((Strncmp(p = *hm, tcache[i].home, (size_t)
1724 (j = tcache[i].hlen)) == 0) && (p[j] == '/' || p[j] == '\0')) {
1726 return tcache[i].user;
1733 * PWP: read a bunch of aliases out of a file QUICKLY. The format
1734 * is almost the same as the result of saying "alias > FILE", except
1735 * that saying "aliases > FILE" does not expand non-letters to printable
1747 Char buf[BUFSIZE], line[BUFSIZE];
1748 char tbuf[BUFSIZE + 1], *tmp;
1749 extern int output_raw; /* PWP: in sh.print.c */
1755 plist(&aliases, VAR_ALL);
1760 gflag = 0, tglob(v);
1764 stderror(ERR_NAME | ERR_NOMATCH);
1767 v = gargv = saveblk(v);
1771 if ((fd = open(tmp = short2str(*v), O_RDONLY|O_LARGEFILE)) < 0)
1772 stderror(ERR_NAME | ERR_SYSTEM, tmp, strerror(errno));
1775 if (setexit() == 0) {
1784 if ((n = read(fd, tbuf, BUFSIZE)) <= 0) {
1786 stderror(ERR_SYSTEM, progname, strerror(errno));
1790 for (i = 0; i < n; i++)
1791 buf[i] = (Char) tbuf[i];
1795 if ((*lp++ = *p++) == '\n') {
1800 for (lp = line; *lp; lp++) {
1805 vec = (Char **) xmalloc((size_t)
1806 (2 * sizeof(Char **)));
1807 vec[0] = Strsave(lp);
1809 setq(strip(line), vec, &aliases, VAR_READWRITE);
1820 blkfree(gargv), gargv = 0;
1823 #endif /* OBSOLETE */
1827 * set the shell-level var to 1 or apply change to it.
1835 if ((cp = getenv("SHLVL")) != NULL) {
1843 if (adrof(STRshlvl) != NULL)
1845 Unsetenv(STRKSHLVL);
1850 (void) Itoa(val, buff, 0, 0);
1851 set(STRshlvl, Strsave(buff), VAR_READWRITE);
1852 tsetenv(STRKSHLVL, buff);
1856 set(STRshlvl, SAVE("1"), VAR_READWRITE);
1857 tsetenv(STRKSHLVL, str2short("1"));
1863 * Try to recover from a read error
1870 case -1: /* Make sure that the code is reachable */
1875 #endif /* EWOULDBLOCK */
1877 #if defined(POSIX) && defined(EAGAIN)
1878 # if !defined(EWOULDBLOCK) || EWOULDBLOCK != EAGAIN
1881 # endif /* !EWOULDBLOCK || EWOULDBLOCK != EAGAIN */
1882 #endif /* POSIX && EAGAIN */
1888 * Great! we have on suns 3 flavors and 5 names...
1889 * I hope that will cover everything.
1890 * I added some more defines... many systems have different defines.
1891 * Rather than dealing with getting the right includes, we'll just
1892 * cover all the known possibilities here. -- sterling@netcom.com
1895 # define O_NONBLOCK 0
1896 # endif /* O_NONBLOCK */
1899 # endif /* O_NDELAY */
1905 # endif /* _FNBIO */
1908 # endif /* FNONBIO */
1910 # define FNONBLOCK 0
1911 # endif /* FNONBLOCK */
1913 # define _FNONBLOCK 0
1914 # endif /* _FNONBLOCK */
1917 # endif /* FNDELAY */
1920 # endif /* _FNDELAY */
1921 # ifndef FNDLEAY /* Some linux versions have this typo */
1923 # endif /* FNDLEAY */
1924 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
1927 e &= ~(O_NDELAY|O_NONBLOCK|FNBIO|_FNBIO|FNONBIO|FNONBLOCK|_FNONBLOCK|
1928 FNDELAY|_FNDELAY|FNDLEAY); /* whew! */
1929 if (fcntl(fd, F_SETFL, e) == -1)
1933 # endif /* F_SETFL */
1937 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
1941 # endif /* FIONBIO */
1943 #endif /* FDRETRY */
1963 #ifdef SHORT_STRINGS
1964 /* This strips the quote bit as a side effect */
1965 char *sa = strsave(short2str(a));
1966 char *sb = strsave(short2str(b));
1968 char *sa = strip(strsave(a));
1969 char *sb = strip(strsave(b));
1970 #endif /* SHORT_STRINGS */
1972 #if defined(NLS) && !defined(NOSTRCOLL)
1973 errno = 0; /* strcoll sets errno, another brain-damage */
1975 rv = strcoll(sa, sb);
1978 * We should be checking for errno != 0, but some systems
1979 * forget to reset errno to 0. So we only check for the
1980 * only documented valid errno value for strcoll [EINVAL]
1982 if (errno == EINVAL) {
1985 stderror(ERR_SYSTEM, "strcoll", strerror(errno));
1988 rv = strcmp(sa, sb);
1989 #endif /* NLS && !NOSTRCOLL */
1999 * From: peter@zeus.dialix.oz.au (Peter Wemm)
2000 * If exec() fails look first for a #! [word] [word] ....
2001 * If it is, splice the header into the argument list and retry.
2003 #define HACKBUFSZ 1024 /* Max chars in #! vector */
2004 #define HACKVECSZ 128 /* Max words in #! vector */
2010 unsigned char lbuf[HACKBUFSZ];
2011 char *sargv[HACKVECSZ];
2012 unsigned char *p, *ws;
2015 int fw = 0; /* found at least one word */
2017 #endif /* WINNT_NATIVE */
2019 if (read(fd, (char *) lbuf, HACKBUFSZ) <= 0)
2022 ws = 0; /* word started = 0 */
2024 for (p = lbuf; p < &lbuf[HACKBUFSZ]; )
2030 #endif /* WINNT_NATIVE */
2031 if (ws) { /* a blank after a word.. save it */
2033 #ifndef WINNT_NATIVE
2034 if (sargc < HACKVECSZ - 1)
2035 sargv[sargc++] = ws;
2037 #else /* WINNT_NATIVE */
2038 if (sargc < HACKVECSZ - 1) {
2039 sargv[sargc] = first_word ? NULL: hb_subst(ws);
2040 if (sargv[sargc] == NULL)
2047 #endif /* WINNT_NATIVE */
2052 case '\0': /* Whoa!! what the hell happened */
2055 case '\n': /* The end of the line. */
2059 #endif /* WINNT_NATIVE */
2060 ws) { /* terminate the last word */
2062 #ifndef WINNT_NATIVE
2063 if (sargc < HACKVECSZ - 1)
2064 sargv[sargc++] = ws;
2065 #else /* WINNT_NATIVE */
2066 if (sargc < HACKVECSZ - 1) { /* deal with the 1-word case */
2067 sargv[sargc] = first_word? NULL : hb_subst(ws);
2068 if (sargv[sargc] == NULL)
2072 #endif /* !WINNT_NATIVE */
2074 sargv[sargc] = NULL;
2077 *vp = blk2short(sargv);
2084 if (!ws) /* Start a new word? */
2091 #endif /* HASHBANG */
2102 (void) sigset(snum, SIG_IGN);
2103 #endif /* UNRELSIGS */
2112 const char *host = NULL;
2114 struct sockaddr_storage saddr;
2115 socklen_t len = sizeof(struct sockaddr_storage);
2116 static char hbuf[NI_MAXHOST];
2119 struct sockaddr_in saddr;
2120 socklen_t len = sizeof(struct sockaddr_in);
2122 #ifdef HAVE_STRUCT_UTMP_UT_HOST
2127 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
2128 (saddr.ss_family == AF_INET6 || saddr.ss_family == AF_INET)) {
2129 int flag = NI_NUMERICHOST;
2131 #ifdef NI_WITHSCOPEID
2132 flag |= NI_WITHSCOPEID;
2134 getnameinfo((struct sockaddr *)&saddr, len, hbuf, sizeof(hbuf),
2138 if (getpeername(SHIN, (struct sockaddr *) &saddr, &len) != -1 &&
2139 saddr.sin_family == AF_INET) {
2141 if ((hp = gethostbyaddr((char *)&saddr.sin_addr, sizeof(struct in_addr),
2146 host = inet_ntoa(saddr.sin_addr);
2149 #ifdef HAVE_STRUCT_UTMP_UT_HOST
2152 char *name = utmphost();
2153 /* Avoid empty names and local X displays */
2154 if (name != NULL && *name != '\0' && *name != ':') {
2155 struct in_addr addr;
2157 /* Look for host:display.screen */
2159 * There is conflict with IPv6 address and X DISPLAY. So,
2160 * we assume there is no IPv6 address in utmp and don't
2163 if ((sptr = strchr(name, ':')) != NULL)
2165 /* Leave IPv4 address as is */
2167 * we use inet_addr here, not inet_aton because many systems
2168 * have not caught up yet.
2170 addr.s_addr = inet_addr(name);
2171 if (addr.s_addr != (unsigned int)~0)
2177 char dbuf[MAXHOSTNAMELEN], cbuf[MAXHOSTNAMELEN];
2178 struct addrinfo hints, *res = NULL;
2180 memset(&hints, 0, sizeof(hints));
2181 hints.ai_family = PF_UNSPEC;
2182 hints.ai_socktype = SOCK_STREAM;
2183 hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
2184 if (strlen(name) < utmphostsize())
2186 if (getaddrinfo(name, NULL, &hints, &res) != 0)
2188 } else if (gethostname(dbuf, sizeof(dbuf) - 1) == 0 &&
2189 (domain = strchr(dbuf, '.')) != NULL) {
2190 for (s = strchr(name, '.');
2191 s != NULL; s = strchr(s + 1, '.')) {
2192 if (*(s + 1) != '\0' &&
2193 (ptr = strstr(domain, s)) != NULL) {
2195 if (len + strlen(ptr) >= sizeof(cbuf))
2197 strncpy(cbuf, name, len);
2198 strcpy(cbuf + len, ptr);
2199 if (getaddrinfo(cbuf, NULL, &hints, &res) != 0)
2206 if (res->ai_canonname != NULL) {
2207 strncpy(hbuf, res->ai_canonname, sizeof(hbuf));
2213 if ((hp = gethostbyname(name)) == NULL) {
2214 /* Try again eliminating the trailing domain */
2215 if ((ptr = strchr(name, '.')) != NULL) {
2217 if ((hp = gethostbyname(name)) != NULL)
2232 tsetenv(STRREMOTEHOST, str2short(host));
2234 #ifdef HAVE_STRUCT_UTMP_UT_HOST
2242 * From: <lesv@ppvku.ericsson.se> (Lennart Svensson)
2247 /* Don't get stuck if the resolver does not work! */
2248 signalfun_t osig = sigset(SIGALRM, palarm);
2261 (void) sigset(SIGALRM, osig);
2264 /* From: casper@fwi.uva.nl (Casper H.S. Dik), for Solaris 2.3 */
2269 #endif /* REMOTEHOST */
2271 #ifndef WINNT_NATIVE
2273 * indicate if a terminal type is defined in terminfo/termcap
2274 * (by default the current term type). This allows ppl to look
2275 * for a working term type automatically in their login scripts
2276 * when using a terminal known as different things on different
2286 * Maximum size of a termcap record. We make it twice as large.
2288 char termcap_buffer[2048];
2291 /* try to find which entry we should be looking for */
2292 termtype = (v[1] == NULL ? getenv("TERM") : short2str(v[1]));
2293 if (termtype == NULL) {
2294 /* no luck - the user didn't provide one and none is
2295 * specified in the environment
2297 set(STRstatus, Strsave(STR1), VAR_READWRITE);
2302 * we use the termcap function - if we are using terminfo we
2303 * will end up with it's compatibility function
2304 * terminfo/termcap will be initialized with the new
2305 * type but we don't care because tcsh has cached all the things
2308 if (tgetent(termcap_buffer, termtype) == 1) {
2309 xprintf("%s\n", termtype);
2310 set(STRstatus, Strsave(STR0), VAR_READWRITE);
2312 set(STRstatus, Strsave(STR1), VAR_READWRITE);
2315 #endif /* WINNT_NATIVE */