3 * ap.c -- parse addresses 822-style
10 #include <h/fmt_scan.h>
11 #include <mts/generic/mts.h>
16 #define WBUFSIZ BUFSIZ
18 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
20 static struct swit switches[] = {
22 { "form formatfile", 0 },
24 { "format string", 5 },
30 { "width columns", 0 },
38 static struct format *fmt;
45 int sc_width (void); /* from termsbr.c */
50 static int process (char *, int, int);
54 main (int argc, char **argv)
56 int addrp = 0, normalize = AD_HOST;
57 int width = 0, status = 0;
58 char *cp, *form = NULL, *format = NULL, *nfs;
59 char buf[BUFSIZ], **argp;
60 char **arguments, *addrs[NADDRS];
63 setlocale(LC_ALL, "");
65 invo_name = r1bindex (argv[0], '/');
67 /* read user profile/context */
71 arguments = getarguments (invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch (++cp, switches)) {
78 ambigsw (cp, switches);
82 adios (NULL, "-%s unknown", cp);
85 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
87 print_help (buf, switches, 1);
90 print_version (invo_name);
94 if (!(form = *argp++) || *form == '-')
95 adios (NULL, "missing argument to %s", argp[-2]);
99 if (!(format = *argp++) || *format == '-')
100 adios (NULL, "missing argument to %s", argp[-2]);
105 if (!(cp = *argp++) || *cp == '-')
106 adios (NULL, "missing argument to %s", argp[-2]);
119 adios (NULL, "more than %d addresses", NADDRS);
126 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
128 /* get new format string */
129 nfs = new_fs (form, format, FORMAT);
132 if ((width = sc_width ()) < WIDTH / 2)
138 fmt_norm = normalize;
139 fmt_compile (nfs, &fmt);
147 for (addrp = 0; addrs[addrp]; addrp++)
148 status += process (addrs[addrp], width, normalize);
150 return done (status);
156 struct pqpair *pq_next;
161 process (char *arg, int length, int norm)
165 char buffer[WBUFSIZ + 1], error[BUFSIZ];
166 register struct comp *cptr;
167 register struct pqpair *p, *q;
169 register struct mailname *mp;
171 (q = &pq)->pq_next = NULL;
172 while ((cp = getname (arg))) {
173 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
174 adios (NULL, "unable to allocate pqpair memory");
175 if ((mp = getm (cp, NULL, 0, norm, error)) == NULL) {
176 p->pq_text = getcpy (cp);
177 p->pq_error = getcpy (error);
181 p->pq_text = getcpy (mp->m_text);
184 q = (q->pq_next = p);
187 for (p = pq.pq_next; p; p = q) {
188 FINDCOMP (cptr, "text");
190 cptr->c_text = p->pq_text;
191 FINDCOMP (cptr, "error");
193 cptr->c_text = p->pq_error;
195 fmt_scan (fmt, buffer, length, dat);
196 fputs (buffer, stdout);