+ }
+
+ ivector_free (seqnum);
+ folder_free(mp);
+ return;
+}
+
+/*
+ * Process a single file in message mode
+ */
+
+static void
+process_single_file(FILE *in, struct msgs_array *comps, int *dat, int msgsize,
+ struct format *fmt, charstring_t buffer, int outwidth,
+ struct fmt_callbacks *cb)
+{
+ int i, state;
+ char name[NAMESZ], rbuf[NMH_BUFSIZ];
+ m_getfld_state_t gstate = 0;
+ struct comp *c;
+ int bufsz;
+
+ /*
+ * 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;
+ }
+
+ /*
+ * 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 (;;) {
+ 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);
+ }
+ }
+
+ 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 = min (outwidth, (int) sizeof rbuf - i);
+ m_getfld(&gstate, name, rbuf + i, &bufsz, in);
+ }
+
+ fmt_addcomptext("body", rbuf);
+ }
+ /* fall through */
+
+ default:
+ goto finished;
+ }
+ }
+finished:
+ fclose(in);
+ 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, outwidth, dat, cb);
+ fputs(charstring_buffer (buffer), stdout);
+ mlistfree();
+}
+
+/*
+ * Run text through the format engine with no special processing
+ */
+
+static void
+process_raw(struct format *fmt, struct msgs_array *text, charstring_t buffer,
+ int outwidth, int *dat, struct fmt_callbacks *cb)
+{
+ int i;
+ struct comp *c;
+
+ if (dat[0] == -1)
+ dat[0] = 0;
+ if (dat[1] == -1)
+ dat[1] = 0;
+ if (dat[2] == -1)
+ dat[2] = 0;
+ if (dat[4] == -1)
+ dat[4] = 0;
+
+ c = fmt_findcomp("text");
+
+ for (i = 0; i < text->size; i++) {
+ if (c != NULL) {
+ if (c->c_text != NULL)
+ free(c->c_text);
+ c->c_text = getcpy(text->msgs[i]);
+ }
+
+ fmt_scan(fmt, buffer, outwidth, dat, cb);
+ fputs(charstring_buffer (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,
+ const 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)
+{
+ struct format *fmt;
+
+ printf("Instruction dump of format string: \n%s\n", nfs);