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/manager.h>
40 #define _(msgid) gettext (msgid)
43 #include <readline/readline.h>
44 #include <readline/history.h>
46 static char *history_file;
48 static char **complete_command_name (const char *, int, int);
49 static char **dont_complete (const char *, int, int);
50 #endif /* HAVE_READLINE */
55 struct getl_interface parent ;
57 bool (*interactive_func) (struct string *line,
62 static bool initialised = false;
64 /* Initialize getl. */
66 readln_initialize (void)
71 rl_basic_word_break_characters = "\n";
74 if (history_file == NULL)
76 const char *home_dir = getenv ("HOME");
79 history_file = xasprintf ("%s/.pspp_history", home_dir);
80 read_history (history_file);
88 readln_uninitialize (void)
93 if (history_file != NULL && false == get_testing_mode() )
94 write_history (history_file);
102 read_interactive (struct getl_interface *s,
103 struct string *line, enum getl_syntax *syntax)
105 struct readln_source *is =
106 (struct readln_source *) s ;
108 *syntax = GETL_INTERACTIVE;
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 ();
134 /* Gets a line from the user and stores it into LINE.
135 Prompts the user with PROMPT.
136 Returns true if successful, false at end of file.
139 readln_read (struct string *line, enum prompt_style style)
141 const char *prompt = prompt_get (style);
146 assert (initialised);
152 if (style == PROMPT_FIRST)
156 rl_attempted_completion_function = (style == PROMPT_FIRST
157 ? complete_command_name
159 string = readline (prompt);
165 add_history (string);
166 ds_assign_cstr (line, string);
171 fputs (prompt, stdout);
173 if (ds_read_line (line, stdin))
175 ds_chomp (line, '\n');
185 readln_close (struct getl_interface *i)
190 /* Creates a source which uses readln to get its line */
191 struct getl_interface *
192 create_readln_source (void)
194 struct readln_source *rlns = xzalloc (sizeof (*rlns));
196 rlns->interactive_func = readln_read;
198 rlns->parent.interactive = always_true;
199 rlns->parent.read = read_interactive;
200 rlns->parent.close = readln_close;
202 return (struct getl_interface *) rlns;
207 static char *command_generator (const char *text, int state);
209 /* Returns a set of command name completions for TEXT.
210 This is of the proper form for assigning to
211 rl_attempted_completion_function. */
213 complete_command_name (const char *text, int start, int end UNUSED)
217 /* Complete command name at start of line. */
218 return rl_completion_matches (text, command_generator);
222 /* Otherwise don't do any completion. */
223 rl_attempted_completion_over = 1;
228 /* Do not do any completion for TEXT. */
230 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
232 rl_attempted_completion_over = 1;
236 /* If STATE is 0, returns the first command name matching TEXT.
237 Otherwise, returns the next command name matching TEXT.
238 Returns a null pointer when no matches are left. */
240 command_generator (const char *text, int state)
242 static const struct command *cmd;
247 name = cmd_complete (text, &cmd);
248 return name ? xstrdup (name) : NULL;
250 #endif /* HAVE_READLINE */