0f0a8d60682ccd1ad772bf08054aeee5f348d5b7
[mmh] / uip / mhmisc.c
1 /*
2 ** mhparse.c -- misc routines to process 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 <errno.h>
11 #include <h/mime.h>
12 #include <h/mhparse.h>
13 #include <h/utils.h>
14 #include <stdarg.h>
15 #include <sys/stat.h>
16
17 extern int debugsw;
18
19 /*
20 ** limit actions to specified parts or content types
21 */
22 int npart = 0;
23 int ntype = 0;
24 char *parts[NPARTS + 1];
25 char *types[NTYPES + 1];
26
27 int endian = 0;  /* little or big endian */
28 int userrs = 0;
29
30 static char *errs = NULL;
31
32
33 /*
34 ** prototypes
35 */
36 int part_ok(CT, int);
37 int type_ok(CT, int);
38 void set_endian(void);
39 int make_intermediates(char *);
40 void content_error(char *, CT, char *, ...);
41 void flush_errors(void);
42
43
44 int
45 part_ok(CT ct, int sP)
46 {
47         char **ap;
48         int len;
49
50         if (npart == 0 || (ct->c_type == CT_MULTIPART &&
51                         (sP || ct->c_subtype)))
52                 return 1;
53
54         for (ap = parts; *ap; ap++) {
55                 len = strlen(*ap);
56                 if (strncmp(*ap, ct->c_partno, len)==0 &&
57                                 (!ct->c_partno[len] ||
58                                 ct->c_partno[len] == '.' ))
59                         return 1;
60         }
61
62         return 0;
63 }
64
65
66 int
67 type_ok(CT ct, int sP)
68 {
69         char **ap;
70         char buffer[BUFSIZ];
71         CI ci = &ct->c_ctinfo;
72
73         if (ntype == 0 || (ct->c_type == CT_MULTIPART &&
74                         (sP || ct->c_subtype)))
75                 return 1;
76
77         snprintf(buffer, sizeof(buffer), "%s/%s", ci->ci_type, ci->ci_subtype);
78         for (ap = types; *ap; ap++)
79                 if (!mh_strcasecmp(*ap, ci->ci_type) ||
80                                 !mh_strcasecmp(*ap, buffer))
81                         return 1;
82
83         return 0;
84 }
85
86
87 void
88 set_endian(void)
89 {
90         union {
91                 long l;
92                 char c[sizeof(long)];
93         } un;
94
95         un.l = 1;
96         endian = un.c[0] ? -1 : 1;
97         if (debugsw)
98                 fprintf(stderr, "%s endian architecture\n",
99                                 endian > 0 ? "big" : "little");
100 }
101
102
103 int
104 make_intermediates(char *file)
105 {
106         char *cp;
107
108         for (cp = file + 1; (cp = strchr(cp, '/')); cp++) {
109                 struct stat st;
110
111                 *cp = '\0';
112                 if (stat(file, &st) == NOTOK) {
113                         int answer;
114                         char *ep;
115                         if (errno != ENOENT) {
116                                 advise(file, "error on directory");
117 losing_directory:
118                                 *cp = '/';
119                                 return NOTOK;
120                         }
121
122                         ep = concat("Create directory \"", file, "\"? ", NULL);
123                         answer = getanswer(ep);
124                         free(ep);
125
126                         if (!answer)
127                                 goto losing_directory;
128                         if (!makedir(file)) {
129                                 advise(NULL, "unable to create directory %s",
130                                                 file);
131                                 goto losing_directory;
132                         }
133                 }
134
135                 *cp = '/';
136         }
137
138         return OK;
139 }
140
141
142 /*
143 ** Construct error message for content
144 */
145
146 void
147 content_error(char *what, CT ct, char *fmt, ...)
148 {
149         va_list arglist;
150         int i, len, buflen;
151         char *bp, buffer[BUFSIZ];
152         CI ci;
153
154         bp = buffer;
155         buflen = sizeof(buffer);
156
157         if (userrs && invo_name && *invo_name) {
158                 snprintf(bp, buflen, "%s: ", invo_name);
159                 len = strlen(bp);
160                 bp += len;
161                 buflen -= len;
162         }
163
164         va_start(arglist, fmt);
165
166         vsnprintf(bp, buflen, fmt, arglist);
167         len = strlen(bp);
168         bp += len;
169         buflen -= len;
170
171         ci = &ct->c_ctinfo;
172
173         if (what) {
174                 char *s;
175
176                 if (*what) {
177                         snprintf(bp, buflen, " %s: ", what);
178                         len = strlen(bp);
179                         bp += len;
180                         buflen -= len;
181                 }
182
183                 if ((s = strerror(errno)))
184                         snprintf(bp, buflen, "%s", s);
185                 else
186                         snprintf(bp, buflen, "Error %d", errno);
187
188                 len = strlen(bp);
189                 bp += len;
190                 buflen -= len;
191         }
192
193         i = strlen(invo_name) + 2;
194
195         /* Now add content type and subtype */
196         snprintf(bp, buflen, "\n%*.*s(content %s/%s", i, i, "",
197                         ci->ci_type, ci->ci_subtype);
198         len = strlen(bp);
199         bp += len;
200         buflen -= len;
201
202         /* Now add the message/part number */
203         if (ct->c_file) {
204                 snprintf(bp, buflen, " in message %s", ct->c_file);
205                 len = strlen(bp);
206                 bp += len;
207                 buflen -= len;
208
209                 if (ct->c_partno) {
210                         snprintf(bp, buflen, ", part %s", ct->c_partno);
211                         len = strlen(bp);
212                         bp += len;
213                         buflen -= len;
214                 }
215         }
216
217         snprintf(bp, buflen, ")");
218         len = strlen(bp);
219         bp += len;
220         buflen -= len;
221
222         if (userrs) {
223                 *bp++ = '\n';
224                 *bp = '\0';
225                 buflen--;
226
227                 errs = add(buffer, errs);
228         } else {
229                 advise(NULL, "%s", buffer);
230         }
231 }
232
233
234 void
235 flush_errors(void)
236 {
237         if (errs) {
238                 fflush(stdout);
239                 fprintf(stderr, "%s", errs);
240                 free(errs);
241                 errs = NULL;
242         }
243 }