From 2dc8d7f2d365da1ff1c837e293b6715125929972 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Sun, 6 Jun 2021 21:57:35 +0200 Subject: [PATCH] FTermDetection now has clean code by using FString instead of char* --- ChangeLog | 1 + src/fterm.cpp | 2 +- src/ftermdebugdata.cpp | 6 +- src/ftermdetection.cpp | 274 ++++++++++------------------- src/include/final/ftermdebugdata.h | 8 +- src/include/final/ftermdetection.h | 88 ++++----- test/ftermdetection-test.cpp | 55 ++---- test/ftermopenbsd-test.cpp | 2 +- 8 files changed, 171 insertions(+), 265 deletions(-) diff --git a/ChangeLog b/ChangeLog index d908d89d..dbddeb34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 2021-06-06 Markus Gans * Bug fixing in FString and FTermDetection + * FTermDetection now has clean code by using FString instead of char* 2021-06-03 Markus Gans * Some FString optimizations diff --git a/src/fterm.cpp b/src/fterm.cpp index 7dcbde1c..dab89514 100644 --- a/src/fterm.cpp +++ b/src/fterm.cpp @@ -2168,7 +2168,7 @@ bool FTerm::init_terminal() const // Terminal detection auto& term_detection = FTerm::getFTermDetection(); term_detection.detect(); - setTermType (term_detection.getTermType()); + setTermType (term_detection.getTermType().c_str()); return true; } diff --git a/src/ftermdebugdata.cpp b/src/ftermdebugdata.cpp index b7bd0f23..71ae8ca1 100644 --- a/src/ftermdebugdata.cpp +++ b/src/ftermdebugdata.cpp @@ -51,21 +51,21 @@ const FString& FTermDebugData::getSecDAString() } //---------------------------------------------------------------------- -const char* FTermDebugData::getTermType_256color() +const FString& FTermDebugData::getTermType_256color() { auto& term_detection = FTerm::getFTermDetection(); return term_detection.getTermType_256color(); } //---------------------------------------------------------------------- -const char* FTermDebugData::getTermType_Answerback() +const FString& FTermDebugData::getTermType_Answerback() { auto& term_detection = FTerm::getFTermDetection(); return term_detection.getTermType_Answerback(); } //---------------------------------------------------------------------- -const char* FTermDebugData::getTermType_SecDA() +const FString& FTermDebugData::getTermType_SecDA() { auto& term_detection = FTerm::getFTermDetection(); return term_detection.getTermType_SecDA(); diff --git a/src/ftermdetection.cpp b/src/ftermdetection.cpp index de4ece36..73f23bc9 100644 --- a/src/ftermdetection.cpp +++ b/src/ftermdetection.cpp @@ -56,29 +56,10 @@ namespace finalcut // constructors and destructor //---------------------------------------------------------------------- -FTermDetection::FTermDetection() -{ - // Preset to true - terminal_detection = true; - - // Preset to false - decscusr_support = false; - - // Gnome terminal id from SecDA - // Example: vte version 0.40.0 = 0 * 100 + 40 * 100 + 0 = 4000 - // a.b.c = a * 100 + b * 100 + c - gnome_terminal_id = 0; - - // Set default ttytype file - std::strncpy (ttytypename, "/etc/ttytype", sizeof(ttytypename)); - ttytypename[sizeof(ttytypename) - 1] = '\0'; -} +FTermDetection::FTermDetection() = default; //---------------------------------------------------------------------- -FTermDetection::~FTermDetection() // destructor -{ - deallocation(); -} +FTermDetection::~FTermDetection() = default; // destructor // public methods of FTermDetection @@ -86,31 +67,28 @@ FTermDetection::~FTermDetection() // destructor #if DEBUG const FString& FTermDetection::getAnswerbackString() const { - return ( answer_back ) ? *answer_back : fc::emptyFString::get(); + return answer_back; } //---------------------------------------------------------------------- const FString& FTermDetection::getSecDAString() const { - return ( sec_da ) ? *sec_da : fc::emptyFString::get(); + return sec_da; } #endif //---------------------------------------------------------------------- -void FTermDetection::setTtyTypeFileName (const char ttytype_filename[]) +void FTermDetection::setTtyTypeFileName (const FString& ttytype_filename) { if ( ! ttytype_filename ) return; - std::strncpy (ttytypename, ttytype_filename, sizeof(ttytypename)); - ttytypename[sizeof(ttytypename) - 1] = '\0'; + ttytypename = ttytype_filename; } //---------------------------------------------------------------------- void FTermDetection::detect() { - deallocation(); - // Set the variable 'termtype' to the predefined type of the terminal getSystemTermType(); @@ -123,16 +101,6 @@ void FTermDetection::detect() // private methods of FTermDetection -//---------------------------------------------------------------------- -void FTermDetection::deallocation() -{ - if ( sec_da ) - delete sec_da; - - if ( answer_back ) - delete answer_back; -} - //---------------------------------------------------------------------- void FTermDetection::getSystemTermType() { @@ -143,8 +111,7 @@ void FTermDetection::getSystemTermType() if ( term_env ) { // Save name in termtype - std::strncpy (termtype, term_env, sizeof(termtype)); - termtype[sizeof(termtype) - 1] = '\0'; + termtype = term_env; return; } else if ( ! termfilename.empty() ) // 1st fallback: use the teminal file name @@ -159,8 +126,7 @@ void FTermDetection::getSystemTermType() } // 2nd fallback: use vt100 if not found - std::strncpy (termtype, "vt100", sizeof(termtype)); - termtype[sizeof(termtype) - 1] = '\0'; + termtype = "vt100"; } //---------------------------------------------------------------------- @@ -188,7 +154,7 @@ bool FTermDetection::getTTYtype() std::array str{}; const auto& fsystem = FTerm::getFSystem(); - if ( (fp = fsystem->fopen(ttytypename, "r")) == nullptr ) + if ( (fp = fsystem->fopen(ttytypename.c_str(), "r")) == nullptr ) return false; // Read and parse the file @@ -213,8 +179,7 @@ bool FTermDetection::getTTYtype() if ( type != nullptr && name != nullptr && ! std::strcmp(name, term_basename) ) { // Save name in termtype - std::strncpy (termtype, type, sizeof(termtype)); - termtype[sizeof(termtype) - 1] = '\0'; + termtype = type; fsystem->fclose(fp); return true; } @@ -249,8 +214,7 @@ bool FTermDetection::getTTYSFileEntry() if ( type != nullptr ) { // Save name in termtype - std::strncpy (termtype, type, sizeof(termtype)); - termtype[sizeof(termtype) - 1] = '\0'; + termtype = type; endttyent(); return true; } @@ -265,48 +229,49 @@ bool FTermDetection::getTTYSFileEntry() void FTermDetection::termtypeAnalysis() { // Cygwin console - if ( std::strncmp(termtype, "cygwin", 6) == 0 ) + + if ( termtype.left(6) == "cygwin" ) terminal_type.cygwin = true; // rxvt terminal emulator (native MS Window System port) on cygwin - if ( std::strncmp(termtype, "rxvt-cygwin-native", 18) == 0 ) + if ( termtype == "rxvt-cygwin-native" ) terminal_type.rxvt = true; // Ansi terminal - if ( std::strncmp(termtype, "ansi", 4) == 0 ) + if ( termtype.left(4) == "ansi" ) { terminal_detection = false; terminal_type.ansi = true; } // Sun Microsystems workstation console - if ( std::strncmp(termtype, "sun", 3) == 0 ) + if ( termtype.left(3) == "sun" ) { terminal_detection = false; terminal_type.sun_con = true; } // Kterm - if ( std::strncmp(termtype, "kterm", 5) == 0 ) + if ( termtype.left(5) == "kterm" ) { terminal_detection = false; terminal_type.kterm = true; } // mlterm - if ( std::strncmp(termtype, "mlterm", 6) == 0 ) + if ( termtype.left(6) == "mlterm" ) terminal_type.mlterm = true; // rxvt - if ( std::strncmp(termtype, "rxvt", 4) == 0 ) + if ( termtype.left(4) == "rxvt" ) terminal_type.rxvt = true; // urxvt - if ( std::strncmp(termtype, "rxvt-unicode", 12) == 0 ) + if ( termtype.left(12) == "rxvt-unicode" ) terminal_type.urxvt = true; // screen/tmux - if ( std::strncmp(termtype, "screen", 6) == 0 ) + if ( termtype.left(6) == "screen" ) { terminal_type.screen = true; auto tmux = std::getenv("TMUX"); @@ -316,16 +281,15 @@ void FTermDetection::termtypeAnalysis() } // Linux console - if ( std::strncmp(termtype, "linux", 5) == 0 - || std::strncmp(termtype, "con", 3) == 0 ) + if ( termtype.left(5) == "linux" || termtype.left(3) == "con" ) terminal_type.linux_con = true; // NetBSD workstation console - if ( std::strncmp(termtype, "wsvt25", 6) == 0 ) + if ( termtype.left(6) == "wsvt25" ) terminal_type.netbsd_con = true; // kitty - if ( std::strncmp(termtype, "xterm-kitty", 11) == 0 ) + if ( termtype.left(11) == "xterm-kitty" ) terminal_type.kitty = true; } @@ -334,7 +298,7 @@ void FTermDetection::detectTerminal() { // Terminal detection - const char* new_termtype{nullptr}; + FString new_termtype{}; if ( terminal_detection ) { @@ -363,24 +327,22 @@ void FTermDetection::detectTerminal() // // Test if the terminal is a xterm - if ( std::strncmp(termtype, "xterm", 5) == 0 - || std::strncmp(termtype, "Eterm", 5) == 0 ) + if ( termtype.left(5) == "xterm" || termtype.left(5) == "Eterm" ) { terminal_type.xterm = true; // Each xterm should be able to use at least 16 colors - if ( ! new_termtype && std::strlen(termtype) == 5 ) + if ( ! new_termtype && termtype.getLength() == 5 ) new_termtype = "xterm-16color"; } - else if ( std::strncmp(termtype, "ansi", 4) == 0 ) // ANSI detection + else if ( termtype.left(4) == "ansi" ) // ANSI detection terminal_type.ansi = true; // set the new environment variable TERM if ( new_termtype ) { - setenv("TERM", new_termtype, 1); - std::strncpy (termtype, new_termtype, sizeof(termtype)); - termtype[sizeof(termtype) - 1] = '\0'; + setenv("TERM", new_termtype.c_str(), 1); + termtype = new_termtype; } #if defined(__CYGWIN__) @@ -394,13 +356,13 @@ void FTermDetection::detectTerminal() } //---------------------------------------------------------------------- -const char* FTermDetection::init_256colorTerminal() +FString FTermDetection::init_256colorTerminal() { - const char* new_termtype{nullptr}; + FString new_termtype{}; if ( get256colorEnvString() ) color256 = true; - else if ( std::strstr (termtype, "256color") ) + else if ( termtype.includes("256color") ) color256 = true; else color256 = false; @@ -408,13 +370,8 @@ const char* FTermDetection::init_256colorTerminal() new_termtype = termtype_256color_quirks(); #if DEBUG - if ( new_termtype ) - { - std::strncpy ( termtype_256color - , new_termtype - , sizeof(termtype_256color) ); - termtype_256color[sizeof(termtype_256color) - 1] = '\0'; - } + if ( ! new_termtype.isEmpty() ) + termtype_256color = new_termtype; #endif // DEBUG return new_termtype; @@ -447,9 +404,9 @@ bool FTermDetection::get256colorEnvString() } //---------------------------------------------------------------------- -const char* FTermDetection::termtype_256color_quirks() +FString FTermDetection::termtype_256color_quirks() { - const char* new_termtype{nullptr}; + FString new_termtype{}; if ( color_env.string2 || (color_env.string1 @@ -469,19 +426,19 @@ const char* FTermDetection::termtype_256color_quirks() if ( ! color256 ) return new_termtype; - if ( std::strncmp(termtype, "xterm", 5) == 0 ) + if ( termtype.left(5) == "xterm" ) new_termtype = "xterm-256color"; - if ( std::strncmp(termtype, "screen", 6) == 0 ) + if ( termtype.left(6) == "screen" ) new_termtype = "screen-256color"; - if ( std::strncmp(termtype, "Eterm", 5) == 0 ) + if ( termtype.left(5) == "Eterm" ) new_termtype = "Eterm-256color"; - if ( std::strncmp(termtype, "mlterm", 6) == 0 ) + if ( termtype.left(6) == "mlterm" ) new_termtype = "mlterm-256color"; - if ( std::strncmp(termtype, "rxvt", 4) != 0 + if ( termtype.left(4) == "rxvt" && color_env.string1 && std::strncmp(color_env.string1, "rxvt-xpm", 8) == 0 ) { @@ -503,11 +460,11 @@ const char* FTermDetection::termtype_256color_quirks() } //---------------------------------------------------------------------- -const char* FTermDetection::determineMaxColor (const char current_termtype[]) +FString FTermDetection::determineMaxColor (const FString& current_termtype) { // Determine xterm maximum number of colors via OSC 4 - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; auto& keyboard = FTerm::getFKeyboard(); keyboard.setNonBlockingInput(); @@ -542,7 +499,7 @@ const char* FTermDetection::determineMaxColor (const char current_termtype[]) } //---------------------------------------------------------------------- -FString FTermDetection::getXTermColorName (FColor color) +FString FTermDetection::getXTermColorName (FColor color) const { FString color_str{""}; std::array buf{}; @@ -598,26 +555,16 @@ FString FTermDetection::getXTermColorName (FColor color) } //---------------------------------------------------------------------- -const char* FTermDetection::parseAnswerbackMsg (const char current_termtype[]) +FString FTermDetection::parseAnswerbackMsg (const FString& current_termtype) { - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; auto& keyboard = FTerm::getFKeyboard(); keyboard.setNonBlockingInput(); // send ENQ and read the answerback message - const auto& ans = getAnswerbackMsg(); + answer_back = getAnswerbackMsg(); keyboard.unsetNonBlockingInput(); - try - { - answer_back = new FString(ans); - } - catch (const std::bad_alloc&) - { - badAllocOutput ("FString"); - return nullptr; - } - - if ( *answer_back == "PuTTY" ) + if ( answer_back == "PuTTY" ) { terminal_type.putty = true; @@ -633,20 +580,15 @@ const char* FTermDetection::parseAnswerbackMsg (const char current_termtype[]) std::fflush (stdout); #if DEBUG - if ( new_termtype ) - { - std::strncpy ( termtype_Answerback - , new_termtype - , sizeof(termtype_Answerback) - 1); - termtype_Answerback[sizeof(termtype_Answerback) - 1] = '\0'; - } + if ( ! new_termtype.isEmpty() ) + termtype_Answerback = new_termtype; #endif // DEBUG return new_termtype; } //---------------------------------------------------------------------- -FString FTermDetection::getAnswerbackMsg() +FString FTermDetection::getAnswerbackMsg() const { FString answerback{""}; fd_set ifds{}; @@ -686,45 +628,31 @@ FString FTermDetection::getAnswerbackMsg() } //---------------------------------------------------------------------- -const char* FTermDetection::parseSecDA (const char current_termtype[]) +FString FTermDetection::parseSecDA (const FString& current_termtype) { // The Linux console and older cygwin terminals knows no Sec_DA if ( isLinuxTerm() || isCygwinTerminal() ) return current_termtype; // Secondary device attributes (SEC_DA) <- decTerminalID string - const auto& ans = getSecDA(); + sec_da = getSecDA(); - try - { - // Secondary device attributes (SEC_DA) <- decTerminalID string - sec_da = new FString(ans); - } - catch (const std::bad_alloc&) - { - badAllocOutput ("FString"); - return current_termtype; - } - - if ( sec_da->getLength() < 6 ) + if ( sec_da.getLength() < 6 ) return current_termtype; // remove the first 3 bytes ("\033[>") - FString temp{sec_da->right(sec_da->getLength() - 3)}; + FString temp(sec_da.right(sec_da.getLength() - 3)); // remove the last byte ("c") temp.remove(temp.getLength() - 1, 1); // split into components - const FStringList sec_da_list = temp.split(';'); - - const uLong num_components = sec_da_list.size(); + const auto sec_da_components = temp.split(';'); + const auto num_components = sec_da_components.size(); // The second device attribute (SEC_DA) always has 3 parameters, // otherwise it usually has a copy of the device attribute (primary DA) if ( num_components < 3 ) return current_termtype; - const FString* sec_da_components = &sec_da_list[0]; - if ( sec_da_components[0].isEmpty() ) return current_termtype; @@ -737,21 +665,18 @@ const char* FTermDetection::parseSecDA (const char current_termtype[]) // Read the terminal hardware option secondary_da.terminal_id_hardware = str2int(sec_da_components[2]); - const char* new_termtype = secDA_Analysis(current_termtype); + FString new_termtype = secDA_Analysis(current_termtype); #if DEBUG - if ( new_termtype ) - { - std::strncpy (termtype_SecDA, new_termtype, sizeof(termtype_SecDA)); - termtype_SecDA[sizeof(termtype_SecDA) - 1] = '\0'; - } + if ( ! new_termtype.isEmpty() ) + termtype_SecDA = new_termtype; #endif // DEBUG return new_termtype; } //---------------------------------------------------------------------- -int FTermDetection::str2int (const FString& s) +int FTermDetection::str2int (const FString& s) const { // This is not a general string to integer conversion method. // It is only used in this class to convert the device attribute @@ -781,7 +706,7 @@ int FTermDetection::str2int (const FString& s) } //---------------------------------------------------------------------- -FString FTermDetection::getSecDA() +FString FTermDetection::getSecDA() const { FString sec_da_str{""}; @@ -831,9 +756,9 @@ FString FTermDetection::getSecDA() } //---------------------------------------------------------------------- -const char* FTermDetection::secDA_Analysis (const char current_termtype[]) +FString FTermDetection::secDA_Analysis (const FString& current_termtype) { - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; switch ( secondary_da.terminal_id_type ) { @@ -854,7 +779,7 @@ const char* FTermDetection::secDA_Analysis (const char current_termtype[]) break; case 32: // Tera Term - new_termtype = secDA_Analysis_32(current_termtype); + new_termtype = secDA_Analysis_32(); break; case 41: // DEC VT420 @@ -867,11 +792,11 @@ const char* FTermDetection::secDA_Analysis (const char current_termtype[]) break; case 67: // Cygwin - new_termtype = secDA_Analysis_67(current_termtype); + new_termtype = secDA_Analysis_67(); break; case 77: // mintty - new_termtype = secDA_Analysis_77(current_termtype); + new_termtype = secDA_Analysis_77(); break; case 82: // rxvt @@ -907,11 +832,11 @@ const char* FTermDetection::secDA_Analysis (const char current_termtype[]) } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_0 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_0 (const FString& current_termtype) { // Terminal ID 0 - DEC VT100 - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; if ( secondary_da.terminal_id_version == 10 && secondary_da.terminal_id_hardware == 1 ) @@ -930,11 +855,11 @@ inline const char* FTermDetection::secDA_Analysis_0 (const char current_termtype } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_1 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_1 (const FString& current_termtype) { // Terminal ID 1 - DEC VT220 - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; if ( isKittyTerminal() ) new_termtype = secDA_Analysis_kitty(new_termtype); @@ -945,11 +870,11 @@ inline const char* FTermDetection::secDA_Analysis_1 (const char current_termtype } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_24 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_24 (const FString& current_termtype) { // Terminal ID 24 - DEC VT320 - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(UNIT_TEST) @@ -957,9 +882,9 @@ inline const char* FTermDetection::secDA_Analysis_24 (const char current_termtyp && FTermOpenBSD::isBSDConsole() ) { // NetBSD/OpenBSD workstation console - if ( std::strncmp(termtype, "wsvt25", 6) == 0 ) + if ( termtype.left(6) == "wsvt25" ) terminal_type.netbsd_con = true; - else if ( std::strncmp(termtype, "vt220", 5) == 0 ) + else if ( termtype.left(5) == "vt220" ) { terminal_type.openbsd_con = true; new_termtype = "pccon"; @@ -972,17 +897,16 @@ inline const char* FTermDetection::secDA_Analysis_24 (const char current_termtyp } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_32 (const char[]) +inline FString FTermDetection::secDA_Analysis_32() { // Terminal ID 32 - Tera Term terminal_type.tera_term = true; - const char* new_termtype = "teraterm"; - return new_termtype; + return "teraterm"; } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_65 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_65 (const FString& current_termtype) { // Terminal ID 65 - DEC VT525 and VTE >= 0.53.0 @@ -990,37 +914,35 @@ inline const char* FTermDetection::secDA_Analysis_65 (const char current_termtyp } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_67 (const char[]) +inline FString FTermDetection::secDA_Analysis_67() { // Terminal ID 67 - cygwin terminal_type.cygwin = true; - const char* new_termtype = "cygwin"; std::fflush(stdout); - return new_termtype; + return "cygwin"; } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_77 (const char[]) +inline FString FTermDetection::secDA_Analysis_77() { // Terminal ID 77 - mintty terminal_type.mintty = true; decscusr_support = true; - const char* new_termtype = "xterm-256color"; std::fflush(stdout); - return new_termtype; + return "xterm-256color"; } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_82() +inline FString FTermDetection::secDA_Analysis_82() { // Terminal ID 82 - rxvt - const char* new_termtype{}; + FString new_termtype{}; terminal_type.rxvt = true; - if ( std::strncmp(termtype, "rxvt-cygwin-native", 18) == 0 ) + if ( termtype == "rxvt-cygwin-native" ) new_termtype = "rxvt-16color"; else new_termtype = "rxvt"; @@ -1029,36 +951,34 @@ inline const char* FTermDetection::secDA_Analysis_82() } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_83 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_83 (const FString& current_termtype) { // Terminal ID 83 - screen - const char* new_termtype = current_termtype; terminal_type.screen = true; - return new_termtype; + return current_termtype; } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_84 (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_84 (const FString& current_termtype) { // Terminal ID 84 - tmux - const char* new_termtype = current_termtype; terminal_type.screen = true; terminal_type.tmux = true; - return new_termtype; + return current_termtype; } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_85() +inline FString FTermDetection::secDA_Analysis_85() { // Terminal ID 85 - rxvt-unicode - const char* new_termtype{}; + FString new_termtype{}; terminal_type.rxvt = true; terminal_type.urxvt = true; - if ( std::strncmp(termtype, "rxvt-", 5) != 0 ) + if ( termtype.left(5) == "rxvt-" ) { if ( color256 ) new_termtype = "rxvt-256color"; @@ -1072,12 +992,12 @@ inline const char* FTermDetection::secDA_Analysis_85() } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_vte (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_vte (const FString& current_termtype) { // VTE terminal library // (Since VTE ) the terminal ID has changed from 1 to 65) - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; if ( secondary_da.terminal_id_version > 1000 ) { @@ -1096,11 +1016,11 @@ inline const char* FTermDetection::secDA_Analysis_vte (const char current_termty } //---------------------------------------------------------------------- -inline const char* FTermDetection::secDA_Analysis_kitty (const char current_termtype[]) +inline FString FTermDetection::secDA_Analysis_kitty (const FString& current_termtype) { // kitty - const char* new_termtype = current_termtype; + FString new_termtype{current_termtype}; if ( secondary_da.terminal_id_version > 3999 ) { diff --git a/src/include/final/ftermdebugdata.h b/src/include/final/ftermdebugdata.h index 5c4ad65b..e671064e 100644 --- a/src/include/final/ftermdebugdata.h +++ b/src/include/final/ftermdebugdata.h @@ -3,7 +3,7 @@ * * * This file is part of the FINAL CUT widget toolkit * * * -* Copyright 2018-2020 Markus Gans * +* Copyright 2018-2021 Markus Gans * * * * FINAL CUT is free software; you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * @@ -65,9 +65,9 @@ class FTermDebugData final // Accessors const FString& getAnswerbackString(); const FString& getSecDAString(); - const char* getTermType_256color(); - const char* getTermType_Answerback(); - const char* getTermType_SecDA(); + const FString& getTermType_256color(); + const FString& getTermType_Answerback(); + const FString& getTermType_SecDA(); #if defined(__linux__) int getFramebufferBpp(); #endif diff --git a/src/include/final/ftermdetection.h b/src/include/final/ftermdetection.h index 9a5fccaf..61819395 100644 --- a/src/include/final/ftermdetection.h +++ b/src/include/final/ftermdetection.h @@ -100,7 +100,7 @@ class FTermDetection final // Accessor FString getClassName() const; - const char* getTermType() const; + const FString& getTermType() const; int getGnomeTerminalID() const; kittyVersion getKittyVersion() const; FTerminalType& getTermTypeStruct(); @@ -108,9 +108,9 @@ class FTermDetection final #if DEBUG const FString& getAnswerbackString() const; const FString& getSecDAString() const; - const char* getTermType_256color() const; - const char* getTermType_Answerback() const; - const char* getTermType_SecDA() const; + const FString& getTermType_256color() const; + const FString& getTermType_Answerback() const; + const FString& getTermType_SecDA() const; #endif // Inquiries @@ -162,7 +162,7 @@ class FTermDetection final void setMltermTerminal (bool = true); void setKittyTerminal (bool = true); void setTerminalDetection (bool = true); - void setTtyTypeFileName (const char[]); + void setTtyTypeFileName (const FString&); // Methods void detect(); @@ -188,7 +188,6 @@ class FTermDetection final }; // Methods - void deallocation(); void getSystemTermType(); bool getTTYtype(); #if F_HAVE_GETTTYNAM @@ -196,45 +195,48 @@ class FTermDetection final #endif void termtypeAnalysis(); void detectTerminal(); - const char* init_256colorTerminal(); + FString init_256colorTerminal(); bool get256colorEnvString(); - const char* termtype_256color_quirks(); - const char* determineMaxColor (const char[]); - FString getXTermColorName (FColor); - const char* parseAnswerbackMsg (const char[]); - FString getAnswerbackMsg(); - const char* parseSecDA (const char[]); - int str2int (const FString&); - FString getSecDA(); - const char* secDA_Analysis (const char[]); - const char* secDA_Analysis_0 (const char[]); - const char* secDA_Analysis_1 (const char[]); - const char* secDA_Analysis_24 (const char[]); - const char* secDA_Analysis_32 (const char[]); - const char* secDA_Analysis_65 (const char[]); - const char* secDA_Analysis_67 (const char[]); - const char* secDA_Analysis_77 (const char[]); - const char* secDA_Analysis_82 (); - const char* secDA_Analysis_83 (const char[]); - const char* secDA_Analysis_84 (const char[]); - const char* secDA_Analysis_85 (); - const char* secDA_Analysis_vte (const char[]); - const char* secDA_Analysis_kitty (const char[]); + FString termtype_256color_quirks(); + FString determineMaxColor (const FString&); + FString getXTermColorName (FColor) const; + FString parseAnswerbackMsg (const FString&); + FString getAnswerbackMsg() const; + FString parseSecDA (const FString&); + int str2int (const FString&) const; + FString getSecDA() const; + FString secDA_Analysis (const FString&); + FString secDA_Analysis_0 (const FString&); + FString secDA_Analysis_1 (const FString&); + FString secDA_Analysis_24 (const FString&); + FString secDA_Analysis_32 (); + FString secDA_Analysis_65 (const FString&); + FString secDA_Analysis_67 (); + FString secDA_Analysis_77 (); + FString secDA_Analysis_82 (); + FString secDA_Analysis_83 (const FString&); + FString secDA_Analysis_84 (const FString&); + FString secDA_Analysis_85 (); + FString secDA_Analysis_vte (const FString&); + FString secDA_Analysis_kitty (const FString&); // Data members #if DEBUG - char termtype_256color[256]{}; - char termtype_Answerback[256]{}; - char termtype_SecDA[256]{}; + FString termtype_256color{}; + FString termtype_Answerback{}; + FString termtype_SecDA{}; #endif - char termtype[256]{}; - char ttytypename[256]{}; - bool decscusr_support{}; - bool terminal_detection{}; + FString termtype{}; + FString ttytypename{"/etc/ttytype"}; // Default ttytype file + bool decscusr_support{false}; // Preset to false + bool terminal_detection{true}; // Preset to true bool color256{}; - int gnome_terminal_id{}; - const FString* answer_back{nullptr}; - const FString* sec_da{nullptr}; + // Gnome terminal id from SecDA + // Example: vte version 0.40.0 = 0 * 100 + 40 * 100 + 0 = 4000 + // a.b.c = a * 100 + b * 100 + c + int gnome_terminal_id{0}; + FString answer_back{}; + FString sec_da{}; FTerminalType terminal_type{}; colorEnv color_env{}; kittyVersion kitty_version{}; @@ -248,7 +250,7 @@ inline FString FTermDetection::getClassName() const { return "FTermDetection"; } //---------------------------------------------------------------------- -inline const char* FTermDetection::getTermType() const +inline const FString& FTermDetection::getTermType() const { return termtype; } //---------------------------------------------------------------------- @@ -265,15 +267,15 @@ inline FTermDetection::FTerminalType& FTermDetection::getTermTypeStruct() #if DEBUG //---------------------------------------------------------------------- -inline const char* FTermDetection::getTermType_256color() const +inline const FString& FTermDetection::getTermType_256color() const { return termtype_256color; } //---------------------------------------------------------------------- -inline const char* FTermDetection::getTermType_Answerback() const +inline const FString& FTermDetection::getTermType_Answerback() const { return termtype_Answerback; } //---------------------------------------------------------------------- -inline const char* FTermDetection::getTermType_SecDA() const +inline const FString& FTermDetection::getTermType_SecDA() const { return termtype_SecDA; } #endif diff --git a/test/ftermdetection-test.cpp b/test/ftermdetection-test.cpp index e7e84166..a8fe7f8e 100644 --- a/test/ftermdetection-test.cpp +++ b/test/ftermdetection-test.cpp @@ -34,23 +34,6 @@ #include #include -#define CPPUNIT_ASSERT_CSTRING(expected, actual) \ - check_c_string (expected, actual, CPPUNIT_SOURCELINE()) - -//---------------------------------------------------------------------- -void check_c_string ( const char* s1 - , const char* s2 - , CppUnit::SourceLine sourceLine ) -{ - if ( s1 == 0 && s2 == 0 ) // Strings are equal - return; - - if ( s1 && s2 && std::strcmp (s1, s2) == 0 ) // Strings are equal - return; - - ::CppUnit::Asserter::fail ("Strings are not equal", sourceLine); -} - //---------------------------------------------------------------------- // class FTermDetectionTest @@ -188,14 +171,14 @@ void FTermDetectionTest::ansiTest() CPPUNIT_ASSERT ( ! detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( ! detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "ansi" ); + CPPUNIT_ASSERT ( detect.getTermType() == "ansi" ); // Test fallback to vt100 without TERM environment variable unsetenv("TERM"); detect.setAnsiTerminal(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isAnsiTerminal() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -326,7 +309,7 @@ void FTermDetectionTest::rxvtTest() CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "rxvt-16color" ); + CPPUNIT_ASSERT ( detect.getTermType() == "rxvt-16color" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -986,7 +969,7 @@ void FTermDetectionTest::linuxTest() detect.setLinuxTerm(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isLinuxTerm() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1061,7 +1044,7 @@ void FTermDetectionTest::freebsdTest() detect.detect(); CPPUNIT_ASSERT ( ! detect.isXTerminal() ); CPPUNIT_ASSERT ( ! detect.isFreeBSDTerm() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1134,7 +1117,7 @@ void FTermDetectionTest::netbsdTest() detect.setNetBSDTerm(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isFreeBSDTerm() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1207,7 +1190,7 @@ void FTermDetectionTest::openbsdTest() detect.setOpenBSDTerm(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isOpenBSDTerm() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1278,7 +1261,7 @@ void FTermDetectionTest::sunTest() detect.setSunTerminal(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isSunTerminal() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1344,12 +1327,12 @@ void FTermDetectionTest::screenTest() CPPUNIT_ASSERT ( ! detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "screen" ); + CPPUNIT_ASSERT ( detect.getTermType() == "screen" ); setenv ("XTERM_VERSION", "XTerm(312)", 1); detect.detect(); CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "screen-256color" ); + CPPUNIT_ASSERT ( detect.getTermType() == "screen-256color" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1416,12 +1399,12 @@ void FTermDetectionTest::tmuxTest() CPPUNIT_ASSERT ( ! detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "screen" ); + CPPUNIT_ASSERT ( detect.getTermType() == "screen" ); setenv ("VTE_VERSION", "3801", 1); detect.detect(); CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "screen-256color" ); + CPPUNIT_ASSERT ( detect.getTermType() == "screen-256color" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1493,7 +1476,7 @@ void FTermDetectionTest::ktermTest() detect.setKtermTerminal(false); detect.detect(); CPPUNIT_ASSERT ( ! detect.isKtermTerminal() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1560,13 +1543,13 @@ void FTermDetectionTest::mltermTest() CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "mlterm-256color" ); + CPPUNIT_ASSERT ( detect.getTermType() == "mlterm-256color" ); setenv ("TERM", "mlterm", 1); unsetenv("COLORFGBG"); detect.detect(); CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "xterm-256color" ); + CPPUNIT_ASSERT ( detect.getTermType() == "xterm-256color" ); printConEmuDebug(); closeConEmuStdStreams(); @@ -1631,7 +1614,7 @@ void FTermDetectionTest::kittyTest() CPPUNIT_ASSERT ( detect.canDisplay256Colors() ); CPPUNIT_ASSERT ( detect.hasTerminalDetection() ); CPPUNIT_ASSERT ( ! detect.hasSetCursorStyleSupport() ); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "xterm-kitty" ); + CPPUNIT_ASSERT ( detect.getTermType() == "xterm-kitty" ); auto kitty_version = detect.getKittyVersion(); CPPUNIT_ASSERT ( kitty_version.primary == 0 ); @@ -1714,17 +1697,17 @@ void FTermDetectionTest::ttytypeTest() // Test /dev/tty3 with linux data.setTermFileName("/dev/tty3"); detect.detect(); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "linux" ); + CPPUNIT_ASSERT ( detect.getTermType() == "linux" ); // Test /dev/ttyp0 with vt100 data.setTermFileName("/dev/ttyp0"); detect.detect(); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); // Test non-existent /dev/tty8 with fallback to vt100 data.setTermFileName("/dev/tty8"); detect.detect(); - CPPUNIT_ASSERT_CSTRING ( detect.getTermType(), "vt100" ); + CPPUNIT_ASSERT ( detect.getTermType() == "vt100" ); printConEmuDebug(); closeConEmuStdStreams(); diff --git a/test/ftermopenbsd-test.cpp b/test/ftermopenbsd-test.cpp index 4b0c35ea..a61c6119 100644 --- a/test/ftermopenbsd-test.cpp +++ b/test/ftermopenbsd-test.cpp @@ -522,7 +522,7 @@ void ftermopenbsdTest::openbsdConsoleTest() CPPUNIT_ASSERT ( data.getTermGeometry().getHeight() == 25 ); CPPUNIT_ASSERT ( ! data.hasShadowCharacter() ); CPPUNIT_ASSERT ( ! data.hasHalfBlockCharacter() ); - CPPUNIT_ASSERT_CSTRING ( term_detection.getTermType(), "pccon" ); + CPPUNIT_ASSERT ( term_detection.getTermType() == "pccon" ); openbsd.finish();