X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=24889faf52f3eb7ce907088bab7ba2886dee61b3;hb=808a29b42448b1229e07933050aa2b4cbe1fc923;hp=ec83ac963c13316c12682dfce6c223becc05d6e2;hpb=ab47641a5334988563520c66f5097dd6d687ea48;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index ec83ac96..24889faf 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -1,34 +1,34 @@ /* * AcessOS Shell Version 3 */ +#define USE_READLINE 1 #include #include #include #include #include "header.h" +#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); -void Command_Colour(int argc, char **argv); +void Command_Help(int argc, char **argv); void Command_Cd(int argc, char **argv); void Command_Dir(int argc, char **argv); // ==== CONSTANT GLOBALS ==== -char *cCOLOUR_NAMES[8] = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"}; struct { char *name; void (*fcn)(int argc, char **argv); } cBUILTINS[] = { {"exit", Command_Logout}, {"logout", Command_Logout}, - {"colour", Command_Colour}, {"clear", Command_Clear}, + {"help", Command_Help}, {"clear", Command_Clear}, {"cd", Command_Cd}, {"dir", Command_Dir} }; static char *cDEFAULT_PATH[] = {"/Acess/Bin"}; @@ -45,14 +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]; - int length = 0; + char *saArgs[32] = {0}; int i; int iArgCount = 0; - int bCached = 1; + tReadline *readline_state = Readline_Init(1); gasEnvironment = envp; @@ -69,43 +68,20 @@ int main(int argc, char *argv[], char *envp[]) } } - write(_stdout, 22, "Acess Shell Version 3\n"); - write(_stdout, 2, "\n"); + printf("Acess Shell Version 3\n\n"); for(;;) { // Free last command & arguments - if(saArgs[0]) free(saArgs); - if(!bCached) free(sCommandStr); - bCached = 0; - write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); - write(_stdout, 3, "$ "); + if(saArgs[0]) free(saArgs[0]); - // Read Command line - sCommandStr = ReadCommandLine( &length ); - - if(!sCommandStr) { - write(_stdout, 25, "PANIC: Out of heap space\n"); - return -1; - } + printf("%s$ ", gsCurrentDirectory); - // 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; - } + // Read Command line + sCommandStr = Readline( readline_state ); + printf("\n"); // 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; @@ -123,92 +99,24 @@ int main(int argc, char *argv[], char *envp[]) // Shall we? CallCommand( &saArgs[1] ); - } -} - -/** - * \fn char *ReadCommandLine(int *Length) - * \brief Read from the command line - */ -char *ReadCommandLine(int *Length) -{ - char *ret; - int len, pos, space = 1023; - char ch; - - // Preset Variables - ret = malloc( space+1 ); - if(!ret) return NULL; - len = 0; pos = 0; - - // Read In Command Line - do { - read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call) - // Ignore control characters - if(ch == '\x1B') { - read(_stdin, 1, &ch); // Read control character - switch(ch) - { - case 'D': if(pos) pos--; break; - case 'C': if(pos space) { - space += 256; - ret = realloc(ret, space+1); - if(!ret) return NULL; - } - write(_stdout, 1, &ch); - ret[pos++] = ch; - len ++; - } while(ch != '\n'); - - // Remove newline - pos --; - ret[pos] = '\0'; - - // Return length - if(Length) *Length = len; - - return ret; + free( sCommandStr ); + } } /** - * \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 ); if(buf == NULL) { dest[0] = NULL; - Print("Parse_Args: Out of heap space!\n"); - return ; + printf("Parse_Args: Out of heap space!\n"); + return 0; } strcpy(buf, str); @@ -235,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; } /** @@ -261,7 +167,7 @@ void CallCommand(char **Args) // Check file existence fd = open(sTmpBuffer, OPENFLAG_EXEC); if(fd == -1) { - Print("Unknown Command: `");Print(Args[0]);Print("'\n"); // Error Message + printf("Unknown Command: `%s'\n", Args[0]); // Error Message return ; } @@ -271,8 +177,7 @@ void CallCommand(char **Args) // Check if the file is a directory if(info.flags & FILEFLAG_DIRECTORY) { - Print("`");Print(sTmpBuffer); // Error Message - Print("' is a directory.\n"); + printf("`%s' is a directory.\n", sTmpBuffer); return ; } } @@ -295,7 +200,7 @@ void CallCommand(char **Args) // Exhausted path directories if( i == giNumPathDirs ) { - Print("Unknown Command: `");Print(exefile);Print("'\n"); + printf("Unknown Command: `%s'\n", exefile); return ; } } @@ -303,10 +208,13 @@ void CallCommand(char **Args) // Create new process pid = clone(CLONE_VM, 0); // Start Task - if(pid == 0) + if(pid == 0) { execve(sTmpBuffer, Args, gasEnvironment); + printf("Execve returned, ... oops\n"); + exit(-1); + } if(pid <= 0) { - Print("Unablt to create process: `");Print(sTmpBuffer);Print("'\n"); // Error Message + printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message } else { int status; @@ -324,76 +232,31 @@ void Command_Logout(int argc, char **argv) } /** - * \fn void Command_Clear(int argc, char **argv) - * \brief Clear the screen + * \fn void Command_Colour(int argc, char **argv) + * \brief Displays the help screen */ -void Command_Clear(int argc, char **argv) +void Command_Help(int argc, char **argv) { - write(_stdout, 4, "\x1B[2J"); //Clear Screen + printf( "Acess 2 Command Line Interface\n" + " By John Hodge (thePowersGang / [TPG])\n" + "\n" + "Builtin Commands:\n" + " logout: Return to the login prompt\n" + " exit: Same\n" + " help: Display this message\n" + " clear: Clear the screen\n" + " cd: Change the current directory\n" + " dir: Print the contents of the current directory\n"); + return; } /** - * \fn void Command_Colour(int argc, char **argv) - * \brief Set the colour of the shell prompt - * \note Conflicts with coloured `dir` display + * \fn void Command_Clear(int argc, char **argv) + * \brief Clear the screen */ -void Command_Colour(int argc, char **argv) +void Command_Clear(int argc, char **argv) { - int fg, bg; - char clrStr[6] = "\x1B[37m"; - - // Verify Arg Count - if(argc < 2) - { - goto usage; - } - - // Check Colour - for(fg=0;fg<8;fg++) - if(strcmp(cCOLOUR_NAMES[fg], argv[1]) == 0) - break; - - // Foreground a valid colour - if(fg == 8) { - Print("Unknown Colour '");Print(argv[1]);Print("'\n"); - goto usage; - } - // Set Foreground - clrStr[3] = '0' + fg; - write(_stdout, 6, clrStr); - - // Need to Set Background? - if(argc > 2) - { - for(bg=0;bg<8;bg++) - if(strcmp(cCOLOUR_NAMES[bg], argv[2]) == 0) - break; - - // Valid colour - if(bg == 8) - { - Print("Unknown Colour '");Print(argv[2]);Print("'\n"); - goto usage; - } - - clrStr[2] = '4'; - clrStr[3] = '0' + bg; - write(_stdout, 6, clrStr); - } - // Return - return; - - // Function Usage (Requested via a Goto (I know it's ugly)) -usage: - Print("Usage: colour []\n"); - Print("Valid Colours are "); - for(fg=0;fg<8;fg++) - { - Print(cCOLOUR_NAMES[fg]); - write(_stdout, 3, ", "); - } - write(_stdout, 4, "\b\b\n"); - return; + write(_stdout, "\x1B[2J", 4); //Clear Screen } /** @@ -408,7 +271,7 @@ void Command_Cd(int argc, char **argv) if(argc < 2) { - Print(gsCurrentDirectory);Print("\n"); + printf("%s\n", gsCurrentDirectory); return; } @@ -416,14 +279,14 @@ void Command_Cd(int argc, char **argv) fp = open(tmpPath, 0); if(fp == -1) { - write(_stdout, 26, "Directory does not exist\n"); + printf("Directory does not exist\n"); return; } finfo(fp, &stats, 0); close(fp); if( !(stats.flags & FILEFLAG_DIRECTORY) ) { - write(_stdout, 17, "Not a Directory\n"); + printf("Not a Directory\n"); return; } @@ -462,26 +325,21 @@ void Command_Dir(int argc, char **argv) // Check if file opened if(dp == -1) { - //printf("Unable to open directory `%s', File cannot be found\n", tmpPath); - write(_stdout, 27, "Unable to open directory `"); - write(_stdout, strlen(tmpPath)+1, tmpPath); - write(_stdout, 25, "', File cannot be found\n"); + printf("Unable to open directory `%s', File cannot be found\n", tmpPath); return; } // Get File Stats if( finfo(dp, &info, 0) == -1 ) { close(dp); - write(_stdout, 34, "stat Failed, Bad File Descriptor\n"); + printf("stat Failed, Bad File Descriptor\n"); return; } // Check if it's a directory if(!(info.flags & FILEFLAG_DIRECTORY)) { close(dp); - write(_stdout, 27, "Unable to open directory `"); - write(_stdout, strlen(tmpPath)+1, tmpPath); - write(_stdout, 20, "', Not a directory\n"); + printf("Unable to open directory `%s', Not a directory\n", tmpPath); return; } @@ -494,12 +352,12 @@ 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) { if(fp == -3) - write(_stdout, 42, "Invalid Permissions to traverse directory\n"); + printf("Invalid Permissions to traverse directory\n"); break; } // Open File @@ -509,49 +367,49 @@ void Command_Dir(int argc, char **argv) finfo(fp, &info, 0); if(info.flags & FILEFLAG_DIRECTORY) - write(_stdout, 1, "d"); + printf("d"); else - write(_stdout, 1, "-"); + printf("-"); // 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] = '-'; - write(_stdout, 10, modeStr); + printf(modeStr); close(fp); // Colour Code if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green - write(_stdout, 6, "\x1B[32m"); + printf("\x1B[32m"); // Default: White // Print Name - write(_stdout, strlen(fileName), fileName); + printf("%s", fileName); // Print slash if applicable if(info.flags & FILEFLAG_DIRECTORY) - write(_stdout, 1, "/"); + printf("/"); // Revert Colour - write(_stdout, 6, "\x1B[37m"); + printf("\x1B[37m"); // Newline! - write(_stdout, 1, "\n"); + printf("\n"); } // Close Directory close(dp);