-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathchresc.c
235 lines (230 loc) · 5 KB
/
chresc.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
227
228
229
230
231
232
233
234
235
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2013 AT&T Intellectual Property *
* Copyright (c) 2020-2024 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
* A copy of the License is available at *
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html *
* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) *
* *
* Glenn Fowler <[email protected]> *
* David Korn <[email protected]> *
* Phong Vo <[email protected]> *
* Martijn Dekker <[email protected]> *
* *
***********************************************************************/
/*
* Glenn Fowler
* AT&T Research
*
* return the next character in the string s
* \ character constants are expanded
* *p is updated to point to the next character in s
* *m is 1 if return value is wide
*/
#include <ast.h>
#include <ctype.h>
#include <ccode.h>
#include <regex.h>
int
chrexp(const char* s, char** p, int* m, int flags)
{
const char* q;
int c;
const char* e;
const char* b;
char* r;
int n;
int w;
w = 0;
for (;;)
{
b = s;
switch (c = mbchar(s))
{
case 0:
s--;
break;
case '\\':
switch (c = *s++)
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c -= '0';
q = s + 2;
while (s < q)
switch (*s)
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c = (c << 3) + *s++ - '0';
break;
default:
q = s;
break;
}
break;
case 'a':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = CC_bel;
break;
case 'b':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = '\b';
break;
case 'c': /*DEPRECATED*/
case 'C':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
if (c = *s)
{
s++;
if (c == '\\')
{
c = chrexp(s - 1, &r, 0, flags);
s = (const char*)r;
}
if (islower(c))
c = toupper(c);
c = ccmapc(c, CC_NATIVE, CC_ASCII);
c ^= 0x40;
c = ccmapc(c, CC_ASCII, CC_NATIVE);
}
break;
case 'e': /*DEPRECATED*/
case 'E':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = CC_esc;
break;
case 'f':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = '\f';
break;
case 'M':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
if (*s == '-')
{
s++;
c = CC_esc;
}
break;
case 'n':
if (flags & FMT_EXP_NONL)
continue;
if (!(flags & FMT_EXP_LINE))
goto noexpand;
c = '\n';
break;
case 'r':
if (flags & FMT_EXP_NOCR)
continue;
if (!(flags & FMT_EXP_LINE))
goto noexpand;
c = '\r';
break;
case 't':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = '\t';
break;
case 'v':
if (!(flags & FMT_EXP_CHAR))
goto noexpand;
c = CC_vt;
break;
case 'u':
q = s + 4;
goto wex;
case 'U':
q = s + 8;
wex:
if (!(flags & FMT_EXP_WIDE))
goto noexpand;
w = 1;
goto hex;
case 'x':
q = s + 2;
hex:
b = e = s;
n = 0;
c = 0;
while (!e || !q || s < q)
{
switch (*s)
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
c = (c << 4) + *s++ - 'a' + 10;
n++;
continue;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
c = (c << 4) + *s++ - 'A' + 10;
n++;
continue;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
c = (c << 4) + *s++ - '0';
n++;
continue;
case '{':
case '[':
if (s != e)
break;
e = 0;
s++;
if (w && *s == 'U' && *(s + 1) == '+')
s += 2;
continue;
case '}':
case ']':
if (!e)
s++;
break;
default:
break;
}
break;
}
if (n <= 2 && !(flags & FMT_EXP_CHAR) || n > 2 && (w = 1) && !(flags & FMT_EXP_WIDE))
{
c = '\\';
s = b;
}
break;
case 0:
s--;
break;
}
break;
default:
if ((s - b) > 1)
w = 1;
break;
}
break;
}
normal:
if (p)
*p = (char*)s;
if (m)
*m = w;
return c;
noexpand:
c = '\\';
s--;
goto normal;
}
int
chresc(const char* s, char** p)
{
return chrexp(s, p, NULL, FMT_EXP_CHAR|FMT_EXP_LINE|FMT_EXP_WIDE);
}