-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcc_strings.c
226 lines (195 loc) · 5.38 KB
/
cc_strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Copyright (C) 2016 Jeremiah Orians
* Copyright (C) 2018 Jan (janneke) Nieuwenhuizen <[email protected]>
* This file is part of M2-Planet.
*
* M2-Planet is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* M2-Planet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cc.h"
#include <stdint.h>
struct token_list* emit(char *s, struct token_list* head);
void require(int bool, char* error);
char upcase(char a)
{
if(in_set(a, "abcdefghijklmnopqrstuvwxyz"))
{
a = a - 32;
}
return a;
}
int char2hex(int c)
{
if (c >= '0' && c <= '9') return (c - 48);
else if (c >= 'a' && c <= 'f') return (c - 87);
else if (c >= 'A' && c <= 'F') return (c - 55);
else return -1;
}
int hexify(int c, int high)
{
int i = char2hex(c);
if(0 > i)
{
fputs("Tried to print non-hex number\n", stderr);
exit(EXIT_FAILURE);
}
if(high)
{
i = i << 4;
}
return i;
}
int digit_is_octal(char digit)
{
return digit >= '0' && digit <= '7';
}
int parse_octal_escape_code(char* digits)
{
int result = 0;
int i = 0;
while(digit_is_octal(digits[i]) && i < 3)
{
result = result * 8;
result = result + (digits[i] - '0');
i = i + 1;
}
/* implementation defined behavior: Octals above 0177 (127) wrap around in 2s complement. Same as GCC. */
while(result > 127)
{
result = result - 256;
}
return result;
}
int amount_of_escaped_chars_to_skip(char* string)
{
if (string[1] == 'x') return 3;
else if (digit_is_octal(string[1]))
{
int i = 2;
while(digit_is_octal(string[i]) && i < 4)
{
i = i + 1;
}
return i - 1;
}
return 1;
}
int escape_lookup(char* c);
int weird(char* string)
{
int c;
string = string + 1;
weird_reset:
c = string[0];
if(0 == c) return FALSE;
if('\\' == c)
{
c = escape_lookup(string);
string = string + amount_of_escaped_chars_to_skip(string);
}
if(!in_set(c, "\t\n !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")) return TRUE;
if(in_set(c, " \t\n\r") && (':' == string[1])) return TRUE;
string = string + 1;
goto weird_reset;
}
/* Lookup escape values */
int escape_lookup(char* c)
{
if('\\' != c[0]) return c[0];
if(c[1] == 'x')
{
int t1 = hexify(c[2], TRUE);
int t2 = hexify(c[3], FALSE);
return t1 + t2;
}
else if(c[1] == 'a') return 7;
else if(c[1] == 'b') return 8;
else if(c[1] == 't') return 9;
else if(c[1] == 'n') return 10;
else if(c[1] == 'v') return 11;
else if(c[1] == 'f') return 12;
else if(c[1] == 'r') return 13;
else if(c[1] == 'e') return 27;
else if(c[1] == '"') return 34;
else if(c[1] == '\'') return 39;
else if(c[1] == '\\') return 92;
else if(c[1] == '?') return 63;
else if(digit_is_octal(c[1])) return parse_octal_escape_code(c + 1);
fputs("Unknown escape received: ", stderr);
fputs(c, stderr);
fputs(" Unable to process\n", stderr);
exit(EXIT_FAILURE);
}
/* Deal with human strings */
char* collect_regular_string(char* string)
{
string_index = 0;
collect_regular_string_reset:
require((MAX_STRING - 3) > string_index, "Attempt at parsing regular string exceeds max length\n");
if(string[0] == '\\')
{
hold_string[string_index] = escape_lookup(string);
string = string + amount_of_escaped_chars_to_skip(string) + 1;
}
else
{
hold_string[string_index] = string[0];
string = string + 1;
}
string_index = string_index + 1;
if(string[0] != 0) goto collect_regular_string_reset;
hold_string[string_index] = '"';
hold_string[string_index + 1] = '\n';
char* message = calloc(string_index + 3, sizeof(char));
require(NULL != message, "Exhausted memory while storing regular string\n");
copy_string(message, hold_string, string_index + 2);
reset_hold_string();
return message;
}
/* Deal with non-human strings */
char* collect_weird_string(char* string)
{
string_index = 1;
int temp;
char* table = "0123456789ABCDEF";
hold_string[0] = '\'';
collect_weird_string_reset:
require((MAX_STRING - 6) > string_index, "Attempt at parsing weird string exceeds max length\n");
string = string + 1;
hold_string[string_index] = ' ';
temp = escape_lookup(string) & 0xFF;
hold_string[string_index + 1] = table[(temp >> 4)];
hold_string[string_index + 2] = table[(temp & 15)];
if(string[0] == '\\')
{
string = string + amount_of_escaped_chars_to_skip(string);
}
string_index = string_index + 3;
if(string[1] != 0) goto collect_weird_string_reset;
hold_string[string_index] = ' ';
hold_string[string_index + 1] = '0';
hold_string[string_index + 2] = '0';
hold_string[string_index + 3] = '\'';
hold_string[string_index + 4] = '\n';
char* hold = calloc(string_index + 6, sizeof(char));
require(NULL != hold, "Exhausted available memory while attempting to collect a weird string\n");
copy_string(hold, hold_string, string_index + 5);
reset_hold_string();
return hold;
}
/* Parse string to deal with hex characters*/
char* parse_string(char* string)
{
/* the string */
if(weird(string)) return collect_weird_string(string);
else return collect_regular_string(string);
}