]> git.sur5r.net Git - openldap/blob - contrib/whois++/config.c
Update lutil_lockf (aka: ldap_lockf) to hide implementation in
[openldap] / contrib / whois++ / config.c
1 #if !defined(lint)
2 static char copyright[] = "Copyright 1992 The University of Adelaide";
3 #endif
4
5 /*
6  *                      C O N F I G
7  *
8  * Author:      Mark R. Prior
9  *              Communications and Systems Branch
10  *              Information Technology Division
11  *              The University of Adelaide
12  * E-mail:      mrp@itd.adelaide.edu.au
13  * Date:        October 1992
14  * Version:     1.7
15  * Description:
16  *              Process the configuration file.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that the above copyright notice and this paragraph are
20  * duplicated in all such forms and that any documentation,
21  * advertising materials, and other materials related to such
22  * distribution and use acknowledge that the software was developed
23  * by the University of Adelaide. The name of the University may not
24  * be used to endorse or promote products derived from this software
25  * without specific prior written permission.
26  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
28  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30
31 #include "whois++.h"
32 #include <stdlib.h>
33
34 static struct {
35         char    *str;
36         int     cmd;
37         } commands[] = {
38 #define CMD_BASE        1
39                 "base",         CMD_BASE,
40 #define CMD_LDAP        2
41                 "ldaphost",     CMD_LDAP,
42 #define CMD_HELPDIR     3
43                 "helpdir",      CMD_HELPDIR,
44 #define CMD_USER        4
45                 "user",         CMD_USER,
46 #define CMD_PASSWORD    5
47                 "password",     CMD_PASSWORD,
48 #define CMD_CONFIGDIR   6
49                 "configdir",    CMD_CONFIGDIR,
50 #define CMD_CONTACT     7
51                 "contact",      CMD_CONTACT,
52 #define CMD_HOSTNAME    8
53                 "hostname",     CMD_HOSTNAME,
54 #define CMD_LANGUAGE    9
55                 "language",     CMD_LANGUAGE,
56 #define CMD_BANNER      10
57                 "banner",       CMD_BANNER,
58 #define CMD_TEMPLATE    11
59                 "template",     CMD_TEMPLATE,
60                 NULL,           NULL
61         };
62
63 static void
64 nextLine( FILE *fp )
65 {
66         /*
67          * We probably should check that the user hasn't put anything else
68          * on the line but I can't be bothered!
69          */
70         register int c;
71
72         while ((c = getc(fp)) != EOF && c != '\n')
73                 ;
74 }
75
76 /*
77  * Get next word, skipping blanks & comments.
78  */
79 static int
80 getWord( char *buffer, int size, FILE *fp )
81 {
82         char    *cp;
83         int     c, string;
84
85         string = 0;
86         cp = buffer;
87         while ((c = getc(fp)) != EOF) {
88                 if (c == '#') {
89                         while ((c = getc(fp)) != EOF && c != '\n')
90                                 ;
91                         continue;
92                 }
93                 if (c == '\n') {
94                         if (cp != buffer)
95                                 ungetc(c, fp);
96                         break;
97                 } else if (c == '\\') {
98                         c = getc(fp);
99                         if (c == EOF)
100                                 c = '\n';
101                 } else if (c == '"') {
102                         string = !string;
103                         continue;
104                 }
105                 if (!string && isspace(c)) {
106                         while (isspace(c = getc(fp)) && c != '\n')
107                                 ;
108                         ungetc(c, fp);
109                         if (cp != buffer)       /* Trailing whitespace */
110                                 break;
111                         continue;               /* Leading whitespace */
112                 }
113                 if (cp >= buffer+size-1)
114                         break;
115                 *cp++ = c;
116         }
117         *cp = '\0';
118         return (cp != buffer);
119 }
120
121 void
122 readConfiguration( FILE *config )
123 {
124         char            buffer[BUFSIZ];
125         char            *s;
126         int             i;
127
128         /*
129          * A procedure to read in the configuration parameters.
130          * At the moment this is just a "quick hack" and it should be
131          * replaced in the next version by a proper scanner.
132          */
133
134         while ( getWord( buffer, BUFSIZ, config ) != NULL ) {
135                 for ( i = 0; commands[i].str != NULL; i++ )
136                         if ( EQ( buffer, commands[i].str ) )
137                                 break;
138                 if ( commands[i].str == NULL ) {
139                         printFormatted( lineLength, TRUE, stdout,
140                                 "Unrecognised command <%s>", buffer );
141                         exit( 1 );
142                 }
143                 if ( getWord( buffer, BUFSIZ, config ) == NULL ) {
144                         printFormatted( lineLength, TRUE, stdout,
145                                 "value missing in configuration file" );
146                         exit( 1 );
147                 }
148                 switch ( commands[i].cmd ) {
149                 case CMD_BASE:
150                         base = strdup( buffer );
151                         break;
152
153                 case CMD_LDAP:
154                         ldaphost = strdup( buffer );
155                         break;
156
157                 case CMD_HELPDIR:
158                         helpDir = strdup( buffer );
159                         break;
160
161                 case CMD_USER:
162                         user = strdup( buffer );
163                         break;
164
165                 case CMD_PASSWORD:
166                         password = strdup( buffer );
167                         break;
168
169                 case CMD_CONFIGDIR:
170                         configDir = strdup( buffer );
171                         break;
172
173                 case CMD_CONTACT:
174                         contact = strdup( buffer );
175                         break;
176
177                 case CMD_HOSTNAME:
178                         hostname = strdup( buffer );
179                         break;
180
181                 case CMD_LANGUAGE:
182                         defaultLanguage = lowerCase( strdup( buffer ) );
183                         break;
184
185                 case CMD_BANNER:
186                         banner = strdup( buffer );
187                         break;
188
189                 case CMD_TEMPLATE:
190                         if ( templateTranslationTable == NULL
191                                 && ( templateTranslationTable = (table *)malloc(sizeof(table)*tableSize) ) == NULL ) {
192                                 printFormatted( lineLength, TRUE, stdout,
193                                         "Malloc failed" );
194                                 exit( 1 );
195                         } else if ( numberOfTemplates+1 == tableSize ) {
196                                 tableSize += TABLE_INCREMENT;
197                                 if ( ( templateTranslationTable = (table *)realloc(templateTranslationTable, sizeof(table)*tableSize) ) == NULL ) {
198                                         printFormatted( lineLength, TRUE, stdout,
199                                                 "Realloc failed" );
200                                         exit( 1 );
201                                 }
202                         }
203                         templateTranslationTable[numberOfTemplates].key =
204                                 lowerCase( strdup( buffer ) );
205                         if ( getWord( buffer, BUFSIZ, config ) == NULL ) {
206                                 printFormatted( lineLength, TRUE, stdout,
207                                         "objectClass missing in config file" );
208                                 exit( 1 );
209                         }
210                         templateTranslationTable[numberOfTemplates].value =
211                                 lowerCase( strdup( buffer ) );
212                         numberOfTemplates++;
213                         break;
214
215                 default:
216                         printFormatted( lineLength, TRUE, stdout,
217                                 "Attribute <%s> not recognised.",
218                                 buffer );
219                         break;
220
221                 }
222                 nextLine( config );
223         }
224 }