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