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);
89 /* Display a welcoming message. */
93 static bool welcomed = false;
97 fputs ("PSPP is free software and you are welcome to distribute copies of "
98 "it\nunder certain conditions; type \"show copying.\" to see the "
99 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
100 "warranty.\" for details.\n", stdout);
103 #if HAVE_READLINE && unix
104 if (history_file == NULL)
106 history_file = tilde_expand ("~/.pspp_history");
108 read_history (history_file);
109 stifle_history (500);
114 /* Gets a line from the user and stores it into LINE.
115 Prompts the user with PROMPT.
116 Returns true if successful, false at end of file.
117 Suitable for passing to getl_append_interactive(). */
119 readln_read (struct string *line, enum getl_prompt_style style)
121 const char *prompt = getl_get_prompt (style);
126 assert (initialised);
133 rl_attempted_completion_function = (style == GETL_PROMPT_FIRST
134 ? complete_command_name
136 string = readline (prompt);
142 add_history (string);
143 ds_assign_cstr (line, string);
148 fputs (prompt, stdout);
150 if (ds_read_line (line, stdin))
152 ds_chomp (line, '\n');
161 static char *command_generator (const char *text, int state);
163 /* Returns a set of command name completions for TEXT.
164 This is of the proper form for assigning to
165 rl_attempted_completion_function. */
167 complete_command_name (const char *text, int start, int end UNUSED)
171 /* Complete command name at start of line. */
172 return rl_completion_matches (text, command_generator);
176 /* Otherwise don't do any completion. */
177 rl_attempted_completion_over = 1;
182 /* Do not do any completion for TEXT. */
184 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
186 rl_attempted_completion_over = 1;
190 /* If STATE is 0, returns the first command name matching TEXT.
191 Otherwise, returns the next command name matching TEXT.
192 Returns a null pointer when no matches are left. */
194 command_generator (const char *text, int state)
196 static const struct command *cmd;
201 name = cmd_complete (text, &cmd);
202 return name ? xstrdup (name) : NULL;
204 #endif /* HAVE_READLINE */