1 /* wxcopy.c- copy stdin or file into cutbuffer
3 * Copyright (c) 1997-1999 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.
20 #define PROG_VERSION "wxcopy 0.3"
29 #include <X11/Xatom.h>
32 #define LINESIZE (4*1024)
33 #define MAXDATA (64*1024)
38 printf("Usage: %s [OPTIONS] [FILE]\n", progn
);
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(" --help display this help and exit");
46 puts(" --version output version information and exit");
50 errorHandler(Display
*dpy
, XErrorEvent
*err
)
52 /* ignore all errors */
58 main(int argc
, char **argv
)
66 char *display_name
="";
70 int clear_selection
= 0;
72 for (i
=1; i
<argc
; i
++) {
73 if (argv
[i
][0]=='-') {
74 if (strcmp(argv
[i
], "--help")==0) {
77 } else if (strcmp(argv
[i
], "--version")==0) {
80 } else if (strcmp(argv
[i
],"-cutbuffer")==0
81 || strcmp(argv
[i
],"--cutbuffer")==0) {
84 if (sscanf(argv
[i
],"%i", &buffer
)!=1) {
85 fprintf(stderr
, "%s: could not convert '%s' to int\n",
89 if (buffer
<0 || buffer
> 7) {
90 fprintf(stderr
, "%s: invalid buffer number %i\n",
95 printf("%s: missing argument for '%s'\n", argv
[0], argv
[i
]);
96 printf("Try '%s --help' for more information\n", argv
[0]);
99 } else if (strcmp(argv
[i
], "-display")==0) {
101 display_name
= argv
[++i
];
103 printf("%s: missing argument for '%s'\n", argv
[0], argv
[i
]);
104 printf("Try '%s --help' for more information\n", argv
[0]);
107 } else if (strcmp(argv
[i
],"-clearselection")==0
108 || strcmp(argv
[i
],"--clear-selection")==0) {
110 } else if (strcmp(argv
[i
],"-nolimit")==0
111 || strcmp(argv
[i
],"--no-limit")==0) {
114 printf("%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
115 printf("Try '%s --help' for more information\n", argv
[0]);
123 file
= fopen(filename
, "r");
126 sprintf(line
, "%s: could not open \"%s\"", argv
[0], filename
);
132 dpy
= XOpenDisplay(display_name
);
133 XSetErrorHandler(errorHandler
);
135 fprintf(stderr
, "%s: could not open display \"%s\"\n", argv
[0],
136 XDisplayName(display_name
));
142 int exists
[8] = {0, 0, 0, 0, 0, 0, 0, 0};
145 /* Create missing CUT_BUFFERs */
146 rootWinProps
= XListProperties(dpy
, DefaultRootWindow(dpy
), &count
);
147 for (i
=0; i
<count
; i
++) {
148 switch(rootWinProps
[i
]) {
150 exists
[0] = 1; break;
152 exists
[1] = 1; break;
154 exists
[2] = 1; break;
156 exists
[3] = 1; break;
158 exists
[4] = 1; break;
160 exists
[5] = 1; break;
162 exists
[6] = 1; break;
164 exists
[7] = 1; break;
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", argv
[0]);
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
) {
227 "%s: too much data in input - more than %d bytes\n"
228 " use the -nolimit argument to remove the limit check.\n",
235 if (clear_selection
) {
236 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
239 XStoreBuffer(dpy
, buf
, l
, buffer
);
243 exit(buf
== NULL
|| errno
!= 0);