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.
20 #define PROG_VERSION "wxcopy 0.3"
28 #include <X11/Xatom.h>
30 #define LINESIZE (4*1024)
31 #define MAXDATA (64*1024)
33 void help(char *progn)
35 printf("Usage: %s [OPTIONS] [FILE]\n", progn);
36 puts("Copies data from FILE or stdin into X cut buffer.");
38 puts(" -display <display> display to use");
39 puts(" --cutbuffer <number> cutbuffer number to put data");
40 puts(" --no-limit do not limit size of input data");
41 puts(" --clear-selection clears the current PRIMARY selection");
42 puts(" --help display this help and exit");
43 puts(" --version output version information and exit");
46 static int errorHandler(Display * dpy, XErrorEvent * err)
48 /* ignore all errors */
52 int main(int argc, char **argv)
57 char *filename = NULL;
60 char *display_name = "";
64 int clear_selection = 0;
66 for (i = 1; i < argc; i++) {
67 if (argv[i][0] == '-') {
68 if (strcmp(argv[i], "--help") == 0) {
71 } else if (strcmp(argv[i], "--version") == 0) {
74 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
77 if (sscanf(argv[i], "%i", &buffer) != 1) {
78 fprintf(stderr, "%s: could not convert '%s' to int\n",
82 if (buffer < 0 || buffer > 7) {
83 fprintf(stderr, "%s: invalid buffer number %i\n", argv[0], buffer);
87 printf("%s: missing argument for '%s'\n", argv[0], argv[i]);
88 printf("Try '%s --help' for more information\n", argv[0]);
91 } else if (strcmp(argv[i], "-display") == 0) {
93 display_name = argv[++i];
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], "-clearselection") == 0
100 || strcmp(argv[i], "--clear-selection") == 0) {
102 } else if (strcmp(argv[i], "-nolimit") == 0 || strcmp(argv[i], "--no-limit") == 0) {
105 printf("%s: invalid argument '%s'\n", argv[0], argv[i]);
106 printf("Try '%s --help' for more information\n", argv[0]);
114 file = fopen(filename, "rb");
117 sprintf(line, "%s: could not open \"%s\"", argv[0], filename);
123 dpy = XOpenDisplay(display_name);
124 XSetErrorHandler(errorHandler);
126 fprintf(stderr, "%s: could not open display \"%s\"\n", argv[0], XDisplayName(display_name));
132 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
135 /* Create missing CUT_BUFFERs */
136 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
137 for (i = 0; i < count; i++) {
138 switch (rootWinProps[i]) {
170 for (i = 0; i < 8; i++) {
172 XStoreBuffer(dpy, "", 0, i);
176 XRotateBuffers(dpy, 1);
180 while (!feof(file)) {
182 char tmp[LINESIZE + 2];
186 * Use read() instead of fgets() to preserve NULLs, since
187 * especially since there's no reason to read one line at a time.
189 if ((nl = fread(tmp, 1, LINESIZE, file)) <= 0) {
193 nbuf = malloc(buf_len = l + nl + 1);
194 } else if (buf_len < l + nl + 1) {
196 * To avoid terrible performance on big input buffers,
197 * grow by doubling, not by the minimum needed for the
200 buf_len = 2 * buf_len + nl + 1;
201 /* some realloc implementations don't do malloc if buf==NULL */
203 nbuf = malloc(buf_len);
205 nbuf = realloc(buf, buf_len);
211 fprintf(stderr, "%s: out of memory\n", argv[0]);
216 * Don't strcat, since it would make the algorithm n-squared.
217 * Don't use strcpy, since it stops on a NUL.
219 memcpy(buf + l, tmp, nl);
221 if (limit_check && l >= MAXDATA) {
224 "%s: too much data in input - more than %d bytes\n"
225 " use the -nolimit argument to remove the limit check.\n", argv[0], MAXDATA);
230 if (clear_selection) {
231 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
234 XStoreBuffer(dpy, buf, l, buffer);
238 exit(buf == NULL || errno != 0);