Make interactive output go to the terminal (bug #17213), by
[pspp-builds.git] / src / ui / terminal / read-line.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "read-line.h"
20
21 #include <stdlib.h>
22 #include <stdbool.h>
23 #include <assert.h>
24 #include <errno.h>
25
26 #include "msg-ui.h"
27
28 #include <data/file-name.h>
29 #include <data/settings.h>
30 #include <language/command.h>
31 #include <libpspp/message.h>
32 #include <libpspp/str.h>
33 #include <libpspp/version.h>
34 #include <language/prompt.h>
35 #include <output/manager.h>
36
37 #include "xalloc.h"
38
39 #include "gettext.h"
40 #define _(msgid) gettext (msgid)
41
42 #if HAVE_READLINE
43 #include <readline/readline.h>
44 #include <readline/history.h>
45
46 static char *history_file;
47
48 static char **complete_command_name (const char *, int, int);
49 static char **dont_complete (const char *, int, int);
50 #endif /* HAVE_READLINE */
51
52
53 struct readln_source
54 {
55   struct getl_interface parent ;
56
57   bool (*interactive_func) (struct string *line,
58                             enum prompt_style) ;
59 };
60
61
62 static bool initialised = false;
63
64 /* Initialize getl. */
65 void
66 readln_initialize (void)
67 {
68   initialised = true;
69
70 #if HAVE_READLINE
71   rl_basic_word_break_characters = "\n";
72   using_history ();
73   stifle_history (500);
74   if (history_file == NULL)
75     {
76       const char *home_dir = getenv ("HOME");
77       if (home_dir != NULL)
78         {
79           history_file = xasprintf ("%s/.pspp_history", home_dir);
80           read_history (history_file);
81         }
82     }
83 #endif
84 }
85
86 /* Close getl. */
87 void
88 readln_uninitialize (void)
89 {
90   initialised = false;
91
92 #if HAVE_READLINE
93   if (history_file != NULL && false == get_testing_mode() )
94     write_history (history_file);
95   clear_history ();
96   free (history_file);
97 #endif
98 }
99
100
101 static bool
102 read_interactive (struct getl_interface *s,
103                   struct string *line, enum getl_syntax *syntax)
104 {
105   struct readln_source *is  =
106     (struct readln_source *) s ;
107
108   *syntax = GETL_INTERACTIVE;
109   return is->interactive_func (line, prompt_get_style ());
110 }
111
112 static bool
113 always_true (const struct getl_interface *s UNUSED)
114 {
115   return true;
116 }
117
118 /* Display a welcoming message. */
119 static void
120 welcome (void)
121 {
122   static bool welcomed = false;
123   if (welcomed)
124     return;
125   welcomed = true;
126   fputs ("PSPP is free software and you are welcome to distribute copies of "
127          "it\nunder certain conditions; type \"show copying.\" to see the "
128          "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
129          "warranty.\" for details.\n", stdout);
130   puts (stat_version);
131   readln_initialize ();
132 }
133
134 /* Gets a line from the user and stores it into LINE.
135    Prompts the user with PROMPT.
136    Returns true if successful, false at end of file.
137    */
138 static bool
139 readln_read (struct string *line, enum prompt_style style)
140 {
141   const char *prompt = prompt_get (style);
142 #if HAVE_READLINE
143   char *string;
144 #endif
145
146   assert (initialised);
147
148   reset_msg_count ();
149
150   welcome ();
151
152   if (style == PROMPT_FIRST)
153     som_flush ();
154
155 #if HAVE_READLINE
156   rl_attempted_completion_function = (style == PROMPT_FIRST
157                                       ? complete_command_name
158                                       : dont_complete);
159   string = readline (prompt);
160   if (string == NULL)
161     return false;
162   else
163     {
164       if (string[0])
165         add_history (string);
166       ds_assign_cstr (line, string);
167       free (string);
168       return true;
169     }
170 #else
171   fputs (prompt, stdout);
172   fflush (stdout);
173   if (ds_read_line (line, stdin))
174     {
175       ds_chomp (line, '\n');
176       return true;
177     }
178   else
179     return false;
180 #endif
181 }
182
183
184 static void
185 readln_close (struct getl_interface *i)
186 {
187   free (i);
188 }
189
190 /* Creates a source which uses readln to get its line */
191 struct getl_interface *
192 create_readln_source (void)
193 {
194   struct readln_source *rlns  = xzalloc (sizeof (*rlns));
195
196   rlns->interactive_func = readln_read;
197
198   rlns->parent.interactive = always_true;
199   rlns->parent.read = read_interactive;
200   rlns->parent.close = readln_close;
201
202   return (struct getl_interface *) rlns;
203 }
204
205
206 #if HAVE_READLINE
207 static char *command_generator (const char *text, int state);
208
209 /* Returns a set of command name completions for TEXT.
210    This is of the proper form for assigning to
211    rl_attempted_completion_function. */
212 static char **
213 complete_command_name (const char *text, int start, int end UNUSED)
214 {
215   if (start == 0)
216     {
217       /* Complete command name at start of line. */
218       return rl_completion_matches (text, command_generator);
219     }
220   else
221     {
222       /* Otherwise don't do any completion. */
223       rl_attempted_completion_over = 1;
224       return NULL;
225     }
226 }
227
228 /* Do not do any completion for TEXT. */
229 static char **
230 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
231 {
232   rl_attempted_completion_over = 1;
233   return NULL;
234 }
235
236 /* If STATE is 0, returns the first command name matching TEXT.
237    Otherwise, returns the next command name matching TEXT.
238    Returns a null pointer when no matches are left. */
239 static char *
240 command_generator (const char *text, int state)
241 {
242   static const struct command *cmd;
243   const char *name;
244
245   if (state == 0)
246     cmd = NULL;
247   name = cmd_complete (text, &cmd);
248   return name ? xstrdup (name) : NULL;
249 }
250 #endif /* HAVE_READLINE */