41554d7ed14c231fb25f2c4ca8327ab9c0d18e37
[mmh] / uip / ap.c
1 /*
2 ** ap.c -- parse addresses 822-style
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12
13 #define NADDRS 100
14
15 #define FORMAT "=%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
16
17 static struct swit switches[] = {
18 #define FORMSW 0
19         { "form formatfile", 0 },
20 #define NORMSW 1
21         { "normalize", 0 },
22 #define NNORMSW 2
23         { "nonormalize", 2 },
24 #define VERSIONSW 3
25         { "Version", 0 },
26 #define HELPSW 4
27         { "help", 0 },
28         { NULL, 0 }
29 };
30
31 static struct format *fmt;
32
33 static int dat[5];
34
35 /*
36 ** static prototypes
37 */
38 static int process(char *, int);
39
40
41 int
42 main(int argc, char **argv)
43 {
44         int addrp = 0, normalize = AD_HOST;
45         int status = 0;
46         char *cp, *form = NULL, *fmtstr;
47         char buf[BUFSIZ], **argp;
48         char **arguments, *addrs[NADDRS];
49
50         setlocale(LC_ALL, "");
51         invo_name = mhbasename(argv[0]);
52
53         /* read user profile/context */
54         context_read();
55
56         arguments = getarguments(invo_name, argc, argv, 1);
57         argp = arguments;
58
59         while ((cp = *argp++)) {
60                 if (*cp == '-') {
61                         switch (smatch(++cp, switches)) {
62                         case AMBIGSW:
63                                 ambigsw(cp, switches);
64                                 /* sysexits.h EX_USAGE */
65                                 exit(1);
66
67                         case UNKWNSW:
68                                 adios(NULL, "-%s unknown", cp);
69
70                         case HELPSW:
71                                 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
72                                 print_help(buf, switches, 1);
73                                 exit(0);
74                         case VERSIONSW:
75                                 print_version(invo_name);
76                                 exit(0);
77
78                         case FORMSW:
79                                 if (!(form = *argp++) || *form == '-')
80                                         adios(NULL, "missing argument to %s", argp[-2]);
81                                 continue;
82
83                         case NORMSW:
84                                 normalize = AD_HOST;
85                                 continue;
86                         case NNORMSW:
87                                 normalize = AD_NHST;
88                                 continue;
89                         }
90                 }
91                 if (addrp > NADDRS)
92                         adios(NULL, "more than %d addresses", NADDRS);
93                 else
94                         addrs[addrp++] = cp;
95         }
96         addrs[addrp] = NULL;
97
98         if (addrp == 0)
99                 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
100
101         /* get new format string */
102         fmtstr = new_fs(form, FORMAT);
103
104         fmt_norm = normalize;
105         fmt_compile(fmtstr, &fmt);
106
107         dat[0] = 0;
108         dat[1] = 0;
109         dat[2] = 0;
110         dat[3] = BUFSIZ;
111         dat[4] = 0;
112
113         for (addrp = 0; addrs[addrp]; addrp++)
114                 status += process(addrs[addrp], normalize);
115
116         exit(status);
117         return 1;
118 }
119
120 struct pqpair {
121         char *pq_text;
122         char *pq_error;
123         struct pqpair *pq_next;
124 };
125
126
127 static int
128 process(char *arg, int norm)
129 {
130         int status = 0;
131         register char *cp;
132         char buffer[BUFSIZ + 1], error[BUFSIZ];
133         register struct comp *cptr;
134         register struct pqpair *p, *q;
135         struct pqpair pq;
136         register struct mailname *mp;
137
138         (q = &pq)->pq_next = NULL;
139         while ((cp = getname(arg))) {
140                 if ((p = (struct pqpair *)
141                                 calloc((size_t) 1, sizeof(*p))) == NULL)
142                         adios(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);
146                         status++;
147                 } else {
148                         p->pq_text = getcpy(mp->m_text);
149                         mnfree(mp);
150                 }
151                 q = (q->pq_next = p);
152         }
153
154         for (p = pq.pq_next; p; p = q) {
155                 FINDCOMP(cptr, "text");
156                 if (cptr)
157                         cptr->c_text = p->pq_text;
158                 FINDCOMP(cptr, "error");
159                 if (cptr)
160                         cptr->c_text = p->pq_error;
161
162                 fmt_scan(fmt, buffer, BUFSIZ, dat);
163                 fputs(buffer, stdout);
164
165                 free(p->pq_text);
166                 if (p->pq_error)
167                         free(p->pq_error);
168                 q = p->pq_next;
169                 free((char *) p);
170         }
171
172         return status;
173 }