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