comparison src/core/ngx_conf_file.c @ 43:53cd05892261

nginx-0.0.1-2002-12-27-19:22:50 import
author Igor Sysoev <igor@sysoev.ru>
date Fri, 27 Dec 2002 16:22:50 +0000
parents src/core/ngx_config_file.c@59e7c7f30d49
children 0e81ac0bb3e2
comparison
equal deleted inserted replaced
42:cd035a94e0b6 43:53cd05892261
1
2 #include <ngx_config.h>
3 #include <ngx_core.h>
4 #include <ngx_conf_file.h>
5
6
7 static int argument_number[] = {
8 NGX_CONF_NOARGS,
9 NGX_CONF_TAKE1,
10 NGX_CONF_TAKE2
11 };
12
13 static int ngx_conf_read_token(ngx_conf_t *cf);
14
15
16 int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
17 {
18 int rc, i;
19 char *error;
20 ngx_str_t *name;
21 ngx_fd_t fd;
22 ngx_conf_file_t *prev;
23 ngx_command_t *cmd;
24
25 if (filename) {
26
27 fd = ngx_open_file(filename->data, NGX_FILE_RDONLY);
28 if (fd == NGX_INVALID_FILE) {
29 ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
30 "ngx_conf_file: "
31 ngx_open_file_n " %s failed", filename->data);
32 return NGX_ERROR;
33 }
34
35 prev = cf->conf_file;
36 ngx_test_null(cf->conf_file,
37 ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)),
38 NGX_ERROR);
39
40 if (ngx_stat_fd(fd, &cf->conf_file->file.info) == -1) {
41 ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
42 "ngx_conf_file: "
43 ngx_stat_fd_n " %s failed", filename->data);
44 }
45
46 ngx_test_null(cf->conf_file->hunk,
47 ngx_create_temp_hunk(cf->pool, 1024, 0, 0),
48 NGX_ERROR);
49
50 cf->conf_file->file.fd = fd;
51 cf->conf_file->file.name.len = filename->len;
52 cf->conf_file->file.name.data = filename->data;
53 cf->conf_file->file.log = cf->log;;
54 cf->conf_file->line = 1;
55 }
56
57 for ( ;; ) {
58 rc = ngx_conf_read_token(cf);
59
60 /* NGX_OK, NGX_ERROR, NGX_CONF_FILE_DONE, NGX_CONF_BLOCK_DONE */
61
62 if (rc == NGX_ERROR || rc == NGX_CONF_FILE_DONE) {
63 return rc;
64 }
65
66 if (cf->handler) {
67
68 if ((*cf->handler)(cf) == NGX_ERROR) {
69 return NGX_ERROR;
70 }
71
72 continue;
73 }
74
75 name = (ngx_str_t *) cf->args->elts;
76
77 for (i = 0; ngx_modules[i]; i++) {
78 if (ngx_modules[i]->type != NULL
79 && ngx_modules[i]->type != cf->type)
80 {
81 continue;
82 }
83
84 cmd = ngx_modules[i]->commands;
85 if (cmd == NULL) {
86 continue;
87 }
88
89 while (cmd->name.len) {
90 if (name->len == cmd->name.len
91 && ngx_strcmp(name->data, cmd->name.data) == 0)
92 {
93
94 ngx_log_debug(cf->log, "command '%s'" _ cmd->name.data);
95
96 cmd->set(cf, cmd, NULL);
97 }
98
99 cmd++;
100 }
101 }
102
103 #if 0
104 cmd = ngx_conf_find_token(cf);
105 if (cmd == NULL) {
106 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
107 "unknown directive \"%s\" in %s:%d",
108 cf->name, cf->file->name, cf->file->line);
109 return NGX_ERROR;
110 }
111
112 if (cmd->type & argument_number[cf->args->nelts - 1]) {
113 error = cmd->set(cf, cmd->offset, cf->args);
114
115 if (error) {
116 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
117 "%s in directive \"%s\" in %s:%d",
118 error, cf->name, cf->file->name, cf->file->line);
119 return NGX_ERROR;
120 }
121 }
122 #endif
123
124 #if 0
125 if (cmd->type == NGX_CONF_CONTAINER) {
126 ngx_conf_parse(cf, cmd->container, NULL);
127
128 } else if (cmd->type == NGX_CONF_FLAG) {
129
130 if (cf->args->nelts != 1) {
131 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
132 "invalid number of arguments "
133 "in directive \"%s\" in %s:%d",
134 cf->name, cf->file->name, cf->file->line);
135 return NGX_ERROR;
136 }
137
138 if (ngx_strcasecmp(cf->args->elts[0], "on") == 0) {
139 flag = 1;
140
141 } else if (ngx_strcasecmp(cf->args->elts[0], "off") == 0) {
142 flag = 0;
143
144 } else {
145 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
146 "invalid flag in directive \"%s\" in %s:%d",
147 cf->name, cf->file->name, cf->file->line);
148 return NGX_ERROR;
149 }
150
151 rv = cmd->set(cf, cmd->offset, flag);
152 if (rv) {
153 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
154 "%s in directive \"%s\" in %s:%d",
155 rv, cf->name, cf->file->name, cf->file->line);
156 return NGX_ERROR;
157 }
158
159 } else if (cmd->type & argument_number[args->nelts]) {
160 rv = cmd->set(cf, cmd->offset, cf->args);
161 if (rv) {
162 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
163 "%s in directive \"%s\" in %s:%d",
164 rv, cf->name, cf->file->name, cf->file->line);
165 return NGX_ERROR;
166 }
167
168 } else {
169 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
170 "invalid number of arguments "
171 "in directive \"%s\" in %s:%d",
172 cf->name, cf->file->name, cf->file->line);
173 return NGX_ERROR;
174 }
175 #endif
176 }
177
178 if (filename) {
179 cf->conf_file = prev;
180
181 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
182 ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno,
183 ngx_close_file_n " %s failed",
184 cf->conf_file->file.name.data);
185 return NGX_ERROR;
186 }
187 }
188
189 return NGX_OK;
190 }
191
192
193 static int ngx_conf_read_token(ngx_conf_t *cf)
194 {
195 char *start, ch, *src, *dst;
196 int len;
197 int found, need_space, last_space, sharp_comment;
198 int quoted, s_quoted, d_quoted;
199 ssize_t n;
200 ngx_str_t *word;
201 ngx_hunk_t *h;
202
203 found = 0;
204 need_space = 0;
205 last_space = 1;
206 sharp_comment = 0;
207 quoted = s_quoted = d_quoted = 0;
208
209 cf->args->nelts = 0;
210 h = cf->conf_file->hunk;
211 start = h->pos.mem;
212
213 ngx_log_debug(cf->log, "TOKEN START");
214
215 for ( ;; ) {
216
217 if (h->pos.mem >= h->last.mem) {
218 if (cf->conf_file->file.offset
219 >= ngx_file_size(cf->conf_file->file.info)) {
220 return NGX_CONF_FILE_DONE;
221 }
222
223 if (h->pos.mem - start) {
224 ngx_memcpy(h->start, start, h->pos.mem - start);
225 }
226
227 n = ngx_read_file(&cf->conf_file->file,
228 h->start + (h->pos.mem - start),
229 h->end - (h->start + (h->pos.mem - start)),
230 cf->conf_file->file.offset);
231
232 if (n == NGX_ERROR) {
233 return NGX_ERROR;
234 }
235
236 h->pos.mem = h->start + (h->pos.mem - start);
237 start = h->start;
238 h->last.mem = h->pos.mem + n;
239 }
240
241 ch = *h->pos.mem++;
242
243 #if 0
244 ngx_log_debug(cf->log, "%d:%d:%d:%d:%d '%c'" _
245 last_space _ need_space _
246 quoted _ s_quoted _ d_quoted _ ch);
247 #endif
248
249 if (ch == LF) {
250 cf->conf_file->line++;
251
252 if (sharp_comment) {
253 sharp_comment = 0;
254 }
255 }
256
257 if (sharp_comment) {
258 continue;
259 }
260
261 if (quoted) {
262 quoted = 0;
263 continue;
264 }
265
266 if (need_space) {
267 if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
268 last_space = 1;
269 need_space = 0;
270 continue;
271 }
272
273 if (ch == ';' || ch == '{') {
274 return NGX_OK;
275 }
276
277 return NGX_ERROR;
278 }
279
280 if (last_space) {
281 if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
282 continue;
283 }
284
285 start = h->pos.mem - 1;
286
287 switch (ch) {
288
289 case ';':
290 case '{':
291 if (cf->args->nelts == 0) {
292 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
293 "unexpected '%c' in %s:%d",
294 ch, cf->conf_file->file.name.data,
295 cf->conf_file->line);
296 return NGX_ERROR;
297 }
298
299 return NGX_OK;
300
301 case '}':
302 if (cf->args->nelts > 0) {
303 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
304 "unexpected '}' in %s:%d",
305 cf->conf_file->file.name.data,
306 cf->conf_file->line);
307 return NGX_ERROR;
308 }
309
310 return NGX_CONF_BLOCK_DONE;
311
312 case '#':
313 sharp_comment = 1;
314 continue;
315
316 case '\\':
317 quoted = 1;
318 last_space = 0;
319 continue;
320
321 case '"':
322 start++;
323 d_quoted = 1;
324 last_space = 0;
325 continue;
326
327 case '\'':
328 start++;
329 s_quoted = 1;
330 last_space = 0;
331 continue;
332
333 default:
334 last_space = 0;
335 }
336
337 } else {
338 if (ch == '\\') {
339 quoted = 1;
340 continue;
341 }
342
343 if (d_quoted) {
344 if (ch == '"') {
345 d_quoted = 0;
346 need_space = 1;
347 found = 1;
348 }
349
350 } else if (s_quoted) {
351 if (ch == '\'') {
352 s_quoted = 0;
353 need_space = 1;
354 found = 1;
355 }
356
357 } else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
358 || ch == ';' || ch == '{') {
359 last_space = 1;
360 found = 1;
361 }
362
363 if (found) {
364 ngx_test_null(word, ngx_push_array(cf->args), NGX_ERROR);
365 ngx_test_null(word->data,
366 ngx_palloc(cf->pool, h->pos.mem - start + 1),
367 NGX_ERROR);
368
369 for (dst = word->data, src = start, len = 0;
370 src < h->pos.mem - 1;
371 len++)
372 {
373 if (*src == '\\') {
374 src++;
375 }
376 *dst++ = *src++;
377 }
378 *dst = '\0';
379 word->len = len;
380
381 ngx_log_debug(cf->log, "FOUND %d:'%s'" _ word->len _ word->data);
382
383 if (ch == ';' || ch == '{') {
384 return NGX_OK;
385 }
386
387 found = 0;
388 }
389 }
390 }
391 }
392
393
394 char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
395 {
396 int size;
397 ngx_str_t *value;
398
399 value = (ngx_str_t *) cf->args->elts;
400
401 size = atoi(value[1].data);
402 if (size < 0) {
403 return "value must be greater or equal to zero";
404 }
405
406 *(int *) (conf + cmd->offset) = size;
407
408 return NULL;
409 }
410
411
412 char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
413 {
414 int size;
415 ngx_str_t *value;
416
417 value = (ngx_str_t *) cf->args->elts;
418
419 size = atoi(value[1].data);
420 if (size < 0) {
421 return "value must be greater or equal to zero";
422 }
423
424 *(int *) (conf + cmd->offset) = size;
425
426 return NULL;
427 }