params.c 19 KB

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