Debian Codegraph @

src/check_table.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 /**
11 * Helper utility to review tables.
12 */
13
14 #include <sys/stat.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <math.h>
21
22 #include "builtin_table.h"
23 #include "table_dump.h"
24 #include "table.h"
25 #include "wrapped.h"
26
27 #define LINE_LENGTH 6
28
29 enum {
30 BASE_STATE,
31 INSIDE_STATE
32 };
33
34 static table_t *check_table(char *filename, int use_hash)
35 {
36 FILE *table_file;
37 char *work;
38 int code;
39 int offset;
40 char *parsed;
41 int err;
42 int size;
43 int check_max_data_length;
44 int ret;
45 int state;
46 int last;
47 char *check;
48
49 table_t *table;
50
51 struct stat table_stat;
52
53 err = stat(filename, &table_stat);
54 if (err == -1) {
55 return NULL;
56 }
57
58 size = 0;
59
60 if (table_stat.st_size > 0) {
61 size = table_stat.st_size / LINE_LENGTH;
62 }
63 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
64 else {
65 size = (512 * table_stat.st_blocks) / LINE_LENGTH;
66 }
67 #endif
68
69 if (size < 500) {
70 size = 500;
71 }
72
73
74 table = table_init(size);
75 if (table == NULL) {
76 return NULL;
77 }
78
79 table->use_hash = use_hash;
80
81 table_file = fopen(filename, "r");
82 if (table_file == NULL) {
83 fprintf(stderr, "Unable to open translation table: %s\n", strerror(errno));
84 return NULL;
85 }
86
87 work = wrapped_malloc(1024);
88
89 parsed = wrapped_malloc(1024);
90
91 last = 0;
92
93 check_max_data_length = 1;
94 state = BASE_STATE;
95
96 while (fgets(work, 1024, table_file) != NULL) {
97 if (*work == '#') {
98
99 /*
100 * Don't even bother
101 */
102 continue;
103 }
104
105 parsed[0] = '\0';
106
107 if (state == BASE_STATE) {
108 ret = sscanf(work, " %s %n", parsed, &offset);
109
110 if (ret == 0) {
111 continue;
112 }
113
114 if (strncasecmp(parsed, "start", 5) == 0) {
115 last = 0;
116
117 if (work[offset] == '\0') {
118 // All languages
119 state = INSIDE_STATE;
120 continue;
121 }
122
123 if (work[offset] == '"') {
124 sscanf(work + offset + 1, "%[^\"]", parsed);
125 } else if (work[offset] == '\'') {
126 sscanf(work + offset + 1, "%[^']", parsed);
127 } else {
128 sscanf(work + offset, "%s", parsed);
129 }
130
131 printf("skipping language subsection for %s\n", parsed);
132
133 continue;
134 }
135
136 if (strncasecmp(parsed, "default", 7) == 0) {
137
138 if (work[offset] == '\0') {
139 table->default_translation = NULL;
140 continue;
141 }
142
143 if (work[offset] == '"') {
144 sscanf(work + offset + 1, "%[^\"]", parsed);
145 } else if (work[offset] == '\'') {
146 sscanf(work + offset + 1, "%[^']", parsed);
147 } else {
148 sscanf(work + offset, "%s", parsed);
149 }
150
151 table->default_translation = wrapped_strdup(parsed);
152
153 if (strlen(parsed) > check_max_data_length) {
154 check_max_data_length = strlen(parsed);
155 }
156
157 continue;
158 }
159
160 continue;
161 }
162
163 /*
164 * Inside state
165 */
166
167 code = -1;
168
169 ret = sscanf(work, "%i %n", &code, &offset);
170
171 if (ret == 0 || code < 0 || offset < 0) {
172
173 /*
174 * Check for end
175 */
176 ret = sscanf(work, " %s %n", parsed, &offset);
177
178 if (ret > 0 && strncasecmp(parsed, "end", 5) == 0) {
179 state = BASE_STATE;
180 }
181
182 continue;
183 }
184
185 if (work[offset] == '\0') {
186 continue;
187 }
188
189 if (work[offset] == '"') {
190 sscanf(work + offset + 1, "%[^\"]", parsed);
191 } else if (work[offset] == '\'') {
192 sscanf(work + offset + 1, "%[^']", parsed);
193 } else {
194 sscanf(work + offset, "%s", parsed);
195 }
196
197 ret = table_put(table, code, parsed);
198 if (ret == -1) {
199 fprintf(stderr, "Failed to add row 0x%04x \"%s\" to translation table\n", code, parsed);
200 table_free(table);
201 fclose(table_file);
202 free(work);
203 free(parsed);
204 return NULL;
205 }
206
207 check = table_get(table, code);
208 if (check == NULL || strcmp(parsed, check) != 0) {
209 fprintf(stderr, "Failed to retrieve row 0x%04x \"%s\" in translation table\n", code, parsed);
210 table_free(table);
211 fclose(table_file);
212 free(work);
213 free(parsed);
214 return NULL;
215 }
216
217 if (strlen(parsed) > check_max_data_length) {
218 check_max_data_length = strlen(parsed);
219 }
220
221 if (code < last) {
222 printf("warning - code 0x%04X appears after 0x%04X\n", code, last);
223 } else if (code == last) {
224 printf("warning - code 0x%04X appears twice in a row\n", code);
225 }
226 last = code;
227 }
228
229 if (table->max_data_length != check_max_data_length) {
230 printf("warning - table max length (%d) doesn't match our calculation (%d)\n", table->max_data_length, check_max_data_length);
231 }
232
233 free(work);
234 free(parsed);
235 fclose(table_file);
236
237 return table;
238 }
239
240 static table_t *check_table_again(char *filename, table_t *source)
241 {
242 FILE *table_file;
243 char *work;
244 int code;
245 int offset;
246 char *parsed;
247 int err;
248 int ret;
249 int state;
250 char *check;
251
252 table_t *table;
253
254 struct stat table_stat;
255
256 err = stat(filename, &table_stat);
257 if (err == -1) {
258 return NULL;
259 }
260
261 table = table_resize(source, ((int) ceil(source->used / BUILTIN_TABLE_MULTIPLE) + 1) * BUILTIN_TABLE_MULTIPLE, 1);
262
263 if (table == NULL) {
264 return NULL;
265 }
266
267 table_file = fopen(filename, "r");
268 if (table_file == NULL) {
269 fprintf(stderr, "Unable to open translation table: %s\n", strerror(errno));
270 return NULL;
271 }
272
273 work = wrapped_malloc(1024);
274
275 parsed = wrapped_malloc(1024);
276
277 state = BASE_STATE;
278
279 while (fgets(work, 1024, table_file) != NULL) {
280 if (*work == '#') {
281
282 /*
283 * Don't even bother
284 */
285 continue;
286 }
287
288 parsed[0] = '\0';
289
290 if (state == BASE_STATE) {
291 ret = sscanf(work, " %s %n", parsed, &offset);
292
293 if (ret == 0) {
294 continue;
295 }
296
297 if (strncasecmp(parsed, "start", 5) == 0) {
298 if (work[offset] == '\0') {
299 // All languages
300 state = INSIDE_STATE;
301 continue;
302 }
303
304 if (work[offset] == '"') {
305 sscanf(work + offset + 1, "%[^\"]", parsed);
306 } else if (work[offset] == '\'') {
307 sscanf(work + offset + 1, "%[^']", parsed);
308 } else {
309 sscanf(work + offset, "%s", parsed);
310 }
311
312 printf("skipping language subsection for %s\n", parsed);
313
314 continue;
315 }
316
317 if (strncasecmp(parsed, "default", 7) == 0) {
318 continue;
319 }
320
321 continue;
322 }
323
324 /*
325 * Inside state
326 */
327
328 code = -1;
329
330 ret = sscanf(work, "%i %n", &code, &offset);
331
332 if (ret == 0 || code < 0 || offset < 0) {
333
334 /*
335 * Check for end
336 */
337 ret = sscanf(work, " %s %n", parsed, &offset);
338
339 if (ret > 0 && strncasecmp(parsed, "end", 5) == 0) {
340 state = BASE_STATE;
341 }
342
343 continue;
344 }
345
346 if (work[offset] == '\0') {
347 continue;
348 }
349
350 if (work[offset] == '"') {
351 sscanf(work + offset + 1, "%[^\"]", parsed);
352 } else if (work[offset] == '\'') {
353 sscanf(work + offset + 1, "%[^']", parsed);
354 } else {
355 sscanf(work + offset, "%s", parsed);
356 }
357
358 check = table_get(table, code);
359 if (check == NULL || strcmp(parsed, check) != 0) {
360 fprintf(stderr, "Failed to retrieve row 0x%04x \"%s\" in resized translation table\n", code, parsed);
361 table_free(table);
362 fclose(table_file);
363 free(work);
364 free(parsed);
365 return NULL;
366 }
367 }
368
369 free(work);
370 free(parsed);
371 fclose(table_file);
372
373 return table;
374 }
375
376 int main(int argc, char **argv)
377 {
378 table_t *table;
379 table_t *new_table;
380 int optcode;
381 int builtin = 0;
382 int dump = 0;
383 int use_hash = 1;
384 int verbose = 0;
385
386 while ((optcode = getopt(argc, argv, "bdlhv")) != -1) {
387 switch (optcode) {
388 case 'b': // simulate a builtin table
389 builtin = 1;
390 break;
391
392 case 'd':
393 dump = 1;
394 break;
395
396 case 'h':
397 printf("check-table [-bdlhv] filename.tbl\n");
398 return 0;
399
400 case 'l': // linear
401 use_hash = 0;
402 break;
403
404 case 'v':
405 verbose = 1;
406 break;
407
408 default:
409 fprintf(stderr, "unknown option: %c\n", optcode);
410 return -1;
411 }
412 }
413
414 if (optind < argc) {
415 printf("checking: %s\n", argv[optind]);
416 table = check_table(argv[optind], use_hash);
417 if (builtin) {
418 new_table = check_table_again(argv[optind], table);
419 table_free(table);
420 table = new_table;
421 new_table = NULL;
422 }
423 if (table) {
424 if (dump) {
425 table_dump(table, verbose);
426 }
427 table_stats(table);
428 table_free(table);
429 }
430 } else {
431 printf("please specify a file to operate on\n");
432 return -1;
433 }
434
435 return 0;
436 }