56a9ce28c8de0224682293d5569c1691b7889994
[gnupg.git] / common / gpgrlhelp.c
1 /* gpgrlhelp.c - A readline wrapper.
2  *      Copyright (C) 2006 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG 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  * GnuPG 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 Street, Fifth Floor, Boston, MA 02110-1301,
19  * USA.
20  */
21
22 /* This module may by used by applications to initializes readline
23    support.  It is required so that we can have hooks in other parts
24    of libcommon without actually requing to link against
25    libreadline. It works along ttyio.c which a proper part of
26    libcommon. */
27
28 #include <config.h>
29 #include <stdlib.h>
30 #include <stddef.h>
31
32 #ifdef HAVE_LIBREADLINE
33 #define GNUPG_LIBREADLINE_H_INCLUDED
34 #include <stdio.h>
35 #include <readline/readline.h>
36 #include <readline/history.h>
37 #endif
38
39 #include "util.h"
40 #include "common-defs.h"
41
42
43 static void
44 set_completer (rl_completion_func_t *completer)
45 {
46   rl_attempted_completion_function = completer;
47   rl_inhibit_completion = 0;
48 }
49
50 static void
51 inhibit_completion (int value)
52 {
53   rl_inhibit_completion = value;
54 }
55
56 static void
57 cleanup_after_signal (void)
58 {
59   rl_free_line_state ();
60   rl_cleanup_after_signal ();
61 }
62
63 static void
64 init_stream (FILE *fp)
65 {
66   rl_catch_signals = 0;
67   rl_instream = rl_outstream = fp;
68   rl_inhibit_completion = 1;
69 }
70
71
72 /* Initialize our readline code.  This should be called as early as
73    possible as it is actually a constructur.  */
74 void
75 gnupg_rl_initialize (void)
76 {
77   tty_private_set_rl_hooks (init_stream,
78                             set_completer,
79                             inhibit_completion,
80                             cleanup_after_signal,
81                             readline,
82                             add_history);
83   rl_readline_name = "GnuPG";
84
85 }
86
87
88
89