]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
Lose previously commented out old code.
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6 #ifdef HAVE_LOCALE_H
7 #include <locale.h>
8 #endif
9
10 #include <ac/string.h>
11 #include <ac/ctype.h>
12 #include <ac/socket.h>
13
14 #include "ldapconfig.h"
15 #include "slap.h"
16
17 #define MAXARGS 100
18
19 /*
20  * defaults for various global variables
21  */
22 int             defsize = SLAPD_DEFAULT_SIZELIMIT;
23 int             deftime = SLAPD_DEFAULT_TIMELIMIT;
24 struct acl      *global_acl = NULL;
25 int             global_default_access = ACL_READ;
26 char            *replogfile;
27 int             global_lastmod;
28 char            *ldap_srvtab = "";
29
30 char   *slapd_pid_file  = NULL;
31 char   *slapd_args_file = NULL;
32
33 static char     *fp_getline(FILE *fp, int *lineno);
34 static void     fp_getline_init(int *lineno);
35 static int      fp_parse_line(char *line, int *argcp, char **argv);
36
37 static char     *strtok_quote(char *line, char *sep);
38
39 int
40 read_config( char *fname )
41 {
42         FILE    *fp;
43         char    *line, *savefname;
44         int     cargc, savelineno;
45         char    *cargv[MAXARGS];
46         int     lineno, i;
47
48         static BackendInfo *bi = NULL;
49         static BackendDB        *be = NULL;
50
51         if ( (fp = fopen( fname, "r" )) == NULL ) {
52                 ldap_syslog = 1;
53                 Debug( LDAP_DEBUG_ANY,
54                     "could not open config file \"%s\" - absolute path?\n",
55                     fname, 0, 0 );
56                 perror( fname );
57                 return 1;
58         }
59
60         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
61
62         fp_getline_init( &lineno );
63
64         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
65                 /* skip comments and blank lines */
66                 if ( line[0] == '#' || line[0] == '\0' ) {
67                         continue;
68                 }
69
70                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
71
72                 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
73                         return( 1 );
74                 }
75
76                 if ( cargc < 1 ) {
77                         Debug( LDAP_DEBUG_ANY,
78                             "%s: line %d: bad config line (ignored)\n",
79                             fname, lineno, 0 );
80                         continue;
81                 }
82
83                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
84                         if ( cargc < 2 ) {
85                                 Debug( LDAP_DEBUG_ANY,
86                 "%s: line %d: missing type in \"backend <type>\" line\n",
87                                     fname, lineno, 0 );
88                                 return( 1 );
89                         }
90
91                         if( be != NULL ) {
92                                 Debug( LDAP_DEBUG_ANY,
93 "%s: line %d: backend line must appear before any database definition\n",
94                                     fname, lineno, 0 );
95                                 return( 1 );
96                         }
97
98                         bi = backend_info( cargv[1] );
99
100                 /* start of a new database definition */
101                 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
102                         if ( cargc < 2 ) {
103                                 Debug( LDAP_DEBUG_ANY,
104                 "%s: line %d: missing type in \"database <type>\" line\n",
105                                     fname, lineno, 0 );
106                                 return( 1 );
107                         }
108                         bi = NULL;
109                         be = backend_db_init( cargv[1] );
110
111                 /* assign a default depth limit for alias deref */
112                 be->be_maxDerefDepth = SLAPD_DEFAULT_MAXDEREFDEPTH; 
113
114                 /* get pid file name */
115                 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
116                         if ( cargc < 2 ) {
117                                 Debug( LDAP_DEBUG_ANY,
118             "%s: line %d: missing file name in \"pidfile <file>\" line\n",
119                                     fname, lineno, 0 );
120                                 return( 1 );
121                         }
122
123                         slapd_pid_file = ch_strdup( cargv[1] );
124
125                 /* get args file name */
126                 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
127                         if ( cargc < 2 ) {
128                                 Debug( LDAP_DEBUG_ANY,
129             "%s: line %d: missing file name in \"argsfile <file>\" line\n",
130                                     fname, lineno, 0 );
131                                 return( 1 );
132                         }
133
134                         slapd_args_file = ch_strdup( cargv[1] );
135
136                 /* set size limit */
137                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
138                         if ( cargc < 2 ) {
139                                 Debug( LDAP_DEBUG_ANY,
140             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
141                                     fname, lineno, 0 );
142                                 return( 1 );
143                         }
144                         if ( be == NULL ) {
145                                 defsize = atoi( cargv[1] );
146                         } else {
147                                 be->be_sizelimit = atoi( cargv[1] );
148                         }
149
150                 /* set time limit */
151                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
152                         if ( cargc < 2 ) {
153                                 Debug( LDAP_DEBUG_ANY,
154             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
155                                     fname, lineno, 0 );
156                                 return( 1 );
157                         }
158                         if ( be == NULL ) {
159                                 deftime = atoi( cargv[1] );
160                         } else {
161                                 be->be_timelimit = atoi( cargv[1] );
162                         }
163
164                 /* set database suffix */
165                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
166                         if ( cargc < 2 ) {
167                                 Debug( LDAP_DEBUG_ANY,
168                     "%s: line %d: missing dn in \"suffix <dn>\" line\n",
169                                     fname, lineno, 0 );
170                                 return( 1 );
171                         } else if ( cargc > 2 ) {
172                                 Debug( LDAP_DEBUG_ANY,
173     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
174                                     fname, lineno, cargv[1] );
175                         }
176                         if ( be == NULL ) {
177                                 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
179                                     fname, lineno, 0 );
180                         } else {
181                                 char *dn = ch_strdup( cargv[1] );
182                                 (void) dn_normalize( dn );
183                                 charray_add( &be->be_suffix, dn );
184                                 (void) dn_upcase( dn );
185                                 charray_add( &be->be_nsuffix, dn );
186                                 free( dn );
187                         }
188
189                 /* set database suffixAlias */
190                 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
191                         if ( cargc < 2 ) {
192                                 Debug( LDAP_DEBUG_ANY,
193                     "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
194                                     fname, lineno, 0 );
195                                 return( 1 );
196                         } else if ( cargc < 3 ) {
197                                 Debug( LDAP_DEBUG_ANY,
198                     "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
199                                     fname, lineno, 0 );
200                                 return( 1 );
201                         } else if ( cargc > 3 ) {
202                                 Debug( LDAP_DEBUG_ANY,
203     "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
204                                     fname, lineno, 0 );
205                         }
206                         if ( be == NULL ) {
207                                 Debug( LDAP_DEBUG_ANY,
208 "%s: line %d: suffixAlias line must appear inside a database definition (ignored)\n",
209                                     fname, lineno, 0 );
210                         } else {
211                                 char *alias, *aliased_dn;
212
213                                                                 alias = ch_strdup( cargv[1] );
214                                 (void) dn_normalize( alias );
215
216                                 aliased_dn = ch_strdup( cargv[2] );
217                                 (void) dn_normalize( aliased_dn );
218
219
220                                                                 if ( strcasecmp( alias, aliased_dn) == 0 ) {
221                                         Debug( LDAP_DEBUG_ANY,
222 "%s: line %d: suffixAlias %s is not different from aliased dn (ignored)\n",
223                                     fname, lineno, alias );
224                                                                 } else {
225                                         (void) dn_normalize_case( alias );
226                                         (void) dn_normalize_case( aliased_dn );
227                                         charray_add( &be->be_suffixAlias, alias );
228                                         charray_add( &be->be_suffixAlias, aliased_dn );
229                                                                 }
230
231                                                                 free(alias);
232                                                                 free(aliased_dn);
233                         }
234
235                /* set max deref depth */
236                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
237                        if ( cargc < 2 ) {
238                                Debug( LDAP_DEBUG_ANY,
239                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
240                                    fname, lineno, 0 );
241                                return( 1 );
242                        }
243                        if ( be == NULL ) {
244                                Debug( LDAP_DEBUG_ANY,
245 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
246                                    fname, lineno, 0 );
247                        } else {
248                            be->be_maxDerefDepth = atoi (cargv[1]);
249                        }
250
251
252                 /* set magic "root" dn for this database */
253                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
254                         if ( cargc < 2 ) {
255                                 Debug( LDAP_DEBUG_ANY,
256                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
257                                     fname, lineno, 0 );
258                                 return( 1 );
259                         }
260                         if ( be == NULL ) {
261                                 Debug( LDAP_DEBUG_ANY,
262 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
263                                     fname, lineno, 0 );
264                         } else {
265                                 be->be_root_dn = ch_strdup( cargv[1] );
266                                 be->be_root_ndn = dn_normalize_case( ch_strdup( cargv[1] ) );
267                         }
268
269                 /* set super-secret magic database password */
270                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
271                         if ( cargc < 2 ) {
272                                 Debug( LDAP_DEBUG_ANY,
273             "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
274                                     fname, lineno, 0 );
275                                 return( 1 );
276                         }
277                         if ( be == NULL ) {
278                                 Debug( LDAP_DEBUG_ANY,
279 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
280                                     fname, lineno, 0 );
281                         } else {
282                                 be->be_root_pw = ch_strdup( cargv[1] );
283                         }
284
285                 /* make this database read-only */
286                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
287                         if ( cargc < 2 ) {
288                                 Debug( LDAP_DEBUG_ANY,
289             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
290                                     fname, lineno, 0 );
291                                 return( 1 );
292                         }
293                         if ( be == NULL ) {
294                                 Debug( LDAP_DEBUG_ANY,
295 "%s: line %d: readonly line must appear inside a database definition (ignored)\n",
296                                     fname, lineno, 0 );
297                         } else {
298                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
299                                         be->be_readonly = 1;
300                                 } else {
301                                         be->be_readonly = 0;
302                                 }
303                         }
304
305                 /* where to send clients when we don't hold it */
306                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
307                         if ( cargc < 2 ) {
308                                 Debug( LDAP_DEBUG_ANY,
309                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
310                                     fname, lineno, 0 );
311                                 return( 1 );
312                         }
313                         default_referral = (char *) ch_malloc( strlen( cargv[1] )
314                             + sizeof("Referral:\n") + 1 );
315                         strcpy( default_referral, "Referral:\n" );
316                         strcat( default_referral, cargv[1] );
317
318                 /* specify locale */
319                 } else if ( strcasecmp( cargv[0], "locale" ) == 0 ) {
320 #ifdef HAVE_LOCALE_H
321                         char *locale;
322                         if ( cargc < 2 ) {
323                                 Debug( LDAP_DEBUG_ANY,
324         "%s: line %d: missing locale in \"locale <name | on | off>\" line\n",
325                                        fname, lineno, 0 );
326                                 return( 1 );
327                         }
328
329                         locale = (strcasecmp(   cargv[1], "on"  ) == 0 ? ""
330                                   : strcasecmp( cargv[1], "off" ) == 0 ? "C"
331                                   : ch_strdup( cargv[1] )                    );
332
333                         if ( setlocale( LC_CTYPE, locale ) == 0 ) {
334                                 Debug( LDAP_DEBUG_ANY,
335                                        (*locale
336                                         ? "%s: line %d: bad locale \"%s\"\n"
337                                         : "%s: line %d: bad locale\n"),
338                                        fname, lineno, locale );
339                                 return( 1 );
340                         }
341 #else
342                         Debug( LDAP_DEBUG_ANY,
343                                "%s: line %d: \"locale\" unsupported\n",
344                                fname, lineno, 0 );
345                         return( 1 );
346 #endif
347                 /* specify an objectclass */
348                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
349                         if ( *cargv[1] == '(' ) {
350                                 char * p;
351                                 p = strchr(line,'(');
352                                 parse_oc( fname, lineno, p );
353                         } else {
354                                 parse_oc_old( be, fname, lineno, cargc, cargv );
355                         }
356
357                 /* specify an attribute */
358                 } else if ( strcasecmp( cargv[0], "attribute" ) == 0 ) {
359                         if ( *cargv[1] == '(' ) {
360                                 char * p;
361                                 p = strchr(line,'(');
362                                 parse_at( fname, lineno, p );
363                         } else {
364                                 attr_syntax_config( fname, lineno, cargc - 1,
365                                     &cargv[1] );
366                         }
367
368                 /* turn on/off schema checking */
369                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
370                         if ( cargc < 2 ) {
371                                 Debug( LDAP_DEBUG_ANY,
372     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
373                                     fname, lineno, 0 );
374                                 return( 1 );
375                         }
376                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
377                                 global_schemacheck = 0;
378                         } else {
379                                 global_schemacheck = 1;
380                         }
381
382                 /* specify access control info */
383                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
384                         parse_acl( be, fname, lineno, cargc, cargv );
385
386                 /* specify default access control info */
387                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
388                         if ( cargc < 2 ) {
389                                 Debug( LDAP_DEBUG_ANY,
390             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
391                                     fname, lineno, 0 );
392                                 return( 1 );
393                         }
394                         if ( be == NULL ) {
395                                 if ( (global_default_access =
396                                     str2access( cargv[1] )) == -1 ) {
397                                         Debug( LDAP_DEBUG_ANY,
398 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
399                                             fname, lineno, cargv[1] );
400                                         return( 1 );
401                                 }
402                         } else {
403                                 if ( (be->be_dfltaccess =
404                                     str2access( cargv[1] )) == -1 ) {
405                                         Debug( LDAP_DEBUG_ANY,
406 "%s: line %d: bad access \"%s\" expecting [self]{none|compare|read|write}\n",
407                                             fname, lineno, cargv[1] );
408                                         return( 1 );
409                                 }
410                         }
411
412                 /* debug level to log things to syslog */
413                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
414                         if ( cargc < 2 ) {
415                                 Debug( LDAP_DEBUG_ANY,
416                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
417                                     fname, lineno, 0 );
418                                 return( 1 );
419                         }
420                         ldap_syslog = atoi( cargv[1] );
421
422                 /* list of replicas of the data in this backend (master only) */
423                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
424                         if ( cargc < 2 ) {
425                                 Debug( LDAP_DEBUG_ANY,
426             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
427                                     fname, lineno, 0 );
428                                 return( 1 );
429                         }
430                         if ( be == NULL ) {
431                                 Debug( LDAP_DEBUG_ANY,
432 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
433                                     fname, lineno, 0 );
434                         } else {
435                                 for ( i = 1; i < cargc; i++ ) {
436                                         if ( strncasecmp( cargv[i], "host=", 5 )
437                                             == 0 ) {
438                                                 charray_add( &be->be_replica,
439                                                              cargv[i] + 5 );
440                                                 break;
441                                         }
442                                 }
443                                 if ( i == cargc ) {
444                                         Debug( LDAP_DEBUG_ANY,
445                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
446                                             fname, lineno, 0 );
447                                 }
448                         }
449
450                 /* dn of master entity allowed to write to replica */
451                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
452                         if ( cargc < 2 ) {
453                                 Debug( LDAP_DEBUG_ANY,
454                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
455                                     fname, lineno, 0 );
456                                 return( 1 );
457                         }
458                         if ( be == NULL ) {
459                                 Debug( LDAP_DEBUG_ANY,
460 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
461                                     fname, lineno, 0 );
462                         } else {
463                                 be->be_update_ndn = ch_strdup( cargv[1] );
464                                 (void) dn_normalize_case( be->be_update_ndn );
465                         }
466
467                 /* replication log file to which changes are appended */
468                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
469                         if ( cargc < 2 ) {
470                                 Debug( LDAP_DEBUG_ANY,
471             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
472                                     fname, lineno, 0 );
473                                 return( 1 );
474                         }
475                         if ( be ) {
476                                 be->be_replogfile = ch_strdup( cargv[1] );
477                         } else {
478                                 replogfile = ch_strdup( cargv[1] );
479                         }
480
481                 /* maintain lastmodified{by,time} attributes */
482                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
483                         if ( cargc < 2 ) {
484                                 Debug( LDAP_DEBUG_ANY,
485             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
486                                     fname, lineno, 0 );
487                                 return( 1 );
488                         }
489                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
490                                 if ( be )
491                                         be->be_lastmod = ON;
492                                 else
493                                         global_lastmod = ON;
494                         } else {
495                                 if ( be )
496                                         be->be_lastmod = OFF;
497                                 else
498                                         global_lastmod = OFF;
499                         }
500
501                 /* include another config file */
502                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
503                         if ( cargc < 2 ) {
504                                 Debug( LDAP_DEBUG_ANY,
505     "%s: line %d: missing filename in \"include <filename>\" line\n",
506                                     fname, lineno, 0 );
507                                 return( 1 );
508                         }
509                         savefname = ch_strdup( cargv[1] );
510                         savelineno = lineno;
511
512                         if ( read_config( savefname ) != 0 ) {
513                                 return( 1 );
514                         }
515
516                         free( savefname );
517                         lineno = savelineno - 1;
518
519                 /* location of kerberos srvtab file */
520                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
521                         if ( cargc < 2 ) {
522                                 Debug( LDAP_DEBUG_ANY,
523             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
524                                     fname, lineno, 0 );
525                                 return( 1 );
526                         }
527                         ldap_srvtab = ch_strdup( cargv[1] );
528
529                 /* pass anything else to the current backend info/db config routine */
530                 } else {
531                         if ( bi != NULL ) {
532                                 if ( bi->bi_config == 0 ) {
533                                         Debug( LDAP_DEBUG_ANY,
534 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
535                                                 fname, lineno, cargv[0] );
536                                 } else {
537                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
538                                                 != 0 )
539                                         {
540                                                 return( 1 );
541                                         }
542                                 }
543                         } else if ( be != NULL ) {
544                                 if ( be->be_config == 0 ) {
545                                         Debug( LDAP_DEBUG_ANY,
546 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
547                                         fname, lineno, cargv[0] );
548                                 } else {
549                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
550                                                 != 0 )
551                                         {
552                                                 return( 1 );
553                                         }
554                                 }
555                         } else {
556                                 Debug( LDAP_DEBUG_ANY,
557 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
558                                     fname, lineno, cargv[0] );
559                         }
560                 }
561         }
562         fclose( fp );
563         return( 0 );
564 }
565
566 static int
567 fp_parse_line(
568     char        *line,
569     int         *argcp,
570     char        **argv
571 )
572 {
573         char *  token;
574
575         *argcp = 0;
576         for ( token = strtok_quote( line, " \t" ); token != NULL;
577             token = strtok_quote( NULL, " \t" ) ) {
578                 if ( *argcp == MAXARGS ) {
579                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
580                             MAXARGS, 0, 0 );
581                         return( 1 );
582                 }
583                 argv[(*argcp)++] = token;
584         }
585         argv[*argcp] = NULL;
586         return 0;
587 }
588
589 static char *
590 strtok_quote( char *line, char *sep )
591 {
592         int             inquote;
593         char            *tmp;
594         static char     *next;
595
596         if ( line != NULL ) {
597                 next = line;
598         }
599         while ( *next && strchr( sep, *next ) ) {
600                 next++;
601         }
602
603         if ( *next == '\0' ) {
604                 next = NULL;
605                 return( NULL );
606         }
607         tmp = next;
608
609         for ( inquote = 0; *next; ) {
610                 switch ( *next ) {
611                 case '"':
612                         if ( inquote ) {
613                                 inquote = 0;
614                         } else {
615                                 inquote = 1;
616                         }
617                         SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
618                         break;
619
620                 case '\\':
621                         if ( next[1] )
622                                 SAFEMEMCPY( next,
623                                             next + 1, strlen( next + 1 ) + 1 );
624                         next++;         /* dont parse the escaped character */
625                         break;
626
627                 default:
628                         if ( ! inquote ) {
629                                 if ( strchr( sep, *next ) != NULL ) {
630                                         *next++ = '\0';
631                                         return( tmp );
632                                 }
633                         }
634                         next++;
635                         break;
636                 }
637         }
638
639         return( tmp );
640 }
641
642 static char     buf[BUFSIZ];
643 static char     *line;
644 static int      lmax, lcur;
645
646 #define CATLINE( buf )  { \
647         int     len; \
648         len = strlen( buf ); \
649         while ( lcur + len + 1 > lmax ) { \
650                 lmax += BUFSIZ; \
651                 line = (char *) ch_realloc( line, lmax ); \
652         } \
653         strcpy( line + lcur, buf ); \
654         lcur += len; \
655 }
656
657 static char *
658 fp_getline( FILE *fp, int *lineno )
659 {
660         char            *p;
661
662         lcur = 0;
663         CATLINE( buf );
664         (*lineno)++;
665
666         /* hack attack - keeps us from having to keep a stack of bufs... */
667         if ( strncasecmp( line, "include", 7 ) == 0 ) {
668                 buf[0] = '\0';
669                 return( line );
670         }
671
672         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
673                 if ( (p = strchr( buf, '\n' )) != NULL ) {
674                         *p = '\0';
675                 }
676                 if ( ! isspace( (unsigned char) buf[0] ) ) {
677                         return( line );
678                 }
679
680                 CATLINE( buf );
681                 (*lineno)++;
682         }
683         buf[0] = '\0';
684
685         return( line[0] ? line : NULL );
686 }
687
688 static void
689 fp_getline_init( int *lineno )
690 {
691         *lineno = -1;
692         buf[0] = '\0';
693 }