aboutsummaryrefslogtreecommitdiff
path: root/bp.c
blob: 51d34cab81ceb44be324c060c04d0551a446838b (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
//
// bp.c - Source code for the bp parser
//
// See `man ./bp.1` for more details
//
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "compiler.h"
#include "file_loader.h"
#include "grammar.h"
#include "json.h"
#include "printing.h"
#include "utils.h"
#include "vm.h"

static const char *usage = (
    "BP - a Parsing Expression Grammar command line tool\n\n"
    "Usage:\n"
    "  bp [flags] <pattern> [<input files>...]\n\n"
    "Flags:\n"
    " -h --help                        print the usage and quit\n"
    " -v --verbose                     print verbose debugging info\n"
    " -e --explain                     explain the matches\n"
    " -j --json                        print matches as a list of JSON objects\n"
    " -I --inplace                     modify a file in-place\n"
    " -i --ignore-case                 preform matching case-insensitively\n"
    " -l --list-files                  list filenames only\n"
    " -d --define <name>:<def>         define a grammar rule\n"
    " -D --define-string <name>:<def>  define a grammar rule (string-pattern)\n"
    " -p --pattern <pat>               provide a pattern (equivalent to bp '\\(<pat>)')\n"
    " -P --pattern-string <pat>        provide a string pattern (may be useful if '<pat>' begins with a '-')\n"
    " -r --replace <replacement>       replace the input pattern with the given replacement\n"
    " -m --mode <mode>                 set the behavior mode (defult: find-all)\n"
    " -g --grammar <grammar file>      use the specified file as a grammar\n");

static print_options_t print_options = 0;

__attribute__((nonnull))
static char *getflag(const char *flag, char *argv[], int *i);
__attribute__((nonnull(3)))
static int process_file(def_t *defs, const char *filename, vm_op_t *pattern, unsigned int flags);

//
// Return a pointer to the value part of a flag, if present, otherwise NULL.
// This works for --foo=value or --foo value
//
static char *getflag(const char *flag, char *argv[], int *i)
{
    size_t n = strlen(flag);
    check(argv[*i], "Attempt to get flag from NULL argument");
    if (strncmp(argv[*i], flag, n) == 0) {
        if (argv[*i][n] == '=') {
            return &argv[*i][n+1];
        } else if (argv[*i][n] == '\0') {
            check(argv[*i+1], "Expected argument after '%s'\n\n%s", flag, usage);
            ++(*i);
            return argv[*i];
        }
    }
    return NULL;
}

//
// For a given filename, open the file and attempt to match the given pattern
// against it, printing any results according to the flags.
//
static int process_file(def_t *defs, const char *filename, vm_op_t *pattern, unsigned int flags)
{
    static int printed_matches = 0;
    int success = 0;
    file_t *f = load_file(NULL, filename);
    check(f, "Could not open file: %s", filename);
    if (flags & BP_INPLACE) // Need to do this before matching
        intern_file(f);
    match_t *m = match(defs, f, f->contents, pattern, flags);
    if (m && print_errors(f, m, print_options) > 0)
        exit(1);

    if (m != NULL && m->end > m->start + 1) {
        success = 1;
        ++printed_matches;

        if (flags & BP_EXPLAIN) {
            if (filename)
                printf("\033[1;4m%s\033[0m\n", filename);
            visualize_match(m);
        } else if (flags & BP_LISTFILES) {
            printf("%s\n", filename);
        } else if (flags & BP_JSON) {
            if (printed_matches > 1)
                printf(",\n");
            printf("{\"filename\":\"%s\",", filename ? filename : "-");
            printf("\"tree\":{\"rule\":\"text\",\"start\":%d,\"end\":%ld,\"children\":[",
                   0, f->end - f->contents);
            json_match(f->contents, m, (flags & BP_VERBOSE) ? 1 : 0);
            printf("]}}\n");
        } else if (flags & BP_INPLACE && filename) {
            FILE *out = fopen(filename, "w");
            print_match(out, f, m, 0);
            fclose(out);
            printf("%s\n", filename);
        } else {
            if (printed_matches > 1)
                fputc('\n', stdout);
            if (filename) {
                if (print_options & PRINT_COLOR)
                    printf("\033[1;4;33m%s\033[0m\n", filename);
                else
                    printf("%s:\n", filename);
            }
            print_match(stdout, f, m,
                filename ? print_options : print_options & (print_options_t)~PRINT_LINE_NUMBERS);
        }
    }

    recycle_if_unused(&m);
    check(recycle_all_matches() == 0, "Memory leak: there should no longer be any matches in use at this point.");
    destroy_file(&f);

    return success;
}

#define FLAG(f) (flag=getflag((f), argv, &i))

int main(int argc, char *argv[])
{
    unsigned int flags = 0;
    char *flag = NULL;
    char path[PATH_MAX] = {0};
    const char *mode = "find-all";

    def_t *defs = NULL;

    file_t *loaded_files = NULL;

    // Define an opcode that is just a reference to the rule `pattern`
    file_t *pat_file = spoof_file(&loaded_files, "<pattern>", "pattern");
    vm_op_t *pattern = bp_pattern(pat_file, pat_file->contents);

    // Load builtins:
    if (access("/etc/xdg/bp/builtins.bp", R_OK) != -1) {
        file_t *f = load_file(&loaded_files, "/etc/xdg/bp/builtins.bp");
        defs = load_grammar(defs, f);
    }
    sprintf(path, "%s/.config/bp/builtins.bp", getenv("HOME"));
    if (access(path, R_OK) != -1) {
        file_t *f = load_file(&loaded_files, path);
        defs = load_grammar(defs, f);
    }

    int i, npatterns = 0;
    check(argc > 1, "%s", usage);
    for (i = 1; i < argc; i++) {
        if (streq(argv[i], "--")) {
            ++i;
            break;
        } else if (streq(argv[i], "--help")) {
          flag_help:
            printf("%s\n", usage);
            return 0;
        } else if (streq(argv[i], "--verbose")) {
            flags |= BP_VERBOSE;
        } else if (streq(argv[i], "--explain")) {
            flags |= BP_EXPLAIN;
        } else if (streq(argv[i], "--json")) {
            flags |= BP_JSON;
        } else if (streq(argv[i], "--inplace")) {
            flags |= BP_INPLACE;
        } else if (streq(argv[i], "--ignore-case")) {
            flags |= BP_IGNORECASE;
        } else if (streq(argv[i], "--list-files")) {
            flags |= BP_LISTFILES;
        } else if (FLAG("--replace") || FLAG("-r")) {
            // TODO: spoof file as sprintf("pattern => '%s'", flag)
            // except that would require handling edge cases like quotation marks etc.
            file_t *replace_file = spoof_file(&loaded_files, "<replace argument>", flag);
            vm_op_t *rep = bp_replacement(replace_file, pattern, replace_file->contents);
            check(rep, "Replacement failed to compile: %s", flag);
            defs = with_def(defs, replace_file, strlen("replacement"), "replacement", rep);
            mode = "replace-all";
        } else if (FLAG("--grammar") || FLAG("-g")) {
            file_t *f = load_file(&loaded_files, flag);
            if (f == NULL) {
                sprintf(path, "%s/.config/bp/%s.bp", getenv("HOME"), flag);
                f = load_file(&loaded_files, path);
            }
            if (f == NULL) {
                sprintf(path, "/etc/xdg/bp/%s.bp", flag);
                f = load_file(&loaded_files, path);
            }
            check(f != NULL, "Couldn't find grammar: %s", flag);
            defs = load_grammar(defs, f); // Keep in memory for debug output
        } else if (FLAG("--define") || FLAG("-d")) {
            char *def = flag;
            char *eq = strchr(def, ':');
            check(eq, "Rule definitions must include an ':'\n\n%s", usage);
            *eq = '\0';
            char *src = ++eq;
            file_t *def_file = spoof_file(&loaded_files, def, src);
            vm_op_t *pat = bp_pattern(def_file, def_file->contents);
            check(pat, "Failed to compile pattern: %s", flag);
            defs = with_def(defs, def_file, strlen(def), def, pat);
        } else if (FLAG("--define-string") || FLAG("-D")) {
            char *def = flag;
            char *eq = strchr(def, ':');
            check(eq, "Rule definitions must include an ':'\n\n%s", usage);
            *eq = '\0';
            char *src = ++eq;
            file_t *def_file = spoof_file(&loaded_files, def, src);
            vm_op_t *pat = bp_stringpattern(def_file, def_file->contents);
            check(pat, "Failed to compile pattern: %s", src);
            defs = with_def(defs, def_file, strlen(def), def, pat);
        } else if (FLAG("--pattern") || FLAG("-p")) {
            check(npatterns == 0, "Cannot define multiple patterns");
            file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", flag);
            vm_op_t *p = bp_pattern(arg_file, arg_file->contents);
            check(p, "Pattern failed to compile: %s", flag);
            defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
            ++npatterns;
        } else if (FLAG("--pattern-string") || FLAG("-P")) {
            file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", flag);
            vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents);
            check(p, "Pattern failed to compile: %s", flag);
            defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
            ++npatterns;
        } else if (FLAG("--mode") || FLAG("-m")) {
            mode = flag;
        } else if (argv[i][0] == '-' && argv[i][1] && argv[i][1] != '-') { // single-char flags
            for (char *c = &argv[i][1]; *c; ++c) {
                switch (*c) {
                    case 'h': goto flag_help; // -h
                    case 'v': flags |= BP_VERBOSE; break; // -v
                    case 'e': flags |= BP_EXPLAIN; break; // -e
                    case 'j': flags |= BP_JSON; break; // -j
                    case 'I': flags |= BP_INPLACE; break; // -I
                    case 'i': flags |= BP_IGNORECASE; break; // -i
                    case 'l': flags |= BP_LISTFILES; break; // -l
                    default:
                        printf("Unrecognized flag: -%c\n\n%s\n", *c, usage);
                        return 1;
                }
            }
        } else if (argv[i][0] != '-') {
            if (npatterns > 0) break;
            // TODO: spoof file with quotation marks for better debugging
            file_t *arg_file = spoof_file(&loaded_files, "<pattern argument>", argv[i]);
            vm_op_t *p = bp_stringpattern(arg_file, arg_file->contents);
            check(p, "Pattern failed to compile: %s", argv[i]);
            defs = with_def(defs, arg_file, strlen("pattern"), "pattern", p);
            ++npatterns;
        } else {
            printf("Unrecognized flag: %s\n\n%s\n", argv[i], usage);
            return 1;
        }
    }

    if (((flags & BP_JSON) != 0) + ((flags & BP_EXPLAIN) != 0) + ((flags & BP_LISTFILES) != 0) > 1) {
        printf("Please choose no more than one of the flags: -j/--json, -e/--explain, and -l/--list-files.\n"
               "They are mutually contradictory.\n");
        return 1;
    }

    if (isatty(STDOUT_FILENO)) {
        print_options |= PRINT_COLOR | PRINT_LINE_NUMBERS;
    }

    // Define an opcode that is just a reference to the overarching mode (e.g. find-all)
    if (lookup(defs, mode) == NULL) {
        printf("The mode '%s' is not defined.\n", mode);
        return 1;
    }
    file_t *mode_file = spoof_file(&loaded_files, "<mode>", mode);
    vm_op_t *mode_op = bp_pattern(mode_file, mode_file->contents);

    int found = 0;
    if (flags & BP_JSON) printf("[");
    if (i < argc) {
        // Files pass in as command line args:
        for (int nfiles = 0; i < argc; nfiles++, i++) {
            found += process_file(defs, argv[i], mode_op, flags);
        }
    } else if (isatty(STDIN_FILENO)) {
        // No files, no piped in input, so use * **/*:
        glob_t globbuf;
        glob("*", 0, NULL, &globbuf);
        glob("**/*", GLOB_APPEND, NULL, &globbuf);
        for (size_t i = 0; i < globbuf.gl_pathc; i++) {
            found += process_file(defs, globbuf.gl_pathv[i], mode_op, flags);
        }
        globfree(&globbuf);
    } else {
        // Piped in input:
        found += process_file(defs, NULL, mode_op, flags);
    }
    if (flags & BP_JSON) printf("]\n");

#ifdef FREE_ON_EXIT
    // This code frees up all residual heap-allocated memory. Since the program
    // is about to exit, this step is unnecessary. However, it is useful for
    // tracking down memory leaks.
    free_defs(&defs, NULL);
    while (loaded_files) {
        file_t *next = loaded_files->next;
        destroy_file(&loaded_files);
        loaded_files = next;
    }
    free_all_matches();
#endif

    return (found > 0) ? 0 : 1;
}

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1