^$&%&*$&)% readline uses \n characters instead of letting the terminal wrap
[jra/samba/.git] / source3 / lib / readline.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba readline wrapper implementation
5    Copyright (C) Simo Sorce 2001
6    Copyright (C) Andrew Tridgell 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 #ifdef HAVE_LIBREADLINE
26 #  ifdef HAVE_READLINE_READLINE_H
27 #    include <readline/readline.h>
28 #    ifdef HAVE_READLINE_HISTORY_H
29 #      include <readline/history.h>
30 #    endif
31 #  else
32 #    ifdef HAVE_READLINE_H
33 #      include <readline.h>
34 #      ifdef HAVE_HISTORY_H
35 #        include <history.h>
36 #      endif
37 #    else
38 #      undef HAVE_LIBREADLINE
39 #    endif
40 #  endif
41 #endif
42
43 /****************************************************************************
44 display the prompt and wait for input. Call callback() regularly
45 ****************************************************************************/
46 static char *smb_readline_replacement(char *prompt, void (*callback)(void), 
47                                       char **(completion_fn)(char *text, 
48                                                              int start, 
49                                                              int end))
50 {
51         fd_set fds;
52         extern FILE *dbf;
53         static pstring line;
54         struct timeval timeout;
55         int fd = fileno(stdin);
56         char *ret;
57
58         fprintf(dbf, "%s", prompt);
59         fflush(dbf);
60
61         while (1) {
62                 timeout.tv_sec = 5;
63                 timeout.tv_usec = 0;
64
65                 FD_ZERO(&fds);
66                 FD_SET(fd,&fds);
67         
68                 if (sys_select_intr(fd+1,&fds,&timeout) == 1) {
69                         ret = fgets(line, sizeof(line), stdin);
70                         return ret;
71                 }
72                 if (callback) callback();
73         }
74 }
75
76 /****************************************************************************
77 display the prompt and wait for input. Call callback() regularly
78 ****************************************************************************/
79 char *smb_readline(char *prompt, void (*callback)(void), 
80                    char **(completion_fn)(char *text, int start, int end))
81 {
82 #if HAVE_LIBREADLINE
83         char *ret;
84
85         /* Aargh!  Readline does bizzare things with the terminal width
86            that mucks up expect(1).  Set CLI_NO_READLINE in the environment
87            to force readline not to be used. */
88
89         if (getenv("CLI_NO_READLINE"))
90                 return smb_readline_replacement(prompt, callback, 
91                                                 completion_fn);
92
93         if (completion_fn) {
94                 rl_attempted_completion_function = completion_fn;
95         }
96
97         if (callback) rl_event_hook = (Function *)callback;
98         ret = readline(prompt);
99         if (ret && *ret) add_history(ret);
100         return ret;
101 #else
102         return smb_readline_replacement(prompt, callback, completion_fn);
103 #endif
104 }
105
106 /****************************************************************************
107 history
108 ****************************************************************************/
109 void cmd_history(void)
110 {
111 #if defined(HAVE_LIBREADLINE)
112         HIST_ENTRY **hlist;
113         int i;
114
115         hlist = history_list();
116         
117         for (i = 0; hlist && hlist[i]; i++) {
118                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
119         }
120 #else
121         DEBUG(0,("no history without readline support\n"));
122 #endif
123 }