svn commit: samba r16274 - branches/SAMBA_3_0/source/client branches/SAMBA_3_0/source/lib trunk/source/client trunk/source/lib

jpeach at samba.org jpeach at samba.org
Thu Jun 15 23:51:20 GMT 2006


Author: jpeach
Date: 2006-06-15 23:51:20 +0000 (Thu, 15 Jun 2006)
New Revision: 16274

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=16274

Log:
Fix the smbclient prompting behaviour for both systems that have
libreadline and those that don't. We always use the built-in readline
replacement for non-interactive mode. Interactive prompts are always
emitted to stdout and non-interactive mode never prompts at all.

Introduce x_fdup to avoid spuriously closing stdout when a logfile is
specified on the command line and setup_logging is called a second time.

Modified:
   branches/SAMBA_3_0/source/client/client.c
   branches/SAMBA_3_0/source/client/smbctool.c
   branches/SAMBA_3_0/source/lib/readline.c
   branches/SAMBA_3_0/source/lib/xfile.c
   trunk/source/client/client.c
   trunk/source/client/smbctool.c
   trunk/source/lib/readline.c
   trunk/source/lib/xfile.c


Changeset:
Modified: branches/SAMBA_3_0/source/client/client.c
===================================================================
--- branches/SAMBA_3_0/source/client/client.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ branches/SAMBA_3_0/source/client/client.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -3422,8 +3422,9 @@
         /* set default debug level to 0 regardless of what smb.conf sets */
 	setup_logging( "smbclient", True );
 	DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
-	dbf = x_stderr;
-	x_setbuf( x_stderr, NULL );
+	if ((dbf = x_fdup(x_stderr))) {
+		x_setbuf( dbf, NULL );
+	}
 
 	pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
 				POPT_CONTEXT_KEEP_FIRST);

Modified: branches/SAMBA_3_0/source/client/smbctool.c
===================================================================
--- branches/SAMBA_3_0/source/client/smbctool.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ branches/SAMBA_3_0/source/client/smbctool.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -3561,10 +3561,11 @@
 	set_global_myname( "" );
 
 		/* set default debug level to 0 regardless of what smb.conf sets */
-	setup_logging( "smbclient", True );
+	setup_logging( "smbctool", True );
 	DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
-	dbf = x_stderr;
-	x_setbuf( x_stderr, NULL );
+	if ((dbf = x_fdup(x_stderr))) {
+		x_setbuf( dbf, NULL );
+	}
 
 	pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
 				POPT_CONTEXT_KEEP_FIRST);

Modified: branches/SAMBA_3_0/source/lib/readline.c
===================================================================
--- branches/SAMBA_3_0/source/lib/readline.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ branches/SAMBA_3_0/source/lib/readline.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -59,8 +59,11 @@
 	int fd = x_fileno(x_stdin);
 	char *ret;
 
-	x_fprintf(dbf, "%s", prompt);
-	x_fflush(dbf);
+	/* Prompt might be NULL in non-interactive mode. */
+	if (prompt) {
+		x_fprintf(x_stdout, "%s", prompt);
+		x_fflush(x_stdout);
+	}
 
 	while (1) {
 		timeout.tv_sec = 5;
@@ -85,34 +88,42 @@
 char *smb_readline(const char *prompt, void (*callback)(void), 
 		   char **(completion_fn)(const char *text, int start, int end))
 {
+	char *ret;
+	BOOL interactive;
+
+	interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
+	if (!interactive) {
+	    return smb_readline_replacement(NULL, callback, completion_fn);
+	}
+
 #if HAVE_LIBREADLINE
-	if (isatty(x_fileno(x_stdin))) {
-		char *ret;
 
-		/* Aargh!  Readline does bizzare things with the terminal width
-		that mucks up expect(1).  Set CLI_NO_READLINE in the environment
-		to force readline not to be used. */
+	/* Aargh!  Readline does bizzare things with the terminal width
+	that mucks up expect(1).  Set CLI_NO_READLINE in the environment
+	to force readline not to be used. */
 
-		if (getenv("CLI_NO_READLINE"))
-			return smb_readline_replacement(prompt, callback, completion_fn);
+	if (getenv("CLI_NO_READLINE"))
+		return smb_readline_replacement(prompt, callback, completion_fn);
 
-		if (completion_fn) {
-			/* The callback prototype has changed slightly between
-			different versions of Readline, so the same function
-			works in all of them to date, but we get compiler
-			warnings in some.  */
-			rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
-		}
+	if (completion_fn) {
+		/* The callback prototype has changed slightly between
+		different versions of Readline, so the same function
+		works in all of them to date, but we get compiler
+		warnings in some.  */
+		rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
+	}
 
-		if (callback)
-			rl_event_hook = (Function *)callback;
-		ret = readline(prompt);
-		if (ret && *ret)
-			add_history(ret);
-		return ret;
-	} else
+	if (callback)
+		rl_event_hook = (Function *)callback;
+	ret = readline(prompt);
+	if (ret && *ret)
+		add_history(ret);
+
+#else
+	ret = smb_readline_replacement(prompt, callback, completion_fn);
 #endif
-	return smb_readline_replacement(prompt, callback, completion_fn);
+
+	return ret;
 }
 
 /****************************************************************************

Modified: branches/SAMBA_3_0/source/lib/xfile.c
===================================================================
--- branches/SAMBA_3_0/source/lib/xfile.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ branches/SAMBA_3_0/source/lib/xfile.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -123,6 +123,28 @@
 	return ret;
 }
 
+XFILE *x_fdup(const XFILE *f)
+{
+	XFILE *ret;
+	int fd;
+
+	fd = dup(x_fileno(f));
+	if (fd < 0) {
+		return NULL;
+	}
+
+	ret = SMB_CALLOC_ARRAY(XFILE, 1);
+	if (!ret) {
+		close(fd);
+		return NULL;
+	}
+
+	ret->fd = fd;
+	ret->open_flags = f->open_flags;
+	x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE);
+	return ret;
+}
+
 /* simulate fclose() */
 int x_fclose(XFILE *f)
 {
@@ -220,7 +242,7 @@
 }
 
 /* at least fileno() is simple! */
-int x_fileno(XFILE *f)
+int x_fileno(const XFILE *f)
 {
 	return f->fd;
 }

Modified: trunk/source/client/client.c
===================================================================
--- trunk/source/client/client.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ trunk/source/client/client.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -3422,8 +3422,9 @@
         /* set default debug level to 0 regardless of what smb.conf sets */
 	setup_logging( "smbclient", True );
 	DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
-	dbf = x_stderr;
-	x_setbuf( x_stderr, NULL );
+	if ((dbf = x_fdup(x_stderr))) {
+		x_setbuf( dbf, NULL );
+	}
 
 	pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
 				POPT_CONTEXT_KEEP_FIRST);

Modified: trunk/source/client/smbctool.c
===================================================================
--- trunk/source/client/smbctool.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ trunk/source/client/smbctool.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -3561,10 +3561,11 @@
 	set_global_myname( "" );
 
 		/* set default debug level to 0 regardless of what smb.conf sets */
-	setup_logging( "smbclient", True );
+	setup_logging( "smbctool", True );
 	DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
-	dbf = x_stderr;
-	x_setbuf( x_stderr, NULL );
+	if ((dbf = x_fdup(x_stderr))) {
+		x_setbuf( dbf, NULL );
+	}
 
 	pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 
 				POPT_CONTEXT_KEEP_FIRST);

Modified: trunk/source/lib/readline.c
===================================================================
--- trunk/source/lib/readline.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ trunk/source/lib/readline.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -59,8 +59,11 @@
 	int fd = x_fileno(x_stdin);
 	char *ret;
 
-	x_fprintf(dbf, "%s", prompt);
-	x_fflush(dbf);
+	/* Prompt might be NULL in non-interactive mode. */
+	if (prompt) {
+		x_fprintf(x_stdout, "%s", prompt);
+		x_fflush(x_stdout);
+	}
 
 	while (1) {
 		timeout.tv_sec = 5;
@@ -85,34 +88,42 @@
 char *smb_readline(const char *prompt, void (*callback)(void), 
 		   char **(completion_fn)(const char *text, int start, int end))
 {
+	char *ret;
+	BOOL interactive;
+
+	interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
+	if (!interactive) {
+	    return smb_readline_replacement(NULL, callback, completion_fn);
+	}
+
 #if HAVE_LIBREADLINE
-	if (isatty(x_fileno(x_stdin))) {
-		char *ret;
 
-		/* Aargh!  Readline does bizzare things with the terminal width
-		that mucks up expect(1).  Set CLI_NO_READLINE in the environment
-		to force readline not to be used. */
+	/* Aargh!  Readline does bizzare things with the terminal width
+	that mucks up expect(1).  Set CLI_NO_READLINE in the environment
+	to force readline not to be used. */
 
-		if (getenv("CLI_NO_READLINE"))
-			return smb_readline_replacement(prompt, callback, completion_fn);
+	if (getenv("CLI_NO_READLINE"))
+		return smb_readline_replacement(prompt, callback, completion_fn);
 
-		if (completion_fn) {
-			/* The callback prototype has changed slightly between
-			different versions of Readline, so the same function
-			works in all of them to date, but we get compiler
-			warnings in some.  */
-			rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
-		}
+	if (completion_fn) {
+		/* The callback prototype has changed slightly between
+		different versions of Readline, so the same function
+		works in all of them to date, but we get compiler
+		warnings in some.  */
+		rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
+	}
 
-		if (callback)
-			rl_event_hook = (Function *)callback;
-		ret = readline(prompt);
-		if (ret && *ret)
-			add_history(ret);
-		return ret;
-	} else
+	if (callback)
+		rl_event_hook = (Function *)callback;
+	ret = readline(prompt);
+	if (ret && *ret)
+		add_history(ret);
+
+#else
+	ret = smb_readline_replacement(prompt, callback, completion_fn);
 #endif
-	return smb_readline_replacement(prompt, callback, completion_fn);
+
+	return ret;
 }
 
 /****************************************************************************

Modified: trunk/source/lib/xfile.c
===================================================================
--- trunk/source/lib/xfile.c	2006-06-15 23:47:41 UTC (rev 16273)
+++ trunk/source/lib/xfile.c	2006-06-15 23:51:20 UTC (rev 16274)
@@ -123,6 +123,28 @@
 	return ret;
 }
 
+XFILE *x_fdup(const XFILE *f)
+{
+	XFILE *ret;
+	int fd;
+
+	fd = dup(x_fileno(f));
+	if (fd < 0) {
+		return NULL;
+	}
+
+	ret = SMB_CALLOC_ARRAY(XFILE, 1);
+	if (!ret) {
+		close(fd);
+		return NULL;
+	}
+
+	ret->fd = fd;
+	ret->open_flags = f->open_flags;
+	x_setvbuf(ret, NULL, X_IOFBF, XBUFSIZE);
+	return ret;
+}
+
 /* simulate fclose() */
 int x_fclose(XFILE *f)
 {
@@ -220,7 +242,7 @@
 }
 
 /* at least fileno() is simple! */
-int x_fileno(XFILE *f)
+int x_fileno(const XFILE *f)
 {
 	return f->fd;
 }



More information about the samba-cvs mailing list