X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=28007e12027ff90b9b9a5564a3d1c81c728188cb;hb=479d0634670b58da044bc58149662adba0ad1d0b;hp=807527b1e59c69ef94afe34ba4a9327ce7ee4e5d;hpb=6c7e82169e66197939b83945812b02787ed0f52e;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index 807527b1..28007e12 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -7,18 +7,14 @@ #include #include #include "header.h" - -#if USE_READLINE -# include "readline.h" -#endif +#include #define _stdin 0 #define _stdout 1 #define _stderr 2 // ==== PROTOTYPES ==== -char *ReadCommandLine(int *Length); -void Parse_Args(char *str, char **dest); + int Parse_Args(const char *str, char **dest); void CallCommand(char **Args); void Command_Logout(int argc, char **argv); void Command_Clear(int argc, char **argv); @@ -49,18 +45,13 @@ char **gasCommandHistory; int giCommandSpace = 0; // ==== CODE ==== -int main(int argc, char *argv[], char *envp[]) +int main(int argc, char *argv[], char **envp) { char *sCommandStr; char *saArgs[32] = {0}; - int length = 0; int i; int iArgCount = 0; - #if !USE_READLINE - int bCached = 1; - #else tReadline *readline_state = Readline_Init(1); - #endif gasEnvironment = envp; @@ -81,46 +72,16 @@ int main(int argc, char *argv[], char *envp[]) for(;;) { // Free last command & arguments - if(saArgs[0]) free(saArgs); - #if !USE_READLINE - if(!bCached) free(sCommandStr); - bCached = 0; - #endif + if(saArgs[0]) free(saArgs[0]); printf("%s$ ", gsCurrentDirectory); // Read Command line - #if USE_READLINE sCommandStr = Readline( readline_state ); printf("\n"); - length = strlen(sCommandStr); - #else - sCommandStr = ReadCommandLine( &length ); - - if(!sCommandStr) { - printf("PANIC: Out of heap space\n"); - return -1; - } - - // Check if the command should be cached - if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0) - { - if(giLastCommand >= giCommandSpace) { - giCommandSpace += 12; - gasCommandHistory = realloc(gasCommandHistory, giCommandSpace*sizeof(char*)); - } - giLastCommand ++; - gasCommandHistory[ giLastCommand ] = sCommandStr; - bCached = 1; - } - #endif // Parse Command Line into arguments - Parse_Args(sCommandStr, saArgs); - - // Count Arguments - iArgCount = 0; - while(saArgs[iArgCount]) iArgCount++; + iArgCount = Parse_Args(sCommandStr, saArgs); // Silently Ignore all empty commands if(saArgs[1][0] == '\0') continue; @@ -139,157 +100,15 @@ int main(int argc, char *argv[], char *envp[]) // Shall we? CallCommand( &saArgs[1] ); - #if USE_READLINE free( sCommandStr ); - #endif } } -#if USE_READLINE /** - * \fn char *ReadCommandLine(int *Length) - * \brief Read from the command line - */ -char *ReadCommandLine(int *Length) -{ - char *ret; - int len, pos, space = 1023; - char ch; - #if 0 - int scrollbackPos = giLastCommand; - #endif - - // Preset Variables - ret = malloc( space+1 ); - if(!ret) return NULL; - len = 0; pos = 0; - - // Read In Command Line - do { - ch = getchar(); // Read Character from stdin (read is a blocking call) - if(ch == '\n') break; - - switch(ch) - { - // Control characters - case '\x1B': - ch = getchar(); // Read control character - switch(ch) - { - //case 'D': if(pos) pos--; break; - //case 'C': if(pos 0 ) break; - - free(ret); - ret = strdup( gasCommandHistory[--scrollbackPos] ); - - len = strlen(ret); - while(pos--) printf("\x1B[D"); - write(_stdout, len, ret); pos = len; - while(pos++ < oldLen) write(_stdout, 1, " "); - } - break; - case 'B': // Down - { - int oldLen = len; - if( scrollbackPos < giLastCommand-1 ) break; - - free(ret); - ret = strdup( gasCommandHistory[++scrollbackPos] ); - - len = strlen(ret); - while(pos--) write(_stdout, 3, "\x1B[D"); - write(_stdout, len, ret); pos = len; - while(pos++ < oldLen) write(_stdout, 1, " "); - } - break; - #endif - case 'D': // Left - if(pos == 0) break; - pos --; - printf("\x1B[D"); - break; - case 'C': // Right - if(pos == len) break; - pos++; - printf("\x1B[C"); - break; - } - } - break; - - // Backspace - case '\b': - if(len <= 0) break; // Protect against underflows - putchar(ch); - if(pos == len) { // Simple case of end of string - len --; - pos--; - } - else { - printf("%.*s ", len-pos, &ret[pos]); // Move Text - printf("\x1B[%iD", len-pos+1); - // Alter Buffer - memmove(&ret[pos-1], &ret[pos], len-pos); - pos --; - len --; - } - break; - - // Tab - case '\t': - //TODO: Implement Tab-Completion - //Currently just ignore tabs - break; - - default: - // Expand Buffer - if(len+1 > space) { - space += 256; - ret = realloc(ret, space+1); - if(!ret) return NULL; - } - - // Editing inside the buffer - if(pos != len) { - putchar(ch); // Print new character - printf("%.*s", len-pos, &ret[pos]); // Move Text - printf("\x1B[%iD", len-pos); - memmove( &ret[pos+1], &ret[pos], len-pos ); - } - else { - putchar(ch); - } - ret[pos++] = ch; - len ++; - break; - } - } while(ch != '\n'); - - // Cap String - ret[len] = '\0'; - printf("\n"); - - // Return length - if(Length) *Length = len; - - return ret; -} -#endif - -/** - * \fn void Parse_Args(char *str, char **dest) + * \fn int Parse_Args(const char *str, char **dest) * \brief Parse a string into an argument array */ -void Parse_Args(char *str, char **dest) +int Parse_Args(const char *str, char **dest) { int i = 1; char *buf = malloc( strlen(str) + 1 ); @@ -297,7 +116,7 @@ void Parse_Args(char *str, char **dest) if(buf == NULL) { dest[0] = NULL; printf("Parse_Args: Out of heap space!\n"); - return ; + return 0; } strcpy(buf, str); @@ -324,10 +143,8 @@ void Parse_Args(char *str, char **dest) if(*buf == '\0') break; } dest[i] = NULL; - if(i == 1) { - free(buf); - dest[0] = NULL; - } + + return i; } /** @@ -348,15 +165,15 @@ void CallCommand(char **Args) { GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer); // Check file existence - fd = open(sTmpBuffer, OPENFLAG_EXEC); + fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC); if(fd == -1) { printf("Unknown Command: `%s'\n", Args[0]); // Error Message return ; } // Get File info and close file - finfo( fd, &info, 0 ); - close( fd ); + _SysFInfo( fd, &info, 0 ); + _SysClose( fd ); // Check if the file is a directory if(info.flags & FILEFLAG_DIRECTORY) { @@ -372,10 +189,10 @@ void CallCommand(char **Args) for( i = 0; i < giNumPathDirs; i++ ) { GeneratePath(exefile, gasPathDirs[i], sTmpBuffer); - fd = open(sTmpBuffer, OPENFLAG_EXEC); + fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC); if(fd == -1) continue; - finfo( fd, &info, 0 ); - close( fd ); + _SysFInfo( fd, &info, 0 ); + _SysClose( fd ); if(info.flags & FILEFLAG_DIRECTORY) continue; // Woohoo! We found a valid command break; @@ -389,16 +206,14 @@ void CallCommand(char **Args) } // Create new process - pid = clone(CLONE_VM, 0); - // Start Task - if(pid == 0) - execve(sTmpBuffer, Args, gasEnvironment); + int fds[] = {0, 1, 2}; + pid = _SysSpawn(sTmpBuffer, (const char **)Args, (const char **)gasEnvironment, 3, fds, NULL); if(pid <= 0) { printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message } else { int status; - waittid(pid, &status); + _SysWaitTID(pid, &status); } } @@ -436,7 +251,7 @@ void Command_Help(int argc, char **argv) */ void Command_Clear(int argc, char **argv) { - write(_stdout, "\x1B[2J", 4); //Clear Screen + _SysWrite(_stdout, "\x1B[2J", 4); //Clear Screen } /** @@ -457,13 +272,13 @@ void Command_Cd(int argc, char **argv) GeneratePath(argv[1], gsCurrentDirectory, tmpPath); - fp = open(tmpPath, 0); + fp = _SysOpen(tmpPath, 0); if(fp == -1) { printf("Directory does not exist\n"); return; } - finfo(fp, &stats, 0); - close(fp); + _SysFInfo(fp, &stats, 0); + _SysClose(fp); if( !(stats.flags & FILEFLAG_DIRECTORY) ) { printf("Not a Directory\n"); @@ -475,7 +290,7 @@ void Command_Cd(int argc, char **argv) strcpy(gsCurrentDirectory, tmpPath); // Register change with kernel - chdir( gsCurrentDirectory ); + _SysChdir( gsCurrentDirectory ); } /** @@ -501,7 +316,7 @@ void Command_Dir(int argc, char **argv) dirLen = strlen(tmpPath); // Open Directory - dp = open(tmpPath, OPENFLAG_READ); + dp = _SysOpen(tmpPath, OPENFLAG_READ); // Check if file opened if(dp == -1) { @@ -509,16 +324,16 @@ void Command_Dir(int argc, char **argv) return; } // Get File Stats - if( finfo(dp, &info, 0) == -1 ) + if( _SysFInfo(dp, &info, 0) == -1 ) { - close(dp); + _SysClose(dp); printf("stat Failed, Bad File Descriptor\n"); return; } // Check if it's a directory if(!(info.flags & FILEFLAG_DIRECTORY)) { - close(dp); + _SysClose(dp); printf("Unable to open directory `%s', Not a directory\n", tmpPath); return; } @@ -532,7 +347,7 @@ void Command_Dir(int argc, char **argv) fileName = (char*)(tmpPath+dirLen); // Read Directory Content - while( (fp = readdir(dp, fileName)) ) + while( (fp = _SysReadDir(dp, fileName)) ) { if(fp < 0) { @@ -541,10 +356,10 @@ void Command_Dir(int argc, char **argv) break; } // Open File - fp = open(tmpPath, 0); + fp = _SysOpen(tmpPath, 0); if(fp == -1) continue; // Get File Stats - finfo(fp, &info, 0); + _SysFInfo(fp, &info, 0); if(info.flags & FILEFLAG_DIRECTORY) printf("d"); @@ -553,25 +368,25 @@ void Command_Dir(int argc, char **argv) // Print Mode // - Owner - acl.group = 0; acl.id = info.uid; + acl.object = info.uid; _SysGetACL(fp, &acl); if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-'; if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-'; if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-'; // - Group - acl.group = 1; acl.id = info.gid; + acl.object = info.gid | 0x80000000; _SysGetACL(fp, &acl); if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-'; if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-'; if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-'; // - World - acl.group = 1; acl.id = -1; + acl.object = 0xFFFFFFFF; _SysGetACL(fp, &acl); if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-'; if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-'; if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-'; printf(modeStr); - close(fp); + _SysClose(fp); // Colour Code if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green @@ -592,5 +407,5 @@ void Command_Dir(int argc, char **argv) printf("\n"); } // Close Directory - close(dp); + _SysClose(dp); }