summaryrefslogtreecommitdiffstats
path: root/src/clipboard.c
blob: ab154cb134722ddb87a0a8e273b1aaa8238c08ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
   Util for external clipboard.

   Copyright (C) 2009-2022
   Free Software Foundation, Inc.

   Written by:
   Ilia Maslakov <il.smind@gmail.com>, 2010.
   Andrew Borodin <aborodin@vmail.ru>, 2014.

   This file is part of the Midnight Commander.

   The Midnight Commander is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   The Midnight Commander is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#include "lib/global.h"
#include "lib/fileloc.h"
#include "lib/mcconfig.h"
#include "lib/util.h"
#include "lib/event.h"

#include "lib/vfs/vfs.h"

#include "src/execute.h"

#include "clipboard.h"

/*** global variables ****************************************************************************/

/* path to X clipboard utility */
char *clipboard_store_path = NULL;
char *clipboard_paste_path = NULL;

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** 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 ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* event callback */
gboolean
clipboard_file_to_ext_clip (const gchar * event_group_name, const gchar * event_name,
                            gpointer init_data, gpointer data)
{
    char *tmp, *cmd;

    (void) event_group_name;
    (void) event_name;
    (void) init_data;
    (void) data;

    if (clipboard_store_path == NULL || clipboard_store_path[0] == '\0')
        return TRUE;

    tmp = mc_config_get_full_path (EDIT_HOME_CLIP_FILE);
    cmd = g_strconcat (clipboard_store_path, " ", tmp, " 2>/dev/null", (char *) NULL);

    if (cmd != NULL)
        my_system (EXECUTE_AS_SHELL, mc_global.shell->path, cmd);

    g_free (cmd);
    g_free (tmp);
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

/* event callback */
gboolean
clipboard_file_from_ext_clip (const gchar * event_group_name, const gchar * event_name,
                              gpointer init_data, gpointer data)
{
    mc_pipe_t *p;
    int file = -1;

    (void) event_group_name;
    (void) event_name;
    (void) init_data;
    (void) data;

    if (clipboard_paste_path == NULL || clipboard_paste_path[0] == '\0')
        return TRUE;

    p = mc_popen (clipboard_paste_path, TRUE, TRUE, NULL);
    if (p == NULL)
        return TRUE;            /* don't show error message */

    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_HOME_CLIP_FILE);
                file = mc_open (fname_vpath, clip_open_flags, clip_open_mode);
                vfs_path_free (fname_vpath, TRUE);

                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);

    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

/* event callback */
gboolean
clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name,
                        gpointer init_data, gpointer data)
{
    int file;
    vfs_path_t *fname_vpath = NULL;
    size_t str_len;
    const char *text = (const char *) data;

    (void) event_group_name;
    (void) event_name;
    (void) init_data;

    if (text == NULL)
        return FALSE;

    fname_vpath = mc_config_get_full_vpath (EDIT_HOME_CLIP_FILE);
    file = mc_open (fname_vpath, clip_open_flags, clip_open_mode);
    vfs_path_free (fname_vpath, TRUE);

    if (file == -1)
        return TRUE;

    str_len = strlen (text);
    {
        ssize_t ret;

        ret = mc_write (file, text, str_len);
        (void) ret;
    }
    mc_close (file);
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */

/* event callback */
gboolean
clipboard_text_from_file (const gchar * event_group_name, const gchar * event_name,
                          gpointer init_data, gpointer data)
{
    char buf[BUF_LARGE];
    FILE *f;
    char *fname = NULL;
    gboolean first = TRUE;
    ev_clipboard_text_from_file_t *event_data = (ev_clipboard_text_from_file_t *) data;

    (void) event_group_name;
    (void) event_name;
    (void) init_data;

    fname = mc_config_get_full_path (EDIT_HOME_CLIP_FILE);
    f = fopen (fname, "r");
    g_free (fname);

    if (f == NULL)
    {
        event_data->ret = FALSE;
        return TRUE;
    }

    *(event_data->text) = NULL;

    while (fgets (buf, sizeof (buf), f))
    {
        size_t len;

        len = strlen (buf);
        if (len > 0)
        {
            if (buf[len - 1] == '\n')
                buf[len - 1] = '\0';

            if (first)
            {
                first = FALSE;
                *(event_data->text) = g_strdup (buf);
            }
            else
            {
                /* remove \n on EOL */
                char *tmp;

                tmp = g_strconcat (*(event_data->text), " ", buf, (char *) NULL);
                g_free (*(event_data->text));
                *(event_data->text) = tmp;
            }
        }
    }

    fclose (f);
    event_data->ret = (*(event_data->text) != NULL);
    return TRUE;
}

/* --------------------------------------------------------------------------------------------- */