|
| 1 | +/** |
| 2 | + * builds the kwp.cxx (keyword tables) |
| 3 | + */ |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <ctype.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <unistd.h> |
| 9 | +#include "../src/ui/strlib.h" |
| 10 | + |
| 11 | +#define code_t int |
| 12 | +#define fcode_t int |
| 13 | +#define pcode_t int |
| 14 | + |
| 15 | +struct keyword_s { |
| 16 | + char name[16]; |
| 17 | + code_t code; |
| 18 | +}; |
| 19 | + |
| 20 | +struct opr_keyword_s { |
| 21 | + char name[16]; |
| 22 | + code_t code; |
| 23 | + code_t opr; |
| 24 | +}; |
| 25 | + |
| 26 | +struct func_keyword_s { |
| 27 | + char name[16]; |
| 28 | + fcode_t fcode; |
| 29 | +}; |
| 30 | + |
| 31 | +struct proc_keyword_s { |
| 32 | + char name[16]; |
| 33 | + pcode_t pcode; |
| 34 | +}; |
| 35 | + |
| 36 | +struct spopr_keyword_s { |
| 37 | + char name[16]; |
| 38 | + code_t code; |
| 39 | +}; |
| 40 | + |
| 41 | + |
| 42 | +#include "../src/common/kw.h" |
| 43 | +#include "../src/languages/keywords.en.c" |
| 44 | + |
| 45 | +struct HelpItem { |
| 46 | + char package[20]; |
| 47 | + char keyword[20]; |
| 48 | + char id[20]; |
| 49 | + char signature[128]; |
| 50 | + char help[1024]; |
| 51 | +}; |
| 52 | + |
| 53 | +int compareHelpItem(const void *p1, const void *p2) { |
| 54 | + HelpItem **i1 = (HelpItem **)p1; |
| 55 | + HelpItem **i2 = (HelpItem **)p2; |
| 56 | + int result = strcasecmp((*i1)->package, (*i2)->package); |
| 57 | + if (result == 0) { |
| 58 | + result = strcasecmp((*i1)->keyword, (*i2)->keyword); |
| 59 | + } |
| 60 | + return result; |
| 61 | +} |
| 62 | + |
| 63 | +bool isKeyword(const char *keyword) { |
| 64 | + bool result = false; |
| 65 | + |
| 66 | + for (int i = 0; !result && keyword_table[i].name[0] != '\0'; i++) { |
| 67 | + if (strcasecmp(keyword_table[i].name, keyword) == 0) { |
| 68 | + result = true; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + for (int i = 0; !result && proc_table[i].name[0] != '\0'; i++) { |
| 73 | + if (strcasecmp(proc_table[i].name, keyword) == 0) { |
| 74 | + result = true; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + for (int i = 0; !result && func_table[i].name[0] != '\0'; i++) { |
| 79 | + if (strcasecmp(func_table[i].name, keyword) == 0) { |
| 80 | + result = true; |
| 81 | + } |
| 82 | + } |
| 83 | + return result; |
| 84 | +} |
| 85 | + |
| 86 | +void logMissing(strlib::List<HelpItem *> *helpItems) { |
| 87 | + for (int i = 0; keyword_table[i].name[0] != '\0'; i++) { |
| 88 | + bool found = false; |
| 89 | + List_each(HelpItem *, it, *helpItems) { |
| 90 | + HelpItem *next = (*it); |
| 91 | + if (strcasecmp(keyword_table[i].name, next->keyword) == 0) { |
| 92 | + found = true; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + if (!found && keyword_table[i].name[0] != '$') { |
| 97 | + fprintf(stderr, "Missing KEYWORD doc: %s\n", keyword_table[i].name); |
| 98 | + } |
| 99 | + } |
| 100 | + for (int i = 0; proc_table[i].name[0] != '\0'; i++) { |
| 101 | + bool found = false; |
| 102 | + List_each(HelpItem *, it, *helpItems) { |
| 103 | + HelpItem *next = (*it); |
| 104 | + if (strcasecmp(proc_table[i].name, next->keyword) == 0) { |
| 105 | + found = true; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + if (!found) { |
| 110 | + fprintf(stderr, "Missing PROC doc: %s\n", proc_table[i].name); |
| 111 | + } |
| 112 | + } |
| 113 | + for (int i = 0; func_table[i].name[0] != '\0'; i++) { |
| 114 | + bool found = false; |
| 115 | + List_each(HelpItem *, it, *helpItems) { |
| 116 | + HelpItem *next = (*it); |
| 117 | + if (strcasecmp(func_table[i].name, next->keyword) == 0) { |
| 118 | + found = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + if (!found) { |
| 123 | + fprintf(stderr, "Missing FUNC doc: %s\n", func_table[i].name); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +bool readHelpReference(strlib::List<HelpItem *> *helpItems) { |
| 129 | + FILE *fp = fopen("sbasic_ref.csv", "r"); |
| 130 | + if (!fp) { |
| 131 | + fprintf(stderr, "Failed to open sbasic_ref.csv"); |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + char lineBuffer[2048]; |
| 136 | + while (1) { |
| 137 | + if (fgets(lineBuffer, sizeof(lineBuffer), fp) == NULL) { |
| 138 | + break; |
| 139 | + } |
| 140 | + HelpItem *item = new HelpItem(); |
| 141 | + int len = strlen(lineBuffer); |
| 142 | + int currentCol = 0; |
| 143 | + int start = 0; |
| 144 | + bool quoted = false; |
| 145 | + |
| 146 | + for (int i = 0; i < len; i++) { |
| 147 | + if (lineBuffer[i] == '\"' && lineBuffer[i + 1] == '\"') { |
| 148 | + // change CSV escape to C escape "" -> \" |
| 149 | + lineBuffer[i] = '\\'; |
| 150 | + i += 1; |
| 151 | + } else if (lineBuffer[i] == '\"') { |
| 152 | + quoted = !quoted; |
| 153 | + } else if (!quoted && (i + 1 == len || lineBuffer[i] == ',')) { |
| 154 | + int fieldLen = i - start; |
| 155 | + if (lineBuffer[start] == '\"') { |
| 156 | + start += 1; |
| 157 | + fieldLen -= 2; |
| 158 | + } |
| 159 | + switch (currentCol) { |
| 160 | + case 0: |
| 161 | + // package |
| 162 | + strncpy(item->package, lineBuffer + start, fieldLen); |
| 163 | + item->package[fieldLen] = '\0'; |
| 164 | + break; |
| 165 | + case 1: |
| 166 | + // type |
| 167 | + break; |
| 168 | + case 2: |
| 169 | + // keyword |
| 170 | + strncpy(item->keyword, lineBuffer + start, fieldLen); |
| 171 | + item->keyword[fieldLen] = '\0'; |
| 172 | + break; |
| 173 | + case 3: |
| 174 | + // nodeID |
| 175 | + strncpy(item->id, lineBuffer + start, fieldLen); |
| 176 | + item->id[fieldLen] = '\0'; |
| 177 | + break; |
| 178 | + case 4: |
| 179 | + // signature |
| 180 | + strncpy(item->signature, lineBuffer + start, fieldLen); |
| 181 | + item->signature[fieldLen] = '\0'; |
| 182 | + break; |
| 183 | + case 5: |
| 184 | + // text |
| 185 | + strncpy(item->help, lineBuffer + start, fieldLen); |
| 186 | + item->help[fieldLen] = '\0'; |
| 187 | + break; |
| 188 | + } |
| 189 | + currentCol++; |
| 190 | + start = i + 1; |
| 191 | + } |
| 192 | + } |
| 193 | + if (isKeyword(item->keyword)) { |
| 194 | + helpItems->add(item); |
| 195 | + } else { |
| 196 | + fprintf(stderr, "OBSOLETE %s\n", item->keyword); |
| 197 | + } |
| 198 | + } |
| 199 | + fclose(fp); |
| 200 | + helpItems->sort(compareHelpItem); |
| 201 | + logMissing(helpItems); |
| 202 | + return true; |
| 203 | +} |
| 204 | + |
| 205 | +int main(int argc, char *argv[]) { |
| 206 | + strlib::List<HelpItem *> helpItems; |
| 207 | + if (!readHelpReference(&helpItems)) { |
| 208 | + exit(1); |
| 209 | + } |
| 210 | + |
| 211 | + fprintf(stdout, "/* automagicaly generated file */\n"); |
| 212 | + fprintf(stdout, "struct KEYWORD_HELP {\n"); |
| 213 | + fprintf(stdout, " const char *package;\n"); |
| 214 | + fprintf(stdout, " const char *keyword;\n"); |
| 215 | + fprintf(stdout, " const char *signature;\n"); |
| 216 | + fprintf(stdout, " const char *help;\n"); |
| 217 | + fprintf(stdout, "} keyword_help[] = {\n"); |
| 218 | + List_each(HelpItem *, it, helpItems) { |
| 219 | + HelpItem *item = (*it); |
| 220 | + fprintf(stdout, "{\"%s\",\"%s\",\"%s\",\"%s\"},\n", item->package, |
| 221 | + item->keyword, item->signature, item->help); |
| 222 | + } |
| 223 | + fprintf(stdout, "};\n"); |
| 224 | + fprintf(stdout, "const int keyword_help_len = %d;\n", helpItems.size()); |
| 225 | + return 0; |
| 226 | +} |
0 commit comments