X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Usermode%2FApplications%2FCLIShell_src%2Fmain.c;h=6de92df42d99804328e83ec3cf86e74025ad2b8c;hb=13078002b01ee4f63eb2001d2ef479a2a006ea32;hp=24889faf52f3eb7ce907088bab7ba2886dee61b3;hpb=808a29b42448b1229e07933050aa2b4cbe1fc923;p=tpg%2Facess2.git diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index 24889faf..6de92df4 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -8,6 +8,8 @@ #include #include "header.h" #include +#include +#include #define _stdin 0 #define _stdout 1 @@ -31,11 +33,11 @@ struct { {"help", Command_Help}, {"clear", Command_Clear}, {"cd", Command_Cd}, {"dir", Command_Dir} }; -static char *cDEFAULT_PATH[] = {"/Acess/Bin"}; +static char *cDEFAULT_PATH[] = {"/Acess/Bin","/Acess/SBin"}; #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0])) // ==== LOCAL VARIABLES ==== - int giNumPathDirs = 1; + int giNumPathDirs = 2; char **gasPathDirs = cDEFAULT_PATH; char **gasEnvironment; char gsCommandBuffer[1024]; @@ -55,7 +57,7 @@ int main(int argc, char *argv[], char **envp) gasEnvironment = envp; - Command_Clear(0, NULL); +// Command_Clear(0, NULL); { char *tmp = getenv("CWD"); @@ -75,10 +77,15 @@ int main(int argc, char *argv[], char **envp) if(saArgs[0]) free(saArgs[0]); printf("%s$ ", gsCurrentDirectory); + fflush(stdout); // Read Command line sCommandStr = Readline( readline_state ); printf("\n"); + if( !sCommandStr ) { + perror("Readline"); + return 1; + } // Parse Command Line into arguments iArgCount = Parse_Args(sCommandStr, saArgs); @@ -165,15 +172,15 @@ void CallCommand(char **Args) { GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer); // Check file existence - fd = open(sTmpBuffer, OPENFLAG_EXEC); + fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC); if(fd == -1) { printf("Unknown Command: `%s'\n", Args[0]); // Error Message return ; } // Get File info and close file - finfo( fd, &info, 0 ); - close( fd ); + _SysFInfo( fd, &info, 0 ); + _SysClose( fd ); // Check if the file is a directory if(info.flags & FILEFLAG_DIRECTORY) { @@ -189,10 +196,10 @@ void CallCommand(char **Args) for( i = 0; i < giNumPathDirs; i++ ) { GeneratePath(exefile, gasPathDirs[i], sTmpBuffer); - fd = open(sTmpBuffer, OPENFLAG_EXEC); + fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC); if(fd == -1) continue; - finfo( fd, &info, 0 ); - close( fd ); + _SysFInfo( fd, &info, 0 ); + _SysClose( fd ); if(info.flags & FILEFLAG_DIRECTORY) continue; // Woohoo! We found a valid command break; @@ -206,19 +213,28 @@ void CallCommand(char **Args) } // Create new process - pid = clone(CLONE_VM, 0); - // Start Task - if(pid == 0) { - execve(sTmpBuffer, Args, gasEnvironment); - printf("Execve returned, ... oops\n"); - exit(-1); - } + int fds[] = {0, 1, 2}; + int status; + pid = _SysSpawn(sTmpBuffer, (const char **)Args, (const char **)gasEnvironment, 3, fds, NULL); if(pid <= 0) { printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message + status = 0; } else { - int status; - waittid(pid, &status); + _SysIOCtl(0, PTY_IOCTL_SETPGRP, &pid); + _SysWaitTID(pid, &status); + } + + // Return terminal to a sane state + { + int zero = 0; + _SysIOCtl(0, PTY_IOCTL_SETPGRP, &zero); + printf("\x1b[0m"); + } + // Print a status message if termination was non-clean + if( status ) + { + printf("[%i] exited %i\n", pid, status); } } @@ -256,7 +272,7 @@ void Command_Help(int argc, char **argv) */ void Command_Clear(int argc, char **argv) { - write(_stdout, "\x1B[2J", 4); //Clear Screen + _SysWrite(_stdout, "\x1B[2J", 4); //Clear Screen } /** @@ -277,13 +293,13 @@ void Command_Cd(int argc, char **argv) GeneratePath(argv[1], gsCurrentDirectory, tmpPath); - fp = open(tmpPath, 0); + fp = _SysOpen(tmpPath, 0); if(fp == -1) { printf("Directory does not exist\n"); return; } - finfo(fp, &stats, 0); - close(fp); + _SysFInfo(fp, &stats, 0); + _SysClose(fp); if( !(stats.flags & FILEFLAG_DIRECTORY) ) { printf("Not a Directory\n"); @@ -295,7 +311,7 @@ void Command_Cd(int argc, char **argv) strcpy(gsCurrentDirectory, tmpPath); // Register change with kernel - chdir( gsCurrentDirectory ); + _SysChdir( gsCurrentDirectory ); } /** @@ -304,55 +320,43 @@ void Command_Cd(int argc, char **argv) */ void Command_Dir(int argc, char **argv) { - int dp, fp, dirLen; + int dp, fp; char modeStr[11] = "RWXrwxRWX "; - char tmpPath[1024]; - char *fileName; + char fileName[256]; t_sysFInfo info; t_sysACL acl; + + // -- Generate and open directory -- // Generate Directory Path + char tmpPath[1024]; if(argc > 1) - dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath); + GeneratePath(argv[1], gsCurrentDirectory, tmpPath); else - { strcpy(tmpPath, gsCurrentDirectory); - } - dirLen = strlen(tmpPath); - // Open Directory - dp = open(tmpPath, OPENFLAG_READ); - // Check if file opened - if(dp == -1) - { + dp = _SysOpen(tmpPath, OPENFLAG_READ); + if(dp == -1) { printf("Unable to open directory `%s', File cannot be found\n", tmpPath); return; } // Get File Stats - if( finfo(dp, &info, 0) == -1 ) + if( _SysFInfo(dp, &info, 0) == -1 ) { - close(dp); + _SysClose(dp); printf("stat Failed, Bad File Descriptor\n"); return; } // Check if it's a directory if(!(info.flags & FILEFLAG_DIRECTORY)) { - close(dp); + _SysClose(dp); printf("Unable to open directory `%s', Not a directory\n", tmpPath); return; } - // Append Shash for file paths - if(tmpPath[dirLen-1] != '/') - { - tmpPath[dirLen++] = '/'; - tmpPath[dirLen] = '\0'; - } - - fileName = (char*)(tmpPath+dirLen); - // Read Directory Content - while( (fp = SysReadDir(dp, fileName)) ) + // -- Read Directory Contents -- + while( (fp = _SysReadDir(dp, fileName)) ) { if(fp < 0) { @@ -361,10 +365,10 @@ void Command_Dir(int argc, char **argv) break; } // Open File - fp = open(tmpPath, 0); + fp = _SysOpenChild(dp, fileName, 0); if(fp == -1) continue; // Get File Stats - finfo(fp, &info, 0); + _SysFInfo(fp, &info, 0); if(info.flags & FILEFLAG_DIRECTORY) printf("d"); @@ -391,7 +395,7 @@ void Command_Dir(int argc, char **argv) if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-'; if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-'; printf(modeStr); - close(fp); + _SysClose(fp); // Colour Code if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green @@ -412,5 +416,5 @@ void Command_Dir(int argc, char **argv) printf("\n"); } // Close Directory - close(dp); + _SysClose(dp); }