You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xioopen.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this is the source file of the extended open function */
  5. #include "xiosysincludes.h"
  6. #include "xioopen.h"
  7. #include "xiomodes.h"
  8. #include "nestlex.h"
  9. static xiofile_t *xioallocfd(void);
  10. xiosingle_t hugo;
  11. static xiosingle_t *xioparse_single(const char **addr);
  12. static xiofile_t *xioparse_dual(const char **addr);
  13. static int xioopen_dual(xiofile_t *xfd, int xioflags);
  14. const struct addrname addressnames[] = {
  15. #if 1
  16. #if WITH_STDIO
  17. { "-", &addr_stdio },
  18. #endif
  19. #if defined(WITH_UNIX) && defined(WITH_ABSTRACT_UNIXSOCKET)
  20. { "abstract", &xioaddr_abstract_client },
  21. { "abstract-client", &xioaddr_abstract_client },
  22. { "abstract-connect", &xioaddr_abstract_connect },
  23. #if WITH_LISTEN
  24. { "abstract-listen", &xioaddr_abstract_listen },
  25. #endif
  26. { "abstract-recv", &xioaddr_abstract_recv },
  27. { "abstract-recvfrom", &xioaddr_abstract_recvfrom },
  28. { "abstract-sendto", &xioaddr_abstract_sendto },
  29. #endif /* defined(WITH_UNIX) && defined(WITH_ABSTRACT_UNIXSOCKET) */
  30. #if WITH_CREAT
  31. { "creat", &addr_creat },
  32. { "create", &addr_creat },
  33. #endif
  34. #if WITH_GENERICSOCKET
  35. { "datagram", &xioaddr_socket_datagram },
  36. { "dgram", &xioaddr_socket_datagram },
  37. #endif
  38. #if WITH_PIPE
  39. { "echo", &addr_pipe },
  40. #endif
  41. #if WITH_EXEC
  42. { "exec", &addr_exec },
  43. #endif
  44. #if WITH_FDNUM
  45. { "fd", &addr_fd },
  46. #endif
  47. #if WITH_PIPE
  48. { "fifo", &addr_pipe },
  49. #endif
  50. #if WITH_FILE
  51. { "file", &addr_open },
  52. #endif
  53. #if WITH_GOPEN
  54. { "gopen", &addr_gopen },
  55. #endif
  56. #if WITH_INTERFACE
  57. { "if", &xioaddr_interface },
  58. #endif
  59. #if (WITH_IP4 || WITH_IP6) && WITH_TCP
  60. { "inet", &addr_tcp_connect },
  61. #endif
  62. #if (WITH_IP4 || WITH_IP6) && WITH_TCP && WITH_LISTEN
  63. { "inet-l", &addr_tcp_listen },
  64. { "inet-listen", &addr_tcp_listen },
  65. #endif
  66. #if WITH_IP4 && WITH_TCP
  67. { "inet4", &addr_tcp4_connect },
  68. #endif
  69. #if WITH_IP4 && WITH_TCP && WITH_LISTEN
  70. { "inet4-l", &addr_tcp4_listen },
  71. { "inet4-listen", &addr_tcp4_listen },
  72. #endif
  73. #if WITH_IP6 && WITH_TCP
  74. { "inet6", &addr_tcp6_connect },
  75. #endif
  76. #if WITH_IP6 && WITH_TCP && WITH_LISTEN
  77. { "inet6-l", &addr_tcp6_listen },
  78. { "inet6-listen", &addr_tcp6_listen },
  79. #endif
  80. #if WITH_INTERFACE
  81. { "interface", &xioaddr_interface },
  82. #endif
  83. #if WITH_RAWIP
  84. #if (WITH_IP4 || WITH_IP6)
  85. { "ip", &addr_rawip_sendto },
  86. { "ip-datagram", &addr_rawip_datagram },
  87. { "ip-dgram", &addr_rawip_datagram },
  88. { "ip-recv", &addr_rawip_recv },
  89. { "ip-recvfrom", &addr_rawip_recvfrom },
  90. { "ip-send", &addr_rawip_sendto },
  91. { "ip-sendto", &addr_rawip_sendto },
  92. #endif
  93. #if WITH_IP4
  94. { "ip4", &addr_rawip4_sendto },
  95. { "ip4-datagram", &addr_rawip4_datagram },
  96. { "ip4-dgram", &addr_rawip4_datagram },
  97. { "ip4-recv", &addr_rawip4_recv },
  98. { "ip4-recvfrom", &addr_rawip4_recvfrom },
  99. { "ip4-send", &addr_rawip4_sendto },
  100. { "ip4-sendto", &addr_rawip4_sendto },
  101. #endif
  102. #if WITH_IP6
  103. { "ip6", &addr_rawip6_sendto },
  104. { "ip6-datagram", &addr_rawip6_datagram },
  105. { "ip6-dgram", &addr_rawip6_datagram },
  106. { "ip6-recv", &addr_rawip6_recv },
  107. { "ip6-recvfrom", &addr_rawip6_recvfrom },
  108. { "ip6-send", &addr_rawip6_sendto },
  109. { "ip6-sendto", &addr_rawip6_sendto },
  110. #endif
  111. #endif /* WITH_RAWIP */
  112. #if WITH_UNIX
  113. { "local", &xioaddr_unix_connect },
  114. #endif
  115. #if WITH_FILE
  116. { "open", &addr_open },
  117. #endif
  118. #if WITH_OPENSSL
  119. { "openssl", &addr_openssl },
  120. { "openssl-connect", &addr_openssl },
  121. #if WITH_LISTEN
  122. { "openssl-listen", &addr_openssl_listen },
  123. #endif
  124. #endif
  125. #if WITH_PIPE
  126. { "pipe", &addr_pipe },
  127. #endif
  128. #if WITH_PROXY
  129. { "proxy", &addr_proxy_connect },
  130. { "proxy-connect", &addr_proxy_connect },
  131. #endif
  132. #if WITH_PTY
  133. { "pty", &addr_pty },
  134. #endif
  135. #if WITH_READLINE
  136. { "readline", &addr_readline },
  137. #endif
  138. #if (WITH_IP4 || WITH_IP6) && WITH_SCTP
  139. { "sctp", &addr_sctp_connect },
  140. { "sctp-connect", &addr_sctp_connect },
  141. #if WITH_LISTEN
  142. { "sctp-l", &addr_sctp_listen },
  143. { "sctp-listen", &addr_sctp_listen },
  144. #endif
  145. #if WITH_IP4
  146. { "sctp4", &addr_sctp4_connect },
  147. { "sctp4-connect", &addr_sctp4_connect },
  148. #if WITH_LISTEN
  149. { "sctp4-l", &addr_sctp4_listen },
  150. { "sctp4-listen", &addr_sctp4_listen },
  151. #endif
  152. #endif /* WITH_IP4 */
  153. #if WITH_IP6
  154. { "sctp6", &addr_sctp6_connect },
  155. { "sctp6-connect", &addr_sctp6_connect },
  156. #if WITH_LISTEN
  157. { "sctp6-l", &addr_sctp6_listen },
  158. { "sctp6-listen", &addr_sctp6_listen },
  159. #endif
  160. #endif /* WITH_IP6 */
  161. #endif /* (WITH_IP4 || WITH_IP6) && WITH_SCTP */
  162. #if WITH_GENERICSOCKET
  163. { "sendto", &xioaddr_socket_sendto },
  164. #endif
  165. #if WITH_GENERICSOCKET
  166. { "socket-connect", &xioaddr_socket_connect },
  167. { "socket-datagram", &xioaddr_socket_datagram },
  168. #if WITH_LISTEN
  169. { "socket-listen", &xioaddr_socket_listen },
  170. #endif /* WITH_LISTEN */
  171. { "socket-recv", &xioaddr_socket_recv },
  172. { "socket-recvfrom", &xioaddr_socket_recvfrom },
  173. { "socket-sendto", &xioaddr_socket_sendto },
  174. #endif
  175. #if WITH_SOCKS4
  176. { "socks", &addr_socks4_connect },
  177. { "socks4", &addr_socks4_connect },
  178. #endif
  179. #if WITH_SOCKS4A
  180. { "socks4a", &addr_socks4a_connect },
  181. #endif
  182. #if WITH_OPENSSL
  183. { "ssl", &addr_openssl },
  184. #if WITH_LISTEN
  185. { "ssl-l", &addr_openssl_listen },
  186. #endif
  187. #endif
  188. #if WITH_STDIO
  189. { "stderr", &addr_stderr },
  190. { "stdin", &addr_stdin },
  191. { "stdio", &addr_stdio },
  192. { "stdout", &addr_stdout },
  193. #endif
  194. #if WITH_SYSTEM
  195. { "system", &addr_system },
  196. #endif
  197. #if (WITH_IP4 || WITH_IP6) && WITH_TCP
  198. { "tcp", &addr_tcp_connect },
  199. { "tcp-connect", &addr_tcp_connect },
  200. #endif
  201. #if (WITH_IP4 || WITH_IP6) && WITH_TCP && WITH_LISTEN
  202. { "tcp-l", &addr_tcp_listen },
  203. { "tcp-listen", &addr_tcp_listen },
  204. #endif
  205. #if WITH_IP4 && WITH_TCP
  206. { "tcp4", &addr_tcp4_connect },
  207. { "tcp4-connect", &addr_tcp4_connect },
  208. #endif
  209. #if WITH_IP4 && WITH_TCP && WITH_LISTEN
  210. { "tcp4-l", &addr_tcp4_listen },
  211. { "tcp4-listen", &addr_tcp4_listen },
  212. #endif
  213. #if WITH_IP6 && WITH_TCP
  214. { "tcp6", &addr_tcp6_connect },
  215. { "tcp6-connect", &addr_tcp6_connect },
  216. #endif
  217. #if WITH_IP6 && WITH_TCP && WITH_LISTEN
  218. { "tcp6-l", &addr_tcp6_listen },
  219. { "tcp6-listen", &addr_tcp6_listen },
  220. #endif
  221. #if WITH_TUN
  222. { "tun", &xioaddr_tun },
  223. #endif
  224. #if (WITH_IP4 || WITH_IP6) && WITH_UDP
  225. { "udp", &addr_udp_connect },
  226. #endif
  227. #if (WITH_IP4 || WITH_IP6) && WITH_UDP
  228. { "udp-connect", &addr_udp_connect },
  229. { "udp-datagram", &addr_udp_datagram },
  230. { "udp-dgram", &addr_udp_datagram },
  231. #endif
  232. #if (WITH_IP4 || WITH_IP6) && WITH_UDP && WITH_LISTEN
  233. { "udp-l", &addr_udp_listen },
  234. { "udp-listen", &addr_udp_listen },
  235. #endif
  236. #if (WITH_IP4 || WITH_IP6) && WITH_UDP
  237. { "udp-recv", &addr_udp_recv },
  238. { "udp-recvfrom", &addr_udp_recvfrom },
  239. { "udp-send", &addr_udp_sendto },
  240. { "udp-sendto", &addr_udp_sendto },
  241. #endif
  242. #if WITH_IP4 && WITH_UDP
  243. { "udp4", &addr_udp4_connect },
  244. { "udp4-connect", &addr_udp4_connect },
  245. { "udp4-datagram", &addr_udp4_datagram },
  246. { "udp4-dgram", &addr_udp4_datagram },
  247. #endif
  248. #if WITH_IP4 && WITH_UDP && WITH_LISTEN
  249. { "udp4-l", &addr_udp4_listen },
  250. { "udp4-listen", &addr_udp4_listen },
  251. #endif
  252. #if WITH_IP4 && WITH_UDP
  253. { "udp4-recv", &addr_udp4_recv },
  254. { "udp4-recvfrom", &addr_udp4_recvfrom },
  255. { "udp4-send", &addr_udp4_sendto },
  256. { "udp4-sendto", &addr_udp4_sendto },
  257. #endif
  258. #if WITH_IP6 && WITH_UDP
  259. { "udp6", &addr_udp6_connect },
  260. { "udp6-connect", &addr_udp6_connect },
  261. { "udp6-datagram", &addr_udp6_datagram },
  262. { "udp6-dgram", &addr_udp6_datagram },
  263. #endif
  264. #if WITH_IP6 && WITH_UDP && WITH_LISTEN
  265. { "udp6-l", &addr_udp6_listen },
  266. { "udp6-listen", &addr_udp6_listen },
  267. #endif
  268. #if WITH_IP6 && WITH_UDP
  269. { "udp6-recv", &addr_udp6_recv },
  270. { "udp6-recvfrom", &addr_udp6_recvfrom },
  271. { "udp6-send", &addr_udp6_sendto },
  272. { "udp6-sendto", &addr_udp6_sendto },
  273. #endif
  274. #if WITH_UNIX
  275. { "unix", &xioaddr_unix_client },
  276. { "unix-client", &xioaddr_unix_client },
  277. { "unix-connect", &xioaddr_unix_connect },
  278. #endif
  279. #if WITH_UNIX && WITH_LISTEN
  280. { "unix-l", &xioaddr_unix_listen },
  281. { "unix-listen", &xioaddr_unix_listen },
  282. #endif
  283. #if WITH_UNIX
  284. { "unix-recv", &xioaddr_unix_recv },
  285. { "unix-recvfrom", &xioaddr_unix_recvfrom },
  286. { "unix-send", &xioaddr_unix_sendto },
  287. { "unix-sendto", &xioaddr_unix_sendto },
  288. #endif
  289. #else /* !0 */
  290. # if WITH_INTEGRATE
  291. # include "xiointegrate.c"
  292. # else
  293. # include "xioaddrtab.c"
  294. # endif
  295. #endif /* !0 */
  296. { NULL } /* end marker */
  297. } ;
  298. int xioopen_single(xiofile_t *xfd, int xioflags);
  299. /* prepares a xiofile_t record for dual address type:
  300. sets the tag and allocates memory for the substreams.
  301. returns 0 on success, or <0 if an error occurred.
  302. */
  303. int xioopen_makedual(xiofile_t *file) {
  304. file->tag = XIO_TAG_DUAL;
  305. file->common.flags = XIO_RDWR;
  306. if ((file->dual.stream[0] = (xiosingle_t *)xioallocfd()) == NULL)
  307. return -1;
  308. file->dual.stream[0]->flags = XIO_RDONLY;
  309. if ((file->dual.stream[1] = (xiosingle_t *)xioallocfd()) == NULL)
  310. return -1;
  311. file->dual.stream[1]->flags = XIO_WRONLY;
  312. return 0;
  313. }
  314. static xiofile_t *xioallocfd(void) {
  315. xiofile_t *fd;
  316. if ((fd = Calloc(1, sizeof(xiofile_t))) == NULL) {
  317. return NULL;
  318. }
  319. /* some default values; 0's and NULL's need not be applied (calloc'ed) */
  320. fd->common.tag = XIO_TAG_INVALID;
  321. /* fd->common.addr = NULL; */
  322. fd->common.flags = XIO_RDWR;
  323. #if WITH_RETRY
  324. /* fd->stream.retry = 0; */
  325. /* fd->stream.forever = false; */
  326. fd->stream.intervall.tv_sec = 1;
  327. /* fd->stream.intervall.tv_nsec = 0; */
  328. #endif /* WITH_RETRY */
  329. /* fd->common.ignoreeof = false; */
  330. /* fd->common.eof = 0; */
  331. fd->stream.fd = -1;
  332. fd->stream.dtype = XIODATA_STREAM;
  333. #if _WITH_SOCKET
  334. /* fd->stream.salen = 0; */
  335. #endif /* _WITH_SOCKET */
  336. fd->stream.howtoend = END_UNSPEC;
  337. /* fd->stream.name = NULL; */
  338. fd->stream.escape = -1;
  339. /* fd->stream.para.exec.pid = 0; */
  340. fd->stream.lineterm = LINETERM_RAW;
  341. return fd;
  342. }
  343. /* parse the argument that specifies a two-directional data stream
  344. and open the resulting address
  345. */
  346. xiofile_t *xioopen(const char *addr, /* address specification */
  347. int xioflags) {
  348. xiofile_t *xfd;
  349. if (xioinitialize() < 0) {
  350. return NULL;
  351. }
  352. if ((xfd = xioparse_dual(&addr)) == NULL) {
  353. return NULL;
  354. }
  355. /*!! support n socks */
  356. if (!sock[0]) {
  357. sock[0] = xfd;
  358. } else {
  359. sock[1] = xfd;
  360. }
  361. if (xioopen_dual(xfd, xioflags) < 0) {
  362. /*!!! free something? */
  363. return NULL;
  364. }
  365. return xfd;
  366. }
  367. static xiofile_t *xioparse_dual(const char **addr) {
  368. xiofile_t *xfd;
  369. xiosingle_t *sfd1;
  370. /* we parse a single address */
  371. if ((sfd1 = xioparse_single(addr)) == NULL) {
  372. return NULL;
  373. }
  374. /* and now we see if we reached a dual-address separator */
  375. if (!strncmp(*addr, xioopts.pipesep, strlen(xioopts.pipesep))) {
  376. /* yes we reached it, so we parse the second single address */
  377. *addr += strlen(xioopts.pipesep);
  378. if ((xfd = xioallocfd()) == NULL) {
  379. free(sfd1); /*! and maybe have free some if its contents */
  380. return NULL;
  381. }
  382. xfd->tag = XIO_TAG_DUAL;
  383. xfd->dual.stream[0] = sfd1;
  384. if ((xfd->dual.stream[1] = xioparse_single(addr)) == NULL) {
  385. return NULL;
  386. }
  387. return xfd;
  388. }
  389. /* a truly single address */
  390. xfd = (xiofile_t *)sfd1; sfd1 = NULL;
  391. return xfd;
  392. }
  393. static int xioopen_dual(xiofile_t *xfd, int xioflags) {
  394. if (xfd->tag == XIO_TAG_DUAL) {
  395. /* a really dual address */
  396. if ((xioflags&XIO_ACCMODE) != XIO_RDWR) {
  397. Warn("unidirectional open of dual address");
  398. }
  399. if (((xioflags&XIO_ACCMODE)+1) & (XIO_RDONLY+1)) {
  400. if (xioopen_single((xiofile_t *)xfd->dual.stream[0], XIO_RDONLY|(xioflags&~XIO_ACCMODE&~XIO_MAYEXEC))
  401. < 0) {
  402. return -1;
  403. }
  404. }
  405. if (((xioflags&XIO_ACCMODE)+1) & (XIO_WRONLY+1)) {
  406. if (xioopen_single((xiofile_t *)xfd->dual.stream[1], XIO_WRONLY|(xioflags&~XIO_ACCMODE&~XIO_MAYEXEC))
  407. < 0) {
  408. xioclose((xiofile_t *)xfd->dual.stream[0]);
  409. return -1;
  410. }
  411. }
  412. return 0;
  413. }
  414. return xioopen_single(xfd, xioflags);
  415. }
  416. static xiosingle_t *xioparse_single(const char **addr) {
  417. xiofile_t *xfd;
  418. xiosingle_t *sfd;
  419. struct addrname *ae;
  420. const struct addrdesc *addrdesc = NULL;
  421. const char *ends[4+1];
  422. const char *hquotes[] = {
  423. "'",
  424. NULL
  425. } ;
  426. const char *squotes[] = {
  427. "\"",
  428. NULL
  429. } ;
  430. const char *nests[] = {
  431. "'", "'",
  432. "(", ")",
  433. "[", "]",
  434. "{", "}",
  435. NULL
  436. } ;
  437. char token[512], *tokp;
  438. size_t len;
  439. int i;
  440. /* init */
  441. i = 0;
  442. /*ends[i++] = xioopts.chainsep;*/ /* default: "|" */
  443. ends[i++] = xioopts.pipesep; /* default: "!!" */
  444. ends[i++] = ","/*xioopts.comma*/; /* default: "," */
  445. ends[i++] = ":"/*xioopts.colon*/; /* default: ":" */
  446. ends[i++] = NULL;
  447. if ((xfd = xioallocfd()) == NULL) {
  448. return NULL;
  449. }
  450. sfd = &xfd->stream;
  451. sfd->argc = 0;
  452. len = sizeof(token); tokp = token;
  453. if (nestlex(addr, &tokp, &len, ends, hquotes, squotes, nests,
  454. true, true, false) < 0) {
  455. Error2("keyword too long, in address \"%s%s\"", token, *addr);
  456. }
  457. *tokp = '\0'; /*! len? */
  458. ae = (struct addrname *)
  459. keyw((struct wordent *)&addressnames, token,
  460. sizeof(addressnames)/sizeof(struct addrname)-1);
  461. if (ae) {
  462. addrdesc = ae->desc;
  463. /* keyword */
  464. if ((sfd->argv[sfd->argc++] = strdup(token)) == NULL) {
  465. Error1("strdup(\"%s\"): out of memory", token);
  466. }
  467. } else {
  468. if (false) {
  469. ;
  470. #if WITH_FDNUM
  471. } else if (isdigit(token[0]&0xff) && token[1] == '\0') {
  472. Info1("interpreting address \"%s\" as file descriptor", token);
  473. addrdesc = &addr_fd;
  474. if ((sfd->argv[sfd->argc++] = strdup("FD")) == NULL) {
  475. Error("strdup(\"FD\"): out of memory");
  476. }
  477. if ((sfd->argv[sfd->argc++] = strdup(token)) == NULL) {
  478. Error1("strdup(\"%s\"): out of memory", token);
  479. }
  480. /*! check argc overflow */
  481. #endif /* WITH_FDNUM */
  482. #if WITH_GOPEN
  483. } else if (strchr(token, '/')) {
  484. Info1("interpreting address \"%s\" as file name", token);
  485. addrdesc = &addr_gopen;
  486. if ((sfd->argv[sfd->argc++] = strdup("GOPEN")) == NULL) {
  487. Error("strdup(\"GOPEN\"): out of memory");
  488. }
  489. if ((sfd->argv[sfd->argc++] = strdup(token)) == NULL) {
  490. Error1("strdup(\"%s\"): out of memory", token);
  491. }
  492. /*! check argc overflow */
  493. #endif /* WITH_GOPEN */
  494. } else {
  495. Error1("unknown device/address \"%s\"", token);
  496. /*!!! free something*/ return NULL;
  497. }
  498. }
  499. sfd->tag = XIO_TAG_RDWR;
  500. sfd->addr = addrdesc;
  501. while (!strncmp(*addr, xioopts.paramsep, strlen(xioopts.paramsep))) {
  502. *addr += strlen(xioopts.paramsep);
  503. len = sizeof(token); tokp = token;
  504. if (nestlex(addr, &tokp, &len, ends, hquotes, squotes, nests,
  505. true, true, false) != 0) {
  506. Error2("syntax error in address \"%s%s\"", token, *addr);
  507. }
  508. *tokp = '\0';
  509. if ((sfd->argv[sfd->argc++] = strdup(token)) == NULL) {
  510. Error1("strdup(\"%s\"): out of memory", token);
  511. }
  512. }
  513. if (parseopts(addr, addrdesc->groups, &sfd->opts) < 0) {
  514. free(xfd);
  515. return NULL;
  516. }
  517. return sfd;
  518. }
  519. int xioopen_single(xiofile_t *xfd, int xioflags) {
  520. const struct addrdesc *addrdesc;
  521. int result;
  522. if ((xioflags&XIO_ACCMODE) == XIO_RDONLY) {
  523. xfd->tag = XIO_TAG_RDONLY;
  524. } else if ((xioflags&XIO_ACCMODE) == XIO_WRONLY) {
  525. xfd->tag = XIO_TAG_WRONLY;
  526. } else if ((xioflags&XIO_ACCMODE) == XIO_RDWR) {
  527. xfd->tag = XIO_TAG_RDWR;
  528. } else {
  529. Error1("invalid mode for address \"%s\"", xfd->stream.argv[0]);
  530. }
  531. xfd->stream.flags &= (~XIO_ACCMODE);
  532. xfd->stream.flags |= (xioflags & XIO_ACCMODE);
  533. addrdesc = xfd->stream.addr;
  534. result = (*addrdesc->func)(xfd->stream.argc, xfd->stream.argv,
  535. xfd->stream.opts, xioflags, xfd,
  536. addrdesc->groups, addrdesc->arg1,
  537. addrdesc->arg2, addrdesc->arg3);
  538. return result;
  539. }