0

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.

LotoLo
  • 327
  • 4
  • 17
  • 1
    The TERM-variable exists exactly to determine the terminal type to be used, there is barely another general way to guess the terminal type and set TERM. You could let the user choose a terminal if it is unset or try to default to `vt100`, since this is often used. – Ctx Jan 23 '17 at 13:17
  • I'm trying to get a more portable than possible application, so I figure if I choose to set to default vt100 I can have some day a portability issue? – LotoLo Jan 23 '17 at 13:21
  • No, it simply will not work as expected if the terminal emulation used is incompatible with vt100. The portable way is: Rely on the setting of the TERM environment variable; if it is not set, the user has to fix his environment – Ctx Jan 23 '17 at 13:22
  • Don't destroy your `TERM` variable. If you cannot resist using `env -i`, use it this way: `env -i TERM=$TERM bash` – Zsigmond Lőrinczy Jan 30 '17 at 18:19

2 Answers2

1

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:

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

It is (somewhat) safe to set TERM=vt100 as default (Ctx's suggestion), as most terminal emulators are set to emulate that anyway. I'd recommend you to print a warning in this case, though.

Min4Builder
  • 148
  • 1
  • 8