#include #include #include #include #include #include #include #include #include "stlfile.h" #include "util.h" struct command { const char *name; void (*func)(char *); }; void search_cmd(char *arg); void submit_cmd(char *arg); void list_cmd(char *arg); void cat_cmd(char *arg); struct command commands[] = { { "submit", submit_cmd }, { "cat", cat_cmd }, { "help", list_cmd }, { "search", search_cmd } }; struct parseinfo lastrun; const char *resultdir; void search_cmd(char *arg) { char *end, *resultfile = NULL; const char *hash; struct dirent *de; int i, which; DIR *d = NULL; if (lastrun.valid) hash = lastrun.hash; else hash = mhash(ask("What is the model name?\n"), -1); d = opendir("scans"); if (!d) return; i = 0; while ((de = readdir(d))) { if (de->d_type != DT_DIR && !strpfcmp(hash, de->d_name)) { printf("%i : %s\n", i, de->d_name); i++; } } if (i == 0) { fprintf(stderr, "Sorry, couldnt find a matching scan result!\n"); goto cleanup; } else { which = strtoul(ask("Which of these results?\n"), &end, 10); if (which >= i || which < 0 || *end) { fprintf(stderr, "Invalid index!\n"); goto cleanup; } } while ((de = readdir(d))) { if (de->d_type != DT_DIR && !strpfcmp(hash, de->d_name)) { if (i == which) { resultfile = aprintf("scans/%s/info", strdup(de->d_name)); break; } i++; } } if (resultfile) dump(resultfile); cleanup: free(resultfile); closedir(d); } int save_submission(char *stldata, int stlsize) { DIR *d; FILE *f; char *dirpath = NULL, *infopath = NULL, *modelpath = NULL; dirpath = aprintf("%s/%s-%i", resultdir, lastrun.hash, time(NULL)); if (mkdir(dirpath, S_IRWXU | S_IRWXG | S_IRWXO)) goto fail; modelpath = aprintf("%s/%s", dirpath, "model"); if (!(f = fopen(modelpath, "w+"))) goto fail; if (fwrite(stldata, 1, stlsize, f) != stlsize) goto fail; fclose(f); infopath = aprintf("%s/%s", dirpath, "info"); if (!(f = fopen(infopath, "w+"))) goto fail; if (save_info(&lastrun, f) != OK) goto fail; fclose(f); free(dirpath); free(modelpath); free(infopath); return OK; fail: remove(infopath); remove(modelpath); remove(dirpath); free(dirpath); free(modelpath); free(infopath); return FAIL; } void submit_cmd(char *arg) { const char *bufp; char *end, *contents; size_t len; bufp = ask("How large is your file?\n"); len = strtoul(bufp, &end, 10); if (len <= 0 || len >= 10000 || *end) { fprintf(stderr, "Invalid file length!\n"); return; } printf("Ok! Im listening..\n"); contents = checkp(malloc(len + 1)); fread(contents, 1, len, stdin); contents[len] = '\0'; lastrun.valid = parse_file(&lastrun, contents, len); if (lastrun.valid) { if (save_submission(contents, len) != OK) fprintf(stderr, "Failed to save your submission!\n"); else printf(">> Your file was saved with ID %s!\n", lastrun.hash); } free(contents); } void cat_cmd(char *arg) { if (arg && !strncmp(arg, "flag", 4)) dump("msgs/cat_flag"); else printf("meow\n"); } void list_cmd(char *arg) { int i; printf("Available commands:\n"); for (i = 0; i < ARRSIZE(commands); i++) printf("%s%s", i ? " " : "", commands[i].name); printf("\n"); } int main() { char linebuf[256], *cp, *arg; int exit, i; if (!(resultdir = getenv("RESULTDIR"))) resultdir = "/data"; dump("msgs/welcome"); exit = 0; while (!exit) { memset(linebuf, '\0', sizeof(linebuf)); printf("$ "); exit = !fgets(linebuf, sizeof(linebuf), stdin); if (exit || !*linebuf) break; if (*linebuf == '\n') continue; linebuf[strlen(linebuf) - 1] = '\0'; cp = strchr(linebuf, ' '); arg = cp ? cp + 1 : NULL; if (cp) *cp = 0; for (i = 0; i < ARRSIZE(commands); i++) { if (!strcmp(commands[i].name, linebuf)) { commands[i].func(arg); break; } } if (i == ARRSIZE(commands) && strlen(linebuf) != 0) fprintf(stderr, "No such command!\n"); } printf("see you later!\n"); free_info(&lastrun); }