[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
tput(1) -T without a tty
From: |
Lauri Tirkkonen |
Subject: |
tput(1) -T without a tty |
Date: |
Thu, 5 Oct 2017 00:28:09 +0300 |
User-agent: |
Mutt/1.9.0 (2017-09-02) |
Hi,
as documented in the manual page, tput(1) no longer works without a
tty:
Until changes made after ncurses 6.0, tput did not modify terminal
modes. tput now uses a similar scheme, using functions shared with
tset (and ultimately based on the 4.4BSD tset). If it is not able
to open a terminal, e.g., when running in cron, tput will return an
error.
I'm having a little trouble understanding why this is also true if using
the -T option. There's at least one (admittedly quite archaic) piece of
software that uses -T to gather information about some predefined
terminals for use in an environment where it cannot rely on ncurses:
namely, the "tigen" script during illumos build queries tput for data to
be used in the kernel debugger kmdb. See
https://github.com/illumos/illumos-gate/blob/master/usr/src/cmd/mdb/tools/scripts/tigen.sh#L32
But using ncurses tput, which requires a tty, the illumos build cannot
be run from cron. I've thrown together the following quick patch to
avoid opening a tty if -T is given; is this a reasonable use case?
diff --git a/progs/tput.c b/progs/tput.c
index 3295d18..f6e8e8f 100644
--- a/progs/tput.c
+++ b/progs/tput.c
@@ -155,7 +155,8 @@ tput_cmd(int fd, TTY * saved_settings, bool opt_x, int
argc, char *argv[])
if (is_reset) {
reset_start(stdout, TRUE, FALSE);
- reset_tty_settings(fd, saved_settings);
+ if (saved_settings)
+ reset_tty_settings(fd, saved_settings);
} else {
reset_start(stdout, FALSE, TRUE);
}
@@ -165,13 +166,16 @@ tput_cmd(int fd, TTY * saved_settings, bool opt_x, int
argc, char *argv[])
#else
(void) fd;
#endif
- set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
- set_conversions(saved_settings);
+ if (saved_settings) {
+ set_control_chars(saved_settings, terasechar, intrchar, tkillchar);
+ set_conversions(saved_settings);
+ }
if (send_init_strings(fd, &oldmode)) {
reset_flush();
}
- update_tty_settings(&oldmode, saved_settings);
+ if (saved_settings)
+ update_tty_settings(&oldmode, saved_settings);
return 0;
}
@@ -282,6 +286,7 @@ main(int argc, char **argv)
int result = 0;
int fd;
TTY tty_settings;
+ TTY *settings = &tty_settings;
bool opt_x = FALSE; /* clear scrollback if possible */
prg_name = check_aliases(_nc_rootname(argv[0]), TRUE);
@@ -296,6 +301,7 @@ main(int argc, char **argv)
case 'T':
use_env(FALSE);
term = optarg;
+ settings = NULL;
break;
case 'V':
puts(curses_version());
@@ -326,7 +332,10 @@ main(int argc, char **argv)
if (term == 0 || *term == '\0')
quit(2, "No value for $TERM and no -T specified");
- fd = save_tty_settings(&tty_settings);
+ if (settings)
+ fd = save_tty_settings(settings);
+ else
+ fd = STDOUT_FILENO;
if (setupterm(term, fd, &errret) != OK && errret <= 0)
quit(3, "unknown terminal \"%s\"", term);
@@ -334,7 +343,7 @@ main(int argc, char **argv)
if (cmdline) {
if ((argc <= 0) && !(is_clear || is_reset || is_init))
usage();
- ExitProgram(tput_cmd(fd, &tty_settings, opt_x, argc, argv));
+ ExitProgram(tput_cmd(fd, settings, opt_x, argc, argv));
}
while (fgets(buf, sizeof(buf), stdin) != 0) {
@@ -355,7 +364,7 @@ main(int argc, char **argv)
argvec[argnum] = 0;
if (argnum != 0
- && tput_cmd(fd, &tty_settings, opt_x, argnum, argvec) != 0) {
+ && tput_cmd(fd, settings, opt_x, argnum, argvec) != 0) {
if (result == 0)
result = 4; /* will return value >4 */
++result;
--
Lauri Tirkkonen | lotheac @ IRCnet
- tput(1) -T without a tty,
Lauri Tirkkonen <=