简体   繁体   English

如何写入 C 中的输入部分?

[英]How to write to input part in C?

I'm trying to write a mini shell in C for a school project, and what I want to do is doing a sort of command history (like in shell), when I press the UP key it writes the previous input into the input part, DOWN does the opposite, etc..., and you can edit it before pressing enter to send it to the program, like this (sorry for the bad english): [] represents the user cursor我正在尝试为学校项目在 C 中编写一个迷你 shell 用于学校项目,而我想做的是做一种命令历史记录(如在 shell 中),当我按下 UP 键时,它将先前的输入写入输入部分, DOWN 则相反,等等...,您可以在按 Enter 之前对其进行编辑以将其发送到程序,如下所示(抱歉英语不好): [] 代表用户 cursor

my_shell$ some input wrote by me
my_shell$ []
my_shell$ some other input
my_shell$ []
and now if I press UP
my_shell$ some other input[]
If I press UP again
my_shell$ some input wrote by me[]

I'm allowed to use termcaps and some other functions isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr, tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs .我可以使用 termcaps 和其他一些函数isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr, tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs

The problem is I can't understand the documentation of ioctl and tty functions, and I can't find well explained tutorials on these functions with examples, and I can't find the documentation for what I'm trying to do with them.问题是我无法理解ioctltty函数的文档,我找不到关于这些函数的详细解释的教程和示例,也找不到我正在尝试使用它们的文档。

Can someone explain me those functions in an understandable way?有人可以以易于理解的方式向我解释这些功能吗? And how should I apply them for what I'm trying to do (I'm searching for a Linux-MacOs compatibility way) Thanks for your help.以及我应该如何将它们应用于我正在尝试做的事情(我正在寻找一种 Linux-MacOs 兼容方式)感谢您的帮助。

What you are asking is non trivial and will require a fair amount of work.您要问的不是微不足道的,并且需要大量的工作。 Basically, what you need to do is use tcsetattr to put the terminal into non-canonical mode where the terminal's input line buffering is disabled and every keystroke will be returned immediately rather than waiting for a newline.基本上,您需要做的是使用tcsetattr将终端置于非规范模式,其中终端的输入行缓冲被禁用,并且每次击键都将立即返回,而不是等待换行符。 You will then have to process every keystroke yourself, including backspace/delete and up/down arrows.然后,您必须自己处理每个按键,包括退格/删除和向上/向下箭头。 Because of what you want to do with line editing, you'll probably also have to disable echoing and do the echo yourself.由于您想对行编辑进行什么操作,您可能还必须禁用回显并自己执行回显。

You'll need to maintain the current line buffer yourself, and you'll also need a data structure that stores all the older lines of input (the history) and when an up-arrow is hit, you'll need to erase what you've currently buffered for the input, and erase it from the screen, then copy the history into your current buffer and echo it to the terminal.您需要自己维护当前行缓冲区,并且您还需要一个数据结构来存储所有较旧的输入行(历史),并且当向上箭头被击中时,您需要擦除您的内容'当前已为输入缓冲,并将其从屏幕上擦除,然后将历史记录复制到当前缓冲区并将其回显到终端。

Another complication is that keys like up-arraow and down-arrow are not part of ascii, so won't be read as a single byte -- instead they'll be multibyte escape sequences (likely beginning with an ESC ( '\x1b' ) character).另一个复杂之处是像向上箭头和向下箭头这样的键不是 ascii 的一部分,因此不会被读取为单个字节——而是它们将是多字节转义序列(可能以 ESC 开头( '\x1b' ) 特点)。 You can use tgetstr to quesry the terminal database to figure out what they are, or just hardcode your shell to use the ANSI sequences which are what almost all terminals you will see these days use.您可以使用tgetstr来查询终端数据库以找出它们是什么,或者只是硬编码您的 shell 以使用您现在看到的几乎所有终端都使用的ANSI 序列

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

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