-
Notifications
You must be signed in to change notification settings - Fork 73
/
xclip.c
981 lines (851 loc) · 26.5 KB
/
xclip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
/*
*
*
* xclip.c - command line interface to X server selections
* Copyright (C) 2001 Kim Saunders
* Copyright (C) 2007-2008 Peter Åstrand
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#ifdef HAVE_ICONV
#include <errno.h>
#include <iconv.h>
#endif
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xmu/Atoms.h>
#include "xcdef.h"
#include "xcprint.h"
#include "xclib.h"
/* command line option table for XrmParseCommand() */
XrmOptionDescRec opt_tab[17];
int opt_tab_size;
/* Options that get set on the command line */
int sloop = 0; /* number of loops */
char *sdisp = NULL; /* X display to connect to */
Atom sseln = XA_PRIMARY; /* X selection to work with */
Atom target = XA_STRING;
int wait = 0; /* wait: stop xclip after wait msec
after last 'paste event', start counting
after first 'paste event' */
/* Flags for command line options */
static int fdiri = T; /* direction is in */
static int ffilt = F; /* filter mode */
static int frmnl = F; /* remove (single) newline character at the very end if present */
static int fsecm = F; /* zero out selection buffer before exiting */
Display *dpy; /* connection to X11 display */
XrmDatabase opt_db = NULL; /* database for options */
char **fil_names; /* names of files to read */
int fil_number = 0; /* number of files to read */
int fil_current = 0;
FILE *fil_handle = NULL;
/* variables to hold Xrm database record and type */
XrmValue rec_val;
char *rec_typ;
int tempi = 0;
struct requestor
{
Window cwin;
Atom pty;
unsigned int context;
unsigned long sel_pos;
int finished;
long chunk_size;
struct requestor *next;
};
static struct requestor *requestors;
static struct requestor *get_requestor(Window win)
{
struct requestor *requestor;
if (requestors) {
for (requestor = requestors; requestor != NULL; requestor = requestor->next) {
if (requestor->cwin == win) {
if (xcverb >= OVERBOSE) {
fprintf(stderr,
" = Reusing requestor for %s\n",
xcnamestr(dpy, win) );
}
return requestor;
}
}
}
if (xcverb >= OVERBOSE) {
fprintf(stderr, " + Creating new requestor for %s\n",
xcnamestr(dpy, win) );
}
requestor = (struct requestor *)calloc(1, sizeof(struct requestor));
if (!requestor) {
errmalloc();
} else {
requestor->context = XCLIB_XCIN_NONE;
}
if (!requestors) {
requestors = requestor;
} else {
requestor->next = requestors;
requestors = requestor;
}
return requestor;
}
static void del_requestor(struct requestor *requestor)
{
struct requestor *reqitr;
if (!requestor) {
return;
}
if (xcverb >= OVERBOSE) {
fprintf(stderr,
" - Deleting requestor for %s\n",
xcnamestr(dpy, requestor->cwin) );
}
if (requestors == requestor) {
requestors = requestors->next;
} else {
for (reqitr = requestors; reqitr != NULL; reqitr = reqitr->next) {
if (reqitr->next == requestor) {
reqitr->next = reqitr->next->next;
break;
}
}
}
free(requestor);
}
int clean_requestors() {
/* Remove any requestors for which the X window has disappeared */
if (xcverb >= ODEBUG) {
fprintf(stderr, "xclip: debug: checking for requestors whose window has closed\n");
}
struct requestor *r = requestors;
Window win;
XWindowAttributes dummy;
while (r) {
win = r->cwin;
// check if window exists by seeing if XGetWindowAttributes works.
// note: this triggers X's BadWindow error and runs xchandler().
if ( !XGetWindowAttributes(dpy, win, &dummy) ) {
if (xcverb >= OVERBOSE) {
fprintf(stderr, " ! Found obsolete requestor 0x%lx\n", win);
}
del_requestor(r);
}
r = r -> next;
}
return 0;
}
/* Use XrmParseCommand to parse command line options to option variable */
static void
doOptMain(int argc, char *argv[])
{
/* Initialise resource manager and parse options into database */
XrmInitialize();
XrmParseCommand(&opt_db, opt_tab, opt_tab_size, PACKAGE_NAME, &argc,
argv);
/* set output level */
if (XrmGetResource(opt_db, "xclip.olevel", "Xclip.Olevel", &rec_typ, &rec_val)
) {
/* set verbose flag according to option */
switch (rec_val.addr[0]) {
case 'S':
xcverb = OSILENT; break;
case 'Q':
xcverb = OQUIET; break;
case 'V':
xcverb = OVERBOSE; break;
case 'D':
xcverb = ODEBUG; break;
}
}
if (xcverb == ODEBUG)
fprintf(stderr, "xclip: debug: Debugging enabled.\n");
/* set direction flag (in or out) */
if (XrmGetResource(opt_db, "xclip.direction", "Xclip.Direction", &rec_typ, &rec_val)
) {
if (strcmp(rec_val.addr, "I") == 0)
fdiri = T;
if (strcmp(rec_val.addr, "O") == 0)
fdiri = F;
}
/* set filter mode */
if (XrmGetResource(opt_db, "xclip.filter", "Xclip.Filter", &rec_typ, &rec_val)
) {
/* filter mode only allowed in silent mode */
if (xcverb == OSILENT)
ffilt = T;
}
/* set "remove last newline character if present" mode */
if (XrmGetResource(opt_db, "xclip.rmlastnl", "Xclip.RmLastNl", &rec_typ, &rec_val)
) {
frmnl = T;
}
/* check for -help and -version */
if (XrmGetResource(opt_db, "xclip.print", "Xclip.Print", &rec_typ, &rec_val)
) {
if (strcmp(rec_val.addr, "H") == 0)
prhelp(argv[0]);
if (strcmp(rec_val.addr, "V") == 0)
prversion();
}
/* check for -display */
if (XrmGetResource(opt_db, "xclip.display", "Xclip.Display", &rec_typ, &rec_val)
) {
sdisp = rec_val.addr;
if (xcverb >= OVERBOSE) /* print in verbose or debug mode only */
fprintf(stderr, "Display: %s\n", sdisp);
}
/* check for -loops */
if (XrmGetResource(opt_db, "xclip.loops", "Xclip.Loops", &rec_typ, &rec_val)
) {
sloop = atoi(rec_val.addr);
if (xcverb >= OVERBOSE)
fprintf(stderr, "Loops: %i\n", sloop);
}
/* check for -sensitive */
if (XrmGetResource(opt_db, "xclip.sensitive", "Xclip.Sensitive", &rec_typ, &rec_val)
) {
wait = 50;
fsecm = T;
if (xcverb >= OVERBOSE) {
fprintf(stderr, "Sensitive Mode Implies -wait 1\n");
fprintf(stderr, "Sensitive Buffers Will Be Zeroed At Exit\n");
}
}
if (XrmGetResource(opt_db, "xclip.wait", "Xclip.Wait", &rec_typ, &rec_val)
) {
wait = atoi(rec_val.addr);
if (xcverb >= OVERBOSE)
fprintf(stderr, "wait: %i msec\n", wait);
}
/* Read remaining options (filenames) */
while ((fil_number + 1) < argc) {
if (fil_number > 0) {
fil_names = xcrealloc(fil_names, (fil_number + 1) * sizeof(char *)
);
}
else {
fil_names = xcmalloc(sizeof(char *));
}
fil_names[fil_number] = argv[fil_number + 1];
fil_number++;
}
/* If filenames were given on the command line,
* default to reading input (unless -o was used).
*/
if (fil_number > 0) {
if (!XrmGetResource(opt_db, "xclip.direction", "Xclip.Direction", &rec_typ, &rec_val)) {
fdiri = T; /* Direction is input */
}
}
}
/* process selection command line option */
static void
doOptSel(void)
{
/* set selection to work with */
if (XrmGetResource(opt_db, "xclip.selection", "Xclip.Selection", &rec_typ, &rec_val)
) {
switch (tolower(rec_val.addr[0])) {
case 'p':
sseln = XA_PRIMARY;
break;
case 's':
sseln = XA_SECONDARY;
break;
case 'c':
sseln = XA_CLIPBOARD(dpy);
break;
case 'b':
sseln = XA_STRING;
break;
}
if (xcverb >= OVERBOSE) {
fprintf(stderr, "Using selection: ");
if (sseln == XA_PRIMARY)
fprintf(stderr, "XA_PRIMARY");
if (sseln == XA_SECONDARY)
fprintf(stderr, "XA_SECONDARY");
if (sseln == XA_CLIPBOARD(dpy))
fprintf(stderr, "XA_CLIPBOARD");
if (sseln == XA_STRING)
fprintf(stderr, "XA_STRING");
fprintf(stderr, "\n");
}
}
}
/* process noutf8 and target command line options */
static void
doOptTarget(void)
{
/* check for -noutf8 */
if (XrmGetResource(opt_db, "xclip.noutf8", "Xclip.noutf8", &rec_typ, &rec_val)
) {
if (xcverb >= OVERBOSE) /* print in verbose or debug mode only */
fprintf(stderr, "Using old UNICODE instead of UTF8.\n");
}
else if (XrmGetResource(opt_db, "xclip.target", "Xclip.Target", &rec_typ, &rec_val)
) {
target = XInternAtom(dpy, rec_val.addr, False);
if (xcverb >= OVERBOSE)
fprintf(stderr, "Using target: %s\n", rec_val.addr);
}
else {
target = XA_UTF8_STRING(dpy);
if (xcverb >= OVERBOSE)
fprintf(stderr, "Using target: UTF8_STRING.\n");
}
}
static int
doIn(Window win, const char *progname)
{
unsigned char *sel_buf = NULL; /* buffer for selection data */
unsigned long sel_len = 0; /* length of sel_buf */
unsigned long sel_all = 0; /* allocated size of sel_buf */
XEvent evt; /* X Event Structures */
int dloop = 0; /* done loops counter */
int x11_fd; /* fd on which XEvents appear */
fd_set in_fds;
struct timeval tv;
/* ConnectionNumber is a macro, it can't fail */
x11_fd = ConnectionNumber(dpy);
/* in mode */
sel_all = 16; /* Reasonable ballpark figure */
sel_buf = xcmalloc(sel_all * sizeof(char));
/* Put chars into inc from stdin or files until we hit EOF */
do {
if (fil_number == 0) {
/* read from stdin if no files specified */
fil_handle = stdin;
}
else {
if ((fil_handle = fopen(fil_names[fil_current], "r")) == NULL) {
errperror(3, progname, ": ", fil_names[fil_current]
);
return EXIT_FAILURE;
}
else {
/* file opened successfully.
*/
if (xcverb >= ODEBUG)
fprintf(stderr, "Reading %s...\n", fil_names[fil_current]);
}
}
fil_current++;
while (!feof(fil_handle)) {
/* If sel_buf is full (used elems =
* allocated elems)
*/
if (sel_len == sel_all) {
/* double the number of
* allocated elements
*/
sel_all *= 2;
sel_buf = (unsigned char *) xcrealloc(sel_buf, sel_all * sizeof(char) );
if (xcverb >= ODEBUG) {
fprintf(stderr, "xclip: debug: Increased buffersize to %ld\n", sel_all);
}
}
sel_len += fread(sel_buf + sel_len, sizeof(char), sel_all - sel_len, fil_handle);
}
if (fil_handle && (fil_handle != stdin)) {
fclose(fil_handle);
fil_handle = NULL;
}
} while (fil_current < fil_number);
/* if there are no files being read from (i.e., input
* is from stdin not files, and we are in filter mode,
* spit all the input back out to stdout
*/
if ((fil_number == 0) && ffilt) {
fwrite(sel_buf, sizeof(char), sel_len, stdout);
fclose(stdout);
}
if (fil_names) {
free(fil_names);
fil_names = NULL;
}
/* remove the last newline character if necessary */
if (frmnl && sel_len && sel_buf[sel_len - 1] == '\n') {
sel_len--;
}
/* Handle cut buffer if needed */
if (sseln == XA_STRING) {
XStoreBuffer(dpy, (char *) sel_buf, (int) sel_len, 0);
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
xcmemzero(sel_buf,sel_len);
return EXIT_SUCCESS;
}
/* take control of the selection so that we receive
* SelectionRequest events from other windows
*/
/* FIXME: Should not use CurrentTime, according to ICCCM section 2.1 */
XSetSelectionOwner(dpy, sseln, win, CurrentTime);
/* Double-check SetSelectionOwner did not "merely appear to succeed". */
Window owner = XGetSelectionOwner(dpy, sseln);
if (owner != win) {
fprintf(stderr, "xclip: error: Failed to take ownership of selection.\n");
return EXIT_FAILURE;
}
/* fork into the background, exit parent process if we
* are in silent mode
*/
if (xcverb == OSILENT) {
pid_t pid;
pid = fork();
/* exit the parent process; */
if (pid) {
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
xcmemzero(sel_buf,sel_len);
exit(EXIT_SUCCESS);
}
}
/* print a message saying what we're waiting for */
if (xcverb > OSILENT) {
if (sloop == 1)
fprintf(stderr, "Waiting for one selection request.\n");
if (sloop < 1)
fprintf(stderr,
"Waiting for selection requests, Control-C to quit\n");
if (sloop > 1)
fprintf(stderr,
"Waiting for %i selection request%s, Control-C to quit\n",
sloop, (sloop==1)?"":"s");
}
/* Avoid making the current directory in use, in case it will need to be umounted */
if (chdir("/") == -1) {
errperror(3, progname, ": ", "chdir to \"/\"");
return EXIT_FAILURE;
}
/* Jump into the middle of two while loops */
goto start;
/* loop and wait for the expected number of
* SelectionRequest events
*/
while (dloop < sloop || sloop < 1) {
if (xcverb >= ODEBUG)
fprintf(stderr, "\n========\n");
/* print messages about what we're waiting for
* if not in silent mode
*/
if (xcverb > OSILENT) {
if (sloop > 1)
fprintf(stderr, " Waiting for selection request %i of %i.\n", dloop + 1, sloop);
if (sloop == 1)
fprintf(stderr, " Waiting for a selection request.\n");
if (sloop < 1)
fprintf(stderr, " Waiting for selection request number %i\n", dloop + 1);
}
/* wait for a SelectionRequest (paste) event */
while (1) {
struct requestor *requestor;
Window requestor_id;
int finished;
if (!XPending(dpy) && wait > 0) {
tv.tv_sec = wait/1000;
tv.tv_usec = (wait%1000)*1000;
/* build fd_set */
FD_ZERO(&in_fds);
FD_SET(x11_fd, &in_fds);
if (!select(x11_fd + 1, &in_fds, 0, 0, &tv)) {
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
xcmemzero(sel_buf,sel_len);
return EXIT_SUCCESS;
}
}
start:
XNextEvent(dpy, &evt);
if (xcverb >= ODEBUG)
fprintf(stderr, "\n");
if (xcverb >= ODEBUG) {
fprintf(stderr, "xclip: debug: Received %s event\n",
evtstr[evt.type]);
}
switch (evt.type) {
case SelectionRequest:
requestor_id = evt.xselectionrequest.requestor;
requestor = get_requestor(requestor_id);
/* FIXME: ICCCM 2.2: check evt.time and refuse requests from
* outside the period of time we have owned the selection. */
break;
case PropertyNotify:
requestor_id = evt.xproperty.window;
requestor = get_requestor(requestor_id);
break;
case SelectionClear:
if (xcverb >= OVERBOSE) {
fprintf(stderr, "Lost selection ownership. ");
requestor_id = XGetSelectionOwner(dpy, sseln);
if (requestor_id == None)
fprintf(stderr, "(Some other client cleared the selection).\n");
else
fprintf(stderr, "(%s did a copy).\n", xcnamestr(dpy, requestor_id) );
}
/* If the client loses ownership(SelectionClear event)
* while it has a transfer in progress, it must continue to
* service the ongoing transfer until it is completed.
* See ICCCM section 2.2.
*/
/* Set dloop to force exit after all transfers finish. */
dloop = sloop;
/* remove requestors for dead windows */
clean_requestors();
/* if there are no more in-progress transfers, force exit */
if (!requestors) {
if (xcverb >= OVERBOSE) {
fprintf(stderr, "Exiting.\n");
}
return EXIT_SUCCESS;
}
else {
if (xcverb >= OVERBOSE) {
struct requestor *r = requestors;
int i=0;
fprintf(stderr, "Requestors: ");
while (r) {
fprintf(stderr, "0x%lx\t", r->cwin);
r = r->next;
i++;
}
fprintf(stderr, "\n");
fprintf(stderr,
"Still transferring data to %d requestor%s.\n",
i, (i==1)?"":"s");
}
}
continue; /* Wait for INCR PropertyNotify events */
default:
/* Ignore all other event types */
if (xcverb >= ODEBUG) {
fprintf(stderr,
"xclip: debug: Ignoring X event type %d (%s)\n",
evt.type, evtstr[evt.type]);
}
continue;
}
if (xcverb >= ODEBUG) {
fprintf(stderr, "xclip: debug: event was sent by %s\n",
xcnamestr(dpy, requestor_id) );
requestor_id=0;
}
finished = xcin(dpy, &(requestor->cwin), evt, &(requestor->pty),
target, sel_buf, sel_len, &(requestor->sel_pos),
&(requestor->context), &(requestor->chunk_size));
if (finished) {
del_requestor(requestor);
break;
}
if (requestor->cwin == 0) {
del_requestor(requestor);
break;
}
}
dloop++; /* increment loop counter */
}
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
xcmemzero(sel_buf,sel_len);
return EXIT_SUCCESS;
}
static void
printSelBuf(FILE * fout, Atom sel_type, unsigned char *sel_buf, size_t sel_len)
{
#ifdef HAVE_ICONV
Atom html = XInternAtom(dpy, "text/html", True);
#endif
if (xcverb >= OVERBOSE) { /* print in verbose mode only */
char *atom_name = XGetAtomName(dpy, sel_type);
fprintf(stderr, "Type is %s.\n", atom_name);
XFree(atom_name);
}
if (sel_type == XA_INTEGER) {
/* if the buffer contains integers, print them */
long *long_buf = (long *) sel_buf;
size_t long_len = sel_len / sizeof(long);
while (long_len--)
fprintf(fout, "%ld\n", *long_buf++);
return;
}
if (sel_type == XA_ATOM) {
/* if the buffer contains atoms, print their names */
Atom *atom_buf = (Atom *) sel_buf;
size_t atom_len = sel_len / sizeof(Atom);
while (atom_len--) {
char *atom_name = XGetAtomName(dpy, *atom_buf++);
fprintf(fout, "%s\n", atom_name);
XFree(atom_name);
}
return;
}
#ifdef HAVE_ICONV
if (html != None && sel_type == html) {
/* if the buffer contains UCS-2 (UTF-16), convert to
* UTF-8. Mozilla-based browsers do this for the
* text/html target.
*/
iconv_t cd;
char *sel_charset = NULL;
if (sel_buf[0] == 0xFF && sel_buf[1] == 0xFE)
sel_charset = "UTF-16LE";
else if (sel_buf[0] == 0xFE && sel_buf[1] == 0xFF)
sel_charset = "UTF-16BE";
if (sel_charset != NULL && (cd = iconv_open("UTF-8", sel_charset)) != (iconv_t) - 1) {
char *out_buf_start = malloc(sel_len), *in_buf = (char *) sel_buf + 2,
*out_buf = out_buf_start;
size_t in_bytesleft = sel_len - 2, out_bytesleft = sel_len;
while (iconv(cd, &in_buf, &in_bytesleft, &out_buf, &out_bytesleft) == -1
&& errno == E2BIG) {
fwrite(out_buf_start, sizeof(char), sel_len - out_bytesleft, fout);
out_buf = out_buf_start;
out_bytesleft = sel_len;
}
if (out_buf != out_buf_start)
fwrite(out_buf_start, sizeof(char), sel_len - out_bytesleft, fout);
free(out_buf_start);
iconv_close(cd);
return;
}
}
#endif
/* otherwise, print the raw buffer out */
fwrite(sel_buf, sizeof(char), sel_len, fout);
}
static int
doOut(Window win)
{
Atom sel_type = None;
unsigned char *sel_buf = NULL; /* buffer for selection data */
unsigned long sel_len = 0; /* length of sel_buf */
XEvent evt; /* X Event Structures */
unsigned int context = XCLIB_XCOUT_NONE;
if (sseln == XA_STRING)
sel_buf = (unsigned char *) XFetchBuffer(dpy, (int *) &sel_len, 0);
else {
while (1) {
/* only get an event if xcout() is doing something */
if (context != XCLIB_XCOUT_NONE)
XNextEvent(dpy, &evt);
/* fetch the selection, or part of it */
xcout(dpy, win, evt, sseln, target, &sel_type, &sel_buf, &sel_len, &context);
if (context == XCLIB_XCOUT_BAD_TARGET) {
if (target == XA_UTF8_STRING(dpy)) {
/* fallback is needed. set XA_STRING to target and restart the loop. */
context = XCLIB_XCOUT_NONE;
target = XA_STRING;
continue;
}
else {
/* no fallback available, exit with failure */
if (fsecm) {
/* If user requested -sensitive, then prevent further pastes (even though we failed to paste) */
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
/* Clear memory buffer */
xcmemzero(sel_buf,sel_len);
}
free(sel_buf);
errconvsel(dpy, target, sseln);
// errconvsel does not return but exits with EXIT_FAILURE
}
}
/* only continue if xcout() is doing something */
if (context == XCLIB_XCOUT_NONE)
break;
}
}
/* remove the last newline character if necessary */
if (frmnl && sel_len && sel_buf[sel_len - 1] == '\n') {
sel_len--;
}
if (sel_len) {
/* only print the buffer out, and free it, if it's not
* empty
*/
printSelBuf(stdout, sel_type, sel_buf, sel_len);
if (fsecm) {
/* If user requested -sensitive, then prevent further pastes */
XSetSelectionOwner(dpy, sseln, None, CurrentTime);
/* Clear memory buffer */
xcmemzero(sel_buf,sel_len);
}
if (sseln == XA_STRING) {
XFree(sel_buf);
}
else {
free(sel_buf);
}
}
return EXIT_SUCCESS;
}
int
main(int argc, char *argv[])
{
/* Declare variables */
Window win; /* Window */
int exit_code;
/* As a convenience to command-line users, default to -o if stdin
* is a tty. Will be overridden by -i or if user specifies a
* filename as input.
*/
if (isatty(0)) {
fdiri = F; /* direction is out */
}
/* set up option table. I can't figure out a better way than this to
* do it while sticking to pure ANSI C. The option and specifier
* members have a type of volatile char *, so they need to be allocated
* by strdup or malloc, you can't set them to a string constant at
* declare time, this is not pure ANSI C apparently, although it does
* work with gcc
*/
int i=0;
/* loop option entry */
opt_tab[i].option = xcstrdup("-loops");
opt_tab[i].specifier = xcstrdup(".loops");
opt_tab[i].argKind = XrmoptionSepArg;
opt_tab[i].value = (XPointer) NULL;
i++;
/* display option entry */
opt_tab[i].option = xcstrdup("-display");
opt_tab[i].specifier = xcstrdup(".display");
opt_tab[i].argKind = XrmoptionSepArg;
opt_tab[i].value = (XPointer) NULL;
i++;
/* selection option entry */
opt_tab[i].option = xcstrdup("-selection");
opt_tab[i].specifier = xcstrdup(".selection");
opt_tab[i].argKind = XrmoptionSepArg;
opt_tab[i].value = (XPointer) NULL;
i++;
/* filter option entry */
opt_tab[i].option = xcstrdup("-filter");
opt_tab[i].specifier = xcstrdup(".filter");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup(ST);
i++;
/* in option entry */
opt_tab[i].option = xcstrdup("-in");
opt_tab[i].specifier = xcstrdup(".direction");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("I");
i++;
/* out option entry */
opt_tab[i].option = xcstrdup("-out");
opt_tab[i].specifier = xcstrdup(".direction");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("O");
i++;
/* version option entry */
opt_tab[i].option = xcstrdup("-version");
opt_tab[i].specifier = xcstrdup(".print");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("V");
i++;
/* help option entry */
opt_tab[i].option = xcstrdup("-help");
opt_tab[i].specifier = xcstrdup(".print");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("H");
i++;
/* silent option entry */
opt_tab[i].option = xcstrdup("-silent");
opt_tab[i].specifier = xcstrdup(".olevel");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("S");
i++;
/* quiet option entry */
opt_tab[i].option = xcstrdup("-quiet");
opt_tab[i].specifier = xcstrdup(".olevel");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("Q");
i++;
/* verbose option entry */
opt_tab[i].option = xcstrdup("-verbose");
opt_tab[i].specifier = xcstrdup(".olevel");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("V");
i++;
/* debug option entry */
opt_tab[i].option = xcstrdup("-debug");
opt_tab[i].specifier = xcstrdup(".olevel");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("D");
i++;
/* utf8 option entry */
opt_tab[i].option = xcstrdup("-noutf8");
opt_tab[i].specifier = xcstrdup(".noutf8");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("N");
i++;
/* target option entry */
opt_tab[i].option = xcstrdup("-target");
opt_tab[i].specifier = xcstrdup(".target");
opt_tab[i].argKind = XrmoptionSepArg;
opt_tab[i].value = (XPointer) NULL;
i++;
/* "remove newline if it is the last character" entry */
opt_tab[i].option = xcstrdup("-rmlastnl");
opt_tab[i].specifier = xcstrdup(".rmlastnl");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup(ST);
i++;
/* sensitive mode for pasting passwords */
opt_tab[i].option = xcstrdup("-sensitive");
opt_tab[i].specifier = xcstrdup(".sensitive");
opt_tab[i].argKind = XrmoptionNoArg;
opt_tab[i].value = (XPointer) xcstrdup("s");
i++;
/* wait option entry */
opt_tab[i].option = xcstrdup("-wait");
opt_tab[i].specifier = xcstrdup(".wait");
opt_tab[i].argKind = XrmoptionSepArg;
opt_tab[i].value = (XPointer) NULL;
i++;
/* save size of opt_tab for doOptMain to use */
opt_tab_size = i;
if ( ( sizeof(opt_tab) / sizeof(opt_tab[0]) ) < opt_tab_size ) {
fprintf(stderr,
"xclip: programming error: opt_tab declared to hold %ld options, but %d defined\n",
sizeof(opt_tab) / sizeof(opt_tab[0]), opt_tab_size);
return EXIT_FAILURE;
}
/* parse command line options */
doOptMain(argc, argv);
/* Connect to the X server. */
if ((dpy = XOpenDisplay(sdisp))) {
/* successful */
if (xcverb >= ODEBUG)
fprintf(stderr, "Connected to X server.\n");
}
else {
/* couldn't connect to X server. Print error and exit */
errxdisplay(sdisp);
}
/* parse selection command line option */
doOptSel();
/* parse noutf8 and target command line options */
doOptTarget();
/* Create a window to trap events */
win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0);
/* get events about property changes */
XSelectInput(dpy, win, PropertyChangeMask);
/* If we get an X error, catch it instead of barfing */
XSetErrorHandler(xchandler);
if (fdiri)
exit_code = doIn(win, argv[0]);
else
exit_code = doOut(win);
/* Disconnect from the X server */
XCloseDisplay(dpy);
/* exit */
return exit_code;
}