Formatting changes also command line parsing

This commit is contained in:
NeGate 2022-06-02 22:02:19 -04:00
parent 0972e80257
commit 4b7f9dad52
10 changed files with 140 additions and 130 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ build
*.exp *.exp
*.o *.o
*.4coder *.4coder
*.rdbg

11
build.cmd Normal file
View File

@ -0,0 +1,11 @@
@echo off
setlocal enabledelayedexpansion
set CIABATTA_OPTIONS=-Iinc -g -gcodeview -nodefaultlibs -D_CRT_SECURE_NO_WARNINGS
for /R %%F in (*.c) do (
clang -c -o build\%%~nF.obj %%F %CIABATTA_OPTIONS%
)
llvm-ar rc ciabatta.lib build\*.obj
clang test\test.c ciabatta.lib -lkernel32 -luser32 -lshell32 -nostdlib %CIABATTA_OPTIONS%

View File

@ -1,55 +1,45 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <_platform.h> #include <_platform.h>
// TODO: use abort() to break // TODO: use abort() to break
#if defined(_compiler_msvc) #if defined(_compiler_msvc)
#include <intrin.h> #include <intrin.h>
#define brk __debugbreak #define brk __debugbreak
#else #else
#define brk __builtin_trap #define brk __builtin_trap
#endif #endif
#if defined(_os_win) #if defined(_os_win)
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include<Windows.h> #include <Windows.h>
// TODO: make it work via printf // TODO: make it work via printf
static void _assert_win32_prints(HANDLE conout, char const *str) {
DWORD written = 0;
WriteConsole(conout, str, strlen(str), &written, NULL);
}
static int _assert_strlen(char const *str) extern void _assert_error(char *cond, char const *func, char const *file, char const *line) {
{ HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
int len = 0; if(conout != INVALID_HANDLE_VALUE) {
while(*str++!=0) ++len; _assert_win32_prints(conout, "Assertion Failed: ");
return len; _assert_win32_prints(conout, cond);
_assert_win32_prints(conout, "\n\tFile: ");
_assert_win32_prints(conout, file);
_assert_win32_prints(conout, "\n\tFunc: ");
_assert_win32_prints(conout, func);
_assert_win32_prints(conout, "\n\tLine: ");
_assert_win32_prints(conout, line);
} }
else {
static void _assert_win32_prints(HANDLE conout, char const *str) // TODO:
{ MessageBoxA(NULL,
DWORD written = 0; "Assertion Failed Somewhere, good luck finding it!\n",
WriteConsole(conout, str, _assert_strlen(str), &written, NULL); "Error",
} MB_OK);
extern void _assert_error(char *cond, char const *func, char const *file, char const *line)
{
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
if(conout != INVALID_HANDLE_VALUE) {
_assert_win32_prints(conout, "Assertion Failed: ");
_assert_win32_prints(conout, cond);
_assert_win32_prints(conout, "\n\tFile: ");
_assert_win32_prints(conout, file);
_assert_win32_prints(conout, "\n\tFunc: ");
_assert_win32_prints(conout, func);
_assert_win32_prints(conout, "\n\tLine: ");
_assert_win32_prints(conout, line);
}
else {
// TODO:
MessageBoxA(NULL,
"Assertion Failed Somewhere, good luck finding it!\n",
"Error",
MB_OK);
}
brk();
} }
brk();
}
#endif #endif

View File

@ -64,7 +64,7 @@ int tolower(int c) {
if(isupper(c)) { if(isupper(c)) {
return c-'A'+'a'; return c-'A'+'a';
} }
return c; return c;
} }
int toupper(int c) { int toupper(int c) {

View File

@ -1,11 +1,65 @@
#include <_platform.h> #include <_platform.h>
#include <locale.h> #include <locale.h>
#include <stdbool.h>
extern int main(); #define WIN32_LEAN_AND_MEAN
#include <windows.h>
int mainCRTStartup() extern int main(int argc, char** argv);
{
setlocale(LC_ALL, "C"); // Some shell32.lib related crap
return main(); DECLSPEC_IMPORT LPWSTR GetCommandLineW();
DECLSPEC_IMPORT LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int* pNumArgs);
static size_t count_wide_chars(const wchar_t* str) {
size_t len = 0;
while (str[len] != 0) len++;
return len;
}
static bool convert_wide_chars_to_ansi(char* out, const wchar_t* str, size_t len) {
for (size_t i = 0; i < len; i++) {
wchar_t ch = *str++;
if (ch <= 0 && ch > 0x7F) {
*out++ = 0;
return false;
}
*out++ = ch;
}
*out++ = 0;
return true;
}
void mainCRTStartup() {
HANDLE heap = GetProcessHeap();
if (heap == NULL) {
ExitProcess(-42069);
}
int arg_count;
LPWSTR* args_wide = CommandLineToArgvW(GetCommandLineW(), &arg_count);
if (!args_wide) {
ExitProcess(-69420);
}
char** args = HeapAlloc(heap, 0, arg_count * sizeof(char*));
if (arg_count == 0) {
arg_count = 1;
args[0] = "";
}
// Convert wide chars into ANSI
for (int i = 0; i < arg_count; i++) {
size_t wide_len = count_wide_chars(args_wide[i]) + 1;
args[i] = HeapAlloc(heap, 0, wide_len);
convert_wide_chars_to_ansi(args[i], args_wide[i], wide_len);
}
setlocale(LC_ALL, "C");
int exit_code = main(arg_count, args);
ExitProcess(exit_code);
} }

View File

@ -1,57 +1,57 @@
#include <string.h> #include <string.h>
void *memcpy(void *restrict s1, const void *restrict s2, size_t n) { void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
const unsigned char *restrict c2 = s2; const unsigned char *restrict c2 = s2;
unsigned char *restrict c1 = s1; unsigned char *restrict c1 = s1;
while (n--) { while (n--) {
*c1++ = *c2++; *c1++ = *c2++;
} }
return s1; return s1;
} }
int memcmp(const void *s1, const void *s2, size_t n) { int memcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *u1 = s1; const unsigned char *u1 = s1;
const unsigned char *u2 = s2; const unsigned char *u2 = s2;
for (; n--; u1++, u2++) { for (; n--; u1++, u2++) {
if (*u1 != *u2) return *u1 - *u2; if (*u1 != *u2) return *u1 - *u2;
} }
return 0; return 0;
} }
void *memset(void *s, int c, size_t n) { void *memset(void *s, int c, size_t n) {
unsigned char *restrict buf = s; unsigned char *restrict buf = s;
while (n--) { while (n--) {
*buf++ = c; *buf++ = c;
} }
return s; return s;
} }
int strcmp(const char *s1, const char *s2) { int strcmp(const char *s1, const char *s2) {
while (*s1 != 0 && *s2 != 0) { while (*s1 != 0 && *s2 != 0) {
if (*s1 != *s2) break; if (*s1 != *s2) break;
} }
if(*s1 > *s2) return 1; if(*s1 > *s2) return 1;
if(*s1 < *s2) return -1; if(*s1 < *s2) return -1;
return 0; return 0;
} }
size_t strlen(const char *s) { size_t strlen(const char *s) {
size_t i = 0; size_t i = 0;
while (s[i]) { while (s[i]) {
i++; i++;
} }
return i; return i;
} }
// __STDC_WANT_LIB_EXT1__ // __STDC_WANT_LIB_EXT1__
size_t strnlen_s(const char *s, size_t maxsize) { size_t strnlen_s(const char *s, size_t maxsize) {
size_t i = 0; size_t i = 0;
while (s[i] && i < maxsize) { while (s[i] && i < maxsize) {
i++; i++;
} }
return i; return i;
} }

View File

@ -1,4 +1,3 @@
#pragma once #pragma once
#include "_macros.h" #include "_macros.h"

View File

@ -1,27 +0,0 @@
# TODO: make shell variable-based path
CLFLAGS=/X /GS- /Iinc /Icode $(shell test\inctoarg)
CC=clang-cl
CFLAGS=$(CLFLAGS)
LDFLAGS=/nologo /nodefaultlib /entry:mainCRTStartup /subsystem:console
SRC_DIR := code
OBJ_DIR := build
SRC_FILES := $(wildcard $(SRC_DIR)/*.c)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.obj,$(SRC_FILES))
ciabatta.lib: $(OBJ_FILES)
lib $(LDFLAGS) /out:$@ $^
$(OBJ_DIR)/%.obj: $(SRC_DIR)/%.c
$(CC) -c -o $@ $< $(CFLAGS)
clean:
del /F /Q build\*
del /F /Q lib\ciabatta.lib
inctoarg:
cl test\inctoarg.c
.PHONY: inctoarg ciabatta.lib

View File

@ -1,22 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
int main() {
char *include = getenv("INCLUDE");
printf(" ");
while (*include != 0) {
printf("-I \"");
while (*include != ';' && *include != 0) {
if (*include == '\\') {
printf("/");
} else {
printf("%c", *include);
}
*include++;
}
if (*include == ';') {
++include;
}
printf("\" ");
}
}

View File

@ -1,9 +1,13 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
int main(void) { int main(int argc, char** argv) {
for (char c = 'a'; c != 'z'; ++c) { /*for (int i = 0; i < argv; i++) {
assert(isupper(toupper(c))); printf("[%d] = %s\n", argv[i]);
}*/
for (char c = 'a'; c != 'z'; ++c) {
assert(isupper(toupper(c)));
} }
return 0; return 0;
} }