WIP: basic io
authorLukas Krickl <lukas@krickl.dev>
Sun, 5 Nov 2023 07:03:08 +0000 (08:03 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 5 Nov 2023 07:03:08 +0000 (08:03 +0100)
include/ulas.h
src/main.c
src/ulas.c

index f916a91f2918b272c14af582dfa99ccf016f88d0..57b106d41ac568862e506775543e0ad022e6d365 100644 (file)
@@ -6,6 +6,8 @@
 #include <stdbool.h>
 #include <string.h>
 
+#define ULAS_PATHMAX 4096
+
 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
 
 
 // configurable tokens
 #define ULAS_TOK_COMMENT ';'
-#define ULAS_TOK_DIRECTIVE_BEGIN '#'
+// start of as directives such as .org 
+#define ULAS_TOK_ASDIR_BEGIN '.'
+// start of preprocessor directives such as #define or #include 
+#define ULAS_TOK_PREPROC_BEGIN '#'
 
 // format macros
 #define ULAS_FMT(f, fmt)                                                       \
@@ -33,9 +38,12 @@ extern FILE *ulasout;
 extern FILE *ulaserr;
 
 struct ulas_config {
+  // argv represents file names 
   char **argv;
   int argc;
 
+  char *output_path;
+
   bool verbose;
 };
 
@@ -46,4 +54,6 @@ void ulas_init(struct ulas_config cfg);
 
 int ulas_main(struct ulas_config cfg);
 
+char *ulas_strndup(const char *src, size_t n);
+
 #endif
index d591da66f41c10f4322f76b040bc13e8e05a063b..90bd1b22afd3f2d631623fc49eda82c45c457daf 100644 (file)
 #define ULAS_OPTS "hvV"
 
 // args with value
-#define ULAS_OPTS_ARG ""
+#define ULAS_OPTS_ARG "o:"
 
-#define ULAS_HELP(a, desc) printf("\t%s\t%s\n", (a), desc);
+#define ULAS_HELP(a, desc) printf("\t-%s\t%s\n", (a), desc);
 
 void ulas_help(void) {
   printf("%s\n", ULAS_NAME);
-  printf("Usage %s [-%s]\n\n", ULAS_NAME, ULAS_OPTS);
-  ULAS_HELP("-h", "display this help and exit");
-  ULAS_HELP("-V", "display version info and exit");
-  ULAS_HELP("-v", "verbose output");
+  printf("Usage %s [-%s] [-o=path] [input]\n\n", ULAS_NAME, ULAS_OPTS);
+  ULAS_HELP("h", "display this help and exit");
+  ULAS_HELP("V", "display version info and exit");
+  ULAS_HELP("v", "verbose output");
+  ULAS_HELP("o=path", "Output file");
 }
 
 void ulas_version(void) { printf("%s version %s\n", ULAS_NAME, ULAS_VER); }
@@ -41,6 +42,9 @@ void ulas_getopt(int argc, char **argv, struct ulas_config *cfg) {
     case 'v':
       cfg->verbose = true;
       break;
+    case 'o':
+      cfg->output_path = ulas_strndup(optarg, ULAS_PATHMAX);
+      break;
     case '?':
       break;
     default:
index 1d88cd92bdc24247ab5ef143563c64083e85d42a..ee6c2997a120c8c841e95a06daefc9dd0c4e5268 100644 (file)
@@ -1,4 +1,6 @@
 #include "ulas.h"
+#include <errno.h>
+#include <string.h>
 
 FILE *ulasin = NULL;
 FILE *ulasout = NULL;
@@ -25,7 +27,30 @@ struct ulas_config ulas_cfg_from_env(void) {
   return cfg;
 }
 
+char *ulas_strndup(const char *src, size_t n) {
+  size_t len = MIN(strlen(src), n);
+  char *dst = malloc(len);
+  strncpy(dst, src, len);
+  return dst;
+}
+
 int ulas_main(struct ulas_config cfg) {
+  if (cfg.output_path) {
+    ulasout = fopen(cfg.output_path, "we");
+    if (!ulasout) {
+      fprintf(ulaserr, "%s: %s\n", cfg.output_path, strerror(errno));
+      free(cfg.output_path);
+      return -1;
+    }
+  }
+
   ulas_init(cfg);
+
+  if (cfg.output_path) {
+    fclose(ulasout);
+    free(cfg.output_path);
+    return -1;
+  }
+
   return 0;
 }