commit 0332906dd17f05b1ad64d5a962bae025a2d212fc
parent ce1154562e5ecadf086fc61b7ed3e5f1eb17651d
Author: kocotian <kocotian@kocotian.pl>
Date: Fri, 20 Nov 2020 21:22:37 +0100
installation, hidden password, exit steering sequence, adding username as argument
Diffstat:
5 files changed, 79 insertions(+), 16 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,3 +1,7 @@
-CC=c99
-nmps: nmps.c config.h
- ${CC} -o $@ $< -pedantic -Wall -Wextra
+CC=cc
+PREFIX=/usr/local
+nmps: nmps.c config.h getch.h
+ ${CC} -o $@ $< -pedantic -Wall -Wextra -std=c99
+
+install: nmps
+ install -Dm 755 nmps ${PREFIX}/bin
diff --git a/getch.h b/getch.h
@@ -0,0 +1,39 @@
+/* UNLICENSED */
+
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <termios.h>
+#include <unistd.h>
+
+static struct termios old, current;
+
+/* Initialize new terminal i/o settings */
+void
+initTermios(int echo)
+{
+ tcgetattr(0, &old); /* grab old terminal i/o settings */
+ current = old; /* make new settings same as old settings */
+ current.c_lflag &= ~ICANON; /* disable buffered i/o */
+ if(echo)
+ current.c_lflag |= ECHO; /* set echo mode */
+ else
+ current.c_lflag &= ~ECHO; /* set no echo mode */
+ tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */
+}
+
+/* Restore old terminal i/o settings */
+void
+resetTermios(void)
+{
+ tcsetattr(0, TCSANOW, &old);
+}
+
+char
+getch(char echo)
+{
+ char ch;
+ initTermios(echo);
+ ch = getchar();
+ resetTermios();
+ return ch;
+}
diff --git a/nmps b/nmps
Binary files differ.
diff --git a/nmps.c b/nmps.c
@@ -1,17 +1,19 @@
#define _XOPEN_SOURCE 700
+#include <sys/socket.h>
+
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/socket.h>
#include <unistd.h>
#include "arg.h"
+#include "getch.h"
#include "http.h"
#include "util.c"
-#define VERSION "a0.2.1"
+#define VERSION "a0.2.2"
extern void herror(const char *s);
@@ -58,9 +60,14 @@ command(char *command, char *args, char *host, char *port)
if (!(reqsize = request(host, atoi(port), command, args, &buffer)))
return -1;
truncbuf = truncateHeader(buffer);
- if (*truncbuf > 0 && *truncbuf < 10)
- ++truncbuf; /* there will be steering sequences,
- reserved for simple comunication server -> client */
+ if ((*truncbuf > 0 && *truncbuf < 10)
+ || (*truncbuf > 10 && *truncbuf < 13)
+ || (*truncbuf > 13 && *truncbuf < 24)) { /* steering sequences,
+ reserved for simple comunication
+ server -> client */
+ if (*truncbuf == 4)
+ exit(*(++truncbuf) - 1);
+ }
printf("%s%c", truncbuf,
buffer[reqsize - 1] == '\n' || buffer[reqsize - 1]
== 030 /* ASCII 030 on the end simply means:
@@ -77,6 +84,7 @@ gameplay(char *username, char *host, char *port)
char *line, *linedup, *token, *cmd,
*args;
size_t argsize;
+ int commandret;
int cash = 100;
short health = 100, saturation = 100,
protection = 100, emotion = 100, hydration = 100;
@@ -97,8 +105,8 @@ gameplay(char *username, char *host, char *port)
args = realloc(args, argsize);
strcat(args, token); strcat(args, "\001");
}
- if(command(cmd, args, host, port))
- die("\033[1;31mSomething went wrong with your request!\033[0m");
+ if((commandret = command(cmd, args, host, port)))
+ return commandret;
}
free(line);
return 0;
@@ -117,15 +125,18 @@ mkconnect(char *username, char *host, char *port)
}
fprintf(stderr, "succeeded!\n");
- fprintf(stderr, "%s@%s's password: \033[8m", username, host);
- while ((character = fgetc(stdin)) != '\n') {
+ fprintf(stderr, "%s@%s's password: ", username, host);
+ while ((character = getch(0)) != '\n') {
+ if (character == 127)
+ password[chiter--] = 0;
+ else
password[++chiter] = character;
}
password[++chiter] = 0;
- fprintf(stderr, "\033[0m");
+ fprintf(stderr, "\n");
if(authorize(host, port, username, password))
- printf("Authorization successful!\nAuth token: %s\n", authtoken);
+ fprintf(stderr, "Authorization successful!\n");
}
static size_t
@@ -150,12 +161,15 @@ int
main(int argc, char *argv[])
{
char *port = "80", *host = "localhost",
- username[32] = "unknown";
+ *username = NULL;
ARGBEGIN {
case 'p':
port = ARGF();
break;
+ case 'u':
+ username = ARGF();
+ break;
case 'h': /* fallthrough */
default:
usage();
@@ -165,12 +179,17 @@ main(int argc, char *argv[])
if (argc != 1)
usage();
- getlogin_r(username, 32);
+ if (username == NULL) {
+ username = malloc(32);
+ getlogin_r(username, 32);
+ }
host = argv[argc - 1];
mkconnect(username, host, port);
command("motd", "", host, port);
gameplay(username, host, port);
+ free(username);
+
return 0;
}
diff --git a/tmpdocumentation b/tmpdocumentation
@@ -0,0 +1 @@
+4 - program stop (next char is status code + 1)