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