72171ae785d964b7a2cf9146886a844e4e4a06f4
[pspp-builds.git] / src / ui / terminal / read-line.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "read-line.h"
22
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <assert.h>
26 #include <errno.h>
27
28 #include "msg-ui.h"
29
30 #include <data/file-name.h>
31 #include <data/settings.h>
32 #include <language/command.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35 #include <libpspp/version.h>
36 #include <language/prompt.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 }
134
135 /* Gets a line from the user and stores it into LINE.
136    Prompts the user with PROMPT.
137    Returns true if successful, false at end of file.
138    */
139 static bool
140 readln_read (struct string *line, enum prompt_style style)
141 {
142   const char *prompt = prompt_get (style);
143 #if HAVE_READLINE
144   char *string;
145 #endif
146
147   assert (initialised);
148
149   reset_msg_count ();
150
151   welcome ();
152
153 #if HAVE_READLINE
154   rl_attempted_completion_function = (style == PROMPT_FIRST
155                                       ? complete_command_name
156                                       : dont_complete);
157   string = readline (prompt);
158   if (string == NULL)
159     return false;
160   else
161     {
162       if (string[0])
163         add_history (string);
164       ds_assign_cstr (line, string);
165       free (string);
166       return true;
167     }
168 #else
169   fputs (prompt, stdout);
170   fflush (stdout);
171   if (ds_read_line (line, stdin))
172     {
173       ds_chomp (line, '\n');
174       return true;
175     }
176   else
177     return false;
178 #endif
179 }
180
181
182 static void
183 readln_close (struct getl_interface *i)
184 {
185   free (i);
186 }
187
188 /* Creates a source which uses readln to get its line */
189 struct getl_interface *
190 create_readln_source (void)
191 {
192   struct readln_source *rlns  = xzalloc (sizeof (*rlns));
193
194   rlns->interactive_func = readln_read;
195
196   rlns->parent.interactive = always_true;
197   rlns->parent.read = read_interactive;
198   rlns->parent.close = readln_close;
199
200   return (struct getl_interface *) rlns;
201 }
202
203
204 #if HAVE_READLINE
205 static char *command_generator (const char *text, int state);
206
207 /* Returns a set of command name completions for TEXT.
208    This is of the proper form for assigning to
209    rl_attempted_completion_function. */
210 static char **
211 complete_command_name (const char *text, int start, int end UNUSED)
212 {
213   if (start == 0)
214     {
215       /* Complete command name at start of line. */
216       return rl_completion_matches (text, command_generator);
217     }
218   else
219     {
220       /* Otherwise don't do any completion. */
221       rl_attempted_completion_over = 1;
222       return NULL;
223     }
224 }
225
226 /* Do not do any completion for TEXT. */
227 static char **
228 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
229 {
230   rl_attempted_completion_over = 1;
231   return NULL;
232 }
233
234 /* If STATE is 0, returns the first command name matching TEXT.
235    Otherwise, returns the next command name matching TEXT.
236    Returns a null pointer when no matches are left. */
237 static char *
238 command_generator (const char *text, int state)
239 {
240   static const struct command *cmd;
241   const char *name;
242
243   if (state == 0)
244     cmd = NULL;
245   name = cmd_complete (text, &cmd);
246   return name ? xstrdup (name) : NULL;
247 }
248 #endif /* HAVE_READLINE */