]>
diplodocus.org Git - nmh/blob - uip/ap.c
1 /* ap.c -- parse addresses 822-style
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.
9 #include "sbr/getarguments.h"
10 #include "sbr/smatch.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/error.h"
16 #include "h/addrsbr.h"
17 #include "h/fmt_scan.h"
21 #include "sbr/terminal.h"
27 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
30 X("form formatfile", 0, FORMSW) \
31 X("format string", 5, FMTSW) \
32 X("width columns", 0, WIDTHSW) \
33 X("version", 0, VERSIONSW) \
34 X("help", 0, HELPSW) \
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(AP
);
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(AP
, switches
);
44 static struct format
*fmt
;
51 static int process (char *, int);
55 main (int argc
, char **argv
)
58 int width
= -1, status
= 0;
59 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
60 char buf
[BUFSIZ
], **argp
;
61 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
63 if (nmh_init(argv
[0], true, false)) { return 1; }
67 argp
= getarguments (invo_name
, argc
, argv
, 1);
68 while ((cp
= *argp
++)) {
70 switch (smatch (++cp
, switches
)) {
72 ambigsw (cp
, switches
);
76 die("-%s unknown", cp
);
79 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
81 print_help (buf
, switches
, 1);
84 print_version (invo_name
);
88 if (!(form
= *argp
++) || *form
== '-')
89 die("missing argument to %s", argp
[-2]);
93 if (!(format
= *argp
++) || *format
== '-')
94 die("missing argument to %s", argp
[-2]);
99 if (!(cp
= *argp
++) || *cp
== '-')
100 die("missing argument to %s", argp
[-2]);
106 die("more than %d addresses", NADDRS
);
112 die("usage: %s [switches] addrs ...", invo_name
);
114 /* get new format string */
115 nfs
= new_fs (form
, format
, FORMAT
);
118 if ((width
= sc_width ()) < WIDTH
/ 2) {
119 /* Default: width of the terminal, but at least WIDTH/2. */
123 } else if (width
== 0) {
124 /* Unlimited width. */
127 fmt_compile (nfs
, &fmt
, 1);
135 for (addrp
= 0; addrs
[addrp
]; addrp
++)
136 status
+= process (addrs
[addrp
], width
);
146 struct pqpair
*pq_next
;
151 process (char *arg
, int length
)
157 struct pqpair
*p
, *q
;
161 (q
= &pq
)->pq_next
= NULL
;
162 while ((cp
= getname (arg
))) {
164 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
165 p
->pq_text
= mh_xstrdup(cp
);
166 p
->pq_error
= mh_xstrdup(error
);
170 p
->pq_text
= getcpy (mp
->m_text
);
173 q
= (q
->pq_next
= p
);
176 for (p
= pq
.pq_next
; p
; p
= q
) {
178 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
180 cptr
= fmt_findcomp ("text");
183 cptr
->c_text
= p
->pq_text
;
186 cptr
= fmt_findcomp ("error");
189 cptr
->c_text
= p
->pq_error
;
193 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
194 fputs (charstring_buffer (scanl
), stdout
);
195 charstring_free (scanl
);