From 09de15a20e7da162cb5da20153ce76057485cb08 Mon Sep 17 00:00:00 2001 From: b Date: Sun, 4 Dec 2022 16:07:55 +0000 Subject: [PATCH] Partial CGI update bacause git disagrees with me. --- bluenhd.cpp => bluenh-cgi.c | 0 makefile | 24 +++- makefile.1.mak | 24 +++- nofadingd.cpp => nofading-cgi.c | 0 npbd.cpp => npb-cgi.c | 0 online-core.1.h | 44 +++++++ online-core.c | 227 ++++++++++++++++++++++++++++++++ insertframe.c => pal-cgi.c | 0 settings-release.txt | 6 + settings.txt | 16 ++- 10 files changed, 327 insertions(+), 14 deletions(-) rename bluenhd.cpp => bluenh-cgi.c (100%) rename nofadingd.cpp => nofading-cgi.c (100%) rename npbd.cpp => npb-cgi.c (100%) create mode 100644 online-core.1.h create mode 100644 online-core.c rename insertframe.c => pal-cgi.c (100%) diff --git a/bluenhd.cpp b/bluenh-cgi.c similarity index 100% rename from bluenhd.cpp rename to bluenh-cgi.c diff --git a/makefile b/makefile index 5813e48..0fff0b4 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,5 @@ # "makefile" is automatically generated from "makefile.1.mak" -# 03.12.2022 +# 04.12.2022 # # Copyright (C) 2022 Balthasar Szczepański # @@ -90,16 +90,25 @@ npb0.h\ npb1.h\ npb2.h\ npb_ong1.h\ -npb_403.h +npb_403.h\ +nh.h + +CGI=\ +bluenh-cgi + +#C_CGI=\ +#bluenh-cgi.c # keep these 2 lists in the same order!: -GENERATE_FROM= +GENERATE_FROM=\ +online-core.1.h -TO_GENERATE= +TO_GENERATE=\ +online-core.h #all: 403 npb npbd npb-ong1 npbd-ong1 bluenh bluenhd insert extract seediff insertframe mremapt-1 compare nofading nofadingd -all: enhance $(STANDALONE) +all: enhance $(STANDALONE) $(CGI) makefile: makefile.1.mak $(CONFIGFILE) @@ -120,6 +129,9 @@ $(H_SUBTOOL): %.h: settings-subtool.txt subtool.h $(TO_GENERATE): $(GENERATE_FROM) $(CONFIGFILE) $(CONFIGURE_CMD) --in $(GENERATE_FROM) --out $(TO_GENERATE) + +$(CGI): %: %.c online-core.c online-core.h $(H_PNG) + $(CC) $(CF) -o $@ $*.c online-core.c $(L_CGI) $(H_PNG): %.h: %.png f2h ./f2h $* $*.png $@ @@ -132,7 +144,7 @@ f2h: f2h.c # $(CC) $(CF) -o test test.c core.c $(LF) clean: - $(RM) -f enhance $(C_STANDALONE) $(STANDALONE) $(TO_GENERATE) f2h $(H_PNG) $(H_SUBTOOL) + $(RM) -f enhance $(C_STANDALONE) $(STANDALONE) $(TO_GENERATE) f2h $(H_PNG) $(H_SUBTOOL) $(CGI) PHONY: all clean diff --git a/makefile.1.mak b/makefile.1.mak index 15d1f31..a66bd07 100644 --- a/makefile.1.mak +++ b/makefile.1.mak @@ -1,5 +1,5 @@ # "makefile" is automatically generated from "makefile.1.mak" -# 03.12.2022 +# 04.12.2022 # # Copyright (C) 2022 Balthasar Szczepański # @@ -90,16 +90,25 @@ npb0.h\ npb1.h\ npb2.h\ npb_ong1.h\ -npb_403.h +npb_403.h\ +nh.h + +CGI=\ +bluenh-cgi + +#C_CGI=\ +#bluenh-cgi.c # keep these 2 lists in the same order!: -GENERATE_FROM= +GENERATE_FROM=\ +online-core.1.h -TO_GENERATE= +TO_GENERATE=\ +online-core.h #all: 403 npb npbd npb-ong1 npbd-ong1 bluenh bluenhd insert extract seediff insertframe mremapt-1 compare nofading nofadingd -all: enhance $(STANDALONE) +all: enhance $(STANDALONE) $(CGI) makefile: makefile.1.mak $(CONFIGFILE) @@ -120,6 +129,9 @@ $(H_SUBTOOL): %.h: settings-subtool.txt subtool.h $(TO_GENERATE): $(GENERATE_FROM) $(CONFIGFILE) $(CONFIGURE_CMD) --in $(GENERATE_FROM) --out $(TO_GENERATE) + +$(CGI): %: %.c online-core.c online-core.h $(H_PNG) + $(CC) $(CF) -o $@ $*.c online-core.c $(L_CGI) $(H_PNG): %.h: %.png f2h ./f2h $* $*.png $@ @@ -132,7 +144,7 @@ f2h: f2h.c # $(CC) $(CF) -o test test.c core.c $(LF) clean: - $(RM) -f enhance $(C_STANDALONE) $(STANDALONE) $(TO_GENERATE) f2h $(H_PNG) $(H_SUBTOOL) + $(RM) -f enhance $(C_STANDALONE) $(STANDALONE) $(TO_GENERATE) f2h $(H_PNG) $(H_SUBTOOL) $(CGI) PHONY: all clean diff --git a/nofadingd.cpp b/nofading-cgi.c similarity index 100% rename from nofadingd.cpp rename to nofading-cgi.c diff --git a/npbd.cpp b/npb-cgi.c similarity index 100% rename from npbd.cpp rename to npb-cgi.c diff --git a/online-core.1.h b/online-core.1.h new file mode 100644 index 0000000..eb15e33 --- /dev/null +++ b/online-core.1.h @@ -0,0 +1,44 @@ +/* +online-core.h is autogenerated from online-core.1.h +Common parts of the online interface +03.12.2022 + +Copyright (C) 2022 Balthasar Szczepański + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +Requires cgilib (http://www.infodrom.org/projects/cgilib/) +*/ + +#include +#include + +// ###C_USERAGENT: #define USERAGENT "enhance" + +// ###C_CP_PATH: #define CP_PATH "cp" +// ###C_WGET_PATH: #define WGET_PATH "wget" +// ###C_RM_PATH: #define RM_PATH "rm" + +// ###C_TEMP_PATH: #define TEMP_PATH "/botm/tmp/enhance" + +// ###C_BLUENH_PATH: #define NPB_PATH "/botm/bin/bluenh" +// ###C_NPB_PATH: #define NPB_PATH "/botm/bin/npb" + +int cp (char *src, char *dst); +int wget (char *url, char *dst); +int rm (char *dst); +void make_tmp_path(char *str, size_t n, unsigned int i, char *ext); +int get_file(s_cgi *cgi, char *name, char *dest, char **path); +int send_file (char *path, char *content_type, unsigned status); +int send_data (const uint8_t *address, size_t size, char *content_type, unsigned status); diff --git a/online-core.c b/online-core.c new file mode 100644 index 0000000..3adb21c --- /dev/null +++ b/online-core.c @@ -0,0 +1,227 @@ +/* +online-core.c +Common parts of the online interface +03.12.2022 + +Copyright (C) 2013, 2014, 2015, 2022 Balthasar Szczepański + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . + +Requires cgilib (http://www.infodrom.org/projects/cgilib/) +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cgi.h" + +#include "online-core.h" + +#define FILE_CHUNK 1024 + + +int cp (char *src, char *dst) +{ + pid_t sub; + int r; + + sub = fork(); + if (sub == 0) + { + r = execl(CP_PATH, src, dst, (char *)0); + exit(r); + } + waitpid(sub, &r, 0); + + return r; +} + +int rm (char *dst) +{ + pid_t sub; + int r; + + sub = fork(); + if (sub == 0) + { + r = execl(RM_PATH, "-f", dst, (char *)0); + exit(r); + } + waitpid(sub, &r, 0); + + return r; +} + +int wget (char *url, char *dst) +{ + pid_t sub; + int r; + + sub = fork(); + if (sub == 0) + { + r = execl(WGET_PATH, "-q", "-t", "2", "-U", USERAGENT, "-O", dst, url, (char *)0); + exit(r); + } + waitpid(sub, &r, 0); + + return r; +} + +void make_tmp_path(char *str, size_t n, unsigned int i, char *ext) +{ + struct timeval tv; + + gettimeofday(&tv, NULL); + + snprintf( + str, + n, + "%s/%lu_%u_%llu_%lu%s", + TEMP_PATH, + (long unsigned) getpid(), + i, + (long long unsigned) (tv.tv_sec), + (long unsigned) (tv.tv_usec), + ext + ); +} + +int get_file(s_cgi *cgi, char *name, char *dest, char **path) +{ + char **vl; + char *v; + s_file *f; + int i; + int r; + + vl = cgiGetFiles(cgi); + if (vl != NULL) + { + for (i=0; vl[i]!= NULL; ++i) + { + if (strcmp(vl[i], name)==0) + { + f = cgiGetFile(cgi, vl[i]); + if (f == NULL) + break; + if (dest != NULL) + { + r = cp(f->tmpfile, dest); + if (r) + return r; + *path = dest; + } + else + *path = f->tmpfile; + return 0; + } + } + } + + if (dest == NULL) + return EINVAL; + + v = cgiGetValue(cgi, name); + if (v==NULL) + return EINVAL; + r = wget(v, dest); + if (r) + return r; + *path = dest; + return 0; +} + +int send_file (char *path, char *content_type, unsigned status) +{ + FILE *f; + struct stat st; + int64_t size; + size_t wr, rd, off; + uint8_t buffer[FILE_CHUNK]; + + f = fopen(path, "rb"); + if (f == NULL) + return EIO; + fstat(fileno(f), &st); + size = st.st_size; + + if (status != 0) + fprintf(stdout, "Status: %u\n", status); + if (content_type!=NULL) + fprintf(stdout, "Content-type: %s", content_type); + fprintf(stdout, "Content-Length: %" PRId64 "\n\n", size); + + while (size>0) + { + rd = fread(buffer, 1, FILE_CHUNK, f); + if (rd == 0) + break; + off =0; + while (off < rd) + { + wr = fwrite(buffer+off, 1, rd - off, stdout); + if (wr == 0) + break; + off += wr; + } + if (wr == 0) + break; + size -= rd; + } + + fclose (f); + return 0; +} + +int send_data (const uint8_t *address, size_t size, char *content_type, unsigned status) +{ + size_t wr, off; + + if (status != 0) + fprintf(stdout, "Status: %u\n", status); + if (content_type!=NULL) + fprintf(stdout, "Content-type: %s", content_type); + fprintf(stdout, "Content-Length: %" PRId64 "\n\n", (uint64_t)size); + + off = 0; + while (off FILE_CHUNK) + wr = FILE_CHUNK; + wr = fwrite(address +off, 1, wr, stdout); + if (wr == 0) + break; + off += wr; + } + + return 0; +} + + + + + + + + \ No newline at end of file diff --git a/insertframe.c b/pal-cgi.c similarity index 100% rename from insertframe.c rename to pal-cgi.c diff --git a/settings-release.txt b/settings-release.txt index 04e70c2..7a10d68 100644 --- a/settings-release.txt +++ b/settings-release.txt @@ -11,8 +11,14 @@ target: release # alternatively, copy configure.pl to source directory. configure: /botm/bin/config/configure.pl +useragent: ENHANCE!ment tool (http://1190.bicyclesonthemoon.info/enhance) +bin_path: /botm/bin/enhance +tmp_path: /botm/tmp/enhance + +cp: /usr/bin/cp rm: /usr/bin/rm +wget: /usr/bin/wget CC: gcc CF: -g -Wall diff --git a/settings.txt b/settings.txt index 3f5212b..0967788 100644 --- a/settings.txt +++ b/settings.txt @@ -1,5 +1,5 @@ # settings.txt -# 12.11.2022 +# 04.12.2022 # # Copyright (C) 2022 Balthasar Szczepański # @@ -19,15 +19,27 @@ _C_DEFINE_STR: #define $0 "@_ESCAPE($1)" # _C_INCLUDE_STR: #include "@_ESCAPE($0)" +_bin_bluenh_path = @_PATH($bin_path, bluenh) +_bin_npb_path = @_PATH($bin_path, npb) MAKE_CONFIGURE = CONFIGURE = $configure MAKE_TARGET = TARGET = $target MAKE_RM = RM =$rm - MAKE_CC = CC =$CC MAKE_CF = CF =$CF MAKE_L_IL = L_IL =$L_IL MAKE_L_CGI = L_CGI=$L_CGI + +C_USERAGENT = @_C_DEFINE_STR(USERAGENT, $useragent) + +C_CP_PATH = @_C_DEFINE_STR(CP_PATH, $cp) +C_WGET_PATH = @_C_DEFINE_STR(WGET_PATH, $wget) +C_RM_PATH = @_C_DEFINE_STR(RM_PATH, $rm) + +C_TEMP_PATH = @_C_DEFINE_STR(TEMP_PATH, $_tmp_path) + +C_BLUENH_PATH = @_C_DEFINE_STR(BLUENH_PATH, $_bin_bluenh_path) +C_NPB_PATH = @_C_DEFINE_STR(NPB_PATH, $_bin_npb_path) -- 2.30.2