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";
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,
104 struct string *line, enum getl_syntax *syntax)
106 struct readln_source *is =
107 (struct readln_source *) s ;
109 *syntax = GETL_INTERACTIVE;
110 return is->interactive_func (line, prompt_get_style ());
114 always_true (const struct getl_interface *s UNUSED)
119 /* Display a welcoming message. */
123 static bool welcomed = false;
127 fputs ("PSPP is free software and you are welcome to distribute copies of "
128 "it\nunder certain conditions; type \"show copying.\" to see the "
129 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
130 "warranty.\" for details.\n", stdout);
132 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);
154 rl_attempted_completion_function = (style == PROMPT_FIRST
155 ? complete_command_name
157 string = readline (prompt);
163 add_history (string);
164 ds_assign_cstr (line, string);
169 fputs (prompt, stdout);
171 if (ds_read_line (line, stdin))
173 ds_chomp (line, '\n');
183 readln_close (struct getl_interface *i)
188 /* Creates a source which uses readln to get its line */
189 struct getl_interface *
190 create_readln_source (void)
192 struct readln_source *rlns = xzalloc (sizeof (*rlns));
194 rlns->interactive_func = readln_read;
196 rlns->parent.interactive = always_true;
197 rlns->parent.read = read_interactive;
198 rlns->parent.close = readln_close;
200 return (struct getl_interface *) rlns;
205 static char *command_generator (const char *text, int state);
207 /* Returns a set of command name completions for TEXT.
208 This is of the proper form for assigning to
209 rl_attempted_completion_function. */
211 complete_command_name (const char *text, int start, int end UNUSED)
215 /* Complete command name at start of line. */
216 return rl_completion_matches (text, command_generator);
220 /* Otherwise don't do any completion. */
221 rl_attempted_completion_over = 1;
226 /* Do not do any completion for TEXT. */
228 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
230 rl_attempted_completion_over = 1;
234 /* If STATE is 0, returns the first command name matching TEXT.
235 Otherwise, returns the next command name matching TEXT.
236 Returns a null pointer when no matches are left. */
238 command_generator (const char *text, int state)
240 static const struct command *cmd;
245 name = cmd_complete (text, &cmd);
246 return name ? xstrdup (name) : NULL;
248 #endif /* HAVE_READLINE */