]> diplodocus.org Git - nmh/blobdiff - uip/picksbr.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / picksbr.c
index ba25e24785842b7e0f35ed7c400062e83e8409a8..54b50e70cafaadad2ff4e263f2a73210d92aed4f 100644 (file)
@@ -62,7 +62,7 @@ DEFINE_SWITCH_ARRAY(PARSE, parswit);
 
 #define        STAR    01
 
-#define LBSIZE  1024
+#define LBSIZE  NMH_BUFSIZ
 #define        ESIZE   1024
 
 
@@ -116,7 +116,7 @@ static unsigned char cc[] = {
 #define        pinform         if (!talked++) inform
 
 struct nexus {
-    int (*n_action)();
+    int (*n_action)(struct nexus *n, FILE *fp, int msgnum, long start, long stop);
 
     union {
        /* for {OR,AND,NOT}action */
@@ -180,13 +180,19 @@ static struct nexus *parse(void);
 static struct nexus *nexp1(void);
 static struct nexus *nexp2(void);
 static struct nexus *nexp3(void);
-static struct nexus *newnexus(int (*)());
+static struct nexus *newnexus(int (*action)(struct nexus *n,
+    FILE *fp, int msgnum, long start, long stop));
 
-static int ORaction();
-static int ANDaction();
-static int NOTaction();
-static int GREPaction();
-static int TWSaction();
+static int ORaction(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop);
+static int ANDaction(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop);
+static int NOTaction(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop);
+static int GREPaction(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop);
+static int TWSaction(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop);
 
 
 int
@@ -198,7 +204,7 @@ pcompile (char **vec, char *date)
     talked = 0;
 
     if ((head = parse ()) == NULL)
-       return (talked ? 0 : 1);
+        return !talked;
 
     if (*argp) {
        inform("%s unexpected", *argp);
@@ -453,7 +459,8 @@ nexp3 (void)
 
 
 static struct nexus *
-newnexus (int (*action)())
+newnexus(int (*action)(struct nexus *n, FILE *fp, int msgnum,
+    long start, long stop))
 {
     struct nexus *p;
 
@@ -463,15 +470,6 @@ newnexus (int (*action)())
 }
 
 
-#define        args(a) a, fp, msgnum, start, stop
-#define        params  args (n)
-#define        plist   \
-           struct nexus  *n; \
-           FILE *fp; \
-           int msgnum; \
-           long    start, \
-                   stop;
-
 int
 pmatches (FILE *fp, int msgnum, long start, long stop, int debug)
 {
@@ -481,7 +479,7 @@ pmatches (FILE *fp, int msgnum, long start, long stop, int debug)
     if (!talked++ && debug)
        PRaction (head, 0);
 
-    return (*head->n_action) (args (head));
+    return (*head->n_action)(head, fp, msgnum, start, stop);
 }
 
 
@@ -521,36 +519,32 @@ PRaction (struct nexus *n, int level)
                dasctime (&n->n_tws, TW_NULL));
        return;
     }
-    fprintf (stderr, "UNKNOWN(0x%x)\n",
-            (unsigned int)(unsigned long) (*n->n_action));
+    fprintf(stderr, "UNKNOWN(%#llx)\n", (unsigned long long)n->n_action);
 }
 
 
 static int
-ORaction (params)
-plist
+ORaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
 {
-    if ((*n->n_L_child->n_action) (args (n->n_L_child)))
+    if ((*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop))
        return 1;
-    return (*n->n_R_child->n_action) (args (n->n_R_child));
+    return (*n->n_R_child->n_action)(n->n_R_child, fp, msgnum, start, stop);
 }
 
 
 static int
-ANDaction (params)
-plist
+ANDaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
 {
-    if (!(*n->n_L_child->n_action) (args (n->n_L_child)))
+    if (!(*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop))
        return 0;
-    return (*n->n_R_child->n_action) (args (n->n_R_child));
+    return (*n->n_R_child->n_action)(n->n_R_child, fp, msgnum, start, stop);
 }
 
 
 static int
-NOTaction (params)
-plist
+NOTaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
 {
-    return (!(*n->n_L_child->n_action) (args (n->n_L_child)));
+    return (!(*n->n_L_child->n_action)(n->n_L_child, fp, msgnum, start, stop));
 }
 
 
@@ -645,8 +639,7 @@ cerror: ;
 
 
 static int
-GREPaction (params)
-plist
+GREPaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
 {
     int c, body, lf;
     long pos = start;
@@ -681,21 +674,18 @@ plist
                    --p2;
                    break;
                }
-               else
-                   lf = 0;
+                lf = 0;
            }
            if (c == '\n') {
                if (body)
                    break;
-               else {
-                   if (lf) {
-                       body++;
-                       break;
-                   }
-                   lf++;
-                   /* Unfold by skipping the newline. */
-                   c = 0;
-               }
+                if (lf) {
+                    body++;
+                    break;
+                }
+                lf++;
+                /* Unfold by skipping the newline. */
+                c = 0;
            }
            if (c && p1 < &linebuf[LBSIZE - 1])
                *p1++ = c;
@@ -841,7 +831,7 @@ tcompile (char *ap, struct tws *tb, int isafter)
     if ((tw = tws_parse (ap, isafter)) == NULL)
        return 0;
 
-    twscopy (tb, tw);
+    *tb = *tw;
     return 1;
 }
 
@@ -928,27 +918,27 @@ tws_special (char *ap)
 
 
 static int
-TWSaction (params)
-plist
+TWSaction(struct nexus *n, FILE *fp, int msgnum, long start, long stop)
 {
     int state;
     char *bp;
-    char buf[BUFSIZ], name[NAMESZ];
+    char buf[NMH_BUFSIZ], name[NAMESZ];
     struct tws *tw;
-    m_getfld_state_t gstate = 0;
+    m_getfld_state_t gstate;
     NMH_UNUSED (stop);
 
     fseek (fp, start, SEEK_SET);
+    gstate = m_getfld_state_init(fp);
     for (bp = NULL;;) {
        int bufsz = sizeof buf;
-       switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
+       switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
            case FLD: 
            case FLDPLUS: 
-                mh_xfree(bp);
+                free(bp);
                bp = mh_xstrdup(buf);
                while (state == FLDPLUS) {
                    bufsz = sizeof buf;
-                   state = m_getfld (&gstate, name, buf, &bufsz, fp);
+                   state = m_getfld2(&gstate, name, buf, &bufsz);
                    bp = add (buf, bp);
                }
                if (!strcasecmp (name, n->n_datef))
@@ -961,7 +951,7 @@ plist
            case FMTERR: 
                if (state == LENERR || state == FMTERR)
                    inform("format error in message %d", msgnum);
-                mh_xfree(bp);
+                free(bp);
                return 0;
 
            default: 
@@ -978,6 +968,6 @@ plist
        state = n->n_after ? (twsort (tw, &n->n_tws) > 0)
            : (twsort (tw, &n->n_tws) < 0);
 
-    mh_xfree(bp);
+    free(bp);
     return state;
 }