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>
17 #define WBUFSIZ BUFSIZ
19 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
21 static struct swit switches[] = {
23 { "form formatfile", 0 },
25 { "format string", 5 },
31 { "width columns", 0 },
39 static struct format *fmt;
46 int sc_width(void); /* from termsbr.c */
51 static int process(char *, int, int);
55 main(int argc, char **argv)
57 int addrp = 0, normalize = AD_HOST;
58 int width = 0, status = 0;
59 char *cp, *form = NULL, *format = NULL, *nfs;
60 char buf[BUFSIZ], **argp;
61 char **arguments, *addrs[NADDRS];
64 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
68 /* read user profile/context */
72 arguments = getarguments(invo_name, argc, argv, 1);
75 while ((cp = *argp++)) {
77 switch (smatch(++cp, switches)) {
79 ambigsw(cp, switches);
83 adios(NULL, "-%s unknown", cp);
86 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
87 print_help(buf, switches, 1);
90 print_version(invo_name);
94 if (!(form = *argp++) || *form == '-')
95 adios(NULL, "missing argument to %s", argp[-2]);
99 if (!(format = *argp++) || *format == '-')
100 adios(NULL, "missing argument to %s", argp[-2]);
105 if (!(cp = *argp++) || *cp == '-')
106 adios(NULL, "missing argument to %s", argp[-2]);
119 adios(NULL, "more than %d addresses", NADDRS);
126 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
128 /* get new format string */
129 nfs = new_fs(form, format, FORMAT);
132 if ((width = sc_width()) < WIDTH / 2)
138 fmt_norm = normalize;
139 fmt_compile(nfs, &fmt);
147 for (addrp = 0; addrs[addrp]; addrp++)
148 status += process(addrs[addrp], width, normalize);
157 struct pqpair *pq_next;
162 process(char *arg, int length, int norm)
166 char buffer[WBUFSIZ + 1], error[BUFSIZ];
167 register struct comp *cptr;
168 register struct pqpair *p, *q;
170 register struct mailname *mp;
172 (q = &pq)->pq_next = NULL;
173 while ((cp = getname(arg))) {
174 if ((p = (struct pqpair *)
175 calloc((size_t) 1, sizeof(*p))) == NULL)
176 adios(NULL, "unable to allocate pqpair memory");
177 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
178 p->pq_text = getcpy(cp);
179 p->pq_error = getcpy(error);
182 p->pq_text = getcpy(mp->m_text);
185 q = (q->pq_next = p);
188 for (p = pq.pq_next; p; p = q) {
189 FINDCOMP(cptr, "text");
191 cptr->c_text = p->pq_text;
192 FINDCOMP(cptr, "error");
194 cptr->c_text = p->pq_error;
196 fmt_scan(fmt, buffer, length, dat);
197 fputs(buffer, stdout);