2 ** mhlistsbr.c -- routines to list information about the
3 ** -- contents of MIME messages
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.
12 #include <h/signals.h>
17 #include <h/mhparse.h>
23 void flush_errors(void);
28 void list_all_messages(CT *, int, int, int);
29 int list_switch(CT, int, int, int);
30 int list_content(CT, int, int, int);
35 static void list_single_message(CT, int, int);
36 static int list_debug(CT);
37 static int list_multi(CT, int, int, int);
38 static int list_partial(CT, int, int, int);
39 static int list_encoding(CT);
43 ** various formats for -list option
45 #define LSTFMT1 "%4s %-5s %-24s %5s %-36s\n"
46 #define LSTFMT2a "%4d "
47 #define LSTFMT2b "%-5s %-24.24s "
48 #define LSTFMT2c1 "%5lu"
49 #define LSTFMT2c2 "%4lu%c"
50 #define LSTFMT2c3 "huge "
52 #define LSTFMT2d1 " %-36.36s"
53 #define LSTFMT2d2 "\t %-65.65s\n"
57 ** Top level entry point to list group of messages
60 list_all_messages(CT *cts, int headers, int verbose, int debug)
65 printf(LSTFMT1, "msg", "part", "type/subtype", "size",
68 for (ctp = cts; *ctp; ctp++) {
70 list_single_message(ct, verbose, debug);
78 ** Entry point to list a single message
81 list_single_message(CT ct, int verbose, int debug)
85 list_switch(ct, 1, verbose, debug);
91 (*ct->c_ceclosefnx) (ct);
97 ** Primary switching routine to list information about a content
100 list_switch(CT ct, int toplevel, int verbose, int debug)
102 switch (ct->c_type) {
104 return list_multi(ct, toplevel, verbose, debug);
108 if (ct->c_subtype == MESSAGE_PARTIAL) {
109 return list_partial(ct, toplevel, verbose, debug);
111 return list_content(ct, toplevel, verbose, debug);
120 return list_content(ct, toplevel, verbose, debug);
124 /* list_debug (ct); */
125 adios(NULL, "unknown content type %d", ct->c_type);
129 return 0; /* NOT REACHED */
133 #define empty(s) ((s) ? (s) : "")
136 ** Method for listing information about a simple/generic content
139 list_content(CT ct, int toplevel, int verbose, int debug)
142 char *cp, buffer[BUFSIZ];
143 CI ci = &ct->c_ctinfo;
145 printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
146 atoi(mhbasename(empty(ct->c_file))));
147 snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
148 empty(ci->ci_subtype));
149 printf(LSTFMT2b, empty(ct->c_partno), buffer);
152 size = (*ct->c_cesizefnx) (ct);
154 size = ct->c_end - ct->c_begin;
156 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
157 for (cp = " KMGT"; size > 9999; size >>= 10)
161 /* print size of this body part */
164 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
165 printf(LSTFMT2c1, size);
171 printf(LSTFMT2c2, size, *cp);
178 /* print Content-Description */
182 dp = trimcpy(cp = getcpy(ct->c_descr));
184 printf(LSTFMT2d1, dp);
192 CI ci = &ct->c_ctinfo;
194 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
195 printf("\t\t%s=\"%s\"\n", *ap, *ep);
199 ** If verbose, print any RFC-822 comments in the
200 ** Content-Type line.
202 if (ci->ci_comment) {
205 dp = trimcpy(cp = add(ci->ci_comment, NULL));
207 snprintf(buffer, sizeof(buffer), "(%s)", dp);
209 printf(LSTFMT2d2, buffer);
221 ** Print debugging information about a content
227 CI ci = &ct->c_ctinfo;
230 fprintf(stderr, " partno \"%s\"\n", empty(ct->c_partno));
232 /* print MIME-Version line */
234 fprintf(stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
236 /* print Content-Type line */
238 fprintf(stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
240 /* print parsed elements of content type */
241 fprintf(stderr, " type \"%s\"\n", empty(ci->ci_type));
242 fprintf(stderr, " subtype \"%s\"\n", empty(ci->ci_subtype));
243 fprintf(stderr, " comment \"%s\"\n", empty(ci->ci_comment));
244 fprintf(stderr, " magic \"%s\"\n", empty(ci->ci_magic));
246 /* print parsed parameters attached to content type */
247 fprintf(stderr, " parameters\n");
248 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
249 fprintf(stderr, " %s=\"%s\"\n", *ap, *ep);
251 /* print internal flags for type/subtype */
252 fprintf(stderr, " type 0x%x subtype 0x%x params 0x%x\n",
253 ct->c_type, ct->c_subtype,
254 (unsigned int)(unsigned long) ct->c_ctparams);
256 fprintf(stderr, " charset \"%s\"\n", empty(ct->c_charset));
257 fprintf(stderr, " showproc \"%s\"\n", empty(ct->c_showproc));
258 fprintf(stderr, " storeproc \"%s\"\n", empty(ct->c_storeproc));
260 /* print transfer encoding information */
262 fprintf(stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
264 /* print internal flags for transfer encoding */
265 fprintf(stderr, " transfer encoding 0x%x params 0x%x\n",
267 (unsigned int)(unsigned long) ct->c_cefile);
269 /* print Content-ID */
271 fprintf(stderr, " %s:%s", ID_FIELD, ct->c_id);
273 /* print Content-Description */
275 fprintf(stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
277 fprintf(stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
278 (unsigned int)(unsigned long) ct->c_fp,
279 empty(ct->c_file), ct->c_begin, ct->c_end);
281 /* print more information about transfer encoding */
291 ** list content information for type "multipart"
294 list_multi(CT ct, int toplevel, int verbose, int debug)
296 struct multipart *m = (struct multipart *) ct->c_ctparams;
299 /* list the content for toplevel of this multipart */
300 list_content(ct, toplevel, verbose, debug);
302 /* now list for all the subparts */
303 for (part = m->mp_parts; part; part = part->mp_next) {
304 CT p = part->mp_part;
306 if (part_ok(p, 1) && type_ok(p, 1))
307 list_switch(p, 0, verbose, debug);
315 ** list content information for type "message/partial"
318 list_partial(CT ct, int toplevel, int verbose, int debug)
320 struct partial *p = (struct partial *) ct->c_ctparams;
322 list_content(ct, toplevel, verbose, debug);
324 printf("\t [message %s, part %d",
325 p->pm_partid, p->pm_partno);
327 printf(" of %d", p->pm_maxno);
336 ** list information about the Content-Transfer-Encoding
337 ** used by a content.
344 if ((ce = ct->c_cefile))
345 fprintf(stderr, " decoded fp 0x%x file \"%s\"\n",
346 (unsigned int)(unsigned long) ce->ce_fp,
347 ce->ce_file ? ce->ce_file : "");