3 * ap.c -- parse addresses 822-style
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/addrsbr.h>
14 #include <h/fmt_scan.h>
20 #define WBUFSIZ BUFSIZ
22 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
24 static struct swit switches[] = {
26 { "form formatfile", 0 },
28 { "format string", 5 },
34 { "width columns", 0 },
42 static struct format *fmt;
49 int sc_width (void); /* from termsbr.c */
54 static int process (char *, int, int);
58 main (int argc, char **argv)
60 int addrp = 0, normalize = AD_HOST;
61 int width = 0, status = 0;
62 char *cp, *form = NULL, *format = NULL, *nfs;
63 char buf[BUFSIZ], **argp;
64 char **arguments, *addrs[NADDRS];
67 setlocale(LC_ALL, "");
69 invo_name = r1bindex (argv[0], '/');
71 /* read user profile/context */
75 arguments = getarguments (invo_name, argc, argv, 1);
78 while ((cp = *argp++)) {
80 switch (smatch (++cp, switches)) {
82 ambigsw (cp, switches);
86 adios (NULL, "-%s unknown", cp);
89 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
91 print_help (buf, switches, 1);
94 print_version (invo_name);
98 if (!(form = *argp++) || *form == '-')
99 adios (NULL, "missing argument to %s", argp[-2]);
103 if (!(format = *argp++) || *format == '-')
104 adios (NULL, "missing argument to %s", argp[-2]);
109 if (!(cp = *argp++) || *cp == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
123 adios (NULL, "more than %d addresses", NADDRS);
130 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
132 /* get new format string */
133 nfs = new_fs (form, format, FORMAT);
136 if ((width = sc_width ()) < WIDTH / 2)
142 fmt_norm = normalize;
143 fmt_compile (nfs, &fmt);
151 for (addrp = 0; addrs[addrp]; addrp++)
152 status += process (addrs[addrp], width, normalize);
154 return done (status);
160 struct pqpair *pq_next;
165 process (char *arg, int length, int norm)
169 char buffer[WBUFSIZ + 1], error[BUFSIZ];
170 register struct comp *cptr;
171 register struct pqpair *p, *q;
173 register struct mailname *mp;
175 (q = &pq)->pq_next = NULL;
176 while ((cp = getname (arg))) {
177 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
178 adios (NULL, "unable to allocate pqpair memory");
179 if ((mp = getm (cp, NULL, 0, norm, error)) == NULL) {
180 p->pq_text = getcpy (cp);
181 p->pq_error = getcpy (error);
185 p->pq_text = getcpy (mp->m_text);
188 q = (q->pq_next = p);
191 for (p = pq.pq_next; p; p = q) {
192 FINDCOMP (cptr, "text");
194 cptr->c_text = p->pq_text;
195 FINDCOMP (cptr, "error");
197 cptr->c_text = p->pq_error;
199 fmt_scan (fmt, buffer, length, dat);
200 fputs (buffer, stdout);