Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / ui / terminal / read-line.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
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.
8
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.
13
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
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include "read-line.h"
22
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <assert.h>
26 #include <errno.h>
27
28 #include "msg-ui.h"
29
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>
37
38 #include "xalloc.h"
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43 #if HAVE_READLINE
44 #include <readline/readline.h>
45 #include <readline/history.h>
46
47 static char *history_file;
48
49 static char **complete_command_name (const char *, int, int);
50 static char **dont_complete (const char *, int, int);
51 #endif /* HAVE_READLINE */
52
53
54 struct readln_source 
55 {
56   struct getl_interface parent ;
57
58   bool (*interactive_func) (struct string *line,
59                             enum prompt_style) ;
60 };
61
62
63 static bool initialised = false;
64
65 /* Initialize getl. */
66 void
67 readln_initialize (void)
68 {
69   initialised = true;
70
71 #if HAVE_READLINE
72   rl_basic_word_break_characters = "\n";
73 #ifdef unix
74   if (history_file == NULL)
75     {
76       history_file = tilde_expand ("~/.pspp_history");
77       using_history ();
78       read_history (history_file);
79       stifle_history (500);
80     }
81 #endif
82 #endif
83 }
84
85 /* Close getl. */
86 void
87 readln_uninitialize (void)
88 {
89   initialised = false;
90
91 #if HAVE_READLINE && unix
92   if (history_file != NULL && false == get_testing_mode() )
93     write_history (history_file);
94   clear_history ();
95   free (history_file);
96 #endif
97 }
98
99
100 static bool
101 read_interactive (struct getl_interface *s,
102                   struct string *line, enum getl_syntax *syntax)
103 {
104   struct readln_source *is  =
105     (struct readln_source *) s ;
106
107   *syntax = GETL_INTERACTIVE;
108   return is->interactive_func (line, prompt_get_style ());
109 }
110
111 static bool
112 always_true (const struct getl_interface *s UNUSED)
113 {
114   return true;
115 }
116
117 /* Display a welcoming message. */
118 static void
119 welcome (void)
120 {
121   static bool welcomed = false;
122   if (welcomed)
123     return;
124   welcomed = true;
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);
129   puts (stat_version);
130
131 #if HAVE_READLINE && unix
132   if (history_file == NULL)
133     {
134       history_file = tilde_expand ("~/.pspp_history");
135       using_history ();
136       read_history (history_file);
137       stifle_history (500);
138     }
139 #endif
140 }
141
142
143
144
145
146
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.
150    */
151 static bool
152 readln_read (struct string *line, enum prompt_style style)
153 {
154   const char *prompt = prompt_get (style);
155 #if HAVE_READLINE
156   char *string;
157 #endif
158   
159   assert (initialised);
160
161   reset_msg_count ();
162
163   welcome ();
164
165 #if HAVE_READLINE
166   rl_attempted_completion_function = (style == PROMPT_FIRST
167                                       ? complete_command_name
168                                       : dont_complete);
169   string = readline (prompt);
170   if (string == NULL)
171     return false;
172   else 
173     {
174       if (string[0])
175         add_history (string);
176       ds_assign_cstr (line, string);
177       free (string);
178       return true; 
179     }
180 #else
181   fputs (prompt, stdout);
182   fflush (stdout);
183   if (ds_read_line (line, stdin)) 
184     {
185       ds_chomp (line, '\n');
186       return true;
187     }
188   else
189     return false;
190 #endif
191 }
192
193
194 static void
195 readln_close (struct getl_interface *i)
196 {
197   free (i);
198 }
199
200 /* Creates a source which uses readln to get its line */
201 struct getl_interface *
202 create_readln_source (void)
203 {
204   struct readln_source *rlns  = xzalloc (sizeof (*rlns));
205
206   rlns->interactive_func = readln_read;
207
208   rlns->parent.interactive = always_true;
209   rlns->parent.read = read_interactive;
210   rlns->parent.close = readln_close;
211
212   return (struct getl_interface *) rlns;
213 }
214
215
216 #if HAVE_READLINE
217 static char *command_generator (const char *text, int state);
218
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. */
222 static char **
223 complete_command_name (const char *text, int start, int end UNUSED)
224 {
225   if (start == 0) 
226     {
227       /* Complete command name at start of line. */
228       return rl_completion_matches (text, command_generator); 
229     }
230   else 
231     {
232       /* Otherwise don't do any completion. */
233       rl_attempted_completion_over = 1;
234       return NULL; 
235     }
236 }
237
238 /* Do not do any completion for TEXT. */
239 static char **
240 dont_complete (const char *text UNUSED, int start UNUSED, int end UNUSED)
241 {
242   rl_attempted_completion_over = 1;
243   return NULL; 
244 }
245
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. */
249 static char *
250 command_generator (const char *text, int state) 
251
252   static const struct command *cmd;
253   const char *name;
254
255   if (state == 0)
256     cmd = NULL;
257   name = cmd_complete (text, &cmd);
258   return name ? xstrdup (name) : NULL;
259 }
260 #endif /* HAVE_READLINE */