1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
|
/**
* \file configfile.c
*
* configuration/rc/ini file handling.
*
* @addtogroup autoopts
* @{
*/
/*
* This file is part of AutoOpts, a companion to AutoGen.
* AutoOpts is free software.
* AutoOpts is Copyright (C) 1992-2016 by Bruce Korb - all rights reserved
*
* AutoOpts is available under any one of two licenses. The license
* in use must be one of these two and the choice is under the control
* of the user of the license.
*
* The GNU Lesser General Public License, version 3 or later
* See the files "COPYING.lgplv3" and "COPYING.gplv3"
*
* The Modified Berkeley Software Distribution License
* See the file "COPYING.mbsd"
*
* These files have the following sha256 sums:
*
* 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
* 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
* 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
*/
/* = = = START-STATIC-FORWARD = = = */
static void
file_preset(tOptions * opts, char const * fname, int dir);
static char *
handle_comment(char * txt);
static char *
handle_cfg(tOptions * opts, tOptState * ost, char * txt, int dir);
static char *
handle_directive(tOptions * opts, char * txt);
static char *
aoflags_directive(tOptions * opts, char * txt);
static char *
program_directive(tOptions * opts, char * txt);
static char *
handle_section(tOptions * opts, char * txt);
static int
parse_xml_encoding(char ** ppz);
static char *
trim_xml_text(char * intxt, char const * pznm, tOptionLoadMode mode);
static void
cook_xml_text(char * pzData);
static char *
handle_struct(tOptions * opts, tOptState * ost, char * txt, int dir);
static char const *
parse_keyword(tOptions * opts, char const * txt, tOptionValue * typ);
static char const *
parse_set_mem(tOptions * opts, char const * txt, tOptionValue * typ);
static char const *
parse_value(char const * txt, tOptionValue * typ);
/* = = = END-STATIC-FORWARD = = = */
/**
* Skip over some unknown attribute
* @param[in] txt start of skpped text
* @returns character after skipped text
*/
inline static char const *
skip_unkn(char const * txt)
{
txt = BRK_END_XML_TOKEN_CHARS(txt);
return (*txt == NUL) ? NULL : txt;
}
/*=export_func configFileLoad
*
* what: parse a configuration file
* arg: + char const * + fname + the file to load +
*
* ret_type: const tOptionValue *
* ret_desc: An allocated, compound value structure
*
* doc:
* This routine will load a named configuration file and parse the
* text as a hierarchically valued option. The option descriptor
* created from an option definition file is not used via this interface.
* The returned value is "named" with the input file name and is of
* type "@code{OPARG_TYPE_HIERARCHY}". It may be used in calls to
* @code{optionGetValue()}, @code{optionNextValue()} and
* @code{optionUnloadNested()}.
*
* err:
* If the file cannot be loaded or processed, @code{NULL} is returned and
* @var{errno} is set. It may be set by a call to either @code{open(2)}
* @code{mmap(2)} or other file system calls, or it may be:
* @itemize @bullet
* @item
* @code{ENOENT} - the file was not found.
* @item
* @code{ENOMSG} - the file was empty.
* @item
* @code{EINVAL} - the file contents are invalid -- not properly formed.
* @item
* @code{ENOMEM} - not enough memory to allocate the needed structures.
* @end itemize
=*/
const tOptionValue *
configFileLoad(char const * fname)
{
tmap_info_t cfgfile;
tOptionValue * res = NULL;
tOptionLoadMode save_mode = option_load_mode;
char * txt = text_mmap(fname, PROT_READ, MAP_PRIVATE, &cfgfile);
if (TEXT_MMAP_FAILED_ADDR(txt))
return NULL; /* errno is set */
option_load_mode = OPTION_LOAD_COOKED;
res = optionLoadNested(txt, fname, strlen(fname));
if (res == NULL) {
int err = errno;
text_munmap(&cfgfile);
errno = err;
} else
text_munmap(&cfgfile);
option_load_mode = save_mode;
return res;
}
/*=export_func optionFindValue
*
* what: find a hierarcicaly valued option instance
* arg: + const tOptDesc * + odesc + an option with a nested arg type +
* arg: + char const * + name + name of value to find +
* arg: + char const * + val + the matching value +
*
* ret_type: const tOptionValue *
* ret_desc: a compound value structure
*
* doc:
* This routine will find an entry in a nested value option or configurable.
* It will search through the list and return a matching entry.
*
* err:
* The returned result is NULL and errno is set:
* @itemize @bullet
* @item
* @code{EINVAL} - the @code{pOptValue} does not point to a valid
* hierarchical option value.
* @item
* @code{ENOENT} - no entry matched the given name.
* @end itemize
=*/
const tOptionValue *
optionFindValue(const tOptDesc * odesc, char const * name, char const * val)
{
const tOptionValue * res = NULL;
if ( (odesc == NULL)
|| (OPTST_GET_ARGTYPE(odesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
}
else if (odesc->optCookie == NULL) {
errno = ENOENT;
}
else do {
tArgList * argl = odesc->optCookie;
int argct = argl->useCt;
void ** poptv = (void **)(argl->apzArgs);
if (argct == 0) {
errno = ENOENT;
break;
}
if (name == NULL) {
res = (tOptionValue *)*poptv;
break;
}
while (--argct >= 0) {
const tOptionValue * ov = *(poptv++);
const tOptionValue * rv = optionGetValue(ov, name);
if (rv == NULL)
continue;
if (val == NULL) {
res = ov;
break;
}
}
if (res == NULL)
errno = ENOENT;
} while (false);
return res;
}
/*=export_func optionFindNextValue
*
* FIXME: the handling of 'pzName' and 'pzVal' is just wrong.
*
* what: find a hierarcicaly valued option instance
* arg: + const tOptDesc * + odesc + an option with a nested arg type +
* arg: + const tOptionValue * + pPrevVal + the last entry +
* arg: + char const * + name + name of value to find +
* arg: + char const * + value + the matching value +
*
* ret_type: const tOptionValue *
* ret_desc: a compound value structure
*
* doc:
* This routine will find the next entry in a nested value option or
* configurable. It will search through the list and return the next entry
* that matches the criteria.
*
* err:
* The returned result is NULL and errno is set:
* @itemize @bullet
* @item
* @code{EINVAL} - the @code{pOptValue} does not point to a valid
* hierarchical option value.
* @item
* @code{ENOENT} - no entry matched the given name.
* @end itemize
=*/
tOptionValue const *
optionFindNextValue(const tOptDesc * odesc, const tOptionValue * pPrevVal,
char const * pzName, char const * pzVal)
{
bool old_found = false;
tOptionValue * res = NULL;
(void)pzName;
(void)pzVal;
if ( (odesc == NULL)
|| (OPTST_GET_ARGTYPE(odesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
}
else if (odesc->optCookie == NULL) {
errno = ENOENT;
}
else do {
tArgList * argl = odesc->optCookie;
int ct = argl->useCt;
void ** poptv = (void **)argl->apzArgs;
while (--ct >= 0) {
tOptionValue * pOV = *(poptv++);
if (old_found) {
res = pOV;
break;
}
if (pOV == pPrevVal)
old_found = true;
}
if (res == NULL)
errno = ENOENT;
} while (false);
return res;
}
/*=export_func optionGetValue
*
* what: get a specific value from a hierarcical list
* arg: + const tOptionValue * + pOptValue + a hierarchcal value +
* arg: + char const * + valueName + name of value to get +
*
* ret_type: const tOptionValue *
* ret_desc: a compound value structure
*
* doc:
* This routine will find an entry in a nested value option or configurable.
* If "valueName" is NULL, then the first entry is returned. Otherwise,
* the first entry with a name that exactly matches the argument will be
* returned. If there is no matching value, NULL is returned and errno is
* set to ENOENT. If the provided option value is not a hierarchical value,
* NULL is also returned and errno is set to EINVAL.
*
* err:
* The returned result is NULL and errno is set:
* @itemize @bullet
* @item
* @code{EINVAL} - the @code{pOptValue} does not point to a valid
* hierarchical option value.
* @item
* @code{ENOENT} - no entry matched the given name.
* @end itemize
=*/
tOptionValue const *
optionGetValue(tOptionValue const * oov, char const * vname)
{
tArgList * arg_list;
tOptionValue * res = NULL;
if ((oov == NULL) || (oov->valType != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
return res;
}
arg_list = oov->v.nestVal;
if (arg_list->useCt > 0) {
int ct = arg_list->useCt;
void ** ovlist = (void **)(arg_list->apzArgs);
if (vname == NULL) {
res = (tOptionValue *)*ovlist;
} else do {
tOptionValue * opt_val = *(ovlist++);
if (strcmp(opt_val->pzName, vname) == 0) {
res = opt_val;
break;
}
} while (--ct > 0);
}
if (res == NULL)
errno = ENOENT;
return res;
}
/*=export_func optionNextValue
*
* what: get the next value from a hierarchical list
* arg: + const tOptionValue * + pOptValue + a hierarchcal list value +
* arg: + const tOptionValue * + pOldValue + a value from this list +
*
* ret_type: const tOptionValue *
* ret_desc: a compound value structure
*
* doc:
* This routine will return the next entry after the entry passed in. At the
* end of the list, NULL will be returned. If the entry is not found on the
* list, NULL will be returned and "@var{errno}" will be set to EINVAL.
* The "@var{pOldValue}" must have been gotten from a prior call to this
* routine or to "@code{opitonGetValue()}".
*
* err:
* The returned result is NULL and errno is set:
* @itemize @bullet
* @item
* @code{EINVAL} - the @code{pOptValue} does not point to a valid
* hierarchical option value or @code{pOldValue} does not point to a
* member of that option value.
* @item
* @code{ENOENT} - the supplied @code{pOldValue} pointed to the last entry.
* @end itemize
=*/
tOptionValue const *
optionNextValue(tOptionValue const * ov_list,tOptionValue const * oov )
{
tArgList * arg_list;
tOptionValue * res = NULL;
int err = EINVAL;
if ((ov_list == NULL) || (ov_list->valType != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
return NULL;
}
arg_list = ov_list->v.nestVal;
{
int ct = arg_list->useCt;
void ** o_list = (void **)(arg_list->apzArgs);
while (ct-- > 0) {
tOptionValue * nov = *(o_list++);
if (nov == oov) {
if (ct == 0) {
err = ENOENT;
} else {
err = 0;
res = (tOptionValue *)*o_list;
}
break;
}
}
}
if (err != 0)
errno = err;
return res;
}
/**
* Load a file containing presetting information (a configuration file).
*/
static void
file_preset(tOptions * opts, char const * fname, int dir)
{
tmap_info_t cfgfile;
tOptState optst = OPTSTATE_INITIALIZER(PRESET);
opt_state_mask_t st_flags = optst.flags;
opt_state_mask_t fl_save = opts->fOptSet;
char * ftext =
text_mmap(fname, PROT_READ|PROT_WRITE, MAP_PRIVATE, &cfgfile);
if (TEXT_MMAP_FAILED_ADDR(ftext))
return;
/*
* While processing config files, we ignore errors.
*/
opts->fOptSet &= ~OPTPROC_ERRSTOP;
if (dir == DIRECTION_CALLED) {
st_flags = OPTST_DEFINED;
dir = DIRECTION_PROCESS;
}
/*
* IF this is called via "optionProcess", then we are presetting.
* This is the default and the PRESETTING bit will be set.
* If this is called via "optionFileLoad", then the bit is not set
* and we consider stuff set herein to be "set" by the client program.
*/
if ((opts->fOptSet & OPTPROC_PRESETTING) == 0)
st_flags = OPTST_SET;
do {
optst.flags = st_flags;
ftext = SPN_WHITESPACE_CHARS(ftext);
if (IS_VAR_FIRST_CHAR(*ftext)) {
ftext = handle_cfg(opts, &optst, ftext, dir);
} else switch (*ftext) {
case '<':
if (IS_VAR_FIRST_CHAR(ftext[1]))
ftext = handle_struct(opts, &optst, ftext, dir);
else switch (ftext[1]) {
case '?':
ftext = handle_directive(opts, ftext);
break;
case '!':
ftext = handle_comment(ftext);
break;
case '/':
ftext = strchr(ftext + 2, '>');
if (ftext++ != NULL)
break;
/* fallthrough */
default:
ftext = NULL;
}
if (ftext == NULL)
goto all_done;
break;
case '[':
ftext = handle_section(opts, ftext);
break;
case '#':
ftext = strchr(ftext + 1, NL);
break;
default:
goto all_done; /* invalid format */
}
} while (ftext != NULL);
all_done:
text_munmap(&cfgfile);
opts->fOptSet = fl_save;
}
/**
* "txt" points to a "<!" sequence.
* Theoretically, we should ensure that it begins with "<!--",
* but actually I don't care that much. It ends with "-->".
*/
static char *
handle_comment(char * txt)
{
char * pz = strstr(txt, "-->");
if (pz != NULL)
pz += 3;
return pz;
}
/**
* "txt" points to the start of some value name.
* The end of the entry is the end of the line that is not preceded by
* a backslash escape character. The string value is always processed
* in "cooked" mode.
*/
static char *
handle_cfg(tOptions * opts, tOptState * ost, char * txt, int dir)
{
char * pzName = txt++;
char * pzEnd = strchr(txt, NL);
if (pzEnd == NULL)
return txt + strlen(txt);
txt = SPN_VALUE_NAME_CHARS(txt);
txt = SPN_WHITESPACE_CHARS(txt);
if (txt > pzEnd) {
name_only:
*pzEnd++ = NUL;
load_opt_line(opts, ost, pzName, dir, OPTION_LOAD_UNCOOKED);
return pzEnd;
}
/*
* Either the first character after the name is a ':' or '=',
* or else we must have skipped over white space. Anything else
* is an invalid format and we give up parsing the text.
*/
if ((*txt == '=') || (*txt == ':')) {
txt = SPN_WHITESPACE_CHARS(txt+1);
if (txt > pzEnd)
goto name_only;
} else if (! IS_WHITESPACE_CHAR(txt[-1]))
return NULL;
/*
* IF the value is continued, remove the backslash escape and push "pzEnd"
* on to a newline *not* preceded by a backslash.
*/
if (pzEnd[-1] == '\\') {
char * pcD = pzEnd-1;
char * pcS = pzEnd;
for (;;) {
char ch = *(pcS++);
switch (ch) {
case NUL:
pcS = NULL;
/* FALLTHROUGH */
case NL:
*pcD = NUL;
pzEnd = pcS;
goto copy_done;
case '\\':
if (*pcS == NL)
ch = *(pcS++);
/* FALLTHROUGH */
default:
*(pcD++) = ch;
}
} copy_done:;
} else {
/*
* The newline was not preceded by a backslash. NUL it out
*/
*(pzEnd++) = NUL;
}
/*
* "pzName" points to what looks like text for one option/configurable.
* It is NUL terminated. Process it.
*/
load_opt_line(opts, ost, pzName, dir, OPTION_LOAD_UNCOOKED);
return pzEnd;
}
/**
* "txt" points to a "<?" sequence.
* We handle "<?program" and "<?auto-options" directives.
* All others are treated as comments.
*
* @param[in,out] opts program option descriptor
* @param[in] txt scanning pointer
* @returns the next character to look at
*/
static char *
handle_directive(tOptions * opts, char * txt)
{
# define DIRECTIVE_TABLE \
_dt_(zCfgProg, program_directive) \
_dt_(zCfgAO_Flags, aoflags_directive)
typedef char * (directive_func_t)(tOptions *, char *);
# define _dt_(_s, _fn) _fn,
static directive_func_t * dir_disp[] = {
DIRECTIVE_TABLE
};
# undef _dt_
# define _dt_(_s, _fn) 1 +
static int const dir_ct = DIRECTIVE_TABLE 0;
static char const * dir_names[DIRECTIVE_TABLE 0];
# undef _dt_
int ix;
if (dir_names[0] == NULL) {
ix = 0;
# define _dt_(_s, _fn) dir_names[ix++] = _s;
DIRECTIVE_TABLE;
# undef _dt_
}
for (ix = 0; ix < dir_ct; ix++) {
size_t len = strlen(dir_names[ix]);
if ( (strncmp(txt + 2, dir_names[ix], len) == 0)
&& (! IS_VALUE_NAME_CHAR(txt[len+2])) )
return dir_disp[ix](opts, txt + len + 2);
}
/*
* We don't know what this is. Skip it.
*/
txt = strchr(txt+2, '>');
if (txt != NULL)
txt++;
return txt;
# undef DIRECTIVE_TABLE
}
/**
* handle AutoOpts mode flags.
*
* @param[in,out] opts program option descriptor
* @param[in] txt scanning pointer
* @returns the next character to look at
*/
static char *
aoflags_directive(tOptions * opts, char * txt)
{
char * pz;
pz = SPN_WHITESPACE_CHARS(txt+1);
txt = strchr(pz, '>');
if (txt != NULL) {
size_t len = (unsigned)(txt - pz);
char * ftxt = AGALOC(len + 1, "aoflags");
memcpy(ftxt, pz, len);
ftxt[len] = NUL;
set_usage_flags(opts, ftxt);
AGFREE(ftxt);
txt++;
}
return txt;
}
/**
* handle program segmentation of config file.
*
* @param[in,out] opts program option descriptor
* @param[in] txt scanning pointer
* @returns the next character to look at
*/
static char *
program_directive(tOptions * opts, char * txt)
{
static char const ttlfmt[] = "<?";
size_t ttl_len = sizeof(ttlfmt) + strlen(zCfgProg);
char * ttl = AGALOC(ttl_len, "prog title");
size_t name_len = strlen(opts->pzProgName);
memcpy(ttl, ttlfmt, sizeof(ttlfmt) - 1);
memcpy(ttl + sizeof(ttlfmt) - 1, zCfgProg, ttl_len - (sizeof(ttlfmt) - 1));
do {
txt = SPN_WHITESPACE_CHARS(txt+1);
if ( (strneqvcmp(txt, opts->pzProgName, (int)name_len) == 0)
&& (IS_END_XML_TOKEN_CHAR(txt[name_len])) ) {
txt += name_len;
break;
}
txt = strstr(txt, ttl);
} while (txt != NULL);
AGFREE(ttl);
if (txt != NULL)
for (;;) {
if (*txt == NUL) {
txt = NULL;
break;
}
if (*(txt++) == '>')
break;
}
return txt;
}
/**
* "txt" points to a '[' character.
* The "traditional" [PROG_NAME] segmentation of the config file.
* Do not ever mix with the "<?program prog-name>" variation.
* The templates reject program names over 16 characters.
*
* @param[in,out] opts program option descriptor
* @param[in] txt scanning pointer
* @returns the next character to look at
*/
static char *
handle_section(tOptions * opts, char * txt)
{
size_t len = strlen(opts->pzPROGNAME);
if ( (strncmp(txt+1, opts->pzPROGNAME, len) == 0)
&& (txt[len+1] == ']'))
return strchr(txt + len + 2, NL);
if (len > 16)
return NULL;
{
char z[24] = "[";
memcpy(z+1, opts->pzPROGNAME, len);
z[++len] = ']';
z[++len] = NUL;
txt = strstr(txt, z);
}
if (txt != NULL)
txt = strchr(txt, NL);
return txt;
}
/**
* parse XML encodings
*/
static int
parse_xml_encoding(char ** ppz)
{
# define XMLTABLE \
_xmlNm_(amp, '&') \
_xmlNm_(lt, '<') \
_xmlNm_(gt, '>') \
_xmlNm_(ff, '\f') \
_xmlNm_(ht, '\t') \
_xmlNm_(cr, '\r') \
_xmlNm_(vt, '\v') \
_xmlNm_(bel, '\a') \
_xmlNm_(nl, NL) \
_xmlNm_(space, ' ') \
_xmlNm_(quot, '"') \
_xmlNm_(apos, '\'')
static struct {
char const * const nm_str;
unsigned short nm_len;
short nm_val;
} const xml_names[] = {
# define _xmlNm_(_n, _v) { #_n ";", sizeof(#_n), _v },
XMLTABLE
# undef _xmlNm_
# undef XMLTABLE
};
static int const nm_ct = sizeof(xml_names) / sizeof(xml_names[0]);
int base = 10;
char * pz = *ppz;
if (*pz == '#') {
pz++;
goto parse_number;
}
if (IS_DEC_DIGIT_CHAR(*pz)) {
unsigned long v;
parse_number:
switch (*pz) {
case 'x': case 'X':
/*
* Some forms specify hex with: &#xNN;
*/
base = 16;
pz++;
break;
case '0':
/*
*  is hex and  is decimal. Cool.
* Ya gotta love it.
*/
if (pz[1] == '0')
base = 16;
break;
}
v = strtoul(pz, &pz, base);
if ((*pz != ';') || (v > 0x7F))
return NUL;
*ppz = pz + 1;
return (int)v;
}
{
int ix = 0;
do {
if (strncmp(pz, xml_names[ix].nm_str, xml_names[ix].nm_len)
== 0) {
*ppz = pz + xml_names[ix].nm_len;
return xml_names[ix].nm_val;
}
} while (++ix < nm_ct);
}
return NUL;
}
/**
* Find the end marker for the named section of XML.
* Trim that text there, trimming trailing white space for all modes
* except for OPTION_LOAD_UNCOOKED.
*/
static char *
trim_xml_text(char * intxt, char const * pznm, tOptionLoadMode mode)
{
size_t nm_len = strlen(pznm);
char * etext;
{
char z[64], *pz = z;
if (nm_len + 4 >= sizeof(z))
pz = AGALOC(nm_len + 4, "scan name");
pz[0] = '<';
pz[1] = '/';
memcpy(pz+2, pznm, nm_len);
nm_len += 2;
pz[nm_len++] = '>';
pz[nm_len] = NUL;
*intxt = ' ';
etext = strstr(intxt, pz);
if (pz != z) AGFREE(pz);
}
if (etext == NULL)
return etext;
{
char * result = etext + nm_len;
if (mode != OPTION_LOAD_UNCOOKED)
etext = SPN_WHITESPACE_BACK(intxt, etext);
*etext = NUL;
return result;
}
}
/**
*/
static void
cook_xml_text(char * pzData)
{
char * pzs = pzData;
char * pzd = pzData;
char bf[4];
bf[2] = NUL;
for (;;) {
int ch = ((int)*(pzs++)) & 0xFF;
switch (ch) {
case NUL:
*pzd = NUL;
return;
case '&':
ch = parse_xml_encoding(&pzs);
*(pzd++) = (char)ch;
if (ch == NUL)
return;
break;
case '%':
bf[0] = *(pzs++);
bf[1] = *(pzs++);
if ((bf[0] == NUL) || (bf[1] == NUL)) {
*pzd = NUL;
return;
}
ch = (int)strtoul(bf, NULL, 16);
/* FALLTHROUGH */
default:
*(pzd++) = (char)ch;
}
}
}
/**
* "txt" points to a '<' character, followed by an alpha.
* The end of the entry is either the "/>" following the name, or else a
* "</name>" string.
*/
static char *
handle_struct(tOptions * opts, tOptState * ost, char * txt, int dir)
{
tOptionLoadMode mode = option_load_mode;
tOptionValue valu;
char * pzName = ++txt;
char * pzData;
char * pcNulPoint;
txt = SPN_VALUE_NAME_CHARS(txt);
pcNulPoint = txt;
valu.valType = OPARG_TYPE_STRING;
switch (*txt) {
case ' ':
case '\t':
txt = VOIDP(parse_attrs(
opts, SPN_WHITESPACE_CHARS(txt), &mode, &valu));
if (txt == NULL)
return txt;
if (*txt == '>')
break;
if (*txt != '/')
return NULL;
/* FALLTHROUGH */
case '/':
if (txt[1] != '>')
return NULL;
*txt = NUL;
txt += 2;
load_opt_line(opts, ost, pzName, dir, mode);
return txt;
case '>':
break;
default:
txt = strchr(txt, '>');
if (txt != NULL)
txt++;
return txt;
}
/*
* If we are here, we have a value. "txt" points to a closing angle
* bracket. Separate the name from the value for a moment.
*/
*pcNulPoint = NUL;
pzData = ++txt;
txt = trim_xml_text(txt, pzName, mode);
if (txt == NULL)
return txt;
/*
* Rejoin the name and value for parsing by "load_opt_line()".
* Erase any attributes parsed by "parse_attrs()".
*/
memset(pcNulPoint, ' ', (size_t)(pzData - pcNulPoint));
/*
* If we are getting a "string" value that is to be cooked,
* then process the XML-ish &xx; XML-ish and %XX hex characters.
*/
if ( (valu.valType == OPARG_TYPE_STRING)
&& (mode == OPTION_LOAD_COOKED))
cook_xml_text(pzData);
/*
* "pzName" points to what looks like text for one option/configurable.
* It is NUL terminated. Process it.
*/
load_opt_line(opts, ost, pzName, dir, mode);
return txt;
}
/**
* Load a configuration file. This may be invoked either from
* scanning the "homerc" list, or from a specific file request.
* (see "optionFileLoad()", the implementation for --load-opts)
*/
LOCAL void
intern_file_load(tOptions * opts)
{
uint32_t svfl;
int idx;
int inc;
char f_name[ AG_PATH_MAX+1 ];
if (opts->papzHomeList == NULL)
return;
svfl = opts->fOptSet;
inc = DIRECTION_PRESET;
/*
* Never stop on errors in config files.
*/
opts->fOptSet &= ~OPTPROC_ERRSTOP;
/*
* Find the last RC entry (highest priority entry)
*/
for (idx = 0; opts->papzHomeList[ idx+1 ] != NULL; ++idx) ;
/*
* For every path in the home list, ... *TWICE* We start at the last
* (highest priority) entry, work our way down to the lowest priority,
* handling the immediate options.
* Then we go back up, doing the normal options.
*/
for (;;) {
struct stat sb;
cch_t * path;
/*
* IF we've reached the bottom end, change direction
*/
if (idx < 0) {
inc = DIRECTION_PROCESS;
idx = 0;
}
path = opts->papzHomeList[ idx ];
/*
* IF we've reached the top end, bail out
*/
if (path == NULL)
break;
idx += inc;
if (! optionMakePath(f_name, (int)sizeof(f_name),
path, opts->pzProgPath))
continue;
/*
* IF the file name we constructed is a directory,
* THEN append the Resource Configuration file name
* ELSE we must have the complete file name
*/
if (stat(f_name, &sb) != 0)
continue; /* bogus name - skip the home list entry */
if (S_ISDIR(sb.st_mode)) {
size_t len = strlen(f_name);
size_t nln = strlen(opts->pzRcName) + 1;
char * pz = f_name + len;
if (len + 1 + nln >= sizeof(f_name))
continue;
if (pz[-1] != DIRCH)
*(pz++) = DIRCH;
memcpy(pz, opts->pzRcName, nln);
}
file_preset(opts, f_name, inc);
/*
* IF we are now to skip config files AND we are presetting,
* THEN change direction. We must go the other way.
*/
{
tOptDesc * od = opts->pOptDesc + opts->specOptIdx.save_opts + 1;
if (DISABLED_OPT(od) && PRESETTING(inc)) {
idx -= inc; /* go back and reprocess current file */
inc = DIRECTION_PROCESS;
}
}
} /* twice for every path in the home list, ... */
opts->fOptSet = svfl;
}
/*=export_func optionFileLoad
*
* what: Load the locatable config files, in order
*
* arg: + tOptions * + opts + program options descriptor +
* arg: + char const * + prog + program name +
*
* ret_type: int
* ret_desc: 0 -> SUCCESS, -1 -> FAILURE
*
* doc:
*
* This function looks in all the specified directories for a configuration
* file ("rc" file or "ini" file) and processes any found twice. The first
* time through, they are processed in reverse order (last file first). At
* that time, only "immediate action" configurables are processed. For
* example, if the last named file specifies not processing any more
* configuration files, then no more configuration files will be processed.
* Such an option in the @strong{first} named directory will have no effect.
*
* Once the immediate action configurables have been handled, then the
* directories are handled in normal, forward order. In that way, later
* config files can override the settings of earlier config files.
*
* See the AutoOpts documentation for a thorough discussion of the
* config file format.
*
* Configuration files not found or not decipherable are simply ignored.
*
* err: Returns the value, "-1" if the program options descriptor
* is out of date or indecipherable. Otherwise, the value "0" will
* always be returned.
=*/
int
optionFileLoad(tOptions * opts, char const * prog)
{
if (! SUCCESSFUL(validate_struct(opts, prog)))
return -1;
/*
* The pointer to the program name is "const". However, the
* structure is in writable memory, so we coerce the address
* of this pointer to point to writable memory.
*/
{
char const ** pp = VOIDP(&(opts->pzProgName));
*pp = prog;
}
intern_file_load(opts);
return 0;
}
/*=export_func optionLoadOpt
* private:
*
* what: Load an option rc/ini file
* arg: + tOptions * + opts + program options descriptor +
* arg: + tOptDesc * + odesc + the descriptor for this arg +
*
* doc:
* Processes the options found in the file named with
* odesc->optArg.argString.
=*/
void
optionLoadOpt(tOptions * opts, tOptDesc * odesc)
{
struct stat sb;
if (opts <= OPTPROC_EMIT_LIMIT)
return;
/*
* IF the option is not being disabled, THEN load the file. There must
* be a file. (If it is being disabled, then the disablement processing
* already took place. It must be done to suppress preloading of ini/rc
* files.)
*/
if ( DISABLED_OPT(odesc)
|| ((odesc->fOptState & OPTST_RESET) != 0))
return;
if (stat(odesc->optArg.argString, &sb) != 0) {
if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
return;
fserr_exit(opts->pzProgName, "stat", odesc->optArg.argString);
/* NOT REACHED */
}
if (! S_ISREG(sb.st_mode)) {
if ((opts->fOptSet & OPTPROC_ERRSTOP) == 0)
return;
errno = EINVAL;
fserr_exit(opts->pzProgName, "stat", odesc->optArg.argString);
/* NOT REACHED */
}
file_preset(opts, odesc->optArg.argString, DIRECTION_CALLED);
}
/**
* Parse the various attributes of an XML-styled config file entry
*
* @returns NULL on failure, otherwise the scan point
*/
LOCAL char const *
parse_attrs(tOptions * opts, char const * txt, tOptionLoadMode * pMode,
tOptionValue * pType)
{
size_t len = 0;
for (;;) {
len = (size_t)(SPN_LOWER_CASE_CHARS(txt) - txt);
/*
* The enumeration used in this switch is derived from this switch
* statement itself. The "find_option_xat_attribute_cmd" function
* will return XAT_CMD_MEMBERS for the "txt" string value
* "members", etc.
*/
switch (find_option_xat_attribute_cmd(txt, len)) {
case XAT_CMD_TYPE:
txt = parse_value(txt+len, pType);
break;
case XAT_CMD_WORDS:
txt = parse_keyword(opts, txt+len, pType);
break;
case XAT_CMD_MEMBERS:
txt = parse_set_mem(opts, txt+len, pType);
break;
case XAT_CMD_COOKED:
txt += len;
if (! IS_END_XML_TOKEN_CHAR(*txt))
goto invalid_kwd;
*pMode = OPTION_LOAD_COOKED;
break;
case XAT_CMD_UNCOOKED:
txt += len;
if (! IS_END_XML_TOKEN_CHAR(*txt))
goto invalid_kwd;
*pMode = OPTION_LOAD_UNCOOKED;
break;
case XAT_CMD_KEEP:
txt += len;
if (! IS_END_XML_TOKEN_CHAR(*txt))
goto invalid_kwd;
*pMode = OPTION_LOAD_KEEP;
break;
default:
case XAT_INVALID_CMD:
invalid_kwd:
pType->valType = OPARG_TYPE_NONE;
return skip_unkn(txt);
}
if (txt == NULL)
return NULL;
txt = SPN_WHITESPACE_CHARS(txt);
switch (*txt) {
case '/': pType->valType = OPARG_TYPE_NONE;
/* FALLTHROUGH */
case '>': return txt;
}
if (! IS_LOWER_CASE_CHAR(*txt))
return NULL;
}
}
/**
* "txt" points to the character after "words=".
* What should follow is a name of a keyword (enumeration) list.
*
* @param opts unused
* @param[in] txt keyword to skip over
* @param type unused value type
* @returns pointer after skipped text
*/
static char const *
parse_keyword(tOptions * opts, char const * txt, tOptionValue * typ)
{
(void)opts;
(void)typ;
return skip_unkn(txt);
}
/**
* "txt" points to the character after "members="
* What should follow is a name of a "set membership".
* A collection of bit flags.
*
* @param opts unused
* @param[in] txt keyword to skip over
* @param type unused value type
* @returns pointer after skipped text
*/
static char const *
parse_set_mem(tOptions * opts, char const * txt, tOptionValue * typ)
{
(void)opts;
(void)typ;
return skip_unkn(txt);
}
/**
* parse the type. The keyword "type" was found, now figure out
* the type that follows the type.
*
* @param[in] txt points to the '=' character after the "type" keyword.
* @param[out] typ where to store the type found
* @returns the next byte after the type name
*/
static char const *
parse_value(char const * txt, tOptionValue * typ)
{
size_t len = 0;
if (*(txt++) != '=')
goto woops;
len = (size_t)(SPN_OPTION_NAME_CHARS(txt) - txt);
if ((len == 0) || (! IS_END_XML_TOKEN_CHAR(txt[len]))) {
woops:
typ->valType = OPARG_TYPE_NONE;
return skip_unkn(txt + len);
}
/*
* The enumeration used in this switch is derived from this switch
* statement itself. The "find_option_value_type_cmd" function
* will return VTP_CMD_INTEGER for the "txt" string value
* "integer", etc.
*/
switch (find_option_value_type_cmd(txt, len)) {
default:
case VTP_INVALID_CMD: goto woops;
case VTP_CMD_STRING:
typ->valType = OPARG_TYPE_STRING;
break;
case VTP_CMD_INTEGER:
typ->valType = OPARG_TYPE_NUMERIC;
break;
case VTP_CMD_BOOL:
case VTP_CMD_BOOLEAN:
typ->valType = OPARG_TYPE_BOOLEAN;
break;
case VTP_CMD_KEYWORD:
typ->valType = OPARG_TYPE_ENUMERATION;
break;
case VTP_CMD_SET:
case VTP_CMD_SET_MEMBERSHIP:
typ->valType = OPARG_TYPE_MEMBERSHIP;
break;
case VTP_CMD_NESTED:
case VTP_CMD_HIERARCHY:
typ->valType = OPARG_TYPE_HIERARCHY;
}
return txt + len;
}
/** @}
*
* Local Variables:
* mode: C
* c-file-style: "stroustrup"
* indent-tabs-mode: nil
* End:
* end of autoopts/configfile.c */
|