backup
[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 <stdlib.h>
15 #include "main.h"
16
17 struct options options = {
18         "/usr/local/pbx/log",           /* log file */
19         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
21          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
22          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
24          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
25          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
26          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
27         0x0000,                         /* debug mode */
28         'a',                            /* a-law */
29         "0",                            /* national prefix */
30         "00",                           /* international prefix */
31         "tones_american",               /* directory of tones */
32         "",                             /* directories of tones to fetch */
33         "extensions",                   /* directory of extensions */
34         0,                              /* dtmf detection on */
35         "",                             /* dummy caller id */
36         0,                              /* use tones by dsp.o */
37         0,                              /* by default use priority 0 */
38         "lcr@your.machine"              /* source mail adress */
39 };
40
41 /* read options
42  *
43  * read options from options.conf
44  */
45 int read_options(void)
46 {
47         FILE *fp=NULL;
48         char filename[128];
49         char *p;
50         char option[32];
51         char param[256];
52         unsigned int line,i;
53         char buffer[256];
54
55         SPRINT(filename, "%s/options.conf", INSTALL_DATA);
56
57         if (!(fp=fopen(filename,"r")))
58         {
59                 PERROR("Cannot open %s\n",filename);
60                 return(-1);
61         }
62
63         line=0;
64         while((fgets(buffer,sizeof(buffer),fp)))
65         {
66                 line++;
67                 buffer[sizeof(buffer)-1]=0;
68                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
69                 p=buffer;
70
71                 while(*p <= 32) /* skip spaces */
72                 {
73                         if (*p == 0)
74                                 break;
75                         p++;
76                 }
77                 if (*p==0 || *p=='#') /* ignore comments and empty line */
78                         continue;
79
80                 option[0]=0;
81                 i=0; /* read option */
82                 while(*p > 32)
83                 {
84                         if (i+1 >= sizeof(option))
85                         {
86                                 PERROR_RUNTIME("Error in %s (line %d): option too long.\n",filename,line);
87                                 goto error;
88                         }
89                         option[i+1] = '\0';
90                         option[i++] = *p++;
91                 }
92
93                 while(*p <= 32) /* skip spaces */
94                 {
95                         if (*p == 0)
96                                 break;
97                         p++;
98                 }
99
100                 param[0]=0;
101                 if (*p!=0 && *p!='#') /* param */
102                 {
103                         i=0; /* read param */
104                         while(*p > 31)
105                         {
106                                 if (i+1 >= sizeof(param))
107                                 {
108                                         PERROR_RUNTIME("Error in %s (line %d): param too long.\n",filename,line);
109                                         goto error;
110                                 }
111                                 param[i+1] = '\0';
112                                 param[i++] = *p++;
113                         }
114                 }
115
116                 /* at this point we have option and param */
117
118                 /* check option */
119                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if"))
120                 {
121                         PERROR_RUNTIME("Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
122                         goto error;
123                 } else
124                 if (!strcmp(option,"debug"))
125                 {
126                         if (param[0]==0)
127                         {
128                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
129                                 goto error;
130                         }
131                         options.deb = strtol(param, NULL, 0);
132
133                         PDEBUG(DEBUG_CONFIG, "debugging: 0x%x\n", options.deb);
134                 } else
135                 if (!strcmp(option,"log"))
136                 {
137                         if (param[0]==0)
138                         {
139                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
140                                 goto error;
141                         }
142                         SCPY(options.log, param);
143
144                         PDEBUG(DEBUG_CONFIG, "log file: %s\n", options.log);
145                 } else
146                 if (!strcmp(option,"port"))
147                 {
148                         i = strtol(param, NULL, 0);
149                         if (i < 1 || i > sizeof(options.ports))
150                         {
151                                 PERROR_RUNTIME("Error in %s (line %d): port number %s out of range.\n", filename, line, option);
152                                 goto error;
153                         }
154                         options.ports[i] |= FLAG_PORT_USE;
155
156                         PDEBUG(DEBUG_CONFIG, "adding interface: %d (param=%s)\n", i, param);
157                         if (strstr(param, "ptp"))
158                         {
159                                 options.ports[i] |= FLAG_PORT_PTP;
160                                 PDEBUG(DEBUG_CONFIG, " -> interface shall be ptp\n");
161                         }
162                 } else
163 #if 0
164                 if (!strcmp(option,"ptp"))
165                 {
166                         options.ptp = 1;
167
168                         PDEBUG(DEBUG_CONFIG, "ptp layer-2 watch and keep established.\n");
169                 } else
170 #endif
171                 if (!strcmp(option,"alaw"))
172                 {
173                         options.law = 'a';
174
175                         PDEBUG(DEBUG_CONFIG, "isdn audio type: alaw\n");
176                 } else
177                 if (!strcmp(option,"ulaw"))
178                 {
179                         options.law = 'u';
180
181                         PDEBUG(DEBUG_CONFIG, "isdn audio type: ulaw\n");
182                 } else
183                 if (!strcmp(option,"tones_dir"))
184                 {
185                         if (param[0]==0)
186                         {
187                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
188                                 goto error;
189                         }
190                         if (param[strlen(param)-1] == '/')
191                                 param[strlen(param)-1]=0;
192                         SCPY(options.tones_dir, param);
193
194                         PDEBUG(DEBUG_CONFIG, "directory of tones: %s\n",param);
195                 } else
196                 if (!strcmp(option,"fetch_tones"))
197                 {
198                         if (param[0]==0)
199                         {
200                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
201                                 goto error;
202                         }
203                         if (param[strlen(param)-1] == '/')
204                                 param[strlen(param)-1]=0;
205                         SCPY(options.fetch_tones, param);
206
207                         PDEBUG(DEBUG_CONFIG, "directories of tones to fetch: %s\n",param);
208                 } else
209                 if (!strcmp(option,"extensions_dir"))
210                 {
211                         if (param[0]==0)
212                         {
213                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
214                                 goto error;
215                         }
216                         if (param[strlen(param)-1] == '/')
217                                 param[strlen(param)-1]=0;
218                         SCPY(options.extensions_dir, param);
219
220                         PDEBUG(DEBUG_CONFIG, "directory of extensions: %s\n",param);
221                 } else
222                 if (!strcmp(option,"national"))
223                 {
224                         SCPY(options.national, param);
225
226                         PDEBUG(DEBUG_CONFIG, "national dial prefix: %s\n", param);
227                 } else
228                 if (!strcmp(option,"international"))
229                 {
230                         SCPY(options.international, param);
231
232                         PDEBUG(DEBUG_CONFIG, "inernational dial prefix: %s\n", param);
233                 } else
234                 if (!strcmp(option,"nodtmf"))
235                 {
236                         options.nodtmf = 1;
237
238                         PDEBUG(DEBUG_CONFIG, "disable dtmf detection\n");
239                 } else
240                 if (!strcmp(option,"dummyid"))
241                 {
242                         SCPY(options.dummyid, param);
243
244                         PDEBUG(DEBUG_CONFIG, "dummy caller id\n", param);
245                 } else
246                 if (!strcmp(option,"dsptones"))
247                 {
248                         if (!strcasecmp(param, "american"))
249                                 options.dsptones = DSP_AMERICAN;
250                         else if (!strcasecmp(param, "german"))
251                                 options.dsptones = DSP_GERMAN;
252                         else if (!strcasecmp(param, "oldgerman"))
253                                 options.dsptones = DSP_OLDGERMAN;
254                         else if (!strcasecmp(param, "none"))
255                                 options.dsptones = DSP_NONE;
256                         else {
257                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
258                                 goto error;
259                         }
260
261                         PDEBUG(DEBUG_CONFIG, "dsp tones = %d\n", options.dsptones);
262                 } else
263                 if (!strcmp(option,"schedule"))
264                 {
265                         options.schedule = atoi(param);
266                         if (options.schedule < 0)
267                         {
268                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
269                                 goto error;
270                         }
271                         if (options.schedule > 99)
272                         {
273                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
274                                 goto error;
275                         }
276
277                         if (atoi(param))
278                                 PDEBUG(DEBUG_CONFIG, "use real time scheduler priority: %d\n", atoi(param));
279                         else
280                                 PDEBUG(DEBUG_CONFIG, "don't use real time scheduler\n");
281                 } else
282                 if (!strcmp(option,"email"))
283                 {
284                         if (param[0]==0)
285                         {
286                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
287                                 goto error;
288                         }
289                         SCPY(options.email, param);
290
291                         PDEBUG(DEBUG_CONFIG, "source mail address of pbx: %s\n", param);
292                 } else
293                 {
294                         PERROR_RUNTIME("Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
295                         goto error;
296                 }
297         }
298
299 #if 0
300         if (!options.dsptones)
301         {
302                 PERROR_RUNTIME("Error in %s (line %d): option 'dsptones' missing.\n", filename);
303                 goto error;
304         }
305 #endif
306         if (!options.tones_dir[0])
307         {
308                 PERROR_RUNTIME("Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
309                 goto error;
310         }
311         if (fp) fclose(fp);
312         return(1);
313 error:
314         if (fp) fclose(fp);
315         return(0);
316 }
317
318