简体   繁体   English

反引号之间执行的程序的termcap

[英]termcap of program executed between backticks

I'm trying to make a C program to select options. 我试图制作一个C程序来选择选项。 It works if I run it this way: 如果我这样运行,它将起作用:

./select choice{1..5}
☐ choice1  ☐ choice3  ☐ choice5
☐ choice2  ☐ choice4

# outputs "choice1 choice2" on stdout

But if I run it between backticks, it's a hell 但是如果我在反引号之间运行它,那真是一个地狱

`./select choice{1..5}` > choices.txt
☐ choice1☐ choice2☐ choice3☐ choice4☐ choice5

# Write "choice1 choice2" in `choices.txt`

I need to be able to retrieve selected options. 我需要能够检索选定的选项。 That is why I've done all my outputs to a file I've opened with 这就是为什么我将所有输出保存到打开的文件中的原因

int tty = open("/dev/tty", O_RDWR);
/* Do my program using outputs on fd `tty` */
printf("%s\n", get_results());

I think this is related with the use of tgoto in my code, to move the writing cursor on the screen. 我认为这与在代码中使用tgoto来移动屏幕上的书写光标有关。

if ((cm = tgetstr("cm", NULL)) == NULL)
    return (-1);
tputs(tgoto(cm, x, y), fd, &my_putchar);
return (0);

I've seen that using isatty(1) returns 0 when executed between backticks, and 1 if executed directly... So, is there a way for me to move the cursor and keep my formating in both situations? 我已经看到,在反引号之间执行时,使用isatty(1)返回0,如果直接执行,则返回1 ...那么,在两种情况下,有没有办法让我移动光标并保持格式?

Thank you for your time 感谢您的时间

The fragment of code shown in the question is using the termcap interface (not curses ). 问题中显示的代码片段使用的是termcap接口(不是curses )。 It uses 它用

tputs(tgoto(cm, x, y), fd, &my_putchar);

where my_putchar is likely writing to the standard output, like the rest of the program (using printf ). 其中my_putchar可能会写入标准输出,就像程序的其余部分一样(使用printf )。 If the program were altered to write all of its screen output to the standard error, then at the end it could write its result to the standard output, simplifying scripting. 如果更改了程序以将其所有屏幕输出都写入标准错误,则最后可以将其结果写入标准输出,从而简化了脚本编写。

There is no need to open /dev/tty ; 无需打开/dev/tty ; rather, replacing 而是替换

printf("choice%d", n);

by 通过

fprintf(stderr, "choice%d", n);

and of course changing my_putchar to something like 当然将my_putchar更改为

int my_putchar(int c) { return fputc(c, stderr); }

is the way to go. 是要走的路。

If this were a curses application, one would change the output streams using newterm rather than initscr . 如果这是一个curses应用程序,则可以使用newterm而不是initscr更改输出流。 dialog uses newterm , of course. 对话框当然使用newterm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM