aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chatbot.c2
-rw-r--r--joshua.c6
-rw-r--r--server.c47
-rw-r--r--util.c13
-rw-r--r--util.h2
5 files changed, 40 insertions, 30 deletions
diff --git a/chatbot.c b/chatbot.c
index 27bbdca..d48c432 100644
--- a/chatbot.c
+++ b/chatbot.c
@@ -104,7 +104,7 @@ void do_chatbot(void)
print_string("\n\n");
print_string(exit_responses[rand()%(sizeof(exit_responses)/sizeof(const char*))]);
print_string("\n--CONNECTION TERMINATED--");
- return;
+ exit(EXIT_SUCCESS);
}
}
for(int i=0;i<sizeof(greetings_triggers)/sizeof(const char*);++i)
diff --git a/joshua.c b/joshua.c
index 9520ed5..16fcd10 100644
--- a/joshua.c
+++ b/joshua.c
@@ -46,7 +46,6 @@ void random_stuff(void) /* print random junk on the screen for about 3 seconds *
}
void be_joshua(int fd)
{
- printf("joshua started.\n");
out_fd=fd;
clear();
signal(SIGINT, &cleanup);
@@ -80,7 +79,7 @@ void be_joshua(int fd)
else if(ret==ERR || strcmp(buf, "joshua") && !gamesPhase)
{
print_string("\nIDENTIFICATION NOT RECOGNIZED BY SYSTEM\n--CONNECTION TERMINATED--");
- return;
+ exit(EXIT_SUCCESS);
}
} while(strcmp(buf, "joshua") || gamesPhase);
random_stuff();
@@ -97,10 +96,11 @@ void be_joshua(int fd)
print_string("\n\n");
print_string(exit_responses[rand()%sizeof(exit_responses)/sizeof(const char*)]);
print_string("\n--CONNECTION TERMINATED--");
- return;
+ exit(EXIT_SUCCESS);
}
}
print_string("\n\nHOW ARE YOU FEELING TODAY?\n\n");
refresh();
do_chatbot();
+ exit(EXIT_SUCCESS);
}
diff --git a/server.c b/server.c
index 568c5e6..629d976 100644
--- a/server.c
+++ b/server.c
@@ -1,4 +1,4 @@
-x/*
+/*
* WarGames - a WOPR emulator written in C
* Copyright (C) 2014 Franklin Wei
*
@@ -18,17 +18,18 @@ x/*
* Contact the author at contact@fwei.tk
*/
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <unistd.h>
+#include "joshua.h"
#include <errno.h>
-#include <netinet/in.h>
#include <netdb.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
+#include <netinet/in.h>
#include <signal.h>
-#include "joshua.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
int server_socket;
uint16_t port;
int pipes[FD_SETSIZE][2];
@@ -58,9 +59,15 @@ int process_data(int fd)
char buf[1024];
memset(buf, 0, sizeof(buf));
int ret=read(fd, buf, sizeof(buf));
+ char ctrl_c[]={0xff, 0xf4, 0xff, 0xfd, 0x06, 0x00};
+ if(strcmp(ctrl_c, buf)==0)
+ {
+ printf("Got CTRL-C from client.\n");
+ return -1;
+ }
if(ret<0) /* error */
{
- printf("error in read()\n");
+ printf("Error in read()\n");
return -1;
}
if(ret==0)
@@ -80,7 +87,6 @@ void serv_cleanup()
printf("preparing to exit...\n");
fflush(stdout);
shutdown(server_socket, SHUT_RDWR);
- close(server_socket);
}
int main(int argc, char* argv[])
{
@@ -89,8 +95,8 @@ int main(int argc, char* argv[])
printf("Usage: %s PORT\n", argv[0]);
return 2;
}
- printf("starting server...\n");
port=atoi(argv[1]);
+ printf("Initializing server on port %u...\n", port);
signal(SIGINT, &serv_cleanup);
int sock=make_server_socket(port);
server_socket=sock;
@@ -98,19 +104,19 @@ int main(int argc, char* argv[])
struct sockaddr_in client;
if(listen(sock, 1)<0)
{
- printf("error listening.\n");
+ printf("Error opening socket.\n");
return 1;
}
FD_ZERO(&active_fd_set);
FD_SET(sock, &active_fd_set);
- printf("listening on port %d\n", port);
+ printf("Server ready, listening on port %d.\n", port);
while(1)
{
read_fd_set=active_fd_set;
int ret=select(FD_SETSIZE, &read_fd_set, 0,0,0);
if(ret<0)
{
- printf("select() returned error.\n");
+ printf("Error in select().\n");
return 1;
}
for(int i=0;i<FD_SETSIZE;++i)
@@ -125,21 +131,22 @@ int main(int argc, char* argv[])
new=accept(sock, (struct sockaddr*) &client, &size);
if(new<0)
{
- printf("error accepting connection.\n");
+ printf("Error accepting new connection.\n");
return 1;
}
- printf("new connection from \n");
+ printf("New connection.\n");
FD_SET(new, &active_fd_set);
int ret=pipe(pipes[new]);
if(ret<0)
{
- printf("pipe error.\n");
+ printf("Pipe error.\n");
}
pid_t pid=fork();
if(pid==0) /* child */
{
be_joshua(new);
- close(new);
+ printf("Client exits\n");
+ shutdown(new, SHUT_RDWR);
FD_CLR(new, &active_fd_set);
exit(0);
}
@@ -149,7 +156,7 @@ int main(int argc, char* argv[])
/* data from existing connection */
if(process_data(i)<0)
{
- close(i);
+ shutdown(i, SHUT_RDWR);
FD_CLR(i, &active_fd_set);
}
}
diff --git a/util.c b/util.c
index b44d0f2..9539b46 100644
--- a/util.c
+++ b/util.c
@@ -40,6 +40,7 @@ void print_string(const char* str) /* print string, slowly */
{
write(out_fd, &str[i], 1);
fsync(out_fd);
+ usleep(SLEEP_TIME);
++i;
}
}
@@ -58,6 +59,9 @@ void remove_punct(char* buf)
}
void clear(void)
{
+ unsigned char clear_sequence[]={ 0x1B, 'c' };
+ write(out_fd, clear_sequence, sizeof(clear_sequence));
+ fsync(out_fd);
}
void refresh(void)
{
@@ -65,16 +69,15 @@ void refresh(void)
}
int getnstr(char* buf, int max)
{
- printf("reading...\n");
memset(buf, 0, max);
int ret=read(pipes[out_fd][0], buf, max);
if(ret!=0)
{
- printf("last char is 0x%02x\n", buf[strlen(buf)-1]);
- /* remove the newline */
- buf[strlen(buf)-2]='\0';
+ /* prevent buffer underflow */
+ if(strlen(buf)-2>=0)
+ /* remove the newline */
+ buf[strlen(buf)-2]='\0';
}
- printf("got string \"%s\"\n", buf);
if(ret<0)
return ERR;
return OK;
diff --git a/util.h b/util.h
index 003f8e7..5f7e170 100644
--- a/util.h
+++ b/util.h
@@ -14,7 +14,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ *
* Contact the author at contact@fwei.tk
*/