summaryrefslogtreecommitdiff
path: root/src/bin
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-22 19:51:13 +0000
committerpeter <peter@pmikkelsen.com>2026-08-22 19:51:13 +0000
commit956cfb9ac26f44d05ffc446f30f84174865685a4 (patch)
treebdfb6203b99809575ec18b3564d73ba75aed290a /src/bin
parentdc9b7f961d1c5f39a07085c9dbad8d94bbacde70 (diff)
Start work on a way to test #! scripts
FossilOrigin-Name: 0637c5749d38b540e2a174b5248bf704098fd4ead61aa1d6e84d4495430c7149
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/main.c97
1 files changed, 91 insertions, 6 deletions
diff --git a/src/bin/main.c b/src/bin/main.c
index 80b2ee0..93d28c0 100644
--- a/src/bin/main.c
+++ b/src/bin/main.c
@@ -1,13 +1,53 @@
+#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
#include "aplwc_repl.h"
int
-main(void)
+main(int argc, char *argv[])
{
struct aplwc *aplwc;
+ struct aplwc_line *line;
+ struct aplwc_value *value;
uint8_t *data;
- int ret;
- size_t memsize;
+ char *input;
+ int ret, exit_code, line_number;
+ size_t memsize, input_size;
+ int opt, xflag, input_file;
+ FILE *f;
+
+ input = NULL;
+ line = NULL;
+ value = NULL;
+ xflag = 0;
+ input_file = 0;
+ exit_code = 0;
+ line_number = 0;
+
+ while((opt = getopt(argc, argv, "x")) != -1){
+ switch(opt){
+ case 'x':
+ xflag = 1;
+ break;
+ default:
+usage:
+ fprintf(stderr, "Usage: %s [-x] [file]\n", argv[0]);
+ return 1;
+ }
+ }
+
+ if((optind+1) == argc){
+ input_file = 1;
+ f = fopen(argv[optind], "r");
+ if(f == NULL){
+ fprintf(stderr, "Could not open file: %s: %s\n", argv[optind], strerror(errno));
+ return 1;
+ }
+ }else if(optind == argc)
+ f = stdin;
+ else
+ goto usage;
memsize = getenv_ulong("APLWC_MEM", APLWC_MIN_MEM);
@@ -24,9 +64,54 @@ main(void)
return 1;
}
- aplwc_destroy(aplwc);
+ if(xflag)
+ goto exit;
- free(data);
+ for(;;){
+ if(input)
+ free(input);
+ if(line)
+ aplwc_free_line(line);
+ if(value)
+ aplwc_free_value(value);
- return 0;
+ input = NULL;
+ if(getline(&input, &input_size, f) < 0){
+ free(input);
+ break;
+ }else{
+ line_number++;
+ if(input_file && (line_number == 1)){
+ if((strlen(input) > 2) && (input[0] == '#') && (input[1] == '!'))
+ continue;
+ }
+
+ line = aplwc_read(aplwc, input);
+ if(line == NULL){
+line_error:
+ fprintf(stderr, "error on line %d: %s: %s\n", line_number, aplwc_errstr(aplwc), input);
+ if(input_file){
+ exit_code = 1;
+ goto exit;
+ }
+ continue;
+ }
+ value = aplwc_eval(line);
+ if(value == NULL)
+ goto line_error;
+ fprintf(stdout, "%s\n", aplwc_value_string(value));
+ }
+ }
+
+exit:
+ if(input)
+ free(input);
+ if(line)
+ aplwc_free_line(line);
+ if(value)
+ aplwc_free_value(value);
+
+ aplwc_destroy(aplwc);
+ free(data);
+ return exit_code;
}