1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "read-line.h"
31 #include <data/file-name.h>
32 #include <data/settings.h>
33 #include <language/command.h>
34 #include <libpspp/message.h>
35 #include <libpspp/str.h>
36 #include <libpspp/version.h>
37 #include <language/prompt.h>
38 #include <output/journal.h>
39 #include <output/manager.h>
40 #include <ui/terminal/terminal.h>
45 #define _(msgid) gettext (msgid)
48 #include <readline/readline.h>
49 #include <readline/history.h>
51 static char *history_file;
53 static char **complete_command_name (const char *, int, int);
54 static char **dont_complete (const char *, int, int);
55 #endif /* HAVE_READLINE */
60 struct getl_interface parent ;
62 bool (*interactive_func) (struct string *line,
67 static bool initialised = false;
69 /* Initialize getl. */
71 readln_initialize (void)
76 rl_basic_word_break_characters = "\n";
79 if (history_file == NULL)
81 const char *home_dir = getenv ("HOME");
84 history_file = xasprintf ("%s/.pspp_history", home_dir);
85 read_history (history_file);
93 readln_uninitialize (void)
98 if (history_file != NULL && false == settings_get_testing_mode () )
99 write_history (history_file);
107 read_interactive (struct getl_interface *s,
110 struct readln_source *is =
111 (struct readln_source *) s ;
113 return is->interactive_func (line, prompt_get_style ());
117 always_true (const struct getl_interface *s UNUSED)
122 /* Display a welcoming message. */
126 static bool welcomed = false;
130 fputs ("PSPP is free software and you are welcome to distribute copies of "
131 "it\nunder certain conditions; type \"show copying.\" to see the "
132 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
133 "warranty.\" for details.\n", stdout);
135 readln_initialize ();
139 /* Gets a line from the user and stores it into LINE.
140 Prompts the user with PROMPT.
141 Returns true if successful, false at end of file.
144 readln_read (struct string *line, enum prompt_style style)
146 const char *prompt = prompt_get (style);
152 assert (initialised);
158 if (style == PROMPT_FIRST)
162 rl_attempted_completion_function = (style == PROMPT_FIRST
163 ? complete_command_name
165 string = readline (prompt);
171 add_history (string);
172 ds_assign_cstr (line, string);
177 fputs (prompt, stdout);
179 if (ds_read_line (line, stdin, SIZE_MAX))
181 ds_chomp (line, '\n');
188 /* Check whether the size of the window has changed, so that
189 the output drivers can adjust their settings as needed. We
190 only do this for the first line of a command, as it's
191 possible that the output drivers are actually in use
192 afterward, and we don't want to confuse them in the middle
194 if (style == PROMPT_FIRST)
195 terminal_check_size ();
201 readln_close (struct getl_interface *i)
206 /* Creates a source which uses readln to get its line */
207 struct getl_interface *
208 create_readln_source (void)
210 struct readln_source *rlns = xzalloc (sizeof (*rlns));
212 rlns->interactive_func = readln_read;
214 rlns->parent.interactive = always_true;
215 rlns->parent.read = read_interactive;
216 rlns->parent.close = readln_close;
218 return (struct getl_interface *) rlns;
223 static char *command_generator (const char *text, int state);
225 /* Returns a set of command name completions for TEXT.
226 This is of the proper form for assigning to
227 rl_attempted_completion_function. */
229 complete_command_name (const char *text, int start, int end UNUSED)
233 /* Complete command name at start of line. */
234 return rl_completion_matches (text, command_generator);
238 /* Otherwise don't do any completion. */
239 rl_attempted_completion_over = 1;
244 /* Do not do any completion for TEXT. */
246 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
248 rl_attempted_completion_over = 1;
252 /* If STATE is 0, returns the first command name matching TEXT.
253 Otherwise, returns the next command name matching TEXT.
254 Returns a null pointer when no matches are left. */
256 command_generator (const char *text, int state)
258 static const struct command *cmd;
263 name = cmd_complete (text, &cmd);
264 return name ? xstrdup (name) : NULL;
266 #endif /* HAVE_READLINE */