From 17914ca9aa3362737a3c860f02965eaac7c6df8a Mon Sep 17 00:00:00 2001 From: Bryan Worth Date: Thu, 8 Dec 2005 20:34:02 +0000 Subject: [PATCH] 2005-12-08 Bryan Worth * src/ttt-curses-client.c: Multiple improvements! Username taken from USER environment variable and stored as preference in ~/.ttt/client.conf (username=xxxxxx). Backspace should now work correctly with all term types. Status messages display in seperate area. MESSAGE reply from server is suppressed. Eliminated most global variables. --- ChangeLog | 9 +++ src/ttt-curses-client.c | 158 +++++++++++++++++++++++++++------------- 2 files changed, 115 insertions(+), 52 deletions(-) diff --git a/ChangeLog b/ChangeLog index e553a92..bc97e1b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2005-12-08 Bryan Worth + * src/ttt-curses-client.c: Multiple improvements! + Username taken from USER environment variable and stored as + preference in ~/.ttt/client.conf (username=xxxxxx). Backspace + should now work correctly with all term types. Status messages + display in seperate area. "MESSAGE" reply from server is + suppressed. Eliminated most global variables. + + 2005-12-08 Carl Worth * src/Makefile.am: Fix rule to auto-generate ttt-lex.h. diff --git a/src/ttt-curses-client.c b/src/ttt-curses-client.c index 47e8370..23cd7d7 100644 --- a/src/ttt-curses-client.c +++ b/src/ttt-curses-client.c @@ -1,4 +1,4 @@ -/* ttt-curses-client.c - curses based tic-tac-toe game client +/*ttt-curses-client.c - curses based tic-tac-toe game client * * Copyright © 2005 Bryan Worth * @@ -102,54 +102,66 @@ void wprint (WINDOW *window, while (xx < l) { outc = string[xx]; - if (string[xx] != '\r') + if (string[xx] != '\r') waddch (window, outc); xx++; } } -char *message2; -int _socket; -size_t maxread; +FILE *sockin, *sockout; int main (int argc, char **argv) { - char *message; - char buffer[1000]; - char *host; - char *port; + char *host="localhost"; + char *port="5334"; chtype c; - ssize_t numread; + char *username; + char *envuser; + char *confpath; + char *conffile; + char *command_string; + int _socket; + char buffer[BUFSIZ]; + char inplin[1024]; + static WINDOW *mainwnd; static WINDOW *dispwin; + static WINDOW *statwin; static WINDOW *inpwin; + FILE *conf_file; mainwnd = initscr(); int dlines = LINES - 2, cols = COLS; (void) nonl (); + (void) nl (); noecho (); cbreak (); nodelay (mainwnd, TRUE); curs_set (0); refresh (); - dispwin = newwin (dlines - 1, cols - 2, 0, 0); + dispwin = newwin (dlines - 5, cols - 2, 0, 0); + statwin = newwin (4, cols - 2, dlines -3, 0); inpwin = newwin (1, cols - 2, dlines, 0); keypad (mainwnd, TRUE); // enable keyboard mapping keypad (inpwin, TRUE); // enable keyboard mapping nodelay (inpwin, TRUE); wrefresh (mainwnd); wrefresh (dispwin); + wrefresh (statwin); wrefresh (inpwin); (void) scrollok (mainwnd, TRUE); (void) scrollok (dispwin, TRUE); + (void) scrollok (statwin, TRUE); (void) scrollok (inpwin, TRUE); (void) idlok (mainwnd, TRUE); (void) idlok (dispwin, TRUE); + (void) idlok (statwin, TRUE); (void) idlok (inpwin, TRUE); wsetscrreg (dispwin, 0, dlines - 2); + wsetscrreg (statwin, 0, 4); (void) signal (SIGINT, finish); /* arrange interrupts to terminate */ if (has_colors ()) { @@ -164,58 +176,104 @@ main (int argc, char **argv) init_pair (COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair (COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); } - wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE)); - wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN)); + //wbkgd (dispwin, COLOR_PAIR (COLOR_WHITE)); + //wbkgd (inpwin, COLOR_PAIR (COLOR_CYAN)); wrefresh (dispwin); wrefresh (inpwin); - xasprintf (&host, "localhost"); - xasprintf (&port, "5334"); - xasprintf (&message, "HELO bryan\r\n"); ttt_socket_create_client (host, port, &_socket); - ttt_socket_write (_socket, message, strlen (message)); - - maxread = 100; - numread = read (_socket, buffer, maxread); - if (numread > 0) { - buffer[numread] = '\0'; - xasprintf (&message, "%s", buffer); - wprint (dispwin, message); + sockin=fdopen(_socket,"r"); + sockout=fdopen(_socket,"w"); + + + xasprintf(&confpath,"%s/.ttt/",getenv("HOME")); + xasprintf(&conffile,"%s/.ttt/client.conf",getenv("HOME")); + username="user"; + if (access (conffile, F_OK) != 0 ) { + envuser=getenv("USER"); + if (envuser != NULL) username=strdup(envuser); + if (access (confpath, F_OK) != 0 ) { + xasprintf(&command_string,"mkdir %s",confpath); + system (command_string); + } + if ((conf_file = fopen(conffile,"w")) != NULL) { + fprintf(conf_file,"username=%s",username); + fclose(conf_file); + } + } + if ((conf_file = fopen(conffile,"r")) != NULL) { + while (fgets(buffer,BUFSIZ,conf_file)) { + if (strncmp(buffer,"username=",9) == 0) { + xasprintf (&username, "%s", &buffer[9]); + break; + } + } + fclose(conf_file); + } + + fprintf(sockout, "HELO %s\r\n",username); + fflush(sockout); + + if (fgets(buffer,BUFSIZ,sockin)) { + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, buffer); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } wrefresh (dispwin); + wrefresh (statwin); } - xasprintf (&message2, "MESSAGE \""); + inplin[0]='\0'; while (1) { curs_set (1); c = wgetch (inpwin); - if (c != ERR) { - xasprintf (&message2, "%s%c", message2, (int) c); - waddch (inpwin, c); + if ((c != ERR) && (strlen(inplin)< 1000)) { + switch ((int) c) { + case 13: + break; + case 10: + break; + case 8: + inplin[strlen(inplin)-1]='\0'; + wmove(inpwin,0,strlen(inplin)); + wclrtoeol(inpwin); + break; + default: + sprintf (inplin,"%s%c",inplin,(int) c); + waddch (inpwin, c); + } wrefresh (inpwin); } - numread = 0; - maxread = 100; - numread = read (_socket, buffer, maxread); - if (numread > 0) { + if (fgets(buffer,BUFSIZ,sockin)) { curs_set (0); - buffer[numread] = '\0'; - wprint (dispwin, buffer); + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, &buffer[15]); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } wrefresh (dispwin); + wrefresh (statwin); wrefresh (inpwin); } - if ((int) c == 13) { + if (((int) c == 13) || ((int) c == 10)) { curs_set (0); - xasprintf (&message2, "%s\"\r\n", message2); - ttt_socket_write (_socket, message2, strlen (message2)); - xasprintf (&message2, "MESSAGE \""); - maxread = 100; - numread = read (_socket, buffer, maxread); - if (numread > 0) { - buffer[numread] = '\0'; - wprint (dispwin, buffer); + fprintf(sockout,"MESSAGE \"%s\"\r\n",inplin); + fflush(sockout); + inplin[0] = '\0'; + if (fgets(buffer,BUFSIZ,sockin)) { + if ((strncmp(buffer,"NOTICE MESSAGE ",15) == 0) && (strlen(buffer) > 15)) { + wprint (dispwin, buffer); + } + else { + if (strncmp(buffer,"MESSAGE",7) != 0) wprint (statwin, buffer); + } } wrefresh (dispwin); + wrefresh (statwin); werase (inpwin); wrefresh (inpwin); } @@ -225,18 +283,14 @@ main (int argc, char **argv) static void finish (int sig) { - char buffer[1024]; - ssize_t numread; + char buffer[BUFSIZ]; + endwin(); /* do your non-curses wrapup here */ - xasprintf (&message2, "QUIT\r\n"); - ttt_socket_write (_socket, message2, strlen (message2)); - - maxread = 100; - numread = read (_socket, buffer, maxread); - buffer[numread] = '\0'; - xasprintf (&message2, "%s", buffer); + fprintf(sockout,"QUIT\r\n"); + fflush(sockout); + while (fgets(buffer,BUFSIZ,sockin)); exit (0); } -- 2.43.0