Configure: Added proper check for Xmu library for WRaster
[wmaker-crm.git] / util / wxcopy.c
blob90303fb066e3b97288705920d159a4219c33b0ff
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,
18 * MA 02110-1301, USA.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xatom.h>
29 #include "../src/wconfig.h"
31 #define LINESIZE (4*1024)
32 #define MAXDATA (64*1024)
34 extern char *__progname;
36 static void print_help(void)
38 printf("Usage: %s [OPTIONS] [FILE]\n", __progname);
39 puts("Copies data from FILE or stdin into X cut buffer.");
40 puts("");
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 */
52 (void) dpy;
53 (void) err;
55 /* ignore all errors */
56 return 0;
59 int main(int argc, char **argv)
61 Display *dpy;
62 int i;
63 int buffer = -1;
64 char *filename = NULL;
65 FILE *file = stdin;
66 char *buf = NULL;
67 char *display_name = "";
68 int l = 0;
69 int buf_len = 0;
70 int limit_check = 1;
71 int clear_selection = 0;
73 for (i = 1; i < argc; i++) {
74 if (argv[i][0] == '-') {
75 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
76 print_help();
77 exit(0);
78 } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
79 printf("%s (Window Maker %s)\n", __progname, VERSION);
80 exit(0);
81 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
82 if (i < argc - 1) {
83 i++;
84 if (sscanf(argv[i], "%i", &buffer) != 1) {
85 fprintf(stderr, "%s: could not convert '%s' to int\n",
86 __progname, argv[i]);
87 exit(1);
89 if (buffer < 0 || buffer > 7) {
90 fprintf(stderr, "%s: invalid buffer number %i\n", __progname, buffer);
91 exit(1);
93 } else {
94 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
95 printf("Try '%s --help' for more information\n", __progname);
96 exit(1);
98 } else if (strcmp(argv[i], "-display") == 0) {
99 if (i < argc - 1) {
100 display_name = argv[++i];
101 } else {
102 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
103 printf("Try '%s --help' for more information\n", __progname);
104 exit(1);
106 } else if (strcmp(argv[i], "-clearselection") == 0
107 || strcmp(argv[i], "--clear-selection") == 0) {
108 clear_selection = 1;
109 } else if (strcmp(argv[i], "-nolimit") == 0 || strcmp(argv[i], "--no-limit") == 0) {
110 limit_check = 0;
111 } else {
112 printf("%s: invalid argument '%s'\n", __progname, argv[i]);
113 printf("Try '%s --help' for more information\n", __progname);
114 exit(1);
116 } else {
117 filename = argv[i];
120 if (filename) {
121 file = fopen(filename, "rb");
122 if (!file) {
123 char line[1024];
124 sprintf(line, "%s: could not open \"%s\"", __progname, filename);
125 perror(line);
126 exit(1);
130 dpy = XOpenDisplay(display_name);
131 XSetErrorHandler(errorHandler);
132 if (!dpy) {
133 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
134 exit(1);
137 if (buffer < 0) {
138 Atom *rootWinProps;
139 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
140 int i, count;
142 /* Create missing CUT_BUFFERs */
143 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
144 for (i = 0; i < count; i++) {
145 switch (rootWinProps[i]) {
146 case XA_CUT_BUFFER0:
147 exists[0] = 1;
148 break;
149 case XA_CUT_BUFFER1:
150 exists[1] = 1;
151 break;
152 case XA_CUT_BUFFER2:
153 exists[2] = 1;
154 break;
155 case XA_CUT_BUFFER3:
156 exists[3] = 1;
157 break;
158 case XA_CUT_BUFFER4:
159 exists[4] = 1;
160 break;
161 case XA_CUT_BUFFER5:
162 exists[5] = 1;
163 break;
164 case XA_CUT_BUFFER6:
165 exists[6] = 1;
166 break;
167 case XA_CUT_BUFFER7:
168 exists[7] = 1;
169 break;
170 default:
171 break;
174 if (rootWinProps) {
175 XFree(rootWinProps);
177 for (i = 0; i < 8; i++) {
178 if (!exists[i]) {
179 XStoreBuffer(dpy, "", 0, i);
183 XRotateBuffers(dpy, 1);
184 buffer = 0;
187 while (!feof(file)) {
188 char *nbuf;
189 char tmp[LINESIZE + 2];
190 int nl = 0;
193 * Use read() instead of fgets() to preserve NULLs, since
194 * especially since there's no reason to read one line at a time.
196 if ((nl = fread(tmp, 1, LINESIZE, file)) <= 0) {
197 break;
199 if (buf_len == 0) {
200 nbuf = malloc(buf_len = l + nl + 1);
201 } else if (buf_len < l + nl + 1) {
203 * To avoid terrible performance on big input buffers,
204 * grow by doubling, not by the minimum needed for the
205 * current line.
207 buf_len = 2 * buf_len + nl + 1;
208 /* some realloc implementations don't do malloc if buf==NULL */
209 if (buf == NULL) {
210 nbuf = malloc(buf_len);
211 } else {
212 nbuf = realloc(buf, buf_len);
214 } else {
215 nbuf = buf;
217 if (!nbuf) {
218 fprintf(stderr, "%s: out of memory\n", __progname);
219 exit(1);
221 buf = nbuf;
223 * Don't strcat, since it would make the algorithm n-squared.
224 * Don't use strcpy, since it stops on a NUL.
226 memcpy(buf + l, tmp, nl);
227 l += nl;
228 if (limit_check && l >= MAXDATA) {
229 fprintf
230 (stderr,
231 "%s: too much data in input - more than %d bytes\n"
232 " use the -nolimit argument to remove the limit check.\n", __progname, MAXDATA);
233 exit(1);
237 if (clear_selection) {
238 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
240 if (buf) {
241 XStoreBuffer(dpy, buf, l, buffer);
243 XFlush(dpy);
244 XCloseDisplay(dpy);
245 exit(buf == NULL || errno != 0);