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