nginx-0.0.1-2003-01-09-08:36:00 import
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 0a0233f..ead1453 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -36,7 +36,7 @@
 
 int ngx_connection_counter;
 
-ngx_array_t  *ngx_listening_sockets;
+ngx_array_t  ngx_listening_sockets;
 
 
 int main(int argc, char *const *argv)
@@ -56,9 +56,9 @@
 
     ngx_init_sockets(&ngx_log);
 
-    /* TODO: read config */
+    ngx_init_array(ngx_listening_sockets, ngx_pool, 10, sizeof(ngx_listen_t),
+                   1);
 
-#if 1
     ngx_memzero(&conf, sizeof(ngx_conf_t));
     ngx_test_null(conf.args,
                   ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1);
@@ -69,16 +69,16 @@
     conf_file.len = sizeof("nginx.conf") - 1;
     conf_file.data = "nginx.conf";
 
-    ngx_conf_parse(&conf, &conf_file);
-#endif
+    if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) {
+        exit(1);
+    }
 
-    ngx_test_null(ngx_listening_sockets,
-                  ngx_create_array(ngx_pool, 10, sizeof(ngx_listen_t)), 1);
-
+#if 0
     /* STUB */
     /* TODO: init chain of global modules (like ngx_http.c),
        they would init its modules and ngx_listening_sockets */
     ngx_http_init(ngx_pool, &ngx_log);
+#endif
 
     ngx_open_listening_sockets(&ngx_log);
 
@@ -86,7 +86,7 @@
 
     /* TODO: fork */
 
-    ngx_pre_thread(ngx_listening_sockets, ngx_pool, &ngx_log);
+    ngx_pre_thread(&ngx_listening_sockets, ngx_pool, &ngx_log);
 
     /* TODO: threads */
 
@@ -125,10 +125,10 @@
          failed = 0;
 
         /* for each listening socket */
-        ls = (ngx_listen_t *) ngx_listening_sockets->elts;
-        for (i = 0; i < ngx_listening_sockets->nelts; i++) {
+        ls = (ngx_listen_t *) ngx_listening_sockets.elts;
+        for (i = 0; i < ngx_listening_sockets.nelts; i++) {
 
-            if (ls[i].done)
+            if (ls[i].bound)
                 continue;
 
             if (ls[i].inherited) {
@@ -137,7 +137,7 @@
                 /* TODO: nonblocking */
                 /* TODO: deferred accept */
 
-                ls[i].done = 1;
+                ls[i].bound = 1;
                 continue;
             }
 
@@ -194,7 +194,7 @@
             /* TODO: deferred accept */
 
             ls[i].fd = s;
-            ls[i].done = 1;
+            ls[i].bound = 1;
         }
 
         if (!failed)
diff --git a/src/core/ngx_alloc.h b/src/core/ngx_alloc.h
index 1d1ad84..7fef65b 100644
--- a/src/core/ngx_alloc.h
+++ b/src/core/ngx_alloc.h
@@ -10,7 +10,7 @@
 #define NGX_MAX_ALLOC_FROM_POOL (8192 - sizeof(ngx_pool_t))
 #define NGX_DEFAULT_POOL_SIZE   (16 * 1024)
 
-#define ngx_test_null(p, alloc, rc)  if ((p = alloc) == NULL) return rc
+#define ngx_test_null(p, alloc, rc)  if ((p = alloc) == NULL) { return rc; }
 
 
 typedef struct ngx_pool_large_s  ngx_pool_large_t;
diff --git a/src/core/ngx_array.c b/src/core/ngx_array.c
index 08ad588..0043929 100644
--- a/src/core/ngx_array.c
+++ b/src/core/ngx_array.c
@@ -2,19 +2,17 @@
 #include <ngx_config.h>
 
 #include <ngx_alloc.h>
+#include <ngx_string.h>
 #include <ngx_array.h>
 
+
 ngx_array_t *ngx_create_array(ngx_pool_t *p, int n, size_t size)
 {
     ngx_array_t *a;
 
-    a = ngx_palloc(p, sizeof(ngx_array_t));
-    if (a == NULL)
-        return NULL;
+    ngx_test_null(a, ngx_palloc(p, sizeof(ngx_array_t)), NULL);
 
-    a->elts = ngx_palloc(p, n * size);
-    if (a->elts == NULL)
-        return NULL;
+    ngx_test_null(a->elts, ngx_palloc(p, n * size), NULL);
 
     a->pool = p;
     a->nelts = 0;
@@ -24,24 +22,31 @@
     return a;
 }
 
+
 void ngx_destroy_array(ngx_array_t *a)
 {
-    ngx_pool_t *p = a->pool;
+    ngx_pool_t  *p;
 
-    if (a->elts + a->size * a->nalloc == p->last)
+    p = a->pool;
+
+    if (a->elts + a->size * a->nalloc == p->last) {
         p->last -= a->size * a->nalloc;
+    }
 
-    if ((char *) a + sizeof(ngx_array_t) == p->last)
+    if ((char *) a + sizeof(ngx_array_t) == p->last) {
         p->last = (char *) a;
+    }
 }
 
+
 void *ngx_push_array(ngx_array_t *a)
 {
-    void *elt;
+    void        *elt, *new;
+    ngx_pool_t  *p;
 
     /* array is full */
     if (a->nelts == a->nalloc) {
-        ngx_pool_t *p = a->pool;
+        p = a->pool;
 
         /* array allocation is the last in the pool */
         if (a->elts + a->size * a->nelts == p->last
@@ -52,11 +57,9 @@
 
         /* allocate new array */
         } else {
-            void *new = ngx_palloc(p, 2 * a->nalloc * a->size);
-            if (new == NULL)
-                return NULL;
+            ngx_test_null(new, ngx_palloc(p, 2 * a->nalloc * a->size), NULL);
 
-            memcpy(new, a->elts, a->nalloc * a->size);
+            ngx_memcpy(new, a->elts, a->nalloc * a->size);
             a->elts = new;
             a->nalloc *= 2;
         }
diff --git a/src/core/ngx_array.h b/src/core/ngx_array.h
index d110b76..22c7c4b 100644
--- a/src/core/ngx_array.h
+++ b/src/core/ngx_array.h
@@ -20,4 +20,9 @@
 void *ngx_push_array(ngx_array_t *a);
 
 
+#define ngx_init_array(a, p, n, s, rc)                                       \
+    ngx_test_null(a.elts, ngx_palloc(p, n * s), rc);                         \
+    a.nelts = 0; a.size = s; a.nalloc = n; a.pool = p;
+
+
 #endif /* _NGX_ARRAY_H_INCLUDED_ */
diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c
index 6b77121..70cb551 100644
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -13,10 +13,11 @@
 static int ngx_conf_read_token(ngx_conf_t *cf);
 
 
-int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
+char *ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
 {
-    int               rc, i;
-    char             *error;
+    int               i, rc, found;
+    char             *rv;
+    void             *conf, **pconf;
     ngx_str_t        *name;
     ngx_fd_t          fd;
     ngx_conf_file_t  *prev;
@@ -29,13 +30,13 @@
             ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
                           "ngx_conf_file: "
                           ngx_open_file_n " %s failed", filename->data);
-            return NGX_ERROR;
+            return NGX_CONF_ERROR;
         }
 
         prev = cf->conf_file;
         ngx_test_null(cf->conf_file,
                       ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)),
-                      NGX_ERROR);
+                      NGX_CONF_ERROR);
 
         if (ngx_stat_fd(fd, &cf->conf_file->file.info) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
@@ -45,7 +46,7 @@
 
         ngx_test_null(cf->conf_file->hunk,
                       ngx_create_temp_hunk(cf->pool, 1024, 0, 0),
-                      NGX_ERROR);
+                      NGX_CONF_ERROR);
 
         cf->conf_file->file.fd = fd;
         cf->conf_file->file.name.len = filename->len;
@@ -59,22 +60,29 @@
 
         /* NGX_OK, NGX_ERROR, NGX_CONF_FILE_DONE, NGX_CONF_BLOCK_DONE */
 
-        if (rc == NGX_ERROR || rc == NGX_CONF_FILE_DONE) {
-            return rc;
+ngx_log_debug(cf->log, "token %d" _ rc);
+
+        if (rc == NGX_ERROR) {
+            return NGX_CONF_ERROR;
+        }
+
+        if (rc != NGX_OK) {
+            return NGX_CONF_OK;
         }
 
         if (cf->handler) {
 
-            if ((*cf->handler)(cf) == NGX_ERROR) {
-                return NGX_ERROR;
+            if ((*cf->handler)(cf) == NGX_CONF_ERROR) {
+                return NGX_CONF_ERROR;
             }
 
             continue;
         }
 
         name = (ngx_str_t *) cf->args->elts;
+        found = 0;
 
-        for (i = 0; ngx_modules[i]; i++) {
+        for (i = 0; !found && ngx_modules[i]; i++) {
             if (ngx_modules[i]->type != NULL
                 && ngx_modules[i]->type != cf->type)
             {
@@ -93,86 +101,56 @@
 
 ngx_log_debug(cf->log, "command '%s'" _ cmd->name.data);
 
-                    cmd->set(cf, cmd, NULL);
+                    if (!(cmd->type & argument_number[cf->args->nelts - 1])) {
+                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                      "invalid number arguments in "
+                                      "directive \"%s\" in %s:%d",
+                                      name->data,
+                                      cf->conf_file->file.name.data,
+                                      cf->conf_file->line);
+                        return NGX_CONF_ERROR;
+                    }
+
+                    conf = NULL;
+                    if (cf->ctx) {
+                        pconf = *(void **) ((char *) cf->ctx + cmd->conf);
+
+                        if (pconf) {
+                            conf = pconf[ngx_modules[i]->index];
+                        }
+                    }
+
+                    rv = cmd->set(cf, cmd, conf);
+
+ngx_log_debug(cf->log, "rv: %d" _ rv);
+
+                    if (rv == NGX_CONF_OK) {
+                        found = 1;
+                        break;
+
+                    } else if (rv == NGX_CONF_ERROR) {
+                        return NGX_CONF_ERROR;
+
+                    } else {
+                        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+                                     "%s", rv);
+                        return NGX_CONF_ERROR;
+                    }
                 }
 
                 cmd++;
             }
-       }
+        }
 
-#if 0
-        cmd = ngx_conf_find_token(cf);
-        if (cmd == NULL) {
+        if (!found) {
             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
                           "unknown directive \"%s\" in %s:%d",
-                          cf->name, cf->file->name, cf->file->line);
-            return NGX_ERROR;
+                          name->data,
+                          cf->conf_file->file.name.data,
+                          cf->conf_file->line);
+
+            return NGX_CONF_ERROR;
         }
-
-        if (cmd->type & argument_number[cf->args->nelts - 1]) {
-            error = cmd->set(cf, cmd->offset, cf->args);
-
-            if (error) {
-                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                               "%s in directive \"%s\" in %s:%d",
-                               error, cf->name, cf->file->name, cf->file->line);
-                return NGX_ERROR;
-            }
-        }
-#endif
-
-#if 0
-        if (cmd->type == NGX_CONF_CONTAINER) {
-            ngx_conf_parse(cf, cmd->container, NULL);
-
-        } else if (cmd->type == NGX_CONF_FLAG) {
-
-            if (cf->args->nelts != 1) {
-                ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                              "invalid number of arguments "
-                              "in directive \"%s\" in %s:%d",
-                              cf->name, cf->file->name, cf->file->line);
-                return NGX_ERROR;
-            }
-
-            if (ngx_strcasecmp(cf->args->elts[0], "on") == 0) {
-                flag = 1;
-
-            } else if (ngx_strcasecmp(cf->args->elts[0], "off") == 0) {
-                flag = 0;
-
-            } else {
-                ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                              "invalid flag in directive \"%s\" in %s:%d",
-                              cf->name, cf->file->name, cf->file->line);
-                return NGX_ERROR;
-            }
-
-            rv = cmd->set(cf, cmd->offset, flag);
-            if (rv) {
-                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                               "%s in directive \"%s\" in %s:%d",
-                               rv, cf->name, cf->file->name, cf->file->line);
-                return NGX_ERROR;
-            }
-
-        } else if (cmd->type & argument_number[args->nelts]) {
-            rv = cmd->set(cf, cmd->offset, cf->args);
-            if (rv) {
-                 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                               "%s in directive \"%s\" in %s:%d",
-                               rv, cf->name, cf->file->name, cf->file->line);
-                return NGX_ERROR;
-            }
-
-        } else {
-            ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
-                          "invalid number of arguments "
-                          "in directive \"%s\" in %s:%d",
-                          cf->name, cf->file->name, cf->file->line);
-            return NGX_ERROR;
-        }
-#endif
     }
 
     if (filename) {
@@ -182,11 +160,11 @@
             ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno,
                           ngx_close_file_n " %s failed",
                           cf->conf_file->file.name.data);
-            return NGX_ERROR;
+            return NGX_CONF_ERROR;
         }
     }
 
-    return NGX_OK;
+    return NGX_CONF_OK;
 }
 
 
@@ -391,6 +369,20 @@
 }
 
 
+char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
+{
+    ngx_str_t  *field, *value;
+
+    field = (ngx_str_t *) conf + cmd->offset;
+    value = (ngx_str_t *) cf->args->elts;
+
+    field->len = value->len;
+    field->data = value->data;
+
+    return NGX_CONF_OK;
+}
+
+
 char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf)
 {
     int         size;
@@ -405,7 +397,7 @@
 
     *(int *) (conf + cmd->offset) = size;
 
-    return NULL;
+    return NGX_CONF_OK;
 }
 
 
@@ -423,5 +415,5 @@
 
     *(int *) (conf + cmd->offset) = size;
 
-    return NULL;
+    return NGX_CONF_OK;
 }
diff --git a/src/core/ngx_conf_file.h b/src/core/ngx_conf_file.h
index cab61a3..77f6b40 100644
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -15,15 +15,16 @@
 #define NGX_CONF_NOARGS      1
 #define NGX_CONF_TAKE1       2
 #define NGX_CONF_TAKE2       4
-#define NGX_CONF_ARGS_NUMBER 0x0ffff
-#define NGX_CONF_ANY         0x10000
-#define NGX_CONF_BLOCK       0x20000
+#define NGX_CONF_ARGS_NUMBER 0x00ffff
+#define NGX_CONF_ANY         0x010000
+#define NGX_CONF_BLOCK       0x020000
 
 
 #define NGX_CONF_UNSET       -1
 
 
-#define NGX_CONF_ERROR       (char *) -1
+#define NGX_CONF_OK          NULL
+#define NGX_CONF_ERROR       (void *) -1
 
 #define NGX_CONF_BLOCK_DONE  1
 #define NGX_CONF_FILE_DONE   2
@@ -46,6 +47,7 @@
 
 
 typedef struct {
+    int             index;
     void           *ctx;
     ngx_command_t  *commands;
     int             type;
@@ -70,13 +72,21 @@
 
     void             *ctx;
     int               type;
-    int             (*handler)(ngx_conf_t *cf);
+    char           *(*handler)(ngx_conf_t *cf);
 };
 
 
-int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename);
+#define ngx_conf_merge(conf, prev, default)                                  \
+    if (conf == NGX_CONF_UNSET) {                                            \
+        conf = (prev == NGX_CONF_UNSET) ? default : prev;                    \
+    }
 
 
+
+char *ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename);
+
+
+char *ngx_conf_set_str_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 char *ngx_conf_set_size_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 char *ngx_conf_set_time_slot(ngx_conf_t *cf, ngx_command_t *cmd, char *conf);
 
diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h
index 1c19aa3..a94640d 100644
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -30,16 +30,18 @@
 
     off_t             sent;
 
-    ngx_log_t        *log;
     int             (*handler)(ngx_connection_t *c);
-    ngx_server_t     *server;
+    void             *ctx;
     ngx_server_t     *servers;
+
     ngx_pool_t       *pool;
+    ngx_log_t        *log;
 
     int               family;
     struct sockaddr  *sockaddr;
     socklen_t         socklen;
-    size_t            addr;
+    int               addr;
+    int               addr_text_max_len;
     ngx_str_t         addr_text;
 
     ngx_hunk_t       *buffer;
diff --git a/src/core/ngx_listen.h b/src/core/ngx_listen.h
index cf4a2ad..91f39ad 100644
--- a/src/core/ngx_listen.h
+++ b/src/core/ngx_listen.h
@@ -9,36 +9,44 @@
 #include <ngx_connection.h>
 
 typedef struct {
-    ngx_socket_t  fd;
+    ngx_socket_t      fd;
 
     struct sockaddr  *sockaddr;
-    socklen_t         socklen;
-    size_t            addr;
+    socklen_t         socklen;    /* size of sockaddr */
+    int               addr;       /* offset to address in sockaddr */
+    int               addr_text_max_len;
     ngx_str_t         addr_text;
 
-    int           family;
-    int           type;
-    int           protocol;
-    int           flags;
+    int               family;
+    int               type;
+    int               protocol;
+    int               flags;      /* Winsock2 flags */
 
-    ngx_log_t    *log;
-    void         *server;
-    int         (*handler)(ngx_connection_t *c);
+    int             (*handler)(ngx_connection_t *c); /* handler of accepted
+                                                        connection */
+    void             *ctx;        /* ngx_http_conf_ctx_t, for example */
+    void             *servers;    /* array of ngx_http_in_addr_t, for example */
 
-    int           backlog;
-    time_t        post_accept_timeout;
+    ngx_log_t        *log;
 
-    unsigned      done:1;
-    unsigned      inherited:1;
-    unsigned      nonblocking:1;
+    int               backlog;
+    time_t            post_accept_timeout;  /* should be here because
+                                               of the deferred accept */
+
+    unsigned          bound:1;       /* already bound */
+    unsigned          inherited:1;   /* inherited from previous process */
+    unsigned          nonblocking:1;
 #if 0
-    unsigned      overlapped:1;
+    unsigned          overlapped:1;  /* Winsock2 overlapped */
 #endif
-    unsigned      shared:1;          /* shared between threads or processes */
+    unsigned          shared:1;    /* shared between threads or processes */
 #if (HAVE_DEFERRED_ACCEPT)
-    unsigned      deferred_accept:1;
+    unsigned          deferred_accept:1;
 #endif
 } ngx_listen_t;
 
 
+extern ngx_array_t ngx_listening_sockets;
+
+
 #endif /* _NGX_LISTEN_H_INCLUDED_ */
diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h
index 9ee3e5e..503ffad 100644
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -18,7 +18,8 @@
 
 #define ngx_memzero               ZeroMemory
 
-#define strcasecmp                stricmp
+#define ngx_strcasecmp            stricmp
+#define ngx_strncmp               strncmp
 #define ngx_strcmp                strcmp
 
 #define ngx_snprintf              _snprintf
@@ -28,6 +29,7 @@
 
 #define ngx_memzero               bzero
 
+#define ngx_strncmp               strncmp
 #define ngx_strcmp                strcmp
 
 #define ngx_snprintf              snprintf