From 130fc6d97bce96bc042bdaa6329b82d00f6316d5 Mon Sep 17 00:00:00 2001 From: Louis Burda Date: Mon, 17 May 2021 21:23:49 +0200 Subject: renamed to STLDoctor --- service/src/Makefile | 6 +- service/src/main.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++ service/src/msgs/welcome | 2 +- service/src/printdoc.c | 282 ----------------------------------------------- service/src/stldoctor | Bin 0 -> 46736 bytes service/src/stlfile.c | 8 +- 6 files changed, 290 insertions(+), 290 deletions(-) create mode 100644 service/src/main.c delete mode 100644 service/src/printdoc.c create mode 100755 service/src/stldoctor (limited to 'service') diff --git a/service/src/Makefile b/service/src/Makefile index 6b981a2..c46988a 100644 --- a/service/src/Makefile +++ b/service/src/Makefile @@ -2,13 +2,13 @@ CFLAGS = -g -I . .PHONY: all clean -all: printdoc +all: stldoctor clean: - rm -f printdoc *.o + rm -f stldoctor *.o %.o: %.c %.h $(CC) -c -o $@ $< $(CFLAGS) $(LDLIBS) -printdoc: printdoc.c stlfile.o util.o +stldoctor: main.c stlfile.o util.o $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS) diff --git a/service/src/main.c b/service/src/main.c new file mode 100644 index 0000000..1e549c9 --- /dev/null +++ b/service/src/main.c @@ -0,0 +1,282 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "stlfile.h" +#include "util.h" + +#define MAXFILESIZE 7000 + +struct command { + const char *name; + void (*func)(char *); +}; + +int save_submission(struct parseinfo *info, char *data, int len); + +void cat_cmd(char *arg); +void list_cmd(char *arg); +void exit_cmd(char *arg); +void echo_cmd(char *arg); +void submit_cmd(char *arg); +void query_cmd(char *arg); + +struct command commands[] = { + { "cat", cat_cmd }, + { "help", list_cmd }, + { "exit", exit_cmd }, + { "echo", echo_cmd }, + { "submit", submit_cmd }, + { "query", query_cmd }, +}; + +struct parseinfo cached; +const char *resultdir; +int echo = 0; + +int +save_submission(struct parseinfo *info, char *stldata, int stlsize) +{ + DIR *d; + FILE *f; + char *dirpath = NULL, *infopath = NULL, *modelpath = NULL; + + dirpath = aprintf("%s/%s-%i", resultdir, info->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); + f = NULL; + + infopath = aprintf("%s/%s", dirpath, "info"); + if (!(f = fopen(infopath, "w+"))) goto fail; + if (save_info(info, f) != OK) goto fail; + fclose(f); + f = NULL; + + free(dirpath); + free(modelpath); + free(infopath); + + return OK; + +fail: + if (f) fclose(f); + + remove(infopath); + remove(modelpath); + remove(dirpath); + + free(dirpath); + free(modelpath); + free(infopath); + + return FAIL; +} + +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"); +} + +void +exit_cmd(char *arg) +{ + exit(0); +} + +void +echo_cmd(char *arg) +{ + echo ^= 1; + printf("Echo is %s\n", echo ? "enabled" : "disabled"); +} + +void +submit_cmd(char *arg) +{ + const char *bufp; + char *end, *contents; + size_t len; + + bufp = ask("> How large is your file? "); + len = strtoul(bufp, &end, 10); + if (len <= 0 || len >= MAXFILESIZE || *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'; + + if ((cached.valid = parse_file(&cached, contents, len))) { + if (save_submission(&cached, contents, len) != OK) + fprintf(stderr, "Failed to save your submission!\n"); + else + printf("> Your file was saved with ID %s!\n", cached.hash); + } + + free(contents); +} + +void +query_cmd(char *arg) +{ + char *end, *scandir = NULL, *infopath = NULL, *modelpath = NULL; + const char *hash; + struct dirent *de; + int i, which, dirstart; + DIR *d = NULL; + FILE *f = NULL; + size_t size; + + if (cached.valid) + hash = cached.hash; + else + hash = mhash(ask("> What is the model name? "), -1); + + if (!(d = opendir(resultdir))) return; + + dirstart = telldir(d); + for (i = 0; (de = readdir(d));) { + if (!strpfcmp(hash, de->d_name) && *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? "), &end, 10); + if (which >= i || which < 0 || *end) { + fprintf(stderr, "Invalid index!\n"); + goto cleanup; + } + } + + seekdir(d, dirstart); + for (i = 0; (de = readdir(d));) { + if (!strpfcmp(hash, de->d_name) && *de->d_name != '.') { + if (i == which) { + scandir = aprintf("%s/%s", resultdir, de->d_name); + break; + } + i++; + } + } + + if (!scandir) { + fprintf(stderr, "Unexpected error!\n"); + goto cleanup; + } + + infopath = aprintf("%s/%s", scandir, "info"); + if (!(f = fopen(infopath, "r"))) goto cleanup; + free_info(&cached); + if (load_info(&cached, f) != OK) goto cleanup; + fclose(f); + f = NULL; + + print_info(&cached); + + if (strchr(ask("> Download the model? "), 'y')) { + modelpath = aprintf("%s/%s", scandir, "model"); + if (!(f = fopen(modelpath, "r"))) goto cleanup; + fseek(f, 0, SEEK_END); + size = ftell(f); + fseek(f, 0, SEEK_SET); + if (size > MAXFILESIZE) goto cleanup; + printf("> Here you go.. (%liB)\n", size); + while ((i = getc(f)) != EOF) + putc(i, stdout); + fclose(f); + f = NULL; + } + +cleanup: + if (f) fclose(f); + closedir(d); + free(scandir); + free(infopath); + free(modelpath); +} + +void +cleanexit() +{ + printf("see you later!\n"); + free_info(&cached); +} + +int +main() +{ + char linebuf[256], *cp, *arg; + int exit, i; + + if (!(resultdir = getenv("RESULTDIR"))) + resultdir = "scans"; + + setvbuf(stdin, NULL, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + atexit(cleanexit); + + 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; + if (linebuf[strlen(linebuf) - 1] == '\n') + linebuf[strlen(linebuf) - 1] = '\0'; + + if (echo) printf("%s\n", linebuf); + + 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"); + } +} diff --git a/service/src/msgs/welcome b/service/src/msgs/welcome index b4eb256..2f77bfb 100644 --- a/service/src/msgs/welcome +++ b/service/src/msgs/welcome @@ -1,2 +1,2 @@ -Welcome to PrintDoc! +Welcome to STLDoctor! Submit a stl file and we'll analyze it! diff --git a/service/src/printdoc.c b/service/src/printdoc.c deleted file mode 100644 index 1e549c9..0000000 --- a/service/src/printdoc.c +++ /dev/null @@ -1,282 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "stlfile.h" -#include "util.h" - -#define MAXFILESIZE 7000 - -struct command { - const char *name; - void (*func)(char *); -}; - -int save_submission(struct parseinfo *info, char *data, int len); - -void cat_cmd(char *arg); -void list_cmd(char *arg); -void exit_cmd(char *arg); -void echo_cmd(char *arg); -void submit_cmd(char *arg); -void query_cmd(char *arg); - -struct command commands[] = { - { "cat", cat_cmd }, - { "help", list_cmd }, - { "exit", exit_cmd }, - { "echo", echo_cmd }, - { "submit", submit_cmd }, - { "query", query_cmd }, -}; - -struct parseinfo cached; -const char *resultdir; -int echo = 0; - -int -save_submission(struct parseinfo *info, char *stldata, int stlsize) -{ - DIR *d; - FILE *f; - char *dirpath = NULL, *infopath = NULL, *modelpath = NULL; - - dirpath = aprintf("%s/%s-%i", resultdir, info->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); - f = NULL; - - infopath = aprintf("%s/%s", dirpath, "info"); - if (!(f = fopen(infopath, "w+"))) goto fail; - if (save_info(info, f) != OK) goto fail; - fclose(f); - f = NULL; - - free(dirpath); - free(modelpath); - free(infopath); - - return OK; - -fail: - if (f) fclose(f); - - remove(infopath); - remove(modelpath); - remove(dirpath); - - free(dirpath); - free(modelpath); - free(infopath); - - return FAIL; -} - -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"); -} - -void -exit_cmd(char *arg) -{ - exit(0); -} - -void -echo_cmd(char *arg) -{ - echo ^= 1; - printf("Echo is %s\n", echo ? "enabled" : "disabled"); -} - -void -submit_cmd(char *arg) -{ - const char *bufp; - char *end, *contents; - size_t len; - - bufp = ask("> How large is your file? "); - len = strtoul(bufp, &end, 10); - if (len <= 0 || len >= MAXFILESIZE || *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'; - - if ((cached.valid = parse_file(&cached, contents, len))) { - if (save_submission(&cached, contents, len) != OK) - fprintf(stderr, "Failed to save your submission!\n"); - else - printf("> Your file was saved with ID %s!\n", cached.hash); - } - - free(contents); -} - -void -query_cmd(char *arg) -{ - char *end, *scandir = NULL, *infopath = NULL, *modelpath = NULL; - const char *hash; - struct dirent *de; - int i, which, dirstart; - DIR *d = NULL; - FILE *f = NULL; - size_t size; - - if (cached.valid) - hash = cached.hash; - else - hash = mhash(ask("> What is the model name? "), -1); - - if (!(d = opendir(resultdir))) return; - - dirstart = telldir(d); - for (i = 0; (de = readdir(d));) { - if (!strpfcmp(hash, de->d_name) && *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? "), &end, 10); - if (which >= i || which < 0 || *end) { - fprintf(stderr, "Invalid index!\n"); - goto cleanup; - } - } - - seekdir(d, dirstart); - for (i = 0; (de = readdir(d));) { - if (!strpfcmp(hash, de->d_name) && *de->d_name != '.') { - if (i == which) { - scandir = aprintf("%s/%s", resultdir, de->d_name); - break; - } - i++; - } - } - - if (!scandir) { - fprintf(stderr, "Unexpected error!\n"); - goto cleanup; - } - - infopath = aprintf("%s/%s", scandir, "info"); - if (!(f = fopen(infopath, "r"))) goto cleanup; - free_info(&cached); - if (load_info(&cached, f) != OK) goto cleanup; - fclose(f); - f = NULL; - - print_info(&cached); - - if (strchr(ask("> Download the model? "), 'y')) { - modelpath = aprintf("%s/%s", scandir, "model"); - if (!(f = fopen(modelpath, "r"))) goto cleanup; - fseek(f, 0, SEEK_END); - size = ftell(f); - fseek(f, 0, SEEK_SET); - if (size > MAXFILESIZE) goto cleanup; - printf("> Here you go.. (%liB)\n", size); - while ((i = getc(f)) != EOF) - putc(i, stdout); - fclose(f); - f = NULL; - } - -cleanup: - if (f) fclose(f); - closedir(d); - free(scandir); - free(infopath); - free(modelpath); -} - -void -cleanexit() -{ - printf("see you later!\n"); - free_info(&cached); -} - -int -main() -{ - char linebuf[256], *cp, *arg; - int exit, i; - - if (!(resultdir = getenv("RESULTDIR"))) - resultdir = "scans"; - - setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - atexit(cleanexit); - - 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; - if (linebuf[strlen(linebuf) - 1] == '\n') - linebuf[strlen(linebuf) - 1] = '\0'; - - if (echo) printf("%s\n", linebuf); - - 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"); - } -} diff --git a/service/src/stldoctor b/service/src/stldoctor new file mode 100755 index 0000000..6038d97 Binary files /dev/null and b/service/src/stldoctor differ diff --git a/service/src/stlfile.c b/service/src/stlfile.c index 60f8b40..024f6cb 100644 --- a/service/src/stlfile.c +++ b/service/src/stlfile.c @@ -251,7 +251,7 @@ int parse_file(struct parseinfo *info, char *buf, size_t len) { int status; - const char *tmp; + const char *resp; char *bp; if (info->valid) free_info(info); @@ -270,12 +270,12 @@ parse_file(struct parseinfo *info, char *buf, size_t len) if (status == FAIL) return FAIL; if (!info->modelname) { - tmp = ask("Please enter your model name:\n"); - if (strlen(tmp) < 4) { + resp = ask("Please enter your model name:\n"); + if (strlen(resp) < 4) { fprintf(stderr, "Model name is too short!\n"); return FAIL; } - info->modelname = checkp(strdup(tmp)); + info->modelname = checkp(strdup(resp)); } info->hash = checkp(strdup(mhash(info->modelname, -1))); -- cgit v1.2.3-71-gd317