aboutsummaryrefslogtreecommitdiffstats
path: root/service
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-06-24 12:35:56 +0200
committerLouis Burda <quent.burda@gmail.com>2021-06-24 12:35:56 +0200
commit7cc88b34e67b3d35ca10bcbf8b393dbc2713b63e (patch)
treed6a13dd30e01369b74773f3bdc90e9e6b4b2aac0 /service
parentcc1bbb8f1e827863b679932496cf06fa3d5bf81a (diff)
downloadenowars5-service-stldoctor-7cc88b34e67b3d35ca10bcbf8b393dbc2713b63e.tar.gz
enowars5-service-stldoctor-7cc88b34e67b3d35ca10bcbf8b393dbc2713b63e.zip
added ability to request mulitple files in search without restarting
Diffstat (limited to 'service')
-rw-r--r--service/src/main.c121
1 files changed, 69 insertions, 52 deletions
diff --git a/service/src/main.c b/service/src/main.c
index 751f2ef..498f33f 100644
--- a/service/src/main.c
+++ b/service/src/main.c
@@ -99,6 +99,57 @@ access_authorized(const char *file)
|| (!loggedin && file[0] != '.');
}
+int
+handle_download(const char *scandir)
+{
+ char *infopath, *modelpath;
+ size_t i, size;
+ FILE *f;
+
+ infopath = aprintf("%s/%s", scandir, "info");
+ if (!(f = fopen(infopath, "r"))) {
+ printf("Selected result is missing!\n");
+ goto cleanup;
+ }
+ free_info(&cached);
+ if (load_info(&cached, f) != OK) {
+ printf("Failed to parse info file!\n");
+ 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"))) {
+ printf("Failed to access file!\n");
+ goto cleanup;
+ }
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+ if (size > MAXFILESIZE) {
+ printf("File is too large to send!\n");
+ goto cleanup;
+ }
+ printf("Here you go.. (%liB)\n", size);
+ while ((i = getc(f)) != EOF)
+ putc(i, stdout);
+ fclose(f);
+ f = NULL;
+ }
+
+ return 0;
+
+cleanup:
+ if (f) fclose(f);
+ free(infopath);
+ free(modelpath);
+ return 1;
+}
+
void
cat_cmd(const char *arg)
{
@@ -176,14 +227,11 @@ cleanup:
void
search_cmd(const char *arg)
{
- char *end, *scandir = NULL, *infopath = NULL,
- *modelpath = NULL, **paths = NULL;
- int i, which, dirstart, ishidden, pathc, pathcap = 100;
- const char *hash, *name;
+ char *end, *scandir = NULL, **paths = NULL;
+ int i, which, pathc, pathcap = 100;
+ const char *hash, *name, *resp;
struct dirent *de;
DIR *d = NULL;
- FILE *f = NULL;
- size_t size;
if (arg && !strcmp(arg, "last")) {
if (!cached.valid) {
@@ -201,7 +249,6 @@ search_cmd(const char *arg)
}
paths = checkp(malloc(pathcap * sizeof(char*)));
- dirstart = telldir(d);
for (pathc = 0; (de = readdir(d));) {
if (access_authorized(de->d_name)
&& !strpfcmp(hash, de->d_name + (loggedin ? 1 : 0))) {
@@ -220,55 +267,25 @@ search_cmd(const char *arg)
goto cleanup;
}
- which = strtoul(ask("Which of these results? "), &end, 10);
- if (which >= pathc || which < 0 || *end) {
- printf("Invalid index!\n");
- goto cleanup;
- }
+ while (1) {
+ resp = ask("Which result [q to quit]? ");
+ if (strchr(resp, 'q')) break;
+ which = strtoul(resp, &end, 10);
+ if (which >= pathc || which < 0 || *end) {
+ printf("Invalid index!\n");
+ goto cleanup;
+ }
- scandir = aprintf("%s/%s", resultdir, paths[which]);
+ scandir = aprintf("%s/%s", resultdir, paths[which]);
- infopath = aprintf("%s/%s", scandir, "info");
- if (!(f = fopen(infopath, "r"))) {
- printf("Selected result is missing!\n");
- goto cleanup;
- }
- free_info(&cached);
- if (load_info(&cached, f) != OK) {
- printf("Failed to parse info file!\n");
- goto cleanup;
- }
- fclose(f);
- f = NULL;
+ if (handle_download(scandir)) goto cleanup;
- print_info(&cached);
-
- if (strchr(ask("Download the model? "), 'y')) {
- modelpath = aprintf("%s/%s", scandir, "model");
- if (!(f = fopen(modelpath, "r"))) {
- printf("Failed to access file!\n");
- goto cleanup;
- }
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fseek(f, 0, SEEK_SET);
- if (size > MAXFILESIZE) {
- printf("File is too large to send!\n");
- goto cleanup;
- }
- printf("Here you go.. (%liB)\n", size);
- while ((i = getc(f)) != EOF)
- putc(i, stdout);
- fclose(f);
- f = NULL;
+ free(scandir);
+ scandir = NULL;
}
cleanup:
- if (f) fclose(f);
-
free(scandir);
- free(infopath);
- free(modelpath);
for (i = 0; i < pathc; i++) free(paths[i]);
free(paths);
@@ -295,10 +312,10 @@ list_cmd(const char *arg)
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)
- printf("Failed to read saved file info!\n");
- else
+ if (load_info(&info, f) == OK)
print_info(&info);
+ else
+ printf("Failed to read saved file info!\n");
fclose(f);
}
free(path);