Bedtime commit, misc changes
authorJohn Hodge <[email protected]>
Wed, 2 Mar 2011 16:08:21 +0000 (00:08 +0800)
committerJohn Hodge <[email protected]>
Wed, 2 Mar 2011 16:08:21 +0000 (00:08 +0800)
- Debugging in threads
- TCP fixes
- Overhaul of IRC client
 > TODO: GUI instead of stream based (or at least implement a consistent prompt)
- Fixed type error in strchr() and friends

Kernel/threads.c
Modules/IPStack/tcp.c
Usermode/Applications/irc_src/main.c
Usermode/include/string.h

index 33b85c6..62bb7e0 100644 (file)
@@ -782,6 +782,8 @@ void Threads_AddActive(tThread *Thread)
                tThread *cur = Proc_GetCurThread();
                Warning("WTF, CPU%i %p (%i %s) is adding %p (%i %s) when it is active",
                        GetCPUNum(), cur, cur->TID, cur->ThreadName, Thread, Thread->TID, Thread->ThreadName);
+               SHORTREL( &glThreadListLock );
+               return ;
        }
        
        // Set state
index 1662ca0..50eacbc 100644 (file)
@@ -386,7 +386,7 @@ void TCP_INT_HandleConnectionPacket(tTCPConnection *Connection, tTCPHeader *Head
                // /NOTES
                
                // Allocate and fill cached packet
-               pkt = malloc( dataLen + sizeof(tTCPStoredPacket) );
+               pkt = malloc( sizeof(tTCPStoredPacket) + dataLen );
                pkt->Next = NULL;
                pkt->Sequence = ntohl(Header->SequenceNumber);
                pkt->Length = dataLen;
index 2599127..0c90c50 100644 (file)
 \r
 // === TYPES ===\r
 typedef struct sServer {\r
+       struct sServer  *Next;\r
         int    FD;\r
        char    InBuf[BUFSIZ+1];\r
         int    ReadPos;\r
+       char    Name[];\r
 } tServer;\r
 \r
+typedef struct sMessage\r
+{\r
+       struct sMessage *Next;\r
+       time_t  Timestamp;\r
+       tServer *Server;\r
+        int    Type;\r
+       char    *Source;        // Pointer into `Data`\r
+       char    Data[];\r
+}      tMessage;\r
+\r
+typedef struct sWindow\r
+{\r
+       struct sWindow  *Next;\r
+       tMessage        *Messages;\r
+       tServer *Server;        //!< Canoical server (can be NULL)\r
+        int    ActivityLevel;\r
+       char    Name[]; // Channel name / remote user\r
+}      tWindow;\r
+\r
+enum eMessageTypes\r
+{\r
+       MSG_TYPE_NULL,\r
+       MSG_TYPE_SERVER,        // Server message\r
+       \r
+       MSG_TYPE_NOTICE,        // NOTICE command\r
+       MSG_TYPE_JOIN,  // JOIN command\r
+       MSG_TYPE_PART,  // PART command\r
+       MSG_TYPE_QUIT,  // QUIT command\r
+       \r
+       MSG_TYPE_STANDARD,      // Standard line\r
+       MSG_TYPE_ACTION,        // /me\r
+       \r
+       MSG_TYPE_UNK\r
+};\r
+\r
 // === PROTOTYPES ===\r
  int   ParseArguments(int argc, const char *argv[]);\r
+ int   ParseUserCommand(char *String);\r
+// --- \r
+tServer        *Server_Connect(const char *Name, const char *AddressString, short PortNumber);\r
+tMessage       *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message);\r
+tWindow        *Window_Create(tServer *Server, const char *Name);\r
+\r
  int   ProcessIncoming(tServer *Server);\r
+// --- Helpers\r
  int   writef(int FD, const char *Format, ...);\r
  int   OpenTCP(const char *AddressString, short PortNumber);\r
+char   *GetValue(char *Str, int *Ofs);\r
+static inline int      isdigit(int ch);\r
 \r
 // === GLOBALS ===\r
 char   *gsUsername = "root";\r
 char   *gsHostname = "acess";\r
-char   *gsRemoteAddress = NULL;\r
 char   *gsRealName = "Acess2 IRC Client";\r
 char   *gsNickname = "acess";\r
-short  giRemotePort = 6667;\r
+tServer        *gpServers;\r
+tWindow        gWindow_Status = {\r
+       NULL, NULL, NULL,       // No next, empty list, no server\r
+       0, ""   // No activity, empty name (rendered as status)\r
+};\r
+tWindow        *gpWindows = &gWindow_Status;\r
+tWindow        *gpCurrentWindow = &gWindow_Status;\r
 \r
 // ==== CODE ====\r
 int main(int argc, const char *argv[], const char *envp[])\r
 {\r
         int    tmp;\r
-       tServer srv;\r
        tReadline       *readline_info;\r
        \r
-       memset(&srv, 0, sizeof(srv));\r
-       \r
        // Parse Command line\r
-       // - Sets the server configuration globals\r
-       if( (tmp = ParseArguments(argc, argv)) ) {\r
-               return tmp;\r
-       }\r
-       \r
-       // Connect to the remove server\r
-       srv.FD = OpenTCP( gsRemoteAddress, giRemotePort );\r
-       if( srv.FD == -1 ) {\r
-               fprintf(stderr, "Unable to create socket\n");\r
-               return -1;\r
-       }\r
-       \r
-       printf("Connection opened\n");\r
-       ProcessIncoming(&srv);\r
+       if( (tmp = ParseArguments(argc, argv)) )        return tmp;\r
        \r
-       writef(srv.FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, gsRemoteAddress, gsRealName);\r
-       writef(srv.FD, "NICK %s\n", gsNickname);\r
+       // HACK: Static server entry\r
+       // UCC (University [of Western Australia] Computer Club) IRC Server\r
+       gWindow_Status.Server = Server_Connect( "UCC", "130.95.13.18", 6667 );\r
        \r
-       printf("Processing\n");\r
+       if( !gWindow_Status.Server )\r
+               return -1;\r
        \r
        readline_info = Readline_Init(1);\r
        \r
        for( ;; )\r
        {\r
-               fd_set  readfds;\r
-                int    rv;\r
+               fd_set  readfds, errorfds;\r
+                int    rv, maxFD = 0;\r
+               tServer *srv;\r
                \r
                FD_ZERO(&readfds);\r
+               FD_ZERO(&errorfds);\r
                FD_SET(0, &readfds);    // stdin\r
-               FD_SET(srv.FD, &readfds);\r
                \r
-               rv = select(srv.FD+1, &readfds, 0, 0, NULL);\r
+               // Fill server FDs in fd_set\r
+               for( srv = gpServers; srv; srv = srv->Next )\r
+               {\r
+                       FD_SET(srv->FD, &readfds);\r
+                       FD_SET(srv->FD, &errorfds);\r
+                       if( srv->FD > maxFD )\r
+                               maxFD = srv->FD;\r
+               }\r
+               \r
+               rv = select(maxFD+1, &readfds, 0, &errorfds, NULL);\r
                if( rv == -1 )  break;\r
                \r
                if(FD_ISSET(0, &readfds))\r
@@ -81,24 +128,40 @@ int main(int argc, const char *argv[], const char *envp[])
                        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
+                               {\r
+                                       ParseUserCommand(cmd);\r
+                               }\r
                                free(cmd);\r
                        }\r
                }\r
                \r
                // Server response\r
-               if(FD_ISSET(srv.FD, &readfds))\r
+               for( srv = gpServers; srv; srv = srv->Next )\r
                {\r
-                       if( ProcessIncoming(&srv) != 0 ) {\r
-                               // Oops, error\r
+                       if(FD_ISSET(srv->FD, &readfds))\r
+                       {\r
+                               if( ProcessIncoming(srv) != 0 ) {\r
+                                       // Oops, error\r
+                                       break;\r
+                               }\r
+                       }\r
+                       \r
+                       if(FD_ISSET(srv->FD, &errorfds))\r
+                       {\r
                                break;\r
                        }\r
                }\r
+               \r
+               // Oops, an error\r
+               if( srv )       break;\r
        }\r
        \r
-       close(srv.FD);\r
+       {\r
+               tServer *srv;\r
+               for( srv = gpServers; srv; srv = srv->Next )\r
+                       close(srv->FD);\r
+       }\r
        return 0;\r
 }\r
 \r
@@ -107,58 +170,293 @@ int main(int argc, const char *argv[], const char *envp[])
  */\r
 int ParseArguments(int argc, const char *argv[])\r
 {\r
-       gsRemoteAddress = "130.95.13.18";       // irc.ucc.asn.au\r
-       \r
        return 0;\r
 }\r
 \r
-void Cmd_PRIVMSG(tServer *Server, const char *Dest, const char *Src, const char *Message)\r
+int ParseUserCommand(char *String)\r
 {\r
-       printf("%p: %s => %s\t%s\n", Server, Src, Dest, Message);\r
+       if( String[0] == '/' )\r
+       {\r
+               char    *command;\r
+                int    pos = 0;\r
+               \r
+               command = GetValue(String, &pos);\r
+               \r
+               if( strcmp(command, "/join") == 0 )\r
+               {\r
+                       char    *channel_name = GetValue(String, &pos);\r
+                       \r
+                       if( gpCurrentWindow->Server )\r
+                       {\r
+                               writef(gpCurrentWindow->Server->FD, "JOIN %s\n",  channel_name);\r
+                       }\r
+               }\r
+               else if( strcmp(command, "/quit") == 0 )\r
+               {\r
+                       char    *quit_message = GetValue(String, &pos);\r
+                       tServer *srv;\r
+                       \r
+                       if( !quit_message )\r
+                               quit_message = "/quit - Acess2 IRC Client";\r
+                       \r
+                       for( srv = gpServers; srv; srv = srv->Next )\r
+                       {\r
+                               writef(srv->FD, "QUIT %s\n", quit_message);\r
+                       }\r
+               }\r
+               else if( strcmp(command, "/window") == 0 || strcmp(command, "/win") == 0 || strcmp(command, "/w") == 0 )\r
+               {\r
+                       char    *window_id = GetValue(String, &pos);\r
+                        int    window_num = atoi(window_id);\r
+                       \r
+                       if( window_num > 0 )\r
+                       {\r
+                               tWindow *win;\r
+                               window_num --;  // Move to base 0\r
+                               // Get `window_num`th window\r
+                               for( win = gpWindows; win && window_num--; win = win->Next );\r
+                               if( win ) {\r
+                                       gpCurrentWindow = win;\r
+                                       if( win->Name[0] )\r
+                                               printf("[%s:%s] ", win->Server->Name, win->Name);\r
+                                       else\r
+                                               printf("[(status)] ", win->Server->Name, win->Name);\r
+                               }\r
+                               // Otherwise, silently ignore\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                        int    len = snprintf(NULL, 0, "Unknown command %s", command);\r
+                       char    buf[len+1];\r
+                       snprintf(buf, len+1, "Unknown command %s", command);\r
+                       Message_Append(NULL, MSG_TYPE_SERVER, "client", "", buf);\r
+               }\r
+       }\r
+       else\r
+       {\r
+               // Message\r
+               // - Only send if server is valid and window name is non-empty\r
+               if( gpCurrentWindow->Server && gpCurrentWindow->Name[0] )\r
+               {\r
+                       writef(gpCurrentWindow->Server->FD,\r
+                               "PRIVMSG %s :%s\n", gpCurrentWindow->Name,\r
+                               String\r
+                               );\r
+               }\r
+       }\r
+       \r
+       return 0;\r
 }\r
 \r
 /**\r
- * \brief Read a space-separated value from a string\r
+ * \brief Connect to a server\r
  */\r
-char *GetValue(char *Src, int *Ofs)\r
+tServer *Server_Connect(const char *Name, const char *AddressString, short PortNumber)\r
 {\r
-        int    pos = *Ofs;\r
-       char    *ret = Src + pos;\r
-       char    *end;\r
+       tServer *ret;\r
        \r
-       if( !Src )      return NULL;\r
+       ret = calloc(1, sizeof(tServer) + strlen(Name) + 1);\r
        \r
-       while( *ret == ' ' )    ret ++;\r
+       strcpy(ret->Name, Name);\r
        \r
-       end = strchr(ret, ' ');\r
-       if( end ) {\r
-               *end = '\0';\r
+       // Connect to the remove server\r
+       ret->FD = OpenTCP( AddressString, PortNumber );\r
+       if( ret->FD == -1 ) {\r
+               fprintf(stderr, "%s: Unable to create socket\n", Name);\r
+               return NULL;\r
        }\r
-       else {\r
-               end = ret + strlen(ret) - 1;\r
+       \r
+       // Append to open list\r
+       ret->Next = gpServers;\r
+       gpServers = ret;\r
+       \r
+       // Read some initial data\r
+       printf("%s: Connection opened\n", Name);\r
+       ProcessIncoming(ret);\r
+       \r
+       // Identify\r
+       writef(ret->FD, "USER %s %s %s : %s\n", gsUsername, gsHostname, AddressString, gsRealName);\r
+       writef(ret->FD, "NICK %s\n", gsNickname);\r
+       printf("%s: Identified\n", Name);\r
+       \r
+       return ret;\r
+}\r
+\r
+tMessage *Message_Append(tServer *Server, int Type, const char *Source, const char *Dest, const char *Message)\r
+{\r
+       tMessage        *ret;\r
+       tWindow *win = NULL;\r
+        int    msgLen = strlen(Message);\r
+       \r
+       // NULL servers are internal messages\r
+       if( Server == NULL )\r
+       {\r
+               win = &gWindow_Status;\r
+       }\r
+       // Determine if it's a channel or PM message\r
+       else if( Dest[0] == '#' || Dest[0] == '&' )     // TODO: Better determining here\r
+       {\r
+               tWindow *prev = NULL;\r
+               for(win = gpWindows; win; prev = win, win = win->Next)\r
+               {\r
+                       if( win->Server == Server && strcmp(win->Name, Dest) == 0 )\r
+                       {\r
+                               break;\r
+                       }\r
+               }\r
+               if( !win ) {\r
+                       win = Window_Create(Server, Dest);\r
+               }\r
+       }\r
+       #if 0\r
+       else if( strcmp(Dest, Server->Nick) != 0 )\r
+       {\r
+               // Umm... message for someone who isn't us?\r
+               win = &gWindow_Status;  // Stick it in the status window, just in case\r
+       }\r
+       #endif\r
+       // Server message?\r
+       else if( strchr(Source, '.') )  // TODO: And again, less hack please\r
+       {\r
+               #if 1\r
+               for(win = gpWindows; win; win = win->Next)\r
+               {\r
+                       if( win->Server == Server && strcmp(win->Name, Source) == 0 )\r
+                       {\r
+                               break;\r
+                       }\r
+               }\r
+               #endif\r
+               if( !win ) {\r
+                       win = &gWindow_Status;\r
+               }\r
+               \r
+       }\r
+       // Private message\r
+       else\r
+       {\r
+               for(win = gpWindows; win; win = win->Next)\r
+               {\r
+                       if( win->Server == Server && strcmp(win->Name, Source) == 0 )\r
+                       {\r
+                               break;\r
+                       }\r
+               }\r
+               if( !win ) {\r
+                       win = Window_Create(Server, Dest);\r
+               }\r
+       }\r
+       \r
+       ret = malloc( sizeof(tMessage) + msgLen + 1 + strlen(Source) + 1 );\r
+       ret->Source = ret->Data + msgLen + 1;\r
+       strcpy(ret->Source, Source);\r
+       strcpy(ret->Data, Message);\r
+       ret->Type = Type;\r
+       ret->Server = Server;\r
+       \r
+       // TODO: Append to window message list\r
+       ret->Next = win->Messages;\r
+       win->Messages = ret;\r
+       \r
+       return ret;\r
+}\r
+\r
+tWindow *Window_Create(tServer *Server, const char *Name)\r
+{\r
+       tWindow *ret, *prev = NULL;\r
+        int    num = 1;\r
+       \r
+       // Get the end of the list\r
+       // TODO: Cache this instead\r
+       for( ret = gpCurrentWindow; ret; prev = ret, ret = ret->Next )\r
+               num ++;\r
+       \r
+       ret = malloc(sizeof(tWindow) + strlen(Name) + 1);\r
+       ret->Messages = NULL;\r
+       ret->Server = Server;\r
+       ret->ActivityLevel = 1;\r
+       strcpy(ret->Name, Name);\r
+       \r
+       if( prev ) {\r
+               ret->Next = prev->Next;\r
+               prev->Next = ret;\r
        }\r
-       *Ofs = end - Src + 1;\r
+       else {  // Shouldn't happen really\r
+               ret->Next = gpWindows;\r
+               gpWindows = ret;\r
+       }\r
+       \r
+       printf("Win %i %s:%s created\n", num, Server->Name, Name);\r
        \r
        return ret;\r
 }\r
 \r
+void Cmd_PRIVMSG(tServer *Server, const char *Dest, const char *Src, const char *Message)\r
+{\r
+       printf("<%s:%s:%s> %s\n", Server->Name, Dest, Src, Message);\r
+}\r
+\r
 /**\r
  */\r
 void ParseServerLine(tServer *Server, char *Line)\r
 {\r
         int    pos = 0;\r
        char    *ident, *cmd;\r
-       if( *Line == ':' ) {\r
-               // Message\r
-               ident = GetValue(Line, &pos);\r
-               pos ++; // Space\r
+       \r
+       // Message?\r
+       if( *Line == ':' )\r
+       {\r
+               ident = GetValue(Line, &pos);   // Ident (user or server)\r
                cmd = GetValue(Line, &pos);\r
                \r
-               if( strcmp(cmd, "PRIVMSG") == 0 ) {\r
+               // Numeric command\r
+               if( isdigit(cmd[0]) && isdigit(cmd[1]) && isdigit(cmd[2]) )\r
+               {\r
+                       char    *user, *message;\r
+                        int    num;\r
+                       num  = (cmd[0] - '0') * 100;\r
+                       num += (cmd[1] - '0') * 10;\r
+                       num += (cmd[2] - '0') * 1;\r
+                       \r
+                       user = GetValue(Line, &pos);\r
+                       \r
+                       if( Line[pos] == ':' ) {\r
+                               message = Line + pos + 1;\r
+                       }\r
+                       else {\r
+                               message = GetValue(Line, &pos);\r
+                       }\r
+                       \r
+                       switch(num)\r
+                       {\r
+                       default:\r
+                               printf("[%s] %i %s\n", Server->Name, num, message);\r
+                               Message_Append(Server, MSG_TYPE_SERVER, ident, user, message);\r
+                               break;\r
+                       }\r
+               }\r
+               else if( strcmp(cmd, "NOTICE") == 0 )\r
+               {\r
+                       char    *class, *message;\r
+                       \r
+                       class = GetValue(Line, &pos);\r
+                       \r
+                       if( Line[pos] == ':' ) {\r
+                               message = Line + pos + 1;\r
+                       }\r
+                       else {\r
+                               message = GetValue(Line, &pos);\r
+                       }\r
+                       \r
+                       printf("[%s] NOTICE %s: %s\n", Server->Name, ident, message);\r
+                       Message_Append(Server, MSG_TYPE_NOTICE, ident, "", message);\r
+               }\r
+               else if( strcmp(cmd, "PRIVMSG") == 0 )\r
+               {\r
                        char    *dest, *message;\r
-                       pos ++;\r
                        dest = GetValue(Line, &pos);\r
-                       pos ++;\r
+                       \r
                        if( Line[pos] == ':' ) {\r
                                message = Line + pos + 1;\r
                        }\r
@@ -166,10 +464,17 @@ void ParseServerLine(tServer *Server, char *Line)
                                message = GetValue(Line, &pos);\r
                        }\r
                        Cmd_PRIVMSG(Server, dest, ident, message);\r
+                       Message_Append(Server, MSG_TYPE_STANDARD, ident, dest, message);\r
+               }\r
+               else\r
+               {\r
+                       printf("Unknown message %s (%s)\n", cmd, Line+pos);\r
                }\r
        }\r
        else {\r
+               \r
                // Command to client\r
+               printf("Client Command: %s", Line);\r
        }\r
 }\r
 \r
@@ -201,7 +506,7 @@ int ProcessIncoming(tServer *Server)
                while( (newline = strchr(ptr, '\n')) )\r
                {\r
                        *newline = '\0';\r
-                       printf("%s\n", ptr);\r
+                       if( newline[-1] == '\r' )       newline[-1] = '\0';\r
                        ParseServerLine(Server, ptr);\r
                        ptr = newline + 1;\r
                }\r
@@ -305,3 +610,36 @@ int OpenTCP(const char *AddressString, short PortNumber)
        // Return descriptor\r
        return fd;\r
 }\r
+\r
+/**\r
+ * \brief Read a space-separated value from a string\r
+ */\r
+char *GetValue(char *Src, int *Ofs)\r
+{\r
+        int    pos = *Ofs;\r
+       char    *ret = Src + pos;\r
+       char    *end;\r
+       \r
+       if( !Src )      return NULL;\r
+       \r
+       while( *ret == ' ' )    ret ++;\r
+       \r
+       end = strchr(ret, ' ');\r
+       if( end ) {\r
+               *end = '\0';\r
+       }\r
+       else {\r
+               end = ret + strlen(ret) - 1;\r
+       }\r
+       \r
+       end ++ ;\r
+       while( *ret == ' ' )    end ++;\r
+       *Ofs = end - Src;\r
+       \r
+       return ret;\r
+}\r
+\r
+static inline int isdigit(int ch)\r
+{\r
+       return '0' <= ch && ch < '9';\r
+}\r
index a3c4d31..00f748d 100644 (file)
@@ -16,15 +16,15 @@ extern char *strncpy(char *dst, const char *src, size_t num);
 extern char    *strcat(char *dst, const char *src);
 extern char    *strdup(const char *src);
 extern char    *strndup(const char *src, int length);
-extern char    *strchr(char *str, int character);
-extern char    *strrchr(char *str, int character);
-extern char    *strstr(char *str1, const char *str2);
+extern char    *strchr(const char *str, int character);
+extern char    *strrchr(const char *str, int character);
+extern char    *strstr(const char *str1, const char *str2);
 
 // Memory
 extern void *memset(void *dest, int val, size_t count);
 extern void *memcpy(void *dest, const void *src, size_t count);
 extern void *memmove(void *dest, const void *src, size_t count);
 extern int     memcmp(const void *mem1, const void *mem2, size_t count);
-extern void    *memchr(void *ptr, int value, size_t num);
+extern void    *memchr(const void *ptr, int value, size_t num);
 
 #endif

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