(mc_global_t::shell): new member to store user's shell
[midnight-commander.git] / src / clipboard.c
blob375a81b79148049bcead2caf441e689650a96574
1 /*
2 Util for external clipboard.
4 Copyright (C) 2009, 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Ilia Maslakov <il.smind@gmail.com>, 2010.
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/mcconfig.h"
34 #include "lib/util.h"
35 #include "lib/event.h"
37 #include "lib/vfs/vfs.h"
39 #include "src/execute.h"
41 #include "clipboard.h"
43 /*** global variables ****************************************************************************/
45 /* path to X clipboard utility */
46 char *clipboard_store_path = NULL;
47 char *clipboard_paste_path = NULL;
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
58 /* --------------------------------------------------------------------------------------------- */
59 /*** public functions ****************************************************************************/
60 /* --------------------------------------------------------------------------------------------- */
62 /* event callback */
63 gboolean
64 clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
65 gpointer init_data, gpointer data)
67 char *tmp, *cmd;
68 int res = 0;
69 const char *d = getenv ("DISPLAY");
71 (void) event_group_name;
72 (void) event_name;
73 (void) init_data;
74 (void) data;
76 if (d == NULL || clipboard_store_path == NULL || clipboard_store_path[0] == '\0')
77 return TRUE;
79 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
80 cmd = g_strconcat (clipboard_store_path, " ", tmp, " 2>/dev/null", (char *) NULL);
82 if (cmd != NULL)
83 res = my_system (EXECUTE_AS_SHELL, mc_global.tty.shell, cmd);
85 g_free (cmd);
86 g_free (tmp);
87 return TRUE;
90 /* --------------------------------------------------------------------------------------------- */
92 /* event callback */
93 gboolean
94 clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
95 gpointer init_data, gpointer data)
97 char *tmp, *cmd;
98 int res = 0;
99 const char *d = getenv ("DISPLAY");
101 (void) event_group_name;
102 (void) event_name;
103 (void) init_data;
104 (void) data;
106 if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0')
107 return TRUE;
109 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
110 cmd = g_strconcat (clipboard_paste_path, " > ", tmp, " 2>/dev/null", (char *) NULL);
112 if (cmd != NULL)
113 res = my_system (EXECUTE_AS_SHELL, mc_global.tty.shell, cmd);
115 g_free (cmd);
116 g_free (tmp);
117 return TRUE;
120 /* --------------------------------------------------------------------------------------------- */
122 /* event callback */
123 gboolean
124 clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
125 gpointer init_data, gpointer data)
127 int file;
128 vfs_path_t *fname_vpath = NULL;
129 ssize_t ret;
130 size_t str_len;
131 const char *text = (const char *) data;
133 (void) event_group_name;
134 (void) event_name;
135 (void) init_data;
137 if (text == NULL)
138 return FALSE;
140 fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE);
141 file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC,
142 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
143 vfs_path_free (fname_vpath);
145 if (file == -1)
146 return TRUE;
148 str_len = strlen (text);
149 ret = mc_write (file, (char *) text, str_len);
150 mc_close (file);
151 return TRUE;
154 /* --------------------------------------------------------------------------------------------- */
156 /* event callback */
157 gboolean
158 clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
159 gpointer init_data, gpointer data)
161 char buf[BUF_LARGE];
162 FILE *f;
163 char *fname = NULL;
164 gboolean first = TRUE;
165 ev_clipboard_text_from_file_t *event_data = (ev_clipboard_text_from_file_t *) data;
167 (void) event_group_name;
168 (void) event_name;
169 (void) init_data;
171 fname = mc_config_get_full_path (EDIT_CLIP_FILE);
172 f = fopen (fname, "r");
173 g_free (fname);
175 if (f == NULL)
177 event_data->ret = FALSE;
178 return TRUE;
181 *(event_data->text) = NULL;
183 while (fgets (buf, sizeof (buf), f))
185 size_t len;
187 len = strlen (buf);
188 if (len > 0)
190 if (buf[len - 1] == '\n')
191 buf[len - 1] = '\0';
193 if (first)
195 first = FALSE;
196 *(event_data->text) = g_strdup (buf);
198 else
200 /* remove \n on EOL */
201 char *tmp;
203 tmp = g_strconcat (*(event_data->text), " ", buf, (char *) NULL);
204 g_free (*(event_data->text));
205 *(event_data->text) = tmp;
210 fclose (f);
211 event_data->ret = (*(event_data->text) != NULL);
212 return TRUE;
215 /* --------------------------------------------------------------------------------------------- */