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>
18 #define FORMAT "=%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
20 static struct swit switches[] = {
22 { "form formatfile", 0 },
34 static struct format *fmt;
41 static int process(char *, int);
45 main(int argc, char **argv)
47 int addrp = 0, normalize = AD_HOST;
49 char *cp, *form = NULL, *fmtstr;
50 char buf[BUFSIZ], **argp;
51 char **arguments, *addrs[NADDRS];
53 setlocale(LC_ALL, "");
54 invo_name = mhbasename(argv[0]);
56 /* read user profile/context */
59 arguments = getarguments(invo_name, argc, argv, 1);
62 while ((cp = *argp++)) {
64 switch (smatch(++cp, switches)) {
66 ambigsw(cp, switches);
70 adios(EX_USAGE, NULL, "-%s unknown", cp);
73 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
74 print_help(buf, switches, 1);
75 exit(argc == 2 ? EX_OK : EX_USAGE);
77 print_version(invo_name);
78 exit(argc == 2 ? EX_OK : EX_USAGE);
81 if (!(form = *argp++) || *form == '-')
82 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
94 adios(EX_USAGE, NULL, "more than %d addresses", NADDRS);
101 adios(EX_USAGE, NULL, "usage: %s [switches] addrs ...", invo_name);
103 /* get new format string */
104 fmtstr = new_fs(form, FORMAT);
106 fmt_norm = normalize;
107 fmt_compile(fmtstr, &fmt);
115 for (addrp = 0; addrs[addrp]; addrp++)
116 status += process(addrs[addrp], normalize);
124 struct pqpair *pq_next;
129 process(char *arg, int norm)
133 char buffer[BUFSIZ + 1], error[BUFSIZ];
135 struct pqpair *p, *q;
139 (q = &pq)->pq_next = NULL;
140 while ((cp = getname(arg))) {
141 if ((p = (struct pqpair *)
142 mh_xcalloc((size_t) 1, sizeof(*p))) == NULL)
143 adios(EX_OSERR, NULL, "unable to allocate pqpair memory");
144 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
145 p->pq_text = getcpy(cp);
146 p->pq_error = getcpy(error);
149 p->pq_text = getcpy(mp->m_text);
152 q = (q->pq_next = p);
155 for (p = pq.pq_next; p; p = q) {
156 FINDCOMP(cptr, "text");
158 cptr->c_text = p->pq_text;
159 FINDCOMP(cptr, "error");
161 cptr->c_text = p->pq_error;
163 fmt_scan(fmt, buffer, BUFSIZ, dat);
164 fputs(buffer, stdout);