Class: Lich::Common::ExecScript

Inherits:
Script
  • Object
show all
Defined in:
lib/common/script.rb

Overview

Represents an executable script that can be run in a separate thread.

Constant Summary collapse

@@name_exec_mutex =
Mutex.new

Instance Attribute Summary collapse

Attributes inherited from Script

#at_exit_procs, #command_line, #current_label, #die_with, #downstream_buffer, #file_name, #hidden, #ignore_pause, #jump_label, #label_order, #match_stack_labels, #match_stack_strings, #name, #no_echo, #no_kill_all, #no_pause_all, #paused, #quiet, #safe, #silent, #unique_buffer, #upstream_buffer, #vars, #want_downstream, #want_downstream_xml, #want_script_output, #want_upstream, #watchfor

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Script

at_exit, #at_exit, #clear, clear_exit_procs, #clear_exit_procs, current, db, distrust, exists?, #exit, #exit!, exit!, #feedme_upstream, #gets, #gets?, #has_thread?, hidden, index, #instance_eval, #instance_variable_get, kill, #kill, #labels, list, list_trusted, log, #match_stack_add, #match_stack_clear, namescript_incoming, new_downstream, new_downstream_xml, new_script_output, new_upstream, open_file, pause, #pause, paused?, #paused?, run, running, running?, #safe?, self, #thread_group, #to_s, trust, #unique_gets, #unique_gets?, unpause, #unpause, #upstream_gets, #upstream_gets?, version

Constructor Details

#initialize(cmd_data, flags = Hash.new) ⇒ ExecScript

Note:

This method may modify the state of the parent class.

Initializes a new ExecScript instance.

FIXME: when modernized, ensure proper use of variables and init of parent class rubocop:disable Lint/MissingSuper

Examples:

exec_script = ExecScript.new("puts 'Hello, World!'", quiet: false)
exec_script.start

Parameters:

  • cmd_data (String)

    The command data to execute.

  • flags (Hash) (defaults to: Hash.new)

    A hash of options for the script, including :quiet and :name.



1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
# File 'lib/common/script.rb', line 1390

def initialize(cmd_data, flags = Hash.new)
  @cmd_data = cmd_data
  @vars = Array.new
  @downstream_buffer = LimitedArray.new
  @killer_mutex = Mutex.new
  @want_downstream = true
  @want_downstream_xml = false
  @upstream_buffer = LimitedArray.new
  @want_upstream = false
  @at_exit_procs = Array.new
  @watchfor = Hash.new
  @hidden = false
  @paused = false
  @silent = false
  if flags[:quiet].nil?
    @quiet = false
  else
    @quiet = flags[:quiet]
  end
  @safe = false
  @no_echo = false
  @thread_group = ThreadGroup.new
  @unique_buffer = LimitedArray.new
  @die_with = Array.new
  @no_pause_all = false
  @no_kill_all = false
  @match_stack_labels = Array.new
  @match_stack_strings = Array.new
  if flags[:name].nil?
    num = '1'; num.succ! while @@running.any? { |s| s.name == "exec#{num}" }
    @name = "exec#{num}"
  else
    num = '1'; num.succ! while @@running.any? { |s| s.name == "#{flags[:name]}#{num}" }
    @name = "#{flags[:name]}#{num}"
  end
  @@running.push(self)
end

Instance Attribute Details

#cmd_dataObject (readonly)

Returns the value of attribute cmd_data.



1296
1297
1298
# File 'lib/common/script.rb', line 1296

def cmd_data
  @cmd_data
end

Class Method Details

.start(cmd_data, options = {}) ⇒ ExecScript, false

Starts a new ExecScript with the given command data and options.

Examples:

script = ExecScript.start("puts 'Hello, World!'", quiet: false)

Parameters:

  • cmd_data (String)

    The command data to execute.

  • options (Hash, Boolean) (defaults to: {})

    Options for script execution. If true, sets quiet mode.

Returns:

  • (ExecScript, false)

    Returns the new ExecScript instance or false if it fails to start.

Raises:

  • (StandardError)

    Raises various exceptions based on execution errors.



1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
# File 'lib/common/script.rb', line 1306

def ExecScript.start(cmd_data, options = {})
  options = { :quiet => true } if options == true
  unless (new_script = ExecScript.new(cmd_data, options))
    respond '--- Lich: failed to start exec script'
    return false
  end
  new_thread = Thread.new {
    100.times { break if Script.current == new_script; sleep 0.01 }

    if (script = Script.current)
      Thread.current.priority = 1
      respond("--- Lich: #{script.name} active.") unless script.quiet
      begin
        script_binding = TRUSTED_SCRIPT_BINDING.call
        eval('script = Script.current', script_binding, script.name.to_s)
        eval(cmd_data, script_binding, script.name.to_s)
        Script.current.kill
      rescue SystemExit
        Script.current.kill
      rescue SyntaxError
        respond "--- SyntaxError: #{$!}"
        respond $!.backtrace.first
        Lich.log "SyntaxError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue ScriptError
        respond "--- ScriptError: #{$!}"
        respond $!.backtrace.first
        Lich.log "ScriptError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue NoMemoryError
        respond "--- NoMemoryError: #{$!}"
        respond $!.backtrace.first
        Lich.log "NoMemoryError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue LoadError
        respond("--- LoadError: #{$!}")
        respond "--- LoadError: #{$!}"
        respond $!.backtrace.first
        Lich.log "LoadError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue SecurityError
        respond "--- SecurityError: #{$!}"
        respond $!.backtrace[0..1]
        Lich.log "SecurityError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue ThreadError
        respond "--- ThreadError: #{$!}"
        respond $!.backtrace.first
        Lich.log "ThreadError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue SystemStackError
        respond "--- SystemStackError: #{$!}"
        respond $!.backtrace.first
        Lich.log "SystemStackError: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue StandardError # Exception
        respond "--- Exception: #{$!}"
        respond $!.backtrace.first
        Lich.log "Exception: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      rescue
        respond "--- Lich: error: #{$!}"
        respond $!.backtrace.first
        Lich.log "Error: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
        Script.current.kill
      end
    else
      respond 'start_exec_script screwed up...'
    end
  }
  new_script.thread_group.add(new_thread)
  new_script
end

Instance Method Details

#get_next_labelnil

Note:

This method is a placeholder and does not perform any action.

Retrieves the next label in the script.

Examples:

label = exec_script.get_next_label

Returns:

  • (nil)

    Always returns nil, as goto labels are not available in exec scripts.



1435
1436
1437
1438
# File 'lib/common/script.rb', line 1435

def get_next_label
  echo 'goto labels are not available in exec scripts.'
  nil
end