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