]> diplodocus.org Git - nmh/blob - uip/ap.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[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("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
28
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(AP);
31 #undef X
32
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(AP, switches);
35 #undef X
36
37 static struct format *fmt;
38
39 static int dat[5];
40
41 /*
42 * static prototypes
43 */
44 static int process (char *, int);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 int addrp = 0;
51 int width = 0, status = 0;
52 char *cp, *form = NULL, *format = NULL, *nfs;
53 char buf[BUFSIZ], **argp;
54 char **arguments, *addrs[NADDRS];
55
56 if (nmh_init(argv[0], 1)) { return 1; }
57
58 mts_init (invo_name);
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 done (1);
68
69 case UNKWNSW:
70 adios (NULL, "-%s unknown", cp);
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf), "%s [switches] addrs ...",
74 invo_name);
75 print_help (buf, switches, 1);
76 done (0);
77 case VERSIONSW:
78 print_version (invo_name);
79 done (0);
80
81 case FORMSW:
82 if (!(form = *argp++) || *form == '-')
83 adios (NULL, "missing argument to %s", argp[-2]);
84 format = NULL;
85 continue;
86 case FMTSW:
87 if (!(format = *argp++) || *format == '-')
88 adios (NULL, "missing argument to %s", argp[-2]);
89 form = NULL;
90 continue;
91
92 case WIDTHSW:
93 if (!(cp = *argp++) || *cp == '-')
94 adios (NULL, "missing argument to %s", argp[-2]);
95 width = atoi (cp);
96 continue;
97 }
98 }
99 if (addrp > NADDRS)
100 adios (NULL, "more than %d addresses", NADDRS);
101 else
102 addrs[addrp++] = cp;
103 }
104 addrs[addrp] = NULL;
105
106 if (addrp == 0)
107 adios (NULL, "usage: %s [switches] addrs ...", invo_name);
108
109 /* get new format string */
110 nfs = new_fs (form, format, FORMAT);
111
112 if (width == 0) {
113 if ((width = sc_width ()) < WIDTH / 2)
114 width = WIDTH / 2;
115 width -= 2;
116 }
117 if (width > WBUFSIZ)
118 width = WBUFSIZ;
119 fmt_compile (nfs, &fmt, 1);
120
121 dat[0] = 0;
122 dat[1] = 0;
123 dat[2] = 0;
124 dat[3] = width;
125 dat[4] = 0;
126
127 for (addrp = 0; addrs[addrp]; addrp++)
128 status += process (addrs[addrp], width);
129
130 fmt_free (fmt, 1);
131 done (status);
132 return 1;
133 }
134
135 struct pqpair {
136 char *pq_text;
137 char *pq_error;
138 struct pqpair *pq_next;
139 };
140
141
142 static int
143 process (char *arg, int length)
144 {
145 int status = 0;
146 register char *cp;
147 char buffer[WBUFSIZ + 1], error[BUFSIZ];
148 register struct comp *cptr;
149 register struct pqpair *p, *q;
150 struct pqpair pq;
151 register struct mailname *mp;
152
153 (q = &pq)->pq_next = NULL;
154 while ((cp = getname (arg))) {
155 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
156 adios (NULL, "unable to allocate pqpair memory");
157 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
158 p->pq_text = getcpy (cp);
159 p->pq_error = getcpy (error);
160 status++;
161 }
162 else {
163 p->pq_text = getcpy (mp->m_text);
164 mnfree (mp);
165 }
166 q = (q->pq_next = p);
167 }
168
169 for (p = pq.pq_next; p; p = q) {
170 cptr = fmt_findcomp ("text");
171 if (cptr) {
172 if (cptr->c_text)
173 free(cptr->c_text);
174 cptr->c_text = p->pq_text;
175 p->pq_text = NULL;
176 }
177 cptr = fmt_findcomp ("error");
178 if (cptr) {
179 if (cptr->c_text)
180 free(cptr->c_text);
181 cptr->c_text = p->pq_error;
182 p->pq_error = NULL;
183 }
184
185 fmt_scan (fmt, buffer, sizeof buffer - 1, length, dat, NULL);
186 fputs (buffer, stdout);
187
188 if (p->pq_text)
189 free (p->pq_text);
190 if (p->pq_error)
191 free (p->pq_error);
192 q = p->pq_next;
193 free ((char *) p);
194 }
195
196 return status;
197 }