2a0bfd5e2be55ca584ef6c6c2f1088a5a90d22c7
[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    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include "read-line.h"
23
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <assert.h>
27 #include <errno.h>
28
29 #include "msg-ui.h"
30
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>
39
40 #include "gettext.h"
41 #define _(msgid) gettext (msgid)
42
43
44 #if HAVE_READLINE
45 #include <readline/readline.h>
46 #include <readline/history.h>
47
48 static char *history_file;
49 #endif /* HAVE_READLINE */
50
51 static bool initialised = false;
52
53 /* Initialize getl. */
54 void
55 readln_initialize (void)
56 {
57   initialised = true;
58
59 #if HAVE_READLINE 
60   rl_completion_entry_function = pspp_completion_function;
61 #ifdef unix
62   if (history_file == NULL)
63     {
64       history_file = tilde_expand ("~/.pspp_history");
65       using_history ();
66       read_history (history_file);
67       stifle_history (500);
68     }
69 #endif
70 #endif
71 }
72
73 /* Close getl. */
74 void
75 readln_uninitialize (void)
76 {
77   initialised = false;
78
79 #if HAVE_READLINE && unix
80   if (history_file != NULL)
81     write_history (history_file);
82 #endif
83 }
84
85 /* Display a welcoming message. */
86 static void
87 welcome (void)
88 {
89   static bool welcomed = false;
90   if (welcomed)
91     return;
92   welcomed = true;
93   fputs ("PSPP is free software and you are welcome to distribute copies of "
94          "it\nunder certain conditions; type \"show copying.\" to see the "
95          "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
96          "warranty.\" for details.\n", stdout);
97   puts (stat_version);
98
99 #if HAVE_READLINE && unix
100   if (history_file == NULL)
101     {
102       history_file = tilde_expand ("~/.pspp_history");
103       using_history ();
104       read_history (history_file);
105       stifle_history (500);
106     }
107 #endif
108 }
109
110 /* Gets a line from the user and stores it into LINE.
111    Prompts the user with PROMPT.
112    Returns true if successful, false at end of file.
113    Suitable for passing to getl_append_interactive(). */
114 bool
115 readln_read (struct string *line, const char *prompt)
116 {
117 #if HAVE_READLINE
118   char *string;
119 #endif
120   
121   assert(initialised);
122
123   reset_msg_count ();
124
125   welcome ();
126
127 #if HAVE_READLINE
128   string = readline (prompt);
129   if (string == NULL)
130     return false;
131   else 
132     {
133       if (string[0])
134         add_history (string);
135       ds_assign_c_str (line, string);
136       free (string);
137       return true; 
138     }
139 #else
140   fputs (prompt, stdout);
141   fflush (stdout);
142   if (ds_gets (line, stdin)) 
143     {
144       ds_chomp (line, '\n');
145       return true;
146     }
147   else
148     return false;
149 #endif
150 }