a46ad513230c0249b0033e41e3ba3a680b759bc1
[bertos.git] / mware / readline.h
1 /**
2  * \file
3  * <!--
4  * Copyright (C) 2004 Giovanni Bajo
5  * Copyright (C) 2004 Develer S.r.l. (http://www.develer.com/)
6  * All Rights Reserved.
7  * -->
8  *
9  * \brief Line editing support with history
10  *
11  * This file implements a kernel for line editing through a terminal, with history of the typed lines.
12  * Basic feature of this module:
13  *
14  * \li Abstracted from I/O. The user must provide hooks for getc and putc functions.
15  * \li Basic support for ANSI escape sequences for input of special codes.
16  * \li Support for command name completion (through a hook).
17  *
18  * \version $Id$
19  *
20  * \author Giovanni Bajo <rasky@develer.com>
21  */
22
23 /*#*
24  *#* $Log$
25  *#* Revision 1.2  2006/07/19 12:56:28  bernie
26  *#* Convert to new Doxygen style.
27  *#*
28  *#* Revision 1.1  2006/06/01 12:27:39  marco
29  *#* Added utilities for protocols
30  *#*
31  *#*/
32
33 #ifndef MWARE_READLINE_H
34 #define MWARE_READLINE_H
35
36 #include <cfg/compiler.h>
37
38 #include <string.h>
39
40 #define HISTORY_SIZE       1024
41
42 typedef int (*getc_hook)(void* user_data);
43 typedef void (*putc_hook)(char ch, void* user_data);
44 typedef const char* (*match_hook)(void* user_data, const char* word, int word_len);
45
46 struct RLContext
47 {
48         getc_hook get;
49         void* get_param;
50
51         putc_hook put;
52         void* put_param;
53
54         match_hook match;
55         void* match_param;
56
57         const char* prompt;
58
59         char real_history[HISTORY_SIZE];
60         char* history;
61         size_t history_pos;
62 };
63
64 INLINE void rl_init_ctx(struct RLContext *ctx)
65 {
66         memset(ctx, 0, sizeof(*ctx));
67         ctx->history = ctx->real_history;
68 }
69
70 INLINE void rl_clear_history(struct RLContext *ctx)
71 {
72         memset(ctx->real_history, 0, sizeof(ctx->real_history));
73         ctx->history_pos = 0;
74         ctx->history = ctx->real_history;
75 }
76
77 INLINE void rl_sethook_get(struct RLContext* ctx, getc_hook get, void* get_param)
78 { ctx->get = get; ctx->get_param = get_param; }
79
80 INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param)
81 { ctx->put = put; ctx->put_param = put_param; }
82
83 INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param)
84 { ctx->match = match; ctx->match_param = match_param; }
85
86 INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt)
87 { ctx->prompt = prompt; }
88
89 const char* rl_readline(struct RLContext* ctx);
90
91 void rl_refresh(struct RLContext* ctx);
92
93 #endif /* MWARE_READLINE_H */