2 * AcessOS Shell Version 3
\r
4 #define USE_READLINE 1
\r
5 #include <acess/sys.h>
\r
12 # include "readline.h"
\r
19 // ==== PROTOTYPES ====
\r
20 char *ReadCommandLine(int *Length);
\r
21 void Parse_Args(char *str, char **dest);
\r
22 void CallCommand(char **Args);
\r
23 void Command_Logout(int argc, char **argv);
\r
24 void Command_Clear(int argc, char **argv);
\r
25 void Command_Help(int argc, char **argv);
\r
26 void Command_Cd(int argc, char **argv);
\r
27 void Command_Dir(int argc, char **argv);
\r
29 // ==== CONSTANT GLOBALS ====
\r
32 void (*fcn)(int argc, char **argv);
\r
34 {"exit", Command_Logout}, {"logout", Command_Logout},
\r
35 {"help", Command_Help}, {"clear", Command_Clear},
\r
36 {"cd", Command_Cd}, {"dir", Command_Dir}
\r
38 static char *cDEFAULT_PATH[] = {"/Acess/Bin"};
\r
39 #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0]))
\r
41 // ==== LOCAL VARIABLES ====
\r
42 int giNumPathDirs = 1;
\r
43 char **gasPathDirs = cDEFAULT_PATH;
\r
44 char **gasEnvironment;
\r
45 char gsCommandBuffer[1024];
\r
46 char *gsCurrentDirectory = NULL;
\r
47 char **gasCommandHistory;
\r
48 int giLastCommand = 0;
\r
49 int giCommandSpace = 0;
\r
52 int main(int argc, char *argv[], char *envp[])
\r
55 char *saArgs[32] = {0};
\r
62 tReadline *readline_state = Readline_Init(1);
\r
65 gasEnvironment = envp;
\r
67 Command_Clear(0, NULL);
\r
70 char *tmp = getenv("CWD");
\r
72 gsCurrentDirectory = malloc(strlen(tmp)+1);
\r
73 strcpy(gsCurrentDirectory, tmp);
\r
75 gsCurrentDirectory = malloc(2);
\r
76 strcpy(gsCurrentDirectory, "/");
\r
80 printf("Acess Shell Version 3\n\n");
\r
83 // Free last command & arguments
\r
84 if(saArgs[0]) free(saArgs);
\r
86 if(!bCached) free(sCommandStr);
\r
90 printf("%s$ ", gsCurrentDirectory);
\r
92 // Read Command line
\r
94 sCommandStr = Readline( readline_state );
\r
96 length = strlen(sCommandStr);
\r
98 sCommandStr = ReadCommandLine( &length );
\r
101 printf("PANIC: Out of heap space\n");
\r
105 // Check if the command should be cached
\r
106 if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0)
\r
108 if(giLastCommand >= giCommandSpace) {
\r
109 giCommandSpace += 12;
\r
110 gasCommandHistory = realloc(gasCommandHistory, giCommandSpace*sizeof(char*));
\r
113 gasCommandHistory[ giLastCommand ] = sCommandStr;
\r
118 // Parse Command Line into arguments
\r
119 Parse_Args(sCommandStr, saArgs);
\r
123 while(saArgs[iArgCount]) iArgCount++;
\r
125 // Silently Ignore all empty commands
\r
126 if(saArgs[1][0] == '\0') continue;
\r
128 // Check Built-In Commands
\r
129 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
131 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
133 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
137 if(i != BUILTIN_COUNT) continue;
\r
140 CallCommand( &saArgs[1] );
\r
143 free( sCommandStr );
\r
150 * \fn char *ReadCommandLine(int *Length)
\r
151 * \brief Read from the command line
\r
153 char *ReadCommandLine(int *Length)
\r
156 int len, pos, space = 1023;
\r
159 int scrollbackPos = giLastCommand;
\r
162 // Preset Variables
\r
163 ret = malloc( space+1 );
\r
164 if(!ret) return NULL;
\r
167 // Read In Command Line
\r
169 ch = getchar(); // Read Character from stdin (read is a blocking call)
\r
170 if(ch == '\n') break;
\r
174 // Control characters
\r
176 ch = getchar(); // Read control character
\r
179 //case 'D': if(pos) pos--; break;
\r
180 //case 'C': if(pos<len) pos++; break;
\r
182 ch = getchar(); // Read control character
\r
189 if( scrollbackPos > 0 ) break;
\r
192 ret = strdup( gasCommandHistory[--scrollbackPos] );
\r
195 while(pos--) printf("\x1B[D");
\r
196 write(_stdout, len, ret); pos = len;
\r
197 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
203 if( scrollbackPos < giLastCommand-1 ) break;
\r
206 ret = strdup( gasCommandHistory[++scrollbackPos] );
\r
209 while(pos--) write(_stdout, 3, "\x1B[D");
\r
210 write(_stdout, len, ret); pos = len;
\r
211 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
216 if(pos == 0) break;
\r
221 if(pos == len) break;
\r
231 if(len <= 0) break; // Protect against underflows
\r
233 if(pos == len) { // Simple case of end of string
\r
238 printf("%.*s ", len-pos, &ret[pos]); // Move Text
\r
239 printf("\x1B[%iD", len-pos+1);
\r
241 memmove(&ret[pos-1], &ret[pos], len-pos);
\r
249 //TODO: Implement Tab-Completion
\r
250 //Currently just ignore tabs
\r
255 if(len+1 > space) {
\r
257 ret = realloc(ret, space+1);
\r
258 if(!ret) return NULL;
\r
261 // Editing inside the buffer
\r
263 putchar(ch); // Print new character
\r
264 printf("%.*s", len-pos, &ret[pos]); // Move Text
\r
265 printf("\x1B[%iD", len-pos);
\r
266 memmove( &ret[pos+1], &ret[pos], len-pos );
\r
275 } while(ch != '\n');
\r
282 if(Length) *Length = len;
\r
289 * \fn void Parse_Args(char *str, char **dest)
\r
290 * \brief Parse a string into an argument array
\r
292 void Parse_Args(char *str, char **dest)
\r
295 char *buf = malloc( strlen(str) + 1 );
\r
299 printf("Parse_Args: Out of heap space!\n");
\r
306 // Trim leading whitespace
\r
307 while(*buf == ' ' && *buf) buf++;
\r
311 dest[i] = buf; // Save start of string
\r
313 while(*buf && *buf != ' ')
\r
317 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
321 if(*buf == '\0') break;
\r
323 while(*++buf == ' ' && *buf);
\r
324 if(*buf == '\0') break;
\r
334 * \fn void CallCommand(char **Args)
\r
336 void CallCommand(char **Args)
\r
341 char sTmpBuffer[1024];
\r
342 char *exefile = Args[0];
\r
344 if(exefile[0] == '/'
\r
345 || (exefile[0] == '.' && exefile[1] == '/')
\r
346 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
349 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
350 // Check file existence
\r
351 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
353 printf("Unknown Command: `%s'\n", Args[0]); // Error Message
\r
357 // Get File info and close file
\r
358 finfo( fd, &info, 0 );
\r
361 // Check if the file is a directory
\r
362 if(info.flags & FILEFLAG_DIRECTORY) {
\r
363 printf("`%s' is a directory.\n", sTmpBuffer);
\r
371 // Check all components of $PATH
\r
372 for( i = 0; i < giNumPathDirs; i++ )
\r
374 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
375 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
376 if(fd == -1) continue;
\r
377 finfo( fd, &info, 0 );
\r
379 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
380 // Woohoo! We found a valid command
\r
384 // Exhausted path directories
\r
385 if( i == giNumPathDirs ) {
\r
386 printf("Unknown Command: `%s'\n", exefile);
\r
391 // Create new process
\r
392 pid = clone(CLONE_VM, 0);
\r
395 execve(sTmpBuffer, Args, gasEnvironment);
\r
397 printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message
\r
401 waittid(pid, &status);
\r
406 * \fn void Command_Logout(int argc, char **argv)
\r
407 * \brief Exit the shell, logging the user out
\r
409 void Command_Logout(int argc, char **argv)
\r
415 * \fn void Command_Colour(int argc, char **argv)
\r
416 * \brief Displays the help screen
\r
418 void Command_Help(int argc, char **argv)
\r
420 printf( "Acess 2 Command Line Interface\n"
\r
421 " By John Hodge (thePowersGang / [TPG])\n"
\r
423 "Builtin Commands:\n"
\r
424 " logout: Return to the login prompt\n"
\r
426 " help: Display this message\n"
\r
427 " clear: Clear the screen\n"
\r
428 " cd: Change the current directory\n"
\r
429 " dir: Print the contents of the current directory\n");
\r
434 * \fn void Command_Clear(int argc, char **argv)
\r
435 * \brief Clear the screen
\r
437 void Command_Clear(int argc, char **argv)
\r
439 write(_stdout, "\x1B[2J", 4); //Clear Screen
\r
443 * \fn void Command_Cd(int argc, char **argv)
\r
444 * \brief Change directory
\r
446 void Command_Cd(int argc, char **argv)
\r
448 char tmpPath[1024];
\r
454 printf("%s\n", gsCurrentDirectory);
\r
458 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
460 fp = open(tmpPath, 0);
\r
462 printf("Directory does not exist\n");
\r
465 finfo(fp, &stats, 0);
\r
468 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
469 printf("Not a Directory\n");
\r
473 free(gsCurrentDirectory);
\r
474 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
475 strcpy(gsCurrentDirectory, tmpPath);
\r
477 // Register change with kernel
\r
478 chdir( gsCurrentDirectory );
\r
482 * \fn void Command_Dir(int argc, char **argv)
\r
483 * \brief Print the contents of a directory
\r
485 void Command_Dir(int argc, char **argv)
\r
487 int dp, fp, dirLen;
\r
488 char modeStr[11] = "RWXrwxRWX ";
\r
489 char tmpPath[1024];
\r
494 // Generate Directory Path
\r
496 dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
499 strcpy(tmpPath, gsCurrentDirectory);
\r
501 dirLen = strlen(tmpPath);
\r
504 dp = open(tmpPath, OPENFLAG_READ);
\r
505 // Check if file opened
\r
508 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
512 if( finfo(dp, &info, 0) == -1 )
\r
515 printf("stat Failed, Bad File Descriptor\n");
\r
518 // Check if it's a directory
\r
519 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
522 printf("Unable to open directory `%s', Not a directory\n", tmpPath);
\r
526 // Append Shash for file paths
\r
527 if(tmpPath[dirLen-1] != '/')
\r
529 tmpPath[dirLen++] = '/';
\r
530 tmpPath[dirLen] = '\0';
\r
533 fileName = (char*)(tmpPath+dirLen);
\r
534 // Read Directory Content
\r
535 while( (fp = readdir(dp, fileName)) )
\r
540 printf("Invalid Permissions to traverse directory\n");
\r
544 fp = open(tmpPath, 0);
\r
545 if(fp == -1) continue;
\r
547 finfo(fp, &info, 0);
\r
549 if(info.flags & FILEFLAG_DIRECTORY)
\r
556 acl.object = info.uid;
\r
557 _SysGetACL(fp, &acl);
\r
558 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
559 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
560 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
562 acl.object = info.gid | 0x80000000;
\r
563 _SysGetACL(fp, &acl);
\r
564 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
565 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
566 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
568 acl.object = 0xFFFFFFFF;
\r
569 _SysGetACL(fp, &acl);
\r
570 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
571 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
572 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
577 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
578 printf("\x1B[32m");
\r
582 printf("%s", fileName);
\r
584 // Print slash if applicable
\r
585 if(info.flags & FILEFLAG_DIRECTORY)
\r
589 printf("\x1B[37m");
\r