Exemple #1
0
/* This function is the core of the line editing capability of linenoise.
 * It expects 'fd' to be already in "raw mode" so that every key pressed
 * will be returned ASAP to read().
 *
 * The resulting string is put into 'buf' when the user type enter, or
 * when ctrl+d is typed.
 *
 * The function returns the length of the current buffer. */
static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, char *prompt)
{
    struct linenoiseState l;

    /* Populate the linenoise state that we pass to functions implementing
     * specific editing functionalities. */
    l.ifd = stdin_fd;
    l.ofd = stdout_fd;
    l.buf = buf;
    l.buflen = buflen;
    l.prompt = prompt;
    l.plen = strlen(prompt);
    l.oldpos = l.pos = 0;
    l.len = 0;
    l.cols = getColumns(stdin_fd, stdout_fd);
    l.maxrows = 0;
    l.history_index = 0;

    /* Buffer starts empty. */
    l.buf[0] = '\0';
    l.buflen--; /* Make sure there is always space for the nulterm */

    /* The latest history entry is always our current buffer, that
     * initially is just an empty string. */
    linenoiseHistoryAdd("");

    if (write(l.ofd,prompt,l.plen) == -1) return -1;
    while(1) {
        char c;
        int nread;
        char seq[3];

        nread = read(l.ifd,&c,1);
        if (nread <= 0) return l.len;

        /* Only autocomplete when the callback is set. It returns < 0 when
         * there was an error reading from fd. Otherwise it will return the
         * character that should be handled next. */
        if (c == 9 && completionCallback != NULL) {
            c = completeLine(&l);
            /* Return on errors */
            if (c < 0) return l.len;
            /* Read next character when 0 */
            if (c == 0) continue;
        }

        switch(c) {
        case ENTER:    /* enter */
            history_len--;
            free(history[history_len]);
            if (mlmode) linenoiseEditMoveEnd(&l);
            return (int)l.len;
        case CTRL_C:     /* ctrl-c */
            errno = EAGAIN;
            return -1;
        case BACKSPACE:   /* backspace */
        case 8:     /* ctrl-h */
            linenoiseEditBackspace(&l);
            break;
        case CTRL_D:     /* ctrl-d, remove char at right of cursor, or if the
                            line is empty, act as end-of-file. */
            if (l.len > 0) {
                linenoiseEditDelete(&l);
            } else {
                history_len--;
                free(history[history_len]);
                return -1;
            }
            break;
        case CTRL_T:    /* ctrl-t, swaps current character with previous. */
            if (l.pos > 0 && l.pos < l.len) {
                int aux = buf[l.pos-1];
                buf[l.pos-1] = buf[l.pos];
                buf[l.pos] = aux;
                if (l.pos != l.len-1) l.pos++;
                refreshLine(&l);
            }
            break;
        case CTRL_B:     /* ctrl-b */
            linenoiseEditMoveLeft(&l);
            break;
        case CTRL_F:     /* ctrl-f */
            linenoiseEditMoveRight(&l);
            break;
        case CTRL_P:    /* ctrl-p */
            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
            break;
        case CTRL_N:    /* ctrl-n */
            linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
            break;
        case ESC:    /* escape sequence */
            /* Read the next two bytes representing the escape sequence.
             * Use two calls to handle slow terminals returning the two
             * chars at different times. */
            if (read(l.ifd,seq,1) == -1) break;
            if (read(l.ifd,seq+1,1) == -1) break;

            /* ESC [ sequences. */
            if (seq[0] == '[') {
                if (seq[1] >= '0' && seq[1] <= '9') {
                    /* Extended escape, read additional byte. */
                    if (read(l.ifd,seq+2,1) == -1) break;
                    if (seq[2] == '~') {
                        switch(seq[1]) {
                        case '3': /* Delete key. */
                            linenoiseEditDelete(&l);
                            break;
                        }
                    }
                } else {
                    switch(seq[1]) {
                    case 'A': /* Up */
                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
                        break;
                    case 'B': /* Down */
                        linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
                        break;
                    case 'C': /* Right */
                        linenoiseEditMoveRight(&l);
                        break;
                    case 'D': /* Left */
                        linenoiseEditMoveLeft(&l);
                        break;
                    case 'H': /* Home */
                        linenoiseEditMoveHome(&l);
                        break;
                    case 'F': /* End*/
                        linenoiseEditMoveEnd(&l);
                        break;
                    }
                }
            }

            /* ESC O sequences. */
            else if (seq[0] == 'O') {
                switch(seq[1]) {
                case 'H': /* Home */
                    linenoiseEditMoveHome(&l);
                    break;
                case 'F': /* End*/
                    linenoiseEditMoveEnd(&l);
                    break;
                }
            }
            break;
        default:
            if (linenoiseEditInsert(&l,c)) return -1;
            break;
        case CTRL_U: /* Ctrl+u, delete the whole line. */
            buf[0] = '\0';
            l.pos = l.len = 0;
            refreshLine(&l);
            break;
        case CTRL_K: /* Ctrl+k, delete from current to end of line. */
            buf[l.pos] = '\0';
            l.len = l.pos;
            refreshLine(&l);
            break;
        case CTRL_A: /* Ctrl+a, go to the start of the line */
            linenoiseEditMoveHome(&l);
            break;
        case CTRL_E: /* ctrl+e, go to the end of the line */
            linenoiseEditMoveEnd(&l);
            break;
        case CTRL_L: /* ctrl+l, clear screen */
            linenoiseClearScreen();
            refreshLine(&l);
            break;
        case CTRL_W: /* ctrl+w, delete previous word */
            linenoiseEditDeletePrevWord(&l);
            break;
        }
    }
    return l.len;
}
static int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
{
	struct linenoiseState l = {
		.buf = buf, .buflen = buflen,
		.prompt = prompt, .plen = strlen(prompt),
		.oldpos = 0, .pos = 0,
		.len = 0,
		.cols = 80, .maxrows = 0,
		.history_index = 0,
	};

	/* Buffer starts empty. */
	buf[0] = '\0';
	buflen--; /* Make sure there is always space for the nulterm */

	/* The latest history entry is always our current buffer, that
	 * initially is just an empty string. */
	linenoiseHistoryAdd("");

	serial.puts(prompt);

	while (1) {
		char c;
		char seq[2] = {0};

		c = serial.getc();

		/* Only autocomplete when the callback is set. */
		if (c == 9 && completionCallback != NULL) {
			c = completeLine(&l);

			/* Return on errors */
			if (c < 0) return l.len;

			/* Read next character when 0 */
			if (c == 0) continue;
		}

		switch (c) {
		case ENTER:    /* enter */
			history_len--;
			free(history[history_len]);
			return (int)l.len;

		case CTRL_C:
			return -1;

		case BACKSPACE:   /* backspace */
		case CTRL_H:     /* ctrl-h */
			linenoiseEditBackspace(&l);
			break;

		case CTRL_D:     /* ctrl-d, remove char at right of cursor, or of the
                            line is empty, act as end-of-file. */
			if (l.len > 0) {
				linenoiseEditDelete(&l);

			} else {
				history_len--;
				free(history[history_len]);
				return -1;
			}

			break;

		case CTRL_T:    /* ctrl-t, swaps current character with previous. */
			if (l.pos > 0 && l.pos < l.len) {
				int aux = buf[l.pos - 1];
				buf[l.pos - 1] = buf[l.pos];
				buf[l.pos] = aux;

				if (l.pos != l.len - 1) l.pos++;

				refreshLine(&l);
			}

			break;

		case CTRL_B:     /* ctrl-b */
			linenoiseEditMoveLeft(&l);
			break;

		case CTRL_F:     /* ctrl-f */
			linenoiseEditMoveRight(&l);
			break;

		case CTRL_P:    /* ctrl-p */
			linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
			break;

		case CTRL_N:    /* ctrl-n */
			linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
			break;

		/* escape sequence */
		case ESC:
			seq[0] = serial.getc();
			seq[1] = serial.getc();

			if (seq[0] == ARROW_PREFIX && seq[1] == LEFT_ARROW) {
				/* Left arrow */
				linenoiseEditMoveLeft(&l);

			} else if (seq[0] == ARROW_PREFIX && seq[1] == RIGHT_ARROW) {
				/* Right arrow */
				linenoiseEditMoveRight(&l);

			} else if (seq[0] == ARROW_PREFIX && (seq[1] == UP_ARROW || seq[1] == DOWN_ARROW)) {
				/* Up and Down arrows */
				linenoiseEditHistoryNext(&l,
							 (seq[1] == UP_ARROW) ? LINENOISE_HISTORY_PREV :
							 LINENOISE_HISTORY_NEXT);
			}

			//Here is also a extanded escape!
			break;

		default:
			linenoiseEditInsert(&l, c);
			break;

		case CTRL_U: /* Ctrl+u, delete the whole line. */
			buf[0] = '\0';
			l.pos = l.len = 0;
			refreshLine(&l);
			break;

		case CTRL_K: /* Ctrl+k, delete from current to end of line. */
			buf[l.pos] = '\0';
			l.len = l.pos;
			refreshLine(&l);
			break;

		case CTRL_A: /* Ctrl+a, go to the start of the line */
			l.pos = 0;
			refreshLine(&l);
			break;

		case CTRL_E: /* ctrl+e, go to the end of the line */
			l.pos = l.len;
			refreshLine(&l);
			break;

		case CTRL_L: /* ctrl+l, clear screen */
			linenoiseClearScreen();
			refreshLine(&l);
			break;

		case CTRL_W: /* ctrl+w, delete previous word */
			linenoiseEditDeletePrevWord(&l);
			break;
		}
	}

	return l.len;
}

static int linenoiseRaw(char *buf, size_t buflen, const char *prompt)
{
	int count;

	count = linenoiseEdit(buf, buflen, prompt);
	serial.puts("\n\r");

	return count;
}
Exemple #3
0
void lineedit_delete_word_prev()
{
    linenoiseEditDeletePrevWord(&l);
}