]>
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/ambigsw.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/error.h"
13 #include "h/addrsbr.h"
14 #include "h/fmt_scan.h"
18 #include "sbr/terminal.h"
24 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
27 X("form formatfile", 0, FORMSW) \
28 X("format string", 5, FMTSW) \
29 X("width columns", 0, WIDTHSW) \
30 X("version", 0, VERSIONSW) \
31 X("help", 0, HELPSW) \
33 #define X(sw, minchars, id) id,
34 DEFINE_SWITCH_ENUM(AP
);
37 #define X(sw, minchars, id) { sw, minchars, id },
38 DEFINE_SWITCH_ARRAY(AP
, switches
);
41 static struct format
*fmt
;
48 static int process (char *, int);
52 main (int argc
, char **argv
)
55 int width
= -1, status
= 0;
56 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
57 char buf
[BUFSIZ
], **argp
;
58 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
60 if (nmh_init(argv
[0], true, false)) { return 1; }
64 argp
= getarguments (invo_name
, argc
, argv
, 1);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
73 die("-%s unknown", cp
);
76 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
78 print_help (buf
, switches
, 1);
81 print_version (invo_name
);
85 if (!(form
= *argp
++) || *form
== '-')
86 die("missing argument to %s", argp
[-2]);
90 if (!(format
= *argp
++) || *format
== '-')
91 die("missing argument to %s", argp
[-2]);
96 if (!(cp
= *argp
++) || *cp
== '-')
97 die("missing argument to %s", argp
[-2]);
103 die("more than %d addresses", NADDRS
);
109 die("usage: %s [switches] addrs ...", invo_name
);
111 /* get new format string */
112 nfs
= new_fs (form
, format
, FORMAT
);
115 if ((width
= sc_width ()) < WIDTH
/ 2) {
116 /* Default: width of the terminal, but at least WIDTH/2. */
120 } else if (width
== 0) {
121 /* Unlimited width. */
124 fmt_compile (nfs
, &fmt
, 1);
132 for (addrp
= 0; addrs
[addrp
]; addrp
++)
133 status
+= process (addrs
[addrp
], width
);
143 struct pqpair
*pq_next
;
148 process (char *arg
, int length
)
154 struct pqpair
*p
, *q
;
158 (q
= &pq
)->pq_next
= NULL
;
159 while ((cp
= getname (arg
))) {
161 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
162 p
->pq_text
= mh_xstrdup(cp
);
163 p
->pq_error
= mh_xstrdup(error
);
167 p
->pq_text
= getcpy (mp
->m_text
);
170 q
= (q
->pq_next
= p
);
173 for (p
= pq
.pq_next
; p
; p
= q
) {
175 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
177 cptr
= fmt_findcomp ("text");
180 cptr
->c_text
= p
->pq_text
;
183 cptr
= fmt_findcomp ("error");
186 cptr
->c_text
= p
->pq_error
;
190 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
191 fputs (charstring_buffer (scanl
), stdout
);
192 charstring_free (scanl
);