3 * mhlistsbr.c -- routines to list information about the
4 * -- contents of MIME messages
8 * This code is Copyright (c) 2002, by the authors of nmh. See the
9 * COPYRIGHT file in the root directory of the nmh distribution for
10 * complete copyright information.
15 #include <h/signals.h>
21 #include <h/mhparse.h>
25 int part_ok (CT, int);
26 int type_ok (CT, int);
27 void flush_errors (void);
32 void list_all_messages (CT *, int, int, int, int);
33 int list_switch (CT, int, int, int, int);
34 int list_content (CT, int, int, int, int);
39 static void list_single_message (CT, int, int, int);
40 static int list_debug (CT);
41 static int list_multi (CT, int, int, int, int);
42 static int list_partial (CT, int, int, int, int);
43 static int list_external (CT, int, int, int, int);
44 static int list_application (CT, int, int, int, int);
45 static int list_encoding (CT);
49 * various formats for -list option
51 #define LSTFMT1 "%4s %-5s %-24s %5s %-36s\n"
52 #define LSTFMT2a "%4d "
53 #define LSTFMT2b "%-5s %-24.24s "
54 #define LSTFMT2c1 "%5lu"
55 #define LSTFMT2c2 "%4lu%c"
56 #define LSTFMT2c3 "huge "
58 #define LSTFMT2d1 " %-36.36s"
59 #define LSTFMT2d2 "\t %-65.65s\n"
63 * Top level entry point to list group of messages
67 list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug)
72 printf (LSTFMT1, "msg", "part", "type/subtype", "size", "description");
74 for (ctp = cts; *ctp; ctp++) {
76 list_single_message (ct, realsize, verbose, debug);
84 * Entry point to list a single message
88 list_single_message (CT ct, int realsize, int verbose, int debug)
90 if (type_ok (ct, 1)) {
92 list_switch (ct, 1, realsize, verbose, debug);
98 (*ct->c_ceclosefnx) (ct);
104 * Primary switching routine to list information about a content
108 list_switch (CT ct, int toplevel, int realsize, int verbose, int debug)
110 switch (ct->c_type) {
112 return list_multi (ct, toplevel, realsize, verbose, debug);
116 switch (ct->c_subtype) {
117 case MESSAGE_PARTIAL:
118 return list_partial (ct, toplevel, realsize, verbose, debug);
121 case MESSAGE_EXTERNAL:
122 return list_external (ct, toplevel, realsize, verbose, debug);
127 return list_content (ct, toplevel, realsize, verbose, debug);
136 return list_content (ct, toplevel, realsize, verbose, debug);
140 return list_application (ct, toplevel, realsize, verbose, debug);
144 /* list_debug (ct); */
145 adios (NULL, "unknown content type %d", ct->c_type);
149 return 0; /* NOT REACHED */
153 #define empty(s) ((s) ? (s) : "")
156 * Method for listing information about a simple/generic content
160 list_content (CT ct, int toplevel, int realsize, int verbose, int debug)
163 char *cp, buffer[BUFSIZ];
164 CI ci = &ct->c_ctinfo;
166 printf (toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
167 atoi (r1bindex (empty (ct->c_file), '/')));
168 snprintf (buffer, sizeof(buffer), "%s/%s", empty (ci->ci_type),
169 empty (ci->ci_subtype));
170 printf (LSTFMT2b, empty (ct->c_partno), buffer);
172 if (ct->c_cesizefnx && realsize)
173 size = (*ct->c_cesizefnx) (ct);
175 size = ct->c_end - ct->c_begin;
177 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
178 for (cp = " KMGT"; size > 9999; size >>= 10)
182 /* print size of this body part */
185 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
186 printf (LSTFMT2c1, size);
192 printf (LSTFMT2c2, size, *cp);
199 /* print Content-Description */
203 dp = trimcpy (cp = add (ct->c_descr, NULL));
205 printf (LSTFMT2d1, dp);
212 * If verbose, print any RFC-822 comments in the
215 if (verbose && ci->ci_comment) {
218 dp = trimcpy (cp = add (ci->ci_comment, NULL));
220 snprintf (buffer, sizeof(buffer), "(%s)", dp);
222 printf (LSTFMT2d2, buffer);
233 * Print debugging information about a content
240 CI ci = &ct->c_ctinfo;
243 fprintf (stderr, " partno \"%s\"\n", empty (ct->c_partno));
245 /* print MIME-Version line */
247 fprintf (stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
249 /* print Content-Type line */
251 fprintf (stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
253 /* print parsed elements of content type */
254 fprintf (stderr, " type \"%s\"\n", empty (ci->ci_type));
255 fprintf (stderr, " subtype \"%s\"\n", empty (ci->ci_subtype));
256 fprintf (stderr, " comment \"%s\"\n", empty (ci->ci_comment));
257 fprintf (stderr, " magic \"%s\"\n", empty (ci->ci_magic));
259 /* print parsed parameters attached to content type */
260 fprintf (stderr, " parameters\n");
261 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
262 fprintf (stderr, " %s=\"%s\"\n", *ap, *ep);
264 /* print internal flags for type/subtype */
265 fprintf (stderr, " type 0x%x subtype 0x%x params 0x%x\n",
266 ct->c_type, ct->c_subtype,
267 (unsigned int)(unsigned long) ct->c_ctparams);
269 fprintf (stderr, " showproc \"%s\"\n", empty (ct->c_showproc));
270 fprintf (stderr, " termproc \"%s\"\n", empty (ct->c_termproc));
271 fprintf (stderr, " storeproc \"%s\"\n", empty (ct->c_storeproc));
273 /* print transfer encoding information */
275 fprintf (stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
277 /* print internal flags for transfer encoding */
278 fprintf (stderr, " transfer encoding 0x%x params 0x%x\n",
279 ct->c_encoding, (unsigned int)(unsigned long) ct->c_cefile);
281 /* print Content-ID */
283 fprintf (stderr, " %s:%s", ID_FIELD, ct->c_id);
285 /* print Content-Description */
287 fprintf (stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
289 fprintf (stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
290 (unsigned int)(unsigned long) ct->c_fp, empty (ct->c_file),
291 ct->c_begin, ct->c_end);
293 /* print more information about transfer encoding */
303 * list content information for type "multipart"
307 list_multi (CT ct, int toplevel, int realsize, int verbose, int debug)
309 struct multipart *m = (struct multipart *) ct->c_ctparams;
312 /* list the content for toplevel of this multipart */
313 list_content (ct, toplevel, realsize, verbose, debug);
315 /* now list for all the subparts */
316 for (part = m->mp_parts; part; part = part->mp_next) {
317 CT p = part->mp_part;
319 if (part_ok (p, 1) && type_ok (p, 1))
320 list_switch (p, 0, realsize, verbose, debug);
328 * list content information for type "message/partial"
332 list_partial (CT ct, int toplevel, int realsize, int verbose, int debug)
334 struct partial *p = (struct partial *) ct->c_ctparams;
336 list_content (ct, toplevel, realsize, verbose, debug);
338 printf ("\t [message %s, part %d", p->pm_partid, p->pm_partno);
340 printf (" of %d", p->pm_maxno);
349 * list content information for type "message/external"
353 list_external (CT ct, int toplevel, int realsize, int verbose, int debug)
355 struct exbody *e = (struct exbody *) ct->c_ctparams;
358 * First list the information for the
359 * message/external content itself.
361 list_content (ct, toplevel, realsize, verbose, debug);
365 printf ("\t name=\"%s\"\n", e->eb_name);
367 printf ("\t directory=\"%s\"\n", e->eb_dir);
369 printf ("\t site=\"%s\"\n", e->eb_site);
371 printf ("\t server=\"%s\"\n", e->eb_server);
373 printf ("\t subject=\"%s\"\n", e->eb_subject);
375 /* This must be defined */
376 printf ("\t access-type=\"%s\"\n", e->eb_access);
379 printf ("\t mode=\"%s\"\n", e->eb_mode);
380 if (e->eb_permission)
381 printf ("\t permission=\"%s\"\n", e->eb_permission);
383 if (e->eb_flags == NOTOK)
384 printf ("\t [service unavailable]\n");
388 * Now list the information for the external content
389 * to which this content points.
391 list_content (e->eb_content, 0, realsize, verbose, debug);
398 * list content information for type "application"
402 list_application (CT ct, int toplevel, int realsize, int verbose, int debug)
404 list_content (ct, toplevel, realsize, verbose, debug);
407 CI ci = &ct->c_ctinfo;
409 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
410 printf ("\t %s=\"%s\"\n", *ap, *ep);
418 * list information about the Content-Transfer-Encoding
423 list_encoding (CT ct)
427 if ((ce = ct->c_cefile))
428 fprintf (stderr, " decoded fp 0x%x file \"%s\"\n",
429 (unsigned int)(unsigned long) ce->ce_fp,
430 ce->ce_file ? ce->ce_file : "");