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"
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>
41 #define _(msgid) gettext (msgid)
44 #include <readline/readline.h>
45 #include <readline/history.h>
47 static char *history_file;
49 static char **complete_command_name (const char *, int, int);
50 static char **dont_complete (const char *, int, int);
51 #endif /* HAVE_READLINE */
56 struct getl_interface parent ;
58 bool (*interactive_func) (struct string *line,
63 static bool initialised = false;
65 /* Initialize getl. */
67 readln_initialize (void)
72 rl_basic_word_break_characters = "\n";
75 if (history_file == NULL)
77 const char *home_dir = getenv ("HOME");
80 history_file = xasprintf ("%s/.pspp_history", home_dir);
81 read_history (history_file);
89 readln_uninitialize (void)
94 if (history_file != NULL && false == get_testing_mode() )
95 write_history (history_file);
103 read_interactive (struct getl_interface *s,
106 struct readln_source *is =
107 (struct readln_source *) s ;
109 return is->interactive_func (line, prompt_get_style ());
113 always_true (const struct getl_interface *s UNUSED)
118 /* Display a welcoming message. */
122 static bool welcomed = false;
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);
131 readln_initialize ();
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.
140 readln_read (struct string *line, enum prompt_style style)
142 const char *prompt = prompt_get (style);
147 assert (initialised);
153 if (style == PROMPT_FIRST)
157 rl_attempted_completion_function = (style == PROMPT_FIRST
158 ? complete_command_name
160 string = readline (prompt);
166 add_history (string);
167 ds_assign_cstr (line, string);
172 fputs (prompt, stdout);
174 if (ds_read_line (line, stdin))
176 ds_chomp (line, '\n');
186 readln_close (struct getl_interface *i)
191 /* Creates a source which uses readln to get its line */
192 struct getl_interface *
193 create_readln_source (void)
195 struct readln_source *rlns = xzalloc (sizeof (*rlns));
197 rlns->interactive_func = readln_read;
199 rlns->parent.interactive = always_true;
200 rlns->parent.read = read_interactive;
201 rlns->parent.close = readln_close;
203 return (struct getl_interface *) rlns;
208 static char *command_generator (const char *text, int state);
210 /* Returns a set of command name completions for TEXT.
211 This is of the proper form for assigning to
212 rl_attempted_completion_function. */
214 complete_command_name (const char *text, int start, int end UNUSED)
218 /* Complete command name at start of line. */
219 return rl_completion_matches (text, command_generator);
223 /* Otherwise don't do any completion. */
224 rl_attempted_completion_over = 1;
229 /* Do not do any completion for TEXT. */
231 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
233 rl_attempted_completion_over = 1;
237 /* If STATE is 0, returns the first command name matching TEXT.
238 Otherwise, returns the next command name matching TEXT.
239 Returns a null pointer when no matches are left. */
241 command_generator (const char *text, int state)
243 static const struct command *cmd;
248 name = cmd_complete (text, &cmd);
249 return name ? xstrdup (name) : NULL;
251 #endif /* HAVE_READLINE */