annotate f-demime/ptext_in.c @ 3:7a6cd16689f5

f-demime/Makefile: add install
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 06:41:20 +0000
parents 05651a1b8ba8
children 882d97266174
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 implements transformations that are specific to text/plain.
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
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 #include <sys/types.h>
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdlib.h>
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "defs.h"
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 extern void (*dec_outf)();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern FILE *tempfile;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern int text_is_utf8;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 int ptext_has_backslash, ptext_has_linesplit;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 static enum {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 FPS_GROUND,
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 FPS_CR,
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 FPS_UTF8
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 } first_pass_state;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 static u_char utf8_buf[4];
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static unsigned utf8_nbytes, utf8_wptr, unicode;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 static unsigned out_line_len, trailing_newline;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 unit_output(str)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 char *str;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 unsigned newlen;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 newlen = strlen(str);
1
05651a1b8ba8 f-demime compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
34 if (out_line_len + newlen >= OUTPUT_LINE_MAX) {
0
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 putc('\\', tempfile);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 putc('\n', tempfile);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 out_line_len = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 ptext_has_linesplit = 1;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fputs(str, tempfile);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 out_line_len += newlen;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 trailing_newline = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 newline_out()
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 putc('\n', tempfile);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 out_line_len = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 trailing_newline++;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (trailing_newline > 2)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 trailing_newline = 2;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 direct_output(ch)
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 char buf[2];
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 buf[0] = ch;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 buf[1] = '\0';
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 unit_output(buf);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 simple_escape(ch)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 char buf[3];
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 buf[0] = '\\';
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 buf[1] = ch;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 buf[2] = '\0';
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 unit_output(buf);
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
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 hex_escape(ch)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 char buf[5];
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 sprintf(buf, "\\x%02X", ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 unit_output(buf);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 regular_byte(ch)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if (ch == '\\') {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 ptext_has_backslash = 1;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 simple_escape(ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 if (ch >= ' ' && ch <= '~') {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 direct_output(ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 switch (ch) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 case 0x07:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 simple_escape('a');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 return;
1
05651a1b8ba8 f-demime compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
101 case 0x08:
0
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 simple_escape('b');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 case 0x09:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 direct_output(ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 case 0x0B:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 simple_escape('v');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 case 0x0C:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 simple_escape('f');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 case 0x0D:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 simple_escape('r');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 case 0x1B:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 simple_escape('e');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 hex_escape(ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 static int
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 utf8_collect()
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 switch (utf8_nbytes) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 case 2:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 unicode = ((utf8_buf[0] & 0x1F) << 6) | (utf8_buf[1] & 0x3F);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 return(1);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 case 3:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 unicode = ((utf8_buf[0] & 0x0F) << 12) |
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 ((utf8_buf[1] & 0x3F) << 6) | (utf8_buf[2] & 0x3F);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 if (unicode & 0xF800)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 return(1);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 else
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 return(0);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 case 4:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 unicode = ((utf8_buf[0] & 0x07) << 18) |
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 ((utf8_buf[1] & 0x3F) << 12) |
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 ((utf8_buf[2] & 0x3F) << 6) | (utf8_buf[3] & 0x3F);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (unicode & 0x1F0000)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 return(1);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 else
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 return(0);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 default:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 return(0);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 unicode_out()
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 char buf[9];
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 if (unicode >= 0x10000)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 sprintf(buf, "\\U%06u", unicode);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 else
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 sprintf(buf, "\\u%04u", unicode);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 unit_output(buf);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 flush_first_pass_state()
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 unsigned n;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 switch (first_pass_state) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 case FPS_CR:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 regular_byte('\r');
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 break;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 case FPS_UTF8:
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 for (n = 0; n < utf8_wptr; n++)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 regular_byte(utf8_buf[n]);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 break;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 first_pass_state = FPS_GROUND;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 static void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 first_pass(ch)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 if (first_pass_state == FPS_CR && ch == '\n') {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 first_pass_state = FPS_GROUND;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 newline_out();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 if (first_pass_state == FPS_UTF8 && ch >= 0x80 && ch <= 0xBF) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 utf8_buf[utf8_wptr++] = ch;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 if (utf8_wptr < utf8_nbytes)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 if (utf8_collect()) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 first_pass_state = FPS_GROUND;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 unicode_out();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 flush_first_pass_state();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 switch (ch) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 case '\n':
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 newline_out();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 case '\r':
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 first_pass_state = FPS_CR;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 if (!text_is_utf8 || ch < 0xC2 || ch > 0xF7) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 regular_byte(ch);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 return;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 first_pass_state = FPS_UTF8;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 utf8_buf[0] = ch;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 utf8_wptr = 1;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 if (ch < 0xE0)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 utf8_nbytes = 2;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 else if (ch < 0xF0)
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 utf8_nbytes = 3;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 else
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 utf8_nbytes = 4;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 ptext_conv_init()
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 dec_outf = first_pass;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 ptext_has_backslash = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 ptext_has_linesplit = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 first_pass_state = FPS_GROUND;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 out_line_len = 0;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 trailing_newline = 1;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 void
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 ptext_conv_finish()
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 flush_first_pass_state();
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 while (trailing_newline < 2) {
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 putc('\n', tempfile);
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 trailing_newline++;
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 }
7e0d08176f32 f-demime starting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 }