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