|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * Copyright (c) 2025 Cyril Hrubis <chrubis@suse.cz> |
| 4 | + */ |
| 5 | + |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <libgen.h> |
| 10 | + |
| 11 | +#include "data_storage.h" |
| 12 | + |
| 13 | +static int started; |
| 14 | + |
| 15 | +static void json_start(char *path) |
| 16 | +{ |
| 17 | + if (started) |
| 18 | + return; |
| 19 | + |
| 20 | + started = 1; |
| 21 | + |
| 22 | + printf(" \"%s\": {\n", basename(path)); |
| 23 | +} |
| 24 | + |
| 25 | +static void json_finish(const char *path) |
| 26 | +{ |
| 27 | + if (!started) |
| 28 | + return; |
| 29 | + |
| 30 | + printf(" \"fname\": \"%s\"\n", path); |
| 31 | + printf(" }"); |
| 32 | +} |
| 33 | + |
| 34 | +enum state { |
| 35 | + NONE, |
| 36 | + START, |
| 37 | + DOC_FIRST, |
| 38 | + DOC, |
| 39 | + ENV_START, |
| 40 | + ENV_FIRST, |
| 41 | + ENV |
| 42 | +}; |
| 43 | + |
| 44 | +static void parse_shell(char *path) |
| 45 | +{ |
| 46 | + char line[4096]; |
| 47 | + FILE *f = fopen(path, "r"); |
| 48 | + enum state state = NONE; |
| 49 | + |
| 50 | + if (!f) { |
| 51 | + fprintf(stderr, "Failed to open '%s': %s\n", |
| 52 | + path, strerror(errno)); |
| 53 | + exit(1); |
| 54 | + } |
| 55 | + |
| 56 | + while (fgets(line, sizeof(line), f)) { |
| 57 | + /* Strip newline */ |
| 58 | + line[strlen(line)-1] = 0; |
| 59 | + |
| 60 | + switch (state) { |
| 61 | + case NONE: |
| 62 | + if (!strcmp(line, "# ---")) |
| 63 | + state = START; |
| 64 | + break; |
| 65 | + case START: |
| 66 | + if (!strcmp(line, "# doc")) { |
| 67 | + json_start(path); |
| 68 | + state = DOC_FIRST; |
| 69 | + printf(" \"doc\": [\n"); |
| 70 | + } else if (!strcmp(line, "# env")) { |
| 71 | + json_start(path); |
| 72 | + state = ENV_START; |
| 73 | + } else { |
| 74 | + state = NONE; |
| 75 | + } |
| 76 | + break; |
| 77 | + case DOC: |
| 78 | + case DOC_FIRST: |
| 79 | + if (!strcmp(line, "# ---")) { |
| 80 | + state = NONE; |
| 81 | + printf("\n ],\n"); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + if (state == DOC_FIRST) |
| 86 | + state = DOC; |
| 87 | + else |
| 88 | + printf(",\n"); |
| 89 | + |
| 90 | + data_fprintf_esc(stdout, 4, line+2); |
| 91 | + break; |
| 92 | + case ENV_START: |
| 93 | + if (!strcmp(line, "# {")) { |
| 94 | + state = ENV_FIRST; |
| 95 | + } else { |
| 96 | + fprintf(stderr, |
| 97 | + "%s: Invalid line in JSON block '%s'", |
| 98 | + path, line); |
| 99 | + } |
| 100 | + break; |
| 101 | + case ENV: |
| 102 | + case ENV_FIRST: |
| 103 | + if (!strcmp(line, "# }")) { |
| 104 | + state = NONE; |
| 105 | + printf(",\n"); |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + if (state == ENV_FIRST) |
| 110 | + state = ENV; |
| 111 | + else |
| 112 | + printf("\n"); |
| 113 | + |
| 114 | + line[0] = ' '; |
| 115 | + line[1] = ' '; |
| 116 | + |
| 117 | + printf("%s", line); |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + json_finish(path); |
| 123 | +} |
| 124 | + |
| 125 | +int main(int argc, char *argv[]) |
| 126 | +{ |
| 127 | + int i; |
| 128 | + |
| 129 | + for (i = 1; i < argc; i++) |
| 130 | + parse_shell(argv[i]); |
| 131 | + |
| 132 | + return 0; |
| 133 | +} |
0 commit comments