1 /* wxcopy.c- copy stdin or file into cutbuffer
3 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <X11/Xatom.h>
28 #include "../src/wconfig.h"
30 #define LINESIZE (4*1024)
31 #define MAXDATA (64*1024)
33 extern char *__progname
;
37 printf("Usage: %s [OPTIONS] [FILE]\n", __progname
);
38 puts("Copies data from FILE or stdin into X cut buffer.");
40 puts(" -display <display> display to use");
41 puts(" --cutbuffer <number> cutbuffer number to put data");
42 puts(" --no-limit do not limit size of input data");
43 puts(" --clear-selection clears the current PRIMARY selection");
44 puts(" -h, --help display this help and exit");
45 puts(" -v, --version output version information and exit");
48 static int errorHandler(Display
* dpy
, XErrorEvent
* err
)
50 /* ignore all errors */
54 int main(int argc
, char **argv
)
59 char *filename
= NULL
;
62 char *display_name
= "";
66 int clear_selection
= 0;
68 for (i
= 1; i
< argc
; i
++) {
69 if (argv
[i
][0] == '-') {
70 if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
73 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
74 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
76 } else if (strcmp(argv
[i
], "-cutbuffer") == 0 || strcmp(argv
[i
], "--cutbuffer") == 0) {
79 if (sscanf(argv
[i
], "%i", &buffer
) != 1) {
80 fprintf(stderr
, "%s: could not convert '%s' to int\n",
84 if (buffer
< 0 || buffer
> 7) {
85 fprintf(stderr
, "%s: invalid buffer number %i\n", __progname
, buffer
);
89 printf("%s: missing argument for '%s'\n", __progname
, argv
[i
]);
90 printf("Try '%s --help' for more information\n", __progname
);
93 } else if (strcmp(argv
[i
], "-display") == 0) {
95 display_name
= argv
[++i
];
97 printf("%s: missing argument for '%s'\n", __progname
, argv
[i
]);
98 printf("Try '%s --help' for more information\n", __progname
);
101 } else if (strcmp(argv
[i
], "-clearselection") == 0
102 || strcmp(argv
[i
], "--clear-selection") == 0) {
104 } else if (strcmp(argv
[i
], "-nolimit") == 0 || strcmp(argv
[i
], "--no-limit") == 0) {
107 printf("%s: invalid argument '%s'\n", __progname
, argv
[i
]);
108 printf("Try '%s --help' for more information\n", __progname
);
116 file
= fopen(filename
, "rb");
119 sprintf(line
, "%s: could not open \"%s\"", __progname
, filename
);
125 dpy
= XOpenDisplay(display_name
);
126 XSetErrorHandler(errorHandler
);
128 fprintf(stderr
, "%s: could not open display \"%s\"\n", __progname
, XDisplayName(display_name
));
134 int exists
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
137 /* Create missing CUT_BUFFERs */
138 rootWinProps
= XListProperties(dpy
, DefaultRootWindow(dpy
), &count
);
139 for (i
= 0; i
< count
; i
++) {
140 switch (rootWinProps
[i
]) {
172 for (i
= 0; i
< 8; i
++) {
174 XStoreBuffer(dpy
, "", 0, i
);
178 XRotateBuffers(dpy
, 1);
182 while (!feof(file
)) {
184 char tmp
[LINESIZE
+ 2];
188 * Use read() instead of fgets() to preserve NULLs, since
189 * especially since there's no reason to read one line at a time.
191 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
195 nbuf
= malloc(buf_len
= l
+ nl
+ 1);
196 } else if (buf_len
< l
+ nl
+ 1) {
198 * To avoid terrible performance on big input buffers,
199 * grow by doubling, not by the minimum needed for the
202 buf_len
= 2 * buf_len
+ nl
+ 1;
203 /* some realloc implementations don't do malloc if buf==NULL */
205 nbuf
= malloc(buf_len
);
207 nbuf
= realloc(buf
, buf_len
);
213 fprintf(stderr
, "%s: out of memory\n", __progname
);
218 * Don't strcat, since it would make the algorithm n-squared.
219 * Don't use strcpy, since it stops on a NUL.
221 memcpy(buf
+ l
, tmp
, nl
);
223 if (limit_check
&& l
>= MAXDATA
) {
226 "%s: too much data in input - more than %d bytes\n"
227 " use the -nolimit argument to remove the limit check.\n", __progname
, MAXDATA
);
232 if (clear_selection
) {
233 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
236 XStoreBuffer(dpy
, buf
, l
, buffer
);
240 exit(buf
== NULL
|| errno
!= 0);