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
54 tReadline *readline_state = Readline_Init(1);
\r
56 gasEnvironment = envp;
\r
58 Command_Clear(0, NULL);
\r
61 char *tmp = getenv("CWD");
\r
63 gsCurrentDirectory = malloc(strlen(tmp)+1);
\r
64 strcpy(gsCurrentDirectory, tmp);
\r
66 gsCurrentDirectory = malloc(2);
\r
67 strcpy(gsCurrentDirectory, "/");
\r
71 printf("Acess Shell Version 3\n\n");
\r
74 // Free last command & arguments
\r
75 if(saArgs[0]) free(saArgs[0]);
\r
77 printf("%s$ ", gsCurrentDirectory);
\r
79 // Read Command line
\r
80 sCommandStr = Readline( readline_state );
\r
83 // Parse Command Line into arguments
\r
84 iArgCount = Parse_Args(sCommandStr, saArgs);
\r
86 // Silently Ignore all empty commands
\r
87 if(saArgs[1][0] == '\0') continue;
\r
89 // Check Built-In Commands
\r
90 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
92 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
94 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
98 if(i != BUILTIN_COUNT) continue;
\r
101 CallCommand( &saArgs[1] );
\r
103 free( sCommandStr );
\r
108 * \fn int Parse_Args(const char *str, char **dest)
\r
109 * \brief Parse a string into an argument array
\r
111 int Parse_Args(const char *str, char **dest)
\r
114 char *buf = malloc( strlen(str) + 1 );
\r
118 printf("Parse_Args: Out of heap space!\n");
\r
125 // Trim leading whitespace
\r
126 while(*buf == ' ' && *buf) buf++;
\r
130 dest[i] = buf; // Save start of string
\r
132 while(*buf && *buf != ' ')
\r
136 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
140 if(*buf == '\0') break;
\r
142 while(*++buf == ' ' && *buf);
\r
143 if(*buf == '\0') break;
\r
151 * \fn void CallCommand(char **Args)
\r
153 void CallCommand(char **Args)
\r
158 char sTmpBuffer[1024];
\r
159 char *exefile = Args[0];
\r
161 if(exefile[0] == '/'
\r
162 || (exefile[0] == '.' && exefile[1] == '/')
\r
163 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
166 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
167 // Check file existence
\r
168 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
170 printf("Unknown Command: `%s'\n", Args[0]); // Error Message
\r
174 // Get File info and close file
\r
175 finfo( fd, &info, 0 );
\r
178 // Check if the file is a directory
\r
179 if(info.flags & FILEFLAG_DIRECTORY) {
\r
180 printf("`%s' is a directory.\n", sTmpBuffer);
\r
188 // Check all components of $PATH
\r
189 for( i = 0; i < giNumPathDirs; i++ )
\r
191 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
192 fd = open(sTmpBuffer, OPENFLAG_EXEC);
\r
193 if(fd == -1) continue;
\r
194 finfo( fd, &info, 0 );
\r
196 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
197 // Woohoo! We found a valid command
\r
201 // Exhausted path directories
\r
202 if( i == giNumPathDirs ) {
\r
203 printf("Unknown Command: `%s'\n", exefile);
\r
208 // Create new process
\r
209 pid = clone(CLONE_VM, 0);
\r
212 execve(sTmpBuffer, Args, gasEnvironment);
\r
213 printf("Execve returned, ... oops\n");
\r
217 printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message
\r
221 waittid(pid, &status);
\r
226 * \fn void Command_Logout(int argc, char **argv)
\r
227 * \brief Exit the shell, logging the user out
\r
229 void Command_Logout(int argc, char **argv)
\r
235 * \fn void Command_Colour(int argc, char **argv)
\r
236 * \brief Displays the help screen
\r
238 void Command_Help(int argc, char **argv)
\r
240 printf( "Acess 2 Command Line Interface\n"
\r
241 " By John Hodge (thePowersGang / [TPG])\n"
\r
243 "Builtin Commands:\n"
\r
244 " logout: Return to the login prompt\n"
\r
246 " help: Display this message\n"
\r
247 " clear: Clear the screen\n"
\r
248 " cd: Change the current directory\n"
\r
249 " dir: Print the contents of the current directory\n");
\r
254 * \fn void Command_Clear(int argc, char **argv)
\r
255 * \brief Clear the screen
\r
257 void Command_Clear(int argc, char **argv)
\r
259 write(_stdout, "\x1B[2J", 4); //Clear Screen
\r
263 * \fn void Command_Cd(int argc, char **argv)
\r
264 * \brief Change directory
\r
266 void Command_Cd(int argc, char **argv)
\r
268 char tmpPath[1024];
\r
274 printf("%s\n", gsCurrentDirectory);
\r
278 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
280 fp = open(tmpPath, 0);
\r
282 printf("Directory does not exist\n");
\r
285 finfo(fp, &stats, 0);
\r
288 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
289 printf("Not a Directory\n");
\r
293 free(gsCurrentDirectory);
\r
294 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
295 strcpy(gsCurrentDirectory, tmpPath);
\r
297 // Register change with kernel
\r
298 chdir( gsCurrentDirectory );
\r
302 * \fn void Command_Dir(int argc, char **argv)
\r
303 * \brief Print the contents of a directory
\r
305 void Command_Dir(int argc, char **argv)
\r
307 int dp, fp, dirLen;
\r
308 char modeStr[11] = "RWXrwxRWX ";
\r
309 char tmpPath[1024];
\r
314 // Generate Directory Path
\r
316 dirLen = GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
319 strcpy(tmpPath, gsCurrentDirectory);
\r
321 dirLen = strlen(tmpPath);
\r
324 dp = open(tmpPath, OPENFLAG_READ);
\r
325 // Check if file opened
\r
328 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
332 if( finfo(dp, &info, 0) == -1 )
\r
335 printf("stat Failed, Bad File Descriptor\n");
\r
338 // Check if it's a directory
\r
339 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
342 printf("Unable to open directory `%s', Not a directory\n", tmpPath);
\r
346 // Append Shash for file paths
\r
347 if(tmpPath[dirLen-1] != '/')
\r
349 tmpPath[dirLen++] = '/';
\r
350 tmpPath[dirLen] = '\0';
\r
353 fileName = (char*)(tmpPath+dirLen);
\r
354 // Read Directory Content
\r
355 while( (fp = readdir(dp, fileName)) )
\r
360 printf("Invalid Permissions to traverse directory\n");
\r
364 fp = open(tmpPath, 0);
\r
365 if(fp == -1) continue;
\r
367 finfo(fp, &info, 0);
\r
369 if(info.flags & FILEFLAG_DIRECTORY)
\r
376 acl.object = info.uid;
\r
377 _SysGetACL(fp, &acl);
\r
378 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
379 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
380 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
382 acl.object = info.gid | 0x80000000;
\r
383 _SysGetACL(fp, &acl);
\r
384 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
385 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
386 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
388 acl.object = 0xFFFFFFFF;
\r
389 _SysGetACL(fp, &acl);
\r
390 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
391 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
392 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
397 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
398 printf("\x1B[32m");
\r
402 printf("%s", fileName);
\r
404 // Print slash if applicable
\r
405 if(info.flags & FILEFLAG_DIRECTORY)
\r
409 printf("\x1B[37m");
\r