Merge "toolbox-ls: sort file and directory lists"
diff --git a/liblinenoise/linenoise.c b/liblinenoise/linenoise.c
index ab57ae6..4f6775c 100644
--- a/liblinenoise/linenoise.c
+++ b/liblinenoise/linenoise.c
@@ -138,8 +138,8 @@
      * We want read to return every single byte, without timeout. */
     raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
 
-    /* put terminal in raw mode after flushing */
-    if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
+    /* put terminal in raw mode */
+    if (tcsetattr(fd,TCSADRAIN,&raw) < 0) goto fatal;
     rawmode = 1;
     return 0;
 
@@ -150,7 +150,7 @@
 
 static void disableRawMode(int fd) {
     /* Don't even check the return value as it's too late. */
-    if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
+    if (rawmode && tcsetattr(fd,TCSADRAIN,&orig_termios) != -1)
         rawmode = 0;
 }
 
@@ -163,16 +163,30 @@
 static int getColumns(void) {
     struct winsize ws;
 
-    if (ioctl(1, TIOCGWINSZ, &ws) == -1) return 80;
+    if (ioctl(1, TIOCGWINSZ, &ws) == -1) return 4096;
     if (ws.ws_col == 0) {
-        return 80;
+        return 4096;
     }
     return ws.ws_col;
 }
 
+static int effectiveLen(const char* prompt) {
+    int col = 0;
+    char c;
+    // TODO: Handle escape sequences.
+    while ( (c = *prompt++) != 0 ) {
+        if (c == '\n') {
+            col = 0;
+        } else {
+            col++;
+        }
+    }
+    return col;
+}
+
 static void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_t pos, size_t cols) {
     char seq[64];
-    size_t plen = strlen(prompt);
+    size_t plen = effectiveLen(prompt);
     
     while((plen+pos) >= cols) {
         buf++;
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 925347d..b0aa5f2 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -41,7 +41,7 @@
 # Create cgroup mount points for process groups
     mkdir /dev/cpuctl
     mount cgroup none /dev/cpuctl cpu
-    chown sytem system /dev/cpuctl
+    chown system system /dev/cpuctl
     chown system system /dev/cpuctl/tasks
     chmod 0777 /dev/cpuctl/tasks
     write /dev/cpuctl/cpu.shares 1024
diff --git a/sh/input.c b/sh/input.c
index bfb80f4..056ee8b 100644
--- a/sh/input.c
+++ b/sh/input.c
@@ -175,6 +175,9 @@
 	return pgetc_macro();
 }
 
+int in_interactive_mode() {
+    return parsefile != NULL && parsefile->fd == 0;
+}
 
 static int
 preadfd(void)
diff --git a/sh/input.h b/sh/input.h
index a9d3a12..99c1b77 100644
--- a/sh/input.h
+++ b/sh/input.h
@@ -46,6 +46,7 @@
 extern char *parsenextc;	/* next character in input buffer */
 extern int init_editline;	/* 0 == not setup, 1 == OK, -1 == failed */
 
+int in_interactive_mode();
 char *pfgets(char *, int);
 int pgetc(void);
 int preadbuffer(void);
diff --git a/sh/parser.c b/sh/parser.c
index d956375..faf0268 100644
--- a/sh/parser.c
+++ b/sh/parser.c
@@ -1629,9 +1629,9 @@
 	if (!el)
 #endif
 #ifdef WITH_LINENOISE
-#else
-		out2str(getprompt(NULL));
+        if (! in_interactive_mode() )
 #endif
+		out2str(getprompt(NULL));
 }
 
 /*