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