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>
18 #include <h/mhparse.h>
24 void flush_errors(void);
29 void list_all_messages(CT *, int, int, int, int);
30 int list_switch(CT, int, int, int, int);
31 int list_content(CT, int, int, int, int);
36 static void list_single_message(CT, int, int, int);
37 static int list_debug(CT);
38 static int list_multi(CT, int, int, int, int);
39 static int list_partial(CT, int, int, int, int);
40 static int list_external(CT, int, int, int, int);
41 static int list_application(CT, int, int, int, int);
42 static int list_encoding(CT);
46 ** various formats for -list option
48 #define LSTFMT1 "%4s %-5s %-24s %5s %-36s\n"
49 #define LSTFMT2a "%4d "
50 #define LSTFMT2b "%-5s %-24.24s "
51 #define LSTFMT2c1 "%5lu"
52 #define LSTFMT2c2 "%4lu%c"
53 #define LSTFMT2c3 "huge "
55 #define LSTFMT2d1 " %-36.36s"
56 #define LSTFMT2d2 "\t %-65.65s\n"
60 ** Top level entry point to list group of messages
64 list_all_messages(CT *cts, int headers, int realsize, int verbose, int debug)
69 printf(LSTFMT1, "msg", "part", "type/subtype", "size",
72 for (ctp = cts; *ctp; ctp++) {
74 list_single_message(ct, realsize, verbose, debug);
82 ** Entry point to list a single message
86 list_single_message(CT ct, int realsize, int verbose, int debug)
90 list_switch(ct, 1, realsize, verbose, debug);
96 (*ct->c_ceclosefnx) (ct);
102 ** Primary switching routine to list information about a content
106 list_switch(CT ct, int toplevel, int realsize, int verbose, int debug)
108 switch (ct->c_type) {
110 return list_multi(ct, toplevel, realsize, verbose, debug);
114 switch (ct->c_subtype) {
115 case MESSAGE_PARTIAL:
116 return list_partial(ct, toplevel, realsize, verbose,
120 case MESSAGE_EXTERNAL:
121 return list_external(ct, toplevel, realsize, verbose,
127 return list_content(ct, toplevel, realsize, verbose,
137 return list_content(ct, toplevel, realsize, verbose, debug);
141 return list_application(ct, toplevel, realsize, verbose,
146 /* list_debug (ct); */
147 adios(NULL, "unknown content type %d", ct->c_type);
151 return 0; /* NOT REACHED */
155 #define empty(s) ((s) ? (s) : "")
158 ** Method for listing information about a simple/generic content
162 list_content(CT ct, int toplevel, int realsize, int verbose, int debug)
165 char *cp, buffer[BUFSIZ];
166 CI ci = &ct->c_ctinfo;
168 printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
169 atoi(mhbasename(empty(ct->c_file))));
170 snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
171 empty(ci->ci_subtype));
172 printf(LSTFMT2b, empty(ct->c_partno), buffer);
174 if (ct->c_cesizefnx && realsize)
175 size = (*ct->c_cesizefnx) (ct);
177 size = ct->c_end - ct->c_begin;
179 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
180 for (cp = " KMGT"; size > 9999; size >>= 10)
184 /* print size of this body part */
187 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
188 printf(LSTFMT2c1, size);
194 printf(LSTFMT2c2, size, *cp);
201 /* print Content-Description */
205 dp = trimcpy(cp = getcpy(ct->c_descr));
207 printf(LSTFMT2d1, dp);
214 ** If verbose, print any RFC-822 comments in the
215 ** Content-Type line.
217 if (verbose && ci->ci_comment) {
220 dp = trimcpy(cp = getcpy(ci->ci_comment));
222 snprintf(buffer, sizeof(buffer), "(%s)", dp);
224 printf(LSTFMT2d2, buffer);
235 ** Print debugging information about a content
242 CI ci = &ct->c_ctinfo;
245 fprintf(stderr, " partno \"%s\"\n", empty(ct->c_partno));
247 /* print MIME-Version line */
249 fprintf(stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
251 /* print Content-Type line */
253 fprintf(stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
255 /* print parsed elements of content type */
256 fprintf(stderr, " type \"%s\"\n", empty(ci->ci_type));
257 fprintf(stderr, " subtype \"%s\"\n", empty(ci->ci_subtype));
258 fprintf(stderr, " comment \"%s\"\n", empty(ci->ci_comment));
259 fprintf(stderr, " magic \"%s\"\n", empty(ci->ci_magic));
261 /* print parsed parameters attached to content type */
262 fprintf(stderr, " parameters\n");
263 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
264 fprintf(stderr, " %s=\"%s\"\n", *ap, *ep);
266 /* print internal flags for type/subtype */
267 fprintf(stderr, " type 0x%x subtype 0x%x params 0x%x\n",
268 ct->c_type, ct->c_subtype,
269 (unsigned int)(unsigned long) ct->c_ctparams);
271 fprintf(stderr, " showproc \"%s\"\n", empty(ct->c_showproc));
272 fprintf(stderr, " termproc \"%s\"\n", empty(ct->c_termproc));
273 fprintf(stderr, " storeproc \"%s\"\n", empty(ct->c_storeproc));
275 /* print transfer encoding information */
277 fprintf(stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
279 /* print internal flags for transfer encoding */
280 fprintf(stderr, " transfer encoding 0x%x params 0x%x\n",
282 (unsigned int)(unsigned long) ct->c_cefile);
284 /* print Content-ID */
286 fprintf(stderr, " %s:%s", ID_FIELD, ct->c_id);
288 /* print Content-Description */
290 fprintf(stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
292 fprintf(stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
293 (unsigned int)(unsigned long) ct->c_fp,
294 empty(ct->c_file), ct->c_begin, ct->c_end);
296 /* print more information about transfer encoding */
306 ** list content information for type "multipart"
310 list_multi(CT ct, int toplevel, int realsize, int verbose, int debug)
312 struct multipart *m = (struct multipart *) ct->c_ctparams;
315 /* list the content for toplevel of this multipart */
316 list_content(ct, toplevel, realsize, verbose, debug);
318 /* now list for all the subparts */
319 for (part = m->mp_parts; part; part = part->mp_next) {
320 CT p = part->mp_part;
322 if (part_ok(p, 1) && type_ok(p, 1))
323 list_switch(p, 0, realsize, verbose, debug);
331 ** list content information for type "message/partial"
335 list_partial(CT ct, int toplevel, int realsize, int verbose, int debug)
337 struct partial *p = (struct partial *) ct->c_ctparams;
339 list_content(ct, toplevel, realsize, verbose, debug);
341 printf("\t [message %s, part %d",
342 p->pm_partid, p->pm_partno);
344 printf(" of %d", p->pm_maxno);
353 ** list content information for type "message/external"
357 list_external(CT ct, int toplevel, int realsize, int verbose, int debug)
359 struct exbody *e = (struct exbody *) ct->c_ctparams;
362 * First list the information for the
363 * message/external content itself.
365 list_content(ct, toplevel, realsize, verbose, debug);
369 printf("\t name=\"%s\"\n", e->eb_name);
371 printf("\t directory=\"%s\"\n", e->eb_dir);
373 printf("\t site=\"%s\"\n", e->eb_site);
375 printf("\t server=\"%s\"\n", e->eb_server);
377 printf("\t subject=\"%s\"\n", e->eb_subject);
379 /* This must be defined */
380 printf("\t access-type=\"%s\"\n", e->eb_access);
383 printf("\t mode=\"%s\"\n", e->eb_mode);
384 if (e->eb_permission)
385 printf("\t permission=\"%s\"\n", e->eb_permission);
387 if (e->eb_flags == NOTOK)
388 printf("\t [service unavailable]\n");
392 ** Now list the information for the external content
393 ** to which this content points.
395 list_content(e->eb_content, 0, realsize, verbose, debug);
402 ** list content information for type "application"
406 list_application(CT ct, int toplevel, int realsize, int verbose, int debug)
408 list_content(ct, toplevel, realsize, verbose, debug);
411 CI ci = &ct->c_ctinfo;
413 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
414 printf("\t %s=\"%s\"\n", *ap, *ep);
422 ** list information about the Content-Transfer-Encoding
423 ** used by a content.
431 if ((ce = ct->c_cefile))
432 fprintf(stderr, " decoded fp 0x%x file \"%s\"\n",
433 (unsigned int)(unsigned long) ce->ce_fp,
434 ce->ce_file ? ce->ce_file : "");