+ register int len;
+ char baddr[BUFSIZ], 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, dfhost, dftype, AD_NAME, error)) == NULL) {
+ snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
+ cp, error);
+ badaddrs = add(baddr, badaddrs);
+ 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);
+}
+
+
+static int
+insert(struct mailname *np)
+{
+ char buffer[BUFSIZ];
+ register struct mailname *mp;
+
+ if (np->m_mbox == NULL)
+ 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))
+ return 0;
+ }
+ if (!ccme && ismymbox(np))
+ return 0;
+
+ if (querysw) {
+ snprintf(buffer, sizeof(buffer), "Reply to %s? ",
+ adrformat(np));
+ if (!gans(buffer, anoyes))
+ return 0;
+ }
+ mp->m_next = np;
+
+#ifdef ISI
+ if (ismymbox(np))
+ ccme = 0;
+#endif
+
+ return 1;
+}
+
+
+/*
+** Call the mhlproc
+**
+** This function expects that argument out has been fflushed by the caller.
+*/
+
+static void
+replfilter(FILE *in, FILE *out, char *filter)
+{
+ int pid;
+ char *mhl;
+ char *errstr;
+
+ if (filter == NULL)
+ return;
+
+ if (access(filter, R_OK) == NOTOK)
+ adios(filter, "unable to read");
+
+ mhl = mhbasename(mhlproc);
+
+ rewind(in);
+ lseek(fileno(in), (off_t) 0, SEEK_SET);
+
+ switch (pid = fork()) {
+ case NOTOK:
+ adios("fork", "unable to");
+
+ case OK:
+ dup2(fileno(in), fileno(stdin));
+ dup2(fileno(out), fileno(stdout));
+ closefds(3);
+
+ execlp(mhlproc, mhl, "-form", filter, "-noclear", NULL);
+ errstr = strerror(errno);
+ write(2, "unable to exec ", 15);
+ write(2, mhlproc, strlen(mhlproc));
+ write(2, ": ", 2);
+ write(2, errstr, strlen(errstr));
+ write(2, "\n", 1);
+ _exit(-1);
+
+ default:
+ if (pidXwait(pid, mhl))
+ done(1);
+ fseek(out, 0L, SEEK_END);
+ break;
+ }