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