간단한 unix shell을 만들고 있습니다

gateway21c의 이미지

학교 과제로 간단한 unix shell을 만들고 있는데요

학교에서 과제의 목표치로 제시한 spec이상의 것을 한번 만들어 보려고
다음과 같은 추가 기능을 shell에 구현해보고자 노력하고 있습니다.

1. 명령줄 history 기능 ( arrow up & down key를 통해 명령어 치환 )
2. auto complete 기능 ( path나 command를 입력하다가 tab key를 치면, 명령어 자동 완성 )
3. 재 지향 기능..

등등..

unix posix에는 getch() 라는 함수가 없어서..( getchar()계열만 있죠, 즉 '\n'들어와야 입력 종료 )
termio.h를 가지고 noncanonical mode로 수정하고 별짓을 다해서..
겨우 명령어를 입력하는 도중에 특수키( arrow key, tab key )가 입력되었을때에 대한 적절한 반응...까지는 구현이 되었는데요

또 하나의 커다란 벽에 진입..

명령어를 입력하다가,,,,
arrow key를 누르면 현재 입력하던 명령어를 지우고 history buffer에 있는 명령어로 치환을 해야 되는데..
이게 쉽지가 않네요

curses.h 같은 라이브러리는 되도록 쓰지 않을 생각이라...이 부분을 어떻게 하면 좋을지 모르겠네요

bash shell source code, vi source code등을 보고 이부분에 대한 idea를 얻고자 했으나
이들 프로그램은 거의 bare상태부터 짜여진거 같아..이해하는게 결코 쉽지도 않고
이들을 이해하는거 자체가 배보다 배꼽이 더 큰 일을 벌이는 거 같아..쉽지가 않습니다..

결론은,,, console 화면에서 arrow key를 누르면 현재 입력하던 명령어를 지우고 history buffer에 있는 명령어로
치환을 하는 방법이 없을까요?

ps ) 혹시나,,해서 fseek( stdout,,,,,,)도 시도해 보았으나..역시 안되네요 ㅋㅋ;;
고수님들 좋은 가르침을 부탁드립니다

우영의 이미지

readline이 바로 그와 같은 기능을 제공하는 library입니다.
readline을 가져다 쓰시거나 아니면 소스를 보면 도움이 되겠지요.

sebi184의 이미지

혹시 이것이 도움이 되실런지..-_-;;

gateway21c의 이미지

readline 소스는 사실 보긴 했는데
bash 소스 안에 readline이라는 소스가 있어서 훑어 봤는데
쉽게 이해가 잘 안되서;;;

의외로..printf("\b") 요놈으로 그럴싸하게 만들었습니다..ㅋㅋㅋ

답변들 감사합니다

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.