wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / util / wxcopy.c
blob256c45b2a39860cb06bf085cb15565b87d860aa6
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 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.");
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 prog_name = argv[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) {
77 print_help();
78 exit(0);
79 } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
80 printf("%s (Window Maker %s)\n", prog_name, VERSION);
81 exit(0);
82 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
83 if (i < argc - 1) {
84 i++;
85 if (sscanf(argv[i], "%i", &buffer) != 1) {
86 fprintf(stderr, "%s: could not convert '%s' to int\n",
87 prog_name, argv[i]);
88 exit(1);
90 if (buffer < 0 || buffer > 7) {
91 fprintf(stderr, "%s: invalid buffer number %i\n", prog_name, buffer);
92 exit(1);
94 } else {
95 printf("%s: missing argument for '%s'\n", prog_name, argv[i]);
96 printf("Try '%s --help' for more information\n", prog_name);
97 exit(1);
99 } else if (strcmp(argv[i], "-display") == 0) {
100 if (i < argc - 1) {
101 display_name = argv[++i];
102 } else {
103 printf("%s: missing argument for '%s'\n", prog_name, argv[i]);
104 printf("Try '%s --help' for more information\n", prog_name);
105 exit(1);
107 } else if (strcmp(argv[i], "-clearselection") == 0
108 || strcmp(argv[i], "--clear-selection") == 0) {
109 clear_selection = 1;
110 } else if (strcmp(argv[i], "-nolimit") == 0 || strcmp(argv[i], "--no-limit") == 0) {
111 limit_check = 0;
112 } else {
113 printf("%s: invalid argument '%s'\n", prog_name, argv[i]);
114 printf("Try '%s --help' for more information\n", prog_name);
115 exit(1);
117 } else {
118 filename = argv[i];
121 if (filename) {
122 file = fopen(filename, "rb");
123 if (!file) {
124 char line[1024];
126 snprintf(line, sizeof(line),
127 "%s: could not open \"%s\"",
128 prog_name, filename);
129 perror(line);
130 exit(1);
134 dpy = XOpenDisplay(display_name);
135 XSetErrorHandler(errorHandler);
136 if (!dpy) {
137 fprintf(stderr, "%s: could not open display \"%s\"\n", prog_name, XDisplayName(display_name));
138 exit(1);
141 if (buffer < 0) {
142 Atom *rootWinProps;
143 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
144 int i, count;
146 /* Create missing CUT_BUFFERs */
147 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
148 for (i = 0; i < count; i++) {
149 switch (rootWinProps[i]) {
150 case XA_CUT_BUFFER0:
151 exists[0] = 1;
152 break;
153 case XA_CUT_BUFFER1:
154 exists[1] = 1;
155 break;
156 case XA_CUT_BUFFER2:
157 exists[2] = 1;
158 break;
159 case XA_CUT_BUFFER3:
160 exists[3] = 1;
161 break;
162 case XA_CUT_BUFFER4:
163 exists[4] = 1;
164 break;
165 case XA_CUT_BUFFER5:
166 exists[5] = 1;
167 break;
168 case XA_CUT_BUFFER6:
169 exists[6] = 1;
170 break;
171 case XA_CUT_BUFFER7:
172 exists[7] = 1;
173 break;
174 default:
175 break;
178 if (rootWinProps) {
179 XFree(rootWinProps);
181 for (i = 0; i < 8; i++) {
182 if (!exists[i]) {
183 XStoreBuffer(dpy, "", 0, i);
187 XRotateBuffers(dpy, 1);
188 buffer = 0;
191 while (!feof(file)) {
192 char *nbuf;
193 char tmp[LINESIZE + 2];
194 int nl = 0;
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) {
201 break;
203 if (buf_len == 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
209 * current line.
211 buf_len = 2 * buf_len + nl + 1;
212 /* some realloc implementations don't do malloc if buf==NULL */
213 if (buf == NULL) {
214 nbuf = malloc(buf_len);
215 } else {
216 nbuf = realloc(buf, buf_len);
218 } else {
219 nbuf = buf;
221 if (!nbuf) {
222 fprintf(stderr, "%s: out of memory\n", prog_name);
223 exit(1);
225 buf = nbuf;
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);
231 l += nl;
232 if (limit_check && l >= MAXDATA) {
233 fprintf
234 (stderr,
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);
237 exit(1);
241 if (clear_selection) {
242 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
244 if (buf) {
245 XStoreBuffer(dpy, buf, l, buffer);
247 XFlush(dpy);
248 XCloseDisplay(dpy);
249 exit(buf == NULL || errno != 0);