Merge pull request #6020 from Icinga/fix/console-home-env

Fix crash when running 'icinga2 console' without HOME environment variable
This commit is contained in:
Noah Hilverling 2018-01-29 16:37:24 +01:00 committed by GitHub
commit 9cb7c9d7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 10 deletions

View File

@ -287,10 +287,14 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
int next_line = 1; int next_line = 1;
#ifdef HAVE_EDITLINE #ifdef HAVE_EDITLINE
String homeEnv = getenv("HOME"); char *homeEnv = getenv("HOME");
String historyPath = homeEnv + "/.icinga2_history";
String historyPath;
std::fstream historyfp; std::fstream historyfp;
if (homeEnv) {
historyPath = String(homeEnv) + "/.icinga2_history";
historyfp.open(historyPath.CStr(), std::fstream::in); historyfp.open(historyPath.CStr(), std::fstream::in);
String line; String line;
@ -298,6 +302,7 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
add_history(line.CStr()); add_history(line.CStr());
historyfp.close(); historyfp.close();
}
#endif /* HAVE_EDITLINE */ #endif /* HAVE_EDITLINE */
l_ScriptFrame = &scriptFrame; l_ScriptFrame = &scriptFrame;
@ -370,10 +375,12 @@ incomplete:
if (commandOnce.IsEmpty() && cline[0] != '\0') { if (commandOnce.IsEmpty() && cline[0] != '\0') {
add_history(cline); add_history(cline);
if (!historyPath.IsEmpty()) {
historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app); historyfp.open(historyPath.CStr(), std::fstream::out | std::fstream::app);
historyfp << cline << "\n"; historyfp << cline << "\n";
historyfp.close(); historyfp.close();
} }
}
line = cline; line = cline;