changeset 129:86ec817aa4f4

Build the argument stuff in program space
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 18 Aug 2011 16:54:59 -0400
parents ca9f01e6e2bf
children 550840bcc140
files src/os/shell.c src/prog/echo.c
diffstat 2 files changed, 39 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/os/shell.c	Thu Aug 11 20:58:46 2011 -0400
+++ b/src/os/shell.c	Thu Aug 18 16:54:59 2011 -0400
@@ -10,22 +10,29 @@
 #include <os/scall.h>
 #include <os/pcb.h>
 
-#define PROGRAM_STACK_START 0x400000 - 160 //this is correct, ignore the documentation
-#define KERNEL_STACK_START 0x380000 - 160 //this is correct, ignore the documentation
+#define PROGRAM_STACK_START 0x400000
+#define KERNEL_STACK_START 0x380000
+#define STACK_SPACING 160
+
+#define MAX_INPUT_LENGTH 128 //128 characters maximum input
+#define MAX_ARGS 10 //no more than ten arguments, including the program name
 
 void start(u64 __memsize)
 {
-	char buffer[128];
-	char *c, *track; //TODO rememember if there was any reason I used b and c as variable names...
+	char buffer[MAX_INPUT_LENGTH];
+	char *c, *track;
+	char *argStartLoc = (void*)PROGRAM_STACK_START - MAX_INPUT_LENGTH;
+
 	ErrCode err;
 	init_all(__memsize);
 	if(isError(init_fs(0x100, __memsize))) die();
-	set_svc_handler(svc_handler, (void*)KERNEL_STACK_START);
+	set_svc_handler(svc_handler, (void*)(KERNEL_STACK_START - STACK_SPACING));
 	
 	sPrint("SOS online!\n");
 	while(1) {
 		sPrint("$ ");
-		sGet(buffer, 78);
+		sGet(buffer, MAX_INPUT_LENGTH);
+		memcpy(argStartLoc, buffer, MAX_INPUT_LENGTH); //copy the command+arguments over
 		c = strtok_r(buffer, " ", &track);
 
 		u32 fid;
@@ -60,21 +67,36 @@
 
 			PCB pcb;
 			memset(&pcb, 0, sizeof(pcb));
-			pcb.registers[15] = PROGRAM_STACK_START;
+
+			/* TODO FUTURE PLAN FOR COMMAND LINE PARAMETERS
+			 * Instead of the current way of copying things, I would like to eventually
+			 * change it so that each argument is copied over and its location added to
+			 * a stack.  Later, the items could be removed from the stack and placed in
+			 * the program stack in the correct order
+			 */
+
+			char **argv = (void*)PROGRAM_STACK_START - MAX_INPUT_LENGTH - MAX_ARGS*(sizeof(char*));
+			char **curArg = argv;
+			char *b = argStartLoc;
+			int argc = 0;
+			
+			while((*curArg++ = strtok_r(b, " ", &track))) {
+				if(b) b = NULL;
+				if(++argc == MAX_ARGS) break; //TODO find a better way to deal with this
+			}
+
+			size_t argOffset = MAX_INPUT_LENGTH + MAX_ARGS*(sizeof(char*));
+			//I know the preceding line is a duplicate from the above, in the future
+			//I want to get rid of MAX_ARGS so this will be actually calculated...
+
+			pcb.registers[2] = argc;
+			pcb.registers[3] = (u64)argv;
+			pcb.registers[15] = PROGRAM_STACK_START - argOffset - STACK_SPACING;
 			pcb.psw.p = 1;
 			pcb.psw.ea = 1;
 			pcb.psw.ba = 1;
 			pcb.psw.ptr = eHdr->e_entry;
 
-			char *clps[10]; //for now, only accept ten values
-			char **argv = clps;
-			pcb.registers[3] = (u64)argv; //TODO where should this actually be stored??
-			
-			while((*argv++ = strtok_r(NULL, " ", &track))) { //there was another item passed to the program
-				pcb.registers[2]++; //this starts at 0
-				if(pcb.registers[2] == 10) break; //TODO find a better way to deal with this
-			}
-
 			swapcontext_pcb(&shellPCB, &pcb);
 		loopCont:
 			free(data);
--- a/src/prog/echo.c	Thu Aug 11 20:58:46 2011 -0400
+++ b/src/prog/echo.c	Thu Aug 18 16:54:59 2011 -0400
@@ -2,7 +2,7 @@
 #include <std.h>
 #include <string.h>
 
-void start(int argc, char** argv) //TODO: accept a command line argument of what to print
+void start(int argc, char** argv)
 {
 	if(argc) {
 		int i;