X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/06efa573c7894caa4e5aa1ac7c64bd10ea25928b..361f9cbca968760822f53a80b407f24ce1713ccd:/uip/scan.c diff --git a/uip/scan.c b/uip/scan.c index 60c5ebcc..c1f27194 100644 --- a/uip/scan.c +++ b/uip/scan.c @@ -5,15 +5,37 @@ * complete copyright information. */ -#include -#include -#include -#include -#include +#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 +#include "h/utils.h" #include "sbr/m_maildir.h" #include "sbr/terminal.h" +#include "sbr/maildir_read_and_sort.h" #define SCAN_SWITCHES \ X("clear", 0, CLRSW) \ @@ -43,7 +65,7 @@ main (int argc, char **argv) { bool clearflag = false; bool hdrflag = false; - int ontty; + int ontty = 0; 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; + charstring_t scanl = NULL; 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)) { - case AMBIGSW: + case AMBIGSW: ambigsw (cp, switches); done (1); - case UNKWNSW: + case UNKWNSW: die("-%s unknown", cp); - case HELPSW: + case HELPSW: 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); - case CLRSW: + case CLRSW: clearflag = true; continue; - case NCLRSW: + case NCLRSW: clearflag = false; continue; - case FORMSW: + case FORMSW: 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; - case HEADSW: + case HEADSW: hdrflag = true; continue; - case NHEADSW: + case NHEADSW: hdrflag = false; continue; - case WIDTHSW: + case WIDTHSW: 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); - /* - * We are scanning a maildrop file - */ if (file) { + /* + * We have a -file argument + */ 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 { - 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) { - charstring_t scanl = NULL; - 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; } + charstring_free (scanl); 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)) { - charstring_t scanl = NULL; 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)) { - case SCNMSG: - case SCNENC: - case SCNERR: + case SCNMSG: + case SCNERR: break; - default: + default: die("scan() botch (%d)", state); - case SCNEOF: + case SCNEOF: inform("message %d: empty", msgnum); break; } - charstring_free (scanl); + if (scanl) + charstring_clear(scanl); scan_finished (); hdrflag = false; fclose (in); @@ -281,6 +356,7 @@ main (int argc, char **argv) fflush (stdout); } } + charstring_free (scanl); ivector_free (seqnum); folder_free (mp); /* free folder/message structure */