@@ -1026,7 +1026,7 @@ void UiModel::Impl::OpenLink(const std::string& p_Url)
10261026 std::string cmd = cmdTemplate;
10271027 StrUtil::ReplaceString (cmd, " %1" , p_Url);
10281028
1029- RunCommand (cmd);
1029+ RunProgram (cmd);
10301030}
10311031
10321032void UiModel::Impl::OpenAttachment (const std::string& p_Path)
@@ -1049,10 +1049,51 @@ void UiModel::Impl::OpenAttachment(const std::string& p_Path)
10491049 std::string cmd = cmdTemplate;
10501050 StrUtil::ReplaceString (cmd, " %1" , p_Path);
10511051
1052- RunCommand (cmd);
1052+ RunProgram (cmd);
10531053}
10541054
1055- void UiModel::Impl::RunCommand (const std::string& p_Cmd)
1055+ // Used when taking over terminal is disallowed or when output needs to be captured
1056+ bool UiModel::Impl::RunCommand (const std::string& p_Cmd, std::string* p_StdOut /* = nullptr*/ )
1057+ {
1058+ const bool logStdErr = Log::GetDebugEnabled ();
1059+ const std::string stdoutFile = (p_StdOut != nullptr ) ? FileUtil::MkTempFile () : " /dev/null" ;
1060+ const std::string stderrFile = logStdErr ? FileUtil::MkTempFile () : " /dev/null" ;
1061+ const std::string cmdPrefix = " 2>'" + stderrFile + " ' " ;
1062+ const std::string cmdSuffix = " >'" + stdoutFile + " '" ;
1063+ const std::string cmd = cmdPrefix + p_Cmd + cmdSuffix;
1064+
1065+ // run command
1066+ LOG_TRACE (" cmd \" %s\" start" , cmd.c_str ());
1067+ const int rv = system (cmd.c_str ());
1068+ if (rv != 0 )
1069+ {
1070+ LOG_WARNING (" cmd \" %s\" failed (%d)" , cmd.c_str (), rv);
1071+ }
1072+
1073+ // stdout
1074+ if ((p_StdOut != nullptr ) && FileUtil::Exists (stdoutFile))
1075+ {
1076+ *p_StdOut = FileUtil::ReadFile (stdoutFile);
1077+ FileUtil::RmFile (stdoutFile);
1078+ }
1079+
1080+ // stderr
1081+ if (logStdErr && FileUtil::Exists (stderrFile))
1082+ {
1083+ const std::string stderrStr = FileUtil::ReadFile (stderrFile);
1084+ FileUtil::RmFile (stderrFile);
1085+ if (!stderrStr.empty ())
1086+ {
1087+ LOG_DEBUG (" cmd \" %s\" stderr:" , cmd.c_str ());
1088+ Log::Dump (stderrStr.c_str ());
1089+ }
1090+ }
1091+
1092+ return (rv == 0 );
1093+ }
1094+
1095+ // Used when taking over terminal is allowed
1096+ void UiModel::Impl::RunProgram (const std::string& p_Cmd)
10561097{
10571098 bool isBackground = (p_Cmd.back () == ' &' );
10581099
@@ -2624,12 +2665,7 @@ void UiModel::Impl::DesktopNotifyUnread(const std::string& p_Name, const std::st
26242665 StrUtil::ReplaceString (cmd, " %2" , text);
26252666
26262667 // run command
2627- LOG_TRACE (" cmd \" %s\" start" , cmd.c_str ());
2628- int rv = system (cmd.c_str ());
2629- if (rv != 0 )
2630- {
2631- LOG_WARNING (" cmd \" %s\" failed (%d)" , cmd.c_str (), rv);
2632- }
2668+ RunCommand (cmd);
26332669}
26342670
26352671void UiModel::Impl::SetHistoryInteraction (bool p_HistoryInteraction)
@@ -3141,7 +3177,7 @@ void UiModel::Impl::StartExtCall(const std::string& p_Phone)
31413177 std::string cmd = cmdTemplate;
31423178 StrUtil::ReplaceString (cmd, " %1" , p_Phone);
31433179
3144- RunCommand (cmd);
3180+ RunProgram (cmd);
31453181}
31463182
31473183void UiModel::Impl::SendProtocolRequest (const std::string& p_ProfileId, std::shared_ptr<RequestMessage> p_Request)
0 commit comments