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 char *version=VERSION;
36 static struct format *fmt;
43 static int process(char *, int);
47 main(int argc, char **argv)
49 int addrp = 0, normalize = AD_HOST;
51 char *cp, *form = NULL, *fmtstr;
52 char buf[BUFSIZ], **argp;
53 char **arguments, *addrs[NADDRS];
55 setlocale(LC_ALL, "");
56 invo_name = mhbasename(argv[0]);
58 /* read user profile/context */
61 arguments = getarguments(invo_name, argc, argv, 1);
64 while ((cp = *argp++)) {
66 switch (smatch(++cp, switches)) {
68 ambigsw(cp, switches);
72 adios(EX_USAGE, NULL, "-%s unknown", cp);
75 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
76 print_help(buf, switches, 1);
77 exit(argc == 2 ? EX_OK : EX_USAGE);
79 print_version(invo_name);
80 exit(argc == 2 ? EX_OK : EX_USAGE);
83 if (!(form = *argp++) || *form == '-')
84 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
96 adios(EX_USAGE, NULL, "more than %d addresses", NADDRS);
103 adios(EX_USAGE, NULL, "usage: %s [switches] addrs ...", invo_name);
105 /* get new format string */
106 fmtstr = new_fs(form, FORMAT);
108 fmt_norm = normalize;
109 fmt_compile(fmtstr, &fmt);
117 for (addrp = 0; addrs[addrp]; addrp++)
118 status += process(addrs[addrp], normalize);
126 struct pqpair *pq_next;
131 process(char *arg, int norm)
135 char buffer[BUFSIZ + 1], error[BUFSIZ];
137 struct pqpair *p, *q;
141 (q = &pq)->pq_next = NULL;
142 while ((cp = getname(arg))) {
143 p = mh_xcalloc(1, sizeof(*p));
144 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
145 p->pq_text = mh_xstrdup(cp);
146 p->pq_error = mh_xstrdup(error);
149 p->pq_text = mh_xstrdup(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);
166 mh_free0(&(p->pq_text));
168 mh_free0(&(p->pq_error));