Linenoise: Ctrl-z(SIGSTOP)๊ฐ€ ์ œ๋Œ€๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2017๋…„ 05์›” 18์ผ  ยท  8์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: antirez/linenoise

ํ„ฐ๋ฏธ๋„์—์„œ ctrl-z ๋ฅผ ๋ˆ„๋ฅด๋ฉด(SIGSTOP ์ „์†ก) ์ž…๋ ฅ ๋ผ์ธ์— โ‰ค ๊ฐ€ ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์ด์œ ์™€ ๋ฐฉ๋ฒ•์€ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘์ง€๋˜๊ณ  ์‰˜๋กœ ๋Œ์•„๊ฐ€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ 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

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

Steve Bennett๊ฐ€ linenoise ํฌํฌ์—์„œ ๋ณด์—ฌ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ CTRL+Z ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/msteveb/linenoise/commit/29aedbd22b5e3529aff1f85ec1b3164118b83291

ํ˜„์žฌ ๋งˆ์Šคํ„ฐ(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 ๋Œ“๊ธ€

ur ํ”„๋กฌํ”„ํŠธ๋ฅผ 0.0์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

@krux02 ๋‚˜๋Š” ๋‹น์‹ ์ด C++๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค. @yhirose ์—๋Š” c++๋กœ ์ด์‹๋œ linenoise๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: https://github.com/yhirose/cpp-linenoise. ๋˜ํ•œ UTF8์ด ํ™œ์„ฑํ™”๋ฉ๋‹ˆ๋‹ค.

linenoise_example ์ฝ”๋“œ์˜ ์š”์ง€๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ?

์‚ฌ์‹ค ์ €๋Š” C++๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋งŽ์€ C++๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋ฐํ–ˆ์ง€๋งŒ ์ง€๊ธˆ์€ Nim์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‚ด ๋ฌธ์ œ๊ฐ€ Nim๊ณผ ๊ด€๋ จ๋œ ์–ด๋–ค ๊ฒƒ์—์„œ๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— C++ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

๋‚ด ๊ฒฝํ—˜์— ๋”ฐ๋ฅด๋ฉด C API๋Š” ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. C++ ๋ฒ„์ „์€ ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ utf8์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์ „ํ˜€ ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ๋†“์นœ ๊ฒƒ์€ ์ด๋ฏธ readline์—์„œ ๋งค์šฐ ์นœ์ˆ™ํ•œ ๋งŽ์€ ํ‚ค๋ณด๋“œ ๋ช…๋ น์ž…๋‹ˆ๋‹ค.

๋‚˜๋Š” linenoise์˜ ๋ž˜ํ•‘๋œ ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฒƒ์ด ์ •๋ง๋กœ ๋‹น์‹ ์—๊ฒŒ ๋งŽ์€ ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์‹ ํ•  ์ˆ˜ ์—†์ง€๋งŒ ์ด๊ฒƒ์€ ๋‚ด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์ด๋ฉฐ linenoise ์ง€ํ–ฅ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค: https://github.com/krux02/opengl -sandbox/blob/master/examples/console.nim

๊ทธ ํŒŒ์ผ์˜ ์•„์ด๋””์–ด๋Š” Nim์˜ ๋งคํฌ๋กœ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ น ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž„์˜์˜ ๊ธฐ๋Šฅ์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ช…๋ น ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” linenoise์ž…๋‹ˆ๋‹ค.

@krux02

๊ธ€์Ž„์š”, ์ €๋Š” ์ „๋ฌธ์ ์œผ๋กœ ์–ธ์–ด์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š์ง€๋งŒ ๊ฐœ์ธ์ ์œผ๋กœ ์ €๋„ C ์†๋ฌผ์ž…๋‹ˆ๋‹ค. ํ™๋‹น๋ฌด: ์ €๋Š” C๋ฅผ ์‚ฌ๋ž‘ํ•ฉ๋‹ˆ๋‹ค.

Linenoise๋Š” ๋””์ž์ธ ๋ชฉํ‘œ์ธ ๊ตฐ๋”๋”๊ธฐ ์—†๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๋Š” ์‚ฌ์‹ค์„ ๊ธฐ์–ตํ•˜์‹ญ์‹œ์˜ค.

๋ถˆํ–‰ํžˆ๋„ ์ด๊ฒƒ์„ ์กฐ์‚ฌํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฃผ๋ง๊นŒ์ง€ ์‹œ๊ฐ„์ด ์—†์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ˆ„๊ตฐ๊ฐ€ ํ•ด๊ฒฐํ•ด ์ฃผ์ง€ ์•Š๋Š” ํ•œ ์—ด์–ด ๋‘์‹ญ์‹œ์˜ค.

์•ผ! ๋‹น์‹ ์€ ๋‹˜์˜ ์ฒซ ํŽ˜์ด์ง€์— ์žˆ์Šต๋‹ˆ๋‹ค! ์ž˜ํ–ˆ์–ด ์•„๋ฅด๋„ค!

@krux02

์ข‹์•„, ์ด C ํ”„๋กœ์ ํŠธ๋Š” ๋‹น์‹ ์ด ์„ค๋ช…ํ•˜๋Š” ๋™์ž‘์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค:

https://github.com/Sonophoto/shellnoise

๊ทธ๋Ÿฌ๋‚˜ Ctrl-C ๋ฐ Ctrl-D๋Š” ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

Linenoise๋Š” Ctrl-Z๋ฅผ ์‰˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๋Œ€์‹  ์ž…๋ ฅ์œผ๋กœ ์บก์ฒ˜ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค(์ด ์˜ˆ์—์„œ๋Š” ๋‹จ์ˆœํžˆ ํ„ฐ๋ฏธ๋„์— ๋ฌธ์ž์—ด๋กœ ์—์ฝ”๋ฉ๋‹ˆ๋‹ค).

์•„ ๊ทธ๋ฆฌ๊ณ  ์ด ํ”„๋กœ์ ํŠธ๋Š” linenoise ๋‹ค์ค‘ ๋ผ์ธ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

@krux02

์•ฝ๊ฐ„์˜ ํŒŒ๊ณ ๋ฅผ ํ†ตํ•ด ๊ฐ€์žฅ ์ข‹์€ ์ถ”์ธก์€ linenoise๊ฐ€ ํ„ฐ๋ฏธ๋„์„ ์›์‹œ ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•˜๊ณ  ๊ทธ ์„ค์ •์œผ๋กœ ์ธํ•ด ASCII ์ œ์–ด ์ฝ”๋“œ ๋Œ€์‹  ๋ฌธ์ž๋กœ Ctrl-Z๋ฅผ ์บก์ฒ˜ํ•˜๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

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

U+001A๋Š” UTF8๋กœ ์ธ์‡„ํ•  ๋•Œ Ctrl-Z ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

@hoelzro ๊ฐ€ ์ด ํŒจ์น˜๋ฅผ

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

Steve Bennett๊ฐ€ linenoise ํฌํฌ์—์„œ ๋ณด์—ฌ์ค€ ๊ฒƒ์ฒ˜๋Ÿผ CTRL+Z ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/msteveb/linenoise/commit/29aedbd22b5e3529aff1f85ec1b3164118b83291

ํ˜„์žฌ ๋งˆ์Šคํ„ฐ(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 ๋“ฑ๊ธ‰