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>
16 #define WBUFSIZ BUFSIZ
18 #define FORMAT "=%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
20 static struct swit switches[] = {
22 { "form formatfile", 0 },
28 { "width columns", 0 },
36 static struct format *fmt;
43 int sc_width(void); /* from termsbr.c */
48 static int process(char *, int, int);
52 main(int argc, char **argv)
54 int addrp = 0, normalize = AD_HOST;
55 int width = 0, status = 0;
56 char *cp, *form = NULL, *nfs;
57 char buf[BUFSIZ], **argp;
58 char **arguments, *addrs[NADDRS];
61 setlocale(LC_ALL, "");
63 invo_name = mhbasename(argv[0]);
65 /* read user profile/context */
68 arguments = getarguments(invo_name, argc, argv, 1);
71 while ((cp = *argp++)) {
73 switch (smatch(++cp, switches)) {
75 ambigsw(cp, switches);
79 adios(NULL, "-%s unknown", cp);
82 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
83 print_help(buf, switches, 1);
86 print_version(invo_name);
90 if (!(form = *argp++) || *form == '-')
91 adios(NULL, "missing argument to %s", argp[-2]);
95 if (!(cp = *argp++) || *cp == '-')
96 adios(NULL, "missing argument to %s", argp[-2]);
109 adios(NULL, "more than %d addresses", NADDRS);
116 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
118 /* get new format string */
119 nfs = new_fs(form, FORMAT);
122 if ((width = sc_width()) < WIDTH / 2)
128 fmt_norm = normalize;
129 fmt_compile(nfs, &fmt);
137 for (addrp = 0; addrs[addrp]; addrp++)
138 status += process(addrs[addrp], width, normalize);
147 struct pqpair *pq_next;
152 process(char *arg, int length, int norm)
156 char buffer[WBUFSIZ + 1], error[BUFSIZ];
157 register struct comp *cptr;
158 register struct pqpair *p, *q;
160 register struct mailname *mp;
162 (q = &pq)->pq_next = NULL;
163 while ((cp = getname(arg))) {
164 if ((p = (struct pqpair *)
165 calloc((size_t) 1, sizeof(*p))) == NULL)
166 adios(NULL, "unable to allocate pqpair memory");
167 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
168 p->pq_text = getcpy(cp);
169 p->pq_error = getcpy(error);
172 p->pq_text = getcpy(mp->m_text);
175 q = (q->pq_next = p);
178 for (p = pq.pq_next; p; p = q) {
179 FINDCOMP(cptr, "text");
181 cptr->c_text = p->pq_text;
182 FINDCOMP(cptr, "error");
184 cptr->c_text = p->pq_error;
186 fmt_scan(fmt, buffer, length, dat);
187 fputs(buffer, stdout);