Merged from external project:
[bertos.git] / bertos / mware / readline.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright (C) 2004 Giovanni Bajo
30  * Copyright (C) 2004 Develer S.r.l. (http://www.develer.com/)
31  * All Rights Reserved.
32  * -->
33  *
34  * \brief Line editing support with history
35  *
36  * This file implements a kernel for line editing through a terminal, with history of the typed lines.
37  * Basic feature of this module:
38  *
39  * \li Abstracted from I/O. The user must provide hooks for getc and putc functions.
40  * \li Basic support for ANSI escape sequences for input of special codes.
41  * \li Support for command name completion (through a hook).
42  *
43  * \version $Id$
44  *
45  * \author Giovanni Bajo <rasky@develer.com>
46  */
47
48
49 #ifndef MWARE_READLINE_H
50 #define MWARE_READLINE_H
51
52 #include <cfg/compiler.h>
53
54 #include <string.h>
55
56 #define HISTORY_SIZE 32
57
58 typedef int (*getc_hook)(void* user_data);
59 typedef void (*putc_hook)(char ch, void* user_data);
60 typedef const char* (*match_hook)(void* user_data, const char* word, int word_len);
61 typedef void (*clear_hook)(void* user_data);
62
63 struct RLContext
64 {
65         getc_hook get;
66         void* get_param;
67
68         putc_hook put;
69         void* put_param;
70
71         match_hook match;
72         void* match_param;
73
74         clear_hook clear;
75         void* clear_param;
76
77         const char* prompt;
78
79         char real_history[HISTORY_SIZE];
80         char* history;
81         size_t history_pos;
82         size_t line_pos;
83 };
84
85 INLINE void rl_init_ctx(struct RLContext *ctx)
86 {
87         memset(ctx, 0, sizeof(*ctx));
88         ctx->history = ctx->real_history;
89 }
90
91 INLINE void rl_clear_history(struct RLContext *ctx)
92 {
93         memset(ctx->real_history, 0, sizeof(ctx->real_history));
94         ctx->history_pos = 0;
95         ctx->history = ctx->real_history;
96 }
97
98 INLINE void rl_sethook_get(struct RLContext* ctx, getc_hook get, void* get_param)
99 { ctx->get = get; ctx->get_param = get_param; }
100
101 INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param)
102 { ctx->put = put; ctx->put_param = put_param; }
103
104 INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param)
105 { ctx->match = match; ctx->match_param = match_param; }
106
107 INLINE void rl_sethook_clear(struct RLContext* ctx, clear_hook clear, void* clear_param)
108 { ctx->clear = clear; ctx->clear_param = clear_param; }
109
110 INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt)
111 { ctx->prompt = prompt; }
112
113 const char* rl_readline(struct RLContext* ctx);
114
115 void rl_refresh(struct RLContext* ctx);
116
117 #endif /* MWARE_READLINE_H */