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 FORMAT "=%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
19 static struct swit switches[] = {
21 { "form formatfile", 0 },
33 static struct format *fmt;
40 static int process(char *, int);
44 main(int argc, char **argv)
46 int addrp = 0, normalize = AD_HOST;
48 char *cp, *form = NULL, *fmtstr;
49 char buf[BUFSIZ], **argp;
50 char **arguments, *addrs[NADDRS];
52 setlocale(LC_ALL, "");
53 invo_name = mhbasename(argv[0]);
55 /* read user profile/context */
58 arguments = getarguments(invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
63 switch (smatch(++cp, switches)) {
65 ambigsw(cp, switches);
69 adios(EX_USAGE, NULL, "-%s unknown", cp);
72 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
73 print_help(buf, switches, 1);
74 exit(argc == 2 ? EX_OK : EX_USAGE);
76 print_version(invo_name);
77 exit(argc == 2 ? EX_OK : EX_USAGE);
80 if (!(form = *argp++) || *form == '-')
81 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
93 adios(EX_USAGE, NULL, "more than %d addresses", NADDRS);
100 adios(EX_USAGE, NULL, "usage: %s [switches] addrs ...", invo_name);
102 /* get new format string */
103 fmtstr = new_fs(form, FORMAT);
105 fmt_norm = normalize;
106 fmt_compile(fmtstr, &fmt);
114 for (addrp = 0; addrs[addrp]; addrp++)
115 status += process(addrs[addrp], normalize);
123 struct pqpair *pq_next;
128 process(char *arg, int norm)
132 char buffer[BUFSIZ + 1], error[BUFSIZ];
134 struct pqpair *p, *q;
138 (q = &pq)->pq_next = NULL;
139 while ((cp = getname(arg))) {
140 if ((p = (struct pqpair *)
141 mh_xcalloc((size_t) 1, sizeof(*p))) == NULL)
142 adios(EX_OSERR, NULL, "unable to allocate pqpair memory");
143 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
144 p->pq_text = getcpy(cp);
145 p->pq_error = getcpy(error);
148 p->pq_text = getcpy(mp->m_text);
151 q = (q->pq_next = p);
154 for (p = pq.pq_next; p; p = q) {
155 FINDCOMP(cptr, "text");
157 cptr->c_text = p->pq_text;
158 FINDCOMP(cptr, "error");
160 cptr->c_text = p->pq_error;
162 fmt_scan(fmt, buffer, BUFSIZ, dat);
163 fputs(buffer, stdout);