X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=ec83ac963c13316c12682dfce6c223becc05d6e2;hb=ab47641a5334988563520c66f5097dd6d687ea48;hp=cb76be1198104dc5c46b745811c5ae01945ca888;hpb=72fcc8d0873e91f713be3d23af32a4e83f40cdf5;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index cb76be11..ec83ac96 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -1,9 +1,10 @@ /* - AcessOS Shell Version 2 -- Based on IOOS CLI Shell -*/ + * AcessOS Shell Version 3 + */ #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 = "/"; -char gsTmpBuffer[1024]; +char *gsCurrentDirectory = NULL; char **gasCommandHistory; int giLastCommand = 0; int giCommandSpace = 0; @@ -43,16 +50,26 @@ int main(int argc, char *argv[], char *envp[]) char *sCommandStr; char *saArgs[32]; 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); - write(_stdout, 1, "\n"); - write(_stdout, 36, "Acess Shell Version 3\n"); - write(_stdout, 30, " Based on CLI Shell for IOOS\n"); + { + char *tmp = getenv("CWD"); + if(tmp) { + gsCurrentDirectory = malloc(strlen(tmp)+1); + strcpy(gsCurrentDirectory, tmp); + } else { + gsCurrentDirectory = malloc(2); + strcpy(gsCurrentDirectory, "/"); + } + } + + write(_stdout, 22, "Acess Shell Version 3\n"); write(_stdout, 2, "\n"); for(;;) { @@ -66,6 +83,11 @@ int main(int argc, char *argv[], char *envp[]) // 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) { @@ -89,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 []\n"); Print("Valid Colours are "); for(fg=0;fg<8;fg++) { @@ -289,11 +396,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 @@ -325,18 +427,26 @@ void Command_Cd(int argc, char **argv) return; } + 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) { int dp, fp, dirLen; - //char modeStr[11] = "RWXrwxRWX "; + char modeStr[11] = "RWXrwxRWX "; char tmpPath[1024]; - char fileName[256]; + char *fileName; t_sysFInfo info; + t_sysACL acl; // Generate Directory Path if(argc > 1) @@ -353,9 +463,9 @@ void Command_Dir(int argc, char **argv) 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"); + write(_stdout, 27, "Unable to open directory `"); + write(_stdout, strlen(tmpPath)+1, tmpPath); + write(_stdout, 25, "', File cannot be found\n"); return; } // Get File Stats @@ -381,6 +491,8 @@ void Command_Dir(int argc, char **argv) tmpPath[dirLen++] = '/'; tmpPath[dirLen] = '\0'; } + + fileName = (char*)(tmpPath+dirLen); // Read Directory Content while( (fp = readdir(dp, fileName)) ) { @@ -390,43 +502,56 @@ void Command_Dir(int argc, char **argv) write(_stdout, 42, "Invalid Permissions to traverse directory\n"); break; } - // Create File Path - strcpy((char*)(tmpPath+dirLen), fileName); // Open File fp = open(tmpPath, 0); if(fp == -1) continue; // Get File Stats finfo(fp, &info, 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 & 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 & 2) modeStr[7] = 'w'; else modeStr[7] = '-'; + if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-'; + write(_stdout, 10, modeStr); close(fp); // Colour Code - write(_stdout, 6, "\x1B[37m"); //White - if(info.flags & FILEFLAG_DIRECTORY) //Directory Green + if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green write(_stdout, 6, "\x1B[32m"); + // Default: White - //Print Mode - #if 0 - if(stats.st_mode & 0400) modeStr[0] = 'R'; else modeStr[0] = '-'; - if(stats.st_mode & 0200) modeStr[1] = 'W'; else modeStr[1] = '-'; - if(stats.st_mode & 0100) modeStr[2] = 'X'; else modeStr[2] = '-'; - if(stats.st_mode & 0040) modeStr[3] = 'R'; else modeStr[3] = '-'; - if(stats.st_mode & 0020) modeStr[4] = 'W'; else modeStr[4] = '-'; - if(stats.st_mode & 0010) modeStr[5] = 'X'; else modeStr[5] = '-'; - if(stats.st_mode & 0004) modeStr[6] = 'R'; else modeStr[6] = '-'; - if(stats.st_mode & 0002) modeStr[7] = 'W'; else modeStr[7] = '-'; - if(stats.st_mode & 0001) modeStr[8] = 'X'; else modeStr[8] = '-'; - write(_stdout, 10, modeStr); - #endif - - //Print Name + // Print Name write(_stdout, strlen(fileName), fileName); - //Print slash if applicable + + // Print slash if applicable if(info.flags & FILEFLAG_DIRECTORY) write(_stdout, 1, "/"); - // Revert Colout and end line + // Revert Colour write(_stdout, 6, "\x1B[37m"); - write(_stdout, 2, "\n"); + + // Newline! + write(_stdout, 1, "\n"); } // Close Directory close(dp);