From 6b02ee48c72841221c88dbdb74dc5b52808c43aa Mon Sep 17 00:00:00 2001
From: Vadim Ushakov <igeekless@gmail.com>
Date: Fri, 16 Oct 2020 00:01:30 +0700
Subject: [PATCH] Allow running clipboard commands if DISPLAY is not set
Implement a new configuration variable clipboard_force_no_display that
allows running commands specified in clipboard_store clipboard_paste even
if the DISPLAY environment variable is not set.
This can be used for running a clipboard script (from a virtual terminal)
that autodetects (or requests from sytemctl) the proper values of DISPLAY
and XAUTHORITY without the need of exporting these variables to the whole
virtual terminal session.
---
doc/man/mc.1.in | 11 +++++++++++
doc/man/ru/mc.1.in | 14 +++++++++++++-
src/clipboard.c | 11 +++++++----
src/clipboard.h | 1 +
src/setup.c | 4 ++++
5 files changed, 36 insertions(+), 5 deletions(-)
diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in
index cdd1f687b..fb2fdef4e 100644
a
|
b
|
For example: |
4192 | 4192 | clipboard_paste=xclip \-o |
4193 | 4193 | .fi |
4194 | 4194 | .TP |
| 4195 | .I clipboard_force_no_display |
| 4196 | Normally, Midnight Commander doesn't run commands specified in |
| 4197 | .I clipboard_store |
| 4198 | and |
| 4199 | .I clipboard_paste |
| 4200 | if the |
| 4201 | .B DISPLAY |
| 4202 | environment variable is not set. Setting this variable to true forces |
| 4203 | Midnight Commander to run the commands unconditionally. |
| 4204 | .fi |
| 4205 | .TP |
4195 | 4206 | .I autodetect_codeset |
4196 | 4207 | This option allows use the `enca' command to autodetect codeset of text files |
4197 | 4208 | in internal viewer and editor. List of valid values can be obtain by the |
diff --git a/doc/man/ru/mc.1.in b/doc/man/ru/mc.1.in
index f029b5f3e..6e2f1497b 100644
a
|
b
|
clipboard_store=xclip \-i |
4762 | 4762 | .nf |
4763 | 4763 | clipboard_paste=xclip \-o |
4764 | 4764 | .fi |
4765 | | .PP |
| 4765 | .TP |
| 4766 | .I clipboard_force_no_display |
| 4767 | По умолчанию Midnight Commander не запускает команды, указанные в |
| 4768 | .I clipboard_store |
| 4769 | и |
| 4770 | .I clipboard_paste\fR, |
| 4771 | если переменная окружения |
| 4772 | .B DISPLAY |
| 4773 | не установлена. Установка этой переменной в значение true обеспечивает |
| 4774 | принудительное исполнение команд без проверки переменной |
| 4775 | .B DISPLAY\fR. |
| 4776 | .fi |
| 4777 | .TP |
4766 | 4778 | .I autodetect_codeset |
4767 | 4779 | .IP |
4768 | 4780 | Эта опция позволяет использовать команду enca для автоматического |
diff --git a/src/clipboard.c b/src/clipboard.c
index 4e0b60c0b..5cceb6047 100644
a
|
b
|
|
48 | 48 | char *clipboard_store_path = NULL; |
49 | 49 | char *clipboard_paste_path = NULL; |
50 | 50 | |
| 51 | /* whether to run commands when DISPLAY is not set */ |
| 52 | gboolean clipboard_force_no_display = 0; |
| 53 | |
51 | 54 | /*** file scope macro definitions ****************************************************************/ |
52 | 55 | |
53 | 56 | /*** file scope type declarations ****************************************************************/ |
… |
… |
clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_ |
71 | 74 | gpointer init_data, gpointer data) |
72 | 75 | { |
73 | 76 | char *tmp, *cmd; |
74 | | const char *d = getenv ("DISPLAY"); |
| 77 | gboolean allowed = (clipboard_force_no_display) || (getenv ("DISPLAY") != NULL); |
75 | 78 | |
76 | 79 | (void) event_group_name; |
77 | 80 | (void) event_name; |
78 | 81 | (void) init_data; |
79 | 82 | (void) data; |
80 | 83 | |
81 | | if (d == NULL || clipboard_store_path == NULL || clipboard_store_path[0] == '\0') |
| 84 | if (!allowed || clipboard_store_path == NULL || clipboard_store_path[0] == '\0') |
82 | 85 | return TRUE; |
83 | 86 | |
84 | 87 | tmp = mc_config_get_full_path (EDIT_HOME_CLIP_FILE); |
… |
… |
clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * even |
101 | 104 | { |
102 | 105 | mc_pipe_t *p; |
103 | 106 | int file = -1; |
104 | | const char *d = getenv ("DISPLAY"); |
| 107 | gboolean allowed = (clipboard_force_no_display) || (getenv ("DISPLAY") != NULL); |
105 | 108 | |
106 | 109 | (void) event_group_name; |
107 | 110 | (void) event_name; |
108 | 111 | (void) init_data; |
109 | 112 | (void) data; |
110 | 113 | |
111 | | if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0') |
| 114 | if (!allowed || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0') |
112 | 115 | return TRUE; |
113 | 116 | |
114 | 117 | p = mc_popen (clipboard_paste_path, NULL); |
diff --git a/src/clipboard.h b/src/clipboard.h
index 9b2fc2216..a701e3655 100644
a
|
b
|
|
15 | 15 | |
16 | 16 | extern char *clipboard_store_path; |
17 | 17 | extern char *clipboard_paste_path; |
| 18 | extern gboolean clipboard_force_no_display; |
18 | 19 | |
19 | 20 | /*** declarations of public functions ************************************************************/ |
20 | 21 | |
diff --git a/src/setup.c b/src/setup.c
index 97da20940..7158d3c53 100644
a
|
b
|
load_setup (void) |
1178 | 1178 | mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_store", ""); |
1179 | 1179 | clipboard_paste_path = |
1180 | 1180 | mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_paste", ""); |
| 1181 | clipboard_force_no_display = |
| 1182 | mc_config_get_bool (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_force_no_display", FALSE); |
1181 | 1183 | } |
1182 | 1184 | |
1183 | 1185 | /* --------------------------------------------------------------------------------------------- */ |
… |
… |
save_setup (gboolean save_options, gboolean save_panel_options) |
1230 | 1232 | clipboard_store_path); |
1231 | 1233 | mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_paste", |
1232 | 1234 | clipboard_paste_path); |
| 1235 | mc_config_set_bool (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_force_no_display", |
| 1236 | clipboard_force_no_display); |
1233 | 1237 | |
1234 | 1238 | tmp_profile = mc_config_get_full_path (MC_CONFIG_FILE); |
1235 | 1239 | ret = mc_config_save_to_file (mc_global.main_config, tmp_profile, NULL); |