Add support for "inc" command and "autoinc" variable.
[unix-history] / usr / src / usr.bin / mail / cmd1.c
index 89aedc7..0c3b416 100644 (file)
@@ -1,25 +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 the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, 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'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)cmd1.c     5.12 (Berkeley) %G%";
+static char sccsid[] = "@(#)cmd1.c     8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "rcv.h"
 #endif /* not lint */
 
 #include "rcv.h"
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -34,6 +25,7 @@ static char sccsid[] = "@(#)cmd1.c    5.12 (Berkeley) %G%";
 
 static int screen;
 
 
 static int screen;
 
+int
 headers(msgvec)
        int *msgvec;
 {
 headers(msgvec)
        int *msgvec;
 {
@@ -63,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");
@@ -72,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[];
 {
@@ -144,6 +105,7 @@ scroll(arg)
 /*
  * Compute screen size.
  */
 /*
  * Compute screen size.
  */
+int
 screensize()
 {
        int s;
 screensize()
 {
        int s;
@@ -158,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);
@@ -177,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);
        /*
@@ -206,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);
@@ -230,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;
@@ -255,6 +219,7 @@ pcmdlist()
 /*
  * Paginate messages, honor ignored fields.
  */
 /*
  * Paginate messages, honor ignored fields.
  */
+int
 more(msgvec)
        int *msgvec;
 {
 more(msgvec)
        int *msgvec;
 {
@@ -264,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;
 {
@@ -274,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;
 {
@@ -284,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;
 {
@@ -295,27 +263,20 @@ 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 (setjmp(pipestop))
+               goto close_pipe;
        if (value("interactive") != NOSTR &&
            (page || (cp = value("crt")) != NOSTR)) {
                nlines = 0;
        if (value("interactive") != NOSTR &&
            (page || (cp = value("crt")) != NOSTR)) {
                nlines = 0;
@@ -326,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;
+                               cp = _PATH_MORE;
                        obuf = Popen(cp, "w");
                        if (obuf == NULL) {
                                perror(cp);
                                obuf = stdout;
                        obuf = Popen(cp, "w");
                        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;
+               /*
+                * Ignore SIGPIPE so it can't cause a duplicate close.
+                */
+               signal(SIGPIPE, SIG_IGN);
                Pclose(obuf);
                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);
 }
@@ -369,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;
@@ -389,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 %d:\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);
@@ -413,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[];
 {
@@ -430,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[];
 {
@@ -447,6 +408,7 @@ 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];
@@ -454,10 +416,32 @@ folders()
 
        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;
        }
        if ((cmd = value("LISTER")) == NOSTR)
                cmd = "ls";
        }
        if ((cmd = value("LISTER")) == NOSTR)
                cmd = "ls";
-       (void) run_command(cmd, 0, -1, -1, dirname, NOSTR);
+       (void) run_command(cmd, 0, -1, -1, dirname, NOSTR, NOSTR);
+       return 0;
+}
+
+/*
+ * Update the mail file with any new messages that have
+ * come in since we started reading mail.
+ */
+inc()
+{
+       int nmsg, mdot;
+
+       nmsg = incfile();
+
+       if (nmsg == 0) {
+               printf("No new mail.\n");
+       } else if (nmsg > 0) {
+               mdot = newfileinfo(msgCount - nmsg);
+               dot = &message[mdot - 1];
+       } else {
+               printf("\"inc\" command failed...\n");
+       }
+
        return 0;
 }
        return 0;
 }