Added utilities for protocols
[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.1  2006/06/01 12:27:39  marco
26  *#* Added utilities for protocols
27  *#*
28  *#*/
29
30 #ifndef MWARE_READLINE_H
31 #define MWARE_READLINE_H
32
33 #include <cfg/compiler.h>
34
35 #include <string.h>
36
37 #define HISTORY_SIZE       1024
38
39 typedef int (*getc_hook)(void* user_data);
40 typedef void (*putc_hook)(char ch, void* user_data);
41 typedef const char* (*match_hook)(void* user_data, const char* word, int word_len);
42
43 struct RLContext
44 {
45         getc_hook get;
46         void* get_param;
47
48         putc_hook put;
49         void* put_param;
50
51         match_hook match;
52         void* match_param;
53
54         const char* prompt;
55
56         char real_history[HISTORY_SIZE];
57         char* history;
58         size_t history_pos;
59 };
60
61 INLINE void rl_init_ctx(struct RLContext *ctx)
62 {
63         memset(ctx, 0, sizeof(*ctx));
64         ctx->history = ctx->real_history;
65 }
66
67 INLINE void rl_clear_history(struct RLContext *ctx)
68 {
69         memset(ctx->real_history, 0, sizeof(ctx->real_history));
70         ctx->history_pos = 0;
71         ctx->history = ctx->real_history;
72 }
73
74 INLINE void rl_sethook_get(struct RLContext* ctx, getc_hook get, void* get_param)
75 { ctx->get = get; ctx->get_param = get_param; }
76
77 INLINE void rl_sethook_put(struct RLContext* ctx, putc_hook put, void* put_param)
78 { ctx->put = put; ctx->put_param = put_param; }
79
80 INLINE void rl_sethook_match(struct RLContext* ctx, match_hook match, void* match_param)
81 { ctx->match = match; ctx->match_param = match_param; }
82
83 INLINE void rl_setprompt(struct RLContext* ctx, const char* prompt)
84 { ctx->prompt = prompt; }
85
86 const char* rl_readline(struct RLContext* ctx);
87
88 void rl_refresh(struct RLContext* ctx);
89
90 #endif /* MWARE_READLINE_H */