@ 261105.1

260809.2 · b2e3c26232ca1e8fa762d6db9f8f4337f6c9e4e7

added terminal output sanitation of user input

reading declarations

Changes to src/cli/main.cpp. All files

src/cli/main.cpp

@@ -38,6 +38,7 @@ along with Highlight.  If not, see <http://www.gnu.org/licenses/>. #include "datadir.h" #include "syntaxreader.h" #include "lspprofile.h"+#include "stringtools.h"  using std::cerr; using std::cin;@@ -97,7 +98,7 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin     bool directoryOK = Platform::getDirectoryEntries ( filePaths, searchDir, true );     if ( !directoryOK ) {         std::cerr << "highlight: Could not access directory "-             <<  searchDir+             <<  StringTools::sanitizeForTerminal(searchDir)              << ", aborted.\n";         return EXIT_FAILURE;     }@@ -108,9 +109,9 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin     std::cout << "\nInstalled " << kind << "s";      if (categoryFilterList.size())-        std::cout << " matching \""<<categoryFilterList<<"\"";+        std::cout << " matching \""<< StringTools::sanitizeForTerminal(categoryFilterList) <<"\""; -    std::cout << " (located in " << where << "):\n\n";+    std::cout << " (located in " << StringTools::sanitizeForTerminal(where) << "):\n\n";     int matchedFileCnt=0;     std::set<string> categoryNames;     std::set<string> categoryFilters;@@ -127,10 +128,11 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin             Diluculum::LuaState ls;             highlight::SyntaxReader::initLuaState(ls, path,"");             ls.doFile(path);-            desc = ls["Description"].value().asString();+            desc = StringTools::sanitizeForTerminal(ls["Description"].value().asString());              suffix = ( path ).substr ( where.length() ) ;             suffix = suffix.substr ( 0, suffix.length()- wildcard.length()+1 );+            suffix = StringTools::sanitizeForTerminal(suffix);              unsigned int filterOKCnt=categoryFilters.size();             if (ls["Categories"].value() !=Diluculum::Nil){@@ -156,7 +158,7 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin                 int extCnt=0;                 for (const auto &[first, second] : dataDir.assocByExtension) {                     if (second==suffix ) {-                        std::cout << ((++extCnt==1)?" ( ":" ") << first;+                        std::cout << ((++extCnt==1)?" ( ":" ") << StringTools::sanitizeForTerminal(first);                     }                 }                 std::cout << ((extCnt)?" )":"");@@ -166,7 +168,7 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin             }             std::cout << endl;         } catch (std::runtime_error &error) {-            std::cout << "Failed to read '" << path << "': " << error.what() << endl;+            std::cout << "Failed to read '" << StringTools::sanitizeForTerminal(path) << "': " << StringTools::sanitizeForTerminal(error.what()) << endl;         }     } @@ -177,7 +179,7 @@ int HLCmdLineApp::printInstalledFiles(const std::string& where, const std::strin         if (!categoryFilters.size()){             std::cout << "\nFound "<<kind<<" categories:\n\n";             for ( const auto &catName : categoryNames)-                std::cout << catName << ' ';+                std::cout << StringTools::sanitizeForTerminal(catName) << ' ';             std::cout << "\n";         } @@ -201,8 +203,8 @@ void HLCmdLineApp::printDebugInfo ( const highlight::SyntaxReader *lang,      map <int, string> HLStateMap; -    cerr << "\nLoading language definition:\n" << langDefPath;-    cerr << "\n\nDescription: " << lang->getDescription();+    cerr << "\nLoading language definition:\n" << StringTools::sanitizeForTerminal(langDefPath);+    cerr << "\n\nDescription: " << StringTools::sanitizeForTerminal(lang->getDescription());     if (level>1) {          if (auto luaState = lang->getLuaState(); luaState) {@@ -211,10 +213,10 @@ void HLCmdLineApp::printDebugInfo ( const highlight::SyntaxReader *lang,              for (const auto &[first, second] : glob) {                 auto elemName = first.asString();-                std::cerr << elemName << ": ";+                std::cerr << StringTools::sanitizeForTerminal(elemName) << ": ";                 switch (second.type()) {                     case  LUA_TSTRING:-                        cerr << "string [ "<<second.asString()<<" ]";+                        cerr << "string [ "<< StringTools::sanitizeForTerminal(second.asString()) <<" ]";                         break;                     case  LUA_TNUMBER:                         cerr << "number [ "<<second.asNumber()<<" ]";@@ -234,13 +236,13 @@ void HLCmdLineApp::printDebugInfo ( const highlight::SyntaxReader *lang,         if (auto regexElements = lang->getRegexElements(); !regexElements.empty()) {             std::cerr << "\nREGEX:\n";             for (const auto *re : regexElements)-                std::cerr << "State " << re->open << " (" << HLStateMap[re->open] << "):\t" << re->pattern << "\n";+                std::cerr << "State " << re->open << " (" << StringTools::sanitizeForTerminal(HLStateMap[re->open]) << "):\t" << StringTools::sanitizeForTerminal(re->pattern) << "\n";         }          if (auto keys = lang->getKeywords(); !keys.empty()) {             std::cerr << "\nKEYWORDS:\n";             for (const auto &[keyword, value] : keys)-                std::cerr << " " << keyword << "(" << value << ")";+                std::cerr << " " << StringTools::sanitizeForTerminal(keyword) << "(" << value << ")";         }     }     cerr <<"\n\n";@@ -294,7 +296,7 @@ void HLCmdLineApp::printCurrentAction ( const string&outfilePath,          << " of "          << total          << ": "-         << outfilePath+         << StringTools::sanitizeForTerminal(outfilePath)          << "\n"; } @@ -311,7 +313,9 @@ void HLCmdLineApp::printIOErrorReport ( unsigned int numberErrorFiles,         cerr<<streamName<<"\n";     }     else {-        copy ( fileList.begin(), fileList.end(), ostream_iterator<string> ( cerr, "\n" ) );+        for (const auto& file : fileList) {+            cerr << StringTools::sanitizeForTerminal(file) << "\n";+        }     }      if ( fileList.size() < numberErrorFiles ) {@@ -344,7 +348,7 @@ bool HLCmdLineApp::serviceModeCheck(CmdLineOptions &options, highlight::CodeGene      cerr << flush;     if (! service_mode_tag.empty())-        std::cout << service_mode_tag << endl;+        std::cout << StringTools::sanitizeForTerminal(service_mode_tag) << endl;      std::cout << flush; @@ -405,10 +409,10 @@ bool HLCmdLineApp::serviceModeCheck(CmdLineOptions &options, highlight::CodeGene             service_mode_tag = modeVal;             modeRes += "Tag: " + service_mode_tag + " ";         } else if (modeKey.length() )-            cerr << "Invalid service mode key change of: " << modeKey << " ignoring." << endl;+            cerr << "Invalid service mode key change of: " << StringTools::sanitizeForTerminal(modeKey) << " ignoring." << endl;     }     if (options.verbosityLevel())-        cerr << "Updated Items: " << modeRes << endl;+        cerr << "Updated Items: " << StringTools::sanitizeForTerminal(modeRes) << endl;      return true; }@@ -484,7 +488,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )             if (lsLegacy==false)                 lsLegacy = profile.legacy;         } else {-            cerr << "highlight: Unknown LSP profile '"<< lsProfile << "'.\n";+            cerr << "highlight: Unknown LSP profile '"<< StringTools::sanitizeForTerminal(lsProfile) << "'.\n";             return EXIT_FAILURE;         }     }@@ -500,7 +504,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )         else if (  scriptKind.find("lang")==0 ) {             return printInstalledFiles(dataDir.getLangPath(""), "*.lang", "langDef", OPT_SYNTAX, options.getCategories());         } else {-            cerr << "highlight: Unknown script type '"<< scriptKind << "'. Apply one of 'themes', 'langs' or 'plug-ins'.\n";+            cerr << "highlight: Unknown script type '"<< StringTools::sanitizeForTerminal(scriptKind) << "'. Apply one of 'themes', 'langs' or 'plug-ins'.\n";             return EXIT_FAILURE;         }     }@@ -596,9 +600,9 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )     for (const auto& pluginFile : pluginFileList) {         if ( !generator->initPluginScript(pluginFile) ) {             cerr << "highlight: "-                 << generator->getPluginScriptError()+                 << StringTools::sanitizeForTerminal(generator->getPluginScriptError())                  << " in "-                 << pluginFile+                 << StringTools::sanitizeForTerminal(pluginFile)                  <<"\n";             return EXIT_FAILURE;         }@@ -606,7 +610,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )      if ( !generator->initTheme ( themePath, options.isLsSemantic() ) ) {         cerr << "highlight: "-             << generator->getThemeInitError()+             << StringTools::sanitizeForTerminal(generator->getThemeInitError())              << "\n";         return EXIT_FAILURE;     }@@ -620,7 +624,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )         string cssOutFile=options.getOutDirectory()  + options.getStyleOutFilename();         bool success=generator->printExternalStyle ( useStdout?"":cssOutFile );         if ( !success ) {-            cerr << "highlight: Could not write " << cssOutFile <<".\n";+            cerr << "highlight: Could not write " << StringTools::sanitizeForTerminal(cssOutFile) <<".\n";             return EXIT_FAILURE;         }         return EXIT_SUCCESS;@@ -630,7 +634,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )      if ( !formattingEnabled && !options.getIndentScheme().empty() ) {         cerr << "highlight: Undefined indentation scheme "-             << options.getIndentScheme()+             << StringTools::sanitizeForTerminal(options.getIndentScheme())              << ".\n";         return EXIT_FAILURE;     }@@ -642,7 +646,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )     ifstream dirTest ( outDirectory.c_str() );     if ( !outDirectory.empty() && !options.quietMode() && !dirTest ) {         cerr << "highlight: Output directory \""-             << outDirectory+             << StringTools::sanitizeForTerminal(outDirectory)              << "\" does not exist.\n";         return EXIT_FAILURE;     }@@ -687,9 +691,9 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )         if (i==0 && twoPassMode) {              if ( !generator->initPluginScript(twoPassOutFile) ) {                 cerr << "highlight: "-                 << generator->getPluginScriptError()+                 << StringTools::sanitizeForTerminal(generator->getPluginScriptError())                  << " in "-                 << twoPassOutFile+                 << StringTools::sanitizeForTerminal(twoPassOutFile)                  <<"\n";                 initError = true;                 break;@@ -726,21 +730,21 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )              if ( loadRes==highlight::LOAD_FAILED_REGEX ) {                 cerr << "highlight: Regex error ( "-                     << generator->getSyntaxRegexError()-                     << " ) in "<<suffix<<".lang\n";+                     << StringTools::sanitizeForTerminal(generator->getSyntaxRegexError())+                     << " ) in "<< StringTools::sanitizeForTerminal(suffix) <<".lang\n";                 initError = true;                 break;             } else if ( loadRes==highlight::LOAD_FAILED_LUA ) {                 cerr << "highlight: Lua error ( "-                     << generator->getSyntaxLuaError()-                     << " ) in "<<suffix<<".lang\n";+                     << StringTools::sanitizeForTerminal(generator->getSyntaxLuaError())+                     << " ) in "<< StringTools::sanitizeForTerminal(suffix) <<".lang\n";                 initError = true;                 break;             } else if ( loadRes==highlight::LOAD_FAILED ) {                 // do also ignore error msg if --syntax parameter should be skipped                 if ( ! (options.forceOutput() || options.quietMode() || options.isSkippedExt ( suffix )) ) {                     cerr << "highlight: Unknown source file extension \""-                         << suffix+                         << StringTools::sanitizeForTerminal(suffix)                          << "\". Consider the "                          << (options.enableBatchMode() ? "--skip" : "--force or --syntax")                          << " option.\n";@@ -873,7 +877,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )             outFilePath = options.getSingleOutFilename();             if ( outFilePath.size() && outFilePath==options.getSingleInFilename() ) {                 cerr << "highlight: Output path equals input path: \""-                     << outFilePath << "\".\n";+                     << StringTools::sanitizeForTerminal(outFilePath) << "\".\n";                 initError = true;                 break;             }@@ -916,12 +920,12 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )              bool success=generator->printPersistentState(twoPassOutFile);             if ( !success ) {-                cerr << "highlight: Could not write "<< twoPassOutFile <<".\n";+                cerr << "highlight: Could not write "<< StringTools::sanitizeForTerminal(twoPassOutFile) <<".\n";                 IOError = true;             } else {                 twoPassMode=true;                 if ( !options.quietMode() &&  !options.forceStdout() ) {-                    std::cout << "Enabling two-pass mode using "<<twoPassOutFile<<"\n";+                    std::cout << "Enabling two-pass mode using "<< StringTools::sanitizeForTerminal(twoPassOutFile) <<"\n";                 }                 //start over, add plug-in to list in next iteration                 usedFileNames.clear();@@ -941,7 +945,7 @@ int HLCmdLineApp::run ( const int argc, const char*argv[] )         string cssOutFile=outDirectory  + options.getStyleOutFilename();         bool success=generator->printExternalStyle ( cssOutFile );         if ( !success ) {-            cerr << "highlight: Could not write " << cssOutFile <<".\n";+            cerr << "highlight: Could not write " << StringTools::sanitizeForTerminal(cssOutFile) <<".\n";             IOError = true;         }     }@@ -989,7 +993,7 @@ int main ( const int argc, const char *argv[] )         HLCmdLineApp app;         return app.run ( argc, argv );     } catch (std::exception &e) {-        std::cerr << "highlight: " << e.what() << std::endl;+        std::cerr << "highlight: " << StringTools::sanitizeForTerminal(e.what()) << std::endl;         return EXIT_FAILURE;     } catch (...) {         std::cerr << "highlight: An unknown error occurred." << std::endl;