Add some other modules.
[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  * $WIZ$ module_name = "readline"
48  * $WIZ$ module_depends = "sprintf"
49  */
50
51
52 #ifndef MWARE_READLINE_H
53 #define MWARE_READLINE_H
54
55 #include <cfg/compiler.h>
56
57 #include <string.h>
58
59 #define HISTORY_SIZE 32
60
61 typedef int (*getc_hook)(void* user_data);
62 typedef void (*putc_hook)(char ch, void* user_data);
63 typedef const char* (*match_hook)(void* user_data, const char* word, int word_len);
64 typedef void (*clear_hook)(void* user_data);
65
66 struct RLContext
67 {
68         getc_hook get;
69         void* get_param;
70
71         putc_hook put;
72         void* put_param;
73
74         match_hook match;
75         void* match_param;
76
77         clear_hook clear;
78         void* clear_param;
79
80         const char* prompt;
81
82         char real_history[HISTORY_SIZE];
83         char* history;
84         size_t history_pos;
85         size_t line_pos;
86 };
87
88 INLINE void rl_init_ctx(struct RLContext *ctx)
89 {
90         memset(ctx, 0, sizeof(*ctx));
91         ctx->history = ctx->real_history;
92 }
93
94 INLINE void rl_clear_history(struct RLContext *ctx)
95 {
96         memset(ctx->real_history, 0, sizeof(ctx->real_history));
97         ctx->history_pos = 0;
98         ctx->history = ctx->real_history;
99 }
100
101 INLINE void rl_sethook_get(struct RLContext* ctx, getc_hook get, void* get_param)
102 { ctx->get = get; ctx->get_param = get_param; }
103
104 INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param)
105 { ctx->put = put; ctx->put_param = put_param; }
106
107 INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param)
108 { ctx->match = match; ctx->match_param = match_param; }
109
110 INLINE void rl_sethook_clear(struct RLContext* ctx, clear_hook clear, void* clear_param)
111 { ctx->clear = clear; ctx->clear_param = clear_param; }
112
113 INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt)
114 { ctx->prompt = prompt; }
115
116 const char* rl_readline(struct RLContext* ctx);
117
118 void rl_refresh(struct RLContext* ctx);
119
120 #endif /* MWARE_READLINE_H */