Implement journaling. Bug #17240.
[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/journal.h>
36 #include <output/manager.h>
37
38 #include "xalloc.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 #if HAVE_READLINE
44 #include <readline/readline.h>
45 #include <readline/history.h>
46
47 static char *history_file;
48
49 static char **complete_command_name (const char *, int, int);
50 static char **dont_complete (const char *, int, int);
51 #endif /* HAVE_READLINE */
52
53
54 struct readln_source
55 {
56   struct getl_interface parent ;
57
58   bool (*interactive_func) (struct string *line,
59                             enum prompt_style) ;
60 };
61
62
63 static bool initialised = false;
64
65 /* Initialize getl. */
66 void
67 readln_initialize (void)
68 {
69   initialised = true;
70
71 #if HAVE_READLINE
72   rl_basic_word_break_characters = "\n";
73   using_history ();
74   stifle_history (500);
75   if (history_file == NULL)
76     {
77       const char *home_dir = getenv ("HOME");
78       if (home_dir != NULL)
79         {
80           history_file = xasprintf ("%s/.pspp_history", home_dir);
81           read_history (history_file);
82         }
83     }
84 #endif
85 }
86
87 /* Close getl. */
88 void
89 readln_uninitialize (void)
90 {
91   initialised = false;
92
93 #if HAVE_READLINE
94   if (history_file != NULL && false == get_testing_mode() )
95     write_history (history_file);
96   clear_history ();
97   free (history_file);
98 #endif
99 }
100
101
102 static bool
103 read_interactive (struct getl_interface *s,
104                   struct string *line, enum getl_syntax *syntax)
105 {
106   struct readln_source *is  =
107     (struct readln_source *) s ;
108
109   *syntax = GETL_INTERACTIVE;
110   return is->interactive_func (line, prompt_get_style ());
111 }
112
113 static bool
114 always_true (const struct getl_interface *s UNUSED)
115 {
116   return true;
117 }
118
119 /* Display a welcoming message. */
120 static void
121 welcome (void)
122 {
123   static bool welcomed = false;
124   if (welcomed)
125     return;
126   welcomed = true;
127   fputs ("PSPP is free software and you are welcome to distribute copies of "
128          "it\nunder certain conditions; type \"show copying.\" to see the "
129          "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
130          "warranty.\" for details.\n", stdout);
131   puts (stat_version);
132   readln_initialize ();
133   journal_enable ();
134 }
135
136 /* Gets a line from the user and stores it into LINE.
137    Prompts the user with PROMPT.
138    Returns true if successful, false at end of file.
139    */
140 static bool
141 readln_read (struct string *line, enum prompt_style style)
142 {
143   const char *prompt = prompt_get (style);
144 #if HAVE_READLINE
145   char *string;
146 #endif
147
148   assert (initialised);
149
150   reset_msg_count ();
151
152   welcome ();
153
154   if (style == PROMPT_FIRST)
155     som_flush ();
156
157 #if HAVE_READLINE
158   rl_attempted_completion_function = (style == PROMPT_FIRST
159                                       ? complete_command_name
160                                       : dont_complete);
161   string = readline (prompt);
162   if (string == NULL)
163     return false;
164   else
165     {
166       if (string[0])
167         add_history (string);
168       ds_assign_cstr (line, string);
169       free (string);
170       return true;
171     }
172 #else
173   fputs (prompt, stdout);
174   fflush (stdout);
175   if (ds_read_line (line, stdin))
176     {
177       ds_chomp (line, '\n');
178       return true;
179     }
180   else
181     return false;
182 #endif
183 }
184
185
186 static void
187 readln_close (struct getl_interface *i)
188 {
189   free (i);
190 }
191
192 /* Creates a source which uses readln to get its line */
193 struct getl_interface *
194 create_readln_source (void)
195 {
196   struct readln_source *rlns  = xzalloc (sizeof (*rlns));
197
198   rlns->interactive_func = readln_read;
199
200   rlns->parent.interactive = always_true;
201   rlns->parent.read = read_interactive;
202   rlns->parent.close = readln_close;
203
204   return (struct getl_interface *) rlns;
205 }
206
207
208 #if HAVE_READLINE
209 static char *command_generator (const char *text, int state);
210
211 /* Returns a set of command name completions for TEXT.
212    This is of the proper form for assigning to
213    rl_attempted_completion_function. */
214 static char **
215 complete_command_name (const char *text, int start, int end UNUSED)
216 {
217   if (start == 0)
218     {
219       /* Complete command name at start of line. */
220       return rl_completion_matches (text, command_generator);
221     }
222   else
223     {
224       /* Otherwise don't do any completion. */
225       rl_attempted_completion_over = 1;
226       return NULL;
227     }
228 }
229
230 /* Do not do any completion for TEXT. */
231 static char **
232 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
233 {
234   rl_attempted_completion_over = 1;
235   return NULL;
236 }
237
238 /* If STATE is 0, returns the first command name matching TEXT.
239    Otherwise, returns the next command name matching TEXT.
240    Returns a null pointer when no matches are left. */
241 static char *
242 command_generator (const char *text, int state)
243 {
244   static const struct command *cmd;
245   const char *name;
246
247   if (state == 0)
248     cmd = NULL;
249   name = cmd_complete (text, &cmd);
250   return name ? xstrdup (name) : NULL;
251 }
252 #endif /* HAVE_READLINE */