Menu parser: added boundary checks in the path-gen for #include file search
[wmaker-crm.git] / util / wxcopy.c
blobd716e812f58c911ea000701da0285abad5d10028
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 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <X11/Xlib.h>
26 #include <X11/Xatom.h>
28 #include "../src/wconfig.h"
30 #define LINESIZE (4*1024)
31 #define MAXDATA (64*1024)
33 extern char *__progname;
35 void print_help()
37 printf("Usage: %s [OPTIONS] [FILE]\n", __progname);
38 puts("Copies data from FILE or stdin into X cut buffer.");
39 puts("");
40 puts(" -display <display> display to use");
41 puts(" --cutbuffer <number> cutbuffer number to put data");
42 puts(" --no-limit do not limit size of input data");
43 puts(" --clear-selection clears the current PRIMARY selection");
44 puts(" -h, --help display this help and exit");
45 puts(" -v, --version output version information and exit");
48 static int errorHandler(Display * dpy, XErrorEvent * err)
50 /* ignore all errors */
51 return 0;
54 int main(int argc, char **argv)
56 Display *dpy;
57 int i;
58 int buffer = -1;
59 char *filename = NULL;
60 FILE *file = stdin;
61 char *buf = NULL;
62 char *display_name = "";
63 int l = 0;
64 int buf_len = 0;
65 int limit_check = 1;
66 int clear_selection = 0;
68 for (i = 1; i < argc; i++) {
69 if (argv[i][0] == '-') {
70 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
71 print_help();
72 exit(0);
73 } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
74 printf("%s (Window Maker %s)\n", __progname, VERSION);
75 exit(0);
76 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
77 if (i < argc - 1) {
78 i++;
79 if (sscanf(argv[i], "%i", &buffer) != 1) {
80 fprintf(stderr, "%s: could not convert '%s' to int\n",
81 __progname, argv[i]);
82 exit(1);
84 if (buffer < 0 || buffer > 7) {
85 fprintf(stderr, "%s: invalid buffer number %i\n", __progname, buffer);
86 exit(1);
88 } else {
89 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
90 printf("Try '%s --help' for more information\n", __progname);
91 exit(1);
93 } else if (strcmp(argv[i], "-display") == 0) {
94 if (i < argc - 1) {
95 display_name = argv[++i];
96 } else {
97 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
98 printf("Try '%s --help' for more information\n", __progname);
99 exit(1);
101 } else if (strcmp(argv[i], "-clearselection") == 0
102 || strcmp(argv[i], "--clear-selection") == 0) {
103 clear_selection = 1;
104 } else if (strcmp(argv[i], "-nolimit") == 0 || strcmp(argv[i], "--no-limit") == 0) {
105 limit_check = 0;
106 } else {
107 printf("%s: invalid argument '%s'\n", __progname, argv[i]);
108 printf("Try '%s --help' for more information\n", __progname);
109 exit(1);
111 } else {
112 filename = argv[i];
115 if (filename) {
116 file = fopen(filename, "rb");
117 if (!file) {
118 char line[1024];
119 sprintf(line, "%s: could not open \"%s\"", __progname, filename);
120 perror(line);
121 exit(1);
125 dpy = XOpenDisplay(display_name);
126 XSetErrorHandler(errorHandler);
127 if (!dpy) {
128 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
129 exit(1);
132 if (buffer < 0) {
133 Atom *rootWinProps;
134 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
135 int i, count;
137 /* Create missing CUT_BUFFERs */
138 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
139 for (i = 0; i < count; i++) {
140 switch (rootWinProps[i]) {
141 case XA_CUT_BUFFER0:
142 exists[0] = 1;
143 break;
144 case XA_CUT_BUFFER1:
145 exists[1] = 1;
146 break;
147 case XA_CUT_BUFFER2:
148 exists[2] = 1;
149 break;
150 case XA_CUT_BUFFER3:
151 exists[3] = 1;
152 break;
153 case XA_CUT_BUFFER4:
154 exists[4] = 1;
155 break;
156 case XA_CUT_BUFFER5:
157 exists[5] = 1;
158 break;
159 case XA_CUT_BUFFER6:
160 exists[6] = 1;
161 break;
162 case XA_CUT_BUFFER7:
163 exists[7] = 1;
164 break;
165 default:
166 break;
169 if (rootWinProps) {
170 XFree(rootWinProps);
172 for (i = 0; i < 8; i++) {
173 if (!exists[i]) {
174 XStoreBuffer(dpy, "", 0, i);
178 XRotateBuffers(dpy, 1);
179 buffer = 0;
182 while (!feof(file)) {
183 char *nbuf;
184 char tmp[LINESIZE + 2];
185 int nl = 0;
188 * Use read() instead of fgets() to preserve NULLs, since
189 * especially since there's no reason to read one line at a time.
191 if ((nl = fread(tmp, 1, LINESIZE, file)) <= 0) {
192 break;
194 if (buf_len == 0) {
195 nbuf = malloc(buf_len = l + nl + 1);
196 } else if (buf_len < l + nl + 1) {
198 * To avoid terrible performance on big input buffers,
199 * grow by doubling, not by the minimum needed for the
200 * current line.
202 buf_len = 2 * buf_len + nl + 1;
203 /* some realloc implementations don't do malloc if buf==NULL */
204 if (buf == NULL) {
205 nbuf = malloc(buf_len);
206 } else {
207 nbuf = realloc(buf, buf_len);
209 } else {
210 nbuf = buf;
212 if (!nbuf) {
213 fprintf(stderr, "%s: out of memory\n", __progname);
214 exit(1);
216 buf = nbuf;
218 * Don't strcat, since it would make the algorithm n-squared.
219 * Don't use strcpy, since it stops on a NUL.
221 memcpy(buf + l, tmp, nl);
222 l += nl;
223 if (limit_check && l >= MAXDATA) {
224 fprintf
225 (stderr,
226 "%s: too much data in input - more than %d bytes\n"
227 " use the -nolimit argument to remove the limit check.\n", __progname, MAXDATA);
228 exit(1);
232 if (clear_selection) {
233 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
235 if (buf) {
236 XStoreBuffer(dpy, buf, l, buffer);
238 XFlush(dpy);
239 XCloseDisplay(dpy);
240 exit(buf == NULL || errno != 0);