document distributed with 4.2BSD
[unix-history] / usr / src / usr.bin / mail / cmd1.c
index 6a858c4..50379b0 100644 (file)
@@ -1,4 +1,13 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)cmd1.c      5.3 (Berkeley) %G%";
+#endif not lint
+
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
 
@@ -8,10 +17,9 @@
  * User commands.
  */
 
  * User commands.
  */
 
-static char *SccsId = "@(#)cmd1.c      1.2 %G%";
-
 /*
  * Print the current active headings.
 /*
  * Print the current active headings.
+ * Don't change dot if invoker didn't give an argument.
  */
 
 static int screen;
  */
 
 static int screen;
@@ -21,25 +29,28 @@ headers(msgvec)
 {
        register int n, mesg, flag;
        register struct message *mp;
 {
        register int n, mesg, flag;
        register struct message *mp;
+       int size;
 
 
+       size = screensize();
        n = msgvec[0];
        if (n != 0)
        n = msgvec[0];
        if (n != 0)
-               screen = (n-1)/SCREEN;
+               screen = (n-1)/size;
        if (screen < 0)
                screen = 0;
        if (screen < 0)
                screen = 0;
-       mp = &message[screen * SCREEN];
+       mp = &message[screen * size];
        if (mp >= &message[msgCount])
        if (mp >= &message[msgCount])
-               mp = &message[msgCount - SCREEN];
+               mp = &message[msgCount - size];
        if (mp < &message[0])
                mp = &message[0];
        flag = 0;
        mesg = mp - &message[0];
        if (mp < &message[0])
                mp = &message[0];
        flag = 0;
        mesg = mp - &message[0];
-       dot = mp;
+       if (dot != &message[n-1])
+               dot = mp;
        for (; mp < &message[msgCount]; mp++) {
                mesg++;
                if (mp->m_flag & MDELETED)
                        continue;
        for (; mp < &message[msgCount]; mp++) {
                mesg++;
                if (mp->m_flag & MDELETED)
                        continue;
-               if (flag++ >= SCREEN)
+               if (flag++ >= size)
                        break;
                printhead(mesg);
                sreset();
                        break;
                printhead(mesg);
                sreset();
@@ -51,6 +62,36 @@ 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
  */
@@ -58,16 +99,17 @@ headers(msgvec)
 scroll(arg)
        char arg[];
 {
 scroll(arg)
        char arg[];
 {
-       register int s;
+       register int s, size;
        int cur[1];
 
        cur[0] = 0;
        int cur[1];
 
        cur[0] = 0;
+       size = screensize();
        s = screen;
        switch (*arg) {
        case 0:
        case '+':
                s++;
        s = screen;
        switch (*arg) {
        case 0:
        case '+':
                s++;
-               if (s*SCREEN > msgCount) {
+               if (s * size > msgCount) {
                        printf("On last screenful of messages\n");
                        return(0);
                }
                        printf("On last screenful of messages\n");
                        return(0);
                }
@@ -89,6 +131,39 @@ scroll(arg)
        return(headers(cur));
 }
 
        return(headers(cur));
 }
 
+/*
+ * 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
+ */
+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);
+}
 
 /*
  * Print out the headlines for each message
 
 /*
  * Print out the headlines for each message
@@ -118,7 +193,7 @@ printhead(mesg)
 {
        struct message *mp;
        FILE *ibuf;
 {
        struct message *mp;
        FILE *ibuf;
-       char headline[LINESIZE], wcount[10], *subjline, dispc;
+       char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
        char pbuf[BUFSIZ];
        int s;
        struct headline hl;
        char pbuf[BUFSIZ];
        int s;
        struct headline hl;
@@ -137,24 +212,31 @@ printhead(mesg)
 
        if (subjline != NOSTR && strlen(subjline) > 28)
                subjline[29] = '\0';
 
        if (subjline != NOSTR && strlen(subjline) > 28)
                subjline[29] = '\0';
+       curind = dot == mp ? '>' : ' ';
        dispc = ' ';
        if (mp->m_flag & MSAVED)
                dispc = '*';
        if (mp->m_flag & MPRESERVE)
                dispc = 'P';
        dispc = ' ';
        if (mp->m_flag & MSAVED)
                dispc = '*';
        if (mp->m_flag & MPRESERVE)
                dispc = 'P';
+       if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
+               dispc = 'N';
+       if ((mp->m_flag & (MREAD|MNEW)) == 0)
+               dispc = 'U';
+       if (mp->m_flag & MBOX)
+               dispc = 'M';
        parse(headline, &hl, pbuf);
        parse(headline, &hl, pbuf);
-       sprintf(wcount, " %d/%d", mp->m_lines, mp->m_size);
+       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)
        s = strlen(wcount);
        cp = wcount + s;
        while (s < 7)
                s++, *cp++ = ' ';
        *cp = '\0';
        if (subjline != NOSTR)
-               printf("%c%3d %-8s %16.16s %s \"%s\"\n", dispc, mesg,
-                   nameof(mp), hl.l_date, wcount, subjline);
+               printf("%c%c%3d %-8s %16.16s %s \"%s\"\n", curind, dispc, mesg,
+                   nameof(mp, 0), hl.l_date, wcount, subjline);
        else
        else
-               printf("%c%3d %-8s %16.16s %s\n", dispc, mesg,
-                   nameof(mp), hl.l_date, wcount);
+               printf("%c%c%3d %-8s %16.16s %s\n", curind, dispc, mesg,
+                   nameof(mp, 0), hl.l_date, wcount);
 }
 
 /*
 }
 
 /*
@@ -193,14 +275,52 @@ pcmdlist()
 }
 
 /*
 }
 
 /*
- * Type out the messages requested.
+ * Paginate messages, honor ignored fields.
  */
  */
+more(msgvec)
+       int *msgvec;
+{
+       return (type1(msgvec, 1, 1));
+}
 
 
-jmp_buf        pipestop;
+/*
+ * Paginate messages, even printing ignored fields.
+ */
+More(msgvec)
+       int *msgvec;
+{
+
+       return (type1(msgvec, 0, 1));
+}
 
 
+/*
+ * Type out messages, honor ignored fields.
+ */
 type(msgvec)
        int *msgvec;
 {
 type(msgvec)
        int *msgvec;
 {
+
+       return(type1(msgvec, 1, 0));
+}
+
+/*
+ * Type out messages, even printing ignored fields.
+ */
+Type(msgvec)
+       int *msgvec;
+{
+
+       return(type1(msgvec, 0, 0));
+}
+
+/*
+ * Type out the messages requested.
+ */
+jmp_buf        pipestop;
+
+type1(msgvec, doign, page)
+       int *msgvec;
+{
        register *ip;
        register struct message *mp;
        register int mesg;
        register *ip;
        register struct message *mp;
        register int mesg;
@@ -212,24 +332,30 @@ type(msgvec)
        obuf = stdout;
        if (setjmp(pipestop)) {
                if (obuf != stdout) {
        obuf = stdout;
        if (setjmp(pipestop)) {
                if (obuf != stdout) {
-                       pclose(obuf);
                        pipef = NULL;
                        pipef = NULL;
+                       pclose(obuf);
                }
                }
-               signal(SIGPIPE, SIG_DFL);
+               sigset(SIGPIPE, SIG_DFL);
                return(0);
        }
                return(0);
        }
-       if (intty && outtty && (cp = value("crt")) != NOSTR) {
-               for (ip = msgvec, nlines = 0; *ip && ip-msgvec < msgCount; ip++)
-                       nlines += message[*ip - 1].m_lines;
-               if (nlines > atoi(cp)) {
-                       obuf = popen(MORE, "w");
+       if (intty && outtty && (page || (cp = value("crt")) != NOSTR)) {
+               nlines = 0;
+               if (!page) {
+                       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
+                               nlines += message[*ip - 1].m_lines;
+               }
+               if (page || nlines > atoi(cp)) {
+                       cp = value("PAGER");
+                       if (cp == NULL || *cp == '\0')
+                               cp = MORE;
+                       obuf = popen(cp, "w");
                        if (obuf == NULL) {
                        if (obuf == NULL) {
-                               perror(MORE);
+                               perror(cp);
                                obuf = stdout;
                        }
                        else {
                                pipef = obuf;
                                obuf = stdout;
                        }
                        else {
                                pipef = obuf;
-                               signal(SIGPIPE, brokpipe);
+                               sigset(SIGPIPE, brokpipe);
                        }
                }
        }
                        }
                }
        }
@@ -238,13 +364,13 @@ type(msgvec)
                touch(mesg);
                mp = &message[mesg-1];
                dot = mp;
                touch(mesg);
                mp = &message[mesg-1];
                dot = mp;
-               print(mp, obuf);
+               print(mp, obuf, doign);
        }
        }
-       signal(SIGPIPE, SIG_DFL);
        if (obuf != stdout) {
        if (obuf != stdout) {
-               pclose(obuf);
                pipef = NULL;
                pipef = NULL;
+               pclose(obuf);
        }
        }
+       sigset(SIGPIPE, SIG_DFL);
        return(0);
 }
 
        return(0);
 }
 
@@ -255,8 +381,9 @@ type(msgvec)
 
 brokpipe()
 {
 
 brokpipe()
 {
-
-       signal(SIGPIPE, SIG_IGN);
+# ifndef VMUNIX
+       signal(SIGPIPE, brokpipe);
+# endif
        longjmp(pipestop, 1);
 }
 
        longjmp(pipestop, 1);
 }
 
@@ -264,7 +391,7 @@ brokpipe()
  * Print the indicated message on standard output.
  */
 
  * Print the indicated message on standard output.
  */
 
-print(mp, obuf)
+print(mp, obuf, doign)
        register struct message *mp;
        FILE *obuf;
 {
        register struct message *mp;
        FILE *obuf;
 {
@@ -272,7 +399,7 @@ print(mp, obuf)
        if (value("quiet") == NOSTR)
                fprintf(obuf, "Message %2d:\n", mp - &message[0] + 1);
        touch(mp - &message[0] + 1);
        if (value("quiet") == NOSTR)
                fprintf(obuf, "Message %2d:\n", mp - &message[0] + 1);
        touch(mp - &message[0] + 1);
-       send(mp, obuf);
+       send(mp, obuf, doign);
 }
 
 /*
 }
 
 /*
@@ -331,9 +458,55 @@ stouch(msgvec)
        register int *ip;
 
        for (ip = msgvec; *ip != 0; ip++) {
        register int *ip;
 
        for (ip = msgvec; *ip != 0; ip++) {
-               touch(*ip);
                dot = &message[*ip-1];
                dot = &message[*ip-1];
+               dot->m_flag |= MTOUCH;
+               dot->m_flag &= ~MPRESERVE;
+       }
+       return(0);
+}
+
+/*
+ * Make sure all passed messages get mboxed.
+ */
+
+mboxit(msgvec)
+       int msgvec[];
+{
+       register int *ip;
+
+       for (ip = msgvec; *ip != 0; ip++) {
+               dot = &message[*ip-1];
+               dot->m_flag |= MTOUCH|MBOX;
                dot->m_flag &= ~MPRESERVE;
        }
        return(0);
 }
                dot->m_flag &= ~MPRESERVE;
        }
        return(0);
 }
+
+/*
+ * List the folders the user currently has.
+ */
+folders()
+{
+       char dirname[BUFSIZ], cmd[BUFSIZ];
+       int pid, s, e;
+
+       if (getfold(dirname) < 0) {
+               printf("No value set for \"folder\"\n");
+               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);
+}