WIP: path resolver
authorLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 17:26:48 +0000 (18:26 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 17:26:48 +0000 (18:26 +0100)
include/ulas.h
src/ulas.c

index 2b6d11fd3e1c0ca581171db27f0242e74dcaddc3..c57f73ada6e050ab26850b691e3c61281525c997 100644 (file)
@@ -7,6 +7,7 @@
 
 // if this is used as a path use stdin or stdout instead
 #define ULAS_STDFILEPATH "-"
+#define ULAS_PATHSEP "/"
 
 #define ULAS_SYMNAMEMAX 256
 #define ULAS_PATHMAX 4096
@@ -411,7 +412,7 @@ extern struct ulas_config ulascfg;
 struct ulas_config ulas_cfg_from_env(void);
 void ulas_init(struct ulas_config cfg);
 void ulas_free(void);
-FILEulas_incpathfopen(const char *path, const char *mode);
+FILE *ulas_incpathfopen(const char *path, const char *mode);
 
 int ulas_main(struct ulas_config cfg);
 
index ccb82c0f460133754dcf76f7404877bd70e19cc7..a6b27580f10eb853b948981d9aa6aa443736b48a 100644 (file)
@@ -62,12 +62,36 @@ void ulas_free(void) {
   ulas_preprocfree(&ulas.pp);
 }
 
+FILE *ulas_incpathfopen(const char *path, const char *mode) {
+  char pathbuf[ULAS_PATHMAX];
+  memset(pathbuf, 0, ULAS_PATHMAX);
+  
+  for (int i = 0; i < ulas.include_paths_len; i++) {
+    pathbuf[0] = '\0';
+    char *ip = ulas.include_paths[i];
+    int len = strlen(ip);
+
+    strcat(pathbuf, ip);
+    if (ip[len-1] != ULAS_PATHSEP[0]) {
+      strcat(pathbuf, ULAS_PATHSEP);
+    }
+    strcat(pathbuf, path);
 
-FILE* ulas_incpathfopen(const char *path, const char *mode) {
-  // TODO: loop inc-paths 
+    FILE *f = fopen(pathbuf, mode);
+    if (f != NULL) {
+      return f;
+    }
+  }
+
+  // TODO: loop inc-paths
   // lastly check .
 
-  return NULL;
+  FILE *f = fopen(path, mode);
+  if (f == NULL) {
+    ULASERR("%s: %s\n", path, strerror(errno));
+  }
+
+  return f;
 }
 
 int ulas_icntr(void) { return ulas.icntr++; }