configfile.c 34 KB

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