summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/codegen_decl.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index 4979236..42e0ba9 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -93,11 +93,18 @@ void emit_preamble(ParserContext *ctx, FILE *out)
"\"Assertion failed: \" "
"__VA_ARGS__); exit(1); }\n",
out);
- fputs("string _z_readln_raw() { char *line = NULL; size_t len = 0; "
- "if(getline(&line, &len, "
- "stdin) == -1) return NULL; if(strlen(line) > 0 && "
- "line[strlen(line)-1] == '\\n') "
- "line[strlen(line)-1] = 0; return line; }\n",
+ fputs("string _z_readln_raw() { "
+ "size_t cap = 64; size_t len = 0; "
+ "char *line = z_malloc(cap); "
+ "if(!line) return NULL; "
+ "int c; "
+ "while((c = fgetc(stdin)) != EOF) { "
+ "if(c == '\\n') break; "
+ "if(len + 1 >= cap) { cap *= 2; char *n = z_realloc(line, cap); "
+ "if(!n) { z_free(line); return NULL; } line = n; } "
+ "line[len++] = c; } "
+ "if(len == 0 && c == EOF) { z_free(line); return NULL; } "
+ "line[len] = 0; return line; }\n",
out);
fputs("int _z_scan_helper(const char *fmt, ...) { char *l = "
"_z_readln_raw(); if(!l) return "