e444cd89566176e3d37795e070e3fb910502397a
[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 WIDTH 78
16 #define WBUFSIZ BUFSIZ
17
18 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
19
20 static struct swit switches[] = {
21 #define FORMSW 0
22         { "form formatfile", 0 },
23 #define FMTSW 1
24         { "format string", 5 },
25 #define NORMSW 2
26         { "normalize", 0 },
27 #define NNORMSW 3
28         { "nonormalize", 0 },
29 #define WIDTHSW 4
30         { "width columns", 0 },
31 #define VERSIONSW 5
32         { "version", 0 },
33 #define HELPSW 6
34         { "help", 0 },
35         { NULL, 0 }
36 };
37
38 static struct format *fmt;
39
40 static int dat[5];
41
42 /*
43 ** prototypes
44 */
45 int sc_width(void);  /* from termsbr.c */
46
47 /*
48 ** static prototypes
49 */
50 static int process(char *, int, int);
51
52
53 int
54 main(int argc, char **argv)
55 {
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];
61
62 #ifdef LOCALE
63         setlocale(LC_ALL, "");
64 #endif
65         invo_name = mhbasename(argv[0]);
66
67         /* read user profile/context */
68         context_read();
69
70         arguments = getarguments(invo_name, argc, argv, 1);
71         argp = arguments;
72
73         while ((cp = *argp++)) {
74                 if (*cp == '-') {
75                         switch (smatch(++cp, switches)) {
76                         case AMBIGSW:
77                                 ambigsw(cp, switches);
78                                 done(1);
79
80                         case UNKWNSW:
81                                 adios(NULL, "-%s unknown", cp);
82
83                         case HELPSW:
84                                 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
85                                 print_help(buf, switches, 1);
86                                 done(1);
87                         case VERSIONSW:
88                                 print_version(invo_name);
89                                 done(1);
90
91                         case FORMSW:
92                                 if (!(form = *argp++) || *form == '-')
93                                         adios(NULL, "missing argument to %s", argp[-2]);
94                                 format = NULL;
95                                 continue;
96                         case FMTSW:
97                                 if (!(format = *argp++) || *format == '-')
98                                         adios(NULL, "missing argument to %s", argp[-2]);
99                                 form = NULL;
100                                 continue;
101
102                         case WIDTHSW:
103                                 if (!(cp = *argp++) || *cp == '-')
104                                         adios(NULL, "missing argument to %s", argp[-2]);
105                                 width = atoi(cp);
106                                 continue;
107
108                         case NORMSW:
109                                 normalize = AD_HOST;
110                                 continue;
111                         case NNORMSW:
112                                 normalize = AD_NHST;
113                                 continue;
114                         }
115                 }
116                 if (addrp > NADDRS)
117                         adios(NULL, "more than %d addresses", NADDRS);
118                 else
119                         addrs[addrp++] = cp;
120         }
121         addrs[addrp] = NULL;
122
123         if (addrp == 0)
124                 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
125
126         /* get new format string */
127         nfs = new_fs(form, format, FORMAT);
128
129         if (width == 0) {
130                 if ((width = sc_width()) < WIDTH / 2)
131                         width = WIDTH / 2;
132                 width -= 2;
133         }
134         if (width > WBUFSIZ)
135                 width = WBUFSIZ;
136         fmt_norm = normalize;
137         fmt_compile(nfs, &fmt);
138
139         dat[0] = 0;
140         dat[1] = 0;
141         dat[2] = 0;
142         dat[3] = width;
143         dat[4] = 0;
144
145         for (addrp = 0; addrs[addrp]; addrp++)
146                 status += process(addrs[addrp], width, normalize);
147
148         done(status);
149         return 1;
150 }
151
152 struct pqpair {
153         char *pq_text;
154         char *pq_error;
155         struct pqpair *pq_next;
156 };
157
158
159 static int
160 process(char *arg, int length, int norm)
161 {
162         int status = 0;
163         register char *cp;
164         char buffer[WBUFSIZ + 1], error[BUFSIZ];
165         register struct comp *cptr;
166         register struct pqpair *p, *q;
167         struct pqpair pq;
168         register struct mailname *mp;
169
170         (q = &pq)->pq_next = NULL;
171         while ((cp = getname(arg))) {
172                 if ((p = (struct pqpair *)
173                                 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);
178                         status++;
179                 } else {
180                         p->pq_text = getcpy(mp->m_text);
181                         mnfree(mp);
182                 }
183                 q = (q->pq_next = p);
184         }
185
186         for (p = pq.pq_next; p; p = q) {
187                 FINDCOMP(cptr, "text");
188                 if (cptr)
189                         cptr->c_text = p->pq_text;
190                 FINDCOMP(cptr, "error");
191                 if (cptr)
192                         cptr->c_text = p->pq_error;
193
194                 fmt_scan(fmt, buffer, length, dat);
195                 fputs(buffer, stdout);
196
197                 free(p->pq_text);
198                 if (p->pq_error)
199                         free(p->pq_error);
200                 q = p->pq_next;
201                 free((char *) p);
202         }
203
204         return status;
205 }