util: clarify a bit of the code for parsing commands in wmgenmenu
[wmaker-crm.git] / util / wxcopy.c
blobd7e01b6caf71e46b52bb614c3b78d2e587b4b905
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];
125 snprintf(line, sizeof(line),
126 "%s: could not open \"%s\"",
127 __progname, filename);
128 perror(line);
129 exit(1);
133 dpy = XOpenDisplay(display_name);
134 XSetErrorHandler(errorHandler);
135 if (!dpy) {
136 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
137 exit(1);
140 if (buffer < 0) {
141 Atom *rootWinProps;
142 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
143 int i, count;
145 /* Create missing CUT_BUFFERs */
146 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
147 for (i = 0; i < count; i++) {
148 switch (rootWinProps[i]) {
149 case XA_CUT_BUFFER0:
150 exists[0] = 1;
151 break;
152 case XA_CUT_BUFFER1:
153 exists[1] = 1;
154 break;
155 case XA_CUT_BUFFER2:
156 exists[2] = 1;
157 break;
158 case XA_CUT_BUFFER3:
159 exists[3] = 1;
160 break;
161 case XA_CUT_BUFFER4:
162 exists[4] = 1;
163 break;
164 case XA_CUT_BUFFER5:
165 exists[5] = 1;
166 break;
167 case XA_CUT_BUFFER6:
168 exists[6] = 1;
169 break;
170 case XA_CUT_BUFFER7:
171 exists[7] = 1;
172 break;
173 default:
174 break;
177 if (rootWinProps) {
178 XFree(rootWinProps);
180 for (i = 0; i < 8; i++) {
181 if (!exists[i]) {
182 XStoreBuffer(dpy, "", 0, i);
186 XRotateBuffers(dpy, 1);
187 buffer = 0;
190 while (!feof(file)) {
191 char *nbuf;
192 char tmp[LINESIZE + 2];
193 int nl = 0;
196 * Use read() instead of fgets() to preserve NULLs, since
197 * especially since there's no reason to read one line at a time.
199 if ((nl = fread(tmp, 1, LINESIZE, file)) <= 0) {
200 break;
202 if (buf_len == 0) {
203 nbuf = malloc(buf_len = l + nl + 1);
204 } else if (buf_len < l + nl + 1) {
206 * To avoid terrible performance on big input buffers,
207 * grow by doubling, not by the minimum needed for the
208 * current line.
210 buf_len = 2 * buf_len + nl + 1;
211 /* some realloc implementations don't do malloc if buf==NULL */
212 if (buf == NULL) {
213 nbuf = malloc(buf_len);
214 } else {
215 nbuf = realloc(buf, buf_len);
217 } else {
218 nbuf = buf;
220 if (!nbuf) {
221 fprintf(stderr, "%s: out of memory\n", __progname);
222 exit(1);
224 buf = nbuf;
226 * Don't strcat, since it would make the algorithm n-squared.
227 * Don't use strcpy, since it stops on a NUL.
229 memcpy(buf + l, tmp, nl);
230 l += nl;
231 if (limit_check && l >= MAXDATA) {
232 fprintf
233 (stderr,
234 "%s: too much data in input - more than %d bytes\n"
235 " use the -nolimit argument to remove the limit check.\n", __progname, MAXDATA);
236 exit(1);
240 if (clear_selection) {
241 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
243 if (buf) {
244 XStoreBuffer(dpy, buf, l, buffer);
246 XFlush(dpy);
247 XCloseDisplay(dpy);
248 exit(buf == NULL || errno != 0);