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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
#include "db.h"
char *keywords[NKEYWORDS] = {"EXIT", "DEBUG",
"CREATE", "DELETE", "OPEN", "SAVE", "CLOSE",
"ADDF", "ADDT", "DELETEF", "DELETET",
"TAG", "REMOVE",
"SHOW", "SHOWT", "SHOWF"};
argList *newArgList(void){
argList *args = malloc(sizeof(argList));
args->argc = 0;
args->argv = NULL;
return args;
}
argList *splitInput(inputBuffer *in){
if(in->inputSize == 0){
return NULL;
}
argList *args = newArgList();
uint64_t len = args->argc = 0;
// Remove leading spaces
while(isspace(in->buffer[len])){
++len;
}
// Count words (alone or quoted)
while(in->buffer[len] != '\0'){
while(isspace(in->buffer[len])){
++len;
}
if(in->buffer[len] == '"'){
do{
++len;
}while(in->buffer[len] != '"');
++args->argc;
}else if(in->buffer[len] == '\''){
do{
++len;
}while(in->buffer[len] != '\'');
++args->argc;
}else{
while(in->buffer[len] != '\0' && !isspace(in->buffer[len])){
++len;
}
++args->argc;
}
++len;
}
uint64_t *wstarts = malloc(args->argc*sizeof(uint64_t)), *wlens = malloc(args->argc*sizeof(uint64_t));
len = args->argc = 0;
while(in->buffer[len] != '\0'){
while(isspace(in->buffer[len])){
++len;
}
if(in->buffer[len] == '"'){
wstarts[args->argc] = len+1;
do{
++len;
}while(in->buffer[len] != '"');
wlens[args->argc] = len - wstarts[args->argc];
++args->argc;
}else if(in->buffer[len] == '\''){
wstarts[args->argc] = len+1;
do{
++len;
}while(in->buffer[len] != '\'');
wlens[args->argc] = len -wstarts[args->argc];
++args->argc;
}else{
wstarts[args->argc] = len;
while(in->buffer[len] != '\0' && !isspace(in->buffer[len])){
++len;
}
wlens[args->argc] = len - wstarts[args->argc];
++args->argc;
}
++len;
}
args->argv = malloc(args->argc*sizeof(char*));
for(uint64_t i = 0; i < args->argc; ++i){
args->argv[i] = calloc(wlens[i]+1, sizeof(char));
memcpy(args->argv[i], &in->buffer[wstarts[i]], wlens[i]);
}
return args;
}
static int commandExit(argList *args, database **db){
// TODO: cleanup functions
return EXIT_CODE;
}
static int commandDebug(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to debug\n");
return -1;
}
debugDatabase(*db);
return 0;
}
static int commandCreate(argList *args, database **db){
if(args->argc < 2){
fprintf(stderr, "Error, not enough arguments for command '%s'\n", args->argv[0]);
return -1;
}
for(int i = 1; i < args->argc; ++i){
int status = storeDatabase(newDatabase(args->argv[i]), args->argv[i]);
if(status != 0){
fprintf(stderr, "Error creating and storing database '%s'\n", args->argv[i]);
return -1;
}
}
return 0;
}
static int commandDelete(argList *args, database **db){
if(args->argc < 2){
fprintf(stderr, "Error, not enough arguments for command '%s'\n", args->argv[0]);
return -1;
}
for(int i = 1; i < args->argc; ++i){
database *t = loadDatabase(args->argv[i]);
int status = deleteDatabase(&t);
if(status != 0){
fprintf(stderr, "Error deleting database '%s'\n", args->argv[i]);
return -1;
}
}
return 0;
}
static int commandOpen(argList *args, database **db){
if(args->argc < 2){
fprintf(stderr, "Error: not enough arguments for command '%s'\n", args->argv[0]);
return -1;
}else if(args->argc > 2){
fprintf(stderr, "Error: too many arguments for command 'OPEN'. Only opening first one ('%s')\n", args->argv[1]);
}
if(*db != NULL){
fprintf(stderr, "Error: database '%s' already opened. Close it before proceeding with \"CLOSE '%s'\"\n", (*db)->name, (*db)->name);
return -1;
}
*db = loadDatabase(args->argv[1]);
if(*db == NULL){
fprintf(stderr, "Error loading database '%s'\n", args->argv[1]);
return -1;
}
return 0;
}
static int commandSave(argList *args, database **db){
if(args->argc > 1){
fprintf(stderr, "Error: too many arguments for function 'SAVE'. Only saving loaded database ('%s')\n", (*db)->name);
}
if(*db == NULL){
fprintf(stderr, "Error: no database to save\n");
return -1;
}
int status = storeDatabase(*db, (*db)->name);
if(status != 0){
fprintf(stderr, "Error storing database '%s'\n", (*db)->name);
return -1;
}
return 0;
}
static int commandClose(argList *args, database **db){
if(args->argc > 1){
fprintf(stderr, "Error: too many arguments for function 'CLOSE'. Only closing loaded database ('%s')\n", (*db)->name);
}
if(*db == NULL){
fprintf(stderr, "Error: no database to close\n");
return -1;
}
int status = storeDatabase(*db, (*db)->name);
if(status != 0){
fprintf(stderr, "Error storing database '%s' before closing it\n", (*db)->name);
return -1;
}
freeDatabase(db);
return 0;
}
static int commandAddf(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to add files to\n");
return -1;
}
char **files = malloc((args->argc-1)*sizeof(char*));
uint64_t fi = 0;
for(uint64_t i = 1; i < args->argc; ++i){
files[fi++] = args->argv[i];
}
if(fi == 0){
fprintf(stderr, "Error: no files given to add to database\n");
return -1;
}
for(uint64_t i = 0; i < fi; ++i){
addFile(*db, files[i]);
}
return 0;
}
static int commandAddt(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to add tags to\n");
return -1;
}
char **tags = malloc((args->argc-1)*sizeof(char*));
uint64_t ti = 0;
for(uint64_t i = 1; i < args->argc; ++i){
tags[ti++] = args->argv[i];
}
if(ti == 0){
fprintf(stderr, "Error: no tags given to add to database\n");
return -1;
}
for(uint64_t i = 0; i < ti; ++i){
addTag(*db, tags[i]);
}
return 0;
}
static int commandDeletef(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to delete files from\n");
return -1;
}
char **files = malloc((args->argc-1)*sizeof(char*));
uint64_t fi = 0;
for(uint64_t i = 1; i < args->argc; ++i){
files[fi++] = args->argv[i];
}
if(fi == 0){
fprintf(stderr, "Error: no files given to remove from database\n");
return -1;
}
for(uint64_t i = 0; i < fi; ++i){
removeFile(*db, files[i]);
}
return 0;
}
static int commandDeletet(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to remove tags from\n");
return -1;
}
char **tags = malloc((args->argc-1)*sizeof(char*));
uint64_t ti = 0;
for(uint64_t i = 1; i < args->argc; ++i){
tags[ti++] = args->argv[i];
}
if(ti == 0){
fprintf(stderr, "Error: no tags given to remove from database\n");
return -1;
}
for(uint64_t i = 0; i < ti; ++i){
removeTag(*db, tags[i]);
}
return 0;
}
static int commandTag(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to add files and tags to\n");
return -1;
}
char **files = malloc((args->argc-1)*sizeof(char*));
uint64_t i, fi = 0, ti = 0;
bool with = false;
for(i = 1; i < args->argc; ++i){
uint32_t l1, l2;
if(sameStr(normalizeStr(args->argv[i], &l1), normalizeStr("WITH", &l2))){
with = true;
++i;
break;
}
files[fi++] = args->argv[i];
}
if(fi == 0){
fprintf(stderr, "Error: no files given to tag\n");
return -1;
}
if(with == false){
fprintf(stderr, "Error: missing 'WITH' keyword on 'TAG' command\n");
return -1;
}
char **tags = malloc((args->argc-i)*sizeof(char*));
for(; i < args->argc; ++i){
tags[ti++] = args->argv[i];
}
if(ti == 0){
fprintf(stderr, "Error: no tags given to tag files\n");
return -1;
}
for(uint64_t f = 0; f < fi; ++f){
for(uint64_t t = 0; t < ti; ++t){
int status = addFileTag(*db, files[f], tags[t]);
if(status != 0){
fprintf(stderr, "Error: couldnt add tag '%s' to file '%s'\n", tags[t], files[f]);
return -1;
}
}
}
return 0;
}
static int commandRemove(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to remove files and tags from\n");
return -1;
}
char **tags = malloc((args->argc-1)*sizeof(char*));
uint64_t i, ti = 0, fi = 0;
bool from = false;
for(i = 1; i < args->argc; ++i){
uint32_t l1, l2;
if(sameStr(normalizeStr(args->argv[i], &l1), normalizeStr("FROM", &l2))){
from = true;
++i;
break;
}
tags[ti++] = args->argv[i];
}
if(ti == 0){
fprintf(stderr, "Error: no tags given to remove\n");
return -1;
}
if(from == false){
fprintf(stderr, "Error: missing 'FROM' keyword on 'REMOVE' command\n");
return -1;
}
char **files = malloc((args->argc-i)*sizeof(char*));
for(; i < args->argc; ++i){
files[fi++] = args->argv[i];
}
if(fi == 0){
fprintf(stderr, "Error: no files given to remove\n");
return -1;
}
for(uint64_t f = 0; f < fi; ++f){
for(uint64_t t = 0; t < ti; ++t){
int status = removeFileTag(*db, files[f], tags[t]);
if(status != 0){
fprintf(stderr, "Error: couldnt remove tag '%s' from file '%s'\n", tags[t], files[f]);
return -1;
}
}
}
return 0;
}
static int commandShow(argList *args, database **db){
if(*db == NULL){
fprintf(stderr, "Error: no database to show\n");
return -1;
}
printDatabase(*db);
return 0;
}
static int commandShowt(argList *args, database **db){
// We only show the files of one tag
if(*db == NULL){
fprintf(stderr, "Error: no database to show tags of file\n");
return -1;
}
if(args->argc > 2){
fprintf(stderr, "Error: too many arguments for 'SHOWT', current support for only one file. Only showing tags of first file ('%s')\n", args->argv[1]);
}
uint64_t rl, *r;
int status = searchFile(*db, args->argv[1], 0, &r, &rl);
if(status == -1){
fprintf(stderr, "Error searching for file '%s'\n", args->argv[1]);
return -1;
}
printf("Tags of file '%s' (%"PRIu64"):\n", args->argv[1], rl);
for(uint64_t i = 0; i < rl; ++i){
printf("\t- %s\n", (*db)->ltags->table[r[i]]);
}
printf("\n");
}
static int commandShowf(argList *args, database **db){
// We only show the tags of one file
if(*db == NULL){
fprintf(stderr, "Error: no database to show files of tag\n");
return -1;
}
if(args->argc > 2){
fprintf(stderr, "Error: too many arguments for 'SHOWF', current support for only one tag. Only showing files of first tag ('%s')\n", args->argv[1]);
}
uint64_t rl, *r;
int status = searchTag(*db, args->argv[1], 0, &r, &rl);
if(status == -1){
fprintf(stderr, "Error searching for tag '%s'\n", args->argv[1]);
return -1;
}
printf("Files of tag '%s' (%"PRIu64"):\n", args->argv[1], rl);
for(uint64_t i = 0; i < rl; ++i){
printf("\t- %s\n", (*db)->lfiles->table[r[i]]);
}
printf("\n");
}
int (*commands[])(argList*, database**) = {
commandExit, commandDebug,
commandCreate, commandDelete, commandOpen, commandSave, commandClose,
commandAddf, commandAddt, commandDeletef, commandDeletet,
commandTag, commandRemove,
commandShow, commandShowt, commandShowf
};
int handleInput(argList *args, database **db){
if(args == NULL){
return 0;
}
int command = -1;
for(int i = 0; i < NKEYWORDS; ++i){
uint32_t l1, l2;
if(sameStr(normalizeStr(args->argv[0], &l1), normalizeStr(keywords[i], &l2))){
command = i;
break;
}
}
if(command == -1){
fprintf(stderr, "Error: command '%s' not found\n", args->argv[0]);
return -1;
}
return commands[command](args, db);
}
|