X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=1f381e26120fab2fb7f8e046d5af9fa4d9e2f91c;hb=0dbc91f0676576b954aa95dafdb434ce19bc805c;hp=dbe18c596840814152f22f63cb3c34fd3f315ed3;hpb=c13663104076a7780d1bbbaf77273416733cbb58;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index dbe18c59..1f381e26 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "header.h" #define _stdin 0 @@ -14,18 +15,19 @@ char *ReadCommandLine(int *Length); void Parse_Args(char *str, char **dest); void CallCommand(char **Args); -void Command_Colour(int argc, char **argv); +void Command_Logout(int argc, char **argv); void Command_Clear(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[] = { - {"colour", Command_Colour}, {"clear", Command_Clear}, + {"exit", Command_Logout}, {"logout", Command_Logout}, + {"help", Command_Help}, {"clear", Command_Clear}, {"cd", Command_Cd}, {"dir", Command_Dir} }; static char *cDEFAULT_PATH[] = {"/Acess/Bin"}; @@ -45,7 +47,7 @@ char **gasCommandHistory; int main(int argc, char *argv[], char *envp[]) { char *sCommandStr; - char *saArgs[32]; + char *saArgs[32] = {0}; int length = 0; int i; int iArgCount = 0; @@ -74,8 +76,9 @@ int main(int argc, char *argv[], char *envp[]) if(saArgs[0]) free(saArgs); if(!bCached) free(sCommandStr); bCached = 0; + write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); - write(_stdout, 3, "$ "); + write(_stdout, 2, "$ "); // Read Command line sCommandStr = ReadCommandLine( &length ); @@ -132,6 +135,9 @@ 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 ); @@ -141,51 +147,126 @@ char *ReadCommandLine(int *Length) // Read In Command Line do { read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call) - // Ignore control characters - if(ch == '\x1B') { + + if(ch == '\n') break; + + switch(ch) + { + // Control characters + case '\x1B': read(_stdin, 1, &ch); // 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"); + 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 --; + write(_stdout, 3, "\x1B[D"); + break; + case 'C': // Right + if(pos == len) break; + pos++; + write(_stdout, 3, "\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 + write(_stdout, 1, &ch); if(pos == len) { // Simple case of end of string - len --; pos--; - } else { + len --; + pos--; + } + else { + char buf[7] = "\x1B[000D"; + buf[2] += ((len-pos+1)/100) % 10; + buf[3] += ((len-pos+1)/10) % 10; + buf[4] += (len-pos+1) % 10; + write(_stdout, len-pos, &ret[pos]); // Move Text + ch = ' '; write(_stdout, 1, &ch); ch = '\b'; // Clear deleted character + write(_stdout, 7, buf); // Update Cursor + // Alter Buffer memmove(&ret[pos-1], &ret[pos], len-pos); pos --; len --; } - write(_stdout, 1, &ch); - continue; - } - // Expand Buffer - if(len > space) { - space += 256; - ret = realloc(ret, space+1); - if(!ret) return NULL; - } + break; + + // Tab + case '\t': + //TODO: Implement Tab-Completion + //Currently just ignore tabs + break; - write(_stdout, 1, &ch); - ret[pos++] = ch; - len ++; + 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) { + char buf[7] = "\x1B[000D"; + buf[2] += ((len-pos)/100) % 10; + buf[3] += ((len-pos)/10) % 10; + buf[4] += (len-pos) % 10; + write(_stdout, 1, &ch); // Print new character + write(_stdout, len-pos, &ret[pos]); // Move Text + write(_stdout, 7, buf); // Update Cursor + memmove( &ret[pos+1], &ret[pos], len-pos ); + } + else { + write(_stdout, 1, &ch); + } + ret[pos++] = ch; + len ++; + break; + } } while(ch != '\n'); - // Remove newline - pos --; - ret[pos] = '\0'; + // Cap String + ret[len] = '\0'; + printf("\n"); // Return length if(Length) *Length = len; @@ -312,65 +393,31 @@ void CallCommand(char **Args) } /** - * \fn void Command_Colour(int argc, char **argv) - * \brief + * \fn void Command_Logout(int argc, char **argv) + * \brief Exit the shell, logging the user out */ -void Command_Colour(int argc, char **argv) +void Command_Logout(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; + exit(0); +} - // 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"); +/** + * \fn void Command_Colour(int argc, char **argv) + * \brief Displays the help screen + */ +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"); return; } @@ -423,6 +470,8 @@ void Command_Cd(int argc, char **argv) } /** + * \fn void Command_Dir(int argc, char **argv) + * \brief Print the contents of a directory */ void Command_Dir(int argc, char **argv) { @@ -447,10 +496,7 @@ 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 @@ -499,21 +545,24 @@ void Command_Dir(int argc, char **argv) write(_stdout, 1, "-"); // Print Mode + // - Owner acl.group = 0; acl.id = 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; _SysGetACL(fp, &acl); if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-'; - if(acl.perms & 1) modeStr[4] = 'w'; else modeStr[4] = '-'; - if(acl.perms & 1) modeStr[5] = 'x'; else modeStr[5] = '-'; + 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; _SysGetACL(fp, &acl); if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-'; - if(acl.perms & 1) modeStr[7] = 'w'; else modeStr[7] = '-'; - if(acl.perms & 1) modeStr[8] = 'x'; else modeStr[8] = '-'; + if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-'; + if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-'; write(_stdout, 10, modeStr); close(fp);