much better readline support from Simo Sorce, with some mods from me
[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 /* user input through readline callback */
26 static char *command_line;
27 static int *readline_event;
28
29 /****************************************************************************
30 samba readline callback function
31 ****************************************************************************/
32 static int smb_rl_callback_handler(char *line_read)
33 {
34         if (!command_line) return RL_ERROR;
35
36         if (line_read)
37         {
38                 pstrcpy(command_line, line_read);
39 #if defined(HAVE_LIBREADLINE)
40 #if    defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H)
41                 if (strlen(line_read)) add_history(line_read);
42                 free(line_read);
43 #endif
44 #endif
45                 *readline_event = RL_GOT_LINE;
46         } else {
47                 *readline_event = RL_GOT_EOF;
48         }
49         return 0;
50 }
51
52 void smb_rl_read_char (void)
53 {
54 #ifdef HAVE_LIBREADLINE
55         *readline_event = RL_NO_EVENTS;
56         rl_callback_read_char ();
57 #else
58         pstring line;
59         fgets(line, sizeof(line), stdin);
60         smb_rl_callback_handler(line);
61 #endif
62 }
63
64 /****************************************************************************
65 init samba readline
66 ****************************************************************************/
67 void init_smb_readline(char *prg_name, char *cline_ptr, int *event_ptr)
68 {
69         command_line = cline_ptr;
70         readline_event = event_ptr;
71
72 #ifdef HAVE_LIBREADLINE
73         rl_readline_name = prg_name;
74         rl_already_prompted = 1;
75         rl_callback_handler_install(NULL, (VFunction *)&smb_rl_callback_handler);
76 #endif
77 }
78
79 /****************************************************************************
80 display the prompt
81 ****************************************************************************/
82 void smb_readline_prompt(char *prompt)
83 {
84         extern FILE *dbf;
85         
86         fprintf(dbf, "%s", prompt);
87         fflush(dbf);
88
89 #ifdef HAVE_LIBREADLINE
90         rl_callback_handler_remove();
91         rl_callback_handler_install(prompt, (VFunction *)&smb_rl_callback_handler);
92 #endif
93 }
94
95 /****************************************************************************
96 removes readline callback handler
97 ****************************************************************************/
98 void smb_readline_remove_handler(void)
99 {
100 #ifdef HAVE_LIBREADLINE
101         rl_callback_handler_remove ();
102 #endif
103
104         readline_event = NULL;
105         command_line = NULL;
106 }
107
108 /****************************************************************************
109 history
110 ****************************************************************************/
111 void cmd_history(void)
112 {
113 #if defined(HAVE_LIBREADLINE) && (defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H))
114         HIST_ENTRY **hlist;
115         int i;
116
117         hlist = history_list ();
118         
119         for (i = 0; hlist && hlist[i]; i++) {
120                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
121         }
122 #else
123         DEBUG(0,("no history without readline support\n"));
124 #endif
125 }
126