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 = r1bindex (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 ...",
88 print_help (buf, switches, 1);
91 print_version (invo_name);
95 if (!(form = *argp++) || *form == '-')
96 adios (NULL, "missing argument to %s", argp[-2]);
100 if (!(format = *argp++) || *format == '-')
101 adios (NULL, "missing argument to %s", argp[-2]);
106 if (!(cp = *argp++) || *cp == '-')
107 adios (NULL, "missing argument to %s", argp[-2]);
120 adios (NULL, "more than %d addresses", NADDRS);
127 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
129 /* get new format string */
130 nfs = new_fs (form, format, FORMAT);
133 if ((width = sc_width ()) < WIDTH / 2)
139 fmt_norm = normalize;
140 fmt_compile (nfs, &fmt);
148 for (addrp = 0; addrs[addrp]; addrp++)
149 status += process (addrs[addrp], width, normalize);
158 struct pqpair *pq_next;
163 process (char *arg, int length, int norm)
167 char buffer[WBUFSIZ + 1], error[BUFSIZ];
168 register struct comp *cptr;
169 register struct pqpair *p, *q;
171 register struct mailname *mp;
173 (q = &pq)->pq_next = NULL;
174 while ((cp = getname (arg))) {
175 if ((p = (struct pqpair *) 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);
183 p->pq_text = getcpy (mp->m_text);
186 q = (q->pq_next = p);
189 for (p = pq.pq_next; p; p = q) {
190 FINDCOMP (cptr, "text");
192 cptr->c_text = p->pq_text;
193 FINDCOMP (cptr, "error");
195 cptr->c_text = p->pq_error;
197 fmt_scan (fmt, buffer, length, dat);
198 fputs (buffer, stdout);