#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("file", 0, FILESW) \
+ X("nofile", 0, NFILESW) \
+ 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("bufsize size-in-bytes", 0, BUFSZSW) \
X("width column-width", 0, WIDTHSW) \
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,
static int lused = 0;
static int lallocated = 0;
-enum mode_t { MESSAGE, ADDRESS, RAW };
+enum mode_t { MESSAGE, ADDRESS, DATE, 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
*/
static void litputs(char *);
static void litputc(char);
static void process_addresses(struct format *, struct msgs_array *, char *,
- int, int, int *, int, struct fmt_callbacks *);
+ int, int, int *, struct fmt_callbacks *);
static void process_raw(struct format *, struct msgs_array *, char *,
int, int, int *, struct fmt_callbacks *);
static void process_messages(struct format *, struct msgs_array *,
struct msgs_array *, char *, char *, int,
- int, int *, struct fmt_callbacks *);
+ int, int, int *, struct fmt_callbacks *);
+static void process_single_file(FILE *, struct msgs_array *, int *, int,
+ struct format *, char *, 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 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)
struct comp *cptr;
struct msgs_array msgs = { 0, 0, NULL }, compargs = { 0, 0, NULL};
int dump = 0, i;
- int outputsize = 0, bufsize = 0, dupaddrs = 1;
+ int outputsize = 0, bufsize = 0, dupaddrs = 1, trace = 0, files = 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, "");
-#endif
- invo_name = r1bindex (argv[0], '/');
-
- /* read user profile/context */
- context_read();
+ if (nmh_init(argv[0], 1)) { return 1; }
arguments = getarguments (invo_name, argc, argv, 1);
argp = arguments;
form = NULL;
continue;
- case NORMSW:
- normalize = AD_HOST;
+ case TRACESW:
+ trace++;
continue;
- case NNORMSW:
- normalize = AD_NHST;
+ case NTRACESW:
+ trace = 0;
continue;
case ADDRSW:
dupaddrs = 0;
continue;
case DATESW:
- mode = RAW;
+ mode = DATE;
defformat = DEFDATEFORMAT;
continue;
+ case FILESW:
+ files++;
+ continue;
+ case NFILESW:
+ files = 0;
+ continue;
+
case DUPADDRSW:
dupaddrs++;
continue;
case DUMPSW:
dump++;
continue;
+ case NDUMPSW:
+ dump = 0;
+ continue;
}
}
* Only interpret as a folder if we're in message mode
*/
- if (mode == MESSAGE && (*cp == '+' || *cp == '@')) {
+ if (mode == MESSAGE && !files && (*cp == '+' || *cp == '@')) {
if (folder)
adios (NULL, "only one folder at a time!");
else
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);
+ }
}
/*
* callback, do that now. Also, prime ismymbox if we use it.
*/
- if (dupaddrs == 0) {
+ if (dupaddrs == 0 || trace) {
memset(&cb, 0, sizeof(cb));
- cb.formataddr = test_formataddr;
- cb.concataddr = test_concataddr;
cbp = &cb;
- if (!ccme)
- ismymbox(NULL);
+
+ 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, cbp);
+ outputsize, files, dat, cbp);
} else {
if (compargs.size) {
for (i = 0; i < compargs.size; i += 2) {
}
if (mode == ADDRESS) {
- fmt_norm = normalize;
process_addresses(fmt, &msgs, buffer, bufsize, outputsize,
- dat, normalize, cbp);
- } else
+ dat, cbp);
+ } else /* Fall-through for RAW or DATE */
process_raw(fmt, &msgs, buffer, bufsize, outputsize, dat, cbp);
}
static void
process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer,
- int bufsize, int outwidth, int *dat, int norm,
- struct fmt_callbacks *cb)
+ int bufsize, int outwidth, int *dat, struct fmt_callbacks *cb)
{
int i;
char *cp, error[BUFSIZ];
while ((cp = getname(addrs->msgs[i]))) {
if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
adios (NULL, "unable to allocate pqpair memory");
- if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
+ if ((mp = getm(cp, NULL, 0, error, sizeof(error))) == NULL) {
p->pq_text = getcpy(cp);
p->pq_error = getcpy(error);
} else {
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, struct fmt_callbacks *cb)
+ int bufsize, int outwidth, int files, 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];
- char *maildir, *cp, name[NAMESZ], rbuf[BUFSIZ];
+ int i, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1];
+ int num_unseen_seq = 0;
+ ivector_t seqnum = ivector_create (0);
+ char *maildir, *cp;
struct msgs *mp;
- struct comp *c;
FILE *in;
+ /*
+ * If 'files' is set, short-circuit everything else and just process
+ * everything now.
+ */
+
+ if (files) {
+ for (i = 0; i < msgs->size; i++) {
+ if ((in = fopen(cp = msgs->msgs[i], "r")) == NULL) {
+ admonish(cp, "unable to open file");
+ continue;
+ }
+ process_single_file(in, comps, dat, msgsize, fmt, buffer,
+ bufsize, outwidth, cb);
+ }
+
+ return;
+ }
+
if (! folder)
folder = getfolder(1);
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)
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)
if (cur == -1)
dat[1] = msgnum == mp->curmsg;
- /*
- * Get our size if we didn't include one
- */
-
- if (msgsize == -1) {
- struct stat st;
-
- if (fstat(fileno(in), &st) < 0)
- dat[2] = 0;
- else
- dat[2] = st.st_size;
- }
-
/*
* Check to see if this is in the unseen sequence
*/
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;
}
* Read in the message and process the components
*/
- for (state = FLD;;) {
- state = m_getfld(state, name, rbuf, sizeof(rbuf), 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);
- fmt_appendcomp(i, name, rbuf);
- }
- }
-
- while (state == FLDPLUS)
- state = m_getfld(state, name, rbuf,
- sizeof(rbuf), in);
- break;
+ process_single_file(in, comps, dat, msgsize, fmt, buffer,
+ bufsize, outwidth, cb);
+ }
+ }
+
+ ivector_free (seqnum);
+ folder_free(mp);
+ return;
+}
+
+/*
+ * Process a single file in message mode
+ */
- case BODY:
- if (fmt_findcomp("body")) {
- if ((i = strlen(rbuf)) < outwidth)
- state = m_getfld(state, name, rbuf + i,
- outwidth - 1, in);
+static void
+process_single_file(FILE *in, struct msgs_array *comps, int *dat, int msgsize,
+ struct format *fmt, char *buffer, int bufsize,
+ int outwidth, struct fmt_callbacks *cb)
+{
+ int i, state;
+ char name[NAMESZ], rbuf[BUFSIZ];
+ m_getfld_state_t gstate = 0;
+ struct comp *c;
+ int bufsz;
+
+ /*
+ * Get our size if we didn't include one
+ */
- fmt_addcomptext("body", rbuf);
- }
- /* fall through */
+ if (msgsize == -1) {
+ struct stat st;
- default:
- goto finished;
+ if (fstat(fileno(in), &st) < 0)
+ dat[2] = 0;
+ else
+ dat[2] = st.st_size;
+ }
+
+ /*
+ * Initialize everyting else
+ */
+
+ if (dat[0] == -1)
+ dat[0] = 0;
+ if (dat[1] == -1)
+ dat[1] = 0;
+ if (dat[4] == -1)
+ dat[4] = 0;
+
+ /*
+ * Read in the message and process the components
+ */
+
+ for (state = FLD;;) {
+ 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) {
+ bufsz = sizeof(rbuf);
+ state = m_getfld(&gstate, name, rbuf, &bufsz, in);
+ fmt_appendcomp(i, name, rbuf);
}
}
-finished:
- fclose(in);
- /*
- * 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]);
- }
+ 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) {
+ bufsz = outwidth - 1;
+ state = m_getfld(&gstate, name, rbuf + i,
+ &bufsz, in);
}
+
+ fmt_addcomptext("body", rbuf);
}
- fmt_scan(fmt, buffer, bufsize, outwidth, dat, cb);
- fputs(buffer, stdout);
- mlistfree();
+ /* fall through */
+
+ default:
+ goto finished;
}
}
+finished:
+ fclose(in);
+ m_getfld_state_destroy(&gstate);
- folder_free(mp);
- return;
+ /*
+ * 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();
}
/*
}
}
+/*
+ * 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=");
+ litputs(str);
+ ctx->str = str;
+ }
+
+ if (changed)
+ printf("\n");
+
+ if (strcmp(outbuf, ctx->outbuf) != 0) {
+ printf("outbuf=");
+ litputs(outbuf);
+ putchar('\n');
+ free(ctx->outbuf);
+ ctx->outbuf = getcpy(outbuf);
+ }
+}
+
static void
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);
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");
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");
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);
}
}
-#define FNORD(v, s) if (t & (v)) { \
- if (i++ > 0) \
- strcat(buf, "|"); \
- strcat(buf, s); }
-
static char *
c_typestr(int t)
{
- register int i;
static char buf[64];
- buf[0] = '\0';
- if (t & ~(CT_ADDR|CT_DATE))
- printf(buf, "0x%x ", t);
- strcat(buf, "<");
- i = 0;
- FNORD(CT_ADDR, "ADDR");
- FNORD(CT_DATE, "DATE");
- strcat(buf, ">");
+ snprintb(buf, sizeof(buf), t, CT_BITS);
return(buf);
}
static char *
c_flagsstr(int t)
{
- register int i;
static char buf[64];
- buf[0] = '\0';
- if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
- printf(buf, "0x%x ", t);
- strcat(buf, "<");
- i = 0;
- FNORD(CF_TRUE, "TRUE");
- FNORD(CF_PARSED, "PARSED");
- FNORD(CF_DATEFAB, "DATEFAB");
- FNORD(CF_TRIMMED, "TRIMMED");
- strcat(buf, ">");
+ snprintb(buf, sizeof(buf), t, CF_BITS);
return(buf);
}
-#undef FNORD
static void
litputs(char *s)
litputc(char c)
{
if (c & ~ 0177) {
- putc('M', stdout);
- putc('-', stdout);
- c &= 0177;
- }
- if (c < 0x20 || c == 0177) {
+ printf("\\x%02x", (unsigned char) c);
+ } else if (c < 0x20 || c == 0177) {
if (c == '\b') {
putc('\\', stdout);
putc('b', stdout);
/* concatenate all the new addresses onto 'buf' */
for (isgroup = 0; (cp = getname (str)); ) {
- if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
+ if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
fprintf(stderr, "bad address \"%s\" -- %s\n", cp, error);
continue;
}
return 0;
for (mp = &mq; mp->m_next; mp = mp->m_next) {
- if (!mh_strcasecmp (np->m_host, mp->m_next->m_host)
- && !mh_strcasecmp (np->m_mbox, mp->m_next->m_mbox))
+ 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))