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 static int process (char *, int, int);
51 main (int argc, char **argv)
53 int addrp = 0, normalize = AD_HOST;
54 int width = 0, status = 0;
55 char *cp, *form = NULL, *format = NULL, *nfs;
56 char buf[BUFSIZ], **argp;
57 char **arguments, *addrs[NADDRS];
60 setlocale(LC_ALL, "");
62 invo_name = r1bindex (argv[0], '/');
64 /* read user profile/context */
68 arguments = getarguments (invo_name, argc, argv, 1);
71 while ((cp = *argp++)) {
73 switch (smatch (++cp, switches)) {
75 ambigsw (cp, switches);
79 adios (NULL, "-%s unknown", cp);
82 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
84 print_help (buf, switches, 1);
87 print_version (invo_name);
91 if (!(form = *argp++) || *form == '-')
92 adios (NULL, "missing argument to %s", argp[-2]);
96 if (!(format = *argp++) || *format == '-')
97 adios (NULL, "missing argument to %s", argp[-2]);
102 if (!(cp = *argp++) || *cp == '-')
103 adios (NULL, "missing argument to %s", argp[-2]);
116 adios (NULL, "more than %d addresses", NADDRS);
123 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
125 /* get new format string */
126 nfs = new_fs (form, format, FORMAT);
129 if ((width = sc_width ()) < WIDTH / 2)
135 fmt_norm = normalize;
136 fmt_compile (nfs, &fmt);
144 for (addrp = 0; addrs[addrp]; addrp++)
145 status += process (addrs[addrp], width, normalize);
154 struct pqpair *pq_next;
159 process (char *arg, int length, int norm)
163 char buffer[WBUFSIZ + 1], error[BUFSIZ];
164 register struct comp *cptr;
165 register struct pqpair *p, *q;
167 register struct mailname *mp;
169 (q = &pq)->pq_next = NULL;
170 while ((cp = getname (arg))) {
171 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
172 adios (NULL, "unable to allocate pqpair memory");
173 if ((mp = getm (cp, NULL, 0, norm, error)) == NULL) {
174 p->pq_text = getcpy (cp);
175 p->pq_error = getcpy (error);
179 p->pq_text = getcpy (mp->m_text);
182 q = (q->pq_next = p);
185 for (p = pq.pq_next; p; p = q) {
186 FINDCOMP (cptr, "text");
188 cptr->c_text = p->pq_text;
189 FINDCOMP (cptr, "error");
191 cptr->c_text = p->pq_error;
193 fmt_scan (fmt, buffer, length, dat);
194 fputs (buffer, stdout);