From 81c0bef366228f216094d21f3b608b9d35fd440a Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Thu, 8 Nov 2007 21:41:36 -0600 Subject: [PATCH 1/7] Added exec* words and wait-for-pid to io.unix.launcher --- extra/io/unix/launcher/launcher.factor | 27 +++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index 7b286feae1..d9490d6725 100644 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -1,11 +1,36 @@ -IN: io.unix.launcher + USING: io io.launcher io.unix.backend io.nonblocking sequences kernel namespaces math system alien.c-types debugger continuations ; +IN: io.unix.launcher + ! Search unix first USE: unix +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! Factor friendly versions of the exec functions + +: >argv ( seq -- alien ) [ malloc-char-string ] map f add >c-void*-array ; + +: execv* ( pathname argv -- int ) [ malloc-char-string ] [ >argv ] bi* execv ; +: execvp* ( filename argv -- int ) [ malloc-char-string ] [ >argv ] bi* execvp ; + +: execve* ( pathname argv envp -- int ) + [ malloc-char-string ] [ >argv ] [ >argv ] tri* execve ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! Wait for a pid to finish without freezing up all the Factor threads. +! Need to find a less kludgy way to do this. + +: wait-for-pid ( pid -- ) + dup "int" WNOHANG waitpid + 0 = [ 100 sleep wait-for-pid ] [ drop ] if ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + : with-fork ( quot -- pid ) fork [ zero? -rot if ] keep ; inline From c632240fa906e691aa08a48116c082c070c76a4c Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Thu, 8 Nov 2007 23:53:59 -0600 Subject: [PATCH 2/7] Add unix.linux.if and unix.linux.sockios --- extra/unix/linux/if/if.factor | 98 +++++++++++++++++++++++++ extra/unix/linux/sockios/sockios.factor | 59 +++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 extra/unix/linux/if/if.factor create mode 100644 extra/unix/linux/sockios/sockios.factor diff --git a/extra/unix/linux/if/if.factor b/extra/unix/linux/if/if.factor new file mode 100644 index 0000000000..0a908831ee --- /dev/null +++ b/extra/unix/linux/if/if.factor @@ -0,0 +1,98 @@ + +USING: alien.syntax ; + +IN: unix.linux.if + +: IFNAMSIZ 16 ; +: IF_NAMESIZE 16 ; +: IFHWADDRLEN 6 ; + +! Standard interface flags (netdevice->flags) + +: IFF_UP HEX: 1 ; ! interface is up +: IFF_BROADCAST HEX: 2 ; ! broadcast address valid +: IFF_DEBUG HEX: 4 ; ! turn on debugging +: IFF_LOOPBACK HEX: 8 ; ! is a loopback net +: IFF_POINTOPOINT HEX: 10 ; ! interface is has p-p link +: IFF_NOTRAILERS HEX: 20 ; ! avoid use of trailers +: IFF_RUNNING HEX: 40 ; ! interface running and carrier ok +: IFF_NOARP HEX: 80 ; ! no ARP protocol +: IFF_PROMISC HEX: 100 ; ! receive all packets +: IFF_ALLMULTI HEX: 200 ; ! receive all multicast packets + +: IFF_MASTER HEX: 400 ; ! master of a load balancer +: IFF_SLAVE HEX: 800 ; ! slave of a load balancer + +: IFF_MULTICAST HEX: 1000 ; ! Supports multicast + +! #define IFF_VOLATILE +! (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|IFF_MASTER|IFF_SLAVE|IFF_RUNNING) + +: IFF_PORTSEL HEX: 2000 ; ! can set media type +: IFF_AUTOMEDIA HEX: 4000 ; ! auto media select active +: IFF_DYNAMIC HEX: 8000 ; ! dialup device with changing addresses + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +C-STRUCT: struct-ifmap + { "ulong" "mem-start" } + { "ulong" "mem-end" } + { "ushort" "base-addr" } + { "uchar" "irq" } + { "uchar" "dma" } + { "uchar" "port" } ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! Hmm... the generic sockaddr type isn't defined anywhere. +! Put it here for now. + +TYPEDEF: ushort sa_family_t + +C-STRUCT: struct-sockaddr + { "sa_family_t" "sa_family" } + { { "char" 14 } "sa_data" } ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +! C-UNION: union-ifr-ifrn { "char" IFNAMSIZ } ; + +C-UNION: union-ifr-ifrn { "char" 16 } ; + +C-UNION: union-ifr-ifru + "struct-sockaddr" +! "sockaddr" + "short" + "int" + "struct-ifmap" +! { "char" IFNAMSIZ } + { "char" 16 } + "caddr_t" ; + +C-STRUCT: struct-ifreq + { "union-ifr-ifrn" "ifr-ifrn" } + { "union-ifr-ifru" "ifr-ifru" } ; + +: ifr-name ( struct-ifreq -- value ) struct-ifreq-ifr-ifrn ; + +: ifr-hwaddr ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; +: ifr-addr ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; +: ifr-dstaddr ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; +: ifr-broadaddr ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; +: ifr-netmask ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; +: ifr-flags ( struct-ifreq -- value ) struct-ifreq-ifr-ifru ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +C-UNION: union-ifc-ifcu "caddr_t" "struct-ifreq*" ; + +C-STRUCT: struct-ifconf + { "int" "ifc-len" } + { "union-ifc-ifcu" "ifc-ifcu" } ; + +: ifc-len ( struct-ifconf -- value ) struct-ifconf-ifc-len ; + +: ifc-buf ( struct-ifconf -- value ) struct-ifconf-ifc-ifcu ; +: ifc-req ( struct-ifconf -- value ) struct-ifconf-ifc-ifcu ; + +! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \ No newline at end of file diff --git a/extra/unix/linux/sockios/sockios.factor b/extra/unix/linux/sockios/sockios.factor new file mode 100644 index 0000000000..22da13f482 --- /dev/null +++ b/extra/unix/linux/sockios/sockios.factor @@ -0,0 +1,59 @@ + +IN: unix.linux.sockios + +! Imported from linux-headers-2.6.15-28-686 on Ubuntu 6.06 + +! Socket configuration controls + +: SIOCGIFNAME HEX: 8910 ; ! get iface name +: SIOCSIFLINK HEX: 8911 ; ! set iface channel +: SIOCGIFCONF HEX: 8912 ; ! get iface list +: SIOCGIFFLAGS HEX: 8913 ; ! get flags +: SIOCSIFFLAGS HEX: 8914 ; ! set flags +: SIOCGIFADDR HEX: 8915 ; ! get PA address +: SIOCSIFADDR HEX: 8916 ; ! set PA address +: SIOCGIFDSTADDR HEX: 8917 ; ! get remote PA address +: SIOCSIFDSTADDR HEX: 8918 ; ! set remote PA address +: SIOCGIFBRDADDR HEX: 8919 ; ! get broadcast PA address +: SIOCSIFBRDADDR HEX: 891a ; ! set broadcast PA address +: SIOCGIFNETMASK HEX: 891b ; ! get network PA mask +: SIOCSIFNETMASK HEX: 891c ; ! set network PA mask +: SIOCGIFMETRIC HEX: 891d ; ! get metric +: SIOCSIFMETRIC HEX: 891e ; ! set metric +: SIOCGIFMEM HEX: 891f ; ! get memory address (BSD) +: SIOCSIFMEM HEX: 8920 ; ! set memory address (BSD) +: SIOCGIFMTU HEX: 8921 ; ! get MTU size +: SIOCSIFMTU HEX: 8922 ; ! set MTU size +: SIOCSIFNAME HEX: 8923 ; ! set interface name +: SIOCSIFHWADDR HEX: 8924 ; ! set hardware address +: SIOCGIFENCAP HEX: 8925 ; ! get/set encapsulations +: SIOCSIFENCAP HEX: 8926 ; +: SIOCGIFHWADDR HEX: 8927 ; ! Get hardware address +: SIOCGIFSLAVE HEX: 8929 ; ! Driver slaving support +: SIOCSIFSLAVE HEX: 8930 ; +: SIOCADDMULTI HEX: 8931 ; ! Multicast address lists +: SIOCDELMULTI HEX: 8932 ; +: SIOCGIFINDEX HEX: 8933 ; ! name -> if_index mapping +: SIOGIFINDEX SIOCGIFINDEX ; ! misprint compatibility :-) +: SIOCSIFPFLAGS HEX: 8934 ; ! set/get extended flags set +: SIOCGIFPFLAGS HEX: 8935 ; +: SIOCDIFADDR HEX: 8936 ; ! delete PA address +: SIOCSIFHWBROADCAST HEX: 8937 ; ! set hardware broadcast addr +: SIOCGIFCOUNT HEX: 8938 ; ! get number of devices + +: SIOCGIFBR HEX: 8940 ; ! Bridging support +: SIOCSIFBR HEX: 8941 ; ! Set bridging options + +: SIOCGIFTXQLEN HEX: 8942 ; ! Get the tx queue length +: SIOCSIFTXQLEN HEX: 8943 ; ! Set the tx queue length + +: SIOCGIFDIVERT HEX: 8944 ; ! Frame diversion support +: SIOCSIFDIVERT HEX: 8945 ; ! Set frame diversion options + +: SIOCETHTOOL HEX: 8946 ; ! Ethtool interface + +: SIOCGMIIPHY HEX: 8947 ; ! Get address of MII PHY in use +: SIOCGMIIREG HEX: 8948 ; ! Read MII PHY register. +: SIOCSMIIREG HEX: 8949 ; ! Write MII PHY register. + +: SIOCWANDEV HEX: 894A ; ! get/set netdev parameters From 9ae82bf050c5cc259900a01a4a8872a2a67b4f83 Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 9 Nov 2007 01:24:37 -0600 Subject: [PATCH 3/7] Fix USING: in io.unix.launcher --- extra/io/unix/launcher/launcher.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index d9490d6725..9804519fb6 100644 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -1,7 +1,7 @@ USING: io io.launcher io.unix.backend io.nonblocking sequences kernel namespaces math system alien.c-types -debugger continuations ; +debugger continuations combinators.lib threads ; IN: io.unix.launcher From 1fd6e4ca34ddbd69cbd032c2a3941230ae1c740d Mon Sep 17 00:00:00 2001 From: Eduardo Cavazos Date: Fri, 9 Nov 2007 01:25:13 -0600 Subject: [PATCH 4/7] Minor edit in factor.el --- misc/factor.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/misc/factor.el b/misc/factor.el index 6598f2cbff..19e29843d6 100644 --- a/misc/factor.el +++ b/misc/factor.el @@ -86,7 +86,8 @@ "IN:" "USING:" "TUPLE:" "^C:" "^M:" "USE:" "REQUIRE:" "PROVIDE:" "REQUIRES:" "GENERIC:" "GENERIC#" "SYMBOL:" "PREDICATE:" "VAR:" "VARS:" - "UNION:" "" "MACRO:" "MACRO::" "DEFER:")) + "C-STRUCT:" + "C-UNION:" "" "MACRO:" "MACRO::" "DEFER:" "TYPEDEF:")) (defun factor-mode () "A mode for editing programs written in the Factor programming language." From 1a53979740c3ffa4174902111b39468c4978726d Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 9 Nov 2007 03:18:37 -0500 Subject: [PATCH 5/7] Fix with-fork stack effect --- extra/io/unix/launcher/launcher.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/unix/launcher/launcher.factor b/extra/io/unix/launcher/launcher.factor index 9804519fb6..61a6f706f6 100644 --- a/extra/io/unix/launcher/launcher.factor +++ b/extra/io/unix/launcher/launcher.factor @@ -31,7 +31,7 @@ USE: unix ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -: with-fork ( quot -- pid ) +: with-fork ( child parent -- pid ) fork [ zero? -rot if ] keep ; inline : prepare-execvp ( args -- cmd args ) From 04a5c54e08eb68fd416a0dc2ee643946c6a4c9b5 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 9 Nov 2007 03:19:01 -0500 Subject: [PATCH 6/7] Remove redundant check-ptr usage --- core/libc/libc-docs.factor | 3 +-- core/libc/libc.factor | 2 +- extra/io/buffers/buffers.factor | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/core/libc/libc-docs.factor b/core/libc/libc-docs.factor index 9596b98292..ba870560d6 100644 --- a/core/libc/libc-docs.factor +++ b/core/libc/libc-docs.factor @@ -26,8 +26,7 @@ HELP: memcpy HELP: check-ptr { $values { "c-ptr" "an alien address, byte array, or " { $link f } } { "checked" "an alien address or byte array with non-zero address" } } -{ $description "Throws an error if the input is " { $link f } ". Otherwise the object remains on the data stack. This word should be used to check the return values of " { $link malloc } " and " { $link realloc } " before use." } -{ $error-description "Callers of " { $link malloc } " and " { $link realloc } " should use " { $link check-ptr } " to throw an error in the case of a memory allocation failure." } ; +{ $description "Throws an error if the input is " { $link f } ". Otherwise the object remains on the data stack." } ; HELP: free { $values { "alien" c-ptr } } diff --git a/core/libc/libc.factor b/core/libc/libc.factor index 71b49e940a..88c5070d1f 100644 --- a/core/libc/libc.factor +++ b/core/libc/libc.factor @@ -84,4 +84,4 @@ PRIVATE> "void" "libc" "memcpy" { "void*" "void*" "ulong" } alien-invoke ; : with-malloc ( size quot -- ) - swap 1 calloc check-ptr swap keep free ; inline + swap 1 calloc swap keep free ; inline diff --git a/extra/io/buffers/buffers.factor b/extra/io/buffers/buffers.factor index e58cf3ead0..5d6eaebe6f 100644 --- a/extra/io/buffers/buffers.factor +++ b/extra/io/buffers/buffers.factor @@ -79,7 +79,7 @@ HINTS: search-buffer-until { fixnum fixnum simple-alien string } ; buffer-fill zero? ; : extend-buffer ( n buffer -- ) - 2dup buffer-ptr swap realloc check-ptr + 2dup buffer-ptr swap realloc over set-buffer-ptr set-buffer-size ; : check-overflow ( n buffer -- ) From e8a74a19c2ef73ad59c8f3269bf1d3e11333252a Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 9 Nov 2007 10:33:25 -0500 Subject: [PATCH 7/7] Fix USING: --- extra/io/windows/nt/sockets/sockets.factor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra/io/windows/nt/sockets/sockets.factor b/extra/io/windows/nt/sockets/sockets.factor index 74538ac06a..47ab7795b0 100755 --- a/extra/io/windows/nt/sockets/sockets.factor +++ b/extra/io/windows/nt/sockets/sockets.factor @@ -1,5 +1,5 @@ USING: alien alien.c-types byte-arrays continuations destructors -io.nonblocking io io.sockets io.sockets.impl +io.nonblocking io io.sockets io.sockets.impl namespaces io.streams.duplex io.windows io.windows.nt io.windows.nt.backend windows.winsock kernel libc math sequences threads tuples.lib ; IN: io.windows.nt.sockets