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
80 // Read Command line
\r
81 sCommandStr = Readline( readline_state );
\r
84 // Parse Command Line into arguments
\r
85 iArgCount = Parse_Args(sCommandStr, saArgs);
\r
87 // Silently Ignore all empty commands
\r
88 if(saArgs[1][0] == '\0') continue;
\r
90 // Check Built-In Commands
\r
91 for( i = 0; i < BUILTIN_COUNT; i++ )
\r
93 if( strcmp(saArgs[1], cBUILTINS[i].name) == 0 )
\r
95 cBUILTINS[i].fcn(iArgCount-1, &saArgs[1]);
\r
99 if(i != BUILTIN_COUNT) continue;
\r
102 CallCommand( &saArgs[1] );
\r
104 free( sCommandStr );
\r
109 * \fn int Parse_Args(const char *str, char **dest)
\r
110 * \brief Parse a string into an argument array
\r
112 int Parse_Args(const char *str, char **dest)
\r
115 char *buf = malloc( strlen(str) + 1 );
\r
119 printf("Parse_Args: Out of heap space!\n");
\r
126 // Trim leading whitespace
\r
127 while(*buf == ' ' && *buf) buf++;
\r
131 dest[i] = buf; // Save start of string
\r
133 while(*buf && *buf != ' ')
\r
137 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
141 if(*buf == '\0') break;
\r
143 while(*++buf == ' ' && *buf);
\r
144 if(*buf == '\0') break;
\r
152 * \fn void CallCommand(char **Args)
\r
154 void CallCommand(char **Args)
\r
159 char sTmpBuffer[1024];
\r
160 char *exefile = Args[0];
\r
162 if(exefile[0] == '/'
\r
163 || (exefile[0] == '.' && exefile[1] == '/')
\r
164 || (exefile[0] == '.' && exefile[1] == '.' && exefile[2] == '/')
\r
167 GeneratePath(exefile, gsCurrentDirectory, sTmpBuffer);
\r
168 // Check file existence
\r
169 fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC);
\r
171 printf("Unknown Command: `%s'\n", Args[0]); // Error Message
\r
175 // Get File info and close file
\r
176 _SysFInfo( fd, &info, 0 );
\r
179 // Check if the file is a directory
\r
180 if(info.flags & FILEFLAG_DIRECTORY) {
\r
181 printf("`%s' is a directory.\n", sTmpBuffer);
\r
189 // Check all components of $PATH
\r
190 for( i = 0; i < giNumPathDirs; i++ )
\r
192 GeneratePath(exefile, gasPathDirs[i], sTmpBuffer);
\r
193 fd = _SysOpen(sTmpBuffer, OPENFLAG_EXEC);
\r
194 if(fd == -1) continue;
\r
195 _SysFInfo( fd, &info, 0 );
\r
197 if(info.flags & FILEFLAG_DIRECTORY) continue;
\r
198 // Woohoo! We found a valid command
\r
202 // Exhausted path directories
\r
203 if( i == giNumPathDirs ) {
\r
204 printf("Unknown Command: `%s'\n", exefile);
\r
209 // Create new process
\r
210 int fds[] = {0, 1, 2};
\r
211 pid = _SysSpawn(sTmpBuffer, (const char **)Args, (const char **)gasEnvironment, 3, fds, NULL);
\r
213 printf("Unable to create process: `%s'\n", sTmpBuffer); // Error Message
\r
217 _SysWaitTID(pid, &status);
\r
222 * \fn void Command_Logout(int argc, char **argv)
\r
223 * \brief Exit the shell, logging the user out
\r
225 void Command_Logout(int argc, char **argv)
\r
231 * \fn void Command_Colour(int argc, char **argv)
\r
232 * \brief Displays the help screen
\r
234 void Command_Help(int argc, char **argv)
\r
236 printf( "Acess 2 Command Line Interface\n"
\r
237 " By John Hodge (thePowersGang / [TPG])\n"
\r
239 "Builtin Commands:\n"
\r
240 " logout: Return to the login prompt\n"
\r
242 " help: Display this message\n"
\r
243 " clear: Clear the screen\n"
\r
244 " cd: Change the current directory\n"
\r
245 " dir: Print the contents of the current directory\n");
\r
250 * \fn void Command_Clear(int argc, char **argv)
\r
251 * \brief Clear the screen
\r
253 void Command_Clear(int argc, char **argv)
\r
255 _SysWrite(_stdout, "\x1B[2J", 4); //Clear Screen
\r
259 * \fn void Command_Cd(int argc, char **argv)
\r
260 * \brief Change directory
\r
262 void Command_Cd(int argc, char **argv)
\r
264 char tmpPath[1024];
\r
270 printf("%s\n", gsCurrentDirectory);
\r
274 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
276 fp = _SysOpen(tmpPath, 0);
\r
278 printf("Directory does not exist\n");
\r
281 _SysFInfo(fp, &stats, 0);
\r
284 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
285 printf("Not a Directory\n");
\r
289 free(gsCurrentDirectory);
\r
290 gsCurrentDirectory = malloc(strlen(tmpPath)+1);
\r
291 strcpy(gsCurrentDirectory, tmpPath);
\r
293 // Register change with kernel
\r
294 _SysChdir( gsCurrentDirectory );
\r
298 * \fn void Command_Dir(int argc, char **argv)
\r
299 * \brief Print the contents of a directory
\r
301 void Command_Dir(int argc, char **argv)
\r
304 char modeStr[11] = "RWXrwxRWX ";
\r
305 char fileName[256];
\r
310 // -- Generate and open directory --
\r
311 // Generate Directory Path
\r
312 char tmpPath[1024];
\r
314 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
316 strcpy(tmpPath, gsCurrentDirectory);
\r
318 dp = _SysOpen(tmpPath, OPENFLAG_READ);
\r
320 printf("Unable to open directory `%s', File cannot be found\n", tmpPath);
\r
324 if( _SysFInfo(dp, &info, 0) == -1 )
\r
327 printf("stat Failed, Bad File Descriptor\n");
\r
330 // Check if it's a directory
\r
331 if(!(info.flags & FILEFLAG_DIRECTORY))
\r
334 printf("Unable to open directory `%s', Not a directory\n", tmpPath);
\r
338 // -- Read Directory Contents --
\r
339 while( (fp = _SysReadDir(dp, fileName)) )
\r
344 printf("Invalid Permissions to traverse directory\n");
\r
348 fp = _SysOpenChild(dp, fileName, 0);
\r
349 if(fp == -1) continue;
\r
351 _SysFInfo(fp, &info, 0);
\r
353 if(info.flags & FILEFLAG_DIRECTORY)
\r
360 acl.object = info.uid;
\r
361 _SysGetACL(fp, &acl);
\r
362 if(acl.perms & 1) modeStr[0] = 'r'; else modeStr[0] = '-';
\r
363 if(acl.perms & 2) modeStr[1] = 'w'; else modeStr[1] = '-';
\r
364 if(acl.perms & 8) modeStr[2] = 'x'; else modeStr[2] = '-';
\r
366 acl.object = info.gid | 0x80000000;
\r
367 _SysGetACL(fp, &acl);
\r
368 if(acl.perms & 1) modeStr[3] = 'r'; else modeStr[3] = '-';
\r
369 if(acl.perms & 2) modeStr[4] = 'w'; else modeStr[4] = '-';
\r
370 if(acl.perms & 8) modeStr[5] = 'x'; else modeStr[5] = '-';
\r
372 acl.object = 0xFFFFFFFF;
\r
373 _SysGetACL(fp, &acl);
\r
374 if(acl.perms & 1) modeStr[6] = 'r'; else modeStr[6] = '-';
\r
375 if(acl.perms & 2) modeStr[7] = 'w'; else modeStr[7] = '-';
\r
376 if(acl.perms & 8) modeStr[8] = 'x'; else modeStr[8] = '-';
\r
381 if(info.flags & FILEFLAG_DIRECTORY) // Directory: Green
\r
382 printf("\x1B[32m");
\r
386 printf("%s", fileName);
\r
388 // Print slash if applicable
\r
389 if(info.flags & FILEFLAG_DIRECTORY)
\r
393 printf("\x1B[37m");
\r