diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..464bc20 --- /dev/null +++ b/build.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +CC=clang + +# NOTE(mal): Silent pushd/popd +pushd () { + command pushd "$@" > /dev/null +} +popd () { + command popd "$@" > /dev/null +} + +echo ~~~ Metadesk Build ~~~ +# TODO(mal): Review these warnings +accepted_clang_warnings="-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option" +compile_flags="-I../source/ $accepted_clang_warnings" + +mkdir -p build +pushd build +echo +echo ~~~ Build All Samples ~~~ +$CC $compile_flags ../samples/old_style_custom_layer.c -o old_style_custom_layer +$CC $compile_flags ../samples/static_site_generator/static_site_generator.c -o static_site_generator +$CC $compile_flags ../samples/output_parse/output_parse.c -o output_parse +echo +echo ~~~ Build All Tests ~~~ +$CC $compile_flags ../tests/sanity_tests.c -o sanity_tests +$CC $compile_flags ../tests/unicode_test.c -o unicode_test +clang++ $compile_flags ../tests/cpp_build_test.cpp +popd + +echo +echo ~~~ Running Sanity Tests ~~~ +pushd build +sanity_tests +popd + +echo +echo ~~~ Running Static Site Generator Sample ~~~ +mkdir -p samples/static_site_generator/example_site/generated +pushd samples/static_site_generator/example_site/generated +../../../../build/static_site_generator --siteinfo ../site_info.md --pagedir ../ +popd + +echo +echo ~~~ Running Output Parse Sample ~~~ +mkdir -p samples/output_parse/examples/output +pushd samples/output_parse/examples/output +../../../../build/output_parse ../example.md ../example2.md +popd diff --git a/build_with_clang.bat b/build_with_clang.bat new file mode 100644 index 0000000..96090c9 --- /dev/null +++ b/build_with_clang.bat @@ -0,0 +1,52 @@ +@echo off + +echo ~~~ Metadesk Build ~~~ +rem TODO(mal): Review these warnings +set accepted_clang_warnings=-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option +set compile_flags=-I../source/ %accepted_clang_warnings% + +if not exist build mkdir build +pushd build +echo. +echo ~~~ Build All Samples ~~~ +clang %compile_flags% ..\samples\old_style_custom_layer.c -o old_style_custom_layer.exe +clang %compile_flags% ..\samples\static_site_generator\static_site_generator.c -o static_site_generator.exe +clang %compile_flags% ..\samples\output_parse\output_parse.c -o output_parse.exe +echo. +echo ~~~ Build All Tests ~~~ +clang %compile_flags% ..\tests\sanity_tests.c -o sanity_tests.exe +clang %compile_flags% ..\tests\unicode_test.c -o unicode_test.exe +clang++ %compile_flags% ..\tests\cpp_build_test.cpp -o cpp_build_test.exe +popd + +echo. +echo ~~~ Running Sanity Tests ~~~ +pushd build +sanity_tests.exe +popd + +echo. +echo ~~~ Running Static Site Generator Sample ~~~ +pushd samples +pushd static_site_generator +pushd example_site +if not exist generated mkdir generated +pushd generated +..\..\..\..\build\static_site_generator.exe --siteinfo ..\site_info.md --pagedir ..\ +popd +popd +popd +popd + +echo. +echo ~~~ Running Output Parse Sample ~~~ +pushd samples +pushd output_parse +pushd examples +if not exist output mkdir output +pushd output +..\..\..\..\build\output_parse.exe ..\example.md ..\example2.md +popd +popd +popd +popd diff --git a/source/md_impl.c b/source/md_impl.c index c5503ce..dc16e48 100644 --- a/source/md_impl.c +++ b/source/md_impl.c @@ -991,7 +991,8 @@ MD_NodeTable_Insert(MD_NodeTable *table, MD_NodeTableCollisionRule collision_rul MD_FUNCTION_IMPL MD_Token MD_ZeroToken(void) { - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); return token; } @@ -1064,7 +1065,8 @@ MD_Parse_BumpNext(MD_ParseCtx *ctx) MD_FUNCTION_IMPL MD_Token MD_Parse_LexNext(MD_ParseCtx *ctx) { - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); MD_u8 *one_past_last = ctx->file_contents.str + ctx->file_contents.size; MD_u8 *first = ctx->at; @@ -1239,7 +1241,8 @@ MD_Parse_PeekSkipSome(MD_ParseCtx *ctx, MD_TokenGroups skip_groups) MD_b32 skip_whitespace = (skip_groups & MD_TokenGroup_Whitespace); MD_b32 skip_regular = (skip_groups & MD_TokenGroup_Regular); - MD_Token result = {0}; + MD_Token result; + _MD_MemoryZero(&result, sizeof(result)); loop: { @@ -1272,6 +1275,7 @@ MD_Parse_Require(MD_ParseCtx *ctx, MD_String8 string) int result = 0; MD_Token token_any = MD_Parse_PeekSkipSome(ctx, 0); + MD_Token token_regular; if(MD_StringMatch(token_any.string, string, 0)) { result = 1; @@ -1279,7 +1283,7 @@ MD_Parse_Require(MD_ParseCtx *ctx, MD_String8 string) goto end; } - MD_Token token_regular = MD_Parse_PeekSkipSome(ctx, MD_TokenGroup_Comment|MD_TokenGroup_Whitespace); + token_regular = MD_Parse_PeekSkipSome(ctx, MD_TokenGroup_Comment|MD_TokenGroup_Whitespace); if(MD_StringMatch(token_regular.string, string, 0)) { result = 1; @@ -1396,7 +1400,8 @@ _MD_ParseOneNode(MD_ParseCtx *ctx) MD_ParseResult result = {0}; result.node = MD_NilNode(); - MD_Token token = {0}; + MD_Token token; + _MD_MemoryZero(&token, sizeof(token)); MD_Node *first_tag = 0; MD_Node *last_tag = 0; @@ -1574,7 +1579,8 @@ _MD_ParseTagList(MD_ParseCtx *ctx, MD_Node **first_out, MD_Node **last_out) for(;MD_Parse_Require(ctx, MD_S8Lit("@"));) { - MD_Token name = {0}; + MD_Token name; + _MD_MemoryZero(&name, sizeof(name)); if(MD_Parse_RequireKind(ctx, MD_TokenKind_Identifier, &name)) { MD_Node *tag = _MD_MakeNodeFromToken_Ctx(ctx, MD_NodeKind_Tag, name); @@ -1905,7 +1911,7 @@ MD_TagCountFromNodeAndString(MD_Node *node, MD_String8 string, MD_StringMatchFla MD_FUNCTION_IMPL void MD_NodeMessage(MD_Node *node, MD_MessageKind kind, MD_String8 str) { - char *kind_name = kind == MD_MessageKind_Error ? "error" : "warning"; + const char *kind_name = kind == MD_MessageKind_Error ? "error" : "warning"; MD_CodeLoc loc = MD_CodeLocFromNode(node); fprintf(stderr, "%.*s(%i:%i): %s: %.*s\n", MD_StringExpand(loc.filename), @@ -2267,12 +2273,13 @@ MD_PRIVATE_FUNCTION_IMPL MD_Expr * _MD_ParseExpr_(_MD_NodeParseCtx *ctx, int precedence_in) { MD_Expr *expr = _MD_ParseUnaryExpr(ctx); + MD_ExprKind expr_kind; if(MD_ExprIsNil(expr)) { goto end_parse; } - MD_ExprKind expr_kind = MD_BinaryExprKindFromNode(ctx->at); + expr_kind = MD_BinaryExprKindFromNode(ctx->at); if(expr_kind != MD_ExprKind_Nil) { for(int precedence = MD_ExprPrecFromExprKind(expr_kind); diff --git a/source/md_malloc.c b/source/md_malloc.c index 253d839..3109f2c 100644 --- a/source/md_malloc.c +++ b/source/md_malloc.c @@ -17,7 +17,7 @@ MD_MALLOC_Alloc(void *ctx, MD_u64 size) static void* MD_MALLOC_GetCtx(void) { - return(MD_MALLOC_Alloc); + return((void *)MD_MALLOC_Alloc); } /* diff --git a/source/md_posix.c b/source/md_posix.c index 17a7843..bc30e0e 100644 --- a/source/md_posix.c +++ b/source/md_posix.c @@ -1,9 +1,57 @@ // LICENSE AT END OF FILE (MIT). +#include +#include + +#define MD_IMPL_FileIterIncrement MD_POSIX_FileIterIncrement + static MD_b32 -_MD_OS_IMPL_FileIter_Increment(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info) +MD_POSIX_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info) { + MD_b32 result = 0; + DIR *dir = (DIR *)(it->state); + if(dir == 0) + { + dir = opendir((char*)path.str); + } + + struct dirent *dir_entry = 0; + if(dir && (dir_entry = readdir(dir))) + { + out_info->filename = MD_PushStringF("%s", dir_entry->d_name); + out_info->flags = 0; + + if(path.size > 1 && path.str[path.size-1] == '/') + { + path.size -= 1; + } + MD_String8 cfile_path = MD_PushStringF("%.*s/%s", MD_StringExpand(path), dir_entry->d_name); + + // NOTE(mal): On Linux, fstatat(2) would save us from doing path manipulation and avoid some race + // conditions but we would need to make space for an extra directory file descriptor + // inside MD_FileIter struct. We would also stop being POSIX-compliant. + struct stat st; + if(stat((char *)cfile_path.str, &st) == 0) + { + if((st.st_mode & S_IFMT) == S_IFDIR) + { + out_info->flags |= MD_FileFlag_Directory; + } + out_info->file_size = st.st_size; + } + result = 1; + } + else + { + closedir(dir); + dir = 0; + result = 0; + } + + it->state = *(MD_u64 *)(&dir); + + return result; } /* @@ -14,4 +62,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ \ No newline at end of file +*/