Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / scvim / bin / sclangpipe_app
blob03de7bc0002c59ffe33ce46470db9da885561a71
1 #!/usr/bin/env ruby
2 #SCvim pipe
3 #this is a script to pass things from scvim to sclang
5 #Copyright 2007 Alex Norman
7 #This file is part of SCVIM.
9 #SCVIM is free software: you can redistribute it and/or modify
10 #it under the terms of the GNU General Public License as published by
11 #the Free Software Foundation, either version 3 of the License, or
12 #(at your option) any later version.
14 #SCVIM is distributed in the hope that it will be useful,
15 #but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 #GNU General Public License for more details.
19 #You should have received a copy of the GNU General Public License
20 #along with SCVIM. If not, see <http://www.gnu.org/licenses/>.
22 require 'fileutils'
23 require 'open3'
24 require 'optparse'
26 pipe_loc = "/tmp/sclang-pipe" unless (pipe_loc = ENV["SCVIM_PIPE_LOC"])
27 rundir = "/tmp/"
28 if ENV["SCLANG_RUNDIR"]
29 rundir = ENV["SCLANG_RUNDIR"]
30 elsif RUBY_PLATFORM =~ /darwin/
31 rundir = "/Applications/SuperCollider/"
32 end
34 pid_loc = "/tmp/sclangpipe_app-pid" unless (pid_loc = ENV["SCVIM_PIPE_PID_LOC"])
36 app = nil
38 `which sclang`.chomp,
39 '/Applications/SuperCollider/SuperCollider.app/Contents/Resources/sclang',
40 '/Applications/SuperCollider/sclang',
41 ].each do |l|
42 if File.exists?(l)
43 app = l
44 break
45 end
46 end
48 unless app
49 puts "cannot find sclang executable, is it in your path?"
50 exit -1
51 end
53 sclangargs = "-i scvim" # this arg must always be passed, so that sclang knows to load classes from folders named "scide_scvim"
55 opts = OptionParser.new do |opts|
56 opts.on("-h", "--help", "Display this help") do
57 puts opts
58 exit
59 end
60 opts.on("-d <path>", "Set runtime directory") do |d|
61 rundir = d
62 end
64 opts.on("-g <memory-growth>[km]", "Set heap growth (default 256k)") do |g|
65 sclangargs = sclangargs + " -g #{g}"
66 end
68 opts.on("-l <path>", "Set library configuration file") do |l|
69 sclangargs = sclangargs + " -l #{l}"
70 end
72 opts.on("-m <memory-space>[km]", "Set initial heap size (default 2m)") do |m|
73 sclangargs = sclangargs + " -m #{m}"
74 end
76 opts.on("-u <network-port-number>", "Set UDP listening port (default 57120)") do |p|
77 sclangargs = sclangargs + " -u #{p}"
78 end
80 opts.on("-r", "Call Main.run on startup") do
81 sclangargs = sclangargs + " -r"
82 end
84 opts.on("-s", "Call Main.stop on shutdown") do
85 sclangargs = sclangargs + " -s"
86 end
88 end
90 opts.parse!(ARGV)
92 File.open(pid_loc, "w"){ |f| f.puts Process.pid }
94 #remove the pipe if it exists
95 FileUtils.rm(pipe_loc) if File.exists?(pipe_loc)
96 #make a new pipe
97 system("mkfifo", pipe_loc)
99 pipeproc = Proc.new {
100 trap("INT") do
101 Process.exit
103 IO.popen("#{app} -d #{rundir.chomp} #{sclangargs}", "w") do |sclang|
104 loop {
105 x = `cat #{pipe_loc}`
106 sclang.print x if x
111 $p = Process.fork { pipeproc.call }
113 #if we get a hup then we kill the pipe process and
114 #restart it
115 trap("HUP") do
116 Process.kill("INT", $p)
117 $p = Process.fork { pipeproc.call }
120 #clean up after us
121 trap("INT") do
122 FileUtils.rm(pipe_loc)
123 FileUtils.rm(pid_loc)
124 exit
127 #we sleep until a signal comes
128 sleep(1) until false