-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.c
240 lines (206 loc) · 6.43 KB
/
cli.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#define MAX_LINE 80
/**
* Read at most `MAX_LINE` characters (plus terminal newline) from `stdin` into
* the buffer `dest`. If the line is too long, will reprompt automatically.
* Returns a bool indicating whether `stdin` is still open or not.
*/
bool read_cmd(char *dest) {
while (true) {
// Return false when input has been closed.
if (feof(stdin)) return false;
printf("> ");
// Process at most MAX_LINE characters from stdin.
for (int i = 0; i <= MAX_LINE; i += 1) {
char ch = getchar();
if (ch == '\n' || ch == '\0') {
dest[i] = '\0';
return true;
}
dest[i] = ch;
}
// We overran the max line length. Consume the rest of the line and
// reprompt.
for (char ch = getchar(); ch != '\n' && ch != '\0'; ch = getchar());
printf(" error; line too long (> 80)\n");
}
}
/**
* Clean up an existing map's memory, freeing both the map itself and its
* contents.
*/
void do_cleanup(map m) {
if (m != NULL) {
for (const char *key = map_first(m); key != NULL; key = map_next(m, key)) {
free(map_get(m, key));
}
map_destroy(m);
}
}
/**
* Make sure a map has been initialized before trying to operate on it. If `m`
* is NULL, will print out an error message suggesting the `init` command and
* then return false. Otherwise, returns true.
*/
bool ensure_exists(map m) {
if (m != NULL) return true;
printf(" error; use `init` first to initialize a new empty map\n");
return false;
}
/**
* Parse a line that should not contain any other arguments. Returns
* `true`/`false` to indicate success.
*/
bool parse(char *line, char *cmd) {
if (strtok(NULL, " ") != NULL) {
printf(" error; use format `%s`\n", cmd);
return false;
}
return true;
}
/**
* Parse a line with a single token argument. The found token string
* (dynamically allocated) will be placed into `tok`. Returns `true`/`false` to
* indicate success.
*/
bool parse_s(char *line, char *cmd, char **tok) {
char *token = strtok(NULL, " ");
if (token == NULL || strtok(NULL, " ") != NULL) {
printf(" error; use format `%s %%[^ ]`\n", cmd);
return false;
}
*tok = strdup(token);
return true;
}
/**
* Parse a line with two token arguments. The found token strings (both
* dynamically allocated) will be placed into `tok1` and `tok2`. Returns
* `true`/`false` to indicate success.
*/
bool parse_ss(char *line, char *cmd, char **tok1, char **tok2) {
char *token1 = strtok(NULL, " ");
char *token2 = strtok(NULL, " ");
if (token2 == NULL || strtok(NULL, " ") != NULL) {
printf(" error; use format `%s %%[^ ] %%[^ ]`\n", cmd);
return false;
}
*tok1 = strdup(token1);
*tok2 = strdup(token2);
return true;
}
/**
* Accepts a command string `command` and runs the correct routine.
*/
void run_cmd(char *line) {
// Stores the map manipulated by the shell.
static map m = NULL;
// Extract the command from the string using `strtok`.
char *cmd = strtok(line, " ");
if (cmd == NULL) return;
// Command: `help`. List commands.
if (strcmp(cmd, "help") == 0) {
if (!parse(line, cmd)) return;
printf(" help List available commands\n");
printf(" exit/quit/q Exit map shell\n");
printf(" init Initialize new empty map\n");
printf(" size Get current map size\n");
printf(" ls/print/dump Get all map contents\n");
printf(" contains <key> Check if map contains <key>\n");
printf(" set <key> <value> Set <value> for <key>\n");
printf(" get <key> Get the value for <key>\n");
printf(" remove/rm <key> Remove the value for <key>\n");
}
// Command: `exit`, `quit`. Closes the shell.
else if (strcmp(cmd, "exit") == 0 || strcmp(cmd, "quit") == 0 ||
strcmp(cmd, "q") == 0) {
if (!parse(line, cmd)) return;
exit(0);
}
// Command: `init`. Creates a new, empty map.
else if (strcmp(cmd, "init") == 0) {
if (!parse(line, cmd)) return;
do_cleanup(m);
m = map_create();
}
// Command: `size`. Gets the current size of the map.
else if (strcmp(cmd, "size") == 0) {
if (!parse(line, cmd)) return;
if (!ensure_exists(m)) return;
printf(" |m| = %d\n", map_size(m));
}
// Command: `ls`, `dump`, `print`. Prints the full map contents.
else if (strcmp(cmd, "ls") == 0 || strcmp(cmd, "dump") == 0 ||
strcmp(cmd, "print") == 0) {
if (!parse(line, cmd)) return;
if (!ensure_exists(m)) return;
for (const char *key = map_first(m); key != NULL; key = map_next(m, key)) {
const char *value = map_get(m, key);
printf(" %s: %s\n", key, value);
}
}
// Command: `contains %[^ ]`. Check if map contains a key.
else if (strcmp(cmd, "contains") == 0) {
char *key;
if (!parse_s(line, cmd, &key)) return;
if (!ensure_exists(m)) return;
if (map_contains(m, key)) {
printf(" true\n");
} else {
printf(" false\n");
}
free(key);
}
// Command: `set %[^ ] %[^ ]`. Set a new value for a key.
else if (strcmp(cmd, "set") == 0) {
char *key;
char *value;
if (!parse_ss(line, cmd, &key, &value)) return;
if (!ensure_exists(m)) return;
map_set(m, key, value);
printf(" %s: %s\n", key, value);
free(key);
}
// Command: `get %d`. Prints the value for a given key.
else if (strcmp(cmd, "get") == 0) {
char *key;
if (!parse_s(line, cmd, &key)) return;
if (!ensure_exists(m)) return;
// Cannot get a nonexistent key.
if (!map_contains(m, key)) {
printf(" error; key not found\n");
} else {
char *value = map_get(m, key);
printf(" %s: %s\n", key, value);
}
free(key);
}
// Command: `remove %[^ ]`. Remove the entry with a given key.
else if (strcmp(cmd, "remove") == 0 || strcmp(cmd, "rm") == 0) {
char *key;
if (!parse_s(line, cmd, &key)) return;
if (!ensure_exists(m)) return;
// Cannot remove nonexistent key.
if (!map_contains(m, key)) {
printf(" error; key not found\n");
} else {
char *value = map_remove(m, key);
printf(" %s: <deleted>\n", key);
free(value);
}
free(key);
}
else {
printf(" error; unknown command (%s)\n", cmd);
}
}
int main() {
printf("Map CLI; use `help` if you are totally lost.\n");
char command[MAX_LINE + 1];
while (read_cmd(command)) {
run_cmd(command);
}
return 0;
}