X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=58432daf2cd985655086792a2ba09174d6ff0b7f;hb=0b1cdd6b52e410ad8784eaf37c24141e947d1091;hp=d6c40d65a4db47e3ffb24e7c8a680bf73839d4ac;hpb=466eda7c917791866a29c253c6c22197faf41bf7;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index d6c40d65..58432daf 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -1,12 +1,17 @@ /* * AcessOS Shell Version 3 */ +#define USE_READLINE 1 #include #include #include #include #include "header.h" +#if USE_READLINE +# include "readline.h" +#endif + #define _stdin 0 #define _stdout 1 #define _stderr 2 @@ -51,7 +56,11 @@ int main(int argc, char *argv[], char *envp[]) int length = 0; int i; int iArgCount = 0; + #if !USE_READLINE int bCached = 1; + #else + tReadline *readline_state = Readline_Init(1); + #endif gasEnvironment = envp; @@ -68,23 +77,28 @@ 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 !USE_READLINE if(!bCached) free(sCommandStr); bCached = 0; + #endif - write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); - write(_stdout, 2, "$ "); + printf("%s$ ", gsCurrentDirectory); // Read Command line + #if USE_READLINE + sCommandStr = Readline( readline_state ); + printf("\n"); + length = strlen(sCommandStr); + #else sCommandStr = ReadCommandLine( &length ); if(!sCommandStr) { - write(_stdout, 25, "PANIC: Out of heap space\n"); + printf("PANIC: Out of heap space\n"); return -1; } @@ -99,6 +113,7 @@ int main(int argc, char *argv[], char *envp[]) gasCommandHistory[ giLastCommand ] = sCommandStr; bCached = 1; } + #endif // Parse Command Line into arguments Parse_Args(sCommandStr, saArgs); @@ -123,9 +138,14 @@ 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 @@ -135,7 +155,9 @@ char *ReadCommandLine(int *Length) char *ret; int len, pos, space = 1023; char ch; - // int scrollbackPos = giLastCommand; + #if 0 + int scrollbackPos = giLastCommand; + #endif // Preset Variables ret = malloc( space+1 ); @@ -144,94 +166,124 @@ char *ReadCommandLine(int *Length) // Read In Command Line do { - read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call) + ch = getchar(); // Read Character from stdin (read is a blocking call) + if(ch == '\n') break; + + switch(ch) + { // Control characters - if(ch == '\x1B') { - read(_stdin, 1, &ch); // Read control character + 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--) write(_stdout, 3, "\x1B[D"); - while(pos++ < len) write(_stdout, 3, "\x1B[C"); + { + int oldLen = len; + if( scrollbackPos > 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 - if( scrollbackPos < giLastCommand-1 ) break; - free(ret); - ret = strdup( gasCommandHistory[++scrollbackPos] ); - - len = strlen(ret); - while(pos--) write(_stdout, 3, "\x1B[D"); - while(pos++ < len) write(_stdout, 3, "\x1B[C"); + { + 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 --; - write(_stdout, 3, "\x1B[D"); + printf("\x1B[D"); break; case 'C': // Right - if(pos == len-1) break; + if(pos == len) break; pos++; - write(_stdout, 3, "\x1B[C"); + printf("\x1B[C"); break; } } - continue; - } + break; + // Backspace - if(ch == '\b') { - if(len <= 0) continue; // Protect against underflows + case '\b': + if(len <= 0) break; // Protect against underflows + putchar(ch); if(pos == len) { // Simple case of end of string - len --; pos--; - } else { + 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 --; } - write(_stdout, 1, &ch); - continue; - } + break; + // Tab - if(ch == '\t') { + case '\t': //TODO: Implement Tab-Completion //Currently just ignore tabs - continue; - } + break; - // Expand Buffer - if(len > space) { - space += 256; - ret = realloc(ret, space+1); - if(!ret) return NULL; + 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; } - - write(_stdout, 1, &ch); - ret[pos++] = ch; - len ++; } while(ch != '\n'); - // Remove newline - pos --; - ret[pos] = '\0'; + // Cap String + ret[len] = '\0'; + printf("\n"); // Return length if(Length) *Length = len; return ret; } +#endif /** * \fn void Parse_Args(char *str, char **dest) @@ -244,7 +296,7 @@ void Parse_Args(char *str, char **dest) if(buf == NULL) { dest[0] = NULL; - Print("Parse_Args: Out of heap space!\n"); + printf("Parse_Args: Out of heap space!\n"); return ; } @@ -298,7 +350,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 ; } @@ -308,8 +360,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 ; } } @@ -332,7 +383,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 ; } } @@ -343,7 +394,7 @@ void CallCommand(char **Args) if(pid == 0) execve(sTmpBuffer, Args, gasEnvironment); 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; @@ -366,17 +417,16 @@ void Command_Logout(int argc, char **argv) */ void Command_Help(int argc, char **argv) { - Print("Acess 2 Command Line Interface\n"); - Print(" By John Hodge (thePowersGang / [TPG])\n"); - Print("\n"); - Print("Builtin Commands:\n"); - Print(" logout: Return to the login prompt\n"); - Print(" exit: Same\n"); - Print(" help: Display this message\n"); - Print(" clear: Clear the screen\n"); - Print(" cd: Change the current directory\n"); - Print(" dir: Print the contents of the current directory\n"); - //Print("\n"); + 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; } @@ -386,7 +436,7 @@ void Command_Help(int argc, char **argv) */ void Command_Clear(int argc, char **argv) { - write(_stdout, 4, "\x1B[2J"); //Clear Screen + write(_stdout, "\x1B[2J", 4); //Clear Screen } /** @@ -401,7 +451,7 @@ void Command_Cd(int argc, char **argv) if(argc < 2) { - Print(gsCurrentDirectory);Print("\n"); + printf("%s\n", gsCurrentDirectory); return; } @@ -409,14 +459,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,16 +512,14 @@ void Command_Dir(int argc, char **argv) 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; } @@ -489,7 +537,7 @@ void Command_Dir(int argc, char **argv) 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 @@ -499,49 +547,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);