summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/lib/remote-tk.rb
blob: 443d66010c3df63b43dd7b8201f929e0c16a308c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#
#               remote-tk.rb - supports to control remote Tk interpreters
#                       by Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>

if defined? MultiTkIp
  fail RuntimeError, "'remote-tk' library must be required before requiring 'multi-tk'"
end

class MultiTkIp; end
class RemoteTkIp < MultiTkIp; end

class MultiTkIp
  @@IP_TABLE = TkUtil.untrust({}) unless defined?(@@IP_TABLE)
  @@TK_TABLE_LIST = TkUtil.untrust([]) unless defined?(@@TK_TABLE_LIST)
  def self._IP_TABLE; @@IP_TABLE; end
  def self._TK_TABLE_LIST; @@TK_TABLE_LIST; end

  @flag = true
  def self._DEFAULT_MASTER
    # work only once
    if @flag
      @flag = nil
      @@DEFAULT_MASTER
    else
      nil
    end
  end
end
class RemoteTkIp
  @@IP_TABLE = MultiTkIp._IP_TABLE unless defined?(@@IP_TABLE)
  @@TK_TABLE_LIST = MultiTkIp._TK_TABLE_LIST unless defined?(@@TK_TABLE_LIST)
end
class << MultiTkIp
  undef _IP_TABLE
  undef _TK_TABLE_LIST
end

require 'multi-tk'

class RemoteTkIp
  if defined?(@@DEFAULT_MASTER)
    MultiTkIp._DEFAULT_MASTER
  else
    @@DEFAULT_MASTER = MultiTkIp._DEFAULT_MASTER
  end
end


###############################

class << RemoteTkIp
  undef new_master, new_slave, new_safe_slave
  undef new_trusted_slave, new_safeTk

  def new(*args, &b)
    ip = __new(*args)
    ip.eval_proc(&b) if b
    ip
  end
end

class RemoteTkIp
  def initialize(remote_ip, displayof=nil, timeout=5)
    if $SAFE >= 4
      fail SecurityError, "cannot access another interpreter at level #{$SAFE}"
    end

    @interp = MultiTkIp.__getip
    if @interp.safe?
      fail SecurityError, "safe-IP cannot create RemoteTkIp"
    end


    @interp.allow_ruby_exit = false
    @appname = @interp._invoke('tk', 'appname')
    @remote = remote_ip.to_s.dup.freeze
    if displayof.kind_of?(TkWindow)
      @displayof = displayof.path.dup.freeze
    else
      @displayof = nil
    end
    if self.deleted?
      fail RuntimeError, "no Tk application named \"#{@remote}\""
    end

    @tk_windows = {}
    @tk_table_list = []
    @slave_ip_tbl = {}
    @slave_ip_top = {}

    @force_default_encoding ||= TkUtil.untrust([false])
    @encoding ||= TkUtil.untrust([nil])
    def @encoding.to_s; self.join(nil); end

    TkUtil.untrust(@tk_windows)    unless @tk_windows.tainted?
    TkUtil.untrust(@tk_table_list) unless @tk_table_list.tainted?
    TkUtil.untrust(@slave_ip_tbl)  unless @slave_ip_tbl.tainted?
    TkUtil.untrust(@slave_ip_top)  unless @slave_ip_top.tainted?

    @system = Object.new

    @threadgroup  = ThreadGroup.new

    @safe_level = [$SAFE]

    @wait_on_mainloop = [true, 0]

    @cmd_queue = Queue.new

=begin
    @cmd_receiver, @receiver_watchdog = _create_receiver_and_watchdog()

    @threadgroup.add @cmd_receiver
    @threadgroup.add @receiver_watchdog

    @threadgroup.enclose
=end
    @@DEFAULT_MASTER.assign_receiver_and_watchdog(self)

    @@IP_TABLE[@threadgroup] = self
    @@TK_TABLE_LIST.size.times{
      (tbl = {}).tainted? || TkUtil.untrust(tbl)
      @tk_table_list << tbl
    }

    @ret_val = TkVariable.new
    if timeout > 0 && ! _available_check(timeout)
      fail RuntimeError, "cannot create connection"
    end
    @ip_id = _create_connection

    class << self
      undef :instance_eval
    end

    self.freeze  # defend against modification
  end

  def manipulable?
    return true if (Thread.current.group == ThreadGroup::Default)
    MultiTkIp.__getip == @interp && ! @interp.safe?
  end
  def self.manipulable?
    true
  end

  def _is_master_of?(tcltkip_obj)
    tcltkip_obj == @interp
  end
  protected :_is_master_of?

  def _ip_id_
    @ip_id
  end

  def _available_check(timeout = 5)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    return nil if timeout < 1
    @ret_val.value = ''
    @interp._invoke('send', '-async', @remote,
                    'send', '-async', Tk.appname,
                    "set #{@ret_val.id} ready")
    Tk.update
    if @ret_val != 'ready'
      (1..(timeout*5)).each{
        sleep 0.2
        Tk.update
        break if @ret_val == 'ready'
      }
    end
    @ret_val.value == 'ready'
  end
  private :_available_check

  def _create_connection
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    ip_id = '_' + @interp._invoke('send', @remote, <<-'EOS') + '_'
      if {[catch {set _rubytk_control_ip_id_} ret] != 0} {
        set _rubytk_control_ip_id_ 0
      } else {
        set _rubytk_control_ip_id_ [expr $ret + 1]
      }
      return $_rubytk_control_ip_id_
    EOS

    @interp._invoke('send', @remote, <<-EOS)
      proc rb_out#{ip_id} args {
        send #{@appname} rb_out \$args
      }
    EOS

    ip_id
  end
  private :_create_connection

  def _appsend(enc_mode, async, *cmds)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    p ['_appsend', [@remote, @displayof], enc_mode, async, cmds] if $DEBUG
    if $SAFE >= 4
      fail SecurityError, "cannot send commands at level 4"
    elsif $SAFE >= 1 && cmds.find{|obj| obj.tainted?}
      fail SecurityError, "cannot send tainted commands at level #{$SAFE}"
    end

    cmds = @interp._merge_tklist(*TkUtil::_conv_args([], enc_mode, *cmds))
    if @displayof
      if async
        @interp.__invoke('send', '-async', '-displayof', @displayof,
                         '--', @remote, *cmds)
      else
        @interp.__invoke('send', '-displayof', @displayof,
                         '--', @remote, *cmds)
      end
    else
      if async
        @interp.__invoke('send', '-async', '--', @remote, *cmds)
      else
        @interp.__invoke('send', '--', @remote, *cmds)
      end
    end
  end
  private :_appsend

  def ready?(timeout=5)
    if timeout < 0
      fail ArgumentError, "timeout must be positive number"
    end
    _available_check(timeout)
  end

  def is_rubytk?
    return false if _appsend(false, false, 'info', 'command', 'ruby') == ""
    [ _appsend(false, false, 'ruby', 'RUBY_VERSION'),
      _appsend(false, false, 'set', 'tk_patchLevel') ]
  end

  def appsend(async, *args)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    if async != true && async != false && async != nil
      args.unshift(async)
      async = false
    end
    if @displayof
      Tk.appsend_displayof(@remote, @displayof, async, *args)
    else
      Tk.appsend(@remote, async, *args)
    end
  end

  def rb_appsend(async, *args)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    if async != true && async != false && async != nil
      args.unshift(async)
      async = false
    end
    if @displayof
      Tk.rb_appsend_displayof(@remote, @displayof, async, *args)
    else
      Tk.rb_appsend(@remote, async, *args)
    end
  end

  def create_slave(name, safe=false)
    if safe
      safe_opt = ''
    else
      safe_opt = '-safe'
    end
    _appsend(false, false, "interp create #{safe_opt} -- #{name}")
  end

  def make_safe
    fail RuntimeError, 'cannot change safe mode of the remote interpreter'
  end

  def safe?
    _appsend(false, false, 'interp issafe')
  end

  def safe_base?
    false
  end

  def allow_ruby_exit?
    false
  end

  def allow_ruby_exit= (mode)
    fail RuntimeError, 'cannot change mode of the remote interpreter'
  end

  def delete
    _appsend(false, true, 'exit')
  end

  def deleted?
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    if @displayof
      lst = @interp._invoke_without_enc('winfo', 'interps',
                                        '-displayof', @displayof)
    else
      lst = @interp._invoke_without_enc('winfo', 'interps')
    end
    # unless @interp._split_tklist(lst).index(@remote)
    unless @interp._split_tklist(lst).index(_toUTF8(@remote))
      true
    else
      false
    end
  end

  def has_mainwindow?
    raise SecurityError, "no permission to manipulate" unless self.manipulable?

    begin
      inf = @interp._invoke_without_enc('info', 'command', '.')
    rescue Exception
      return nil
    end
    if !inf.kind_of?(String) || inf != '.'
      false
    else
      true
    end
  end

  def invalid_namespace?
    false
  end

  def restart
    fail RuntimeError, 'cannot restart the remote interpreter'
  end

  def __eval(str)
    _appsend(false, false, str)
  end
  def _eval(str)
    _appsend(nil, false, str)
  end
  def _eval_without_enc(str)
    _appsend(false, false, str)
  end
  def _eval_with_enc(str)
    _appsend(true, false, str)
  end

  def _invoke(*args)
    _appsend(nil, false, *args)
  end

  def __invoke(*args)
    _appsend(false, false, *args)
  end
  def _invoke(*args)
    _appsend(nil, false, *args)
  end
  def _invoke_without_enc(*args)
    _appsend(false, false, *args)
  end
  def _invoke_with_enc(*args)
    _appsend(true, false, *args)
  end

  def _toUTF8(str, encoding=nil)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._toUTF8(str, encoding)
  end

  def _fromUTF8(str, encoding=nil)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._fromUTF8(str, encoding)
  end

  def _thread_vwait(var_name)
    _appsend(false, 'thread_vwait', varname)
  end

  def _thread_tkwait(mode, target)
    _appsend(false, 'thread_tkwait', mode, target)
  end

  def _return_value
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._return_value
  end

  def _get_variable(var_name, flag)
    # ignore flag
    _appsend(false, 'set', TkComm::_get_eval_string(var_name))
  end
  def _get_variable2(var_name, index_name, flag)
    # ignore flag
    _appsend(false, 'set', "#{TkComm::_get_eval_string(var_name)}(#{TkComm::_get_eval_string(index_name)})")
  end

  def _set_variable(var_name, value, flag)
    # ignore flag
    _appsend(false, 'set', TkComm::_get_eval_string(var_name), TkComm::_get_eval_string(value))
  end
  def _set_variable2(var_name, index_name, value, flag)
    # ignore flag
    _appsend(false, 'set', "#{TkComm::_get_eval_string(var_name)}(#{TkComm::_get_eval_string(index_name)})", TkComm::_get_eval_string(value))
  end

  def _unset_variable(var_name, flag)
    # ignore flag
    _appsend(false, 'unset', TkComm::_get_eval_string(var_name))
  end
  def _unset_variable2(var_name, index_name, flag)
    # ignore flag
    _appsend(false, 'unset', "#{var_name}(#{index_name})")
  end

  def _get_global_var(var_name)
    _appsend(false, 'set', TkComm::_get_eval_string(var_name))
  end
  def _get_global_var2(var_name, index_name)
    _appsend(false, 'set', "#{TkComm::_get_eval_string(var_name)}(#{TkComm::_get_eval_string(index_name)})")
  end

  def _set_global_var(var_name, value)
    _appsend(false, 'set', TkComm::_get_eval_string(var_name), TkComm::_get_eval_string(value))
  end
  def _set_global_var2(var_name, index_name, value)
    _appsend(false, 'set', "#{TkComm::_get_eval_string(var_name)}(#{TkComm::_get_eval_string(index_name)})", TkComm::_get_eval_string(value))
  end

  def _unset_global_var(var_name)
    _appsend(false, 'unset', TkComm::_get_eval_string(var_name))
  end
  def _unset_global_var2(var_name, index_name)
    _appsend(false, 'unset', "#{var_name}(#{index_name})")
  end

  def _split_tklist(str)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._split_tklist(str)
  end

  def _merge_tklist(*args)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._merge_tklist(*args)
  end

  def _conv_listelement(str)
    raise SecurityError, "no permission to manipulate" unless self.manipulable?
    @interp._conv_listelement(str)
  end

  def _create_console
    fail RuntimeError, 'not support "_create_console" on the remote interpreter'
  end

  def mainloop
    fail RuntimeError, 'not support "mainloop" on the remote interpreter'
  end
  def mainloop_watchdog
    fail RuntimeError, 'not support "mainloop_watchdog" on the remote interpreter'
  end
  def do_one_event(flag = nil)
    fail RuntimeError, 'not support "do_one_event" on the remote interpreter'
  end
  def mainloop_abort_on_exception
    fail RuntimeError, 'not support "mainloop_abort_on_exception" on the remote interpreter'
  end
  def mainloop_abort_on_exception=(mode)
    fail RuntimeError, 'not support "mainloop_abort_on_exception=" on the remote interpreter'
  end
  def set_eventloop_tick(*args)
    fail RuntimeError, 'not support "set_eventloop_tick" on the remote interpreter'
  end
  def get_eventloop_tick
    fail RuntimeError, 'not support "get_eventloop_tick" on the remote interpreter'
  end
  def set_no_event_wait(*args)
    fail RuntimeError, 'not support "set_no_event_wait" on the remote interpreter'
  end
  def get_no_event_wait
    fail RuntimeError, 'not support "get_no_event_wait" on the remote interpreter'
  end
  def set_eventloop_weight(*args)
    fail RuntimeError, 'not support "set_eventloop_weight" on the remote interpreter'
  end
  def get_eventloop_weight
    fail RuntimeError, 'not support "get_eventloop_weight" on the remote interpreter'
  end
end

class << RemoteTkIp
  def mainloop(*args)
    fail RuntimeError, 'not support "mainloop" on the remote interpreter'
  end
  def mainloop_watchdog(*args)
    fail RuntimeError, 'not support "mainloop_watchdog" on the remote interpreter'
  end
  def do_one_event(flag = nil)
    fail RuntimeError, 'not support "do_one_event" on the remote interpreter'
  end
  def mainloop_abort_on_exception
    fail RuntimeError, 'not support "mainloop_abort_on_exception" on the remote interpreter'
  end
  def mainloop_abort_on_exception=(mode)
    fail RuntimeError, 'not support "mainloop_abort_on_exception=" on the remote interpreter'
  end
  def set_eventloop_tick(*args)
    fail RuntimeError, 'not support "set_eventloop_tick" on the remote interpreter'
  end
  def get_eventloop_tick
    fail RuntimeError, 'not support "get_eventloop_tick" on the remote interpreter'
  end
  def set_no_event_wait(*args)
    fail RuntimeError, 'not support "set_no_event_wait" on the remote interpreter'
  end
  def get_no_event_wait
    fail RuntimeError, 'not support "get_no_event_wait" on the remote interpreter'
  end
  def set_eventloop_weight(*args)
    fail RuntimeError, 'not support "set_eventloop_weight" on the remote interpreter'
  end
  def get_eventloop_weight
    fail RuntimeError, 'not support "get_eventloop_weight" on the remote interpreter'
  end
end