X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=uip%2Fpick.c;h=e0d61368af4c90ebd0053a7e1edffb0bffadca16;hp=d2f72efb768874137525e17cbd170bfabeb71bb9;hb=d8916ff5d389de5ab225cd6f40aeda1b285d0f28;hpb=1691e80890e5d8ba258c51c214a3e91880e1db2b diff --git a/uip/pick.c b/uip/pick.c index d2f72ef..e0d6136 100644 --- a/uip/pick.c +++ b/uip/pick.c @@ -3,18 +3,16 @@ * pick.c -- search for messages by content * * $Id$ + * + * This code is Copyright (c) 2002, 2008, by the authors of nmh. See the + * COPYRIGHT file in the root directory of the nmh distribution for + * complete copyright information. */ #include -#include +#include #include - -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - +#include static struct swit switches[] = { #define ANDSW 0 @@ -64,24 +62,28 @@ static struct swit switches[] = { #define VERSIONSW 22 { "version", 0 }, #define HELPSW 23 - { "help", 4 }, + { "help", 0 }, { NULL, 0 } }; -static int listsw = 0; +static int listsw = -1; +static void putzero_done (int) NORETURN; int main (int argc, char **argv) { int publicsw = -1, zerosw = 1, seqp = 0, vecp = 0; - int nummsgs, maxmsgs, lo, hi, msgnum; + int lo, hi, msgnum; char *maildir, *folder = NULL, buf[100]; char *cp, **argp, **arguments; - char **msgs, *seqs[NUMATTRS + 1], *vec[MAXARGS]; + char *seqs[NUMATTRS + 1], *vec[MAXARGS]; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; register FILE *fp; + done=putzero_done; + #ifdef LOCALE setlocale(LC_ALL, ""); #endif @@ -93,15 +95,6 @@ main (int argc, char **argv) arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; - /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - if (!(msgs = (char **) malloc ((size_t) (maxmsgs * sizeof(*msgs))))) - adios (NULL, "unable to allocate storage"); - while ((cp = *argp++)) { if (*cp == '-') { if (*++cp == '-') { @@ -111,6 +104,7 @@ main (int argc, char **argv) switch (smatch (cp, switches)) { case AMBIGSW: ambigsw (cp, switches); + listsw = 0; /* HACK */ done (1); case UNKWNSW: adios (NULL, "-%s unknown", cp); @@ -123,6 +117,7 @@ main (int argc, char **argv) done (1); case VERSIONSW: print_version(invo_name); + listsw = 0; /* HACK */ done (1); case CCSW: @@ -158,8 +153,11 @@ main (int argc, char **argv) /* check if too many sequences specified */ if (seqp >= NUMATTRS) adios (NULL, "too many sequences (more than %d) specified", NUMATTRS); + + if (!seq_nameok (cp)) + done (1); + seqs[seqp++] = cp; - listsw = 0; continue; case PUBLSW: publicsw = 1; @@ -175,7 +173,7 @@ main (int argc, char **argv) continue; case LISTSW: - listsw++; + listsw = 1; continue; case NLISTSW: listsw = 0; @@ -186,20 +184,9 @@ main (int argc, char **argv) if (folder) adios (NULL, "only one folder at a time!"); else - folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); - } else { - /* - * Check if we need to allocate more space - * for message name/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - if (!(msgs = (char **) realloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))))) - adios (NULL, "unable to reallocate msgs storage"); - } - msgs[nummsgs++] = cp; - } + folder = pluspath (cp); + } else + app_msgarg(&msgs, cp); } vec[vecp] = NULL; @@ -210,8 +197,8 @@ main (int argc, char **argv) * If we didn't specify which messages to search, * then search the whole folder. */ - if (!nummsgs) - msgs[nummsgs++] = "all"; + if (!msgs.size) + app_msgarg(&msgs, "all"); if (!folder) folder = getfolder (1); @@ -229,17 +216,17 @@ main (int argc, char **argv) adios (NULL, "no messages in %s", folder); /* parse all the message ranges/sequences and set SELECTED */ - for (msgnum = 0; msgnum < nummsgs; msgnum++) - if (!m_convert (mp, msgs[msgnum])) + for (msgnum = 0; msgnum < msgs.size; msgnum++) + if (!m_convert (mp, msgs.msgs[msgnum])) done (1); seq_setprev (mp); /* set the previous-sequence */ /* * If we aren't saving the results to a sequence, - * we need to list the results. + * we default to list the results. */ - if (seqp == 0) - listsw++; + if (listsw == -1) + listsw = !seqp; if (publicsw == 1 && is_readonly(mp)) adios (NULL, "folder %s is read-only, so -public not allowed", folder); @@ -250,6 +237,11 @@ main (int argc, char **argv) lo = mp->lowsel; hi = mp->hghsel; + /* If printing message numbers to standard out, force line buffering on. + */ + if (listsw) + setvbuf (stdout, NULL, _IOLBF, 0); + /* * Scan through all the SELECTED messages and check for a * match. If the message does not match, then unselect it. @@ -263,6 +255,9 @@ main (int argc, char **argv) lo = msgnum; if (msgnum > hi) hi = msgnum; + + if (listsw) + printf ("%s\n", m_name (msgnum)); } else { /* if it doesn't match, then unselect it */ unset_selected (mp, msgnum); @@ -289,13 +284,9 @@ main (int argc, char **argv) done (1); /* - * Print the name of all the matches + * Print total matched if not printing each matched message number. */ - if (listsw) { - for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) - if (is_selected (mp, msgnum)) - printf ("%s\n", m_name (msgnum)); - } else { + if (!listsw) { printf ("%d hit%s\n", mp->numsel, mp->numsel == 1 ? "" : "s"); } @@ -304,11 +295,12 @@ main (int argc, char **argv) context_save (); /* save the context file */ folder_free (mp); /* free folder/message structure */ done (0); + return 1; } -void -done (int status) +static void +putzero_done (int status) { if (listsw && status && !isatty (fileno (stdout))) printf ("0\n");