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