From: Joel Reicher Date: Tue, 11 Apr 2006 14:09:11 +0000 (+0000) Subject: Created app_msgarg() (append message arg) and a simple resizable array X-Git-Tag: nmh-1_3_RC1~40 X-Git-Url: http://git.marmaro.de/?p=mmh;a=commitdiff_plain;h=174d375bb4b9bcaa0c3f28c422216f68703494a8 Created app_msgarg() (append message arg) and a simple resizable array struct to go with it that do almost exactly what the code they've replaced was doing. Replaced this (duplicated) code in many files with a call to app_msgarg(). --- diff --git a/h/utils.h b/h/utils.h index 8246a5d..3eda9d3 100644 --- a/h/utils.h +++ b/h/utils.h @@ -11,3 +11,10 @@ char *pwd(void); char *add(char *, char *); void create_folder(char *, int, void (*)()); int num_digits(int); + +struct msgs_array { + int max, size; + char **msgs; +}; + +void app_msgarg(struct msgs_array *, char *); diff --git a/sbr/utils.c b/sbr/utils.c index c433e8f..8aba5b6 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -15,6 +15,12 @@ #include /* + * We allocate space for messages (msgs array) + * this number of elements at a time. + */ +#define MAXMSGS 256 + +/* * Safely call malloc */ void * @@ -165,3 +171,16 @@ num_digits (int n) return ndigits; } + +/* + * Append a message arg to an array of them, resizing it if necessary. + * The function is written to suit the arg parsing code it was extracted + * from, and will probably be changed when the other code is cleaned up. + */ +void +app_msgarg(struct msgs_array *msgs, char *cp) +{ + if(msgs->size >= msgs->max) + msgs->msgs = mh_xrealloc(msgs->msgs, (msgs->max+=MAXMSGS)*sizeof(*msgs->msgs)); + msgs->msgs[msgs->size++] = cp; +} diff --git a/uip/anno.c b/uip/anno.c index 9fbb37f..0d44134 100644 --- a/uip/anno.c +++ b/uip/anno.c @@ -48,13 +48,6 @@ #include #include -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define COMPSW 0 { "component field", 0 }, @@ -99,10 +92,11 @@ int main (int argc, char **argv) { int inplace = 1, datesw = 1; - int nummsgs, maxmsgs, msgnum; + int msgnum; char *cp, *maildir, *comp = NULL; char *text = NULL, *folder = NULL, buf[BUFSIZ]; - char **argp, **arguments, **msgs; + char **argp, **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; int append = 0; /* append annotations instead of default prepend */ int delete = -2; /* delete header element if set */ @@ -122,14 +116,6 @@ main (int argc, char **argv) arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; - /* - * Allocate the initial space to record message - * names and ranges. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - while ((cp = *argp++)) { if (*cp == '-') { switch (smatch (++cp, switches)) { @@ -226,18 +212,8 @@ main (int argc, char **argv) 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. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* @@ -247,7 +223,7 @@ main (int argc, char **argv) */ if (draft != (char *)0) { - if (nummsgs != 0) + if (msgs.size != 0) adios(NULL, "can only have message numbers or -draft."); draft = getcpy(m_draft(folder, (char *)0, 1, &isdf)); @@ -269,8 +245,8 @@ main (int argc, char **argv) if (!context_find ("path")) free (path ("./", TFOLDER)); - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -287,8 +263,8 @@ 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); make_comp (&comp); diff --git a/uip/mark.c b/uip/mark.c index 0333722..e26ca6a 100644 --- a/uip/mark.c +++ b/uip/mark.c @@ -14,13 +14,6 @@ #include #include -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define ADDSW 0 { "add", 0 }, @@ -59,10 +52,11 @@ main (int argc, char **argv) { int addsw = 0, deletesw = 0, debugsw = 0; int listsw = 0, publicsw = -1, zerosw = 0; - int seqp = 0, msgnum, nummsgs, maxmsgs; + int seqp = 0, msgnum; char *cp, *maildir, *folder = NULL, buf[BUFSIZ]; char **argp, **arguments; - char *seqs[NUMATTRS + 1], **msgs; + char *seqs[NUMATTRS + 1]; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; #ifdef LOCALE @@ -77,14 +71,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -152,18 +138,8 @@ main (int argc, char **argv) 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* @@ -180,8 +156,8 @@ main (int argc, char **argv) if (!context_find ("path")) free (path ("./", TFOLDER)); - if (!nummsgs) - msgs[nummsgs++] = listsw ? "all" :"cur"; + if (!msgs.size) + app_msgarg(&msgs, listsw ? "all" :"cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -202,8 +178,8 @@ 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); if (publicsw == 1 && is_readonly(mp)) diff --git a/uip/mhlist.c b/uip/mhlist.c index 03480b3..b130cf1 100644 --- a/uip/mhlist.c +++ b/uip/mhlist.c @@ -26,13 +26,6 @@ # include #endif -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define CHECKSW 0 { "check", 0 }, @@ -130,10 +123,11 @@ int main (int argc, char **argv) { int sizesw = 1, headsw = 1; - int nummsgs, maxmsgs, msgnum, *icachesw; + int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp = NULL; CT ct, *ctp; @@ -149,14 +143,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -257,18 +243,8 @@ do_cache: 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* null terminate the list of acceptable parts/types */ @@ -299,7 +275,7 @@ do_cache: if (!context_find ("path")) free (path ("./", TFOLDER)); - if (file && nummsgs) + if (file && msgs.size) adios (NULL, "cannot specify msg and file at same time!"); /* @@ -316,8 +292,8 @@ do_cache: /* * message(s) are coming from a folder */ - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -334,8 +310,8 @@ do_cache: 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 */ diff --git a/uip/mhn.c b/uip/mhn.c index 685a74e..cfeac94 100644 --- a/uip/mhn.c +++ b/uip/mhn.c @@ -26,13 +26,6 @@ # include #endif -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define AUTOSW 0 { "auto", 0 }, @@ -218,10 +211,11 @@ int main (int argc, char **argv) { int sizesw = 1, headsw = 1; - int nummsgs, maxmsgs, msgnum, *icachesw; + int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp = NULL; CT ct, *ctp; FILE *fp; @@ -238,14 +232,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -442,18 +428,8 @@ do_cache: 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* null terminate the list of acceptable parts/types */ @@ -524,9 +500,9 @@ do_cache: if (showsw || storesw || cachesw) adios (NULL, "cannot use -build with -show, -store, -cache"); - if (nummsgs < 1) + if (msgs.size < 1) adios (NULL, "need to specify a %s composition file", invo_name); - if (nummsgs > 1) + if (msgs.size > 1) adios (NULL, "only one %s composition file at a time", invo_name); vecp = 0; @@ -542,7 +518,7 @@ do_cache: else if (rfc934sw == -1) vec[vecp++] = "-norfc934mode"; - vec[vecp++] = msgs[0]; + vec[vecp++] = msgs.msgs[0]; vec[vecp] = NULL; execvp ("mhbuild", vec); @@ -553,10 +529,10 @@ do_cache: /* * Process a mhn composition file (old MH style) */ - if (nummsgs == 1 && !folder && !npart && !cachesw + if (msgs.size == 1 && !folder && !npart && !cachesw && !showsw && !storesw && !ntype && !file && (cp = getenv ("mhdraft")) - && strcmp (cp, msgs[0]) == 0) { + && strcmp (cp, msgs.msgs[0]) == 0) { char *vec[MAXARGS]; int vecp; @@ -582,7 +558,7 @@ do_cache: _exit (-1); } - if (file && nummsgs) + if (file && msgs.size) adios (NULL, "cannot specify msg and file at same time!"); /* @@ -599,8 +575,8 @@ do_cache: /* * message(s) are coming from a folder */ - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -617,8 +593,8 @@ do_cache: 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 */ diff --git a/uip/mhpath.c b/uip/mhpath.c index 4a88605..00983d5 100644 --- a/uip/mhpath.c +++ b/uip/mhpath.c @@ -20,20 +20,14 @@ static struct swit switches[] = { { NULL, 0 } }; -/* - * Add space for message/sequence names - * by MAXMSGS at a time. - */ -#define MAXMSGS 256 - - int main(int argc, char **argv) { - int i, maxmsgs, nummsgs; + int i; char *cp, *maildir, *folder = NULL; - char **argp, **msgs; + char **argp; char **arguments, buf[BUFSIZ]; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; #ifdef LOCALE @@ -48,14 +42,6 @@ main(int argc, char **argv) argp = arguments; /* - * Allocate initial space to record message - * and sequence names - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -82,18 +68,8 @@ main(int argc, char **argv) adios (NULL, "only one folder at a time!"); else folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); - } else { - /* - * if necessary, reallocate space for - * message/sequence names - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!context_find ("path")) @@ -104,7 +80,7 @@ main(int argc, char **argv) maildir = m_maildir (folder); /* If no messages are given, print folder pathname */ - if (!nummsgs) { + if (!msgs.size) { printf ("%s\n", maildir); done (0); } @@ -133,8 +109,8 @@ main(int argc, char **argv) mp->msgflags |= ALLOW_NEW; /* allow the "new" sequence */ /* parse all the message ranges/sequences and set SELECTED */ - for (i = 0; i < nummsgs; i++) - if (!m_convert (mp, msgs[i])) + for (i = 0; i < msgs.size; i++) + if (!m_convert (mp, msgs.msgs[i])) done (1); seq_setprev (mp); /* set the previous-sequence */ diff --git a/uip/mhshow.c b/uip/mhshow.c index 909c440..df4cf51 100644 --- a/uip/mhshow.c +++ b/uip/mhshow.c @@ -26,13 +26,6 @@ # include #endif -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define CHECKSW 0 { "check", 0 }, @@ -145,10 +138,11 @@ static RETSIGTYPE pipeser (int); int main (int argc, char **argv) { - int nummsgs, maxmsgs, msgnum, *icachesw; + int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp = NULL; CT ct, *ctp; FILE *fp; @@ -165,14 +159,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -298,18 +284,8 @@ do_cache: 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* null terminate the list of acceptable parts/types */ @@ -365,7 +341,7 @@ do_cache: if (!context_find ("path")) free (path ("./", TFOLDER)); - if (file && nummsgs) + if (file && msgs.size) adios (NULL, "cannot specify msg and file at same time!"); /* @@ -382,8 +358,8 @@ do_cache: /* * message(s) are coming from a folder */ - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -400,8 +376,8 @@ do_cache: 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); /* diff --git a/uip/mhstore.c b/uip/mhstore.c index b756956..08891f5 100644 --- a/uip/mhstore.c +++ b/uip/mhstore.c @@ -26,13 +26,6 @@ # include #endif -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define AUTOSW 0 { "auto", 0 }, @@ -123,10 +116,11 @@ static RETSIGTYPE pipeser (int); int main (int argc, char **argv) { - int nummsgs, maxmsgs, msgnum, *icachesw; + int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp = NULL; CT ct, *ctp; FILE *fp; @@ -143,14 +137,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -244,18 +230,8 @@ do_cache: 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* null terminate the list of acceptable parts/types */ @@ -311,7 +287,7 @@ do_cache: if (!context_find ("path")) free (path ("./", TFOLDER)); - if (file && nummsgs) + if (file && msgs.size) adios (NULL, "cannot specify msg and file at same time!"); /* @@ -328,8 +304,8 @@ do_cache: /* * message(s) are coming from a folder */ - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -346,8 +322,8 @@ do_cache: 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 */ diff --git a/uip/mhtest.c b/uip/mhtest.c index 298ef03..140c7d9 100644 --- a/uip/mhtest.c +++ b/uip/mhtest.c @@ -26,13 +26,6 @@ # include #endif -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define CHECKSW 0 { "check", 0 }, @@ -126,10 +119,11 @@ static RETSIGTYPE pipeser (int); int main (int argc, char **argv) { - int nummsgs, maxmsgs, msgnum, *icachesw; + int msgnum, *icachesw; char *cp, *file = NULL, *folder = NULL; char *maildir, buf[100], *outfile = NULL; - char **argp, **arguments, **msgs; + char **argp, **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp = NULL; CT ct, *ctp; @@ -145,14 +139,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -245,18 +231,8 @@ do_cache: 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } /* null terminate the list of acceptable parts/types */ @@ -290,7 +266,7 @@ do_cache: if (!context_find ("path")) free (path ("./", TFOLDER)); - if (file && nummsgs) + if (file && msgs.size) adios (NULL, "cannot specify msg and file at same time!"); /* @@ -307,8 +283,8 @@ do_cache: /* * message(s) are coming from a folder */ - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -325,8 +301,8 @@ do_cache: 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 */ diff --git a/uip/packf.c b/uip/packf.c index 6f3638c..e074505 100644 --- a/uip/packf.c +++ b/uip/packf.c @@ -15,13 +15,6 @@ #include #include -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define FILESW 0 { "file name", 0 }, @@ -46,9 +39,10 @@ char *file = NULL; int main (int argc, char **argv) { - int nummsgs, maxmsgs, fd, msgnum; + int fd, msgnum; char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ]; - char **argp, **arguments, **msgs; + char **argp, **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; struct stat st; @@ -63,13 +57,6 @@ main (int argc, char **argv) arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; - /* Allocate the initial space to record message - * names and ranges. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - /* * Parse arguments */ @@ -112,18 +99,8 @@ main (int argc, char **argv) if (folder) adios (NULL, "only one folder at a time!"); folder = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); - } else { - /* - * Check if we need to allocate more space - * for message name/ranges. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!file) @@ -147,8 +124,8 @@ main (int argc, char **argv) free (path ("./", TFOLDER)); /* default is to pack whole folder */ - if (!nummsgs) - msgs[nummsgs++] = "all"; + if (!msgs.size) + app_msgarg(&msgs, "all"); if (!folder) folder = getfolder (1); @@ -166,8 +143,8 @@ 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 */ diff --git a/uip/pick.c b/uip/pick.c index 8cdfa58..8fd294e 100644 --- a/uip/pick.c +++ b/uip/pick.c @@ -14,13 +14,6 @@ #include #include -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define ANDSW 0 { "and", 0 }, @@ -80,10 +73,11 @@ 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; @@ -98,14 +92,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; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - while ((cp = *argp++)) { if (*cp == '-') { if (*++cp == '-') { @@ -192,18 +178,8 @@ main (int argc, char **argv) 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; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } vec[vecp] = NULL; @@ -214,8 +190,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); @@ -233,8 +209,8 @@ 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 */ diff --git a/uip/refile.c b/uip/refile.c index bb7f00a..acafbf4 100644 --- a/uip/refile.c +++ b/uip/refile.c @@ -15,13 +15,6 @@ #include #include -/* - * We allocate spaces for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define DRAFTSW 0 { "draft", 0 }, @@ -73,12 +66,13 @@ main (int argc, char **argv) { int linkf = 0, preserve = 0, filep = 0; int foldp = 0, isdf = 0, unlink_msgs = 0; - int i, msgnum, nummsgs, maxmsgs; + int i, msgnum; char *cp, *folder = NULL, buf[BUFSIZ]; - char **argp, **arguments, **msgs; + char **argp, **arguments; char *filevec[NFOLDERS + 2]; char **files = &filevec[1]; /* leave room for remove_files:vec[0] */ struct st_fold folders[NFOLDERS + 1]; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; #ifdef LOCALE @@ -93,14 +87,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -178,18 +164,8 @@ main (int argc, char **argv) adios (NULL, "only %d folders allowed!", NFOLDERS); folders[foldp++].f_name = path (cp + 1, *cp == '+' ? TFOLDER : TSUBCWF); - } else { - /* - * Check if we need to allocate more space - * for message names, ranges, and sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!context_find ("path")) @@ -198,7 +174,7 @@ main (int argc, char **argv) adios (NULL, "no folder specified"); #ifdef WHATNOW - if (!nummsgs && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp) + if (!msgs.size && !foldp && !filep && (cp = getenv ("mhdraft")) && *cp) files[filep++] = cp; #endif /* WHATNOW */ @@ -206,7 +182,7 @@ main (int argc, char **argv) * We are refiling a file to the folders */ if (filep > 0) { - if (folder || nummsgs) + if (folder || msgs.size) adios (NULL, "use -file or some messages, not both"); opnfolds (folders, foldp); for (i = 0; i < filep; i++) @@ -218,8 +194,8 @@ main (int argc, char **argv) done (0); } - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); strncpy (maildir, m_maildir (folder), sizeof(maildir)); @@ -236,8 +212,8 @@ main (int argc, char **argv) adios (NULL, "no messages in %s", folder); /* parse the message range/sequence/name 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 */ diff --git a/uip/rmm.c b/uip/rmm.c index b9fd80d..75b1c70 100644 --- a/uip/rmm.c +++ b/uip/rmm.c @@ -12,12 +12,6 @@ #include #include -/* - * We allocate space for message names and ranges - * (msgs array) this number of elements at a time. - */ -#define MAXMSGS 256 - static struct swit switches[] = { #define UNLINKSW 0 { "unlink", 0 }, @@ -34,10 +28,11 @@ static struct swit switches[] = { int main (int argc, char **argv) { - int nummsgs, maxmsgs, msgnum, unlink_msgs = 0; + int msgnum, unlink_msgs = 0; char *cp, *maildir, *folder = NULL; char buf[BUFSIZ], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; #ifdef LOCALE @@ -51,14 +46,6 @@ main (int argc, char **argv) arguments = getarguments (invo_name, argc, argv, 1); argp = arguments; - /* - * Allocate the initial space to record message - * names and ranges. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - /* parse arguments */ while ((cp = *argp++)) { if (*cp == '-') { @@ -91,24 +78,14 @@ main (int argc, char **argv) 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 names/ranges. - */ - if (nummsgs >= maxmsgs){ - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!context_find ("path")) free (path ("./", TFOLDER)); - if (!nummsgs) - msgs[nummsgs++] = "cur"; + if (!msgs.size) + app_msgarg(&msgs, "cur"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -125,8 +102,8 @@ 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 */ diff --git a/uip/scan.c b/uip/scan.c index 705ff69..4334efe 100644 --- a/uip/scan.c +++ b/uip/scan.c @@ -17,13 +17,6 @@ #include #include -/* - * We allocate space for message names (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define CLRSW 0 { "clear", 0 }, @@ -71,11 +64,12 @@ main (int argc, char **argv) { int clearflag = 0, hdrflag = 0, ontty; int width = 0, revflag = 0; - int i, state, msgnum, nummsgs, maxmsgs; + int i, state, msgnum; int seqnum[NUMATTRS], unseen, num_unseen_seq = 0; char *cp, *maildir, *file = NULL, *folder = NULL; char *form = NULL, *format = NULL, buf[BUFSIZ]; - char **argp, *nfs, **arguments, **msgs; + char **argp, *nfs, **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; FILE *in; @@ -92,14 +86,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names, ranges, and sequences. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -170,18 +156,8 @@ main (int argc, char **argv) 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 names/ranges/sequences. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!context_find ("path")) @@ -196,7 +172,7 @@ main (int argc, char **argv) * We are scanning a maildrop file */ if (file) { - if (nummsgs) + if (msgs.size) adios (NULL, "\"msgs\" not allowed with -file"); if (folder) adios (NULL, "\"+folder\" not allowed with -file"); @@ -231,8 +207,8 @@ main (int argc, char **argv) * We are scanning a folder */ - if (!nummsgs) - msgs[nummsgs++] = "all"; + if (!msgs.size) + app_msgarg(&msgs, "all"); if (!folder) folder = getfolder (1); maildir = m_maildir (folder); @@ -249,8 +225,8 @@ 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 */ diff --git a/uip/sortm.c b/uip/sortm.c index 9263c79..5faec79 100644 --- a/uip/sortm.c +++ b/uip/sortm.c @@ -13,13 +13,6 @@ #include #include -/* - * We allocate space for messages (msgs array) - * this number of elements at a time. - */ -#define MAXMSGS 256 - - static struct swit switches[] = { #define DATESW 0 { "datefield field", 0 }, @@ -76,10 +69,11 @@ static void rename_msgs (struct msgs *, struct smsg **); int main (int argc, char **argv) { - int nummsgs, maxmsgs, i, msgnum; + int i, msgnum; char *cp, *maildir, *datesw = NULL; char *folder = NULL, buf[BUFSIZ], **argp; - char **arguments, **msgs; + char **arguments; + struct msgs_array msgs = { 0, 0, NULL }; struct msgs *mp; struct smsg **dlist; @@ -95,14 +89,6 @@ main (int argc, char **argv) argp = arguments; /* - * Allocate the initial space to record message - * names and ranges. - */ - nummsgs = 0; - maxmsgs = MAXMSGS; - msgs = (char **) mh_xmalloc ((size_t) (maxmsgs * sizeof(*msgs))); - - /* * Parse arguments */ while ((cp = *argp++)) { @@ -175,24 +161,14 @@ main (int argc, char **argv) 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 names/ranges. - */ - if (nummsgs >= maxmsgs) { - maxmsgs += MAXMSGS; - msgs = (char **) mh_xrealloc (msgs, - (size_t) (maxmsgs * sizeof(*msgs))); - } - msgs[nummsgs++] = cp; - } + } else + app_msgarg(&msgs, cp); } if (!context_find ("path")) free (path ("./", TFOLDER)); - if (!nummsgs) - msgs[nummsgs++] = "all"; + if (!msgs.size) + app_msgarg(&msgs, "all"); if (!datesw) datesw = "date"; if (!folder) @@ -211,8 +187,8 @@ 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 */