1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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, see <http://www.gnu.org/licenses/>. */
25 #include <readline/readline.h>
26 #include <readline/history.h>
28 static char *history_file;
30 static char **complete_command_name (const char *, int, int);
31 static char **dont_complete (const char *, int, int);
32 static char *command_generator (const char *text, int state);
34 static const bool have_readline = true;
37 static const bool have_readline = false;
42 #include "ui/terminal/terminal-reader.h"
49 #include "data/file-name.h"
50 #include "data/settings.h"
51 #include "language/command.h"
52 #include "language/lexer/lexer.h"
53 #include "libpspp/assertion.h"
54 #include "libpspp/cast.h"
55 #include "libpspp/message.h"
56 #include "libpspp/prompt.h"
57 #include "libpspp/str.h"
58 #include "libpspp/version.h"
59 #include "output/driver.h"
60 #include "output/journal.h"
61 #include "ui/terminal/terminal.h"
63 #include "gl/minmax.h"
64 #include "gl/xalloc.h"
67 #define _(msgid) gettext (msgid)
69 struct terminal_reader
71 struct lex_reader reader;
77 static int n_terminal_readers;
79 static void readline_init (void);
80 static void readline_done (void);
81 static bool readline_read (struct substring *, enum prompt_style);
83 /* Display a welcoming message. */
87 static bool welcomed = false;
91 fputs ("PSPP is free software and you are welcome to distribute copies of "
92 "it\nunder certain conditions; type \"show copying.\" to see the "
93 "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
94 "warranty.\" for details.\n", stdout);
99 static struct terminal_reader *
100 terminal_reader_cast (struct lex_reader *r)
102 return UP_CAST (r, struct terminal_reader, reader);
106 terminal_reader_read (struct lex_reader *r_, char *buf, size_t n,
107 enum prompt_style prompt_style)
109 struct terminal_reader *r = terminal_reader_cast (r_);
112 if (r->offset >= r->s.length && !r->eof)
115 msg_ui_reset_counts ();
119 if (! readline_read (&r->s, prompt_style))
122 fprintf (rl_outstream, "\n");
126 r->eof = ss_is_empty (r->s);
128 /* Check whether the size of the window has changed, so that
129 the output drivers can adjust their settings as needed. We
130 only do this for the first line of a command, as it's
131 possible that the output drivers are actually in use
132 afterward, and we don't want to confuse them in the middle
134 if (prompt_style == PROMPT_FIRST)
135 terminal_check_size ();
138 chunk = MIN (n, r->s.length - r->offset);
139 memcpy (buf, r->s.string + r->offset, chunk);
145 terminal_reader_close (struct lex_reader *r_)
147 struct terminal_reader *r = terminal_reader_cast (r_);
150 free (r->reader.file_name);
153 if (!--n_terminal_readers)
157 static struct lex_reader_class terminal_reader_class =
159 terminal_reader_read,
160 terminal_reader_close
163 /* Creates a source which uses readln to get its line */
165 terminal_reader_create (void)
167 struct terminal_reader *r;
169 if (!n_terminal_readers++)
172 r = xzalloc (sizeof *r);
173 r->reader.class = &terminal_reader_class;
174 r->reader.syntax = LEX_SYNTAX_INTERACTIVE;
175 r->reader.error = LEX_ERROR_INTERACTIVE;
176 r->reader.file_name = NULL;
186 readline_prompt (enum prompt_style style)
202 case PROMPT_DOCUMENT:
205 case PROMPT_DO_REPEAT:
206 return "DO REPEAT> ";
215 static bool sigint_received ;
222 write (pfd[1], "x", 1);
223 rl_echo_signal_char (sig);
228 A function similar to getc from stdio.
229 However this one may be interrupted by SIGINT.
230 If that happens it will return EOF and the global variable
231 sigint_received will be set to true.
234 interruptible_getc (FILE *fp)
240 struct timeval timeout = {0, 50000};
246 max_fd = (max_fd > fd) ? max_fd : fd;
249 max_fd = (max_fd > fd) ? max_fd : fd;
251 ret = select (max_fd + 1, &what, NULL, NULL, &timeout);
252 if ( ret == -1 && errno != EINTR)
254 perror ("Select failed");
260 if (FD_ISSET (pfd[0], &what))
263 read (pfd[0], dummy, 1);
264 sigint_received = true;
271 /* This will not block! */
272 read (fileno (fp), &c, 1);
284 if ( 0 != pipe2 (pfd, O_NONBLOCK))
285 perror ("Cannot create pipe");
287 if ( SIG_ERR == signal (SIGINT, handler))
288 perror ("Cannot add signal handler");
290 rl_catch_signals = 0;
291 rl_getc_function = interruptible_getc;
292 rl_basic_word_break_characters = "\n";
294 stifle_history (500);
295 if (history_file == NULL)
297 const char *home_dir = getenv ("HOME");
298 if (home_dir != NULL)
300 history_file = xasprintf ("%s/.pspp_history", home_dir);
301 read_history (history_file);
309 if (history_file != NULL && false == settings_get_testing_mode () )
310 write_history (history_file);
315 /* Prompt the user for a line of input and return it in LINE.
316 Returns true if the LINE should be considered valid, false otherwise.
319 readline_read (struct substring *line, enum prompt_style style)
323 rl_attempted_completion_function = (style == PROMPT_FIRST
324 ? complete_command_name
326 sigint_received = false;
327 string = readline (readline_prompt (style));
339 add_history (string);
341 end = strchr (string, '\0');
343 *line = ss_buffer (string, end - string + 1);
351 /* Returns a set of command name completions for TEXT.
352 This is of the proper form for assigning to
353 rl_attempted_completion_function. */
355 complete_command_name (const char *text, int start, int end UNUSED)
359 /* Complete command name at start of line. */
360 return rl_completion_matches (text, command_generator);
364 /* Otherwise don't do any completion. */
365 rl_attempted_completion_over = 1;
370 /* Do not do any completion for TEXT. */
372 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
374 rl_attempted_completion_over = 1;
378 /* If STATE is 0, returns the first command name matching TEXT.
379 Otherwise, returns the next command name matching TEXT.
380 Returns a null pointer when no matches are left. */
382 command_generator (const char *text, int state)
384 static const struct command *cmd;
389 name = cmd_complete (text, &cmd);
390 return name ? xstrdup (name) : NULL;
393 #else /* !HAVE_READLINE */
406 readline_read (struct substring *line, enum prompt_style style)
408 struct string string;
409 const char *prompt = readline_prompt (style);
411 fputs (prompt, stdout);
413 ds_init_empty (&string);
414 ds_read_line (&string, stdin, SIZE_MAX);
420 #endif /* !HAVE_READLINE */