FreeCalypso > hg > falcon-mail-tools
annotate f-demime/main.c @ 5:882d97266174
f-demime: fix \u and \U escape formats
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 06 May 2023 09:19:12 +0000 |
parents | 612c4d0df768 |
children |
rev | line source |
---|---|
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the main() function for f-demime. The following |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * functions are called to pass input to further processing: |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * begin_new_message(): called after processing a "From " line |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 * message_input_line(): called for all message lines after the "From " line |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 * finish_msg_body(): called when hitting EOF or a new "From " line |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 */ |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdio.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <stdlib.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <string.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <strings.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <time.h> |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include "defs.h" |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 char *att_filename_buf, *att_filename_base, *att_filename_tail; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 static void |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 get_attachment_dir() |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 static char varname[] = "FDEMIME_ATT_DIR"; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 char *env, *cp; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 unsigned dir_name_len; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 time_t curtime; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 struct tm *tm; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 env = getenv(varname); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 if (!env) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 fprintf(stderr, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 "error: required environment variable %s is not set\n", |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 varname); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (!env[0]) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 fprintf(stderr, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 "error: empty string value for %s is not allowed\n", |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 varname); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 dir_name_len = strlen(env); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 att_filename_buf = malloc(dir_name_len + 31); /* Y10K extra margin */ |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 if (!att_filename_buf) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 fprintf(stderr, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 "error: unable to malloc buffer for attachment filenames\n"); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 strcpy(att_filename_buf, env); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cp = att_filename_buf + dir_name_len; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 *cp++ = '/'; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 att_filename_base = cp; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 time(&curtime); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 tm = gmtime(&curtime); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 sprintf(cp, "%u%02u%02ua", tm->tm_year + 1900, tm->tm_mon + 1, |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 tm->tm_mday); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 att_filename_tail = index(cp, '\0'); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 main(argc, argv) |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 char **argv; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 { |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
62 char linebuf[LINE_BUF_SIZE]; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
63 int c, unterm, prev_unterm; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
64 unsigned lineno; |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (argc > 3) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 fprintf(stderr, "usage: %s [infile [outfile]]\n", argv[0]); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 get_attachment_dir(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 if (argc > 1 && !freopen(argv[1], "r", stdin)) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 perror(argv[1]); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (argc > 2 && !freopen(argv[2], "w", stdout)) { |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 perror(argv[2]); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 exit(1); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
79 lineno = 0; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
80 for (prev_unterm = 0; fgets(linebuf, sizeof linebuf, stdin); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
81 prev_unterm = unterm) { |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
82 if (!prev_unterm) |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
83 lineno++; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
84 if (index(linebuf, '\n')) |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
85 unterm = 0; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
86 else |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
87 unterm = 1; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
88 if (!prev_unterm && !strncmp(linebuf, "From ", 5)) { |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 finish_msg_body(); |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
90 fputs(linebuf, stdout); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
91 if (unterm) { |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
92 fprintf(stderr, |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
93 "f-demime warning: From line %u too long or unterminated\n", |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
94 lineno); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
95 for (;;) { |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
96 c = getchar(); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
97 if (c == EOF || c == '\n') |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
98 break; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
99 } |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
100 putchar('\n'); |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
101 unterm = 0; |
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
102 } |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 begin_new_message(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 continue; |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 } |
4
612c4d0df768
f-demime: rework for unboundedly long lines
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
106 message_input_line(linebuf, unterm, prev_unterm); |
0
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 finish_msg_body(); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 exit(0); |
7e0d08176f32
f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 } |