Restructured tones_dir, added tones_dir to interface.conf
[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,                              /* by default use priority 0 */
31         "lcr@your.machine",             /* source mail adress */
32         "/var/tmp",                     /* path of lock files */
33         0700,                           /* rights of lcr admin socket */
34         0                               /* enable gsm */
35 };
36
37 char options_error[256];
38
39 /* read options
40  *
41  * read options from options.conf
42  */
43 int read_options(void)
44 {
45         FILE *fp=NULL;
46         char filename[128];
47         char *p;
48         char option[32];
49         char param[256];
50         unsigned int line,i;
51         char buffer[256];
52
53         SPRINT(filename, "%s/options.conf", CONFIG_DATA);
54
55         if (!(fp=fopen(filename,"r"))) {
56                 SPRINT(options_error, "Cannot open %s\n",filename);
57                 return(0);
58         }
59
60         line=0;
61         while((fgets(buffer,sizeof(buffer),fp))) {
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                         if (*p == 0)
69                                 break;
70                         p++;
71                 }
72                 if (*p==0 || *p=='#') /* ignore comments and empty line */
73                         continue;
74
75                 option[0]=0;
76                 i=0; /* read option */
77                 while(*p > 32) {
78                         if (i+1 >= sizeof(option)) {
79                                 SPRINT(options_error, "Error in %s (line %d): option too long.\n",filename,line);
80                                 goto error;
81                         }
82                         option[i+1] = '\0';
83                         option[i++] = *p++;
84                 }
85
86                 while(*p <= 32) { /* skip spaces */
87                         if (*p == 0)
88                                 break;
89                         p++;
90                 }
91
92                 param[0]=0;
93                 if (*p!=0 && *p!='#') { /* param */
94                         i=0; /* read param */
95                         while(*p > 31) {
96                                 if (i+1 >= sizeof(param)) {
97                                         SPRINT(options_error, "Error in %s (line %d): param too long.\n",filename,line);
98                                         goto error;
99                                 }
100                                 param[i+1] = '\0';
101                                 param[i++] = *p++;
102                         }
103                 }
104
105                 /* at this point we have option and param */
106
107                 /* check option */
108                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if")) {
109                         SPRINT(options_error, "Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
110                         goto error;
111                 } else
112                 if (!strcmp(option,"debug")) {
113                         if (param[0]==0) {
114                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
115                                 goto error;
116                         }
117                         options.deb = strtol(param, NULL, 0);
118
119                 } else
120                 if (!strcmp(option,"log")) {
121                         if (param[0]==0) {
122                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
123                                 goto error;
124                         }
125                         SCPY(options.log, param);
126
127                 } else
128                 if (!strcmp(option,"alaw")) {
129                         options.law = 'a';
130
131                 } else
132                 if (!strcmp(option,"ulaw")) {
133                         options.law = 'u';
134
135                 } else
136                 if (!strcmp(option,"tones_dir")) {
137                         if (param[0]==0) {
138                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
139                                 goto error;
140                         }
141                         if (param[strlen(param)-1] == '/')
142                                 param[strlen(param)-1]=0;
143                         SCPY(options.tones_dir, param);
144
145                 } else
146                 if (!strcmp(option,"fetch_tones")) {
147                         if (param[0]==0) {
148                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
149                                 goto error;
150                         }
151                         if (param[strlen(param)-1] == '/')
152                                 param[strlen(param)-1]=0;
153                         SCPY(options.fetch_tones, param);
154
155                 } else
156                 if (!strcmp(option,"extensions_dir")) {
157                         // obsolete
158                 } else
159                 if (!strcmp(option,"national")) {
160                         SCPY(options.national, param);
161
162                 } else
163                 if (!strcmp(option,"international")) {
164                         SCPY(options.international, param);
165
166                 } else
167                 if (!strcmp(option,"dummyid")) {
168                         SCPY(options.dummyid, param);
169
170                 } else
171                 if (!strcmp(option,"dsptones")) {
172                         SPRINT(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,option);
173                         goto error;
174                 } else
175                 if (!strcmp(option,"schedule")) {
176                         options.schedule = atoi(param);
177                         if (options.schedule < 0) {
178                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
179                                 goto error;
180                         }
181                         if (options.schedule > 99) {
182                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
183                                 goto error;
184                         }
185
186                 } else
187                 if (!strcmp(option,"email")) {
188                         if (param[0]==0) {
189                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
190                                 goto error;
191                         }
192                         SCPY(options.email, param);
193
194                 } else
195                 if (!strcmp(option,"lock")) {
196                         if (param[0]==0) {
197                                 SPRINT(options_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
198                                 goto error;
199                         }
200                         if (param[strlen(param)-1] == '/')
201                                 param[strlen(param)-1]=0;
202                         SCPY(options.lock, param);
203
204                 } else
205                 if (!strcmp(option,"socketrights")) {
206                         options.socketrights = strtol(param, NULL, 0);
207                 } else
208                 if (!strcmp(option,"gsm")) {
209                         options.gsm = 1;
210                 } else {
211                         SPRINT(options_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
212                         goto error;
213                 }
214         }
215
216         if (!options.tones_dir[0]) {
217                 SPRINT(options_error, "Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
218                 goto error;
219         }
220         if (fp) fclose(fp);
221         return(1);
222 error:
223         if (fp) fclose(fp);
224         return(0);
225 }
226
227