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