From cafb2b7d0f84af5fab6190dedec492d65c48078a Mon Sep 17 00:00:00 2001 From: mbays Date: Fri, 2 Apr 2021 00:00:00 +0000 Subject: [PATCH] pass initial query as INIT_QUERY rather than writing it --- examples/zyg.sh | 27 ++++++++++++++++++--------- main.c | 18 ++++++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/examples/zyg.sh b/examples/zyg.sh index 078fe2d..eaeda45 100755 --- a/examples/zyg.sh +++ b/examples/zyg.sh @@ -6,17 +6,26 @@ var=/var/gemini/zyg/ [ -n "$TLS_CLIENT_HASH" ] || exit 1 -echo "Choose your poison:" -ls -1 "$var/games/" - -while read input; do - sanitised="$(tr -dc [:alnum:]_ <<< "$input")" +if [ -n "$INIT_QUERY" ]; then + sanitised="$(tr -dc [:alnum:]_ <<< "$INIT_QUERY")" gamefile="$var/games/$sanitised" - if [ -n "$sanitised" ] && [ -e "$gamefile" ]; then - break; + if ! ( [ -n "$sanitised" ] && [ -e "$gamefile" ] ); then + echo "Unknown game: $sanitised" + exit 1 fi - echo "Please enter one of the listed games" -done +else + echo "Choose your poison:" + ls -1 "$var/games/" + + while read input; do + sanitised="$(tr -dc [:alnum:]_ <<< "$input")" + gamefile="$var/games/$sanitised" + if [ -n "$sanitised" ] && [ -e "$gamefile" ]; then + break; + fi + echo "Please enter one of the listed games" + done +fi echo "Starting $sanitised. Savefiles will be preserved." echo diff --git a/main.c b/main.c index d41e05a..56b09fa 100644 --- a/main.c +++ b/main.c @@ -51,7 +51,8 @@ typedef struct State { Child children[MAX_CHILDREN]; } State; -static bool spawn(const char *command, char *const *args, Child *child, int socket) +static bool spawn(const char *command, char *const *args, const char *query, + Child *child, int socket) { int infds[2], outfds[2], errfds[2]; if (pipe(infds) == -1 || pipe(outfds) == -1 || pipe(errfds) == -1) { @@ -78,9 +79,17 @@ static bool spawn(const char *command, char *const *args, Child *child, int sock setbuffer(stdout, NULL, 0); setbuffer(stderr, NULL, 0); setsid(); + char tlsenv[81]; snprintf(tlsenv, 81, "TLS_CLIENT_HASH=%s", child->owner); putenv(tlsenv); + + if (query != NULL) { + char qenv[1035]; + snprintf(qenv, 1035, "INIT_QUERY=%s", query); + putenv(qenv); + } + execvp(command, args); exit(1); } else { @@ -236,6 +245,8 @@ void respond(void *object, const Request_Info *request_info, int socket) } else if (slot == NULL || slot->exists) slot = c; } + const char *q = request_info->query_string_decoded; + if (child == NULL) { if (slot == NULL || (slot->exists && state->num_children < state->max_children)) { slot = &state->children[state->num_children++]; @@ -257,7 +268,7 @@ void respond(void *object, const Request_Info *request_info, int socket) if (request_info->tls_client_hash == NULL) child->owner[0] = 0; else strncpy(child->owner, request_info->tls_client_hash, 64); - if (!spawn(state->command, state->args, child, socket)) { + if (!spawn(state->command, state->args, q, child, socket)) { put("40 Spawn failure.\r\n"); return; } @@ -268,7 +279,6 @@ void respond(void *object, const Request_Info *request_info, int socket) spawned = true; } - const char *q = request_info->query_string_decoded; if (*q == '!') { ++q; if (*q == '?') { @@ -344,7 +354,7 @@ void respond(void *object, const Request_Info *request_info, int socket) if (!spawned) kill(-child->pid, SIGCONT); int qlen = strlen(q); - if (!spawned || qlen > 0) { + if (!spawned) { bool newline = true; if (q[qlen-1] == '\\') { --qlen; -- 2.11.4.GIT