[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: svn commit: r1422706 - in /subversion/trunk/subversion: libsvn_subr/opt.c tests/cmdline/svntest/main.py

From: Stefan Sperling <stsp_at_elego.de>
Date: Mon, 17 Dec 2012 21:39:49 +0100

On Mon, Dec 17, 2012 at 01:44:43PM -0600, Peter Samuelson wrote:
> [Stefan Sperling]
> > We could use iatty() to enable --non-interactive if output is not
> > going to a terminal, for instance.
>
> I floated this idea some time ago and I'm still in favor of it. But I
> think a simple isatty(STDERR_FILENO) would be wrong.

Why? Not all ttys are interactive of course. But I don't really care if
people piping their line printers or modems into svn's stdin get interactive
mode by default. Is there any other downside to using isatty()?

And, actually, don't we want to check stdin, not stderr or stdout?

> What you want is
> to detect that there is a controlling terminal at all - something like:
>
> #if WINDOWS
> #define CON "CON:"
> #else
> #define CON "/dev/tty"
> #endif
> fd = open(CON, O_RDWR);
> if (fd >= 0) {
> close(fd);
> ...
> }
>
> (Of course I have no idea if "CON:" behaves that way. But there must
> be _some_ way to determine, on Windows, whether you have a terminal
> window available.)

The simple patch below implements it with the isatty() on *nix and
_isatty() on Windows (untested -- thanks for the hint Bert!).

We can still change this implementation to use your open() /dev/tty idea later.

However, this is enough to suppress prompts if stdin is piped into 'svn'
and if 'svn' is run from cron. I believe that's an improvement.

> Someone - was it Mark, perhaps? - objected to this idea on the basis
> that some wrapper programs out there may try to "scrape" the prompts,
> in interactive mode, and supply the correct input from, e.g., GUI
> dialog boxes.

Well, I would guess there are more people who are bitten by forgetting
to pass --non-interactive in normal scripts than there are people writing
scrapers that type answers into the prompts. I've met one person who
made this mistake just last week, who couldn't figure out why 'svn' was
hanging up their automated build jobs (it was asking for credentials).

[[[
Add a new function to the cmdline library to determine whether standard
input is connected to a terminal device. Set the --non-interactive flag
by default if standard input is not a termina device.

* subversion/include/svn_cmdline.h
  (svn_cmdline__stdin_isatty): Declare.

* subversion/libsvn_subr/cmdline.c: Include io.h on Windows.
  (svn_cmdline__stdin_isatty): New.

* subversion/svn/svn.c
  (sub_main): Right after parsing command line options, set the
    --non-interactive option based on whether stdin is a tty.
]]]

Index: subversion/include/svn_cmdline.h
===================================================================
--- subversion/include/svn_cmdline.h (revision 1423113)
+++ subversion/include/svn_cmdline.h (working copy)
@@ -381,6 +381,11 @@ svn_cmdline__getopt_init(apr_getopt_t **os,
                          const char *argv[],
                          apr_pool_t *pool);
 
+/* Determine whether standard input is associated with a terminal.
+ * @since New in 1.8. */
+svn_boolean_t
+svn_cmdline__stdin_isatty(void);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
Index: subversion/libsvn_subr/cmdline.c
===================================================================
--- subversion/libsvn_subr/cmdline.c (revision 1423113)
+++ subversion/libsvn_subr/cmdline.c (working copy)
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #else
 #include <crtdbg.h>
+#include <io.h>
 #endif
 
 #include <apr_errno.h> /* for apr_strerror */
@@ -923,3 +924,13 @@ svn_cmdline__print_xml_prop_hash(svn_stringbuf_t *
 
     return SVN_NO_ERROR;
 }
+
+svn_boolean_t
+svn_cmdline__stdin_isatty(void)
+{
+#ifdef WIN32
+ return (_isatty(0) != 0);
+#else
+ return (isatty(STDIN_FILENO) != 0);
+#endif
+}
Index: subversion/svn/svn.c
===================================================================
--- subversion/svn/svn.c (revision 1423113)
+++ subversion/svn/svn.c (working copy)
@@ -2191,6 +2191,10 @@ sub_main(int argc, const char *argv[], apr_pool_t
       }
     }
 
+ /* If stdin is not a terminal, set --non-interactive. */
+ if (!opt_state.non_interactive)
+ opt_state.non_interactive = !svn_cmdline__stdin_isatty();
+
   /* Turn our hash of changelists into an array of unique ones. */
   SVN_INT_ERR(svn_hash_keys(&(opt_state.changelists), changelists, pool));
 
Received on 2012-12-17 21:40:35 CET

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.