diff mbox

[U-Boot,v3,12/16] main: Use autoconf in command line reading

Message ID 1361895069-7343-13-git-send-email-sjg@chromium.org
State Deferred
Delegated to: Tom Rini
Headers show

Commit Message

Simon Glass Feb. 26, 2013, 4:11 p.m. UTC
Remove #ifdefs in favour of autoconf for this code. This involves removing
a few unnecessary #ifdefs in headers also.

We have two versions of the code - one that handles command line editing and
one that is just a simple implementation. Create a new function called
readline_into_buffer() which calls either cread_line() or the new
simple_readline(), created to hold the 'simple' code.

The cread_print_hist_list() function is not actually used anywhere, so punt
it.

Signed-off-by: Simon Glass <sjg@chromium.org>
---
Changes in v3: None
Changes in v2: None

 common/main.c     | 164 ++++++++++++++++++++++++------------------------------
 include/command.h |   2 -
 include/common.h  |   2 -
 3 files changed, 73 insertions(+), 95 deletions(-)

Comments

Joe Hershberger Feb. 27, 2013, 9:17 a.m. UTC | #1
Hi Simon,

On Tue, Feb 26, 2013 at 10:11 AM, Simon Glass <sjg@chromium.org> wrote:
> Remove #ifdefs in favour of autoconf for this code. This involves removing
> a few unnecessary #ifdefs in headers also.
>
> We have two versions of the code - one that handles command line editing and
> one that is just a simple implementation. Create a new function called
> readline_into_buffer() which calls either cread_line() or the new
> simple_readline(), created to hold the 'simple' code.
>
> The cread_print_hist_list() function is not actually used anywhere, so punt
> it.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> Changes in v3: None
> Changes in v2: None
>
>  common/main.c     | 164 ++++++++++++++++++++++++------------------------------
>  include/command.h |   2 -
>  include/common.h  |   2 -
>  3 files changed, 73 insertions(+), 95 deletions(-)
>

Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>
diff mbox

Patch

diff --git a/common/main.c b/common/main.c
index 074ba60..66b74b6 100644
--- a/common/main.c
+++ b/common/main.c
@@ -512,8 +512,6 @@  void reset_cmd_timeout(void)
 }
 #endif
 
-#ifdef CONFIG_CMDLINE_EDITING
-
 /*
  * cmdline-editing related codes from vivi.
  * Author: Janghoon Lyu <nandy@mizi.com>
@@ -616,27 +614,6 @@  static char* hist_next(void)
 	return (ret);
 }
 
-#ifndef CONFIG_CMDLINE_EDITING
-static void cread_print_hist_list(void)
-{
-	int i;
-	unsigned long n;
-
-	n = hist_num - hist_max;
-
-	i = hist_add_idx + 1;
-	while (1) {
-		if (i > hist_max)
-			i = 0;
-		if (i == hist_add_idx)
-			break;
-		printf("%s\n", hist_list[i]);
-		n++;
-		i++;
-	}
-}
-#endif /* CONFIG_CMDLINE_EDITING */
-
 #define BEGINNING_OF_LINE() {			\
 	while (num) {				\
 		getcmd_putch(CTL_BACKSPACE);	\
@@ -898,27 +875,27 @@  static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 			REFRESH_TO_EOL();
 			continue;
 		}
-#ifdef CONFIG_AUTO_COMPLETE
-		case '\t': {
-			int num2, col;
+		case '\t':
+			if (autoconf_auto_complete()) {
+				int num2, col;
 
-			/* do not autocomplete when in the middle */
-			if (num < eol_num) {
-				getcmd_cbeep();
-				break;
-			}
+				/* do not autocomplete when in the middle */
+				if (num < eol_num) {
+					getcmd_cbeep();
+					break;
+				}
 
-			buf[num] = '\0';
-			col = strlen(prompt) + eol_num;
-			num2 = num;
-			if (cmd_auto_complete(prompt, buf, &num2, &col)) {
-				col = num2 - num;
-				num += col;
-				eol_num += col;
+				buf[num] = '\0';
+				col = strlen(prompt) + eol_num;
+				num2 = num;
+				if (cmd_auto_complete(prompt, buf, &num2,
+						&col)) {
+					col = num2 - num;
+					num += col;
+					eol_num += col;
+				}
+				break;
 			}
-			break;
-		}
-#endif
 		default:
 			cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
 			break;
@@ -934,8 +911,6 @@  static int cread_line(const char *const prompt, char *buf, unsigned int *len,
 	return 0;
 }
 
-#endif /* CONFIG_CMDLINE_EDITING */
-
 /****************************************************************************/
 
 /*
@@ -957,46 +932,14 @@  int readline (const char *const prompt)
 	return readline_into_buffer(prompt, console_buffer, 0);
 }
 
-
-int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
+static int simple_readline(const char *const prompt, int plen, char *p,
+			   int timeout)
 {
-	char *p = buffer;
-#ifdef CONFIG_CMDLINE_EDITING
-	unsigned int len = CONFIG_SYS_CBSIZE;
-	int rc;
-	static int initted = 0;
-
-	/*
-	 * History uses a global array which is not
-	 * writable until after relocation to RAM.
-	 * Revert to non-history version if still
-	 * running from flash.
-	 */
-	if (gd->flags & GD_FLG_RELOC) {
-		if (!initted) {
-			hist_init();
-			initted = 1;
-		}
-
-		if (prompt)
-			puts (prompt);
-
-		rc = cread_line(prompt, p, &len, timeout);
-		return rc < 0 ? rc : len;
-
-	} else {
-#endif	/* CONFIG_CMDLINE_EDITING */
 	char * p_buf = p;
 	int	n = 0;				/* buffer index		*/
-	int	plen = 0;			/* prompt length	*/
 	int	col;				/* output column cnt	*/
 	char	c;
 
-	/* print prompt */
-	if (prompt) {
-		plen = strlen (prompt);
-		puts (prompt);
-	}
 	col = plen;
 
 	for (;;) {
@@ -1009,12 +952,12 @@  int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
 		}
 		WATCHDOG_RESET();	/* Trigger watchdog, if needed */
 
-#ifdef CONFIG_SHOW_ACTIVITY
-		while (!tstc()) {
-			show_activity(0);
-			WATCHDOG_RESET();
+		if (autoconf_show_activity()) {
+			while (!tstc()) {
+				show_activity(0);
+				WATCHDOG_RESET();
+			}
 		}
-#endif
 		c = getc();
 
 		/*
@@ -1061,14 +1004,20 @@  int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
 			 */
 			if (n < CONFIG_SYS_CBSIZE-2) {
 				if (c == '\t') {	/* expand TABs */
-#ifdef CONFIG_AUTO_COMPLETE
-					/* if auto completion triggered just continue */
-					*p = '\0';
-					if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
-						p = p_buf + n;	/* reset */
-						continue;
+					if (autoconf_auto_complete()) {
+						/*
+						 * if auto completion triggered
+						 * just continue
+						 */
+						*p = '\0';
+						if (cmd_auto_complete(prompt,
+								console_buffer,
+								&n, &col)) {
+							/* reset */
+							p = p_buf + n;
+							continue;
+						}
 					}
-#endif
 					puts (tab_seq+(col&07));
 					col += 8 - (col&07);
 				} else {
@@ -1090,9 +1039,42 @@  int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
 			}
 		}
 	}
-#ifdef CONFIG_CMDLINE_EDITING
+}
+
+int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
+{
+	unsigned int len = CONFIG_SYS_CBSIZE;
+	int rc;
+	static int initted;
+
+	/*
+	 * History uses a global array which is not
+	 * writable until after relocation to RAM.
+	 * Revert to non-history version if still
+	 * running from flash.
+	 */
+	if (autoconf_cmdline_editing() && (gd->flags & GD_FLG_RELOC)) {
+		if (!initted) {
+			hist_init();
+			initted = 1;
+		}
+
+		if (prompt)
+			puts(prompt);
+
+		rc = cread_line(prompt, buffer, &len, timeout);
+		return rc < 0 ? rc : len;
+
+	} else {
+		int plen = 0;			/* prompt length */
+
+		/* print prompt */
+		if (prompt) {
+			plen = strlen(prompt);
+			puts(prompt);
+		}
+		return simple_readline(prompt, plen, buffer, timeout);
 	}
-#endif
 }
 
 /****************************************************************************/
diff --git a/include/command.h b/include/command.h
index 3785eb9..80da938 100644
--- a/include/command.h
+++ b/include/command.h
@@ -75,10 +75,8 @@  cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
 
 extern int cmd_usage(const cmd_tbl_t *cmdtp);
 
-#ifdef CONFIG_AUTO_COMPLETE
 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
-#endif
 
 /*
  * Monitor Command
diff --git a/include/common.h b/include/common.h
index fb219fd..1457349 100644
--- a/include/common.h
+++ b/include/common.h
@@ -857,9 +857,7 @@  int	pcmcia_init (void);
 
 #include <bootstage.h>
 
-#ifdef CONFIG_SHOW_ACTIVITY
 void show_activity(int arg);
-#endif
 
 /* Multicore arch functions */
 #ifdef CONFIG_MP