2 ** mhparse.c -- misc routines to process MIME messages
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.
12 #include <h/mhparse.h>
18 ** limit actions to specified parts or content types
22 char *parts[NPARTS + 1];
23 char *types[NTYPES + 1];
25 int endian = 0; /* little or big endian */
28 static char *errs = NULL;
36 void set_endian(void);
37 int make_intermediates(char *);
38 void content_error(char *, CT, char *, ...);
39 void flush_errors(void);
43 part_ok(CT ct, int sP)
48 if (npart == 0 || (ct->c_type == CT_MULTIPART &&
49 (sP || ct->c_subtype)))
52 for (ap = parts; *ap; ap++) {
54 if (strncmp(*ap, ct->c_partno, len)==0 &&
55 (!ct->c_partno[len] ||
56 ct->c_partno[len] == '.' ))
65 type_ok(CT ct, int sP)
69 CI ci = &ct->c_ctinfo;
71 if (ntype == 0 || (ct->c_type == CT_MULTIPART &&
72 (sP || ct->c_subtype)))
75 snprintf(buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
76 for (ap = types; *ap; ap++)
77 if (!mh_strcasecmp(*ap, ci->ci_type) ||
78 !mh_strcasecmp(*ap, buffer))
94 endian = un.c[0] ? -1 : 1;
96 fprintf(stderr, "%s endian architecture\n",
97 endian > 0 ? "big" : "little");
102 make_intermediates(char *file)
106 for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
110 if (stat(file, &st) == NOTOK) {
113 if (errno != ENOENT) {
114 advise(file, "error on directory");
120 ep = concat("Create directory \"", file, "\"? ", NULL);
121 answer = getanswer(ep);
125 goto losing_directory;
126 if (!makedir(file)) {
127 advise(NULL, "unable to create directory %s",
129 goto losing_directory;
141 ** Construct error message for content
145 content_error(char *what, CT ct, char *fmt, ...)
149 char *bp, buffer[BUFSIZ];
153 buflen = sizeof(buffer);
155 if (userrs && invo_name && *invo_name) {
156 snprintf(bp, buflen, "%s: ", invo_name);
162 va_start(arglist, fmt);
164 vsnprintf(bp, buflen, fmt, arglist);
175 snprintf(bp, buflen, " %s: ", what);
181 if ((s = strerror(errno)))
182 snprintf(bp, buflen, "%s", s);
184 snprintf(bp, buflen, "Error %d", errno);
191 i = strlen(invo_name) + 2;
193 /* Now add content type and subtype */
194 snprintf(bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
195 ci->ci_type, ci->ci_subtype);
200 /* Now add the message/part number */
202 snprintf(bp, buflen, " in message %s", ct->c_file);
208 snprintf(bp, buflen, ", part %s", ct->c_partno);
215 snprintf(bp, buflen, ")");
225 errs = add(buffer, errs);
227 advise(NULL, "%s", buffer);
237 fprintf(stderr, "%s", errs);