Add .gitignore
[lcr.git] / gsm_conf.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
15 /* read options
16  *
17  * read options from options.conf
18  */
19 int gsm_conf(struct gsm_conf *gsm_conf, char *conf_error)
20 {
21         FILE *fp=NULL;
22         char filename[128];
23         char *p;
24         char option[32];
25         char params[11][256];
26         int pnum;
27         unsigned int line,i;
28         char buffer[256];
29
30         /* set defaults */
31         SCPY(gsm_conf->debug, "");
32         SCPY(gsm_conf->interface_bsc, "mISDN_l1loop.1");
33         SCPY(gsm_conf->interface_lcr, "mISDN_l1loop.2");
34         SCPY(gsm_conf->hlr, "hlr.sqlite3");
35         SCPY(gsm_conf->openbsc_cfg, "openbsc.cfg");
36         gsm_conf->reject_cause = 0;
37         gsm_conf->keep_l2 = 0;
38         gsm_conf->noemergshut = 0;
39
40         SPRINT(filename, "%s/gsm.conf", CONFIG_DATA);
41
42         if (!(fp=fopen(filename,"r"))) {
43                 UPRINT(conf_error, "Cannot open %s\n",filename);
44                 return(0);
45         }
46
47         line=0;
48         while((GETLINE(buffer, fp))) {
49                 line++;
50                 p=buffer;
51
52                 while(*p <= 32) { /* skip spaces */
53                         if (*p == 0)
54                                 break;
55                         p++;
56                 }
57                 if (*p==0 || *p=='#') /* ignore comments and empty line */
58                         continue;
59
60                 option[0]=0;
61                 i=0; /* read option */
62                 while(*p > 32) {
63                         if (i+1 >= sizeof(option)) {
64                                 UPRINT(conf_error, "Error in %s (line %d): option too long.\n",filename,line);
65                                 goto error;
66                         }
67                         option[i+1] = '\0';
68                         option[i++] = *p++;
69                 }
70
71                 while(*p <= 32) { /* skip spaces */
72                         if (*p == 0)
73                                 break;
74                         p++;
75                 }
76
77                 params[0][0] = 0;
78                 pnum = 0;
79                 while(*p!=0 && *p!='#' && pnum < 10) { /* param */
80                         i=0; /* read param */
81                         while(*p > 32) {
82                                 if (i+1 >= sizeof(params[pnum])) {
83                                         UPRINT(conf_error, "Error in %s (line %d): param too long.\n",filename,line);
84                                         goto error;
85                                 }
86                                 params[pnum][i+1] = '\0';
87                                 params[pnum][i++] = *p++;
88                         }
89                         while(*p <= 32) { /* skip spaces */
90                                 if (*p == 0)
91                                         break;
92                                 p++;
93                         }
94                         pnum++;
95                         params[pnum][0] = 0;
96                 }
97
98                 /* at this point we have option and param */
99
100                 /* check option */
101                 if (!strcmp(option,"debug")) {
102                         if (params[0][0]==0) {
103                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
104                                 goto error;
105                         }
106                         SCPY(gsm_conf->debug, params[0]);
107
108                 } else
109                 if (!strcmp(option,"interface-bsc")) {
110                         if (params[0][0]==0) {
111                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
112                                 goto error;
113                         }
114                         SCPY(gsm_conf->interface_bsc, params[0]);
115
116                 } else
117                 if (!strcmp(option,"interface-lcr")) {
118                         if (params[0][0]==0) {
119                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
120                                 goto error;
121                         }
122                         SCPY(gsm_conf->interface_lcr, params[0]);
123
124                 } else
125                 if (!strcmp(option,"config")) {
126                         if (params[0][0]==0) {
127                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
128                                 goto error;
129                         }
130                         SCPY(gsm_conf->openbsc_cfg, params[0]);
131
132                 } else
133                 if (!strcmp(option,"hlr")) {
134                         if (params[0][0]==0) {
135                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
136                                 goto error;
137                         }
138                         SCPY(gsm_conf->hlr, params[0]);
139
140                 } else
141                 if (!strcmp(option,"reject-cause")) {
142                         UPRINT(conf_error, "Option '%s' in gsm.conf has moved to openbsc.cfg", option);
143                         goto error;
144                 } else
145                 if (!strcmp(option,"allow-all")) {
146                         gsm_conf->allow_all = 1;
147                 } else
148                 if (!strcmp(option,"keep-l2")) {
149                         gsm_conf->keep_l2 = 1;
150
151                 } else
152                 if (!strcmp(option,"no-mergency-shutdown")) {
153                         gsm_conf->noemergshut = 1;
154                 } else
155                 if (!strcmp(option,"pcapfile")) {
156                         if (params[0][0]==0) {
157                                 UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
158                                 goto error;
159                         }
160                         SCPY(gsm_conf->pcapfile, params[0]);
161                 } else {
162                         UPRINT(conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
163                         goto error;
164                 }
165         }
166
167         if (fp) fclose(fp);
168         return(1);
169 error:
170         if (fp) fclose(fp);
171         return(0);
172 }
173
174