Readline() 함수 사용하여 Exit 할경우 문제 (source 소스 포함)

안상준의 이미지

아래와 같이 30초후에 Exit 되면 그 다음부터는 readline 함수의 영향으로 입력은 되지만 입력된 값은 안보입니다. 그렇지만 실제로는 그 값이 입력되어 있습니다. 그리고 enter를 치면 enter가 무시됩니다. 아마 readline의 영향인거 같습니다. readline 함수가 리턴할때는 \n을 제외한 문자열을 리턴합니다.

어떻게 해결할 수 있나요? 도움 부탁드립니다.

컴파일 방법.
gcc -o readline_exit readline_exit.c -Wall -lreadline -lncurses

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>


int g_iCnt = 0;

void alarm_handler (int sig)
{
	
	if (g_iCnt > 30) {
		printf("program is exit\n");
		exit(0);
	}
	
	g_iCnt++;
	
	signal (SIGALRM, alarm_handler);
	alarm (1);
}

int main(int argc, char *argv[])
{
	char	prompt[64];
	char	*line;
	
	signal (SIGALRM, alarm_handler);
	alarm (1);
	
	while(1) {
		strcpy(prompt, "(input)$ ");
		line = readline (prompt);
		
		printf("line = [%s]\n", line);
		free(line);
	}
}
sunyzero의 이미지

반향값(echo)를 무시한다면 stty로 다시 바꿔주면 됩니다.

stty echo

라고 명령을 내리시면 됩니다.

========================================
* The truth will set you free.

안상준의 이미지

답변에서 아이디어를 얻어서 tty를 원상태로 돌려 놓을 수 있도록 해서 해결하였습니다. 답변 감사합니다.

해결한 코드는 아래입니다. readline을 하기 전에 tty 세팅을 저장했다가 exit 하기전에 처음 tty 상태로 reset를 하였습니다.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <readline/readline.h>
#include <readline/history.h>

struct termios	save_termios;	
int tty_init(int fd)
{
	if (tcgetattr(fd, &save_termios) < 0) {
		printf("tcgetattr error : %s\n", strerror(errno));
		return -1;
	}
	return 0;
}

int tty_reset(int fd)
{
	if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) {
		printf("tcsetattr error : %s\n", strerror(errno));
		return -1;
	}
	
	return 0;
}
int g_iCnt = 0;

void alarm_handler (int sig)
{
	
	if (g_iCnt > 10) {
		tty_reset(STDIN_FILENO);
		printf("program is exit\n");
		exit(0);
	}
	
	g_iCnt++;
	
	signal (SIGALRM, alarm_handler);
	alarm (1);
}

int main(int argc, char *argv[])
{
	char	prompt[64];
	char	*line;
	
	tty_init(STDIN_FILENO);
	signal (SIGALRM, alarm_handler);
	alarm (1);
	
	while(1) {
		strcpy(prompt, "(input)$ ");
		line = readline (prompt);
		
		printf("line = [%s]\n", line);
		free(line);
	}
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.