Updated copiright notice.
[bertos.git] / 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  *#* $Log$
50  *#* Revision 1.2  2006/07/19 12:56:28  bernie
51  *#* Convert to new Doxygen style.
52  *#*
53  *#* Revision 1.1  2006/06/01 12:27:39  marco
54  *#* Added utilities for protocols
55  *#*
56  *#*/
57
58 #ifndef MWARE_READLINE_H
59 #define MWARE_READLINE_H
60
61 #include <cfg/compiler.h>
62
63 #include <string.h>
64
65 #define HISTORY_SIZE       1024
66
67 typedef int (*getc_hook)(void* user_data);
68 typedef void (*putc_hook)(char ch, void* user_data);
69 typedef const char* (*match_hook)(void* user_data, const char* word, int word_len);
70
71 struct RLContext
72 {
73         getc_hook get;
74         void* get_param;
75
76         putc_hook put;
77         void* put_param;
78
79         match_hook match;
80         void* match_param;
81
82         const char* prompt;
83
84         char real_history[HISTORY_SIZE];
85         char* history;
86         size_t history_pos;
87 };
88
89 INLINE void rl_init_ctx(struct RLContext *ctx)
90 {
91         memset(ctx, 0, sizeof(*ctx));
92         ctx->history = ctx->real_history;
93 }
94
95 INLINE void rl_clear_history(struct RLContext *ctx)
96 {
97         memset(ctx->real_history, 0, sizeof(ctx->real_history));
98         ctx->history_pos = 0;
99         ctx->history = ctx->real_history;
100 }
101
102 INLINE void rl_sethook_get(struct RLContext* ctx, getc_hook get, void* get_param)
103 { ctx->get = get; ctx->get_param = get_param; }
104
105 INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param)
106 { ctx->put = put; ctx->put_param = put_param; }
107
108 INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param)
109 { ctx->match = match; ctx->match_param = match_param; }
110
111 INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt)
112 { ctx->prompt = prompt; }
113
114 const char* rl_readline(struct RLContext* ctx);
115
116 void rl_refresh(struct RLContext* ctx);
117
118 #endif /* MWARE_READLINE_H */