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, int);
29 int list_switch(CT, int, int, int, int);
30 int list_content(CT, int, int, int, int);
35 static void list_single_message(CT, int, int, int);
36 static int list_debug(CT);
37 static int list_multi(CT, int, int, int, int);
38 static int list_partial(CT, int, 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 realsize, int verbose, int debug)
65 printf(LSTFMT1, "msg", "part", "type/subtype", "size",
68 for (ctp = cts; *ctp; ctp++) {
70 list_single_message(ct, realsize, verbose, debug);
78 ** Entry point to list a single message
81 list_single_message(CT ct, int realsize, int verbose, int debug)
85 list_switch(ct, 1, realsize, 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 realsize, int verbose, int debug)
102 switch (ct->c_type) {
104 return list_multi(ct, toplevel, realsize, verbose, debug);
108 if (ct->c_subtype == MESSAGE_PARTIAL) {
109 return list_partial(ct, toplevel, realsize, verbose,
112 return list_content(ct, toplevel, realsize, verbose,
122 return list_content(ct, toplevel, realsize, verbose, debug);
126 /* list_debug (ct); */
127 adios(NULL, "unknown content type %d", ct->c_type);
131 return 0; /* NOT REACHED */
135 #define empty(s) ((s) ? (s) : "")
138 ** Method for listing information about a simple/generic content
141 list_content(CT ct, int toplevel, int realsize, int verbose, int debug)
144 char *cp, buffer[BUFSIZ];
145 CI ci = &ct->c_ctinfo;
147 printf(toplevel > 0 ? LSTFMT2a : toplevel < 0 ? "part " : " ",
148 atoi(mhbasename(empty(ct->c_file))));
149 snprintf(buffer, sizeof(buffer), "%s/%s", empty(ci->ci_type),
150 empty(ci->ci_subtype));
151 printf(LSTFMT2b, empty(ct->c_partno), buffer);
153 if (ct->c_cesizefnx && realsize)
154 size = (*ct->c_cesizefnx) (ct);
156 size = ct->c_end - ct->c_begin;
158 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
159 for (cp = " KMGT"; size > 9999; size >>= 10)
163 /* print size of this body part */
166 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
167 printf(LSTFMT2c1, size);
173 printf(LSTFMT2c2, size, *cp);
180 /* print Content-Description */
184 dp = trimcpy(cp = getcpy(ct->c_descr));
186 printf(LSTFMT2d1, dp);
194 CI ci = &ct->c_ctinfo;
196 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
197 printf("\t\t%s=\"%s\"\n", *ap, *ep);
201 ** If verbose, print any RFC-822 comments in the
202 ** Content-Type line.
204 if (ci->ci_comment) {
207 dp = trimcpy(cp = add(ci->ci_comment, NULL));
209 snprintf(buffer, sizeof(buffer), "(%s)", dp);
211 printf(LSTFMT2d2, buffer);
223 ** Print debugging information about a content
229 CI ci = &ct->c_ctinfo;
232 fprintf(stderr, " partno \"%s\"\n", empty(ct->c_partno));
234 /* print MIME-Version line */
236 fprintf(stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
238 /* print Content-Type line */
240 fprintf(stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
242 /* print parsed elements of content type */
243 fprintf(stderr, " type \"%s\"\n", empty(ci->ci_type));
244 fprintf(stderr, " subtype \"%s\"\n", empty(ci->ci_subtype));
245 fprintf(stderr, " comment \"%s\"\n", empty(ci->ci_comment));
246 fprintf(stderr, " magic \"%s\"\n", empty(ci->ci_magic));
248 /* print parsed parameters attached to content type */
249 fprintf(stderr, " parameters\n");
250 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
251 fprintf(stderr, " %s=\"%s\"\n", *ap, *ep);
253 /* print internal flags for type/subtype */
254 fprintf(stderr, " type 0x%x subtype 0x%x params 0x%x\n",
255 ct->c_type, ct->c_subtype,
256 (unsigned int)(unsigned long) ct->c_ctparams);
258 fprintf(stderr, " charset \"%s\"\n", empty(ct->c_charset));
259 fprintf(stderr, " showproc \"%s\"\n", empty(ct->c_showproc));
260 fprintf(stderr, " storeproc \"%s\"\n", empty(ct->c_storeproc));
262 /* print transfer encoding information */
264 fprintf(stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
266 /* print internal flags for transfer encoding */
267 fprintf(stderr, " transfer encoding 0x%x params 0x%x\n",
269 (unsigned int)(unsigned long) ct->c_cefile);
271 /* print Content-ID */
273 fprintf(stderr, " %s:%s", ID_FIELD, ct->c_id);
275 /* print Content-Description */
277 fprintf(stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
279 fprintf(stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
280 (unsigned int)(unsigned long) ct->c_fp,
281 empty(ct->c_file), ct->c_begin, ct->c_end);
283 /* print more information about transfer encoding */
293 ** list content information for type "multipart"
296 list_multi(CT ct, int toplevel, int realsize, int verbose, int debug)
298 struct multipart *m = (struct multipart *) ct->c_ctparams;
301 /* list the content for toplevel of this multipart */
302 list_content(ct, toplevel, realsize, verbose, debug);
304 /* now list for all the subparts */
305 for (part = m->mp_parts; part; part = part->mp_next) {
306 CT p = part->mp_part;
308 if (part_ok(p, 1) && type_ok(p, 1))
309 list_switch(p, 0, realsize, verbose, debug);
317 ** list content information for type "message/partial"
320 list_partial(CT ct, int toplevel, int realsize, int verbose, int debug)
322 struct partial *p = (struct partial *) ct->c_ctparams;
324 list_content(ct, toplevel, realsize, verbose, debug);
326 printf("\t [message %s, part %d",
327 p->pm_partid, p->pm_partno);
329 printf(" of %d", p->pm_maxno);
338 ** list information about the Content-Transfer-Encoding
339 ** used by a content.
346 if ((ce = ct->c_cefile))
347 fprintf(stderr, " decoded fp 0x%x file \"%s\"\n",
348 (unsigned int)(unsigned long) ce->ce_fp,
349 ce->ce_file ? ce->ce_file : "");