aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-09-02 16:00:39 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-09-02 16:06:09 +0100
commitf1891b0c2e85262a8f2dc0a0d13eea192fe00c26 (patch)
tree103eedef5ae4be4792ffb8868aaef1ba30b40e0f
parent89b9c0f94d290d5fb774af7fec5868deda0fd878 (diff)
downloadobf-f1891b0c2e85262a8f2dc0a0d13eea192fe00c26.tar.gz
obf-f1891b0c2e85262a8f2dc0a0d13eea192fe00c26.tar.bz2
obf-f1891b0c2e85262a8f2dc0a0d13eea192fe00c26.zip
Better command line usage
Now you can use command line arguments to list a set of input files for the interpreter to use. Currently outputs the data parsed per file.
-rw-r--r--main.c56
1 files changed, 41 insertions, 15 deletions
diff --git a/main.c b/main.c
index 41039b3..e2a0ec9 100644
--- a/main.c
+++ b/main.c
@@ -231,25 +231,51 @@ char *fread_all(FILE *fp)
return buffer.data;
}
-int main(void)
+int main(int argc, char *argv[])
{
- const char *filepath = "./input.txt";
- FILE *handle = fopen(filepath, "r");
- char *file_data = fread_all(handle);
- fclose(handle);
- buffer_t *buffer = buffer_init_str(filepath, file_data, strlen(file_data));
- struct PResult res = parse_input(buffer);
- if (res.nodes == NULL)
+ if (argc == 1)
{
- fputs("Exiting early...\n", stderr);
- goto error;
+ fprintf(
+ stderr,
+ "Usage: %s [FILE]...\nReads FILES sequentially on the same machine\n",
+ argv[0]);
+ return 1;
}
- char *str = ast_to_str(res.nodes, res.size);
- free(str);
- free(res.nodes);
- free(buffer);
- free(file_data);
+ char *filepath, *file_data;
+ buffer_t *buffer;
+ struct PResult res;
+
+ for (int i = 1; i < argc; ++i)
+ {
+ filepath = argv[i];
+
+ FILE *handle = fopen(filepath, "r");
+ if (!handle)
+ {
+ fprintf(stderr, "ERROR: Could not open \"%s\"\n", filepath);
+ goto error;
+ }
+ file_data = fread_all(handle);
+ fclose(handle);
+
+ buffer = buffer_init_str(filepath, file_data, strlen(file_data));
+ res = parse_input(buffer);
+ if (res.nodes == NULL)
+ {
+ fputs("Exiting early...\n", stderr);
+ goto error;
+ }
+
+ char *str = ast_to_str(res.nodes, res.size);
+
+ printf("%s=>%s\n", filepath, str);
+
+ free(str);
+ free(res.nodes);
+ free(buffer);
+ free(file_data);
+ }
return 0;
error:
if (buffer)