From: David Morgan Date: Sat, 5 Nov 2016 06:22:21 +0000 (+0000) Subject: add ability to do near bare-metal builds X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=3653164fe3262ada8fda653a031e5ab2e1864fc7;p=console%2Ftaihen-parser.git add ability to do near bare-metal builds --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bd33ff..ee14261 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,9 @@ cmake_minimum_required(VERSION 3.1.0) -project(taihen-config) +project(taihen-parser) + +option(TEST "build and perform tests" OFF) + include_directories(include) add_subdirectory(src) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 12cf0ce..1fb12b9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,23 @@ +include(CheckIncludeFile) + if (MSVC) add_definitions(-Dinline=__inline) endif() -add_library(taihenconfig lexer.c parser.c) +check_include_file(ctype.h HAVE_CTYPE) +check_include_file(string.h HAVE_STRING) + +if (NOT ${HAVE_CTYPE}) + add_definitions(-DNO_CTYPE) +endif() + +if (NOT ${HAVE_STRING}) + add_definitions(-DNO_STRING) +endif() + +add_library(taihenparser lexer.c parser.c) -install(TARGETS taihenconfig +install(TARGETS taihenparser RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) diff --git a/src/lexer.c b/src/lexer.c index e9ee49c..9ac2cee 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -9,14 +9,72 @@ #include +#ifndef NO_STRING #include +#endif // NO_STRING +#ifndef NO_CTYPE #include +#endif // NO_CTYPE static const char TOKEN_EMPTY = '\0'; static const char TOKEN_COMMENT_START = '#'; static const char TOKEN_SECTION_START = '*'; static const char TOKEN_HALT = '!'; +#ifdef NO_CTYPE +static int isspace(int c) +{ + // we use "C" locale + return (c == ' ') + || (c == '\t') + || (c == '\n') + || (c == '\v') + || (c == '\f') + || (c == '\r'); +} +#endif // NO_CTYPE + +#ifdef NO_STRING +#include + +static size_t strlen(const char *s) +{ + size_t idx = 0; + + while (s[idx]) + { + ++idx; + } + + return idx; +} + +static void *memset(void *s, int c, size_t len) +{ + unsigned char *p = (unsigned char *)s; + + while (len--) + { + *p++ = (unsigned char)c; + } + + return s; +} + +static void *memcpy(void *s1, const void * s2, size_t len) +{ + char *dest = (char *)s1; + const char *src = (const char *)s2; + + while (len--) + { + *dest++ = *src++; + } + + return s1; +} +#endif // NO_STRING + static char *skip_whitespace(char *input) { while (isspace((unsigned char)*input)) diff --git a/src/parser.c b/src/parser.c index 800a987..65a732d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -10,11 +10,40 @@ #include #include +#ifndef NO_STRING #include +#endif // NO_STRING static const char *TOKEN_ALL_SECTION = "ALL"; static const char *TOKEN_KERNEL_SECTION = "KERNEL"; +#ifdef NO_STRING +#include + +static size_t strlen(const char *s) +{ + size_t idx = 0; + + while (s[idx]) + { + ++idx; + } + + return idx; +} + +static int strcmp(const char * s1, const char * s2) +{ + while ((*s1) && (*s1 == *s2)) + { + ++s1; + ++s2; + } + return (*s1 - *s2); +} + +#endif // NO_STRING + static inline int is_continuation_byte(char b) { return ((b & 0xC0) == 0x80);