Finished autoconf.
[lcr.git] / options.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "stdio.h"
13 #include "string.h"
14 #include "stdarg.h"
15 #include "unistd.h"
16 #include "stdlib.h"
17 #include "macro.h"
18 #include "extension.h"
19 #include "options.h"
20
21 struct options options = {
22         "/usr/local/lcr/log",           /* log file */
23         0x0000,                         /* debug mode */
24         'a',                            /* a-law */
25         "0",                            /* national prefix */
26         "00",                           /* international prefix */
27         "tones_american",               /* directory of tones */
28         "",                             /* directories of tones to fetch */
29         "",                             /* dummy caller id */
30         0,                              /* use tones by dsp.o */
31         0,                              /* by default use priority 0 */
32         "lcr@your.machine",             /* source mail adress */
33         "/var/tmp",                     /* path of lock files */
34         0700                            /* rights of lcr admin socket */
35 };
36
37 char options_error[256];
38
39 /* read options
40  *
41  * read options from options.conf
42  */
43 int read_options(void)
44 {
45         FILE *fp=NULL;
46         char filename[128];
47         char *p;
48         char option[32];
49         char param[256];
50         unsigned int line,i;
51         char buffer[256];
52
53         SPRINT(filename, "%s/options.conf", CONFIG_DATA);
54
55         if (!(fp=fopen(filename,"r")))
56         {
57                 SPRINT(options_error, "Cannot open %s\n",filename);
58                 return(0);
59         }
60
61         line=0;
62         while((fgets(buffer,sizeof(buffer),fp)))
63         {
64                 line++;
65                 buffer[sizeof(buffer)-1]=0;
66                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
67                 p=buffer;
68
69                 while(*p <= 32) /* skip spaces */
70                 {
71                         if (*p == 0)
72                                 break;
73                         p++;
74                 }
75                 if (*p==0 || *p=='#') /* ignore comments and empty line */
76                         continue;
77
78                 option[0]=0;
79                 i=0; /* read option */
80                 while(*p > 32)
81                 {
82                         if (i+1 >= sizeof(option))
83                         {
84                                 SPRINT(options_error, "Error in %s (line %d): option too long.\n",filename,line);
85                                 goto error;
86                         }
87                         option[i+1] = '\0';
88                         option[i++] = *p++;
89                 }
90
91                 while(*p <= 32) /* skip spaces */
92                 {
93                         if (*p == 0)
94                                 break;
95                         p++;
96                 }
97
98                 param[0]=0;
99                 if (*p!=0 && *p!='#') /* param */
100                 {
101                         i=0; /* read param */
102                         while(*p > 31)
103                         {
104                                 if (i+1 >= sizeof(param))
105                                 {
106                                         SPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
107                                         goto error;
108                                 }
109                                 param[i+1] = '\0';
110                                 param[i++] = *p++;
111                         }
112                 }
113
114                 /* at this point we have option and param */
115
116                 /* check option */
117                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if"))
118                 {
119                         SPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
120                         goto error;
121                 } else
122                 if (!strcmp(option,"debug"))
123                 {
124                         if (param[0]==0)
125                         {
126                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
127                                 goto error;
128                         }
129                         options.deb = strtol(param, NULL, 0);
130
131                 } else
132                 if (!strcmp(option,"log"))
133                 {
134                         if (param[0]==0)
135                         {
136                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
137                                 goto error;
138                         }
139                         SCPY(options.log, param);
140
141                 } else
142                 if (!strcmp(option,"alaw"))
143                 {
144                         options.law = 'a';
145
146                 } else
147                 if (!strcmp(option,"ulaw"))
148                 {
149                         options.law = 'u';
150
151                 } else
152                 if (!strcmp(option,"tones_dir"))
153                 {
154                         if (param[0]==0)
155                         {
156                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
157                                 goto error;
158                         }
159                         if (param[strlen(param)-1] == '/')
160                                 param[strlen(param)-1]=0;
161                         SCPY(options.tones_dir, param);
162
163                 } else
164                 if (!strcmp(option,"fetch_tones"))
165                 {
166                         if (param[0]==0)
167                         {
168                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
169                                 goto error;
170                         }
171                         if (param[strlen(param)-1] == '/')
172                                 param[strlen(param)-1]=0;
173                         SCPY(options.fetch_tones, param);
174
175                 } else
176                 if (!strcmp(option,"extensions_dir"))
177                 {
178                         // obsolete
179                 } else
180                 if (!strcmp(option,"national"))
181                 {
182                         SCPY(options.national, param);
183
184                 } else
185                 if (!strcmp(option,"international"))
186                 {
187                         SCPY(options.international, param);
188
189                 } else
190                 if (!strcmp(option,"dummyid"))
191                 {
192                         SCPY(options.dummyid, param);
193
194                 } else
195                 if (!strcmp(option,"dsptones"))
196                 {
197                         if (!strcasecmp(param, "american"))
198                                 options.dsptones = DSP_AMERICAN;
199                         else if (!strcasecmp(param, "german"))
200                                 options.dsptones = DSP_GERMAN;
201                         else if (!strcasecmp(param, "oldgerman"))
202                                 options.dsptones = DSP_OLDGERMAN;
203                         else if (!strcasecmp(param, "none"))
204                                 options.dsptones = DSP_NONE;
205                         else {
206                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
207                                 goto error;
208                         }
209
210                 } else
211                 if (!strcmp(option,"schedule"))
212                 {
213                         options.schedule = atoi(param);
214                         if (options.schedule < 0)
215                         {
216                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
217                                 goto error;
218                         }
219                         if (options.schedule > 99)
220                         {
221                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
222                                 goto error;
223                         }
224
225                 } else
226                 if (!strcmp(option,"email"))
227                 {
228                         if (param[0]==0)
229                         {
230                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
231                                 goto error;
232                         }
233                         SCPY(options.email, param);
234
235                 } else
236                 if (!strcmp(option,"lock"))
237                 {
238                         if (param[0]==0)
239                         {
240                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
241                                 goto error;
242                         }
243                         if (param[strlen(param)-1] == '/')
244                                 param[strlen(param)-1]=0;
245                         SCPY(options.lock, param);
246
247                 } else
248                 if (!strcmp(option,"socketrights"))
249                 {
250                         options.socketrights = strtol(param, NULL, 0);
251                 } else
252                 {
253                         SPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
254                         goto error;
255                 }
256         }
257
258 #if 0
259         if (!options.dsptones)
260         {
261                 SPRINT(options_error, "Error in %s (line %d): option 'dsptones' missing.\n", filename);
262                 goto error;
263         }
264 #endif
265         if (!options.tones_dir[0])
266         {
267                 SPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
268                 goto error;
269         }
270         if (fp) fclose(fp);
271         return(1);
272 error:
273         if (fp) fclose(fp);
274         return(0);
275 }
276
277