aboutsummaryrefslogtreecommitdiffstats
path: root/service/src/main.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-06-24 21:23:02 +0200
committerLouis Burda <quent.burda@gmail.com>2021-06-24 21:23:02 +0200
commit14bc8a3883663656cd4254567a978002c062992d (patch)
tree66f2c72f026c0142d9f0e74d272af5ed4b4863bb /service/src/main.c
parent5569ec2abd2c8f31554a02587fda842e4da7eba3 (diff)
downloadenowars5-service-stldoctor-14bc8a3883663656cd4254567a978002c062992d.tar.gz
enowars5-service-stldoctor-14bc8a3883663656cd4254567a978002c062992d.zip
refactored code for readability and keeping within 80ch limit, updated service source
Diffstat (limited to 'service/src/main.c')
-rw-r--r--service/src/main.c114
1 files changed, 55 insertions, 59 deletions
diff --git a/service/src/main.c b/service/src/main.c
index 7ff0f11..f363aec 100644
--- a/service/src/main.c
+++ b/service/src/main.c
@@ -50,9 +50,10 @@ int echo = 0, loggedin = 0;
int
save_submission(struct parseinfo *info, char *stldata, int stlsize)
{
- DIR *d;
- FILE *f = NULL;
char *dirpath = NULL, *infopath = NULL, *modelpath = NULL;
+ FILE *f = NULL;
+ int status = 0;
+ DIR *d;
if (loggedin)
dirpath = aprintf("%s/.%s-%i", resultdir, info->hash, time(NULL));
@@ -63,33 +64,29 @@ save_submission(struct parseinfo *info, char *stldata, int stlsize)
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;
+ NFCLOSE(f);
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;
+ NFCLOSE(f);
+
+exit:
+ if (f) fclose(f);
free(dirpath);
free(modelpath);
free(infopath);
- return OK;
+ return status;
fail:
- if (f) fclose(f);
-
if (infopath) remove(infopath);
if (modelpath) remove(modelpath);
if (dirpath) remove(dirpath);
- free(dirpath);
- free(modelpath);
- free(infopath);
-
- return FAIL;
+ status = 1;
+ goto exit;
}
int
@@ -104,50 +101,51 @@ handle_download(const char *scandir)
{
char *infopath, *modelpath;
size_t i, size;
+ int status = 0;
FILE *f;
infopath = aprintf("%s/%s", scandir, "info");
if (!(f = fopen(infopath, "r"))) {
- printf("ERR: Selected result is missing!\n");
- goto cleanup;
+ ERR("Selected result is missing!\n");
+ goto fail;
}
free_info(&cached);
if (load_info(&cached, f) != OK) {
- printf("ERR: Failed to parse info file!\n");
- goto cleanup;
+ ERR("Failed to parse info file!\n");
+ goto fail;
}
- fclose(f);
- f = NULL;
+ NFCLOSE(f);
print_info(&cached);
if (strchr(ask("Download the model? "), 'y')) {
modelpath = aprintf("%s/%s", scandir, "model");
if (!(f = fopen(modelpath, "r"))) {
- printf("ERR: Failed to access file!\n");
- goto cleanup;
+ ERR("Failed to access file!\n");
+ goto fail;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size > MAXFILESIZE) {
- printf("ERR: File is too large!\n");
- goto cleanup;
+ ERR("File is too large!\n");
+ goto fail;
}
printf("Here you go.. (%liB)\n", size);
while ((i = getc(f)) != EOF)
putc(i, stdout);
- fclose(f);
- f = NULL;
+ NFCLOSE(f);
}
- return 0;
-
-cleanup:
+exit:
if (f) fclose(f);
free(infopath);
free(modelpath);
- return 1;
+ return status;
+
+fail:
+ status = 1;
+ goto exit;
}
void
@@ -201,21 +199,21 @@ upload_cmd(const char *arg)
resp = ask("How large is your file? ");
len = strtoul(resp, &end, 10);
if (len <= 0 || len >= MAXFILESIZE || *end) {
- printf("ERR: Invalid file length!\n");
+ ERR("Invalid file length!\n");
return;
}
printf("Ok! Im listening..\n");
contents = checkp(malloc(len + 1));
if (fread(contents, 1, len, stdin) != len) {
- printf("ERR: Not enough data received!\n");
+ ERR("Not enough data received!\n");
goto cleanup;
}
contents[len] = '\0';
if ((cached.valid = parse_file(&cached, contents, len))) {
if (save_submission(&cached, contents, len) != OK)
- printf("ERR: Failed to save your submission!\n");
+ ERR("Failed to save your submission!\n");
else
printf("Your file was saved with ID %s!\n", cached.hash);
}
@@ -235,7 +233,7 @@ search_cmd(const char *arg)
if (arg && !strcmp(arg, "last")) {
if (!cached.valid) {
- printf("ERR: No cached info report available\n");
+ ERR("No cached info report available\n");
return;
}
hash = cached.hash;
@@ -244,7 +242,7 @@ search_cmd(const char *arg)
}
if (!(d = opendir(resultdir))) {
- printf("ERR: Unable to access upload directory!\n");
+ ERR("Unable to access upload directory!\n");
return;
}
@@ -256,14 +254,15 @@ search_cmd(const char *arg)
paths[pathc++] = checkp(strdup(de->d_name));
if (pathc == pathcap) {
pathcap *= 2;
- paths = checkp(realloc(paths, pathcap * sizeof(char*)));
+ paths = realloc(paths, pathcap * sizeof(char*));
+ checkp(paths);
}
}
}
closedir(d);
if (pathc == 0) {
- printf("ERR: Couldn't find a matching scan result!\n");
+ ERR("Couldn't find a matching scan result!\n");
goto cleanup;
}
@@ -272,16 +271,13 @@ search_cmd(const char *arg)
if (strchr(resp, 'q')) break;
which = strtoul(resp, &end, 10);
if (which >= pathc || which < 0 || *end) {
- printf("ERR: Invalid index!\n");
+ ERR("Invalid index!\n");
goto cleanup;
}
scandir = aprintf("%s/%s", resultdir, paths[which]);
-
if (handle_download(scandir)) goto cleanup;
-
- free(scandir);
- scandir = NULL;
+ NFREE(scandir);
}
cleanup:
@@ -301,7 +297,7 @@ list_cmd(const char *arg)
DIR *d;
if (!loggedin) {
- printf("ERR: Not logged in!\n");
+ ERR("Not logged in!\n");
return;
}
@@ -311,13 +307,12 @@ list_cmd(const char *arg)
if (*de->d_name == '.' && !strchr(".", de->d_name[1])) {
printf(">> %s\n", de->d_name);
path = aprintf("%s/%s/info", resultdir, de->d_name);
- if ((f = fopen(path, "r"))) {
- if (load_info(&info, f) == OK)
- print_info(&info);
- else
- printf("ERR: Failed to read saved file info!\n");
- fclose(f);
+ if ((f = fopen(path, "r")) && load_info(&info, f) == OK) {
+ print_info(&info);
+ } else {
+ ERR("Failed to read saved file info!\n");
}
+ if (f) fclose(f);
free(path);
}
}
@@ -331,7 +326,7 @@ auth_cmd(const char *arg)
int ret;
if (loggedin) {
- printf("ERR: Already logged in!\n");
+ ERR("Already logged in!\n");
return;
}
@@ -343,7 +338,7 @@ auth_cmd(const char *arg)
} else if (ret && errno == EEXIST) {
printf("Success!\nWelcome back!\n");
} else {
- printf("ERR: Auth failed!\n");
+ ERR("Auth failed!\n");
return;
}
@@ -364,14 +359,13 @@ cleanexit()
int
main()
{
+ struct command *c;
const char *cmd, *envstr;
char *cp, *arg;
int exit, i, cmdlen;
- if (!(envstr = getenv("RESULTDIR"))) {
- printf("ERR: RESULTDIR not defined\n");
- return 1;
- }
+ if (!(envstr = getenv("RESULTDIR")))
+ die("RESULTDIR not defined\n");
resultdir = checkp(strdup(envstr));
@@ -395,14 +389,16 @@ main()
cmdlen = cp ? cp - cmd : strlen(cmd);
for (i = 0; i < ARRSIZE(commands); i++) {
- if (!strncmp(commands[i].name, cmd, cmdlen)
- && cmdlen == strlen(commands[i].name)) {
- commands[i].func(arg);
+ c = &commands[i];
+ if (!strncmp(c->name, cmd, cmdlen) && !c->name[cmdlen]) {
+ c->func(arg);
break;
}
}
if (i == ARRSIZE(commands) && strlen(cmd) != 0)
- printf("No such command!\n");
+ ERR("No such command!\n");
}
+
+ return EXIT_SUCCESS;
}