2 * AcessOS Shell Version 3
\r
4 #define USE_READLINE 1
\r
5 #include <acess/sys.h>
\r
10 #include <readline.h>
\r
17 // ==== PROTOTYPES ====
\r
18 int Parse_Args(const char *str, char **dest);
\r
19 void CallCommand(char **Args);
\r
20 void Command_Logout(int argc, char **argv);
\r
21 void Command_Clear(int argc, char **argv);
\r
22 void Command_Help(int argc, char **argv);
\r
23 void Command_Cd(int argc, char **argv);
\r
24 void Command_Dir(int argc, char **argv);
\r
26 // ==== CONSTANT GLOBALS ====
\r
29 void (*fcn)(int argc, char **argv);
\r
31 {"exit", Command_Logout}, {"logout", Command_Logout},
\r
32 {"help", Command_Help}, {"clear", Command_Clear},
\r
33 {"cd", Command_Cd}, {"dir", Command_Dir}
\r
35 static char *cDEFAULT_PATH[] = {"/Acess/Bin","/Acess/SBin"};
\r
36 #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0]))
\r
38 // ==== LOCAL VARIABLES ====
\r
39 int giNumPathDirs = 2;
\r
40 char **gasPathDirs = cDEFAULT_PATH;
\r
41 char **gasEnvironment;
\r
42 char gsCommandBuffer[1024];
\r
43 char *gsCurrentDirectory = NULL;
\r
44 char **gasCommandHistory;
\r
45 int giLastCommand = 0;
\r
46 int giCommandSpace = 0;
\r
49 int main(int argc, char *argv[], char **envp)
\r
52 char *saArgs[32] = {0};
\r
55 tReadline *readline_state = Readline_Init(1);
\r
57 gasEnvironment = envp;
\r
59 // Command_Clear(0, NULL);
\r
62 char *tmp = getenv("CWD");
\r
64 gsCurrentDirectory = malloc(strlen(tmp)+1);
\r
65 strcpy(gsCurrentDirectory, tmp);
\r
67 gsCurrentDirectory = malloc(2);
\r
68 strcpy(gsCurrentDirectory, "/");
\r
72 printf("Acess Shell Version 3\n\n");
\r
75 // Free last command & arguments
\r
76 if(saArgs[0]) free(saArgs[0]);
\r
78 printf("%s$ ", gsCurrentDirectory);
\r
81 // Read Command line
\r
82 sCommandStr = Readline( readline_state );
\r
84 if( !sCommandStr ) {
\r
89 // Parse Command Line into arguments
\r
90 iArgCount = Parse_Args(sCommandStr, saArgs);
\r
92 // Silently Ignore all empty commands
\r
93 if(saArgs[1][0] == '\0') continue;
\r
95 // Check Built-In Commands
\r
96 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
98 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
100 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
104 if(i != BUILTIN_COUNT) continue;
\r
107 CallCommand( &saArgs[1] );
\r
109 free( sCommandStr );
\r
114 * \fn int Parse_Args(const char *str, char **dest)
\r
115 * \brief Parse a string into an argument array
\r
117 int Parse_Args(const char *str, char **dest)
\r
120 char *buf = malloc( strlen(str) + 1 );
\r
124 printf("Parse_Args: Out of heap space!\n");
\r
131 // Trim leading whitespace
\r
132 while(*buf == ' ' && *buf) buf++;
\r
136 dest[i] = buf; // Save start of string
\r
138 while(*buf && *buf != ' ')
\r
142 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
146 if(*buf == '\0') break;
\r
148 while(*++buf == ' ' && *buf);
\r
149 if(*buf == '\0') break;
\r
157 * \fn void CallCommand(char **Args)
\r
159 void CallCommand(char **Args)
\r
164 char sTmpBuffer[1024];
\r
165 char *exefile = Args[0];
\r
167 if(exefile[0] == '/'
\r
168 || (exefile[0] == '.' && exefile[1] == '/')
\r
169 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
172 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
173 // Check file existence
\r
174 fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC);
\r
176 printf("Unknown Command: `%s'\n", Args[0]); // Error Message
\r
180 // Get File info and close file
\r
181 _SysFInfo( fd, &info, 0 );
\r
184 // Check if the file is a directory
\r
185 if(info.flags & FILEFLAG_DIRECTORY) {
\r
186 printf("`%s' is a directory.\n", sTmpBuffer);
\r
194 // Check all components of $PATH
\r
195 for( i = 0; i < giNumPathDirs; i++ )
\r
197 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
198 fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC);
\r
199 if(fd == -1) continue;
\r
200 _SysFInfo( fd, &info, 0 );
\r
202 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
203 // Woohoo! We found a valid command
\r
207 // Exhausted path directories
\r
208 if( i == giNumPathDirs ) {
\r
209 printf("Unknown Command: `%s'\n", exefile);
\r
214 // Create new process
\r
215 int fds[] = {0, 1, 2};
\r
216 pid = _SysSpawn(sTmpBuffer, (const char **)Args, (const char **)gasEnvironment, 3, fds, NULL);
\r
218 printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message
\r
222 _SysWaitTID(pid, &status);
\r
227 * \fn void Command_Logout(int argc, char **argv)
\r
228 * \brief Exit the shell, logging the user out
\r
230 void Command_Logout(int argc, char **argv)
\r
236 * \fn void Command_Colour(int argc, char **argv)
\r
237 * \brief Displays the help screen
\r
239 void Command_Help(int argc, char **argv)
\r
241 printf( "Acess 2 Command Line Interface\n"
\r
242 " By John Hodge (thePowersGang / [TPG])\n"
\r
244 "Builtin Commands:\n"
\r
245 " logout: Return to the login prompt\n"
\r
247 " help: Display this message\n"
\r
248 " clear: Clear the screen\n"
\r
249 " cd: Change the current directory\n"
\r
250 " dir: Print the contents of the current directory\n");
\r
255 * \fn void Command_Clear(int argc, char **argv)
\r
256 * \brief Clear the screen
\r
258 void Command_Clear(int argc, char **argv)
\r
260 _SysWrite(_stdout, "\x1B[2J", 4); //Clear Screen
\r
264 * \fn void Command_Cd(int argc, char **argv)
\r
265 * \brief Change directory
\r
267 void Command_Cd(int argc, char **argv)
\r
269 char tmpPath[1024];
\r
275 printf("%s\n", gsCurrentDirectory);
\r
279 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
281 fp = _SysOpen(tmpPath, 0);
\r
283 printf("Directory does not exist\n");
\r
286 _SysFInfo(fp, &stats, 0);
\r
289 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
290 printf("Not a Directory\n");
\r
294 free(gsCurrentDirectory);
\r
295 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
296 strcpy(gsCurrentDirectory, tmpPath);
\r
298 // Register change with kernel
\r
299 _SysChdir( gsCurrentDirectory );
\r
303 * \fn void Command_Dir(int argc, char **argv)
\r
304 * \brief Print the contents of a directory
\r
306 void Command_Dir(int argc, char **argv)
\r
309 char modeStr[11] = "RWXrwxRWX ";
\r
310 char fileName[256];
\r
315 // -- Generate and open directory --
\r
316 // Generate Directory Path
\r
317 char tmpPath[1024];
\r
319 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
321 strcpy(tmpPath, gsCurrentDirectory);
\r
323 dp = _SysOpen(tmpPath, OPENFLAG_READ);
\r
325 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
329 if( _SysFInfo(dp, &info, 0) == -1 )
\r
332 printf("stat Failed, Bad File Descriptor\n");
\r
335 // Check if it's a directory
\r
336 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
339 printf("Unable to open directory `%s', Not a directory\n", tmpPath);
\r
343 // -- Read Directory Contents --
\r
344 while( (fp = _SysReadDir(dp, fileName)) )
\r
349 printf("Invalid Permissions to traverse directory\n");
\r
353 fp = _SysOpenChild(dp, fileName, 0);
\r
354 if(fp == -1) continue;
\r
356 _SysFInfo(fp, &info, 0);
\r
358 if(info.flags & FILEFLAG_DIRECTORY)
\r
365 acl.object = info.uid;
\r
366 _SysGetACL(fp, &acl);
\r
367 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
368 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
369 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
371 acl.object = info.gid | 0x80000000;
\r
372 _SysGetACL(fp, &acl);
\r
373 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
374 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
375 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
377 acl.object = 0xFFFFFFFF;
\r
378 _SysGetACL(fp, &acl);
\r
379 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
380 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
381 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
386 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
387 printf("\x1B[32m");
\r
391 printf("%s", fileName);
\r
393 // Print slash if applicable
\r
394 if(info.flags & FILEFLAG_DIRECTORY)
\r
398 printf("\x1B[37m");
\r