-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
269 lines (227 loc) · 7.53 KB
/
utils.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
utils.c -- Various useful shell utility functions.
Copyright (C) 2016 Lawrence Sebald
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "utils.h"
size_t count_spaces(const char *str) {
size_t rv = 0;
while(*str) {
if(isspace(*str++))
++rv;
}
return rv;
}
void flush_input(FILE *fp) {
int c;
/* Read until we get an EOF character or a newline. */
while(1) {
c = fgetc(fp);
if(c == '\n' || c == EOF)
return;
}
}
char *unescape(const char *str, FILE *errf) {
size_t len = strlen(str), i;
char *unesc, *rv;
char cur;
char quoted = 0;
/* Allocate space for the new string. Since only potentially removing
characters, it will be no larger than the original string... */
if(!(rv = (char *)malloc(len + 1))) {
fprintf(errf, "shell error: %s\n", strerror(errno));
return NULL;
}
unesc = rv;
/* Scan through the string... */
while(*str) {
cur = *str++;
/* Is this the beginning of an escape sequence? */
if(!quoted && cur == '\\') {
cur = *str++;
switch(cur) {
case '\0':
fprintf(errf, "shell error: illegal escape sequence\n");
free(rv);
return NULL;
case 'n':
*unesc++ = '\n';
continue;
case 'a':
*unesc++ = '\a';
continue;
case 'b':
*unesc++ = '\b';
continue;
case 'r':
*unesc++ = '\r';
continue;
case '\\':
*unesc++ = '\\';
continue;
case 'f':
*unesc++ = '\f';
continue;
case 'v':
*unesc++ = '\v';
continue;
case '\'':
*unesc++ = '\'';
continue;
case '"':
*unesc++ = '"';
continue;
case '?':
*unesc++ = '?';
continue;
case '*':
*unesc++ = '*';
continue;
case '$':
*unesc++ = '$';
continue;
case 't':
*unesc++ = '\t';
continue;
case ' ':
*unesc++ = ' ';
continue;
case '!':
*unesc++ = '!';
continue;
/* Ugh... Octal. */
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int tmp = (cur - '0') << 6;
cur = *str++;
if(cur < '0' || cur > '7') {
free(rv);
return NULL;
}
tmp |= (cur - '0') << 3;
cur = *str++;
if(cur < '0' || cur > '7') {
free(rv);
return NULL;
}
tmp |= (cur - '0');
*unesc++ = (char)tmp;
continue;
}
/* And, for more fun, hex! */
case 'x':
case 'X':
{
int tmp;
cur = *str++;
if(cur >= '0' && cur <= '9')
tmp = (cur - '0') << 4;
else if(cur >= 'a' && cur <= 'f')
tmp = (cur - 'a' + 10) << 4;
else if(cur >= 'A' && cur <= 'F')
tmp = (cur - 'A' + 10) << 4;
else {
free(rv);
return NULL;
}
cur = *str++;
if(cur >= '0' && cur <= '9')
tmp |= (cur - '0');
else if(cur >= 'a' && cur <= 'f')
tmp |= (cur - 'a' + 10);
else if(cur >= 'A' && cur <= 'F')
tmp |= (cur - 'A' + 10);
else {
free(rv);
return NULL;
}
*unesc++ = (char)tmp;
continue;
}
default:
*unesc++ = cur;
}
continue;
}
/* Escape sequences in quoted strings are very limited... */
else if(quoted && cur == '\\') {
cur = *str++;
if(!cur) {
fprintf(errf, "shell error: invalid escape sequence\n");
free(rv);
return NULL;
}
if(cur != quoted)
*unesc++ = '\\';
}
/* Is this the beginning of a quoted string? */
else if(!quoted && (cur == '\'' || cur == '"')) {
quoted = cur;
continue;
}
else if(quoted && cur == quoted) {
quoted = 0;
continue;
}
/* If we get here, it's not part of an escape, so copy it directly. */
*unesc++ = cur;
}
/* Did we terminate any quotes that we started? */
if(quoted) {
fprintf(errf, "shell error: unterminated quote\n");
free(rv);
return NULL;
}
*unesc = 0;
return rv;
}
int first_unquoted_space(const char *str) {
int pos = 0;
const char *tmp = str;
char quoted = 0, last = 0, cur;
while(*tmp) {
last = cur;
cur = *tmp;
if(last != '\\') {
if(!quoted && (cur == '\'' || cur == '"'))
quoted = cur;
else if(quoted && cur == quoted)
quoted = 0;
if(!quoted && isspace(cur))
return pos;
}
++pos;
++tmp;
}
return -1;
}