]> gitweb.factorcode.org Git - factor.git/commitdiff
tools.ps: implemented on macosx.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 25 Apr 2013 16:12:42 +0000 (09:12 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 25 Apr 2013 16:12:42 +0000 (09:12 -0700)
basis/tools/ps/linux/authors.txt [new file with mode: 0644]
basis/tools/ps/linux/linux.factor [new file with mode: 0644]
basis/tools/ps/linux/platforms.txt [new file with mode: 0644]
basis/tools/ps/macosx/authors.txt [new file with mode: 0644]
basis/tools/ps/macosx/macosx.factor [new file with mode: 0644]
basis/tools/ps/macosx/platforms.txt [new file with mode: 0644]
basis/tools/ps/platforms.txt [deleted file]
basis/tools/ps/ps.factor
basis/tools/ps/summary.txt
basis/tools/ps/windows/platforms.txt [new file with mode: 0644]
basis/tools/ps/windows/windows.factor [new file with mode: 0644]

diff --git a/basis/tools/ps/linux/authors.txt b/basis/tools/ps/linux/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/basis/tools/ps/linux/linux.factor b/basis/tools/ps/linux/linux.factor
new file mode 100644 (file)
index 0000000..016d3d5
--- /dev/null
@@ -0,0 +1,22 @@
+! Copyright (C) 2012 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors assocs io.directories io.pathnames kernel
+math.parser prettyprint sequences splitting unix.linux.proc ;
+IN: tools.ps.linux
+
+! If cmdline is empty, read the filename from /proc/pid/stat
+: ps-cmdline ( path -- path string )
+    dup parse-proc-pid-cmdline [
+        dup parse-proc-pid-stat filename>>
+        [ "()" member? ] trim
+        "[" "]" surround
+    ] [
+        "\0" split " " join
+    ] if-empty ;
+
+M: linux ps ( -- assoc )
+    "/proc" [
+        "." directory-files
+        [ file-name string>number ] filter
+        [ ps-cmdline ] { } map>assoc
+    ] with-directory ;
diff --git a/basis/tools/ps/linux/platforms.txt b/basis/tools/ps/linux/platforms.txt
new file mode 100644 (file)
index 0000000..a08e1f3
--- /dev/null
@@ -0,0 +1 @@
+linux
diff --git a/basis/tools/ps/macosx/authors.txt b/basis/tools/ps/macosx/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/basis/tools/ps/macosx/macosx.factor b/basis/tools/ps/macosx/macosx.factor
new file mode 100644 (file)
index 0000000..86e9406
--- /dev/null
@@ -0,0 +1,155 @@
+! Copyright (C) 2013 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+
+USING: accessors alien.c-types alien.data alien.syntax arrays
+assocs byte-arrays classes.struct continuations fry grouping
+kernel literals math sequences sorting splitting strings system
+system-info.macosx tools.ps unix unix.time unix.types ;
+
+QUALIFIED-WITH: alien.c-types c
+
+IN: tools.ps.macosx
+
+<PRIVATE
+
+: system-type ( -- str ) { 1 1 } sysctl-query-string ;
+: system-release ( -- str ) { 1 2 } sysctl-query-string ;
+: system-revision ( -- str ) { 1 3 } sysctl-query-string ;
+: system-version ( -- str ) { 1 4 } sysctl-query-string ;
+: max-vnodes ( -- n ) { 1 5 } sysctl-query-uint ;
+: max-processes ( -- n ) { 1 6 } sysctl-query-uint ;
+: max-open-files ( -- n ) { 1 7 } sysctl-query-uint ;
+: max-arguments ( -- args ) { 1 8 } sysctl-query-uint ;
+: system-security-level ( -- n ) { 1 9 } sysctl-query-uint ;
+: hostname ( -- str ) { 1 10 } sysctl-query-string ;
+
+: sysctl-query-bytes ( seq -- n )
+    [ make-int-array ] [ length ] bi f 0 uint <ref>
+    [ f 0 sysctl io-error ] keep uint deref ;
+
+STRUCT: _pcred
+    { pc_lock char[72] }
+    { pc_ucred void* }
+    { p_ruid uid_t }
+    { p_svuid uid_t }
+    { p_rgid gid_t }
+    { p_svgid gid_t }
+    { p_refcnt int } ;
+
+STRUCT: _ucred
+    { cr_ref int32_t }
+    { cr_uid uid_t }
+    { cr_ngroups c:short }
+    { cr_groups gid_t[16] } ;
+
+STRUCT: vmspace
+    { dummy int32_t }
+    { dummy2 caddr_t }
+    { dummy3 int32_t[5] }
+    { dummy4 caddr_t[3] } ;
+
+TYPEDEF: int32_t segsz_t
+TYPEDEF: uint32_t fixpt_t
+TYPEDEF: uint64_t u_quad_t
+TYPEDEF: uint32_t sigset_t
+
+STRUCT: itimerval
+    { it_interval timeval }
+    { it_value timeval } ;
+
+STRUCT: extern_proc
+    { __p_starttime timeval }
+    { p_vmspace void* }
+    { p_sigacts void* }
+    { p_flag int }
+    { p_stat char }
+    { p_pid pid_t }
+    { p_oppid pid_t }
+    { p_dupfd int }
+    { user_stack caddr_t }
+    { exit_thread void* }
+    { p_debugger int }
+    { sigwait boolean_t }
+    { p_estcpu uint }
+    { p_cpticks int }
+    { p_pctcpu fixpt_t }
+    { p_wchan void* }
+    { p_wmesg void* }
+    { p_swtime uint }
+    { p_slptime uint }
+    { p_realtimer itimerval }
+    { p_rtime timeval }
+    { p_uticks u_quad_t }
+    { p_sticks u_quad_t }
+    { p_iticks u_quad_t }
+    { p_traceflag int }
+    { p_tracep void* }
+    { p_siglist int }
+    { p_textvp void* }
+    { p_holdcnt int }
+    { p_sigmask sigset_t }
+    { p_sigignore sigset_t }
+    { p_sigcatch sigset_t }
+    { p_priority uchar }
+    { p_usrpri uchar }
+    { p_nice char }
+    { p_comm char[16] }
+    { p_pgrp void* }
+    { p_addr void* }
+    { p_xstat ushort }
+    { p_acflag ushort }
+    { p_ru void* } ;
+
+STRUCT: kinfo_proc
+    { kp_proc extern_proc }
+    { e_paddr void* }
+    { e_sess void* }
+    { e_pcred _pcred }
+    { e_ucred _ucred }
+    { e_vm vmspace }
+    { e_ppid pid_t }
+    { e_pgid pid_t }
+    { e_joc c:short }
+    { e_tdev dev_t }
+    { e_tpgid pid_t }
+    { e_tsess void* }
+    { e_mesg char[8] }
+    { e_xsize segsz_t }
+    { e_xrssize c:short }
+    { e_xccount c:short }
+    { e_xswrss c:short }
+    { e_flag int32_t }
+    { e_login char[12] }
+    { e_spare int32_t[4] } ;
+
+: split1-skip-slice ( seq quot: ( elt -- ? ) -- before-slice after-slice )
+    [ find drop dup ]
+    [ [ not ] compose find-from drop over or ]
+    [ drop snip-slice ] 2tri ; inline
+
+: head-split-skip ( seq n quot: ( elt -- ? ) -- pieces )
+    [ dup 0 >= ] swap '[
+        [ _ split1-skip-slice ] [ 1 - rot ] bi*
+    ] produce 2nip ; inline
+
+: args ( pid -- args )
+    [ 1 49 ] dip 0 4array max-arguments sysctl-query
+    4 cut-slice swap >byte-array uint deref
+    [ zero? ] head-split-skip [ >string ] map ;
+
+: procs ( -- seq )
+    { 1 14 0 0 } dup sysctl-query-bytes sysctl-query
+    kinfo_proc struct-size group
+    [ kinfo_proc memory>struct ] map ;
+
+: ps-arg ( kp_proc -- arg )
+    [ p_pid>> args rest " " join ] [
+        drop p_comm>> 0 over index [ head ] when* >string
+    ] recover ;
+
+PRIVATE>
+
+M: macosx ps ( -- assoc )
+    procs [ kp_proc>> p_pid>> 0 > ] filter
+    [ kp_proc>> [ p_pid>> ] [ ps-arg ] bi ] { } map>assoc
+    sort-keys ;
diff --git a/basis/tools/ps/macosx/platforms.txt b/basis/tools/ps/macosx/platforms.txt
new file mode 100644 (file)
index 0000000..6e806f4
--- /dev/null
@@ -0,0 +1 @@
+macosx
diff --git a/basis/tools/ps/platforms.txt b/basis/tools/ps/platforms.txt
deleted file mode 100644 (file)
index a08e1f3..0000000
+++ /dev/null
@@ -1 +0,0 @@
-linux
index f0c7125909be15a2f2e561eb6162dfa5c4e4e533..b6e41f424eaa9e4a8c92d0adf52f1b1d131c89c9 100644 (file)
@@ -1,25 +1,15 @@
-! Copyright (C) 2012 Doug Coleman.
+! Copyright (C) 2012-2013 Doug Coleman, John Benediktsson.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors assocs io.directories io.pathnames kernel
-math.parser prettyprint sequences splitting unix.linux.proc ;
+USING: combinators prettyprint system vocabs ;
 IN: tools.ps
 
-! If cmdline is empty, read the filename from /proc/pid/stat
-: ps-cmdline ( path -- path string )
-    dup parse-proc-pid-cmdline [
-        dup parse-proc-pid-stat filename>>
-        [ "()" member? ] trim
-        "[" "]" surround
-    ] [
-        "\0" split " " join
-    ] if-empty ;
+HOOK: ps os ( -- assoc )
 
-: ps ( -- assoc )
-    "/proc" [
-        "." directory-files
-        [ file-name string>number ] filter
-        [ ps-cmdline ] { } map>assoc
-    ] with-directory ;
+{
+    { [ os macosx?  ] [ "tools.ps.macosx"  ] }
+    { [ os linux?   ] [ "tools.ps.linux"   ] }
+    { [ os windows? ] [ "tools.ps.windows" ] }
+} cond require
 
 : ps. ( -- )
     ps simple-table. ;
index b6c670821b2d2604fdd02cb3aaee787fe074366e..19af4b04cdc9e2ee5686b20cceea43ab3b139a01 100644 (file)
@@ -1 +1 @@
-A basic ps utility for Linux.
+Process listing utility
diff --git a/basis/tools/ps/windows/platforms.txt b/basis/tools/ps/windows/platforms.txt
new file mode 100644 (file)
index 0000000..8e1a559
--- /dev/null
@@ -0,0 +1 @@
+windows
diff --git a/basis/tools/ps/windows/windows.factor b/basis/tools/ps/windows/windows.factor
new file mode 100644 (file)
index 0000000..97b21b3
--- /dev/null
@@ -0,0 +1,4 @@
+USING: system tools.ps ;
+IN: tools.ps.windows
+
+M: windows ps ( -- assoc ) { } ;