diff --git a/Makefile b/Makefile index 914f1b31..a1d8c88b 100644 --- a/Makefile +++ b/Makefile @@ -125,7 +125,7 @@ mongoose.c: Makefile $(wildcard src/*) (cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@ mongoose.h: $(HDRS) Makefile - (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/config.h src/arch.h src/arch_*.h src/str.h src/log.h src/timer.h src/util.h src/fs.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@ + (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/util.h src/fs.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@ clean: rm -rf $(PROG) *.o *.dSYM unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack diff --git a/mongoose.c b/mongoose.c index 0be9e235..39a5b1f4 100644 --- a/mongoose.c +++ b/mongoose.c @@ -515,7 +515,7 @@ struct mg_fs mg_fs_packed = {packed_stat, packed_list, packed_open, #endif -#if defined(FOPEN_MAX) +#if MG_ENABLE_FILE static int p_stat(const char *path, size_t *size, time_t *mtime) { #ifdef _WIN32 struct _stati64 st; @@ -691,9 +691,9 @@ static size_t p_write(void *fp, const void *buf, size_t len) { } static size_t p_seek(void *fp, size_t offset) { -#if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || \ - (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ - (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) +#if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || \ + (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ + (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) fseeko((FILE *) fp, (off_t) offset, SEEK_SET); #else fseek((FILE *) fp, (long) offset, SEEK_SET); @@ -1127,6 +1127,7 @@ char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) { return buf; } +#if MG_ENABLE_FILE int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, const char *dir) { char offset[40] = "", name[200] = "", path[256]; @@ -1152,6 +1153,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, } } } +#endif static void static_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { @@ -1796,7 +1798,9 @@ void mg_iobuf_free(struct mg_iobuf *io) { #if MG_ENABLE_LOG static void mg_log_stdout(const void *buf, size_t len, void *userdata) { (void) userdata; +#if MG_ENABLE_FILE fwrite(buf, 1, len, stdout); +#endif } static const char *s_spec = "2"; @@ -4163,6 +4167,7 @@ struct mg_str mg_url_pass(const char *url) { #include #endif +#if MG_ENABLE_FILE char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; @@ -4204,6 +4209,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) { } return result; } +#endif bool mg_file_printf(const char *path, const char *fmt, ...) { char tmp[256], *buf = tmp; @@ -4512,8 +4518,8 @@ unsigned long mg_millis(void) { uint64_t ticks = mach_absolute_time(); static mach_timebase_info_data_t timebase; mach_timebase_info(&timebase); - double ticks_to_nanos = (double)timebase.numer / timebase.denom; - uint64_t uptime_nanos = (uint64_t)(ticks_to_nanos * ticks); + double ticks_to_nanos = (double) timebase.numer / timebase.denom; + uint64_t uptime_nanos = (uint64_t) (ticks_to_nanos * ticks); return (unsigned long) (uptime_nanos / 1000000); #else struct timespec ts; diff --git a/mongoose.h b/mongoose.h index 90a8a1f4..4c9202eb 100644 --- a/mongoose.h +++ b/mongoose.h @@ -25,82 +25,6 @@ extern "C" { #endif -#ifndef MG_ENABLE_SOCKET -#define MG_ENABLE_SOCKET 1 -#endif - -#ifndef MG_ENABLE_MBEDTLS -#define MG_ENABLE_MBEDTLS 0 -#endif - -#ifndef MG_ENABLE_OPENSSL -#define MG_ENABLE_OPENSSL 0 -#endif - -#ifndef MG_ENABLE_CUSTOM_TLS -#define MG_ENABLE_CUSTOM_TLS 0 -#endif - -#ifndef MG_ENABLE_SSI -#define MG_ENABLE_SSI 1 -#endif - -#ifndef MG_ENABLE_IPV6 -#define MG_ENABLE_IPV6 0 -#endif - -#ifndef MG_ENABLE_LOG -#define MG_ENABLE_LOG 1 -#endif - -#ifndef MG_ENABLE_MD5 -#define MG_ENABLE_MD5 0 -#endif - -// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) -#ifndef MG_ENABLE_WINSOCK -#define MG_ENABLE_WINSOCK 1 -#endif - -#ifndef MG_ENABLE_DIRLIST -#define MG_ENABLE_DIRLIST 0 -#endif - -#ifndef MG_ENABLE_CUSTOM_RANDOM -#define MG_ENABLE_CUSTOM_RANDOM 0 -#endif - -#ifndef MG_ENABLE_PACKED_FS -#define MG_ENABLE_PACKED_FS 0 -#endif - -// Granularity of the send/recv IO buffer growth -#ifndef MG_IO_SIZE -#define MG_IO_SIZE 2048 -#endif - -// Maximum size of the recv IO buffer -#ifndef MG_MAX_RECV_BUF_SIZE -#define MG_MAX_RECV_BUF_SIZE (3 * 1024 * 1024) -#endif - -#ifndef MG_MAX_HTTP_HEADERS -#define MG_MAX_HTTP_HEADERS 40 -#endif - -#ifndef MG_HTTP_INDEX -#define MG_HTTP_INDEX "index.html" -#endif - -#ifndef MG_PATH_MAX -#define MG_PATH_MAX PATH_MAX -#endif - -#ifndef MG_SOCK_LISTEN_BACKLOG_SIZE -#define MG_SOCK_LISTEN_BACKLOG_SIZE 128 -#endif - - #define MG_ARCH_CUSTOM 0 #define MG_ARCH_UNIX 1 #define MG_ARCH_WIN32 2 @@ -205,11 +129,6 @@ static __inline struct tm *localtime_r(time_t *t, struct tm *tm) { #include #include -#undef MG_PATH_MAX -#undef MG_ENABLE_DIRLIST - -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" #define MG_PATH_MAX 128 #define MG_ENABLE_DIRLIST 1 @@ -237,11 +156,6 @@ static __inline struct tm *localtime_r(time_t *t, struct tm *tm) { #include -#undef MG_PATH_MAX -#undef MG_ENABLE_DIRLIST - -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" #define MG_PATH_MAX 128 #define MG_ENABLE_DIRLIST 1 @@ -321,9 +235,6 @@ static inline void *mg_calloc(int cnt, size_t size) { #include #include -#define MG_INT64_FMT "%lld" -#define MG_DIRSEP '/' - // Why FreeRTOS-TCP did not implement a clean BSD API, but its own thing // with FreeRTOS_ prefix, is beyond me #define IPPROTO_TCP FREERTOS_IPPROTO_TCP @@ -418,9 +329,7 @@ struct timeval { #include #include -#define MG_DIRSEP '/' #define MG_INT64_FMT "%" PRId64 -#undef MG_ENABLE_DIRLIST #define MG_ENABLE_DIRLIST 1 #endif @@ -525,6 +434,102 @@ static __inline struct tm *localtime_r(time_t *t, struct tm *tm) { #endif +#ifndef MG_ENABLE_SOCKET +#define MG_ENABLE_SOCKET 1 +#endif + +#ifndef MG_ENABLE_MBEDTLS +#define MG_ENABLE_MBEDTLS 0 +#endif + +#ifndef MG_ENABLE_OPENSSL +#define MG_ENABLE_OPENSSL 0 +#endif + +#ifndef MG_ENABLE_CUSTOM_TLS +#define MG_ENABLE_CUSTOM_TLS 0 +#endif + +#ifndef MG_ENABLE_SSI +#define MG_ENABLE_SSI 1 +#endif + +#ifndef MG_ENABLE_IPV6 +#define MG_ENABLE_IPV6 0 +#endif + +#ifndef MG_ENABLE_LOG +#define MG_ENABLE_LOG 1 +#endif + +#ifndef MG_ENABLE_MD5 +#define MG_ENABLE_MD5 0 +#endif + +// Set MG_ENABLE_WINSOCK=0 for Win32 builds with external IP stack (like LWIP) +#ifndef MG_ENABLE_WINSOCK +#define MG_ENABLE_WINSOCK 1 +#endif + +#ifndef MG_ENABLE_DIRLIST +#define MG_ENABLE_DIRLIST 0 +#endif + +#ifndef MG_ENABLE_CUSTOM_RANDOM +#define MG_ENABLE_CUSTOM_RANDOM 0 +#endif + +#ifndef MG_ENABLE_PACKED_FS +#define MG_ENABLE_PACKED_FS 0 +#endif + +// Granularity of the send/recv IO buffer growth +#ifndef MG_IO_SIZE +#define MG_IO_SIZE 2048 +#endif + +// Maximum size of the recv IO buffer +#ifndef MG_MAX_RECV_BUF_SIZE +#define MG_MAX_RECV_BUF_SIZE (3 * 1024 * 1024) +#endif + +#ifndef MG_MAX_HTTP_HEADERS +#define MG_MAX_HTTP_HEADERS 40 +#endif + +#ifndef MG_HTTP_INDEX +#define MG_HTTP_INDEX "index.html" +#endif + +#ifndef MG_PATH_MAX +#ifdef PATH_MAX +#define MG_PATH_MAX PATH_MAX +#else +#define MG_PATH_MAX 128 +#endif +#endif + +#ifndef MG_SOCK_LISTEN_BACKLOG_SIZE +#define MG_SOCK_LISTEN_BACKLOG_SIZE 128 +#endif + +#ifndef MG_DIRSEP +#define MG_DIRSEP '/' +#endif + +#ifndef MG_INT64_FMT +#define MG_INT64_FMT "%lld" +#endif + +#ifndef MG_ENABLE_FILE +#if defined(FOPEN_MAX) +#define MG_ENABLE_FILE 1 +#else +#define MG_ENABLE_FILE 0 +#endif +#endif + + #include #include diff --git a/src/arch_esp32.h b/src/arch_esp32.h index 650880a8..a6925a3c 100644 --- a/src/arch_esp32.h +++ b/src/arch_esp32.h @@ -17,11 +17,6 @@ #include #include -#undef MG_PATH_MAX -#undef MG_ENABLE_DIRLIST - -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" #define MG_PATH_MAX 128 #define MG_ENABLE_DIRLIST 1 diff --git a/src/arch_esp8266.h b/src/arch_esp8266.h index a26b2626..8c00ec94 100644 --- a/src/arch_esp8266.h +++ b/src/arch_esp8266.h @@ -21,11 +21,6 @@ #include -#undef MG_PATH_MAX -#undef MG_ENABLE_DIRLIST - -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" #define MG_PATH_MAX 128 #define MG_ENABLE_DIRLIST 1 diff --git a/src/arch_freertos_tcp.h b/src/arch_freertos_tcp.h index 4bc6b4ab..db2c9147 100644 --- a/src/arch_freertos_tcp.h +++ b/src/arch_freertos_tcp.h @@ -19,9 +19,6 @@ #include #include -#define MG_INT64_FMT "%lld" -#define MG_DIRSEP '/' - // Why FreeRTOS-TCP did not implement a clean BSD API, but its own thing // with FreeRTOS_ prefix, is beyond me #define IPPROTO_TCP FREERTOS_IPPROTO_TCP diff --git a/src/arch_unix.h b/src/arch_unix.h index cb6585f6..d74fe2df 100644 --- a/src/arch_unix.h +++ b/src/arch_unix.h @@ -29,9 +29,7 @@ #include #include -#define MG_DIRSEP '/' #define MG_INT64_FMT "%" PRId64 -#undef MG_ENABLE_DIRLIST #define MG_ENABLE_DIRLIST 1 #endif diff --git a/src/config.h b/src/config.h index 153fbd74..8cad23eb 100644 --- a/src/config.h +++ b/src/config.h @@ -68,9 +68,29 @@ #endif #ifndef MG_PATH_MAX +#ifdef PATH_MAX #define MG_PATH_MAX PATH_MAX +#else +#define MG_PATH_MAX 128 +#endif #endif #ifndef MG_SOCK_LISTEN_BACKLOG_SIZE #define MG_SOCK_LISTEN_BACKLOG_SIZE 128 #endif + +#ifndef MG_DIRSEP +#define MG_DIRSEP '/' +#endif + +#ifndef MG_INT64_FMT +#define MG_INT64_FMT "%lld" +#endif + +#ifndef MG_ENABLE_FILE +#if defined(FOPEN_MAX) +#define MG_ENABLE_FILE 1 +#else +#define MG_ENABLE_FILE 0 +#endif +#endif diff --git a/src/fs_posix.c b/src/fs_posix.c index d9b6d3ca..fa12c2cd 100644 --- a/src/fs_posix.c +++ b/src/fs_posix.c @@ -1,6 +1,6 @@ #include "fs.h" -#if defined(FOPEN_MAX) +#if MG_ENABLE_FILE static int p_stat(const char *path, size_t *size, time_t *mtime) { #ifdef _WIN32 struct _stati64 st; @@ -176,9 +176,9 @@ static size_t p_write(void *fp, const void *buf, size_t len) { } static size_t p_seek(void *fp, size_t offset) { -#if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || \ - (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ - (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) +#if (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64) || \ + (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ + (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 600) fseeko((FILE *) fp, (off_t) offset, SEEK_SET); #else fseek((FILE *) fp, (long) offset, SEEK_SET); diff --git a/src/http.c b/src/http.c index 87bbf570..f0f4e8ec 100644 --- a/src/http.c +++ b/src/http.c @@ -378,6 +378,7 @@ char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) { return buf; } +#if MG_ENABLE_FILE int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, const char *dir) { char offset[40] = "", name[200] = "", path[256]; @@ -403,6 +404,7 @@ int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, } } } +#endif static void static_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { diff --git a/src/log.c b/src/log.c index 5c433b7c..cde69323 100644 --- a/src/log.c +++ b/src/log.c @@ -4,7 +4,9 @@ #if MG_ENABLE_LOG static void mg_log_stdout(const void *buf, size_t len, void *userdata) { (void) userdata; +#if MG_ENABLE_FILE fwrite(buf, 1, len, stdout); +#endif } static const char *s_spec = "2"; diff --git a/src/util.c b/src/util.c index 9eb8f615..8576d987 100644 --- a/src/util.c +++ b/src/util.c @@ -6,6 +6,7 @@ #include #endif +#if MG_ENABLE_FILE char *mg_file_read(const char *path, size_t *sizep) { FILE *fp; char *data = NULL; @@ -47,6 +48,7 @@ bool mg_file_write(const char *path, const void *buf, size_t len) { } return result; } +#endif bool mg_file_printf(const char *path, const char *fmt, ...) { char tmp[256], *buf = tmp; @@ -355,8 +357,8 @@ unsigned long mg_millis(void) { uint64_t ticks = mach_absolute_time(); static mach_timebase_info_data_t timebase; mach_timebase_info(&timebase); - double ticks_to_nanos = (double)timebase.numer / timebase.denom; - uint64_t uptime_nanos = (uint64_t)(ticks_to_nanos * ticks); + double ticks_to_nanos = (double) timebase.numer / timebase.denom; + uint64_t uptime_nanos = (uint64_t) (ticks_to_nanos * ticks); return (unsigned long) (uptime_nanos / 1000000); #else struct timespec ts; diff --git a/test/mongoose_custom.h b/test/mongoose_custom.h index a01c66b1..522be510 100644 --- a/test/mongoose_custom.h +++ b/test/mongoose_custom.h @@ -15,9 +15,5 @@ #include #include -#define MG_DIRSEP '/' -#define MG_INT64_FMT "%lld" -#undef MG_PATH_MAX #define MG_PATH_MAX 100 -#undef MG_ENABLE_SOCKET #define MG_ENABLE_SOCKET 0