4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd1.c
index 6bec2eb..e1f55aa 100644 (file)
@@ -1,22 +1,16 @@
-/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+/*-
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)cmd1.c     5.7 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)cmd1.c     8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
-#include <sys/stat.h>
-#include <sys/wait.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -31,6 +25,7 @@ static char sccsid[] = "@(#)cmd1.c    5.7 (Berkeley) %G%";
 
 static int screen;
 
 
 static int screen;
 
+int
 headers(msgvec)
        int *msgvec;
 {
 headers(msgvec)
        int *msgvec;
 {
@@ -60,7 +55,6 @@ headers(msgvec)
                if (flag++ >= size)
                        break;
                printhead(mesg);
                if (flag++ >= size)
                        break;
                printhead(mesg);
-               sreset();
        }
        if (flag == 0) {
                printf("No more mail.\n");
        }
        if (flag == 0) {
                printf("No more mail.\n");
@@ -69,40 +63,10 @@ headers(msgvec)
        return(0);
 }
 
        return(0);
 }
 
-/*
- * Set the list of alternate names for out host.
- */
-local(namelist)
-       char **namelist;
-{
-       register int c;
-       register char **ap, **ap2, *cp;
-
-       c = argcount(namelist) + 1;
-       if (c == 1) {
-               if (localnames == 0)
-                       return(0);
-               for (ap = localnames; *ap; ap++)
-                       printf("%s ", *ap);
-               printf("\n");
-               return(0);
-       }
-       if (localnames != 0)
-               cfree((char *) localnames);
-       localnames = (char **) calloc((unsigned) c, sizeof (char *));
-       for (ap = namelist, ap2 = localnames; *ap; ap++, ap2++) {
-               cp = (char *) calloc((unsigned) strlen(*ap) + 1, sizeof (char));
-               strcpy(cp, *ap);
-               *ap2 = cp;
-       }
-       *ap2 = 0;
-       return(0);
-}
-
 /*
  * Scroll to the next/previous screen
  */
 /*
  * Scroll to the next/previous screen
  */
-
+int
 scroll(arg)
        char arg[];
 {
 scroll(arg)
        char arg[];
 {
@@ -141,6 +105,7 @@ scroll(arg)
 /*
  * Compute screen size.
  */
 /*
  * Compute screen size.
  */
+int
 screensize()
 {
        int s;
 screensize()
 {
        int s;
@@ -155,16 +120,14 @@ screensize()
  * Print out the headlines for each message
  * in the passed message list.
  */
  * Print out the headlines for each message
  * in the passed message list.
  */
-
+int
 from(msgvec)
        int *msgvec;
 {
        register int *ip;
 
 from(msgvec)
        int *msgvec;
 {
        register int *ip;
 
-       for (ip = msgvec; *ip != NULL; ip++) {
+       for (ip = msgvec; *ip != NULL; ip++)
                printhead(*ip);
                printhead(*ip);
-               sreset();
-       }
        if (--ip >= msgvec)
                dot = &message[*ip - 1];
        return(0);
        if (--ip >= msgvec)
                dot = &message[*ip - 1];
        return(0);
@@ -174,17 +137,19 @@ from(msgvec)
  * Print out the header of a specific message.
  * This is a slight improvement to the standard one.
  */
  * Print out the header of a specific message.
  * This is a slight improvement to the standard one.
  */
-
+void
 printhead(mesg)
 printhead(mesg)
+       int mesg;
 {
        struct message *mp;
        char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
        char pbuf[BUFSIZ];
        struct headline hl;
        int subjlen;
 {
        struct message *mp;
        char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
        char pbuf[BUFSIZ];
        struct headline hl;
        int subjlen;
+       char *name;
 
        mp = &message[mesg-1];
 
        mp = &message[mesg-1];
-       readline(setinput(mp), headline);
+       (void) readline(setinput(mp), headline, LINESIZE);
        if ((subjline = hfield("subject", mp)) == NOSTR)
                subjline = hfield("subj", mp);
        /*
        if ((subjline = hfield("subject", mp)) == NOSTR)
                subjline = hfield("subj", mp);
        /*
@@ -203,21 +168,23 @@ printhead(mesg)
        if (mp->m_flag & MBOX)
                dispc = 'M';
        parse(headline, &hl, pbuf);
        if (mp->m_flag & MBOX)
                dispc = 'M';
        parse(headline, &hl, pbuf);
-       sprintf(wcount, "%3d/%-4ld", mp->m_lines, mp->m_size);
+       sprintf(wcount, "%3d/%-5ld", mp->m_lines, mp->m_size);
        subjlen = screenwidth - 50 - strlen(wcount);
        subjlen = screenwidth - 50 - strlen(wcount);
+       name = value("show-rcpt") != NOSTR ?
+               skin(hfield("to", mp)) : nameof(mp, 0);
        if (subjline == NOSTR || subjlen < 0)           /* pretty pathetic */
                printf("%c%c%3d %-20.20s  %16.16s %s\n",
        if (subjline == NOSTR || subjlen < 0)           /* pretty pathetic */
                printf("%c%c%3d %-20.20s  %16.16s %s\n",
-                       curind, dispc, mesg, nameof(mp, 0), hl.l_date, wcount);
+                       curind, dispc, mesg, name, hl.l_date, wcount);
        else
                printf("%c%c%3d %-20.20s  %16.16s %s \"%.*s\"\n",
        else
                printf("%c%c%3d %-20.20s  %16.16s %s \"%.*s\"\n",
-                       curind, dispc, mesg, nameof(mp, 0), hl.l_date, wcount,
+                       curind, dispc, mesg, name, hl.l_date, wcount,
                        subjlen, subjline);
 }
 
 /*
  * Print out the value of dot.
  */
                        subjlen, subjline);
 }
 
 /*
  * Print out the value of dot.
  */
-
+int
 pdot()
 {
        printf("%d\n", dot - &message[0] + 1);
 pdot()
 {
        printf("%d\n", dot - &message[0] + 1);
@@ -227,7 +194,7 @@ pdot()
 /*
  * Print out all the possible commands.
  */
 /*
  * Print out all the possible commands.
  */
-
+int
 pcmdlist()
 {
        register struct cmd *cp;
 pcmdlist()
 {
        register struct cmd *cp;
@@ -252,6 +219,7 @@ pcmdlist()
 /*
  * Paginate messages, honor ignored fields.
  */
 /*
  * Paginate messages, honor ignored fields.
  */
+int
 more(msgvec)
        int *msgvec;
 {
 more(msgvec)
        int *msgvec;
 {
@@ -261,6 +229,7 @@ more(msgvec)
 /*
  * Paginate messages, even printing ignored fields.
  */
 /*
  * Paginate messages, even printing ignored fields.
  */
+int
 More(msgvec)
        int *msgvec;
 {
 More(msgvec)
        int *msgvec;
 {
@@ -271,6 +240,7 @@ More(msgvec)
 /*
  * Type out messages, honor ignored fields.
  */
 /*
  * Type out messages, honor ignored fields.
  */
+int
 type(msgvec)
        int *msgvec;
 {
 type(msgvec)
        int *msgvec;
 {
@@ -281,6 +251,7 @@ type(msgvec)
 /*
  * Type out messages, even printing ignored fields.
  */
 /*
  * Type out messages, even printing ignored fields.
  */
+int
 Type(msgvec)
        int *msgvec;
 {
 Type(msgvec)
        int *msgvec;
 {
@@ -292,28 +263,22 @@ Type(msgvec)
  * Type out the messages requested.
  */
 jmp_buf        pipestop;
  * Type out the messages requested.
  */
 jmp_buf        pipestop;
-
+int
 type1(msgvec, doign, page)
        int *msgvec;
 type1(msgvec, doign, page)
        int *msgvec;
+       int doign, page;
 {
        register *ip;
        register struct message *mp;
 {
        register *ip;
        register struct message *mp;
-       register int mesg;
        register char *cp;
        int nlines;
        register char *cp;
        int nlines;
-       int brokpipe();
        FILE *obuf;
 
        obuf = stdout;
        FILE *obuf;
 
        obuf = stdout;
-       if (setjmp(pipestop)) {
-               if (obuf != stdout) {
-                       pipef = NULL;
-                       pclose(obuf);
-               }
-               signal(SIGPIPE, SIG_DFL);
-               return(0);
-       }
-       if (intty && outtty && (page || (cp = value("crt")) != NOSTR)) {
+       if (setjmp(pipestop))
+               goto close_pipe;
+       if (value("interactive") != NOSTR &&
+           (page || (cp = value("crt")) != NOSTR)) {
                nlines = 0;
                if (!page) {
                        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
                nlines = 0;
                if (!page) {
                        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
@@ -322,40 +287,42 @@ type1(msgvec, doign, page)
                if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
                        cp = value("PAGER");
                        if (cp == NULL || *cp == '\0')
                if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
                        cp = value("PAGER");
                        if (cp == NULL || *cp == '\0')
-                               cp = MORE;
-                       obuf = popen(cp, "w");
+                               cp = _PATH_MORE;
+                       obuf = Popen(cp, "w");
                        if (obuf == NULL) {
                                perror(cp);
                                obuf = stdout;
                        if (obuf == NULL) {
                                perror(cp);
                                obuf = stdout;
-                       } else {
-                               pipef = obuf;
+                       } else
                                signal(SIGPIPE, brokpipe);
                                signal(SIGPIPE, brokpipe);
-                       }
                }
        }
        for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
                }
        }
        for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
+               mp = &message[*ip - 1];
+               touch(mp);
                dot = mp;
                if (value("quiet") == NOSTR)
                dot = mp;
                if (value("quiet") == NOSTR)
-                       fprintf(obuf, "Message %d:\n", mesg);
-               send(mp, obuf, doign ? ignore : 0);
+                       fprintf(obuf, "Message %d:\n", *ip);
+               (void) send(mp, obuf, doign ? ignore : 0, NOSTR);
        }
        }
+close_pipe:
        if (obuf != stdout) {
        if (obuf != stdout) {
-               pipef = NULL;
-               pclose(obuf);
+               /*
+                * Ignore SIGPIPE so it can't cause a duplicate close.
+                */
+               signal(SIGPIPE, SIG_IGN);
+               Pclose(obuf);
+               signal(SIGPIPE, SIG_DFL);
        }
        }
-       signal(SIGPIPE, SIG_DFL);
        return(0);
 }
 
 /*
  * Respond to a broken pipe signal --
        return(0);
 }
 
 /*
  * Respond to a broken pipe signal --
- * probably caused by using quitting more.
+ * probably caused by quitting more.
  */
  */
-
-brokpipe()
+void
+brokpipe(signo)
+       int signo;
 {
        longjmp(pipestop, 1);
 }
 {
        longjmp(pipestop, 1);
 }
@@ -365,13 +332,12 @@ brokpipe()
  * The number of lines is taken from the variable "toplines"
  * and defaults to 5.
  */
  * The number of lines is taken from the variable "toplines"
  * and defaults to 5.
  */
-
+int
 top(msgvec)
        int *msgvec;
 {
        register int *ip;
        register struct message *mp;
 top(msgvec)
        int *msgvec;
 {
        register int *ip;
        register struct message *mp;
-       register int mesg;
        int c, topl, lines, lineb;
        char *valtop, linebuf[LINESIZE];
        FILE *ibuf;
        int c, topl, lines, lineb;
        char *valtop, linebuf[LINESIZE];
        FILE *ibuf;
@@ -385,18 +351,17 @@ top(msgvec)
        }
        lineb = 1;
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
        }
        lineb = 1;
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
+               mp = &message[*ip - 1];
+               touch(mp);
                dot = mp;
                if (value("quiet") == NOSTR)
                dot = mp;
                if (value("quiet") == NOSTR)
-                       printf("Message %2d:\n", mesg);
+                       printf("Message %d:\n", *ip);
                ibuf = setinput(mp);
                c = mp->m_lines;
                if (!lineb)
                        printf("\n");
                for (lines = 0; lines < c && lines <= topl; lines++) {
                ibuf = setinput(mp);
                c = mp->m_lines;
                if (!lineb)
                        printf("\n");
                for (lines = 0; lines < c && lines <= topl; lines++) {
-                       if (readline(ibuf, linebuf) < 0)
+                       if (readline(ibuf, linebuf, LINESIZE) < 0)
                                break;
                        puts(linebuf);
                        lineb = blankline(linebuf);
                                break;
                        puts(linebuf);
                        lineb = blankline(linebuf);
@@ -409,7 +374,7 @@ top(msgvec)
  * Touch all the given messages so that they will
  * get mboxed.
  */
  * Touch all the given messages so that they will
  * get mboxed.
  */
-
+int
 stouch(msgvec)
        int msgvec[];
 {
 stouch(msgvec)
        int msgvec[];
 {
@@ -426,7 +391,7 @@ stouch(msgvec)
 /*
  * Make sure all passed messages get mboxed.
  */
 /*
  * Make sure all passed messages get mboxed.
  */
-
+int
 mboxit(msgvec)
        int msgvec[];
 {
 mboxit(msgvec)
        int msgvec[];
 {
@@ -443,28 +408,18 @@ mboxit(msgvec)
 /*
  * List the folders the user currently has.
  */
 /*
  * List the folders the user currently has.
  */
+int
 folders()
 {
        char dirname[BUFSIZ];
 folders()
 {
        char dirname[BUFSIZ];
-       int pid, e;
-       union wait s;
+       char *cmd;
 
        if (getfold(dirname) < 0) {
                printf("No value set for \"folder\"\n");
 
        if (getfold(dirname) < 0) {
                printf("No value set for \"folder\"\n");
-               return(-1);
-       }
-       switch ((pid = fork())) {
-       case 0:
-               execlp("ls", "ls", dirname, 0);
-               _exit(1);
-
-       case -1:
-               perror("fork");
-               return(-1);
-
-       default:
-               while ((e = wait(&s)) != -1 && e != pid)
-                       ;
+               return 1;
        }
        }
-       return(0);
+       if ((cmd = value("LISTER")) == NOSTR)
+               cmd = "ls";
+       (void) run_command(cmd, 0, -1, -1, dirname, NOSTR, NOSTR);
+       return 0;
 }
 }