4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd2.c
index b4abaae..e3734a7 100644 (file)
@@ -1,7 +1,17 @@
-#
+/*
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)cmd2.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
  * More user commands.
  */
 
  * More user commands.
  */
 
-static char *SccsId = "@(#)cmd2.c      2.1 %G%";
-
 /*
  * If any arguments were given, go to the next applicable argument
  * following dot, otherwise, go to the next applicable message.
  * If given as first command with no arguments, print first message.
  */
 /*
  * If any arguments were given, go to the next applicable argument
  * following dot, otherwise, go to the next applicable message.
  * If given as first command with no arguments, print first message.
  */
-
+int
 next(msgvec)
        int *msgvec;
 {
 next(msgvec)
        int *msgvec;
 {
@@ -92,18 +100,44 @@ hitit:
 }
 
 /*
 }
 
 /*
- * Save the indicated messages at the end of the passed file name.
+ * Save a message in a file.  Mark the message as saved
+ * so we can discard when the user quits.
  */
  */
-
+int
 save(str)
        char str[];
 {
 save(str)
        char str[];
 {
-       register int *ip, mesg;
+
+       return save1(str, 1, "save", saveignore);
+}
+
+/*
+ * Copy a message to a file without affected its saved-ness
+ */
+int
+copycmd(str)
+       char str[];
+{
+
+       return save1(str, 0, "copy", saveignore);
+}
+
+/*
+ * Save/copy the indicated messages at the end of the passed file name.
+ * If mark is true, mark the message "saved."
+ */
+int
+save1(str, mark, cmd, ignore)
+       char str[];
+       int mark;
+       char *cmd;
+       struct ignoretab *ignore;
+{
+       register int *ip;
        register struct message *mp;
        char *file, *disp;
        register struct message *mp;
        char *file, *disp;
-       int f, *msgvec, lc, cc, t;
+       int f, *msgvec;
        FILE *obuf;
        FILE *obuf;
-       struct stat statb;
 
        msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
        if ((file = snarf(str, &f)) == NOSTR)
 
        msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
        if ((file = snarf(str, &f)) == NOSTR)
@@ -111,7 +145,7 @@ save(str)
        if (!f) {
                *msgvec = first(0, MMNORM);
                if (*msgvec == NULL) {
        if (!f) {
                *msgvec = first(0, MMNORM);
                if (*msgvec == NULL) {
-                       printf("No messages to save.\n");
+                       printf("No messages to %s.\n", cmd);
                        return(1);
                }
                msgvec[1] = NULL;
                        return(1);
                }
                msgvec[1] = NULL;
@@ -121,34 +155,31 @@ save(str)
        if ((file = expand(file)) == NOSTR)
                return(1);
        printf("\"%s\" ", file);
        if ((file = expand(file)) == NOSTR)
                return(1);
        printf("\"%s\" ", file);
-       flush();
-       if (stat(file, &statb) >= 0)
+       fflush(stdout);
+       if (access(file, 0) >= 0)
                disp = "[Appended]";
        else
                disp = "[New file]";
                disp = "[Appended]";
        else
                disp = "[New file]";
-       if ((obuf = fopen(file, "a")) == NULL) {
+       if ((obuf = Fopen(file, "a")) == NULL) {
                perror(NOSTR);
                return(1);
        }
                perror(NOSTR);
                return(1);
        }
-       cc = lc = 0;
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
-               if ((t = send(mp, obuf)) < 0) {
+               mp = &message[*ip - 1];
+               touch(mp);
+               if (send(mp, obuf, ignore, NOSTR) < 0) {
                        perror(file);
                        perror(file);
-                       fclose(obuf);
+                       Fclose(obuf);
                        return(1);
                }
                        return(1);
                }
-               lc += t;
-               cc += msize(mp);
-               mp->m_flag |= MSAVED;
+               if (mark)
+                       mp->m_flag |= MSAVED;
        }
        fflush(obuf);
        if (ferror(obuf))
                perror(file);
        }
        fflush(obuf);
        if (ferror(obuf))
                perror(file);
-       fclose(obuf);
-       printf("%s %d/%d\n", disp, lc, cc);
+       Fclose(obuf);
+       printf("%s\n", disp);
        return(0);
 }
 
        return(0);
 }
 
@@ -156,65 +187,12 @@ save(str)
  * Write the indicated messages at the end of the passed
  * file name, minus header and trailing blank line.
  */
  * Write the indicated messages at the end of the passed
  * file name, minus header and trailing blank line.
  */
-
+int
 swrite(str)
        char str[];
 {
 swrite(str)
        char str[];
 {
-       register int *ip, mesg;
-       register struct message *mp;
-       register char *file, *disp;
-       char linebuf[BUFSIZ];
-       int f, *msgvec, lc, cc, t;
-       FILE *obuf, *mesf;
-       struct stat statb;
 
 
-       msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
-       if ((file = snarf(str, &f)) == NOSTR)
-               return(1);
-       if ((file = expand(file)) == NOSTR)
-               return(1);
-       if (!f) {
-               *msgvec = first(0, MMNORM);
-               if (*msgvec == NULL) {
-                       printf("No messages to write.\n");
-                       return(1);
-               }
-               msgvec[1] = NULL;
-       }
-       if (f && getmsglist(str, msgvec, 0) < 0)
-               return(1);
-       printf("\"%s\" ", file);
-       flush();
-       if (stat(file, &statb) >= 0)
-               disp = "[Appended]";
-       else
-               disp = "[New file]";
-       if ((obuf = fopen(file, "a")) == NULL) {
-               perror(NOSTR);
-               return(1);
-       }
-       cc = lc = 0;
-       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
-               mesf = setinput(mp);
-               t = mp->m_lines - 2;
-               readline(mesf, linebuf);
-               while (t-- > 0) {
-                       fgets(linebuf, BUFSIZ, mesf);
-                       fputs(linebuf, obuf);
-                       cc += strlen(linebuf);
-               }
-               lc += mp->m_lines - 2;
-               mp->m_flag |= MSAVED;
-       }
-       fflush(obuf);
-       if (ferror(obuf))
-               perror(file);
-       fclose(obuf);
-       printf("%s %d/%d\n", disp, lc, cc);
-       return(0);
+       return save1(str, 1, "write", ignoreall);
 }
 
 /*
 }
 
 /*
@@ -240,7 +218,7 @@ snarf(linebuf, flag)
         * Strip away trailing blanks.
         */
 
         * Strip away trailing blanks.
         */
 
-       while (*cp == ' ' && cp > linebuf)
+       while (cp > linebuf && isspace(*cp))
                cp--;
        *++cp = 0;
 
                cp--;
        *++cp = 0;
 
@@ -248,13 +226,13 @@ snarf(linebuf, flag)
         * Now search for the beginning of the file name.
         */
 
         * Now search for the beginning of the file name.
         */
 
-       while (cp > linebuf && !any(*cp, "\t "))
+       while (cp > linebuf && !isspace(*cp))
                cp--;
        if (*cp == '\0') {
                printf("No file specified.\n");
                return(NOSTR);
        }
                cp--;
        if (*cp == '\0') {
                printf("No file specified.\n");
                return(NOSTR);
        }
-       if (any(*cp, " \t"))
+       if (isspace(*cp))
                *cp++ = 0;
        else
                *flag = 0;
                *cp++ = 0;
        else
                *flag = 0;
@@ -264,33 +242,36 @@ snarf(linebuf, flag)
 /*
  * Delete messages.
  */
 /*
  * Delete messages.
  */
-
+int
 delete(msgvec)
        int msgvec[];
 {
 delete(msgvec)
        int msgvec[];
 {
-       return(delm(msgvec));
+       delm(msgvec);
+       return 0;
 }
 
 /*
  * Delete messages, then type the new dot.
  */
 }
 
 /*
  * Delete messages, then type the new dot.
  */
-
+int
 deltype(msgvec)
        int msgvec[];
 {
        int list[2];
 deltype(msgvec)
        int msgvec[];
 {
        int list[2];
+       int lastdot;
 
 
+       lastdot = dot - &message[0] + 1;
        if (delm(msgvec) >= 0) {
        if (delm(msgvec) >= 0) {
-               list[0] = dot - &message[0];
-               list[0]++;
-               touch(list[0]);
-               list[1] = NULL;
-               return(type(list));
-       }
-       else {
+               list[0] = dot - &message[0] + 1;
+               if (list[0] > lastdot) {
+                       touch(dot);
+                       list[1] = NULL;
+                       return(type(list));
+               }
+               printf("At EOF\n");
+       } else
                printf("No more messages\n");
                printf("No more messages\n");
-               return(0);
-       }
+       return(0);
 }
 
 /*
 }
 
 /*
@@ -298,22 +279,21 @@ deltype(msgvec)
  * Set dot to some nice place afterwards.
  * Internal interface.
  */
  * Set dot to some nice place afterwards.
  * Internal interface.
  */
-
+int
 delm(msgvec)
        int *msgvec;
 {
        register struct message *mp;
 delm(msgvec)
        int *msgvec;
 {
        register struct message *mp;
-       register *ip, mesg;
+       register *ip;
        int last;
 
        last = NULL;
        for (ip = msgvec; *ip != NULL; ip++) {
        int last;
 
        last = NULL;
        for (ip = msgvec; *ip != NULL; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
+               mp = &message[*ip - 1];
+               touch(mp);
                mp->m_flag |= MDELETED|MTOUCH;
                mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
                mp->m_flag |= MDELETED|MTOUCH;
                mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
-               last = mesg;
+               last = *ip;
        }
        if (last != NULL) {
                dot = &message[last-1];
        }
        if (last != NULL) {
                dot = &message[last-1];
@@ -338,47 +318,187 @@ delm(msgvec)
 /*
  * Undelete the indicated messages.
  */
 /*
  * Undelete the indicated messages.
  */
-
+int
 undelete(msgvec)
        int *msgvec;
 {
        register struct message *mp;
 undelete(msgvec)
        int *msgvec;
 {
        register struct message *mp;
-       register *ip, mesg;
-
-       for (ip = msgvec; ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               if (mesg == 0)
-                       return;
-               touch(mesg);
-               mp = &message[mesg-1];
+       register *ip;
+
+       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
+               mp = &message[*ip - 1];
+               touch(mp);
                dot = mp;
                mp->m_flag &= ~MDELETED;
        }
                dot = mp;
                mp->m_flag &= ~MDELETED;
        }
+       return 0;
 }
 
 /*
  * Interactively dump core on "core"
  */
 }
 
 /*
  * Interactively dump core on "core"
  */
-
+int
 core()
 {
 core()
 {
-       register int pid;
-       int status;
+       int pid;
+       extern union wait wait_status;
 
 
-       if ((pid = vfork()) == -1) {
+       switch (pid = vfork()) {
+       case -1:
                perror("fork");
                return(1);
                perror("fork");
                return(1);
-       }
-       if (pid == 0) {
+       case 0:
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
-       while (wait(&status) != pid)
-               ;
-       if (status & 0200)
-               printf(" -- Core dumped\n");
+       wait_child(pid);
+       if (wait_status.w_coredump)
+               printf(" -- Core dumped.\n");
+       else
+               printf(" -- Can't dump core.\n");
+       return 0;
+}
+
+/*
+ * Clobber as many bytes of stack as the user requests.
+ */
+int
+clobber(argv)
+       char **argv;
+{
+       register int times;
+
+       if (argv[0] == 0)
+               times = 1;
        else
        else
-               printf("\n");
+               times = (atoi(argv[0]) + 511) / 512;
+       clob1(times);
+       return 0;
+}
+
+/*
+ * Clobber the stack.
+ */
+void
+clob1(n)
+       int n;
+{
+       char buf[512];
+       register char *cp;
+
+       if (n <= 0)
+               return;
+       for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
+               ;
+       clob1(n - 1);
+}
+
+/*
+ * Add the given header fields to the retained list.
+ * If no arguments, print the current list of retained fields.
+ */
+int
+retfield(list)
+       char *list[];
+{
+
+       return ignore1(list, ignore + 1, "retained");
+}
+
+/*
+ * Add the given header fields to the ignored list.
+ * If no arguments, print the current list of ignored fields.
+ */
+int
+igfield(list)
+       char *list[];
+{
+
+       return ignore1(list, ignore, "ignored");
+}
+
+int
+saveretfield(list)
+       char *list[];
+{
+
+       return ignore1(list, saveignore + 1, "retained");
+}
+
+int
+saveigfield(list)
+       char *list[];
+{
+
+       return ignore1(list, saveignore, "ignored");
+}
+
+int
+ignore1(list, tab, which)
+       char *list[];
+       struct ignoretab *tab;
+       char *which;
+{
+       char field[BUFSIZ];
+       register int h;
+       register struct ignore *igp;
+       char **ap;
+
+       if (*list == NOSTR)
+               return igshow(tab, which);
+       for (ap = list; *ap != 0; ap++) {
+               istrcpy(field, *ap);
+               if (member(field, tab))
+                       continue;
+               h = hash(field);
+               igp = (struct ignore *) calloc(1, sizeof (struct ignore));
+               igp->i_field = calloc((unsigned) strlen(field) + 1,
+                       sizeof (char));
+               strcpy(igp->i_field, field);
+               igp->i_link = tab->i_head[h];
+               tab->i_head[h] = igp;
+               tab->i_count++;
+       }
+       return 0;
+}
+
+/*
+ * Print out all currently retained fields.
+ */
+int
+igshow(tab, which)
+       struct ignoretab *tab;
+       char *which;
+{
+       register int h;
+       struct ignore *igp;
+       char **ap, **ring;
+       int igcomp();
+
+       if (tab->i_count == 0) {
+               printf("No fields currently being %s.\n", which);
+               return 0;
+       }
+       ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
+       ap = ring;
+       for (h = 0; h < HSHSIZE; h++)
+               for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
+                       *ap++ = igp->i_field;
+       *ap = 0;
+       qsort(ring, tab->i_count, sizeof (char *), igcomp);
+       for (ap = ring; *ap != 0; ap++)
+               printf("%s\n", *ap);
+       return 0;
+}
+
+/*
+ * Compare two names for sorting ignored field list.
+ */
+int
+igcomp(l, r)
+       const void *l, *r;
+{
+       return (strcmp(*(char **)l, *(char **)r));
 }
 }