[BusyBox] Small ASH patch

Jordan Crouse jordanc at censoft.com
Tue Oct 1 17:17:03 UTC 2002


All -

Attached is a small patch to ash.c that adds support for the -n parameter of the builtin 'read' function - this allows one to read N characters instead of a single line.  Normally this wouldn't be an issue, but I found myself wanting it when I was writing a few scripts for a thin client platform - I like it better when you can enter Y or N without having to press ENTER.

Its all #ifdef'ed by CONFIG_ASH_READCHARS, so enable or disable it as you see fit.  It will add about 144 bytes to the default build of ash.o.

Enjoy,
Jordan

--- busybox.old/ash.c	Tue Oct  1 16:47:07 2002
+++ busybox.stable/ash.c	Tue Oct  1 16:58:48 2002
@@ -33,6 +33,13 @@
  */
 
 
+/* Enable better 'read' support.  This enables the -n N option for the 
+   builtin read which will allow you to read N charaters instead of
+   an entire line of data.  This adds about 144 bytes on an x86 system
+*/
+
+#define CONFIG_ASH_READCHARS
+
 /* This enables alias support in ash.  If you want to support things
  * like "alias ls='ls -l'" with ash, enable this.  This is only useful
  * when ash is used as an intractive shell.   This adds about 1.5k */
@@ -106,7 +113,7 @@
 #include <glob.h>
 #endif
 
-#ifdef BB_FEATURE_ASH_JOB_CONTROL
+#if defined(BB_FEATURE_ASH_JOB_CONTROL) || defined(CONFIG_ASH_READCHARS)
 #include <termios.h>
 #endif
 
@@ -7788,16 +7795,34 @@
 	int startword;
 	int status;
 	int i;
+	int tty = isatty(0);
+
+#ifdef CONFIG_ASH_READCHARS
+	int count = -1;
+	struct termios ios;
+#endif
+
+#ifdef CONFIG_ASH_READCHARS
+#define READARGS "p:n:r"
+#else
+#define READARGS "p:r"
+#endif
 
 	rflag = 0;
 	prompt = NULL;
-	while ((i = nextopt("p:r")) != '\0') {
+	while ((i = nextopt(READARGS)) != '\0') {
 		if (i == 'p')
 			prompt = optionarg;
-		else
+		else if (i == 'r')
 			rflag = 1;
+#ifdef CONFIG_ASH_READCHARS
+		else if (i == 'n') {
+		  count = atoi(optionarg);
+		  if (count <= 0) count = -1;
+		}
+#endif		 
 	}
-	if (prompt && isatty(0)) {
+	if (prompt && tty) {
 		out2str(prompt);	/* read without cmdedit */
 		flushall();
 	}
@@ -7809,7 +7834,22 @@
 	startword = 1;
 	backslash = 0;
 	STARTSTACKSTR(p);
+
+#ifdef CONFIG_ASH_READCHARS
+	
+	if (tty) {
+	  tcgetattr(0, &ios);
+	  ios.c_lflag &= ~ICANON;
+
+	  tcsetattr(0, 0, &ios);
+	}
+#endif
+
 	for (;;) {
+#ifdef CONFIG_ASH_READCHARS
+	        /* If we have read enough chars, bail out */
+	        if (!count) break;
+#endif
 		if (read(0, &c, 1) != 1) {
 			status = 1;
 			break;
@@ -7826,8 +7866,9 @@
 			backslash++;
 			continue;
 		}
-		if (c == '\n')
-			break;
+		if (c == '\n') 
+		  break;
+
 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
 			continue;
 		}
@@ -7847,7 +7888,20 @@
 		} else {
 			STPUTC(c, p);
 		}
+
+#ifdef CONFIG_ASH_READCHARS
+		if (count) count--;
+#endif
+
 	}
+#ifdef CONFIG_ASH_READCHARS
+	/* Restore the TTY */
+	if (tty) {
+	  ios.c_lflag |= ICANON;
+	  tcsetattr(0, 0, &ios);
+	}
+#endif
+
 	STACKSTRNUL(p);
 	/* Remove trailing blanks */
 	while (stackblock() <= --p && strchr(ifs, *p) != NULL)







More information about the busybox mailing list