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,
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);
132 #if HAVE_READLINE && unix
133 if (history_file == NULL)
135 history_file = tilde_expand ("~/.pspp_history");
137 read_history (history_file);
138 stifle_history (500);
148 /* Gets a line from the user and stores it into LINE.
149 Prompts the user with PROMPT.
150 Returns true if successful, false at end of file.
153 readln_read (struct string *line, enum prompt_style style)
155 const char *prompt = prompt_get (style);
160 assert (initialised);
167 rl_attempted_completion_function = (style == PROMPT_FIRST
168 ? complete_command_name
170 string = readline (prompt);
176 add_history (string);
177 ds_assign_cstr (line, string);
182 fputs (prompt, stdout);
184 if (ds_read_line (line, stdin))
186 ds_chomp (line, '\n');
196 readln_close (struct getl_interface *i)
201 /* Creates a source which uses readln to get its line */
202 struct getl_interface *
203 create_readln_source (void)
205 struct readln_source *rlns = xzalloc (sizeof (*rlns));
207 rlns->interactive_func = readln_read;
209 rlns->parent.interactive = always_true;
210 rlns->parent.read = read_interactive;
211 rlns->parent.close = readln_close;
213 return (struct getl_interface *) rlns;
218 static char *command_generator (const char *text, int state);
220 /* Returns a set of command name completions for TEXT.
221 This is of the proper form for assigning to
222 rl_attempted_completion_function. */
224 complete_command_name (const char *text, int start, int end UNUSED)
228 /* Complete command name at start of line. */
229 return rl_completion_matches (text, command_generator);
233 /* Otherwise don't do any completion. */
234 rl_attempted_completion_over = 1;
239 /* Do not do any completion for TEXT. */
241 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
243 rl_attempted_completion_over = 1;
247 /* If STATE is 0, returns the first command name matching TEXT.
248 Otherwise, returns the next command name matching TEXT.
249 Returns a null pointer when no matches are left. */
251 command_generator (const char *text, int state)
253 static const struct command *cmd;
258 name = cmd_complete (text, &cmd);
259 return name ? xstrdup (name) : NULL;
261 #endif /* HAVE_READLINE */