Looking in /etc/ttys for the type of terminal

This commit is contained in:
Markus Gans 2017-04-08 02:40:22 +02:00
parent 278ac9d921
commit 7ead64f2b8
7 changed files with 257 additions and 141 deletions

View File

@ -1,3 +1,6 @@
2017-04-08 Markus Gans <guru.mail@muenster.de>
* Looking in /etc/ttys for the type of terminal
2017-04-06 Markus Gans <guru.mail@muenster.de>
* Change cursor style on a FreeBSD console

View File

@ -563,7 +563,10 @@ const wchar_t* FString::wc_str() const
//----------------------------------------------------------------------
const char* FString::c_str() const
{
return wc_to_c_str (string);
if ( string )
return wc_to_c_str (string);
else
return 0;
}
//----------------------------------------------------------------------

View File

@ -35,7 +35,7 @@ FSwitch::~FSwitch() // destructor
// public methods of FSwitch
//----------------------------------------------------------------------
void FSwitch::setText (FString txt)
void FSwitch::setText (const FString& txt)
{
FToggleButton::setText(txt);
switch_offset_pos = int(txt.getLength()) + 1;

View File

@ -57,7 +57,7 @@ class FSwitch : public FToggleButton
const char* getClassName() const;
// Mutator
void setText (FString);
void setText (const FString&);
// Event handlers
void onKeyPress (FKeyEvent*);

View File

@ -92,6 +92,7 @@ const FString* FTerm::xterm_font = 0;
const FString* FTerm::xterm_title = 0;
const FString* FTerm::answer_back = 0;
const FString* FTerm::sec_da = 0;
const FString* FTerm::empty_string = 0;
FOptiMove* FTerm::opti_move = 0;
FOptiAttr* FTerm::opti_attr = 0;
FTerm::modifier_key FTerm::mod_key;
@ -2057,7 +2058,7 @@ int FTerm::closeConsole()
}
//----------------------------------------------------------------------
void FTerm::identifyTermType()
void FTerm::getSystemTermType()
{
// Import the untrusted environment variable TERM
const char* const& term_env = std::getenv(const_cast<char*>("TERM"));
@ -2067,35 +2068,34 @@ void FTerm::identifyTermType()
std::strncpy (termtype, term_env, sizeof(termtype) - 1);
return;
}
else if ( term_name )
else if ( term_name ) // fallback: look into /etc/ttytype or /etc/ttys
{
// fallback: look into /etc/ttytype or /etc/ttys
//
// get term basename
const char* term_basename = std::strrchr(term_name, '/');
if ( term_basename == 0 )
term_basename = term_name;
else
term_basename++;
// Analyse /etc/ttytype
// --------------------
// file format:
// <terminal type> <whitespace> <tty name>
//
// Example:
// linux tty1
// vt100 ttys0
// linux tty1
// vt100 ttys0
std::FILE *fp;
if ( (fp = std::fopen("/etc/ttytype", "r")) != 0
|| (fp = std::fopen("/etc/ttys", "r")) != 0 ) // FreeBSD: man 5 ttys
if ( (fp = std::fopen("/etc/ttytype", "r")) != 0 )
{
char* p;
char* type;
char* name;
char str[BUFSIZ];
// get term basename
const char* term_basename = std::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 )
{
@ -2124,6 +2124,25 @@ void FTerm::identifyTermType()
std::fclose(fp);
}
// Analyse /etc/ttys
// --------------------
struct ttyent* ttys_entryt;
ttys_entryt = getttynam(term_basename);
if ( ttys_entryt )
{
char* type = ttys_entryt->ty_type;
if ( type != 0 )
{
std::strncpy (termtype, type, sizeof(termtype) - 1);
endttyent();
return;
}
}
endttyent();
}
// use vt100 if not found
@ -3436,11 +3455,12 @@ void FTerm::init()
char* new_termtype = 0;
term_initialized = true;
init_term_object = this;
fd_tty = -1;
opti_move = new FOptiMove();
opti_attr = new FOptiAttr();
term = new FRect(0,0,0,0);
mouse = new FPoint(0,0);
fd_tty = -1;
opti_move = new FOptiMove();
opti_attr = new FOptiAttr();
term = new FRect(0,0,0,0);
mouse = new FPoint(0,0);
empty_string = new FString("");
vt100_alt_char = new std::map<uChar,uChar>;
encoding_set = new std::map<std::string,fc::encoding>;
@ -3506,21 +3526,21 @@ void FTerm::init()
#endif
#if defined(BSD)
// initialize BSD console
// Initialize BSD console
initBSDConsole();
#endif
// save termios settings
// Save termios settings
storeTTYsettings();
// get output baud rate
// Get output baud rate
baudrate = getBaudRate(&term_init);
if ( isatty(stdout_no) )
opti_move->setBaudRate(int(baudrate));
// get the set type of the terminal
identifyTermType();
// Set the variable 'termtype' to the predefined type of the terminal
getSystemTermType();
if ( std::strncmp(termtype, "cygwin", 6) == 0 )
cygwin_terminal = true;
@ -3543,7 +3563,7 @@ void FTerm::init()
else
linux_terminal = false;
// terminal detection
// Terminal detection
if ( terminal_detection )
{
struct termios t;
@ -3553,7 +3573,7 @@ void FTerm::init()
t.c_cc[VMIN] = 0; // Minimum number of characters
tcsetattr (stdin_no, TCSANOW, &t);
// initialize 256 colors terminals
// Initialize 256 colors terminals
new_termtype = init_256colorTerminal();
// Identify the terminal via the answerback-message
@ -3920,6 +3940,9 @@ void FTerm::finish()
if ( vt100_alt_char )
delete vt100_alt_char;
if ( empty_string )
delete empty_string;
if ( sec_da )
delete sec_da;

View File

@ -56,6 +56,7 @@
#include <langinfo.h>
#include <term.h> // termcap
#include <termios.h>
#include <ttyent.h>
#include <unistd.h>
#include <cmath>
@ -110,7 +111,7 @@ class FTerm
} mod_key;
// Constructor
FTerm (bool = false);
explicit FTerm (bool = false);
// Destructor
virtual ~FTerm();
@ -367,7 +368,7 @@ class FTerm
static int openConsole();
static int closeConsole();
static void identifyTermType();
static void getSystemTermType();
static void storeTTYsettings();
static void restoreTTYsettings();
@ -459,6 +460,7 @@ class FTerm
static const FString* xterm_title;
static const FString* answer_back;
static const FString* sec_da;
static const FString* empty_string;
struct
{
@ -492,11 +494,11 @@ inline int FTerm::getMaxColor()
#if DEBUG
//----------------------------------------------------------------------
inline const FString& FTerm::getAnswerbackString()
{ return *answer_back; }
{ return (answer_back) ? *answer_back : *empty_string; }
//----------------------------------------------------------------------
inline const FString& FTerm::getSecDAString()
{ return *sec_da; }
{ return (sec_da) ? *sec_da : *empty_string; }
#endif
//----------------------------------------------------------------------

View File

@ -11,15 +11,15 @@
static FVTerm* terminal;
// function prototype
void tcapBooleans (std::string, bool);
void tcapNumeric (std::string, int);
void termcapString (std::string, char*);
void tcapBooleans (const std::string&, bool);
void tcapNumeric (const std::string&, int);
void tcapString (const std::string&, const char*);
//----------------------------------------------------------------------
// functions
//----------------------------------------------------------------------
void tcapBooleans (std::string name, bool cap_bool)
void tcapBooleans (const std::string& name, bool cap_bool)
{
std::cout << "FTermcap::" << name << ": ";
@ -30,13 +30,13 @@ void tcapBooleans (std::string name, bool cap_bool)
}
//----------------------------------------------------------------------
void tcapNumeric (std::string name, int cap_num)
void tcapNumeric (const std::string& name, int cap_num)
{
std::cout << "FTermcap::" << name << ": " << cap_num << "\r\n";
}
//----------------------------------------------------------------------
void tcapString (std::string name, const char* cap_str)
void tcapString (const std::string& name, const char* cap_str)
{
uInt len;
std::string sequence;
@ -52,9 +52,16 @@ void tcapString (std::string name, const char* cap_str)
for (uInt i=0; i < len; i++)
{
uChar c = cap_str[i];
char c = cap_str[i];
if ( c < 32 )
if ( c < 0 )
{
std::ostringstream o;
o << std::oct << int(uChar(c));
sequence += "\\";
sequence += o.str();
}
else if ( c < 32 )
{
if ( c == 27 )
sequence += "\\E";
@ -64,13 +71,6 @@ void tcapString (std::string name, const char* cap_str)
sequence += c + 64;
}
}
else if ( c >= 127 )
{
std::ostringstream o;
o << std::oct << int(c);
sequence += "\\";
sequence += o.str();
}
else
sequence += c;
}
@ -104,13 +104,13 @@ int main (int argc, char* argv[])
std::cout << "| after parseSecDA(): "
<< terminal->termtype_SecDA << "\r\n";
if ( &terminal->getAnswerbackString() )
tcapString ( "| The answerback String"
, terminal->getAnswerbackString().c_str() );
if ( const FString& s = terminal->getAnswerbackString() )
tcapString ("| The answerback String", s);
if ( &terminal->getSecDAString() )
tcapString ( "| The SecDA String"
, terminal->getSecDAString().c_str() );
if ( const FString& s = terminal->getSecDAString() )
tcapString ("| The SecDA String", s);
std::cout << "`------------------- debug -------------------\r\n";
#endif
@ -132,91 +132,176 @@ int main (int argc, char* argv[])
, FTermcap::no_utf8_acs_chars );
std::cout << "\r\n[Numeric]\r\n";
tcapNumeric ("max_color", FTermcap::max_color);
tcapNumeric ("tabstop", FTermcap::tabstop);
tcapNumeric ("attr_without_color", FTermcap::attr_without_color);
tcapNumeric ("max_color"
, FTermcap::max_color);
tcapNumeric ("tabstop"
, FTermcap::tabstop);
tcapNumeric ("attr_without_color"
, FTermcap::attr_without_color);
std::cout << "\r\n[String]\r\n";
tcapString ("t_bell", tcap[fc::t_bell].string);
tcapString ("t_erase_chars", tcap[fc::t_erase_chars].string);
tcapString ("t_clear_screen", tcap[fc::t_clear_screen].string);
tcapString ("t_clr_eos", tcap[fc::t_clr_eos].string);
tcapString ("t_clr_eol", tcap[fc::t_clr_eol].string);
tcapString ("t_clr_bol", tcap[fc::t_clr_bol].string);
tcapString ("t_cursor_home", tcap[fc::t_cursor_home].string);
tcapString ("t_cursor_to_ll", tcap[fc::t_cursor_to_ll].string);
tcapString ("t_carriage_return", tcap[fc::t_carriage_return].string);
tcapString ("t_tab", tcap[fc::t_tab].string);
tcapString ("t_back_tab", tcap[fc::t_back_tab].string);
tcapString ("t_insert_padding", tcap[fc::t_insert_padding].string);
tcapString ("t_insert_character", tcap[fc::t_insert_character].string);
tcapString ("t_parm_ich", tcap[fc::t_parm_ich].string);
tcapString ("t_repeat_char", tcap[fc::t_repeat_char].string);
tcapString ("t_initialize_color", tcap[fc::t_initialize_color].string);
tcapString ("t_initialize_pair", tcap[fc::t_initialize_pair].string);
tcapString ("t_set_a_foreground", tcap[fc::t_set_a_foreground].string);
tcapString ("t_set_a_background", tcap[fc::t_set_a_background].string);
tcapString ("t_set_foreground", tcap[fc::t_set_foreground].string);
tcapString ("t_set_background", tcap[fc::t_set_background].string);
tcapString ("t_set_color_pair", tcap[fc::t_set_color_pair].string);
tcapString ("t_orig_pair", tcap[fc::t_orig_pair].string);
tcapString ("t_orig_colors", tcap[fc::t_orig_colors].string);
tcapString ("t_no_color_video", tcap[fc::t_no_color_video].string);
tcapString ("t_cursor_address", tcap[fc::t_cursor_address].string);
tcapString ("t_column_address", tcap[fc::t_column_address].string);
tcapString ("t_row_address", tcap[fc::t_row_address].string);
tcapString ("t_cursor_visible", tcap[fc::t_cursor_visible].string);
tcapString ("t_cursor_invisible", tcap[fc::t_cursor_invisible].string);
tcapString ("t_cursor_normal", tcap[fc::t_cursor_normal].string);
tcapString ("t_cursor_up", tcap[fc::t_cursor_up].string);
tcapString ("t_cursor_down", tcap[fc::t_cursor_down].string);
tcapString ("t_cursor_left", tcap[fc::t_cursor_left].string);
tcapString ("t_cursor_right", tcap[fc::t_cursor_right].string);
tcapString ("t_parm_up_cursor", tcap[fc::t_parm_up_cursor].string);
tcapString ("t_parm_down_cursor", tcap[fc::t_parm_down_cursor].string);
tcapString ("t_parm_left_cursor", tcap[fc::t_parm_left_cursor].string);
tcapString ("t_parm_right_cursor", tcap[fc::t_parm_right_cursor].string);
tcapString ("t_save_cursor", tcap[fc::t_save_cursor].string);
tcapString ("t_restore_cursor", tcap[fc::t_restore_cursor].string);
tcapString ("t_scroll_forward", tcap[fc::t_scroll_forward].string);
tcapString ("t_scroll_reverse", tcap[fc::t_scroll_reverse].string);
tcapString ("t_enter_ca_mode", tcap[fc::t_enter_ca_mode].string);
tcapString ("t_exit_ca_mode", tcap[fc::t_exit_ca_mode].string);
tcapString ("t_enable_acs", tcap[fc::t_enable_acs].string);
tcapString ("t_enter_bold_mode", tcap[fc::t_enter_bold_mode].string);
tcapString ("t_exit_bold_mode", tcap[fc::t_exit_bold_mode].string);
tcapString ("t_enter_dim_mode", tcap[fc::t_enter_dim_mode].string);
tcapString ("t_exit_dim_mode", tcap[fc::t_exit_dim_mode].string);
tcapString ("t_enter_italics_mode", tcap[fc::t_enter_italics_mode].string);
tcapString ("t_exit_italics_mode", tcap[fc::t_exit_italics_mode].string);
tcapString ("t_enter_underline_mode", tcap[fc::t_enter_underline_mode].string);
tcapString ("t_exit_underline_mode", tcap[fc::t_exit_underline_mode].string);
tcapString ("t_enter_blink_mode", tcap[fc::t_enter_blink_mode].string);
tcapString ("t_exit_blink_mode", tcap[fc::t_exit_blink_mode].string);
tcapString ("t_enter_reverse_mode", tcap[fc::t_enter_reverse_mode].string);
tcapString ("t_exit_reverse_mode", tcap[fc::t_exit_reverse_mode].string);
tcapString ("t_enter_standout_mode", tcap[fc::t_enter_standout_mode].string);
tcapString ("t_exit_standout_mode", tcap[fc::t_exit_standout_mode].string);
tcapString ("t_enter_secure_mode", tcap[fc::t_enter_secure_mode].string);
tcapString ("t_exit_secure_mode", tcap[fc::t_exit_secure_mode].string);
tcapString ("t_enter_protected_mode", tcap[fc::t_enter_protected_mode].string);
tcapString ("t_exit_protected_mode", tcap[fc::t_exit_protected_mode].string);
tcapString ("t_enter_crossed_out_mode", tcap[fc::t_enter_crossed_out_mode].string);
tcapString ("t_exit_crossed_out_mode", tcap[fc::t_exit_crossed_out_mode].string);
tcapString ("t_enter_dbl_underline_mode", tcap[fc::t_enter_dbl_underline_mode].string);
tcapString ("t_exit_dbl_underline_mode", tcap[fc::t_exit_dbl_underline_mode].string);
tcapString ("t_set_attributes", tcap[fc::t_set_attributes].string);
tcapString ("t_exit_attribute_mode", tcap[fc::t_exit_attribute_mode].string);
tcapString ("t_enter_alt_charset_mode", tcap[fc::t_enter_alt_charset_mode].string);
tcapString ("t_exit_alt_charset_mode", tcap[fc::t_exit_alt_charset_mode].string);
tcapString ("t_enter_pc_charset_mode", tcap[fc::t_enter_pc_charset_mode].string);
tcapString ("t_exit_pc_charset_mode", tcap[fc::t_exit_pc_charset_mode].string);
tcapString ("t_enter_insert_mode", tcap[fc::t_enter_insert_mode].string);
tcapString ("t_exit_insert_mode", tcap[fc::t_exit_insert_mode].string);
tcapString ("t_enter_am_mode", tcap[fc::t_enter_am_mode].string);
tcapString ("t_exit_am_mode", tcap[fc::t_exit_am_mode].string);
tcapString ("t_acs_chars", tcap[fc::t_acs_chars].string);
tcapString ("t_keypad_xmit", tcap[fc::t_keypad_xmit].string);
tcapString ("t_keypad_local", tcap[fc::t_keypad_local].string);
tcapString ("t_key_mouse", tcap[fc::t_key_mouse].string);
tcapString ("t_bell"
, tcap[fc::t_bell].string);
tcapString ("t_erase_chars"
, tcap[fc::t_erase_chars].string);
tcapString ("t_clear_screen"
, tcap[fc::t_clear_screen].string);
tcapString ("t_clr_eos"
, tcap[fc::t_clr_eos].string);
tcapString ("t_clr_eol"
, tcap[fc::t_clr_eol].string);
tcapString ("t_clr_bol"
, tcap[fc::t_clr_bol].string);
tcapString ("t_cursor_home"
, tcap[fc::t_cursor_home].string);
tcapString ("t_cursor_to_ll"
, tcap[fc::t_cursor_to_ll].string);
tcapString ("t_carriage_return"
, tcap[fc::t_carriage_return].string);
tcapString ("t_tab"
, tcap[fc::t_tab].string);
tcapString ("t_back_tab"
, tcap[fc::t_back_tab].string);
tcapString ("t_insert_padding"
, tcap[fc::t_insert_padding].string);
tcapString ("t_insert_character"
, tcap[fc::t_insert_character].string);
tcapString ("t_parm_ich"
, tcap[fc::t_parm_ich].string);
tcapString ("t_repeat_char"
, tcap[fc::t_repeat_char].string);
tcapString ("t_initialize_color"
, tcap[fc::t_initialize_color].string);
tcapString ("t_initialize_pair"
, tcap[fc::t_initialize_pair].string);
tcapString ("t_set_a_foreground"
, tcap[fc::t_set_a_foreground].string);
tcapString ("t_set_a_background"
, tcap[fc::t_set_a_background].string);
tcapString ("t_set_foreground"
, tcap[fc::t_set_foreground].string);
tcapString ("t_set_background"
, tcap[fc::t_set_background].string);
tcapString ("t_set_color_pair"
, tcap[fc::t_set_color_pair].string);
tcapString ("t_orig_pair"
, tcap[fc::t_orig_pair].string);
tcapString ("t_orig_colors"
, tcap[fc::t_orig_colors].string);
tcapString ("t_no_color_video"
, tcap[fc::t_no_color_video].string);
tcapString ("t_cursor_address"
, tcap[fc::t_cursor_address].string);
tcapString ("t_column_address"
, tcap[fc::t_column_address].string);
tcapString ("t_row_address"
, tcap[fc::t_row_address].string);
tcapString ("t_cursor_visible"
, tcap[fc::t_cursor_visible].string);
tcapString ("t_cursor_invisible"
, tcap[fc::t_cursor_invisible].string);
tcapString ("t_cursor_normal"
, tcap[fc::t_cursor_normal].string);
tcapString ("t_cursor_up"
, tcap[fc::t_cursor_up].string);
tcapString ("t_cursor_down"
, tcap[fc::t_cursor_down].string);
tcapString ("t_cursor_left"
, tcap[fc::t_cursor_left].string);
tcapString ("t_cursor_right"
, tcap[fc::t_cursor_right].string);
tcapString ("t_parm_up_cursor"
, tcap[fc::t_parm_up_cursor].string);
tcapString ("t_parm_down_cursor"
, tcap[fc::t_parm_down_cursor].string);
tcapString ("t_parm_left_cursor"
, tcap[fc::t_parm_left_cursor].string);
tcapString ("t_parm_right_cursor"
, tcap[fc::t_parm_right_cursor].string);
tcapString ("t_save_cursor"
, tcap[fc::t_save_cursor].string);
tcapString ("t_restore_cursor"
, tcap[fc::t_restore_cursor].string);
tcapString ("t_scroll_forward"
, tcap[fc::t_scroll_forward].string);
tcapString ("t_scroll_reverse"
, tcap[fc::t_scroll_reverse].string);
tcapString ("t_enter_ca_mode"
, tcap[fc::t_enter_ca_mode].string);
tcapString ("t_exit_ca_mode"
, tcap[fc::t_exit_ca_mode].string);
tcapString ("t_enable_acs"
, tcap[fc::t_enable_acs].string);
tcapString ("t_enter_bold_mode"
, tcap[fc::t_enter_bold_mode].string);
tcapString ("t_exit_bold_mode"
, tcap[fc::t_exit_bold_mode].string);
tcapString ("t_enter_dim_mode"
, tcap[fc::t_enter_dim_mode].string);
tcapString ("t_exit_dim_mode"
, tcap[fc::t_exit_dim_mode].string);
tcapString ("t_enter_italics_mode"
, tcap[fc::t_enter_italics_mode].string);
tcapString ("t_exit_italics_mode"
, tcap[fc::t_exit_italics_mode].string);
tcapString ("t_enter_underline_mode"
, tcap[fc::t_enter_underline_mode].string);
tcapString ("t_exit_underline_mode"
, tcap[fc::t_exit_underline_mode].string);
tcapString ("t_enter_blink_mode"
, tcap[fc::t_enter_blink_mode].string);
tcapString ("t_exit_blink_mode"
, tcap[fc::t_exit_blink_mode].string);
tcapString ("t_enter_reverse_mode"
, tcap[fc::t_enter_reverse_mode].string);
tcapString ("t_exit_reverse_mode"
, tcap[fc::t_exit_reverse_mode].string);
tcapString ("t_enter_standout_mode"
, tcap[fc::t_enter_standout_mode].string);
tcapString ("t_exit_standout_mode"
, tcap[fc::t_exit_standout_mode].string);
tcapString ("t_enter_secure_mode"
, tcap[fc::t_enter_secure_mode].string);
tcapString ("t_exit_secure_mode"
, tcap[fc::t_exit_secure_mode].string);
tcapString ("t_enter_protected_mode"
, tcap[fc::t_enter_protected_mode].string);
tcapString ("t_exit_protected_mode"
, tcap[fc::t_exit_protected_mode].string);
tcapString ("t_enter_crossed_out_mode"
, tcap[fc::t_enter_crossed_out_mode].string);
tcapString ("t_exit_crossed_out_mode"
, tcap[fc::t_exit_crossed_out_mode].string);
tcapString ("t_enter_dbl_underline_mode"
, tcap[fc::t_enter_dbl_underline_mode].string);
tcapString ("t_exit_dbl_underline_mode"
, tcap[fc::t_exit_dbl_underline_mode].string);
tcapString ("t_set_attributes"
, tcap[fc::t_set_attributes].string);
tcapString ("t_exit_attribute_mode"
, tcap[fc::t_exit_attribute_mode].string);
tcapString ("t_enter_alt_charset_mode"
, tcap[fc::t_enter_alt_charset_mode].string);
tcapString ("t_exit_alt_charset_mode"
, tcap[fc::t_exit_alt_charset_mode].string);
tcapString ("t_enter_pc_charset_mode"
, tcap[fc::t_enter_pc_charset_mode].string);
tcapString ("t_exit_pc_charset_mode"
, tcap[fc::t_exit_pc_charset_mode].string);
tcapString ("t_enter_insert_mode"
, tcap[fc::t_enter_insert_mode].string);
tcapString ("t_exit_insert_mode"
, tcap[fc::t_exit_insert_mode].string);
tcapString ("t_enter_am_mode"
, tcap[fc::t_enter_am_mode].string);
tcapString ("t_exit_am_mode"
, tcap[fc::t_exit_am_mode].string);
tcapString ("t_acs_chars"
, tcap[fc::t_acs_chars].string);
tcapString ("t_keypad_xmit"
, tcap[fc::t_keypad_xmit].string);
tcapString ("t_keypad_local"
, tcap[fc::t_keypad_local].string);
tcapString ("t_key_mouse"
, tcap[fc::t_key_mouse].string);
}