]> diplodocus.org Git - nmh/blobdiff - uip/scan.c
Refined number of bytes copied to buffer in several places.
[nmh] / uip / scan.c
index 60c5ebccbdd582ea8714603dfc153d6dd08cfd4a..c1f27194de1420f3c61f9a5af914bd7033116c34 100644 (file)
@@ -5,15 +5,37 @@
  * complete copyright information.
  */
 
  * complete copyright information.
  */
 
-#include <h/mh.h>
-#include <h/fmt_scan.h>
-#include <h/scansbr.h>
-#include <h/tws.h>
-#include <h/mts.h>
+#include "h/mh.h"
+#include "sbr/fmt_new.h"
+#include "sbr/dtime.h"
+#include "scansbr.h"
+#include "sbr/m_name.h"
+#include "sbr/getarguments.h"
+#include "sbr/seq_setprev.h"
+#include "sbr/seq_save.h"
+#include "sbr/smatch.h"
+#include "sbr/m_convert.h"
+#include "sbr/getfolder.h"
+#include "sbr/folder_read.h"
+#include "sbr/folder_free.h"
+#include "sbr/context_save.h"
+#include "sbr/context_replace.h"
+#include "sbr/context_find.h"
+#include "sbr/brkstring.h"
+#include "sbr/ambigsw.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/seq_getnum.h"
+#include "sbr/error.h"
+#include "h/fmt_scan.h"
+#include "h/tws.h"
+#include "h/mts.h"
 #include "h/done.h"
 #include "h/done.h"
-#include <h/utils.h>
+#include "h/utils.h"
 #include "sbr/m_maildir.h"
 #include "sbr/terminal.h"
 #include "sbr/m_maildir.h"
 #include "sbr/terminal.h"
+#include "sbr/maildir_read_and_sort.h"
 
 #define SCAN_SWITCHES \
     X("clear", 0, CLRSW) \
 
 #define SCAN_SWITCHES \
     X("clear", 0, CLRSW) \
@@ -43,7 +65,7 @@ main (int argc, char **argv)
 {
     bool clearflag = false;
     bool hdrflag = false;
 {
     bool clearflag = false;
     bool hdrflag = false;
-    int ontty;
+    int ontty = 0;
     int width = -1;
     bool revflag = false;
     int i, state, msgnum;
     int width = -1;
     bool revflag = false;
     int i, state, msgnum;
@@ -55,6 +77,7 @@ main (int argc, char **argv)
     char **argp, *nfs, **arguments;
     struct msgs_array msgs = { 0, 0, NULL };
     struct msgs *mp;
     char **argp, *nfs, **arguments;
     struct msgs_array msgs = { 0, 0, NULL };
     struct msgs *mp;
+    charstring_t scanl = NULL;
     FILE *in;
 
     if (nmh_init(argv[0], true, true)) { return 1; }
     FILE *in;
 
     if (nmh_init(argv[0], true, true)) { return 1; }
@@ -69,13 +92,13 @@ main (int argc, char **argv)
     while ((cp = *argp++)) {
        if (*cp == '-') {
            switch (smatch (++cp, switches)) {
     while ((cp = *argp++)) {
        if (*cp == '-') {
            switch (smatch (++cp, switches)) {
-               case AMBIGSW: 
+               case AMBIGSW:
                    ambigsw (cp, switches);
                    done (1);
                    ambigsw (cp, switches);
                    done (1);
-               case UNKWNSW: 
+               case UNKWNSW:
                    die("-%s unknown", cp);
 
                    die("-%s unknown", cp);
 
-               case HELPSW: 
+               case HELPSW:
                    snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
                        invo_name);
                    print_help (buf, switches, 1);
                    snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
                        invo_name);
                    print_help (buf, switches, 1);
@@ -84,32 +107,32 @@ main (int argc, char **argv)
                    print_version(invo_name);
                    done (0);
 
                    print_version(invo_name);
                    done (0);
 
-               case CLRSW: 
+               case CLRSW:
                    clearflag = true;
                    continue;
                    clearflag = true;
                    continue;
-               case NCLRSW: 
+               case NCLRSW:
                    clearflag = false;
                    continue;
 
                    clearflag = false;
                    continue;
 
-               case FORMSW: 
+               case FORMSW:
                    if (!(form = *argp++) || *form == '-')
                        die("missing argument to %s", argp[-2]);
                    format = NULL;
                    continue;
                    if (!(form = *argp++) || *form == '-')
                        die("missing argument to %s", argp[-2]);
                    format = NULL;
                    continue;
-               case FMTSW: 
+               case FMTSW:
                    if (!(format = *argp++) || *format == '-')
                        die("missing argument to %s", argp[-2]);
                    form = NULL;
                    continue;
 
                    if (!(format = *argp++) || *format == '-')
                        die("missing argument to %s", argp[-2]);
                    form = NULL;
                    continue;
 
-               case HEADSW: 
+               case HEADSW:
                    hdrflag = true;
                    continue;
                    hdrflag = true;
                    continue;
-               case NHEADSW: 
+               case NHEADSW:
                    hdrflag = false;
                    continue;
 
                    hdrflag = false;
                    continue;
 
-               case WIDTHSW: 
+               case WIDTHSW:
                    if (!(cp = *argp++) || *cp == '-')
                        die("missing argument to %s", argp[-2]);
                    width = atoi (cp);
                    if (!(cp = *argp++) || *cp == '-')
                        die("missing argument to %s", argp[-2]);
                    width = atoi (cp);
@@ -145,10 +168,10 @@ main (int argc, char **argv)
      */
     nfs = new_fs (form, format, FORMAT);
 
      */
     nfs = new_fs (form, format, FORMAT);
 
-    /*
-     * We are scanning a maildrop file
-     */
     if (file) {
     if (file) {
+        /*
+         * We have a -file argument
+         */
        if (msgs.size)
            die("\"msgs\" not allowed with -file");
        if (folder)
        if (msgs.size)
            die("\"msgs\" not allowed with -file");
        if (folder)
@@ -159,24 +182,77 @@ main (int argc, char **argv)
            in = stdin;
            file = "stdin";
        } else {
            in = stdin;
            file = "stdin";
        } else {
-           if ((in = fopen (file, "r")) == NULL)
-               adios (file, "unable to open");
+           /* check if "file" is really a Maildir folder */
+           struct stat st;
+           if (stat (file, &st) == NOTOK)
+               adios (file, "unable to stat");
+           if (!(st.st_mode & S_IFDIR)) {
+               if ((in = fopen (file, "r")) == NULL)
+                   adios (file, "unable to open");
+           } else {
+               /*
+                * We are scanning a Maildir folder
+                */
+               struct Maildir_entry *Maildir;
+               int num_maildir_entries;
+               int msgnum = 0;
+               char *fnp;
+               FILE *in;
+               int i;
+
+               maildir_read_and_sort(file, &Maildir, &num_maildir_entries);
+               for (i = 0; i < num_maildir_entries; i++) {
+                   msgnum++;
+                   fnp = Maildir[i].filename;
+
+                   if ((in = fopen (fnp, "r")) == NULL) {
+                       admonish (fnp, "unable to open message");
+                       continue;
+                   }
+
+                   switch (state = scan (in, msgnum, 0, nfs, width,
+                                         0, 0, hdrflag ? file : NULL,
+                                         0L, 1, &scanl)) {
+                   case SCNMSG:
+                   case SCNERR:
+                       break;
+
+                   default:
+                       die("scan() botch (%d)", state);
+
+                   case SCNEOF:
+                       inform("message %d: empty", msgnum);
+                       break;
+                   }
+                   if (scanl)
+                       charstring_clear(scanl);
+                   scan_finished ();
+                   hdrflag = false;
+                   fclose (in);
+                   if (ontty)
+                       fflush (stdout);
+               }
+               done (0);
+           }
        }
 
        }
 
+       /*
+        * We are scanning a maildrop file
+        */
        if (hdrflag) {
            printf ("FOLDER %s\t%s\n", file, dtimenow (1));
        }
 
        scan_detect_mbox_style (in);
        for (msgnum = 1; ; ++msgnum) {
        if (hdrflag) {
            printf ("FOLDER %s\t%s\n", file, dtimenow (1));
        }
 
        scan_detect_mbox_style (in);
        for (msgnum = 1; ; ++msgnum) {
-           charstring_t scanl = NULL;
-
            state = scan (in, msgnum, -1, nfs, width, 0, 0,
                          hdrflag ? file : NULL, 0L, 1, &scanl);
            state = scan (in, msgnum, -1, nfs, width, 0, 0,
                          hdrflag ? file : NULL, 0L, 1, &scanl);
-           charstring_free (scanl);
-           if (state != SCNMSG && state != SCNENC)
+           if (scanl)
+               charstring_clear(scanl);
+           if (state != SCNMSG)
                break;
        }
                break;
        }
+       charstring_free (scanl);
        scan_finished ();
        fclose (in);
        done (0);
        scan_finished ();
        fclose (in);
        done (0);
@@ -235,7 +311,6 @@ main (int argc, char **argv)
         (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
         msgnum += (revflag ? -1 : 1)) {
        if (is_selected(mp, msgnum)) {
         (revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
         msgnum += (revflag ? -1 : 1)) {
        if (is_selected(mp, msgnum)) {
-           charstring_t scanl = NULL;
 
            if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
                    admonish (cp, "unable to open message");
 
            if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
                    admonish (cp, "unable to open message");
@@ -261,19 +336,19 @@ main (int argc, char **argv)
            switch (state = scan (in, msgnum, 0, nfs, width,
                        msgnum == mp->curmsg, unseen,
                        folder, 0L, 1, &scanl)) {
            switch (state = scan (in, msgnum, 0, nfs, width,
                        msgnum == mp->curmsg, unseen,
                        folder, 0L, 1, &scanl)) {
-               case SCNMSG: 
-               case SCNENC: 
-               case SCNERR: 
+               case SCNMSG:
+               case SCNERR:
                    break;
 
                    break;
 
-               default: 
+               default:
                    die("scan() botch (%d)", state);
 
                    die("scan() botch (%d)", state);
 
-               case SCNEOF: 
+               case SCNEOF:
                    inform("message %d: empty", msgnum);
                    break;
            }
                    inform("message %d: empty", msgnum);
                    break;
            }
-           charstring_free (scanl);
+           if (scanl)
+               charstring_clear(scanl);
            scan_finished ();
            hdrflag = false;
            fclose (in);
            scan_finished ();
            hdrflag = false;
            fclose (in);
@@ -281,6 +356,7 @@ main (int argc, char **argv)
                fflush (stdout);
        }
     }
                fflush (stdout);
        }
     }
+    charstring_free (scanl);
 
     ivector_free (seqnum);
     folder_free (mp);  /* free folder/message structure */
 
     ivector_free (seqnum);
     folder_free (mp);  /* free folder/message structure */