File: xsel.c

package info (click to toggle)
xsel 1.2.0%2Bgit9bfc13d.20180109-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 356 kB
  • sloc: ansic: 1,453; sh: 588; makefile: 9
file content (2306 lines) | stat: -rw-r--r-- 64,801 bytes parent folder | download | duplicates (2)
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
/*
 * xsel -- manipulate the X selection
 * Copyright (C) 2001 Conrad Parker <conrad@vergenet.net>
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or
 * implied warranty.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/time.h>
#include <setjmp.h>
#include <signal.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>

#include "xsel.h"


/* The name we were invoked as (argv[0]) */
static char * progname;

/* Verbosity level for debugging */
static int debug_level = DEBUG_LEVEL;

/* Our X Display and Window */
static Display * display;
static Window window;

/* Maxmimum request size supported by this X server */
static long max_req;

/* Our timestamp for all operations */
static Time timestamp;

static Atom timestamp_atom; /* The TIMESTAMP atom */
static Atom multiple_atom; /* The MULTIPLE atom */
static Atom targets_atom; /* The TARGETS atom */
static Atom delete_atom; /* The DELETE atom */
static Atom incr_atom; /* The INCR atom */
static Atom null_atom; /* The NULL atom */
static Atom text_atom; /* The TEXT atom */
static Atom utf8_atom; /* The UTF8 atom */
static Atom compound_text_atom; /* The COMPOUND_TEXT atom */

/* Number of selection targets served by this.
 * (MULTIPLE, INCR, TARGETS, TIMESTAMP, DELETE, TEXT, UTF8_STRING and STRING)
 * NB. We do not currently serve COMPOUND_TEXT; we can retrieve it but do not
 * perform charset conversion.
 */
#define MAX_NUM_TARGETS 9
static int NUM_TARGETS;
static Atom supported_targets[MAX_NUM_TARGETS];

/* do_zeroflush: Use only last zero-separated part of input.
 * All previous parts are discarded */
static Bool do_zeroflush = False;

/* do_follow: Follow mode for output */
static Bool do_follow = False;

/* nodaemon: Disable daemon mode if True. */
static Bool no_daemon = False;

/* logfile: name of file to log error messages to when detached */
static char logfile[MAXFNAME];

/* fstat() on stdin and stdout */
static struct stat in_statbuf, out_statbuf;

static int total_input = 0;
static int current_alloc = 0;

static long timeout = 0;
static struct itimerval timer;

#define USEC_PER_SEC 1000000

static int saved_argc;
static char ** saved_argv;

/*
 * usage ()
 *
 * print usage information.
 */
static void
usage (void)
{
  printf ("Usage: xsel [options]\n");
  printf ("Manipulate the X selection.\n\n");
  printf ("By default the current selection is output and not modified if both\n");
  printf ("standard input and standard output are terminals (ttys).  Otherwise,\n");
  printf ("the current selection is output if standard output is not a terminal\n");
  printf ("(tty), and the selection is set from standard input if standard input\n");
  printf ("is not a terminal (tty). If any input or output options are given then\n");
  printf ("the program behaves only in the requested mode.\n\n");
  printf ("If both input and output is required then the previous selection is\n");
  printf ("output before being replaced by the contents of standard input.\n\n");
  printf ("Input options\n");
  printf ("  -a, --append          Append standard input to the selection\n");
  printf ("  -f, --follow          Append to selection as standard input grows\n");
  printf ("  -z, --zeroflush       Overwrites selection when zero ('\\0') is received\n");
  printf ("  -i, --input           Read standard input into the selection\n\n");
  printf ("Output options\n");
  printf ("  -o, --output          Write the selection to standard output\n\n");
  printf ("Action options\n");
  printf ("  -c, --clear           Clear the selection\n");
  printf ("  -d, --delete          Request that the selection be cleared and that\n");
  printf ("                        the application owning it delete its contents\n\n");
  printf ("Selection options\n");
  printf ("  -p, --primary         Operate on the PRIMARY selection (default)\n");
  printf ("  -s, --secondary       Operate on the SECONDARY selection\n");
  printf ("  -b, --clipboard       Operate on the CLIPBOARD selection\n\n");
  printf ("  -k, --keep            Do not modify the selections, but make the PRIMARY\n");
  printf ("                        and SECONDARY selections persist even after the\n");
  printf ("                        programs they were selected in exit.\n");
  printf ("  -x, --exchange        Exchange the PRIMARY and SECONDARY selections\n\n");
  printf ("X options\n");
  printf ("  --display displayname\n");
  printf ("                        Specify the connection to the X server\n");
  printf ("  -t ms, --selectionTimeout ms\n");
  printf ("                        Specify the timeout in milliseconds within which the\n");
  printf ("                        selection must be retrieved. A value of 0 (zero)\n");
  printf ("                        specifies no timeout (default)\n\n");
  printf ("Miscellaneous options\n");
  printf ("  -l, --logfile         Specify file to log errors to when detached.\n");
  printf ("  -n, --nodetach        Do not detach from the controlling terminal. Without\n");
  printf ("                        this option, xsel will fork to become a background\n");
  printf ("                        process in input, exchange and keep modes.\n\n");
  printf ("  -h, --help            Display this help and exit\n");
  printf ("  -v, --verbose         Print informative messages\n");
  printf ("  --version             Output version information and exit\n\n");
  printf ("Please report bugs to <conrad@vergenet.net>.\n");
}

/*
 * exit_err (fmt)
 *
 * Print a formatted error message and errno information to stderr,
 * then exit with return code 1.
 */
static void
exit_err (const char * fmt, ...)
{
  va_list ap;
  int errno_save;
  char buf[MAXLINE];
  int n;

  errno_save = errno;

  va_start (ap, fmt);

  snprintf (buf, MAXLINE, "%s: ", progname);
  n = strlen (buf);

  vsnprintf (buf+n, MAXLINE-n, fmt, ap);
  n = strlen (buf);

  snprintf (buf+n, MAXLINE-n, ": %s\n", strerror (errno_save));

  fflush (stdout); /* in case stdout and stderr are the same */
  fputs (buf, stderr);
  fflush (NULL);

  va_end (ap);
  exit (1);
}

/*
 * print_err (fmt)
 *
 * Print a formatted error message to stderr.
 */
static void
print_err (const char * fmt, ...)
{
  va_list ap;
  int errno_save;
  char buf[MAXLINE];
  int n;

  errno_save = errno;

  va_start (ap, fmt);

  snprintf (buf, MAXLINE, "%s: ", progname);
  n = strlen (buf);

  vsnprintf (buf+n, MAXLINE-n, fmt, ap);
  n = strlen (buf);

  fflush (stdout); /* in case stdout and stderr are the same */
  fputs (buf, stderr);
  fputc ('\n', stderr);
  fflush (NULL);

  va_end (ap);
}

/*
 * print_debug (level, fmt)
 *
 * Print a formatted debugging message of level 'level' to stderr
 */
#define print_debug(x,y...) {if (x <= debug_level) print_err (y);}

/*
 * get_atom_name (atom)
 *
 * Returns a string with a printable name for the Atom 'atom'.
 */
static char *
get_atom_name (Atom atom)
{
  char * ret;
  static char atom_name[MAXLINE+1];

  if (atom == None) return "None";
  if (atom == XA_STRING) return "STRING";
  if (atom == XA_PRIMARY) return "PRIMARY";
  if (atom == XA_SECONDARY) return "SECONDARY";
  if (atom == timestamp_atom) return "TIMESTAMP";
  if (atom == multiple_atom) return "MULTIPLE";
  if (atom == targets_atom) return "TARGETS";
  if (atom == delete_atom) return "DELETE";
  if (atom == incr_atom) return "INCR";
  if (atom == null_atom) return "NULL";
  if (atom == text_atom) return "TEXT";
  if (atom == utf8_atom) return "UTF8_STRING";

  ret = XGetAtomName (display, atom);
  //strncpy (atom_name, ret, sizeof (atom_name));
  snprintf(atom_name, sizeof (atom_name), "%s", ret);
  if (atom_name[MAXLINE] != '\0')
    {
      atom_name[MAXLINE-3] = '.';
      atom_name[MAXLINE-2] = '.';
      atom_name[MAXLINE-1] = '.';
      atom_name[MAXLINE] = '\0';
    }
  XFree (ret);

  return atom_name;
}

/*
 * debug_property (level, requestor, property, target, length)
 *
 * Print debugging information (at level 'level') about a property received.
 */
static void
debug_property (int level, Window requestor, Atom property, Atom target,
                unsigned long length)
{
  print_debug (level, "Got window property: requestor 0x%x, property 0x%x, target 0x%x %s, length %ld bytes", requestor, property, target, get_atom_name (target), length);
}

/*
 * xs_malloc (size)
 *
 * Malloc wrapper. Always returns a successful allocation. Exits if the
 * allocation didn't succeed.
 */
static void *
xs_malloc (size_t size)
{
  void * ret;

  if (size == 0) size = 1;
  if ((ret = malloc (size)) == NULL) {
    exit_err ("malloc error");
  }

  return ret;
}

/*
 * xs_strdup (s)
 *
 * strdup wrapper for unsigned char *
 */
#define xs_strdup(s) ((unsigned char *) _xs_strdup ((const char *)s))
static char * _xs_strdup (const char * s)
{
  char * ret;

  if (s == NULL) return NULL;
  if ((ret = strdup(s)) == NULL) {
    exit_err ("strdup error");
  }

  return ret; 
}

/*
 * xs_strlen (s)
 *
 * strlen wrapper for unsigned char *
 */
#define xs_strlen(s) (strlen ((const char *) s))

/*
 * xs_strncpy (s)
 *
 * strncpy wrapper for unsigned char *
 */
#define xs_strncpy(dest,s,n) (_xs_strncpy ((char *)dest, (const char *)s, n))
static char *
_xs_strncpy (char * dest, const char * src, size_t n)
{
  if (n > 0) {
    //strncpy (dest, src, n);
	snprintf (dest, n, "%s", src);
    dest[n-1] = '\0';
  }
  return dest;
}

/*
 * get_xdg_cache_home ()
 *
 * Get the user's cache directory
 */
static char *
get_xdg_cache_home (void)
{
  char * cachedir;
  char * homedir;
  static const char * slashbasename = "/.cache";

  if ((cachedir = getenv ("XDG_CACHE_HOME")) == NULL) {
    if ((homedir = getenv ("HOME")) == NULL) {
      exit_err ("no HOME directory");
    }
    cachedir = xs_malloc (strlen (homedir) + strlen (slashbasename) + 1);
    strcpy (cachedir, homedir);
    strcat (cachedir, slashbasename);
  } else {
    cachedir = _xs_strdup (cachedir);
  }

  mkdir (cachedir, S_IRWXU|S_IRGRP|S_IXGRP);

  return cachedir;
}

/*
 * The set of terminal signals we block while handling SelectionRequests.
 *
 * If we exit in the middle of handling a SelectionRequest, we might leave the
 * requesting client hanging, so we try to be nice and finish handling
 * requests before terminating.  Hence we block SIG{ALRM,INT,TERM} while
 * handling requests and unblock them only while waiting in XNextEvent().
 */
static sigset_t exit_sigs;

static void block_exit_sigs(void)
{
  sigprocmask (SIG_BLOCK, &exit_sigs, NULL);
}

static void unblock_exit_sigs(void)
{
  sigprocmask (SIG_UNBLOCK, &exit_sigs, NULL);
}

/* The jmp_buf to longjmp out of the signal handler */
static sigjmp_buf env_alrm;

/*
 * alarm_handler (sig)
 *
 * Signal handler for catching SIGALRM.
 */
static void
alarm_handler (int sig)
{
  siglongjmp (env_alrm, 1);
}

/*
 * set_timer_timeout ()
 *
 * Set timer parameters according to specified timeout.
 */
static void
set_timer_timeout (void)
{
  timer.it_interval.tv_sec = timeout / USEC_PER_SEC;
  timer.it_interval.tv_usec = timeout % USEC_PER_SEC;
  timer.it_value.tv_sec = timeout / USEC_PER_SEC;
  timer.it_value.tv_usec = timeout % USEC_PER_SEC;
}

/*
 * set_daemon_timeout ()
 *
 * Set up a timer to cause the daemon to exit after the desired
 * amount of time.
 */
static void
set_daemon_timeout (void)
{
  if (signal (SIGALRM, alarm_handler) == SIG_ERR) {
    exit_err ("error setting timeout handler");
  }

  set_timer_timeout ();

  if (sigsetjmp (env_alrm, 0) == 0) {
    setitimer (ITIMER_REAL, &timer, (struct itimerval *)0);
  } else {
    print_debug (D_INFO, "daemon exiting after %d ms", timeout / 1000);
    exit (0);
  }
}


/*
 * become_daemon ()
 *
 * Perform the required procedure to become a daemon process, as
 * outlined in the Unix programming FAQ:
 * http://www.steve.org.uk/Reference/Unix/faq_2.html#SEC16
 * and open a logfile.
 */
static void
become_daemon (void)
{
  pid_t pid;
  int null_r_fd, null_w_fd, log_fd;
  char * cachedir;

  if (no_daemon) {
	  /* If the user has specified a timeout, enforce it even if we don't
	   * actually daemonize */
	  set_daemon_timeout ();
	  return;
  }

  cachedir = get_xdg_cache_home();

  /* Check that we can open a logfile before continuing */

  /* If the user has specified a --logfile, use that ... */
  if (logfile[0] == '\0') {
    /* ... otherwise use the default logfile */
    snprintf (logfile, MAXFNAME, "%s/xsel.log", cachedir);
  }

  /* Make sure to create the logfile with sane permissions */
  log_fd = open (logfile, O_WRONLY|O_APPEND|O_CREAT, 0600);
  if (log_fd == -1) {
    exit_err ("error opening logfile %s for writing", logfile);
  }
  print_debug (D_INFO, "opened logfile %s", logfile);

  if ((pid = fork()) == -1) {
    exit_err ("error forking");
  } else if (pid > 0) {
    _exit (0);
  }

  if (setsid () == -1) {
    exit_err ("setsid error");
  }

  if ((pid = fork()) == -1) {
    exit_err ("error forking");
  } else if (pid > 0) {
    _exit (0);
  }

  umask (0);

  if (chdir (cachedir) == -1) {
    print_debug (D_WARN, "Could not chdir to %s\n", cachedir);
    if (chdir ("/") == -1) {
      exit_err ("Error chdir to /");
    }
  }

  /* dup2 /dev/null on stdin unless following input */
  if (!do_follow) {
    null_r_fd = open ("/dev/null", O_RDONLY);
    if (null_r_fd == -1) {
      exit_err ("error opening /dev/null for reading");
    }
    if (dup2 (null_r_fd, 0) == -1) {
      exit_err ("error duplicating /dev/null on stdin");
    }
  }

  /* dup2 /dev/null on stdout */
  null_w_fd = open ("/dev/null", O_WRONLY|O_APPEND);
  if (null_w_fd == -1) {
    exit_err ("error opening /dev/null for writing");
  }
  if (dup2 (null_w_fd, 1) == -1) {
    exit_err ("error duplicating /dev/null on stdout");
  }

  /* dup2 logfile on stderr */
  if (dup2 (log_fd, 2) == -1) {
    exit_err ("error duplicating logfile %s on stderr", logfile);
  }

  set_daemon_timeout ();

  free (cachedir);
}

/*
 * get_timestamp ()
 *
 * Get the current X server time.
 *
 * This is done by doing a zero-length append to a random property of the
 * window, and checking the time on the subsequent PropertyNotify event.
 *
 * PRECONDITION: the window must have PropertyChangeMask set.
 */
static Time
get_timestamp (void)
{
  XEvent event;

  XChangeProperty (display, window, XA_WM_NAME, XA_STRING, 8,
                   PropModeAppend, NULL, 0);

  while (1) {
    XNextEvent (display, &event);

    if (event.type == PropertyNotify)
      return event.xproperty.time;
  }
}

/*
 * SELECTION RETRIEVAL
 * ===================
 *
 * The following functions implement retrieval of an X selection,
 * optionally within a user-specified timeout.
 *
 *
 * Selection timeout handling.
 * ---------------------------
 *
 * The selection retrieval can time out if no response is received within
 * a user-specified time limit. In order to ensure we time the entire
 * selection retrieval, we use an interval timer and catch SIGALRM.
 * [Calling select() on the XConnectionNumber would only provide a timeout
 * to the first XEvent.]
 */

/*
 * get_append_property ()
 *
 * Get a window property and append its data to a buffer at a given offset
 * pointed to by *offset. 'offset' is modified by this routine to point to
 * the end of the data.
 *
 * Returns True if more data is available for receipt.
 *
 * If an error is encountered, the buffer is free'd.
 */
static Bool
get_append_property (XSelectionEvent * xsl, unsigned char ** buffer,
                     unsigned long * offset, unsigned long * alloc)
{
  unsigned char * ptr;
  Atom target;
  int format;
  unsigned long bytesafter, length;
  unsigned char * value;

  XGetWindowProperty (xsl->display, xsl->requestor, xsl->property,
                      0L, 1000000, True, (Atom)AnyPropertyType,
                      &target, &format, &length, &bytesafter, &value);

  debug_property (D_TRACE, xsl->requestor, xsl->property, target, length);

  if (target != XA_STRING && target != utf8_atom &&
      target != compound_text_atom) {
    print_debug (D_OBSC, "target %s not XA_STRING nor UTF8_STRING in get_append_property()",
                 get_atom_name (target));
    free (*buffer);
    *buffer = NULL;
    return False;
  } else if (length == 0) {
    /* A length of 0 indicates the end of the transfer */
    print_debug (D_TRACE, "Got zero length property; end of INCR transfer");
    return False;
  } else if (format == 8) {
    if (*offset + length + 1 > *alloc) {
      *alloc = *offset + length + 1;
      if ((*buffer = realloc (*buffer, *alloc)) == NULL) {
        exit_err ("realloc error");
      }
    }
    ptr = *buffer + *offset;
    memcpy (ptr, value, length);
    ptr[length] = '\0';
    *offset += length;
    print_debug (D_TRACE, "Appended %d bytes to buffer\n", length);
  } else {
    print_debug (D_WARN, "Retrieved non-8-bit data\n");
  }

  return True;
}


/*
 * wait_incr_selection (selection)
 *
 * Retrieve a property of target type INCR. Perform incremental retrieval
 * and return the resulting data.
 */
static unsigned char *
wait_incr_selection (Atom selection, XSelectionEvent * xsl, int init_alloc)
{
  XEvent event;
  unsigned char * incr_base = NULL, * incr_ptr = NULL;
  unsigned long incr_alloc = 0, incr_xfer = 0;
  Bool wait_prop = True;

  print_debug (D_TRACE, "Initialising incremental retrieval of at least %d bytes\n", init_alloc);

  /* Take an interest in the requestor */
  XSelectInput (xsl->display, xsl->requestor, PropertyChangeMask);

  incr_alloc = init_alloc;
  incr_base = xs_malloc (incr_alloc);
  incr_ptr = incr_base;

  print_debug (D_TRACE, "Deleting property that informed of INCR transfer");
  XDeleteProperty (xsl->display, xsl->requestor, xsl->property);

  print_debug (D_TRACE, "Waiting on PropertyNotify events");
  while (wait_prop) {
    XNextEvent (xsl->display, &event);

    switch (event.type) {
    case PropertyNotify:
      if (event.xproperty.state != PropertyNewValue) break;

      wait_prop = get_append_property (xsl, &incr_base, &incr_xfer,
                                       &incr_alloc);
      break;
    default:
      break;
    }
  }

  /* when zero length found, finish up & delete last */
  XDeleteProperty (xsl->display, xsl->requestor, xsl->property);

  print_debug (D_TRACE, "Finished INCR retrieval");

  return incr_base;
}

/*
 * wait_selection (selection, request_target)
 *
 * Block until we receive a SelectionNotify event, and return its
 * contents; or NULL in the case of a deletion or error. This assumes we
 * have already called XConvertSelection, requesting a string (explicitly
 * XA_STRING) or deletion (delete_atom).
 */
static unsigned char *
wait_selection (Atom selection, Atom request_target)
{
  XEvent event;
  Atom target;
  int format;
  unsigned long bytesafter, length;
  unsigned char * value, * retval = NULL;
  Bool keep_waiting = True;

  while (keep_waiting) {
    XNextEvent (display, &event);

    switch (event.type) {
    case SelectionNotify:
      if (event.xselection.selection != selection) break;

      if (event.xselection.property == None) {
        print_debug (D_WARN, "Conversion refused");
        value = NULL;
        keep_waiting = False;
      } else if (event.xselection.property == null_atom &&
                 request_target == delete_atom) {
      } else {
	XGetWindowProperty (event.xselection.display,
			    event.xselection.requestor,
			    event.xselection.property, 0L, 1000000,
			    False, (Atom)AnyPropertyType, &target,
			    &format, &length, &bytesafter, &value);

        debug_property (D_TRACE, event.xselection.requestor,
                        event.xselection.property, target, length);

        if (request_target == delete_atom && value == NULL) {
          keep_waiting = False;
        } else if (target == incr_atom) {
          /* Handle INCR transfers */
          retval = wait_incr_selection (selection, &event.xselection,
                                        *(long *)value);
          keep_waiting = False;
        } else if (target != utf8_atom && target != XA_STRING &&
                   target != compound_text_atom &&
                   request_target != delete_atom) {
          /* Report non-TEXT atoms */
          print_debug (D_WARN, "Selection (type %s) is not a string.",
                       get_atom_name (target));
          free (retval);
          retval = NULL;
          keep_waiting = False;
        } else {
          retval = xs_strdup (value);
          XFree (value);
          keep_waiting = False;
        }

        XDeleteProperty (event.xselection.display,
                         event.xselection.requestor,
                         event.xselection.property);

      }
      break;
    default:
      break;
    }
  }

  /* Now that we've received the SelectionNotify event, clear any
   * remaining timeout. */
  if (timeout > 0) {
    setitimer (ITIMER_REAL, (struct itimerval *)0, (struct itimerval *)0);
  }

  return retval;
}

/*
 * get_selection (selection, request_target)
 *
 * Retrieves the specified selection and returns its value.
 *
 * If a non-zero timeout is specified then set a virtual interval
 * timer. Return NULL and print an error message if the timeout
 * expires before the selection has been retrieved.
 */
static unsigned char *
get_selection (Atom selection, Atom request_target)
{
  Atom prop;
  unsigned char * retval;

  prop = XInternAtom (display, "XSEL_DATA", False);
  XConvertSelection (display, selection, request_target, prop, window,
                     timestamp);
  XSync (display, False);

  if (timeout > 0) {
    if (signal (SIGALRM, alarm_handler) == SIG_ERR) {
      exit_err ("error setting timeout handler");
    }

    set_timer_timeout ();

    if (sigsetjmp (env_alrm, 0) == 0) {
      setitimer (ITIMER_REAL, &timer, (struct itimerval *)0);
      retval = wait_selection (selection, request_target);
    } else {
      print_debug (D_WARN, "selection timed out");
      retval = NULL;
    }
  } else {
    retval = wait_selection (selection, request_target);
  }

  return retval;
}

/*
 * get_selection_text (Atom selection)
 *
 * Retrieve a text selection. First attempt to retrieve it as UTF_STRING,
 * and if that fails attempt to retrieve it as a plain XA_STRING.
 *
 * NB. Before implementing this, an attempt was made to query TARGETS and
 * request UTF8_STRING only if listed there, as described in:
 * http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text
 * However, that did not seem to work reliably when tested against various
 * applications (eg. Mozilla Firefox). This method is of course more
 * reliable.
 */
static unsigned char *
get_selection_text (Atom selection)
{
  unsigned char * retval;

  if ((retval = get_selection (selection, utf8_atom)) == NULL)
    retval = get_selection (selection, XA_STRING);

  return retval;
}


/*
 * SELECTION SETTING
 * =================
 *
 * The following functions allow a given selection to be set, appended to
 * or cleared, or to exchange the primary and secondary selections.
 */

/*
 * copy_sel (s)
 *
 * Copy a string into a new selection buffer, and intitialise
 * current_alloc and total_input to exactly its length.
 */
static unsigned char *
copy_sel (unsigned char * s)
{
  if (s) {
    current_alloc = total_input = xs_strlen (s);
    return xs_strdup (s);
  }
  current_alloc = total_input = 0;
  return NULL;
}

/*
 * read_input (read_buffer, do_select)
 *
 * Read input from stdin into the specified read_buffer.
 *
 * read_buffer must have been dynamically allocated before calling this
 * function, or be NULL. Input is read until end-of-file is reached, and
 * read_buffer will be reallocated to accomodate the entire contents of
 * the input. read_buffer, which may have been reallocated, is returned
 * upon completion.
 *
 * If 'do_select' is True, this function will first check if any data
 * is available for reading, and return immediately if not.
 */
static unsigned char *
read_input (unsigned char * read_buffer, Bool do_select)
{
  int insize = in_statbuf.st_blksize;
  unsigned char * new_buffer = NULL;
  int d, fatal = 0, nfd;
  ssize_t n;
  fd_set fds;
  struct timeval select_timeout;

  do {

    if (do_select) {
try_read:
      /* Check if data is available for reading -- if not, return immediately */
      FD_ZERO (&fds);
      FD_SET (0, &fds);

      select_timeout.tv_sec = (time_t)0;
      select_timeout.tv_usec = (time_t)0;

      nfd = select (1, &fds, NULL, NULL, &select_timeout);
      if (nfd == -1) {
        if (errno == EINTR) goto try_read;
        else exit_err ("select error");
      } else if (nfd == 0) {
        print_debug (D_TRACE, "No data available for reading");
        break;
      }
    }

    /* check if buffer is full */
    if (current_alloc == total_input) {
      if ((d = (current_alloc % insize)) != 0) current_alloc += (insize-d);
      current_alloc *= 2;
      new_buffer = realloc (read_buffer, current_alloc);
      if (new_buffer == NULL) {
        exit_err ("realloc error");
      }
      read_buffer = new_buffer;
    }

    /* read the remaining data, up to the optimal block length */
    n = read (0, &read_buffer[total_input],
              MIN(current_alloc - total_input, insize));
    if (n == -1) {
      switch (errno) {
      case EAGAIN:
      case EINTR:
        break;
      default:
        perror ("read error");
        fatal = 1;
        break;
      }
    }
    total_input += n;
  } while (n != 0 && !fatal);

  read_buffer[total_input] = '\0';

  if(do_zeroflush && total_input > 0) {
    int i;
    for(i=total_input-1; i>=0; i--) {
      if(read_buffer[i] == '\0') {
        print_debug (D_TRACE, "Flushing input at %d", i);
        memmove(&read_buffer[0], &read_buffer[i+1], total_input - i);
        total_input = total_input - i - 1;
        read_buffer[total_input] = '\0';
        break;
      }
    }
  }

  print_debug (D_TRACE, "Accumulated %d bytes input", total_input);

  return read_buffer;
}

/*
 * initialise_read (read_buffer)
 *
 * Initialises the read_buffer and the state variable current_alloc.
 * read_buffer is reallocated to accomodate either the entire input
 * if stdin is a regular file, or at least one block of input otherwise.
 * If the supplied read_buffer is NULL, a new buffer will be allocated.
 */
static unsigned char *
initialise_read (unsigned char * read_buffer)
{
  int insize = in_statbuf.st_blksize;
  unsigned char * new_buffer = NULL;

  if (S_ISREG (in_statbuf.st_mode) && in_statbuf.st_size > 0) {
    current_alloc += in_statbuf.st_size;
  } else {
    current_alloc += insize;
  }

  if ((new_buffer = realloc (read_buffer, current_alloc)) == NULL) {
    exit_err ("realloc error");
  }

  read_buffer = new_buffer;

  return read_buffer;
}

/* Forward declaration of refuse_all_incr () */
static void
refuse_all_incr (void);

/*
 * handle_x_errors ()
 *
 * XError handler.
 */
static int
handle_x_errors (Display * display, XErrorEvent * eev)
{
  char err_buf[MAXLINE];

  /* Make sure to send a refusal to all waiting INCR requests
   * and delete the corresponding properties. */
  if (eev->error_code == BadAlloc) refuse_all_incr ();

  XGetErrorText (display, eev->error_code, err_buf, MAXLINE);
  exit_err (err_buf);

  return 0;
}

/*
 * clear_selection (selection)
 *
 * Clears the specified X selection 'selection'. This requests that no
 * process should own 'selection'; thus the X server will respond to
 * SelectionRequests with an empty property and we don't need to leave
 * a daemon hanging around to service this selection.
 */
static void
clear_selection (Atom selection)
{
  XSetSelectionOwner (display, selection, None, timestamp);
  /* Call XSync to ensure this operation completes before program
   * termination, especially if this is all we are doing. */
  XSync (display, False);
}

/*
 * own_selection (selection)
 *
 * Requests ownership of the X selection. Returns True if ownership was
 * granted, and False otherwise.
 */
static Bool
own_selection (Atom selection)
{
  Window owner;

  XSetSelectionOwner (display, selection, window, timestamp);
  /* XGetSelectionOwner does a round trip to the X server, so there is
   * no need to call XSync here. */
  owner = XGetSelectionOwner (display, selection);
  if (owner != window) {
    return False;
  } else {
    XSetErrorHandler (handle_x_errors);
    return True;
  }
}


static IncrTrack * incrtrack_list = NULL;

/*
 * add_incrtrack (it)
 *
 * Add 'it' to the head of incrtrack_list.
 */
static void
add_incrtrack (IncrTrack * it)
{
  if (incrtrack_list) {
    incrtrack_list->prev = it;
  }
  it->prev = NULL;
  it->next = incrtrack_list;
  incrtrack_list = it;
}

/*
 * remove_incrtrack (it)
 *
 * Remove 'it' from incrtrack_list.
 */
static void
remove_incrtrack (IncrTrack * it)
{
  if (it->prev) {
    it->prev->next = it->next;
  }
  if (it->next) {
    it->next->prev = it->prev;
  }

  if (incrtrack_list == it) {
    incrtrack_list = it->next;
  }
}

/*
 * fresh_incrtrack ()
 *
 * Create a new incrtrack, and add it to incrtrack_list.
 */
static IncrTrack *
fresh_incrtrack (void)
{
  IncrTrack * it;

  it = xs_malloc (sizeof (IncrTrack));
  add_incrtrack (it);

  return it;
}

/*
 * trash_incrtrack (it)
 *
 * Remove 'it' from incrtrack_list, and free it.
 */
static void
trash_incrtrack (IncrTrack * it)
{
  remove_incrtrack (it);
  free (it);
}

/*
 * find_incrtrack (atom)
 *
 * Find the IncrTrack structure within incrtrack_list pertaining to 'atom',
 * if it exists.
 */
static IncrTrack *
find_incrtrack (Atom atom)
{
  IncrTrack * iti;

  for (iti = incrtrack_list; iti; iti = iti->next) {
    if (atom == iti->property) return iti;
  }

  return NULL;
}

/* Forward declaration of handle_multiple() */
static HandleResult
handle_multiple (Display * display, Window requestor, Atom property,
                 unsigned char * sel, Atom selection, Time time,
                 MultTrack * mparent);

/* Forward declaration of process_multiple() */
static HandleResult
process_multiple (MultTrack * mt, Bool do_parent);

/*
 * confirm_incr (it)
 *
 * Confirm the selection request of ITER tracked by 'it'.
 */
static void
notify_incr (IncrTrack * it, HandleResult hr)
{
  XSelectionEvent ev;

  /* Call XSync here to make sure any BadAlloc errors are caught before
   * confirming the conversion. */
  XSync (it->display, False);

  print_debug (D_TRACE, "Confirming conversion");

  /* Prepare a SelectionNotify event to send, placing the selection in the
   * requested property. */
  ev.type = SelectionNotify;
  ev.display = it->display;
  ev.requestor = it->requestor;
  ev.selection = it->selection;
  ev.time = it->time;
  ev.target = it->target;

  if (hr & HANDLE_ERR) ev.property = None;
  else ev.property = it->property;

  XSendEvent (display, ev.requestor, False,
              (unsigned long)NULL, (XEvent *)&ev);
}

/*
 * refuse_all_incr ()
 *
 * Refuse all INCR transfers in progress. ASSUMES that this is called in
 * response to an error, and that the program is about to bail out;
 * ie. incr_track is not cleaned out.
 */
static void
refuse_all_incr (void)
{
  IncrTrack * it;

  for (it = incrtrack_list; it; it = it->next) {
    XDeleteProperty (it->display, it->requestor, it->property);
    notify_incr (it, HANDLE_ERR);
    /* Don't bother trashing and list-removing these; we are about to
     * bail out anyway. */
  }
}

/*
 * complete_incr (it)
 *
 * Finish off an INCR retrieval. If it was part of a multiple, continue
 * that; otherwise, send confirmation that this completed.
 */
static void
complete_incr (IncrTrack * it, HandleResult hr)
{
  MultTrack * mparent = it->mparent;

  if (mparent) {
    trash_incrtrack (it);
    process_multiple (mparent, True);
  } else {
    notify_incr (it, hr);
    trash_incrtrack (it);
  }
}

/*
 * notify_multiple (mt, hr)
 *
 * Confirm the selection request initiated with MULTIPLE tracked by 'mt'.
 */
static void
notify_multiple (MultTrack * mt, HandleResult hr)
{
  XSelectionEvent ev;

  /* Call XSync here to make sure any BadAlloc errors are caught before
   * confirming the conversion. */
  XSync (mt->display, False);

  /* Prepare a SelectionNotify event to send, placing the selection in the
   * requested property. */
  ev.type = SelectionNotify;
  ev.display = mt->display;
  ev.requestor = mt->requestor;
  ev.selection = mt->selection;
  ev.time = mt->time;
  ev.target = multiple_atom;

  if (hr & HANDLE_ERR) ev.property = None;
  else ev.property = mt->property;

  XSendEvent (display, ev.requestor, False,
              (unsigned long)NULL, (XEvent *)&ev);
}

/*
 * complete_multiple (mt, do_parent, hr)
 *
 * Complete a MULTIPLE transfer. Iterate to its parent MULTIPLE if
 * 'do_parent' is true. If there is not parent MULTIPLE, send notification
 * of its completion with status 'hr'.
 */
static void
complete_multiple (MultTrack * mt, Bool do_parent, HandleResult hr)
{
  MultTrack * mparent = mt->mparent;

  if (mparent) {
    free (mt);
    if (do_parent) process_multiple (mparent, True);
  } else {
    notify_multiple (mt, hr);
    free (mt);
  }
}

/*
 * change_property (display, requestor, property, target, format, mode,
 *                  data, nelements)
 *
 * Wrapper to XChangeProperty that performs INCR transfer if required and
 * returns status of entire transfer.
 */
static HandleResult
change_property (Display * display, Window requestor, Atom property,
                 Atom target, int format, int mode,
                 unsigned char * data, int nelements,
                 Atom selection, Time time, MultTrack * mparent)
{
  XSelectionEvent ev;
  long nr_bytes;
  IncrTrack * it;

  print_debug (D_TRACE, "change_property ()");

  nr_bytes = nelements * format / 8;

  if (nr_bytes <= max_req) {
    print_debug (D_TRACE, "data within maximum request size");
    XChangeProperty (display, requestor, property, target, format, mode,
                     data, nelements);

    return HANDLE_OK;
  }

  /* else */
  print_debug (D_TRACE, "large data transfer");


  /* Send a SelectionNotify event */
  ev.type = SelectionNotify;
  ev.display = display;
  ev.requestor = requestor;
  ev.selection = selection;
  ev.time = time;
  ev.target = target;
  ev.property = property;

  XSelectInput (ev.display, ev.requestor, PropertyChangeMask);

  XChangeProperty (ev.display, ev.requestor, ev.property, incr_atom, 32,
                   PropModeReplace, (unsigned char *)&nr_bytes, 1);

  XSendEvent (display, requestor, False,
              (unsigned long)NULL, (XEvent *)&ev);

  /* Set up the IncrTrack to track this */
  it = fresh_incrtrack ();

  it->mparent = mparent;
  it->state = S_INCR_1;
  it->display = display;
  it->requestor = requestor;
  it->property = property;
  it->selection = selection;
  it->time = time;
  it->target = target;
  it->format = format;
  it->data = data;
  it->nelements = nelements;
  it->offset = 0;

  /* Maximum nr. of elements that can be transferred in one go */
  it->max_elements = max_req * 8 / format;

  /* Nr. of elements to transfer in this instance */
  it->chunk = MIN (it->max_elements, it->nelements - it->offset);

  /* Wait for that property to get deleted */
  print_debug (D_TRACE, "Waiting on initial property deletion (%s)",
               get_atom_name (it->property));

  return HANDLE_INCOMPLETE;
}

static HandleResult
incr_stage_1 (IncrTrack * it)
{
  /* First pass: PropModeReplace, from data, size chunk */
  print_debug (D_TRACE, "Writing first chunk (%d bytes) (target 0x%x %s) to property 0x%x of requestor 0x%x", it->chunk, it->target, get_atom_name(it->target), it->property, it->requestor);
  XChangeProperty (it->display, it->requestor, it->property, it->target,
                   it->format, PropModeReplace, it->data, it->chunk);

  it->offset += it->chunk;

  /* wait for PropertyNotify events */
  print_debug (D_TRACE, "Waiting on subsequent deletions ...");

  it->state = S_INCR_2;

  return HANDLE_INCOMPLETE;
}

static HandleResult
incr_stage_2 (IncrTrack * it)
{
  it->chunk = MIN (it->max_elements, it->nelements - it->offset);

  if (it->chunk <= 0) {

    /* Now write zero-length data to the property */
    XChangeProperty (it->display, it->requestor, it->property, it->target,
                     it->format, PropModeAppend, NULL, 0);
    it->state = S_NULL;
    print_debug (D_TRACE, "Set si to state S_NULL");
    return HANDLE_OK;
  } else {
    print_debug (D_TRACE, "Writing chunk (%d bytes) to property",
                 it->chunk);
    XChangeProperty (it->display, it->requestor, it->property, it->target,
                     it->format, PropModeAppend, it->data+it->offset,
                     it->chunk);
    it->offset += it->chunk;
    print_debug (D_TRACE, "%d bytes remaining",
                 it->nelements - it->offset);
    return HANDLE_INCOMPLETE;
  }
}


/*
 * handle_timestamp (display, requestor, property)
 *
 * Handle a TIMESTAMP request.
 */
static HandleResult
handle_timestamp (Display * display, Window requestor, Atom property,
                  Atom selection, Time time, MultTrack * mparent)
{
  return
    change_property (display, requestor, property, XA_INTEGER, 32,
                     PropModeReplace, (unsigned char *)&timestamp, 1,
                     selection, time, mparent);
}

/*
 * handle_targets (display, requestor, property)
 *
 * Handle a TARGETS request.
 */
static HandleResult
handle_targets (Display * display, Window requestor, Atom property,
                Atom selection, Time time, MultTrack * mparent)
{
  Atom * targets_cpy;
  HandleResult r;

  targets_cpy = malloc (sizeof (supported_targets));
  memcpy (targets_cpy, supported_targets, sizeof (supported_targets));

  r = change_property (display, requestor, property, XA_ATOM, 32,
                     PropModeReplace, (unsigned char *)targets_cpy,
                     NUM_TARGETS, selection, time, mparent);
  free(targets_cpy);
  return r;
}

/*
 * handle_string (display, requestor, property, sel)
 *
 * Handle a STRING request; setting 'sel' as the data
 */
static HandleResult
handle_string (Display * display, Window requestor, Atom property,
               unsigned char * sel, Atom selection, Time time,
               MultTrack * mparent)
{
  return
    change_property (display, requestor, property, XA_STRING, 8,
                     PropModeReplace, sel, xs_strlen(sel),
                     selection, time, mparent);
}

/*
 * handle_utf8_string (display, requestor, property, sel)
 *
 * Handle a UTF8_STRING request; setting 'sel' as the data
 */
static HandleResult
handle_utf8_string (Display * display, Window requestor, Atom property,
                    unsigned char * sel, Atom selection, Time time,
                    MultTrack * mparent)
{
  return
    change_property (display, requestor, property, utf8_atom, 8,
                     PropModeReplace, sel, xs_strlen(sel),
                     selection, time, mparent);
}

/*
 * handle_delete (display, requestor, property)
 *
 * Handle a DELETE request.
 */
static HandleResult
handle_delete (Display * display, Window requestor, Atom property)
{
  XChangeProperty (display, requestor, property, null_atom, 0,
                   PropModeReplace, NULL, 0);

  return DID_DELETE;
}

/*
 * process_multiple (mt, do_parent)
 *
 * Iterate through a MultTrack until it completes, or until one of its
 * entries initiates an interated selection.
 *
 * If 'do_parent' is true, and the actions proscribed in 'mt' are
 * completed during the course of this call, then process_multiple
 * is iteratively called on mt->mparent.
 */
static HandleResult
process_multiple (MultTrack * mt, Bool do_parent)
{
  HandleResult retval = HANDLE_OK;
  unsigned long i;

  if (!mt) return retval;

  for (; mt->index < mt->length; mt->index += 2) {
    i = mt->index;
    if (mt->atoms[i] == timestamp_atom) {
      retval |= handle_timestamp (mt->display, mt->requestor, mt->atoms[i+1],
                                  mt->selection, mt->time, mt);
    } else if (mt->atoms[i] == targets_atom) {
      retval |= handle_targets (mt->display, mt->requestor, mt->atoms[i+1],
                                mt->selection, mt->time, mt);
    } else if (mt->atoms[i] == multiple_atom) {
      retval |= handle_multiple (mt->display, mt->requestor, mt->atoms[i+1],
                                 mt->sel, mt->selection, mt->time, mt);
    } else if (mt->atoms[i] == XA_STRING || mt->atoms[i] == text_atom) {
      retval |= handle_string (mt->display, mt->requestor, mt->atoms[i+1],
                               mt->sel, mt->selection, mt->time, mt);
    } else if (mt->atoms[i] == utf8_atom) {
      retval |= handle_utf8_string (mt->display, mt->requestor, mt->atoms[i+1],
                                    mt->sel, mt->selection, mt->time, mt);
    } else if (mt->atoms[i] == delete_atom) {
      retval |= handle_delete (mt->display, mt->requestor, mt->atoms[i+1]);
    } else if (mt->atoms[i] == None) {
      /* the only other thing we know to handle is None, for which we
       * do nothing. This block is, like, __so__ redundant. Welcome to
       * Over-engineering 101 :) This comment is just here to keep the
       * logic documented and separate from the 'else' block. */
    } else {
      /* for anything we don't know how to handle, we fail the conversion
       * by setting this: */
      mt->atoms[i] = None;
    }

    /* If any of the conversions failed, signify this by setting that
     * atom to None ...*/
    if (retval & HANDLE_ERR) {
      mt->atoms[i] = None;
    }
    /* ... but don't propogate HANDLE_ERR */
    retval &= (~HANDLE_ERR);

    if (retval & HANDLE_INCOMPLETE) break;
  }

  if ((retval & HANDLE_INCOMPLETE) == 0) {
    complete_multiple (mt, do_parent, retval);
  }

  return retval;
}

/*
 * continue_incr (it)
 *
 * Continue an incremental transfer of IncrTrack * it.
 *
 * NB. If the incremental transfer was part of a multiple request, this
 * function calls process_multiple with do_parent=True because it is
 * assumed we are continuing an interrupted ITER, thus we must continue
 * the multiple as its original handler did not complete.
 */
static HandleResult
continue_incr (IncrTrack * it)
{
  HandleResult retval = HANDLE_OK;

  if (it->state == S_INCR_1) {
    retval = incr_stage_1 (it);
  } else if (it->state == S_INCR_2) {
    retval = incr_stage_2 (it);
  }

  /* If that completed the INCR, deal with completion */
  if ((retval & HANDLE_INCOMPLETE) == 0) {
    complete_incr (it, retval);
  }

  return retval;
}

/*
 * handle_multiple (display, requestor, property, sel, selection, time)
 *
 * Handle a MULTIPLE request; possibly setting 'sel' if any STRING
 * requests are processed within it. Return value has DID_DELETE bit set
 * if any delete requests are processed.
 *
 * NB. This calls process_multiple with do_parent=False because it is
 * assumed we are "handling" the multiple request on behalf of a
 * multiple already in progress, or (more likely) directly off a
 * SelectionRequest event.
 */
static HandleResult
handle_multiple (Display * display, Window requestor, Atom property,
                 unsigned char * sel, Atom selection, Time time,
                 MultTrack * mparent)
{
  MultTrack * mt;
  int format;
  unsigned long bytesafter;
  HandleResult retval = HANDLE_OK;

  mt = xs_malloc (sizeof (MultTrack));

  XGetWindowProperty (display, requestor, property, 0L, 1000000,
                      False, (Atom)AnyPropertyType, &mt->property,
                      &format, &mt->length, &bytesafter,
                      (unsigned char **)&mt->atoms);

  /* Make sure we got the Atom list we want */
  if (format != 32) return HANDLE_OK;


  mt->mparent = mparent;
  mt->display = display;
  mt->requestor = requestor;
  mt->sel = sel;
  mt->selection = selection;
  mt->time = time;
  mt->index = 0;

  retval = process_multiple (mt, False);

  return retval;
}

/*
 * handle_selection_request (event, sel)
 *
 * Processes a SelectionRequest event 'event' and replies to its
 * sender appropriately, eg. with the contents of the string 'sel'.
 * Returns False if a DELETE request is processed, indicating to
 * the calling function to delete the corresponding selection.
 * Returns True otherwise.
 */
static Bool
handle_selection_request (XEvent event, unsigned char * sel)
{
  XSelectionRequestEvent * xsr = &event.xselectionrequest;
  XSelectionEvent ev;
  HandleResult hr = HANDLE_OK;
  Bool retval = True;

  print_debug (D_TRACE, "handle_selection_request, property=0x%x (%s), target=0x%x (%s)",
               xsr->property, get_atom_name (xsr->property),
               xsr->target, get_atom_name (xsr->target));

  /* Prepare a SelectionNotify event to send, either as confirmation of
   * placing the selection in the requested property, or as notification
   * that this could not be performed. */
  ev.type = SelectionNotify;
  ev.display = xsr->display;
  ev.requestor = xsr->requestor;
  ev.selection = xsr->selection;
  ev.time = xsr->time;
  ev.target = xsr->target;

  if (xsr->property == None && ev.target != multiple_atom) {
      /* Obsolete requestor */
      xsr->property = xsr->target;
  }

  if (ev.time != CurrentTime && ev.time < timestamp) {
    /* If the time is outside the period we have owned the selection,
     * which is any time later than timestamp, or if the requested target
     * is not a string, then refuse the SelectionRequest. NB. Some broken
     * clients don't set a valid timestamp, so we have to check against
     * CurrentTime here. */
    ev.property = None;
  } else if (ev.target == timestamp_atom) {
    /* Return timestamp used to acquire ownership if target is TIMESTAMP */
    ev.property = xsr->property;
    hr = handle_timestamp (ev.display, ev.requestor, ev.property,
                           ev.selection, ev.time, NULL);
  } else if (ev.target == targets_atom) {
    /* Return a list of supported targets (TARGETS)*/
    ev.property = xsr->property;
    hr = handle_targets (ev.display, ev.requestor, ev.property,
                         ev.selection, ev.time, NULL);
  } else if (ev.target == multiple_atom) {
    if (xsr->property == None) { /* Invalid MULTIPLE request */
      ev.property = None;
    } else {
      /* Handle MULTIPLE request */
      hr = handle_multiple (ev.display, ev.requestor, ev.property, sel,
                            ev.selection, ev.time, NULL);
    }
  } else if (ev.target == XA_STRING || ev.target == text_atom) {
    /* Received STRING or TEXT request */
    ev.property = xsr->property;
    hr = handle_string (ev.display, ev.requestor, ev.property, sel,
                        ev.selection, ev.time, NULL);
  } else if (ev.target == utf8_atom) {
    /* Received UTF8_STRING request */
    ev.property = xsr->property;
    hr = handle_utf8_string (ev.display, ev.requestor, ev.property, sel,
                             ev.selection, ev.time, NULL);
  } else if (ev.target == delete_atom) {
    /* Received DELETE request */
    ev.property = xsr->property;
    hr = handle_delete (ev.display, ev.requestor, ev.property);
    retval = False;
  } else {
    /* Cannot convert to requested target. This includes most non-string
     * datatypes, and INSERT_SELECTION, INSERT_PROPERTY */
    ev.property = None;
  }

  /* Return False if a DELETE was processed */
  retval = (hr & DID_DELETE) ? False : True;

  /* If there was an error in the transfer, it should be refused */
  if (hr & HANDLE_ERR) {
    print_debug (D_TRACE, "Error in transfer");
    ev.property = None;
  }

  if ((hr & HANDLE_INCOMPLETE) == 0) {
    if (ev.property == None) {print_debug (D_TRACE, "Refusing conversion");}
    else { print_debug (D_TRACE, "Confirming conversion");}

    XSendEvent (display, ev.requestor, False,
                (unsigned long)NULL, (XEvent *)&ev);

    /* If we return False here, we may quit immediately, so sync out the
     * X queue. */
    if (!retval) XSync (display, False);
  }

  return retval;
}

/*
 * set_selection (selection, sel)
 *
 * Takes ownership of the selection 'selection', then loops waiting for
 * its SelectionClear or SelectionRequest events.
 *
 * Handles SelectionRequest events, first checking for additional
 * input if the user has specified 'follow' mode. Returns when a
 * SelectionClear event is received for the specified selection.
 */
static void
set_selection (Atom selection, unsigned char * sel)
{
  XEvent event;
  IncrTrack * it;

  if (own_selection (selection) == False) return;

  for (;;) {
    /* Flush before unblocking signals so we send replies before exiting */
    XFlush (display);
    unblock_exit_sigs ();
    XNextEvent (display, &event);
    block_exit_sigs ();

    switch (event.type) {
    case SelectionClear:
      if (event.xselectionclear.selection == selection) return;
      break;
    case SelectionRequest:
      if (event.xselectionrequest.selection != selection) break;

      if (do_follow)
        sel = read_input (sel, True);
      
      if (!handle_selection_request (event, sel)) return;
      
      break;
    case PropertyNotify:
      if (event.xproperty.state != PropertyDelete) break;

      it = find_incrtrack (event.xproperty.atom);

      if (it != NULL) {
        continue_incr (it);
      }

      break;
    default:
      break;
    }
  }
}

/*
 * set_selection__daemon (selection, sel)
 *
 * Creates a daemon process to handle selection requests for the
 * specified selection 'selection', to respond with selection text 'sel'.
 * If 'sel' is an empty string (NULL or "") then no daemon process is
 * created and the specified selection is cleared instead.
 */
static void
set_selection__daemon (Atom selection, unsigned char * sel)
{
  if (empty_string (sel) && !do_follow) {
    clear_selection (selection);
    return;
  }

  become_daemon ();

  set_selection (selection, sel);
}

/*
 * set_selection_pair (sel_p, sel_s)
 *
 * Handles SelectionClear and SelectionRequest events for both the
 * primary and secondary selections. Returns once SelectionClear events
 * have been received for both selections. Responds to SelectionRequest
 * events for the primary selection with text 'sel_p' and for the
 * secondary selection with text 'sel_s'.
 */
static void
set_selection_pair (unsigned char * sel_p, unsigned char * sel_s)
{
  XEvent event;
  IncrTrack * it;
  
  if (sel_p) {
    if (own_selection (XA_PRIMARY) == False)
      free_string (sel_p);
  } else {
    clear_selection (XA_PRIMARY);
  }

  if (sel_s) {
    if (own_selection (XA_SECONDARY) == False)
      free_string (sel_s);
  } else {
    clear_selection (XA_SECONDARY);
  }

  for (;;) {
    /* Flush before unblocking signals so we send replies before exiting */
    XFlush (display);
    unblock_exit_sigs ();
    XNextEvent (display, &event);
    block_exit_sigs ();

    switch (event.type) {
    case SelectionClear:
      if (event.xselectionclear.selection == XA_PRIMARY) {
        free_string (sel_p);
        if (sel_s == NULL) return;
      } else if (event.xselectionclear.selection == XA_SECONDARY) {
        free_string (sel_s);
        if (sel_p == NULL) return;
      }
      break;
    case SelectionRequest:
      if (event.xselectionrequest.selection == XA_PRIMARY) {
        if (!handle_selection_request (event, sel_p)) {
          free_string (sel_p);
          if (sel_s == NULL) return;
        }
      } else if (event.xselectionrequest.selection == XA_SECONDARY) {
        if (!handle_selection_request (event, sel_s)) {
          free_string (sel_s);
          if (sel_p == NULL) return;
        }
      }
      break;
    case PropertyNotify:
      if (event.xproperty.state != PropertyDelete) break;

      it = find_incrtrack (event.xproperty.atom);

      if (it != NULL) {
        continue_incr (it);
      }
      break;
    default:
      break;
    }
  }
}

/*
 * set_selection_pair__daemon (sel_p, sel_s)
 *
 * Creates a daemon process to handle selection requests for both the
 * primary and secondary selections with texts 'sel_p' and 'sel_s'
 * respectively.
 *
 * If both 'sel_p' and 'sel_s' are empty strings (NULL or "") then no
 * daemon process is created, and both selections are cleared instead.
 */
static void
set_selection_pair__daemon (unsigned char * sel_p, unsigned char * sel_s)
{
  if (empty_string (sel_p) && empty_string (sel_s)) {
    clear_selection (XA_PRIMARY);
    clear_selection (XA_SECONDARY);
    return;
  }

  become_daemon ();

  set_selection_pair (sel_p, sel_s);
}

/*
 * keep_selections ()
 *
 * Takes ownership of both the primary and secondary selections. The current
 * selection texts are retrieved and a new daemon process is created to
 * handle both selections unmodified.
 */
static void
keep_selections (void)
{
  unsigned char * text1, * text2;

  text1 = get_selection_text (XA_PRIMARY);
  text2 = get_selection_text (XA_SECONDARY);

  set_selection_pair__daemon (text1, text2);
}

/*
 * exchange_selections ()
 *
 * Exchanges the primary and secondary selections. The current selection
 * texts are retrieved and a new daemon process is created to handle both
 * selections with their texts exchanged.
 */
static void
exchange_selections (void)
{
  unsigned char * text1, * text2;

  text1 = get_selection_text (XA_PRIMARY);
  text2 = get_selection_text (XA_SECONDARY);

  set_selection_pair__daemon (text2, text1);
}

/*
 * free_saved_argv ()
 *
 * atexit function for freeing argv, after it has been relocated to the
 * heap.
 */
static void
free_saved_argv (void)
{
  int i;

  for (i=0; i < saved_argc; i++) {
    free (saved_argv[i]);
  }
  free (saved_argv);
}

/*
 * expand_argv (&argc, &argv)
 *
 * Explodes single letter options so that the argument parser can see
 * all of them. Relocates argv and all arguments to the heap.
 */
static void 
expand_argv(int * argc, char **argv[])
{
  int i, new_i, arglen, new_argc = *argc;
  char ** new_argv;
  char * arg;
 
  /* Calculate new argc */
  for (i = 0; i < *argc; i++) {
    arglen = strlen((*argv)[i]);
    /* An option we need to expand? */
    if ((arglen > 2) && (*argv)[i][0] == '-' && (*argv)[i][1] != '-')
      new_argc += arglen-2;
  }

  /* Allocate new_argv */
  new_argv = xs_malloc (new_argc * sizeof(char *));

  /* Copy args into new argv */
  for (i = 0, new_i = 0; i < *argc; i++) {
    arglen = strlen((*argv)[i]);
   
    /* An option we need to expand? */
    if ((arglen > 2)
	&& (*argv)[i][0] == '-' && (*argv)[i][1] != '-') {
      /* Make each letter a new argument. */

      char * c = ((*argv)[i] + 1);
     
      while (*c != '\0') {
	arg = xs_malloc(sizeof(char) * 3);
	arg[0] = '-';
	arg[1] = *c;
	arg[2] = '\0';
        new_argv[new_i++] = arg;
        c++;
      }
    } else {
      /* Simply copy the argument pointer to new_argv */
      new_argv[new_i++] = _xs_strdup ((*argv)[i]);
    }
  }

  /* Set the expected return values */
  *argc = new_argc;
  *argv = new_argv;

  /* Save the new argc, argv values and free them on exit */
  saved_argc = new_argc;
  saved_argv = new_argv;
  atexit (free_saved_argv);
}

/*
 * main (argc, argv)
 * =================
 *
 * Parse user options and set behaviour.
 *
 * By default the current selection is output and not modified if both
 * standard input and standard output are terminals (ttys). Otherwise,
 * the current selection is output if standard output is not a terminal
 * (tty), and the selection is set from standard input if standard input
 * is not a terminal (tty). If any input or output options are given then
 * the program behaves only in the requested mode.
 *
 * If both input and output is required then the previous selection is
 * output before being replaced by the contents of standard input.
 */
int
main(int argc, char *argv[])
{
  Bool show_version = False;
  Bool show_help = False;
  Bool do_append = False, do_clear = False;
  Bool do_keep = False, do_exchange = False;
  Bool do_input = False, do_output = False;
  Bool force_input = False, force_output = False;
  Bool want_clipboard = False, do_delete = False;
  Window root;
  Atom selection = XA_PRIMARY, test_atom;
  int black;
  int i, s=0;
  unsigned char * old_sel = NULL, * new_sel = NULL;
  char * display_name = NULL;
  long timeout_ms = 0L;

  progname = argv[0];

  /* Specify default behaviour based on input and output file types */
  if (isatty(0) && isatty(1)) {
    /* Solo invocation; display the selection and exit */
    do_input = False; do_output = True;
  } else {
    /* Use only what is not attached to the tty */
    /* Gives expected behaviour with *basic* usage of "xsel < foo", "xsel > foo", etc. */
    do_input = !isatty(0); do_output = !isatty(1);
  }
  /* NOTE:
   * Checking stdin/stdout for being a tty is NOT reliable to tell what the user wants.
   * This is because child processes inherit the file descriptors of their parents;
   * an xsel called in a script that is e.g. daemonized (not attached to a tty), or called
   * with a redirection or in a pipeline will have non-tty file descriptors on default.
   * The redirection/piping issue also applies to "grouped" or "compound" commands
   * in the shell (functions, subshells, curly-brace blocks, conditionals, loops, etc.).
   * In all these cases, the user *must* set the mode of operation explicitly.
   */

#define OPT(s) (strcmp (argv[i], (s)) == 0)

  /* Expand argv array before parsing to uncombine arguments. */
  expand_argv(&argc, &argv);

  /* Parse options; modify behaviour according to user-specified options */
  for (i=1; i < argc; i++) {
    if (OPT("--help") || OPT("-h")) {
      show_help = True;
    } else if (OPT("--version")) {
      show_version = True;
    } else if (OPT("--verbose") || OPT("-v")) {
      debug_level++;
    } else if (OPT("--append") || OPT("-a")) {
      force_input = True;
      do_output = False;
      do_append = True;
    } else if (OPT("--input") || OPT("-i")) {
      force_input = True;
      do_output = False;
    } else if (OPT("--clear") || OPT("-c")) {
      do_output = False;
      do_clear = True;
    } else if (OPT("--output") || OPT("-o")) {
      do_input = False;
      force_output = True;
    } else if (OPT("--follow") || OPT("-f")) {
      force_input = True;
      do_output = False;
      do_follow = True;
    } else if (OPT("--zeroflush") || OPT("-z")) {
      force_input = True;
      do_output = False;
      do_follow = True;
      do_zeroflush = True;
    } else if (OPT("--primary") || OPT("-p")) {
      selection = XA_PRIMARY;
    } else if (OPT("--secondary") || OPT("-s")) {
      selection = XA_SECONDARY;
    } else if (OPT("--clipboard") || OPT("-b")) {
      want_clipboard = True;
    } else if (OPT("--keep") || OPT("-k")) {
      do_keep = True;
    } else if (OPT("--exchange") || OPT("-x")) {
      do_exchange = True;
    } else if (OPT("--display")) {
      i++; if (i >= argc) goto usage_err;
      display_name = argv[i];
    } else if (OPT("--selectionTimeout") || OPT("-t")) {
      i++; if (i >= argc) goto usage_err;
      timeout_ms = strtol(argv[i], (char **)NULL, 10);
      if (timeout_ms < 0) timeout_ms = 0;
    } else if (OPT("--nodetach") || OPT("-n")) {
      no_daemon = True;
    } else if (OPT("--delete") || OPT("-d")) {
      do_output = False;
      do_delete = True;
    } else if (OPT("--logfile") || OPT("-l")) {
      i++; if (i >= argc) goto usage_err;
      _xs_strncpy (logfile, argv[i], MAXFNAME);
    } else {
      goto usage_err;
    }
  }

  if (show_version) {
    printf ("xsel version " VERSION " by " AUTHOR "\n");
  }

  if (show_help) {
    usage ();
  }

  if (show_version || show_help) {
    exit (0);
  }

  if (do_input || force_input) {
    if (fstat (0, &in_statbuf) == -1) {
      exit_err ("fstat error on stdin");
    }
    if (S_ISDIR(in_statbuf.st_mode)) {
      exit_err ("-: Is a directory\n");
    }
  }

  if (do_output || force_output) {
    if (fstat (1, &out_statbuf) == -1) {
      exit_err ("fstat error on stdout");
    }
    if (S_ISDIR(out_statbuf.st_mode)) {
      exit_err ("stdout: Is a directory\n");
    }
  }

  timeout = timeout_ms * 1000;

  display = XOpenDisplay (display_name);
  if (display==NULL) {
    exit_err ("Can't open display: %s\n",
              display_name ? display_name : "(null)");
  }
  root = XDefaultRootWindow (display);
  
  /* Create an unmapped window for receiving events */
  black = BlackPixel (display, DefaultScreen (display));
  window = XCreateSimpleWindow (display, root, 0, 0, 1, 1, 0, black, black);

  print_debug (D_INFO, "Window id: 0x%x (unmapped)", window);

  /* Get a timestamp */
  XSelectInput (display, window, PropertyChangeMask);
  timestamp = get_timestamp ();

  print_debug (D_OBSC, "Timestamp: %lu", timestamp);

  /* Get the maximum incremental selection size in bytes */
  /*max_req = MAX_SELECTION_INCR (display);*/
  max_req = 4000;

  print_debug (D_OBSC, "Maximum request size: %ld bytes", max_req);

  /* Consistency check */
  test_atom = XInternAtom (display, "PRIMARY", False);
  if (test_atom != XA_PRIMARY)
    print_debug (D_WARN, "XA_PRIMARY not named \"PRIMARY\"\n");
  test_atom = XInternAtom (display, "SECONDARY", False);
  if (test_atom != XA_SECONDARY)
    print_debug (D_WARN, "XA_SECONDARY not named \"SECONDARY\"\n");

  NUM_TARGETS=0;

  /* Get the TIMESTAMP atom */
  timestamp_atom = XInternAtom (display, "TIMESTAMP", False);
  supported_targets[s++] = timestamp_atom;
  NUM_TARGETS++;

  /* Get the MULTIPLE atom */
  multiple_atom = XInternAtom (display, "MULTIPLE", False);
  supported_targets[s++] = multiple_atom;
  NUM_TARGETS++;

  /* Get the TARGETS atom */
  targets_atom = XInternAtom (display, "TARGETS", False);
  supported_targets[s++] = targets_atom;
  NUM_TARGETS++;

  /* Get the DELETE atom */
  delete_atom = XInternAtom (display, "DELETE", False);
  supported_targets[s++] = delete_atom;
  NUM_TARGETS++;

  /* Get the INCR atom */
  incr_atom = XInternAtom (display, "INCR", False);
  supported_targets[s++] = incr_atom;
  NUM_TARGETS++;

  /* Get the TEXT atom */
  text_atom = XInternAtom (display, "TEXT", False);
  supported_targets[s++] = text_atom;
  NUM_TARGETS++;

  /* Get the UTF8_STRING atom */
  utf8_atom = XInternAtom (display, "UTF8_STRING", True);
  if(utf8_atom != None) {
    supported_targets[s++] = utf8_atom;
    NUM_TARGETS++;
  } else {
    utf8_atom = XA_STRING;
  }

  supported_targets[s++] = XA_STRING;
  NUM_TARGETS++;

  if (NUM_TARGETS > MAX_NUM_TARGETS) {
    exit_err ("internal error num-targets (%d) > max-num-targets (%d)\n",
              NUM_TARGETS, MAX_NUM_TARGETS);
  }

  /* Get the NULL atom */
  null_atom = XInternAtom (display, "NULL", False);

  /* Get the COMPOUND_TEXT atom.
   * NB. We do not currently serve COMPOUND_TEXT; we can retrieve it but
   * do not perform charset conversion.
   */
  compound_text_atom = XInternAtom (display, "COMPOUND_TEXT", False);

  sigemptyset (&exit_sigs);
  sigaddset (&exit_sigs, SIGALRM);
  sigaddset (&exit_sigs, SIGINT);
  sigaddset (&exit_sigs, SIGTERM);

  /* handle selection keeping and exit if so */
  if (do_keep) {
    keep_selections ();
    _exit (0);
  }

  /* handle selection exchange and exit if so */
  if (do_exchange) {
    exchange_selections ();
    _exit (0);
  }

  /* Find the "CLIPBOARD" selection if required */
  if (want_clipboard) {
    selection = XInternAtom (display, "CLIPBOARD", False);
  }

  /* handle output modes */
  if (do_output || force_output) {
    /* Get the current selection */
    old_sel = get_selection_text (selection);
    if (old_sel)
      {
         printf ("%s", old_sel);
         if (!do_append && *old_sel != '\0' && isatty(1) &&
             old_sel[xs_strlen (old_sel) - 1] != '\n')
           {
             fflush (stdout);
             fprintf (stderr, "\n\\ No newline at end of selection\n");
           }
      }
  }

  /* handle input and clear modes */
  if (do_delete) {
    get_selection (selection, delete_atom);
  } else if (do_clear) {
    clear_selection (selection);
  }
  else if (do_input || force_input) {
    if (do_output || force_output) fflush (stdout);
    if (do_append) {
      if (!old_sel) old_sel = get_selection_text (selection);
      new_sel = copy_sel (old_sel);
    }
    new_sel = initialise_read (new_sel);
    if(!do_follow)
      new_sel = read_input (new_sel, False);
    set_selection__daemon (selection, new_sel);
  }
  
  exit (0);

usage_err:
  usage ();
  exit (0);
}