Fixed `inc -silent' by not building a scan line if we don't want to print it.
authormarkus schnalke <meillo@marmaro.de>
Sat, 24 Mar 2012 14:41:46 +0000 (15:41 +0100)
committermarkus schnalke <meillo@marmaro.de>
Sat, 24 Mar 2012 14:41:46 +0000 (15:41 +0100)
scan() is still ugly and I'm still not pleased with the scan/inc merge,
but I don't see a better solution yet.

uip/scansbr.c

index eba09f0..56583f1 100644 (file)
@@ -79,25 +79,31 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
        int incing = (outnum != SCN_MBOX && outnum != SCN_FOLD);
        int scanfolder = (outnum == SCN_FOLD);
        long fpos;
+       struct stat st;
 
        /* first-time only initialization */
        if (!scanl) {
-               if (width == 0) {
-                       if ((width = sc_width()) < WIDTH/2)
-                               width = WIDTH/2;
-                       else if (width > MAXSCANL)
-                               width = MAXSCANL;
-               }
-               dat[3] = slwidth = width;
-               scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
-                               (slwidth + 2));  /* probably for \n and \0 */
                if (incing)
                        umask(~m_gmprot());
 
-               /* Compile format string */
-               ncomps = fmt_compile(fmtstr, &fmt) + 1;
+               if (fmtstr) {
+                       if (width == 0) {
+                               if ((width = sc_width()) < WIDTH/2)
+                                       width = WIDTH/2;
+                               else if (width > MAXSCANL)
+                                       width = MAXSCANL;
+                       }
+                       dat[3] = slwidth = width;
+                       scanl = (char *) mh_xmalloc((size_t) SCAN_CHARWIDTH *
+                                       (slwidth + 2));  /* probably for \n and \0 */
+                       /* Compile format string */
+                       ncomps = fmt_compile(fmtstr, &fmt) + 1;
+                       FINDCOMP(datecomp, "date");
+               } else {
+                       ncomps = 1;
+                       datecomp = NULL;
+               }
 
-               FINDCOMP(datecomp, "date");
                nxtbuf = compbuffers = (char **) calloc((size_t) ncomps,
                                sizeof(char *));
                if (!nxtbuf)
@@ -176,21 +182,24 @@ scan(FILE *inb, int innum, int outnum, char *fmtstr, int width, int curflg,
                        ** temp buffer (buffer switching saves an extra
                        ** copy of the component text).
                        */
-                       if ((cptr = wantcomp[CHASH(name)])) {
+                       if (fmtstr && (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; cp >= tmpbuf; cp--)
-                                                               if (isspace(*cp))
-                                                                       *cp = 0;
-                                                               else
-                                                                       break;
-                                                       *--savecomp = cptr;
-                                                       tmpbuf = *nxtbuf++;
+                                       if (mh_strcasecmp(name, cptr->c_name)!=0) {
+                                               continue;
+                                       }
+                                       if (!cptr->c_text) {
+                                               cptr->c_text = tmpbuf;
+                                               cp = tmpbuf+strlen(tmpbuf)-1;
+                                               for (; cp >= tmpbuf; cp--) {
+                                                       if (isspace(*cp))
+                                                               *cp = '\0';
+                                                       else
+                                                               break;
                                                }
-                                               break;
+                                               *--savecomp = cptr;
+                                               tmpbuf = *nxtbuf++;
                                        }
+                                       break;
                                } while ((cptr = cptr->c_next));
                        }
 
@@ -263,13 +272,12 @@ finished:
                dat[2] -= fpos;
        }
 
-       if ((datecomp && !datecomp->c_text) || scanfolder) {
-               struct stat st;
-
+       if (fmtstr) {
                fstat(fileno(inb), &st);
-               if (scanfolder)
+               if (scanfolder) {
                        dat[2] = st.st_size;
-               if (datecomp) {
+               }
+               if (datecomp && !datecomp->c_text) {
                        if (!datecomp->c_text) {
                                if (!datecomp->c_tws)
                                        datecomp->c_tws = (struct tws *) calloc((size_t) 1, sizeof(*datecomp->c_tws));
@@ -281,12 +289,11 @@ finished:
                                datecomp->c_flags &= ~CF_DATEFAB;
                        }
                }
-       }
 
-       /* print the scan line */
-       fmt_scan(fmt, scanl, slwidth, dat);
-       if (fmtstr)
+               /* print the scan line */
+               fmt_scan(fmt, scanl, slwidth, dat);
                fputs(scanl, stdout);
+       }
 
        /* return dynamically allocated buffers to pool */
        while ((cptr = *savecomp++)) {