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 },
24 { "format string", 5 },
30 { "width columns", 0 },
38 static struct format *fmt;
45 int sc_width(void); /* from termsbr.c */
50 static int process(char *, int, int);
54 main(int argc, char **argv)
56 int addrp = 0, normalize = AD_HOST;
57 int width = 0, status = 0;
58 char *cp, *form = NULL, *format = NULL, *nfs;
59 char buf[BUFSIZ], **argp;
60 char **arguments, *addrs[NADDRS];
63 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
70 arguments = getarguments(invo_name, argc, argv, 1);
73 while ((cp = *argp++)) {
75 switch (smatch(++cp, switches)) {
77 ambigsw(cp, switches);
81 adios(NULL, "-%s unknown", cp);
84 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
85 print_help(buf, switches, 1);
88 print_version(invo_name);
92 if (!(form = *argp++) || *form == '-')
93 adios(NULL, "missing argument to %s", argp[-2]);
97 if (!(format = *argp++) || *format == '-')
98 adios(NULL, "missing argument to %s", argp[-2]);
103 if (!(cp = *argp++) || *cp == '-')
104 adios(NULL, "missing argument to %s", argp[-2]);
117 adios(NULL, "more than %d addresses", NADDRS);
124 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
126 /* get new format string */
127 nfs = new_fs(form, format, FORMAT);
130 if ((width = sc_width()) < WIDTH / 2)
136 fmt_norm = normalize;
137 fmt_compile(nfs, &fmt);
145 for (addrp = 0; addrs[addrp]; addrp++)
146 status += process(addrs[addrp], width, normalize);
155 struct pqpair *pq_next;
160 process(char *arg, int length, int norm)
164 char buffer[WBUFSIZ + 1], error[BUFSIZ];
165 register struct comp *cptr;
166 register struct pqpair *p, *q;
168 register struct mailname *mp;
170 (q = &pq)->pq_next = NULL;
171 while ((cp = getname(arg))) {
172 if ((p = (struct pqpair *)
173 calloc((size_t) 1, sizeof(*p))) == NULL)
174 adios(NULL, "unable to allocate pqpair memory");
175 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
176 p->pq_text = getcpy(cp);
177 p->pq_error = getcpy(error);
180 p->pq_text = getcpy(mp->m_text);
183 q = (q->pq_next = p);
186 for (p = pq.pq_next; p; p = q) {
187 FINDCOMP(cptr, "text");
189 cptr->c_text = p->pq_text;
190 FINDCOMP(cptr, "error");
192 cptr->c_text = p->pq_error;
194 fmt_scan(fmt, buffer, length, dat);
195 fputs(buffer, stdout);