LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-27-2023, 07:12 AM   #1
codeandfire
LQ Newbie
 
Registered: Oct 2022
Posts: 4

Rep: Reputation: 2
Question Making getch() in ncurses read from the terminal device


As a project that I've set myself, I'm working on a simple pager, such as "less". Of course it would be best practice to build this pager on top of ncurses. It's my first time working with ncurses, though I've read the TLDP How-to on ncurses and I've reasonably familiarized myself with the API. Currently, I'm facing only one specific problem.

A pager should be able to take input both from filename arguments, as in
Code:
$ pager foo.txt bar.txt ...
and from standard input:
Code:
$ <command> | pager
But, a pager's key commands, such as Space for one screenful forward and the b key for one screenful backward, also have to be intercepted. And, if the pager's input comes from stdin then it can't read these keys by reading stdin.

My reference is an old Unix implementation of a pager, and the idea used there to intercept these keys is to read from the terminal device directly (and not from stdin).

As I understand, getch() is the standard way of getting character input in ncurses. Is there any way to make getch() read from the terminal device and not stdin?

Thanks!
 
Old 12-28-2023, 04:51 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,927

Rep: Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043
I can see a couple of alternatives.
  1. Duplicate the file descriptor for stdin to a new number and then reopen FD 0 to /dev/tty before calling initscr(). Then you use the new FD for reading stdin and use ncurses as normal.
  2. Open /dev/tty to a new FD and call newterm() and newwin() on it instead of using initscr(). You then have to use the window specific ncurses functions (such as wgetch()), but can treat stdin as normal.

Personally, I'd go with the second.

Last edited by GazL; 12-28-2023 at 04:55 AM. Reason: typo
 
2 members found this post helpful.
Old 12-29-2023, 05:07 AM   #3
codeandfire
LQ Newbie
 
Registered: Oct 2022
Posts: 4

Original Poster
Rep: Reputation: 2
This does the job, thanks GazL!
 
1 members found this post helpful.
Old 12-29-2023, 07:15 AM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,927

Rep: Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043Reputation: 5043
Quote:
Originally Posted by codeandfire View Post
This does the job, thanks GazL!
You're welcome. Just out of interest, which option did you go with?
 
Old 12-31-2023, 02:15 PM   #5
codeandfire
LQ Newbie
 
Registered: Oct 2022
Posts: 4

Original Poster
Rep: Reputation: 2
Well, as you said, the second - I did a small test which suggests that it will do the job. I'll soon post a working snippet (in case this thread is of use to anybody else). You are right of course in recommending the second; it would certainly be desirable to avoid meddling with stdin.
 
1 members found this post helpful.
  


Reply

Tags
ncurses



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getch() like feel under the Linux Terminal Fa773N_M0nK LinuxQuestions.org Member Success Stories 0 11-03-2012 09:33 AM
ncurses: making getstr and getch work together MALDATA Programming 2 01-03-2010 03:09 PM
nCurses getch() question alpha_gamma Programming 7 03-07-2007 07:30 PM
Equivalents of the getch() from conio.h in curses and ncurses aneeshm Programming 1 09-11-2004 07:19 AM
ncurses getch() deveraux83 Programming 3 08-06-2003 07:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration