params.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* -------------------------------------------------------------------------- **
  2. * Microsoft Network Services for Unix, AKA., Andrew Tridgell's SAMBA.
  3. *
  4. * This module Copyright (C) 1990-1998 Karl Auer
  5. *
  6. * Rewritten almost completely by Christopher R. Hertel
  7. * at the University of Minnesota, September, 1997.
  8. * This module Copyright (C) 1997-1998 by the University of Minnesota
  9. * -------------------------------------------------------------------------- **
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. *
  25. * -------------------------------------------------------------------------- **
  26. *
  27. * Module name: params
  28. *
  29. * -------------------------------------------------------------------------- **
  30. *
  31. * This module performs lexical analysis and initial parsing of a
  32. * Windows-like parameter file. It recognizes and handles four token
  33. * types: section-name, parameter-name, parameter-value, and
  34. * end-of-file. Comments and line continuation are handled
  35. * internally.
  36. *
  37. * The entry point to the module is function pm_process(). This
  38. * function opens the source file, calls the Parse() function to parse
  39. * the input, and then closes the file when either the EOF is reached
  40. * or a fatal error is encountered.
  41. *
  42. * A sample parameter file might look like this:
  43. *
  44. * [section one]
  45. * parameter one = value string
  46. * parameter two = another value
  47. * [section two]
  48. * new parameter = some value or t'other
  49. *
  50. * The parameter file is divided into sections by section headers:
  51. * section names enclosed in square brackets (eg. [section one]).
  52. * Each section contains parameter lines, each of which consist of a
  53. * parameter name and value delimited by an equal sign. Roughly, the
  54. * syntax is:
  55. *
  56. * <file> :== { <section> } EOF
  57. *
  58. * <section> :== <section header> { <parameter line> }
  59. *
  60. * <section header> :== '[' NAME ']'
  61. *
  62. * <parameter line> :== NAME '=' VALUE '\n'
  63. *
  64. * Blank lines and comment lines are ignored. Comment lines are lines
  65. * beginning with either a semicolon (';') or a pound sign ('#').
  66. *
  67. * All whitespace in section names and parameter names is compressed
  68. * to single spaces. Leading and trailing whitespace is stipped from
  69. * both names and values.
  70. *
  71. * Only the first equals sign in a parameter line is significant.
  72. * Parameter values may contain equals signs, square brackets and
  73. * semicolons. Internal whitespace is retained in parameter values,
  74. * with the exception of the '\r' character, which is stripped for
  75. * historic reasons. Parameter names may not start with a left square
  76. * bracket, an equal sign, a pound sign, or a semicolon, because these
  77. * are used to identify other tokens.
  78. *
  79. * -------------------------------------------------------------------------- **
  80. */
  81. #include "includes.h"
  82. /* -------------------------------------------------------------------------- **
  83. * Constants...
  84. */
  85. #define BUFR_INC 1024
  86. /* -------------------------------------------------------------------------- **
  87. * Variables...
  88. *
  89. * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG()
  90. * messages will be produced.
  91. * bufr - pointer to a global buffer. This is probably a kludge,
  92. * but it was the nicest kludge I could think of (for now).
  93. * bSize - The size of the global buffer <bufr>.
  94. */
  95. extern int DEBUGLEVEL;
  96. static char *bufr = NULL;
  97. static int bSize = 0;
  98. /* -------------------------------------------------------------------------- **
  99. * Functions...
  100. */
  101. static int EatWhitespace( FILE *InFile )
  102. /* ------------------------------------------------------------------------ **
  103. * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
  104. * character, or newline, or EOF.
  105. *
  106. * Input: InFile - Input source.
  107. *
  108. * Output: The next non-whitespace character in the input stream.
  109. *
  110. * Notes: Because the config files use a line-oriented grammar, we
  111. * explicitly exclude the newline character from the list of
  112. * whitespace characters.
  113. * - Note that both EOF (-1) and the nul character ('\0') are
  114. * considered end-of-file markers.
  115. *
  116. * ------------------------------------------------------------------------ **
  117. */
  118. {
  119. int c;
  120. for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
  121. ;
  122. return( c );
  123. } /* EatWhitespace */
  124. static int EatComment( FILE *InFile )
  125. /* ------------------------------------------------------------------------ **
  126. * Scan to the end of a comment.
  127. *
  128. * Input: InFile - Input source.
  129. *
  130. * Output: The character that marks the end of the comment. Normally,
  131. * this will be a newline, but it *might* be an EOF.
  132. *
  133. * Notes: Because the config files use a line-oriented grammar, we
  134. * explicitly exclude the newline character from the list of
  135. * whitespace characters.
  136. * - Note that both EOF (-1) and the nul character ('\0') are
  137. * considered end-of-file markers.
  138. *
  139. * ------------------------------------------------------------------------ **
  140. */
  141. {
  142. int c;
  143. for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
  144. ;
  145. return( c );
  146. } /* EatComment */
  147. static int Continuation( char *line, int pos )
  148. /* ------------------------------------------------------------------------ **
  149. * Scan backards within a string to discover if the last non-whitespace
  150. * character is a line-continuation character ('\\').
  151. *
  152. * Input: line - A pointer to a buffer containing the string to be
  153. * scanned.
  154. * pos - This is taken to be the offset of the end of the
  155. * string. This position is *not* scanned.
  156. *
  157. * Output: The offset of the '\\' character if it was found, or -1 to
  158. * indicate that it was not.
  159. *
  160. * ------------------------------------------------------------------------ **
  161. */
  162. {
  163. pos--;
  164. while( (pos >= 0) && isspace(line[pos]) )
  165. pos--;
  166. return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
  167. } /* Continuation */
  168. static BOOL Section( FILE *InFile, BOOL (*sfunc)(const char *) )
  169. /* ------------------------------------------------------------------------ **
  170. * Scan a section name, and pass the name to function sfunc().
  171. *
  172. * Input: InFile - Input source.
  173. * sfunc - Pointer to the function to be called if the section
  174. * name is successfully read.
  175. *
  176. * Output: True if the section name was read and True was returned from
  177. * <sfunc>. False if <sfunc> failed or if a lexical error was
  178. * encountered.
  179. *
  180. * ------------------------------------------------------------------------ **
  181. */
  182. {
  183. int c;
  184. int i;
  185. int end;
  186. const char *func = "params.c:Section() -";
  187. i = 0; /* <i> is the offset of the next free byte in bufr[] and */
  188. end = 0; /* <end> is the current "end of string" offset. In most */
  189. /* cases these will be the same, but if the last */
  190. /* character written to bufr[] is a space, then <end> */
  191. /* will be one less than <i>. */
  192. c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
  193. /* past initial white space. */
  194. while( (EOF != c) && (c > 0) )
  195. {
  196. /* Check that the buffer is big enough for the next character. */
  197. if( i > (bSize - 2) )
  198. {
  199. bSize += BUFR_INC;
  200. bufr = Realloc( bufr, bSize );
  201. if( NULL == bufr )
  202. {
  203. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  204. return( False );
  205. }
  206. }
  207. /* Handle a single character. */
  208. switch( c )
  209. {
  210. case ']': /* Found the closing bracket. */
  211. bufr[end] = '\0';
  212. if( 0 == end ) /* Don't allow an empty name. */
  213. {
  214. DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
  215. return( False );
  216. }
  217. if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */
  218. return( False );
  219. (void)EatComment( InFile ); /* Finish off the line. */
  220. return( True );
  221. case '\n': /* Got newline before closing ']'. */
  222. i = Continuation( bufr, i ); /* Check for line continuation. */
  223. if( i < 0 )
  224. {
  225. bufr[end] = '\0';
  226. DEBUG(0, ("%s Badly formed line in configuration file: %s\n",
  227. func, bufr ));
  228. return( False );
  229. }
  230. end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
  231. c = getc( InFile ); /* Continue with next line. */
  232. break;
  233. default: /* All else are a valid name chars. */
  234. if( isspace( c ) ) /* One space per whitespace region. */
  235. {
  236. bufr[end] = ' ';
  237. i = end + 1;
  238. c = EatWhitespace( InFile );
  239. }
  240. else /* All others copy verbatim. */
  241. {
  242. bufr[i++] = c;
  243. end = i;
  244. c = getc( InFile );
  245. }
  246. }
  247. }
  248. /* We arrive here if we've met the EOF before the closing bracket. */
  249. DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr ));
  250. return( False );
  251. } /* Section */
  252. static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(const char *, const char *), int c )
  253. /* ------------------------------------------------------------------------ **
  254. * Scan a parameter name and value, and pass these two fields to pfunc().
  255. *
  256. * Input: InFile - The input source.
  257. * pfunc - A pointer to the function that will be called to
  258. * process the parameter, once it has been scanned.
  259. * c - The first character of the parameter name, which
  260. * would have been read by Parse(). Unlike a comment
  261. * line or a section header, there is no lead-in
  262. * character that can be discarded.
  263. *
  264. * Output: True if the parameter name and value were scanned and processed
  265. * successfully, else False.
  266. *
  267. * Notes: This function is in two parts. The first loop scans the
  268. * parameter name. Internal whitespace is compressed, and an
  269. * equal sign (=) terminates the token. Leading and trailing
  270. * whitespace is discarded. The second loop scans the parameter
  271. * value. When both have been successfully identified, they are
  272. * passed to pfunc() for processing.
  273. *
  274. * ------------------------------------------------------------------------ **
  275. */
  276. {
  277. int i = 0; /* Position within bufr. */
  278. int end = 0; /* bufr[end] is current end-of-string. */
  279. int vstart = 0; /* Starting position of the parameter value. */
  280. const char *func = "params.c:Parameter() -";
  281. /* Read the parameter name. */
  282. while( 0 == vstart ) /* Loop until we've found the start of the value. */
  283. {
  284. if( i > (bSize - 2) ) /* Ensure there's space for next char. */
  285. {
  286. bSize += BUFR_INC;
  287. bufr = Realloc( bufr, bSize );
  288. if( NULL == bufr )
  289. {
  290. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  291. return( False );
  292. }
  293. }
  294. switch( c )
  295. {
  296. case '=': /* Equal sign marks end of param name. */
  297. if( 0 == end ) /* Don't allow an empty name. */
  298. {
  299. DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
  300. return( False );
  301. }
  302. bufr[end++] = '\0'; /* Mark end of string & advance. */
  303. i = end; /* New string starts here. */
  304. vstart = end; /* New string is parameter value. */
  305. bufr[i] = '\0'; /* New string is nul, for now. */
  306. break;
  307. case '\n': /* Find continuation char, else error. */
  308. i = Continuation( bufr, i );
  309. if( i < 0 )
  310. {
  311. bufr[end] = '\0';
  312. DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n",
  313. func, bufr ));
  314. return( True );
  315. }
  316. end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
  317. c = getc( InFile ); /* Read past eoln. */
  318. break;
  319. case '\0': /* Shouldn't have EOF within param name. */
  320. case EOF:
  321. bufr[i] = '\0';
  322. DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr ));
  323. return( True );
  324. default:
  325. if( isspace( c ) ) /* One ' ' per whitespace region. */
  326. {
  327. bufr[end] = ' ';
  328. i = end + 1;
  329. c = EatWhitespace( InFile );
  330. }
  331. else /* All others verbatim. */
  332. {
  333. bufr[i++] = c;
  334. end = i;
  335. c = getc( InFile );
  336. }
  337. }
  338. }
  339. /* Now parse the value. */
  340. c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
  341. while( (EOF !=c) && (c > 0) )
  342. {
  343. if( i > (bSize - 2) ) /* Make sure there's enough room. */
  344. {
  345. bSize += BUFR_INC;
  346. bufr = Realloc( bufr, bSize );
  347. if( NULL == bufr )
  348. {
  349. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  350. return( False );
  351. }
  352. }
  353. switch( c )
  354. {
  355. case '\r': /* Explicitly remove '\r' because the older */
  356. c = getc( InFile ); /* version called fgets_slash() which also */
  357. break; /* removes them. */
  358. case '\n': /* Marks end of value unless there's a '\'. */
  359. i = Continuation( bufr, i );
  360. if( i < 0 )
  361. c = 0;
  362. else
  363. {
  364. for( end = i; (end >= 0) && isspace(bufr[end]); end-- )
  365. ;
  366. c = getc( InFile );
  367. }
  368. break;
  369. default: /* All others verbatim. Note that spaces do */
  370. bufr[i++] = c; /* not advance <end>. This allows trimming */
  371. if( !isspace( c ) ) /* of whitespace at the end of the line. */
  372. end = i;
  373. c = getc( InFile );
  374. break;
  375. }
  376. }
  377. bufr[end] = '\0'; /* End of value. */
  378. return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
  379. } /* Parameter */
  380. static BOOL Parse( FILE *InFile,
  381. BOOL (*sfunc)(const char *),
  382. BOOL (*pfunc)(const char *, const char *) )
  383. /* ------------------------------------------------------------------------ **
  384. * Scan & parse the input.
  385. *
  386. * Input: InFile - Input source.
  387. * sfunc - Function to be called when a section name is scanned.
  388. * See Section().
  389. * pfunc - Function to be called when a parameter is scanned.
  390. * See Parameter().
  391. *
  392. * Output: True if the file was successfully scanned, else False.
  393. *
  394. * Notes: The input can be viewed in terms of 'lines'. There are four
  395. * types of lines:
  396. * Blank - May contain whitespace, otherwise empty.
  397. * Comment - First non-whitespace character is a ';' or '#'.
  398. * The remainder of the line is ignored.
  399. * Section - First non-whitespace character is a '['.
  400. * Parameter - The default case.
  401. *
  402. * ------------------------------------------------------------------------ **
  403. */
  404. {
  405. int c;
  406. c = EatWhitespace( InFile );
  407. while( (EOF != c) && (c > 0) )
  408. {
  409. switch( c )
  410. {
  411. case '\n': /* Blank line. */
  412. c = EatWhitespace( InFile );
  413. break;
  414. case ';': /* Comment line. */
  415. case '#':
  416. c = EatComment( InFile );
  417. break;
  418. case '[': /* Section Header. */
  419. if( !Section( InFile, sfunc ) )
  420. return( False );
  421. c = EatWhitespace( InFile );
  422. break;
  423. case '\\': /* Bogus backslash. */
  424. c = EatWhitespace( InFile );
  425. break;
  426. default: /* Parameter line. */
  427. if( !Parameter( InFile, pfunc, c ) )
  428. return( False );
  429. c = EatWhitespace( InFile );
  430. break;
  431. }
  432. }
  433. return( True );
  434. } /* Parse */
  435. static FILE *OpenConfFile( const char *FileName )
  436. /* ------------------------------------------------------------------------ **
  437. * Open a configuration file.
  438. *
  439. * Input: FileName - The pathname of the config file to be opened.
  440. *
  441. * Output: A pointer of type (FILE *) to the opened file, or NULL if the
  442. * file could not be opened.
  443. *
  444. * ------------------------------------------------------------------------ **
  445. */
  446. {
  447. FILE *OpenedFile;
  448. const char *func = "params.c:OpenConfFile() -";
  449. extern BOOL in_client;
  450. int lvl = in_client?1:0;
  451. if( NULL == FileName || 0 == *FileName )
  452. {
  453. DEBUG( lvl, ("%s No configuration filename specified.\n", func) );
  454. return( NULL );
  455. }
  456. OpenedFile = sys_fopen( FileName, "r" );
  457. if( NULL == OpenedFile )
  458. {
  459. DEBUG( lvl,
  460. ("%s Unable to open configuration file \"%s\":\n\t%s\n",
  461. func, FileName, strerror(errno)) );
  462. }
  463. return( OpenedFile );
  464. } /* OpenConfFile */
  465. BOOL pm_process( const char *FileName,
  466. BOOL (*sfunc)(const char *),
  467. BOOL (*pfunc)(const char *, const char *) )
  468. /* ------------------------------------------------------------------------ **
  469. * Process the named parameter file.
  470. *
  471. * Input: FileName - The pathname of the parameter file to be opened.
  472. * sfunc - A pointer to a function that will be called when
  473. * a section name is discovered.
  474. * pfunc - A pointer to a function that will be called when
  475. * a parameter name and value are discovered.
  476. *
  477. * Output: TRUE if the file was successfully parsed, else FALSE.
  478. *
  479. * ------------------------------------------------------------------------ **
  480. */
  481. {
  482. int result;
  483. FILE *InFile;
  484. const char *func = "params.c:pm_process() -";
  485. InFile = OpenConfFile( FileName ); /* Open the config file. */
  486. if( NULL == InFile )
  487. return( False );
  488. DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
  489. if( NULL != bufr ) /* If we already have a buffer */
  490. result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */
  491. /* use it. */
  492. else /* If we don't have a buffer */
  493. { /* allocate one, then parse, */
  494. bSize = BUFR_INC; /* then free. */
  495. bufr = (char *)malloc( bSize );
  496. if( NULL == bufr )
  497. {
  498. DEBUG(0,("%s memory allocation failure.\n", func));
  499. fclose(InFile);
  500. return( False );
  501. }
  502. result = Parse( InFile, sfunc, pfunc );
  503. free( bufr );
  504. bufr = NULL;
  505. bSize = 0;
  506. }
  507. fclose(InFile);
  508. if( !result ) /* Generic failure. */
  509. {
  510. DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func));
  511. return( False );
  512. }
  513. return( True ); /* Generic success. */
  514. } /* pm_process */
  515. /* -------------------------------------------------------------------------- */