3 * ap.c -- parse addresses 822-style
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
18 #define WBUFSIZ BUFSIZ
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
22 static struct swit switches[] = {
24 { "form formatfile", 0 },
26 { "format string", 5 },
32 { "width columns", 0 },
40 static struct format *fmt;
47 int sc_width (void); /* from termsbr.c */
52 static int process (char *, int, int);
56 main (int argc, char **argv)
58 int addrp = 0, normalize = AD_HOST;
59 int width = 0, status = 0;
60 char *cp, *form = NULL, *format = NULL, *nfs;
61 char buf[BUFSIZ], **argp;
62 char **arguments, *addrs[NADDRS];
65 setlocale(LC_ALL, "");
67 invo_name = r1bindex (argv[0], '/');
69 /* read user profile/context */
73 arguments = getarguments (invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch (++cp, switches)) {
80 ambigsw (cp, switches);
84 adios (NULL, "-%s unknown", cp);
87 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
89 print_help (buf, switches, 1);
92 print_version (invo_name);
96 if (!(form = *argp++) || *form == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
101 if (!(format = *argp++) || *format == '-')
102 adios (NULL, "missing argument to %s", argp[-2]);
107 if (!(cp = *argp++) || *cp == '-')
108 adios (NULL, "missing argument to %s", argp[-2]);
121 adios (NULL, "more than %d addresses", NADDRS);
128 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
130 /* get new format string */
131 nfs = new_fs (form, format, FORMAT);
134 if ((width = sc_width ()) < WIDTH / 2)
140 fmt_norm = normalize;
141 fmt_compile (nfs, &fmt);
149 for (addrp = 0; addrs[addrp]; addrp++)
150 status += process (addrs[addrp], width, normalize);
159 struct pqpair *pq_next;
164 process (char *arg, int length, int norm)
168 char buffer[WBUFSIZ + 1], error[BUFSIZ];
169 register struct comp *cptr;
170 register struct pqpair *p, *q;
172 register struct mailname *mp;
174 (q = &pq)->pq_next = NULL;
175 while ((cp = getname (arg))) {
176 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
177 adios (NULL, "unable to allocate pqpair memory");
178 if ((mp = getm (cp, NULL, 0, norm, error)) == NULL) {
179 p->pq_text = getcpy (cp);
180 p->pq_error = getcpy (error);
184 p->pq_text = getcpy (mp->m_text);
187 q = (q->pq_next = p);
190 for (p = pq.pq_next; p; p = q) {
191 FINDCOMP (cptr, "text");
193 cptr->c_text = p->pq_text;
194 FINDCOMP (cptr, "error");
196 cptr->c_text = p->pq_error;
198 fmt_scan (fmt, buffer, length, dat);
199 fputs (buffer, stdout);