Skip to content

Commit 3bf8519

Browse files
Inline single-use load_extension_input and read_line_dynamic (closes #216)
1 parent 423b2f1 commit 3bf8519

1 file changed

Lines changed: 23 additions & 32 deletions

File tree

src/main.c

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,6 @@ static int buf_append(char **buf, size_t *len, size_t *cap, const char *s) {
7474
return 0;
7575
}
7676

77-
static char *read_line_dynamic(FILE *in) {
78-
if (!in) {
79-
return NULL;
80-
}
81-
char *line = NULL;
82-
size_t len = 0;
83-
size_t cap = 0;
84-
char chunk[512];
85-
86-
while (fgets(chunk, sizeof(chunk), in)) {
87-
if (buf_append(&line, &len, &cap, chunk) != 0) {
88-
free(line);
89-
return NULL;
90-
}
91-
size_t n = strlen(chunk);
92-
if (n > 0 && chunk[n - 1] == '\n') {
93-
break;
94-
}
95-
}
96-
97-
if (len == 0 && feof(in)) {
98-
free(line);
99-
return NULL;
100-
}
101-
102-
if (!line) {
103-
line = strdup("");
104-
}
105-
return line;
106-
}
107-
10877
static int is_exit_meta_command(const char *text) {
10978
if (!text) {
11079
return 0;
@@ -199,7 +168,29 @@ static int run_repl(int verbose, int private_flag) {
199168
fputs(in_continuation ? "\x1b[38;2;153;221;255m..>\033[0m " : "\x1b[38;2;153;221;255m>>>\033[0m ", stdout);
200169
fflush(stdout);
201170

202-
char *line = read_line_dynamic(stdin);
171+
char *line = NULL;
172+
{
173+
size_t rllen = 0;
174+
size_t rlcap = 0;
175+
char chunk[512];
176+
while (fgets(chunk, sizeof(chunk), stdin)) {
177+
if (buf_append(&line, &rllen, &rlcap, chunk) != 0) {
178+
free(line);
179+
line = NULL;
180+
break;
181+
}
182+
size_t n = strlen(chunk);
183+
if (n > 0 && chunk[n - 1] == '\n') {
184+
break;
185+
}
186+
}
187+
if (rllen == 0 && feof(stdin)) {
188+
free(line);
189+
line = NULL;
190+
} else if (!line) {
191+
line = strdup("");
192+
}
193+
}
203194
int eof = (line == NULL);
204195

205196
if (!eof) {

0 commit comments

Comments
 (0)