port -file switch to pick
authorPhilipp Takacs <philipp@bureaucracy.de>
Sun, 28 May 2017 12:49:52 +0000 (14:49 +0200)
committerPhilipp Takacs <philipp@bureaucracy.de>
Sun, 28 May 2017 12:49:52 +0000 (14:49 +0200)
now scan can full replaced by pick

uip/pick.c

index 464a734..25bd6b6 100644 (file)
@@ -73,9 +73,11 @@ static struct swit switches[] = {
     { "width columns", 0 },
 #define THREADSW  24
        { "thread", 0 },
     { "width columns", 0 },
 #define THREADSW  24
        { "thread", 0 },
-#define VERSIONSW  25
+#define FILESW  25
+       { "file file", 0 },
+#define VERSIONSW  26
        { "Version", 0 },
        { "Version", 0 },
-#define HELPSW  26
+#define HELPSW  27
        { "help", 0 },
        { NULL, 0 }
 };
        { "help", 0 },
        { NULL, 0 }
 };
@@ -100,6 +102,7 @@ static int pcompile(char **, char *);
 static int pmatches(FILE *, int);
 static struct nexus * createonethread(char *);
 static struct nexus * createpickthread(char *);
 static int pmatches(FILE *, int);
 static struct nexus * createonethread(char *);
 static struct nexus * createpickthread(char *);
+static void scan_mbox(char *, char *, int);
 
 
 static int listsw = -1;
 
 
 static int listsw = -1;
@@ -122,6 +125,7 @@ main(int argc, char **argv)
        char *form = NULL;
        char *fmtstr;
        FILE *fp;
        char *form = NULL;
        char *fmtstr;
        FILE *fp;
+       char *file = NULL;
 
        if (atexit(putzero_done) != 0) {
                adios(EX_OSERR, NULL, "atexit failed");
 
        if (atexit(putzero_done) != 0) {
                adios(EX_OSERR, NULL, "atexit failed");
@@ -237,6 +241,15 @@ main(int argc, char **argv)
                                }
                                width = atoi(cp);
                                continue;
                                }
                                width = atoi(cp);
                                continue;
+                       case FILESW:
+                               if (!(cp = *argp++) || (cp[0] == '-' && cp[1])) {
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               }
+                               if (strcmp(file = cp, "-")!=0) {
+                                       file = mh_xstrdup(expanddir(cp));
+                               }
+                               continue;
                        }
                }
                if (*cp == '+' || *cp == '@') {
                        }
                }
                if (*cp == '+' || *cp == '@') {
@@ -251,6 +264,21 @@ main(int argc, char **argv)
 
        fmtstr = new_fs(form, "pick.default");
 
 
        fmtstr = new_fs(form, "pick.default");
 
+       if (file) {
+               if (folder) {
+                       adios(EX_USAGE, NULL, "\"+folder\" not allowed with -file");
+               }
+               if (msgs.size) {
+                       adios(EX_USAGE, NULL, "\"msgs\" not allowed with -file");
+               }
+               if (vecp) {
+                       adios(EX_USAGE, NULL, "section arguments not allowed with -file");
+               }
+
+               scan_mbox(file, fmtstr, width);
+               exit(EX_OK);
+       }
+
        /*
        ** If we didn't specify which messages to search,
        ** then search the whole folder.
        /*
        ** If we didn't specify which messages to search,
        ** then search the whole folder.
@@ -363,6 +391,29 @@ main(int argc, char **argv)
        return 0;
 }
 
        return 0;
 }
 
+static void
+scan_mbox(char *file, char *fmtstr, int width)
+{
+       FILE *in;
+       int msgnum;
+       int state;
+
+       if (strcmp(file, "-") == 0) {
+               in = stdin;
+               file = "stdin";
+       } else if (!(in = fopen(file, "r"))) {
+               adios(EX_IOERR, file, "unable to open");
+       }
+
+       for (msgnum = 1; ;msgnum++) {
+               state = scan(in, msgnum, SCN_MBOX, fmtstr, width, 0, 0);
+               if (state != SCNMSG) {
+                       break;
+               }
+       }
+       fclose(in);
+}
+
 
 void
 putzero_done()
 
 void
 putzero_done()