Thread: How to have fgets() return edited line like in shell?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    How to have fgets() return edited line like in shell?

    When I'm in a Linux bash shell, pressing the left and right arrow keys can be used to move the cursor on the command line, while the up and down arrow key can recall command history. But if I write a program that reads an input line using gets() as below, pressing those keys just produces the raw escape sequence. Is there a way to have the line edited, like in the shell, before it is returned by fgets()? Is there an alternative function?

    Code:
     while (fgets(buf, sizeof(buf), stdin))
            do_command(buf);

    I tried first calling noraw() and nocbreak(), but it does make a difference.

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Look into GNU Readline.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Thank you. That works and is very simple.

    You would think there would be an option in the shell to have all fgets(stdin) automatically inherit this behavior, so that it would not be necessary to modify the apps.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Actually, I came across "rlfe" which is a wrapper for readline. That might just do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM