1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #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>
42 #define _(msgid) gettext (msgid)
45 #include <readline/readline.h>
46 #include <readline/history.h>
48 static char *history_file;
50 static char **complete_command_name (const char *, int, int);
51 static char **dont_complete (const char *, int, int);
52 #endif /* HAVE_READLINE */
57 struct getl_interface parent ;
59 bool (*interactive_func) (struct string *line,
64 static bool initialised = false;
66 /* Initialize getl. */
68 readln_initialize (void)
73 rl_basic_word_break_characters = "\n";
75 if (history_file == NULL)
77 history_file = tilde_expand ("~/.pspp_history");
79 read_history (history_file);
88 readln_uninitialize (void)
92 #if HAVE_READLINE && unix
93 if (history_file != NULL && false == get_testing_mode() )
94 write_history (history_file);
102 read_interactive (struct getl_interface *s, struct string *line)
104 struct readln_source *is =
105 (struct readln_source *) s ;
107 return is->interactive_func (line, prompt_get_style ());
111 always_true (const struct getl_interface *s UNUSED)
116 /* Display a welcoming message. */
120 static bool welcomed = false;
124 fputs ("PSPP is free software and you are welcome to distribute copies of "
125 "it\nunder certain conditions; type \"show copying.\" to see the "
126 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
127 "warranty.\" for details.\n", stdout);
130 #if HAVE_READLINE && unix
131 if (history_file == NULL)
133 history_file = tilde_expand ("~/.pspp_history");
135 read_history (history_file);
136 stifle_history (500);
146 /* Gets a line from the user and stores it into LINE.
147 Prompts the user with PROMPT.
148 Returns true if successful, false at end of file.
151 readln_read (struct string *line, enum prompt_style style)
153 const char *prompt = prompt_get (style);
158 assert (initialised);
165 rl_attempted_completion_function = (style == PROMPT_FIRST
166 ? complete_command_name
168 string = readline (prompt);
174 add_history (string);
175 ds_assign_cstr (line, string);
180 fputs (prompt, stdout);
182 if (ds_read_line (line, stdin))
184 ds_chomp (line, '\n');
194 readln_close (struct getl_interface *i)
199 /* Creates a source which uses readln to get its line */
200 struct getl_interface *
201 create_readln_source (void)
203 struct readln_source *rlns = xzalloc (sizeof (*rlns));
205 rlns->interactive_func = readln_read;
207 rlns->parent.interactive = always_true;
208 rlns->parent.read = read_interactive;
209 rlns->parent.close = readln_close;
211 return (struct getl_interface *) rlns;
216 static char *command_generator (const char *text, int state);
218 /* Returns a set of command name completions for TEXT.
219 This is of the proper form for assigning to
220 rl_attempted_completion_function. */
222 complete_command_name (const char *text, int start, int end UNUSED)
226 /* Complete command name at start of line. */
227 return rl_completion_matches (text, command_generator);
231 /* Otherwise don't do any completion. */
232 rl_attempted_completion_over = 1;
237 /* Do not do any completion for TEXT. */
239 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
241 rl_attempted_completion_over = 1;
245 /* If STATE is 0, returns the first command name matching TEXT.
246 Otherwise, returns the next command name matching TEXT.
247 Returns a null pointer when no matches are left. */
249 command_generator (const char *text, int state)
251 static const struct command *cmd;
256 name = cmd_complete (text, &cmd);
257 return name ? xstrdup (name) : NULL;
259 #endif /* HAVE_READLINE */