Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame^] | 1 | |
| 2 | #include <ngx_config.h> |
| 3 | |
| 4 | #include <ngx_core.h> |
| 5 | |
| 6 | #include <ngx_config_file.h> |
| 7 | |
| 8 | |
| 9 | static int argument_number[] = { |
| 10 | NGX_CONF_NOARGS, |
| 11 | NGX_CONF_TAKE1, |
| 12 | NGX_CONF_TAKE2 |
| 13 | }; |
| 14 | |
| 15 | #if 0 |
| 16 | |
| 17 | int ngx_conf_parse(ngx_conf_t *cf, char *filename) |
| 18 | { |
| 19 | int rc; |
| 20 | char *error; |
| 21 | ngx_fd_t fd; |
| 22 | ngx_conf_file_t *prev; |
| 23 | |
| 24 | if (filename) { |
| 25 | |
| 26 | fd = ngx_open_file(filename, NGX_FILE_RDONLY); |
| 27 | if (fd == NGX_INVALID_FILE) { |
| 28 | ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno, |
| 29 | "ngx_conf_open: " |
| 30 | ngx_open_file_n " %s failed", filename); |
| 31 | return NGX_ERROR; |
| 32 | } |
| 33 | |
| 34 | prev = cf->file; |
| 35 | ngx_test_null(cf->file, ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)), |
| 36 | NGX_ERROR); |
| 37 | |
| 38 | cf->file->fd = fd; |
| 39 | cf->file->name = filename; |
| 40 | cf->file->line = 1; |
| 41 | cf->file->pos = 0; |
| 42 | } |
| 43 | |
| 44 | for ( ;; ) { |
| 45 | rc = ngx_conf_read_token(cf); |
| 46 | |
| 47 | /* ??? NGX_OK, NGX_ERROR, NGX_CONF_FILE_DONE, NGX_CONF_BLOCK_DONE */ |
| 48 | |
| 49 | if (rc != NGX_OK) { |
| 50 | return rc; |
| 51 | } |
| 52 | |
| 53 | /* ???? |
| 54 | "listen address:port;" |
| 55 | "location /images/ {" */ |
| 56 | |
| 57 | if (cf->handler) { |
| 58 | |
| 59 | if (*(cf->handler)(cf) == NGX_ERROR) { |
| 60 | return NGX_ERROR; |
| 61 | } |
| 62 | |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | cmd = ngx_conf_find_token(cf); |
| 67 | if (cmd == NULL) { |
| 68 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 69 | "unknown directive \"%s\" in %s:%d", |
| 70 | cf->name, cf->file->name, cf->file->line); |
| 71 | return NGX_ERROR; |
| 72 | } |
| 73 | |
| 74 | if (cmd->type & argument_number[cf->args->nelts]) { |
| 75 | error = cmd->set(cf, cmd->offset, cf->args); |
| 76 | |
| 77 | if (error) { |
| 78 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 79 | "%s in directive \"%s\" in %s:%d", |
| 80 | error, cf->name, cf->file->name, cf->file->line); |
| 81 | return NGX_ERROR; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | #if 0 |
| 86 | if (cmd->type == NGX_CONF_CONTAINER) { |
| 87 | ngx_conf_parse(cf, cmd->container, NULL); |
| 88 | |
| 89 | } else if (cmd->type == NGX_CONF_FLAG) { |
| 90 | |
| 91 | if (cf->args->nelts != 1) { |
| 92 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 93 | "invalid number of arguments " |
| 94 | "in directive \"%s\" in %s:%d", |
| 95 | cf->name, cf->file->name, cf->file->line); |
| 96 | return NGX_ERROR; |
| 97 | } |
| 98 | |
| 99 | if (ngx_strcasecmp(cf->args->elts[0], "on") == 0) { |
| 100 | flag = 1; |
| 101 | |
| 102 | } else if (ngx_strcasecmp(cf->args->elts[0], "off") == 0) { |
| 103 | flag = 0; |
| 104 | |
| 105 | } else { |
| 106 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 107 | "invalid flag in directive \"%s\" in %s:%d", |
| 108 | cf->name, cf->file->name, cf->file->line); |
| 109 | return NGX_ERROR; |
| 110 | } |
| 111 | |
| 112 | rv = cmd->set(cf, cmd->offset, flag); |
| 113 | if (rv) { |
| 114 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 115 | "%s in directive \"%s\" in %s:%d", |
| 116 | rv, cf->name, cf->file->name, cf->file->line); |
| 117 | return NGX_ERROR; |
| 118 | } |
| 119 | |
| 120 | } else if (cmd->type & argument_number[args->nelts]) { |
| 121 | rv = cmd->set(cf, cmd->offset, cf->args); |
| 122 | if (rv) { |
| 123 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 124 | "%s in directive \"%s\" in %s:%d", |
| 125 | rv, cf->name, cf->file->name, cf->file->line); |
| 126 | return NGX_ERROR; |
| 127 | } |
| 128 | |
| 129 | } else { |
| 130 | ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
| 131 | "invalid number of arguments " |
| 132 | "in directive \"%s\" in %s:%d", |
| 133 | cf->name, cf->file->name, cf->file->line); |
| 134 | return NGX_ERROR; |
| 135 | } |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | if (filename) { |
| 140 | cf->file = prev; |
| 141 | |
| 142 | if (ngx_close_file(fd) == NGX_FILE_ERROR) { |
| 143 | ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno, |
| 144 | ngx_close_file_n " %s failed", cf->name); |
| 145 | return NGX_ERROR; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return NGX_OK; |
| 150 | } |
| 151 | |
| 152 | #endif |
| 153 | |
| 154 | #if 0 |
| 155 | |
| 156 | int ngx_conf_read_token(ngx_conf_t *cf) |
| 157 | { |
| 158 | char *start, ch, *src, *dst; |
| 159 | int n, need_space, last_space, len, quoted, s_quoted, d_quoted; |
| 160 | ngx_hunk_t *h; |
| 161 | |
| 162 | need_space = 0; |
| 163 | last_space = 1; |
| 164 | len = 0; |
| 165 | quoted = s_quoted = d_quoted = 0; |
| 166 | |
| 167 | cf->args->nelts = 0; |
| 168 | h = cf->conf_file->hunk; |
| 169 | |
| 170 | for (start = h->pos.mem; /* end_of_file */ ; h->pos.mem++) { |
| 171 | |
| 172 | if (h->pos.mem >= h->last.mem) { |
| 173 | if (h->pos.mem - start) { |
| 174 | ngx_memcpy(h->start, start, h->pos.mem - start); |
| 175 | } |
| 176 | |
| 177 | n = ngx_read_file(cf->conf_file->file, |
| 178 | h->start + (h->pos.mem - start), |
| 179 | h->end - (h->start + (h->pos.mem - start)), |
| 180 | cf->conf_file->file.offset); |
| 181 | |
| 182 | if (n == NGX_ERROR) { |
| 183 | return NGX_ERROR; |
| 184 | } |
| 185 | |
| 186 | cf->conf_file.file.offset += n; |
| 187 | h->pos.mem = h->start + (h->pos.mem - start); |
| 188 | start = h->start; |
| 189 | h->last.mem = h->pos.mem + n; |
| 190 | } |
| 191 | |
| 192 | ch = *h->pos.mem; |
| 193 | |
| 194 | if (ch == LF) { |
| 195 | cf->conf_file->line++; |
| 196 | } |
| 197 | |
| 198 | if (quoted) { |
| 199 | quoted = 0; |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | len++; |
| 204 | |
| 205 | if (last_space) { |
| 206 | start = h->pos.mem; |
| 207 | |
| 208 | if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) { |
| 209 | start++; |
| 210 | continue; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (ch = '\\') { |
| 215 | quoted = 1; |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | if (d_quoted) { |
| 220 | |
| 221 | if (ch == '"') { |
| 222 | d_quoted = 0; |
| 223 | need_space = 1; |
| 224 | last_space = 1; |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | } else if (s_quoted) { |
| 229 | |
| 230 | if (ch == '\'') { |
| 231 | s_quoted = 0; |
| 232 | need_space = 1; |
| 233 | last_space = 1; |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | } else { |
| 238 | |
| 239 | if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) { |
| 240 | ngx_test_null(word, ngx_push_array(cf->args), NGX_ERROR); |
| 241 | ngx_test_null(word->data, ngx_palloc(cf->temp_pool, len + 1), |
| 242 | NGX_ERROR); |
| 243 | word->len = len; |
| 244 | |
| 245 | for (dst = word->data, src = start; src < h->pos; /* void */) { |
| 246 | if (*src == '\\') |
| 247 | src++; |
| 248 | *dst++ = *src++; |
| 249 | } |
| 250 | *dst = '\0'; |
| 251 | |
| 252 | need_space = 0; |
| 253 | last_space = 1; |
| 254 | continue; |
| 255 | } |
| 256 | |
| 257 | if (need_space) { |
| 258 | return NGX_ERROR; |
| 259 | } |
| 260 | |
| 261 | if (ch == ';') { |
| 262 | return NGX_OK; |
| 263 | } |
| 264 | |
| 265 | if (ch == '{') { |
| 266 | return NGX_OK; |
| 267 | } |
| 268 | |
| 269 | if (ch == '}') { |
| 270 | return NGX_BLOCK_DONE; |
| 271 | } |
| 272 | |
| 273 | if (last_space) { |
| 274 | if (ch == '"') { |
| 275 | d_quoted = 1; |
| 276 | continue; |
| 277 | } |
| 278 | |
| 279 | if (ch == '\'') { |
| 280 | s_quoted = 1; |
| 281 | continue; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | last_space = 0; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | #endif |
| 291 | |
| 292 | char *ngx_conf_set_size_slot(char *conf, int offset, char *value) |
| 293 | { |
| 294 | int size; |
| 295 | |
| 296 | size = atoi(value); |
| 297 | if (size < 0) |
| 298 | return "value must be greater or equal to zero"; |
| 299 | |
| 300 | *(int *) (conf + offset) = size; |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | char *ngx_conf_set_time_slot(char *conf, int offset, char *value) |
| 305 | { |
| 306 | int size; |
| 307 | |
| 308 | size = atoi(value); |
| 309 | if (size < 0) |
| 310 | return "value must be greater or equal to zero"; |
| 311 | |
| 312 | *(int *) (conf + offset) = size; |
| 313 | return NULL; |
| 314 | } |