Search code examples
ctermiostermcap

May I set TERM environment variable safely?


I'm writing a program that uses termcaps and I need to know which kind of terminal I am using.
I am aware I can get TERM variable via getenv("TERM"), but I can launch my program with "$ env -i ./myprog" and TERM will not be set.

So how can I determine which terminal type must I use?
May I safely set TERM variable to xterm/xterm-256color in my application?
Will it cause non-portability issues?
Is there a method to do such (get termtype) safely?
I've red many manuals (getty - getttab - tty - ttys) and posts but I can't spot any solution.

I'm also worried because if I launch a shell (like zsh or tcsh) I have issues with some keys.

For example launching zsh like so:
$env -i zsh
will cause troubles with arrows and any keys implying termcaps (even Ctr-d).
Instead bash and tcsh will behave normally on many keys but not all.


Solution

  • If you're actually using termcap (and not some minimal implementation such as busybox), you're likely using a system that provides tset, which can offer the user a default choice for TERM that can be modified.

    Something like this:

    eval `tset -s vt100`
    

    in the shell initialization would work.

    Actually tset isn't limited to termcap-systems, but that's where it started.

    Further reading: