1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "read-line.h"
30 #include <data/file-name.h>
31 #include <data/settings.h>
32 #include <language/command.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35 #include <libpspp/version.h>
36 #include <language/prompt.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";
74 if (history_file == NULL)
76 history_file = tilde_expand ("~/.pspp_history");
78 read_history (history_file);
87 readln_uninitialize (void)
91 #if HAVE_READLINE && unix
92 if (history_file != NULL && false == get_testing_mode() )
93 write_history (history_file);
101 read_interactive (struct getl_interface *s,
102 struct string *line, enum getl_syntax *syntax)
104 struct readln_source *is =
105 (struct readln_source *) s ;
107 *syntax = GETL_INTERACTIVE;
108 return is->interactive_func (line, prompt_get_style ());
112 always_true (const struct getl_interface *s UNUSED)
117 /* Display a welcoming message. */
121 static bool welcomed = false;
125 fputs ("PSPP is free software and you are welcome to distribute copies of "
126 "it\nunder certain conditions; type \"show copying.\" to see the "
127 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
128 "warranty.\" for details.\n", stdout);
131 #if HAVE_READLINE && unix
132 if (history_file == NULL)
134 history_file = tilde_expand ("~/.pspp_history");
136 read_history (history_file);
137 stifle_history (500);
147 /* Gets a line from the user and stores it into LINE.
148 Prompts the user with PROMPT.
149 Returns true if successful, false at end of file.
152 readln_read (struct string *line, enum prompt_style style)
154 const char *prompt = prompt_get (style);
159 assert (initialised);
166 rl_attempted_completion_function = (style == PROMPT_FIRST
167 ? complete_command_name
169 string = readline (prompt);
175 add_history (string);
176 ds_assign_cstr (line, string);
181 fputs (prompt, stdout);
183 if (ds_read_line (line, stdin))
185 ds_chomp (line, '\n');
195 readln_close (struct getl_interface *i)
200 /* Creates a source which uses readln to get its line */
201 struct getl_interface *
202 create_readln_source (void)
204 struct readln_source *rlns = xzalloc (sizeof (*rlns));
206 rlns->interactive_func = readln_read;
208 rlns->parent.interactive = always_true;
209 rlns->parent.read = read_interactive;
210 rlns->parent.close = readln_close;
212 return (struct getl_interface *) rlns;
217 static char *command_generator (const char *text, int state);
219 /* Returns a set of command name completions for TEXT.
220 This is of the proper form for assigning to
221 rl_attempted_completion_function. */
223 complete_command_name (const char *text, int start, int end UNUSED)
227 /* Complete command name at start of line. */
228 return rl_completion_matches (text, command_generator);
232 /* Otherwise don't do any completion. */
233 rl_attempted_completion_over = 1;
238 /* Do not do any completion for TEXT. */
240 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
242 rl_attempted_completion_over = 1;
246 /* If STATE is 0, returns the first command name matching TEXT.
247 Otherwise, returns the next command name matching TEXT.
248 Returns a null pointer when no matches are left. */
250 command_generator (const char *text, int state)
252 static const struct command *cmd;
257 name = cmd_complete (text, &cmd);
258 return name ? xstrdup (name) : NULL;
260 #endif /* HAVE_READLINE */