I'm trying to emulate a terminal using a C program in Linux and need my program to display a custom prompt while the program executes. Is there a way to display it using my C program? (I can always try to printf "My-prompt" every line manually, but I'm looking for a better way). Also I can't use any additional libraries other than the basic ones so GNU Readline library and editline library wouldn't work (as suggested in another thread). for example:
我正在尝试使用Linux中的C程序模拟终端,并且需要我的程序在程序执行时显示自定义提示。有没有办法使用我的C程序显示它? (我总是可以尝试手动打印每行“我的提示”,但我正在寻找更好的方法)。此外,我不能使用除基本库以外的任何其他库,因此GNU Readline库和编辑行库将无法工作(如另一个线程中所建议的)。例如:
user@mypc:~$ ./a.out
my_custom_prompt>3+5
my_custom_prompt>8
my_custom_prompt>exit
user@mypc:~$
3 个解决方案
#1
1
In your example, you already have got a terminal. You want to write a command-line interface with a prompt, not a terminal.
在您的示例中,您已经有了一个终端。您希望使用提示而不是终端编写命令行界面。
I can always try to printf "My-prompt" every line manually, but I'm looking for a better way
我总是可以尝试手动打印每行“我的提示”,但我正在寻找更好的方法
There’s nothing wrong with this approach. You have a loop which prints the prompt and waits for input afterwards. As Kunerd said in the comment, one line of code.
这种方法没有错。你有一个循环打印提示,然后等待输入。正如Kunerd在评论中所说,一行代码。
Normally, a prompt is printed to stderr
rather than stdout
. This has the advantage, that the prompt appears before a newline is written, as stderr
is unbuffered (and in combination with piping and redirection it seems reasonable to me, that this stuff doesn’t go to the same stream as the actual output).
通常,会向stderr而不是stdout打印提示。这样做的好处是,在写入换行符之前出现提示,因为stderr是无缓冲的(并且与管道和重定向相结合,对我来说这似乎是合理的,这些东西不会与实际输出相同)。
Also I can't use any additional libraries other than the basic ones so GNU Readline library and Editline library wouldn't work
另外我不能使用除基本库以外的任何其他库,因此GNU Readline库和Editline库不起作用
Doing this in a way strictly conforming to the C standard and not using any libraries but the standard one makes things like line editing (other than using backspace) or a command history (close to) impossible. If that’s OK for you, look for fgets
etc. and keep in mind, that stdin
is usually line-buffered.
以严格符合C标准并且不使用任何库但不使用任何库的方式执行此操作会使行编辑(使用退格除外)或命令历史(接近)成为不可能。如果你没问题,请查看fgets等,请记住,stdin通常是行缓冲的。
POSIX specifies some additional properties of terminals, see e.g. http://pubs.opengroup.org/onlinepubs/9699919799/. Maybe curses
is also of interest for you.
POSIX指定终端的一些附加属性,参见例如http://pubs.opengroup.org/onlinepubs/9699919799/。也许诅咒对你也很感兴趣。
#2
2
I believe what the OP wants is to simply have the "prompt" printed along with any program output, without having to add this manually every time. There is a way to do this, if you write a wrapper function on top of printf to do this, and call that instead of printf directly. Probably this will help: http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html
我相信OP想要的是简单地将“提示”与任何程序输出一起打印,而不必每次都手动添加。有一种方法可以做到这一点,如果你在printf上编写一个包装函数来执行此操作,并直接调用而不是printf。可能会有所帮助:http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html
#1
1
In your example, you already have got a terminal. You want to write a command-line interface with a prompt, not a terminal.
在您的示例中,您已经有了一个终端。您希望使用提示而不是终端编写命令行界面。
I can always try to printf "My-prompt" every line manually, but I'm looking for a better way
我总是可以尝试手动打印每行“我的提示”,但我正在寻找更好的方法
There’s nothing wrong with this approach. You have a loop which prints the prompt and waits for input afterwards. As Kunerd said in the comment, one line of code.
这种方法没有错。你有一个循环打印提示,然后等待输入。正如Kunerd在评论中所说,一行代码。
Normally, a prompt is printed to stderr
rather than stdout
. This has the advantage, that the prompt appears before a newline is written, as stderr
is unbuffered (and in combination with piping and redirection it seems reasonable to me, that this stuff doesn’t go to the same stream as the actual output).
通常,会向stderr而不是stdout打印提示。这样做的好处是,在写入换行符之前出现提示,因为stderr是无缓冲的(并且与管道和重定向相结合,对我来说这似乎是合理的,这些东西不会与实际输出相同)。
Also I can't use any additional libraries other than the basic ones so GNU Readline library and Editline library wouldn't work
另外我不能使用除基本库以外的任何其他库,因此GNU Readline库和Editline库不起作用
Doing this in a way strictly conforming to the C standard and not using any libraries but the standard one makes things like line editing (other than using backspace) or a command history (close to) impossible. If that’s OK for you, look for fgets
etc. and keep in mind, that stdin
is usually line-buffered.
以严格符合C标准并且不使用任何库但不使用任何库的方式执行此操作会使行编辑(使用退格除外)或命令历史(接近)成为不可能。如果你没问题,请查看fgets等,请记住,stdin通常是行缓冲的。
POSIX specifies some additional properties of terminals, see e.g. http://pubs.opengroup.org/onlinepubs/9699919799/. Maybe curses
is also of interest for you.
POSIX指定终端的一些附加属性,参见例如http://pubs.opengroup.org/onlinepubs/9699919799/。也许诅咒对你也很感兴趣。
#2
2
I believe what the OP wants is to simply have the "prompt" printed along with any program output, without having to add this manually every time. There is a way to do this, if you write a wrapper function on top of printf to do this, and call that instead of printf directly. Probably this will help: http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html
我相信OP想要的是简单地将“提示”与任何程序输出一起打印,而不必每次都手动添加。有一种方法可以做到这一点,如果你在printf上编写一个包装函数来执行此操作,并直接调用而不是printf。可能会有所帮助:http://www.ozzu.com/cpp-tutorials/tutorial-writing-custom-printf-wrapper-function-t89166.html