From cc94c82e35667c170c5a0640c073195ec83fe79f Mon Sep 17 00:00:00 2001
From: Roland McGrath <roland@gnu.org>
Date: Fri, 17 Aug 2001 04:47:44 +0000
Subject: 2001-08-16  Roland McGrath  <roland@frob.com>

	* boot_script.h (struct cmd): Change `task' member type to `task_t'.
	(boot_script_task_create, boot_script_task_resume,
	boot_script_prompt_task_resume, boot_script_free_task): Declare new
	functions that callers must define.
	* userland-boot.c: Define those.
	* boot_script.c (create_task, resume_task, prompt_resume_task): Use
	those instead of direct Mach calls.
	(free_cmd): Likewise.
	(read_file): Function removed.
	(builtin_symbols): Remove the "read-file" element.
	* boot_script.h (boot_script_malloc, boot_script_free): Declare new
	functions that callers must define.
	* boot_script.c: All uses of malloc/free changed to use those instead.
	* userland-boot.c: New file.  Defines them using malloc/free.
	* Makefile (SRCS, OBJS): Add it.
	* boot.c (boot_script_malloc, boot_script_free): Old cruft functions
	removed.
	* boot_script.c (boot_script_parse_line): Take new first argument HOOK.
	Store it in CMD->hook.
	(main): Update caller.
	(boot_script_exec): Pass CMD->hook as 1st arg to boot_script_exec_cmd.
	(boot_script_exec_cmd): Take new first argument HOOK.
	* boot.c (boot_script_exec_cmd): Likewise.
	* boot_script.h (struct cmd): New member `void *hook'.
	Update decls for HOOK arguments added.
---
 boot/boot_script.c | 112 +++++++++++++++++++----------------------------------
 boot/boot_script.h |  25 ++++++++++--
 2 files changed, 61 insertions(+), 76 deletions(-)

(limited to 'boot')

diff --git a/boot/boot_script.c b/boot/boot_script.c
index 04ede101..205fcce4 100644
--- a/boot/boot_script.c
+++ b/boot/boot_script.c
@@ -3,11 +3,14 @@
 /* Written by Shantanu Goel (goel@cs.columbia.edu).  */
 
 #include <mach.h>
-#include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
 #include "boot_script.h"
 
+/*
+#include <stdio.h>
+#include <stdlib.h>
+*/
+
 
 /* This structure describes a symbol.  */
 struct sym
@@ -67,60 +70,27 @@ static int symtab_alloc = 0;
 /* Next available slot in `symtab'.  */
 static int symtab_index = 0;
 
-/* Create a task.  */
+/* Create a task and suspend it.  */
 static int
 create_task (struct cmd *cmd, int *val)
 {
-  if (task_create (mach_task_self (), 0, (task_t *) &cmd->task) ||
-      task_suspend (cmd->task))
-    {
-      printf ("(bootstrap): %s: Cannot create task\n", cmd->path);
-      return BOOT_SCRIPT_MACH_ERROR;
-    }
+  int err = boot_script_task_create (cmd);
   *val = (int) cmd->task;
-  return 0;
+  return err;
 }
 
 /* Resume a task.  */
 static int
 resume_task (struct cmd *cmd, int *val)
 {
-  if (task_resume (cmd->task))
-    {
-      printf ("(bootstrap): %s: Cannot resume task\n", cmd->path);
-      return BOOT_SCRIPT_MACH_ERROR;
-    }
-  return 0;
+  return boot_script_task_resume (cmd);
 }
 
 /* Resume a task when the user hits return.  */
 static int
 prompt_resume_task (struct cmd *cmd, int *val)
 {
-  char xx[5];
-
-  printf ("Hit return to resume %s...", cmd->path);
-  safe_gets (xx, sizeof xx);
-
-  if (task_resume (cmd->task))
-    {
-      printf ("(bootstrap): %s: Cannot resume task\n", cmd->path);
-      return BOOT_SCRIPT_MACH_ERROR;
-    }
-
-  return 0;
-}
-
-static int
-read_file (struct cmd *cmd, int *val)
-{
-  *val = boot_script_read_file (cmd->path);
-  if (*val == 0)
-    {
-      printf ("(bootstrap): %s: Cannot read boot script\n", cmd->path);
-      return BOOT_SCRIPT_MACH_ERROR;
-    }
-  return 0;
+  return boot_script_prompt_task_resume (cmd);
 }
 
 /* List of builtin symbols.  */
@@ -129,7 +99,6 @@ static struct sym builtin_symbols[] =
   { "task-create", VAL_FUNC, (int) create_task, VAL_PORT, 0 },
   { "task-resume", VAL_FUNC, (int) resume_task, VAL_NONE, 1 },
   { "prompt-task-resume", VAL_FUNC, (int) prompt_resume_task, VAL_NONE, 1 },
-  { "read-file", VAL_FUNC, (int) read_file, VAL_PORT, 0 },
 };
 #define NUM_BUILTIN (sizeof (builtin_symbols) / sizeof (builtin_symbols[0]))
 
@@ -140,23 +109,18 @@ static void
 free_cmd (struct cmd *cmd, int aborting)
 {
   if (cmd->task)
-    {
-      if (aborting)
-	task_terminate (cmd->task);
-      else
-	mach_port_deallocate (mach_task_self (), cmd->task);
-    }
+    boot_script_free_task (cmd->task, aborting);
   if (cmd->args)
     {
       int i;
-
       for (i = 0; i < cmd->args_index; i++)
-	free (cmd->args[i]);
-      free (cmd->args);
+	boot_script_free (cmd->args[i], sizeof *cmd->args[i]);
+      boot_script_free (cmd->args, sizeof cmd->args[0] * cmd->args_alloc);
     }
   if (cmd->exec_funcs)
-    free (cmd->exec_funcs);
-  free (cmd);
+    boot_script_free (cmd->exec_funcs,
+		      sizeof cmd->exec_funcs[0] * cmd->exec_funcs_alloc);
+  boot_script_free (cmd, sizeof *cmd);
 }
 
 /* Free all storage allocated by the parser.
@@ -168,13 +132,13 @@ cleanup (int aborting)
 
   for (i = 0; i < cmds_index; i++)
     free_cmd (cmds[i], aborting);
-  free (cmds);
+  boot_script_free (cmds, sizeof cmds[0] * cmds_alloc);
   cmds = 0;
   cmds_index = cmds_alloc = 0;
 
   for (i = 0; i < symtab_index; i++)
-    free (symtab[i]);
-  free (symtab);
+    boot_script_free (symtab[i], sizeof *symtab[i]);
+  boot_script_free (symtab, sizeof symtab[0] * symtab_alloc);
   symtab = 0;
   symtab_index = symtab_alloc = 0;
 }
@@ -192,7 +156,7 @@ add_list (void *ptr, void ***ptr_list, int *alloc, int *index, int incr)
       void **p;
 
       *alloc += incr;
-      p = malloc (*alloc * sizeof (void *));
+      p = boot_script_malloc (*alloc * sizeof (void *));
       if (! p)
 	{
 	  *alloc -= incr;
@@ -201,7 +165,7 @@ add_list (void *ptr, void ***ptr_list, int *alloc, int *index, int incr)
       if (*ptr_list)
 	{
 	  memcpy (p, *ptr_list, *index * sizeof (void *));
-	  free (*ptr_list);
+	  boot_script_free (*ptr_list, (*alloc - incr) * sizeof (void *));
 	}
       *ptr_list = p;
     }
@@ -217,7 +181,7 @@ add_arg (struct cmd *cmd, char *text, int type, int val)
 {
   struct arg *arg;
 
-  arg = malloc (sizeof (struct arg));
+  arg = boot_script_malloc (sizeof (struct arg));
   if (arg)
     {
       arg->text = text;
@@ -226,7 +190,7 @@ add_arg (struct cmd *cmd, char *text, int type, int val)
       if (add_list (arg, (void ***) &cmd->args,
 		    &cmd->args_alloc, &cmd->args_index, 5))
 	{
-	  free (arg);
+	  boot_script_free (arg, sizeof *arg);
 	  return 0;
 	}
     }
@@ -251,14 +215,14 @@ sym_enter (const char *name)
 {
   struct sym *sym;
 
-  sym = malloc (sizeof (struct sym));
+  sym = boot_script_malloc (sizeof (struct sym));
   if (sym)
     {
       memset (sym, 0, sizeof (struct sym));
       sym->name = name;
       if (add_list (sym, (void ***) &symtab, &symtab_alloc, &symtab_index, 20))
 	{
-	  free (sym);
+	  boot_script_free (sym, sizeof *sym);
 	  return 0;
 	}
     }
@@ -267,7 +231,7 @@ sym_enter (const char *name)
 
 /* Parse the command line CMDLINE.  */
 int
-boot_script_parse_line (char *cmdline)
+boot_script_parse_line (void *hook, char *cmdline)
 {
   char *p, *q;
   int error;
@@ -296,10 +260,11 @@ boot_script_parse_line (char *cmdline)
   *q = '\0';
 
   /* Allocate a command structure.  */
-  cmd = malloc (sizeof (struct cmd));
+  cmd = boot_script_malloc (sizeof (struct cmd));
   if (! cmd)
     return BOOT_SCRIPT_NOMEM;
   memset (cmd, 0, sizeof (struct cmd));
+  cmd->hook = hook;
   cmd->path = p;
   p = q + 1;
 
@@ -507,7 +472,7 @@ boot_script_parse_line (char *cmdline)
       char *ptr; \
       int alloc, i; \
       alloc = cmdline_alloc + len - (cmdline_alloc - cmdline_index) + 100; \
-      ptr = malloc (alloc); \
+      ptr = boot_script_malloc (alloc); \
       if (! ptr) \
 	{ \
 	  error = BOOT_SCRIPT_NOMEM; \
@@ -516,7 +481,7 @@ boot_script_parse_line (char *cmdline)
       memcpy (ptr, cmdline, cmdline_index); \
       for (i = 0; i < argc; ++i) \
 	argv[i] = ptr + (argv[i] - cmdline); \
-      free (cmdline); \
+      boot_script_free (cmdline, cmdline_alloc); \
       cmdline = ptr; \
       cmdline_alloc = alloc; \
     } \
@@ -542,7 +507,7 @@ boot_script_exec ()
       /* Allocate a command line and copy command name.  */
       cmdline_index = strlen (cmd->path) + 1;
       cmdline_alloc = cmdline_index + 100;
-      cmdline = malloc (cmdline_alloc);
+      cmdline = boot_script_malloc (cmdline_alloc);
       if (! cmdline)
 	{
 	  cleanup (1);
@@ -551,10 +516,10 @@ boot_script_exec ()
       memcpy (cmdline, cmd->path, cmdline_index);
 
       /* Allocate argument vector.  */
-      argv = malloc (sizeof (char *) * (cmd->args_index + 2));
+      argv = boot_script_malloc (sizeof (char *) * (cmd->args_index + 2));
       if (! argv)
 	{
-	  free (cmdline);
+	  boot_script_free (cmdline, cmdline_alloc);
 	  cleanup (1);
 	  return BOOT_SCRIPT_NOMEM;
 	}
@@ -650,7 +615,7 @@ boot_script_exec ()
       argv[argc] = 0;
 
       /* Execute the command.  */
-      if (boot_script_exec_cmd (cmd->task, cmd->path,
+      if (boot_script_exec_cmd (cmd->hook, cmd->task, cmd->path,
 				argc, argv, cmdline, cmdline_index))
 	{
 	  error = BOOT_SCRIPT_EXEC_ERROR;
@@ -660,8 +625,8 @@ boot_script_exec ()
       error = 0;
 
     done:
-      free (cmdline);
-      free (argv);
+      boot_script_free (cmdline, cmdline_alloc);
+      boot_script_free (argv, sizeof (char *) * (cmd->args_index + 2));
       if (error)
 	{
 	  cleanup (1);
@@ -763,7 +728,8 @@ boot_script_error_string (int err)
 #include <stdio.h>
 
 int
-boot_script_exec_cmd (mach_port_t task, char *path, int argc,
+boot_script_exec_cmd (void *hook,
+		      mach_port_t task, char *path, int argc,
 		      char **argv, char *strings, int stringlen)
 {
   int i;
@@ -807,7 +773,7 @@ main (int argc, char **argv)
       int i, err;
 
       i = strlen (p) + 1;
-      err = boot_script_parse_line (p);
+      err = boot_script_parse_line (0, p);
       if (err)
 	{
 	  fprintf (stderr, "error %s\n", boot_script_error_string (err));
diff --git a/boot/boot_script.h b/boot/boot_script.h
index d2db1a14..6a30f09f 100644
--- a/boot/boot_script.h
+++ b/boot/boot_script.h
@@ -22,11 +22,14 @@
 /* This structure describes a command.  */
 struct cmd
 {
+  /* Cookie passed in to boot_script_parse_line.  */
+  void *hook;
+
   /* Path of executable.  */
   char *path;
 
   /* Task port.  */
-  mach_port_t task;
+  task_t task;
 
   /* Argument list.  */
   struct arg **args;
@@ -48,19 +51,35 @@ struct cmd
 };
 
 
+/* The user must define these functions, we work like malloc and free.  */
+void *boot_script_malloc (size_t);
+void boot_script_free (void *, size_t);
+
 /* The user must define this function.  Load the image of the
    executable specified by PATH in TASK.  Create a thread
    in TASK and point it at the executable's entry point.  Initialize
    TASK's stack with argument vector ARGV of length ARGC whose
    strings are STRINGS.  STRINGS has length STRINGLEN.
    Return 0 for success, non-zero otherwise.  */
-int boot_script_exec_cmd (mach_port_t task, char *path, int argc,
+int boot_script_exec_cmd (void *hook,
+			  mach_port_t task, char *path, int argc,
 			  char **argv, char *strings, int stringlen);
 
 /* The user must define this function.  Load the contents of FILE
    into a fresh anonymous memory object and return the memory object port.  */
 mach_port_t boot_script_read_file (const char *file);
 
+/* The user must define this functions to perform the corresponding
+   Mach task manipulations.  */
+int boot_script_task_create (struct cmd *); /* task_create + task_suspend */
+int boot_script_task_resume (struct cmd *);
+int boot_script_prompt_task_resume (struct cmd *);
+
+/* The user must define this function to clean up the `task_t'
+   returned by boot_script_task_create.  */
+void boot_script_free_task (task_t task, int aborting);
+
+
 /* Parse the command line LINE.  This causes the command line to be
    converted into an internal format.  Returns 0 for success, non-zero
    otherwise.
@@ -68,7 +87,7 @@ mach_port_t boot_script_read_file (const char *file);
    NOTE: The parser writes into the line so it must not be a string constant.
    It is also the responsibility of the caller not to deallocate the line
    across calls to the parser.  */
-int boot_script_parse_line (char *cmdline);
+int boot_script_parse_line (void *hook, char *cmdline);
 
 /* Execute the command lines prevously parsed.
    Returns 0 for success, non-zero otherwise.  */
-- 
cgit v1.2.3