Added .incbin directive
authorLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 21:34:11 +0000 (22:34 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 21:34:11 +0000 (22:34 +0100)
src/ulas.c
tests/inc.bin [new file with mode: 0644]
tests/t0.bin
tests/t0.s

index f07f0124f3537c28e91682b26ed0b9dc3bdb7f81..43c253c9f24ca3832538829635dee47d56fa86b2 100644 (file)
@@ -66,8 +66,8 @@ FILE *ulas_incpathfopen(const char *path, const char *mode) {
   char pathbuf[ULAS_PATHMAX];
   memset(pathbuf, 0, ULAS_PATHMAX);
   int baselen = strlen(path);
-  // check all include paths 
+
+  // check all include paths
   for (int i = 0; i < ulas.include_paths_len; i++) {
     pathbuf[0] = '\0';
     char *ip = ulas.include_paths[i];
@@ -77,7 +77,7 @@ FILE *ulas_incpathfopen(const char *path, const char *mode) {
     }
 
     strcat(pathbuf, ip);
-    if (ip[len-1] != ULAS_PATHSEP[0]) {
+    if (ip[len - 1] != ULAS_PATHSEP[0]) {
       strcat(pathbuf, ULAS_PATHSEP);
     }
     strcat(pathbuf, path);
@@ -2280,6 +2280,29 @@ int ulas_asmdirstr(FILE *dst, const char **line, unsigned long n, int *rc) {
   return written;
 }
 
+int ulas_asmdirincbin(FILE *dst, const char **line, unsigned long n, int *rc) {
+  char *path = ulas_strexpr(line, n, rc);
+  char buf[256];
+  memset(buf, 0, 256);
+  int written = 0;
+
+  FILE *f = ulas_incpathfopen(path, "re");
+  if (!f) {
+    *rc = -1;
+    return 0;
+  }
+
+  int read = 0;
+  while ((read = fread(buf, 1, 256, f))) {
+    ulas_asmout(dst, buf, read);
+    written += read;
+  }
+
+  fclose(f);
+
+  return written;
+}
+
 int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
   // this buffer is written both to dst and to verbose output
   char outbuf[ULAS_OUTBUFMAX];
@@ -2344,6 +2367,8 @@ int ulas_asmline(FILE *dst, FILE *src, const char *line, unsigned long n) {
       other_writes += ulas_asmdirstr(dst, &line, n, &rc);
       break;
     case ULAS_ASMDIR_INCBIN:
+      other_writes += ulas_asmdirincbin(dst, &line, n, &rc);
+      break;
     case ULAS_ASMDIR_PAD:
       // TODO: pad is the same as .fill n, $ - n
     case ULAS_ASMDIR_NONE:
diff --git a/tests/inc.bin b/tests/inc.bin
new file mode 100644 (file)
index 0000000..698181d
--- /dev/null
@@ -0,0 +1 @@
+ª»ÌÝ
\ No newline at end of file
index 5f3faedb0e43472151f75f8091ef024132d022c2..384eb120297436f96d5a63451160bc7374e7dfe3 100644 (file)
Binary files a/tests/t0.bin and b/tests/t0.bin differ
index f3999dc717658c9a2fe4757100546e995d2f96db..36daff4bb8c3c81ba4aedf7f857f7d4f91daa485 100644 (file)
@@ -93,3 +93,5 @@ l2:
 .def str s2 = "Hello"
 
 .str "test1", "test2"
+
+.incbin "tests/inc.bin"