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