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/file-name.h>
33 #include <data/settings.h>
34 #include <language/command.h>
35 #include <libpspp/message.h>
36 #include <libpspp/str.h>
37 #include <libpspp/version.h>
38 #include <output/table.h>
43 #define _(msgid) gettext (msgid)
46 #include <readline/readline.h>
47 #include <readline/history.h>
49 static char *history_file;
51 static char **complete_command_name (const char *, int, int);
52 static char **dont_complete (const char *, int, int);
53 #endif /* HAVE_READLINE */
55 static bool initialised = false;
57 /* Initialize getl. */
59 readln_initialize (void)
64 rl_basic_word_break_characters = "\n";
66 if (history_file == NULL)
68 history_file = tilde_expand ("~/.pspp_history");
70 read_history (history_file);
79 readln_uninitialize (void)
83 #if HAVE_READLINE && unix
84 if (history_file != NULL)
85 write_history (history_file);
91 /* Display a welcoming message. */
95 static bool welcomed = false;
99 fputs ("PSPP is free software and you are welcome to distribute copies of "
100 "it\nunder certain conditions; type \"show copying.\" to see the "
101 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
102 "warranty.\" for details.\n", stdout);
105 #if HAVE_READLINE && unix
106 if (history_file == NULL)
108 history_file = tilde_expand ("~/.pspp_history");
110 read_history (history_file);
111 stifle_history (500);
116 /* Gets a line from the user and stores it into LINE.
117 Prompts the user with PROMPT.
118 Returns true if successful, false at end of file.
119 Suitable for passing to getl_append_interactive(). */
121 readln_read (struct string *line, enum getl_prompt_style style)
123 const char *prompt = getl_get_prompt (style);
128 assert (initialised);
135 rl_attempted_completion_function = (style == GETL_PROMPT_FIRST
136 ? complete_command_name
138 string = readline (prompt);
144 add_history (string);
145 ds_assign_cstr (line, string);
150 fputs (prompt, stdout);
152 if (ds_read_line (line, stdin))
154 ds_chomp (line, '\n');
163 static char *command_generator (const char *text, int state);
165 /* Returns a set of command name completions for TEXT.
166 This is of the proper form for assigning to
167 rl_attempted_completion_function. */
169 complete_command_name (const char *text, int start, int end UNUSED)
173 /* Complete command name at start of line. */
174 return rl_completion_matches (text, command_generator);
178 /* Otherwise don't do any completion. */
179 rl_attempted_completion_over = 1;
184 /* Do not do any completion for TEXT. */
186 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
188 rl_attempted_completion_over = 1;
192 /* If STATE is 0, returns the first command name matching TEXT.
193 Otherwise, returns the next command name matching TEXT.
194 Returns a null pointer when no matches are left. */
196 command_generator (const char *text, int state)
198 static const struct command *cmd;
203 name = cmd_complete (text, &cmd);
204 return name ? xstrdup (name) : NULL;
206 #endif /* HAVE_READLINE */