/*! * @brief read line from consult */ static int readline (char * prompt) { uint8_t len = SHELL_CB_SIZE; int8_t rc; char * p = console_buffer; static uint8_t initted = 0; /* break console_buffer so that is will not repeatable */ console_buffer[0] = '\0'; if (!initted) { #ifdef SHELL_CONFIG_USE_HIST hist_init(); #endif initted = 1; printf("\r\nSHELL (build: %s)\r\n", __DATE__); printf("Copyright (c) 2013 http://www.beyondcore.net\r\n"); } if (prompt) { putstr (prompt); } rc = cread_line(prompt, p, &len); return rc < 0 ? rc : len; }
int ubreadline_into_buffer(const char *const prompt, char *buffer, int timeout) { char *p = buffer; 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 (0) { if (!initted) { hist_init(); initted = 1; } if (prompt) printf (prompt); rc = cread_line(prompt, p, &len, timeout); return rc < 0 ? rc : len; } else { 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); printf (prompt); } col = plen; for (;;) { c = serial_getchar(); /* * Special character handling */ switch (c) { case '\r': /* Enter */ case '\n': *p = '\0'; printf ("\r\n"); return p - p_buf; case '\0': /* nul */ continue; case 0x03: /* ^C - break */ p_buf[0] = '\0'; /* discard input */ return -1; case 0x15: /* ^U - erase line */ while (col > plen) { printf (erase_seq); --col; } p = p_buf; n = 0; continue; case 0x17: /* ^W - erase word */ p=delete_char(p_buf, p, &col, &n, plen); while ((n > 0) && (*p != ' ')) { p=delete_char(p_buf, p, &col, &n, plen); } continue; case 0x08: /* ^H - backspace */ case 0x7F: /* DEL - backspace */ p=delete_char(p_buf, p, &col, &n, plen); continue; default: /* * Must be a normal character then */ if (n < CONFIG_SYS_CBSIZE-2) { if (c == '\t') { /* expand TABs */ /* if auto completion triggered just continue */ *p = '\0'; if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { p = p_buf + n; /* reset */ continue; } printf (tab_seq+(col&07)); col += 8 - (col&07); } else { char buf[2]; /* * Echo input using printf() to force an * LCD flush if we are using an LCD */ ++col; buf[0] = c; buf[1] = '\0'; printf(buf); } *p++ = c; ++n; } else { /* Buffer full */ serial_putchar('\a'); } } } } }