[syslinux:elflink] ldlinux: Move cmdline processing out of cli.c

syslinux-bot for Matt Fleming matt.fleming at linux.intel.com
Wed Apr 27 14:30:37 PDT 2011


Commit-ID:  9d9b677dd1b3bf235c0ee2dd64d476fd9d85db3a
Gitweb:     http://syslinux.zytor.com/commit/9d9b677dd1b3bf235c0ee2dd64d476fd9d85db3a
Author:     Matt Fleming <matt.fleming at linux.intel.com>
AuthorDate: Wed, 6 Apr 2011 14:37:43 +0100
Committer:  Matt Fleming <matt.fleming at linux.intel.com>
CommitDate: Sat, 16 Apr 2011 14:46:37 +0100

ldlinux: Move cmdline processing out of cli.c

While it makes perfect sense for cli.c to read input from the user and
parse certain characters (Ctrl-R, TABs, etc), once that processing is
done it should be up to the caller to decide what to do with the
expanded cmdline string. For example, the logic to figure out what
type of kernel we're loading (calculated from a kernel's filename
extension as typed on the command-line) is currently in cli.c, and
that is just wrong. That kind of logic belongs in ldlinux.c

So move process_command() from cli.c to ldlinux.c and extend it so
that if the user types a label name at the prompt, we load the kernel
for that label as described in the config file.

Now that we've moved all cli history code into edit_cmdline() it seems
like a good time to turn it on permanently.

Signed-off-by: Matt Fleming <matt.fleming at linux.intel.com>


---
 com32/elflink/ldlinux/cli.c        |   74 +++-----------------------------
 com32/elflink/ldlinux/ldlinux.c    |   84 +++++++++++++++++++++++++++--------
 com32/elflink/ldlinux/readconfig.c |    2 +-
 com32/elflink/modules/cli.h        |    1 -
 com32/elflink/modules/menu.h       |    2 +
 5 files changed, 74 insertions(+), 89 deletions(-)

diff --git a/com32/elflink/ldlinux/cli.c b/com32/elflink/ldlinux/cli.c
index 2f3ef29..3026cac 100644
--- a/com32/elflink/ldlinux/cli.c
+++ b/com32/elflink/ldlinux/cli.c
@@ -445,76 +445,14 @@ const char *edit_cmdline(const char *input, int top /*, int width */ ,
     }
 
     printf("\033[?7h");
-    return ret;
-}
-
-void process_command(const char *cmd, bool history)
-{
-	char **argv = malloc((MAX_COMMAND_ARGS + 1) * sizeof(char *));
-	char *temp_cmd = (char *)malloc(sizeof(char) * (strlen(cmd) + 1));
-	int argc = 1, len_mn;
-	char *crt_arg, *module_name;
-
-	/* return if user only press enter */
-	if (cmd[0] == '\0') {
-		printf("\n");
-		return;
-	}
-	printf("\n");
-
-	if (history) {
-		struct cli_command  *comm;
 
-		comm = malloc(sizeof(struct cli_command));
-		comm->command = malloc(sizeof(char) * (strlen(cmd) + 1));
-		strcpy(comm->command, cmd);
-		list_add(&(comm->list), &cli_history_head);
-	}
-
-	//	dprintf("raw cmd = %s", cmd);
-	strcpy(temp_cmd, cmd);
-	module_name = strtok((char *)cmd, COMMAND_DELIM);
-	len_mn = strlen(module_name);
-
-	if (!strcmp(module_name + len_mn - 4, ".c32")) {
-		if (module_find(module_name) != NULL) {
-			/* make module re-enterable */
-		  //		dprintf("Module %s is already running");
-		}
-		do {
-			argv[0] = module_name;
-			crt_arg = strtok(NULL, COMMAND_DELIM);
-			if (crt_arg != NULL && strlen(crt_arg) > 0) {
-				argv[argc] = crt_arg;
-				argc++;
-			} else
-				break;
-		} while (argc < MAX_COMMAND_ARGS);
-		argv[argc] = NULL;
-		module_load_dependencies(module_name, MODULES_DEP);
-		spawn_load(module_name, (const char **)argv);
-	} else if (!strcmp(module_name + len_mn - 2, ".0")) {
-		execute(cmd, KT_PXE);
-	} else if (!strcmp(module_name + len_mn - 3, ".bs")) {
-	} else if (!strcmp(module_name + len_mn - 4, ".img")) {
-		execute(cmd, KT_FDIMAGE);
-	} else if (!strcmp(module_name + len_mn - 4, ".bin")) {
-	} else if (!strcmp(module_name + len_mn - 4, ".bss")) {
-		execute(cmd, KT_BSS);
-	} else if (!strcmp(module_name + len_mn - 4, ".com")
-	       || !strcmp(module_name + len_mn - 4, ".cbt")) {
-		execute(cmd, KT_COMBOOT);
-	} else if (!strcmp(module_name + len_mn - 4, ".cfg")
-	       || !strcmp(module_name + len_mn - 5, ".conf")
-	       || !strcmp(module_name + len_mn - 7, ".config")) {
-		execute(module_name, KT_CONFIG);
-	}
-	/* use KT_KERNEL as default */
-	else
-		execute(temp_cmd, KT_KERNEL);
+    /* Add the command to the history */
+    comm_counter = malloc(sizeof(struct cli_command));
+    comm_counter->command = malloc(sizeof(char) * (strlen(ret) + 1));
+    strcpy(comm_counter->command, ret);
+    list_add(&(comm_counter->list), &cli_history_head);
 
-	free(argv);
-	free(temp_cmd);
+    return ret;
 }
 
 static int cli_init(void)
diff --git a/com32/elflink/ldlinux/ldlinux.c b/com32/elflink/ldlinux/ldlinux.c
index ea889cc..36eaded 100644
--- a/com32/elflink/ldlinux/ldlinux.c
+++ b/com32/elflink/ldlinux/ldlinux.c
@@ -11,35 +11,60 @@
 
 #include <sys/module.h>
 
-static void enter_cmdline(void)
+/*
+ * Attempt to load a kernel after deciding what type of image it is.
+ *
+ * We only return from this function if something went wrong loading
+ * the the kernel. If we return the caller should call enter_cmdline()
+ * so that the user can help us out.
+ */
+static void load_kernel(const char *kernel)
 {
+	struct menu_entry *me;
+	enum kernel_type type;
 	const char *cmdline;
+	char *kernel_name;
+	int len;
 
-	/* Enter endless command line prompt, should support "exit" */
-	while (1) {
-		cmdline = edit_cmdline("syslinux$", 1, NULL, cat_help_file);
-		if (!cmdline)
-			continue;
-		/* feng: give up the aux check here */
-		//aux = list_entry(cli_history_head.next, typeof(*aux), list);
-		//if (strcmp(aux->command, cmdline)) {
-			process_command(cmdline, true);
-		//}
+	/* Virtual kernel? */
+	me = find_label(kernel);
+	if (me) {
+		/* XXX we don't handle LOCALBOOT yet */
+		execute(me->cmdline, KT_KERNEL);
+		/* We shouldn't return */
+		goto bad_kernel;
 	}
-}
 
-static void load_kernel(void)
-{
-	enum kernel_type type;
-	const char *cmdline;
+	if (!allowimplicit)
+		goto bad_implicit;
+
+	kernel_name = strtok((char *)kernel, COMMAND_DELIM);
+	len = strlen(kernel_name);
 
-	if (defaultlevel == LEVEL_UI)
+	if (!strcmp(kernel_name + len - 4, ".c32")) {
 		type = KT_COM32;
+	} else if (!strcmp(kernel_name + len - 2, ".0")) {
+		type = KT_PXE;
+	} else if (!strcmp(kernel_name + len - 3, ".bs")) {
+		type = KT_BOOT;
+	} else if (!strcmp(kernel_name + len - 4, ".img")) {
+		type = KT_FDIMAGE;
+	} else if (!strcmp(kernel_name + len - 4, ".bin")) {
+		type = KT_BOOT;
+	} else if (!strcmp(kernel_name + len - 4, ".bss")) {
+		type = KT_BSS;
+	} else if (!strcmp(kernel_name + len - 4, ".com")
+	       || !strcmp(kernel_name + len - 4, ".cbt")) {
+		type = KT_COMBOOT;
+	}
+	/* use KT_KERNEL as default */
 	else
 		type = KT_KERNEL;
 
-	execute(default_cmd, type);
+	execute(kernel, type);
 
+bad_implicit:
+bad_kernel:
 	/*
 	 * If we fail to boot the kernel execute the "onerror" command
 	 * line.
@@ -50,6 +75,27 @@ static void load_kernel(void)
 	}
 }
 
+static void enter_cmdline(void)
+{
+	const char *cmdline;
+
+	/* Enter endless command line prompt, should support "exit" */
+	while (1) {
+		cmdline = edit_cmdline("syslinux$", 1, NULL, cat_help_file);
+		if (!cmdline)
+			continue;
+
+		/* return if user only press enter */
+		if (cmdline[0] == '\0') {
+			printf("\n");
+			continue;
+		}
+		printf("\n");
+
+		load_kernel(cmdline);
+	}
+}
+
 static int ldlinux_main(int argc, char **argv)
 {
 	openconsole(&dev_rawcon_r, &dev_ansiserial_w);
@@ -67,7 +113,7 @@ static int ldlinux_main(int argc, char **argv)
 	 */
 	if (defaultlevel || noescape) {
 		if (defaultlevel) {
-			load_kernel();	/* Shouldn't return */
+			load_kernel(default_cmd); /* Shouldn't return */
 		} else {
 			printf("No DEFAULT or UI configuration directive found!\n");
 
diff --git a/com32/elflink/ldlinux/readconfig.c b/com32/elflink/ldlinux/readconfig.c
index 66e84df..d78c015 100644
--- a/com32/elflink/ldlinux/readconfig.c
+++ b/com32/elflink/ldlinux/readconfig.c
@@ -432,7 +432,7 @@ void print_labels(const char *prefix, size_t len)
     }
 }
 
-static struct menu_entry *find_label(const char *str)
+struct menu_entry *find_label(const char *str)
 {
     const char *p;
     struct menu_entry *me;
diff --git a/com32/elflink/modules/cli.h b/com32/elflink/modules/cli.h
index 4ae9caf..513c171 100644
--- a/com32/elflink/modules/cli.h
+++ b/com32/elflink/modules/cli.h
@@ -15,7 +15,6 @@ extern int mygetkey(clock_t timeout);
 extern const char *edit_cmdline(const char *input, int top /*, int width */ ,
 				int (*pDraw_Menu) (int, int, int),
 				void (*show_fkey) (int));
-extern void process_command(const char *cmd, bool history);
 
 extern struct menu *root_menu, *start_menu, *hide_menu, *menu_list, *default_menu;
 #endif
diff --git a/com32/elflink/modules/menu.h b/com32/elflink/modules/menu.h
index 8041fc9..e79fb3e 100644
--- a/com32/elflink/modules/menu.h
+++ b/com32/elflink/modules/menu.h
@@ -204,6 +204,8 @@ extern const int message_base_color;
 extern const char *current_background;
 void set_background(const char *new_background);
 
+extern struct menu_entry *find_label(const char *str);
+
 /* execute.c */
 void execute(const char *cmdline, enum kernel_type type);
 



More information about the Syslinux-commits mailing list