nginx-0.0.1-2002-12-23-09:29:22 import
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 3d1d11d..17a3d10 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -39,6 +39,9 @@
 
 int main(int argc, char *const *argv)
 {
+    ngx_str_t   conf_file;
+    ngx_conf_t  conf;
+
     /* STUB */
     ngx_log.log_level = NGX_LOG_DEBUG;
 
@@ -53,6 +56,19 @@
 
     /* TODO: read config */
 
+#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);
+    conf.pool = ngx_pool;
+    conf.log = &ngx_log;
+
+    conf_file.len = sizeof("nginx.conf") - 1;
+    conf_file.data = "nginx.conf";
+
+    ngx_conf_parse(&conf, &conf_file);
+#endif
+
     ngx_test_null(ngx_listening_sockets,
                   ngx_create_array(ngx_pool, 10, sizeof(ngx_listen_t)), 1);
 
diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h
index 66199de..f3eb116 100644
--- a/src/core/ngx_config.h
+++ b/src/core/ngx_config.h
@@ -9,6 +9,8 @@
    ngx_inline inline __inline __inline__
 */
 
+/* STUB */
+#undef  FD_SETSIZE
 #define FD_SETSIZE  1024
 
 
@@ -85,6 +87,11 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#ifndef HAVE_POLL
+#define HAVE_POLL  1
+#include <poll.h>
+#endif
+
 #define ngx_inline   inline
 
 
diff --git a/src/core/ngx_config_file.c b/src/core/ngx_config_file.c
index d161c54..80252e8 100644
--- a/src/core/ngx_config_file.c
+++ b/src/core/ngx_config_file.c
@@ -12,9 +12,9 @@
     NGX_CONF_TAKE2
 };
 
-#if 0
+#if 1
 
-int ngx_conf_parse(ngx_conf_t *cf, char *filename)
+int ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
 {
     int    rc;
     char  *error;
@@ -23,22 +23,34 @@
 
     if (filename) {
 
-        fd = ngx_open_file(filename, NGX_FILE_RDONLY);
+        fd = ngx_open_file(filename->data, NGX_FILE_RDONLY);
         if (fd == NGX_INVALID_FILE) {
             ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
-                          "ngx_conf_open: "
-                          ngx_open_file_n " %s failed", filename);
+                          "ngx_conf_file: "
+                          ngx_open_file_n " %s failed", filename->data);
             return NGX_ERROR;
         }
 
-        prev = cf->file;
-        ngx_test_null(cf->file, ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)),
+        prev = cf->conf_file;
+        ngx_test_null(cf->conf_file,
+                      ngx_palloc(cf->pool, sizeof(ngx_conf_file_t)),
                       NGX_ERROR);
 
-        cf->file->fd = fd;
-        cf->file->name = filename;
-        cf->file->line = 1;
-        cf->file->pos = 0;
+        if (ngx_stat_fd(fd, &cf->conf_file->file.info) == -1) {
+            ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
+                          "ngx_conf_file: "
+                          ngx_stat_fd_n " %s failed", filename->data);
+        }
+
+        ngx_test_null(cf->conf_file->hunk,
+                      ngx_create_temp_hunk(cf->pool, 1024, 0, 0),
+                      NGX_ERROR);
+
+        cf->conf_file->file.fd = fd;
+        cf->conf_file->file.name.len = filename->len;
+        cf->conf_file->file.name.data = filename->data;
+        cf->conf_file->file.log = cf->log;;
+        cf->conf_file->line = 1;
     }
 
     for ( ;; ) {
@@ -56,13 +68,14 @@
 
         if (cf->handler) {
 
-            if (*(cf->handler)(cf) == NGX_ERROR) {
+            if ((*cf->handler)(cf) == NGX_ERROR) {
                 return NGX_ERROR;
             }
 
             continue;
         }
 
+#if 0
         cmd = ngx_conf_find_token(cf);
         if (cmd == NULL) {
             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
@@ -71,7 +84,7 @@
             return NGX_ERROR;
         }
 
-        if (cmd->type & argument_number[cf->args->nelts]) {
+        if (cmd->type & argument_number[cf->args->nelts - 1]) {
             error = cmd->set(cf, cmd->offset, cf->args);
 
             if (error) {
@@ -81,6 +94,7 @@
                 return NGX_ERROR;
             }
         }
+#endif
 
 #if 0
         if (cmd->type == NGX_CONF_CONTAINER) {
@@ -133,32 +147,36 @@
                           cf->name, cf->file->name, cf->file->line);
             return NGX_ERROR;
         }
-    }
 #endif
+    }
 
     if (filename) {
-        cf->file = prev;
+        cf->conf_file = prev;
 
         if (ngx_close_file(fd) == NGX_FILE_ERROR) {
             ngx_log_error(NGX_LOG_ERR, cf->log, ngx_errno,
-                          ngx_close_file_n " %s failed", cf->name);
+                          ngx_close_file_n " %s failed",
+                          cf->conf_file->file.name.data);
             return NGX_ERROR;
         }
     }
 
     return NGX_OK;
-}    
+}
 
 #endif
 
-#if 0
+#if 1
 
 int ngx_conf_read_token(ngx_conf_t *cf)
 {
     char        *start, ch, *src, *dst;
-    int          n, need_space, last_space, len, quoted, s_quoted, d_quoted;
+    int          found, need_space, last_space, len, quoted, s_quoted, d_quoted;
+    ssize_t      n;
+    ngx_str_t   *word;
     ngx_hunk_t  *h;
 
+    found = 0;
     need_space = 0;
     last_space = 1;
     len = 0;
@@ -167,14 +185,21 @@
     cf->args->nelts = 0;
     h = cf->conf_file->hunk;
 
-    for (start = h->pos.mem; /* end_of_file */ ; h->pos.mem++) {
+ngx_log_debug(cf->log, "TOKEN START");
+
+    for (start = h->pos.mem; /* void */ ; /* void */) {
 
         if (h->pos.mem >= h->last.mem) {
+            if (cf->conf_file->file.offset
+                                  >= ngx_file_size(cf->conf_file->file.info)) {
+                return NGX_FILE_DONE;
+            }
+
             if (h->pos.mem - start) {
                 ngx_memcpy(h->start, start, h->pos.mem - start);
             }
 
-            n = ngx_read_file(cf->conf_file->file,
+            n = ngx_read_file(&cf->conf_file->file,
                               h->start + (h->pos.mem - start),
                               h->end - (h->start + (h->pos.mem - start)),
                               cf->conf_file->file.offset);
@@ -188,12 +213,30 @@
             h->last.mem = h->pos.mem + n;
         }
 
-        ch = *h->pos.mem;
+        ch = *h->pos.mem++;
+
+ngx_log_debug(cf->log, "%d:%d:%d:%d:%d '%c'" _
+              last_space _ need_space _
+              quoted _ s_quoted _ d_quoted _ ch);
 
         if (ch == LF) {
             cf->conf_file->line++;
         }
 
+        if (need_space) {
+            if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
+                last_space = 1;
+                need_space = 0;
+                continue;
+            }
+
+            if (ch == ';' || ch == '{') {
+                return NGX_OK;
+            }
+
+            return NGX_ERROR;
+        }
+
         if (quoted) {
             quoted = 0;
             continue;
@@ -202,86 +245,93 @@
         len++;
 
         if (last_space) {
-            start = h->pos.mem;
-
             if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
+                len = 0;
+                continue;
+            }
+
+            start = h->pos.mem - 1;
+
+            switch (ch) {
+
+            case '\\':
+                quoted = 1;
+                last_space = 0;
+                continue;
+
+            case '"':
                 start++;
+                len--;
+                d_quoted = 1;
+                last_space = 0;
                 continue;
-            }
-        }
 
-        if (ch = '\\') {
-            quoted = 1;
-            continue;
-        }
-
-        if (d_quoted) {
-
-            if (ch == '"') {
-                d_quoted = 0;
-                need_space = 1;
-                last_space = 1;
+            case '\'':
+                start++;
+                len--;
+                s_quoted = 1;
+                last_space = 0;
                 continue;
-            }
 
-        } else if (s_quoted) {
-
-            if (ch == '\'') {
-                s_quoted = 0;
-                need_space = 1;
-                last_space = 1;
-                continue;
+            default:
+                last_space = 0;
             }
 
         } else {
+            if (ch == '\\') {
+                quoted = 1;
+                continue;
+            }
 
-            if (ch == ' ' || ch == '\t' || ch == CR || ch == LF) {
+            if (d_quoted) {
+                if (ch == '"') {
+                    len--;
+                    d_quoted = 0;
+                    need_space = 1;
+                    found = 1;
+                }
+
+            } else if (s_quoted) {
+                if (ch == '\'') {
+                    len--;
+                    s_quoted = 0;
+                    need_space = 1;
+                    found = 1;
+                }
+
+            } else if (ch == ' ' || ch == '\t' || ch == CR || ch == LF
+                       || ch == ';' || ch == '{') {
+                len--;
+                last_space = 1;
+                found = 1;
+            }
+
+            if (found) {
                 ngx_test_null(word, ngx_push_array(cf->args), NGX_ERROR);
-                ngx_test_null(word->data, ngx_palloc(cf->temp_pool, len + 1),
+                ngx_test_null(word->data, ngx_palloc(cf->pool, len + 1),
                               NGX_ERROR);
                 word->len = len;
 
-                for (dst = word->data, src = start; src < h->pos; /* void */) {
-                    if (*src == '\\')
+                for (dst = word->data, src = start;
+                     src < h->pos.mem - 1;
+                     /* void */)
+                {
+                    if (*src == '\\') {
                         src++;
+                    }
                     *dst++ = *src++;
                 }
                 *dst = '\0';
 
-                need_space = 0;
-                last_space = 1;
-                continue;
-            }
+ngx_log_debug(cf->log, "FOUND %d:'%s'" _ word->len _ word->data);
 
-            if (need_space) {
-                return NGX_ERROR;
-            }
-
-            if (ch == ';') {
-                return NGX_OK;
-            }
-
-            if (ch == '{') {
-                return NGX_OK;
-            }
-
-            if (ch == '}') {
-                return NGX_BLOCK_DONE;
-            }
-
-            if (last_space) {
-                if (ch == '"') {
-                    d_quoted = 1;
-                    continue;
+                if (ch == ';' || ch == '{') {
+                    return NGX_OK;
                 }
 
-                if (ch == '\'') {
-                    s_quoted = 1;
-                    continue;
-                }
+                found = 0;
+                len = 0;
             }
-
-            last_space = 0;
         }
     }
 }
diff --git a/src/core/ngx_config_file.h b/src/core/ngx_config_file.h
index 8b1d03e..341668a 100644
--- a/src/core/ngx_config_file.h
+++ b/src/core/ngx_config_file.h
@@ -19,6 +19,10 @@
 #define NGX_CONF_UNSET    -1
 
 
+#define NGX_BLOCK_DONE     1
+#define NGX_FILE_DONE      2
+
+
 typedef struct {
     ngx_file_t   file;
     ngx_hunk_t  *hunk;
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 244edc3..30e42ad 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -13,6 +13,7 @@
 #include <ngx_config.h>
 #include <ngx_errno.h>
 #include <ngx_time.h>
+#include <ngx_process.h>
 #include <ngx_string.h>
 #include <ngx_log.h>
 
@@ -44,12 +45,9 @@
     len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
                         " [%s] ", err_levels[level]);
 
+    /* pid#tid */
     len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
-#if (WIN32)
-                        "%d#%d: ", 0, 0);
-#else
-                        "%d#%d: ", getpid(), 0);
-#endif
+                        "%d#%d: ", ngx_getpid(), 0);
 
 #if (HAVE_VARIADIC_MACROS)
     va_start(args, fmt);
@@ -60,12 +58,14 @@
 #endif
 
     if (err) {
-        if ((unsigned) err < 0x80000000)
+#if (WIN32)
+        if ((unsigned) err >= 0x80000000)
             len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
-                            " (%d: ", err);
+                                " (%X: ", err);
         else
+#endif
             len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1,
-                            " (%X: ", err);
+                                " (%d: ", err);
 
         len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1);
         if (len < sizeof(errstr) - 2) {
@@ -75,16 +75,26 @@
         }
     }
 
-    if (level != NGX_LOG_DEBUG && log->handler)
+    if (level != NGX_LOG_DEBUG && log->handler) {
         len += log->handler(log->data, errstr + len, sizeof(errstr) - len - 1);
+    }
 
-    if (len > sizeof(errstr) - 2)
+    if (len > sizeof(errstr) - 2) {
         len = sizeof(errstr) - 2;
-    errstr[len] = '\n';
-    errstr[len + 1] = '\0';
+    }
 
+#if (WIN32)
+    errstr[len++] = '\r';
+#endif
+    errstr[len++] = '\n';
+
+    write(2, errstr, len);
+
+#if 0
+    errstr[len] = '\0';
     fputs(errstr, stderr);
     fflush(stderr);
+#endif
 }
 
 #if !(HAVE_VARIADIC_MACROS)