Removed the space between function names and the opening parenthesis.
[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 = r1bindex(argv[0], '/');
67
68         /* read user profile/context */
69         context_read();
70
71         mts_init(invo_name);
72         arguments = getarguments(invo_name, argc, argv, 1);
73         argp = arguments;
74
75         while ((cp = *argp++)) {
76                 if (*cp == '-') {
77                         switch (smatch(++cp, switches)) {
78                                 case AMBIGSW:
79                                         ambigsw(cp, switches);
80                                         done(1);
81
82                                 case UNKWNSW:
83                                         adios(NULL, "-%s unknown", cp);
84
85                                 case HELPSW:
86                                         snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
87                                         print_help(buf, switches, 1);
88                                         done(1);
89                                 case VERSIONSW:
90                                         print_version(invo_name);
91                                         done(1);
92
93                                 case FORMSW:
94                                         if (!(form = *argp++) || *form == '-')
95                                                 adios(NULL, "missing argument to %s", argp[-2]);
96                                         format = NULL;
97                                         continue;
98                                 case FMTSW:
99                                         if (!(format = *argp++) || *format == '-')
100                                                 adios(NULL, "missing argument to %s", argp[-2]);
101                                         form = NULL;
102                                         continue;
103
104                                 case WIDTHSW:
105                                         if (!(cp = *argp++) || *cp == '-')
106                                                 adios(NULL, "missing argument to %s", argp[-2]);
107                                         width = atoi(cp);
108                                         continue;
109
110                                 case NORMSW:
111                                         normalize = AD_HOST;
112                                         continue;
113                                 case NNORMSW:
114                                         normalize = AD_NHST;
115                                         continue;
116                         }
117                 }
118                 if (addrp > NADDRS)
119                         adios(NULL, "more than %d addresses", NADDRS);
120                 else
121                         addrs[addrp++] = cp;
122         }
123         addrs[addrp] = NULL;
124
125         if (addrp == 0)
126                 adios(NULL, "usage: %s [switches] addrs ...", invo_name);
127
128         /* get new format string */
129         nfs = new_fs(form, format, FORMAT);
130
131         if (width == 0) {
132                 if ((width = sc_width()) < WIDTH / 2)
133                         width = WIDTH / 2;
134                 width -= 2;
135         }
136         if (width > WBUFSIZ)
137                 width = WBUFSIZ;
138         fmt_norm = normalize;
139         fmt_compile(nfs, &fmt);
140
141         dat[0] = 0;
142         dat[1] = 0;
143         dat[2] = 0;
144         dat[3] = width;
145         dat[4] = 0;
146
147         for (addrp = 0; addrs[addrp]; addrp++)
148                 status += process(addrs[addrp], width, normalize);
149
150         done(status);
151         return 1;
152 }
153
154 struct pqpair {
155         char *pq_text;
156         char *pq_error;
157         struct pqpair *pq_next;
158 };
159
160
161 static int
162 process(char *arg, int length, int norm)
163 {
164         int status = 0;
165         register char *cp;
166         char buffer[WBUFSIZ + 1], error[BUFSIZ];
167         register struct comp *cptr;
168         register struct pqpair *p, *q;
169         struct pqpair pq;
170         register struct mailname *mp;
171
172         (q = &pq)->pq_next = NULL;
173         while ((cp = getname(arg))) {
174                 if ((p = (struct pqpair *)
175                                 calloc((size_t) 1, sizeof(*p))) == NULL)
176                         adios(NULL, "unable to allocate pqpair memory");
177                 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
178                         p->pq_text = getcpy(cp);
179                         p->pq_error = getcpy(error);
180                         status++;
181                 } else {
182                         p->pq_text = getcpy(mp->m_text);
183                         mnfree(mp);
184                 }
185                 q = (q->pq_next = p);
186         }
187
188         for (p = pq.pq_next; p; p = q) {
189                 FINDCOMP(cptr, "text");
190                 if (cptr)
191                         cptr->c_text = p->pq_text;
192                 FINDCOMP(cptr, "error");
193                 if (cptr)
194                         cptr->c_text = p->pq_error;
195
196                 fmt_scan(fmt, buffer, length, dat);
197                 fputs(buffer, stdout);
198
199                 free(p->pq_text);
200                 if (p->pq_error)
201                         free(p->pq_error);
202                 q = p->pq_next;
203                 free((char *) p);
204         }
205
206         return status;
207 }