nmps

simple network-based command-line rpg wrote in c99
git clone git://git.kocotian.pl/nmps.git
Log | Files | Refs | LICENSE

commit efb61edc4fc508f9d27d8f5d4113d3ce4470002a
parent 67ab7b9ad2f51941dd3a5fbce5e5f2ad91ff67ab
Author: kocotian <kocotian@kocotian.pl>
Date:   Sat,  5 Dec 2020 19:25:57 +0100

new PS1 & stdin handle, signals, prepare 4 events

Diffstat:
Mconfig.h | 5++---
Mnmps | 0
Mnmps.c | 91++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
3 files changed, 78 insertions(+), 18 deletions(-)

diff --git a/config.h b/config.h @@ -1,6 +1,5 @@ -#define PS1 "\033[0;37m💚 %d 🍗 %d 🛡️ %d 😂 %d 🌊 %d 💲 %d\n%s@%s \033[0m$ ",\ - health, saturation, protection, \ - emotion, hydration, cash, \ +#define PS1 "\033[0;37m💵 %d\n%s@%s \033[0m$ ",\ + cash, \ username, host static const char *defaultusername = NULL; diff --git a/nmps b/nmps Binary files differ. diff --git a/nmps.c b/nmps.c @@ -3,6 +3,7 @@ #include <sys/socket.h> #include <netdb.h> +#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -13,7 +14,7 @@ #include "http.h" #include "util.c" -#define VERSION "a0.2.2.1" +#define VERSION "a0.2.3" extern void herror(const char *s); @@ -24,7 +25,10 @@ static void mkconnect(char *username, char *host, char *port); static size_t request(char *hostname, unsigned short port, char *command, char *args, char **buffer); static void usage(void); +static void sighandler(int signo); + static char *authtoken = NULL; +static int lastsigno = -1; char *argv0; #include "config.h" @@ -84,29 +88,69 @@ gameplay(char *username, char *host, char *port) char *line, *linedup, *token, *cmd, *args; size_t argsize; - int commandret; + int character, chiter = -1, commandret; int cash = 100; - short health = 100, saturation = 100, - protection = 100, emotion = 100, hydration = 100; + /* short health = 100, saturation = 100, */ + /* protection = 100, emotion = 100, hydration = 100; */ line = malloc(linesize); while (1) { argsize = 0; printf(PS1); - getline(&line, &linesize, stdin); + /* getline(&line, &linesize, stdin); */ + strcpy(line, ""); + while ((character = getch(0)) != '\n') { + switch (character) { + case 127: + line[chiter--] = 0; + printf("\033[1D \033[1D"); + break; + case 12: + printf("\033c\030"PS1); + for (int i = 0; i <= chiter; ++i) + fputc(line[i], stdout); + break; + case 27: + printf("^["); getch(1); getch(1); + break; + case -1: + if (lastsigno == SIGINT) { + printf("-- interrupted"); + strcpy(line, ""); + chiter = -1; + } + printf("\n"PS1); + if (lastsigno == SIGUSR1) { + for (int i = 0; i <= chiter; ++i) + fputc(line[i], stdout); + break; + } + lastsigno = -1; + break; + default: + line[++chiter] = character; + printf("%c", character); + break; + } + } + line[++chiter] = 0; + chiter = -1; + puts(""); if (line[strlen(line) - 1] == '\n') line[strlen(line) - 1] = 0; - linedup = strdup(line); - cmd = strtok_r(linedup, " ", &linedup); - args = calloc(0, 1); - while ((token = strtok_r(linedup, " ", &linedup))) { - argsize += strlen(token) + 1; - args = realloc(args, argsize); - strcat(args, token); strcat(args, "\001"); + if (strlen(line)) { + linedup = strdup(line); + cmd = strtok_r(linedup, " ", &linedup); + args = calloc(0, 1); + while ((token = strtok_r(linedup, " ", &linedup))) { + argsize += strlen(token) + 1; + args = realloc(args, argsize); + strcat(args, token); strcat(args, "\001"); + } + if((commandret = command(cmd, args, host, port))) + return commandret; } - if((commandret = command(cmd, args, host, port))) - return commandret; } free(line); return 0; @@ -188,10 +232,27 @@ main(int argc, char *argv[]) host = argv[argc - 1]; mkconnect(username, host, port); + signal(SIGINT, sighandler); + signal(SIGUSR1, sighandler); command("motd", "", host, port); - gameplay(username, host, port); + while (1) + gameplay(username, host, port); free(username); return 0; } + +static void +sighandler(int signo) +{ + signal(signo, sighandler); + lastsigno = signo; + switch (signo) { + case SIGINT: + puts(""); + break; + case SIGUSR1: + break; + } +}