diff --git a/ChangeLog b/ChangeLog index 9ce62d65..b191420f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2016-05-01 Markus Gans + * Better terminal identification + 2016-04-30 Markus Gans * Improve 256 color terminal detection diff --git a/src/fterm.cpp b/src/fterm.cpp index 85228e6a..256d5f50 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -263,6 +263,63 @@ int FTerm::isConsole() && ((arg == KB_101) || (arg == KB_84)) ); } +//---------------------------------------------------------------------- +void FTerm::identifyTermType() +{ + // Import the untrusted environment variable TERM + const char* term_env = getenv(const_cast("TERM")); + if ( term_env ) + { + strncpy (termtype, term_env, sizeof(termtype) - 1); + return; + } + else if ( term_name ) + { + FILE *fp; + // fallback: look into /etc/ttytype or /etc/ttys + if ( (fp = fopen("/etc/ttytype", "r")) != 0 + || (fp = fopen("/etc/ttys", "r")) != 0 ) + { + char str[BUFSIZ]; + char *p, *type, *name; + + // get term basename + const char *term_basename = strrchr(term_name, '/'); + if ( term_basename == 0 ) + term_basename = term_name; + else + term_basename++; + + // read and parse the file + while ( fgets(str, sizeof(str) - 1, fp) != 0 ) + { + type = name = 0; // 0 == not found + p = str; + + while ( *p ) + { + if ( isspace(uChar(*p)) ) + *p = '\0'; + else if ( type == 0 ) + type = p; + else if ( name == 0 && p != str && p[-1] == '\0' ) + name = p; + p++; + } + if ( type != 0 && name != 0 && ! strcmp(name, term_basename) ) + { + strncpy (termtype, type, sizeof(termtype) - 1); + fclose(fp); + return; + } + } + fclose(fp); + } + } + // use vt100 if not found + strncpy (termtype, const_cast("vt100"), 6); +} + //---------------------------------------------------------------------- int FTerm::getScreenFont() { @@ -1584,7 +1641,6 @@ void FTerm::init() std::abort(); term_name = ttyname(stdout_no); - // -> possible fallback: look into /etc/ttytype for the type? // initialize terminal and Linux console init_console(); @@ -1608,12 +1664,8 @@ void FTerm::init() if ( isatty(stdout_no) ) opti_move->setBaudRate(int(baudrate)); - // Import the untrusted environment variable TERM - const char* term_env = getenv(const_cast("TERM")); - if ( term_env ) - strncpy (termtype, term_env, sizeof(termtype) - 1); - else - strncpy (termtype, const_cast("vt100"), 6); + // detect the type of the terminal + identifyTermType(); // initialize 256 colors terminals new_termtype = init_256colorTerminal(); diff --git a/src/fterm.h b/src/fterm.h index fcedfd24..fd4438f5 100644 --- a/src/fterm.h +++ b/src/fterm.h @@ -232,6 +232,7 @@ class FTerm static int openConsole(); static int closeConsole(); static int isConsole(); + static void identifyTermType(); static int getScreenFont(); static int setScreenFont (uChar*, uInt, uInt, uInt, bool = false); static int setUnicodeMap (struct unimapdesc*);