使用system()函数时,“stty: stdin不是终端”

时间:2022-06-18 15:11:06

We're reading a file from stdin into file_buffer, and then stepping into a method more.

我们正在从stdin中读取文件到file_buffer,然后进一步进入一个方法。

As soon as we use system("stty cbreak -echo");, the output prints "stty: stdin isn't a terminal" and doesn't set our terminal to the settings we asked for.

一旦我们使用system(“stty cbreak -echo”),输出将打印“stty: stdin不是终端”,并且不会将我们的终端设置为我们需要的设置。

This problem only exists when we use standard in. If we use a file argument, the program works fine -- the terminal settings get set, and there is no error message.

这个问题只存在于我们使用standard in时。如果我们使用一个文件参数,程序可以正常工作——终端设置设置设置,并且没有错误消息。

So, this is okay: myprogram file1.txt

好的,myprogram file1.txt

But this is not: myprogram < file1.txt

但这不是:myprogram < file1.txt

Either way the contents are being read into file_buffer before being used at all. What the heck is wrong with using stty if we're taking input from stdin??

无论哪种方式,在使用之前都要先将内容读入file_buffer。如果我们从stdin中获取输入,使用stty有什么问题?

2 个解决方案

#1


1  

When the standard input is a file, it isn't a terminal, so setting terminal attributes on stty's standard input won't work.

当标准输入是一个文件时,它不是一个终端,所以在stty的标准输入上设置终端属性是行不通的。

It sounds daft at first, but you will probably find that you can use either stdout or stderr as the input for stty and it will adjust the terminal. Therefore:

它起初听起来很傻,但您可能会发现,您可以使用stdout或stderr作为stty的输入,它将调整终端。因此:

system("stty cbreak -echo <&2");

is likely to set the terminal characteristics. If you have a GNU version of stty, you could also use:

很可能设置终端特征。如果您有一个GNU版本的stty,您也可以使用:

system("stty -F /dev/stderr cbreak -echo");

or substitute /dev/stdout or /dev/tty for /dev/stderr. You could also use any of the named devices instead of the &2 in the redirection in the first variant.

或者用/dev/stdout或者/dev/tty来替换/dev/stderr您也可以使用任何指定的设备,而不是&2在第一个变体的重定向。

#2


1  

If you use input redirection or pipes, then stdin is not a TTY.

如果您使用输入重定向或管道,那么stdin不是TTY。

You can use isatty to check for that.

你可以用isatty来检查。

#1


1  

When the standard input is a file, it isn't a terminal, so setting terminal attributes on stty's standard input won't work.

当标准输入是一个文件时,它不是一个终端,所以在stty的标准输入上设置终端属性是行不通的。

It sounds daft at first, but you will probably find that you can use either stdout or stderr as the input for stty and it will adjust the terminal. Therefore:

它起初听起来很傻,但您可能会发现,您可以使用stdout或stderr作为stty的输入,它将调整终端。因此:

system("stty cbreak -echo <&2");

is likely to set the terminal characteristics. If you have a GNU version of stty, you could also use:

很可能设置终端特征。如果您有一个GNU版本的stty,您也可以使用:

system("stty -F /dev/stderr cbreak -echo");

or substitute /dev/stdout or /dev/tty for /dev/stderr. You could also use any of the named devices instead of the &2 in the redirection in the first variant.

或者用/dev/stdout或者/dev/tty来替换/dev/stderr您也可以使用任何指定的设备,而不是&2在第一个变体的重定向。

#2


1  

If you use input redirection or pipes, then stdin is not a TTY.

如果您使用输入重定向或管道,那么stdin不是TTY。

You can use isatty to check for that.

你可以用isatty来检查。