From b9e06b5f2c44c30d7d53ce442e1992ab83fd7941 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 5 Dec 2014 11:36:31 +0300 Subject: [PATCH 1/3] Ticket #3339: fix pasting from clipboard. When clipboard_paste option was set but clipboard utility was not installed, nothing was pasted and clipboard file became empty. Solution: reimplement pasting way using mc_pipe_t instead of my_system(). Signed-off-by: Andrew Borodin --- src/clipboard.c | 62 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/src/clipboard.c b/src/clipboard.c index 65a88821f..226b3fba3 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -6,6 +6,7 @@ Written by: Ilia Maslakov , 2010. + Andrew Borodin , 2014. This file is part of the Midnight Commander. @@ -93,7 +94,8 @@ gboolean clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name, gpointer init_data, gpointer data) { - char *tmp, *cmd; + mc_pipe_t *p; + int file = -1; const char *d = getenv ("DISPLAY"); (void) event_group_name; @@ -104,14 +106,60 @@ clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * even if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0') return TRUE; - tmp = mc_config_get_full_path (EDIT_CLIP_FILE); - cmd = g_strconcat (clipboard_paste_path, " > ", tmp, " 2>/dev/null", (char *) NULL); + p = mc_popen (clipboard_paste_path, NULL); + if (p == NULL) + return TRUE; /* don't show error message */ - if (cmd != NULL) - my_system (EXECUTE_AS_SHELL, mc_global.tty.shell, cmd); + p->out.null_term = FALSE; + p->err.null_term = TRUE; + + while (TRUE) + { + GError *error = NULL; + + p->out.len = MC_PIPE_BUFSIZE; + p->err.len = MC_PIPE_BUFSIZE; + + mc_pread (p, &error); + + if (error != NULL) + { + /* don't show error message */ + g_error_free (error); + break; + } + + /* ignore stderr and get stdout */ + if (p->out.len == MC_PIPE_STREAM_EOF || p->out.len == MC_PIPE_ERROR_READ) + break; + + if (p->out.len > 0) + { + ssize_t nwrite; + + if (file < 0) + { + vfs_path_t *fname_vpath; + + fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE); + file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY); + vfs_path_free (fname_vpath); + + if (file < 0) + break; + } + + nwrite = mc_write (file, p->out.buf, p->out.len); + (void) nwrite; + } + } + + if (file >= 0) + mc_close (file); + + mc_pclose (p, NULL); - g_free (cmd); - g_free (tmp); return TRUE; } From dcb19a3c832c075ccab2fdeeccc770b1fd6bf347 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 8 Dec 2014 14:05:19 +0300 Subject: [PATCH 2/3] Clarify flags and mode to open clipboard file. Signed-off-by: Andrew Borodin --- src/clipboard.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/clipboard.c b/src/clipboard.c index 226b3fba3..aa98b96f5 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -28,6 +28,8 @@ #include #include +#include +#include #include "lib/global.h" #include "lib/fileloc.h" @@ -53,6 +55,10 @@ char *clipboard_paste_path = NULL; /*** file scope variables ************************************************************************/ +static const int clip_open_flags = O_CREAT | O_WRONLY | O_TRUNC | O_BINARY; +static const mode_t clip_open_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; + +/* --------------------------------------------------------------------------------------------- */ /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ @@ -142,8 +148,7 @@ clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * even vfs_path_t *fname_vpath; fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE); - file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY); + file = mc_open (fname_vpath, clip_open_flags, clip_open_mode); vfs_path_free (fname_vpath); if (file < 0) @@ -183,8 +188,7 @@ clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name return FALSE; fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE); - file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY); + file = mc_open (fname_vpath, clip_open_flags, clip_open_mode); vfs_path_free (fname_vpath); if (file == -1) From ede5f9bad459f7f85564ddbdd7d656e0d058ee38 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 8 Dec 2014 14:09:28 +0300 Subject: [PATCH 3/3] Clarify description of clipboard_store and clipboard_paste options. Signed-off-by: Andrew Borodin --- doc/man/es/mc.1.in | 4 ++-- doc/man/mc.1.in | 4 ++-- doc/man/ru/mc.1.in | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/man/es/mc.1.in b/doc/man/es/mc.1.in index a1c3729fc..88f40724b 100644 --- a/doc/man/es/mc.1.in +++ b/doc/man/es/mc.1.in @@ -3927,7 +3927,7 @@ para cargar texto de un archivo como selección en X Window. Por ejemplo: .PP .nf -clipboard_store=xclip \-i +clipboard_store=/usr/bin/xclip \-i .fi .TP .I clipboard_paste @@ -3936,7 +3936,7 @@ para volcar la selección de X Window a la salida estándar. Por ejemplo: .PP .nf -clipboard_paste=xclip \-o +clipboard_paste=/usr/bin/xclip \-o .fi .TP .I autodetect_codeset diff --git a/doc/man/mc.1.in b/doc/man/mc.1.in index 79cad2906..34b6ddb31 100644 --- a/doc/man/mc.1.in +++ b/doc/man/mc.1.in @@ -4108,7 +4108,7 @@ utility like 'xclip' to read text into X selection from file. For example: .PP .nf -clipboard_store=xclip \-i +clipboard_store=/usr/bin/xclip \-i .fi .TP .I clipboard_paste @@ -4117,7 +4117,7 @@ utility like 'xclip' to print the selection to standard out. For example: .PP .nf -clipboard_paste=xclip \-o +clipboard_paste=/usr/bin/xclip \-o .fi .TP .I autodetect_codeset diff --git a/doc/man/ru/mc.1.in b/doc/man/ru/mc.1.in index cb3c0529f..4b8e74c43 100644 --- a/doc/man/ru/mc.1.in +++ b/doc/man/ru/mc.1.in @@ -4491,7 +4491,7 @@ mc.ext\&. Например: .PP .nf -clipboard_store=xclip \-i +clipboard_store=/usr/bin/xclip \-i .fi .TP .I clipboard_paste @@ -4501,7 +4501,7 @@ clipboard_store=xclip \-i Например: .PP .nf -clipboard_paste=xclip \-o +clipboard_paste=/usr/bin/xclip \-o .fi .PP .I autodetect_codeset