Added GSM network support.
[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         0                               /* enable gsm */
36 };
37
38 char options_error[256];
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", CONFIG_DATA);
55
56         if (!(fp=fopen(filename,"r")))
57         {
58                 SPRINT(options_error, "Cannot open %s\n",filename);
59                 return(0);
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                                 SPRINT(options_error, "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                                         SPRINT(options_error, "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                         SPRINT(options_error, "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                                 SPRINT(options_error, "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                 } else
133                 if (!strcmp(option,"log"))
134                 {
135                         if (param[0]==0)
136                         {
137                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
138                                 goto error;
139                         }
140                         SCPY(options.log, param);
141
142                 } else
143                 if (!strcmp(option,"alaw"))
144                 {
145                         options.law = 'a';
146
147                 } else
148                 if (!strcmp(option,"ulaw"))
149                 {
150                         options.law = 'u';
151
152                 } else
153                 if (!strcmp(option,"tones_dir"))
154                 {
155                         if (param[0]==0)
156                         {
157                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
158                                 goto error;
159                         }
160                         if (param[strlen(param)-1] == '/')
161                                 param[strlen(param)-1]=0;
162                         SCPY(options.tones_dir, param);
163
164                 } else
165                 if (!strcmp(option,"fetch_tones"))
166                 {
167                         if (param[0]==0)
168                         {
169                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
170                                 goto error;
171                         }
172                         if (param[strlen(param)-1] == '/')
173                                 param[strlen(param)-1]=0;
174                         SCPY(options.fetch_tones, param);
175
176                 } else
177                 if (!strcmp(option,"extensions_dir"))
178                 {
179                         // obsolete
180                 } else
181                 if (!strcmp(option,"national"))
182                 {
183                         SCPY(options.national, param);
184
185                 } else
186                 if (!strcmp(option,"international"))
187                 {
188                         SCPY(options.international, param);
189
190                 } else
191                 if (!strcmp(option,"dummyid"))
192                 {
193                         SCPY(options.dummyid, param);
194
195                 } else
196                 if (!strcmp(option,"dsptones"))
197                 {
198                         if (!strcasecmp(param, "american"))
199                                 options.dsptones = DSP_AMERICAN;
200                         else if (!strcasecmp(param, "german"))
201                                 options.dsptones = DSP_GERMAN;
202                         else if (!strcasecmp(param, "oldgerman"))
203                                 options.dsptones = DSP_OLDGERMAN;
204                         else if (!strcasecmp(param, "none"))
205                                 options.dsptones = DSP_NONE;
206                         else {
207                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
208                                 goto error;
209                         }
210
211                 } else
212                 if (!strcmp(option,"schedule"))
213                 {
214                         options.schedule = atoi(param);
215                         if (options.schedule < 0)
216                         {
217                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
218                                 goto error;
219                         }
220                         if (options.schedule > 99)
221                         {
222                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
223                                 goto error;
224                         }
225
226                 } else
227                 if (!strcmp(option,"email"))
228                 {
229                         if (param[0]==0)
230                         {
231                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
232                                 goto error;
233                         }
234                         SCPY(options.email, param);
235
236                 } else
237                 if (!strcmp(option,"lock"))
238                 {
239                         if (param[0]==0)
240                         {
241                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
242                                 goto error;
243                         }
244                         if (param[strlen(param)-1] == '/')
245                                 param[strlen(param)-1]=0;
246                         SCPY(options.lock, param);
247
248                 } else
249                 if (!strcmp(option,"socketrights"))
250                 {
251                         options.socketrights = strtol(param, NULL, 0);
252                 } else
253                 if (!strcmp(option,"gsm"))
254                 {
255                         options.gsm = 1;
256                 } else
257                 {
258                         SPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
259                         goto error;
260                 }
261         }
262
263 #if 0
264         if (!options.dsptones)
265         {
266                 SPRINT(options_error, "Error in %s (line %d): option 'dsptones' missing.\n", filename);
267                 goto error;
268         }
269 #endif
270         if (!options.tones_dir[0])
271         {
272                 SPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
273                 goto error;
274         }
275         if (fp) fclose(fp);
276         return(1);
277 error:
278         if (fp) fclose(fp);
279         return(0);
280 }
281
282