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,6 +1,6 @@
#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
@ -16,22 +16,12 @@
#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) {
static int _assert_strlen(char const *str)
{
int len = 0;
while(*str++!=0) ++len;
return len;
}
static void _assert_win32_prints(HANDLE conout, char const *str)
{
DWORD written = 0; DWORD written = 0;
WriteConsole(conout, str, _assert_strlen(str), &written, NULL); WriteConsole(conout, str, strlen(str), &written, NULL);
} }
extern void _assert_error(char *cond, char const *func, char const *file, char const *line) extern void _assert_error(char *cond, char const *func, char const *file, char const *line) {
{
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
if(conout != INVALID_HANDLE_VALUE) { if(conout != INVALID_HANDLE_VALUE) {
_assert_win32_prints(conout, "Assertion Failed: "); _assert_win32_prints(conout, "Assertion Failed: ");

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