LinuxQuestions.org
Visit Jeremy's Blog.
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 01-23-2009, 05:15 PM   #1
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375
Blog Entries: 24

Rep: Reputation: 43
curses terminal application


Hi. I'm programming a Linux console app in C with the ncurses library. Terminal independence is important in this case, so I'm having to pay a lot of attention to things like proper handling of colors, screen-resizing, etc. It would also be ideal to prevent the user from closing the program internally, i.e., without either shutting down his terminal, or sending it a SIGTERM/SIGKILL from outside the program.

My questions:
1. What happens to a curses-program if someone starts it using a non-standards-compliant terminal? (I can't think of an example of one to be honest, but I would think it could easily happen if someone was logged in over SSH or something...) Would the program crash, or would the users display be garbled? The man page for initscr says that "If errors occur, initscr writes an appropriate error message to standard error and exits", but provides no information as to what constitutes an error.

2. Is there someway to detect before-hand whether or not initscr is going to work? So that you could, say, provide functionality through a standard CLI interface instead?

Bonus question:

3. Is it possible to replace the monitor and keyboard on my Debian box with a teleprinter? (Really, I seriously want to know...)
 
Old 01-23-2009, 07:42 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Read all about this in the ncurses man page. That will tell you of the significance of the TERM environment variable, and how that ties in with the /usr/share/terminfo directory. That page will also tell you about the TERMINFO environment variable, if you wish to create your own terminfo directory.

initscr() looks at the TERMINFO environment variable if it is defined, and also at the TERM variable. If the appropriate file contains a valid terminal definition, you can rely on initscr() succeeding. If it doesn't, you can rely on initscr() failing.

There are no standards involved except the format of the terminal definition file.

Hope this helps.
 
Old 01-24-2009, 01:37 AM   #3
CoderMan
Member
 
Registered: Jan 2009
Location: Gemini Capsule 25164
Distribution: Gentoo
Posts: 375

Original Poster
Blog Entries: 24

Rep: Reputation: 43
That helps a little, but doesn't solve my problem. Let me explain:

My program runs fine in all actual terminal types I have tried. However, if before running the program I set the TERM environment variable to "blahblahgoogly", and try to run the program, curses exits upon initscr with this error:

Code:
Error opening terminal: blahblahgoogly.
This, of course, makes perfect sense. However, if I try the same thing before running the emacs text-editor, I get this:

Code:
emacs: Terminal type blahblahgoogly is not defined.
If that is not the actual type of terminal you have,
use the Bourne shell command `TERM=... export TERM' (C-shell:
`setenv TERM ...') to specify the correct type.  It may be necessary
to do `unset TERMINFO' (C-shell: `unsetenv TERMINFO') as well.
This long, detailed response message from emacs indicates that the emacs developers have some kind of control over /how/ the program responds to the undefined terminal type.

What I want to know is if I can do the same thing with my curses program -- output a customized message, or even do something more useful, instead of just the one-liner and an exit message that I get now.

And if this info is in the ncurses, initscr, or terminfo man pages, please let me know which section because I seem to be missing it.
 
Old 01-24-2009, 04:56 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
I thought I had explained this, but I've been off my feed lately and sometimes that affects my writing. :)

First, to review, this is what initscr() does:
Quote:
initscr() looks at the TERMINFO environment variable if it is defined, and also at the TERM variable. If the appropriate file contains a valid terminal definition, you can rely on initscr() succeeding. If it doesn't, you can rely on initscr() failing.
So if that's what initscr() does, and you want to avoid having initscr() blowing up on you, do all those things in your own C code before calling initscr().

I found a detailed description of how to use those two environment variables in the man page for ncurses. That man page also describes in detail the layout of the terminfo directory, which you'll need to know if you want to determine whether a particular terminal is defined.

I haven't actually looked at the emacs code, but I'm confident that this is how emacs does it; it's very simple to do.

In fact, you can go emacs one better. If you detect that TERMINFO is not defined, you can refrain from sending out the final sentence of the error message you quoted. The emacs guys were just being lazy, which is unusual; emacs has a quite rich (and arguably nice) feature set.
 
  


Reply



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
how to run an application from terminal mode 450Nappa Linux - Newbie 12 09-22-2008 03:51 PM
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
terminal based ftp application linuxmandrake Linux - Software 2 08-11-2006 08:13 AM
getting the time in C/C++ in a terminal application Error1312 Programming 3 02-24-2004 10:58 AM
Curses - Colour in terminal windows GrepKing Programming 0 01-29-2003 06:11 PM

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

All times are GMT -5. The time now is 01:55 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