Adding TX-dejitter feature for briged data to mISDN
[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 #include <grp.h>
21 #include <pwd.h>
22
23 struct options options = {
24         "/usr/local/lcr/log",           /* log file */
25         0x0000,                         /* debug mode */
26         'a',                            /* a-law */
27         "0",                            /* national prefix */
28         "00",                           /* international prefix */
29         "tones_american",               /* directory of tones */
30         "",                             /* directories of tones to fetch */
31         "",                             /* dummy caller id */
32         0,                              /* by default use priority 0 */
33         "lcr@your.machine",             /* source mail adress */
34         "/var/tmp",                     /* path of lock files */
35         0700,                           /* rights of lcr admin socket */
36         -1,                             /* socket user (-1= no change) */
37         -1,                             /* socket group (-1= no change) */
38         1,                              /* use polling of main loop */
39         "mISDN_l1loop.1",               /* GSM/Asterisk side */
40         "mISDN_l1loop.2",               /* LCR side */
41 };
42
43 char options_error[256];
44
45 /* read options
46  *
47  * read options from options.conf
48  */
49 int read_options(char *options_error)
50 {
51         FILE *fp=NULL;
52         char filename[128];
53         char *p;
54         char option[32];
55         char param[256];
56         unsigned int line,i;
57         char buffer[256];
58
59         SPRINT(filename, "%s/options.conf", CONFIG_DATA);
60
61         if (!(fp=fopen(filename,"r"))) {
62                 UPRINT(options_error, "Cannot open %s\n",filename);
63                 return(0);
64         }
65
66         line=0;
67         while((fgets(buffer,sizeof(buffer),fp))) {
68                 line++;
69                 buffer[sizeof(buffer)-1]=0;
70                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
71                 p=buffer;
72
73                 while(*p <= 32) { /* skip spaces */
74                         if (*p == 0)
75                                 break;
76                         p++;
77                 }
78                 if (*p==0 || *p=='#') /* ignore comments and empty line */
79                         continue;
80
81                 option[0]=0;
82                 i=0; /* read option */
83                 while(*p > 32) {
84                         if (i+1 >= sizeof(option)) {
85                                 UPRINT(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                         if (*p == 0)
94                                 break;
95                         p++;
96                 }
97
98                 param[0]=0;
99                 if (*p!=0 && *p!='#') { /* param */
100                         i=0; /* read param */
101                         while(*p > 31) {
102                                 if (i+1 >= sizeof(param)) {
103                                         UPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
104                                         goto error;
105                                 }
106                                 param[i+1] = '\0';
107                                 param[i++] = *p++;
108                         }
109                 }
110
111                 /* at this point we have option and param */
112
113                 /* check option */
114                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if")) {
115                         UPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
116                         goto error;
117                 } else
118                 if (!strcmp(option,"debug")) {
119                         if (param[0]==0) {
120                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
121                                 goto error;
122                         }
123                         options.deb = strtol(param, NULL, 0);
124
125                 } else
126                 if (!strcmp(option,"log")) {
127                         if (param[0]==0) {
128                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
129                                 goto error;
130                         }
131                         SCPY(options.log, param);
132
133                 } else
134                 if (!strcmp(option,"alaw")) {
135                         options.law = 'a';
136
137                 } else
138                 if (!strcmp(option,"ulaw")) {
139                         options.law = 'u';
140
141                 } else
142                 if (!strcmp(option,"tones_dir")) {
143                         if (param[0]==0) {
144                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
145                                 goto error;
146                         }
147                         if (param[strlen(param)-1] == '/')
148                                 param[strlen(param)-1]=0;
149                         SCPY(options.tones_dir, param);
150
151                 } else
152                 if (!strcmp(option,"fetch_tones")) {
153                         if (param[0]==0) {
154                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
155                                 goto error;
156                         }
157                         if (param[strlen(param)-1] == '/')
158                                 param[strlen(param)-1]=0;
159                         SCPY(options.fetch_tones, param);
160
161                 } else
162                 if (!strcmp(option,"extensions_dir")) {
163                         // obsolete
164                 } else
165                 if (!strcmp(option,"national")) {
166                         SCPY(options.national, param);
167
168                 } else
169                 if (!strcmp(option,"international")) {
170                         SCPY(options.international, param);
171
172                 } else
173                 if (!strcmp(option,"dummyid")) {
174                         SCPY(options.dummyid, param);
175
176                 } else
177                 if (!strcmp(option,"dsptones")) {
178                         UPRINT(options_error, "Error in %s (line %d): parameter 'dsptones' is obsolete. Just define the tones (american,german,oldgerman) at 'tones_dir' option.\n",filename,line);
179                         goto error;
180                 } else
181                 if (!strcmp(option,"schedule")) {
182                         options.schedule = atoi(param);
183                         if (options.schedule < 0) {
184                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
185                                 goto error;
186                         }
187                         if (options.schedule > 99) {
188                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
189                                 goto error;
190                         }
191
192                 } else
193                 if (!strcmp(option,"email")) {
194                         if (param[0]==0) {
195                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
196                                 goto error;
197                         }
198                         SCPY(options.email, param);
199
200                 } else
201                 if (!strcmp(option,"lock")) {
202                         if (param[0]==0) {
203                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
204                                 goto error;
205                         }
206                         if (param[strlen(param)-1] == '/')
207                                 param[strlen(param)-1]=0;
208                         SCPY(options.lock, param);
209
210                 } else
211                 if (!strcmp(option,"socketuser")) {
212                         char * endptr = NULL;
213                         options.socketuser = strtol(param, &endptr, 10);
214                         if (*endptr != '\0') {
215                                 struct passwd * pwd = getpwnam(param);
216                                 if (pwd == NULL) {
217                                         UPRINT(options_error, "Error in %s (line %d): no such user: %s.\n",filename,line,param);
218                                         goto error;
219                                 }
220                                 options.socketuser = pwd->pw_uid;
221                         }
222                 } else
223                 if (!strcmp(option,"socketgroup")) {
224                         char * endptr = NULL;
225                         options.socketgroup = strtol(param, &endptr, 10);
226                         if (*endptr != '\0') {
227                                 struct group * grp = getgrnam(param);
228                                 if (grp == NULL) {
229                                         UPRINT(options_error, "Error in %s (line %d): no such group: %s.\n",filename,line,param);
230                                         goto error;
231                                 }
232                                 options.socketgroup = grp->gr_gid;
233                         }
234                 } else
235                 if (!strcmp(option,"socketrights")) {
236                         options.socketrights = strtol(param, NULL, 0);
237                 } else
238                 if (!strcmp(option,"polling")) {
239                         options.polling = 1;
240                 } else
241                 if (!strcmp(option,"loopback-ext")) {
242                         if (param[0]==0) {
243                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
244                                 goto error;
245                         }
246                         SCPY(options.loopback_ext, param);
247
248                 } else
249                 if (!strcmp(option,"loopback-lcr")) {
250                         if (param[0]==0) {
251                                 UPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
252                                 goto error;
253                         }
254                         SCPY(options.loopback_lcr, param);
255
256                 } else {
257                         UPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
258                         goto error;
259                 }
260         }
261
262         if (!options.tones_dir[0]) {
263                 UPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename,line);
264                 goto error;
265         }
266         if (fp) fclose(fp);
267         return(1);
268 error:
269         if (fp) fclose(fp);
270         return(0);
271 }
272
273