1 ;; Connect Maxima to a socket which has been opened by
2 ;; some third party, typically a GUI program which supplies
4 ;; Note that this code DOES NOT create a Maxima server:
5 ;; Maxima is the client!
10 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
11 #+sbcl
(require 'sb-posix
)
12 (require 'sb-bsd-sockets
))
14 (defvar $in_netmath nil
)
15 (defvar $show_openplot t
)
16 (defvar *socket-connection
*)
20 #+ecl
(defvar *old-stdin
*)
21 #+ecl
(defvar *old-stdout
*)
22 #+ecl
(defvar *old-sterr
*)
23 #+ecl
(defvar *old-term-io
*)
24 #+ecl
(defvar *old-debug-io
*)
26 (defun setup-client (port &optional
(host "localhost"))
27 ;; The following command has to be executed on windows before
28 ;; the connection is opened. If it isn't the first unicode
29 ;; character maxima wants to send causes sbcl to wait indefinitely.
30 #+sbcl
(setf sb-impl
::*default-external-format
* :utf-8
)
31 (multiple-value-bind (sock condition
) (ignore-errors (open-socket host port
))
33 ; It appears that we were unable to open a socket or connect to the
35 (mtell (intl:gettext
"~%Unable to connect Maxima to port ~:M.~%") port
)
36 (mtell (intl:gettext
"Error: ~A~%") condition
)
38 ;; Some lisps if the front-end dies by default don't quit but output an
39 ;; error message to the front-end that (as the front-end doesn't exist
40 ;; any more) causes an error message that...
41 #+gcl
(setq si
::*sigpipe-action
* 'si
::bye
)
42 #+ecl
(ext:set-signal-handler EXT
:+SIGPIPE
+ 'ext
:quit
)
44 (setq *socket-connection
* sock
)
45 (setq $old_stderr
*error-output
*
46 $old_stdout
*standard-output
*)
47 #+ecl
(setq *old-stdin
* *standard-input
*
48 *old-stdout
* *standard-output
*
49 *old-sterr
* *error-output
*
50 *old-term-io
* *terminal-io
*
51 *old-debug-io
* *debug-io
*)
52 (setq *standard-input
* sock
)
53 (setq *standard-output
* sock
)
54 (setq *error-output
* sock
)
55 (setq *terminal-io
* sock
)
56 (setq *trace-output
* sock
)
57 (format t
"pid=~a~%" (getpid))
59 (setq *debug-io
* sock
))
62 (defun close-client ()
63 #+ecl
(setq *standard-input
* *old-stdin
*
64 *standard-output
* *old-stdout
*
65 *error-output
* *old-sterr
*
66 *terminal-io
* *old-term-io
*
67 *debug-io
* *old-debug-io
*)
68 #+ecl
(close *socket-connection
*))
70 ;;; from CLOCC: <http://clocc.sourceforge.net>
71 (defun open-socket (host port
&optional bin
)
72 "Open a socket connection to `host' at `port'."
73 (declare (type (or integer string
) host
) (fixnum port
) (type boolean bin
))
75 (declare (ignore bin
))
77 (let ((host (etypecase host
79 ;; Can't actually handle this case for lack of HOSTENT-NAME and RESOLVE-HOST-IPADDR.
80 ;;(integer (hostent-name (resolve-host-ipaddr host)))))
81 (integer (merror (intl:gettext
"OPEN-SOCKET: can't handle integer host argument (host=~M)~%") host
))))
82 #+(and ccl openmcl-unicode-strings
)
83 (ccl:*default-socket-character-encoding
* :utf-8
))
84 #+allegro
(socket:make-socket
:remote-host host
:remote-port port
85 :format
(if bin
:binary
:text
))
86 #+clisp
(socket:socket-connect port host
:element-type
87 (if bin
'(unsigned-byte 8) 'character
))
88 #+scl
(sys:make-fd-stream
(ext:connect-to-inet-socket host port
)
89 :input t
:output t
:element-type
90 (if bin
'(unsigned-byte 8) 'character
))
91 #+cmu
(sys:make-fd-stream
(ext:connect-to-inet-socket host port
)
92 :input t
:output t
:element-type
93 (if bin
'(unsigned-byte 8) 'character
)
94 #+unicode
:external-format
#+unicode
:utf-8
96 #+(or ecl sbcl
) (let ((socket (make-instance 'sb-bsd-sockets
:inet-socket
97 :type
:stream
:protocol
:tcp
)))
98 (sb-bsd-sockets:socket-connect
99 socket
(sb-bsd-sockets:host-ent-address
100 (sb-bsd-sockets:get-host-by-name host
)) port
)
101 (sb-bsd-sockets:socket-make-stream
102 socket
:input t
:output t
:buffering
(if bin
:none
:line
)
103 :element-type
(if bin
'(unsigned-byte 8) 'character
)
104 #+sb-unicode
:external-format
#+sb-unicode
:utf-8
))
105 #+gcl
(si::socket port
:host host
)
106 #+lispworks
(comm:open-tcp-stream host port
:direction
:io
:element-type
107 (if bin
'unsigned-byte
'base-char
))
108 #+ccl
(ccl::make-socket
:remote-host host
:remote-port port
)
109 #-
(or allegro clisp cmu scl sbcl gcl lispworks ecl ccl
)
110 (error 'not-implemented
:proc
(list 'open-socket host port bin
))))
114 (defun start-client (port &optional
(host "localhost"))
115 (format t
(intl:gettext
"Connecting Maxima to server on port ~a~%") port
)
117 (setq $show_openplot nil
)
118 (setup-client port host
))
121 (defun getpid-from-environment ()
123 (values (parse-integer (maxima-getenv "PID")))
124 ((or type-error parse-error
) () -
1)))
126 ;;; For gcl, getpid imported from system in maxima-package.lisp
129 #+clisp
(os:process-id
)
130 #+(or cmu scl
) (unix:unix-getpid
)
131 #+sbcl
(sb-unix:unix-getpid
)
132 #+gcl
(system:getpid
)
133 #+openmcl
(ccl::getpid
)
134 #+lispworks
(system::getpid
)
137 #+allegro
(excl::getpid
)
138 #-
(or clisp cmu scl sbcl gcl openmcl lispworks ecl ccl allegro
)
139 (getpid-from-environment)
142 #+(or gcl clisp cmu scl sbcl lispworks ecl ccl allegro
)
146 #+(or cmu scl
) (unix::unix-chdir w
)
147 #+sbcl
(sb-posix:chdir w
)
148 #+lispworks
(hcl:change-directory w
)
151 #+allegro
(excl:chdir w
)