C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-13-2006, 10:34 PM   #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
hkluis is offline   Reply With Quote
Old 10-13-2006, 11:04 PM   #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
zx-1 is offline   Reply With Quote
Old 10-14-2006, 01:07 AM   #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.
hkluis is offline   Reply With Quote
Old 10-14-2006, 01:46 AM   #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.
hkluis is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22