2 ** ap.c -- parse addresses 822-style
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
15 #define FORMAT "=%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
17 static struct swit switches[] = {
19 { "form formatfile", 0 },
31 static struct format *fmt;
38 static int process(char *, int);
42 main(int argc, char **argv)
44 int addrp = 0, normalize = AD_HOST;
46 char *cp, *form = NULL, *fmtstr;
47 char buf[BUFSIZ], **argp;
48 char **arguments, *addrs[NADDRS];
50 setlocale(LC_ALL, "");
51 invo_name = mhbasename(argv[0]);
53 /* read user profile/context */
56 arguments = getarguments(invo_name, argc, argv, 1);
59 while ((cp = *argp++)) {
61 switch (smatch(++cp, switches)) {
63 ambigsw(cp, switches);
67 adios(NULL, "-%s unknown", cp);
70 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
71 print_help(buf, switches, 1);
74 print_version(invo_name);
78 if (!(form = *argp++) || *form == '-')
79 adios(NULL, "missing argument to %s", argp[-2]);
91 adios(NULL, "more than %d addresses", NADDRS);
98 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
100 /* get new format string */
101 fmtstr = new_fs(form, FORMAT);
103 fmt_norm = normalize;
104 fmt_compile(fmtstr, &fmt);
112 for (addrp = 0; addrs[addrp]; addrp++)
113 status += process(addrs[addrp], normalize);
122 struct pqpair *pq_next;
127 process(char *arg, int norm)
131 char buffer[BUFSIZ + 1], error[BUFSIZ];
132 register struct comp *cptr;
133 register struct pqpair *p, *q;
135 register struct mailname *mp;
137 (q = &pq)->pq_next = NULL;
138 while ((cp = getname(arg))) {
139 if ((p = (struct pqpair *)
140 calloc((size_t) 1, sizeof(*p))) == NULL)
141 adios(NULL, "unable to allocate pqpair memory");
142 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
143 p->pq_text = getcpy(cp);
144 p->pq_error = getcpy(error);
147 p->pq_text = getcpy(mp->m_text);
150 q = (q->pq_next = p);
153 for (p = pq.pq_next; p; p = q) {
154 FINDCOMP(cptr, "text");
156 cptr->c_text = p->pq_text;
157 FINDCOMP(cptr, "error");
159 cptr->c_text = p->pq_error;
161 fmt_scan(fmt, buffer, BUFSIZ, dat);
162 fputs(buffer, stdout);