]> git.sur5r.net Git - openldap/blob - contrib/whois++/config.c
SASL version check
[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
33 static struct {
34         char    *str;
35         int     cmd;
36         } commands[] = {
37 #define CMD_BASE        1
38                 "base",         CMD_BASE,
39 #define CMD_LDAP        2
40                 "ldaphost",     CMD_LDAP,
41 #define CMD_HELPDIR     3
42                 "helpdir",      CMD_HELPDIR,
43 #define CMD_USER        4
44                 "user",         CMD_USER,
45 #define CMD_PASSWORD    5
46                 "password",     CMD_PASSWORD,
47 #define CMD_CONFIGDIR   6
48                 "configdir",    CMD_CONFIGDIR,
49 #define CMD_CONTACT     7
50                 "contact",      CMD_CONTACT,
51 #define CMD_HOSTNAME    8
52                 "hostname",     CMD_HOSTNAME,
53 #define CMD_LANGUAGE    9
54                 "language",     CMD_LANGUAGE,
55 #define CMD_BANNER      10
56                 "banner",       CMD_BANNER,
57 #define CMD_TEMPLATE    11
58                 "template",     CMD_TEMPLATE,
59                 NULL,           NULL
60         };
61
62 static  nextLine(fp)
63 FILE    *fp;
64 {
65         /*
66          * We probably should check that the user hasn't put anything else
67          * on the line but I can't be bothered!
68          */
69         register int c;
70
71         while ((c = getc(fp)) != EOF && c != '\n')
72                 ;
73 }
74
75 /*
76  * Get next word, skipping blanks & comments.
77  */
78 static int      getWord(buffer, size, fp)
79 char            *buffer;
80 int             size;
81 FILE            *fp;
82 {
83         char    *cp;
84         int     c, string;
85
86         string = 0;
87         cp = buffer;
88         while ((c = getc(fp)) != EOF) {
89                 if (c == '#') {
90                         while ((c = getc(fp)) != EOF && c != '\n')
91                                 ;
92                         continue;
93                 }
94                 if (c == '\n') {
95                         if (cp != buffer)
96                                 ungetc(c, fp);
97                         break;
98                 } else if (c == '\\') {
99                         c = getc(fp);
100                         if (c == EOF)
101                                 c = '\n';
102                 } else if (c == '"') {
103                         string = !string;
104                         continue;
105                 }
106                 if (!string && isspace(c)) {
107                         while (isspace(c = getc(fp)) && c != '\n')
108                                 ;
109                         ungetc(c, fp);
110                         if (cp != buffer)       /* Trailing whitespace */
111                                 break;
112                         continue;               /* Leading whitespace */
113                 }
114                 if (cp >= buffer+size-1)
115                         break;
116                 *cp++ = c;
117         }
118         *cp = '\0';
119         return (cp != buffer);
120 }
121
122 void    readConfiguration( config )
123 FILE    *config;
124
125 {
126         char            buffer[BUFSIZ];
127         char            *s;
128         int             i;
129
130         /*
131          * A procedure to read in the configuration parameters.
132          * At the moment this is just a "quick hack" and it should be
133          * replaced in the next version by a proper scanner.
134          */
135
136         while ( getWord( buffer, BUFSIZ, config ) != NULL ) {
137                 for ( i = 0; commands[i].str != NULL; i++ )
138                         if ( EQ( buffer, commands[i].str ) )
139                                 break;
140                 if ( commands[i].str == NULL ) {
141                         printFormatted( lineLength, TRUE, stdout,
142                                 "Unrecognised command <%s>", buffer );
143                         exit( 1 );
144                 }
145                 if ( getWord( buffer, BUFSIZ, config ) == NULL ) {
146                         printFormatted( lineLength, TRUE, stdout,
147                                 "value missing in configuration file" );
148                         exit( 1 );
149                 }
150                 switch ( commands[i].cmd ) {
151                 case CMD_BASE:
152                         base = strdup( buffer );
153                         break;
154
155                 case CMD_LDAP:
156                         ldaphost = strdup( buffer );
157                         break;
158
159                 case CMD_HELPDIR:
160                         helpDir = strdup( buffer );
161                         break;
162
163                 case CMD_USER:
164                         user = strdup( buffer );
165                         break;
166
167                 case CMD_PASSWORD:
168                         password = strdup( buffer );
169                         break;
170
171                 case CMD_CONFIGDIR:
172                         configDir = strdup( buffer );
173                         break;
174
175                 case CMD_CONTACT:
176                         contact = strdup( buffer );
177                         break;
178
179                 case CMD_HOSTNAME:
180                         hostname = strdup( buffer );
181                         break;
182
183                 case CMD_LANGUAGE:
184                         defaultLanguage = lowerCase( strdup( buffer ) );
185                         break;
186
187                 case CMD_BANNER:
188                         banner = strdup( buffer );
189                         break;
190
191                 case CMD_TEMPLATE:
192                         if ( templateTranslationTable == NULL
193                                 && ( templateTranslationTable = (table *)malloc(sizeof(table)*tableSize) ) == NULL ) {
194                                 printFormatted( lineLength, TRUE, stdout,
195                                         "Malloc failed" );
196                                 exit( 1 );
197                         } else if ( numberOfTemplates+1 == tableSize ) {
198                                 tableSize += TABLE_INCREMENT;
199                                 if ( ( templateTranslationTable = (table *)realloc(templateTranslationTable, sizeof(table)*tableSize) ) == NULL ) {
200                                         printFormatted( lineLength, TRUE, stdout,
201                                                 "Realloc failed" );
202                                         exit( 1 );
203                                 }
204                         }
205                         templateTranslationTable[numberOfTemplates].key =
206                                 lowerCase( strdup( buffer ) );
207                         if ( getWord( buffer, BUFSIZ, config ) == NULL ) {
208                                 printFormatted( lineLength, TRUE, stdout,
209                                         "objectClass missing in config file" );
210                                 exit( 1 );
211                         }
212                         templateTranslationTable[numberOfTemplates].value =
213                                 lowerCase( strdup( buffer ) );
214                         numberOfTemplates++;
215                         break;
216
217                 default:
218                         printFormatted( lineLength, TRUE, stdout,
219                                 "Attribute <%s> not recognised.",
220                                 buffer );
221                         break;
222
223                 }
224                 nextLine( config );
225         }
226 }