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 static const char *prog_name
;
36 static void print_help(void)
38 printf("Usage: %s [OPTIONS] [FILE]\n", prog_name
);
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 /* Parameter not used, but tell the compiler that it is ok */
55 /* ignore all errors */
59 int main(int argc
, char **argv
)
64 char *filename
= NULL
;
67 char *display_name
= "";
71 int clear_selection
= 0;
74 for (i
= 1; i
< argc
; i
++) {
75 if (argv
[i
][0] == '-') {
76 if (strcmp(argv
[i
], "-h") == 0 || strcmp(argv
[i
], "--help") == 0) {
79 } else if (strcmp(argv
[i
], "-v") == 0 || strcmp(argv
[i
], "--version") == 0) {
80 printf("%s (Window Maker %s)\n", prog_name
, VERSION
);
82 } else if (strcmp(argv
[i
], "-cutbuffer") == 0 || strcmp(argv
[i
], "--cutbuffer") == 0) {
85 if (sscanf(argv
[i
], "%i", &buffer
) != 1) {
86 fprintf(stderr
, "%s: could not convert '%s' to int\n",
90 if (buffer
< 0 || buffer
> 7) {
91 fprintf(stderr
, "%s: invalid buffer number %i\n", prog_name
, buffer
);
95 printf("%s: missing argument for '%s'\n", prog_name
, argv
[i
]);
96 printf("Try '%s --help' for more information\n", prog_name
);
99 } else if (strcmp(argv
[i
], "-display") == 0) {
101 display_name
= argv
[++i
];
103 printf("%s: missing argument for '%s'\n", prog_name
, argv
[i
]);
104 printf("Try '%s --help' for more information\n", prog_name
);
107 } else if (strcmp(argv
[i
], "-clearselection") == 0
108 || strcmp(argv
[i
], "--clear-selection") == 0) {
110 } else if (strcmp(argv
[i
], "-nolimit") == 0 || strcmp(argv
[i
], "--no-limit") == 0) {
113 printf("%s: invalid argument '%s'\n", prog_name
, argv
[i
]);
114 printf("Try '%s --help' for more information\n", prog_name
);
122 file
= fopen(filename
, "rb");
126 snprintf(line
, sizeof(line
),
127 "%s: could not open \"%s\"",
128 prog_name
, filename
);
134 dpy
= XOpenDisplay(display_name
);
135 XSetErrorHandler(errorHandler
);
137 fprintf(stderr
, "%s: could not open display \"%s\"\n", prog_name
, XDisplayName(display_name
));
143 int exists
[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
146 /* Create missing CUT_BUFFERs */
147 rootWinProps
= XListProperties(dpy
, DefaultRootWindow(dpy
), &count
);
148 for (i
= 0; i
< count
; i
++) {
149 switch (rootWinProps
[i
]) {
181 for (i
= 0; i
< 8; i
++) {
183 XStoreBuffer(dpy
, "", 0, i
);
187 XRotateBuffers(dpy
, 1);
191 while (!feof(file
)) {
193 char tmp
[LINESIZE
+ 2];
197 * Use read() instead of fgets() to preserve NULLs, since
198 * especially since there's no reason to read one line at a time.
200 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
204 nbuf
= malloc(buf_len
= l
+ nl
+ 1);
205 } else if (buf_len
< l
+ nl
+ 1) {
207 * To avoid terrible performance on big input buffers,
208 * grow by doubling, not by the minimum needed for the
211 buf_len
= 2 * buf_len
+ nl
+ 1;
212 /* some realloc implementations don't do malloc if buf==NULL */
214 nbuf
= malloc(buf_len
);
216 nbuf
= realloc(buf
, buf_len
);
222 fprintf(stderr
, "%s: out of memory\n", prog_name
);
227 * Don't strcat, since it would make the algorithm n-squared.
228 * Don't use strcpy, since it stops on a NUL.
230 memcpy(buf
+ l
, tmp
, nl
);
232 if (limit_check
&& l
>= MAXDATA
) {
235 "%s: too much data in input - more than %d bytes\n"
236 " use the -nolimit argument to remove the limit check.\n", prog_name
, MAXDATA
);
241 if (clear_selection
) {
242 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
245 XStoreBuffer(dpy
, buf
, l
, buffer
);
249 exit(buf
== NULL
|| errno
!= 0);