shell: dont't use malloc on each line

This commit is contained in:
Christian Mehlis 2013-10-29 21:48:26 +01:00
parent 97482a0227
commit bb85cb33d9
2 changed files with 7 additions and 8 deletions

View File

@ -34,6 +34,8 @@ typedef struct shell_t {
void (*put_char)(int);
} shell_t;
#define SHELL_BUFFER_SIZE (127)
/**
* @brief Initialize a shell object
* @param shell Pointer to preallocated shell object

View File

@ -92,9 +92,10 @@ static void print_help(const shell_command_t *command_list)
static void handle_input_line(shell_t *shell, char *line)
{
char line_copy[SHELL_BUFFER_SIZE];
char *saveptr;
char *linedup = strdup(line);
char *command = strtok_r(linedup, " ", &saveptr);
strncpy(line_copy, line, sizeof(line_copy));
char *command = strtok_r(line_copy, " ", &saveptr);
void (*handler)(char *) = NULL;
@ -113,8 +114,6 @@ static void handle_input_line(shell_t *shell, char *line)
}
}
}
free(linedup);
}
static int readline(shell_t *shell, char *buf, size_t size)
@ -155,7 +154,7 @@ static inline void print_prompt(shell_t *shell)
void shell_run(shell_t *shell)
{
char line_buf[255];
char line_buf[SHELL_BUFFER_SIZE];
print_prompt(shell);
@ -163,9 +162,7 @@ void shell_run(shell_t *shell)
int res = readline(shell, line_buf, sizeof(line_buf));
if (!res) {
char *line_copy = strdup(line_buf);
handle_input_line(shell, line_copy);
free(line_copy);
handle_input_line(shell, line_buf);
}
print_prompt(shell);