/* Read up from standard input up to the first newline (\n) character, and at most size-1 characters, into the buffer s, with echoing and support for backspace. Returns the number of characters read. You can check whether a full line was read by seeing whether a newline precedes the terminating \0. */ ssize_t readline(char *s, size_t size) { size_t count = 0; while (1) { int c = getc_raw(); switch (c) { case '\r': /* Treat as newline */ case '\n': putc('\n'); goto stop; break; case 127: if (count > 0) { putc('\010'); putc(' '); putc('\010'); count--; } break; default: if (count<size-1) { putc(s[count++]=c); } } } stop: s[count] = '\0'; return count; }
int getc(void) { unsigned char ch; uint64_t start; /* * For 100us we read the characters from the serial driver * into a kfifo. This helps us not to lose characters * in small hardware fifos. */ start = get_time_ns(); while (1) { if (tstc()) { kfifo_putc(console_input_buffer, getc_raw()); start = get_time_ns(); } if (is_timeout(start, 100 * USECOND) && kfifo_len(console_input_buffer)) break; } kfifo_getc(console_input_buffer, &ch); return ch; }
/* Prompt for input, and read from standard input up to the first newline (\n) character, with support for backspace, into dynamically reallocated buffer buf of initial size BUFSIZE. Returns buf */ char *readline(char *prompt) { int c; char *buf; char *newbuf; size_t bufsize = BUFSIZE; size_t count = 0; buf = malloc(bufsize*sizeof(char)); puts(prompt); while (1) { c = getc_raw(); switch (c) { case '\r': /* Treat as newline */ case '\n': putc('\n'); goto stop; break; case 127: /* handle DELelte */ if (count > 0) { putc('\010'); putc(' '); putc('\010'); count--; } break; default: if (count >= bufsize-1) { bufsize *= 2; newbuf = realloc(buf, bufsize*sizeof(char)); if (newbuf == NULL) goto stop; else buf = newbuf; } buf[count] = c; putc(c); count++; } } stop: buf[count] = '\0'; newbuf = realloc(buf, (count+1)*sizeof(char)); if (newbuf == NULL) newbuf = buf; return newbuf; }
/* Read character from standard input, with echoing. Returns a non-negative integer on success, which can be casted to char. */ int getc(void) { char c = getc_raw(); syscall_write(stdout, &c, 1); /* Echo back at user. */ return c; }
/* Read character from standard input, with echoing. Returns a non-negative integer on success, which can be casted to char. */ int getc(void) { char c = getc_raw(); syscall_write(FILEHANDLE_STDOUT, &c, 1); /* Echo back at user. */ return c; }