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