-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
37 lines (31 loc) · 808 Bytes
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "aash.h"
struct array tokenizer(char *string, const char *delim) {
struct array ret = {malloc(sizeof(char *)), 1};
char *tmpbuffer = NULL;
while (string) {
tmpbuffer = strsep(&string, delim);
if (strlen(tmpbuffer)) {
ret.items[ret.numitems - 1] = tmpbuffer;
ret.items = (char **) realloc(ret.items, sizeof(char *) * ++ret.numitems);
}
}
ret.items[ret.numitems - 1] = (char *) NULL;
return ret;
}
struct array tokenizer_whitespace(char *string) {
return tokenizer(string, SPACE);
}
struct array tokenizer_semicolon(char *string) {
return tokenizer(string, ";");
}
char *get_input() {
char *input = NULL;
while (! input) {
print_prompt();
if (scanf("%m[^\n]", &input) == EOF) {
return NULL;
}
scanf("%*c");
}
return input;
}