util: Added a few missing 'static' attributes for functions
[wmaker-crm.git] / util / wxcopy.c
blob89bd4a138394a084feb57a6e955226f3ed74b17a
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 /* ignore all errors */
52 return 0;
55 int main(int argc, char **argv)
57 Display *dpy;
58 int i;
59 int buffer = -1;
60 char *filename = NULL;
61 FILE *file = stdin;
62 char *buf = NULL;
63 char *display_name = "";
64 int l = 0;
65 int buf_len = 0;
66 int limit_check = 1;
67 int clear_selection = 0;
69 for (i = 1; i < argc; i++) {
70 if (argv[i][0] == '-') {
71 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
72 print_help();
73 exit(0);
74 } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
75 printf("%s (Window Maker %s)\n", __progname, VERSION);
76 exit(0);
77 } else if (strcmp(argv[i], "-cutbuffer") == 0 || strcmp(argv[i], "--cutbuffer") == 0) {
78 if (i < argc - 1) {
79 i++;
80 if (sscanf(argv[i], "%i", &buffer) != 1) {
81 fprintf(stderr, "%s: could not convert '%s' to int\n",
82 __progname, argv[i]);
83 exit(1);
85 if (buffer < 0 || buffer > 7) {
86 fprintf(stderr, "%s: invalid buffer number %i\n", __progname, buffer);
87 exit(1);
89 } else {
90 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
91 printf("Try '%s --help' for more information\n", __progname);
92 exit(1);
94 } else if (strcmp(argv[i], "-display") == 0) {
95 if (i < argc - 1) {
96 display_name = argv[++i];
97 } else {
98 printf("%s: missing argument for '%s'\n", __progname, argv[i]);
99 printf("Try '%s --help' for more information\n", __progname);
100 exit(1);
102 } else if (strcmp(argv[i], "-clearselection") == 0
103 || strcmp(argv[i], "--clear-selection") == 0) {
104 clear_selection = 1;
105 } else if (strcmp(argv[i], "-nolimit") == 0 || strcmp(argv[i], "--no-limit") == 0) {
106 limit_check = 0;
107 } else {
108 printf("%s: invalid argument '%s'\n", __progname, argv[i]);
109 printf("Try '%s --help' for more information\n", __progname);
110 exit(1);
112 } else {
113 filename = argv[i];
116 if (filename) {
117 file = fopen(filename, "rb");
118 if (!file) {
119 char line[1024];
120 sprintf(line, "%s: could not open \"%s\"", __progname, filename);
121 perror(line);
122 exit(1);
126 dpy = XOpenDisplay(display_name);
127 XSetErrorHandler(errorHandler);
128 if (!dpy) {
129 fprintf(stderr, "%s: could not open display \"%s\"\n", __progname, XDisplayName(display_name));
130 exit(1);
133 if (buffer < 0) {
134 Atom *rootWinProps;
135 int exists[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
136 int i, count;
138 /* Create missing CUT_BUFFERs */
139 rootWinProps = XListProperties(dpy, DefaultRootWindow(dpy), &count);
140 for (i = 0; i < count; i++) {
141 switch (rootWinProps[i]) {
142 case XA_CUT_BUFFER0:
143 exists[0] = 1;
144 break;
145 case XA_CUT_BUFFER1:
146 exists[1] = 1;
147 break;
148 case XA_CUT_BUFFER2:
149 exists[2] = 1;
150 break;
151 case XA_CUT_BUFFER3:
152 exists[3] = 1;
153 break;
154 case XA_CUT_BUFFER4:
155 exists[4] = 1;
156 break;
157 case XA_CUT_BUFFER5:
158 exists[5] = 1;
159 break;
160 case XA_CUT_BUFFER6:
161 exists[6] = 1;
162 break;
163 case XA_CUT_BUFFER7:
164 exists[7] = 1;
165 break;
166 default:
167 break;
170 if (rootWinProps) {
171 XFree(rootWinProps);
173 for (i = 0; i < 8; i++) {
174 if (!exists[i]) {
175 XStoreBuffer(dpy, "", 0, i);
179 XRotateBuffers(dpy, 1);
180 buffer = 0;
183 while (!feof(file)) {
184 char *nbuf;
185 char tmp[LINESIZE + 2];
186 int nl = 0;
189 * Use read() instead of fgets() to preserve NULLs, since
190 * especially since there's no reason to read one line at a time.
192 if ((nl = fread(tmp, 1, LINESIZE, file)) <= 0) {
193 break;
195 if (buf_len == 0) {
196 nbuf = malloc(buf_len = l + nl + 1);
197 } else if (buf_len < l + nl + 1) {
199 * To avoid terrible performance on big input buffers,
200 * grow by doubling, not by the minimum needed for the
201 * current line.
203 buf_len = 2 * buf_len + nl + 1;
204 /* some realloc implementations don't do malloc if buf==NULL */
205 if (buf == NULL) {
206 nbuf = malloc(buf_len);
207 } else {
208 nbuf = realloc(buf, buf_len);
210 } else {
211 nbuf = buf;
213 if (!nbuf) {
214 fprintf(stderr, "%s: out of memory\n", __progname);
215 exit(1);
217 buf = nbuf;
219 * Don't strcat, since it would make the algorithm n-squared.
220 * Don't use strcpy, since it stops on a NUL.
222 memcpy(buf + l, tmp, nl);
223 l += nl;
224 if (limit_check && l >= MAXDATA) {
225 fprintf
226 (stderr,
227 "%s: too much data in input - more than %d bytes\n"
228 " use the -nolimit argument to remove the limit check.\n", __progname, MAXDATA);
229 exit(1);
233 if (clear_selection) {
234 XSetSelectionOwner(dpy, XA_PRIMARY, None, CurrentTime);
236 if (buf) {
237 XStoreBuffer(dpy, buf, l, buffer);
239 XFlush(dpy);
240 XCloseDisplay(dpy);
241 exit(buf == NULL || errno != 0);