Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/tsm.rb

Instance Method Summary collapse

Instance Method Details

#prompt(pat, timeout = 9999999) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/tsm.rb', line 237

def prompt(pat,timeout=9999999)
		buf = ''
		pat = [pat] unless pat.respond_to?('each')
		result = nil
		while true
return result if IO.select([self],nil,nil,timeout).nil? or eof?
begin
	buf << getc.chr
rescue Errno::EIO
	result = [buf,''] and break
end

pat.each { |p|
	if mat=p.match(buf)
		result = [buf,*mat.to_a] and break
	end
}
break if result
		end
		result
end

#response(str, timeout = 9999999, &block) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/tsm.rb', line 259

def response(str,timeout=9999999,&block)
	tbuf = '' and buf = ''
	mode = :chr
	while true
		result = nil and break if IO.select([self],nil,nil,timeout).nil? or eof?
		if mode == :chr
			begin
				tbuf << getc.chr
			rescue Errno::EIO
				result = result.to_s
				result << tbuf and break
			end
			buf << tbuf and tbuf = '' and mode = :line if str[0...tbuf.size] != tbuf
			buf << tbuf and result = buf and break if tbuf == str
			buf << tbuf if tbuf[-($/.size)..-1] == $/
			next
		end

		buf << gets and mode = :chr
		yield buf
		buf = ''
	end 
	result
end