From 67542c0d5a95a2050322ec0c4b6c4f60c0fe14da Mon Sep 17 00:00:00 2001 From: Chris Double Date: Thu, 9 Aug 2012 23:04:48 +1200 Subject: [PATCH] Port MOSREF 'fork' command from mosvm --- mod/lib/terminal.mo | Bin 0 -> 1660 bytes mod/lib/terminal.ms | 104 +++++++++++++++++++++++++++++++++++++++++ mod/mosref/cmd/fork.mo | Bin 0 -> 709 bytes mod/mosref/cmd/fork.ms | 38 +++++++++++++++ mod/mosref/cmds.mo | Bin 442 -> 470 bytes mod/mosref/cmds.ms | 2 +- 6 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 mod/lib/terminal.mo create mode 100644 mod/lib/terminal.ms create mode 100644 mod/mosref/cmd/fork.mo create mode 100644 mod/mosref/cmd/fork.ms diff --git a/mod/lib/terminal.mo b/mod/lib/terminal.mo new file mode 100644 index 0000000000000000000000000000000000000000..bc1b1b8ba9ca2e3f267df70168ddc3ae7b8e0646 GIT binary patch literal 1660 zcmZ8fU2oh(6utJ2_baOzZlZ<5#J#FkI5&2J8@Z=1kh2i>!_Yw+V+^ zPTzx{%&3uf{)ccKpR-BWU-{h-_P3fV>jv9&+nunKz&`KLP}tv@>I?e^=JjlgMR}xm zml^LZGU^;`=g*h9?=QI|cHDors_*%9Z$5TAM?$YLe*oTxt`^p@;#?2rL!a)?zgk?$k1)>zCsN)1 zj~$(8y)pmJS%=$>qO??jB)Jj1ohJ7c*m$BOY$g1 zJjtfIFoIefSNR1(_z#m(Q2XffZ@>QXn@5iYHOshADig^?&NHv;Mv=Xv;I z;zq6^6R4ulML?n|ymfSzl=4CqWukMbPvljXWTBp(Cxt3yt_+$Mbgk-B=CK^_-QV8D zZ!9tQi7qn6`!4~4Rx0~sXygc8jlga5gqPzn#_PK2Wq8D3Fp{A{>udEWLz9?P)yT#M z5N{}GpfRuvBe|IC5WrByXIhSoN>d3((geNoIK!iQ-NP=tjAD~qz6tWIo+=|F7&DVt{zT8V1M(hXfoyucCqeRy zC`r{sM#iXYYB;Z)Mzeg`J`w%=DQ=02URl;^fj#RiOmd@6753GQa-D}KdAPq9P^VPI ng$hlK8}kCVhpm +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU Lesser General Public License, version 2.1 +; as published by the Free Software Foundation. +; +; This library is distributed in the hope that it will be useful, but WITHOUT +; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +; for more details. +; +; You should have received a copy of the GNU Lesser General Public License +; along with this library; if not, write to the Free Software Foundation, +; Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +; Provides the ability to spawn a terminal connected to the returned stream. + +(module "lib/terminal") +(import "lib/env") +;(import "lib/foe") + +(define *in-screen* (or (env-is "TERMINAL" "screen") ;; From the manpage + (env-is "TERM" "screen"))) ;; From the source.. Way to go.. + +(define has-util + (if *in-win32* + (function (has-util/win32 name) + (member name '("nc"))) + + (function (has-util/posix name) + (locate-cmd name))) ) + +(define *has-xterm* (has-util "xterm")) +(define *has-screen* (has-util "screen")) +(define *has-netcat* (has-util "nc")) +(define *has-rlwrap* (has-util "rlwrap")) +(define *has-osascript* (if *in-macosx* (has-util "osascript"))) + +(define (bg-command cmd) + (if *in-win32* + (string-append "cmd /c start " cmd) + (string-append cmd " &"))) + +(define (run-terminal title portno) + (define inner-cmd + (cond + ((not *has-netcat*) + (error 'term "This version of lib/terminal requires netcat")) + (*has-rlwrap* + (string-append "rlwrap nc 127.0.0.1 " (format portno))) + (else + (string-append "nc 127.0.0.1 " (format portno))))) + + (define cmd (cond + ((and *in-screen* *has-screen*) + (string-append "screen -t '" title "' " inner-cmd)) + ((and *in-x11* *has-xterm*) + (bg-command + (string-append "xterm -T '" title "' -e " inner-cmd))) + (*in-win32* + (bg-command + (string-append "cmd /c \"title '" title + "' && " inner-cmd "\""))) + (*has-osascript* + ;; And you thought win32 was bad.. + (string-append + "open -a terminal; " + "echo 'tell application \"Terminal\"\n" + "activate\n" + "do script \"" inner-cmd "\"\n" + "end tell' | osascript")) + (else + (error 'term "Cannot determine how to get a new window.")))) + + (unless (= (run-command cmd) 0) + (error 'term "Failed attempt to spawn terminal" cmd))) + +;;TODO: This should be site configured. +(define *min-accept* 30000) +(define *max-accept* 40000) + +(define (spawn-terminal title) + + (define portno #f) + (define listener #f) + + (until listener + (set! portno (random-integer *min-accept* *max-accept*)) + (set! listener (guard (lambda (err) #f) + (serve-tcp portno)))) + + (guard (lambda (err) + (send 'close listener) + (re-error err)) + (run-terminal title portno)) + + ;;TODO: Timeout. + (define stream (wait listener)) + (close-service listener) + stream) + +(export spawn-terminal) + diff --git a/mod/mosref/cmd/fork.mo b/mod/mosref/cmd/fork.mo new file mode 100644 index 0000000000000000000000000000000000000000..b44afa32568adcffc7e5a05af616aa3150a39867 GIT binary patch literal 709 zcmY*VOOMkq5S|3P+h+UPZh2pf#MRpif&;Guf*V4DBjVsDF11L;k?kn#&A*vnz&L%t z%8D}n9y8wW=V~-m7sRGBNv48|OZui{&tVqbD&~<9 zF;DCQE~t418H=yRu;lSAfmbIGvN^FiL8~Ob9&k#KF6;9phx@Tj8B^B>1ZNQr&ZK+z zKajFNq8N`Oo|bxY7D`HMZ(%&0x`3@APg{Hq~Zl-7lg{(^?l8Oplb`>$+xe zdAy>n$TzLcb`!K~462&LVr#IH2Zf|w3|Q*MV4Z&A`k>Y0=wSiy?vV+IwSBguM!)QMY2NMdvlSp9_PcRZi8l zu0?OcoM$vzXc|tl)7od?Ry5J!+0_fi28SU#-XUJ zOpT?n9ImaGlnr$mD3CPRc7enCE^50lz%xTaM?Sghuq4^>m4>BKo3tW_t2=OdWDa~t WNy8P=m>tR&FF7n-ho_ewdi({X46ITB literal 0 HcmV?d00001 diff --git a/mod/mosref/cmd/fork.ms b/mod/mosref/cmd/fork.ms new file mode 100644 index 0000000..7fbf6bb --- /dev/null +++ b/mod/mosref/cmd/fork.ms @@ -0,0 +1,38 @@ +; Copyright (C) 2006, Ephemeral Security, LLC +; Modifications (C) 2012, Chris Double +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU Lesser General Public License, version 2.1 +; as published by the Free Software Foundation. +; +; This library is distributed in the hope that it will be useful, but WITHOUT +; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License +; for more details. +; +; You should have received a copy of the GNU Lesser General Public License +; along with this library; if not, write to the Free Software Foundation, +; Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +(module "mosref/cmd/fork") +(import "mosref/shell") +(import "mosref/node") +(import "lib/terminal") + +(define-cmd "fork" + "fork []" + (string-append + "Creates a new MOSREF console shell session that may be used" + " in parallel with the current one.\n\n") + + (define conn (spawn-terminal + (if (tc-empty? terms) + "MOSREF" + (tc-next! terms)))) + (with-io (os-connection-input conn) + (os-connection-output conn) + + (define console (mosref-shell-console shell)) + (define node (mosref-shell-node shell)) + (spawn (lambda () (run-mosref-shell console node))))) + diff --git a/mod/mosref/cmds.mo b/mod/mosref/cmds.mo index 3c5ac1e5c35cefdfed26157787e3c87da519ae80..1fb127ece809350f7a8e2583a5f8e8149938c39e 100644 GIT binary patch delta 35 rcmdnRe2tlvL70hQ=|t99oQzzY49pB13_=s