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