ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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...)
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.