1 ;;; web-shell.el --- interact with a SHELL through a web interface
3 ;; Copyright (C) 2013 Eric Schulte <schulte.eric@gmail.com>
7 ;; DO NOT RUN THIS EXAMPLE!
9 ;; At least not if anyone has network access to your computer.
11 ;; This example starts a local shell using the `shell' function. The
12 ;; resulting comint buffer is then exported using web sockets.
13 ;; Clients can run local shell commands and see their results through
16 ;; This example is included because it should be easily generalizable
17 ;; to build web interfaces to other comint buffers using web sockets.
20 (defvar web-shell-port 9018)
22 (defun web-shell-f-to-s (f)
24 (insert-file-contents-literally
27 (or load-file-name buffer-file-name default-directory))))
30 (defvar web-shell-js (web-shell-f-to-s "018-web-shell.js"))
32 (defvar web-shell-html (web-shell-f-to-s "018-web-shell.html"))
34 (defvar web-shell-socket nil)
36 (defun web-shell-socket-respond (string)
37 (when web-shell-socket
38 (process-send-string web-shell-socket (ws-web-socket-frame string))))
40 (defun web-shell-socket-handler (process string)
41 (message "recieved %S" string)
42 (with-current-buffer "*shell*"
43 (goto-char (process-mark (get-buffer-process (current-buffer))))
47 (defun web-shell-handler (request)
48 (with-slots (process headers) request
49 ;; if a web-socket request
50 (if (ws-web-socket-connect request 'web-shell-socket-handler)
51 ;; then connect and keep open
53 (setq web-shell-socket process)
54 (add-hook 'comint-output-filter-functions 'web-shell-socket-respond))
55 ;; otherwise send the html and javascript
56 (save-window-excursion (shell))
57 (ws-response-header process 200 '("Content-type" . "text/html"))
58 (process-send-string process
59 (format web-shell-html (format web-shell-js web-shell-port))))))
61 (ws-start 'web-shell-handler 9018)