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
51
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
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
113
114
115
116
117
118
119
120
121
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
180
181
185
186
190
191
192
193
196
197
198
201
202
206
207
211
212
213
214
218
219
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
243
244
248
249
270
271
275
276
277
278
279
289
290
293
294
295
296
297
298
299
300
303
306
307
308
309
320
321
322
323
324
325
326
329
330
331
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
368
369
370
371
372
373
374
375
379
380
381
382
383
384
385
386
387
388
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
444
447
448
455
456
459
460
461
462
463
464
468
473
474
475
476
477
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
504
508
509
510
511
512
513
516
521
522
523
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
555
556
560
561
562
563
564
565
566
567
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
618
619
620
621
622
629
630
641
642
650
651
652
653
654
655
656
657
658
669
670
671
672
673
675
676
677
678
682
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
730
733
734
735
736
737
738
741
744
745
757
760
761
762
766
775
776
777
778
779
780
781
782
783
784
787
788
789
790
791
792
793
794
795
796
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
823
824
829
830
831
832
833
834
837
838
839
842
845
846
847
848
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
876
877
878
879
883
884
885
886
887
888
889
894
895
896
900
901
902
903
907
908
909
912
913
914
915
916
917
918
919
920
921
922
928
931
932
933
938
939
940
941
942
943
944
945
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
967
968
969
970
971
976
977
978
979
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
1000
1001
1002
1003
1004
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1024
1025
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1071
1072
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1113
1114
1115
1122
1123
1124
1131
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1156
1157
1160
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
1196
1199
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1244
1245
1247
1248
1249
1250
1253
1254
1255
1256
1257
1258
1259
1260
1261
1277
1280
1281
1282
1283
1284
1285
1290
1297
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
1331
1332
1337
1338
1339
1342
1353
1354
1355
1356
1357
1358
1359
1360
1368
1369
1370
1371
1372
1373
1376
1377
1378
1379
1380
1386
1387
1393
1394
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1419
1434
1435
1436
1440
1441
1442
1445
1446
1447
1450
1451
1452
1453
1456
1457
1458
1459
1460
1463
1466
1467
1470
1471
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1505
1506
1507
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1529
1530
1531
1532
1533
1534
1535
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
1586
1587
1588
1589
1590
1594
1598
1599
1600
1605
1606
1607
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
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1892
1895
1898
1901
1902
1903
1904
1905
1906
1907
1908
1918
1919
1920
1921
1927
1928
1933
1934
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
2002
2003
2004
2005
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2023
2024
2025
2026
2027
2028
2029
2030
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2073
2074
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2093
2094
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
2158
2159
2160
2161
2166
2167
2168
2169
2170
2171
2172
2175
2176
2177
2178
2179
2180
2181
2184
2185
2189
2190
2191
2192
2193
2194
2201
2202
2203
2204
2209
2216
2217
2220
2221
2222
2223
2224
2225
2226
2229
2230
2231
2232
2233
2234
2235
2236
2239
2240
2241
2242
2243
2244
2245
2246
2247
2251
2252
2253
2256
2257
2258
2259
2260
2261
2262
2263
2266
2267
2268
2269
2270
2271
2272
2275
2276
2277
2278
2279
2280
2281
2288
2291
2292
2293
2294
2295
2299
2313
2324
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2345
2346
2347
2350
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
/* ... */
#include "jimautoconf.h"
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
5 includes#ifdef HAVE_UNISTD_H
#include <unistd.h>
#include <sys/stat.h>
/* ... */#endif
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#include "jim.h"
#include "jimiocompat.h"
#if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_SELECT) && defined(HAVE_NETINET_IN_H) && defined(HAVE_NETDB_H) && defined(HAVE_ARPA_INET_H)
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
5 includes#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#define HAVE_SOCKETS
/* ... */#elif defined (__MINGW32__)
#else
#define JIM_ANSIC
#endif
#if defined(JIM_SSL)
#include <openssl/ssl.h>
#include <openssl/err.h>
/* ... */#endif
#ifdef HAVE_TERMIOS_H
#include <jim-tty.h>
#endif
#include "jim-eventloop.h"
#include "jim-subcmd.h"
#define AIO_CMD_LEN 32
#define AIO_BUF_LEN 256
#ifndef HAVE_FTELLO
#define ftello ftell
#endif
#ifndef HAVE_FSEEKO
#define fseeko fseek
#endif
#define AIO_KEEPOPEN 1
#define AIO_NODELETE 2
#define AIO_EOF 4
#if defined(JIM_IPV6)
#define IPV6 1
#else
#define IPV6 0
#ifndef PF_INET6
#define PF_INET6 0
#endif/* ... */
#endif
#if defined(HAVE_SYS_UN_H) && defined(PF_UNIX)
#define UNIX_SOCKETS 1
#else
#define UNIX_SOCKETS 0
#endif
#ifdef JIM_ANSIC
#undef HAVE_PIPE
#undef HAVE_SOCKETPAIR/* ... */
#endif
#if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
union sockaddr_any {
struct sockaddr sa;
struct sockaddr_in sin;
#if IPV6
struct sockaddr_in6 sin6;
#endif
#if UNIX_SOCKETS
struct sockaddr_un sun;
#endif
...};
#ifndef HAVE_INET_NTOP
const char *inet_ntop(int af, const void *src, char *dst, int size)
{
if (af != PF_INET) {
return NULL;
}if (af != PF_INET) { ... }
snprintf(dst, size, "%s", inet_ntoa(((struct sockaddr_in *)src)->sin_addr));
return dst;
}inet_ntop (int af, const void *src, char *dst, int size) { ... }
/* ... */#endif/* ... */
#endif
struct AioFile;
typedef struct {
int (*writer)(struct AioFile *af, const char *buf, int len);
int (*reader)(struct AioFile *af, char *buf, int len);
const char *(*getline)(struct AioFile *af, char *buf, int len);
int (*error)(const struct AioFile *af);
const char *(*strerror)(struct AioFile *af);
int (*verify)(struct AioFile *af);
int (*eof)(struct AioFile *af);
int (*pending)(struct AioFile *af);
...} JimAioFopsType;
typedef struct AioFile
{
FILE *fp;
Jim_Obj *filename;
int type;
int flags;
int fd;
int addr_family;
void *ssl;
const JimAioFopsType *fops;
...} AioFile;
static int stdio_writer(struct AioFile *af, const char *buf, int len)
{
return fwrite(buf, 1, len, af->fp);
}{ ... }
static int stdio_reader(struct AioFile *af, char *buf, int len)
{
return fread(buf, 1, len, af->fp);
}{ ... }
static const char *stdio_getline(struct AioFile *af, char *buf, int len)
{
return fgets(buf, len, af->fp);
}{ ... }
static int stdio_error(const AioFile *af)
{
if (!ferror(af->fp)) {
return JIM_OK;
}if (!ferror(af->fp)) { ... }
clearerr(af->fp);
if (feof(af->fp) || errno == EAGAIN || errno == EINTR) {
return JIM_OK;
}if (feof(af->fp) || errno == EAGAIN || errno == EINTR) { ... }
#ifdef ECONNRESET
if (errno == ECONNRESET) {
return JIM_OK;
}if (errno == ECONNRESET) { ... }
/* ... */#endif
#ifdef ECONNABORTED
if (errno == ECONNABORTED) {
return JIM_OK;
}if (errno == ECONNABORTED) { ... }
/* ... */#endif
return JIM_ERR;
}{ ... }
static const char *stdio_strerror(struct AioFile *af)
{
return strerror(errno);
}{ ... }
static int stdio_eof(struct AioFile *af)
{
return feof(af->fp);
}{ ... }
static const JimAioFopsType stdio_fops = {
stdio_writer,
stdio_reader,
stdio_getline,
stdio_error,
stdio_strerror,
NULL,
stdio_eof,
NULL,
...};
#if defined(JIM_SSL) && !defined(JIM_BOOTSTRAP)
static SSL_CTX *JimAioSslCtx(Jim_Interp *interp);
static int ssl_writer(struct AioFile *af, const char *buf, int len)
{
return SSL_write(af->ssl, buf, len);
}ssl_writer (struct AioFile *af, const char *buf, int len) { ... }
static int ssl_pending(struct AioFile *af)
{
return SSL_pending(af->ssl);
}ssl_pending (struct AioFile *af) { ... }
static int ssl_reader(struct AioFile *af, char *buf, int len)
{
int ret = SSL_read(af->ssl, buf, len);
switch (SSL_get_error(af->ssl, ret)) {
case SSL_ERROR_NONE:
return ret;case SSL_ERROR_NONE:
case SSL_ERROR_SYSCALL:
case SSL_ERROR_ZERO_RETURN:
if (errno != EAGAIN) {
af->flags |= AIO_EOF;
}if (errno != EAGAIN) { ... }
return 0;case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SSL:
default:
if (errno == EAGAIN) {
return 0;
}if (errno == EAGAIN) { ... }
af->flags |= AIO_EOF;
return -1;default
}switch (SSL_get_error(af->ssl, ret)) { ... }
}ssl_reader (struct AioFile *af, char *buf, int len) { ... }
static int ssl_eof(struct AioFile *af)
{
return (af->flags & AIO_EOF);
}ssl_eof (struct AioFile *af) { ... }
static const char *ssl_getline(struct AioFile *af, char *buf, int len)
{
size_t i;
for (i = 0; i < len - 1 && !ssl_eof(af); i++) {
int ret = ssl_reader(af, &buf[i], 1);
if (ret != 1) {
break;
}if (ret != 1) { ... }
if (buf[i] == '\n') {
i++;
break;
}if (buf[i] == '\n') { ... }
}for (i = 0; i < len - 1 && !ssl_eof(af); i++) { ... }
buf[i] = '\0';
if (i == 0 && ssl_eof(af)) {
return NULL;
}if (i == 0 && ssl_eof(af)) { ... }
return buf;
}ssl_getline (struct AioFile *af, char *buf, int len) { ... }
static int ssl_error(const struct AioFile *af)
{
int ret = SSL_get_error(af->ssl, 0);
if (ret == SSL_ERROR_ZERO_RETURN || ret == SSL_ERROR_NONE) {
return JIM_OK;
}if (ret == SSL_ERROR_ZERO_RETURN || ret == SSL_ERROR_NONE) { ... }
if (ret == SSL_ERROR_SYSCALL) {
return stdio_error(af);
}if (ret == SSL_ERROR_SYSCALL) { ... }
return JIM_ERR;
}ssl_error (const struct AioFile *af) { ... }
static const char *ssl_strerror(struct AioFile *af)
{
int err = ERR_get_error();
if (err) {
return ERR_error_string(err, NULL);
}if (err) { ... }
else {
return stdio_strerror(af);
}else { ... }
}ssl_strerror (struct AioFile *af) { ... }
static int ssl_verify(struct AioFile *af)
{
X509 *cert;
cert = SSL_get_peer_certificate(af->ssl);
if (!cert) {
return JIM_ERR;
}if (!cert) { ... }
X509_free(cert);
if (SSL_get_verify_result(af->ssl) == X509_V_OK) {
return JIM_OK;
}if (SSL_get_verify_result(af->ssl) == X509_V_OK) { ... }
return JIM_ERR;
}ssl_verify (struct AioFile *af) { ... }
static const JimAioFopsType ssl_fops = {
ssl_writer,
ssl_reader,
ssl_getline,
ssl_error,
ssl_strerror,
ssl_verify,
ssl_eof,
ssl_pending,
...};/* ... */
#endif
static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
static AioFile *JimMakeChannel(Jim_Interp *interp, FILE *fh, int fd, Jim_Obj *filename,
const char *hdlfmt, int family, const char *mode, int flags);
#if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
#ifndef HAVE_GETADDRINFO
/* ... */
struct addrinfo {
int ai_family;
socklen_t ai_addrlen;
struct sockaddr *ai_addr;
union sockaddr_any ai_storage;
...};
static int getaddrinfo(const char *hostname, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
struct hostent *he;
char *end;
unsigned long port = strtoul(servname, &end, 10);
if (port == 0 || port > 65536 || *end) {
errno = EINVAL;
return -1;
}if (port == 0 || port > 65536 || *end) { ... }
if ((he = gethostbyname(hostname)) != NULL) {
int i;
for (i = 0; he->h_addr_list[i]; i++) {
if (he->h_addrtype == hints->ai_family) {
struct addrinfo *ai = malloc(sizeof(*ai));
memset(ai, 0, sizeof(*ai));
ai->ai_family = he->h_addrtype;
ai->ai_addr = &ai->ai_storage.sa;
if (ai->ai_family == PF_INET) {
ai->ai_addrlen = sizeof(ai->ai_storage.sin);
ai->ai_storage.sin.sin_family = he->h_addrtype;
assert(sizeof(ai->ai_storage.sin.sin_addr) == he->h_length);
memcpy(&ai->ai_storage.sin.sin_addr, he->h_addr_list[i], he->h_length);
ai->ai_storage.sin.sin_port = htons(port);
}if (ai->ai_family == PF_INET) { ... }
#if IPV6
else {
ai->ai_addrlen = sizeof(ai->ai_storage.sin6);
ai->ai_storage.sin6.sin6_family = he->h_addrtype;
assert(sizeof(ai->ai_storage.sin6.sin6_addr) == he->h_length);
memcpy(&ai->ai_storage.sin6.sin6_addr, he->h_addr_list[i], he->h_length);
ai->ai_storage.sin6.sin6_port = htons(port);
}else { ... }
/* ... */#endif
*res = ai;
return 0;
}if (he->h_addrtype == hints->ai_family) { ... }
}for (i = 0; he->h_addr_list[i]; i++) { ... }
}if ((he = gethostbyname(hostname)) != NULL) { ... }
return ENOENT;
}getaddrinfo (const char *hostname, const char *servname, const struct addrinfo *hints, struct addrinfo **res) { ... }
static void freeaddrinfo(struct addrinfo *ai)
{
free(ai);
}freeaddrinfo (struct addrinfo *ai) { ... }
/* ... */#endif
static int JimParseIPv6Address(Jim_Interp *interp, int socktype, const char *hostport, union sockaddr_any *sa, socklen_t *salen)
{
#if IPV6
/* ... */
char *sthost = NULL;
const char *stport;
int ret = JIM_OK;
struct addrinfo req;
struct addrinfo *ai;
stport = strrchr(hostport, ':');
if (!stport) {
stport = hostport;
hostport = "::";
sthost = Jim_StrDup(hostport);
}if (!stport) { ... }
else {
stport++;
}else { ... }
if (*hostport == '[') {
char *pt = strchr(++hostport, ']');
if (pt) {
sthost = Jim_StrDupLen(hostport, pt - hostport);
}if (pt) { ... }
}if (*hostport == '[') { ... }
if (!sthost) {
sthost = Jim_StrDupLen(hostport, stport - hostport - 1);
}if (!sthost) { ... }
memset(&req, '\0', sizeof(req));
req.ai_family = PF_INET6;
req.ai_socktype = socktype;
if (getaddrinfo(sthost, stport, &req, &ai)) {
Jim_SetResultFormatted(interp, "Not a valid address: %s:%s", sthost, stport);
ret = JIM_ERR;
}if (getaddrinfo(sthost, stport, &req, &ai)) { ... }
else {
memcpy(&sa->sin6, ai->ai_addr, ai->ai_addrlen);
*salen = ai->ai_addrlen;
freeaddrinfo(ai);
}else { ... }
Jim_Free(sthost);
return ret;/* ... */
#else
Jim_SetResultString(interp, "ipv6 not supported", -1);
return JIM_ERR;/* ... */
#endif
}JimParseIPv6Address (Jim_Interp *interp, int socktype, const char *hostport, union sockaddr_any *sa, socklen_t *salen) { ... }
static int JimParseIpAddress(Jim_Interp *interp, int socktype, const char *hostport, union sockaddr_any *sa, socklen_t *salen)
{
/* ... */
char *sthost = NULL;
const char *stport;
int ret = JIM_OK;
struct addrinfo req;
struct addrinfo *ai;
stport = strrchr(hostport, ':');
if (!stport) {
stport = hostport;
sthost = Jim_StrDup("0.0.0.0");
}if (!stport) { ... }
else {
sthost = Jim_StrDupLen(hostport, stport - hostport);
stport++;
}else { ... }
memset(&req, '\0', sizeof(req));
req.ai_family = PF_INET;
req.ai_socktype = socktype;
if (getaddrinfo(sthost, stport, &req, &ai)) {
ret = JIM_ERR;
}if (getaddrinfo(sthost, stport, &req, &ai)) { ... }
else {
memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen);
*salen = ai->ai_addrlen;
freeaddrinfo(ai);
}else { ... }
Jim_Free(sthost);
if (ret != JIM_OK) {
Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport);
}if (ret != JIM_OK) { ... }
return ret;
}JimParseIpAddress (Jim_Interp *interp, int socktype, const char *hostport, union sockaddr_any *sa, socklen_t *salen) { ... }
#if UNIX_SOCKETS
static int JimParseDomainAddress(Jim_Interp *interp, const char *path, union sockaddr_any *sa, socklen_t *salen)
{
sa->sun.sun_family = PF_UNIX;
snprintf(sa->sun.sun_path, sizeof(sa->sun.sun_path), "%s", path);
*salen = strlen(sa->sun.sun_path) + 1 + sizeof(sa->sun.sun_family);
return JIM_OK;
}JimParseDomainAddress (Jim_Interp *interp, const char *path, union sockaddr_any *sa, socklen_t *salen) { ... }
/* ... */#endif
static int JimParseSocketAddress(Jim_Interp *interp, int family, int socktype, const char *addr, union sockaddr_any *sa, socklen_t *salen)
{
switch (family) {
#if UNIX_SOCKETS
case PF_UNIX:
return JimParseDomainAddress(interp, addr, sa, salen);/* ... */
#endifcase PF_UNIX:
case PF_INET6:
return JimParseIPv6Address(interp, socktype, addr, sa, salen);case PF_INET6:
case PF_INET:
return JimParseIpAddress(interp, socktype, addr, sa, salen);case PF_INET:
}switch (family) { ... }
return JIM_ERR;
}JimParseSocketAddress (Jim_Interp *interp, int family, int socktype, const char *addr, union sockaddr_any *sa, socklen_t *salen) { ... }
/* ... */
static Jim_Obj *JimFormatSocketAddress(Jim_Interp *interp, const union sockaddr_any *sa, socklen_t salen)
{
char addrbuf[60];
const char *addr = addrbuf;
int addrlen = -1;
switch (sa->sa.sa_family) {
#if UNIX_SOCKETS
case PF_UNIX:
addr = sa->sun.sun_path;
addrlen = salen - 1 - sizeof(sa->sun.sun_family);
if (addrlen < 0) {
addrlen = 0;
}if (addrlen < 0) { ... }
break;/* ... */
#endif
#if IPV6case PF_UNIX:
case PF_INET6:
addrbuf[0] = '[';
inet_ntop(sa->sa.sa_family, &sa->sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa->sin6.sin6_port));
break;/* ... */
#endifcase PF_INET6:
case PF_INET:
inet_ntop(sa->sa.sa_family, &sa->sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa->sin.sin_port));
break;
case PF_INET:
default:
addr = "";
break;default
}switch (sa->sa.sa_family) { ... }
return Jim_NewStringObj(interp, addr, addrlen);
}JimFormatSocketAddress (Jim_Interp *interp, const union sockaddr_any *sa, socklen_t salen) { ... }
static int JimSetVariableSocketAddress(Jim_Interp *interp, Jim_Obj *varObjPtr, const union sockaddr_any *sa, socklen_t salen)
{
int ret;
Jim_Obj *objPtr = JimFormatSocketAddress(interp, sa, salen);
Jim_IncrRefCount(objPtr);
ret = Jim_SetVariable(interp, varObjPtr, objPtr);
Jim_DecrRefCount(interp, objPtr);
return ret;
}JimSetVariableSocketAddress (Jim_Interp *interp, Jim_Obj *varObjPtr, const union sockaddr_any *sa, socklen_t salen) { ... }
static Jim_Obj *aio_sockname(Jim_Interp *interp, AioFile *af)
{
union sockaddr_any sa;
socklen_t salen = sizeof(sa);
if (getsockname(af->fd, &sa.sa, &salen) < 0) {
return NULL;
}if (getsockname(af->fd, &sa.sa, &salen) < 0) { ... }
return JimFormatSocketAddress(interp, &sa, salen);
}aio_sockname (Jim_Interp *interp, AioFile *af) { ... }
/* ... */#endif
static const char *JimAioErrorString(AioFile *af)
{
if (af && af->fops)
return af->fops->strerror(af);
return strerror(errno);
}{ ... }
static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name)
{
AioFile *af = Jim_CmdPrivData(interp);
if (name) {
Jim_SetResultFormatted(interp, "%#s: %s", name, JimAioErrorString(af));
}if (name) { ... }
else {
Jim_SetResultString(interp, JimAioErrorString(af), -1);
}else { ... }
}{ ... }
static int JimCheckStreamError(Jim_Interp *interp, AioFile *af)
{
int ret = af->fops->error(af);
if (ret) {
JimAioSetError(interp, af->filename);
}if (ret) { ... }
return ret;
}{ ... }
static void JimAioDelProc(Jim_Interp *interp, void *privData)
{
AioFile *af = privData;
JIM_NOTUSED(interp);
#if UNIX_SOCKETS
if (af->addr_family == PF_UNIX && (af->flags & AIO_NODELETE) == 0) {
Jim_Obj *filenameObj = aio_sockname(interp, af);
if (filenameObj) {
if (Jim_Length(filenameObj)) {
remove(Jim_String(filenameObj));
}if (Jim_Length(filenameObj)) { ... }
Jim_FreeNewObj(interp, filenameObj);
}if (filenameObj) { ... }
}if (af->addr_family == PF_UNIX && (af->flags & AIO_NODELETE) == 0) { ... }
/* ... */#endif
Jim_DecrRefCount(interp, af->filename);
#ifdef jim_ext_eventloop
Jim_DeleteFileHandler(interp, af->fd, JIM_EVENT_READABLE | JIM_EVENT_WRITABLE | JIM_EVENT_EXCEPTION);/* ... */
#endif
#if defined(JIM_SSL)
if (af->ssl != NULL) {
SSL_free(af->ssl);
}if (af->ssl != NULL) { ... }
/* ... */#endif
if (!(af->flags & AIO_KEEPOPEN)) {
fclose(af->fp);
}if (!(af->flags & AIO_KEEPOPEN)) { ... }
Jim_Free(af);
}{ ... }
static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
char buf[AIO_BUF_LEN];
Jim_Obj *objPtr;
int nonewline = 0;
int pending = 0;
jim_wide neededLen = -1;
static const char * const options[] = { "-pending", "-nonewline", NULL };
enum { OPT_PENDING, OPT_NONEWLINE };
int option;
if (argc) {
if (*Jim_String(argv[0]) == '-') {
if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { ... }
switch (option) {
case OPT_PENDING:
if (!af->fops->pending) {
Jim_SetResultString(interp, "-pending not supported on this connection type", -1);
return JIM_ERR;
}if (!af->fops->pending) { ... }
pending++;
break;case OPT_PENDING:
case OPT_NONEWLINE:
nonewline++;
break;case OPT_NONEWLINE:
}switch (option) { ... }
}if (*Jim_String(argv[0]) == '-') { ... }
else {
if (Jim_GetWide(interp, argv[0], &neededLen) != JIM_OK)
return JIM_ERR;
if (neededLen < 0) {
Jim_SetResultString(interp, "invalid parameter: negative len", -1);
return JIM_ERR;
}if (neededLen < 0) { ... }
}else { ... }
argc--;
argv++;
}if (argc) { ... }
if (argc) {
return -1;
}if (argc) { ... }
objPtr = Jim_NewStringObj(interp, NULL, 0);
while (neededLen != 0) {
int retval;
int readlen;
if (neededLen == -1) {
readlen = AIO_BUF_LEN;
}if (neededLen == -1) { ... }
else {
readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen);
}else { ... }
retval = af->fops->reader(af, buf, pending ? 1 : readlen);
if (retval > 0) {
Jim_AppendString(interp, objPtr, buf, retval);
if (neededLen != -1) {
neededLen -= retval;
}if (neededLen != -1) { ... }
else if (pending) {
/* ... */
neededLen = af->fops->pending(af);
}else if (pending) { ... }
}if (retval > 0) { ... }
if (retval <= 0) {
break;
}if (retval <= 0) { ... }
}while (neededLen != 0) { ... }
if (JimCheckStreamError(interp, af)) {
Jim_FreeNewObj(interp, objPtr);
return JIM_ERR;
}if (JimCheckStreamError(interp, af)) { ... }
if (nonewline) {
int len;
const char *s = Jim_GetString(objPtr, &len);
if (len > 0 && s[len - 1] == '\n') {
objPtr->length--;
objPtr->bytes[objPtr->length] = '\0';
}if (len > 0 && s[len - 1] == '\n') { ... }
}if (nonewline) { ... }
Jim_SetResult(interp, objPtr);
return JIM_OK;
}{ ... }
AioFile *Jim_AioFile(Jim_Interp *interp, Jim_Obj *command)
{
Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG);
if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) {
return (AioFile *) cmdPtr->u.native.privData;
}if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { ... }
Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command);
return NULL;
}{ ... }
FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command)
{
AioFile *af;
af = Jim_AioFile(interp, command);
if (af == NULL) {
return NULL;
}if (af == NULL) { ... }
return af->fp;
}{ ... }
static int aio_cmd_getfd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
fflush(af->fp);
Jim_SetResultInt(interp, fileno(af->fp));
return JIM_OK;
}{ ... }
static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
jim_wide count = 0;
jim_wide maxlen = JIM_WIDE_MAX;
AioFile *outf = Jim_AioFile(interp, argv[0]);
if (outf == NULL) {
return JIM_ERR;
}if (outf == NULL) { ... }
if (argc == 2) {
if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) { ... }
}if (argc == 2) { ... }
while (count < maxlen) {
char buf[AIO_BUF_LEN];
jim_wide len = maxlen - count;
if (len > sizeof(buf)) {
len = sizeof(buf);
}if (len > sizeof(buf)) { ... }
len = af->fops->reader(af, buf, len);
if (len <= 0) {
break;
}if (len <= 0) { ... }
if (outf->fops->writer(outf, buf, len) != len) {
break;
}if (outf->fops->writer(outf, buf, len) != len) { ... }
count += len;
}while (count < maxlen) { ... }
if (JimCheckStreamError(interp, af) || JimCheckStreamError(interp, outf)) {
return JIM_ERR;
}if (JimCheckStreamError(interp, af) || JimCheckStreamError(interp, outf)) { ... }
Jim_SetResultInt(interp, count);
return JIM_OK;
}{ ... }
static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
char buf[AIO_BUF_LEN];
Jim_Obj *objPtr;
int len;
errno = 0;
objPtr = Jim_NewStringObj(interp, NULL, 0);
while (1) {
buf[AIO_BUF_LEN - 1] = '_';
if (af->fops->getline(af, buf, AIO_BUF_LEN) == NULL)
break;
if (buf[AIO_BUF_LEN - 1] == '\0' && buf[AIO_BUF_LEN - 2] != '\n') {
Jim_AppendString(interp, objPtr, buf, AIO_BUF_LEN - 1);
}if (buf[AIO_BUF_LEN - 1] == '\0' && buf[AIO_BUF_LEN - 2] != '\n') { ... }
else {
len = strlen(buf);
if (len && (buf[len - 1] == '\n')) {
len--;
}if (len && (buf[len - 1] == '\n')) { ... }
Jim_AppendString(interp, objPtr, buf, len);
break;
}else { ... }
}while (1) { ... }
if (JimCheckStreamError(interp, af)) {
Jim_FreeNewObj(interp, objPtr);
return JIM_ERR;
}if (JimCheckStreamError(interp, af)) { ... }
if (argc) {
if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) {
Jim_FreeNewObj(interp, objPtr);
return JIM_ERR;
}if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) { ... }
len = Jim_Length(objPtr);
if (len == 0 && af->fops->eof(af)) {
len = -1;
}if (len == 0 && af->fops->eof(af)) { ... }
Jim_SetResultInt(interp, len);
}if (argc) { ... }
else {
Jim_SetResult(interp, objPtr);
}else { ... }
return JIM_OK;
}{ ... }
static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
int wlen;
const char *wdata;
Jim_Obj *strObj;
if (argc == 2) {
if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) {
return -1;
}if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { ... }
strObj = argv[1];
}if (argc == 2) { ... }
else {
strObj = argv[0];
}else { ... }
wdata = Jim_GetString(strObj, &wlen);
if (af->fops->writer(af, wdata, wlen) == wlen) {
if (argc == 2 || af->fops->writer(af, "\n", 1) == 1) {
return JIM_OK;
}if (argc == 2 || af->fops->writer(af, "\n", 1) == 1) { ... }
}if (af->fops->writer(af, wdata, wlen) == wlen) { ... }
JimAioSetError(interp, af->filename);
return JIM_ERR;
}{ ... }
static int aio_cmd_isatty(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
#ifdef HAVE_ISATTY
AioFile *af = Jim_CmdPrivData(interp);
Jim_SetResultInt(interp, isatty(fileno(af->fp)));/* ... */
#else
Jim_SetResultInt(interp, 0);
#endif
return JIM_OK;
}{ ... }
#if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
static int aio_cmd_recvfrom(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
char *buf;
union sockaddr_any sa;
long len;
socklen_t salen = sizeof(sa);
int rlen;
if (Jim_GetLong(interp, argv[0], &len) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetLong(interp, argv[0], &len) != JIM_OK) { ... }
buf = Jim_Alloc(len + 1);
rlen = recvfrom(fileno(af->fp), buf, len, 0, &sa.sa, &salen);
if (rlen < 0) {
Jim_Free(buf);
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (rlen < 0) { ... }
buf[rlen] = 0;
Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, rlen));
if (argc > 1) {
return JimSetVariableSocketAddress(interp, argv[1], &sa, salen);
}if (argc > 1) { ... }
return JIM_OK;
}aio_cmd_recvfrom (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
static int aio_cmd_sendto(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
int wlen;
int len;
const char *wdata;
union sockaddr_any sa;
const char *addr = Jim_String(argv[1]);
socklen_t salen;
if (JimParseSocketAddress(interp, af->addr_family, SOCK_DGRAM, addr, &sa, &salen) != JIM_OK) {
return JIM_ERR;
}if (JimParseSocketAddress(interp, af->addr_family, SOCK_DGRAM, addr, &sa, &salen) != JIM_OK) { ... }
wdata = Jim_GetString(argv[0], &wlen);
len = sendto(fileno(af->fp), wdata, wlen, 0, &sa.sa, salen);
if (len < 0) {
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (len < 0) { ... }
Jim_SetResultInt(interp, len);
return JIM_OK;
}aio_cmd_sendto (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
static int aio_cmd_accept(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
int sock;
union sockaddr_any sa;
socklen_t salen = sizeof(sa);
sock = accept(af->fd, &sa.sa, &salen);
if (sock < 0) {
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (sock < 0) { ... }
if (argc > 0) {
if (JimSetVariableSocketAddress(interp, argv[0], &sa, salen) != JIM_OK) {
return JIM_ERR;
}if (JimSetVariableSocketAddress(interp, argv[0], &sa, salen) != JIM_OK) { ... }
}if (argc > 0) { ... }
return JimMakeChannel(interp, NULL, sock, Jim_NewStringObj(interp, "accept", -1),
"aio.sockstream%ld", af->addr_family, "r+", AIO_NODELETE) ? JIM_OK : JIM_ERR;
}aio_cmd_accept (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
static int aio_cmd_sockname(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
Jim_Obj *objPtr = aio_sockname(interp, af);
if (objPtr == NULL) {
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (objPtr == NULL) { ... }
Jim_SetResult(interp, objPtr);
return JIM_OK;
}aio_cmd_sockname (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
static int aio_cmd_peername(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
union sockaddr_any sa;
socklen_t salen = sizeof(sa);
if (getpeername(af->fd, &sa.sa, &salen) < 0) {
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (getpeername(af->fd, &sa.sa, &salen) < 0) { ... }
Jim_SetResult(interp, JimFormatSocketAddress(interp, &sa, salen));
return JIM_OK;
}aio_cmd_peername (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
static int aio_cmd_listen(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
long backlog;
if (Jim_GetLong(interp, argv[0], &backlog) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetLong(interp, argv[0], &backlog) != JIM_OK) { ... }
if (listen(af->fd, backlog)) {
JimAioSetError(interp, NULL);
return JIM_ERR;
}if (listen(af->fd, backlog)) { ... }
return JIM_OK;
}aio_cmd_listen (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
/* ... */#endif
static int aio_cmd_flush(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
if (fflush(af->fp) == EOF) {
JimAioSetError(interp, af->filename);
return JIM_ERR;
}if (fflush(af->fp) == EOF) { ... }
return JIM_OK;
}{ ... }
static int aio_cmd_eof(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
Jim_SetResultInt(interp, !!af->fops->eof(af));
return JIM_OK;
}{ ... }
static int aio_cmd_close(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
if (argc == 3) {
int option = -1;
#if defined(HAVE_SOCKETS)
static const char * const options[] = { "r", "w", "-nodelete", NULL };
enum { OPT_R, OPT_W, OPT_NODELETE };
AioFile *af = Jim_CmdPrivData(interp);
if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetEnum(interp, argv[2], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { ... }
/* ... */#endif
switch (option) {
#if defined(HAVE_SHUTDOWN)
case OPT_R:
case OPT_W:
if (shutdown(af->fd, option == OPT_R ? SHUT_RD : SHUT_WR) == 0) {
return JIM_OK;
}if (shutdown(af->fd, option == OPT_R ? SHUT_RD : SHUT_WR) == 0) { ... }
JimAioSetError(interp, NULL);
return JIM_ERR;/* ... */
#endif
#if UNIX_SOCKETS
case OPT_NODELETE:
if (af->addr_family == PF_UNIX) {
af->flags |= AIO_NODELETE;
break;
}if (af->addr_family == PF_UNIX) { ... }
/* ... */
#endifcase OPT_NODELETE:
default:
Jim_SetResultString(interp, "not supported", -1);
return JIM_ERR;default
}switch (option) { ... }
}if (argc == 3) { ... }
return Jim_DeleteCommand(interp, argv[0]);
}{ ... }
static int aio_cmd_seek(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
int orig = SEEK_SET;
jim_wide offset;
if (argc == 2) {
if (Jim_CompareStringImmediate(interp, argv[1], "start"))
orig = SEEK_SET;
else if (Jim_CompareStringImmediate(interp, argv[1], "current"))
orig = SEEK_CUR;
else if (Jim_CompareStringImmediate(interp, argv[1], "end"))
orig = SEEK_END;
else {
return -1;
}else { ... }
}if (argc == 2) { ... }
if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) { ... }
if (fseeko(af->fp, offset, orig) == -1) {
JimAioSetError(interp, af->filename);
return JIM_ERR;
}if (fseeko(af->fp, offset, orig) == -1) { ... }
return JIM_OK;
}{ ... }
static int aio_cmd_tell(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
Jim_SetResultInt(interp, ftello(af->fp));
return JIM_OK;
}{ ... }
static int aio_cmd_filename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
Jim_SetResult(interp, af->filename);
return JIM_OK;
}{ ... }
#ifdef O_NDELAY
static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
int fmode = fcntl(af->fd, F_GETFL);
if (argc) {
long nb;
if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) { ... }
if (nb) {
fmode |= O_NDELAY;
}if (nb) { ... }
else {
fmode &= ~O_NDELAY;
}else { ... }
(void)fcntl(af->fd, F_SETFL, fmode);
}if (argc) { ... }
Jim_SetResultInt(interp, (fmode & O_NONBLOCK) ? 1 : 0);
return JIM_OK;
}aio_cmd_ndelay (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
/* ... */#endif
#if defined(HAVE_SOCKETS) && !defined(JIM_BOOTSTRAP)
#define SOCKOPT_BOOL 0
#define SOCKOPT_INT 1
#define SOCKOPT_TIMEVAL 2
static const struct sockopt_def {
const char *name;
int level;
int opt;
int type;
...} sockopts[] = {
#ifdef SOL_SOCKET
#ifdef SO_BROADCAST
{ "broadcast", SOL_SOCKET, SO_BROADCAST },
#endif
#ifdef SO_DEBUG
{ "debug", SOL_SOCKET, SO_DEBUG },
#endif
#ifdef SO_KEEPALIVE
{ "keepalive", SOL_SOCKET, SO_KEEPALIVE },
#endif
#ifdef SO_NOSIGPIPE
{ "nosigpipe", SOL_SOCKET, SO_NOSIGPIPE },
#endif
#ifdef SO_OOBINLINE
{ "oobinline", SOL_SOCKET, SO_OOBINLINE },
#endif
#ifdef SO_SNDBUF
{ "sndbuf", SOL_SOCKET, SO_SNDBUF, SOCKOPT_INT },
#endif
#ifdef SO_RCVBUF
{ "rcvbuf", SOL_SOCKET, SO_RCVBUF, SOCKOPT_INT },
#endif
#if 0 && defined(SO_SNDTIMEO)
{ "sndtimeo", SOL_SOCKET, SO_SNDTIMEO, SOCKOPT_TIMEVAL }defined (SO_SNDTIMEO) { ... },
#endif
#if 0 && defined(SO_RCVTIMEO)
{ "rcvtimeo", SOL_SOCKET, SO_RCVTIMEO, SOCKOPT_TIMEVAL }defined (SO_RCVTIMEO) { ... },
#endif/* ... */
#endif
#ifdef IPPROTO_TCP
#ifdef TCP_NODELAY
{ "tcp_nodelay", IPPROTO_TCP, TCP_NODELAY },
#endif/* ... */
#endif
...};
static int aio_cmd_sockopt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
size_t i;
if (argc == 0) {
Jim_Obj *dictObjPtr = Jim_NewListObj(interp, NULL, 0);
for (i = 0; i < sizeof(sockopts) / sizeof(*sockopts); i++) {
int value = 0;
socklen_t len = sizeof(value);
if (getsockopt(af->fd, sockopts[i].level, sockopts[i].opt, (void *)&value, &len) == 0) {
if (sockopts[i].type == SOCKOPT_BOOL) {
value = !!value;
}if (sockopts[i].type == SOCKOPT_BOOL) { ... }
Jim_ListAppendElement(interp, dictObjPtr, Jim_NewStringObj(interp, sockopts[i].name, -1));
Jim_ListAppendElement(interp, dictObjPtr, Jim_NewIntObj(interp, value));
}if (getsockopt(af->fd, sockopts[i].level, sockopts[i].opt, (void *)&value, &len) == 0) { ... }
}for (i = 0; i < sizeof(sockopts) / sizeof(*sockopts); i++) { ... }
Jim_SetResult(interp, dictObjPtr);
return JIM_OK;
}if (argc == 0) { ... }
if (argc == 1) {
return -1;
}if (argc == 1) { ... }
for (i = 0; i < sizeof(sockopts) / sizeof(*sockopts); i++) {
if