- /* 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) MB_CUR_MAX * (slwidth + 2) );
- 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");
- 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;
- ncomps++;
- }
- FINDCOMP (cptr, "dtimenow");
- if (cptr)
- cptr->c_text = getcpy(dtimenow (0));
- nxtbuf = compbuffers = (char **) calloc((size_t) ncomps, sizeof(char *));
- if (nxtbuf == NULL)
- adios (NULL, "unable to allocate component buffers");
- used_buf = (struct comp **) calloc((size_t) (ncomps+1),
- sizeof(struct comp *));
- if (used_buf == NULL)
- adios (NULL, "unable to allocate component buffer stack");
- used_buf += ncomps+1; *--used_buf = 0;
- rlwidth = bodycomp && (width > SBUFSIZ) ? width : SBUFSIZ;
- for (i = ncomps; i--; )
- *nxtbuf++ = mh_xmalloc(rlwidth);
- }
-
- /*
- * each-message initialization
- */
- nxtbuf = compbuffers;
- savecomp = used_buf;
- tmpbuf = *nxtbuf++;
- dat[0] = innum ? innum : outnum;
- dat[1] = curflg;
- dat[4] = unseen;
-
- /*
- * Get the first field. If the message is non-empty
- * and we're doing an "inc", open the output file.
- */
- if ((state = m_getfld (FLD, name, tmpbuf, rlwidth, inb)) == FILEEOF) {
- if (ferror(inb)) {
- advise("read", "unable to"); /* "read error" */
- return SCNFAT;
- } else {
- return SCNEOF;
- }
- }
-
- if (outnum) {
- if (outnum > 0) {
- scnmsg = m_name (outnum);
- if (*scnmsg == '?') /* msg num out of range */
- return SCNNUM;
- } else {
- scnmsg = "/dev/null";
- }
- if ((scnout = fopen (scnmsg, "w")) == NULL)
- adios (scnmsg, "unable to write");
-#ifdef RPATHS
- /*
- * Add the Return-Path and Delivery-Date
- * header fields to message.
- */
- if (get_returnpath (returnpath, sizeof(returnpath),
- deliverydate, sizeof(deliverydate))) {
- FPUTS ("Return-Path: ");
- FPUTS (returnpath);
- FPUTS ("Delivery-Date: ");
- FPUTS (deliverydate);
- }
-#endif /* RPATHS */
- }
-
- /* scan - main loop */
- for (compnum = 1; ; state = m_getfld (state, name, tmpbuf, rlwidth, inb)) {
- switch (state) {
- case FLD:
- case FLDPLUS:
- compnum++;
- if (outnum) {
- FPUTS (name);
- if ( putc (':', scnout) == EOF) DIEWRERR();
- FPUTS (tmpbuf);
- }
- /*
- * if we're interested in this component, save a pointer
- * to the component text, then start using our next free
- * 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;
- cp >= tmpbuf; cp--)
- if (isspace (*cp))
- *cp = 0;
- else
- break;
- *--savecomp = cptr;
- tmpbuf = *nxtbuf++;
- }
- break;