]> diplodocus.org Git - nmh/blob - uip/mhlistsbr.c
Add -D_FORTIFY_SOURCE=2 to CPPFLAGS instead of AM_CPPFLAGS.
[nmh] / uip / mhlistsbr.c
1 /* mhlistsbr.c -- routines to list information about the
2 * -- contents of MIME messages
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/mts.h>
12 #include <h/tws.h>
13 #include <h/mime.h>
14 #include <h/mhparse.h>
15 #include <h/utils.h>
16
17 /* mhmisc.c */
18 int part_ok (CT);
19 int type_ok (CT, int);
20 void flush_errors (void);
21
22 /*
23 * static prototypes
24 */
25 static void list_single_message (CT, int, int, int, int);
26 static int list_debug (CT);
27 static int list_multi (CT, int, int, int, int, int);
28 static int list_partial (CT, int, int, int, int, int);
29 static int list_external (CT, int, int, int, int, int);
30 static int list_application (CT, int, int, int, int, int);
31 static int list_encoding (CT);
32
33
34 /*
35 * various formats for -list option
36 */
37 #define LSTFMT1 "%4s %-5s %-24s %5s %s\n"
38 #define LSTFMT2a "%4d "
39 #define LSTFMT2b "%-5s %-24.24s "
40 #define LSTFMT2bv "%-5s %-24s "
41 #define LSTFMT2c1 "%5lu"
42 #define LSTFMT2c2 "%4lu%c"
43 #define LSTFMT2c3 "huge "
44 #define LSTFMT2c4 " "
45 #define LSTFMT2d1 " %.36s"
46 #define LSTFMT2d1v " %s"
47 #define LSTFMT2d2 "\t %-65s\n"
48
49
50 /*
51 * Top level entry point to list group of messages
52 */
53
54 void
55 list_all_messages (CT *cts, int headers, int realsize, int verbose, int debug,
56 int dispo)
57 {
58 CT ct, *ctp;
59
60 if (headers)
61 printf (LSTFMT1, "msg", "part", "type/subtype", "size", "description");
62
63 for (ctp = cts; *ctp; ctp++) {
64 ct = *ctp;
65 list_single_message (ct, realsize, verbose, debug, dispo);
66 }
67
68 flush_errors ();
69 }
70
71
72 /*
73 * Entry point to list a single message
74 */
75
76 static void
77 list_single_message (CT ct, int realsize, int verbose, int debug, int dispo)
78 {
79 if (type_ok (ct, 1)) {
80 umask (ct->c_umask);
81 list_switch (ct, 1, realsize, verbose, debug, dispo);
82 if (ct->c_fp) {
83 fclose (ct->c_fp);
84 ct->c_fp = NULL;
85 }
86 if (ct->c_ceclosefnx)
87 (*ct->c_ceclosefnx) (ct);
88 }
89 }
90
91
92 /*
93 * Primary switching routine to list information about a content
94 */
95
96 int
97 list_switch (CT ct, int toplevel, int realsize, int verbose, int debug,
98 int dispo)
99 {
100 switch (ct->c_type) {
101 case CT_MULTIPART:
102 return list_multi (ct, toplevel, realsize, verbose, debug, dispo);
103
104 case CT_MESSAGE:
105 switch (ct->c_subtype) {
106 case MESSAGE_PARTIAL:
107 return list_partial (ct, toplevel, realsize, verbose,
108 debug, dispo);
109
110 case MESSAGE_EXTERNAL:
111 return list_external (ct, toplevel, realsize, verbose,
112 debug, dispo);
113
114 case MESSAGE_RFC822:
115 default:
116 return list_content (ct, toplevel, realsize, verbose,
117 debug, dispo);
118 }
119
120 case CT_TEXT:
121 case CT_AUDIO:
122 case CT_IMAGE:
123 case CT_VIDEO:
124 return list_content (ct, toplevel, realsize, verbose, debug, dispo);
125
126 case CT_APPLICATION:
127 default:
128 return list_application (ct, toplevel, realsize, verbose, debug,
129 dispo);
130 }
131
132 return 0; /* NOT REACHED */
133 }
134
135
136 /*
137 * Method for listing information about a simple/generic content
138 */
139
140 int
141 list_content (CT ct, int toplevel, int realsize, int verbose, int debug,
142 int dispo)
143 {
144 unsigned long size;
145 char *cp, buffer[BUFSIZ];
146 CI ci = &ct->c_ctinfo;
147 PM pm;
148
149 if (toplevel > 0)
150 printf (LSTFMT2a, atoi (r1bindex (FENDNULL(ct->c_file), '/')));
151 else
152 printf(toplevel < 0 ? "part " : " ");
153
154 snprintf (buffer, sizeof(buffer), "%s/%s", FENDNULL(ci->ci_type),
155 FENDNULL(ci->ci_subtype));
156 if (verbose)
157 printf (LSTFMT2bv, FENDNULL(ct->c_partno), buffer);
158 else
159 printf (LSTFMT2b, FENDNULL(ct->c_partno), buffer);
160
161 if (ct->c_cesizefnx && realsize)
162 size = (*ct->c_cesizefnx) (ct);
163 else
164 size = ct->c_end - ct->c_begin;
165
166 /* find correct scale for size (Kilo/Mega/Giga/Tera) */
167 for (cp = " KMGT"; size > 9999; size /= 1000)
168 if (!*++cp)
169 break;
170
171 /* print size of this body part */
172 switch (*cp) {
173 case ' ':
174 if (size > 0 || ct->c_encoding != CE_EXTERNAL)
175 printf (LSTFMT2c1, size);
176 else
177 printf (LSTFMT2c4);
178 break;
179
180 default:
181 printf (LSTFMT2c2, size, *cp);
182 break;
183
184 case '\0':
185 printf (LSTFMT2c3);
186 }
187
188 /* print Content-Description */
189 if (ct->c_descr) {
190 char *dp;
191
192 dp = cpytrim (ct->c_descr);
193 if (verbose)
194 printf (LSTFMT2d1v, dp);
195 else
196 printf (LSTFMT2d1, dp);
197 free (dp);
198 }
199
200 putchar('\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", FENDNULL(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", FENDNULL(ci->ci_type));
265 fprintf (stderr, " subtype \"%s\"\n", FENDNULL(ci->ci_subtype));
266 fprintf (stderr, " comment \"%s\"\n", FENDNULL(ci->ci_comment));
267 fprintf (stderr, " magic \"%s\"\n", FENDNULL(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", FENDNULL(ct->c_showproc));
281 fprintf (stderr, " termproc \"%s\"\n", FENDNULL(ct->c_termproc));
282 fprintf (stderr, " storeproc \"%s\"\n", FENDNULL(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", FENDNULL(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, FENDNULL(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
321 /*
322 * list content information for type "multipart"
323 */
324
325 static int
326 list_multi (CT ct, int toplevel, int realsize, int verbose, int debug,
327 int dispo)
328 {
329 struct multipart *m = (struct multipart *) ct->c_ctparams;
330 struct part *part;
331
332 /* list the content for toplevel of this multipart */
333 list_content (ct, toplevel, realsize, verbose, debug, dispo);
334
335 /* now list for all the subparts */
336 for (part = m->mp_parts; part; part = part->mp_next) {
337 CT p = part->mp_part;
338
339 if (part_ok (p) && type_ok (p, 1))
340 list_switch (p, 0, realsize, verbose, debug, dispo);
341 }
342
343 return OK;
344 }
345
346
347 /*
348 * list content information for type "message/partial"
349 */
350
351 static int
352 list_partial (CT ct, int toplevel, int realsize, int verbose, int debug,
353 int dispo)
354 {
355 struct partial *p = (struct partial *) ct->c_ctparams;
356
357 list_content (ct, toplevel, realsize, verbose, debug, dispo);
358 if (verbose) {
359 printf ("\t [message %s, part %d", p->pm_partid, p->pm_partno);
360 if (p->pm_maxno)
361 printf (" of %d", p->pm_maxno);
362 puts("]");
363 }
364
365 return OK;
366 }
367
368
369 /*
370 * list content information for type "message/external"
371 */
372
373 static int
374 list_external (CT ct, int toplevel, int realsize, int verbose, int debug,
375 int dispo)
376 {
377 struct exbody *e = (struct exbody *) ct->c_ctparams;
378
379 /*
380 * First list the information for the
381 * message/external content itself.
382 */
383 list_content (ct, toplevel, realsize, verbose, debug, dispo);
384
385 if (verbose) {
386 if (e->eb_name)
387 printf ("\t name=\"%s\"\n", e->eb_name);
388 if (e->eb_dir)
389 printf ("\t directory=\"%s\"\n", e->eb_dir);
390 if (e->eb_site)
391 printf ("\t site=\"%s\"\n", e->eb_site);
392 if (e->eb_server)
393 printf ("\t server=\"%s\"\n", e->eb_server);
394 if (e->eb_subject)
395 printf ("\t subject=\"%s\"\n", e->eb_subject);
396 if (e->eb_url)
397 printf ("\t url=\"%s\"\n", e->eb_url);
398
399 /* This must be defined */
400 printf ("\t access-type=\"%s\"\n", e->eb_access);
401
402 if (e->eb_mode)
403 printf ("\t mode=\"%s\"\n", e->eb_mode);
404 if (e->eb_permission)
405 printf ("\t permission=\"%s\"\n", e->eb_permission);
406
407 if (e->eb_flags == NOTOK)
408 puts("\t [service unavailable]");
409
410 }
411
412 /*
413 * Now list the information for the external content
414 * to which this content points.
415 */
416 list_content (e->eb_content, 0, realsize, verbose, debug, dispo);
417
418 return OK;
419 }
420
421
422 /*
423 * list content information for type "application"
424 * This no longer needs to be a separate function. It used to
425 * produce some output with verbose enabled, but that has been
426 * moved to list_content ().
427 */
428
429 static int
430 list_application (CT ct, int toplevel, int realsize, int verbose, int debug,
431 int dispo)
432 {
433 list_content (ct, toplevel, realsize, verbose, debug, dispo);
434
435 return OK;
436 }
437
438
439 /*
440 * list information about the Content-Transfer-Encoding
441 * used by a content.
442 */
443
444 static int
445 list_encoding (CT ct)
446 {
447 CE ce = &ct->c_cefile;
448
449 fprintf (stderr, " decoded fp 0x%x file \"%s\"\n",
450 (unsigned int)(unsigned long) ce->ce_fp,
451 FENDNULL(ce->ce_file));
452
453 return OK;
454 }