fixed # in file() command -- prevfile is now set up right
[unix-history] / usr / src / usr.bin / mail / cmd3.c
index 85a31c1..ebd97a7 100644 (file)
@@ -9,7 +9,7 @@
  * Still more user commands.
  */
 
  * Still more user commands.
  */
 
-static char *SccsId = "@(#)cmd3.c      1.2 %G%";
+static char *SccsId = "@(#)cmd3.c      1.5 %G%";
 
 /*
  * Process a shell escape by saving signals, ignoring signals,
 
 /*
  * Process a shell escape by saving signals, ignoring signals,
@@ -470,20 +470,102 @@ null(e)
 }
 
 /*
 }
 
 /*
- * Print out the current edit file, if we are editting.
+ * Print out the current edit file, if we are editing.
  * Otherwise, print the name of the person who's mail
  * we are reading.
  */
 
  * Otherwise, print the name of the person who's mail
  * we are reading.
  */
 
-file(e)
+file(argv)
+       char **argv;
 {
        register char *cp;
 {
        register char *cp;
+       char fname[BUFSIZ];
 
 
-       if (edit)
-               printf("Reading \"%s\"\n", editfile);
-       else
-               printf("Reading %s's mail\n", rindex(mailname, '/') + 1);
-       return(0);
+       if (argv[0] == NOSTR) {
+               if (edit)
+                       printf("Reading \"%s\"", editfile);
+               else
+                       printf("Reading %s's mail",
+                           rindex(mailname, '/') + 1);
+               printf("; %d message(s)\n", msgCount);
+               return(0);
+       }
+
+       /*
+        * Acker's!  Must switch to the new file.
+        * We use a funny interpretation --
+        *      # -- gets the previous file
+        *      % -- gets the invoker's post office box
+        *      %user -- gets someone else's post office box
+        *      & -- gets invoker's mbox file
+        *      string -- reads the given file
+        */
+
+       cp = getfilename(argv[0]);
+       if (cp == NOSTR)
+               return(-1);
+       return(setfile(cp, 1));
+}
+
+/*
+ * Evaluate the string given as a new mailbox name.
+ * Ultimately, we want this to support a number of meta characters.
+ * Possibly:
+ *     % -- for my system mail box
+ *     %user -- for user's system mail box
+ *     # -- for previous file
+ *     & -- get's invoker's mbox file
+ *     file name -- for any other file
+ */
+
+char   prevfile[PATHSIZE];
+
+char *
+getfilename(name)
+       char *name;
+{
+       register char *cp;
+       char savename[BUFSIZ];
+
+       switch (*name) {
+       case '%':
+               strcpy(prevfile, mailname);
+               if (name[1] != 0) {
+                       strcpy(savename, myname);
+                       strncpy(myname, name+1, PATHSIZE-1);
+                       myname[PATHSIZE-1] = 0;
+                       findmail();
+                       cp = mailname;
+                       strcpy(myname, savename);
+                       return(cp);
+               }
+               findmail();
+               return(mailname);
+
+       case '#':
+               if (name[1] != 0)
+                       goto regular;
+               if (prevfile[0] == 0) {
+                       printf("No previous file\n");
+                       return(NOSTR);
+               }
+               strcpy(savename, prevfile);
+               strcpy(prevfile, mailname);
+               strcpy(mailname, savename);
+               return(mailname);
+
+       case '&':
+               strcpy(prevfile, mailname);
+               if (name[1] == 0)
+                       return(mbox);
+               /* Fall into . . . */
+
+       default:
+regular:
+               strcpy(prevfile, mailname);
+               cp = expand(name);
+               return(cp);
+       }
 }
 
 /*
 }
 
 /*
@@ -546,3 +628,79 @@ Respond(msgvec)
        mail1(&head);
        return(0);
 }
        mail1(&head);
        return(0);
 }
+
+/*
+ * Conditional commands.  These allow one to parameterize one's
+ * .mailrc and do some things if sending, others if receiving.
+ */
+
+ifcmd(argv)
+       char **argv;
+{
+       register char *cp;
+
+       if (cond != CANY) {
+               printf("Illegal nested \"if\"\n");
+               return(1);
+       }
+       cond = CANY;
+       cp = argv[0];
+       switch (*cp) {
+       case 'r': case 'R':
+               cond = CRCV;
+               break;
+
+       case 's': case 'S':
+               cond = CSEND;
+               break;
+
+       default:
+               printf("Unrecognized if-keyword: \"%s\"\n", cp);
+               return(1);
+       }
+       return(0);
+}
+
+/*
+ * Implement 'else'.  This is pretty simple -- we just
+ * flip over the conditional flag.
+ */
+
+elsecmd()
+{
+
+       switch (cond) {
+       case CANY:
+               printf("\"Else\" without matching \"if\"\n");
+               return(1);
+
+       case CSEND:
+               cond = CRCV;
+               break;
+
+       case CRCV:
+               cond = CSEND;
+               break;
+
+       default:
+               printf("Mail's idea of conditions is screwed up\n");
+               cond = CANY;
+               break;
+       }
+       return(0);
+}
+
+/*
+ * End of if statement.  Just set cond back to anything.
+ */
+
+endifcmd()
+{
+
+       if (cond == CANY) {
+               printf("\"Endif\" without matching \"if\"\n");
+               return(1);
+       }
+       cond = CANY;
+       return(0);
+}