4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd1.c
index 50379b0..e1f55aa 100644 (file)
@@ -1,15 +1,16 @@
-/*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char *sccsid = "@(#)cmd1.c      5.3 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)cmd1.c     8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
-#include <sys/stat.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -24,6 +25,7 @@ static char *sccsid = "@(#)cmd1.c     5.3 (Berkeley) %G%";
 
 static int screen;
 
 
 static int screen;
 
+int
 headers(msgvec)
        int *msgvec;
 {
 headers(msgvec)
        int *msgvec;
 {
@@ -53,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");
@@ -62,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(c, sizeof (char *));
-       for (ap = namelist, ap2 = localnames; *ap; ap++, ap2++) {
-               cp = (char *) calloc(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[];
 {
@@ -132,53 +103,31 @@ scroll(arg)
 }
 
 /*
 }
 
 /*
- * Compute what the screen size should be.
- * We use the following algorithm:
- *     If user specifies with screen option, use that.
- *     If baud rate < 1200, use  5
- *     If baud rate = 1200, use 10
- *     If baud rate > 1200, use 20
+ * Compute screen size.
  */
  */
+int
 screensize()
 {
 screensize()
 {
-       register char *cp;
-       register int s;
-#ifdef TIOCGWINSZ
-       struct winsize ws;
-#endif
-
-       if ((cp = value("screen")) != NOSTR) {
-               s = atoi(cp);
-               if (s > 0)
-                       return(s);
-       }
-       if (baud < B1200)
-               s = 5;
-       else if (baud == B1200)
-               s = 10;
-#ifdef TIOCGWINSZ
-       else if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) == 0 && ws.ws_row != 0)
-               s = ws.ws_row - 4;
-#endif
-       else
-               s = 20;
-       return(s);
+       int s;
+       char *cp;
+
+       if ((cp = value("screen")) != NOSTR && (s = atoi(cp)) > 0)
+               return s;
+       return screenheight - 4;
 }
 
 /*
  * 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);
@@ -188,30 +137,24 @@ 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;
 {
        struct message *mp;
-       FILE *ibuf;
        char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
        char pbuf[BUFSIZ];
        char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
        char pbuf[BUFSIZ];
-       int s;
        struct headline hl;
        struct headline hl;
-       register char *cp;
+       int subjlen;
+       char *name;
 
        mp = &message[mesg-1];
 
        mp = &message[mesg-1];
-       ibuf = setinput(mp);
-       readline(ibuf, headline);
-       subjline = hfield("subject", mp);
-       if (subjline == NOSTR)
+       (void) readline(setinput(mp), headline, LINESIZE);
+       if ((subjline = hfield("subject", mp)) == NOSTR)
                subjline = hfield("subj", mp);
                subjline = hfield("subj", mp);
-
        /*
         * Bletch!
         */
        /*
         * Bletch!
         */
-
-       if (subjline != NOSTR && strlen(subjline) > 28)
-               subjline[29] = '\0';
        curind = dot == mp ? '>' : ' ';
        dispc = ' ';
        if (mp->m_flag & MSAVED)
        curind = dot == mp ? '>' : ' ';
        dispc = ' ';
        if (mp->m_flag & MSAVED)
@@ -225,24 +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, " %d/%ld", mp->m_lines, mp->m_size);
-       s = strlen(wcount);
-       cp = wcount + s;
-       while (s < 7)
-               s++, *cp++ = ' ';
-       *cp = '\0';
-       if (subjline != NOSTR)
-               printf("%c%c%3d %-8s %16.16s %s \"%s\"\n", curind, dispc, mesg,
-                   nameof(mp, 0), hl.l_date, wcount, subjline);
+       sprintf(wcount, "%3d/%-5ld", mp->m_lines, mp->m_size);
+       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",
+                       curind, dispc, mesg, name, hl.l_date, wcount);
        else
        else
-               printf("%c%c%3d %-8s %16.16s %s\n", curind, dispc, mesg,
-                   nameof(mp, 0), hl.l_date, wcount);
+               printf("%c%c%3d %-20.20s  %16.16s %s \"%.*s\"\n",
+                       curind, dispc, mesg, name, hl.l_date, wcount,
+                       subjlen, subjline);
 }
 
 /*
  * Print out the value of dot.
  */
 }
 
 /*
  * Print out the value of dot.
  */
-
+int
 pdot()
 {
        printf("%d\n", dot - &message[0] + 1);
 pdot()
 {
        printf("%d\n", dot - &message[0] + 1);
@@ -252,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;
@@ -277,6 +219,7 @@ pcmdlist()
 /*
  * Paginate messages, honor ignored fields.
  */
 /*
  * Paginate messages, honor ignored fields.
  */
+int
 more(msgvec)
        int *msgvec;
 {
 more(msgvec)
        int *msgvec;
 {
@@ -286,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;
 {
@@ -296,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;
 {
@@ -306,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;
 {
@@ -317,103 +263,81 @@ 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;
        register char *cp;
-       int c, nlines;
-       int brokpipe();
-       FILE *ibuf, *obuf;
+       int nlines;
+       FILE *obuf;
 
        obuf = stdout;
 
        obuf = stdout;
-       if (setjmp(pipestop)) {
-               if (obuf != stdout) {
-                       pipef = NULL;
-                       pclose(obuf);
-               }
-               sigset(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 += message[*ip - 1].m_lines;
                }
                nlines = 0;
                if (!page) {
                        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
                                nlines += message[*ip - 1].m_lines;
                }
-               if (page || nlines > atoi(cp)) {
+               if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
                        cp = value("PAGER");
                        if (cp == NULL || *cp == '\0')
                        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;
-                               sigset(SIGPIPE, brokpipe);
-                       }
+                       } else
+                               signal(SIGPIPE, brokpipe);
                }
        }
                }
        }
-       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
+       for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
+               mp = &message[*ip - 1];
+               touch(mp);
                dot = mp;
                dot = mp;
-               print(mp, obuf, doign);
+               if (value("quiet") == NOSTR)
+                       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);
        }
        }
-       sigset(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;
 {
 {
-# ifndef VMUNIX
-       signal(SIGPIPE, brokpipe);
-# endif
        longjmp(pipestop, 1);
 }
 
        longjmp(pipestop, 1);
 }
 
-/*
- * Print the indicated message on standard output.
- */
-
-print(mp, obuf, doign)
-       register struct message *mp;
-       FILE *obuf;
-{
-
-       if (value("quiet") == NOSTR)
-               fprintf(obuf, "Message %2d:\n", mp - &message[0] + 1);
-       touch(mp - &message[0] + 1);
-       send(mp, obuf, doign);
-}
-
 /*
  * Print the top so many lines of each desired message.
  * The number of lines is taken from the variable "toplines"
  * and defaults to 5.
  */
 /*
  * Print the top so many lines of each desired message.
  * 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;
@@ -427,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);
@@ -451,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[];
 {
@@ -468,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[];
 {
@@ -485,28 +408,18 @@ mboxit(msgvec)
 /*
  * List the folders the user currently has.
  */
 /*
  * List the folders the user currently has.
  */
+int
 folders()
 {
 folders()
 {
-       char dirname[BUFSIZ], cmd[BUFSIZ];
-       int pid, s, e;
+       char dirname[BUFSIZ];
+       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);
+               return 1;
        }
        }
-       switch ((pid = fork())) {
-       case 0:
-               sigchild();
-               execlp("ls", "ls", dirname, 0);
-               _exit(1);
-
-       case -1:
-               perror("fork");
-               return(-1);
-
-       default:
-               while ((e = wait(&s)) != -1 && e != pid)
-                       ;
-       }
-       return(0);
+       if ((cmd = value("LISTER")) == NOSTR)
+               cmd = "ls";
+       (void) run_command(cmd, 0, -1, -1, dirname, NOSTR, NOSTR);
+       return 0;
 }
 }