usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / usbmodeswitch / jim / jim-aio.c
blob710a47d362962a545de48546c71b8d30c2db044b
2 /* Jim - A small embeddable Tcl interpreter
4 * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 * Copyright 2005 Clemens Hintze <c.hintze@gmx.net>
6 * Copyright 2005 patthoyts - Pat Thoyts <patthoyts@users.sf.net>
7 * Copyright 2008 oharboe - √ėyvind Harboe - oyvind.harboe@zylin.com
8 * Copyright 2008 Andrew Lunn <andrew@lunn.ch>
9 * Copyright 2008 Duane Ellis <openocd@duaneellis.com>
10 * Copyright 2008 Uwe Klein <uklein@klein-messgeraete.de>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * The views and conclusions contained in the software and documentation
37 * are those of the authors and should not be interpreted as representing
38 * official policies, either expressed or implied, of the Jim Tcl Project.
39 **/
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <fcntl.h>
47 #include "jim.h"
48 #include "jimautoconf.h"
50 #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_SELECT) && defined(HAVE_NETINET_IN_H) && defined(HAVE_NETDB_H) && defined(HAVE_ARPA_INET_H)
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <netdb.h>
55 #ifdef HAVE_SYS_UN_H
56 #include <sys/un.h>
57 #endif
58 #else
59 #define JIM_ANSIC
60 #endif
62 #include "jim-eventloop.h"
63 #include "jim-subcmd.h"
65 #define AIO_CMD_LEN 32 /* e.g. aio.handleXXXXXX */
66 #define AIO_BUF_LEN 256 /* Can keep this small and rely on stdio buffering */
68 #define AIO_KEEPOPEN 1
70 #if defined(JIM_IPV6)
71 #define IPV6 1
72 #else
73 #define IPV6 0
74 #ifndef PF_INET6
75 #define PF_INET6 0
76 #endif
77 #endif
79 #if !defined(JIM_ANSIC) && !defined(JIM_BOOTSTRAP)
80 union sockaddr_any {
81 struct sockaddr sa;
82 struct sockaddr_in sin;
83 #if IPV6
84 struct sockaddr_in6 sin6;
85 #endif
88 #ifndef HAVE_INET_NTOP
89 const char *inet_ntop(int af, const void *src, char *dst, int size)
91 if (af != PF_INET) {
92 return NULL;
94 snprintf(dst, size, "%s", inet_ntoa(((struct sockaddr_in *)src)->sin_addr));
95 return dst;
97 #endif
98 #endif /* JIM_BOOTSTRAP */
100 typedef struct AioFile
102 FILE *fp;
103 Jim_Obj *filename;
104 int type;
105 int OpenFlags; /* AIO_KEEPOPEN? keep FILE* */
106 int fd;
107 #ifdef O_NDELAY
108 int flags;
109 #endif
110 Jim_Obj *rEvent;
111 Jim_Obj *wEvent;
112 Jim_Obj *eEvent;
113 int addr_family;
114 } AioFile;
116 static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
117 static int JimMakeChannel(Jim_Interp *interp, FILE *fh, int fd, Jim_Obj *filename,
118 const char *hdlfmt, int family, const char *mode);
120 #if !defined(JIM_ANSIC) && !defined(JIM_BOOTSTRAP)
121 static int JimParseIPv6Address(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen)
123 #if IPV6
125 * An IPv6 addr/port looks like:
126 * [::1]
127 * [::1]:2000
128 * [fe80::223:6cff:fe95:bdc0%en1]:2000
129 * [::]:2000
130 * 2000
132 * Note that the "any" address is ::, which is the same as when no address is specified.
134 char *sthost = NULL;
135 const char *stport;
136 int ret = JIM_OK;
137 struct addrinfo req;
138 struct addrinfo *ai;
140 stport = strrchr(hostport, ':');
141 if (!stport) {
142 /* No : so, the whole thing is the port */
143 stport = hostport;
144 hostport = "::";
145 sthost = Jim_StrDup(hostport);
147 else {
148 stport++;
151 if (*hostport == '[') {
152 /* This is a numeric ipv6 address */
153 char *pt = strchr(++hostport, ']');
154 if (pt) {
155 sthost = Jim_StrDupLen(hostport, pt - hostport);
159 if (!sthost) {
160 sthost = Jim_StrDupLen(hostport, stport - hostport - 1);
163 memset(&req, '\0', sizeof(req));
164 req.ai_family = PF_INET6;
166 if (getaddrinfo(sthost, NULL, &req, &ai)) {
167 Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport);
168 ret = JIM_ERR;
170 else {
171 memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen);
172 *salen = ai->ai_addrlen;
174 sa->sin.sin_port = htons(atoi(stport));
176 freeaddrinfo(ai);
178 Jim_Free(sthost);
180 return ret;
181 #else
182 Jim_SetResultString(interp, "ipv6 not supported", -1);
183 return JIM_ERR;
184 #endif
187 static int JimParseIpAddress(Jim_Interp *interp, const char *hostport, union sockaddr_any *sa, int *salen)
189 /* An IPv4 addr/port looks like:
190 * 192.168.1.5
191 * 192.168.1.5:2000
192 * 2000
194 * If the address is missing, INADDR_ANY is used.
195 * If the port is missing, 0 is used (only useful for server sockets).
197 char *sthost = NULL;
198 const char *stport;
199 int ret = JIM_OK;
201 stport = strrchr(hostport, ':');
202 if (!stport) {
203 /* No : so, the whole thing is the port */
204 stport = hostport;
205 sthost = Jim_StrDup("0.0.0.0");
207 else {
208 sthost = Jim_StrDupLen(hostport, stport - hostport);
209 stport++;
213 #ifdef HAVE_GETADDRINFO
214 struct addrinfo req;
215 struct addrinfo *ai;
216 memset(&req, '\0', sizeof(req));
217 req.ai_family = PF_INET;
219 if (getaddrinfo(sthost, NULL, &req, &ai)) {
220 ret = JIM_ERR;
222 else {
223 memcpy(&sa->sin, ai->ai_addr, ai->ai_addrlen);
224 *salen = ai->ai_addrlen;
225 freeaddrinfo(ai);
227 #else
228 struct hostent *he;
230 ret = JIM_ERR;
232 if ((he = gethostbyname(sthost)) != NULL) {
233 if (he->h_length == sizeof(sa->sin.sin_addr)) {
234 *salen = sizeof(sa->sin);
235 sa->sin.sin_family= he->h_addrtype;
236 memcpy(&sa->sin.sin_addr, he->h_addr, he->h_length); /* set address */
237 ret = JIM_OK;
240 #endif
242 sa->sin.sin_port = htons(atoi(stport));
244 Jim_Free(sthost);
246 if (ret != JIM_OK) {
247 Jim_SetResultFormatted(interp, "Not a valid address: %s", hostport);
250 return ret;
253 #ifdef HAVE_SYS_UN_H
254 static int JimParseDomainAddress(Jim_Interp *interp, const char *path, struct sockaddr_un *sa)
256 sa->sun_family = PF_UNIX;
257 snprintf(sa->sun_path, sizeof(sa->sun_path), "%s", path);
259 return JIM_OK;
261 #endif
262 #endif /* JIM_BOOTSTRAP */
264 static void JimAioSetError(Jim_Interp *interp, Jim_Obj *name)
266 if (name) {
267 Jim_SetResultFormatted(interp, "%#s: %s", name, strerror(errno));
269 else {
270 Jim_SetResultString(interp, strerror(errno), -1);
274 static void JimAioDelProc(Jim_Interp *interp, void *privData)
276 AioFile *af = privData;
278 JIM_NOTUSED(interp);
280 Jim_DecrRefCount(interp, af->filename);
282 if (!(af->OpenFlags & AIO_KEEPOPEN)) {
283 fclose(af->fp);
285 #ifdef jim_ext_eventloop
286 /* remove existing EventHandlers */
287 if (af->rEvent) {
288 Jim_DeleteFileHandler(interp, af->fp);
290 if (af->wEvent) {
291 Jim_DeleteFileHandler(interp, af->fp);
293 if (af->eEvent) {
294 Jim_DeleteFileHandler(interp, af->fp);
296 #endif
297 Jim_Free(af);
300 static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
302 AioFile *af = Jim_CmdPrivData(interp);
303 char buf[AIO_BUF_LEN];
304 Jim_Obj *objPtr;
305 int nonewline = 0;
306 int neededLen = -1; /* -1 is "read as much as possible" */
308 if (argc && Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) {
309 nonewline = 1;
310 argv++;
311 argc--;
313 if (argc == 1) {
314 jim_wide wideValue;
316 if (Jim_GetWide(interp, argv[0], &wideValue) != JIM_OK)
317 return JIM_ERR;
318 if (wideValue < 0) {
319 Jim_SetResultString(interp, "invalid parameter: negative len", -1);
320 return JIM_ERR;
322 neededLen = (int)wideValue;
324 else if (argc) {
325 return -1;
327 objPtr = Jim_NewStringObj(interp, NULL, 0);
328 while (neededLen != 0) {
329 int retval;
330 int readlen;
332 if (neededLen == -1) {
333 readlen = AIO_BUF_LEN;
335 else {
336 readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen);
338 retval = fread(buf, 1, readlen, af->fp);
339 if (retval > 0) {
340 Jim_AppendString(interp, objPtr, buf, retval);
341 if (neededLen != -1) {
342 neededLen -= retval;
345 if (retval != readlen)
346 break;
348 /* Check for error conditions */
349 if (ferror(af->fp)) {
350 clearerr(af->fp);
351 /* eof and EAGAIN are not error conditions */
352 if (!feof(af->fp) && errno != EAGAIN) {
353 /* I/O error */
354 Jim_FreeNewObj(interp, objPtr);
355 JimAioSetError(interp, af->filename);
356 return JIM_ERR;
359 if (nonewline) {
360 int len;
361 const char *s = Jim_GetString(objPtr, &len);
363 if (len > 0 && s[len - 1] == '\n') {
364 objPtr->length--;
365 objPtr->bytes[objPtr->length] = '\0';
368 Jim_SetResult(interp, objPtr);
369 return JIM_OK;
372 static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
374 AioFile *af = Jim_CmdPrivData(interp);
375 long count = 0;
376 long maxlen = LONG_MAX;
377 FILE *outfh = Jim_AioFilehandle(interp, argv[0]);
379 if (outfh == NULL) {
380 return JIM_ERR;
383 if (argc == 2) {
384 if (Jim_GetLong(interp, argv[1], &maxlen) != JIM_OK) {
385 return JIM_ERR;
389 while (count < maxlen) {
390 int ch = fgetc(af->fp);
392 if (ch == EOF || fputc(ch, outfh) == EOF) {
393 break;
395 count++;
398 if (ferror(af->fp)) {
399 Jim_SetResultFormatted(interp, "error while reading: %s", strerror(errno));
400 clearerr(af->fp);
401 return JIM_ERR;
404 if (ferror(outfh)) {
405 Jim_SetResultFormatted(interp, "error while writing: %s", strerror(errno));
406 clearerr(outfh);
407 return JIM_ERR;
410 Jim_SetResultInt(interp, count);
412 return JIM_OK;
415 static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
417 AioFile *af = Jim_CmdPrivData(interp);
418 char buf[AIO_BUF_LEN];
419 Jim_Obj *objPtr;
420 int len;
422 errno = 0;
424 objPtr = Jim_NewStringObj(interp, NULL, 0);
425 while (1) {
426 buf[AIO_BUF_LEN - 1] = '_';
427 if (fgets(buf, AIO_BUF_LEN, af->fp) == NULL)
428 break;
430 if (buf[AIO_BUF_LEN - 1] == '\0' && buf[AIO_BUF_LEN - 2] != '\n') {
431 Jim_AppendString(interp, objPtr, buf, AIO_BUF_LEN - 1);
433 else {
434 len = strlen(buf);
436 if (len && (buf[len - 1] == '\n')) {
437 /* strip "\n" */
438 len--;
441 Jim_AppendString(interp, objPtr, buf, len);
442 break;
445 if (ferror(af->fp) && errno != EAGAIN && errno != EINTR) {
446 /* I/O error */
447 Jim_FreeNewObj(interp, objPtr);
448 JimAioSetError(interp, af->filename);
449 clearerr(af->fp);
450 return JIM_ERR;
453 if (argc) {
454 if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) {
455 Jim_FreeNewObj(interp, objPtr);
456 return JIM_ERR;
459 len = Jim_Length(objPtr);
461 if (len == 0 && feof(af->fp)) {
462 /* On EOF returns -1 if varName was specified */
463 len = -1;
465 Jim_SetResultInt(interp, len);
467 else {
468 Jim_SetResult(interp, objPtr);
470 return JIM_OK;
473 static int aio_cmd_puts(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
475 AioFile *af = Jim_CmdPrivData(interp);
476 int wlen;
477 const char *wdata;
478 Jim_Obj *strObj;
480 if (argc == 2) {
481 if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) {
482 return -1;
484 strObj = argv[1];
486 else {
487 strObj = argv[0];
490 wdata = Jim_GetString(strObj, &wlen);
491 if (fwrite(wdata, 1, wlen, af->fp) == (unsigned)wlen) {
492 if (argc == 2 || putc('\n', af->fp) != EOF) {
493 return JIM_OK;
496 JimAioSetError(interp, af->filename);
497 return JIM_ERR;
500 #if !defined(JIM_ANSIC) && !defined(JIM_BOOTSTRAP)
501 static int aio_cmd_recvfrom(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
503 AioFile *af = Jim_CmdPrivData(interp);
504 char *buf;
505 union sockaddr_any sa;
506 long len;
507 socklen_t salen = sizeof(sa);
508 int rlen;
510 if (Jim_GetLong(interp, argv[0], &len) != JIM_OK) {
511 return JIM_ERR;
514 buf = Jim_Alloc(len + 1);
516 rlen = recvfrom(fileno(af->fp), buf, len, 0, &sa.sa, &salen);
517 if (rlen < 0) {
518 Jim_Free(buf);
519 JimAioSetError(interp, NULL);
520 return JIM_ERR;
522 buf[rlen] = 0;
523 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, buf, rlen));
525 if (argc > 1) {
526 /* INET6_ADDRSTRLEN is 46. Add some for [] and port */
527 char addrbuf[60];
529 #if IPV6
530 if (sa.sa.sa_family == PF_INET6) {
531 addrbuf[0] = '[';
532 /* Allow 9 for []:65535\0 */
533 inet_ntop(sa.sa.sa_family, &sa.sin6.sin6_addr, addrbuf + 1, sizeof(addrbuf) - 9);
534 snprintf(addrbuf + strlen(addrbuf), 8, "]:%d", ntohs(sa.sin.sin_port));
536 else
537 #endif
539 /* Allow 7 for :65535\0 */
540 inet_ntop(sa.sa.sa_family, &sa.sin.sin_addr, addrbuf, sizeof(addrbuf) - 7);
541 snprintf(addrbuf + strlen(addrbuf), 7, ":%d", ntohs(sa.sin.sin_port));
544 if (Jim_SetVariable(interp, argv[1], Jim_NewStringObj(interp, addrbuf, -1)) != JIM_OK) {
545 return JIM_ERR;
549 return JIM_OK;
553 static int aio_cmd_sendto(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
555 AioFile *af = Jim_CmdPrivData(interp);
556 int wlen;
557 int len;
558 const char *wdata;
559 union sockaddr_any sa;
560 const char *addr = Jim_String(argv[1]);
561 int salen;
563 if (IPV6 && af->addr_family == PF_INET6) {
564 if (JimParseIPv6Address(interp, addr, &sa, &salen) != JIM_OK) {
565 return JIM_ERR;
568 else if (JimParseIpAddress(interp, addr, &sa, &salen) != JIM_OK) {
569 return JIM_ERR;
571 wdata = Jim_GetString(argv[0], &wlen);
573 /* Note that we don't validate the socket type. Rely on sendto() failing if appropriate */
574 len = sendto(fileno(af->fp), wdata, wlen, 0, &sa.sa, salen);
575 if (len < 0) {
576 JimAioSetError(interp, NULL);
577 return JIM_ERR;
579 Jim_SetResultInt(interp, len);
580 return JIM_OK;
583 static int aio_cmd_accept(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
585 AioFile *af = Jim_CmdPrivData(interp);
586 int sock;
587 union sockaddr_any sa;
588 socklen_t addrlen = sizeof(sa);
590 sock = accept(af->fd, &sa.sa, &addrlen);
591 if (sock < 0) {
592 JimAioSetError(interp, NULL);
593 return JIM_ERR;
596 /* Create the file command */
597 return JimMakeChannel(interp, NULL, sock, Jim_NewStringObj(interp, "accept", -1),
598 "aio.sockstream%ld", af->addr_family, "r+");
601 static int aio_cmd_listen(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
603 AioFile *af = Jim_CmdPrivData(interp);
604 long backlog;
606 if (Jim_GetLong(interp, argv[0], &backlog) != JIM_OK) {
607 return JIM_ERR;
610 if (listen(af->fd, backlog)) {
611 JimAioSetError(interp, NULL);
612 return JIM_ERR;
615 return JIM_OK;
617 #endif /* JIM_BOOTSTRAP */
619 static int aio_cmd_flush(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
621 AioFile *af = Jim_CmdPrivData(interp);
623 if (fflush(af->fp) == EOF) {
624 JimAioSetError(interp, af->filename);
625 return JIM_ERR;
627 return JIM_OK;
630 static int aio_cmd_eof(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
632 AioFile *af = Jim_CmdPrivData(interp);
634 Jim_SetResultInt(interp, feof(af->fp));
635 return JIM_OK;
638 static int aio_cmd_close(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
640 Jim_DeleteCommand(interp, Jim_String(argv[0]));
641 return JIM_OK;
644 static int aio_cmd_seek(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
646 AioFile *af = Jim_CmdPrivData(interp);
647 int orig = SEEK_SET;
648 long offset;
650 if (argc == 2) {
651 if (Jim_CompareStringImmediate(interp, argv[1], "start"))
652 orig = SEEK_SET;
653 else if (Jim_CompareStringImmediate(interp, argv[1], "current"))
654 orig = SEEK_CUR;
655 else if (Jim_CompareStringImmediate(interp, argv[1], "end"))
656 orig = SEEK_END;
657 else {
658 return -1;
661 if (Jim_GetLong(interp, argv[0], &offset) != JIM_OK) {
662 return JIM_ERR;
664 if (fseek(af->fp, offset, orig) == -1) {
665 JimAioSetError(interp, af->filename);
666 return JIM_ERR;
668 return JIM_OK;
671 static int aio_cmd_tell(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
673 AioFile *af = Jim_CmdPrivData(interp);
675 Jim_SetResultInt(interp, ftell(af->fp));
676 return JIM_OK;
679 static int aio_cmd_filename(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
681 AioFile *af = Jim_CmdPrivData(interp);
683 Jim_SetResult(interp, af->filename);
684 return JIM_OK;
687 #ifdef O_NDELAY
688 static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
690 AioFile *af = Jim_CmdPrivData(interp);
692 int fmode = af->flags;
694 if (argc) {
695 long nb;
697 if (Jim_GetLong(interp, argv[0], &nb) != JIM_OK) {
698 return JIM_ERR;
700 if (nb) {
701 fmode |= O_NDELAY;
703 else {
704 fmode &= ~O_NDELAY;
706 fcntl(af->fd, F_SETFL, fmode);
707 af->flags = fmode;
709 Jim_SetResultInt(interp, (fmode & O_NONBLOCK) ? 1 : 0);
710 return JIM_OK;
712 #endif
714 static int aio_cmd_buffering(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
716 AioFile *af = Jim_CmdPrivData(interp);
718 static const char * const options[] = {
719 "none",
720 "line",
721 "full",
722 NULL
724 enum
726 OPT_NONE,
727 OPT_LINE,
728 OPT_FULL,
730 int option;
732 if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) {
733 return JIM_ERR;
735 switch (option) {
736 case OPT_NONE:
737 setvbuf(af->fp, NULL, _IONBF, 0);
738 break;
739 case OPT_LINE:
740 setvbuf(af->fp, NULL, _IOLBF, BUFSIZ);
741 break;
742 case OPT_FULL:
743 setvbuf(af->fp, NULL, _IOFBF, BUFSIZ);
744 break;
746 return JIM_OK;
749 #ifdef jim_ext_eventloop
750 static void JimAioFileEventFinalizer(Jim_Interp *interp, void *clientData)
752 Jim_Obj *objPtr = clientData;
754 Jim_DecrRefCount(interp, objPtr);
757 static int JimAioFileEventHandler(Jim_Interp *interp, void *clientData, int mask)
759 Jim_Obj *objPtr = clientData;
761 return Jim_EvalObjBackground(interp, objPtr);
764 static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, Jim_Obj **scriptHandlerObj,
765 int argc, Jim_Obj * const *argv)
767 int scriptlen = 0;
769 if (argc == 0) {
770 /* Return current script */
771 if (*scriptHandlerObj) {
772 Jim_SetResult(interp, *scriptHandlerObj);
774 return JIM_OK;
777 if (*scriptHandlerObj) {
778 /* Delete old handler */
779 Jim_DeleteFileHandler(interp, af->fp);
780 *scriptHandlerObj = NULL;
783 /* Now possibly add the new script(s) */
784 Jim_GetString(argv[0], &scriptlen);
785 if (scriptlen == 0) {
786 /* Empty script, so done */
787 return JIM_OK;
790 /* A new script to add */
791 Jim_IncrRefCount(argv[0]);
792 *scriptHandlerObj = argv[0];
794 Jim_CreateFileHandler(interp, af->fp, mask,
795 JimAioFileEventHandler, *scriptHandlerObj, JimAioFileEventFinalizer);
797 return JIM_OK;
800 static int aio_cmd_readable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
802 AioFile *af = Jim_CmdPrivData(interp);
804 return aio_eventinfo(interp, af, JIM_EVENT_READABLE, &af->rEvent, argc, argv);
807 static int aio_cmd_writable(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
809 AioFile *af = Jim_CmdPrivData(interp);
811 return aio_eventinfo(interp, af, JIM_EVENT_WRITABLE, &af->wEvent, argc, argv);
814 static int aio_cmd_onexception(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
816 AioFile *af = Jim_CmdPrivData(interp);
818 return aio_eventinfo(interp, af, JIM_EVENT_EXCEPTION, &af->wEvent, argc, argv);
820 #endif
822 static const jim_subcmd_type aio_command_table[] = {
823 { .cmd = "read",
824 .args = "?-nonewline? ?len?",
825 .function = aio_cmd_read,
826 .minargs = 0,
827 .maxargs = 2,
828 .description = "Read and return bytes from the stream. To eof if no len."
830 { .cmd = "copyto",
831 .args = "handle ?size?",
832 .function = aio_cmd_copy,
833 .minargs = 1,
834 .maxargs = 2,
835 .description = "Copy up to 'size' bytes to the given filehandle, or to eof if no size."
837 { .cmd = "gets",
838 .args = "?var?",
839 .function = aio_cmd_gets,
840 .minargs = 0,
841 .maxargs = 1,
842 .description = "Read one line and return it or store it in the var"
844 { .cmd = "puts",
845 .args = "?-nonewline? str",
846 .function = aio_cmd_puts,
847 .minargs = 1,
848 .maxargs = 2,
849 .description = "Write the string, with newline unless -nonewline"
851 #if !defined(JIM_ANSIC) && !defined(JIM_BOOTSTRAP)
852 { .cmd = "recvfrom",
853 .args = "len ?addrvar?",
854 .function = aio_cmd_recvfrom,
855 .minargs = 1,
856 .maxargs = 2,
857 .description = "Receive up to 'len' bytes on the socket. Sets 'addrvar' with receive address, if set"
859 { .cmd = "sendto",
860 .args = "str address",
861 .function = aio_cmd_sendto,
862 .minargs = 2,
863 .maxargs = 2,
864 .description = "Send 'str' to the given address (dgram only)"
866 { .cmd = "accept",
867 .function = aio_cmd_accept,
868 .description = "Server socket only: Accept a connection and return stream"
870 { .cmd = "listen",
871 .args = "backlog",
872 .function = aio_cmd_listen,
873 .minargs = 1,
874 .maxargs = 1,
875 .description = "Set the listen backlog for server socket"
877 #endif /* JIM_BOOTSTRAP */
878 { .cmd = "flush",
879 .function = aio_cmd_flush,
880 .description = "Flush the stream"
882 { .cmd = "eof",
883 .function = aio_cmd_eof,
884 .description = "Returns 1 if stream is at eof"
886 { .cmd = "close",
887 .flags = JIM_MODFLAG_FULLARGV,
888 .function = aio_cmd_close,
889 .description = "Closes the stream"
891 { .cmd = "seek",
892 .args = "offset ?start|current|end",
893 .function = aio_cmd_seek,
894 .minargs = 1,
895 .maxargs = 2,
896 .description = "Seeks in the stream (default 'current')"
898 { .cmd = "tell",
899 .function = aio_cmd_tell,
900 .description = "Returns the current seek position"
902 { .cmd = "filename",
903 .function = aio_cmd_filename,
904 .description = "Returns the original filename"
906 #ifdef O_NDELAY
907 { .cmd = "ndelay",
908 .args = "?0|1?",
909 .function = aio_cmd_ndelay,
910 .minargs = 0,
911 .maxargs = 1,
912 .description = "Set O_NDELAY (if arg). Returns current/new setting."
914 #endif
915 { .cmd = "buffering",
916 .args = "none|line|full",
917 .function = aio_cmd_buffering,
918 .minargs = 1,
919 .maxargs = 1,
920 .description = "Sets buffering"
922 #ifdef jim_ext_eventloop
923 { .cmd = "readable",
924 .args = "?readable-script?",
925 .minargs = 0,
926 .maxargs = 1,
927 .function = aio_cmd_readable,
928 .description = "Returns script, or invoke readable-script when readable, {} to remove",
930 { .cmd = "writable",
931 .args = "?writable-script?",
932 .minargs = 0,
933 .maxargs = 1,
934 .function = aio_cmd_writable,
935 .description = "Returns script, or invoke writable-script when writable, {} to remove",
937 { .cmd = "onexception",
938 .args = "?exception-script?",
939 .minargs = 0,
940 .maxargs = 1,
941 .function = aio_cmd_onexception,
942 .description = "Returns script, or invoke exception-script when oob data, {} to remove",
944 #endif
945 { 0 }
948 static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
950 return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, aio_command_table, argc, argv), argc, argv);
953 static int JimAioOpenCommand(Jim_Interp *interp, int argc,
954 Jim_Obj *const *argv)
956 FILE *fp;
957 const char *hdlfmt;
958 const char *mode;
960 if (argc != 2 && argc != 3) {
961 Jim_WrongNumArgs(interp, 1, argv, "filename ?mode?");
962 return JIM_ERR;
965 mode = (argc == 3) ? Jim_String(argv[2]) : "r";
966 hdlfmt = Jim_String(argv[1]);
967 if (Jim_CompareStringImmediate(interp, argv[1], "stdin")) {
968 fp = stdin;
970 else if (Jim_CompareStringImmediate(interp, argv[1], "stdout")) {
971 fp = stdout;
973 else if (Jim_CompareStringImmediate(interp, argv[1], "stderr")) {
974 fp = stderr;
976 else {
977 const char *filename = Jim_String(argv[1]);
980 #ifdef jim_ext_tclcompat
981 /* If the filename starts with '|', use popen instead */
982 if (*filename == '|') {
983 Jim_Obj *evalObj[3];
985 evalObj[0] = Jim_NewStringObj(interp, "popen", -1);
986 evalObj[1] = Jim_NewStringObj(interp, filename + 1, -1);
987 evalObj[2] = Jim_NewStringObj(interp, mode, -1);
989 return Jim_EvalObjVector(interp, 3, evalObj);
991 #endif
992 hdlfmt = "aio.handle%ld";
993 fp = NULL;
996 /* Create the file command */
997 return JimMakeChannel(interp, fp, -1, argv[1], hdlfmt, 0, mode);
1001 * Creates a channel for fh/fd/filename.
1003 * If fh is not NULL, uses that as the channel (and set AIO_KEEPOPEN).
1004 * Otherwise, if fd is >= 0, uses that as the chanel.
1005 * Otherwise opens 'filename' with mode 'mode'.
1007 * hdlfmt is a sprintf format for the filehandle. Anything with %ld at the end will do.
1008 * mode is used for open or fdopen.
1010 * Creates the command and sets the name as the current result.
1012 static int JimMakeChannel(Jim_Interp *interp, FILE *fh, int fd, Jim_Obj *filename,
1013 const char *hdlfmt, int family, const char *mode)
1015 AioFile *af;
1016 char buf[AIO_CMD_LEN];
1017 int OpenFlags = 0;
1019 Jim_IncrRefCount(filename);
1021 if (fh == NULL) {
1022 if (fd < 0) {
1023 fh = fopen(Jim_String(filename), mode);
1025 else {
1026 fh = fdopen(fd, mode);
1029 else {
1030 OpenFlags = AIO_KEEPOPEN;
1033 if (fh == NULL) {
1034 JimAioSetError(interp, filename);
1035 close(fd);
1036 Jim_DecrRefCount(interp, filename);
1037 return JIM_ERR;
1040 /* Create the file command */
1041 af = Jim_Alloc(sizeof(*af));
1042 memset(af, 0, sizeof(*af));
1043 af->fp = fh;
1044 af->fd = fileno(fh);
1045 af->filename = filename;
1046 #ifdef FD_CLOEXEC
1047 if ((OpenFlags & AIO_KEEPOPEN) == 0) {
1048 fcntl(af->fd, F_SETFD, FD_CLOEXEC);
1049 af->OpenFlags = OpenFlags;
1051 #endif
1052 #ifdef O_NDELAY
1053 af->flags = fcntl(af->fd, F_GETFL);
1054 #endif
1055 af->addr_family = family;
1056 snprintf(buf, sizeof(buf), hdlfmt, Jim_GetId(interp));
1057 Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc);
1059 Jim_SetResultString(interp, buf, -1);
1061 return JIM_OK;
1064 #if !defined(JIM_ANSIC) && !defined(JIM_BOOTSTRAP)
1066 static int JimAioSockCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
1068 const char *hdlfmt = "aio.unknown%ld";
1069 const char *socktypes[] = {
1070 "unix",
1071 "unix.server",
1072 "dgram",
1073 "dgram.server",
1074 "stream",
1075 "stream.server",
1076 "pipe",
1077 NULL
1079 enum
1081 SOCK_UNIX,
1082 SOCK_UNIX_SERVER,
1083 SOCK_DGRAM_CLIENT,
1084 SOCK_DGRAM_SERVER,
1085 SOCK_STREAM_CLIENT,
1086 SOCK_STREAM_SERVER,
1087 SOCK_STREAM_PIPE,
1088 SOCK_DGRAM6_CLIENT,
1089 SOCK_DGRAM6_SERVER,
1090 SOCK_STREAM6_CLIENT,
1091 SOCK_STREAM6_SERVER,
1093 int socktype;
1094 int sock;
1095 const char *hostportarg = NULL;
1096 int res;
1097 int on = 1;
1098 const char *mode = "r+";
1099 int family = PF_INET;
1100 Jim_Obj *argv0 = argv[0];
1101 int ipv6 = 0;
1103 if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-ipv6")) {
1104 if (!IPV6) {
1105 Jim_SetResultString(interp, "ipv6 not supported", -1);
1106 return JIM_ERR;
1108 ipv6 = 1;
1109 family = PF_INET6;
1111 argc -= ipv6;
1112 argv += ipv6;
1114 if (argc < 2) {
1115 wrongargs:
1116 Jim_WrongNumArgs(interp, 1, &argv0, "?-ipv6? type ?address?");
1117 return JIM_ERR;
1120 if (Jim_GetEnum(interp, argv[1], socktypes, &socktype, "socket type", JIM_ERRMSG) != JIM_OK)
1121 return JIM_ERR;
1123 Jim_SetEmptyResult(interp);
1125 hdlfmt = "aio.sock%ld";
1127 if (argc > 2) {
1128 hostportarg = Jim_String(argv[2]);
1131 switch (socktype) {
1132 case SOCK_DGRAM_CLIENT:
1133 if (argc == 2) {
1134 /* No address, so an unconnected dgram socket */
1135 sock = socket(family, SOCK_DGRAM, 0);
1136 if (sock < 0) {
1137 JimAioSetError(interp, NULL);
1138 return JIM_ERR;
1140 break;
1142 /* fall through */
1143 case SOCK_STREAM_CLIENT:
1145 union sockaddr_any sa;
1146 int salen;
1148 if (argc != 3) {
1149 goto wrongargs;
1152 if (ipv6) {
1153 if (JimParseIPv6Address(interp, hostportarg, &sa, &salen) != JIM_OK) {
1154 return JIM_ERR;
1157 else if (JimParseIpAddress(interp, hostportarg, &sa, &salen) != JIM_OK) {
1158 return JIM_ERR;
1160 sock = socket(family, (socktype == SOCK_DGRAM_CLIENT) ? SOCK_DGRAM : SOCK_STREAM, 0);
1161 if (sock < 0) {
1162 JimAioSetError(interp, NULL);
1163 return JIM_ERR;
1165 res = connect(sock, &sa.sa, salen);
1166 if (res) {
1167 JimAioSetError(interp, argv[2]);
1168 close(sock);
1169 return JIM_ERR;
1172 break;
1174 case SOCK_STREAM_SERVER:
1175 case SOCK_DGRAM_SERVER:
1177 union sockaddr_any sa;
1178 int salen;
1180 if (argc != 3) {
1181 goto wrongargs;
1184 if (ipv6) {
1185 if (JimParseIPv6Address(interp, hostportarg, &sa, &salen) != JIM_OK) {
1186 return JIM_ERR;
1189 else if (JimParseIpAddress(interp, hostportarg, &sa, &salen) != JIM_OK) {
1190 return JIM_ERR;
1192 sock = socket(family, (socktype == SOCK_DGRAM_SERVER) ? SOCK_DGRAM : SOCK_STREAM, 0);
1193 if (sock < 0) {
1194 JimAioSetError(interp, NULL);
1195 return JIM_ERR;
1198 /* Enable address reuse */
1199 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1201 res = bind(sock, &sa.sa, salen);
1202 if (res) {
1203 JimAioSetError(interp, argv[2]);
1204 close(sock);
1205 return JIM_ERR;
1207 if (socktype == SOCK_STREAM_SERVER) {
1208 res = listen(sock, 5);
1209 if (res) {
1210 JimAioSetError(interp, NULL);
1211 close(sock);
1212 return JIM_ERR;
1215 hdlfmt = "aio.socksrv%ld";
1217 break;
1219 #ifdef HAVE_SYS_UN_H
1220 case SOCK_UNIX:
1222 struct sockaddr_un sa;
1223 socklen_t len;
1225 if (argc != 3 || ipv6) {
1226 goto wrongargs;
1229 if (JimParseDomainAddress(interp, hostportarg, &sa) != JIM_OK) {
1230 JimAioSetError(interp, argv[2]);
1231 return JIM_ERR;
1233 family = PF_UNIX;
1234 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1235 if (sock < 0) {
1236 JimAioSetError(interp, NULL);
1237 return JIM_ERR;
1239 len = strlen(sa.sun_path) + 1 + sizeof(sa.sun_family);
1240 res = connect(sock, (struct sockaddr *)&sa, len);
1241 if (res) {
1242 JimAioSetError(interp, argv[2]);
1243 close(sock);
1244 return JIM_ERR;
1246 hdlfmt = "aio.sockunix%ld";
1247 break;
1250 case SOCK_UNIX_SERVER:
1252 struct sockaddr_un sa;
1253 socklen_t len;
1255 if (argc != 3 || ipv6) {
1256 goto wrongargs;
1259 if (JimParseDomainAddress(interp, hostportarg, &sa) != JIM_OK) {
1260 JimAioSetError(interp, argv[2]);
1261 return JIM_ERR;
1263 family = PF_UNIX;
1264 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1265 if (sock < 0) {
1266 JimAioSetError(interp, NULL);
1267 return JIM_ERR;
1269 len = strlen(sa.sun_path) + 1 + sizeof(sa.sun_family);
1270 res = bind(sock, (struct sockaddr *)&sa, len);
1271 if (res) {
1272 JimAioSetError(interp, argv[2]);
1273 close(sock);
1274 return JIM_ERR;
1276 res = listen(sock, 5);
1277 if (res) {
1278 JimAioSetError(interp, NULL);
1279 close(sock);
1280 return JIM_ERR;
1282 hdlfmt = "aio.sockunixsrv%ld";
1283 break;
1285 #endif
1287 #ifdef HAVE_PIPE
1288 case SOCK_STREAM_PIPE:
1290 int p[2];
1292 if (argc != 2 || ipv6) {
1293 goto wrongargs;
1296 if (pipe(p) < 0) {
1297 JimAioSetError(interp, NULL);
1298 return JIM_ERR;
1301 if (JimMakeChannel(interp, NULL, p[0], argv[1], "aio.pipe%ld", 0, "r") == JIM_OK) {
1302 Jim_Obj *objPtr = Jim_NewListObj(interp, NULL, 0);
1303 Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp));
1305 if (JimMakeChannel(interp, NULL, p[1], argv[1], "aio.pipe%ld", 0, "w") == JIM_OK) {
1306 Jim_ListAppendElement(interp, objPtr, Jim_GetResult(interp));
1307 Jim_SetResult(interp, objPtr);
1308 return JIM_OK;
1311 /* Can only be here if fdopen() failed */
1312 close(p[0]);
1313 close(p[1]);
1314 JimAioSetError(interp, NULL);
1315 return JIM_ERR;
1317 break;
1318 #endif
1319 default:
1320 Jim_SetResultString(interp, "Unsupported socket type", -1);
1321 return JIM_ERR;
1324 return JimMakeChannel(interp, NULL, sock, argv[1], hdlfmt, family, mode);
1326 #endif /* JIM_BOOTSTRAP */
1328 FILE *Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command)
1330 Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG);
1332 if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) {
1333 return ((AioFile *) cmdPtr->u.native.privData)->fp;
1335 Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command);
1336 return NULL;
1339 int Jim_aioInit(Jim_Interp *interp)
1341 if (Jim_PackageProvide(interp, "aio", "1.0", JIM_ERRMSG))
1342 return JIM_ERR;
1344 Jim_CreateCommand(interp, "open", JimAioOpenCommand, NULL, NULL);
1345 #ifndef JIM_ANSIC
1346 Jim_CreateCommand(interp, "socket", JimAioSockCommand, NULL, NULL);
1347 #endif
1349 /* Takeover stdin, stdout and stderr */
1350 Jim_EvalGlobal(interp, "open stdin; open stdout; open stderr");
1352 return JIM_OK;