函数功能:提示用户输入命令,并读取命令
/****************************************************************************//*
* Prompt for input and read a line. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, * time out when time goes past endtime (timebase time in ticks). * Return: number of read characters * -1 if break * -2 if timed out */int readline (const char *const prompt){ #ifdef CONFIG_CMDLINE_EDITING char *p = console_buffer; unsigned int len=MAX_CMDBUF_SIZE; int rc; static int initted = 0;if (!initted) {
hist_init(); initted = 1; }puts (prompt);
rc = cread_line(p, &len);
return rc < 0 ? rc : len;#else char *p = console_buffer; // console_buffer是作为读取命令的缓冲区 int n = 0; /* buffer index */ int plen = 0; /* prompt length */ int col; /* output column cnt */ char c;/* print prompt */
if (prompt) { //如果提示符非零,就输出提示符 plen = strlen (prompt); puts (prompt); } col = plen;for (;;) {
#ifdef CONFIG_BOOT_RETRY_TIME while (!tstc()) { /* while no incoming data */ if (retry_time >= 0 && get_ticks() > endtime) return (-2); /* timed out */ }#endif WATCHDOG_RESET(); /* Trigger watchdog, if needed */#ifdef CONFIG_SHOW_ACTIVITY
while (!tstc()) { extern void show_activity(int arg); show_activity(0); }#endif c = getc(); //从串口读取一个字符/*
* Special character handling */ switch (c) { case '\r': /* Enter */ case '\n': *p = '\0'; puts ("\r\n"); return (p - console_buffer);//返回输入命令的长度case '\0': /* nul */
continue;case 0x03: /* ^C - break */ //终止输入
console_buffer[0] = '\0'; /* discard input */ return (-1);case 0x15: /* ^U - erase line */
while (col > plen) { puts (erase_seq); --col; } p = console_buffer; n = 0; continue;case 0x17: /* ^W - erase word */
p=delete_char(console_buffer, p, &col, &n, plen); while ((n > 0) && (*p != ' ')) { p=delete_char(console_buffer, p, &col, &n, plen); } continue;case 0x08: /* ^H - backspace */
case 0x7F: /* DEL - backspace */ //删除操作 p=delete_char(console_buffer, p, &col, &n, plen); continue;default:
/* * Must be a normal character then */ if (n < CFG_CBSIZE-2) { if (c == '\t') { /* expand TABs */#ifdef CONFIG_AUTO_COMPLETE //自动补全功能 /* if auto completion triggered just continue */ *p = '\0'; if (cmd_auto_complete(prompt, console_buffer, &n, &col)) { p = console_buffer + n; /* reset */ continue; }#endif puts (tab_seq+(col&07)); col += 8 - (col&07); } else { ++col; /* echo input */ //打印出输入的命令 putc (c); } *p++ = c; /*把字符保存在buffer中*/ ++n; } else { /* Buffer full */ putc ('\a'); } } }#endif /* CONFIG_CMDLINE_EDITING */}参考: