2 * This file is part of the libpayload project.
4 * Copyright (C) 2008 coresystems GmbH
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @file libc/readline.c
32 * Simple readline implementation
35 #include <libpayload.h>
37 static char *readline_buffer;
38 static int readline_bufferlen;
41 * Read a line from the terminal and return it.
43 * This readline implementation is rather simple, but it does more than the
44 * original readline() because it allows us to have a pre-filled buffer.
45 * To pre-fill the buffer, use the getline() function.
47 * @param prompt A prompt to display on the line.
48 * @return A pointer to the input string.
50 char *readline(const char *prompt)
53 int current, ch, nonspace_seen;
55 if (!readline_buffer || !readline_bufferlen) {
56 #define READLINE_BUFFERSIZE 256
57 readline_buffer = malloc(READLINE_BUFFERSIZE);
60 readline_bufferlen = READLINE_BUFFERSIZE;
61 memset(readline_buffer, 0, readline_bufferlen);
64 buffer = readline_buffer;
69 while (prompt[current]) {
70 putchar(prompt[current]);
75 /* print existing buffer, if there is one */
77 while (buffer[current]) {
78 putchar(buffer[current]);
100 case 'W' & 0x1f: /* CTRL-W */
104 if (buffer[current - 1] != ' ')
110 if (nonspace_seen && (current < readline_bufferlen - 1)
111 && (current > 0) && (buffer[current - 1] == ' '))
115 case 'U' & 0x1f: /* CTRL-U */
126 /* all other characters */
128 /* ignore control characters */
132 /* ignore unprintables */
136 if (current + 1 < readline_bufferlen) {
137 /* print new character */
139 /* and add it to the array */
140 buffer[current] = ch;
147 if (current >= readline_bufferlen)
148 current = readline_bufferlen - 1;
149 buffer[current] = '\0';
155 * Read a line from the input and store it in a buffer.
157 * This function allows the user to pass a predefined buffer to readline().
158 * The buffer may be filled with a default value which will be displayed by
159 * readline() and can be edited as normal.
160 * The final input string returned by readline() will be returned in
161 * the buffer and the function will return the length of the string.
163 * @param buffer Pointer to a buffer to store the line in.
164 * @param len Length of the buffer.
165 * @return The final length of the string.
167 int getline(char *buffer, int len)
169 readline_buffer = buffer;
170 readline_bufferlen = len;
173 return strlen(buffer);