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