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