WIP: preproc
authorLukas Krickl <lukas@krickl.dev>
Sun, 5 Nov 2023 19:18:22 +0000 (20:18 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 5 Nov 2023 19:18:22 +0000 (20:18 +0100)
include/ulas.h
makefile
src/preproc.c
src/test.c

index b7ff99bf4ee216dfdc6dbd2a5cc41f9f1439eb0f..ba8c24c414bd80bc7ee5731375c821dacd8476b1 100644 (file)
@@ -7,6 +7,7 @@
 #include <string.h>
 
 #define ULAS_PATHMAX 4096
+#define ULAS_LINEMAX 4096
 
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
@@ -27,6 +28,9 @@
 // start of preprocessor directives such as #define or #include
 #define ULAS_TOK_PREPROC_BEGIN '#'
 
+#define ULASERR(...) fprintf(ulaserr, __VA_ARGS__);
+#define ULASWARN(...) fprintf(ulaserr, __VA_ARGS__);
+
 // format macros
 #define ULAS_FMT(f, fmt)                                                       \
   if (isatty(fileno(f)) && ulascfg.color) {                                    \
index 3465ca5bd6b1adfc288e4634e9a009f78c8d2e17..aa03e421ab7271c4ee9d21bca100ace4e73246d5 100644 (file)
--- a/makefile
+++ b/makefile
@@ -4,7 +4,7 @@ SDIR=./src
 CC=gcc
 DBGCFLAGS=-g -fsanitize=address
 DBGLDFLAGS=-fsanitize=address 
-CFLAGS=-I$(IDIR) -Wall -pedantic $(DBGCFLAGS) -std=c99
+CFLAGS=-I$(IDIR) -Wall -pedantic $(DBGCFLAGS) -std=gnu99
 LIBS=
 TEST_LIBS=
 LDFLAGS=$(DBGLDFLAGS) $(LIBS)
index 5ddacfe1a5b341a67e67fe64ad0a6f21a5e4f2b8..a469713e52c813eb5332732c6e0275d1e667e459 100644 (file)
@@ -1,4 +1,17 @@
 #include "preproc.h"
+#include "ulas.h"
 
+int ulas_preproc(FILE *dst, FILE *src) {
+  char buf[ULAS_LINEMAX];
+  memset(buf, 0, ULAS_LINEMAX);
 
-int ulas_preproc(FILE *dst, FILE *src) { return 0; }
+  if (!dst || !src) {
+    ULASERR("[preproc] Unable to read from dst or write to src!\n");
+    return -1;
+  }
+
+  while (fgets(buf, ULAS_LINEMAX, src) == NULL) {
+  }
+
+  return 0;
+}
index da84d42f92d270f5482f6309c697b7022b132dfd..e17777c456a4b218a0468b4c17210b8c7ba5a32e 100644 (file)
@@ -1,6 +1,7 @@
 #include "ulas.h"
 #include <stdio.h>
 #include <assert.h>
+#include "preproc.h"
 
 #define TESTBEGIN(name) printf("[test %s]\n", (name));
 #define TESTEND(name) printf("[%s ok]\n", (name));
@@ -25,14 +26,35 @@ void test_tok(void) {
   TESTEND("tok");
 }
 
+#define assert_preproc(expect_dst, expect_ret, input)                          \
+  {                                                                            \
+    char dstbuf[ULAS_LINEMAX];                                                 \
+    memset(dstbuf, 0, ULAS_LINEMAX);                                           \
+    FILE *src = fmemopen((input), strlen((input)), "r");                       \
+    FILE *dst = fmemopen(dstbuf, ULAS_LINEMAX, "w");                           \
+    assert(ulas_preproc(dst, src) == (expect_ret));                            \
+    assert(strcmp(dstbuf, (expect_dst)) == 0);                                 \
+    fclose(src);                                                               \
+    fclose(dst);                                                               \
+  }
+
+void test_preproc(void) {
+  TESTBEGIN("preproc");
+
+  assert_preproc("", 0, "test");
+
+  TESTEND("preproc");
+}
+
 int main(int arc, char **argv) {
   ulas_init(ulas_cfg_from_env());
 
-  if (!ulascfg.verbose) {
+  /*if (!ulascfg.verbose) {
     fclose(stderr);
-  }
+  }*/
 
   test_tok();
+  test_preproc();
 
   return 0;
 }