irb/init.rb - irb initialize module
$Release Version: 0.9.5$
$Revision: 24483 $
$Date: 2009-08-09 17:44:15 +0900 (Sun, 09 Aug 2009) $
by Keiju ISHITSUKA(keiju@ruby-lang.org)
—
| STDIN_FILE_NAME | = | "(line)" |
InputMethod
StdioInputMethod
FileInputMethod
(ReadlineInputMethod)
|
|
| FEATURE_IOPT_CHANGE_VERSION | = | "1.9.0" | ||
| IRBRC_EXT | = | "rc" |
# File lib/irb/ext/multi-irb.rb, line 143
143: def IRB.CurrentContext
144: IRB.JobManager.irb(Thread.current).context
145: end
@CONF default setting
# File lib/irb/init.rb, line 29
29: def IRB.init_config(ap_path)
30: # class instance variables
31: @TRACER_INITIALIZED = false
32:
33: # default configurations
34: unless ap_path and @CONF[:AP_NAME]
35: ap_path = File.join(File.dirname(File.dirname(__FILE__)), "irb.rb")
36: end
37: @CONF[:AP_NAME] = File::basename(ap_path, ".rb")
38:
39: @CONF[:IRB_NAME] = "irb"
40: @CONF[:IRB_LIB_PATH] = File.dirname(__FILE__)
41:
42: @CONF[:RC] = true
43: @CONF[:LOAD_MODULES] = []
44: @CONF[:IRB_RC] = nil
45:
46: @CONF[:MATH_MODE] = false
47: @CONF[:USE_READLINE] = false unless defined?(ReadlineInputMethod)
48: @CONF[:INSPECT_MODE] = nil
49: @CONF[:USE_TRACER] = false
50: @CONF[:USE_LOADER] = false
51: @CONF[:IGNORE_SIGINT] = true
52: @CONF[:IGNORE_EOF] = false
53: @CONF[:ECHO] = nil
54: @CONF[:VERBOSE] = nil
55:
56: @CONF[:EVAL_HISTORY] = nil
57: @CONF[:SAVE_HISTORY] = nil
58:
59: @CONF[:BACK_TRACE_LIMIT] = 16
60:
61: @CONF[:PROMPT] = {
62: :NULL => {
63: :PROMPT_I => nil,
64: :PROMPT_N => nil,
65: :PROMPT_S => nil,
66: :PROMPT_C => nil,
67: :RETURN => "%s\n"
68: },
69: :DEFAULT => {
70: :PROMPT_I => "%N(%m):%03n:%i> ",
71: :PROMPT_N => "%N(%m):%03n:%i> ",
72: :PROMPT_S => "%N(%m):%03n:%i%l ",
73: :PROMPT_C => "%N(%m):%03n:%i* ",
74: :RETURN => "=> %s\n"
75: },
76: :CLASSIC => {
77: :PROMPT_I => "%N(%m):%03n:%i> ",
78: :PROMPT_N => "%N(%m):%03n:%i> ",
79: :PROMPT_S => "%N(%m):%03n:%i%l ",
80: :PROMPT_C => "%N(%m):%03n:%i* ",
81: :RETURN => "%s\n"
82: },
83: :SIMPLE => {
84: :PROMPT_I => ">> ",
85: :PROMPT_N => ">> ",
86: :PROMPT_S => nil,
87: :PROMPT_C => "?> ",
88: :RETURN => "=> %s\n"
89: },
90: :INF_RUBY => {
91: :PROMPT_I => "%N(%m):%03n:%i> ",
92: # :PROMPT_N => "%N(%m):%03n:%i> ",
93: :PROMPT_N => nil,
94: :PROMPT_S => nil,
95: :PROMPT_C => nil,
96: :RETURN => "%s\n",
97: :AUTO_INDENT => true
98: },
99: :XMP => {
100: :PROMPT_I => nil,
101: :PROMPT_N => nil,
102: :PROMPT_S => nil,
103: :PROMPT_C => nil,
104: :RETURN => " ==>%s\n"
105: }
106: }
107:
108: @CONF[:PROMPT_MODE] = (STDIN.tty? ? :DEFAULT : :NULL)
109: @CONF[:AUTO_INDENT] = false
110:
111: @CONF[:CONTEXT_MODE] = 3 # use binding in function on TOPLEVEL_BINDING
112: @CONF[:SINGLE_IRB] = false
113:
114: # @CONF[:LC_MESSAGES] = "en"
115: @CONF[:LC_MESSAGES] = Locale.new
116:
117: @CONF[:AT_EXIT] = []
118:
119: @CONF[:DEBUG_LEVEL] = 1
120: end
# File lib/irb/init.rb, line 122
122: def IRB.init_error
123: @CONF[:LC_MESSAGES].load("irb/error.rb")
124: end
initialize tracing function
# File lib/irb/ext/tracer.rb, line 17
17: def IRB.initialize_tracer
18: Tracer.verbose = false
19: Tracer.add_filter {
20: |event, file, line, id, binding, *rests|
21: /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and
22: File::basename(file) != "irb.rb"
23: }
24: end
invoke multi-irb
# File lib/irb/ext/multi-irb.rb, line 148
148: def IRB.irb(file = nil, *main)
149: workspace = WorkSpace.new(*main)
150: parent_thread = Thread.current
151: Thread.start do
152: begin
153: irb = Irb.new(workspace, file)
154: rescue
155: print "Subirb can't start with context(self): ", workspace.main.inspect, "\n"
156: print "return to main irb\n"
157: Thread.pass
158: Thread.main.wakeup
159: Thread.exit
160: end
161: @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
162: @JobManager.insert(irb)
163: @JobManager.current_job = irb
164: begin
165: system_exit = false
166: catch(:IRB_EXIT) do
167: irb.eval_input
168: end
169: rescue SystemExit
170: system_exit = true
171: raise
172: #fail
173: ensure
174: unless system_exit
175: @JobManager.delete(irb)
176: if parent_thread.alive?
177: @JobManager.current_job = @JobManager.irb(parent_thread)
178: parent_thread.run
179: else
180: @JobManager.current_job = @JobManager.main_irb
181: @JobManager.main_thread.run
182: end
183: end
184: end
185: end
186: Thread.stop
187: @JobManager.current_job = @JobManager.irb(Thread.current)
188: end
# File lib/irb.rb, line 87
87: def IRB.irb_abort(irb, exception = Abort)
88: if defined? Thread
89: irb.context.thread.raise exception, "abort then interrupt!!"
90: else
91: raise exception, "abort then interrupt!!"
92: end
93: end
# File lib/irb.rb, line 79
79: def IRB.irb_at_exit
80: @CONF[:AT_EXIT].each{|hook| hook.call}
81: end
loading modules
# File lib/irb/init.rb, line 251
251: def IRB.load_modules
252: for m in @CONF[:LOAD_MODULES]
253: begin
254: require m
255: rescue
256: print $@[0], ":", $!.class, ": ", $!, "\n"
257: end
258: end
259: end
option analyzing
# File lib/irb/init.rb, line 129
129: def IRB.parse_opts
130: load_path = []
131: while opt = ARGV.shift
132: case opt
133: when "-f"
134: @CONF[:RC] = false
135: when "-m"
136: @CONF[:MATH_MODE] = true
137: when "-d"
138: $DEBUG = true
139: when /^-r(.+)?/
140: opt = $1 || ARGV.shift
141: @CONF[:LOAD_MODULES].push opt if opt
142: when /^-I(.+)?/
143: opt = $1 || ARGV.shift
144: load_path.concat(opt.split(File::PATH_SEPARATOR)) if opt
145: when /^-K(.)/
146: $KCODE = $1
147: when "--inspect"
148: @CONF[:INSPECT_MODE] = true
149: when "--noinspect"
150: @CONF[:INSPECT_MODE] = false
151: when "--readline"
152: @CONF[:USE_READLINE] = true
153: when "--noreadline"
154: @CONF[:USE_READLINE] = false
155: when "--echo"
156: @CONF[:ECHO] = true
157: when "--noecho"
158: @CONF[:ECHO] = false
159: when "--verbose"
160: @CONF[:VERBOSE] = true
161: when "--noverbose"
162: @CONF[:VERBOSE] = false
163: when "--prompt-mode", "--prompt"
164: prompt_mode = ARGV.shift.upcase.tr("-", "_").intern
165: @CONF[:PROMPT_MODE] = prompt_mode
166: when "--noprompt"
167: @CONF[:PROMPT_MODE] = :NULL
168: when "--inf-ruby-mode"
169: @CONF[:PROMPT_MODE] = :INF_RUBY
170: when "--sample-book-mode", "--simple-prompt"
171: @CONF[:PROMPT_MODE] = :SIMPLE
172: when "--tracer"
173: @CONF[:USE_TRACER] = true
174: when "--back-trace-limit"
175: @CONF[:BACK_TRACE_LIMIT] = ARGV.shift.to_i
176: when "--context-mode"
177: @CONF[:CONTEXT_MODE] = ARGV.shift.to_i
178: when "--single-irb"
179: @CONF[:SINGLE_IRB] = true
180: when "--irb_debug"
181: @CONF[:DEBUG_LEVEL] = ARGV.shift.to_i
182: when "-v", "--version"
183: print IRB.version, "\n"
184: exit 0
185: when "-h", "--help"
186: require "irb/help"
187: IRB.print_usage
188: exit 0
189: when /^-/
190: IRB.fail UnrecognizedSwitch, opt
191: else
192: @CONF[:SCRIPT] = opt
193: $0 = opt
194: break
195: end
196: end
197: if RUBY_VERSION >= FEATURE_IOPT_CHANGE_VERSION
198: load_path.collect! do |path|
199: /\A\.\// =~ path ? path : File.expand_path(path)
200: end
201: end
202: $LOAD_PATH.unshift(*load_path)
203: end
# File lib/irb/help.rb, line 14
14: def IRB.print_usage
15: lc = IRB.conf[:LC_MESSAGES]
16: path = lc.find("irb/help-message")
17: space_line = false
18: File.foreach(path) do
19: |l|
20: if /^\s*$/ =~ l
21: lc.puts l unless space_line
22: space_line = true
23: next
24: end
25: space_line = false
26:
27: l.sub!(/#.*$/, "")
28: next if /^\s*$/ =~ l
29: lc.puts l
30: end
31: end
# File lib/irb/init.rb, line 222
222: def IRB.rc_file(ext = IRBRC_EXT)
223: if !@CONF[:RC_NAME_GENERATOR]
224: rc_file_generators do |rcgen|
225: @CONF[:RC_NAME_GENERATOR] ||= rcgen
226: if File.exist?(rcgen.call(IRBRC_EXT))
227: @CONF[:RC_NAME_GENERATOR] = rcgen
228: break
229: end
230: end
231: end
232: @CONF[:RC_NAME_GENERATOR].call ext
233: end
enumerate possible rc-file base name generators
# File lib/irb/init.rb, line 236
236: def IRB.rc_file_generators
237: if irbrc = ENV["IRBRC"]
238: yield proc{|rc| rc == "rc" ? irbrc : irbrc+rc}
239: end
240: if home = ENV["HOME"]
241: yield proc{|rc| home+"/.irb#{rc}"}
242: end
243: home = Dir.pwd
244: yield proc{|rc| home+"/.irb#{rc}"}
245: yield proc{|rc| home+"/irb#{rc.sub(/\A_?/, '.')}"}
246: yield proc{|rc| home+"/_irb#{rc}"}
247: yield proc{|rc| home+"/$irb#{rc}"}
248: end
running config
# File lib/irb/init.rb, line 206
206: def IRB.run_config
207: if @CONF[:RC]
208: begin
209: load rc_file
210: rescue LoadError, Errno::ENOENT
211: rescue
212: print "load error: #{rc_file}\n"
213: print $!.class, ": ", $!, "\n"
214: for err in $@[0, $@.size - 2]
215: print "\t", err, "\n"
216: end
217: end
218: end
219: end
initialize config
# File lib/irb/init.rb, line 16
16: def IRB.setup(ap_path)
17: IRB.init_config(ap_path)
18: IRB.init_error
19: IRB.parse_opts
20: IRB.run_config
21: IRB.load_modules
22:
23: unless @CONF[:PROMPT][@CONF[:PROMPT_MODE]]
24: IRB.fail(UndefinedPromptMode, @CONF[:PROMPT_MODE])
25: end
26: end
initialize IRB and start TOP_LEVEL irb
# File lib/irb.rb, line 51
51: def IRB.start(ap_path = nil)
52: $0 = File::basename(ap_path, ".rb") if ap_path
53:
54: IRB.setup(ap_path)
55:
56: if @CONF[:SCRIPT]
57: irb = Irb.new(nil, @CONF[:SCRIPT])
58: else
59: irb = Irb.new
60: end
61:
62: @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
63: @CONF[:MAIN_CONTEXT] = irb.context
64:
65: trap("SIGINT") do
66: irb.signal_handle
67: end
68:
69: begin
70: catch(:IRB_EXIT) do
71: irb.eval_input
72: end
73: ensure
74: irb_at_exit
75: end
76: # print "\n"
77: end