clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name cfgfile.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/home/kfp/aldor/aldor/aldor/subcmd/unitools -fcoverage-compilation-dir=/home/kfp/aldor/aldor/aldor/subcmd/unitools -resource-dir /usr/local/lib/clang/18 -D PACKAGE_NAME="aldor" -D PACKAGE_TARNAME="aldor" -D PACKAGE_VERSION="1.4.0" -D PACKAGE_STRING="aldor 1.4.0" -D PACKAGE_BUGREPORT="aldor@xinutec.org" -D PACKAGE_URL="" -D PACKAGE="aldor" -D VERSION="1.4.0" -D YYTEXT_POINTER=1 -D HAVE_STDIO_H=1 -D HAVE_STDLIB_H=1 -D HAVE_STRING_H=1 -D HAVE_INTTYPES_H=1 -D HAVE_STDINT_H=1 -D HAVE_STRINGS_H=1 -D HAVE_SYS_STAT_H=1 -D HAVE_SYS_TYPES_H=1 -D HAVE_UNISTD_H=1 -D STDC_HEADERS=1 -D HAVE_LIBREADLINE=1 -D HAVE_READLINE_READLINE_H=1 -D HAVE_READLINE_HISTORY=1 -D HAVE_READLINE_HISTORY_H=1 -D USE_GLOOP_SHELL=1 -D GENERATOR_COROUTINES=0 -D HAVE_DLFCN_H=1 -D LT_OBJDIR=".libs/" -I . -I ./../../src -I ../../src -internal-isystem /usr/local/lib/clang/18/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2026-01-15-223856-845667-1 -x c cfgfile.c
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "cfgfile.h" |
| 10 | #include "debug.h" |
| 11 | #include "file.h" |
| 12 | #include "opsys.h" |
| 13 | #include "stdio.h0" |
| 14 | #include "store.h" |
| 15 | #include "strops.h" |
| 16 | |
| 17 | Bool cfgDebug = false; |
| 18 | |
| 19 | #define cfgDEBUG DEBUG_IF(cfg) afprintf |
| 20 | |
| 21 | static String cfgGetLine(FILE *file, Bool *atEof); |
| 22 | static Bool cfgIsSection(char *line, char *name); |
| 23 | static void cfgReadAddError(String); |
| 24 | static void cfgReadDupItemError(ConfigItem, String); |
| 25 | static Bool cfgEqual(ConfigItem, ConfigItem); |
| 26 | static ConfigItemList cfgGetKeys(FILE *file, String section); |
| 27 | static ConfigItem cfgParseLine(String line); |
| 28 | |
| 29 | CREATE_LIST(ConfigItem); |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | #define IsWhiteSpace(c) ((c)==' ' || (c)=='\n' || (c) == '\t') |
| 37 | |
| 38 | static StringList cfgExpandList (StringList *plst, |
| 39 | StringList **end, |
| 40 | ConfigItemList lst, |
| 41 | Bool keep); |
| 42 | static Bool cfgCheckCondition(String name, ConfigItemList cfg); |
| 43 | static ConfigItem cfgExpandVar(ConfigItem, ConfigItemList); |
| 44 | |
| 45 | static String cfgKeyNameValue(String str); |
| 46 | static String cfgStringValue(String str); |
| 47 | static StringList cfgStringListValue(String str); |
| 48 | static Bool cfgBooleanValue(String str); |
| 49 | static String cfgKeyNameValue(String str); |
| 50 | |
| 51 | static ConfigItem |
| 52 | cfgExpandVar(ConfigItem item, ConfigItemList lst) |
| 53 | { |
| 54 | char c; |
| 55 | int i; |
| 56 | |
| 57 | if (!item) return NULL; |
| 58 | i = 0; |
| 59 | while ( (c = cfgVal(item)[i]) == ' ' || c == '\t') i++; |
| 60 | if (cfgVal(item)[i] == '$') { |
| 61 | item = cfgLookup(cfgKeyNameValue(&cfgVal(item)[i+1]), lst); |
| 62 | return cfgExpandVar(item, lst); |
| 63 | } |
| 64 | return item; |
| 65 | } |
| 66 | |
| 67 | ConfigItemList |
| 68 | cfgLookupList(char *string, ConfigItemList lst) |
| 69 | { |
| 70 | ConfigItemList result = listNil(ConfigItem); |
| 71 | |
| 72 | cfgDEBUG(dbOut, "Getting key list: %s\n", string); |
| 73 | |
| 74 | while (lst != listNil(ConfigItem)) { |
| 75 | if (strEqual(string, cfgName(car(lst)))) |
| 76 | result = listCons(ConfigItem)(car(lst), result); |
| 77 | lst = cdr(lst); |
| 78 | } |
| 79 | return listNReverse(ConfigItem)(result); |
| 80 | } |
| 81 | |
| 82 | ConfigItem |
| 83 | cfgLookup(char *string, ConfigItemList lst) |
| 84 | { |
| 85 | cfgDEBUG(dbOut, "Getting key: %s\n", string); |
| 86 | |
| 87 | while (lst != listNil(ConfigItem)) { |
| 88 | if (strEqual(string, cfgName(car(lst)))) |
| 89 | return car(lst); |
| 90 | lst = cdr(lst); |
| 91 | } |
| 92 | return NULL; |
| 93 | } |
| 94 | |
| 95 | static StringList |
| 96 | cfgLookupUnexpandedStringList(String str, ConfigItemList lst) |
| 97 | { |
| 98 | ConfigItem item = cfgLookup(str, lst); |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | if (!item) { |
| 105 | printf("config file: failed to find key `%s'\n", str); |
| 106 | return NULL; |
| 107 | } |
| 108 | return cfgStringListValue(cfgVal(item)); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | StringList |
| 113 | cfgLookupStringList(String str, ConfigItemList lst) |
| 114 | { |
| 115 | StringList ll, *foo; |
| 116 | ll = cfgLookupUnexpandedStringList(str, lst); |
| 117 | |
| 118 | if (ll == listNil(String)) |
| 1 | Assuming 'll' is not equal to listNil(String) | |
|
| |
| 119 | return NULL; |
| 120 | ll = cfgExpandList(&ll, &foo, lst, true); |
| |
| 121 | return ll; |
| 122 | } |
| 123 | |
| 124 | #define cfgIfTerminator(x) ((x)[1] == '\0' && ((x)[0] == ':' || (x)[0] == ';')) |
| 125 | |
| 126 | static StringList |
| 127 | cfgExpandList(StringList *plst, StringList **end, ConfigItemList cfg, Bool keep) |
| 128 | { |
| 129 | StringList *pp; |
| 130 | StringList head; |
| 131 | String error = NULL; |
| 132 | pp = plst; |
| 133 | head = *plst; |
| 134 | while (*pp != listNil(String) && !cfgIfTerminator(car(*pp))) { |
| 4 | | Assuming the condition is false | |
|
| 5 | | Loop condition is true. Entering loop body | |
|
| 14 | | Assuming the condition is false | |
|
| 15 | | Loop condition is true. Entering loop body | |
|
| 19 | | Execution continues on line 134 | |
|
| 20 | | Assuming pointer value is null | |
|
| 135 | String name; |
| 136 | if (car(*pp)[0] != '$') { |
| 6 | | Assuming the condition is false | |
|
| |
| 16 | | Assuming the condition is true | |
|
| |
| 137 | if (keep) pp = &cdr(*pp); |
| |
| 138 | else *pp = cdr(*pp); |
| 139 | continue; |
| 140 | } |
| 141 | name = &(car(*pp)[1]); |
| 142 | if (name[0] == '?') { |
| 8 | | Assuming the condition is true | |
|
| |
| 143 | Bool flg = cfgCheckCondition(&name[1], cfg); |
| 144 | StringList *iend; |
| 145 | *pp = cdr(*pp); |
| 146 | *pp = cfgExpandList(pp, &iend, cfg, flg && keep); |
| 147 | if (car(*iend)[0] != ':') |
| |
| 148 | error = "Missing `:'"; |
| 149 | if (cdr(*iend)) *iend = cdr(*iend); |
| 11 | | Assuming field 'rest' is non-null | |
|
| |
| 150 | else |
| 151 | error = "Nothing after `:'."; |
| 152 | cfgExpandList(iend, &iend, cfg, !flg && keep); |
| 13 | | Calling 'cfgExpandList' | |
|
| 22 | | Returning from 'cfgExpandList' | |
|
| 153 | |
| 154 | *iend = cdr(*iend); |
| 23 | | Access to field 'rest' results in a dereference of a null pointer |
|
| 155 | pp = iend; |
| 156 | } |
| 157 | else { |
| 158 | StringList ll; |
| 159 | if (!keep) *pp = cdr(*pp); |
| 160 | else { |
| 161 | ll = cfgLookupUnexpandedStringList(name, cfg); |
| 162 | *pp = listNConcat(String)(ll, cdr(*pp)); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | if (error) |
| |
| 167 | printf("Error in cfg: %s\n", error); |
| 168 | *end = pp; |
| 169 | return *plst; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | String |
| 174 | cfgLookupKeyName(String str, ConfigItemList lst) |
| 175 | { |
| 176 | ConfigItem item = cfgLookup(str, lst); |
| 177 | item = cfgExpandVar(item, lst); |
| 178 | |
| 179 | if (!item) { |
| 180 | return NULL; |
| 181 | } |
| 182 | return cfgKeyNameValue(cfgVal(item)); |
| 183 | } |
| 184 | |
| 185 | StringList |
| 186 | cfgLookupKeyNameList(String str, ConfigItemList lst) |
| 187 | { |
| 188 | ConfigItemList items = cfgLookupList(str, lst); |
| 189 | StringList result = listNil(String); |
| 190 | |
| 191 | |
| 192 | |
| 193 | for (; items; items = cdr(items)) { |
| 194 | String value; |
| 195 | ConfigItem item; |
| 196 | |
| 197 | |
| 198 | |
| 199 | item = car(items); |
| 200 | |
| 201 | |
| 202 | |
| 203 | item = cfgExpandVar(item, lst); |
| 204 | |
| 205 | |
| 206 | |
| 207 | if (!item) continue; |
| 208 | |
| 209 | |
| 210 | |
| 211 | value = cfgKeyNameValue(cfgVal(item)); |
| 212 | |
| 213 | |
| 214 | |
| 215 | result = listCons(String)(value, result); |
| 216 | } |
| 217 | |
| 218 | return listNReverse(String)(result); |
| 219 | } |
| 220 | |
| 221 | String |
| 222 | cfgLookupString(String str, ConfigItemList lst) |
| 223 | { |
| 224 | ConfigItem item = cfgLookup(str, lst); |
| 225 | item = cfgExpandVar(item, lst); |
| 226 | |
| 227 | if (!item) { |
| 228 | return NULL; |
| 229 | } |
| 230 | return cfgStringValue(cfgVal(item)); |
| 231 | } |
| 232 | |
| 233 | Bool |
| 234 | cfgLookupBoolean(String str, ConfigItemList lst) |
| 235 | { |
| 236 | ConfigItem item = cfgLookup(str, lst); |
| 237 | item = cfgExpandVar(item, lst); |
| 238 | |
| 239 | if (!item) { |
| 240 | return 0; |
| 241 | } |
| 242 | return cfgBooleanValue(cfgVal(item)); |
| 243 | } |
| 244 | |
| 245 | static StringList |
| 246 | cfgStringListValue(String str) |
| 247 | { |
| 248 | int argc; |
| 249 | char **argv; |
| 250 | StringList lst; |
| 251 | int i; |
| 252 | |
| 253 | if (str[0] == ',') |
| 254 | cstrParseCommaified(str+1, &argc, &argv); |
| 255 | else |
| 256 | cstrParseUnquoted(str, &argc, &argv); |
| 257 | |
| 258 | lst = listNil(String); |
| 259 | for (i=0; i<argc; i++) lst = listCons(String)(argv[argc - i - 1], lst); |
| 260 | |
| 261 | return lst; |
| 262 | } |
| 263 | |
| 264 | static Bool |
| 265 | cfgBooleanValue(String str) |
| 266 | { |
| 267 | String s = cfgKeyNameValue(str); |
| 268 | |
| 269 | if (strEqual(s, "yes")) return true; |
| 270 | if (strEqual(s, "no")) return false; |
| 271 | if (strEqual(s, "true")) return true; |
| 272 | if (strEqual(s, "false")) return false; |
| 273 | |
| 274 | printf("Illegal value for boolean: %s\n", s); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | static String |
| 279 | cfgKeyNameValue(String str) |
| 280 | { |
| 281 | char **argv; |
| 282 | int argc; |
| 283 | cstrParseUnquoted(str, &argc, &argv); |
| 284 | if (argc != 1) { |
| 285 | printf("Bad identifier format: `%s'\n", str); |
| 286 | return ""; |
| 287 | } |
| 288 | |
| 289 | str = argv[0]; |
| 290 | stoFree(argv); |
| 291 | return str; |
| 292 | } |
| 293 | |
| 294 | static String |
| 295 | cfgStringValue(String str) |
| 296 | { |
| 297 | char *p, *start, *new; |
| 298 | |
| 299 | p = str; |
| 300 | while (IsWhiteSpace(*p) && *p != '\0') p++; |
| 301 | if (*p == '"') p++; |
| 302 | if (*p == '\0') return strCopy(""); |
| 303 | start = p; |
| 304 | p = start + strlen(start) - 1; |
| 305 | while (IsWhiteSpace(*p) && p != start) p--; |
| 306 | if (*p == '"') p--; |
| 307 | new = strCopy(start); |
| 308 | new[p - start +1] = '\0'; |
| 309 | return new; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | void |
| 321 | cstrParseUnquoted(char *str, int *pargc, char ***pargv) |
| 322 | { |
| 323 | char *p = str; |
| 324 | char *wstart; |
| 325 | char **argv; |
| 326 | char c; |
| 327 | int n, lim; |
| 328 | |
| 329 | n = 0; |
| 330 | lim = 0; |
| 331 | argv = NULL; |
| 332 | while (1) { |
| 333 | while (IsWhiteSpace(*p) && *p != '\0') p++; |
| 334 | if (*p == '\0') break; |
| 335 | wstart = p; |
| 336 | |
| 337 | while (!IsWhiteSpace(*p) && *p != '\0') p++; |
| 338 | c = *p; |
| 339 | *p = '\0'; |
| 340 | if (n==lim) { |
| 341 | char **targv = argv; |
| 342 | int i; |
| 343 | argv = (char **) stoAlloc(OB_Other, (n + 5)*sizeof(char *)); |
| 344 | lim += 5; |
| 345 | for (i=0; i<n; i++) argv[i] = targv[i]; |
| 346 | if (targv) stoFree(targv); |
| 347 | } |
| 348 | argv[n++] = strCopy(wstart); |
| 349 | *p = c; |
| 350 | if (c == '\0') break; |
| 351 | } |
| 352 | |
| 353 | *pargc = n; |
| 354 | *pargv = argv; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | void |
| 359 | cstrParseCommaified(char *opts, int *pargc, char ***pargv) |
| 360 | { |
| 361 | String str = strCopy(opts); |
| 362 | char **argv; |
| 363 | char *p, *q; |
| 364 | int argc, i; |
| 365 | |
| 366 | p = str; |
| 367 | q = p; |
| 368 | argc = 1; |
| 369 | while (*p != '\0') { |
| 370 | if (*p == '\\') |
| 371 | p++; |
| 372 | else if (*p == ',') { |
| 373 | *p = '\0'; |
| 374 | argc++; |
| 375 | } |
| 376 | *q = *p; |
| 377 | p++; q++; |
| 378 | } |
| 379 | *q = '\0'; |
| 380 | p = str; |
| 381 | argv = (char **) stoAlloc(OB_Other, argc * sizeof(char *)); |
| 382 | for (i=0; i<argc; i++) { |
| 383 | argv[i] = p; |
| 384 | while (*p != '\0') p++; |
| 385 | p++; |
| 386 | } |
| 387 | |
| 388 | *pargc = argc; |
| 389 | *pargv = argv; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | static int (*cfgCondFn)(String); |
| 404 | |
| 405 | local void cfgFreeConfPath(void); |
| 406 | |
| 407 | void |
| 408 | cfgSetCondFunc(int (*check)(String)) |
| 409 | { |
| 410 | cfgCondFn = check; |
| 411 | } |
| 412 | |
| 413 | static Bool |
| 414 | cfgCheckCondition(String name, ConfigItemList cfg) |
| 415 | { |
| 416 | int val; |
| 417 | if ( (val = (*cfgCondFn)(name)) != -1) |
| 418 | return val; |
| 419 | |
| 420 | return cfgLookupBoolean(name, cfg); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | static StringList cfgPath; |
| 430 | |
| 431 | ConfigItem |
| 432 | cfgNew(char *key, char *val) |
| 433 | { |
| 434 | ConfigItem item; |
| 435 | int nkey, nval; |
| 436 | nkey = strlen(key); |
| 437 | nval = strlen(val); |
| 438 | |
| 439 | item = (ConfigItem) stoAlloc(OB_Other, fullsizeof(struct _ConfigItem, nkey + nval + 2, char)); |
| 440 | item->optName = item->content; |
| 441 | strcpy(item->optName, key); |
| 442 | item->optVal = item->content + nkey + 1; |
| 443 | strcpy(item->optVal, val); |
| 444 | |
| 445 | return item; |
| 446 | } |
| 447 | |
| 448 | void cfgFree(ConfigItem item) |
| 449 | { |
| 450 | stoFree(item); |
| 451 | } |
| 452 | |
| 453 | void cfgSetConfPath(char *path) |
| 454 | { |
| 455 | Bool done; |
| 456 | String s = strCopy(path); |
| 457 | char *start, *p; |
| 458 | p = s; |
| 459 | cfgPath = listNil(String); |
| 460 | done = false; |
| 461 | while (!done) { |
| 462 | char c; |
| 463 | start = p; |
| 464 | while (*p != '\0' && *p != osPathSeparator()) |
| 465 | p++; |
| 466 | c = *p; |
| 467 | *p = '\0'; |
| 468 | cfgPath = listCons(String)(start, cfgPath); |
| 469 | if (c == '\0') done = true; |
| 470 | p++; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | local void |
| 475 | cfgFreeConfPath(void) |
| 476 | { |
| 477 | if (cfgPath) strFree(car(cfgPath)); |
| 478 | listFree(String)(cfgPath); |
| 479 | } |
| 480 | |
| 481 | FileName |
| 482 | cfgFindFile(String basename, String ext) |
| 483 | { |
| 484 | StringList lst; |
| 485 | |
| 486 | lst = cfgPath; |
| 487 | while (lst != listNil(String)) { |
| 488 | FileName name = fnameNew(car(lst), basename, ext); |
| 489 | if (fileIsOpenable(name, osIoRdMode)) |
| 490 | return name; |
| 491 | lst = cdr(lst); |
| 492 | } |
| 493 | |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | static StringList CfgReadErrs = listNil(String); |
| 504 | |
| 505 | |
| 506 | ConfigItemList |
| 507 | cfgRead(FILE *file, char *name) |
| 508 | { |
| 509 | Bool done = false; |
| 510 | |
| 511 | |
| 512 | cfgReadClearErrors(); |
| 513 | |
| 514 | |
| 515 | |
| 516 | while (!done) { |
| 517 | String line = cfgGetLine(file, &done); |
| 518 | if (cfgIsSection(line, name)) |
| 519 | return cfgGetKeys(file, name); |
| 520 | } |
| 521 | return NULL; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | |
| 526 | StringList |
| 527 | cfgReadGetErrors(void) |
| 528 | { |
| 529 | return CfgReadErrs; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | |
| 534 | void |
| 535 | cfgReadClearErrors(void) |
| 536 | { |
| 537 | |
| 538 | listFreeDeeply(String)(CfgReadErrs, strFree); |
| 539 | |
| 540 | |
| 541 | |
| 542 | CfgReadErrs = listNil(String); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | |
| 547 | static void |
| 548 | cfgReadAddError(String error) |
| 549 | { |
| 550 | CfgReadErrs = listCons(String)(strCopy(error), CfgReadErrs); |
| 551 | } |
| 552 | |
| 553 | |
| 554 | |
| 555 | static void |
| 556 | cfgReadDupItemError(ConfigItem item, String section) |
| 557 | { |
| 558 | String msg; |
| 559 | |
| 560 | |
| 561 | msg = strlConcat( |
| 562 | "Duplicate key `", cfgName(item), |
| 563 | "' found in section [", section, "]", |
| 564 | " of the compiler configuration file.", |
| 565 | (String)NULL |
| 566 | ); |
| 567 | |
| 568 | |
| 569 | |
| 570 | cfgReadAddError(msg); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | |
| 575 | static Bool |
| 576 | cfgEqual(ConfigItem itemA, ConfigItem itemB) |
| 577 | { |
| 578 | return strEqual(cfgName(itemA), cfgName(itemB)); |
| 579 | } |
| 580 | |
| 581 | static Bool |
| 582 | cfgIsSection(char *line, char *name) |
| 583 | { |
| 584 | char *p = line; |
| 585 | while (*p != ']' && *p != '\0') p++; |
| 586 | *p = 0; |
| 587 | return (strEqual(line+1, name)); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | static ConfigItemList |
| 592 | cfgGetKeys(FILE *file, String section) |
| 593 | { |
| 594 | ConfigItemList lst; |
| 595 | Bool atEof; |
| 596 | |
| 597 | atEof = false; |
| 598 | lst = listNil(ConfigItem); |
| 599 | while (true) { |
| 600 | ConfigItem item; |
| 601 | String line = cfgGetLine(file, &atEof); |
| 602 | |
| 603 | |
| 604 | |
| 605 | if (line[0] == '[') break; |
| 606 | |
| 607 | |
| 608 | |
| 609 | item = cfgParseLine(line); |
| 610 | |
| 611 | |
| 612 | |
| 613 | if (item && !strEqual(cfgName(item), "inherit")) |
| 614 | { |
| 615 | if (listMember(ConfigItem)(lst, item, cfgEqual)) |
| 616 | cfgReadDupItemError(item, section); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | if (item) lst = listCons(ConfigItem)(item, lst); |
| 626 | |
| 627 | |
| 628 | |
| 629 | if (atEof) break; |
| 630 | |
| 631 | |
| 632 | |
| 633 | strFree(line); |
| 634 | } |
| 635 | return lst; |
| 636 | } |
| 637 | |
| 638 | static ConfigItem |
| 639 | cfgParseLine(String line) |
| 640 | { |
| 641 | String key; |
| 642 | String val; |
| 643 | char *p = line, *keyEnd; |
| 644 | |
| 645 | while (*p == ' ' || *p == '\t') p++; |
| 646 | |
| 647 | if (*p == '#') return NULL; |
| 648 | key = p; |
| 649 | |
| 650 | while (*p != ' ' && *p != '\t' && *p != '=' && *p != '\0') p++; |
| 651 | keyEnd = p; |
| 652 | |
| 653 | if (*p == '\0') return NULL; |
| 654 | |
| 655 | while (*p != '=') p++; |
| 656 | |
| 657 | p++; |
| 658 | val = p; |
| 659 | *keyEnd = '\0'; |
| 660 | |
| 661 | return cfgNew(key, val); |
| 662 | } |
| 663 | |
| 664 | static String |
| 665 | cfgGetLine(FILE *file, Bool *atEof) |
| 666 | { |
| 667 | String s; |
| 668 | int n, lim, c; |
| 669 | |
| 670 | lim = 50; |
| 671 | s = strAlloc(lim); |
| 672 | n = 0; |
| 673 | |
| 674 | c = fgetc(file); |
| 675 | while (c != '\n' && c != EOF) { |
| 676 | if (n == lim) { |
| 677 | int const incr = 20; |
| 678 | String tmp = s; |
| 679 | s = strAlloc(lim + incr); |
| 680 | strncpy(s, tmp, n); |
| 681 | strFree(tmp); |
| 682 | lim += incr; |
| 683 | } |
| 684 | s[n] = c; |
| 685 | c = fgetc(file); |
| 686 | n++; |
| 687 | } |
| 688 | while (n < lim) |
| 689 | s[n++] = '\0'; |
| 690 | if (c == EOF) *atEof = true; |
| 691 | return s; |
| 692 | } |
| 693 | |