623e0030ac3f0ac143d2c1adb4dd3f9e1917b212
[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", 0 },
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 #ifdef LOCALE
51         setlocale(LC_ALL, "");
52 #endif
53         invo_name = mhbasename(argv[0]);
54
55         /* read user profile/context */
56         context_read();
57
58         arguments = getarguments(invo_name, argc, argv, 1);
59         argp = arguments;
60
61         while ((cp = *argp++)) {
62                 if (*cp == '-') {
63                         switch (smatch(++cp, switches)) {
64                         case AMBIGSW:
65                                 ambigsw(cp, switches);
66                                 done(1);
67
68                         case UNKWNSW:
69                                 adios(NULL, "-%s unknown", cp);
70
71                         case HELPSW:
72                                 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
73                                 print_help(buf, switches, 1);
74                                 done(1);
75                         case VERSIONSW:
76                                 print_version(invo_name);
77                                 done(1);
78
79                         case FORMSW:
80                                 if (!(form = *argp++) || *form == '-')
81                                         adios(NULL, "missing argument to %s", argp[-2]);
82                                 continue;
83
84                         case NORMSW:
85                                 normalize = AD_HOST;
86                                 continue;
87                         case NNORMSW:
88                                 normalize = AD_NHST;
89                                 continue;
90                         }
91                 }
92                 if (addrp > NADDRS)
93                         adios(NULL, "more than %d addresses", NADDRS);
94                 else
95                         addrs[addrp++] = cp;
96         }
97         addrs[addrp] = NULL;
98
99         if (addrp == 0)
100                 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
101
102         /* get new format string */
103         fmtstr = new_fs(form, FORMAT);
104
105         fmt_norm = normalize;
106         fmt_compile(fmtstr, &fmt);
107
108         dat[0] = 0;
109         dat[1] = 0;
110         dat[2] = 0;
111         dat[3] = BUFSIZ;
112         dat[4] = 0;
113
114         for (addrp = 0; addrs[addrp]; addrp++)
115                 status += process(addrs[addrp], normalize);
116
117         done(status);
118         return 1;
119 }
120
121 struct pqpair {
122         char *pq_text;
123         char *pq_error;
124         struct pqpair *pq_next;
125 };
126
127
128 static int
129 process(char *arg, int norm)
130 {
131         int status = 0;
132         register char *cp;
133         char buffer[BUFSIZ + 1], error[BUFSIZ];
134         register struct comp *cptr;
135         register struct pqpair *p, *q;
136         struct pqpair pq;
137         register struct mailname *mp;
138
139         (q = &pq)->pq_next = NULL;
140         while ((cp = getname(arg))) {
141                 if ((p = (struct pqpair *)
142                                 calloc((size_t) 1, sizeof(*p))) == NULL)
143                         adios(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);
147                         status++;
148                 } else {
149                         p->pq_text = getcpy(mp->m_text);
150                         mnfree(mp);
151                 }
152                 q = (q->pq_next = p);
153         }
154
155         for (p = pq.pq_next; p; p = q) {
156                 FINDCOMP(cptr, "text");
157                 if (cptr)
158                         cptr->c_text = p->pq_text;
159                 FINDCOMP(cptr, "error");
160                 if (cptr)
161                         cptr->c_text = p->pq_error;
162
163                 fmt_scan(fmt, buffer, BUFSIZ, dat);
164                 fputs(buffer, stdout);
165
166                 free(p->pq_text);
167                 if (p->pq_error)
168                         free(p->pq_error);
169                 q = p->pq_next;
170                 free((char *) p);
171         }
172
173         return status;
174 }