changed start up code to exit if there are 0 messages and
[unix-history] / usr / src / usr.bin / mail / aux.c
index 0330e38..0d82d3c 100644 (file)
@@ -11,7 +11,7 @@
  * Auxiliary functions.
  */
 
  * Auxiliary functions.
  */
 
-static char *SccsId = "@(#)aux.c       1.2 %G%";
+static char *SccsId = "@(#)aux.c       2.2 %G%";
 
 /*
  * Return a pointer to a dynamic copy of the argument.
 
 /*
  * Return a pointer to a dynamic copy of the argument.
@@ -380,7 +380,10 @@ icequal(s1, s2)
  */
 
 static int     ssp = -1;               /* Top of file stack */
  */
 
 static int     ssp = -1;               /* Top of file stack */
-static FILE    *sstack[_NFILE];        /* Saved input files */
+struct sstack {
+       FILE    *s_file;                /* File we were in. */
+       int     s_cond;                 /* Saved state of conditionals */
+} sstack[_NFILE];
 
 /*
  * Pushdown current input file and switch to a new one.
 
 /*
  * Pushdown current input file and switch to a new one.
@@ -392,9 +395,12 @@ source(name)
        char name[];
 {
        register FILE *fi;
        char name[];
 {
        register FILE *fi;
+       register char *cp;
 
 
-       if ((fi = fopen(name, "r")) == NULL) {
-               perror(name);
+       if ((cp = expand(name)) == NOSTR)
+               return(1);
+       if ((fi = fopen(cp, "r")) == NULL) {
+               perror(cp);
                return(1);
        }
        if (ssp >= _NFILE-2) {
                return(1);
        }
        if (ssp >= _NFILE-2) {
@@ -402,7 +408,9 @@ source(name)
                fclose(fi);
                return(1);
        }
                fclose(fi);
                return(1);
        }
-       sstack[++ssp] = input;
+       sstack[++ssp].s_file = input;
+       sstack[ssp].s_cond = cond;
+       cond = CANY;
        input = fi;
        sourcing++;
        return(0);
        input = fi;
        sourcing++;
        return(0);
@@ -436,7 +444,10 @@ unstack()
                return(1);
        }
        fclose(input);
                return(1);
        }
        fclose(input);
-       input = sstack[ssp--];
+       if (cond != CANY)
+               printf("Unmatched \"if\"\n");
+       cond = sstack[ssp].s_cond;
+       input = sstack[ssp--].s_file;
        if (ssp < 0)
                sourcing = 0;
        return(0);
        if (ssp < 0)
                sourcing = 0;
        return(0);
@@ -469,14 +480,9 @@ alter(name)
        time_p[1] = statb.st_mtime;
        utime(name, time_p);
 #else
        time_p[1] = statb.st_mtime;
        utime(name, time_p);
 #else
-       if ((pid = fork()) != 0)
-               return;
-       clrbuf(stdout);
-       clrbuf(stderr);
-       clrbuf(stdin);
        sleep(1);
        if ((f = open(name, 0)) < 0)
        sleep(1);
        if ((f = open(name, 0)) < 0)
-               exit(1);
+               return;
        read(f, &w, 1);
        exit(0);
 #endif
        read(f, &w, 1);
        exit(0);
 #endif
@@ -498,12 +504,88 @@ blankline(linebuf)
        return(1);
 }
 
        return(1);
 }
 
+/*
+ * Get sender's name from this message.  If the message has
+ * a bunch of arpanet stuff in it, we may have to skin the name
+ * before returning it.
+ */
+char *
+nameof(mp, reptype)
+       register struct message *mp;
+{
+
+       return(skin(name1(mp, reptype)));
+}
+
+/*
+ * Skin an arpa net address according to the RFC 733 interpretation
+ * of "host-phrase."
+ */
+char *
+skin(name)
+       char *name;
+{
+       register int c;
+       register char *cp, *cp2;
+       int gotlt, lastsp;
+       char nbuf[BUFSIZ];
+
+       if (name == NOSTR)
+               return(NOSTR);
+       if (index(name, '(') == NOSTR && index(name, '<') == NOSTR)
+               return(name);
+       gotlt = 0;
+       lastsp = 0;
+       for (cp = name, cp2 = nbuf, c = *cp++; *cp; c = *cp++) {
+               switch (c) {
+               case '(':
+                       while (*cp != ')' && *cp != 0)
+                               cp++;
+                       if (*cp)
+                               cp++;
+                       break;
+
+               case ' ':
+                       lastsp = 1;
+                       break;
+
+               case '<':
+                       cp2 = nbuf;
+                       gotlt++;
+                       lastsp = 0;
+                       break;
+
+               case '>':
+                       if (gotlt)
+                               goto done;
+
+                       /* Fall into . . . */
+
+               default:
+                       if (lastsp) {
+                               lastsp = 0;
+                               *cp2++ = ' ';
+                       }
+                       *cp2++ = c;
+                       break;
+               }
+       }
+done:
+       *cp2 = 0;
+
+       return(savestr(nbuf));
+}
+
 /*
  * Fetch the sender's name from the passed message.
 /*
  * Fetch the sender's name from the passed message.
+ * Reptype can be
+ *     0 -- get sender's name for display purposes
+ *     1 -- get sender's name for reply
+ *     2 -- get sender's name for Reply
  */
 
 char *
  */
 
 char *
-nameof(mp)
+name1(mp, reptype)
        register struct message *mp;
 {
        char namebuf[LINESIZE];
        register struct message *mp;
 {
        char namebuf[LINESIZE];
@@ -512,10 +594,12 @@ nameof(mp)
        register FILE *ibuf;
        int first = 1;
 
        register FILE *ibuf;
        int first = 1;
 
-       if ((cp = hfield("reply-to", mp)) != NOSTR) {
-               strcpy(namebuf, cp);
-               return(namebuf);
-       }
+#ifndef DELIVERMAIL
+       if ((cp = hfield("from", mp)) != NOSTR)
+               return(cp);
+       if (reptype == 0 && (cp = hfield("sender", mp)) != NOSTR)
+               return(cp);
+#endif
        ibuf = setinput(mp);
        copy("", namebuf);
        if (readline(ibuf, linebuf) <= 0)
        ibuf = setinput(mp);
        copy("", namebuf);
        if (readline(ibuf, linebuf) <= 0)