1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009 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/cast.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37 #include <libpspp/version.h>
38 #include <language/prompt.h>
39 #include <output/journal.h>
40 #include <output/driver.h>
41 #include <ui/terminal/terminal.h>
46 #define _(msgid) gettext (msgid)
49 #include <readline/readline.h>
50 #include <readline/history.h>
52 static char *history_file;
54 static char **complete_command_name (const char *, int, int);
55 static char **dont_complete (const char *, int, int);
56 #endif /* HAVE_READLINE */
61 struct getl_interface parent ;
63 bool (*interactive_func) (struct string *line,
68 static bool initialised = false;
70 /* Initialize getl. */
72 readln_initialize (void)
77 rl_basic_word_break_characters = "\n";
80 if (history_file == NULL)
82 const char *home_dir = getenv ("HOME");
85 history_file = xasprintf ("%s/.pspp_history", home_dir);
86 read_history (history_file);
94 readln_uninitialize (void)
99 if (history_file != NULL && false == settings_get_testing_mode () )
100 write_history (history_file);
108 read_interactive (struct getl_interface *s,
111 struct readln_source *is = UP_CAST (s, struct readln_source, parent);
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);
154 msg_ui_reset_counts ();
161 rl_attempted_completion_function = (style == PROMPT_FIRST
162 ? complete_command_name
164 string = readline (prompt);
170 add_history (string);
171 ds_assign_cstr (line, string);
176 fputs (prompt, stdout);
178 if (ds_read_line (line, stdin, SIZE_MAX))
180 ds_chomp (line, '\n');
187 /* Check whether the size of the window has changed, so that
188 the output drivers can adjust their settings as needed. We
189 only do this for the first line of a command, as it's
190 possible that the output drivers are actually in use
191 afterward, and we don't want to confuse them in the middle
193 if (style == PROMPT_FIRST)
194 terminal_check_size ();
200 readln_close (struct getl_interface *i)
205 /* Creates a source which uses readln to get its line */
206 struct getl_interface *
207 create_readln_source (void)
209 struct readln_source *rlns = xzalloc (sizeof (*rlns));
211 rlns->interactive_func = readln_read;
213 rlns->parent.interactive = always_true;
214 rlns->parent.read = read_interactive;
215 rlns->parent.close = readln_close;
217 return &rlns->parent;
222 static char *command_generator (const char *text, int state);
224 /* Returns a set of command name completions for TEXT.
225 This is of the proper form for assigning to
226 rl_attempted_completion_function. */
228 complete_command_name (const char *text, int start, int end UNUSED)
232 /* Complete command name at start of line. */
233 return rl_completion_matches (text, command_generator);
237 /* Otherwise don't do any completion. */
238 rl_attempted_completion_over = 1;
243 /* Do not do any completion for TEXT. */
245 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
247 rl_attempted_completion_over = 1;
251 /* If STATE is 0, returns the first command name matching TEXT.
252 Otherwise, returns the next command name matching TEXT.
253 Returns a null pointer when no matches are left. */
255 command_generator (const char *text, int state)
257 static const struct command *cmd;
262 name = cmd_complete (text, &cmd);
263 return name ? xstrdup (name) : NULL;
265 #endif /* HAVE_READLINE */