]>
diplodocus.org Git - nmh/blob - uip/ap.c
3 * ap.c -- parse addresses 822-style
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
20 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("width columns", 0, WIDTHSW) \
26 X("version", 0, VERSIONSW) \
27 X("help", 0, HELPSW) \
29 #define X(sw, minchars, id) id,
30 DEFINE_SWITCH_ENUM(AP
);
33 #define X(sw, minchars, id) { sw, minchars, id },
34 DEFINE_SWITCH_ARRAY(AP
, switches
);
37 static struct format
*fmt
;
44 static int process (char *, int);
48 main (int argc
, char **argv
)
51 int width
= -1, status
= 0;
52 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
53 char buf
[BUFSIZ
], **argp
;
54 char *addrs
[NADDRS
+ 1]; /* Includes terminating NULL. */
56 if (nmh_init(argv
[0], 2)) { return 1; }
60 argp
= getarguments (invo_name
, argc
, argv
, 1);
61 while ((cp
= *argp
++)) {
63 switch (smatch (++cp
, switches
)) {
65 ambigsw (cp
, switches
);
69 adios (NULL
, "-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [switches] addrs ...",
74 print_help (buf
, switches
, 1);
77 print_version (invo_name
);
81 if (!(form
= *argp
++) || *form
== '-')
82 adios (NULL
, "missing argument to %s", argp
[-2]);
86 if (!(format
= *argp
++) || *format
== '-')
87 adios (NULL
, "missing argument to %s", argp
[-2]);
92 if (!(cp
= *argp
++) || *cp
== '-')
93 adios (NULL
, "missing argument to %s", argp
[-2]);
99 adios (NULL
, "more than %d addresses", NADDRS
);
106 adios (NULL
, "usage: %s [switches] addrs ...", invo_name
);
108 /* get new format string */
109 nfs
= new_fs (form
, format
, FORMAT
);
112 if ((width
= sc_width ()) < WIDTH
/ 2) {
113 /* Default: width of the terminal, but at least WIDTH/2. */
117 } else if (width
== 0) {
118 /* Unlimited width. */
121 fmt_compile (nfs
, &fmt
, 1);
129 for (addrp
= 0; addrs
[addrp
]; addrp
++)
130 status
+= process (addrs
[addrp
], width
);
140 struct pqpair
*pq_next
;
145 process (char *arg
, int length
)
151 struct pqpair
*p
, *q
;
155 (q
= &pq
)->pq_next
= NULL
;
156 while ((cp
= getname (arg
))) {
158 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
159 p
->pq_text
= mh_xstrdup(cp
);
160 p
->pq_error
= mh_xstrdup(error
);
164 p
->pq_text
= getcpy (mp
->m_text
);
167 q
= (q
->pq_next
= p
);
170 for (p
= pq
.pq_next
; p
; p
= q
) {
172 charstring_create (length
< NMH_BUFSIZ
? length
: NMH_BUFSIZ
);
174 cptr
= fmt_findcomp ("text");
176 mh_xfree(cptr
->c_text
);
177 cptr
->c_text
= p
->pq_text
;
180 cptr
= fmt_findcomp ("error");
182 mh_xfree(cptr
->c_text
);
183 cptr
->c_text
= p
->pq_error
;
187 fmt_scan (fmt
, scanl
, length
, dat
, NULL
);
188 fputs (charstring_buffer (scanl
), stdout
);
189 charstring_free (scanl
);
191 mh_xfree(p
->pq_text
);
192 mh_xfree(p
->pq_error
);