85ec80439de24dc004b29076ca318067cffa3f0f
[sip-router] / cfg_parser.c
1 /*
2  * $Id$
3  */
4
5 #include <errno.h>
6 #include <string.h>
7 #include <stdio.h>
8
9 #include "cfg_parser.h"
10 #include "dprint.h"
11 #include "parser_f.h"
12 #include "route.h"
13
14
15
16
17 /* params: null terminated text line => fills cl
18  * returns 0, or on error -1. */
19 int cfg_parse_line(char* line, struct cfg_line* cl)
20 {
21         /* format:
22                 line = rule | comment
23                 comment = SP* '#'.*
24                 rule = SP* method_re SP* uri_re SP* ip_address comment?
25         */
26                 
27         char* tmp;
28         char* end;
29         
30         end=line+strlen(line);
31         tmp=eat_space(line, end-line);
32         if ((tmp==end)||(is_empty(tmp, end-tmp))) {
33                 cl->type=CFG_EMPTY;
34                 goto skip;
35         }
36         if (*tmp=='#'){
37                 cl->type=CFG_COMMENT;
38                 goto skip;
39         }
40         cl->method=tmp;
41         tmp=eat_token(cl->method,end-cl->method);
42         if (tmp==end) goto error;
43         printf("%d\n", tmp-line);
44         *tmp=0;
45         tmp++;
46         cl->uri=eat_space(tmp,end-tmp);
47         if (tmp==end) goto error;
48         tmp=eat_token(cl->uri,end-cl->uri);
49         if (tmp==end) goto error;
50         printf("%d\n", tmp-line);
51         *tmp=0;
52         tmp++;
53         cl->address=eat_space(tmp,end-tmp);
54         if (tmp==end) goto error;
55         tmp=eat_token(cl->address, end-cl->address);
56         printf("%d(%02x)\n", tmp-line, *tmp);
57         if (tmp<end) {
58                 *tmp=0;
59                 if (tmp+1<end){
60                         if (!is_empty(tmp+1,end-tmp-1)){
61                                 printf("%d(%02x) e: %d\n", tmp-line, *tmp, end-line);
62                                 /* check if comment */
63                                 tmp=eat_space(tmp+1, end-tmp-1);
64                                 printf("%d(%02x) e: %d\n", tmp-line, *tmp, end-line);
65                                 if (*tmp!='#'){
66                                         /* extra chars at the end of line */
67                                         goto error;
68                                 }
69                         }
70                 }
71         }
72                 
73         cl->type=CFG_RULE;
74 skip:
75         return 0;
76 error:
77         cl->type=CFG_ERROR;
78         return -1;
79 }
80
81
82
83 /* parses the cfg, returns 0 on success, line no otherwise */
84 int cfg_parse_stream(FILE* stream)
85 {
86         int line;
87         struct cfg_line cl;
88         char buf[MAX_LINE_SIZE];
89         int ret;
90
91         line=1;
92         while(!feof(stream)){
93                 if (fgets(buf, MAX_LINE_SIZE, stream)){
94                         cfg_parse_line(buf, &cl);
95                         switch (cl.type){
96                                 case CFG_RULE:
97                                         if ((ret=add_rule(&cl, &rlist))!=0){
98                                                 DPrint("ERROR: could not compile rule at line %d\n",
99                                                         line);
100                                                 DPrint(" ----: add_rule returned %d\n", ret);
101                                                 goto error;
102                                         }
103                                         break;
104                                 case CFG_COMMENT:
105                                 case CFG_SKIP:
106                                         break;
107                                 case CFG_ERROR:
108                                         DPrint("ERROR: bad config line (%d):%s\n", line, buf);
109                                         goto error;
110                                         break;
111                         }
112                         line++;
113                 }else{
114                         if (ferror(stream)){
115                                 DPrint("ERROR: reading configuration: %s\n", strerror(errno));
116                                 goto error;
117                         }
118                         break;
119                 }
120         }
121         return 0;
122
123 error:
124         return line;
125 }
126