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