Fix missing va_end call in uip/mhmisc.c
[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/utils.h>
13 #include <locale.h>
14 #include <sysexits.h>
15
16 #define NADDRS 100
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", 2 },
27 #define VERSIONSW 3
28         { "Version", 0 },
29 #define HELPSW 4
30         { "help", 0 },
31         { NULL, 0 }
32 };
33
34 static struct format *fmt;
35
36 static int dat[5];
37
38 /*
39 ** static prototypes
40 */
41 static int process(char *, int);
42
43
44 int
45 main(int argc, char **argv)
46 {
47         int addrp = 0, normalize = AD_HOST;
48         int status = 0;
49         char *cp, *form = NULL, *fmtstr;
50         char buf[BUFSIZ], **argp;
51         char **arguments, *addrs[NADDRS];
52
53         setlocale(LC_ALL, "");
54         invo_name = mhbasename(argv[0]);
55
56         /* read user profile/context */
57         context_read();
58
59         arguments = getarguments(invo_name, argc, argv, 1);
60         argp = arguments;
61
62         while ((cp = *argp++)) {
63                 if (*cp == '-') {
64                         switch (smatch(++cp, switches)) {
65                         case AMBIGSW:
66                                 ambigsw(cp, switches);
67                                 exit(EX_USAGE);
68
69                         case UNKWNSW:
70                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
71
72                         case HELPSW:
73                                 snprintf(buf, sizeof(buf), "%s [switches] addrs ...", invo_name);
74                                 print_help(buf, switches, 1);
75                                 exit(argc == 2 ? EX_OK : EX_USAGE);
76                         case VERSIONSW:
77                                 print_version(invo_name);
78                                 exit(argc == 2 ? EX_OK : EX_USAGE);
79
80                         case FORMSW:
81                                 if (!(form = *argp++) || *form == '-')
82                                         adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
83                                 continue;
84
85                         case NORMSW:
86                                 normalize = AD_HOST;
87                                 continue;
88                         case NNORMSW:
89                                 normalize = AD_NHST;
90                                 continue;
91                         }
92                 }
93                 if (addrp > NADDRS)
94                         adios(EX_USAGE, NULL, "more than %d addresses", NADDRS);
95                 else
96                         addrs[addrp++] = cp;
97         }
98         addrs[addrp] = NULL;
99
100         if (addrp == 0)
101                 adios(EX_USAGE, NULL, "usage: %s [switches] addrs ...", invo_name);
102
103         /* get new format string */
104         fmtstr = new_fs(form, FORMAT);
105
106         fmt_norm = normalize;
107         fmt_compile(fmtstr, &fmt);
108
109         dat[0] = 0;
110         dat[1] = 0;
111         dat[2] = 0;
112         dat[3] = BUFSIZ;
113         dat[4] = 0;
114
115         for (addrp = 0; addrs[addrp]; addrp++)
116                 status += process(addrs[addrp], normalize);
117
118         return status;
119 }
120
121 struct pqpair {
122         char *pq_text;
123         char *pq_error;
124         struct pqpair *pq_next;
125 };
126
127
128 static int
129 process(char *arg, int norm)
130 {
131         int status = 0;
132         char *cp;
133         char buffer[BUFSIZ + 1], error[BUFSIZ];
134         struct comp *cptr;
135         struct pqpair *p, *q;
136         struct pqpair pq;
137         struct mailname *mp;
138
139         (q = &pq)->pq_next = NULL;
140         while ((cp = getname(arg))) {
141                 p = mh_xcalloc(1, sizeof(*p));
142                 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
143                         p->pq_text = mh_xstrdup(cp);
144                         p->pq_error = mh_xstrdup(error);
145                         status++;
146                 } else {
147                         p->pq_text = mh_xstrdup(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                 mh_free0(&(p->pq_text));
165                 if (p->pq_error)
166                         mh_free0(&(p->pq_error));
167                 q = p->pq_next;
168                 mh_free0(&p);
169         }
170
171         return status;
172 }