params.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. const char *unix_error_string (int error_num);
  83. /* -------------------------------------------------------------------------- **
  84. * Constants...
  85. */
  86. #define BUFR_INC 1024
  87. /* -------------------------------------------------------------------------- **
  88. * Variables...
  89. *
  90. * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG()
  91. * messages will be produced.
  92. * bufr - pointer to a global buffer. This is probably a kludge,
  93. * but it was the nicest kludge I could think of (for now).
  94. * bSize - The size of the global buffer <bufr>.
  95. */
  96. extern int DEBUGLEVEL;
  97. static char *bufr = NULL;
  98. static int bSize = 0;
  99. /* -------------------------------------------------------------------------- **
  100. * Functions...
  101. */
  102. static int EatWhitespace( FILE *InFile )
  103. /* ------------------------------------------------------------------------ **
  104. * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
  105. * character, or newline, or EOF.
  106. *
  107. * Input: InFile - Input source.
  108. *
  109. * Output: The next non-whitespace character in the input stream.
  110. *
  111. * Notes: Because the config files use a line-oriented grammar, we
  112. * explicitly exclude the newline character from the list of
  113. * whitespace characters.
  114. * - Note that both EOF (-1) and the nul character ('\0') are
  115. * considered end-of-file markers.
  116. *
  117. * ------------------------------------------------------------------------ **
  118. */
  119. {
  120. int c;
  121. for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
  122. ;
  123. return( c );
  124. } /* EatWhitespace */
  125. static int EatComment( FILE *InFile )
  126. /* ------------------------------------------------------------------------ **
  127. * Scan to the end of a comment.
  128. *
  129. * Input: InFile - Input source.
  130. *
  131. * Output: The character that marks the end of the comment. Normally,
  132. * this will be a newline, but it *might* be an EOF.
  133. *
  134. * Notes: Because the config files use a line-oriented grammar, we
  135. * explicitly exclude the newline character from the list of
  136. * whitespace characters.
  137. * - Note that both EOF (-1) and the nul character ('\0') are
  138. * considered end-of-file markers.
  139. *
  140. * ------------------------------------------------------------------------ **
  141. */
  142. {
  143. int c;
  144. for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
  145. ;
  146. return( c );
  147. } /* EatComment */
  148. static int Continuation( char *line, int pos )
  149. /* ------------------------------------------------------------------------ **
  150. * Scan backards within a string to discover if the last non-whitespace
  151. * character is a line-continuation character ('\\').
  152. *
  153. * Input: line - A pointer to a buffer containing the string to be
  154. * scanned.
  155. * pos - This is taken to be the offset of the end of the
  156. * string. This position is *not* scanned.
  157. *
  158. * Output: The offset of the '\\' character if it was found, or -1 to
  159. * indicate that it was not.
  160. *
  161. * ------------------------------------------------------------------------ **
  162. */
  163. {
  164. pos--;
  165. while( (pos >= 0) && isspace(line[pos]) )
  166. pos--;
  167. return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
  168. } /* Continuation */
  169. static BOOL Section( FILE *InFile, BOOL (*sfunc)(const char *) )
  170. /* ------------------------------------------------------------------------ **
  171. * Scan a section name, and pass the name to function sfunc().
  172. *
  173. * Input: InFile - Input source.
  174. * sfunc - Pointer to the function to be called if the section
  175. * name is successfully read.
  176. *
  177. * Output: True if the section name was read and True was returned from
  178. * <sfunc>. False if <sfunc> failed or if a lexical error was
  179. * encountered.
  180. *
  181. * ------------------------------------------------------------------------ **
  182. */
  183. {
  184. int c;
  185. int i;
  186. int end;
  187. const char *func = "params.c:Section() -";
  188. i = 0; /* <i> is the offset of the next free byte in bufr[] and */
  189. end = 0; /* <end> is the current "end of string" offset. In most */
  190. /* cases these will be the same, but if the last */
  191. /* character written to bufr[] is a space, then <end> */
  192. /* will be one less than <i>. */
  193. c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
  194. /* past initial white space. */
  195. while( (EOF != c) && (c > 0) )
  196. {
  197. /* Check that the buffer is big enough for the next character. */
  198. if( i > (bSize - 2) )
  199. {
  200. bSize += BUFR_INC;
  201. bufr = Realloc( bufr, bSize );
  202. if( NULL == bufr )
  203. {
  204. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  205. return( False );
  206. }
  207. }
  208. /* Handle a single character. */
  209. switch( c )
  210. {
  211. case ']': /* Found the closing bracket. */
  212. bufr[end] = '\0';
  213. if( 0 == end ) /* Don't allow an empty name. */
  214. {
  215. DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
  216. return( False );
  217. }
  218. if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */
  219. return( False );
  220. (void)EatComment( InFile ); /* Finish off the line. */
  221. return( True );
  222. case '\n': /* Got newline before closing ']'. */
  223. i = Continuation( bufr, i ); /* Check for line continuation. */
  224. if( i < 0 )
  225. {
  226. bufr[end] = '\0';
  227. DEBUG(0, ("%s Badly formed line in configuration file: %s\n",
  228. func, bufr ));
  229. return( False );
  230. }
  231. end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
  232. c = getc( InFile ); /* Continue with next line. */
  233. break;
  234. default: /* All else are a valid name chars. */
  235. if( isspace( c ) ) /* One space per whitespace region. */
  236. {
  237. bufr[end] = ' ';
  238. i = end + 1;
  239. c = EatWhitespace( InFile );
  240. }
  241. else /* All others copy verbatim. */
  242. {
  243. bufr[i++] = c;
  244. end = i;
  245. c = getc( InFile );
  246. }
  247. }
  248. }
  249. /* We arrive here if we've met the EOF before the closing bracket. */
  250. DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr ));
  251. return( False );
  252. } /* Section */
  253. static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(const char *, const char *), int c )
  254. /* ------------------------------------------------------------------------ **
  255. * Scan a parameter name and value, and pass these two fields to pfunc().
  256. *
  257. * Input: InFile - The input source.
  258. * pfunc - A pointer to the function that will be called to
  259. * process the parameter, once it has been scanned.
  260. * c - The first character of the parameter name, which
  261. * would have been read by Parse(). Unlike a comment
  262. * line or a section header, there is no lead-in
  263. * character that can be discarded.
  264. *
  265. * Output: True if the parameter name and value were scanned and processed
  266. * successfully, else False.
  267. *
  268. * Notes: This function is in two parts. The first loop scans the
  269. * parameter name. Internal whitespace is compressed, and an
  270. * equal sign (=) terminates the token. Leading and trailing
  271. * whitespace is discarded. The second loop scans the parameter
  272. * value. When both have been successfully identified, they are
  273. * passed to pfunc() for processing.
  274. *
  275. * ------------------------------------------------------------------------ **
  276. */
  277. {
  278. int i = 0; /* Position within bufr. */
  279. int end = 0; /* bufr[end] is current end-of-string. */
  280. int vstart = 0; /* Starting position of the parameter value. */
  281. const char *func = "params.c:Parameter() -";
  282. /* Read the parameter name. */
  283. while( 0 == vstart ) /* Loop until we've found the start of the value. */
  284. {
  285. if( i > (bSize - 2) ) /* Ensure there's space for next char. */
  286. {
  287. bSize += BUFR_INC;
  288. bufr = Realloc( bufr, bSize );
  289. if( NULL == bufr )
  290. {
  291. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  292. return( False );
  293. }
  294. }
  295. switch( c )
  296. {
  297. case '=': /* Equal sign marks end of param name. */
  298. if( 0 == end ) /* Don't allow an empty name. */
  299. {
  300. DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
  301. return( False );
  302. }
  303. bufr[end++] = '\0'; /* Mark end of string & advance. */
  304. i = end; /* New string starts here. */
  305. vstart = end; /* New string is parameter value. */
  306. bufr[i] = '\0'; /* New string is nul, for now. */
  307. break;
  308. case '\n': /* Find continuation char, else error. */
  309. i = Continuation( bufr, i );
  310. if( i < 0 )
  311. {
  312. bufr[end] = '\0';
  313. DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n",
  314. func, bufr ));
  315. return( True );
  316. }
  317. end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
  318. c = getc( InFile ); /* Read past eoln. */
  319. break;
  320. case '\0': /* Shouldn't have EOF within param name. */
  321. case EOF:
  322. bufr[i] = '\0';
  323. DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr ));
  324. return( True );
  325. default:
  326. if( isspace( c ) ) /* One ' ' per whitespace region. */
  327. {
  328. bufr[end] = ' ';
  329. i = end + 1;
  330. c = EatWhitespace( InFile );
  331. }
  332. else /* All others verbatim. */
  333. {
  334. bufr[i++] = c;
  335. end = i;
  336. c = getc( InFile );
  337. }
  338. }
  339. }
  340. /* Now parse the value. */
  341. c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
  342. while( (EOF !=c) && (c > 0) )
  343. {
  344. if( i > (bSize - 2) ) /* Make sure there's enough room. */
  345. {
  346. bSize += BUFR_INC;
  347. bufr = Realloc( bufr, bSize );
  348. if( NULL == bufr )
  349. {
  350. DEBUG(0, ("%s Memory re-allocation failure.", func) );
  351. return( False );
  352. }
  353. }
  354. switch( c )
  355. {
  356. case '\r': /* Explicitly remove '\r' because the older */
  357. c = getc( InFile ); /* version called fgets_slash() which also */
  358. break; /* removes them. */
  359. case '\n': /* Marks end of value unless there's a '\'. */
  360. i = Continuation( bufr, i );
  361. if( i < 0 )
  362. c = 0;
  363. else
  364. {
  365. for( end = i; (end >= 0) && isspace(bufr[end]); end-- )
  366. ;
  367. c = getc( InFile );
  368. }
  369. break;
  370. default: /* All others verbatim. Note that spaces do */
  371. bufr[i++] = c; /* not advance <end>. This allows trimming */
  372. if( !isspace( c ) ) /* of whitespace at the end of the line. */
  373. end = i;
  374. c = getc( InFile );
  375. break;
  376. }
  377. }
  378. bufr[end] = '\0'; /* End of value. */
  379. return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
  380. } /* Parameter */
  381. static BOOL Parse( FILE *InFile,
  382. BOOL (*sfunc)(const char *),
  383. BOOL (*pfunc)(const char *, const char *) )
  384. /* ------------------------------------------------------------------------ **
  385. * Scan & parse the input.
  386. *
  387. * Input: InFile - Input source.
  388. * sfunc - Function to be called when a section name is scanned.
  389. * See Section().
  390. * pfunc - Function to be called when a parameter is scanned.
  391. * See Parameter().
  392. *
  393. * Output: True if the file was successfully scanned, else False.
  394. *
  395. * Notes: The input can be viewed in terms of 'lines'. There are four
  396. * types of lines:
  397. * Blank - May contain whitespace, otherwise empty.
  398. * Comment - First non-whitespace character is a ';' or '#'.
  399. * The remainder of the line is ignored.
  400. * Section - First non-whitespace character is a '['.
  401. * Parameter - The default case.
  402. *
  403. * ------------------------------------------------------------------------ **
  404. */
  405. {
  406. int c;
  407. c = EatWhitespace( InFile );
  408. while( (EOF != c) && (c > 0) )
  409. {
  410. switch( c )
  411. {
  412. case '\n': /* Blank line. */
  413. c = EatWhitespace( InFile );
  414. break;
  415. case ';': /* Comment line. */
  416. case '#':
  417. c = EatComment( InFile );
  418. break;
  419. case '[': /* Section Header. */
  420. if( !Section( InFile, sfunc ) )
  421. return( False );
  422. c = EatWhitespace( InFile );
  423. break;
  424. case '\\': /* Bogus backslash. */
  425. c = EatWhitespace( InFile );
  426. break;
  427. default: /* Parameter line. */
  428. if( !Parameter( InFile, pfunc, c ) )
  429. return( False );
  430. c = EatWhitespace( InFile );
  431. break;
  432. }
  433. }
  434. return( True );
  435. } /* Parse */
  436. static FILE *OpenConfFile( const char *FileName )
  437. /* ------------------------------------------------------------------------ **
  438. * Open a configuration file.
  439. *
  440. * Input: FileName - The pathname of the config file to be opened.
  441. *
  442. * Output: A pointer of type (FILE *) to the opened file, or NULL if the
  443. * file could not be opened.
  444. *
  445. * ------------------------------------------------------------------------ **
  446. */
  447. {
  448. FILE *OpenedFile;
  449. const char *func = "params.c:OpenConfFile() -";
  450. extern BOOL in_client;
  451. int lvl = in_client?1:0;
  452. if( NULL == FileName || 0 == *FileName )
  453. {
  454. DEBUG( lvl, ("%s No configuration filename specified.\n", func) );
  455. return( NULL );
  456. }
  457. OpenedFile = sys_fopen( FileName, "r" );
  458. if( NULL == OpenedFile )
  459. {
  460. DEBUG( lvl,
  461. ("%s Unable to open configuration file \"%s\":\n\t%s\n",
  462. func, FileName, unix_error_string (errno)) );
  463. }
  464. return( OpenedFile );
  465. } /* OpenConfFile */
  466. BOOL pm_process( const char *FileName,
  467. BOOL (*sfunc)(const char *),
  468. BOOL (*pfunc)(const char *, const char *) )
  469. /* ------------------------------------------------------------------------ **
  470. * Process the named parameter file.
  471. *
  472. * Input: FileName - The pathname of the parameter file to be opened.
  473. * sfunc - A pointer to a function that will be called when
  474. * a section name is discovered.
  475. * pfunc - A pointer to a function that will be called when
  476. * a parameter name and value are discovered.
  477. *
  478. * Output: TRUE if the file was successfully parsed, else FALSE.
  479. *
  480. * ------------------------------------------------------------------------ **
  481. */
  482. {
  483. int result;
  484. FILE *InFile;
  485. const char *func = "params.c:pm_process() -";
  486. InFile = OpenConfFile( FileName ); /* Open the config file. */
  487. if( NULL == InFile )
  488. return( False );
  489. DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
  490. if( NULL != bufr ) /* If we already have a buffer */
  491. result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */
  492. /* use it. */
  493. else /* If we don't have a buffer */
  494. { /* allocate one, then parse, */
  495. bSize = BUFR_INC; /* then free. */
  496. bufr = (char *)malloc( bSize );
  497. if( NULL == bufr )
  498. {
  499. DEBUG(0,("%s memory allocation failure.\n", func));
  500. fclose(InFile);
  501. return( False );
  502. }
  503. result = Parse( InFile, sfunc, pfunc );
  504. free( bufr );
  505. bufr = NULL;
  506. bSize = 0;
  507. }
  508. fclose(InFile);
  509. if( !result ) /* Generic failure. */
  510. {
  511. DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func));
  512. return( False );
  513. }
  514. return( True ); /* Generic success. */
  515. } /* pm_process */
  516. /* -------------------------------------------------------------------------- */