IRC Client - Added Readline support, error catching
authorJohn Hodge <[email protected]>
Wed, 2 Mar 2011 06:11:45 +0000 (14:11 +0800)
committerJohn Hodge <[email protected]>
Wed, 2 Mar 2011 06:11:45 +0000 (14:11 +0800)
Usermode/Applications/irc_src/Makefile
Usermode/Applications/irc_src/main.c

index 5103870..9fab060 100644 (file)
@@ -2,7 +2,7 @@
 
 -include ../Makefile.cfg
 
-LDFLAGS += -lnet
+LDFLAGS += -lnet -lreadline
 
 OBJ = main.o
 BIN = irc
index c7535fa..2599127 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdio.h>\r
 #include <string.h>\r
 #include <net.h>\r
+#include <readline.h>\r
 \r
 #define BUFSIZ 1023\r
 \r
@@ -18,7 +19,7 @@ typedef struct sServer {
 \r
 // === PROTOTYPES ===\r
  int   ParseArguments(int argc, const char *argv[]);\r
-void   ProcessIncoming(tServer *Server);\r
+ int   ProcessIncoming(tServer *Server);\r
  int   writef(int FD, const char *Format, ...);\r
  int   OpenTCP(const char *AddressString, short PortNumber);\r
 \r
@@ -35,6 +36,7 @@ int main(int argc, const char *argv[], const char *envp[])
 {\r
         int    tmp;\r
        tServer srv;\r
+       tReadline       *readline_info;\r
        \r
        memset(&srv, 0, sizeof(srv));\r
        \r
@@ -59,6 +61,8 @@ int main(int argc, const char *argv[], const char *envp[])
        \r
        printf("Processing\n");\r
        \r
+       readline_info = Readline_Init(1);\r
+       \r
        for( ;; )\r
        {\r
                fd_set  readfds;\r
@@ -68,18 +72,29 @@ int main(int argc, const char *argv[], const char *envp[])
                FD_SET(0, &readfds);    // stdin\r
                FD_SET(srv.FD, &readfds);\r
                \r
-               rv = select(srv.FD, &readfds, 0, 0, NULL);\r
+               rv = select(srv.FD+1, &readfds, 0, 0, NULL);\r
                if( rv == -1 )  break;\r
                \r
                if(FD_ISSET(0, &readfds))\r
                {\r
                        // User input\r
+                       char    *cmd = Readline_NonBlock(readline_info);\r
+                       if( cmd )\r
+                       {\r
+                               // TODO: Implement windows / proper commands, but meh\r
+                               if( cmd[0] )\r
+                                       writef(srv.FD, "%s\n", cmd);\r
+                               free(cmd);\r
+                       }\r
                }\r
                \r
                // Server response\r
                if(FD_ISSET(srv.FD, &readfds))\r
                {\r
-                       ProcessIncoming(&srv);\r
+                       if( ProcessIncoming(&srv) != 0 ) {\r
+                               // Oops, error\r
+                               break;\r
+                       }\r
                }\r
        }\r
        \r
@@ -161,7 +176,7 @@ void ParseServerLine(tServer *Server, char *Line)
 /**\r
  * \brief Process incoming lines from the server\r
  */\r
-void ProcessIncoming(tServer *Server)\r
+int ProcessIncoming(tServer *Server)\r
 {      \r
        char    *ptr, *newline;\r
         int    len;\r
@@ -176,6 +191,9 @@ void ProcessIncoming(tServer *Server)
        #endif\r
                // Read data\r
                len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]);\r
+               if( len == -1 ) {\r
+                       return -1;\r
+               }\r
                Server->InBuf[Server->ReadPos + len] = '\0';\r
                \r
                // Break into lines\r
@@ -203,6 +221,8 @@ void ProcessIncoming(tServer *Server)
        #if NON_BLOCK_READ\r
        }\r
        #endif\r
+       \r
+       return 0;\r
 }\r
 \r
 /**\r

UCC git Repository :: git.ucc.asn.au