diff options
Diffstat (limited to 'src/bin/main.c')
| -rw-r--r-- | src/bin/main.c | 56 |
1 files changed, 48 insertions, 8 deletions
diff --git a/src/bin/main.c b/src/bin/main.c index 93d28c0..572d170 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,50 @@ 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); + input = NULL; + } + if(xflag) goto exit; @@ -76,16 +121,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: |