Add n_psignal()
[s-mailx.git] / mime-types.c
blob06fe7ca632d04355b9ad3476976efd10dbc7b77a
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ `(un)?mimetype' and other mime.types(5) related facilities.
3 *@ "Keep in sync with" ./mime.types.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #undef n_FILE
20 #define n_FILE mime_types
22 #ifndef HAVE_AMALGAMATION
23 # include "nail.h"
24 #endif
26 enum mime_type {
27 _MT_APPLICATION,
28 _MT_AUDIO,
29 _MT_IMAGE,
30 _MT_MESSAGE,
31 _MT_MULTIPART,
32 _MT_TEXT,
33 _MT_VIDEO,
34 _MT_OTHER,
35 __MT_TMIN = 0,
36 __MT_TMAX = _MT_OTHER,
37 __MT_TMASK = 0x07,
39 _MT_CMD = 1<< 8, /* Via `mimetype' (not struct mtbltin) */
40 _MT_USR = 1<< 9, /* VAL_MIME_TYPES_USR */
41 _MT_SYS = 1<<10, /* VAL_MIME_TYPES_SYS */
42 _MT_FSPEC = 1<<11, /* Loaded via f= *mimetypes-load-control* spec. */
44 _MT_PLAIN = 1<<16, /* Without pipe handler display as text */
45 _MT_SOUP_h = 2<<16, /* Ditto, but HTML tagsoup parser if possible */
46 _MT_SOUP_H = 3<<16, /* HTML tagsoup parser, else NOT plain text */
47 __MT_MARKMASK = _MT_SOUP_H
50 enum mime_type_class {
51 _MT_C_CLEAN = 0, /* Plain RFC 5322 message */
52 _MT_C_NCTT = 1<<0, /* *contenttype == NULL */
53 _MT_C_ISTXT = 1<<1, /* *contenttype =~ text\/ */
54 _MT_C_ISTXTCOK = 1<<2, /* _ISTXT + *mime-allow-text-controls* */
55 _MT_C_HIGHBIT = 1<<3, /* Not 7bit clean */
56 _MT_C_LONGLINES = 1<<4, /* MIME_LINELEN_LIMIT exceed. */
57 _MT_C_CTRLCHAR = 1<<5, /* Control characters seen */
58 _MT_C_HASNUL = 1<<6, /* Contains \0 characters */
59 _MT_C_NOTERMNL = 1<<7, /* Lacks a final newline */
60 _MT_C_FROM_ = 1<<8, /* ^From_ seen */
61 _MT_C_FROM_1STLINE = 1<<9, /* From_ line seen */
62 _MT_C_SUGGEST_DONE = 1<<16, /* Inspector suggests to stop further parse */
63 _MT_C__1STLINE = 1<<17 /* .. */
66 struct mtbltin {
67 ui32_t mtb_flags;
68 ui32_t mtb_mtlen;
69 char const *mtb_line;
72 struct mtnode {
73 struct mtnode *mt_next;
74 ui32_t mt_flags;
75 ui32_t mt_mtlen; /* Length of MIME type string, rest thereafter */
76 /* C99 forbids flexible arrays in union, so unfortunately we waste a pointer
77 * that could already store character data here */
78 char const *mt_line;
81 struct mtlookup {
82 char const *mtl_name;
83 size_t mtl_nlen;
84 struct mtnode const *mtl_node;
85 char *mtl_result; /* If requested, salloc()ed MIME type */
88 struct mt_class_arg {
89 char const *mtca_buf;
90 size_t mtca_len;
91 ssize_t mtca_curlen;
92 char mtca_lastc;
93 char mtca_c;
94 enum mime_type_class mtca_mtc;
97 static struct mtbltin const _mt_bltin[] = {
98 #include "gen-mime-types.h"
101 static char const _mt_typnames[][16] = {
102 "application/", "audio/", "image/",
103 "message/", "multipart/", "text/",
104 "video/"
106 n_CTAV(_MT_APPLICATION == 0 && _MT_AUDIO == 1 && _MT_IMAGE == 2 &&
107 _MT_MESSAGE == 3 && _MT_MULTIPART == 4 && _MT_TEXT == 5 &&
108 _MT_VIDEO == 6);
110 /* */
111 static bool_t _mt_is_init;
112 static struct mtnode *_mt_list;
114 /* Initialize MIME type list in order */
115 static void _mt_init(void);
116 static bool_t __mt_load_file(ui32_t orflags,
117 char const *file, char **line, size_t *linesize);
119 /* Create (prepend) a new MIME type; cmdcalled results in a bit more verbosity
120 * for `mimetype' */
121 static struct mtnode * _mt_create(bool_t cmdcalled, ui32_t orflags,
122 char const *line, size_t len);
124 /* Try to find MIME type by X (after zeroing mtlp), return NULL if not found;
125 * if with_result >mtl_result will be created upon success for the former */
126 static struct mtlookup * _mt_by_filename(struct mtlookup *mtlp,
127 char const *name, bool_t with_result);
128 static struct mtlookup * _mt_by_mtname(struct mtlookup *mtlp,
129 char const *mtname);
131 /* In-depth inspection of raw content: call _round() repeatedly, last time with
132 * a 0 length buffer, finally check .mtca_mtc for result.
133 * No further call is needed if _round() return includes _MT_C_SUGGEST_DONE,
134 * as the resulting classification is unambiguous */
135 SINLINE struct mt_class_arg * _mt_classify_init(struct mt_class_arg *mtcap,
136 enum mime_type_class initval);
137 static enum mime_type_class _mt_classify_round(struct mt_class_arg *mtcap);
139 /* We need an in-depth inspection of an application/octet-stream part */
140 static enum mimecontent _mt_classify_os_part(ui32_t mce, struct mimepart *mpp);
142 /* Check whether a *pipe-XY* handler is applicable, and adjust flags according
143 * to the defined trigger characters; upon entry MIME_HDL_NULL is set, and that
144 * isn't changed if mhp doesn't apply */
145 static enum mime_handler_flags _mt_pipe_check(struct mime_handler *mhp);
147 static void
148 _mt_init(void)
150 struct mtnode *tail;
151 char c, *line; /* TODO line pool (below) */
152 size_t linesize;
153 ui32_t i, j;
154 char const *srcs_arr[10], *ccp, **srcs;
155 NYD_ENTER;
157 /*if (_mt_is_init)
158 * goto jleave;*/
160 /* Always load our builtins */
161 for (tail = NULL, i = 0; i < n_NELEM(_mt_bltin); ++i) {
162 struct mtbltin const *mtbp = _mt_bltin + i;
163 struct mtnode *mtnp = smalloc(sizeof *mtnp);
165 if (tail != NULL)
166 tail->mt_next = mtnp;
167 else
168 _mt_list = mtnp;
169 tail = mtnp;
170 mtnp->mt_next = NULL;
171 mtnp->mt_flags = mtbp->mtb_flags;
172 mtnp->mt_mtlen = mtbp->mtb_mtlen;
173 mtnp->mt_line = mtbp->mtb_line;
176 /* Decide which files sources have to be loaded */
177 if ((ccp = ok_vlook(mimetypes_load_control)) == NULL)
178 ccp = "US";
179 else if (*ccp == '\0')
180 goto jleave;
182 srcs = srcs_arr + 2;
183 srcs[-1] = srcs[-2] = NULL;
185 if (strchr(ccp, '=') != NULL) {
186 line = savestr(ccp);
188 while ((ccp = n_strsep(&line, ',', TRU1)) != NULL) {
189 switch ((c = *ccp)) {
190 case 'S': case 's':
191 srcs_arr[1] = VAL_MIME_TYPES_SYS;
192 if (0) {
193 /* FALLTHRU */
194 case 'U': case 'u':
195 srcs_arr[0] = VAL_MIME_TYPES_USR;
197 if (ccp[1] != '\0')
198 goto jecontent;
199 break;
200 case 'F': case 'f':
201 if (*++ccp == '=' && *++ccp != '\0') {
202 if (PTR2SIZE(srcs - srcs_arr) < n_NELEM(srcs_arr))
203 *srcs++ = ccp;
204 else
205 n_err(_("*mimetypes-load-control*: too many sources, "
206 "skipping %s\n"), n_shexp_quote_cp(ccp, FAL0));
207 continue;
209 /* FALLTHRU */
210 default:
211 goto jecontent;
214 } else for (i = 0; (c = ccp[i]) != '\0'; ++i)
215 switch (c) {
216 case 'S': case 's': srcs_arr[1] = VAL_MIME_TYPES_SYS; break;
217 case 'U': case 'u': srcs_arr[0] = VAL_MIME_TYPES_USR; break;
218 default:
219 jecontent:
220 n_err(_("*mimetypes-load-control*: unsupported content: %s\n"), ccp);
221 goto jleave;
224 /* Load all file-based sources in the desired order */
225 line = NULL;
226 linesize = 0;
227 for (j = 0, i = (ui32_t)PTR2SIZE(srcs - srcs_arr), srcs = srcs_arr;
228 i > 0; ++j, ++srcs, --i)
229 if (*srcs == NULL)
230 continue;
231 else if (!__mt_load_file((j == 0 ? _MT_USR
232 : (j == 1 ? _MT_SYS : _MT_FSPEC)), *srcs, &line, &linesize)) {
233 if ((n_poption & n_PO_D_V) || j > 1)
234 n_err(_("*mimetypes-load-control*: can't open or load %s\n"),
235 n_shexp_quote_cp(*srcs, FAL0));
237 if (line != NULL)
238 free(line);
239 jleave:
240 _mt_is_init = TRU1;
241 NYD_LEAVE;
244 static bool_t
245 __mt_load_file(ui32_t orflags, char const *file, char **line, size_t *linesize)
247 char const *cp;
248 FILE *fp;
249 struct mtnode *head, *tail, *mtnp;
250 size_t len;
251 NYD_ENTER;
253 if ((cp = fexpand(file, FEXP_LOCAL | FEXP_NOPROTO)) == NULL ||
254 (fp = Fopen(cp, "r")) == NULL) {
255 cp = NULL;
256 goto jleave;
259 for (head = tail = NULL; fgetline(line, linesize, NULL, &len, fp, 0) != 0;)
260 if ((mtnp = _mt_create(FAL0, orflags, *line, len)) != NULL) {
261 if (head == NULL)
262 head = tail = mtnp;
263 else
264 tail->mt_next = mtnp;
265 tail = mtnp;
267 if (head != NULL) {
268 tail->mt_next = _mt_list;
269 _mt_list = head;
272 Fclose(fp);
273 jleave:
274 NYD_LEAVE;
275 return (cp != NULL);
278 static struct mtnode *
279 _mt_create(bool_t cmdcalled, ui32_t orflags, char const *line, size_t len)
281 struct mtnode *mtnp = NULL;
282 char const *typ, *subtyp;
283 size_t tlen, i;
284 NYD_ENTER;
286 /* Drop anything after a comment first */
287 if ((typ = memchr(line, '#', len)) != NULL)
288 len = PTR2SIZE(typ - line);
290 /* Then trim any trailing whitespace from line (including NL/CR) */
291 /* C99 */{
292 struct str work;
294 work.s = n_UNCONST(line);
295 work.l = len;
296 line = n_str_trim(&work)->s;
297 len = work.l;
299 typ = line;
301 /* (But wait - is there a type marker?) */
302 if (!(orflags & (_MT_USR | _MT_SYS)) && *typ == '@') {
303 if (len < 2)
304 goto jeinval;
305 if (typ[1] == ' ') {
306 orflags |= _MT_PLAIN;
307 typ += 2;
308 len -= 2;
309 line += 2;
310 } else if (len > 4 && typ[2] == '@' && typ[3] == ' ') {
311 switch (typ[1]) {
312 case 't': orflags |= _MT_PLAIN; goto jexttypmar;
313 case 'h': orflags |= _MT_SOUP_h; goto jexttypmar;
314 case 'H': orflags |= _MT_SOUP_H;
315 jexttypmar:
316 typ += 4;
317 len -= 4;
318 line += 4;
319 break;
320 default:
321 goto jeinval;
323 } else
324 goto jeinval;
327 while (len > 0 && !blankchar(*line))
328 ++line, --len;
329 /* Ignore empty lines and even incomplete specifications (only MIME type)
330 * because this is quite common in mime.types(5) files */
331 if (len == 0 || (tlen = PTR2SIZE(line - typ)) == 0) {
332 if (cmdcalled)
333 n_err(_("Empty MIME type or no extensions given: %s\n"),
334 (len == 0 ? _("(no value)") : line));
335 goto jleave;
338 if ((subtyp = memchr(typ, '/', tlen)) == NULL) {
339 jeinval:
340 if (cmdcalled || (n_poption & n_PO_D_V))
341 n_err(_("%s MIME type: %s\n"),
342 (cmdcalled ? _("Invalid") : _("mime.types(5): invalid")), typ);
343 goto jleave;
345 ++subtyp;
347 /* Map to mime_type */
348 tlen = PTR2SIZE(subtyp - typ);
349 for (i = __MT_TMIN;;) {
350 if (!ascncasecmp(_mt_typnames[i], typ, tlen)) {
351 orflags |= i;
352 tlen = PTR2SIZE(line - subtyp);
353 typ = subtyp;
354 break;
356 if (++i == __MT_TMAX) {
357 orflags |= _MT_OTHER;
358 tlen = PTR2SIZE(line - typ);
359 break;
363 /* Strip leading whitespace from the list of extensions;
364 * trailing WS has already been trimmed away above.
365 * Be silent on slots which define a mimetype without any value */
366 while (len > 0 && blankchar(*line))
367 ++line, --len;
368 if (len == 0)
369 goto jleave;
371 /* */
372 mtnp = smalloc(sizeof(*mtnp) + tlen + len +1);
373 mtnp->mt_next = NULL;
374 mtnp->mt_flags = orflags;
375 mtnp->mt_mtlen = (ui32_t)tlen;
376 { char *l = (char*)(mtnp + 1);
377 mtnp->mt_line = l;
378 memcpy(l, typ, tlen);
379 memcpy(l + tlen, line, len);
380 tlen += len;
381 l[tlen] = '\0';
384 jleave:
385 NYD_LEAVE;
386 return mtnp;
389 static struct mtlookup *
390 _mt_by_filename(struct mtlookup *mtlp, char const *name, bool_t with_result)
392 struct mtnode *mtnp;
393 size_t nlen, i, j;
394 char const *ext, *cp;
395 NYD2_ENTER;
397 memset(mtlp, 0, sizeof *mtlp);
399 if ((nlen = strlen(name)) == 0) /* TODO name should be a URI */
400 goto jnull_leave;
401 /* We need a period TODO we should support names like README etc. */
402 for (i = nlen; name[--i] != '.';)
403 if (i == 0 || name[i] == '/') /* XXX no magics */
404 goto jnull_leave;
405 /* While here, basename() it */
406 while (i > 0 && name[i - 1] != '/')
407 --i;
408 name += i;
409 nlen -= i;
410 mtlp->mtl_name = name;
411 mtlp->mtl_nlen = nlen;
413 if (!_mt_is_init)
414 _mt_init();
416 /* ..all the MIME types */
417 for (mtnp = _mt_list; mtnp != NULL; mtnp = mtnp->mt_next)
418 for (ext = mtnp->mt_line + mtnp->mt_mtlen;; ext = cp) {
419 cp = ext;
420 while (whitechar(*cp))
421 ++cp;
422 ext = cp;
423 while (!whitechar(*cp) && *cp != '\0')
424 ++cp;
426 if ((i = PTR2SIZE(cp - ext)) == 0)
427 break;
428 /* Don't allow neither of ".txt" or "txt" to match "txt" */
429 else if (i + 1 >= nlen || name[(j = nlen - i) - 1] != '.' ||
430 ascncasecmp(name + j, ext, i))
431 continue;
433 /* Found it */
434 mtlp->mtl_node = mtnp;
436 if (!with_result)
437 goto jleave;
439 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
440 name = n_empty;
441 j = 0;
442 } else {
443 name = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
444 j = strlen(name);
446 i = mtnp->mt_mtlen;
447 mtlp->mtl_result = salloc(i + j +1);
448 if (j > 0)
449 memcpy(mtlp->mtl_result, name, j);
450 memcpy(mtlp->mtl_result + j, mtnp->mt_line, i);
451 mtlp->mtl_result[j += i] = '\0';
452 goto jleave;
454 jnull_leave:
455 mtlp = NULL;
456 jleave:
457 NYD2_LEAVE;
458 return mtlp;
461 static struct mtlookup *
462 _mt_by_mtname(struct mtlookup *mtlp, char const *mtname)
464 struct mtnode *mtnp;
465 size_t nlen, i, j;
466 char const *cp;
467 NYD2_ENTER;
469 memset(mtlp, 0, sizeof *mtlp);
471 if ((mtlp->mtl_nlen = nlen = strlen(mtlp->mtl_name = mtname)) == 0)
472 goto jnull_leave;
474 if (!_mt_is_init)
475 _mt_init();
477 /* ..all the MIME types */
478 for (mtnp = _mt_list; mtnp != NULL; mtnp = mtnp->mt_next) {
479 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
480 cp = n_empty;
481 j = 0;
482 } else {
483 cp = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
484 j = strlen(cp);
486 i = mtnp->mt_mtlen;
488 if (i + j == mtlp->mtl_nlen) {
489 char *xmt = ac_alloc(i + j +1);
490 if (j > 0)
491 memcpy(xmt, cp, j);
492 memcpy(xmt + j, mtnp->mt_line, i);
493 xmt[j += i] = '\0';
494 i = asccasecmp(mtname, xmt);
495 ac_free(xmt);
497 if (!i) {
498 /* Found it */
499 mtlp->mtl_node = mtnp;
500 goto jleave;
504 jnull_leave:
505 mtlp = NULL;
506 jleave:
507 NYD2_LEAVE;
508 return mtlp;
511 SINLINE struct mt_class_arg *
512 _mt_classify_init(struct mt_class_arg * mtcap, enum mime_type_class initval)
514 NYD2_ENTER;
515 memset(mtcap, 0, sizeof *mtcap);
516 mtcap->mtca_lastc = mtcap->mtca_c = EOF;
517 mtcap->mtca_mtc = initval | _MT_C__1STLINE;
518 NYD2_LEAVE;
519 return mtcap;
522 static enum mime_type_class
523 _mt_classify_round(struct mt_class_arg *mtcap) /* TODO dig UTF-8 for !text/!! */
525 /* TODO BTW., after the MIME/send layer rewrite we could use a MIME
526 * TODO boundary of "=-=-=" if we would add a B_ in EQ spirit to F_,
527 * TODO and report that state to the outer world */
528 #define F_ "From "
529 #define F_SIZEOF (sizeof(F_) -1)
530 char f_buf[F_SIZEOF], *f_p = f_buf;
531 char const *buf;
532 size_t blen;
533 ssize_t curlen;
534 int c, lastc;
535 enum mime_type_class mtc;
536 NYD2_ENTER;
538 buf = mtcap->mtca_buf;
539 blen = mtcap->mtca_len;
540 curlen = mtcap->mtca_curlen;
541 c = mtcap->mtca_c;
542 lastc = mtcap->mtca_lastc;
543 mtc = mtcap->mtca_mtc;
545 for (;; ++curlen) {
546 lastc = c;
547 if (blen == 0) {
548 /* Real EOF, or only current buffer end? */
549 if (mtcap->mtca_len == 0)
550 c = EOF;
551 else
552 break;
553 } else
554 c = (uc_i)*buf++;
555 --blen;
557 if (c == '\0') {
558 mtc |= _MT_C_HASNUL;
559 if (!(mtc & _MT_C_ISTXTCOK)) {
560 mtc |= _MT_C_SUGGEST_DONE;
561 break;
563 continue;
565 if (c == '\n' || c == EOF) {
566 mtc &= ~_MT_C__1STLINE;
567 if (curlen >= MIME_LINELEN_LIMIT)
568 mtc |= _MT_C_LONGLINES;
569 if (c == EOF) {
570 break;
572 f_p = f_buf;
573 curlen = -1;
574 continue;
576 /* A bit hairy is handling of \r=\x0D=CR.
577 * RFC 2045, 6.7:
578 * Control characters other than TAB, or CR and LF as parts of CRLF
579 * pairs, must not appear. \r alone does not force _CTRLCHAR below since
580 * we cannot peek the next character. Thus right here, inspect the last
581 * seen character for if its \r and set _CTRLCHAR in a delayed fashion */
582 /*else*/ if (lastc == '\r')
583 mtc |= _MT_C_CTRLCHAR;
585 /* Control character? XXX this is all ASCII here */
586 if (c < 0x20 || c == 0x7F) {
587 /* RFC 2045, 6.7, as above ... */
588 if (c != '\t' && c != '\r')
589 mtc |= _MT_C_CTRLCHAR;
590 /* If there is a escape sequence in reverse solidus notation defined
591 * for this in ANSI X3.159-1989 (ANSI C89), don't treat it as a control
592 * for real. I.e., \a=\x07=BEL, \b=\x08=BS, \t=\x09=HT. Don't follow
593 * libmagic(1) in respect to \v=\x0B=VT. \f=\x0C=NP; do ignore
594 * \e=\x1B=ESC */
595 if ((c >= '\x07' && c <= '\x0D') || c == '\x1B')
596 continue;
597 mtc |= _MT_C_HASNUL; /* Force base64 */
598 if (!(mtc & _MT_C_ISTXTCOK)) {
599 mtc |= _MT_C_SUGGEST_DONE;
600 break;
602 } else if ((ui8_t)c & 0x80) {
603 mtc |= _MT_C_HIGHBIT;
604 /* TODO count chars with HIGHBIT? libmagic?
605 * TODO try encode part - base64 if bails? */
606 if (!(mtc & (_MT_C_NCTT | _MT_C_ISTXT))) { /* TODO _NCTT?? */
607 mtc |= _MT_C_HASNUL /* Force base64 */ | _MT_C_SUGGEST_DONE;
608 break;
610 } else if (!(mtc & _MT_C_FROM_) && UICMP(z, curlen, <, F_SIZEOF)) {
611 *f_p++ = (char)c;
612 if (UICMP(z, curlen, ==, F_SIZEOF - 1) &&
613 PTR2SIZE(f_p - f_buf) == F_SIZEOF &&
614 !memcmp(f_buf, F_, F_SIZEOF)){
615 mtc |= _MT_C_FROM_;
616 if (mtc & _MT_C__1STLINE)
617 mtc |= _MT_C_FROM_1STLINE;
621 if (c == EOF && lastc != '\n')
622 mtc |= _MT_C_NOTERMNL;
624 mtcap->mtca_curlen = curlen;
625 mtcap->mtca_lastc = lastc;
626 mtcap->mtca_c = c;
627 mtcap->mtca_mtc = mtc;
628 NYD2_LEAVE;
629 return mtc;
630 #undef F_
631 #undef F_SIZEOF
634 static enum mimecontent
635 _mt_classify_os_part(ui32_t mce, struct mimepart *mpp)
637 struct str in = {NULL, 0}, outrest, inrest, dec;
638 struct mt_class_arg mtca;
639 bool_t did_inrest;
640 enum mime_type_class mtc;
641 int lc, c;
642 size_t cnt, lsz;
643 FILE *ibuf;
644 off_t start_off;
645 enum mimecontent mc;
646 NYD2_ENTER;
648 assert(mpp->m_mime_enc != MIMEE_BIN);
650 outrest = inrest = dec = in;
651 mc = MIME_UNKNOWN;
652 n_UNINIT(mtc, 0);
654 /* TODO v15-compat Note we actually bypass our usual file handling by
655 * TODO directly using fseek() on mb.mb_itf -- the v15 rewrite will change
656 * TODO all of this, and until then doing it like this is the only option
657 * TODO to integrate nicely into whoever calls us */
658 start_off = ftell(mb.mb_itf);
659 if ((ibuf = setinput(&mb, (struct message*)mpp, NEED_BODY)) == NULL) {
660 jos_leave:
661 fseek(mb.mb_itf, start_off, SEEK_SET);
662 goto jleave;
664 cnt = mpp->m_size;
666 /* Skip part headers */
667 for (lc = '\0'; cnt > 0; lc = c, --cnt)
668 if ((c = getc(ibuf)) == EOF || (c == '\n' && lc == '\n'))
669 break;
670 if (cnt == 0 || ferror(ibuf))
671 goto jos_leave;
673 /* So now let's inspect the part content, decoding content-transfer-encoding
674 * along the way TODO this should simply be "mime_factory_create(MPP)"! */
675 _mt_classify_init(&mtca, _MT_C_ISTXT);
677 for (lsz = 0;;) {
678 bool_t dobuf;
680 c = (--cnt == 0) ? EOF : getc(ibuf);
681 if ((dobuf = (c == '\n'))) {
682 /* Ignore empty lines */
683 if (lsz == 0)
684 continue;
685 } else if ((dobuf = (c == EOF))) {
686 if (lsz == 0 && outrest.l == 0)
687 break;
690 if (in.l + 1 >= lsz)
691 in.s = srealloc(in.s, lsz += LINESIZE);
692 if (c != EOF)
693 in.s[in.l++] = (char)c;
694 if (!dobuf)
695 continue;
697 jdobuf:
698 switch (mpp->m_mime_enc) {
699 case MIMEE_B64:
700 if (!b64_decode_part(&dec, &in, &outrest,
701 (did_inrest ? NULL : &inrest))) {
702 mtca.mtca_mtc = _MT_C_HASNUL;
703 goto jstopit; /* break;break; */
705 break;
706 case MIMEE_QP:
707 /* Drin */
708 if (!qp_decode_part(&dec, &in, &outrest, &inrest)) {
709 mtca.mtca_mtc = _MT_C_HASNUL;
710 goto jstopit; /* break;break; */
712 if (dec.l == 0 && c != EOF) {
713 in.l = 0;
714 continue;
716 break;
717 default:
718 /* Temporarily switch those two buffers.. */
719 dec = in;
720 in.s = NULL;
721 in.l = 0;
722 break;
725 mtca.mtca_buf = dec.s;
726 mtca.mtca_len = (ssize_t)dec.l;
727 if ((mtc = _mt_classify_round(&mtca)) & _MT_C_SUGGEST_DONE) {
728 mtc = _MT_C_HASNUL;
729 break;
732 if (c == EOF)
733 break;
734 /* ..and restore switched */
735 if (in.s == NULL) {
736 in = dec;
737 dec.s = NULL;
739 in.l = dec.l = 0;
742 if ((in.l = inrest.l) > 0) {
743 in.s = inrest.s;
744 did_inrest = TRU1;
745 goto jdobuf;
747 if (outrest.l > 0)
748 goto jdobuf;
749 jstopit:
750 if (in.s != NULL)
751 free(in.s);
752 if (dec.s != NULL)
753 free(dec.s);
754 if (outrest.s != NULL)
755 free(outrest.s);
756 if (inrest.s != NULL)
757 free(inrest.s);
759 fseek(mb.mb_itf, start_off, SEEK_SET);
761 if (!(mtc & (_MT_C_HASNUL /*| _MT_C_CTRLCHAR XXX really? */))) {
762 mc = MIME_TEXT_PLAIN;
763 if (mce & MIMECE_ALL_OVWR)
764 mpp->m_ct_type_plain = "text/plain";
765 if (mce & (MIMECE_BIN_OVWR | MIMECE_ALL_OVWR))
766 mpp->m_ct_type_usr_ovwr = "text/plain";
768 jleave:
769 NYD2_LEAVE;
770 return mc;
773 static enum mime_handler_flags
774 _mt_pipe_check(struct mime_handler *mhp)
776 enum mime_handler_flags rv_orig, rv;
777 char const *cp;
778 NYD2_ENTER;
780 rv_orig = rv = mhp->mh_flags;
782 /* Do we have any handler for this part? */
783 if (*(cp = mhp->mh_shell_cmd) == '\0')
784 goto jleave;
785 else if (*cp++ != '@') {
786 rv |= MIME_HDL_CMD;
787 goto jleave;
788 } else if (*cp == '\0') {
789 rv |= MIME_HDL_TEXT;
790 goto jleave;
793 jnextc:
794 switch (*cp) {
795 case '*': rv |= MIME_HDL_ALWAYS; ++cp; goto jnextc;
796 case '#': rv |= MIME_HDL_NOQUOTE; ++cp; goto jnextc;
797 case '&': rv |= MIME_HDL_ASYNC; ++cp; goto jnextc;
798 case '!': rv |= MIME_HDL_NEEDSTERM; ++cp; goto jnextc;
799 case '+':
800 if (rv & MIME_HDL_TMPF)
801 rv |= MIME_HDL_TMPF_UNLINK;
802 rv |= MIME_HDL_TMPF;
803 ++cp;
804 goto jnextc;
805 case '=':
806 rv |= MIME_HDL_TMPF_FILL;
807 ++cp;
808 goto jnextc;
809 case '@':
810 ++cp;
811 /* FALLTHRU */
812 default:
813 break;
815 mhp->mh_shell_cmd = cp;
817 /* Implications */
818 if (rv & MIME_HDL_TMPF_FILL)
819 rv |= MIME_HDL_TMPF;
821 /* Exceptions */
822 if (rv & MIME_HDL_ISQUOTE) {
823 if (rv & MIME_HDL_NOQUOTE)
824 goto jerr;
826 /* Cannot fetch data back from asynchronous process */
827 if (rv & MIME_HDL_ASYNC)
828 goto jerr;
830 /* TODO Can't use a "needsterminal" program for quoting */
831 if (rv & MIME_HDL_NEEDSTERM)
832 goto jerr;
835 if (rv & MIME_HDL_NEEDSTERM) {
836 if (rv & MIME_HDL_ASYNC) {
837 n_err(_("MIME type handlers: can't use needsterminal and "
838 "x-nail-async together\n"));
839 goto jerr;
842 /* needsterminal needs a terminal */
843 if (!(n_psonce & n_PSO_INTERACTIVE))
844 goto jerr;
847 if (!(rv & MIME_HDL_ALWAYS) && !(n_pstate & n_PS_MSGLIST_DIRECT)) {
848 /* Viewing multiple messages in one go, don't block system */
849 mhp->mh_msg.l = strlen(mhp->mh_msg.s = n_UNCONST(
850 _("[-- Directly address message only for display --]\n")));
851 rv |= MIME_HDL_MSG;
852 goto jleave;
855 rv |= MIME_HDL_CMD;
856 jleave:
857 mhp->mh_flags = rv;
858 NYD2_LEAVE;
859 return rv;
860 jerr:
861 rv = rv_orig;
862 goto jleave;
865 FL int
866 c_mimetype(void *v)
868 char **argv = v;
869 struct mtnode *mtnp;
870 NYD_ENTER;
872 if (!_mt_is_init)
873 _mt_init();
875 if (*argv == NULL) {
876 FILE *fp;
877 size_t l;
879 if (_mt_list == NULL) {
880 fprintf(n_stdout,
881 _("*mimetypes-load-control*: no mime.types(5) available\n"));
882 goto jleave;
885 if ((fp = Ftmp(NULL, "mimelist", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
886 NULL) {
887 n_perr(_("tmpfile"), 0);
888 v = NULL;
889 goto jleave;
892 for (l = 0, mtnp = _mt_list; mtnp != NULL; ++l, mtnp = mtnp->mt_next) {
893 char const *tmark, *typ;
895 switch (mtnp->mt_flags & __MT_MARKMASK) {
896 case _MT_PLAIN: tmark = "/t"; break;
897 case _MT_SOUP_h: tmark = "/h"; break;
898 case _MT_SOUP_H: tmark = "/H"; break;
899 default: tmark = " "; break;
901 typ = ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER)
902 ? n_empty : _mt_typnames[mtnp->mt_flags & __MT_TMASK];
904 fprintf(fp, "%c%s %s%.*s %s\n",
905 (mtnp->mt_flags & _MT_USR ? 'U'
906 : (mtnp->mt_flags & _MT_SYS ? 'S'
907 : (mtnp->mt_flags & _MT_FSPEC ? 'F'
908 : (mtnp->mt_flags & _MT_CMD ? 'C' : 'B')))),
909 tmark, typ, (int)mtnp->mt_mtlen, mtnp->mt_line,
910 mtnp->mt_line + mtnp->mt_mtlen);
913 page_or_print(fp, l);
914 Fclose(fp);
915 } else {
916 for (; *argv != NULL; ++argv) {
917 mtnp = _mt_create(TRU1, _MT_CMD, *argv, strlen(*argv));
918 if (mtnp != NULL) {
919 mtnp->mt_next = _mt_list;
920 _mt_list = mtnp;
921 } else
922 v = NULL;
925 jleave:
926 NYD_LEAVE;
927 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
930 FL int
931 c_unmimetype(void *v)
933 char **argv = v;
934 struct mtnode *lnp, *mtnp;
935 bool_t match;
936 NYD_ENTER;
938 /* Need to load that first as necessary */
939 if (!_mt_is_init)
940 _mt_init();
942 for (; *argv != NULL; ++argv) {
943 if (!asccasecmp(*argv, "reset")) {
944 _mt_is_init = FAL0;
945 goto jdelall;
948 if (argv[0][0] == '*' && argv[0][1] == '\0') {
949 jdelall:
950 while ((mtnp = _mt_list) != NULL) {
951 _mt_list = mtnp->mt_next;
952 free(mtnp);
954 continue;
957 for (match = FAL0, lnp = NULL, mtnp = _mt_list; mtnp != NULL;) {
958 char const *typ;
959 char *val;
960 size_t i;
962 if ((mtnp->mt_flags & __MT_TMASK) == _MT_OTHER) {
963 typ = n_empty;
964 i = 0;
965 } else {
966 typ = _mt_typnames[mtnp->mt_flags & __MT_TMASK];
967 i = strlen(typ);
970 val = ac_alloc(i + mtnp->mt_mtlen +1);
971 memcpy(val, typ, i);
972 memcpy(val + i, mtnp->mt_line, mtnp->mt_mtlen);
973 val[i += mtnp->mt_mtlen] = '\0';
974 i = asccasecmp(val, *argv);
975 ac_free(val);
977 if (!i) {
978 struct mtnode *nnp = mtnp->mt_next;
979 if (lnp == NULL)
980 _mt_list = nnp;
981 else
982 lnp->mt_next = nnp;
983 free(mtnp);
984 mtnp = nnp;
985 match = TRU1;
986 } else
987 lnp = mtnp, mtnp = mtnp->mt_next;
989 if (!match) {
990 if (!(n_pstate & n_PS_ROBOT) || (n_poption & n_PO_D_V))
991 n_err(_("No such MIME type: %s\n"), *argv);
992 v = NULL;
995 NYD_LEAVE;
996 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
999 FL bool_t
1000 n_mimetype_check_mtname(char const *name)
1002 struct mtlookup mtl;
1003 bool_t rv;
1004 NYD_ENTER;
1006 rv = (_mt_by_mtname(&mtl, name) != NULL);
1007 NYD_LEAVE;
1008 return rv;
1011 FL char *
1012 n_mimetype_classify_filename(char const *name)
1014 struct mtlookup mtl;
1015 NYD_ENTER;
1017 _mt_by_filename(&mtl, name, TRU1);
1018 NYD_LEAVE;
1019 return mtl.mtl_result;
1022 FL enum conversion
1023 n_mimetype_classify_file(FILE *fp, char const **contenttype,
1024 char const **charset, int *do_iconv)
1026 /* TODO classify once only PLEASE PLEASE PLEASE */
1027 /* TODO message/rfc822 is special in that it may only be 7bit, 8bit or
1028 * TODO binary according to RFC 2046, 5.2.1
1029 * TODO The handling of which is a hack */
1030 bool_t rfc822;
1031 enum mime_type_class mtc;
1032 enum mime_enc menc;
1033 off_t fpsz;
1034 enum conversion c;
1035 NYD_ENTER;
1037 assert(ftell(fp) == 0x0l);
1039 *do_iconv = 0;
1041 if (*contenttype == NULL) {
1042 mtc = _MT_C_NCTT;
1043 rfc822 = FAL0;
1044 } else if (!ascncasecmp(*contenttype, "text/", 5)) {
1045 mtc = ok_blook(mime_allow_text_controls)
1046 ? _MT_C_ISTXT | _MT_C_ISTXTCOK : _MT_C_ISTXT;
1047 rfc822 = FAL0;
1048 } else if (!asccasecmp(*contenttype, "message/rfc822")) {
1049 mtc = _MT_C_ISTXT;
1050 rfc822 = TRU1;
1051 } else {
1052 mtc = _MT_C_CLEAN;
1053 rfc822 = FAL0;
1056 menc = mime_enc_target();
1058 if ((fpsz = fsize(fp)) == 0)
1059 goto j7bit;
1060 else {
1061 char buf[BUFFER_SIZE];
1062 struct mt_class_arg mtca;
1064 _mt_classify_init(&mtca, mtc);
1065 for (;;) {
1066 mtca.mtca_len = fread(buf, sizeof(buf[0]), n_NELEM(buf), fp);
1067 mtca.mtca_buf = buf;
1068 if ((mtc = _mt_classify_round(&mtca)) & _MT_C_SUGGEST_DONE)
1069 break;
1070 if (mtca.mtca_len == 0)
1071 break;
1073 /* TODO ferror(fp) ! */
1074 rewind(fp);
1077 if (mtc & _MT_C_HASNUL) {
1078 menc = MIMEE_B64;
1079 /* Don't overwrite a text content-type to allow UTF-16 and such, but only
1080 * on request; else enforce what file(1)/libmagic(3) would suggest */
1081 if (mtc & _MT_C_ISTXTCOK)
1082 goto jcharset;
1083 if (mtc & (_MT_C_NCTT | _MT_C_ISTXT))
1084 *contenttype = "application/octet-stream";
1085 if (*charset == NULL)
1086 *charset = "binary";
1087 goto jleave;
1090 if (mtc &
1091 (_MT_C_LONGLINES | _MT_C_CTRLCHAR | _MT_C_NOTERMNL | _MT_C_FROM_)) {
1092 if (menc != MIMEE_B64)
1093 menc = MIMEE_QP;
1094 goto jstepi;
1096 if (mtc & _MT_C_HIGHBIT) {
1097 jstepi:
1098 if (mtc & (_MT_C_NCTT | _MT_C_ISTXT))
1099 *do_iconv = ((mtc & _MT_C_HIGHBIT) != 0);
1100 } else
1101 j7bit:
1102 menc = MIMEE_7B;
1103 if (mtc & _MT_C_NCTT)
1104 *contenttype = "text/plain";
1106 /* Not an attachment with specified charset? */
1107 jcharset:
1108 if (*charset == NULL) /* TODO MIME/send: iter active? iter! else */
1109 *charset = (mtc & _MT_C_HIGHBIT) ? charset_iter_or_fallback()
1110 : ok_vlook(charset_7bit);
1111 jleave:
1112 /* TODO mime_type_file_classify() shouldn't return conversion */
1113 if (rfc822) {
1114 if (mtc & _MT_C_FROM_1STLINE) {
1115 n_err(_("Pre-v15 %s cannot handle message/rfc822 that "
1116 "indeed is a RFC 4155 MBOX!\n"
1117 " Forcing a content-type of application/mbox!\n"),
1118 n_uagent);
1119 *contenttype = "application/mbox";
1120 goto jnorfc822;
1122 c = (menc == MIMEE_7B ? CONV_7BIT
1123 : (menc == MIMEE_8B ? CONV_8BIT
1124 : CONV_NONE));
1125 } else
1126 jnorfc822:
1127 c = (menc == MIMEE_7B ? CONV_7BIT
1128 : (menc == MIMEE_8B ? CONV_8BIT
1129 : (menc == MIMEE_QP ? CONV_TOQP : CONV_TOB64)));
1130 NYD_LEAVE;
1131 return c;
1134 FL enum mimecontent
1135 n_mimetype_classify_part(struct mimepart *mpp) /* FIXME charset=binary ??? */
1137 struct mtlookup mtl;
1138 enum mimecontent mc;
1139 char const *ct;
1140 union {char const *cp; ui32_t f;} mce;
1141 bool_t is_os;
1142 NYD_ENTER;
1144 mc = MIME_UNKNOWN;
1145 if ((ct = mpp->m_ct_type_plain) == NULL) /* TODO may not */
1146 ct = n_empty;
1148 if((mce.cp = ok_vlook(mime_counter_evidence)) != NULL && *mce.cp != '\0'){
1149 if((n_idec_ui32_cp(&mce.f, mce.cp, 0, NULL
1150 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1151 ) != n_IDEC_STATE_CONSUMED){
1152 n_err(_("Invalid *mime-counter-evidence* value content\n"));
1153 is_os = FAL0;
1154 }else{
1155 mce.f |= MIMECE_SET;
1156 is_os = !asccasecmp(ct, "application/octet-stream");
1158 if(mpp->m_filename != NULL && (is_os || (mce.f & MIMECE_ALL_OVWR))){
1159 if(_mt_by_filename(&mtl, mpp->m_filename, TRU1) == NULL){
1160 if(is_os)
1161 goto jos_content_check;
1162 }else if(is_os || asccasecmp(ct, mtl.mtl_result)){
1163 if(mce.f & MIMECE_ALL_OVWR)
1164 mpp->m_ct_type_plain = ct = mtl.mtl_result;
1165 if(mce.f & (MIMECE_BIN_OVWR | MIMECE_ALL_OVWR))
1166 mpp->m_ct_type_usr_ovwr = ct = mtl.mtl_result;
1170 }else
1171 is_os = FAL0;
1173 if(strchr(ct, '/') == NULL) /* For compatibility with non-MIME */
1174 mc = MIME_TEXT;
1175 else if(is_asccaseprefix("text/", ct)){
1176 ct += sizeof("text/") -1;
1177 if(!asccasecmp(ct, "plain"))
1178 mc = MIME_TEXT_PLAIN;
1179 else if(!asccasecmp(ct, "html"))
1180 mc = MIME_TEXT_HTML;
1181 else
1182 mc = MIME_TEXT;
1183 }else if(is_asccaseprefix("message/", ct)){
1184 ct += sizeof("message/") -1;
1185 if(!asccasecmp(ct, "rfc822"))
1186 mc = MIME_822;
1187 else
1188 mc = MIME_MESSAGE;
1189 }else if(is_asccaseprefix("multipart/", ct)){
1190 struct multi_types{
1191 char mt_name[12];
1192 enum mimecontent mt_mc;
1193 } const mta[] = {
1194 {"alternative\0", MIME_ALTERNATIVE},
1195 {"related", MIME_RELATED},
1196 {"digest", MIME_DIGEST},
1197 {"signed", MIME_SIGNED},
1198 {"encrypted", MIME_ENCRYPTED}
1199 }, *mtap;
1201 for(ct += sizeof("multipart/") -1, mtap = mta;;)
1202 if(!asccasecmp(ct, mtap->mt_name)){
1203 mc = mtap->mt_mc;
1204 break;
1205 }else if(++mtap == mta + n_NELEM(mta)){
1206 mc = MIME_MULTI;
1207 break;
1209 }else if(is_asccaseprefix("application/", ct)){
1210 if(is_os)
1211 goto jos_content_check;
1212 ct += sizeof("application/") -1;
1213 if(!asccasecmp(ct, "pkcs7-mime") || !asccasecmp(ct, "x-pkcs7-mime"))
1214 mc = MIME_PKCS7;
1216 jleave:
1217 NYD_LEAVE;
1218 return mc;
1220 jos_content_check:
1221 if((mce.f & MIMECE_BIN_PARSE) && mpp->m_mime_enc != MIMEE_BIN &&
1222 mpp->m_charset != NULL && asccasecmp(mpp->m_charset, "binary"))
1223 mc = _mt_classify_os_part(mce.f, mpp);
1224 goto jleave;
1227 FL enum mime_handler_flags
1228 n_mimetype_handler(struct mime_handler *mhp, struct mimepart const *mpp,
1229 enum sendaction action)
1231 #define __S "pipe-"
1232 #define __L (sizeof(__S) -1)
1233 struct mtlookup mtl;
1234 char *buf, *cp;
1235 enum mime_handler_flags rv;
1236 char const *es, *cs, *ccp;
1237 size_t el, cl, l;
1238 NYD_ENTER;
1240 memset(mhp, 0, sizeof *mhp);
1241 buf = NULL;
1243 rv = MIME_HDL_NULL;
1244 if (action == SEND_QUOTE || action == SEND_QUOTE_ALL)
1245 rv |= MIME_HDL_ISQUOTE;
1246 else if (action != SEND_TODISP && action != SEND_TODISP_ALL)
1247 goto jleave;
1249 el = ((es = mpp->m_filename) != NULL && (es = strrchr(es, '.')) != NULL &&
1250 *++es != '\0') ? strlen(es) : 0;
1251 cl = ((cs = mpp->m_ct_type_usr_ovwr) != NULL ||
1252 (cs = mpp->m_ct_type_plain) != NULL) ? strlen(cs) : 0;
1253 if ((l = n_MAX(el, cl)) == 0) {
1254 /* TODO this should be done during parse time! */
1255 goto jleave;
1258 /* We don't pass the flags around, so ensure carrier is up-to-date */
1259 mhp->mh_flags = rv;
1261 buf = ac_alloc(__L + l +1);
1262 memcpy(buf, __S, __L);
1264 /* File-extension handlers take precedence.
1265 * Yes, we really "fail" here for file extensions which clash MIME types */
1266 if (el > 0) {
1267 memcpy(buf + __L, es, el +1);
1268 for (cp = buf + __L; *cp != '\0'; ++cp)
1269 *cp = lowerconv(*cp);
1271 if ((mhp->mh_shell_cmd = ccp = n_var_vlook(buf, FAL0)) != NULL) {
1272 rv = _mt_pipe_check(mhp);
1273 goto jleave;
1277 /* Then MIME Content-Type:, if any */
1278 if (cl == 0)
1279 goto jleave;
1281 memcpy(buf + __L, cs, cl +1);
1282 for (cp = buf + __L; *cp != '\0'; ++cp)
1283 *cp = lowerconv(*cp);
1285 if ((mhp->mh_shell_cmd = n_var_vlook(buf, FAL0)) != NULL) {
1286 rv = _mt_pipe_check(mhp);
1287 goto jleave;
1290 if (_mt_by_mtname(&mtl, cs) != NULL)
1291 switch (mtl.mtl_node->mt_flags & __MT_MARKMASK) {
1292 #ifndef HAVE_FILTER_HTML_TAGSOUP
1293 case _MT_SOUP_H:
1294 break;
1295 #endif
1296 case _MT_SOUP_h:
1297 #ifdef HAVE_FILTER_HTML_TAGSOUP
1298 case _MT_SOUP_H:
1299 mhp->mh_ptf = &htmlflt_process_main;
1300 mhp->mh_msg.l = strlen(mhp->mh_msg.s =
1301 n_UNCONST(_("Builtin HTML tagsoup filter")));
1302 rv ^= MIME_HDL_NULL | MIME_HDL_PTF;
1303 goto jleave;
1304 #endif
1305 /* FALLTHRU */
1306 case _MT_PLAIN:
1307 mhp->mh_msg.l = strlen(mhp->mh_msg.s = n_UNCONST(_("Plain text")));
1308 rv ^= MIME_HDL_NULL | MIME_HDL_TEXT;
1309 goto jleave;
1310 default:
1311 break;
1314 jleave:
1315 if (buf != NULL)
1316 ac_free(buf);
1318 mhp->mh_flags = rv;
1319 if ((rv &= MIME_HDL_TYPE_MASK) == MIME_HDL_NULL)
1320 mhp->mh_msg.l = strlen(mhp->mh_msg.s = n_UNCONST(
1321 _("[-- No MIME handler installed or none applicable --]")));
1322 NYD_LEAVE;
1323 return rv;
1324 #undef __L
1325 #undef __S
1328 /* s-it-mode */