nginx-0.0.1-2003-06-03-19:42:58 import
diff --git a/src/core/nginx.c b/src/core/nginx.c
index 820d6e3..295e2a9 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -31,21 +31,25 @@
 int main(int argc, char *const *argv)
 {
     int         i;
-    ngx_str_t   conf_file;
-    ngx_conf_t  conf;
+    ngx_str_t    conf_file;
+    ngx_log_t   *log;
+    ngx_conf_t   conf;
 
     ngx_max_sockets = -1;
 
+#if 0
     ngx_log.fd = STDERR_FILENO;
     ngx_log.log_level = NGX_LOG_INFO;
 
     /* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
+#endif
+    log = ngx_log_init_errlog();
 
-    if (ngx_os_init(&ngx_log) == NGX_ERROR) {
+    if (ngx_os_init(log) == NGX_ERROR) {
         return 1;
     }
 
-    ngx_pool = ngx_create_pool(16 * 1024, &ngx_log);
+    ngx_pool = ngx_create_pool(16 * 1024, log);
     /* */
 
     ngx_max_module = 0;
@@ -72,7 +76,7 @@
 
         conf.ctx = ngx_conf_ctx;
         conf.pool = ngx_pool;
-        conf.log = &ngx_log;
+        conf.log = log;
         conf.module_type = NGX_CORE_MODULE;
         conf.cmd_type = NGX_MAIN_CONF;
 
@@ -83,6 +87,11 @@
             return 1;
         }
 
+#if 0
+        log = (ngx_log_t *) ngx_get_conf(ngx_errlog_module);
+        /* STUB */ log->log_level = NGX_LOG_DEBUG;
+#endif
+
         ngx_init_temp_number();
 
         ngx_io = ngx_os_io;
@@ -95,7 +104,7 @@
             }
         }
 
-        if (ngx_open_listening_sockets(&ngx_log) == NGX_ERROR) {
+        if (ngx_open_listening_sockets(log) == NGX_ERROR) {
             return 1;
         }
 
@@ -103,12 +112,12 @@
 
         /* TODO: fork */
 
-        ngx_pre_thread(&ngx_listening_sockets, ngx_pool, &ngx_log);
+        ngx_pre_thread(&ngx_listening_sockets, ngx_pool, log);
 
         /* TODO: threads */
 
         /* STUB */
-        ngx_worker(&ngx_log);
+        ngx_worker(log);
     }
 
     return 0;
diff --git a/src/core/ngx_alloc.c b/src/core/ngx_alloc.c
index f2608f7..af59a22 100644
--- a/src/core/ngx_alloc.c
+++ b/src/core/ngx_alloc.c
@@ -1,10 +1,6 @@
 
 #include <ngx_config.h>
-
-#include <ngx_errno.h>
-#include <ngx_log.h>
-#include <ngx_string.h>
-#include <ngx_alloc.h>
+#include <ngx_core.h>
 
 
 void *ngx_alloc(size_t size, ngx_log_t *log)
diff --git a/src/core/ngx_alloc.h b/src/core/ngx_alloc.h
index 1d2f2f7..b63f4bd 100644
--- a/src/core/ngx_alloc.h
+++ b/src/core/ngx_alloc.h
@@ -3,8 +3,8 @@
 
 
 #include <ngx_config.h>
+#include <ngx_core.h>
 
-#include <ngx_log.h>
 
 /* NGX_MAX_ALLOC_FROM_POOL should be (PAGE_SIZE - 1), i.e. 4095 on x86.
    On FreeBSD 5.x it allows to use zero copy send.
@@ -18,12 +18,15 @@
 
 
 typedef struct ngx_pool_large_s  ngx_pool_large_t;
+
 struct ngx_pool_large_s {
     ngx_pool_large_t  *next;
     void              *alloc;
 };
 
-typedef struct ngx_pool_s  ngx_pool_t;
+
+typedef struct ngx_pool_s        ngx_pool_t;
+
 struct ngx_pool_s {
     char              *last;
     char              *end;
diff --git a/src/core/ngx_array.c b/src/core/ngx_array.c
index dd91141..8d5cec9 100644
--- a/src/core/ngx_array.c
+++ b/src/core/ngx_array.c
@@ -1,9 +1,6 @@
 
 #include <ngx_config.h>
-
-#include <ngx_alloc.h>
-#include <ngx_string.h>
-#include <ngx_array.h>
+#include <ngx_core.h>
 
 
 ngx_array_t *ngx_create_array(ngx_pool_t *p, int n, size_t size)
diff --git a/src/core/ngx_array.h b/src/core/ngx_array.h
index 22cb3fc..d6443d6 100644
--- a/src/core/ngx_array.h
+++ b/src/core/ngx_array.h
@@ -3,8 +3,8 @@
 
 
 #include <ngx_config.h>
+#include <ngx_core.h>
 
-#include <ngx_alloc.h>
 
 typedef struct {
     void       *elts;
diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c
index 241c7a8..306bf1c 100644
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -1,9 +1,6 @@
 
 #include <ngx_config.h>
-
 #include <ngx_core.h>
-#include <ngx_files.h>
-#include <ngx_conf_file.h>
 
 
 char ngx_conf_errstr[MAX_CONF_ERRSTR];
diff --git a/src/core/ngx_conf_file.h b/src/core/ngx_conf_file.h
index 20747b6..b517ff0 100644
--- a/src/core/ngx_conf_file.h
+++ b/src/core/ngx_conf_file.h
@@ -3,14 +3,7 @@
 
 
 #include <ngx_config.h>
-#include <ngx_files.h>
-#include <ngx_log.h>
-#include <ngx_file.h>
-#include <ngx_time.h>
-#include <ngx_string.h>
-#include <ngx_alloc.h>
-#include <ngx_hunk.h>
-#include <ngx_array.h>
+#include <ngx_core.h>
 
 
 /*
@@ -51,10 +44,6 @@
 extern  char ngx_conf_errstr[MAX_CONF_ERRSTR];
 
 
-typedef struct ngx_conf_s  ngx_conf_t;
-
-
-typedef struct ngx_command_s  ngx_command_t;
 struct ngx_command_s {
     ngx_str_t  name;
     int        type;
@@ -66,14 +55,15 @@
 
 #define ngx_null_command   {ngx_null_string, 0, NULL, 0, 0, NULL}
 
-typedef struct {
+
+struct ngx_module_s {
     int             ctx_index;
     int             index;
     void           *ctx;
     ngx_command_t  *commands;
     int             type;
     int           (*init_module)(ngx_pool_t *p);
-} ngx_module_t;
+};
 
 
 typedef struct {
diff --git a/src/core/ngx_connection.h b/src/core/ngx_connection.h
index c3e9e09..c75fd1f 100644
--- a/src/core/ngx_connection.h
+++ b/src/core/ngx_connection.h
@@ -1,26 +1,10 @@
 #ifndef _NGX_CONNECTION_H_INCLUDED_
 #define _NGX_CONNECTION_H_INCLUDED_
 
+#include <ngx_config.h>
 #include <ngx_core.h>
-
-#if 0
-#include <ngx_socket.h>
-#include <ngx_log.h>
-#include <ngx_alloc.h>
-#include <ngx_hunk.h>
-#include <ngx_array.h>
-#include <ngx_string.h>
-
-#include <ngx_server.h>
-#endif
-
-#if 0
-typedef struct ngx_connection_s  ngx_connection_t;
-#endif
-
-#ifdef NGX_EVENT
 #include <ngx_event.h>
-#endif
+
 
 struct ngx_connection_s {
     ngx_socket_t     fd;
diff --git a/src/core/ngx_core.h b/src/core/ngx_core.h
index a612200..8963d08 100644
--- a/src/core/ngx_core.h
+++ b/src/core/ngx_core.h
@@ -5,19 +5,25 @@
 #include <ngx_types.h>
 #include <ngx_time.h>
 #include <ngx_socket.h>
-#include <ngx_files.h>
 #include <ngx_errno.h>
 #include <ngx_process.h>
 
-typedef struct ngx_connection_s  ngx_connection_t;
+typedef struct ngx_module_s      ngx_module_t;
+typedef struct ngx_conf_s        ngx_conf_t;
+typedef struct ngx_command_s     ngx_command_t;
+
+typedef struct ngx_file_s        ngx_file_t;
 typedef struct ngx_event_s       ngx_event_t;
+typedef struct ngx_connection_s  ngx_connection_t;
 
 #include <ngx_log.h>
 #include <ngx_alloc.h>
+#include <ngx_string.h>
 #include <ngx_hunk.h>
 #include <ngx_array.h>
-#include <ngx_string.h>
+#include <ngx_table.h>
 #include <ngx_file.h>
+#include <ngx_files.h>
 #include <ngx_inet.h>
 #include <ngx_conf_file.h>
 #include <ngx_os_init.h>
diff --git a/src/core/ngx_file.c b/src/core/ngx_file.c
index b85b472..455b785 100644
--- a/src/core/ngx_file.c
+++ b/src/core/ngx_file.c
@@ -1,10 +1,6 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_hunk.h>
-#include <ngx_array.h>
-#include <ngx_file.h>
-#include <ngx_files.h>
 
 
 static int ngx_temp_number;
diff --git a/src/core/ngx_file.h b/src/core/ngx_file.h
index 442f843..6f521c2 100644
--- a/src/core/ngx_file.h
+++ b/src/core/ngx_file.h
@@ -2,11 +2,9 @@
 #define _NGX_FILE_H_INCLUDED_
 
 
-#include <ngx_log.h>
-#include <ngx_alloc.h>
-#include <ngx_string.h>
+#include <ngx_config.h>
+#include <ngx_core.h>
 
-typedef struct ngx_file_s  ngx_file_t;
 
 struct ngx_file_s {
     ngx_fd_t         fd;
diff --git a/src/core/ngx_hunk.c b/src/core/ngx_hunk.c
index 691387f..32a52c9 100644
--- a/src/core/ngx_hunk.c
+++ b/src/core/ngx_hunk.c
@@ -1,6 +1,6 @@
 
 #include <ngx_config.h>
-#include <ngx_hunk.h>
+#include <ngx_core.h>
 
 
 ngx_hunk_t *ngx_create_temp_hunk(ngx_pool_t *pool, int size,
diff --git a/src/core/ngx_hunk.h b/src/core/ngx_hunk.h
index c0e0dbf..b948895 100644
--- a/src/core/ngx_hunk.h
+++ b/src/core/ngx_hunk.h
@@ -1,11 +1,9 @@
-#ifndef _NGX_CHUNK_H_INCLUDED_
-#define _NGX_CHUNK_H_INCLUDED_
+#ifndef _NGX_HUNK_H_INCLUDED_
+#define _NGX_HUNK_H_INCLUDED_
 
 
 #include <ngx_config.h>
-#include <ngx_types.h>
-#include <ngx_file.h>
-#include <ngx_alloc.h>
+#include <ngx_core.h>
 
 
 /* hunk type */
@@ -35,7 +33,8 @@
 
 
 
-typedef struct ngx_hunk_s ngx_hunk_t;
+typedef struct ngx_hunk_s        ngx_hunk_t;
+
 struct ngx_hunk_s {
     char        *pos;
     char        *last;
@@ -53,7 +52,8 @@
 };
 
 
-typedef struct ngx_chain_s  ngx_chain_t;
+typedef struct ngx_chain_s       ngx_chain_t;
+
 struct ngx_chain_s {
     ngx_hunk_t  *hunk;
     ngx_chain_t *next;
@@ -83,4 +83,4 @@
             } while (0);
 
 
-#endif /* _NGX_CHUNK_H_INCLUDED_ */
+#endif /* _NGX_HUNK_H_INCLUDED_ */
diff --git a/src/core/ngx_inet.c b/src/core/ngx_inet.c
index b566e1a..55b8354 100644
--- a/src/core/ngx_inet.c
+++ b/src/core/ngx_inet.c
@@ -1,7 +1,6 @@
 
 #include <ngx_config.h>
-#include <ngx_string.h>
-#include <ngx_inet.h>
+#include <ngx_core.h>
 
 
 /* AF_INET only */
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index aff3998..55807ef 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -14,6 +14,36 @@
 #include <ngx_core.h>
 
 
+static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
+
+
+static ngx_str_t  errlog_name = ngx_string("errlog");
+
+static ngx_command_t  ngx_errlog_commands[] = {
+
+    {ngx_string("error_log"),
+     NGX_MAIN_CONF|NGX_CONF_TAKE1,
+     ngx_set_error_log,
+     0,
+     0,
+     NULL},
+
+    ngx_null_command
+};
+
+
+ngx_module_t  ngx_errlog_module = {
+    NGX_MODULE,
+    &errlog_name,                          /* module context */
+    ngx_errlog_commands,                   /* module directives */
+    NGX_CORE_MODULE,                       /* module type */
+    NULL                                   /* init module */
+};
+
+
+static ngx_log_t  ngx_log;
+
+
 static const char *err_levels[] = {
     "stderr", "emerg", "alert", "crit", "error",
     "warn", "notice", "info", "debug"
@@ -33,6 +63,9 @@
 #if (HAVE_VARIADIC_MACROS)
     va_list   args;
 #endif
+#if (WIN32)
+    int       written;
+#endif
 
     ngx_localtime(&tm);
     len = ngx_snprintf(errstr, sizeof(errstr), "%4d/%02d/%02d %02d:%02d:%02d",
@@ -87,10 +120,15 @@
 
 #if (WIN32)
     errstr[len++] = '\r';
-#endif
     errstr[len++] = '\n';
-
+    if (log->fd) {
+        WriteFile(log->fd, errstr, len, &written, NULL);
+    }
+#else
+    errstr[len++] = '\n';
     write(log->fd, errstr, len);
+#endif
+
 
 #if 0
     errstr[len] = '\0';
@@ -137,6 +175,8 @@
 #endif
 
 
+#if 0
+
 void ngx_log_stderr(ngx_event_t *ev)
 {
     char       errstr[MAX_ERROR_STR];
@@ -165,3 +205,79 @@
         ngx_log_error(NGX_LOG_STDERR, &ngx_log, 0, "%s", errstr);
     }
 }
+
+#endif
+
+
+static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+    return ngx_log_set_errlog(cf, cmd, &ngx_log);
+}
+
+
+
+ngx_log_t *ngx_log_init_errlog()
+{
+#if (WIN32)
+    ngx_log.fd = GetStdHandle(STD_ERROR_HANDLE);
+
+    if (ngx_log.fd == NGX_INVALID_FILE) {
+        /* TODO: where we can log error ? */
+        return NULL;
+
+    } else if (ngx_log.fd == NULL) {
+        /* there are no associated standard handles */
+        /* TODO: where we can log possible errors ? */
+    }
+
+#else
+    ngx_log.fd = STDERR_FILENO;
+#endif
+
+    ngx_log.log_level = NGX_LOG_INFO;
+    /* STUB */ ngx_log.log_level = NGX_LOG_DEBUG;
+
+    return &ngx_log;
+}
+
+
+char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log)
+{
+    int         len;
+    ngx_err_t   err;
+    ngx_str_t  *value;
+
+    value = cf->args->elts;
+
+    log->fd = ngx_open_file(value[1].data,
+                            NGX_FILE_RDWR,
+                            NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND);
+
+    if (log->fd == NGX_INVALID_FILE) {
+        err = ngx_errno;
+        len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
+                          ngx_open_file_n " \"%s\" failed (%d: ",
+                          value[1].data, err);
+        len += ngx_strerror_r(err, ngx_conf_errstr + len,
+                              sizeof(ngx_conf_errstr) - len - 1);
+        ngx_conf_errstr[len++] = ')';
+        ngx_conf_errstr[len++] = '\0';
+        return ngx_conf_errstr;
+    }
+
+#if (WIN32)
+    if (ngx_file_append_mode(log->fd) == NGX_ERROR) {
+        err = ngx_errno;
+        len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1,
+                          ngx_file_append_mode_n " \"%s\" failed (%d: ",
+                          value[1].data, err);
+        len += ngx_strerror_r(err, ngx_conf_errstr + len,
+                              sizeof(ngx_conf_errstr) - len - 1);
+        ngx_conf_errstr[len++] = ')';
+        ngx_conf_errstr[len++] = '\0';
+        return ngx_conf_errstr;
+    }
+#endif
+
+    return NGX_CONF_OK;
+}
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
index b549d80..d1d382c 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -2,8 +2,9 @@
 #define _NGX_LOG_H_INCLUDED_
 
 
-#include <ngx_types.h>
-#include <ngx_errno.h>
+#include <ngx_config.h>
+#include <ngx_core.h>
+
 
 typedef enum {
     NGX_LOG_STDERR = 0,
@@ -160,4 +161,11 @@
 #endif /* VARIADIC MACROS */
 
 
+ngx_log_t *ngx_log_init_errlog();
+char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log);
+
+
+extern ngx_module_t  ngx_errlog_module;
+
+
 #endif /* _NGX_LOG_H_INCLUDED_ */
diff --git a/src/core/ngx_modules.c b/src/core/ngx_modules.c
index 27d150c..b25c413 100644
--- a/src/core/ngx_modules.c
+++ b/src/core/ngx_modules.c
@@ -1,9 +1,9 @@
 
 #include <ngx_config.h>
-
-#include <ngx_conf_file.h>
+#include <ngx_core.h>
 
 
+extern ngx_module_t  ngx_errlog_module;
 extern ngx_module_t  ngx_events_module;
 extern ngx_module_t  ngx_event_module;
 
@@ -42,6 +42,10 @@
 
 ngx_module_t *ngx_modules[] = {
 
+    /* core */
+
+    &ngx_errlog_module,
+
     /* events */
 
     &ngx_events_module,
diff --git a/src/core/ngx_sendfile.c b/src/core/ngx_sendfile.c
deleted file mode 100644
index e930fe5..0000000
--- a/src/core/ngx_sendfile.c
+++ /dev/null
@@ -1,25 +0,0 @@
-
-#include <ngx_config.h>
-
-#if !(HAVE_SENDFILE)
-
-#include <ngx_core.h>
-#include <ngx_log.h>
-#include <ngx_socket.h>
-#include <ngx_sendv.h>
-
-int ngx_sendfile(ngx_socket_t s,
-                 ngx_iovec_t *headers, int hdr_cnt,
-                 ngx_fd_t fd, off_t offset, size_t nbytes,
-                 ngx_iovec_t *trailers, int trl_cnt,
-                 off_t *sent,
-                 ngx_log_t *log)
-{
-    ngx_log_error(NGX_LOG_INFO, log, 0,
-                  "ngx_sendfile: sendfile is not implemented");
-
-
-    return NGX_ERROR;
-}
-
-#endif
diff --git a/src/core/ngx_sendfile.h b/src/core/ngx_sendfile.h
deleted file mode 100644
index 3c211f5..0000000
--- a/src/core/ngx_sendfile.h
+++ /dev/null
@@ -1,23 +0,0 @@
-#ifndef _NGX_SENDFILE_H_INCLUDED_
-#define _NGX_SENDFILE_H_INCLUDED_
-
-
-#include <ngx_config.h>
-#include <ngx_types.h>
-#include <ngx_files.h>
-#include <ngx_socket.h>
-#include <ngx_log.h>
-#include <ngx_connection.h>
-#include <ngx_sendv.h>
-
-int ngx_sendfile(ngx_connection_t *c,
-                 ngx_iovec_t *headers, int hdr_cnt,
-                 ngx_fd_t fd, off_t offset, size_t nbytes,
-                 ngx_iovec_t *trailers, int trl_cnt,
-                 off_t *sent, u_int flags);
-
-
-extern u_int ngx_sendfile_flags;
-
-
-#endif /* _NGX_SENDFILE_H_INCLUDED_ */
diff --git a/src/core/ngx_server.h b/src/core/ngx_server.h
deleted file mode 100644
index 6372b9a..0000000
--- a/src/core/ngx_server.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#ifndef _NGX_SERVER_H_INCLUDED_
-#define _NGX_SERVER_H_INCLUDED_
-
-
-#include <ngx_config.h>
-#include <ngx_types.h>
-#include <ngx_alloc.h>
-
-typedef struct {
-    int          log_level;
-    ngx_pool_t  *pool;
-    int        (*handler)(void *data);
-    int          buff_size;
-} ngx_server_t;
-
-
-#endif /* _NGX_SERVER_H_INCLUDED_ */
diff --git a/src/core/ngx_string.c b/src/core/ngx_string.c
index 56a9861..312e819 100644
--- a/src/core/ngx_string.c
+++ b/src/core/ngx_string.c
@@ -1,7 +1,6 @@
 
 #include <ngx_config.h>
 #include <ngx_core.h>
-#include <ngx_string.h>
 
 
 char *ngx_cpystrn(char *dst, char *src, size_t n)
diff --git a/src/core/ngx_string.h b/src/core/ngx_string.h
index daf5f00..036194b 100644
--- a/src/core/ngx_string.h
+++ b/src/core/ngx_string.h
@@ -3,6 +3,7 @@
 
 
 #include <ngx_config.h>
+#include <ngx_core.h>
 
 
 typedef struct {
diff --git a/src/core/ngx_table.h b/src/core/ngx_table.h
index 9936648..f49ec3a 100644
--- a/src/core/ngx_table.h
+++ b/src/core/ngx_table.h
@@ -3,9 +3,8 @@
 
 
 #include <ngx_config.h>
-#include <ngx_string.h>
-#include <ngx_alloc.h>
-#include <ngx_array.h>
+#include <ngx_core.h>
+
 
 typedef ngx_array_t  ngx_table_t;
 
@@ -14,6 +13,7 @@
     ngx_str_t  value;
 } ngx_table_elt_t;
 
+
 #define ngx_create_table(p, n)  ngx_create_array(p, n, 2 * sizeof(ngx_str_t))
 #define ngx_push_table(t)       ngx_push_array(t)