parser.c

Sun, 03 Nov 2002 03:51:35 +0100

author
tuomov
date
Sun, 03 Nov 2002 03:51:35 +0100
changeset 42
4fdc7ea2bc38
parent 40
0a050a9536a1
child 46
c6deeee169aa
permissions
-rw-r--r--

trunk: changeset 45
(void*)+n fixed

0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
1 /*
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
2 * libtu/parser.c
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
3 *
36
63cd573ffbcf trunk: changeset 39
tuomov
parents: 35
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2002.
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
5 * See the included file LICENSE for details.
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
6 */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
7
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
8 #include <string.h>
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
9 #include <errno.h>
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
10
5
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 2
diff changeset
11 #include <libtu/parser.h>
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 2
diff changeset
12 #include <libtu/misc.h>
f878a9ffa3e0 trunk: changeset 8
tuomov
parents: 2
diff changeset
13 #include <libtu/output.h>
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
14
13
9eb09d246c9f trunk: changeset 16
tuomov
parents: 9
diff changeset
15 #define MAX_TOKENS 256
9eb09d246c9f trunk: changeset 16
tuomov
parents: 9
diff changeset
16 #define MAX_NEST 256
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
17
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
18
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
19 enum{
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
20 P_NONE=1,
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
21 P_EOF,
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
22 P_STMT,
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
23 P_STMT_NS,
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
24 P_STMT_SECT,
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
25 P_BEG_SECT,
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
26 P_END_SECT
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
27 };
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
28
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
29
20
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
30 /* */
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
31
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
32
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
33 static bool opt_include(Tokenizer *tokz, int n, Token *toks);
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
34
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
35
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
36 static ConfOpt common_opts[]={
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
37 {"include", "s", opt_include, NULL},
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
38 {NULL, NULL, NULL, NULL}
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
39 };
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
40
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
41
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
42 /* */
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
43
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
44
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
45 static int read_statement(Tokenizer *tokz, Token *tokens, int *ntok_ret)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
46 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
47 int ntokens=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
48 Token *tok=NULL;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
49 int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
50 int retval=0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
51 int e=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
52
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
53 while(1){
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
54 tok=&tokens[ntokens];
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
55
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
56 if(!tokz_get_token(tokz, tok)){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
57 e=1;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
58 continue;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
59 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
60
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
61 if(ntokens==MAX_TOKENS-1){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
62 e=E_TOKZ_TOKEN_LIMIT;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
63 tokz_warn_error(tokz, tok->line, e);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
64 if(!(tokz->flags&TOKZ_ERROR_TOLERANT))
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
65 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
66 }else{
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
67 ntokens++;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
68 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
69
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
70 if(!TOK_IS_OP(tok)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
71 if(ntokens==1 && !had_comma){
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
72 /* first token */
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
73 had_comma=2;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
74 }else{
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
75 if(had_comma==0)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
76 goto syntax;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
77
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
78 had_comma=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
79 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
80 continue;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
81 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
82
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
83 /* It is an operator */
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
84 ntokens--;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
85
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
86 switch(TOK_OP_VAL(tok)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
87 case OP_SCOLON:
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
88 retval=(ntokens==0 ? P_NONE : P_STMT_NS);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
89 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
90
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
91 case OP_NEXTLINE:
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
92 retval=(ntokens==0 ? P_NONE : P_STMT);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
93 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
94
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
95 case OP_L_BRC:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
96 retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
97 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
98
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
99 case OP_R_BRC:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
100 if(ntokens==0){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
101 retval=P_END_SECT;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
102 }else{
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
103 tokz_unget_token(tokz, tok);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
104 retval=P_STMT_NS;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
105 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
106 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
107
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
108 case OP_EOF:
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
109 retval=(ntokens==0 ? P_EOF : P_STMT_NS);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
110
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
111 if(had_comma==1){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
112 e=E_TOKZ_UNEXPECTED_EOF;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
113 goto handle_error;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
114 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
115
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
116 goto end;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
117
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
118 case OP_COMMA:
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
119 if(had_comma!=0)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
120 goto syntax;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
121
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
122 had_comma=1;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
123 continue;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
124
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
125 default:
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
126 goto syntax;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
127 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
128
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
129 if(had_comma!=1)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
130 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
131
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
132 syntax:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
133 e=E_TOKZ_SYNTAX;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
134 handle_error:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
135 tokz_warn_error(tokz, tok->line, e);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
136
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
137 if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
138 break;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
139 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
140
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
141 end:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
142 if(e!=0)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
143 retval=-retval;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
144
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
145 *ntok_ret=ntokens;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
146
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
147 return retval;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
148 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
149
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
150
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
151 static bool find_beg_sect(Tokenizer *tokz)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
152 {
24
c3fa6c2785dd trunk: changeset 27
tuomov
parents: 22
diff changeset
153 Token tok=TOK_INIT;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
154
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
155 while(tokz_get_token(tokz, &tok)){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
156 if(TOK_IS_OP(&tok)){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
157 if(TOK_OP_VAL(&tok)==OP_NEXTLINE)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
158 continue;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
159
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
160 if(TOK_OP_VAL(&tok)==OP_SCOLON)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
161 return FALSE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
162
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
163 if(TOK_OP_VAL(&tok)==OP_L_BRC)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
164 return TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
165 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
166
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
167 tokz_unget_token(tokz, &tok);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
168 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
169 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
170 return FALSE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
171 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
172
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
173
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
174 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
175
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
176
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
177 static const ConfOpt* lookup_option(const ConfOpt *opts, const char *name)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
178 {
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
179 while(opts->optname!=NULL){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
180 if(strcmp(opts->optname, name)==0)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
181 return opts;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
182 opts++;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
183 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
184 return NULL;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
185 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
186
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
187
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
188 static bool call_end_sect(Tokenizer *tokz, const ConfOpt *opts)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
189 {
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
190 opts=lookup_option(opts, "#end");
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
191 if(opts!=NULL)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
192 return opts->fn(tokz, 0, NULL);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
193
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
194 return TRUE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
195 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
196
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
197
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
198 static bool call_cancel_sect(Tokenizer *tokz, const ConfOpt *opts)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
199 {
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
200 opts=lookup_option(opts, "#cancel");
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
201 if(opts!=NULL)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
202 return opts->fn(tokz, 0, NULL);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
203
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
204 return TRUE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
205 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
206
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
207
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
208 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
209
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
210
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
211 bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
212 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
213 Token tokens[MAX_TOKENS];
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
214 bool alloced_optstack=FALSE;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
215 int i, t, ntokens=0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
216 int init_nest_lvl;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
217 bool had_error;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
218 int errornest=0;
35
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
219 bool is_default=FALSE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
220
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
221 /* Allocate tokz->optstack if it does not yet exist (if it does,
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
222 * we have been called from an option handler)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
223 */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
224 if(!tokz->optstack){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
225 tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
226 if(!tokz->optstack){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
227 warn_err();
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
228 return FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
229 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
230
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
231 memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
232 init_nest_lvl=tokz->nest_lvl=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
233 alloced_optstack=TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
234 }else{
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
235 init_nest_lvl=tokz->nest_lvl;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
236 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
237
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
238 tokz->optstack[init_nest_lvl]=options;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
239
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
240 for(i=0; i<MAX_TOKENS; i++)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
241 tok_init(&tokens[i]);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
242
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
243
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
244 while(1){
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
245 had_error=FALSE;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
246
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
247 /* free the tokens */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
248 while(ntokens--)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
249 tok_free(&tokens[ntokens]);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
250
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
251 /* read the tokens */
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
252 t=read_statement(tokz, tokens, &ntokens);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
253
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
254 if((had_error=t<0))
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
255 t=-t;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
256
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
257 switch(t){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
258 case P_STMT:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
259 case P_STMT_NS:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
260 case P_STMT_SECT:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
261
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
262 if(errornest)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
263 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
264 else if(tokz->flags&TOKZ_PARSER_INDENT_MODE)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
265 verbose_indent(tokz->nest_lvl);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
266
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
267 if(!TOK_IS_IDENT(tokens+0)){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
268 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
269 tokz_warn_error(tokz, tokens->line,
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
270 E_TOKZ_IDENTIFIER_EXPECTED);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
271 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
272
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
273 if(t==P_STMT){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
274 if(find_beg_sect(tokz))
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
275 t=P_STMT_SECT;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
276 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
277
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
278 if(had_error)
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
279 break;
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
280
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
281 /* Got the statement and its type */
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
282
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
283 options=lookup_option(tokz->optstack[tokz->nest_lvl],
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
284 TOK_IDENT_VAL(tokens+0));
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
285 if(options==NULL)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
286 options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0));
35
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
287 if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
288 options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default");
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
289 is_default=(options!=NULL);
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
290 }
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
291
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
292 if(options==NULL){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
293 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
294 tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION);
35
5a71d53d0228 trunk: changeset 38
tuomov
parents: 24
diff changeset
295 }else if(!is_default) {
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
296 had_error=!check_args(tokz, tokens, ntokens, options->argfmt);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
297 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
298
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
299 if(had_error)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
300 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
301
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
302 /* Found the option and arguments are ok */
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
303
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
304 if(options->opts!=NULL){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
305 if(t!=P_STMT_SECT){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
306 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
307 tokz_warn_error(tokz, tokz->line, E_TOKZ_LBRACE_EXPECTED);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
308 }else if(tokz->nest_lvl==MAX_NEST-1){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
309 tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
310 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
311 }else{
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
312 tokz->nest_lvl++;
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
313 tokz->optstack[tokz->nest_lvl]=options->opts;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
314 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
315 }else if(t==P_STMT_SECT){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
316 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
317 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
318 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
319
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
320 if(!had_error && options->fn!=NULL){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
321 had_error=!options->fn(tokz, ntokens, tokens);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
322 if(t==P_STMT_SECT && had_error)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
323 tokz->nest_lvl--;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
324 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
325 break;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
326
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
327 case P_EOF:
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
328 if(tokz_popf(tokz)){
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
329 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
330 }else if(tokz->nest_lvl>0 || errornest>0){
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
331 tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
332 had_error=TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
333 }
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
334 goto eof;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
335
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
336 case P_BEG_SECT:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
337 had_error=TRUE;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
338 errornest++;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
339 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
340 break;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
341
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
342 case P_END_SECT:
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
343 if(tokz->nest_lvl+errornest==0){
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
344 tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
345 had_error=TRUE;
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
346 }
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
347
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
348 if(had_error)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
349 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
350
20
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
351 if(errornest!=0){
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
352 errornest--;
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
353 }else{
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
354 had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]);
20
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
355 tokz->nest_lvl--;
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
356 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
357
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
358 if(tokz->nest_lvl<init_nest_lvl)
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
359 goto eof;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
360 }
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
361
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
362 if(!had_error)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
363 continue;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
364
22
79fb689317ea trunk: changeset 25
tuomov
parents: 20
diff changeset
365 if(t==P_STMT_SECT)
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
366 errornest++;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
367
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
368 if(!(tokz->flags&TOKZ_ERROR_TOLERANT))
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
369 break;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
370 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
371
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
372 eof:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
373 /* free the tokens */
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
374 while(ntokens--)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
375 tok_free(&tokens[ntokens]);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
376
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
377 while(tokz->nest_lvl>=init_nest_lvl){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
378 if(tokz->flags&TOKZ_ERROR_TOLERANT || !had_error)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
379 call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
380 else
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
381 call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
382 tokz->nest_lvl--;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
383 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
384
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
385 /* Free optstack if it was alloced by this call */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
386 if(alloced_optstack){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
387 free(tokz->optstack);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
388 tokz->optstack=NULL;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
389 tokz->nest_lvl=0;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
390 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
391
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
392 if(tokz->flags&TOKZ_PARSER_INDENT_MODE)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
393 verbose_indent(init_nest_lvl);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
394
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
395 return !had_error;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
396 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
397
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
398
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
399 /* */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
400
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
401
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
402 bool parse_config(const char *fname, const ConfOpt *options, int flags)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
403 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
404 Tokenizer *tokz;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
405 bool ret;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
406
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
407 tokz=tokz_open(fname);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
408
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
409 if(tokz==NULL)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
410 return FALSE;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
411
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
412 tokz->flags|=flags&~TOKZ_READ_COMMENTS;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
413
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
414 ret=parse_config_tokz(tokz, options);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
415
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
416 tokz_close(tokz);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
417
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
418 return ret;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
419 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
420
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
421
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
422 bool parse_config_file(FILE *file, const ConfOpt *options, int flags)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
423 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
424 Tokenizer *tokz;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
425 bool ret;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
426
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
427 tokz=tokz_open_file(file, NULL);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
428
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
429 if(tokz==NULL)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
430 return FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
431
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
432 tokz->flags|=flags&~TOKZ_READ_COMMENTS;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
433
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
434 ret=parse_config_tokz(tokz, options);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
435
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
436 tokz_close(tokz);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
437
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
438 return ret;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
439 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
440
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
441
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
442 /*
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
443 * Argument validity checking stuff
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
444 */
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
445
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
446
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
447 static int arg_match(Token *tok, char c, bool si)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
448 {
20
3f11f764772a trunk: changeset 23
tuomov
parents: 17
diff changeset
449 char c2=tok->type;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
450
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
451 if(c=='.' || c=='*')
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
452 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
453
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
454 if(c2==c)
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
455 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
456
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
457 if(si){
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
458 if(c2=='i' && c=='s'){
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
459 TOK_SET_IDENT(tok, TOK_STRING_VAL(tok));
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
460 return 0;
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
461 }
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
462
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
463 if(c2=='s' && c=='i'){
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
464 TOK_SET_STRING(tok, TOK_IDENT_VAL(tok));
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
465 return 0;
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
466 }
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
467 }
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
468
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
469 if(c2=='c' && c=='l'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
470 TOK_SET_LONG(tok, TOK_CHAR_VAL(tok));
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
471 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
472 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
473
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
474 if(c2=='l' && c=='c'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
475 TOK_SET_CHAR(tok, TOK_LONG_VAL(tok));
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
476 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
477 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
478
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
479 if(c2=='l' && c=='d'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
480 TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok));
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
481 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
482 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
483
17
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
484 if(c=='b'){
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
485 if(c2=='l'){
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
486 TOK_SET_BOOL(tok, TOK_LONG_VAL(tok));
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
487 return 0;
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
488 }else if(c2=='i'){
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
489 if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
490 tok_free(tok);
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
491 TOK_SET_BOOL(tok, TRUE);
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
492 return 0;
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
493 }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
494 tok_free(tok);
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
495 TOK_SET_BOOL(tok, FALSE);
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
496 return 0;
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
497 }
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
498 }
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
499 }
a034888a2227 trunk: changeset 20
tuomov
parents: 14
diff changeset
500
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
501 return E_TOKZ_INVALID_ARGUMENT;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
502 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
503
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
504
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
505 static int check_argument(const char **pret, Token *tok, const char *p,
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
506 bool si)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
507 {
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
508 int mode;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
509 int e=E_TOKZ_TOO_MANY_ARGS;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
510
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
511 again:
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
512 mode=0;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
513
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
514 if(*p=='*'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
515 *pret=p;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
516 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
517 }else if(*p=='?'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
518 mode=1;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
519 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
520 }else if(*p==':'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
521 mode=2;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
522 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
523 }else if(*p=='+'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
524 *pret=p;
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
525 return arg_match(tok, *(p-1), si);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
526 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
527
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
528 while(*p!='\0'){
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
529 e=arg_match(tok, *p, si);
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
530 if(e==0){
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
531 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
532 while(mode==2 && *p==':'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
533 if(*++p=='\0')
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
534 break; /* Invalid argument format string, though... */
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
535 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
536 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
537 *pret=p;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
538 return 0;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
539 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
540
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
541 if(mode==0)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
542 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
543
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
544 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
545
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
546 if(mode==1)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
547 goto again;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
548
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
549 /* mode==2 */
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
550
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
551 if(*p!=':')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
552 break;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
553 p++;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
554 e=E_TOKZ_TOO_MANY_ARGS;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
555 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
556
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
557 *pret=p;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
558 return e;
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
559 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
560
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
561
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
562 static bool args_at_end(const char *p)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
563 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
564 if(p==NULL)
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
565 return TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
566
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
567 while(*p!='\0'){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
568 if(*p=='*' || *p=='+')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
569 p++;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
570 else if(*p=='?')
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
571 p+=2;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
572 else
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
573 return FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
574 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
575
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
576 return TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
577 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
578
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
579
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
580 bool do_check_args(const Tokenizer *tokz, Token *tokens, int ntokens,
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
581 const char *fmt, bool si)
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
582 {
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
583 int i;
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
584 int e;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
585
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
586 if(fmt==NULL){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
587 if(ntokens!=1)
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
588 tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS);
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
589 return ntokens==1;
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
590 }
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
591
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
592 for(i=1; i<ntokens; i++){
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
593 e=check_argument(&fmt, &tokens[i], fmt, si);
2
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
594 if(e!=0){
e14a1aba4c56 trunk: changeset 5
tuomov
parents: 1
diff changeset
595 tokz_warn_error(tokz, tokens[i].line, e);
0
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
596 return FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
597 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
598 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
599
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
600 if(!args_at_end(fmt)){
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
601 tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS);
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
602 return FALSE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
603 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
604
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
605 return TRUE;
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
606 }
86b7f6f9c5c0 Tailorization of trunk
tailor@f281.ttorni.ton.tut.fi
parents:
diff changeset
607
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
608
40
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
609 bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens,
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
610 const char *fmt)
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
611 {
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
612 return do_check_args(tokz, tokens, ntokens, fmt, FALSE);
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
613 }
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
614
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
615
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
616 bool check_args_loose(const Tokenizer *tokz, Token *tokens, int ntokens,
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
617 const char *fmt)
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
618 {
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
619 return do_check_args(tokz, tokens, ntokens, fmt, TRUE);
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
620 }
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
621
0a050a9536a1 trunk: changeset 43
tuomov
parents: 36
diff changeset
622
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
623 /* */
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
624
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
625
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
626 static bool try_include(Tokenizer *tokz, const char *fname)
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
627 {
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
628 FILE *f;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
629
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
630 f=fopen(fname, "r");
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
631
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
632 if(f==NULL)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
633 return FALSE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
634
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
635 if(!tokz_pushf_file(tokz, f, fname)){
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
636 fclose(f);
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
637 return FALSE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
638 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
639
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
640 return TRUE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
641 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
642
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
643
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
644 static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen,
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
645 const char *file)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
646 {
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
647 char *tmpname;
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
648 bool retval;
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
649
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
650 tmpname=scatn(dir, dlen, file, -1);
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
651
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
652 if(tmpname==NULL){
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
653 warn_err();
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
654 return FALSE;
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
655 }
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
656
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
657 retval=try_include(tokz, tmpname);
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
658
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
659 free(tmpname);
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
660
1
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
661 return retval;
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
662 }
6e704fc09528 trunk: changeset 4
tuomov
parents: 0
diff changeset
663
14
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
664
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
665 static bool opt_include(Tokenizer *tokz, int n, Token *toks)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
666 {
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
667 const char *fname=TOK_STRING_VAL(toks+1);
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
668 const char *lastndx=NULL;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
669 bool retval, e;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
670 int i=0;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
671
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
672 if(fname[0]!='/' && tokz->name!=NULL)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
673 lastndx=strrchr(tokz->name, '/');
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
674
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
675 if(lastndx==NULL)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
676 retval=try_include(tokz, fname);
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
677 else
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
678 retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname);
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
679
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
680 if(retval==TRUE)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
681 return TRUE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
682
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
683 e=errno;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
684
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
685 if(tokz->includepaths!=NULL){
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
686 while(tokz->includepaths[i]!=NULL){
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
687 if(try_include_dir(tokz, tokz->includepaths[i], -1, fname))
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
688 return TRUE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
689 i++;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
690 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
691 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
692
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
693 warn_obj(fname, "%s", strerror(e));
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
694
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
695 return FALSE;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
696 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
697
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
698
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
699 extern void tokz_set_includepaths(Tokenizer *tokz, char **paths)
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
700 {
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
701 tokz->includepaths=paths;
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
702 }
91f0c87b62a7 trunk: changeset 17
tuomov
parents: 13
diff changeset
703

mercurial