Added -i option for include path management
authorLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 21:40:55 +0000 (22:40 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 9 Dec 2023 21:40:55 +0000 (22:40 +0100)
include/ulas.h
src/main.c
src/ulas.c

index c57f73ada6e050ab26850b691e3c61281525c997..575d284fc7b93f3ffb4a70aa327b51e7ed0dbd4c 100644 (file)
@@ -102,6 +102,10 @@ struct ulas_config {
 
   int verbose;
   int preproc_only;
+
+  // all include search paths
+  char **incpaths;
+  int incpathslen;
 };
 
 /**
@@ -231,10 +235,6 @@ struct ulas {
   // internal counter
   // used whenever a new unique number might be needed
   int icntr;
-
-  // all include search paths
-  char **include_paths;
-  int include_paths_len;
 };
 
 extern struct ulas ulas;
index d801c9bf9d51eb3348adfb923fb27a69b9e89794..e7484df48014c8ef48452a90573432037fe474db 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <assert.h>
 #include <string.h>
 #include "ulas.h"
 #include <unistd.h>
 #define ULAS_OPTS "hvVp"
 
 // args with value
-#define ULAS_OPTS_ARG "o:l:s:"
+#define ULAS_OPTS_ARG "o:l:s:i:"
 
 #define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc);
 
+#define ULAS_INCPATHSMAX 256
+
+const char *incpaths[ULAS_INCPATHSMAX];
+long incpathslen = 0;
+
 void ulas_help(void) {
   printf("%s\n", ULAS_NAME);
   printf("Usage %s [-%s] [-o=path] [input]\n\n", ULAS_NAME, ULAS_OPTS);
@@ -26,6 +32,7 @@ void ulas_help(void) {
   ULAS_HELP("o=path", "Output file");
   ULAS_HELP("l=path", "Listing file");
   ULAS_HELP("s=path", "Symbols file");
+  ULAS_HELP("i=path", "Add include search path");
 }
 
 void ulas_version(void) { printf("%s version %s\n", ULAS_NAME, ULAS_VER); }
@@ -57,6 +64,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) {
     case 'l':
       cfg->lst_path = strndup(optarg, ULAS_PATHMAX);
       break;
+    case 'i':
+      assert(incpathslen < ULAS_INCPATHSMAX);
+      incpaths[incpathslen++] = strndup(optarg, ULAS_PATHMAX);
     case '?':
       break;
     default:
@@ -76,6 +86,8 @@ int main(int argc, char **argv) {
   struct ulas_config cfg = ulas_cfg_from_env();
 
   ulas_getopt(argc, argv, &cfg);
+  cfg.incpaths = incpaths;
+  cfg.incpathslen = incpathslen;
 
   int res = ulas_main(cfg);
 
@@ -91,5 +103,9 @@ int main(int argc, char **argv) {
     free(cfg.lst_path);
   }
 
+  for (int i = 0; i < incpathslen; i++) {
+    free(incpaths[i]);
+  }
+
   return res;
 }
index 43c253c9f24ca3832538829635dee47d56fa86b2..3989b5a89c4d71c6ef2d3d5829df9eccbf6dfd2a 100644 (file)
@@ -68,9 +68,9 @@ FILE *ulas_incpathfopen(const char *path, const char *mode) {
   int baselen = strlen(path);
 
   // check all include paths
-  for (int i = 0; i < ulas.include_paths_len; i++) {
+  for (int i = 0; i < ulascfg.incpathslen; i++) {
     pathbuf[0] = '\0';
-    char *ip = ulas.include_paths[i];
+    char *ip = ulascfg.incpaths[i];
     int len = strlen(ip);
     if (len + baselen + 1 >= ULAS_PATHMAX) {
       continue;