]>
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>
19 #define FORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
22 X("form formatfile", 0, FORMSW) \
23 X("format string", 5, FMTSW) \
24 X("width columns", 0, WIDTHSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(AP
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(AP
, switches
);
36 static struct format
*fmt
;
43 static int process (char *, int);
47 main (int argc
, char **argv
)
50 int width
= -1, status
= 0;
51 char *cp
, *form
= NULL
, *format
= NULL
, *nfs
;
52 char buf
[BUFSIZ
], **argp
;
53 char **arguments
, *addrs
[NADDRS
];
55 if (nmh_init(argv
[0], 1)) { return 1; }
58 arguments
= 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. */
115 } else if (width
== 0) {
116 /* 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
)
150 register struct comp
*cptr
;
151 register struct pqpair
*p
, *q
;
153 register struct mailname
*mp
;
155 (q
= &pq
)->pq_next
= NULL
;
156 while ((cp
= getname (arg
))) {
157 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
158 adios (NULL
, "unable to allocate pqpair memory");
159 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
160 p
->pq_text
= getcpy (cp
);
161 p
->pq_error
= getcpy (error
);
165 p
->pq_text
= getcpy (mp
->m_text
);
168 q
= (q
->pq_next
= p
);
171 for (p
= pq
.pq_next
; p
; p
= q
) {
172 charstring_t scanl
= charstring_create (length
);
174 cptr
= fmt_findcomp ("text");
178 cptr
->c_text
= p
->pq_text
;
181 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
);