configfile.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * $Id: configfile.c,v 1.20 2007/02/04 17:44:12 bkorb Exp $
  3. * Time-stamp: "2007-01-13 12:49:10 bkorb"
  4. *
  5. * configuration/rc/ini file handling.
  6. */
  7. /*
  8. * Automated Options copyright 1992-2007 Bruce Korb
  9. *
  10. * Automated Options is free software.
  11. * You may redistribute it and/or modify it under the terms of the
  12. * GNU General Public License, as published by the Free Software
  13. * Foundation; either version 2, or (at your option) any later version.
  14. *
  15. * Automated Options is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Automated Options. See the file "COPYING". If not,
  22. * write to: The Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor,
  24. * Boston, MA 02110-1301, USA.
  25. *
  26. * As a special exception, Bruce Korb gives permission for additional
  27. * uses of the text contained in his release of AutoOpts.
  28. *
  29. * The exception is that, if you link the AutoOpts library with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the AutoOpts library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by Bruce Korb under
  39. * the name AutoOpts. If you copy code from other sources under the
  40. * General Public License into a copy of AutoOpts, as the General Public
  41. * License permits, the exception does not apply to the code that you add
  42. * in this way. To avoid misleading anyone as to the status of such
  43. * modified files, you must delete this exception notice from them.
  44. *
  45. * If you write modifications of your own for AutoOpts, it is your choice
  46. * whether to permit this exception to apply to your modifications.
  47. * If you do not wish that, delete this exception notice.
  48. */
  49. /* = = = START-STATIC-FORWARD = = = */
  50. /* static forward declarations maintained by :mkfwd */
  51. static void
  52. filePreset(
  53. tOptions* pOpts,
  54. char const* pzFileName,
  55. int direction );
  56. static char*
  57. handleComment( char* pzText );
  58. static char*
  59. handleConfig(
  60. tOptions* pOpts,
  61. tOptState* pOS,
  62. char* pzText,
  63. int direction );
  64. static char*
  65. handleDirective(
  66. tOptions* pOpts,
  67. char* pzText );
  68. static char*
  69. handleProgramSection(
  70. tOptions* pOpts,
  71. char* pzText );
  72. static char*
  73. handleStructure(
  74. tOptions* pOpts,
  75. tOptState* pOS,
  76. char* pzText,
  77. int direction );
  78. static char*
  79. parseKeyWordType(
  80. tOptions* pOpts,
  81. char* pzText,
  82. tOptionValue* pType );
  83. static char*
  84. parseLoadMode(
  85. char* pzText,
  86. tOptionLoadMode* pMode );
  87. static char*
  88. parseSetMemType(
  89. tOptions* pOpts,
  90. char* pzText,
  91. tOptionValue* pType );
  92. static char*
  93. parseValueType(
  94. char* pzText,
  95. tOptionValue* pType );
  96. static char*
  97. skipUnknown( char* pzText );
  98. /* = = = END-STATIC-FORWARD = = = */
  99. /*=export_func configFileLoad
  100. *
  101. * what: parse a configuration file
  102. * arg: + char const* + pzFile + the file to load +
  103. *
  104. * ret_type: const tOptionValue*
  105. * ret_desc: An allocated, compound value structure
  106. *
  107. * doc:
  108. * This routine will load a named configuration file and parse the
  109. * text as a hierarchically valued option. The option descriptor
  110. * created from an option definition file is not used via this interface.
  111. * The returned value is "named" with the input file name and is of
  112. * type "@code{OPARG_TYPE_HIERARCHY}". It may be used in calls to
  113. * @code{optionGetValue()}, @code{optionNextValue()} and
  114. * @code{optionUnloadNested()}.
  115. *
  116. * err:
  117. * If the file cannot be loaded or processed, @code{NULL} is returned and
  118. * @var{errno} is set. It may be set by a call to either @code{open(2)}
  119. * @code{mmap(2)} or other file system calls, or it may be:
  120. * @itemize @bullet
  121. * @item
  122. * @code{ENOENT} - the file was empty.
  123. * @item
  124. * @code{EINVAL} - the file contents are invalid -- not properly formed.
  125. * @item
  126. * @code{ENOMEM} - not enough memory to allocate the needed structures.
  127. * @end itemize
  128. =*/
  129. const tOptionValue*
  130. configFileLoad( char const* pzFile )
  131. {
  132. tmap_info_t cfgfile;
  133. tOptionValue* pRes = NULL;
  134. tOptionLoadMode save_mode = option_load_mode;
  135. char* pzText =
  136. text_mmap( pzFile, PROT_READ, MAP_PRIVATE, &cfgfile );
  137. if (TEXT_MMAP_FAILED_ADDR(pzText))
  138. return NULL; /* errno is set */
  139. option_load_mode = OPTION_LOAD_COOKED;
  140. pRes = optionLoadNested(pzText, pzFile, strlen(pzFile));
  141. if (pRes == NULL) {
  142. int err = errno;
  143. text_munmap( &cfgfile );
  144. errno = err;
  145. } else
  146. text_munmap( &cfgfile );
  147. option_load_mode = save_mode;
  148. return pRes;
  149. }
  150. /*=export_func optionFindValue
  151. *
  152. * what: find a hierarcicaly valued option instance
  153. * arg: + const tOptDesc* + pOptDesc + an option with a nested arg type +
  154. * arg: + char const* + name + name of value to find +
  155. * arg: + char const* + value + the matching value +
  156. *
  157. * ret_type: const tOptionValue*
  158. * ret_desc: a compound value structure
  159. *
  160. * doc:
  161. * This routine will find an entry in a nested value option or configurable.
  162. * It will search through the list and return a matching entry.
  163. *
  164. * err:
  165. * The returned result is NULL and errno is set:
  166. * @itemize @bullet
  167. * @item
  168. * @code{EINVAL} - the @code{pOptValue} does not point to a valid
  169. * hierarchical option value.
  170. * @item
  171. * @code{ENOENT} - no entry matched the given name.
  172. * @end itemize
  173. =*/
  174. const tOptionValue*
  175. optionFindValue( const tOptDesc* pOptDesc,
  176. char const* pzName, char const* pzVal )
  177. {
  178. const tOptionValue* pRes = NULL;
  179. if ( (pOptDesc == NULL)
  180. || (OPTST_GET_ARGTYPE(pOptDesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
  181. errno = EINVAL;
  182. }
  183. else if (pOptDesc->optCookie == NULL) {
  184. errno = ENOENT;
  185. }
  186. else do {
  187. tArgList* pAL = pOptDesc->optCookie;
  188. int ct = pAL->useCt;
  189. void** ppOV = (void**)(pAL->apzArgs);
  190. if (ct == 0) {
  191. errno = ENOENT;
  192. break;
  193. }
  194. if (pzName == NULL) {
  195. pRes = (tOptionValue*)*ppOV;
  196. break;
  197. }
  198. while (--ct >= 0) {
  199. const tOptionValue* pOV = *(ppOV++);
  200. const tOptionValue* pRV = optionGetValue( pOV, pzName );
  201. if (pRV == NULL)
  202. continue;
  203. if (pzVal == NULL) {
  204. pRes = pOV;
  205. break;
  206. }
  207. }
  208. if (pRes == NULL)
  209. errno = ENOENT;
  210. } while (0);
  211. return pRes;
  212. }
  213. /*=export_func optionFindNextValue
  214. *
  215. * what: find a hierarcicaly valued option instance
  216. * arg: + const tOptDesc* + pOptDesc + an option with a nested arg type +
  217. * arg: + const tOptionValue* + pPrevVal + the last entry +
  218. * arg: + char const* + name + name of value to find +
  219. * arg: + char const* + value + the matching value +
  220. *
  221. * ret_type: const tOptionValue*
  222. * ret_desc: a compound value structure
  223. *
  224. * doc:
  225. * This routine will find the next entry in a nested value option or
  226. * configurable. It will search through the list and return the next entry
  227. * that matches the criteria.
  228. *
  229. * err:
  230. * The returned result is NULL and errno is set:
  231. * @itemize @bullet
  232. * @item
  233. * @code{EINVAL} - the @code{pOptValue} does not point to a valid
  234. * hierarchical option value.
  235. * @item
  236. * @code{ENOENT} - no entry matched the given name.
  237. * @end itemize
  238. =*/
  239. const tOptionValue*
  240. optionFindNextValue( const tOptDesc* pOptDesc, const tOptionValue* pPrevVal,
  241. char const* pzName, char const* pzVal )
  242. {
  243. int foundOldVal = 0;
  244. tOptionValue* pRes = NULL;
  245. if ( (pOptDesc == NULL)
  246. || (OPTST_GET_ARGTYPE(pOptDesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
  247. errno = EINVAL;
  248. }
  249. else if (pOptDesc->optCookie == NULL) {
  250. errno = ENOENT;
  251. }
  252. else do {
  253. tArgList* pAL = pOptDesc->optCookie;
  254. int ct = pAL->useCt;
  255. void** ppOV = (void**)pAL->apzArgs;
  256. if (ct == 0) {
  257. errno = ENOENT;
  258. break;
  259. }
  260. while (--ct >= 0) {
  261. tOptionValue* pOV = *(ppOV++);
  262. if (foundOldVal) {
  263. pRes = pOV;
  264. break;
  265. }
  266. if (pOV == pPrevVal)
  267. foundOldVal = 1;
  268. }
  269. if (pRes == NULL)
  270. errno = ENOENT;
  271. } while (0);
  272. return pRes;
  273. }
  274. /*=export_func optionGetValue
  275. *
  276. * what: get a specific value from a hierarcical list
  277. * arg: + const tOptionValue* + pOptValue + a hierarchcal value +
  278. * arg: + char const* + valueName + name of value to get +
  279. *
  280. * ret_type: const tOptionValue*
  281. * ret_desc: a compound value structure
  282. *
  283. * doc:
  284. * This routine will find an entry in a nested value option or configurable.
  285. * If "valueName" is NULL, then the first entry is returned. Otherwise,
  286. * the first entry with a name that exactly matches the argument will be
  287. * returned.
  288. *
  289. * err:
  290. * The returned result is NULL and errno is set:
  291. * @itemize @bullet
  292. * @item
  293. * @code{EINVAL} - the @code{pOptValue} does not point to a valid
  294. * hierarchical option value.
  295. * @item
  296. * @code{ENOENT} - no entry matched the given name.
  297. * @end itemize
  298. =*/
  299. const tOptionValue*
  300. optionGetValue( const tOptionValue* pOld, char const* pzValName )
  301. {
  302. tArgList* pAL;
  303. tOptionValue* pRes = NULL;
  304. if ((pOld == NULL) || (pOld->valType != OPARG_TYPE_HIERARCHY)) {
  305. errno = EINVAL;
  306. return NULL;
  307. }
  308. pAL = pOld->v.nestVal;
  309. if (pAL->useCt > 0) {
  310. int ct = pAL->useCt;
  311. void** papOV = (void**)(pAL->apzArgs);
  312. if (pzValName == NULL) {
  313. pRes = (tOptionValue*)*papOV;
  314. }
  315. else do {
  316. tOptionValue* pOV = *(papOV++);
  317. if (strcmp( pOV->pzName, pzValName ) == 0) {
  318. pRes = pOV;
  319. break;
  320. }
  321. } while (--ct > 0);
  322. }
  323. if (pRes == NULL)
  324. errno = ENOENT;
  325. return pRes;
  326. }
  327. /*=export_func optionNextValue
  328. *
  329. * what: get the next value from a hierarchical list
  330. * arg: + const tOptionValue* + pOptValue + a hierarchcal list value +
  331. * arg: + const tOptionValue* + pOldValue + a value from this list +
  332. *
  333. * ret_type: const tOptionValue*
  334. * ret_desc: a compound value structure
  335. *
  336. * doc:
  337. * This routine will return the next entry after the entry passed in. At the
  338. * end of the list, NULL will be returned. If the entry is not found on the
  339. * list, NULL will be returned and "@var{errno}" will be set to EINVAL.
  340. * The "@var{pOldValue}" must have been gotten from a prior call to this
  341. * routine or to "@code{opitonGetValue()}".
  342. *
  343. * err:
  344. * The returned result is NULL and errno is set:
  345. * @itemize @bullet
  346. * @item
  347. * @code{EINVAL} - the @code{pOptValue} does not point to a valid
  348. * hierarchical option value or @code{pOldValue} does not point to a
  349. * member of that option value.
  350. * @item
  351. * @code{ENOENT} - the supplied @code{pOldValue} pointed to the last entry.
  352. * @end itemize
  353. =*/
  354. tOptionValue const *
  355. optionNextValue(tOptionValue const * pOVList,tOptionValue const * pOldOV )
  356. {
  357. tArgList* pAL;
  358. tOptionValue* pRes = NULL;
  359. int err = EINVAL;
  360. if ((pOVList == NULL) || (pOVList->valType != OPARG_TYPE_HIERARCHY)) {
  361. errno = EINVAL;
  362. return NULL;
  363. }
  364. pAL = pOVList->v.nestVal;
  365. {
  366. int ct = pAL->useCt;
  367. void** papNV = (void**)(pAL->apzArgs);
  368. while (ct-- > 0) {
  369. tOptionValue* pNV = *(papNV++);
  370. if (pNV == pOldOV) {
  371. if (ct == 0) {
  372. err = ENOENT;
  373. } else {
  374. err = 0;
  375. pRes = (tOptionValue*)*papNV;
  376. }
  377. break;
  378. }
  379. }
  380. }
  381. if (err != 0)
  382. errno = err;
  383. return pRes;
  384. }
  385. /* filePreset
  386. *
  387. * Load a file containing presetting information (a configuration file).
  388. */
  389. static void
  390. filePreset(
  391. tOptions* pOpts,
  392. char const* pzFileName,
  393. int direction )
  394. {
  395. tmap_info_t cfgfile;
  396. tOptState st = OPTSTATE_INITIALIZER(PRESET);
  397. char* pzFileText =
  398. text_mmap( pzFileName, PROT_READ|PROT_WRITE, MAP_PRIVATE, &cfgfile );
  399. if (TEXT_MMAP_FAILED_ADDR(pzFileText))
  400. return;
  401. if (direction == DIRECTION_CALLED) {
  402. st.flags = OPTST_DEFINED;
  403. direction = DIRECTION_PROCESS;
  404. }
  405. /*
  406. * IF this is called via "optionProcess", then we are presetting.
  407. * This is the default and the PRESETTING bit will be set.
  408. * If this is called via "optionFileLoad", then the bit is not set
  409. * and we consider stuff set herein to be "set" by the client program.
  410. */
  411. if ((pOpts->fOptSet & OPTPROC_PRESETTING) == 0)
  412. st.flags = OPTST_SET;
  413. do {
  414. while (isspace( (int)*pzFileText )) pzFileText++;
  415. if (isalpha( (int)*pzFileText )) {
  416. pzFileText = handleConfig( pOpts, &st, pzFileText, direction );
  417. } else switch (*pzFileText) {
  418. case '<':
  419. if (isalpha( (int)pzFileText[1] ))
  420. pzFileText = handleStructure(pOpts, &st, pzFileText, direction);
  421. else switch (pzFileText[1]) {
  422. case '?':
  423. pzFileText = handleDirective( pOpts, pzFileText );
  424. break;
  425. case '!':
  426. pzFileText = handleComment( pzFileText );
  427. break;
  428. case '/':
  429. pzFileText = strchr( pzFileText+2, '>' );
  430. if (pzFileText++ != NULL)
  431. break;
  432. default:
  433. goto all_done;
  434. }
  435. break;
  436. case '[':
  437. pzFileText = handleProgramSection( pOpts, pzFileText );
  438. break;
  439. case '#':
  440. pzFileText = strchr( pzFileText+1, '\n' );
  441. break;
  442. default:
  443. goto all_done; /* invalid format */
  444. }
  445. } while (pzFileText != NULL);
  446. all_done:
  447. text_munmap( &cfgfile );
  448. }
  449. /* handleComment
  450. *
  451. * "pzText" points to a "<!" sequence.
  452. * Theoretically, we should ensure that it begins with "<!--",
  453. * but actually I don't care that much. It ends with "-->".
  454. */
  455. static char*
  456. handleComment( char* pzText )
  457. {
  458. char* pz = strstr( pzText, "-->" );
  459. if (pz != NULL)
  460. pz += 3;
  461. return pz;
  462. }
  463. /* handleConfig
  464. *
  465. * "pzText" points to the start of some value name.
  466. * The end of the entry is the end of the line that is not preceded by
  467. * a backslash escape character. The string value is always processed
  468. * in "cooked" mode.
  469. */
  470. static char*
  471. handleConfig(
  472. tOptions* pOpts,
  473. tOptState* pOS,
  474. char* pzText,
  475. int direction )
  476. {
  477. char* pzName = pzText++;
  478. char* pzEnd = strchr( pzText, '\n' );
  479. if (pzEnd == NULL)
  480. return pzText + strlen(pzText);
  481. while (ISNAMECHAR( (int)*pzText )) pzText++;
  482. while (isspace( (int)*pzText )) pzText++;
  483. if (pzText > pzEnd) {
  484. name_only:
  485. *pzEnd++ = NUL;
  486. loadOptionLine( pOpts, pOS, pzName, direction, OPTION_LOAD_UNCOOKED );
  487. return pzEnd;
  488. }
  489. /*
  490. * Either the first character after the name is a ':' or '=',
  491. * or else we must have skipped over white space. Anything else
  492. * is an invalid format and we give up parsing the text.
  493. */
  494. if ((*pzText == '=') || (*pzText == ':')) {
  495. while (isspace( (int)*++pzText )) ;
  496. if (pzText > pzEnd)
  497. goto name_only;
  498. } else if (! isspace((int)pzText[-1]))
  499. return NULL;
  500. /*
  501. * IF the value is continued, remove the backslash escape and push "pzEnd"
  502. * on to a newline *not* preceded by a backslash.
  503. */
  504. if (pzEnd[-1] == '\\') {
  505. char* pcD = pzEnd-1;
  506. char* pcS = pzEnd;
  507. for (;;) {
  508. char ch = *(pcS++);
  509. switch (ch) {
  510. case NUL:
  511. pcS = NULL;
  512. case '\n':
  513. *pcD = NUL;
  514. pzEnd = pcS;
  515. goto copy_done;
  516. case '\\':
  517. if (*pcS == '\n') {
  518. ch = *(pcS++);
  519. }
  520. /* FALLTHROUGH */
  521. default:
  522. *(pcD++) = ch;
  523. }
  524. } copy_done:;
  525. } else {
  526. /*
  527. * The newline was not preceded by a backslash. NUL it out
  528. */
  529. *(pzEnd++) = NUL;
  530. }
  531. /*
  532. * "pzName" points to what looks like text for one option/configurable.
  533. * It is NUL terminated. Process it.
  534. */
  535. loadOptionLine( pOpts, pOS, pzName, direction, OPTION_LOAD_UNCOOKED );
  536. return pzEnd;
  537. }
  538. /* handleDirective
  539. *
  540. * "pzText" points to a "<?" sequence.
  541. * For the moment, we only handle "<?program" directives.
  542. */
  543. static char*
  544. handleDirective(
  545. tOptions* pOpts,
  546. char* pzText )
  547. {
  548. char ztitle[32] = "<?";
  549. size_t title_len = strlen( zProg );
  550. size_t name_len;
  551. if ( (strncmp( pzText+2, zProg, title_len ) != 0)
  552. || (! isspace( (int)pzText[title_len+2] )) ) {
  553. pzText = strchr( pzText+2, '>' );
  554. if (pzText != NULL)
  555. pzText++;
  556. return pzText;
  557. }
  558. name_len = strlen( pOpts->pzProgName );
  559. strcpy( ztitle+2, zProg );
  560. title_len += 2;
  561. do {
  562. pzText += title_len;
  563. if (isspace((int)*pzText)) {
  564. while (isspace((int)*pzText)) pzText++;
  565. if ( (strneqvcmp( pzText, pOpts->pzProgName, (int)name_len) == 0)
  566. && (pzText[name_len] == '>')) {
  567. pzText += name_len + 1;
  568. break;
  569. }
  570. }
  571. pzText = strstr( pzText, ztitle );
  572. } while (pzText != NULL);
  573. return pzText;
  574. }
  575. /* handleProgramSection
  576. *
  577. * "pzText" points to a '[' character.
  578. * The "traditional" [PROG_NAME] segmentation of the config file.
  579. * Do not ever mix with the "<?program prog-name>" variation.
  580. */
  581. static char*
  582. handleProgramSection(
  583. tOptions* pOpts,
  584. char* pzText )
  585. {
  586. size_t len = strlen( pOpts->pzPROGNAME );
  587. if ( (strncmp( pzText+1, pOpts->pzPROGNAME, len ) == 0)
  588. && (pzText[len+1] == ']'))
  589. return strchr( pzText + len + 2, '\n' );
  590. if (len > 16)
  591. return NULL;
  592. {
  593. char z[24];
  594. sprintf( z, "[%s]", pOpts->pzPROGNAME );
  595. pzText = strstr( pzText, z );
  596. }
  597. if (pzText != NULL)
  598. pzText = strchr( pzText, '\n' );
  599. return pzText;
  600. }
  601. /* handleStructure
  602. *
  603. * "pzText" points to a '<' character, followed by an alpha.
  604. * The end of the entry is either the "/>" following the name, or else a
  605. * "</name>" string.
  606. */
  607. static char*
  608. handleStructure(
  609. tOptions* pOpts,
  610. tOptState* pOS,
  611. char* pzText,
  612. int direction )
  613. {
  614. tOptionLoadMode mode = option_load_mode;
  615. tOptionValue valu;
  616. char* pzName = ++pzText;
  617. char* pzData;
  618. char* pcNulPoint;
  619. while (ISNAMECHAR( *pzText )) pzText++;
  620. pcNulPoint = pzText;
  621. valu.valType = OPARG_TYPE_STRING;
  622. switch (*pzText) {
  623. case ' ':
  624. case '\t':
  625. pzText = parseAttributes( pOpts, pzText, &mode, &valu );
  626. if (*pzText == '>')
  627. break;
  628. if (*pzText != '/')
  629. return NULL;
  630. /* FALLTHROUGH */
  631. case '/':
  632. if (pzText[1] != '>')
  633. return NULL;
  634. *pzText = NUL;
  635. pzText += 2;
  636. loadOptionLine( pOpts, pOS, pzName, direction, mode );
  637. return pzText;
  638. case '>':
  639. break;
  640. default:
  641. pzText = strchr( pzText, '>');
  642. if (pzText != NULL)
  643. pzText++;
  644. return pzText;
  645. }
  646. /*
  647. * If we are here, we have a value. "pzText" points to a closing angle
  648. * bracket. Separate the name from the value for a moment.
  649. */
  650. *pcNulPoint = NUL;
  651. pzData = ++pzText;
  652. /*
  653. * Find the end of the option text and NUL terminate it
  654. */
  655. {
  656. char z[64], *pz = z;
  657. size_t len = strlen(pzName) + 4;
  658. if (len > sizeof(z))
  659. pz = AGALOC(len, "scan name");
  660. sprintf( pz, "</%s>", pzName );
  661. *pzText = ' ';
  662. pzText = strstr( pzText, pz );
  663. if (pz != z) AGFREE(pz);
  664. if (pzText == NULL)
  665. return pzText;
  666. *pzText = NUL;
  667. pzText += len-1;
  668. }
  669. /*
  670. * Rejoin the name and value for parsing by "loadOptionLine()".
  671. * Erase any attributes parsed by "parseAttributes()".
  672. */
  673. memset(pcNulPoint, ' ', pzData - pcNulPoint);
  674. /*
  675. * "pzName" points to what looks like text for one option/configurable.
  676. * It is NUL terminated. Process it.
  677. */
  678. loadOptionLine( pOpts, pOS, pzName, direction, mode );
  679. return pzText;
  680. }
  681. /* internalFileLoad
  682. *
  683. * Load a configuration file. This may be invoked either from
  684. * scanning the "homerc" list, or from a specific file request.
  685. * (see "optionFileLoad()", the implementation for --load-opts)
  686. */
  687. LOCAL void
  688. internalFileLoad( tOptions* pOpts )
  689. {
  690. int idx;
  691. int inc = DIRECTION_PRESET;
  692. char zFileName[ AG_PATH_MAX+1 ];
  693. if (pOpts->papzHomeList == NULL)
  694. return;
  695. /*
  696. * Find the last RC entry (highest priority entry)
  697. */
  698. for (idx = 0; pOpts->papzHomeList[ idx+1 ] != NULL; ++idx) ;
  699. /*
  700. * For every path in the home list, ... *TWICE* We start at the last
  701. * (highest priority) entry, work our way down to the lowest priority,
  702. * handling the immediate options.
  703. * Then we go back up, doing the normal options.
  704. */
  705. for (;;) {
  706. struct stat StatBuf;
  707. cch_t* pzPath;
  708. /*
  709. * IF we've reached the bottom end, change direction
  710. */
  711. if (idx < 0) {
  712. inc = DIRECTION_PROCESS;
  713. idx = 0;
  714. }
  715. pzPath = pOpts->papzHomeList[ idx ];
  716. /*
  717. * IF we've reached the top end, bail out
  718. */
  719. if (pzPath == NULL)
  720. break;
  721. idx += inc;
  722. if (! optionMakePath( zFileName, (int)sizeof(zFileName),
  723. pzPath, pOpts->pzProgPath ))
  724. continue;
  725. /*
  726. * IF the file name we constructed is a directory,
  727. * THEN append the Resource Configuration file name
  728. * ELSE we must have the complete file name
  729. */
  730. if (stat( zFileName, &StatBuf ) != 0)
  731. continue; /* bogus name - skip the home list entry */
  732. if (S_ISDIR( StatBuf.st_mode )) {
  733. size_t len = strlen( zFileName );
  734. char* pz;
  735. if (len + 1 + strlen( pOpts->pzRcName ) >= sizeof( zFileName ))
  736. continue;
  737. pz = zFileName + len;
  738. if (pz[-1] != DIRCH)
  739. *(pz++) = DIRCH;
  740. strcpy( pz, pOpts->pzRcName );
  741. }
  742. filePreset( pOpts, zFileName, inc );
  743. /*
  744. * IF we are now to skip config files AND we are presetting,
  745. * THEN change direction. We must go the other way.
  746. */
  747. if (SKIP_RC_FILES(pOpts) && PRESETTING(inc)) {
  748. idx -= inc; /* go back and reprocess current file */
  749. inc = DIRECTION_PROCESS;
  750. }
  751. } /* For every path in the home list, ... */
  752. }
  753. /*=export_func optionFileLoad
  754. *
  755. * what: Load the locatable config files, in order
  756. *
  757. * arg: + tOptions* + pOpts + program options descriptor +
  758. * arg: + char const* + pzProg + program name +
  759. *
  760. * ret_type: int
  761. * ret_desc: 0 -> SUCCESS, -1 -> FAILURE
  762. *
  763. * doc:
  764. *
  765. * This function looks in all the specified directories for a configuration
  766. * file ("rc" file or "ini" file) and processes any found twice. The first
  767. * time through, they are processed in reverse order (last file first). At
  768. * that time, only "immediate action" configurables are processed. For
  769. * example, if the last named file specifies not processing any more
  770. * configuration files, then no more configuration files will be processed.
  771. * Such an option in the @strong{first} named directory will have no effect.
  772. *
  773. * Once the immediate action configurables have been handled, then the
  774. * directories are handled in normal, forward order. In that way, later
  775. * config files can override the settings of earlier config files.
  776. *
  777. * See the AutoOpts documentation for a thorough discussion of the
  778. * config file format.
  779. *
  780. * Configuration files not found or not decipherable are simply ignored.
  781. *
  782. * err: Returns the value, "-1" if the program options descriptor
  783. * is out of date or indecipherable. Otherwise, the value "0" will
  784. * always be returned.
  785. =*/
  786. int
  787. optionFileLoad( tOptions* pOpts, char const* pzProgram )
  788. {
  789. if (! SUCCESSFUL( validateOptionsStruct( pOpts, pzProgram )))
  790. return -1;
  791. pOpts->pzProgName = pzProgram;
  792. internalFileLoad( pOpts );
  793. return 0;
  794. }
  795. /*=export_func optionLoadOpt
  796. * private:
  797. *
  798. * what: Load an option rc/ini file
  799. * arg: + tOptions* + pOpts + program options descriptor +
  800. * arg: + tOptDesc* + pOptDesc + the descriptor for this arg +
  801. *
  802. * doc:
  803. * Processes the options found in the file named with
  804. * pOptDesc->optArg.argString.
  805. =*/
  806. void
  807. optionLoadOpt( tOptions* pOpts, tOptDesc* pOptDesc )
  808. {
  809. /*
  810. * IF the option is not being disabled,
  811. * THEN load the file. There must be a file.
  812. * (If it is being disabled, then the disablement processing
  813. * already took place. It must be done to suppress preloading
  814. * of ini/rc files.)
  815. */
  816. if (! DISABLED_OPT( pOptDesc )) {
  817. struct stat sb;
  818. if (stat( pOptDesc->optArg.argString, &sb ) != 0) {
  819. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
  820. return;
  821. fprintf( stderr, zFSErrOptLoad, errno, strerror( errno ),
  822. pOptDesc->optArg.argString );
  823. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  824. /* NOT REACHED */
  825. }
  826. if (! S_ISREG( sb.st_mode )) {
  827. if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
  828. return;
  829. fprintf( stderr, zNotFile, pOptDesc->optArg.argString );
  830. (*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
  831. /* NOT REACHED */
  832. }
  833. filePreset(pOpts, pOptDesc->optArg.argString, DIRECTION_CALLED);
  834. }
  835. }
  836. /* parseAttributes
  837. *
  838. * Parse the various attributes of an XML-styled config file entry
  839. */
  840. LOCAL char*
  841. parseAttributes(
  842. tOptions* pOpts,
  843. char* pzText,
  844. tOptionLoadMode* pMode,
  845. tOptionValue* pType )
  846. {
  847. size_t lenLoadType = strlen( zLoadType );
  848. size_t lenKeyWords = strlen( zKeyWords );
  849. size_t lenSetMem = strlen( zSetMembers );
  850. do {
  851. switch (*pzText) {
  852. case '/': pType->valType = OPARG_TYPE_NONE;
  853. case '>': return pzText;
  854. default:
  855. case NUL: return NULL;
  856. case ' ':
  857. case '\t':
  858. case '\n':
  859. case '\f':
  860. case '\r':
  861. case '\v':
  862. break;
  863. }
  864. while (isspace( (int)*++pzText )) ;
  865. if (strncmp( pzText, zLoadType, lenLoadType ) == 0) {
  866. pzText = parseValueType( pzText+lenLoadType, pType );
  867. continue;
  868. }
  869. if (strncmp( pzText, zKeyWords, lenKeyWords ) == 0) {
  870. pzText = parseKeyWordType( pOpts, pzText+lenKeyWords, pType );
  871. continue;
  872. }
  873. if (strncmp( pzText, zSetMembers, lenSetMem ) == 0) {
  874. pzText = parseSetMemType( pOpts, pzText+lenSetMem, pType );
  875. continue;
  876. }
  877. pzText = parseLoadMode( pzText, pMode );
  878. } while (pzText != NULL);
  879. return pzText;
  880. }
  881. /* parseKeyWordType
  882. *
  883. * "pzText" points to the character after "words=".
  884. * What should follow is a name of a keyword (enumeration) list.
  885. */
  886. static char*
  887. parseKeyWordType(
  888. tOptions* pOpts,
  889. char* pzText,
  890. tOptionValue* pType )
  891. {
  892. return skipUnknown( pzText );
  893. }
  894. /* parseLoadMode
  895. *
  896. * "pzText" points to some name character. We check for "cooked" or
  897. * "uncooked" or "keep". This function should handle any attribute
  898. * that does not have an associated value.
  899. */
  900. static char*
  901. parseLoadMode(
  902. char* pzText,
  903. tOptionLoadMode* pMode )
  904. {
  905. {
  906. size_t len = strlen(zLoadCooked);
  907. if (strncmp( pzText, zLoadCooked, len ) == 0) {
  908. if ( (pzText[len] == '>')
  909. || (pzText[len] == '/')
  910. || isspace((int)pzText[len])) {
  911. *pMode = OPTION_LOAD_COOKED;
  912. return pzText + len;
  913. }
  914. goto unknown;
  915. }
  916. }
  917. {
  918. size_t len = strlen(zLoadUncooked);
  919. if (strncmp( pzText, zLoadUncooked, len ) == 0) {
  920. if ( (pzText[len] == '>')
  921. || (pzText[len] == '/')
  922. || isspace((int)pzText[len])) {
  923. *pMode = OPTION_LOAD_UNCOOKED;
  924. return pzText + len;
  925. }
  926. goto unknown;
  927. }
  928. }
  929. {
  930. size_t len = strlen(zLoadKeep);
  931. if (strncmp( pzText, zLoadKeep, len ) == 0) {
  932. if ( (pzText[len] == '>')
  933. || (pzText[len] == '/')
  934. || isspace((int)pzText[len])) {
  935. *pMode = OPTION_LOAD_KEEP;
  936. return pzText + len;
  937. }
  938. goto unknown;
  939. }
  940. }
  941. unknown:
  942. return skipUnknown( pzText );
  943. }
  944. /* parseSetMemType
  945. *
  946. * "pzText" points to the character after "members="
  947. * What should follow is a name of a "set membership".
  948. * A collection of bit flags.
  949. */
  950. static char*
  951. parseSetMemType(
  952. tOptions* pOpts,
  953. char* pzText,
  954. tOptionValue* pType )
  955. {
  956. return skipUnknown( pzText );
  957. }
  958. /* parseValueType
  959. *
  960. * "pzText" points to the character after "type="
  961. */
  962. static char*
  963. parseValueType(
  964. char* pzText,
  965. tOptionValue* pType )
  966. {
  967. {
  968. size_t len = strlen(zLtypeString);
  969. if (strncmp( pzText, zLtypeString, len ) == 0) {
  970. if ((pzText[len] == '>') || isspace((int)pzText[len])) {
  971. pType->valType = OPARG_TYPE_STRING;
  972. return pzText + len;
  973. }
  974. goto unknown;
  975. }
  976. }
  977. {
  978. size_t len = strlen(zLtypeInteger);
  979. if (strncmp( pzText, zLtypeInteger, len ) == 0) {
  980. if ((pzText[len] == '>') || isspace((int)pzText[len])) {
  981. pType->valType = OPARG_TYPE_NUMERIC;
  982. return pzText + len;
  983. }
  984. goto unknown;
  985. }
  986. }
  987. {
  988. size_t len = strlen(zLtypeBool);
  989. if (strncmp( pzText, zLtypeBool, len ) == 0) {
  990. if ((pzText[len] == '>') || isspace(pzText[len])) {
  991. pType->valType = OPARG_TYPE_BOOLEAN;
  992. return pzText + len;
  993. }
  994. goto unknown;
  995. }
  996. }
  997. {
  998. size_t len = strlen(zLtypeKeyword);
  999. if (strncmp( pzText, zLtypeKeyword, len ) == 0) {
  1000. if ((pzText[len] == '>') || isspace((int)pzText[len])) {
  1001. pType->valType = OPARG_TYPE_ENUMERATION;
  1002. return pzText + len;
  1003. }
  1004. goto unknown;
  1005. }
  1006. }
  1007. {
  1008. size_t len = strlen(zLtypeSetMembership);
  1009. if (strncmp( pzText, zLtypeSetMembership, len ) == 0) {
  1010. if ((pzText[len] == '>') || isspace((int)pzText[len])) {
  1011. pType->valType = OPARG_TYPE_MEMBERSHIP;
  1012. return pzText + len;
  1013. }
  1014. goto unknown;
  1015. }
  1016. }
  1017. {
  1018. size_t len = strlen(zLtypeNest);
  1019. if (strncmp( pzText, zLtypeNest, len ) == 0) {
  1020. if ((pzText[len] == '>') || isspace((int)pzText[len])) {
  1021. pType->valType = OPARG_TYPE_HIERARCHY;
  1022. return pzText + len;
  1023. }
  1024. goto unknown;
  1025. }
  1026. }
  1027. unknown:
  1028. pType->valType = OPARG_TYPE_NONE;
  1029. return skipUnknown( pzText );
  1030. }
  1031. /* skipUnknown
  1032. *
  1033. * Skip over some unknown attribute
  1034. */
  1035. static char*
  1036. skipUnknown( char* pzText )
  1037. {
  1038. for (;; pzText++) {
  1039. if (isspace( (int)*pzText )) return pzText;
  1040. switch (*pzText) {
  1041. case NUL: return NULL;
  1042. case '/':
  1043. case '>': return pzText;
  1044. }
  1045. }
  1046. }
  1047. /* validateOptionsStruct
  1048. *
  1049. * Make sure the option descriptor is there and that we understand it.
  1050. * This should be called from any user entry point where one needs to
  1051. * worry about validity. (Some entry points are free to assume that
  1052. * the call is not the first to the library and, thus, that this has
  1053. * already been called.)
  1054. */
  1055. LOCAL tSuccess
  1056. validateOptionsStruct( tOptions* pOpts, char const* pzProgram )
  1057. {
  1058. if (pOpts == NULL) {
  1059. fputs( zAO_Bad, stderr );
  1060. exit( EXIT_FAILURE );
  1061. }
  1062. /*
  1063. * IF the client has enabled translation and the translation procedure
  1064. * is available, then go do it.
  1065. */
  1066. if ( ((pOpts->fOptSet & OPTPROC_TRANSLATE) != 0)
  1067. && (pOpts->pTransProc != 0) ) {
  1068. (*pOpts->pTransProc)();
  1069. pOpts->fOptSet &= ~OPTPROC_TRANSLATE;
  1070. }
  1071. /*
  1072. * IF the struct version is not the current, and also
  1073. * either too large (?!) or too small,
  1074. * THEN emit error message and fail-exit
  1075. */
  1076. if ( ( pOpts->structVersion != OPTIONS_STRUCT_VERSION )
  1077. && ( (pOpts->structVersion > OPTIONS_STRUCT_VERSION )
  1078. || (pOpts->structVersion < OPTIONS_MINIMUM_VERSION )
  1079. ) ) {
  1080. fprintf( stderr, zAO_Err, pOpts->origArgVect[0],
  1081. NUM_TO_VER( pOpts->structVersion ));
  1082. if (pOpts->structVersion > OPTIONS_STRUCT_VERSION )
  1083. fputs( zAO_Big, stderr );
  1084. else
  1085. fputs( zAO_Sml, stderr );
  1086. return FAILURE;
  1087. }
  1088. /*
  1089. * If the program name hasn't been set, then set the name and the path
  1090. * and the set of equivalent characters.
  1091. */
  1092. if (pOpts->pzProgName == NULL) {
  1093. char const* pz = strrchr( pzProgram, DIRCH );
  1094. if (pz == NULL)
  1095. pOpts->pzProgName = pzProgram;
  1096. else pOpts->pzProgName = pz+1;
  1097. pOpts->pzProgPath = pzProgram;
  1098. /*
  1099. * when comparing long names, these are equivalent
  1100. */
  1101. strequate( zSepChars );
  1102. }
  1103. return SUCCESS;
  1104. }
  1105. /**
  1106. * Local Variables:
  1107. * mode: C
  1108. * c-file-style: "stroustrup"
  1109. * indent-tabs-mode: nil
  1110. * End:
  1111. * end of autoopts/configfile.c */