* main.c (main): Set MAKE_TTYOUT and MAKE_TTYERR.
authorPaul Smith <psmith@gnu.org>
Sun, 14 Sep 2014 05:03:19 +0000 (01:03 -0400)
committerPaul Smith <psmith@gnu.org>
Sun, 14 Sep 2014 05:03:19 +0000 (01:03 -0400)
* configure.ac: Test for isatty() and ttyname()
* makeint.h: provide a substitute for ttyname() if it's not available.
* config.ami.template, config.h-vms.template, config.h.W32.template:
define/undefine HAVE_ISATTY/HAVE_TTYNAME macros.
* NEWS, doc/make.texi: Document these new variables.

NEWS
config.ami.template
config.h-vms.template
config.h.W32.template
configure.ac
doc/make.texi
main.c
makeint.h

diff --git a/NEWS b/NEWS
index ae725fc10a5875d1400e088345f4bb0f52ff1343..c39485052d604d2678458f275f62bd79cbe715ea 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ A complete list of bugs fixed in this version is available here:
 
 http://sv.gnu.org/bugs/index.php?group=make&report_id=111&fix_release_id=105&set=custom
 
+* New variables: $(MAKE_TTYOUT) and $(MAKE_TTYERR) are set if stdout or
+  stderr, respectively, are believed to be writing to a terminal.
+
 * Allow a no-text-argument form of the $(file ...) function.  Without a text
   argument nothing is written to the file: it is simply opened in the
   requested mode, then closed again.
index d9163c053eac1d39bf47fe773ef2c9cfb9e18759..419824ff501b7847c39519fb42997ed83094060f 100644 (file)
@@ -318,6 +318,12 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 /* Define if you have the kstat library (-lkstat).  */
 /* #undef HAVE_LIBKSTAT */
 
+/* Define to 1 if you have the `isatty' function. */
+/* #undef HAVE_ISATTY */
+
+/* Define to 1 if you have the `ttyname' function. */
+/* #undef HAVE_TTYNAME */
+
 /* Define if you have the sun library (-lsun).  */
 /* #undef HAVE_LIBSUN */
 
index 71fe269f3a763be36d8dd2a5b672720fd4d292e0..8c0a0783006f237e99386dff869f7b85b363ccdb 100644 (file)
@@ -362,6 +362,12 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 /* Define to 1 if you have the sun library (-lsun).  */
 /* #undef HAVE_LIBSUN */
 
+/* Define to 1 if you have the `isatty' function. */
+/* #undef HAVE_ISATTY */
+
+/* Define to 1 if you have the `ttyname' function. */
+/* #undef HAVE_TTYNAME */
+
 /* Use high resolution file timestamps if nonzero. */
 #define FILE_TIMESTAMP_HI_RES 0
 
index f7aed9008b1d60c705f667c189edebac13bec3ef..849a8550b4088acb800e97bbd09e26ffb1b7f794 100644 (file)
@@ -292,6 +292,12 @@ this program.  If not, see <http://www.gnu.org/licenses/>.  */
 /* Define to 1 if you have the 'strsignal' function. */
 /* #undef HAVE_STRSIGNAL */
 
+/* Define to 1 if you have the `isatty' function. */
+#define HAVE_ISATTY 1
+
+/* Define to 1 if you have the `ttyname' function. */
+/* #undef HAVE_TTYNAME */
+
 /* Define to 1 if 'n_un.n_name' is a member of 'struct nlist'. */
 /* #undef HAVE_STRUCT_NLIST_N_UN_N_NAME */
 
index 59333d13afa354df5ccb2f6ebfde4432bc4eb3d3..97fd090cef17935163816b13648d5ece69c9eb91 100644 (file)
@@ -140,7 +140,7 @@ AC_CHECK_FUNCS([strdup strndup mkstemp mktemp fdopen fileno \
                 dup dup2 getcwd realpath sigsetmask sigaction \
                 getgroups seteuid setegid setlinebuf setreuid setregid \
                 getrlimit setrlimit setvbuf pipe strerror strsignal \
-                lstat readlink atexit])
+                lstat readlink atexit isatty ttyname])
 
 # We need to check declarations, not just existence, because on Tru64 this
 # function is not declared without special flags, which themselves cause
index 2aacc70730f6d411fd6ab0663fbe81c8fcf6951d..3a873d7a5cc192e70407fc145138a033d6e3feec 100644 (file)
@@ -6321,6 +6321,28 @@ will contain the number of times this instance has restarted.  Note
 this is not the same as recursion (counted by the @code{MAKELEVEL}
 variable).  You should not set, modify, or export this variable.
 
+@vindex MAKE_TTYOUT @r{(whether stdout is a terminal)}
+@vindex MAKE_TTYERR @r{(whether stderr is a terminal)}
+@item MAKE_TTYOUT
+@itemx MAKE_TTYERR
+When @code{make} starts it will check whether stdout and stderr will
+show their output on a terminal.  If so, it will set
+@code{MAKE_TTYOUT} and @code{MAKE_TTYERR}, respectively, to the name
+of the terminal device (or @code{true} if this cannot be determined).
+If set these variables will be marked for export.  These variables
+will not be changed by @code{make} and they will not be modified if
+already set.
+
+These values can be used (particularly in combination with output
+synchronization (@pxref{Parallel Output, ,Output During Parallel
+Execution}) to determine whether @code{make} itself is writing to a
+terminal; they can be tested to decide whether to force recipe
+commands to generate colorized output for example.
+
+If you invoke a sub-@code{make} and redirect its stdout or stderr it
+is your responsibility to reset or unexport these variables as well,
+if your makefiles rely on them.
+
 @vindex .RECIPEPREFIX @r{(change the recipe prefix character)}
 @item .RECIPEPREFIX
 The first character of the value of this variable is used as the
diff --git a/main.c b/main.c
index 6aecd80308df82ff95c49e7c4fe31bb098c18015..4a79b60f7913c0a1c2c9797e58e59f376f5f3545 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1374,7 +1374,6 @@ main (int argc, char **argv, char **envp)
 #endif
 
   /* Decode the switches.  */
-
   decode_env_switches (STRING_SIZE_TUPLE ("GNUMAKEFLAGS"));
 
   /* Clear GNUMAKEFLAGS to avoid duplication.  */
@@ -1398,6 +1397,17 @@ main (int argc, char **argv, char **envp)
 
   decode_switches (argc, (const char **)argv, 0);
 
+    /* Set a variable specifying whether stdout/stdin is hooked to a TTY.  */
+#ifdef HAVE_ISATTY
+    if (isatty (fileno (stdout)))
+      define_variable_cname ("MAKE_TTYOUT", TTYNAME (fileno (stdout)),
+                             o_default, 0)->export = v_export;
+
+    if (isatty (fileno (stderr)))
+      define_variable_cname ("MAKE_TTYERR", TTYNAME (fileno (stderr)),
+                             o_default, 0)->export = v_export;
+#endif
+
   /* Reset in case the switches changed our minds.  */
   syncing = (output_sync == OUTPUT_SYNC_LINE
              || output_sync == OUTPUT_SYNC_TARGET);
index ab416654db581276d3c939dab43c5139e5d4cefd..da267cb26e38ddd7ad62dcb431851c293339f29a 100644 (file)
--- a/makeint.h
+++ b/makeint.h
@@ -424,6 +424,12 @@ extern struct rlimit stack_limit;
 /* The number of bytes needed to represent the largest integer as a string.  */
 #define INTSTR_LENGTH         CSTRLEN ("18446744073709551616")
 
+#ifdef HAVE_TTYNAME
+# define TTYNAME(_f) ttyname (_f)
+#else
+# define TTYNAME(_f) "true"
+#endif
+
 \f
 const char *concat (unsigned int, ...);
 void message (int prefix, size_t length, const char *fmt, ...)