a89a953b5d0b06c5d5a5b41ecd65c516d4235793
[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_basic_word_break_characters = "\n";
61   rl_attempted_completion_function = pspp_attempted_completion_function;
62 #ifdef unix
63   if (history_file == NULL)
64     {
65       history_file = tilde_expand ("~/.pspp_history");
66       using_history ();
67       read_history (history_file);
68       stifle_history (500);
69     }
70 #endif
71 #endif
72 }
73
74 /* Close getl. */
75 void
76 readln_uninitialize (void)
77 {
78   initialised = false;
79
80 #if HAVE_READLINE && unix
81   if (history_file != NULL)
82     write_history (history_file);
83 #endif
84 }
85
86 /* Display a welcoming message. */
87 static void
88 welcome (void)
89 {
90   static bool welcomed = false;
91   if (welcomed)
92     return;
93   welcomed = true;
94   fputs ("PSPP is free software and you are welcome to distribute copies of "
95          "it\nunder certain conditions; type \"show copying.\" to see the "
96          "conditions.\nThere is ABSOLUTELY NO WARRANTY for PSPP; type \"show "
97          "warranty.\" for details.\n", stdout);
98   puts (stat_version);
99
100 #if HAVE_READLINE && unix
101   if (history_file == NULL)
102     {
103       history_file = tilde_expand ("~/.pspp_history");
104       using_history ();
105       read_history (history_file);
106       stifle_history (500);
107     }
108 #endif
109 }
110
111 /* Gets a line from the user and stores it into LINE.
112    Prompts the user with PROMPT.
113    Returns true if successful, false at end of file.
114    Suitable for passing to getl_append_interactive(). */
115 bool
116 readln_read (struct string *line, const char *prompt)
117 {
118 #if HAVE_READLINE
119   char *string;
120 #endif
121   
122   assert(initialised);
123
124   reset_msg_count ();
125
126   welcome ();
127
128 #if HAVE_READLINE
129   string = readline (prompt);
130   if (string == NULL)
131     return false;
132   else 
133     {
134       if (string[0])
135         add_history (string);
136       ds_assign_c_str (line, string);
137       free (string);
138       return true; 
139     }
140 #else
141   fputs (prompt, stdout);
142   fflush (stdout);
143   if (ds_gets (line, stdin)) 
144     {
145       ds_chomp (line, '\n');
146       return true;
147     }
148   else
149     return false;
150 #endif
151 }