Add code to parse argc, argv; REGPARM-ize assembly code
authorhpa <hpa>
Fri, 17 Dec 2004 19:39:19 +0000 (19:39 +0000)
committerhpa <hpa>
Fri, 17 Dec 2004 19:39:19 +0000 (19:39 +0000)
com32/lib/sys/argv.c [new file with mode: 0644]
com32/lib/sys/entry.S

diff --git a/com32/lib/sys/argv.c b/com32/lib/sys/argv.c
new file mode 100644 (file)
index 0000000..62c3b04
--- /dev/null
@@ -0,0 +1,90 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *   
+ *   Copyright 2004 H. Peter Anvin - All Rights Reserved
+ *
+ *   Permission is hereby granted, free of charge, to any person
+ *   obtaining a copy of this software and associated documentation
+ *   files (the "Software"), to deal in the Software without
+ *   restriction, including without limitation the rights to use,
+ *   copy, modify, merge, publish, distribute, sublicense, and/or
+ *   sell copies of the Software, and to permit persons to whom
+ *   the Software is furnished to do so, subject to the following
+ *   conditions:
+ *   
+ *   The above copyright notice and this permission notice shall
+ *   be included in all copies or substantial portions of the Software.
+ *   
+ *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ *   OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * argv.c
+ *
+ * Parse a single C string into argc and argv (argc is return value.)
+ * memptr points to available memory.
+ */
+
+#include <inttypes.h>
+#include <stddef.h>
+
+#define ALIGN_UP(p,t)       ((t *)(((uintptr_t)(p) + (sizeof(t)-1)) & ~(sizeof(t)-1)))
+
+extern char _end[];         /* Symbol created by linker */
+void *__mem_end = &_end;     /* Global variable for use by malloc() */
+
+int __parse_argv(char ***argv, const char *str)
+{
+  char *mem = __mem_end;
+  const char *p = str;
+  char *q = mem;
+  char *r;
+  char **arg;
+  int wasspace = 0;
+  int argc = 1;
+
+  /* First copy the string, turning whitespace runs into nulls */
+  for ( p = str ; ; p++ ) {
+    if ( *p <= ' ' ) {
+      if ( !wasspace ) {
+       wasspace = 1;
+       *q++ = '\0';
+      }
+    } else {
+      if ( wasspace ) {
+       argc++;
+       wasspace = 0;
+      }
+      *q++ = *p;
+    }
+
+    /* This test is AFTER we have processed the null byte;
+       we treat it as a whitespace character */
+    if ( ! *p )
+      break;
+  }
+  q--;                         /* Point to final null */
+
+  /* Now create argv */
+  *argv = arg = ALIGN_UP(q,char *);
+  *arg++ = mem;                        /* argv[0] */
+
+  for ( r = mem ; r < q ; r++ ) {
+    if ( *r == '\0' )
+      *arg++ = r+1;
+  }
+
+  *arg++ = NULL;               /* Null pointer at the end */
+  __mem_end = arg;             /* End of memory we used */
+
+  return argc;
+}
+
index 4019452..26efde0 100644 (file)
@@ -57,6 +57,13 @@ _start:
                movl (%esi),%ecx
 1:             rep ; movsl
 
+               # Parse the command line (assumes REGPARM)
+               movl $__com32+4,%edx            # Command line
+               pushl %edx                      # Make space for argv
+               movl %esp,%eax
+               call __parse_argv
+               pushl %eax                      # Save argc
+
                # Look for library initialization functions
                movl $__ctors_start, %esi
 2:
@@ -65,13 +72,13 @@ _start:
                call *(%esi)
                addl $4,%esi
                jmp 2b
-
 #
-# Run program.  Note that we dont actually pass argc, argv to main...
+# Actually run main.  This assumes REGPARM is used!!!!
 #
 3:
+               popl %eax                       # argc
+               popl %edx                       # argv
                call main
-               pushl %eax
                call *(__exit_handler)
                hlt
                .size _start, .-_start