summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter <peter@pmikkelsen.com>2026-08-29 16:23:56 +0000
committerpeter <peter@pmikkelsen.com>2026-08-29 16:23:56 +0000
commitd40b1684e245f2fbdfd561b77477b99a26e66560 (patch)
treede5423bf7f6d6646a8c7b439a83a3087a358c7c3
parenta01b62d7ace95aef73b9f2d33f01486e0856233f (diff)
Commit work in progress changes.
FossilOrigin-Name: ebe13a745f4816a4239d9dfe937bc9daf97ffc466e8bbdc412b05c3c891118fd
-rw-r--r--src/bin/main.c55
1 files changed, 47 insertions, 8 deletions
diff --git a/src/bin/main.c b/src/bin/main.c
index 93d28c0..e799202 100644
--- a/src/bin/main.c
+++ b/src/bin/main.c
@@ -11,10 +11,11 @@ main(int argc, char *argv[])
struct aplwc_line *line;
struct aplwc_value *value;
uint8_t *data;
- char *input;
+ char *input, *src;
int ret, exit_code, line_number;
- size_t memsize, input_size;
+ size_t memsize, input_size, src_size, i;
int opt, xflag, input_file;
+ long tmp;
FILE *f;
input = NULL;
@@ -64,6 +65,49 @@ usage:
return 1;
}
+ if(input_file){
+ if(fseek(f, 0, SEEK_END)){
+file_size:
+ fprintf(stderr, "Failed to determine file size\n");
+ return 1;
+ }
+ tmp = ftell(f);
+ if(tmp < 0)
+ goto file_size;
+ input_size = tmp;
+ rewind(f);
+ input = malloc(input_size);
+ if(input == NULL){
+ fprintf(stderr, "Failed to allocate space for file contents\n");
+ return 1;
+ }
+ if(fread(input, 1, input_size, f) < input_size){
+ fprintf(stderr, "Failed to read file contents\n");
+ return 1;
+ }
+ fclose(f);
+
+ src = input;
+ src_size = input_size;
+ if((input_size > 2) && (input[0] == '#') && (input[1] == '!')){
+ for(i = 0; i < input_size; i++){
+ if(input[i] == '\n'){
+ src += i;
+ src_size -= i;
+ xflag = 1;
+ break;
+ }
+ }
+ }
+
+ ret = aplwc_load_module(aplwc, "Main", src, src_size);
+ if(ret){
+ fprintf(stderr, "Couldn't load Main module from file %s: %s\n", argv[optind], aplwc_errstr_rc(ret));
+ return 1;
+ }
+ free(input);
+ }
+
if(xflag)
goto exit;
@@ -76,16 +120,11 @@ usage:
aplwc_free_value(value);
input = NULL;
- if(getline(&input, &input_size, f) < 0){
+ if(getline(&input, &input_size, stdin) < 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: