aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhpa <hpa>2004-12-17 19:39:19 +0000
committerhpa <hpa>2004-12-17 19:39:19 +0000
commit14c52318cda1a548e284a90167446fb8feba5e99 (patch)
treef4e89fa780b5678783a9183157bbbd63320bb87d
parent3a3aea8eed5247c650befe2d5ca82e1b220b54c7 (diff)
downloadsyslinux-14c52318cda1a548e284a90167446fb8feba5e99.tar.gz
syslinux-14c52318cda1a548e284a90167446fb8feba5e99.tar.xz
syslinux-14c52318cda1a548e284a90167446fb8feba5e99.zip
Add code to parse argc, argv; REGPARM-ize assembly code
-rw-r--r--com32/lib/sys/argv.c90
-rw-r--r--com32/lib/sys/entry.S13
2 files changed, 100 insertions, 3 deletions
diff --git a/com32/lib/sys/argv.c b/com32/lib/sys/argv.c
new file mode 100644
index 00000000..62c3b048
--- /dev/null
+++ b/com32/lib/sys/argv.c
@@ -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;
+}
+
diff --git a/com32/lib/sys/entry.S b/com32/lib/sys/entry.S
index 40194528..26efde00 100644
--- a/com32/lib/sys/entry.S
+++ b/com32/lib/sys/entry.S
@@ -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