summaryrefslogtreecommitdiff
path: root/src/core/command_parse.c
blob: f96bf6ccd4499b7a15d553ff376bbabe63d4a97b (plain)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "hybbx/command.h"
#include "hybbx/limits.h"
#include "hybbx/util.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

static char *hybbx_strdup(const char *s)
{
    size_t len;
    char *copy;

    if (s == NULL) {
        return NULL;
    }

    len = strlen(s) + 1;
    if (!hybbx_size_ok(len)) {
        return NULL;
    }
    copy = malloc(len);
    if (copy != NULL) {
        memcpy(copy, s, len);
    }
    return copy;
}

static const char *skip_leading_space(const char *line)
{
    if (line == NULL) {
        return NULL;
    }

    while (*line != '\0' && isspace((unsigned char)*line)) {
        line++;
    }

    return line;
}

static char *ltrim_copy(char *s)
{
    char *start;

    if (s == NULL) {
        return NULL;
    }

    start = (char *)skip_leading_space(s);
    if (start != s) {
        memmove(s, start, strlen(start) + 1);
    }

    return s;
}

static int str_ieq(const char *a, const char *b)
{
    if (a == NULL || b == NULL) {
        return 0;
    }

    while (*a != '\0' && *b != '\0') {
        char ca = (char)(*a >= 'A' && *a <= 'Z' ? *a + 32 : *a);
        char cb = (char)(*b >= 'A' && *b <= 'Z' ? *b + 32 : *b);

        if (ca != cb) {
            return 0;
        }
        a++;
        b++;
    }

    return *a == '\0' && *b == '\0';
}

static int strip_one_command_alias(char *rest, const char *alias)
{
    size_t alias_len;

    if (alias == NULL || rest == NULL) {
        return -1;
    }

    alias_len = strlen(alias);

    if (str_ieq(rest, alias)) {
        return 1;
    }

    if (strncmp(rest, alias, alias_len) == 0 &&
        isspace((unsigned char)rest[alias_len])) {
        memmove(rest, rest + alias_len, strlen(rest + alias_len) + 1);
        ltrim_copy(rest);
        return rest[0] == '\0' ? 1 : 0;
    }

    return -1;
}

static int strip_command_alias(char *rest)
{
    static const char *aliases[] = { "commands", "command", "cmd", NULL };
    int rc;
    size_t i;

    if (rest == NULL) {
        return 0;
    }

    if (rest[0] == '\0') {
        return 1;
    }

    for (i = 0; aliases[i] != NULL; i++) {
        rc = strip_one_command_alias(rest, aliases[i]);
        if (rc >= 0) {
            return rc;
        }
    }

    return 0;
}

hybbx_command_scope_t hybbx_command_classify(const char *line)
{
    const char *p = skip_leading_space(line);

    if (p == NULL || *p == '\0') {
        return HYBBX_CMD_SCOPE_LOCAL;
    }

    if (*p == '/') {
        return HYBBX_CMD_SCOPE_HYBBX;
    }

    if (*p == ';' || *p == '#') {
        return HYBBX_CMD_SCOPE_COMMENT;
    }

    return HYBBX_CMD_SCOPE_LOCAL;
}

int hybbx_command_is_comment(const char *line)
{
    return hybbx_command_classify(line) == HYBBX_CMD_SCOPE_COMMENT;
}

int hybbx_command_is_hybbx(const char *line)
{
    return hybbx_command_classify(line) == HYBBX_CMD_SCOPE_HYBBX;
}

static size_t count_tokens(const char *line)
{
    size_t count = 0;
    size_t i = 0;
    int in_token = 0;

    while (line[i] != '\0') {
        if (!isspace((unsigned char)line[i])) {
            if (!in_token) {
                count++;
                in_token = 1;
            }
        } else {
            in_token = 0;
        }
        i++;
    }

    return count;
}

static hybbx_result_t split_tokens(char *line, char **tokens, size_t max)
{
    size_t count = 0;
    size_t i = 0;

    while (line[i] != '\0' && count < max) {
        while (line[i] != '\0' && isspace((unsigned char)line[i])) {
            i++;
        }
        if (line[i] == '\0') {
            break;
        }
        tokens[count++] = line + i;
        while (line[i] != '\0' && !isspace((unsigned char)line[i])) {
            i++;
        }
        if (line[i] != '\0') {
            line[i] = '\0';
            i++;
        }
    }

    return HYBBX_OK;
}

static hybbx_result_t parse_hybbx_line(const char *line, hybbx_parsed_command_t *out)
{
    char *rest;
    char *work;
    size_t token_count;
    char **tokens;
    size_t i;
    hybbx_result_t rc;

    if (line[0] != '/') {
        return HYBBX_ERR_INVALID;
    }

    rest = hybbx_strdup(line + 1);
    if (rest == NULL) {
        return HYBBX_ERR_NOMEM;
    }

    ltrim_copy(rest);

    if (strip_command_alias(rest)) {
        out->verb = hybbx_strdup("help");
        if (out->verb == NULL) {
            free(rest);
            return HYBBX_ERR_NOMEM;
        }
        free(rest);
        return HYBBX_OK;
    }

    token_count = count_tokens(rest);
    if (token_count == 0) {
        out->verb = hybbx_strdup("help");
        free(rest);
        if (out->verb == NULL) {
            return HYBBX_ERR_NOMEM;
        }
        return HYBBX_OK;
    }

    if (token_count > HYBBX_CMD_TOKEN_MAX) {
        free(rest);
        return HYBBX_ERR_INVALID;
    }

    work = hybbx_strdup(rest);
    free(rest);
    if (work == NULL) {
        return HYBBX_ERR_NOMEM;
    }

    tokens = calloc(token_count, sizeof(*tokens));
    if (tokens == NULL) {
        free(work);
        return HYBBX_ERR_NOMEM;
    }

    rc = split_tokens(work, tokens, token_count);
    if (rc != HYBBX_OK) {
        free(tokens);
        free(work);
        return rc;
    }

    out->verb = hybbx_strdup(tokens[0]);
    if (out->verb == NULL) {
        free(tokens);
        free(work);
        return HYBBX_ERR_NOMEM;
    }

    out->argc = token_count > 1 ? token_count - 1 : 0;
    if (out->argc > 0) {
        out->argv = calloc(out->argc, sizeof(*out->argv));
        if (out->argv == NULL) {
            free(tokens);
            free(work);
            hybbx_command_free(out);
            return HYBBX_ERR_NOMEM;
        }

        for (i = 0; i < out->argc; i++) {
            out->argv[i] = hybbx_strdup(tokens[i + 1]);
            if (out->argv[i] == NULL) {
                free(tokens);
                free(work);
                hybbx_command_free(out);
                return HYBBX_ERR_NOMEM;
            }
        }
    }

    free(tokens);
    free(work);
    return HYBBX_OK;
}

hybbx_result_t hybbx_command_parse(const char *line,
                                   hybbx_parsed_command_t *out)
{
    char *trimmed;
    hybbx_result_t rc;

    if (line == NULL || out == NULL) {
        return HYBBX_ERR_INVALID;
    }

    memset(out, 0, sizeof(*out));

    trimmed = hybbx_strdup(skip_leading_space(line));
    if (trimmed == NULL) {
        return HYBBX_ERR_NOMEM;
    }

    out->scope = hybbx_command_classify(trimmed);
    out->line = trimmed;

    if (out->scope != HYBBX_CMD_SCOPE_HYBBX) {
        return HYBBX_OK;
    }

    rc = parse_hybbx_line(trimmed, out);
    if (rc != HYBBX_OK) {
        hybbx_command_free(out);
    }

    return rc;
}

void hybbx_command_free(hybbx_parsed_command_t *cmd)
{
    size_t i;

    if (cmd == NULL) {
        return;
    }

    free(cmd->line);
    free(cmd->verb);

    for (i = 0; i < cmd->argc; i++) {
        free(cmd->argv[i]);
    }
    free(cmd->argv);

    memset(cmd, 0, sizeof(*cmd));
}
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com