(input_history_strip_password): actually url is not the pointer to constant.
[midnight-commander.git] / src / clipboard.c
blob5b04600e8ee1a6ed9c04547104bc706220b5aa18
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 "main.h"
40 #include "src/execute.h"
42 #include "clipboard.h"
44 /*** global variables ****************************************************************************/
46 /* path to X clipboard utility */
47 char *clipboard_store_path = NULL;
48 char *clipboard_paste_path = NULL;
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 /* --------------------------------------------------------------------------------------------- */
60 /*** public functions ****************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /* event callback */
64 gboolean
65 clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
66 gpointer init_data, gpointer data)
68 char *tmp, *cmd;
69 int res = 0;
70 const char *d = getenv ("DISPLAY");
72 (void) event_group_name;
73 (void) event_name;
74 (void) init_data;
75 (void) data;
77 if (d == NULL || clipboard_store_path == NULL || clipboard_store_path[0] == '\0')
78 return TRUE;
80 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
81 cmd = g_strconcat (clipboard_store_path, " ", tmp, " 2>/dev/null", (char *) NULL);
83 if (cmd != NULL)
84 res = my_system (EXECUTE_AS_SHELL, shell, cmd);
86 g_free (cmd);
87 g_free (tmp);
88 return TRUE;
91 /* --------------------------------------------------------------------------------------------- */
93 /* event callback */
94 gboolean
95 clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
96 gpointer init_data, gpointer data)
98 char *tmp, *cmd;
99 int res = 0;
100 const char *d = getenv ("DISPLAY");
102 (void) event_group_name;
103 (void) event_name;
104 (void) init_data;
105 (void) data;
107 if (d == NULL || clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0')
108 return TRUE;
110 tmp = mc_config_get_full_path (EDIT_CLIP_FILE);
111 cmd = g_strconcat (clipboard_paste_path, " > ", tmp, " 2>/dev/null", (char *) NULL);
113 if (cmd != NULL)
114 res = my_system (EXECUTE_AS_SHELL, shell, cmd);
116 g_free (cmd);
117 g_free (tmp);
118 return TRUE;
121 /* --------------------------------------------------------------------------------------------- */
123 /* event callback */
124 gboolean
125 clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
126 gpointer init_data, gpointer data)
128 int file;
129 vfs_path_t *fname_vpath = NULL;
130 ssize_t ret;
131 size_t str_len;
132 const char *text = (const char *) data;
134 (void) event_group_name;
135 (void) event_name;
136 (void) init_data;
138 if (text == NULL)
139 return FALSE;
141 fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE);
142 file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC,
143 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
144 vfs_path_free (fname_vpath);
146 if (file == -1)
147 return TRUE;
149 str_len = strlen (text);
150 ret = mc_write (file, (char *) text, str_len);
151 mc_close (file);
152 return TRUE;
155 /* --------------------------------------------------------------------------------------------- */
157 /* event callback */
158 gboolean
159 clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
160 gpointer init_data, gpointer data)
162 char buf[BUF_LARGE];
163 FILE *f;
164 char *fname = NULL;
165 gboolean first = TRUE;
166 ev_clipboard_text_from_file_t *event_data = (ev_clipboard_text_from_file_t *) data;
168 (void) event_group_name;
169 (void) event_name;
170 (void) init_data;
172 fname = mc_config_get_full_path (EDIT_CLIP_FILE);
173 f = fopen (fname, "r");
174 g_free (fname);
176 if (f == NULL)
178 event_data->ret = FALSE;
179 return TRUE;
182 *(event_data->text) = NULL;
184 while (fgets (buf, sizeof (buf), f))
186 size_t len;
188 len = strlen (buf);
189 if (len > 0)
191 if (buf[len - 1] == '\n')
192 buf[len - 1] = '\0';
194 if (first)
196 first = FALSE;
197 *(event_data->text) = g_strdup (buf);
199 else
201 /* remove \n on EOL */
202 char *tmp;
204 tmp = g_strconcat (*(event_data->text), " ", buf, (char *) NULL);
205 g_free (*(event_data->text));
206 *(event_data->text) = tmp;
211 fclose (f);
212 event_data->ret = (*(event_data->text) != NULL);
213 return TRUE;
216 /* --------------------------------------------------------------------------------------------- */