3 * mhparse.c -- misc routines to process 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.
13 #include <h/mhparse.h>
19 * limit actions to specified parts or content types
23 char *parts[NPARTS + 1];
24 char *types[NTYPES + 1];
26 int endian = 0; /* little or big endian */
29 static char *errs = NULL;
35 int part_ok (CT, int);
36 int type_ok (CT, int);
37 void set_endian (void);
38 int make_intermediates (char *);
39 void content_error (char *, CT, char *, ...);
40 void flush_errors (void);
44 part_ok (CT ct, int sP)
49 if (npart == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
52 for (ap = parts; *ap; ap++) {
54 if (!strncmp (*ap, ct->c_partno, len) &&
55 (!ct->c_partno[len] || ct->c_partno[len] == '.' ))
64 type_ok (CT ct, int sP)
68 CI ci = &ct->c_ctinfo;
70 if (ntype == 0 || (ct->c_type == CT_MULTIPART && (sP || ct->c_subtype)))
73 snprintf (buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
74 for (ap = types; *ap; ap++)
75 if (!mh_strcasecmp (*ap, ci->ci_type) || !mh_strcasecmp (*ap, buffer))
91 endian = un.c[0] ? -1 : 1;
93 fprintf (stderr, "%s endian architecture\n",
94 endian > 0 ? "big" : "little");
99 make_intermediates (char *file)
103 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
107 if (stat (file, &st) == NOTOK) {
110 if (errno != ENOENT) {
111 advise (file, "error on directory");
117 ep = concat ("Create directory \"", file, "\"? ", NULL);
118 answer = getanswer (ep);
122 goto losing_directory;
123 if (!makedir (file)) {
124 advise (NULL, "unable to create directory %s", file);
125 goto losing_directory;
137 * Construct error message for content
141 content_error (char *what, CT ct, char *fmt, ...)
145 char *bp, buffer[BUFSIZ];
149 buflen = sizeof(buffer);
151 if (userrs && invo_name && *invo_name) {
152 snprintf (bp, buflen, "%s: ", invo_name);
158 va_start (arglist, fmt);
160 vsnprintf (bp, buflen, fmt, arglist);
171 snprintf (bp, buflen, " %s: ", what);
177 if ((s = strerror (errno)))
178 snprintf (bp, buflen, "%s", s);
180 snprintf (bp, buflen, "Error %d", errno);
187 i = strlen (invo_name) + 2;
189 /* Now add content type and subtype */
190 snprintf (bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
191 ci->ci_type, ci->ci_subtype);
196 /* Now add the message/part number */
198 snprintf (bp, buflen, " in message %s", ct->c_file);
204 snprintf (bp, buflen, ", part %s", ct->c_partno);
211 snprintf (bp, buflen, ")");
221 errs = add (buffer, errs);
223 advise (NULL, "%s", buffer);
233 fprintf (stderr, "%s", errs);