aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-git-receiver/shell-command-parser.c
blob: 842c67a867620137460416e3888782fe1ff7d225 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <squareball.h>

#include "shell-command-parser.h"

typedef enum {
    START_COMMAND = 1,
    START_REPO,
    START_REPO2,
    REPO,
    START_ESCAPED,
} command_state_t;


char*
bgr_shell_command_parse(const char *command)
{
    command_state_t state = START_COMMAND;
    size_t start = 0;
    size_t command_len = strlen(command);

    sb_string_t *rv = sb_string_new();

    for (size_t current = 0; current < command_len; current++) {

        char c = command[current];

        switch (state) {
            case START_COMMAND:
                if (c == ' ') {
                    if (((current == 16) &&
                         (0 == strncmp("git-receive-pack", command, 16))) ||
                        ((current == 15) &&
                         (0 == strncmp("git-upload-pack", command, 15))) ||
                        ((current == 18) &&
                         (0 == strncmp("git-upload-archive", command, 18))))
                    {
                        state = START_REPO;
                        break;
                    }
                    goto error;
                }
                break;

            case START_REPO:
                if (c == '\'') {  // never saw git using double-quotes
                    state = START_REPO2;
                    break;
                }
                if (c == '\\') {  // escaped ! or '
                    state = START_ESCAPED;
                    break;
                }
                goto error;

            case START_REPO2:
                if (c == '\'') {
                    state = START_REPO;
                    break;
                }
                start = current;
                if (rv->len == 0 && c == '/') {  // no absolute urls
                    start = current + 1;
                }
                state = REPO;
                break;

            case START_ESCAPED:
                if (c == '!' || c == '\'') {
                    sb_string_append_c(rv, c);
                    state = START_REPO;
                    break;
                }
                goto error;

            case REPO:
                if (c == '\'') {
                    sb_string_append_len(rv, command + start, current - start);
                    state = START_REPO;
                    break;
                }
                break;
        }
    }

    if (rv->len > 0)
        return sb_string_free(rv, false);

error:
    sb_string_free(rv, true);
    return NULL;
}