From 043dfb37e6927399a26e686726f6e1b4f3c47264 Mon Sep 17 00:00:00 2001 From: Patrick Gaskin Date: Wed, 13 May 2020 11:38:05 -0400 Subject: [PATCH] Implemented quiet option for cmd_output (fixes #9) (#21) --- res/doc | 1 + src/action_cc.cc | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/res/doc b/res/doc index b2ca7cc..2a27d26 100644 --- a/res/doc +++ b/res/doc @@ -60,6 +60,7 @@ # cmd_spawn - the command line to pass to /bin/sh -c (started in /) # It can be prefixed with "quiet:" to prevent the toast with the process PID from being displayed. # cmd_output - the timeout in milliseconds (0 < t < 10000), a colon, then the command line to pass to /bin/sh -c (started in /) +# It can be prefixed with "quiet:" to prevent the message box with the output from being displayed (i.e. you'd use this where you'd normally use >/dev/null 2>&1). # # chain:: # chain_failure:: diff --git a/src/action_cc.cc b/src/action_cc.cc index 7f19877..139db0d 100644 --- a/src/action_cc.cc +++ b/src/action_cc.cc @@ -405,14 +405,22 @@ NM_ACTION_(cmd_spawn) { NM_ACTION_(cmd_output) { #define NM_ERR_RET nullptr + // split the timeout into timeout, put the command into cmd char *tmp = strdup(arg); - char *cmd = tmp; char *tmp1 = strtrim(strsep(&cmd, ":")), *tmp2; long timeout = strtol(tmp1, &tmp2, 10); cmd = strtrim(cmd); NM_ASSERT(*tmp1 && !*tmp2 && timeout > 0 && timeout < 10000, "invalid timeout '%s'", tmp1); + // parse the quiet option and update cmd if it's specified + char *tmp3 = strdup(cmd); + char *tmp4 = tmp3; + char *tmp5 = strtrim(strsep(&tmp4, ":")); + bool quiet = tmp4 && !strcmp(tmp5, "quiet"); + if (tmp4 && quiet) + cmd = strtrim(tmp4); // update cmd to exclude "quiet:" + QProcess proc; proc.setProcessChannelMode(QProcess::MergedChannels); proc.setWorkingDirectory(QStringLiteral("/")); @@ -445,9 +453,10 @@ NM_ACTION_(cmd_output) { if (out.length() > 500) out = out.left(500) + "..."; + free(tmp3); free(tmp); - NM_RETURN_OK(nm_action_result_msg("%s", qPrintable(out))); + NM_RETURN_OK(quiet ? nm_action_result_silent() : nm_action_result_msg("%s", qPrintable(out))); #undef NM_ERR_RET }