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
98 length = strlen(sCommandStr);
\r
100 sCommandStr = ReadCommandLine( &length );
\r
103 write(_stdout, 25, "PANIC: Out of heap space\n");
\r
107 // Check if the command should be cached
\r
108 if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0)
\r
110 if(giLastCommand >= giCommandSpace) {
\r
111 giCommandSpace += 12;
\r
112 gasCommandHistory = realloc(gasCommandHistory, giCommandSpace*sizeof(char*));
\r
115 gasCommandHistory[ giLastCommand ] = sCommandStr;
\r
120 // Parse Command Line into arguments
\r
121 Parse_Args(sCommandStr, saArgs);
\r
125 while(saArgs[iArgCount]) iArgCount++;
\r
127 // Silently Ignore all empty commands
\r
128 if(saArgs[1][0] == '\0') continue;
\r
130 // Check Built-In Commands
\r
131 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
133 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
135 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
139 if(i != BUILTIN_COUNT) continue;
\r
142 CallCommand( &saArgs[1] );
\r
145 free( sCommandStr );
\r
151 * \fn char *ReadCommandLine(int *Length)
\r
152 * \brief Read from the command line
\r
154 char *ReadCommandLine(int *Length)
\r
157 int len, pos, space = 1023;
\r
160 int scrollbackPos = giLastCommand;
\r
163 // Preset Variables
\r
164 ret = malloc( space+1 );
\r
165 if(!ret) return NULL;
\r
168 // Read In Command Line
\r
170 read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call)
\r
172 if(ch == '\n') break;
\r
176 // Control characters
\r
178 read(_stdin, 1, &ch); // Read control character
\r
181 //case 'D': if(pos) pos--; break;
\r
182 //case 'C': if(pos<len) pos++; break;
\r
184 read(_stdin, 1, &ch); // Read control character
\r
191 if( scrollbackPos > 0 ) break;
\r
194 ret = strdup( gasCommandHistory[--scrollbackPos] );
\r
197 while(pos--) write(_stdout, 3, "\x1B[D");
\r
198 write(_stdout, len, ret); pos = len;
\r
199 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
205 if( scrollbackPos < giLastCommand-1 ) break;
\r
208 ret = strdup( gasCommandHistory[++scrollbackPos] );
\r
211 while(pos--) write(_stdout, 3, "\x1B[D");
\r
212 write(_stdout, len, ret); pos = len;
\r
213 while(pos++ < oldLen) write(_stdout, 1, " ");
\r
218 if(pos == 0) break;
\r
220 write(_stdout, 3, "\x1B[D");
\r
223 if(pos == len) break;
\r
225 write(_stdout, 3, "\x1B[C");
\r
233 if(len <= 0) break; // Protect against underflows
\r
234 write(_stdout, 1, &ch);
\r
235 if(pos == len) { // Simple case of end of string
\r
240 char buf[7] = "\x1B[000D";
\r
241 buf[2] += ((len-pos+1)/100) % 10;
\r
242 buf[3] += ((len-pos+1)/10) % 10;
\r
243 buf[4] += (len-pos+1) % 10;
\r
244 write(_stdout, len-pos, &ret[pos]); // Move Text
\r
245 ch = ' '; write(_stdout, 1, &ch); ch = '\b'; // Clear deleted character
\r
246 write(_stdout, 7, buf); // Update Cursor
\r
248 memmove(&ret[pos-1], &ret[pos], len-pos);
\r
256 //TODO: Implement Tab-Completion
\r
257 //Currently just ignore tabs
\r
262 if(len+1 > space) {
\r
264 ret = realloc(ret, space+1);
\r
265 if(!ret) return NULL;
\r
268 // Editing inside the buffer
\r
270 char buf[7] = "\x1B[000D";
\r
271 buf[2] += ((len-pos)/100) % 10;
\r
272 buf[3] += ((len-pos)/10) % 10;
\r
273 buf[4] += (len-pos) % 10;
\r
274 write(_stdout, 1, &ch); // Print new character
\r
275 write(_stdout, len-pos, &ret[pos]); // Move Text
\r
276 write(_stdout, 7, buf); // Update Cursor
\r
277 memmove( &ret[pos+1], &ret[pos], len-pos );
\r
280 write(_stdout, 1, &ch);
\r
286 } while(ch != '\n');
\r
293 if(Length) *Length = len;
\r
299 * \fn void Parse_Args(char *str, char **dest)
\r
300 * \brief Parse a string into an argument array
\r
302 void Parse_Args(char *str, char **dest)
\r
305 char *buf = malloc( strlen(str) + 1 );
\r
309 Print("Parse_Args: Out of heap space!\n");
\r
316 // Trim leading whitespace
\r
317 while(*buf == ' ' && *buf) buf++;
\r
321 dest[i] = buf; // Save start of string
\r
323 while(*buf && *buf != ' ')
\r
327 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
331 if(*buf == '\0') break;
\r
333 while(*++buf == ' ' && *buf);
\r
334 if(*buf == '\0') break;
\r
344 * \fn void CallCommand(char **Args)
\r
346 void CallCommand(char **Args)
\r
351 char sTmpBuffer[1024];
\r
352 char *exefile = Args[0];
\r
354 if(exefile[0] == '/'
\r
355 || (exefile[0] == '.' && exefile[1] == '/')
\r
356 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
359 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
360 // Check file existence
\r
361 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
363 Print("Unknown Command: `");Print(Args[0]);Print("'\n"); // Error Message
\r
367 // Get File info and close file
\r
368 finfo( fd, &info, 0 );
\r
371 // Check if the file is a directory
\r
372 if(info.flags & FILEFLAG_DIRECTORY) {
\r
373 Print("`");Print(sTmpBuffer); // Error Message
\r
374 Print("' is a directory.\n");
\r
382 // Check all components of $PATH
\r
383 for( i = 0; i < giNumPathDirs; i++ )
\r
385 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
386 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
387 if(fd == -1) continue;
\r
388 finfo( fd, &info, 0 );
\r
390 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
391 // Woohoo! We found a valid command
\r
395 // Exhausted path directories
\r
396 if( i == giNumPathDirs ) {
\r
397 Print("Unknown Command: `");Print(exefile);Print("'\n");
\r
402 // Create new process
\r
403 pid = clone(CLONE_VM, 0);
\r
406 execve(sTmpBuffer, Args, gasEnvironment);
\r
408 Print("Unablt to create process: `");Print(sTmpBuffer);Print("'\n"); // Error Message
\r
412 waittid(pid, &status);
\r
417 * \fn void Command_Logout(int argc, char **argv)
\r
418 * \brief Exit the shell, logging the user out
\r
420 void Command_Logout(int argc, char **argv)
\r
426 * \fn void Command_Colour(int argc, char **argv)
\r
427 * \brief Displays the help screen
\r
429 void Command_Help(int argc, char **argv)
\r
431 Print("Acess 2 Command Line Interface\n");
\r
432 Print(" By John Hodge (thePowersGang / [TPG])\n");
\r
434 Print("Builtin Commands:\n");
\r
435 Print(" logout: Return to the login prompt\n");
\r
436 Print(" exit: Same\n");
\r
437 Print(" help: Display this message\n");
\r
438 Print(" clear: Clear the screen\n");
\r
439 Print(" cd: Change the current directory\n");
\r
440 Print(" dir: Print the contents of the current directory\n");
\r
446 * \fn void Command_Clear(int argc, char **argv)
\r
447 * \brief Clear the screen
\r
449 void Command_Clear(int argc, char **argv)
\r
451 write(_stdout, 4, "\x1B[2J"); //Clear Screen
\r
455 * \fn void Command_Cd(int argc, char **argv)
\r
456 * \brief Change directory
\r
458 void Command_Cd(int argc, char **argv)
\r
460 char tmpPath[1024];
\r
466 Print(gsCurrentDirectory);Print("\n");
\r
470 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
472 fp = open(tmpPath, 0);
\r
474 write(_stdout, 26, "Directory does not exist\n");
\r
477 finfo(fp, &stats, 0);
\r
480 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
481 write(_stdout, 17, "Not a Directory\n");
\r
485 free(gsCurrentDirectory);
\r
486 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
487 strcpy(gsCurrentDirectory, tmpPath);
\r
489 // Register change with kernel
\r
490 chdir( gsCurrentDirectory );
\r
494 * \fn void Command_Dir(int argc, char **argv)
\r
495 * \brief Print the contents of a directory
\r
497 void Command_Dir(int argc, char **argv)
\r
499 int dp, fp, dirLen;
\r
500 char modeStr[11] = "RWXrwxRWX ";
\r
501 char tmpPath[1024];
\r
506 // Generate Directory Path
\r
508 dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
511 strcpy(tmpPath, gsCurrentDirectory);
\r
513 dirLen = strlen(tmpPath);
\r
516 dp = open(tmpPath, OPENFLAG_READ);
\r
517 // Check if file opened
\r
520 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
524 if( finfo(dp, &info, 0) == -1 )
\r
527 write(_stdout, 34, "stat Failed, Bad File Descriptor\n");
\r
530 // Check if it's a directory
\r
531 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
534 write(_stdout, 27, "Unable to open directory `");
\r
535 write(_stdout, strlen(tmpPath)+1, tmpPath);
\r
536 write(_stdout, 20, "', Not a directory\n");
\r
540 // Append Shash for file paths
\r
541 if(tmpPath[dirLen-1] != '/')
\r
543 tmpPath[dirLen++] = '/';
\r
544 tmpPath[dirLen] = '\0';
\r
547 fileName = (char*)(tmpPath+dirLen);
\r
548 // Read Directory Content
\r
549 while( (fp = readdir(dp, fileName)) )
\r
554 write(_stdout, 42, "Invalid Permissions to traverse directory\n");
\r
558 fp = open(tmpPath, 0);
\r
559 if(fp == -1) continue;
\r
561 finfo(fp, &info, 0);
\r
563 if(info.flags & FILEFLAG_DIRECTORY)
\r
564 write(_stdout, 1, "d");
\r
566 write(_stdout, 1, "-");
\r
570 acl.group = 0; acl.id = info.uid;
\r
571 _SysGetACL(fp, &acl);
\r
572 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
573 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
574 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
576 acl.group = 1; acl.id = info.gid;
\r
577 _SysGetACL(fp, &acl);
\r
578 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
579 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
580 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
582 acl.group = 1; acl.id = -1;
\r
583 _SysGetACL(fp, &acl);
\r
584 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
585 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
586 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
587 write(_stdout, 10, modeStr);
\r
591 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
592 write(_stdout, 6, "\x1B[32m");
\r
596 write(_stdout, strlen(fileName), fileName);
\r
598 // Print slash if applicable
\r
599 if(info.flags & FILEFLAG_DIRECTORY)
\r
600 write(_stdout, 1, "/");
\r
603 write(_stdout, 6, "\x1B[37m");
\r
606 write(_stdout, 1, "\n");
\r