+ switch (c) {
+ case '\n':
+ case '\r':
+ /* Enter */
+ *(command + len++ + 1) = c;
+ xwrite(outputFd, &c, 1);
+ break_out = 1;
+ break;
+ case 1:
+ /* Control-a -- Beginning of line */
+ input_home(outputFd, &cursor);
+ case 2:
+ /* Control-b -- Move back one character */
+ input_backward(outputFd, &cursor);
+ break;
+ case 3:
+ /* Control-c -- leave the current line,
+ * and start over on the next line */
+
+ /* Go to the next line */
+ xwrite(outputFd, "\n", 1);
+
+ /* Rewrite the prompt */
+ xwrite(outputFd, prompt, strlen(prompt));
+
+ /* Reset the command string */
+ memset(command, 0, BUFSIZ);
+ len = cursor = 0;
+
+ break;
+ case 4:
+ /* Control-d -- Delete one character, or exit
+ * if the len=0 and no chars to delete */
+ if (len == 0) {
+ xwrite(outputFd, "exit", 4);
+ clean_up_and_die(0);
+ } else {
+ input_delete(command, outputFd, cursor, &len);
+ }
+ break;
+ case 5:
+ /* Control-e -- End of line */
+ input_end(outputFd, &cursor, len);
+ break;
+ case 6:
+ /* Control-f -- Move forward one character */
+ input_forward(outputFd, &cursor, len);
+ break;
+ case '\b':
+ case DEL:
+ /* Control-h and DEL */
+ input_backspace(command, outputFd, &cursor, &len);
+ break;
+ case '\t':
+#ifdef BB_FEATURE_SH_TAB_COMPLETION
+ input_tab(command, prompt, outputFd, &cursor, &len);
+#endif
+ break;
+ case 14:
+ /* Control-n -- Get next command in history */
+ if (hp && hp->n && hp->n->s) {
+ get_next_history(&hp, command);
+ goto rewrite_line;
+ } else {
+ xwrite(outputFd, "\007", 1);
+ }
+ break;
+ case 16:
+ /* Control-p -- Get previous command from history */
+ if (hp && hp->p) {
+ get_previous_history(&hp, command);
+ goto rewrite_line;
+ } else {
+ xwrite(outputFd, "\007", 1);
+ }
+ break;
+ case ESC:{
+ /* escape sequence follows */
+ if ((ret = read(inputFd, &c, 1)) < 1)
+ return;
+
+ if (c == '[') { /* 91 */
+ if ((ret = read(inputFd, &c, 1)) < 1)
+ return;
+
+ switch (c) {
+ case 'A':
+ /* Up Arrow -- Get previous command from history */
+ if (hp && hp->p) {
+ get_previous_history(&hp, command);
+ goto rewrite_line;
+ } else {
+ xwrite(outputFd, "\007", 1);
+ }
+ break;
+ case 'B':
+ /* Down Arrow -- Get next command in history */
+ if (hp && hp->n && hp->n->s) {
+ get_next_history(&hp, command);
+ goto rewrite_line;
+ } else {
+ xwrite(outputFd, "\007", 1);
+ }
+ break;
+
+ /* Rewrite the line with the selected history item */
+ rewrite_line:
+ /* erase old command from command line */
+ len = strlen(command)-strlen(hp->s);
+
+ while (len>cursor)
+ input_delete(command, outputFd, cursor, &len);
+ while (cursor>0)
+ input_backspace(command, outputFd, &cursor, &len);
+ input_home(outputFd, &cursor);
+
+ /* write new command */
+ strcpy(command, hp->s);
+ len = strlen(hp->s);
+ xwrite(outputFd, command, len);
+ cursor = len;
+ break;
+ case 'C':
+ /* Right Arrow -- Move forward one character */
+ input_forward(outputFd, &cursor, len);
+ break;
+ case 'D':
+ /* Left Arrow -- Move back one character */
+ input_backward(outputFd, &cursor);
+ break;
+ case '3':
+ /* Delete */
+ input_delete(command, outputFd, cursor, &len);
+ break;
+ case '1':
+ /* Home (Ctrl-A) */
+ input_home(outputFd, &cursor);
+ break;
+ case '4':
+ /* End (Ctrl-E) */
+ input_end(outputFd, &cursor, len);
+ break;
+ default:
+ xwrite(outputFd, "\007", 1);
+ }
+ if (c == '1' || c == '3' || c == '4')
+ if ((ret = read(inputFd, &c, 1)) < 1)
+ return; /* read 126 (~) */
+ }
+ if (c == 'O') {
+ /* 79 */
+ if ((ret = read(inputFd, &c, 1)) < 1)
+ return;
+ switch (c) {
+ case 'H':
+ /* Home (xterm) */
+ input_home(outputFd, &cursor);
+ break;
+ case 'F':
+ /* End (xterm) */
+ input_end(outputFd, &cursor, len);
+ break;
+ default:
+ xwrite(outputFd, "\007", 1);
+ }
+ }
+ c = 0;
+ break;
+ }
+
+ default: /* If it's regular input, do the normal thing */
+
+ if (!isprint(c)) { /* Skip non-printable characters */
+ break;
+ }
+
+ if (len >= (BUFSIZ - 2)) /* Need to leave space for enter */
+ break;
+
+ len++;
+
+ if (cursor == (len - 1)) { /* Append if at the end of the line */
+ *(command + cursor) = c;
+ } else { /* Insert otherwise */
+ memmove(command + cursor + 1, command + cursor,
+ len - cursor - 1);
+
+ *(command + cursor) = c;
+
+ for (j = cursor; j < len; j++)
+ xwrite(outputFd, command + j, 1);
+ for (; j > cursor; j--)
+ xwrite(outputFd, "\033[D", 3);
+ }