Debian Codegraph @

extra/generate-display-names/generate.c

raw symbols SCIP
1 #include <config.h>
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include "internal.h"
9 #include "displayLanguage.h"
10
11 static const char *
12 displayLanguage(const char *lang) {
13 return DisplayLanguage((char *)lang);
14 }
15
16 static const char *
17 displayRegion(const char *region) {
18 return DisplayRegion((char *)region);
19 }
20
21 static char *
22 generateDisplayName(const char *table) {
23 char *name;
24 char *language;
25 char *region;
26 int typeIsLiterary = 0;
27 int typeIsComputer = 0;
28 char *dots;
29 char *contraction;
30 char *grade;
31 char *version;
32 char *query;
33 char **matches;
34 char *n;
35 char *q;
36 char **m;
37 int tableLen = strlen(table);
38 name = (char *)malloc(100 * sizeof(*name));
39 n = name;
40 query = (char *)malloc(100 * sizeof(*query));
41 q = query;
42 language = lou_getTableInfo(table, "language");
43 if (!language || language[0] == '*') return NULL;
44 n += sprintf(n, "%s", displayLanguage(language));
45 q += sprintf(q, "language:%s", language);
46 region = lou_getTableInfo(table, "region");
47 if (region) q += sprintf(q, " region:%s", region);
48 {
49 char *q_save = q;
50 q += sprintf(q, " type:literary");
51 matches = lou_findTables(query);
52 if (matches) {
53 for (m = matches; *m; m++) {
54 if (!typeIsLiterary) {
55 int matchLen = strlen(*m);
56 if (tableLen < matchLen && !strcmp(&(*m)[matchLen - tableLen], table)) {
57 typeIsLiterary = 1;
58 q_save = q;
59 }
60 }
61 // free(*m);
62 }
63 // free(matches);
64 }
65 if (!typeIsLiterary) {
66 q = q_save;
67 *q = '\0';
68 }
69 }
70 if (typeIsLiterary) {
71 int uncontracted = 0;
72 int fullyContracted = 0;
73 int partiallyContracted = 0;
74 int otherUncontracted = 0;
75 int otherFullyContracted = 0;
76 int otherPartiallyContracted = 0;
77 int twoOrMorePartiallyContracted = 0;
78 contraction = lou_getTableInfo(table, "contraction");
79 if (contraction) {
80 char *q_save = q;
81 uncontracted = !strcmp(contraction, "no");
82 fullyContracted = !strcmp(contraction, "full");
83 partiallyContracted = !strcmp(contraction, "partial");
84 otherUncontracted = 0;
85 q += sprintf(q, " contraction:no");
86 matches = lou_findTables(query);
87 if (matches) {
88 if (!uncontracted || matches[0] && matches[1]) otherUncontracted = 1;
89 // for (m = matches; *m; m++) free(*m);
90 // free(matches);
91 }
92 q = q_save;
93 *q = '\0';
94 otherPartiallyContracted = 0;
95 twoOrMorePartiallyContracted = 0;
96 grade = NULL;
97 q += sprintf(q, " contraction:partial");
98 matches = lou_findTables(query);
99 if (matches) {
100 for (m = matches; *m; m++) {
101 if (!twoOrMorePartiallyContracted) {
102 char *g = lou_getTableInfo(*m, "grade");
103 if (g) {
104 if (!grade)
105 grade = g;
106 else if (strcmp(grade, g))
107 twoOrMorePartiallyContracted = 1;
108 }
109 }
110 // free(*m);
111 }
112 // free(matches);
113 if (!partiallyContracted || twoOrMorePartiallyContracted)
114 otherPartiallyContracted = 1;
115 if (twoOrMorePartiallyContracted)
116 grade = lou_getTableInfo(table, "grade");
117 else
118 grade = NULL;
119 }
120 q = q_save;
121 *q = '\0';
122 otherFullyContracted = 0;
123 q += sprintf(q, " contraction:full");
124 matches = lou_findTables(query);
125 if (matches) {
126 if (!fullyContracted || matches[0] && matches[1])
127 otherFullyContracted = 1;
128 // for (m = matches; *m; m++) free(*m);
129 // free(matches);
130 }
131 q = q_save;
132 *q = '\0';
133 }
134 dots = lou_getTableInfo(table, "dots");
135 if (dots) {
136 int otherDots = 0;
137 char *q_save = q;
138 if (contraction)
139 q += sprintf(q, " contraction:%s", contraction);
140 matches = lou_findTables(query);
141 if (matches) {
142 for (m = matches; *m; m++) {
143 if (!otherDots) {
144 char *d = lou_getTableInfo(*m, "dots");
145 if (d && strcmp(dots, d)) otherDots = 1;
146 }
147 // free(*m);
148 }
149 // free(matches);
150 }
151 if (otherDots) n += sprintf(n, " %s-dot", dots);
152 q = q_save;
153 *q = '\0';
154 q += sprintf(q, " dots:%s", dots);
155 // free(dots);
156 }
157 // free(contraction);
158 if (uncontracted) {
159 if (otherFullyContracted || otherPartiallyContracted)
160 n += sprintf(n, " uncontracted");
161 } else if (fullyContracted) {
162 if (otherPartiallyContracted) {
163 if (twoOrMorePartiallyContracted && grade)
164 n += sprintf(n, " grade %s contracted", grade);
165 else
166 n += sprintf(n, " contracted");
167 } else if (otherUncontracted) {
168 n += sprintf(n, " contracted");
169 }
170 } else if (partiallyContracted) {
171 if (twoOrMorePartiallyContracted && grade)
172 n += sprintf(n, " grade %s contracted", grade);
173 else
174 n += sprintf(n, " partially contracted");
175 }
176 // free(grade);
177 } else {
178 {
179 char *q_save = q;
180 q += sprintf(q, " type:computer");
181 matches = lou_findTables(query);
182 if (matches) {
183 for (m = matches; *m; m++) {
184 if (!typeIsComputer) {
185 int matchLen = strlen(*m);
186 if (tableLen < matchLen && !strcmp(&(*m)[matchLen - tableLen], table)) {
187 typeIsComputer = 1;
188 q_save = q;
189 }
190 }
191 // free(*m);
192 }
193 // free(matches);
194 }
195 if (!typeIsComputer) {
196 q = q_save;
197 *q = '\0';
198 }
199 }
200 if (typeIsComputer) {
201 dots = lou_getTableInfo(table, "dots");
202 if (dots) {
203 if (!strcmp(dots, "6")) {
204 n += sprintf(n, " %s-dot", dots);
205 } else {
206 char *q_save = q;
207 q += sprintf(q, " dots:6");
208 matches = lou_findTables(query);
209 if (matches) {
210 n += sprintf(n, " %s-dot", dots);
211 // for (m = matches; *m; m++) free(*m);
212 // free(matches);
213 }
214 q = q_save;
215 *q = '\0';
216 }
217 q += sprintf(q, " dots:%s", dots);
218 // free(dots);
219 }
220 n += sprintf(n, " computer");
221 }
222 }
223 n += sprintf(n, " braille");
224 if (region && strlen(region) > strlen(language) &&
225 !strncmp(language, region, strlen(language)) &&
226 region[strlen(language)] == '-') {
227 char *r = displayRegion(&region[strlen(language) + 1]);
228 if (r && *r) n += sprintf(n, " as used in %s", r);
229 }
230 // free(region);
231 // free(language);
232 version = lou_getTableInfo(table, "version");
233 if (version) {
234 int otherVersions = 0;
235 matches = lou_findTables(query);
236 if (matches) {
237 for (m = matches; *m; m++) {
238 if (!otherVersions) {
239 char *v = lou_getTableInfo(*m, "version");
240 if (!v || strcmp(version, v)) otherVersions = 1;
241 }
242 // free(*m);
243 }
244 // free(matches);
245 if (otherVersions) n += sprintf(n, " (%s standard)", version);
246 }
247 // free(version);
248 }
249 return name;
250 }
251
252 int
253 main(int argc, char **argv) {
254 int COLUMN_INDEX_NAME = -1;
255 int COLUMN_DISPLAY_NAME = -1;
256 int result = 0;
257 FILE *fp;
258 char *line = NULL;
259 size_t len = 0;
260 if (argc != 2) {
261 fprintf(stderr, "One argument expected\n");
262 exit(EXIT_FAILURE);
263 }
264 fp = fopen(argv[1], "rb");
265 if (!fp) {
266 fprintf(stderr, "Could not open file: %s\n", argv[1]);
267 exit(EXIT_FAILURE);
268 }
269 lou_setLogLevel(LOU_LOG_WARN);
270 char cwd[PATH_MAX];
271 if (getcwd(cwd, sizeof(cwd)) == NULL) {
272 fprintf(stderr, "Unexpected error\n");
273 exit(EXIT_FAILURE);
274 }
275 int cwdLen = strlen(cwd);
276 char *tablePath = _lou_getTablePath();
277 if (strncmp(cwd, tablePath, cwdLen) || tablePath[cwdLen] != '/') {
278 fprintf(stderr, "Unexpected table path: %s\n", tablePath);
279 exit(EXIT_FAILURE);
280 }
281 int tablePathLen = strlen(tablePath);
282 char **tables = lou_listTables();
283 int tableCount = 0;
284 for (char **t = tables; *t; t++) {
285 tableCount++;
286 if (strncmp(tablePath, *t, tablePathLen) || (*t)[tablePathLen] != '/') {
287 fprintf(stderr, "Unexpected table location: %s\n", *t);
288 exit(EXIT_FAILURE);
289 }
290 }
291 free(tablePath);
292 while (getline(&line, &len, fp) != -1) {
293 char *cp = line;
294 int generate = 0;
295 if (*cp == '*') {
296 generate = 1;
297 cp++;
298 }
299 while (*cp == ' ') cp++;
300 if (*cp == '\0' || *cp == '\n' || *cp == '#') {
301 if (!generate)
302 continue;
303 else
304 goto parse_error;
305 }
306 char *table = cp;
307 cp++;
308 while (*cp != ' ' && *cp != '\0' && *cp != '\n' && *cp != '#') cp++;
309 if (*cp != ' ') goto parse_error;
310 *cp = '\0';
311 cp++;
312 while (*cp == ' ') cp++;
313 if (*cp == '\0' || *cp == '\n' || *cp == '#') goto parse_error;
314 char *expectedIndexName = cp;
315 if (COLUMN_INDEX_NAME < 0)
316 COLUMN_INDEX_NAME = expectedIndexName - line;
317 else if (expectedIndexName != line + COLUMN_INDEX_NAME)
318 goto parse_error;
319 while (*cp != '\0' && *cp != '\n' && *cp != '#') {
320 if (*cp == ' ' && cp[1] == ' ') {
321 *cp = '\0';
322 cp++;
323 break;
324 }
325 cp++;
326 }
327 if (*cp != ' ') goto parse_error;
328 while (*cp == ' ') cp++;
329 if (*cp == '\0' || *cp == '\n' || *cp == '#') goto parse_error;
330 char *expectedDisplayName = cp;
331 if (COLUMN_DISPLAY_NAME < 0)
332 COLUMN_DISPLAY_NAME = expectedDisplayName - line;
333 else if (expectedDisplayName != line + COLUMN_DISPLAY_NAME)
334 goto parse_error;
335 while (*cp != '\0' && *cp != '\n' && *cp != '#') {
336 if (*cp == ' ' && cp[1] == ' ') break;
337 cp++;
338 }
339 *cp = '\0';
340 int found = 0;
341 for (int k = 0; k < tableCount; k++) {
342 if (tables[k]) {
343 if (!strcmp(&tables[k][cwdLen + 1], table)) {
344 tables[k] = NULL;
345 found = 1;
346 break;
347 }
348 }
349 }
350 if (!found) {
351 fprintf(stderr, "Table not in table path: %s\n", table);
352 result = 1;
353 }
354 const char *actualIndexName = lou_getTableInfo(table, "index-name");
355 if (!actualIndexName) {
356 fprintf(stderr, "No index-name field in table %s\n", table);
357 result = 1;
358 } else {
359 if (strcmp(actualIndexName, expectedIndexName) != 0) {
360 fprintf(stderr, "%s: index-name: %s != %s\n", table, actualIndexName,
361 expectedIndexName);
362 fprintf(stderr,
363 " cat %s | sed 's/^\\(#-index-name: *\\).*$/\\1%s/g' > "
364 "%s.tmp\n",
365 table, expectedIndexName, table);
366 fprintf(stderr, " mv %s.tmp %s\n", table, table);
367 result = 1;
368 }
369 }
370 const char *actualDisplayName = lou_getTableInfo(table, "display-name");
371 if (!actualDisplayName) {
372 fprintf(stderr, "No display-name field in table %s\n", table);
373 result = 1;
374 } else {
375 if (strcmp(actualDisplayName, expectedDisplayName) != 0) {
376 fprintf(stderr, "%s: display-name: %s != %s\n", table, actualDisplayName,
377 expectedDisplayName);
378 fprintf(stderr,
379 " cat %s | sed 's/^\\(#-display-name: *\\).*$/\\1%s/g' > "
380 "%s.tmp\n",
381 table, expectedDisplayName, table);
382 fprintf(stderr, " mv %s.tmp %s\n", table, table);
383 result = 1;
384 }
385 const char *generatedDisplayName = generateDisplayName(table);
386 if (!generatedDisplayName || !*generatedDisplayName) {
387 if (generate) {
388 fprintf(stderr, "No display-name could be generated for table %s\n",
389 table);
390 result = 1;
391 }
392 } else if (strcmp(actualDisplayName, generatedDisplayName) != 0) {
393 if (generate) {
394 fprintf(stderr, "%s: generated display-name: %s != %s\n", table, actualDisplayName,
395 generatedDisplayName);
396 result = 1;
397 }
398 } else {
399 if (!generate) {
400 fprintf(stderr, "%s: %s == %s\n", table, actualDisplayName,
401 generatedDisplayName);
402 result = 1;
403 }
404 }
405 }
406 continue;
407 parse_error:
408 fprintf(stderr, "Could not parse line: %s\n", line);
409 exit(EXIT_FAILURE);
410 }
411 free(line);
412 for (int k = 0; k < tableCount; k++) {
413 if (tables[k]) {
414 fprintf(stderr, "Table not in list: %s\n", tables[k]);
415 result = 1;
416 free(tables[k]);
417 }
418 }
419 free(tables);
420 return result;
421 }