Fixed a couple of signed-unsigned comparisons.
[mmh] / uip / scansbr.c
index 3d66bcf..d186845 100644 (file)
 #define SBUFSIZ 512
 
 static struct format *fmt;
-#ifdef JLR
-static struct format *fmt_top;
-#endif /* JLR */
-
 static struct comp *datecomp;          /* pntr to "date" comp             */
 static struct comp *bodycomp;          /* pntr to "body" pseudo-comp      *
                                         * (if referenced)                 */
@@ -78,6 +74,7 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
     FILE *scnout = NULL;
     char name[NAMESZ];
     static int rlwidth, slwidth;
+    static size_t scanl_size;
 
     /* first-time only initialization */
     if (!scanl) {
@@ -88,32 +85,60 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
                width = MAXSCANL;
        }
        dat[3] = slwidth = width;
-       scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH * (slwidth + 2) );
+       /* Arbitrarily allocate 20 * slwidth to provide room for lots
+          of escape sequences. */
+       scanl_size = SCAN_CHARWIDTH * (20 * slwidth + 2);
+       scanl = (char *) mh_xmalloc (scanl_size);
        if (outnum)
            umask(~m_gmprot());
 
        /* Compile format string */
-       ncomps = fmt_compile (nfs, &fmt) + 1;
-
-#ifdef JLR
-       fmt_top = fmt;
-#endif /* JLR */
-       FINDCOMP(bodycomp, "body");
-       FINDCOMP(datecomp, "date");
-       FINDCOMP(cptr, "folder");
+       ncomps = fmt_compile (nfs, &fmt, 1) + 1;
+
+       bodycomp = fmt_findcomp("body");
+       datecomp = fmt_findcomp("date");
+       cptr = fmt_findcomp("folder");
        if (cptr && folder)
-           cptr->c_text = folder;
-       FINDCOMP(cptr, "encrypted");
-       if (!cptr)
-           if ((cptr = (struct comp *) calloc (1, sizeof(*cptr)))) {
-               cptr->c_name = "encrypted";
-               cptr->c_next = wantcomp[i = CHASH (cptr->c_name)];
-               wantcomp[i] = cptr;
+           cptr->c_text = getcpy(folder);
+       if (fmt_addcompentry("encrypted")) {
                ncomps++;
        }
-       FINDCOMP (cptr, "dtimenow");
+       cptr =  fmt_findcomp("dtimenow");
        if (cptr)
            cptr->c_text = getcpy(dtimenow (0));
+
+       /*
+        * In other programs I got rid of this complicated buffer switching,
+        * but since scan reads lots of messages at once and this complicated
+        * memory management, I decided to keep it; otherwise there was
+        * the potential for a lot of malloc() and free()s, and I could
+        * see the malloc() pool really getting fragmented.  Maybe it
+        * wouldn't be an issue in practice; perhaps this will get
+        * revisited someday.
+        *
+        * So, some notes for what's going on:
+        *
+        * nxtbuf is an array of pointers that contains malloc()'d buffers
+        * to hold our component text.  used_buf is an array of struct comp
+        * pointers that holds pointers to component structures we found while
+        * processing a message.
+        *
+        * We read in the message with m_getfld(), using "tmpbuf" as our
+        * input buffer.  tmpbuf is set at the start of message processing
+        * to the first buffer in our buffer pool (nxtbuf).
+        *
+        * Every time we find a component we care about, we set that component's
+        * text buffer to the current value of tmpbuf, and then switch tmpbuf
+        * to the next buffer in our pool.  We also add that component to
+        * our used_buf pool.
+        *
+        * When we're done, we go back and zero out all of the component
+        * text buffer pointers that we saved in used_buf.
+        *
+        * Note that this means c_text memory is NOT owned by the fmt_module
+        * and it's our responsibility to free it.
+        */
+
        nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
        if (nxtbuf == NULL)
            adios (NULL, "unable to allocate component buffers");
@@ -179,23 +204,18 @@ scan (FILE *inb, int innum, int outnum, char *nfs, int width, int curflg,
                 * buffer as the component temp buffer (buffer switching
                 * saves an extra copy of the component text).
                 */
-               if ((cptr = wantcomp[CHASH(name)])) {
-                   do {
-                       if (!mh_strcasecmp(name, cptr->c_name)) {
-                           if (! cptr->c_text) {
-                               cptr->c_text = tmpbuf;
-                               for (cp = tmpbuf + strlen (tmpbuf) - 1; 
+               if ((cptr = fmt_findcasecomp(name))) {
+                   if (! cptr->c_text) {
+                       cptr->c_text = tmpbuf;
+                       for (cp = tmpbuf + strlen (tmpbuf) - 1; 
                                        cp >= tmpbuf; cp--)
-                                   if (isspace (*cp))
-                                       *cp = 0;
-                                   else
-                                       break;
-                               *--savecomp = cptr;
-                               tmpbuf = *nxtbuf++;
-                           }
-                           break;
-                       }
-                   } while ((cptr = cptr->c_next));
+                           if (isspace (*cp))
+                               *cp = 0;
+                           else
+                               break;
+                       *--savecomp = cptr;
+                       tmpbuf = *nxtbuf++;
+                   }
                }
 
                while (state == FLDPLUS) {
@@ -330,13 +350,7 @@ finished:
        }
     }
 
-    fmt_scan (fmt, scanl, slwidth, dat);
-
-#if 0
-    fmt = fmt_scan (fmt, scanl, slwidth, dat);
-    if (!fmt)
-       fmt = fmt_top;          /* reset for old format files */
-#endif
+    fmt_scan (fmt, scanl, scanl_size, slwidth, dat);
 
     if (bodycomp)
        bodycomp->c_text = saved_c_text;
@@ -344,7 +358,7 @@ finished:
     if (noisy)
        fputs (scanl, stdout);
 
-    FINDCOMP (cptr, "encrypted");
+    cptr = fmt_findcomp ("encrypted");
     encrypted = cptr && cptr->c_text;
 
     /* return dynamically allocated buffers to pool */