]> diplodocus.org Git - nmh/blobdiff - uip/fmttest.c
Look at post instead of inc to determine build options in build_nmh.
[nmh] / uip / fmttest.c
index 687cb50efa71eb573c03278902427c844668d80b..e18e2cd5c6d51ec07059506673ff93b985ec2943 100644 (file)
 #define FMTTEST_SWITCHES \
     X("form formatfile", 0, FORMSW) \
     X("format string", 5, FMTSW) \
-    X("dump", 0, DUMPSW) \
     X("address", 0, ADDRSW) \
     X("raw", 0, RAWSW) \
     X("date", 0, DATESW) \
     X("message", 0, MESSAGESW) \
+    X("-component-name component-text", 0, OTHERSW) \
+    X("dupaddrs", 0, DUPADDRSW) \
+    X("nodupaddrs", 0, NDUPADDRSW) \
+    X("ccme", 0, CCMESW) \
+    X("noccme", 0, NCCMESW) \
     X("normalize", 0, NORMSW) \
     X("nonormalize", 0, NNORMSW) \
     X("outsize size-in-characters", 0, OUTSIZESW) \
     X("msgcur flag", 0, MSGCURSW) \
     X("msgsize size", 0, MSGSIZESW) \
     X("unseen flag", 0, UNSEENSW) \
+    X("dump", 0, DUMPSW) \
+    X("nodump", 0, NDUMPSW) \
+    X("trace", 0, TRACESW) \
+    X("notrace", 0, NTRACESW) \
     X("version", 0, VERSIONSW) \
-    X("-component-name component-text", 0, OTHERSW) \
     X("help", 0, HELPSW) \
 
 #define X(sw, minchars, id) id,
@@ -55,6 +62,16 @@ enum mode_t { MESSAGE, ADDRESS, RAW };
 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
 
+/*
+ * Context structure used by the tracing routines
+ */
+
+struct trace_context {
+    int num;
+    char *str;
+    char *outbuf;
+};
+
 /*
  * static prototypes
  */
@@ -69,13 +86,22 @@ static char *c_flagsstr(int);
 static void litputs(char *);
 static void litputc(char);
 static void process_addresses(struct format *, struct msgs_array *, char *,
-                             int, int, int *, int);
+                             int, int, int *, int, struct fmt_callbacks *);
 static void process_raw(struct format *, struct msgs_array *, char *,
-                       int, int, int *);
+                       int, int, int *, struct fmt_callbacks *);
 static void process_messages(struct format *, struct msgs_array *,
                             struct msgs_array *, char *, char *, int,
-                            int, int *);
-
+                            int, int *, struct fmt_callbacks *);
+static void test_trace(void *, struct format *, int, char *, char *);
+static char *test_formataddr(char *, char *);
+static char *test_concataddr(char *, char *);
+static int insert(struct mailname *);
+static void mlistfree(void);
+
+static int nodupcheck = 0;     /* If set, no check for duplicates */
+static int ccme = 0;           /* Should I cc myself? */
+static struct mailname mq;     /* Mail addresses to check for duplicates */
+static char *dummy = "dummy";
 
 int
 main (int argc, char **argv)
@@ -86,11 +112,12 @@ main (int argc, char **argv)
     struct comp *cptr;
     struct msgs_array msgs = { 0, 0, NULL }, compargs = { 0, 0, NULL};
     int dump = 0, i;
-    int outputsize = 0, bufsize = 0;
+    int outputsize = 0, bufsize = 0, dupaddrs = 1, trace = 0;
     int colwidth = -1, msgnum = -1, msgcur = -1, msgsize = -1, msgunseen = -1;
     int normalize = AD_HOST;
     enum mode_t mode = MESSAGE;
     int dat[5];
+    struct fmt_callbacks cb, *cbp = NULL;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -110,7 +137,9 @@ main (int argc, char **argv)
             * save the component name and the next argument for the text.
             */
            if (*++cp == '-') {
-               app_msgarg(&compargs, --cp);
+               if (*++cp == '\0')
+                   adios(NULL, "missing component name after --");
+               app_msgarg(&compargs, cp);
                /* Grab next argument for component text */
                if (!(cp = *argp++))
                    adios(NULL, "missing argument to %s", argp[-2]);
@@ -169,6 +198,13 @@ main (int argc, char **argv)
                    normalize = AD_NHST;
                    continue;
 
+               case TRACESW:
+                   trace++;
+                   continue;
+               case NTRACESW:
+                   trace = 0;
+                   continue;
+
                case ADDRSW:
                    mode = ADDRESS;
                    defformat = DEFADDRFORMAT;
@@ -179,12 +215,27 @@ main (int argc, char **argv)
                case MESSAGESW:
                    mode = MESSAGE;
                    defformat = FORMAT;
+                   dupaddrs = 0;
                    continue;
                case DATESW:
                    mode = RAW;
                    defformat = DEFDATEFORMAT;
                    continue;
 
+               case DUPADDRSW:
+                   dupaddrs++;
+                   continue;
+               case NDUPADDRSW:
+                   dupaddrs = 0;
+                   continue;
+
+               case CCMESW:
+                   ccme++;
+                   continue;
+               case NCCMESW:
+                   ccme = 0;
+                   continue;
+
                case WIDTHSW:
                    if (!(cp = *argp++) || *cp == '-')
                        adios(NULL, "missing argument to %s", argp[-2]);
@@ -214,6 +265,9 @@ main (int argc, char **argv)
                case DUMPSW:
                    dump++;
                    continue;
+               case NDUMPSW:
+                   dump = 0;
+                   continue;
 
            }
        }
@@ -246,16 +300,29 @@ main (int argc, char **argv)
               invo_name);
    }
 
+   /*
+    * If you're picking "raw" as a mode, then you have to select
+    * a format.
+    */
+
+   if (mode == RAW && form == NULL && format == NULL) {
+       adios (NULL, "You must specify a format with -form or -format when "
+              "using -raw");
+   }
+
     /*
      * Get new format string.  Must be before chdir().
      */
     nfs = new_fs (form, format, defformat);
     (void) fmt_compile(nfs, &fmt, 1);
 
-    if (dump) {
-       fmt_dump(nfs, fmt);
-       if (compargs.size == 0 && msgs.size == 0)
-           done(0);
+    if (dump || trace) {
+        initlabels(fmt);
+       if (dump) {
+           fmt_dump(nfs, fmt);
+           if (compargs.size == 0 && msgs.size == 0)
+               done(0);
+       }
     }
 
     /*
@@ -270,7 +337,7 @@ main (int argc, char **argv)
     if (outputsize < 0)
        outputsize = bufsize - 1;       /* For the trailing NUL */
     else if (outputsize == 0) {
-       if (mode == ADDRESS
+       if (mode == MESSAGE
            outputsize = sc_width();
        else
            outputsize = bufsize - 1;
@@ -282,9 +349,38 @@ main (int argc, char **argv)
     dat[3] = colwidth == -1 ? outputsize : colwidth;
     dat[4] = msgunseen;
 
+    /*
+     * If we want to provide our own formataddr, concactaddr, or tracing
+     * callback, do that now.  Also, prime ismymbox if we use it.
+     */
+
+    if (dupaddrs == 0 || trace) {
+       memset(&cb, 0, sizeof(cb));
+       cbp = &cb;
+
+       if (dupaddrs == 0) {
+           cb.formataddr = test_formataddr;
+           cb.concataddr = test_concataddr;
+           if (!ccme)
+               ismymbox(NULL);
+       }
+
+       if (trace) {
+           struct trace_context *ctx;
+
+           ctx = mh_xmalloc(sizeof(*ctx));
+           ctx->num = -1;
+           ctx->str = dummy;
+           ctx->outbuf = getcpy(NULL);
+
+           cb.trace_func = test_trace;
+           cb.trace_context = ctx;
+       }
+    }
+
     if (mode == MESSAGE) {
        process_messages(fmt, &compargs, &msgs, buffer, folder, bufsize,
-                        outputsize, dat);
+                        outputsize, dat, cbp);
     } else {
        if (compargs.size) {
            for (i = 0; i < compargs.size; i += 2) {
@@ -297,9 +393,9 @@ main (int argc, char **argv)
        if (mode == ADDRESS) {
            fmt_norm = normalize;
            process_addresses(fmt, &msgs, buffer, bufsize, outputsize,
-                             dat, normalize);
+                             dat, normalize, cbp);
        } else
-           process_raw(fmt, &msgs, buffer, bufsize, outputsize, dat);
+           process_raw(fmt, &msgs, buffer, bufsize, outputsize, dat, cbp);
     }
 
     fmt_free(fmt, 1);
@@ -320,7 +416,8 @@ struct pqpair {
 
 static void
 process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer,
-                 int bufsize, int outwidth, int *dat, int norm)
+                 int bufsize, int outwidth, int *dat, int norm,
+                 struct fmt_callbacks *cb)
 {
     int i;
     char *cp, error[BUFSIZ];
@@ -369,8 +466,9 @@ process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer,
                p->pq_error = NULL;
            }
 
-           fmt_scan(fmt, buffer, bufsize, outwidth, dat, NULL);
+           fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb);
            fputs(buffer, stdout);
+           mlistfree();
 
            if (p->pq_text)
                free(p->pq_text);
@@ -390,13 +488,17 @@ process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer,
 static void
 process_messages(struct format *fmt, struct msgs_array *comps,
                 struct msgs_array *msgs, char *buffer, char *folder,
-                int bufsize, int outwidth, int *dat)
+                int bufsize, int outwidth, int *dat, struct fmt_callbacks *cb)
 {
     int i, state, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1];
-    int num_unseen_seq = 0, seqnum[NUMATTRS];
+    int num_unseen_seq = 0;
+    ivector_t seqnum = ivector_create (0);
     char *maildir, *cp, name[NAMESZ], rbuf[BUFSIZ];
     struct msgs *mp;
+    struct comp *c;
     FILE *in;
+    m_getfld_state_t gstate = 0;
+    int bufsz;
 
     if (! folder)
        folder = getfolder(1);
@@ -406,7 +508,7 @@ process_messages(struct format *fmt, struct msgs_array *comps,
     if (chdir(maildir) < 0)
        adios(maildir, "unable to change directory to");
 
-    if (!(mp = folder_read(folder)))
+    if (!(mp = folder_read(folder, 1)))
        adios(NULL, "unable to read folder %s", folder);
 
     if (mp->nummsg == 0)
@@ -433,7 +535,7 @@ process_messages(struct format *fmt, struct msgs_array *comps,
            dp = getcpy(cp);
            ap = brkstring(dp, " ", "\n");
            for (i = 0; ap && *ap; i++, ap++)
-               seqnum[i] = seq_getnum(mp, *ap);
+               ivector_push_back (seqnum, seq_getnum(mp, *ap));
                
            num_unseen_seq = i;
            if (dp)
@@ -475,7 +577,7 @@ process_messages(struct format *fmt, struct msgs_array *comps,
 
            dat[4] = 0;
            for (i = 0; i < num_unseen_seq; i++) {
-               if (in_sequence(mp, seqnum[i], msgnum)) {
+               if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
                    dat[4] = 1;
                    break;
                }
@@ -486,29 +588,33 @@ process_messages(struct format *fmt, struct msgs_array *comps,
             */
 
            for (state = FLD;;) {
-               state = m_getfld(state, name, rbuf, sizeof(rbuf), in);
+               bufsz = sizeof(rbuf);
+               state = m_getfld(&gstate, name, rbuf, &bufsz, in);
                switch (state) {
                case FLD:
                case FLDPLUS:
                    i = fmt_addcomptext(name, rbuf);
                    if (i != -1) {
                        while (state == FLDPLUS) {
-                           state = m_getfld(state, name, rbuf,
-                                            sizeof(rbuf), in);
+                           bufsz = sizeof(rbuf);
+                           state = m_getfld(&gstate, name, rbuf, &bufsz, in);
                            fmt_appendcomp(i, name, rbuf);
                        }
                    }
 
-                   while (state == FLDPLUS)
-                       state = m_getfld(state, name, rbuf,
-                                        sizeof(rbuf), in);
+                   while (state == FLDPLUS) {
+                       bufsz = sizeof(rbuf);
+                       state = m_getfld(&gstate, name, rbuf, &bufsz, in);
+                   }
                    break;
 
                case BODY:
                    if (fmt_findcomp("body")) {
-                       if ((i = strlen(rbuf)) < outwidth)
-                           state = m_getfld(state, name, rbuf + i,
-                                            outwidth - 1, in);
+                       if ((i = strlen(rbuf)) < outwidth) {
+                           bufsz = outwidth - 1;
+                           state = m_getfld(&gstate, name, rbuf + i,
+                                            &bufsz, in);
+                       }
 
                        fmt_addcomptext("body", rbuf);
                    }
@@ -520,11 +626,28 @@ process_messages(struct format *fmt, struct msgs_array *comps,
            }
 finished:
            fclose(in);
-           fmt_scan(fmt, buffer, bufsize, outwidth, dat, NULL);
+           m_getfld_state_destroy(&gstate);
+
+           /*
+            * Do this now to override any components in the original message
+            */
+           if (comps->size) {
+               for (i = 0; i < comps->size; i += 2) {
+                   c = fmt_findcomp(comps->msgs[i]);
+                   if (c) {
+                       if (c->c_text)
+                           free(c->c_text);
+                       c->c_text = getcpy(comps->msgs[i + 1]);
+                   }
+               }
+           }
+           fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb);
            fputs(buffer, stdout);
+           mlistfree();
        }
     }
 
+    ivector_free (seqnum);
     folder_free(mp);
     return;
 }
@@ -535,7 +658,7 @@ finished:
 
 static void
 process_raw(struct format *fmt, struct msgs_array *text, char *buffer,
-           int bufsize, int outwidth, int *dat)
+           int bufsize, int outwidth, int *dat, struct fmt_callbacks *cb)
 {
     int i;
     struct comp *c;
@@ -558,8 +681,47 @@ process_raw(struct format *fmt, struct msgs_array *text, char *buffer,
            c->c_text = getcpy(text->msgs[i]);
        }
 
-       fmt_scan(fmt, buffer, bufsize, outwidth, dat, NULL);
+       fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb);
        fputs(buffer, stdout);
+       mlistfree();
+    }
+}
+
+/*
+ * Our basic tracing support callback.
+ *
+ * Print out each instruction as it's executed, including the values of
+ * the num and str registers if they've changed.
+ */
+
+static void
+test_trace(void *context, struct format *fmt, int num, char *str, char *outbuf)
+{
+    struct trace_context *ctx = (struct trace_context *) context;
+    int changed = 0;
+
+    dumpone(fmt);
+
+    if (num != ctx->num) {
+       printf("num=%d", num);
+       ctx->num = num;
+       changed++;
+    }
+
+    if (str != ctx->str) {
+       if (changed++)
+           printf(" ");
+       printf("str=\"%s\"", str ? str : "NULL");
+       ctx->str = str;
+    }
+
+    if (changed)
+       printf("\n");
+
+    if (strcmp(outbuf, ctx->outbuf) != 0) {
+       printf("outbuf=\"%s\"\n", outbuf);
+       free(ctx->outbuf);
+       ctx->outbuf = getcpy(outbuf);
     }
 }
 
@@ -570,8 +732,6 @@ fmt_dump (char *nfs, struct format *fmth)
 
        printf("Instruction dump of format string: \n%s\n", nfs);
 
-       initlabels(fmth);
-
        /* Dump them out! */
        for (fmt = fmth; fmt; ++fmt) {
                dumpone(fmt);
@@ -837,8 +997,8 @@ f_typestr(int t)
        case FT_LS_COMP: return("LS_COMP");
        case FT_LS_LIT: return("LS_LIT");
        case FT_LS_GETENV: return("LS_GETENV");
-       case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
-       case FT_LS_DECODE: return("FT_LS_DECODE");
+       case FT_LS_DECODECOMP: return("LS_DECODECOMP");
+       case FT_LS_DECODE: return("LS_DECODE");
        case FT_LS_TRIM: return("LS_TRIM");
        case FT_LV_COMP: return("LV_COMP");
        case FT_LV_COMPFLAG: return("LV_COMPFLAG");
@@ -882,6 +1042,7 @@ f_typestr(int t)
        case FT_LS_FRIENDLY: return("LS_FRIENDLY");
        case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
        case FT_LV_INGRPF: return("LV_INGRPF");
+       case FT_LS_UNQUOTE: return("LS_UNQUOTE");
        case FT_LV_NOHOSTF: return("LV_NOHOSTF");
        case FT_LOCALDATE: return("LOCALDATE");
        case FT_GMTDATE: return("GMTDATE");
@@ -915,7 +1076,7 @@ f_typestr(int t)
        case FT_V_MATCH: return("V_MATCH");
        case FT_V_AMATCH: return("V_AMATCH");
        default:
-               printf(buf, "/* ??? #%d */", t);
+               snprintf(buf, sizeof(buf), "/* ??? #%d */", t);
                return(buf);
        }
 }
@@ -1005,3 +1166,168 @@ litputc(char c)
        } else
                putc(c, stdout);
 }
+
+/*
+ * Routines/code to support the duplicate address suppression code, adapted
+ * from replsbr.c
+ */
+
+static char *buf;              /* our current working buffer */
+static char *bufend;           /* end of working buffer */
+static char *last_dst;         /* buf ptr at end of last call */
+static unsigned int bufsiz=0;  /* current size of buf */
+
+#define BUFINCR 512            /* how much to expand buf when if fills */
+
+#define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
+
+/*
+ * check if there's enough room in buf for str.
+ * add more mem if needed
+ */
+#define CHECKMEM(str) \
+           if ((len = strlen (str)) >= bufend - dst) {\
+               int i = dst - buf;\
+               int n = last_dst - buf;\
+               bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
+               buf = mh_xrealloc (buf, bufsiz);\
+               dst = buf + i;\
+               last_dst = buf + n;\
+               bufend = buf + bufsiz;\
+           }
+
+
+/*
+ * These are versions of similar routines from replsbr.c; the purpose is
+ * to suppress duplicate addresses from being added to a list when building
+ * up addresses for the %(formataddr) format function.  This is used by
+ * repl to prevent duplicate addresses from being added to the "to" line.
+ * See replsbr.c for more information.
+ *
+ * We can't use the functions in replsbr.c directly because they are slightly
+ * different and depend on the rest of replsbr.c
+ */
+static char *
+test_formataddr (char *orig, char *str)
+{
+    register int len;
+    char error[BUFSIZ];
+    register int isgroup;
+    register char *dst;
+    register char *cp;
+    register char *sp;
+    register struct mailname *mp = NULL;
+
+    /* if we don't have a buffer yet, get one */
+    if (bufsiz == 0) {
+       buf = mh_xmalloc (BUFINCR);
+       last_dst = buf;         /* XXX */
+       bufsiz = BUFINCR - 6;  /* leave some slop */
+       bufend = buf + bufsiz;
+    }
+    /*
+     * If "orig" points to our buffer we can just pick up where we
+     * left off.  Otherwise we have to copy orig into our buffer.
+     */
+    if (orig == buf)
+       dst = last_dst;
+    else if (!orig || !*orig) {
+       dst = buf;
+       *dst = '\0';
+    } else {
+       dst = last_dst;         /* XXX */
+       CHECKMEM (orig);
+       CPY (orig);
+    }
+
+    /* concatenate all the new addresses onto 'buf' */
+    for (isgroup = 0; (cp = getname (str)); ) {
+       if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
+           fprintf(stderr, "bad address \"%s\" -- %s\n", cp, error);
+           continue;
+       }
+       if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
+           *dst++ = ';';
+           isgroup = 0;
+       }
+       if (insert (mp)) {
+           /* if we get here we're going to add an address */
+           if (dst != buf) {
+               *dst++ = ',';
+               *dst++ = ' ';
+           }
+           if (mp->m_gname) {
+               CHECKMEM (mp->m_gname);
+               CPY (mp->m_gname);
+               isgroup++;
+           }
+           sp = adrformat (mp);
+           CHECKMEM (sp);
+           CPY (sp);
+       }
+    }
+
+    if (isgroup)
+       *dst++ = ';';
+
+    *dst = '\0';
+    last_dst = dst;
+    return (buf);
+}
+
+
+/*
+ * The companion to test_formataddr(); it behaves the same way, except doesn't
+ * do duplicate address detection.
+ */
+static char *
+test_concataddr(char *orig, char *str)
+{
+    char *cp;
+
+    nodupcheck = 1;
+    cp = test_formataddr(orig, str);
+    nodupcheck = 0;
+    return cp;
+}
+
+static int
+insert (struct mailname *np)
+{
+    struct mailname *mp;
+
+    if (nodupcheck)
+       return 1;
+
+    if (np->m_mbox == NULL)
+       return 0;
+
+    for (mp = &mq; mp->m_next; mp = mp->m_next) {
+       if (!strcasecmp (np->m_host ? np->m_host : "",
+                        mp->m_next->m_host ? mp->m_next->m_host : "") &&
+           !strcasecmp (np->m_mbox ? np->m_mbox : "",
+                        mp->m_next->m_mbox ? mp->m_next->m_mbox : ""))
+           return 0;
+    }
+    if (!ccme && ismymbox (np))
+       return 0;
+
+    mp->m_next = np;
+
+    return 1;
+}
+
+/*
+ * Reset our duplicate address list
+ */
+
+void
+mlistfree(void)
+{
+    struct mailname *mp, *mp2;
+
+    for (mp = mq.m_next; mp; mp = mp2->m_next) {
+       mp2 = mp;
+       mnfree(mp);
+    }
+}