| 1 | /* |
| 2 | Periodic background shell command support. |
| 3 | |
| 4 | Copyright (C) 2021 |
| 5 | Free Software Foundation, Inc. |
| 6 | |
| 7 | Written by: |
| 8 | Sebastian Gniazdowski <sgniazdowski@gmail.com>, 2021 |
| 9 | |
| 10 | This file is part of the Midnight Commander. |
| 11 | |
| 12 | The Midnight Commander is free software: you can redistribute it |
| 13 | and/or modify it under the terms of the GNU General Public License as |
| 14 | published by the Free Software Foundation, either version 3 of the License, |
| 15 | or (at your option) any later version. |
| 16 | |
| 17 | The Midnight Commander is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | /** \file periodic_command.c |
| 27 | * \brief Periodic background shell command support. |
| 28 | * \author Sebastian Gniazdowski |
| 29 | * \date 2021 |
| 30 | * |
| 31 | * Periodic running of a specified shell command, after either: |
| 32 | * - specified interval passes, |
| 33 | * - specified # of chars will be inserted. |
| 34 | * |
| 35 | * The command is being run in background. |
| 36 | */ |
| 37 | |
| 38 | #include <config.h> |
| 39 | |
| 40 | |
| 41 | #include "lib/global.h" |
| 42 | #include "lib/widget.h" |
| 43 | #include "src/history.h" |
| 44 | #include "periodic_command.h" |
| 45 | |
| 46 | /*** global variables ****************************************************************************/ |
| 47 | |
| 48 | periodic_config_t periodic_config = {0}; |
| 49 | |
| 50 | /*** file scope macro definitions ****************************************************************/ |
| 51 | |
| 52 | /*** file scope type declarations ****************************************************************/ |
| 53 | |
| 54 | /*** file scope variables ************************************************************************/ |
| 55 | |
| 56 | /*** file scope functions ************************************************************************/ |
| 57 | /* --------------------------------------------------------------------------------------------- */ |
| 58 | |
| 59 | static void |
| 60 | periodic_idle_hook(void *data) |
| 61 | { |
| 62 | periodic_config_t * config = (periodic_config_t *) data; |
| 63 | gboolean should_run = FALSE; |
| 64 | gint64 cur_time; |
| 65 | |
| 66 | if (config == NULL || !config->periodic_enable) |
| 67 | return; |
| 68 | |
| 69 | if (!hook_present(idle_hook, periodic_idle_hook)) |
| 70 | add_hook(&idle_hook, periodic_idle_hook, data); |
| 71 | |
| 72 | /* Events exceeded? */ |
| 73 | config->cur_event_count ++; |
| 74 | if (config->event_lim <= config->cur_event_count) |
| 75 | should_run = TRUE; |
| 76 | |
| 77 | /* Limit time passed? */ |
| 78 | cur_time = g_get_real_time(); |
| 79 | if (cur_time - config->last_run_time >= config->interval) |
| 80 | should_run = TRUE; |
| 81 | |
| 82 | /* Should run? */ |
| 83 | if (should_run) |
| 84 | { |
| 85 | gboolean ret; |
| 86 | GError *error = NULL; |
| 87 | char *command_esc, *cline; |
| 88 | |
| 89 | config->last_run_time = cur_time; |
| 90 | config->cur_event_count = 0; |
| 91 | |
| 92 | command_esc = g_strescape(config->command_str, NULL); |
| 93 | cline = g_strdup_printf("sh -c \"%s >/dev/null 2>&1\"", command_esc); |
| 94 | ret = g_spawn_command_line_async(cline, &error); |
| 95 | |
| 96 | if (!ret) |
| 97 | { |
| 98 | config->periodic_enable = FALSE; |
| 99 | message(D_ERROR, _("Periodic command warning"), |
| 100 | _("Error running periodic command: %s. Periodic command stopped."), |
| 101 | error != NULL ? error->message : _("<no error message available>")); |
| 102 | g_error_free(error); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* --------------------------------------------------------------------------------------------- */ |
| 108 | /*** public functions ****************************************************************************/ |
| 109 | /* --------------------------------------------------------------------------------------------- */ |
| 110 | |
| 111 | void |
| 112 | edit_periodic_command_cmd(WDialog *h) |
| 113 | { |
| 114 | char *interval = NULL, *event_lim = NULL; |
| 115 | const char *pwd_dirs[3] = {N_("&Run where .git is located"), |
| 116 | N_("&Run where TAGS file is placed"), |
| 117 | N_("&Run in mcedit start dir")}; |
| 118 | gboolean was_enabled; |
| 119 | |
| 120 | quick_widget_t quick_widgets[] = { |
| 121 | /* *INDENT-OFF* */ |
| 122 | QUICK_LABELED_INPUT (_("Command"), input_label_above, INPUT_LAST_TEXT, |
| 123 | MC_HISTORY_PERIODIC_COMMAND, &periodic_config.command_str, |
| 124 | NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS), |
| 125 | QUICK_LABELED_INPUT (_("Interval [sec]"), input_label_above, INPUT_LAST_TEXT, |
| 126 | "periodic-interval", &interval, |
| 127 | NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS), |
| 128 | QUICK_LABELED_INPUT (_("Change limit trigger [number]"), input_label_above, INPUT_LAST_TEXT, |
| 129 | "periodic-change-limit", &event_lim, |
| 130 | NULL, FALSE, FALSE, INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS), |
| 131 | QUICK_SEPARATOR (TRUE), |
| 132 | QUICK_CHECKBOX (N_("&Enable periodic command"), &periodic_config.periodic_enable, NULL), |
| 133 | QUICK_SEPARATOR (TRUE), |
| 134 | QUICK_RADIO (3, pwd_dirs, &periodic_config.pwd_type_idx, NULL), |
| 135 | QUICK_BUTTONS_OK_CANCEL, |
| 136 | QUICK_END |
| 137 | /* *INDENT-ON* */ |
| 138 | }; |
| 139 | |
| 140 | quick_dialog_t qdlg = { |
| 141 | -1, -1, 50, |
| 142 | N_("Periodic command"), "[Periodic command]", |
| 143 | quick_widgets, dlg_default_callback, NULL |
| 144 | }; |
| 145 | was_enabled = periodic_config.periodic_enable; |
| 146 | |
| 147 | if (quick_dialog (&qdlg) != B_CANCEL) |
| 148 | { |
| 149 | periodic_config.last_run_time = g_get_real_time(); |
| 150 | periodic_config.cur_event_count = 0; |
| 151 | |
| 152 | errno = 0; |
| 153 | periodic_config.interval = strtol(interval, NULL, 10) * 1000000; |
| 154 | periodic_config.event_lim = strtol(event_lim, NULL, 10); |
| 155 | if (errno != 0) |
| 156 | { |
| 157 | message(D_ERROR, _("Periodic command warning"), |
| 158 | _("Provided values are not numeric (%s)"), g_strerror(errno)); |
| 159 | periodic_config.periodic_enable = FALSE; |
| 160 | } |
| 161 | else if (periodic_config.periodic_enable && !hook_present(idle_hook, periodic_idle_hook)) |
| 162 | add_hook(&idle_hook, periodic_idle_hook, &periodic_config); |
| 163 | } else |
| 164 | { |
| 165 | periodic_config.periodic_enable = was_enabled; |
| 166 | } |
| 167 | } |