2 AcessOS Shell Version 2
\r
3 - Based on IOOS CLI Shell
\r
5 #include <acess/sys.h>
\r
13 // ==== PROTOTYPES ====
\r
14 char *ReadCommandLine(int *Length);
\r
15 void Parse_Args(char *str, char **dest);
\r
16 void Command_Colour(int argc, char **argv);
\r
17 void Command_Clear(int argc, char **argv);
\r
18 //void Command_Ls(int argc, char **argv);
\r
19 void Command_Cd(int argc, char **argv);
\r
20 //void Command_Cat(int argc, char **argv);
\r
22 // ==== CONSTANT GLOBALS ====
\r
23 char *cCOLOUR_NAMES[8] = {"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"};
\r
26 void (*fcn)(int argc, char **argv);
\r
28 {"colour", Command_Colour}, {"clear", Command_Clear}, {"cd", Command_Cd}
\r
30 #define BUILTIN_COUNT (sizeof(cBUILTINS)/sizeof(cBUILTINS[0]))
\r
32 // ==== LOCAL VARIABLES ====
\r
33 char gsCommandBuffer[1024];
\r
34 char *gsCurrentDirectory = "/";
\r
35 char gsTmpBuffer[1024];
\r
36 char **gasCommandHistory;
\r
37 int giLastCommand = 0;
\r
38 int giCommandSpace = 0;
\r
41 int main(int argc, char *argv[], char *envp[])
\r
51 //Command_Clear(0, NULL);
\r
53 write(_stdout, 1, "\n");
\r
54 write(_stdout, 36, "Acess Shell Version 3\n");
\r
55 write(_stdout, 30, " Based on CLI Shell for IOOS\n");
\r
56 write(_stdout, 2, "\n");
\r
59 // Free last command & arguments
\r
60 if(saArgs[0]) free(saArgs);
\r
61 if(!bCached) free(sCommandStr);
\r
63 write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory);
\r
64 write(_stdout, 3, "$ ");
\r
66 // Read Command line
\r
67 sCommandStr = ReadCommandLine( &length );
\r
69 // Check if the command should be cached
\r
70 if(gasCommandHistory == NULL || strcmp(sCommandStr, gasCommandHistory[giLastCommand]) != 0)
\r
72 if(giLastCommand >= giCommandSpace) {
\r
73 giCommandSpace += 12;
\r
74 gasCommandHistory = realloc(gasCommandHistory, giCommandSpace*sizeof(char*));
\r
77 gasCommandHistory[ giLastCommand ] = sCommandStr;
\r
81 // Parse Command Line into arguments
\r
82 Parse_Args(sCommandStr, saArgs);
\r
86 while(saArgs[iArgCount]) iArgCount++;
\r
88 // Silently Ignore all empty commands
\r
89 if(saArgs[1][0] == '\0') continue;
\r
91 // Check Built-In Commands
\r
92 // [HACK] Mem Usage - Use Length in place of `i'
\r
93 for(length=0;length<BUILTIN_COUNT;length++)
\r
95 if(strcmp(saArgs[1], cBUILTINS[length].name) == 0)
\r
97 cBUILTINS[length].fcn(iArgCount-1, &saArgs[1]);
\r
102 if(length != BUILTIN_COUNT) continue;
\r
104 // - Calling a file
\r
105 GeneratePath(saArgs[1], gsCurrentDirectory, gsTmpBuffer);
\r
106 // Use length in place of fp
\r
107 length = open(gsTmpBuffer, 0);
\r
108 // Check file existence
\r
110 Print("Unknown Command: `");Print(saArgs[1]);Print("'\n"); // Error Message
\r
113 // Check if the file is a directory
\r
114 finfo( length, &info );
116 if(info.flags & FILEFLAG_DIRECTORY) {
\r
117 Print("`");Print(saArgs[1]); // Error Message
\r
118 Print("' is a directory.\n");
\r
121 pid = clone(CLONE_VM, 0);
\r
122 if(pid == 0) execve(gsTmpBuffer, &saArgs[1], NULL);
\r
124 Print("Unablt to create process: `");Print(gsTmpBuffer);Print("'\n"); // Error Message
125 //SysDebug("pid = %i\n", pid);
\r
129 waitpid(pid, &status);
\r
135 * \fn char *ReadCommandLine(int *Length)
\r
136 * \brief Read from the command line
\r
138 char *ReadCommandLine(int *Length)
\r
141 int len, pos, space = 1023;
\r
144 // Preset Variables
\r
145 ret = malloc( space+1 );
\r
149 // Read In Command Line
\r
151 read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call)
\r
152 // Ignore control characters
\r
153 if(ch < 0) continue;
\r
156 if(len <= 0) continue; // Protect against underflows
\r
157 if(pos == len) { // Simple case of end of string
\r
160 memmove(&ret[pos-1], &ret[pos], len-pos);
\r
164 write(_stdout, 1, &ch);
\r
170 ret = realloc(ret, space+1);
\r
171 if(!ret) return NULL;
\r
174 write(_stdout, 1, &ch);
\r
177 } while(ch != '\n');
\r
184 if(Length) *Length = len;
\r
190 * \fn void Parse_Args(char *str, char **dest)
\r
191 * \brief Parse a string into an argument array
\r
193 void Parse_Args(char *str, char **dest)
\r
196 char *buf = malloc( strlen(str) + 1 );
\r
201 // Trim leading whitespace
\r
202 while(*buf == ' ' && *buf) buf++;
\r
206 dest[i] = buf; // Save start of string
\r
208 while(*buf && *buf != ' ')
\r
212 while(*buf && !(*buf == '"' && buf[-1] != '\\'))
\r
216 if(*buf == '\0') break;
\r
218 while(*++buf == ' ' && *buf);
\r
219 if(*buf == '\0') break;
\r
229 * \fn void Command_Colour(int argc, char **argv)
\r
232 void Command_Colour(int argc, char **argv)
\r
235 char clrStr[6] = "\x1B[37m";
\r
237 // Verify Arg Count
\r
240 Print("Please specify a colour\n");
\r
245 for(fg=0;fg<8;fg++)
\r
246 if(strcmp(cCOLOUR_NAMES[fg], argv[1]) == 0)
\r
249 // Foreground a valid colour
\r
251 Print("Unknown Colour '");Print(argv[1]);Print("'\n");
\r
255 clrStr[3] = '0' + fg;
\r
256 write(_stdout, 6, clrStr);
\r
258 // Need to Set Background?
\r
261 for(bg=0;bg<8;bg++)
\r
262 if(strcmp(cCOLOUR_NAMES[bg], argv[2]) == 0)
\r
268 Print("Unknown Colour '");Print(argv[2]);Print("'\n");
\r
273 clrStr[3] = '0' + bg;
\r
274 write(_stdout, 6, clrStr);
\r
279 // Function Usage (Requested via a Goto (I know it's ugly))
\r
281 Print("Valid Colours are ");
\r
282 for(fg=0;fg<8;fg++)
\r
284 Print(cCOLOUR_NAMES[fg]);
\r
285 write(_stdout, 3, ", ");
\r
287 write(_stdout, 4, "\b\b\n");
\r
291 void Command_Clear(int argc, char **argv)
\r
293 write(_stdout, 4, "\x1B[2J"); //Clear Screen
\r
297 * \fn void Command_Cd(int argc, char **argv)
\r
298 * \brief Change directory
\r
300 void Command_Cd(int argc, char **argv)
\r
302 char tmpPath[1024];
\r
308 Print(gsCurrentDirectory);Print("\n");
\r
312 GeneratePath(argv[1], gsCurrentDirectory, tmpPath);
\r
314 fp = open(tmpPath, 0);
\r
316 write(_stdout, 26, "Directory does not exist\n");
\r
322 if( !(stats.flags & FILEFLAG_DIRECTORY) ) {
\r
323 write(_stdout, 17, "Not a Directory\n");
\r
327 strcpy(gsCurrentDirectory, tmpPath);
\r