]>
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/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
12 #include "h/addrsbr.h"
13 #include "h/fmt_scan.h"
17 #include "sbr/terminal.h"
23 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
26 X("form formatfile", 0, FORMSW) \
27 X("format string", 5, FMTSW) \
28 X("width columns", 0, WIDTHSW) \
29 X("version", 0, VERSIONSW) \
30 X("help", 0, HELPSW) \
32 #define X(sw, minchars, id) id,
33 DEFINE_SWITCH_ENUM(AP
);
36 #define X(sw, minchars, id) { sw, minchars, id },
37 DEFINE_SWITCH_ARRAY(AP
, switches
);
40 static struct format
*fmt
;
47 static int process (char *, int);
51 main (int argc
, char **argv
)
54 int width
= -1, status
= 0;
55 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
56 char buf
[BUFSIZ
], **argp
;
57 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
59 if (nmh_init(argv
[0], true, false)) { return 1; }
63 argp
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
72 die("-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
77 print_help (buf
, switches
, 1);
80 print_version (invo_name
);
84 if (!(form
= *argp
++) || *form
== '-')
85 die("missing argument to %s", argp
[-2]);
89 if (!(format
= *argp
++) || *format
== '-')
90 die("missing argument to %s", argp
[-2]);
95 if (!(cp
= *argp
++) || *cp
== '-')
96 die("missing argument to %s", argp
[-2]);
102 die("more than %d addresses", NADDRS
);
108 die("usage: %s [switches] addrs ...", invo_name
);
110 /* get new format string */
111 nfs
= new_fs (form
, format
, FORMAT
);
114 if ((width
= sc_width ()) < WIDTH
/ 2) {
115 /* Default: width of the terminal, but at least WIDTH/2. */
119 } else if (width
== 0) {
120 /* Unlimited width. */
123 fmt_compile (nfs
, &fmt
, 1);
131 for (addrp
= 0; addrs
[addrp
]; addrp
++)
132 status
+= process (addrs
[addrp
], width
);
142 struct pqpair
*pq_next
;
147 process (char *arg
, int length
)
153 struct pqpair
*p
, *q
;
157 (q
= &pq
)->pq_next
= NULL
;
158 while ((cp
= getname (arg
))) {
160 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
161 p
->pq_text
= mh_xstrdup(cp
);
162 p
->pq_error
= mh_xstrdup(error
);
166 p
->pq_text
= getcpy (mp
->m_text
);
169 q
= (q
->pq_next
= p
);
172 for (p
= pq
.pq_next
; p
; p
= q
) {
174 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
176 cptr
= fmt_findcomp ("text");
179 cptr
->c_text
= p
->pq_text
;
182 cptr
= fmt_findcomp ("error");
185 cptr
->c_text
= p
->pq_error
;
189 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
190 fputs (charstring_buffer (scanl
), stdout
);
191 charstring_free (scanl
);