]> diplodocus.org Git - nmh/blob - uip/mhlistsbr.c
Try -v with SunStudio cc. Also, try setting CC instead of modifying
[nmh] / uip / mhlistsbr.c
1
2 /*
3 * mhlistsbr.c -- routines to list information about the
4 * -- contents of MIME messages
5 *
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
9 */
10
11 #include <h/mh.h>
12 #include <fcntl.h>
13 #include <h/signals.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/utils.h>
19
20 /* mhmisc.c */
21 int part_ok (CT, int);
22 int type_ok (CT, int);
23 void flush_errors (void);
24
25 /*
26 * prototypes
27 */
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);
31
32 /*
33 * static prototypes
34 */
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_external (CT, int, int, int, int);
40 static int list_application (CT, int, int, int, int);
41 static int list_encoding (CT);
42
43
44 /*
45 * various formats for -list option
46 */
47 #define LSTFMT1 "%4s %-5s %-24s %5s %s\n"
48 #define LSTFMT2a "%4d "
49 #define LSTFMT2b "%-5s %-24.24s "
50 #define LSTFMT2c1 "%5lu"
51 #define LSTFMT2c2 "%4lu%c"
52 #define LSTFMT2c3 "huge "
53 #define LSTFMT2c4 " "
54 #define LSTFMT2d1 " %.36s"
55 #define LSTFMT2d2 "\t %-65.65s\n"
56
57
58 /*
59 * Top level entry point to list group of messages
60 */
61
62 void
63 list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug)
64 {
65 CT ct, *ctp;
66
67 if (headers)
68 printf (LSTFMT1, "msg", "part", "type/subtype", "size", "description");
69
70 for (ctp = cts; *ctp; ctp++) {
71 ct = *ctp;
72 list_single_message (ct, realsize, verbose, debug);
73 }
74
75 flush_errors ();
76 }
77
78
79 /*
80 * Entry point to list a single message
81 */
82
83 static void
84 list_single_message (CT ct, int realsize, int verbose, int debug)
85 {
86 if (type_ok (ct, 1)) {
87 umask (ct->c_umask);
88 list_switch (ct, 1, realsize, verbose, debug);
89 if (ct->c_fp) {
90 fclose (ct->c_fp);
91 ct->c_fp = NULL;
92 }
93 if (ct->c_ceclosefnx)
94 (*ct->c_ceclosefnx) (ct);
95 }
96 }
97
98
99 /*
100 * Primary switching routine to list information about a content
101 */
102
103 int
104 list_switch (CT ct, int toplevel, int realsize, int verbose, int debug)
105 {
106 switch (ct->c_type) {
107 case CT_MULTIPART:
108 return list_multi (ct, toplevel, realsize, verbose, debug);
109 break;
110
111 case CT_MESSAGE:
112 switch (ct->c_subtype) {
113 case MESSAGE_PARTIAL:
114 return list_partial (ct, toplevel, realsize, verbose, debug);
115 break;
116
117 case MESSAGE_EXTERNAL:
118 return list_external (ct, toplevel, realsize, verbose, debug);
119 break;
120
121 case MESSAGE_RFC822:
122 default:
123 return list_content (ct, toplevel, realsize, verbose, debug);
124 break;
125 }
126 break;
127
128 case CT_TEXT:
129 case CT_AUDIO:
130 case CT_IMAGE:
131 case CT_VIDEO:
132 return list_content (ct, toplevel, realsize, verbose, debug);
133 break;
134
135 case CT_APPLICATION:
136 return list_application (ct, toplevel, realsize, verbose, debug);
137 break;
138
139 default:
140 /* list_debug (ct); */
141 adios (NULL, "unknown content type %d", ct->c_type);
142 break;
143 }
144
145 return 0; /* NOT REACHED */
146 }
147
148
149 #define empty(s) ((s) ? (s) : "")
150
151 /*
152 * Method for listing information about a simple/generic content
153 */
154
155 int
156 list_content (CT ct, int toplevel, int realsize, int verbose, int debug)
157 {
158 unsigned long size;
159 char *cp, buffer[BUFSIZ];
160 CI ci = &ct->c_ctinfo;
161
162 if (toplevel > 0)
163 printf (LSTFMT2a, atoi (r1bindex (empty (ct->c_file), '/')));
164 else
165 printf(toplevel < 0 ? "part " : " ");
166
167 snprintf (buffer, sizeof(buffer), "%s/%s", empty (ci->ci_type),
168 empty (ci->ci_subtype));
169 printf (LSTFMT2b, empty (ct->c_partno), buffer);
170
171 if (ct->c_cesizefnx && realsize)
172 size = (*ct->c_cesizefnx) (ct);
173 else
174 size = ct->c_end - ct->c_begin;
175
176 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
177 for (cp = " KMGT"; size > 9999; size >>= 10)
178 if (!*++cp)
179 break;
180
181 /* print size of this body part */
182 switch (*cp) {
183 case ' ':
184 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
185 printf (LSTFMT2c1, size);
186 else
187 printf (LSTFMT2c4);
188 break;
189
190 default:
191 printf (LSTFMT2c2, size, *cp);
192 break;
193
194 case '\0':
195 printf (LSTFMT2c3);
196 }
197
198 /* print Content-Description */
199 if (ct->c_descr) {
200 char *dp;
201
202 dp = cpytrim (ct->c_descr);
203 printf (LSTFMT2d1, dp);
204 free (dp);
205 }
206
207 printf ("\n");
208
209 if (verbose) {
210 char **ap, **ep;
211 CI ci = &ct->c_ctinfo;
212
213 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
214 printf ("\t %s=\"%s\"\n", *ap, *ep);
215
216 /*
217 * If verbose, print any RFC-822 comments in the
218 * Content-Type line.
219 */
220 if (ci->ci_comment) {
221 char *dp;
222
223 dp = cpytrim (ci->ci_comment);
224 snprintf (buffer, sizeof(buffer), "(%s)", dp);
225 free (dp);
226 printf (LSTFMT2d2, buffer);
227 }
228 }
229
230 if (debug)
231 list_debug (ct);
232
233 return OK;
234 }
235
236
237 /*
238 * Print debugging information about a content
239 */
240
241 static int
242 list_debug (CT ct)
243 {
244 char **ap, **ep;
245 CI ci = &ct->c_ctinfo;
246
247 fflush (stdout);
248 fprintf (stderr, " partno \"%s\"\n", empty (ct->c_partno));
249
250 /* print MIME-Version line */
251 if (ct->c_vrsn)
252 fprintf (stderr, " %s:%s\n", VRSN_FIELD, ct->c_vrsn);
253
254 /* print Content-Type line */
255 if (ct->c_ctline)
256 fprintf (stderr, " %s:%s\n", TYPE_FIELD, ct->c_ctline);
257
258 /* print parsed elements of content type */
259 fprintf (stderr, " type \"%s\"\n", empty (ci->ci_type));
260 fprintf (stderr, " subtype \"%s\"\n", empty (ci->ci_subtype));
261 fprintf (stderr, " comment \"%s\"\n", empty (ci->ci_comment));
262 fprintf (stderr, " magic \"%s\"\n", empty (ci->ci_magic));
263
264 /* print parsed parameters attached to content type */
265 fprintf (stderr, " parameters\n");
266 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
267 fprintf (stderr, " %s=\"%s\"\n", *ap, *ep);
268
269 /* print internal flags for type/subtype */
270 fprintf (stderr, " type 0x%x subtype 0x%x params 0x%x\n",
271 ct->c_type, ct->c_subtype,
272 (unsigned int)(unsigned long) ct->c_ctparams);
273
274 fprintf (stderr, " showproc \"%s\"\n", empty (ct->c_showproc));
275 fprintf (stderr, " termproc \"%s\"\n", empty (ct->c_termproc));
276 fprintf (stderr, " storeproc \"%s\"\n", empty (ct->c_storeproc));
277
278 /* print transfer encoding information */
279 if (ct->c_celine)
280 fprintf (stderr, " %s:%s", ENCODING_FIELD, ct->c_celine);
281
282 /* print internal flags for transfer encoding */
283 fprintf (stderr, " transfer encoding 0x%x params 0x%x\n",
284 ct->c_encoding, (unsigned int)(unsigned long) &ct->c_cefile);
285
286 /* print Content-ID */
287 if (ct->c_id)
288 fprintf (stderr, " %s:%s", ID_FIELD, ct->c_id);
289
290 /* print Content-Description */
291 if (ct->c_descr)
292 fprintf (stderr, " %s:%s", DESCR_FIELD, ct->c_descr);
293
294 fprintf (stderr, " read fp 0x%x file \"%s\" begin %ld end %ld\n",
295 (unsigned int)(unsigned long) ct->c_fp, empty (ct->c_file),
296 ct->c_begin, ct->c_end);
297
298 /* print more information about transfer encoding */
299 list_encoding (ct);
300
301 return OK;
302 }
303
304 #undef empty
305
306
307 /*
308 * list content information for type "multipart"
309 */
310
311 static int
312 list_multi (CT ct, int toplevel, int realsize, int verbose, int debug)
313 {
314 struct multipart *m = (struct multipart *) ct->c_ctparams;
315 struct part *part;
316
317 /* list the content for toplevel of this multipart */
318 list_content (ct, toplevel, realsize, verbose, debug);
319
320 /* now list for all the subparts */
321 for (part = m->mp_parts; part; part = part->mp_next) {
322 CT p = part->mp_part;
323
324 if (part_ok (p, 1) && type_ok (p, 1))
325 list_switch (p, 0, realsize, verbose, debug);
326 }
327
328 return OK;
329 }
330
331
332 /*
333 * list content information for type "message/partial"
334 */
335
336 static int
337 list_partial (CT ct, int toplevel, int realsize, int verbose, int debug)
338 {
339 struct partial *p = (struct partial *) ct->c_ctparams;
340
341 list_content (ct, toplevel, realsize, verbose, debug);
342 if (verbose) {
343 printf ("\t [message %s, part %d", p->pm_partid, p->pm_partno);
344 if (p->pm_maxno)
345 printf (" of %d", p->pm_maxno);
346 printf ("]\n");
347 }
348
349 return OK;
350 }
351
352
353 /*
354 * list content information for type "message/external"
355 */
356
357 static int
358 list_external (CT ct, int toplevel, int realsize, int verbose, int debug)
359 {
360 struct exbody *e = (struct exbody *) ct->c_ctparams;
361
362 /*
363 * First list the information for the
364 * message/external content itself.
365 */
366 list_content (ct, toplevel, realsize, verbose, debug);
367
368 if (verbose) {
369 if (e->eb_name)
370 printf ("\t name=\"%s\"\n", e->eb_name);
371 if (e->eb_dir)
372 printf ("\t directory=\"%s\"\n", e->eb_dir);
373 if (e->eb_site)
374 printf ("\t site=\"%s\"\n", e->eb_site);
375 if (e->eb_server)
376 printf ("\t server=\"%s\"\n", e->eb_server);
377 if (e->eb_subject)
378 printf ("\t subject=\"%s\"\n", e->eb_subject);
379
380 /* This must be defined */
381 printf ("\t access-type=\"%s\"\n", e->eb_access);
382
383 if (e->eb_mode)
384 printf ("\t mode=\"%s\"\n", e->eb_mode);
385 if (e->eb_permission)
386 printf ("\t permission=\"%s\"\n", e->eb_permission);
387
388 if (e->eb_flags == NOTOK)
389 printf ("\t [service unavailable]\n");
390 }
391
392 /*
393 * Now list the information for the external content
394 * to which this content points.
395 */
396 list_content (e->eb_content, 0, realsize, verbose, debug);
397
398 return OK;
399 }
400
401
402 /*
403 * list content information for type "application"
404 * This no longer needs to be a separate function. It used to
405 * produce some output with verbose enabled, but that has been
406 * moved to list_content ().
407 */
408
409 static int
410 list_application (CT ct, int toplevel, int realsize, int verbose, int debug)
411 {
412 list_content (ct, toplevel, realsize, verbose, debug);
413
414 return OK;
415 }
416
417
418 /*
419 * list information about the Content-Transfer-Encoding
420 * used by a content.
421 */
422
423 static int
424 list_encoding (CT ct)
425 {
426 CE ce = &ct->c_cefile;
427
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 : "");
431
432 return OK;
433 }