From 00a23ee330d4015cc2295cbb648cec8bfbcfdaaf Mon Sep 17 00:00:00 2001
From: rojer <rojer@rojer.me>
Date: Sat, 13 Feb 2016 10:31:34 +0100
Subject: [PATCH] Good bye osdep.h, you won't be missed

    PUBLISHED_FROM=f08a8324dfde5f9e22eee0e5e4dbae0e3d45cb3f
---
 examples/ESP8266_RTOS/Makefile         |   2 +-
 examples/ESP8266_RTOS/user/Makefile    |   9 +-
 examples/ESP8266_RTOS/user/esp_libc.c  |   7 +-
 examples/ESP8266_RTOS/user/user_main.c |   9 +-
 mongoose.c                             |  50 +--
 mongoose.h                             | 485 +++++++++++++++++++------
 6 files changed, 419 insertions(+), 143 deletions(-)

diff --git a/examples/ESP8266_RTOS/Makefile b/examples/ESP8266_RTOS/Makefile
index 0317ff878..592c54e93 100644
--- a/examples/ESP8266_RTOS/Makefile
+++ b/examples/ESP8266_RTOS/Makefile
@@ -55,7 +55,7 @@ LINKFLAGS_eagle.app.v6 = \
     -u call_user_start	\
 	-Wl,-static						\
 	-Wl,--start-group					\
-	-lcirom \
+	-lc \
 	-lgcc					\
 	-lhal					\
 	-lphy	\
diff --git a/examples/ESP8266_RTOS/user/Makefile b/examples/ESP8266_RTOS/user/Makefile
index e005b7096..703f6265c 100644
--- a/examples/ESP8266_RTOS/user/Makefile
+++ b/examples/ESP8266_RTOS/user/Makefile
@@ -23,11 +23,14 @@ endif
 #   makefile at its root level - these are then overridden
 #   for a subtree within the makefile rooted therein
 #
-DEFINES += -DMG_DISABLE_DAV -DMG_DISABLE_SYNC_RESOLVER -DMG_DISABLE_CGI \
-           -DMG_DISABLE_SOCKETPAIR -DMG_DISABLE_DIRECTORY_LISTING \
+DEFINES += -DMG_LOCALS \
+           -DMG_NO_BSD_SOCKETS \
+           -DMG_DISABLE_FILESYSTEM \
+					 -DMG_DISABLE_STDIO \
            -DMG_MAX_HTTP_HEADERS=20 -DMG_MAX_HTTP_REQUEST_SIZE=1024 \
            -DMG_MAX_PATH=40 -DMG_MAX_HTTP_SEND_IOBUF=1024 \
-           -DMG_ESP8266 -DRTOS_SDK -DMG_LWIP
+           -DRTOS_SDK -DMG_LWIP -DLWIP_TIMEVAL_PRIVATE=0 \
+					 -DMG_INTERNAL=
 
 #############################################################
 # Recursion Magic - Don't touch this!!
diff --git a/examples/ESP8266_RTOS/user/esp_libc.c b/examples/ESP8266_RTOS/user/esp_libc.c
index a48893f0b..6eedddcff 100644
--- a/examples/ESP8266_RTOS/user/esp_libc.c
+++ b/examples/ESP8266_RTOS/user/esp_libc.c
@@ -16,6 +16,7 @@
 #include "esp_common.h"
 
 /* Makes fprintf(stdout) and stderr work. */
+/*
 _ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
   if (fd == 1 || fd == 2) {
     size_t i;
@@ -26,6 +27,7 @@ _ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
   }
   return -1;
 }
+*/
 
 /*
  * You'll need to implement _open_r and friends if you want file operations. See
@@ -42,7 +44,6 @@ void abort(void) {
 }
 
 void _exit(int status) {
-  printf("_exit(%d)\n", status);
   abort();
 }
 
@@ -52,3 +53,7 @@ int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
   tp->tv_usec = time % 1000000;
   return 0;
 }
+
+long int random(void) {
+  return os_random();
+}
diff --git a/examples/ESP8266_RTOS/user/user_main.c b/examples/ESP8266_RTOS/user/user_main.c
index e52e4784b..d7dbcb4b1 100644
--- a/examples/ESP8266_RTOS/user/user_main.c
+++ b/examples/ESP8266_RTOS/user/user_main.c
@@ -37,6 +37,7 @@ void ev_handler(struct mg_connection *nc, int ev, void *p) {
     case MG_EV_HTTP_REQUEST: {
       char addr[32];
       struct http_message *hm = (struct http_message *) p;
+      (void) hm;
       mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
                           MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
       LOG(LL_INFO,
@@ -100,9 +101,13 @@ xTaskHandle s_mg_task_handle;
 void user_init(void) {
   uart_div_modify(0, UART_CLK_FREQ / 115200);
 
-  setvbuf(stdout, NULL, _IONBF, 0);
-  setvbuf(stderr, NULL, _IONBF, 0);
+//  setvbuf(stdout, NULL, _IONBF, 0);
+//  setvbuf(stderr, NULL, _IONBF, 0);
 
   xTaskCreate(mg_task, (const signed char *) "mongoose", MG_TASK_STACK_SIZE,
               NULL, MG_TASK_PRIORITY, &s_mg_task_handle);
 }
+
+void call_user_start(void) {
+  user_init();
+}
diff --git a/mongoose.c b/mongoose.c
index 949d3194f..878653669 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -76,8 +76,7 @@ MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa,
                                  int *proto, char *host, size_t host_len);
 MG_INTERNAL void mg_call(struct mg_connection *nc,
                          mg_event_handler_t ev_handler, int ev, void *ev_data);
-MG_INTERNAL void mg_forward(struct mg_connection *from,
-                            struct mg_connection *to);
+void mg_forward(struct mg_connection *from, struct mg_connection *to);
 MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c);
 MG_INTERNAL void mg_remove_conn(struct mg_connection *c);
 MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max);
@@ -251,6 +250,7 @@ void cs_base64_encode(const unsigned char *src, int src_len, char *dst) {
 #undef BASE64_OUT
 #undef BASE64_FLUSH
 
+#ifndef CS_DISABLE_STDIO
 #define BASE64_OUT(ch)      \
   do {                      \
     fprintf(f, "%c", (ch)); \
@@ -265,6 +265,7 @@ void cs_fprint_base64(FILE *f, const unsigned char *src, int src_len) {
 
 #undef BASE64_OUT
 #undef BASE64_FLUSH
+#endif /* !CS_DISABLE_STDIO */
 
 /* Convert one byte of encoded base64 input stream to 6-bit chunk */
 static unsigned char from_b64(unsigned char ch) {
@@ -346,6 +347,7 @@ enum cs_log_level s_cs_log_level =
     LL_ERROR;
 #endif
 
+#ifndef CS_DISABLE_STDIO
 void cs_log_printf(const char *fmt, ...) {
   va_list ap;
   va_start(ap, fmt);
@@ -354,6 +356,7 @@ void cs_log_printf(const char *fmt, ...) {
   fputc('\n', stderr);
   fflush(stderr);
 }
+#endif /* !CS_DISABLE_STDIO */
 
 void cs_log_set_level(enum cs_log_level level) {
   s_cs_log_level = level;
@@ -368,7 +371,6 @@ void cs_log_set_level(enum cs_log_level level) {
 
 #ifndef EXCLUDE_COMMON
 
-/* Amalgamated: #include "common/osdep.h" */
 /* Amalgamated: #include "common/cs_dirent.h" */
 
 /*
@@ -1630,7 +1632,7 @@ void cs_hmac_sha1(const unsigned char *key, size_t keylen,
 
 #ifndef EXCLUDE_COMMON
 
-/* Amalgamated: #include "common/osdep.h" */
+/* Amalgamated: #include "common/platform.h" */
 /* Amalgamated: #include "common/str_util.h" */
 
 #ifdef _MG_PROVIDE_STRNLEN
@@ -2038,7 +2040,7 @@ void mg_mgr_init(struct mg_mgr *m, void *user_data) {
     WSADATA data;
     WSAStartup(MAKEWORD(2, 2), &data);
   }
-#elif !defined(AVR_LIBC) && !defined(MG_ESP8266)
+#elif defined(__unix__)
   /* Ignore SIGPIPE signal, so if client cancels the request, it
    * won't kill the whole process. */
   signal(SIGPIPE, SIG_IGN);
@@ -3984,7 +3986,9 @@ int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) {
 enum http_proto_data_type { DATA_NONE, DATA_FILE, DATA_PUT, DATA_CGI };
 
 struct proto_data_http {
-  FILE *fp;         /* Opened file. */
+#ifndef MG_DISABLE_FILESYSTEM
+  FILE *fp; /* Opened file. */
+#endif
   int64_t cl;       /* Content-Length. How many bytes to send. */
   int64_t sent;     /* How many bytes have been already sent. */
   int64_t body_len; /* How many bytes of chunked body was reassembled. */
@@ -4521,17 +4525,18 @@ static void ws_handshake(struct mg_connection *nc, const struct mg_str *key) {
 static void free_http_proto_data(struct mg_connection *nc) {
   struct proto_data_http *dp = (struct proto_data_http *) nc->proto_data;
   if (dp != NULL) {
-    if (dp->fp != NULL) {
-      fclose(dp->fp);
-    }
-    if (dp->cgi_nc != NULL) {
-      dp->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
-    }
+#ifndef MG_DISABLE_FILESYSTEM
+    if (dp->fp != NULL) fclose(dp->fp);
+#endif
+#ifndef MG_DISABLE_CGI
+    if (dp->cgi_nc != NULL) dp->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY;
+#endif
     MG_FREE(dp);
     nc->proto_data = NULL;
   }
 }
 
+#ifndef MG_DISABLE_FILESYSTEM
 static void transfer_file_data(struct mg_connection *nc) {
   struct proto_data_http *dp = (struct proto_data_http *) nc->proto_data;
   char buf[MG_MAX_HTTP_SEND_IOBUF];
@@ -4583,6 +4588,7 @@ static void transfer_file_data(struct mg_connection *nc) {
     }
   }
 }
+#endif /* MG_DISABLE_FILESYSTEM */
 
 /*
  * Parse chunked-encoded buffer. Return 0 if the buffer is not encoded, or
@@ -4689,7 +4695,7 @@ MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc,
  * If a big structure is declared in a big function, lx106 gcc will make it
  * even bigger (round up to 4k, from 700 bytes of actual size).
  */
-#ifdef MG_ESP8266
+#ifdef __xtensa__
 static void http_handler2(struct mg_connection *nc, int ev, void *ev_data,
                           struct http_message *hm) __attribute__((noinline));
 
@@ -4700,11 +4706,11 @@ void http_handler(struct mg_connection *nc, int ev, void *ev_data) {
 
 static void http_handler2(struct mg_connection *nc, int ev, void *ev_data,
                           struct http_message *hm) {
-#else
+#else  /* !__XTENSA__ */
 void http_handler(struct mg_connection *nc, int ev, void *ev_data) {
   struct http_message shm;
   struct http_message *hm = &shm;
-#endif
+#endif /* __XTENSA__ */
   struct mbuf *io = &nc->recv_mbuf;
   int req_len;
   const int is_req = (nc->listener != NULL);
@@ -4725,9 +4731,11 @@ void http_handler(struct mg_connection *nc, int ev, void *ev_data) {
     free_http_proto_data(nc);
   }
 
+#ifndef MG_DISABLE_FILESYSTEM
   if (nc->proto_data != NULL) {
     transfer_file_data(nc);
   }
+#endif
 
   mg_call(nc, nc->handler, ev, ev_data);
 
@@ -6273,10 +6281,6 @@ static pid_t start_process(const char *interp, const char *cmd, const char *env,
   }
   to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd));
 
-#if 0
-  printf("[%ls] [%ls]\n", full_dir, wcmd);
-#endif
-
   if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP,
                      (void *) env, full_dir, &si, &pi) != 0) {
     spawn_stdio_thread(sock, a[1], push_to_stdin);
@@ -7113,7 +7117,7 @@ void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len,
     if (inet_ntop(sa->sa.sa_family, addr, start, capacity) == NULL) {
       *buf = '\0';
     }
-#elif defined(_WIN32) || defined(MG_ESP8266)
+#elif defined(_WIN32) || defined(MG_LWIP)
     /* Only Windoze Vista (and newer) have inet_ntop() */
     strncpy(buf, inet_ntoa(sa->sin.sin_addr), len);
 #else
@@ -7199,9 +7203,10 @@ int mg_avprintf(char **buf, size_t size, const char *fmt, va_list ap) {
   return len;
 }
 
-#if !defined(NO_LIBC) && !defined(MG_DISABLE_HEXDUMP)
+#if !defined(MG_DISABLE_HEXDUMP)
 void mg_hexdump_connection(struct mg_connection *nc, const char *path,
                            const void *buf, int num_bytes, int ev) {
+#if !defined(NO_LIBC) && !defined(MG_DISABLE_STDIO)
   FILE *fp = NULL;
   char *hexbuf, src[60], dst[60];
   int buf_size = num_bytes * 5 + 100;
@@ -7236,6 +7241,7 @@ void mg_hexdump_connection(struct mg_connection *nc, const char *path,
     MG_FREE(hexbuf);
   }
   if (fp != stdin && fp != stdout) fclose(fp);
+#endif
 }
 #endif
 
@@ -7554,7 +7560,7 @@ static int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) {
       var_len = 2;
       break;
     default:
-      printf("TODO: UNHANDLED COMMAND %d\n", cmd);
+      /* Unhandled command */
       break;
   }
 
diff --git a/mongoose.h b/mongoose.h
index 2290e64bd..3da51fd69 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -20,6 +20,9 @@
  * license, as set out in <https://www.cesanta.com/license>.
  */
 
+#ifndef _MG_COMMON_H_
+#define _MG_COMMON_H_
+
 #define MG_VERSION "6.2"
 
 /* Local tweaks, applied before any of Mongoose's own headers. */
@@ -30,53 +33,89 @@
 #if defined(MG_ENABLE_DEBUG) && !defined(CS_ENABLE_DEBUG)
 #define CS_ENABLE_DEBUG
 #endif
+#if defined(MG_DISABLE_STDIO) && !defined(CS_DISABLE_STDIO)
+#define CS_DISABLE_STDIO
+#endif
+
+/* All of the below features depend on filesystem access, disable them. */
+#ifdef MG_DISABLE_FILESYSTEM
+#ifndef MG_DISABLE_DAV
+#define MG_DISABLE_DAV
+#endif
+#ifndef MG_DISABLE_CGI
+#define MG_DISABLE_CGI
+#endif
+#ifndef MG_DISABLE_DIRECTORY_LISTING
+#define MG_DISABLE_DIRECTORY_LISTING
+#endif
+#ifndef MG_DISABLE_DAV
+#define MG_DISABLE_DAV
+#endif
+#endif /* MG_DISABLE_FILESYSTEM */
+
+#ifdef MG_NO_BSD_SOCKETS
+#ifndef MG_DISABLE_SYNC_RESOLVER
+#define MG_DISABLE_SYNC_RESOLVER
+#endif
+#ifndef MG_DISABLE_SOCKETPAIR
+#define MG_DISABLE_SOCKETPAIR
+#endif
+#endif /* MG_NO_BSD_SOCKETS */
+
+
+#endif /* _MG_COMMON_H_ */
+#ifndef _CS_PLATFORM_H_
+#define _CS_PLATFORM_H_
 
 /*
- * Copyright (c) 2015 Cesanta Software Limited
- * All rights reserved
+ * For the "custom" platform, includes and dependencies can be
+ * provided through mg_locals.h.
  */
+#define CS_P_CUSTOM 0
+#define CS_P_UNIX 1
+#define CS_P_WINDOWS 2
+#define CS_P_ESP_LWIP 3
+#define CS_P_CC3200 4
 
-#ifndef OSDEP_HEADER_INCLUDED
-#define OSDEP_HEADER_INCLUDED
+/* If not specified explicitly, we guess platform by defines. */
+#ifndef CS_PLATFORM
 
-#if !defined(MG_DISABLE_FILESYSTEM) && defined(AVR_NOFS)
-#define MG_DISABLE_FILESYSTEM
+#if defined(__unix__) || defined(__APPLE__)
+#define CS_PLATFORM CS_P_UNIX
+#elif defined(_WIN32)
+#define CS_PLATFORM CS_P_WINDOWS
 #endif
 
-#undef UNICODE                /* Use ANSI WinAPI functions */
-#undef _UNICODE               /* Use multibyte encoding on Windows */
-#define _MBCS                 /* Use multibyte encoding on Windows */
-#define _INTEGRAL_MAX_BITS 64 /* Enable _stati64() on Windows */
-#ifndef _CRT_SECURE_NO_WARNINGS
-#define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005+ */
-#endif
-#undef WIN32_LEAN_AND_MEAN /* Let windows.h always include winsock2.h */
-#undef _XOPEN_SOURCE
-#define _XOPEN_SOURCE 600    /* For flockfile() on Linux */
-#define __STDC_FORMAT_MACROS /* <inttypes.h> wants this for C++ */
-#define __STDC_LIMIT_MACROS  /* C++ wants that for INT64_MAX */
-#ifndef _LARGEFILE_SOURCE
-#define _LARGEFILE_SOURCE /* Enable fseeko() and ftello() functions */
+#ifndef CS_PLATFORM
+#error "CS_PLATFORM is not specified and we couldn't guess it."
 #endif
-#define _FILE_OFFSET_BITS 64 /* Enable 64-bit file offsets */
 
-#if !(defined(AVR_LIBC) || defined(PICOTCP))
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <time.h>
-#include <signal.h>
-#endif
+#endif /* !defined(CS_PLATFORM) */
+
+
+/* Common stuff */
+
+#ifdef __GNUC__
+#define NORETURN __attribute__((noreturn))
+#define UNUSED __attribute__((unused))
+#define NOINLINE __attribute__((noinline))
+#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+#define NORETURN
+#define UNUSED
+#define NOINLINE
+#define WARN_UNUSED_RESULT
+#endif /* __GNUC__ */
 
-#ifndef BYTE_ORDER
-#define LITTLE_ENDIAN 0x41424344
-#define BIG_ENDIAN 0x44434241
-#define PDP_ENDIAN 0x42414443
-/* TODO(lsm): fix for big-endian machines. 'ABCD' is not portable */
-/*#define BYTE_ORDER 'ABCD'*/
-#define BYTE_ORDER LITTLE_ENDIAN
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
 #endif
 
+#endif /* _CS_PLATFORM_H_ */
+#ifndef _CS_PLATFORM_WINDOWS_H_
+#define _CS_PLATFORM_WINDOWS_H_
+#if CS_PLATFORM == CS_P_WINDOWS
+
 /*
  * MSVC++ 14.0 _MSC_VER == 1900 (Visual Studio 2015)
  * MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio 2013)
@@ -94,34 +133,15 @@
 #pragma warning(disable : 4204) /* missing c99 support */
 #endif
 
-#ifdef PICOTCP
-#define time(x) PICO_TIME()
-#ifndef SOMAXCONN
-#define SOMAXCONN (16)
-#endif
-#ifdef _POSIX_VERSION
-#define signal(...)
-#endif
-#endif
-
 #include <assert.h>
-#include <ctype.h>
-#include <limits.h>
-#include <stdarg.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
-
-#ifndef va_copy
-#ifdef __va_copy
-#define va_copy __va_copy
-#else
-#define va_copy(x, y) (x) = (y)
-#endif
-#endif
+#include <sys/stat.h>
 
-#ifdef _WIN32
 #define random() rand()
 #ifdef _MSC_VER
 #pragma comment(lib, "ws2_32.lib") /* Linking with winsock library */
@@ -203,93 +223,321 @@ DIR *opendir(const char *name);
 int closedir(DIR *dir);
 struct dirent *readdir(DIR *dir);
 
-#elif /* not _WIN32 */ defined(MG_CC3200)
+#ifndef va_copy
+#ifdef __va_copy
+#define va_copy __va_copy
+#else
+#define va_copy(x, y) (x) = (y)
+#endif
+#endif
 
-#include <fcntl.h>
-#include <unistd.h>
-#include <cc3200_libc.h>
-#include <cc3200_socket.h>
+#endif /* CS_PLATFORM == CS_P_WINDOWS */
+#endif /* _CS_PLATFORM_WINDOWS_H_ */
+#ifndef _CS_PLATFORM_UNIX_H_
+#define _CS_PLATFORM_UNIX_H_
+#if CS_PLATFORM == CS_P_UNIX
 
-#elif /* not CC3200 */ defined(MG_LWIP)
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
 
-#include <lwip/sockets.h>
-#include <lwip/netdb.h>
-#include <lwip/dns.h>
+/* <inttypes.h> wants this for C++ */
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS
+#endif
 
-#if defined(MG_ESP8266) && defined(RTOS_SDK)
-#include <esp_libc.h>
-#define random() os_random()
+/* C++ wants that for INT64_MAX */
+#ifndef __STDC_LIMIT_MACROS
+#define __STDC_LIMIT_MACROS
 #endif
 
-/* TODO(alashkin): check if zero is OK */
-#define SOMAXCONN 0
-#include <stdlib.h>
+/* Enable fseeko() and ftello() functions */
+#ifndef _LARGEFILE_SOURCE
+#define _LARGEFILE_SOURCE
+#endif
 
-#elif /* not ESP8266 RTOS */ !defined(NO_LIBC) && !defined(NO_BSD_SOCKETS)
+/* Enable 64-bit file offsets */
+#ifndef _FILE_OFFSET_BITS
+#define _FILE_OFFSET_BITS 64
+#endif
 
+#include <arpa/inet.h>
+#include <assert.h>
+#include <ctype.h>
 #include <dirent.h>
+#include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
+#include <math.h>
 #include <netdb.h>
-#include <pthread.h>
-#include <unistd.h>
-#include <arpa/inet.h> /* For inet_pton() when MG_ENABLE_IPV6 is defined */
 #include <netinet/in.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/socket.h>
 #include <sys/select.h>
-#endif
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
 
-#ifndef LWIP_PROVIDE_ERRNO
-#include <errno.h>
+typedef int sock_t;
+#define INVALID_SOCKET (-1)
+#define SIZE_T_FMT "zu"
+typedef struct stat cs_stat_t;
+#define DIRSEP '/'
+#define to64(x) strtoll(x, NULL, 10)
+#define INT64_FMT PRId64
+#define INT64_X_FMT PRIx64
+#define __cdecl
+
+#ifndef va_copy
+#ifdef __va_copy
+#define va_copy __va_copy
+#else
+#define va_copy(x, y) (x) = (y)
+#endif
 #endif
 
-#ifndef _WIN32
+#define closesocket(x) close(x)
+
+#endif /* CS_PLATFORM == CS_P_UNIX */
+#endif /* _CS_PLATFORM_UNIX_H_ */
+#ifndef _CS_PLATFORM_ESP_LWIP_H_
+#define _CS_PLATFORM_ESP_LWIP_H_
+#if CS_PLATFORM == CS_P_ESP_LWIP
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
 #include <inttypes.h>
-#include <stdarg.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
 
-#ifndef AVR_LIBC
-#ifndef MG_ESP8266
-#define closesocket(x) close(x)
-#endif
-#ifndef __cdecl
-#define __cdecl
+#include <lwip/err.h>
+#include <lwip/ip_addr.h>
+#include <lwip/inet.h>
+#include <lwip/netdb.h>
+#include <lwip/dns.h>
+
+#define LWIP_TIMEVAL_PRIVATE 0
+
+#if LWIP_SOCKET
+#include <lwip/sockets.h>
+#define SOMAXCONN 10
+#else
+/* We really need the definitions from sockets.h. */
+#undef LWIP_SOCKET
+#define LWIP_SOCKET 1
+#include <lwip/sockets.h>
+#undef LWIP_SOCKET
+#define LWIP_SOCKET 0
 #endif
 
+typedef int sock_t;
 #define INVALID_SOCKET (-1)
-#define INT64_FMT PRId64
-#define INT64_X_FMT PRIx64
-#if defined(ESP8266) || defined(MG_ESP8266) || defined(MG_CC3200)
 #define SIZE_T_FMT "u"
-#else
-#define SIZE_T_FMT "zu"
-#endif
+typedef struct stat cs_stat_t;
+#define DIRSEP '/'
 #define to64(x) strtoll(x, NULL, 10)
+#define INT64_FMT PRId64
+#define INT64_X_FMT PRIx64
+#define __cdecl
+
+#endif /* CS_PLATFORM == CS_P_ESP_LWIP */
+#endif /* _CS_PLATFORM_ESP_LWIP_H_ */
+/*
+ * Copyright (c) 2014-2016 Cesanta Software Limited
+ * All rights reserved
+ */
+
+#ifndef _CS_PLATFORM_CC3200_H_
+#define _CS_PLATFORM_CC3200_H_
+#if CS_PLATFORM == CS_P_CC3200
+
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <time.h>
+
+#include <simplelink.h>
+
+#define SOMAXCONN 8
+
+/* Undefine a bunch of conflicting symbols so we can use SDK defs verbatim. */
+
+#undef FD_CLR
+#undef FD_SET
+#undef FD_ZERO
+#undef FD_ISSET
+#undef FD_SETSIZE
+#undef fd_set
+
+#undef EACCES
+#undef EBADF
+#undef EAGAIN
+#undef EWOULDBLOCK
+#undef ENOMEM
+#undef EFAULT
+#undef EINVAL
+#undef EDESTADDRREQ
+#undef EPROTOTYPE
+#undef ENOPROTOOPT
+#undef EPROTONOSUPPORT
+#undef EOPNOTSUPP
+#undef EAFNOSUPPORT
+#undef EAFNOSUPPORT
+#undef EADDRINUSE
+#undef EADDRNOTAVAIL
+#undef ENETUNREACH
+#undef ENOBUFS
+#undef EISCONN
+#undef ENOTCONN
+#undef ETIMEDOUT
+#undef ECONNREFUSED
+
+/* The following comes from $SDK/simplelink/include/socket.h */
+/* clang-format off */
+#define FD_SETSIZE                          SL_FD_SETSIZE
+
+#define SOCK_STREAM                         SL_SOCK_STREAM
+#define SOCK_DGRAM                          SL_SOCK_DGRAM
+#define SOCK_RAW                            SL_SOCK_RAW
+#define IPPROTO_TCP                         SL_IPPROTO_TCP
+#define IPPROTO_UDP                         SL_IPPROTO_UDP
+#define IPPROTO_RAW                         SL_IPPROTO_RAW
+
+#define AF_INET                             SL_AF_INET
+#define AF_INET6                            SL_AF_INET6
+#define AF_INET6_EUI_48                     SL_AF_INET6_EUI_48
+#define AF_RF                               SL_AF_RF
+#define AF_PACKET                           SL_AF_PACKET
+
+#define PF_INET                             SL_PF_INET
+#define PF_INET6                            SL_PF_INET6
+
+#define INADDR_ANY                          SL_INADDR_ANY
+#define ERROR                               SL_SOC_ERROR
+#define INEXE                               SL_INEXE
+#define EBADF                               SL_EBADF
+#define ENSOCK                              SL_ENSOCK
+#define EAGAIN                              SL_EAGAIN
+#define EWOULDBLOCK                         SL_EWOULDBLOCK
+#define ENOMEM                              SL_ENOMEM
+#define EACCES                              SL_EACCES
+#define EFAULT                              SL_EFAULT
+#define EINVAL                              SL_EINVAL
+#define EDESTADDRREQ                        SL_EDESTADDRREQ
+#define EPROTOTYPE                          SL_EPROTOTYPE
+#define ENOPROTOOPT                         SL_ENOPROTOOPT
+#define EPROTONOSUPPORT                     SL_EPROTONOSUPPORT
+#define ESOCKTNOSUPPORT                     SL_ESOCKTNOSUPPORT
+#define EOPNOTSUPP                          SL_EOPNOTSUPP
+#define EAFNOSUPPORT                        SL_EAFNOSUPPORT
+#define EADDRINUSE                          SL_EADDRINUSE
+#define EADDRNOTAVAIL                       SL_EADDRNOTAVAIL
+#define ENETUNREACH                         SL_ENETUNREACH
+#define ENOBUFS                             SL_ENOBUFS
+#define EOBUFF                              SL_EOBUFF
+#define EISCONN                             SL_EISCONN
+#define ENOTCONN                            SL_ENOTCONN
+#define ETIMEDOUT                           SL_ETIMEDOUT
+#define ECONNREFUSED                        SL_ECONNREFUSED
+
+#define SOL_SOCKET                          SL_SOL_SOCKET
+#define IPPROTO_IP                          SL_IPPROTO_IP
+#define SO_KEEPALIVE                        SL_SO_KEEPALIVE
+
+#define SO_RCVTIMEO                         SL_SO_RCVTIMEO
+#define SO_NONBLOCKING                      SL_SO_NONBLOCKING
+
+#define IP_MULTICAST_IF                     SL_IP_MULTICAST_IF
+#define IP_MULTICAST_TTL                    SL_IP_MULTICAST_TTL
+#define IP_ADD_MEMBERSHIP                   SL_IP_ADD_MEMBERSHIP
+#define IP_DROP_MEMBERSHIP                  SL_IP_DROP_MEMBERSHIP
+
+#define socklen_t                           SlSocklen_t
+#define timeval                             SlTimeval_t
+#define sockaddr                            SlSockAddr_t
+#define in6_addr                            SlIn6Addr_t
+#define sockaddr_in6                        SlSockAddrIn6_t
+#define in_addr                             SlInAddr_t
+#define sockaddr_in                         SlSockAddrIn_t
+
+#define MSG_DONTWAIT                        SL_MSG_DONTWAIT
+
+#define FD_SET                              SL_FD_SET
+#define FD_CLR                              SL_FD_CLR
+#define FD_ISSET                            SL_FD_ISSET
+#define FD_ZERO                             SL_FD_ZERO
+#define fd_set                              SlFdSet_t
+
+#define socket                              sl_Socket
+#define close                               sl_Close
+#define accept                              sl_Accept
+#define bind                                sl_Bind
+#define listen                              sl_Listen
+#define connect                             sl_Connect
+#define select                              sl_Select
+#define setsockopt                          sl_SetSockOpt
+#define getsockopt                          sl_GetSockOpt
+#define recv                                sl_Recv
+#define recvfrom                            sl_RecvFrom
+#define write                               sl_Write
+#define send                                sl_Send
+#define sendto                              sl_SendTo
+/* rojer: gethostbyname() and sl_NetAppDnsGetHostByName are NOT compatible. */
+/* #define gethostbyname                    sl_NetAppDnsGetHostByName */
+#define htonl                               sl_Htonl
+#define ntohl                               sl_Ntohl
+#define htons                               sl_Htons
+#define ntohs                               sl_Ntohs
+/* clang-format on */
+
 typedef int sock_t;
+#define INVALID_SOCKET (-1)
+#define SIZE_T_FMT "u"
 typedef struct stat cs_stat_t;
 #define DIRSEP '/'
-#endif /* !AVR_LIBC */
+#define to64(x) strtoll(x, NULL, 10)
+#define INT64_FMT PRId64
+#define INT64_X_FMT PRIx64
+#define __cdecl
 
-#ifdef __APPLE__
-int64_t strtoll(const char *str, char **endptr, int base);
-#endif
-#endif /* !_WIN32 */
+#define closesocket(x) close(x)
 
-#ifndef ARRAY_SIZE
-#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
-#endif
+/* Some functions we implement for Mongoose. */
 
-#ifdef __GNUC__
-#define NORETURN __attribute__((noreturn))
-#define UNUSED __attribute__((unused))
-#define NOINLINE __attribute__((noinline))
-#define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
-#else
-#define NORETURN
-#define UNUSED
-#define NOINLINE
-#define WARN_UNUSED_RESULT
-#endif
+const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
+char *inet_ntoa(struct in_addr in);
+int inet_pton(int af, const char *src, void *dst);
 
-#endif /* OSDEP_HEADER_INCLUDED */
+void cc3200_set_non_blocking_mode(int fd);
+
+struct hostent {
+  char *h_name;       /* official name of host */
+  char **h_aliases;   /* alias list */
+  int h_addrtype;     /* host address type */
+  int h_length;       /* length of address */
+  char **h_addr_list; /* list of addresses */
+};
+struct hostent *gethostbyname(const char *name);
+
+struct timeval;
+int gettimeofday(struct timeval *t, void *tz);
+
+long int random(void);
+
+#endif /* CS_PLATFORM == CS_P_CC3200 */
+#endif /* _CS_PLATFORM_CC3200_H_ */
 /*
  * Copyright (c) 2014-2016 Cesanta Software Limited
  * All rights reserved
@@ -313,6 +561,8 @@ enum cs_log_level {
 extern enum cs_log_level s_cs_log_level;
 void cs_log_set_level(enum cs_log_level level);
 
+#ifndef CS_DISABLE_STDIO
+
 void cs_log_printf(const char *fmt, ...);
 
 #define LOG(l, x)                        \
@@ -335,6 +585,13 @@ void cs_log_printf(const char *fmt, ...);
 
 #endif
 
+#else /* CS_DISABLE_STDIO */
+
+#define LOG(l, x)
+#define DBG(x)
+
+#endif
+
 #endif /* _CS_DBG_H_ */
 /*
  * Copyright (c) 2014-2016 Cesanta Software Limited
-- 
GitLab