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., 51 Franklin St, Fifth Floor, Boston,
27 #include <X11/Xatom.h>
29 #include "../src/wconfig.h"
31 #define LINESIZE (4*1024)
32 #define MAXDATA (64*1024)
34 extern char *__progname
;
38 printf("Usage: %s [OPTIONS] [FILE]\n", __progname
);
39 puts("Copies data from FILE or stdin into X cut buffer.");
41 puts(" -display <display> display to use");
42 puts(" --cutbuffer <number> cutbuffer number to put data");
43 puts(" --no-limit do not limit size of input data");
44 puts(" --clear-selection clears the current PRIMARY selection");
45 puts(" -h, --help display this help and exit");
46 puts(" -v, --version output version information and exit");
49 static int errorHandler(Display
* dpy
, XErrorEvent
* err
)
51 /* ignore all errors */
55 int main(int argc
, char **argv
)
60 char *filename
= NULL
;
63 char *display_name
= "";
67 int clear_selection
= 0;
69 for (i
= 1; i
< argc
; i
++) {
70 if (argv
[i
][0] == '-') {
71 if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
74 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
75 printf("%s (Window Maker %s)\n", __progname
, VERSION
);
77 } else if (strcmp(argv
[i
], "-cutbuffer") == 0 || strcmp(argv
[i
], "--cutbuffer") == 0) {
80 if (sscanf(argv
[i
], "%i", &buffer
) != 1) {
81 fprintf(stderr
, "%s: could not convert '%s' to int\n",
85 if (buffer
< 0 || buffer
> 7) {
86 fprintf(stderr
, "%s: invalid buffer number %i\n", __progname
, buffer
);
90 printf("%s: missing argument for '%s'\n", __progname
, argv
[i
]);
91 printf("Try '%s --help' for more information\n", __progname
);
94 } else if (strcmp(argv
[i
], "-display") == 0) {
96 display_name
= argv
[++i
];
98 printf("%s: missing argument for '%s'\n", __progname
, argv
[i
]);
99 printf("Try '%s --help' for more information\n", __progname
);
102 } else if (strcmp(argv
[i
], "-clearselection") == 0
103 || strcmp(argv
[i
], "--clear-selection") == 0) {
105 } else if (strcmp(argv
[i
], "-nolimit") == 0 || strcmp(argv
[i
], "--no-limit") == 0) {
108 printf("%s: invalid argument '%s'\n", __progname
, argv
[i
]);
109 printf("Try '%s --help' for more information\n", __progname
);
117 file
= fopen(filename
, "rb");
120 sprintf(line
, "%s: could not open \"%s\"", __progname
, filename
);
126 dpy
= XOpenDisplay(display_name
);
127 XSetErrorHandler(errorHandler
);
129 fprintf(stderr
, "%s: could not open display \"%s\"\n", __progname
, XDisplayName(display_name
));
135 int exists
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
138 /* Create missing CUT_BUFFERs */
139 rootWinProps
= XListProperties(dpy
, DefaultRootWindow(dpy
), &count
);
140 for (i
= 0; i
< count
; i
++) {
141 switch (rootWinProps
[i
]) {
173 for (i
= 0; i
< 8; i
++) {
175 XStoreBuffer(dpy
, "", 0, i
);
179 XRotateBuffers(dpy
, 1);
183 while (!feof(file
)) {
185 char tmp
[LINESIZE
+ 2];
189 * Use read() instead of fgets() to preserve NULLs, since
190 * especially since there's no reason to read one line at a time.
192 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
196 nbuf
= malloc(buf_len
= l
+ nl
+ 1);
197 } else if (buf_len
< l
+ nl
+ 1) {
199 * To avoid terrible performance on big input buffers,
200 * grow by doubling, not by the minimum needed for the
203 buf_len
= 2 * buf_len
+ nl
+ 1;
204 /* some realloc implementations don't do malloc if buf==NULL */
206 nbuf
= malloc(buf_len
);
208 nbuf
= realloc(buf
, buf_len
);
214 fprintf(stderr
, "%s: out of memory\n", __progname
);
219 * Don't strcat, since it would make the algorithm n-squared.
220 * Don't use strcpy, since it stops on a NUL.
222 memcpy(buf
+ l
, tmp
, nl
);
224 if (limit_check
&& l
>= MAXDATA
) {
227 "%s: too much data in input - more than %d bytes\n"
228 " use the -nolimit argument to remove the limit check.\n", __progname
, MAXDATA
);
233 if (clear_selection
) {
234 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
237 XStoreBuffer(dpy
, buf
, l
, buffer
);
241 exit(buf
== NULL
|| errno
!= 0);