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 write(_stdout, 22, "Acess Shell Version 3\n");
\r
81 write(_stdout, 2, "\n");
\r
84 // Free last command & arguments
\r
85 if(saArgs[0]) free(saArgs);
\r
87 if(!bCached) free(sCommandStr);
\r
91 write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory);
\r
92 write(_stdout, 2, "$ ");
\r
94 // Read Command line
\r
96 sCommandStr = Readline( readline_state );
\r
97 length = strlen(sCommandStr);
\r
99 sCommandStr = ReadCommandLine( &length );
\r
102 write(_stdout, 25, "PANIC: Out of heap space\n");
\r
106 // Check if the command should be cached
\r
107 if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0)
\r
109 if(giLastCommand >= giCommandSpace) {
\r
110 giCommandSpace += 12;
\r
111 gasCommandHistory = realloc(gasCommandHistory, giCommandSpace*sizeof(char*));
\r
114 gasCommandHistory[ giLastCommand ] = sCommandStr;
\r
119 // Parse Command Line into arguments
\r
120 Parse_Args(sCommandStr, saArgs);
\r
124 while(saArgs[iArgCount]) iArgCount++;
\r
126 // Silently Ignore all empty commands
\r
127 if(saArgs[1][0] == '\0') continue;
\r
129 // Check Built-In Commands
\r
130 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
132 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
134 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
138 if(i != BUILTIN_COUNT) continue;
\r
141 CallCommand( &saArgs[1] );
\r
144 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 read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call)
\r
171 if(ch == '\n') break;
\r
175 // Control characters
\r
177 read(_stdin, 1, &ch); // Read control character
\r
180 //case 'D': if(pos) pos--; break;
\r
181 //case 'C': if(pos<len) pos++; break;
\r
183 read(_stdin, 1, &ch); // Read control character
\r
190 if( scrollbackPos > 0 ) break;
\r
193 ret = strdup( gasCommandHistory[--scrollbackPos] );
\r
196 while(pos--) write(_stdout, 3, "\x1B[D");
\r
197 write(_stdout, len, ret); pos = len;
\r
198 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
204 if( scrollbackPos < giLastCommand-1 ) break;
\r
207 ret = strdup( gasCommandHistory[++scrollbackPos] );
\r
210 while(pos--) write(_stdout, 3, "\x1B[D");
\r
211 write(_stdout, len, ret); pos = len;
\r
212 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
217 if(pos == 0) break;
\r
219 write(_stdout, 3, "\x1B[D");
\r
222 if(pos == len) break;
\r
224 write(_stdout, 3, "\x1B[C");
\r
232 if(len <= 0) break; // Protect against underflows
\r
233 write(_stdout, 1, &ch);
\r
234 if(pos == len) { // Simple case of end of string
\r
239 char buf[7] = "\x1B[000D";
\r
240 buf[2] += ((len-pos+1)/100) % 10;
\r
241 buf[3] += ((len-pos+1)/10) % 10;
\r
242 buf[4] += (len-pos+1) % 10;
\r
243 write(_stdout, len-pos, &ret[pos]); // Move Text
\r
244 ch = ' '; write(_stdout, 1, &ch); ch = '\b'; // Clear deleted character
\r
245 write(_stdout, 7, buf); // Update Cursor
\r
247 memmove(&ret[pos-1], &ret[pos], len-pos);
\r
255 //TODO: Implement Tab-Completion
\r
256 //Currently just ignore tabs
\r
261 if(len+1 > space) {
\r
263 ret = realloc(ret, space+1);
\r
264 if(!ret) return NULL;
\r
267 // Editing inside the buffer
\r
269 char buf[7] = "\x1B[000D";
\r
270 buf[2] += ((len-pos)/100) % 10;
\r
271 buf[3] += ((len-pos)/10) % 10;
\r
272 buf[4] += (len-pos) % 10;
\r
273 write(_stdout, 1, &ch); // Print new character
\r
274 write(_stdout, len-pos, &ret[pos]); // Move Text
\r
275 write(_stdout, 7, buf); // Update Cursor
\r
276 memmove( &ret[pos+1], &ret[pos], len-pos );
\r
279 write(_stdout, 1, &ch);
\r
285 } while(ch != '\n');
\r
292 if(Length) *Length = len;
\r
298 * \fn void Parse_Args(char *str, char **dest)
\r
299 * \brief Parse a string into an argument array
\r
301 void Parse_Args(char *str, char **dest)
\r
304 char *buf = malloc( strlen(str) + 1 );
\r
308 Print("Parse_Args: Out of heap space!\n");
\r
315 // Trim leading whitespace
\r
316 while(*buf == ' ' && *buf) buf++;
\r
320 dest[i] = buf; // Save start of string
\r
322 while(*buf && *buf != ' ')
\r
326 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
330 if(*buf == '\0') break;
\r
332 while(*++buf == ' ' && *buf);
\r
333 if(*buf == '\0') break;
\r
343 * \fn void CallCommand(char **Args)
\r
345 void CallCommand(char **Args)
\r
350 char sTmpBuffer[1024];
\r
351 char *exefile = Args[0];
\r
353 if(exefile[0] == '/'
\r
354 || (exefile[0] == '.' && exefile[1] == '/')
\r
355 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
358 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
359 // Check file existence
\r
360 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
362 Print("Unknown Command: `");Print(Args[0]);Print("'\n"); // Error Message
\r
366 // Get File info and close file
\r
367 finfo( fd, &info, 0 );
\r
370 // Check if the file is a directory
\r
371 if(info.flags & FILEFLAG_DIRECTORY) {
\r
372 Print("`");Print(sTmpBuffer); // Error Message
\r
373 Print("' is a directory.\n");
\r
381 // Check all components of $PATH
\r
382 for( i = 0; i < giNumPathDirs; i++ )
\r
384 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
385 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
386 if(fd == -1) continue;
\r
387 finfo( fd, &info, 0 );
\r
389 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
390 // Woohoo! We found a valid command
\r
394 // Exhausted path directories
\r
395 if( i == giNumPathDirs ) {
\r
396 Print("Unknown Command: `");Print(exefile);Print("'\n");
\r
401 // Create new process
\r
402 pid = clone(CLONE_VM, 0);
\r
405 execve(sTmpBuffer, Args, gasEnvironment);
\r
407 Print("Unablt to create process: `");Print(sTmpBuffer);Print("'\n"); // Error Message
\r
411 waittid(pid, &status);
\r
416 * \fn void Command_Logout(int argc, char **argv)
\r
417 * \brief Exit the shell, logging the user out
\r
419 void Command_Logout(int argc, char **argv)
\r
425 * \fn void Command_Colour(int argc, char **argv)
\r
426 * \brief Displays the help screen
\r
428 void Command_Help(int argc, char **argv)
\r
430 Print("Acess 2 Command Line Interface\n");
\r
431 Print(" By John Hodge (thePowersGang / [TPG])\n");
\r
433 Print("Builtin Commands:\n");
\r
434 Print(" logout: Return to the login prompt\n");
\r
435 Print(" exit: Same\n");
\r
436 Print(" help: Display this message\n");
\r
437 Print(" clear: Clear the screen\n");
\r
438 Print(" cd: Change the current directory\n");
\r
439 Print(" dir: Print the contents of the current directory\n");
\r
445 * \fn void Command_Clear(int argc, char **argv)
\r
446 * \brief Clear the screen
\r
448 void Command_Clear(int argc, char **argv)
\r
450 write(_stdout, 4, "\x1B[2J"); //Clear Screen
\r
454 * \fn void Command_Cd(int argc, char **argv)
\r
455 * \brief Change directory
\r
457 void Command_Cd(int argc, char **argv)
\r
459 char tmpPath[1024];
\r
465 Print(gsCurrentDirectory);Print("\n");
\r
469 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
471 fp = open(tmpPath, 0);
\r
473 write(_stdout, 26, "Directory does not exist\n");
\r
476 finfo(fp, &stats, 0);
\r
479 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
480 write(_stdout, 17, "Not a Directory\n");
\r
484 free(gsCurrentDirectory);
\r
485 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
486 strcpy(gsCurrentDirectory, tmpPath);
\r
488 // Register change with kernel
\r
489 chdir( gsCurrentDirectory );
\r
493 * \fn void Command_Dir(int argc, char **argv)
\r
494 * \brief Print the contents of a directory
\r
496 void Command_Dir(int argc, char **argv)
\r
498 int dp, fp, dirLen;
\r
499 char modeStr[11] = "RWXrwxRWX ";
\r
500 char tmpPath[1024];
\r
505 // Generate Directory Path
\r
507 dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
510 strcpy(tmpPath, gsCurrentDirectory);
\r
512 dirLen = strlen(tmpPath);
\r
515 dp = open(tmpPath, OPENFLAG_READ);
\r
516 // Check if file opened
\r
519 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
523 if( finfo(dp, &info, 0) == -1 )
\r
526 write(_stdout, 34, "stat Failed, Bad File Descriptor\n");
\r
529 // Check if it's a directory
\r
530 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
533 write(_stdout, 27, "Unable to open directory `");
\r
534 write(_stdout, strlen(tmpPath)+1, tmpPath);
\r
535 write(_stdout, 20, "', Not a directory\n");
\r
539 // Append Shash for file paths
\r
540 if(tmpPath[dirLen-1] != '/')
\r
542 tmpPath[dirLen++] = '/';
\r
543 tmpPath[dirLen] = '\0';
\r
546 fileName = (char*)(tmpPath+dirLen);
\r
547 // Read Directory Content
\r
548 while( (fp = readdir(dp, fileName)) )
\r
553 write(_stdout, 42, "Invalid Permissions to traverse directory\n");
\r
557 fp = open(tmpPath, 0);
\r
558 if(fp == -1) continue;
\r
560 finfo(fp, &info, 0);
\r
562 if(info.flags & FILEFLAG_DIRECTORY)
\r
563 write(_stdout, 1, "d");
\r
565 write(_stdout, 1, "-");
\r
569 acl.group = 0; acl.id = info.uid;
\r
570 _SysGetACL(fp, &acl);
\r
571 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
572 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
573 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
575 acl.group = 1; acl.id = info.gid;
\r
576 _SysGetACL(fp, &acl);
\r
577 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
578 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
579 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
581 acl.group = 1; acl.id = -1;
\r
582 _SysGetACL(fp, &acl);
\r
583 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
584 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
585 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
586 write(_stdout, 10, modeStr);
\r
590 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
591 write(_stdout, 6, "\x1B[32m");
\r
595 write(_stdout, strlen(fileName), fileName);
\r
597 // Print slash if applicable
\r
598 if(info.flags & FILEFLAG_DIRECTORY)
\r
599 write(_stdout, 1, "/");
\r
602 write(_stdout, 6, "\x1B[37m");
\r
605 write(_stdout, 1, "\n");
\r