X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=2e51419f599dd2069937710ce0c1eff91d02f71e;hb=a2210987109ab5a6337c72b45f7e52cfc9092f8f;hp=e806a53746edd71dae3974467f5bc9ba45607dca;hpb=6098f7e3a0a3247c9517f9b04be814d16a98a564;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index e806a537..2e51419f 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 @@ -13,8 +14,10 @@ // ==== PROTOTYPES ==== char *ReadCommandLine(int *Length); void Parse_Args(char *str, char **dest); -void Command_Colour(int argc, char **argv); +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_Cd(int argc, char **argv); void Command_Dir(int argc, char **argv); @@ -24,15 +27,19 @@ struct { char *name; void (*fcn)(int argc, char **argv); } cBUILTINS[] = { + {"exit", Command_Logout}, {"logout", Command_Logout}, {"colour", Command_Colour}, {"clear", Command_Clear}, {"cd", Command_Cd}, {"dir", Command_Dir} }; +static char *cDEFAULT_PATH[] = {"/Acess/Bin"}; #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0])) // ==== LOCAL VARIABLES ==== + int giNumPathDirs = 1; +char **gasPathDirs = cDEFAULT_PATH; +char **gasEnvironment; char gsCommandBuffer[1024]; char *gsCurrentDirectory = NULL; -char gsTmpBuffer[1024]; char **gasCommandHistory; int giLastCommand = 0; int giCommandSpace = 0; @@ -41,14 +48,15 @@ char **gasCommandHistory; int main(int argc, char *argv[], char *envp[]) { char *sCommandStr; - char *saArgs[32]; + char *saArgs[32] = {0}; int length = 0; - int pid = -1; + int i; int iArgCount = 0; int bCached = 1; - t_sysFInfo info; - //Command_Clear(0, NULL); + gasEnvironment = envp; + + Command_Clear(0, NULL); { char *tmp = getenv("CWD"); @@ -61,7 +69,6 @@ int main(int argc, char *argv[], char *envp[]) } } - write(_stdout, 1, "\n"); write(_stdout, 22, "Acess Shell Version 3\n"); write(_stdout, 2, "\n"); for(;;) @@ -70,13 +77,17 @@ int main(int argc, char *argv[], char *envp[]) if(saArgs[0]) free(saArgs); if(!bCached) free(sCommandStr); bCached = 0; - write(_stdout, 1, "\n"); write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); write(_stdout, 3, "$ "); // Read Command line sCommandStr = ReadCommandLine( &length ); + if(!sCommandStr) { + write(_stdout, 25, "PANIC: Out of heap space\n"); + return -1; + } + // Check if the command should be cached if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0) { @@ -100,46 +111,18 @@ int main(int argc, char *argv[], char *envp[]) if(saArgs[1][0] == '\0') continue; // Check Built-In Commands - // [HACK] Mem Usage - Use Length in place of `i' - for(length=0;length space) { space += 256; @@ -207,6 +212,12 @@ void Parse_Args(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 ; + } + strcpy(buf, str); dest[0] = buf; @@ -237,9 +248,101 @@ void Parse_Args(char *str, char **dest) } } +/** + * \fn void CallCommand(char **Args) + */ +void CallCommand(char **Args) +{ + t_sysFInfo info; + int pid = -1; + int fd = 0; + char sTmpBuffer[1024]; + char *exefile = Args[0]; + + if(exefile[0] == '/' + || (exefile[0] == '.' && exefile[1] == '/') + || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/') + ) + { + GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer); + // Check file existence + fd = open(sTmpBuffer, OPENFLAG_EXEC); + if(fd == -1) { + Print("Unknown Command: `");Print(Args[0]);Print("'\n"); // Error Message + return ; + } + + // Get File info and close file + finfo( fd, &info, 0 ); + close( fd ); + + // Check if the file is a directory + if(info.flags & FILEFLAG_DIRECTORY) { + Print("`");Print(sTmpBuffer); // Error Message + Print("' is a directory.\n"); + return ; + } + } + else + { + int i; + + // Check all components of $PATH + for( i = 0; i < giNumPathDirs; i++ ) + { + GeneratePath(exefile, gasPathDirs[i], sTmpBuffer); + fd = open(sTmpBuffer, OPENFLAG_EXEC); + if(fd == -1) continue; + finfo( fd, &info, 0 ); + close( fd ); + if(info.flags & FILEFLAG_DIRECTORY) continue; + // Woohoo! We found a valid command + break; + } + + // Exhausted path directories + if( i == giNumPathDirs ) { + Print("Unknown Command: `");Print(exefile);Print("'\n"); + return ; + } + } + + // Create new process + pid = clone(CLONE_VM, 0); + // Start Task + if(pid == 0) + execve(sTmpBuffer, Args, gasEnvironment); + if(pid <= 0) { + Print("Unablt to create process: `");Print(sTmpBuffer);Print("'\n"); // Error Message + } + else { + int status; + waittid(pid, &status); + } +} + +/** + * \fn void Command_Logout(int argc, char **argv) + * \brief Exit the shell, logging the user out + */ +void Command_Logout(int argc, char **argv) +{ + exit(0); +} + +/** + * \fn void Command_Clear(int argc, char **argv) + * \brief Clear the screen + */ +void Command_Clear(int argc, char **argv) +{ + write(_stdout, 4, "\x1B[2J"); //Clear Screen +} + /** * \fn void Command_Colour(int argc, char **argv) - * \brief + * \brief Set the colour of the shell prompt + * \note Conflicts with coloured `dir` display */ void Command_Colour(int argc, char **argv) { @@ -249,7 +352,6 @@ void Command_Colour(int argc, char **argv) // Verify Arg Count if(argc < 2) { - Print("Please specify a colour\n"); goto usage; } @@ -290,6 +392,7 @@ void Command_Colour(int argc, char **argv) // 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++) { @@ -300,11 +403,6 @@ usage: return; } -void Command_Clear(int argc, char **argv) -{ - write(_stdout, 4, "\x1B[2J"); //Clear Screen -} - /** * \fn void Command_Cd(int argc, char **argv) * \brief Change directory @@ -339,9 +437,14 @@ void Command_Cd(int argc, char **argv) free(gsCurrentDirectory); gsCurrentDirectory = malloc(strlen(tmpPath)+1); strcpy(gsCurrentDirectory, tmpPath); + + // Register change with kernel + chdir( gsCurrentDirectory ); } /** + * \fn void Command_Dir(int argc, char **argv) + * \brief Print the contents of a directory */ void Command_Dir(int argc, char **argv) { @@ -366,10 +469,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 @@ -412,34 +512,41 @@ void Command_Dir(int argc, char **argv) // Get File Stats finfo(fp, &info, 0); - //Print Mode - //#if 0 + if(info.flags & FILEFLAG_DIRECTORY) + write(_stdout, 1, "d"); + else + 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); - //#endif close(fp); // Colour Code if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green write(_stdout, 6, "\x1B[32m"); - else - write(_stdout, 6, "\x1B[37m"); // Default: White + // Default: White + // Print Name write(_stdout, strlen(fileName), fileName); + // Print slash if applicable if(info.flags & FILEFLAG_DIRECTORY) write(_stdout, 1, "/"); @@ -447,10 +554,8 @@ void Command_Dir(int argc, char **argv) // Revert Colour write(_stdout, 6, "\x1B[37m"); - // Put Size - printf("\n", info.size); - - //write(_stdout, 1, "\n"); + // Newline! + write(_stdout, 1, "\n"); } // Close Directory close(dp);