2 * AcessOS Shell Version 3
\r
4 #define USE_READLINE 1
\r
5 #include <acess/sys.h>
\r
10 #include <readline.h>
\r
16 // ==== PROTOTYPES ====
\r
17 int Parse_Args(const char *str, char **dest);
\r
18 void CallCommand(char **Args);
\r
19 void Command_Logout(int argc, char **argv);
\r
20 void Command_Clear(int argc, char **argv);
\r
21 void Command_Help(int argc, char **argv);
\r
22 void Command_Cd(int argc, char **argv);
\r
23 void Command_Dir(int argc, char **argv);
\r
25 // ==== CONSTANT GLOBALS ====
\r
28 void (*fcn)(int argc, char **argv);
\r
30 {"exit", Command_Logout}, {"logout", Command_Logout},
\r
31 {"help", Command_Help}, {"clear", Command_Clear},
\r
32 {"cd", Command_Cd}, {"dir", Command_Dir}
\r
34 static char *cDEFAULT_PATH[] = {"/Acess/Bin"};
\r
35 #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0]))
\r
37 // ==== LOCAL VARIABLES ====
\r
38 int giNumPathDirs = 1;
\r
39 char **gasPathDirs = cDEFAULT_PATH;
\r
40 char **gasEnvironment;
\r
41 char gsCommandBuffer[1024];
\r
42 char *gsCurrentDirectory = NULL;
\r
43 char **gasCommandHistory;
\r
44 int giLastCommand = 0;
\r
45 int giCommandSpace = 0;
\r
48 int main(int argc, char *argv[], char **envp)
\r
51 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
80 // Read Command line
\r
81 sCommandStr = Readline( readline_state );
\r
83 length = strlen(sCommandStr);
\r
85 // Parse Command Line into arguments
\r
86 iArgCount = Parse_Args(sCommandStr, saArgs);
\r
88 // Silently Ignore all empty commands
\r
89 if(saArgs[1][0] == '\0') continue;
\r
91 // Check Built-In Commands
\r
92 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
94 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
96 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
100 if(i != BUILTIN_COUNT) continue;
\r
103 CallCommand( &saArgs[1] );
\r
105 free( sCommandStr );
\r
110 * \fn int Parse_Args(const char *str, char **dest)
\r
111 * \brief Parse a string into an argument array
\r
113 int Parse_Args(const char *str, char **dest)
\r
116 char *buf = malloc( strlen(str) + 1 );
\r
120 printf("Parse_Args: Out of heap space!\n");
\r
127 // Trim leading whitespace
\r
128 while(*buf == ' ' && *buf) buf++;
\r
132 dest[i] = buf; // Save start of string
\r
134 while(*buf && *buf != ' ')
\r
138 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
142 if(*buf == '\0') break;
\r
144 while(*++buf == ' ' && *buf);
\r
145 if(*buf == '\0') break;
\r
153 * \fn void CallCommand(char **Args)
\r
155 void CallCommand(char **Args)
\r
160 char sTmpBuffer[1024];
\r
161 char *exefile = Args[0];
\r
163 if(exefile[0] == '/'
\r
164 || (exefile[0] == '.' && exefile[1] == '/')
\r
165 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
168 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
169 // Check file existence
\r
170 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
172 printf("Unknown Command: `%s'\n", Args[0]); // Error Message
\r
176 // Get File info and close file
\r
177 finfo( fd, &info, 0 );
\r
180 // Check if the file is a directory
\r
181 if(info.flags & FILEFLAG_DIRECTORY) {
\r
182 printf("`%s' is a directory.\n", sTmpBuffer);
\r
190 // Check all components of $PATH
\r
191 for( i = 0; i < giNumPathDirs; i++ )
\r
193 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
194 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
195 if(fd == -1) continue;
\r
196 finfo( fd, &info, 0 );
\r
198 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
199 // Woohoo! We found a valid command
\r
203 // Exhausted path directories
\r
204 if( i == giNumPathDirs ) {
\r
205 printf("Unknown Command: `%s'\n", exefile);
\r
210 // Create new process
\r
211 pid = clone(CLONE_VM, 0);
\r
214 execve(sTmpBuffer, Args, gasEnvironment);
\r
215 printf("Execve returned, ... oops\n");
\r
219 printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message
\r
223 waittid(pid, &status);
\r
228 * \fn void Command_Logout(int argc, char **argv)
\r
229 * \brief Exit the shell, logging the user out
\r
231 void Command_Logout(int argc, char **argv)
\r
237 * \fn void Command_Colour(int argc, char **argv)
\r
238 * \brief Displays the help screen
\r
240 void Command_Help(int argc, char **argv)
\r
242 printf( "Acess 2 Command Line Interface\n"
\r
243 " By John Hodge (thePowersGang / [TPG])\n"
\r
245 "Builtin Commands:\n"
\r
246 " logout: Return to the login prompt\n"
\r
248 " help: Display this message\n"
\r
249 " clear: Clear the screen\n"
\r
250 " cd: Change the current directory\n"
\r
251 " dir: Print the contents of the current directory\n");
\r
256 * \fn void Command_Clear(int argc, char **argv)
\r
257 * \brief Clear the screen
\r
259 void Command_Clear(int argc, char **argv)
\r
261 write(_stdout, "\x1B[2J", 4); //Clear Screen
\r
265 * \fn void Command_Cd(int argc, char **argv)
\r
266 * \brief Change directory
\r
268 void Command_Cd(int argc, char **argv)
\r
270 char tmpPath[1024];
\r
276 printf("%s\n", gsCurrentDirectory);
\r
280 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
282 fp = open(tmpPath, 0);
\r
284 printf("Directory does not exist\n");
\r
287 finfo(fp, &stats, 0);
\r
290 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
291 printf("Not a Directory\n");
\r
295 free(gsCurrentDirectory);
\r
296 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
297 strcpy(gsCurrentDirectory, tmpPath);
\r
299 // Register change with kernel
\r
300 chdir( gsCurrentDirectory );
\r
304 * \fn void Command_Dir(int argc, char **argv)
\r
305 * \brief Print the contents of a directory
\r
307 void Command_Dir(int argc, char **argv)
\r
309 int dp, fp, dirLen;
\r
310 char modeStr[11] = "RWXrwxRWX ";
\r
311 char tmpPath[1024];
\r
316 // Generate Directory Path
\r
318 dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
321 strcpy(tmpPath, gsCurrentDirectory);
\r
323 dirLen = strlen(tmpPath);
\r
326 dp = open(tmpPath, OPENFLAG_READ);
\r
327 // Check if file opened
\r
330 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
334 if( finfo(dp, &info, 0) == -1 )
\r
337 printf("stat Failed, Bad File Descriptor\n");
\r
340 // Check if it's a directory
\r
341 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
344 printf("Unable to open directory `%s', Not a directory\n", tmpPath);
\r
348 // Append Shash for file paths
\r
349 if(tmpPath[dirLen-1] != '/')
\r
351 tmpPath[dirLen++] = '/';
\r
352 tmpPath[dirLen] = '\0';
\r
355 fileName = (char*)(tmpPath+dirLen);
\r
356 // Read Directory Content
\r
357 while( (fp = readdir(dp, fileName)) )
\r
362 printf("Invalid Permissions to traverse directory\n");
\r
366 fp = open(tmpPath, 0);
\r
367 if(fp == -1) continue;
\r
369 finfo(fp, &info, 0);
\r
371 if(info.flags & FILEFLAG_DIRECTORY)
\r
378 acl.object = info.uid;
\r
379 _SysGetACL(fp, &acl);
\r
380 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
381 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
382 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
384 acl.object = info.gid | 0x80000000;
\r
385 _SysGetACL(fp, &acl);
\r
386 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
387 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
388 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
390 acl.object = 0xFFFFFFFF;
\r
391 _SysGetACL(fp, &acl);
\r
392 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
393 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
394 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
399 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
400 printf("\x1B[32m");
\r
404 printf("%s", fileName);
\r
406 // Print slash if applicable
\r
407 if(info.flags & FILEFLAG_DIRECTORY)
\r
411 printf("\x1B[37m");
\r