Hex Artifact Content
Not logged in

Artifact 682b6eea78199d77ce33102cce319ff31a0383e3:


0000: 23 21 20 2f 75 73 72 2f 62 69 6e 2f 65 6e 76 20  #! /usr/bin/env 
0010: 73 63 68 65 6d 65 2d 73 63 72 69 70 74 0a 3b 3b  scheme-script.;;
0020: 20 2d 2a 2d 20 6d 6f 64 65 3a 20 73 63 68 65 6d   -*- mode: schem
0030: 65 20 2d 2a 2d 0a 3b 3b 0a 3b 3b 20 43 6f 70 79  e -*-.;;.;; Copy
0040: 72 69 67 68 74 20 32 30 31 36 20 41 6c 64 6f 20  right 2016 Aldo 
0050: 4e 69 63 6f 6c 61 73 20 42 72 75 6e 6f 0a 3b 3b  Nicolas Bruno.;;
0060: 0a 3b 3b 20 4c 69 63 65 6e 73 65 64 20 75 6e 64  .;; Licensed und
0070: 65 72 20 74 68 65 20 41 70 61 63 68 65 20 4c 69  er the Apache Li
0080: 63 65 6e 73 65 2c 20 56 65 72 73 69 6f 6e 20 32  cense, Version 2
0090: 2e 30 20 28 74 68 65 20 22 4c 69 63 65 6e 73 65  .0 (the "License
00a0: 22 29 3b 0a 3b 3b 20 79 6f 75 20 6d 61 79 20 6e  ");.;; you may n
00b0: 6f 74 20 75 73 65 20 74 68 69 73 20 66 69 6c 65  ot use this file
00c0: 20 65 78 63 65 70 74 20 69 6e 20 63 6f 6d 70 6c   except in compl
00d0: 69 61 6e 63 65 20 77 69 74 68 20 74 68 65 20 4c  iance with the L
00e0: 69 63 65 6e 73 65 2e 0a 3b 3b 20 59 6f 75 20 6d  icense..;; You m
00f0: 61 79 20 6f 62 74 61 69 6e 20 61 20 63 6f 70 79  ay obtain a copy
0100: 20 6f 66 20 74 68 65 20 4c 69 63 65 6e 73 65 20   of the License 
0110: 61 74 0a 3b 3b 0a 3b 3b 20 20 20 20 20 68 74 74  at.;;.;;     htt
0120: 70 3a 2f 2f 77 77 77 2e 61 70 61 63 68 65 2e 6f  p://www.apache.o
0130: 72 67 2f 6c 69 63 65 6e 73 65 73 2f 4c 49 43 45  rg/licenses/LICE
0140: 4e 53 45 2d 32 2e 30 0a 3b 3b 0a 3b 3b 20 55 6e  NSE-2.0.;;.;; Un
0150: 6c 65 73 73 20 72 65 71 75 69 72 65 64 20 62 79  less required by
0160: 20 61 70 70 6c 69 63 61 62 6c 65 20 6c 61 77 20   applicable law 
0170: 6f 72 20 61 67 72 65 65 64 20 74 6f 20 69 6e 20  or agreed to in 
0180: 77 72 69 74 69 6e 67 2c 20 73 6f 66 74 77 61 72  writing, softwar
0190: 65 0a 3b 3b 20 64 69 73 74 72 69 62 75 74 65 64  e.;; distributed
01a0: 20 75 6e 64 65 72 20 74 68 65 20 4c 69 63 65 6e   under the Licen
01b0: 73 65 20 69 73 20 64 69 73 74 72 69 62 75 74 65  se is distribute
01c0: 64 20 6f 6e 20 61 6e 20 22 41 53 20 49 53 22 20  d on an "AS IS" 
01d0: 42 41 53 49 53 2c 0a 3b 3b 20 57 49 54 48 4f 55  BASIS,.;; WITHOU
01e0: 54 20 57 41 52 52 41 4e 54 49 45 53 20 4f 52 20  T WARRANTIES OR 
01f0: 43 4f 4e 44 49 54 49 4f 4e 53 20 4f 46 20 41 4e  CONDITIONS OF AN
0200: 59 20 4b 49 4e 44 2c 20 65 69 74 68 65 72 20 65  Y KIND, either e
0210: 78 70 72 65 73 73 20 6f 72 20 69 6d 70 6c 69 65  xpress or implie
0220: 64 2e 0a 3b 3b 20 53 65 65 20 74 68 65 20 4c 69  d..;; See the Li
0230: 63 65 6e 73 65 20 66 6f 72 20 74 68 65 20 73 70  cense for the sp
0240: 65 63 69 66 69 63 20 6c 61 6e 67 75 61 67 65 20  ecific language 
0250: 67 6f 76 65 72 6e 69 6e 67 20 70 65 72 6d 69 73  governing permis
0260: 73 69 6f 6e 73 20 61 6e 64 0a 3b 3b 20 6c 69 6d  sions and.;; lim
0270: 69 74 61 74 69 6f 6e 73 20 75 6e 64 65 72 20 74  itations under t
0280: 68 65 20 4c 69 63 65 6e 73 65 2e 0a 0a 3b 0a 3b  he License...;.;
0290: 20 65 78 61 6d 70 6c 65 20 6f 66 20 72 65 6d 6f   example of remo
02a0: 74 65 20 52 45 50 4c 0a 3b 20 79 6f 75 20 63 61  te REPL.; you ca
02b0: 6e 20 75 73 65 20 61 6e 79 20 74 72 61 6e 73 70  n use any transp
02c0: 6f 72 74 2c 20 74 63 70 3a 2f 2f 20 69 70 63 3a  ort, tcp:// ipc:
02d0: 2f 2f 20 77 65 62 73 6f 63 6b 65 74 20 69 6e 70  // websocket inp
02e0: 72 6f 63 0a 3b 20 73 65 65 20 68 74 74 70 3a 2f  roc.; see http:/
02f0: 2f 6e 61 6e 6f 6d 73 67 2e 6f 72 67 2f 76 31 2e  /nanomsg.org/v1.
0300: 30 2e 30 2f 6e 61 6e 6f 6d 73 67 2e 37 2e 68 74  0.0/nanomsg.7.ht
0310: 6d 6c 0a 3b 20 61 6e 64 20 6e 61 6e 6f 6d 73 67  ml.; and nanomsg
0320: 2e 73 6c 73 20 66 6f 72 20 64 65 74 61 69 6c 73  .sls for details
0330: 0a 3b 0a 3b 20 2e 2f 6c 6f 63 61 6c 2d 72 65 70  .;.; ./local-rep
0340: 6c 20 74 63 70 3a 2f 2f 31 32 37 2e 30 2e 30 2e  l tcp://127.0.0.
0350: 31 3a 31 32 33 34 35 0a 3b 20 41 42 43 0a 3b 0a  1:12345.; ABC.;.
0360: 3b 20 6d 65 61 6e 77 68 69 6c 65 20 69 6e 20 61  ; meanwhile in a
0370: 6e 6f 74 68 65 72 20 74 65 72 6d 69 6e 61 6c 2e  nother terminal.
0380: 2e 2e 0a 3b 20 2e 2f 72 65 6d 6f 74 65 2d 72 65  ...; ./remote-re
0390: 70 6c 20 74 63 70 3a 2f 2f 31 32 37 2e 30 2e 30  pl tcp://127.0.0
03a0: 2e 31 3a 31 32 33 34 35 0a 3b 20 3e 20 28 2b 20  .1:12345.; > (+ 
03b0: 31 20 32 29 0a 3b 20 33 0a 3b 20 3e 20 28 70 72  1 2).; 3.; > (pr
03c0: 69 6e 74 66 20 22 41 42 43 7e 6e 22 29 20 2d 2d  intf "ABC~n") --
03d0: 3e 20 77 69 6c 6c 20 70 72 69 6e 74 20 41 42 43  > will print ABC
03e0: 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 72 65   on the local re
03f0: 70 6c 20 70 72 6f 63 65 73 73 0a 3b 0a 3b 20 54  pl process.;.; T
0400: 68 65 20 6e 69 63 65 20 74 68 69 6e 67 20 69 73  he nice thing is
0410: 20 74 68 61 74 20 79 6f 75 20 63 61 6e 20 73 63   that you can sc
0420: 61 6c 65 20 74 6f 20 61 6e 79 20 70 72 6f 74 6f  ale to any proto
0430: 63 6f 6c 20 61 6e 64 0a 3b 20 74 68 61 74 20 69  col and.; that i
0440: 74 20 63 61 6e 20 62 65 20 69 6e 74 65 67 72 61  t can be integra
0450: 74 65 64 20 69 6e 20 61 6e 20 65 76 65 6e 74 2f  ted in an event/
0460: 6d 61 69 6e 20 6c 6f 6f 70 0a 3b 20 69 74 20 75  main loop.; it u
0470: 73 65 73 20 6e 6f 6e 2d 62 6c 6f 63 6b 69 6e 67  ses non-blocking
0480: 20 28 72 65 61 64 29 20 61 6e 64 20 79 6f 75 20   (read) and you 
0490: 69 6e 76 6f 6b 65 20 28 6d 79 2d 6c 6f 63 61 6c  invoke (my-local
04a0: 2d 72 65 70 6c 29 20 75 6e 74 69 6c 20 3d 3e 20  -repl) until => 
04b0: 23 66 0a 3b 0a 3b 20 54 4f 44 4f 3a 20 61 64 64  #f.;.; TODO: add
04c0: 20 61 20 63 6f 6d 6d 61 6e 64 20 66 6f 72 20 6c   a command for l
04d0: 6f 63 61 6c 20 65 78 69 74 2e 20 23 21 65 6f 66  ocal exit. #!eof
04e0: 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 6f 6e 6c   object will onl
04f0: 79 20 63 6c 6f 73 65 20 74 68 65 20 72 65 6d 6f  y close the remo
0500: 74 65 20 0a 3b 20 63 6f 6e 6e 65 63 74 69 6f 6e  te .; connection
0510: 2e 2e 2e 2e 0a 0a 23 21 63 68 65 7a 73 63 68 65  ......#!chezsche
0520: 6d 65 0a 0a 28 69 6d 70 6f 72 74 20 28 63 68 65  me..(import (che
0530: 7a 73 63 68 65 6d 65 29 20 28 6e 61 6e 6f 6d 73  zscheme) (nanoms
0540: 67 29 29 0a 28 6e 61 6e 6f 6d 73 67 2d 6c 69 62  g)).(nanomsg-lib
0550: 72 61 72 79 2d 69 6e 69 74 29 0a 0a 28 64 65 66  rary-init)..(def
0560: 69 6e 65 20 61 72 67 76 20 28 63 6f 6d 6d 61 6e  ine argv (comman
0570: 64 2d 6c 69 6e 65 2d 61 72 67 75 6d 65 6e 74 73  d-line-arguments
0580: 29 29 0a 0a 28 64 65 66 69 6e 65 20 73 6f 63 6b  ))..(define sock
0590: 20 28 6e 6e 2d 73 6f 63 6b 65 74 20 41 46 5f 53   (nn-socket AF_S
05a0: 50 20 4e 4e 5f 52 45 50 29 29 0a 28 64 65 66 69  P NN_REP)).(defi
05b0: 6e 65 20 65 69 64 20 28 6e 6e 2d 62 69 6e 64 20  ne eid (nn-bind 
05c0: 73 6f 63 6b 20 28 63 61 72 20 61 72 67 76 29 29  sock (car argv))
05d0: 29 0a 0a 28 64 65 66 69 6e 65 20 28 6d 79 2d 6c  )..(define (my-l
05e0: 6f 63 61 6c 2d 72 65 70 6c 29 0a 20 20 28 63 61  ocal-repl).  (ca
05f0: 6c 6c 2f 63 63 20 0a 20 20 20 28 6c 61 6d 62 64  ll/cc .   (lambd
0600: 61 20 28 72 65 74 75 72 6e 29 0a 20 20 20 20 20  a (return).     
0610: 28 6c 65 74 2a 20 28 5b 62 75 66 20 28 62 6f 78  (let* ([buf (box
0620: 20 23 74 29 5d 0a 09 20 20 20 20 5b 72 20 28 6e   #t)]..    [r (n
0630: 6e 2d 72 65 63 76 20 73 6f 63 6b 20 62 75 66 20  n-recv sock buf 
0640: 4e 4e 5f 4d 53 47 20 4e 4e 5f 44 4f 4e 54 57 41  NN_MSG NN_DONTWA
0650: 49 54 29 5d 29 0a 20 20 20 20 20 20 20 28 77 68  IT)]).       (wh
0660: 65 6e 20 72 0a 09 20 20 20 20 20 28 67 75 61 72  en r..     (guar
0670: 64 20 28 65 20 5b 65 6c 73 65 20 28 70 72 69 6e  d (e [else (prin
0680: 74 66 20 22 65 72 72 6f 72 20 69 6e 20 65 76 61  tf "error in eva
0690: 6c 20 7e 64 7e 6e 22 20 65 29 5d 29 0a 09 09 20  l ~d~n" e)])... 
06a0: 20 20 20 28 77 69 74 68 2d 69 6e 70 75 74 2d 66     (with-input-f
06b0: 72 6f 6d 2d 73 74 72 69 6e 67 20 28 75 74 66 38  rom-string (utf8
06c0: 2d 3e 73 74 72 69 6e 67 20 28 75 6e 62 6f 78 20  ->string (unbox 
06d0: 62 75 66 29 29 0a 09 09 20 20 20 20 20 20 28 6c  buf))...      (l
06e0: 61 6d 62 64 61 20 28 29 0a 09 09 09 28 6c 65 74  ambda ()....(let
06f0: 20 28 5b 70 72 20 28 63 61 6c 6c 2d 77 69 74 68   ([pr (call-with
0700: 2d 73 74 72 69 6e 67 2d 6f 75 74 70 75 74 2d 70  -string-output-p
0710: 6f 72 74 0a 09 09 09 09 20 20 20 28 6c 61 6d 62  ort.....   (lamb
0720: 64 61 20 28 70 29 0a 09 09 09 09 20 20 20 20 20  da (p).....     
0730: 28 6c 65 74 20 28 5b 74 6f 6b 65 6e 20 28 72 65  (let ([token (re
0740: 61 64 29 5d 29 0a 09 09 09 09 20 20 20 20 20 20  ad)]).....      
0750: 20 28 69 66 20 23 66 3b 28 65 6f 66 2d 6f 62 6a   (if #f;(eof-obj
0760: 65 63 74 3f 20 74 6f 6b 65 6e 29 20 0a 09 09 09  ect? token) ....
0770: 09 09 20 20 20 28 72 65 74 75 72 6e 20 23 66 29  ..   (return #f)
0780: 0a 09 09 09 09 09 20 20 20 28 70 72 65 74 74 79  ......   (pretty
0790: 2d 70 72 69 6e 74 20 0a 09 09 09 09 09 20 20 20  -print ......   
07a0: 20 28 65 76 61 6c 20 74 6f 6b 65 6e 20 0a 09 09   (eval token ...
07b0: 09 09 09 09 20 20 28 69 6e 74 65 72 61 63 74 69  ....  (interacti
07c0: 6f 6e 2d 65 6e 76 69 72 6f 6e 6d 65 6e 74 29 29  on-environment))
07d0: 20 70 29 29 29 29 29 5d 29 0a 09 09 09 20 20 28   p)))))])....  (
07e0: 6e 6e 2d 73 65 6e 64 20 73 6f 63 6b 20 28 73 74  nn-send sock (st
07f0: 72 69 6e 67 2d 3e 75 74 66 38 20 70 72 29 20 30  ring->utf8 pr) 0
0800: 29 29 29 29 29 29 29 0a 20 20 20 20 20 23 74 29  ))))))).     #t)
0810: 29 29 0a 0a 28 6c 65 74 20 6c 6f 6f 70 20 28 29  ))..(let loop ()
0820: 20 0a 20 20 28 77 68 65 6e 20 28 6d 79 2d 6c 6f   .  (when (my-lo
0830: 63 61 6c 2d 72 65 70 6c 29 0a 09 28 73 6c 65 65  cal-repl)..(slee
0840: 70 20 28 6d 61 6b 65 2d 74 69 6d 65 20 27 74 69  p (make-time 'ti
0850: 6d 65 2d 64 75 72 61 74 69 6f 6e 20 31 30 30 30  me-duration 1000
0860: 30 30 30 20 30 29 29 0a 09 28 6c 6f 6f 70 29 29  000 0))..(loop))
0870: 29 0a                                            ).