Linenoise: Ctrl-z (SIGSTOP) 处理不当。

创建于 2017-05-18  ·  8评论  ·  资料来源: antirez/linenoise

当我在终端上按ctrl-z (发送 SIGSTOP)时,它只会在输入行上放一个 。 我不知道为什么以及如何,但程序应该停止并返回到 shell。 然后使用命令fg我应该能够回到这个过程。

┬─[arne@arne-thinkpad:~/proj/c++/linenoise]─[18:05:53]
╰─>$ ./linenoise_example 
hello> foo
echo: 'foo'
hello> 
echo: ''
hello> 

┬─[arne@arne-thinkpad:~/proj/c++/linenoise]─[18:05:54]
╰─>$ python
Python 3.6.1 (default, Mar 27 2017, 00:27:06) 
[GCC 6.3.1 20170306] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = 17
'python' has gestoppt

┬─[arne@arne-thinkpad:~/proj/c++/linenoise]─[18:06:07]
│ 1 28968   0%  gestoppt    python
╰─>$ fg
Job 1, 'python' in den Vordergrund schicken

>>> print(foo)
17

最有用的评论

有一种非常简单的方法来实现 CTRL+Z 功能,如 Steve Bennett 在他的 linenoise 分支中所示: https :

我已经修改了补丁,因此它适用于当前主服务器(https://github.com/antirez/linenoise/commit/4a961c0108720741e2683868eb10495f015ee422),结果如下:

commit d9400774549967cda04cfa04f56ed956cd79147c
Author: Steve Bennett <[email protected]>
Date:   Mon Jan 1 13:11:16 2018 +1000

    Enable ^Z (SUSP) support

    Allows the current process to be backgrounded and then resumed.

    Signed-off-by: Steve Bennett <[email protected]>

    (adaptations)

    Signed-off-by: Alexander F. Mayer <[email protected]>

diff --git a/linenoise.c b/linenoise.c
index 10ffd71..d861505 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -109,6 +109,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <sys/stat.h>
@@ -168,6 +169,7 @@ enum KEY_ACTION{
    CTRL_T = 20,        /* Ctrl-t */
    CTRL_U = 21,        /* Ctrl+u */
    CTRL_W = 23,        /* Ctrl+w */
+   CTRL_Z = 26,        /* Ctrl+z */
    ESC = 27,           /* Escape */
    BACKSPACE =  127    /* Backspace */
 };
@@ -830,6 +832,16 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
         case CTRL_C:     /* ctrl-c */
             errno = EAGAIN;
             return -1;
+        case CTRL_Z:     /* ctrl-z */
+#ifdef SIGTSTP
+            /* send ourselves SIGSUSP */
+            disableRawMode(STDIN_FILENO);
+            raise(SIGTSTP);
+            /* and resume */
+            enableRawMode(STDIN_FILENO);
+            refreshLine(&l);
+#endif
+            continue;
         case BACKSPACE:   /* backspace */
         case 8:     /* ctrl-h */
             linenoiseEditBackspace(&l);

所有8条评论

你如何让你的提示看起来像 0.0

它是

@krux02我看到你在使用 c++, @yhirose有一个已移植到 c++ 的 linenoise: https :

您能分享一下 linenoise_example 代码的要点吗?

其实我没有使用C++。 我曾经编写过很多 C++,但现在我编写 Nim 。 仅举个例子,我想确保我的问题不是来自 Nim 相关的任何事情,所以我创建了一个 c++ 项目。

根据我的经验,C API 很好,我不需要 C++ 版本。 此外,我在这个库中使用 utf8 完全没有问题。 我错过了许多我在 readline 上已经非常熟悉的键盘命令。

我使用 linenoise 的包装版本,所以我不确定它对你来说是否真的很有价值,但这是我使用的例子,它不是面向 linenoise 的: https :

该文件中的想法是使用 Nim 的宏系统在命令解释器中提供任意函数。 但在引擎盖下,命令解释器是 linenoise。

@krux02

好吧,我在专业上是语言不可知论者,但就我个人而言,我也是一个 C 势利小人:脸红:我喜欢 C。

请记住,Linenoise 是一个简单的骨架,没有多余的库,这是一个设计目标。

不幸的是,我想调查一下这个问题,直到本周末才有时间,除非有人为您解决,否则请保持打开状态。

嘿! 你在 Nim 的头版! 干得好阿恩!

@krux02

好的,这个 C 项目显示了您所描述的行为:

https://github.com/Sonophoto/shellnoise

然而,Ctrl-C 和 Ctrl-D 表现正常。

Linenoise 似乎将 Ctrl-Z 作为输入捕获(在本例中,它只是作为字符串回显到终端上),而不是将其传递给 shell。

哦,请注意,该项目使用的是 linenoise 多行输入。

@krux02

我最好的猜测是 linenoise 将终端置于原始模式,并且该设置导致它捕获 Ctrl-Z 作为字符而不是 ASCII 控制代码。

https://github.com/antirez/linenoise/blob/master/linenoise.c#L217 -L251

当以 UTF8 打印时,U+001A 是 Ctrl-Z 代码

@hoelzro提出了这个补丁,但它没有正确处理 Ctrl-C。 然而,这也可以被捕获并简单地在 linenoise 中处理。

https://github.com/hoelzro/p6-linenoise/commit/7d0bb83cab34ca58f7a487b5df30736d058112cf

有一种非常简单的方法来实现 CTRL+Z 功能,如 Steve Bennett 在他的 linenoise 分支中所示: https :

我已经修改了补丁,因此它适用于当前主服务器(https://github.com/antirez/linenoise/commit/4a961c0108720741e2683868eb10495f015ee422),结果如下:

commit d9400774549967cda04cfa04f56ed956cd79147c
Author: Steve Bennett <[email protected]>
Date:   Mon Jan 1 13:11:16 2018 +1000

    Enable ^Z (SUSP) support

    Allows the current process to be backgrounded and then resumed.

    Signed-off-by: Steve Bennett <[email protected]>

    (adaptations)

    Signed-off-by: Alexander F. Mayer <[email protected]>

diff --git a/linenoise.c b/linenoise.c
index 10ffd71..d861505 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -109,6 +109,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <sys/stat.h>
@@ -168,6 +169,7 @@ enum KEY_ACTION{
    CTRL_T = 20,        /* Ctrl-t */
    CTRL_U = 21,        /* Ctrl+u */
    CTRL_W = 23,        /* Ctrl+w */
+   CTRL_Z = 26,        /* Ctrl+z */
    ESC = 27,           /* Escape */
    BACKSPACE =  127    /* Backspace */
 };
@@ -830,6 +832,16 @@ static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen,
         case CTRL_C:     /* ctrl-c */
             errno = EAGAIN;
             return -1;
+        case CTRL_Z:     /* ctrl-z */
+#ifdef SIGTSTP
+            /* send ourselves SIGSUSP */
+            disableRawMode(STDIN_FILENO);
+            raise(SIGTSTP);
+            /* and resume */
+            enableRawMode(STDIN_FILENO);
+            refreshLine(&l);
+#endif
+            continue;
         case BACKSPACE:   /* backspace */
         case 8:     /* ctrl-h */
             linenoiseEditBackspace(&l);

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

fatcerberus picture fatcerberus  ·  5评论

ozancaglayan picture ozancaglayan  ·  21评论

JelteF picture JelteF  ·  8评论

ghost picture ghost  ·  4评论

denisvm picture denisvm  ·  9评论