]>
diplodocus.org Git - nmh/blob - uip/ap.c
e33a434f6d72d84998699a38b973e7e229995626
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_help.h"
10 #include "sbr/error.h"
11 #include "h/addrsbr.h"
12 #include "h/fmt_scan.h"
16 #include "sbr/terminal.h"
22 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
25 X("form formatfile", 0, FORMSW) \
26 X("format string", 5, FMTSW) \
27 X("width columns", 0, WIDTHSW) \
28 X("version", 0, VERSIONSW) \
29 X("help", 0, HELPSW) \
31 #define X(sw, minchars, id) id,
32 DEFINE_SWITCH_ENUM(AP
);
35 #define X(sw, minchars, id) { sw, minchars, id },
36 DEFINE_SWITCH_ARRAY(AP
, switches
);
39 static struct format
*fmt
;
46 static int process (char *, int);
50 main (int argc
, char **argv
)
53 int width
= -1, status
= 0;
54 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
55 char buf
[BUFSIZ
], **argp
;
56 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
58 if (nmh_init(argv
[0], true, false)) { return 1; }
62 argp
= getarguments (invo_name
, argc
, argv
, 1);
63 while ((cp
= *argp
++)) {
65 switch (smatch (++cp
, switches
)) {
67 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
76 print_help (buf
, switches
, 1);
79 print_version (invo_name
);
83 if (!(form
= *argp
++) || *form
== '-')
84 die("missing argument to %s", argp
[-2]);
88 if (!(format
= *argp
++) || *format
== '-')
89 die("missing argument to %s", argp
[-2]);
94 if (!(cp
= *argp
++) || *cp
== '-')
95 die("missing argument to %s", argp
[-2]);
101 die("more than %d addresses", NADDRS
);
107 die("usage: %s [switches] addrs ...", invo_name
);
109 /* get new format string */
110 nfs
= new_fs (form
, format
, FORMAT
);
113 if ((width
= sc_width ()) < WIDTH
/ 2) {
114 /* Default: width of the terminal, but at least WIDTH/2. */
118 } else if (width
== 0) {
119 /* Unlimited width. */
122 fmt_compile (nfs
, &fmt
, 1);
130 for (addrp
= 0; addrs
[addrp
]; addrp
++)
131 status
+= process (addrs
[addrp
], width
);
141 struct pqpair
*pq_next
;
146 process (char *arg
, int length
)
152 struct pqpair
*p
, *q
;
156 (q
= &pq
)->pq_next
= NULL
;
157 while ((cp
= getname (arg
))) {
159 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
160 p
->pq_text
= mh_xstrdup(cp
);
161 p
->pq_error
= mh_xstrdup(error
);
165 p
->pq_text
= getcpy (mp
->m_text
);
168 q
= (q
->pq_next
= p
);
171 for (p
= pq
.pq_next
; p
; p
= q
) {
173 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
175 cptr
= fmt_findcomp ("text");
178 cptr
->c_text
= p
->pq_text
;
181 cptr
= fmt_findcomp ("error");
184 cptr
->c_text
= p
->pq_error
;
188 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
189 fputs (charstring_buffer (scanl
), stdout
);
190 charstring_free (scanl
);