-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
235 lines (182 loc) · 4.09 KB
/
string.c
File metadata and controls
235 lines (182 loc) · 4.09 KB
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
#define _GNU_SOURCE
#include "cy.h"
static char *fmt_tmp_buf;
static int tmp_buf_len;
static void append_buf(const char *str, int len)
{
fmt_tmp_buf = realloc(fmt_tmp_buf, tmp_buf_len + len + 1);
strncpy(fmt_tmp_buf + tmp_buf_len, str, len);
tmp_buf_len += len;
}
static void drop_buf(void)
{
free(fmt_tmp_buf);
tmp_buf_len = 0;
}
static char *fixup_buf(void)
{
char *ret = fmt_tmp_buf;
ret[tmp_buf_len] = '\0';
fmt_tmp_buf = NULL;
tmp_buf_len = 0;
return ret;
}
static int format_one(char *str, char **end, struct cy_token *st)
{
int len, ret;
*end = strchr(str, ')');
if (*end == NULL) {
show_token_err(st, "Error scanning format");
return -1;
}
len = *end - str - 1;
{
char fbuf[len + 1], *fmt;
struct cy_value cv;
char i_aux[32];
strncpy(fbuf, str + 1, len);
fbuf[len] = '\0';
fmt = strchr(fbuf, ':');
if (fmt != NULL) {
*fmt = '\0';
fmt++;
}
if (try_deref_symbol(fbuf, &cv, false) < 0)
return -1;
switch (cv.t) {
case CY_V_STRING:
append_buf(cv.v_str, strlen(cv.v_str));
break;
case CY_V_NUMBER:
if (fmt == NULL)
fmt = "%li";
ret = sprintf(i_aux, fmt, cv.v_i);
append_buf(i_aux, ret);
break;
default:
show_token_err(st, "Bad symbol (%s) for format", vtype2s(cv.t));
return -1;
}
}
return 0;
}
static int format_string(struct cy_token *st, struct cy_value *sv)
{
const char *s = st->v.v_str;
char *aux;
sv->t = CY_V_STRING;
while (1) {
aux = strchrnul(s, '\\');
append_buf(s, aux - s);
if (*aux == '\0')
break;
if (aux[0] == '\\' && aux[1] == '(') {
if (format_one(aux + 1, &aux, st) < 0) {
drop_buf();
return -1;
}
}
s = aux + 1;
}
sv->v_str = fixup_buf();
return 1;
}
static int eval_format(struct cy_token *t, struct cy_file *f)
{
struct cy_token st;
if (cy_eval_next_x(f, &st, CY_V_STRING) <= 0)
return -1;
return format_string(&st, &t->v);
}
static int decode_string(struct cy_token *st, struct cy_value *v)
{
char *end;
v->t = CY_V_NUMBER;
v->v_i = strtol(st->v.v_str, &end, 0);
if (*end != '\0') {
show_token_err(st, "Not a number");
return -1; /* XXX -- errors */
}
return 1;
}
static int eval_atoi(struct cy_token *t, struct cy_file *f)
{
struct cy_token st;
if (cy_eval_next_x(f, &st, CY_V_STRING) <= 0)
return -1;
return decode_string(&st, &t->v);
}
static int split_string(const char *str, struct cy_value *v)
{
const char *aux = NULL;
make_list(v);
while (*str != '\0') {
if (is_space_at(str)) {
str++;
continue;
}
aux = str + 1;
while (*aux != '\0') {
if (!is_space_at(aux)) {
aux++;
continue;
}
break;
}
{
struct cy_list_value *lv;
lv = malloc(sizeof(*lv));
lv->v.t = CY_V_STRING;
lv->v.v_str = strndup(str, aux - str);
list_add_tail(&lv->l, &v->v_list->h);
}
if (*aux != '\0')
str = aux + 1;
else
str = aux;
}
return 1;
}
static int eval_split(struct cy_token *t, struct cy_file *f)
{
struct cy_token st;
if (cy_eval_next_x(f, &st, CY_V_STRING) <= 0)
return -1;
return split_string(st.v.v_str, &t->v);
}
#define OP_CHK_PREFIX 1
#define OP_CHK_SUFFIX 2
static int eval_chk_match(struct cy_token *t, struct cy_file *f)
{
struct cy_token st, pt;
unsigned len;
if (cy_eval_next_x(f, &st, CY_V_STRING) <= 0)
return -1;
if (cy_eval_next_x(f, &pt, CY_V_STRING) <= 0)
return -1;
len = strlen(pt.v.v_str);
t->v.t = CY_V_BOOL;
if (t->typ->priv == OP_CHK_PREFIX)
t->v.v_bool = (strncmp(st.v.v_str, pt.v.v_str, len) == 0);
else {
unsigned slen;
slen = strlen(st.v.v_str);
if (slen < len)
t->v.v_bool = false;
else
t->v.v_bool = (strcmp(st.v.v_str + slen - len, pt.v.v_str) == 0);
}
return 1;
}
static struct cy_command cmd_format[] = {
{ .name = "%%", .t = { .ts = "format", .eval = eval_format, }, },
{ .name = "%~", .t = { .ts = "atoi", .eval = eval_atoi, }, },
{ .name = "%/", .t = { .ts = "split", .eval = eval_split, }, },
{ .name = "%^", .t = { .ts = "startswith", .eval = eval_chk_match, .priv = OP_CHK_PREFIX, }, },
{ .name = "%$", .t = { .ts = "endswith", .eval = eval_chk_match, .priv = OP_CHK_SUFFIX, }, },
{},
};
void init_string(void)
{
add_commands(cmd_format);
}