Class: Lich::Common::ExecScript

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

Overview

Represents an executable script that can be run within the Lich engine.

This class handles the execution of command scripts.

Constant Summary collapse

@@name_exec_mutex =
Mutex.new

Constants inherited from Script

Script::JUMP, Script::JUMP_ERROR

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, #kill_source, #killed_externally, #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, #custom?, 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?

Constructor Details

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

rubocop:disable Lint/MissingSuper



1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
# File 'documented/common/script.rb', line 1005

def initialize(cmd_data, flags = Hash.new)
  @cmd_data = cmd_data
  @custom = false
  @vars = Array.new
  @downstream_buffer = LimitedArray.new
  @downstream_buffer.max_size = 400
  @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_data ⇒ Object (readonly)

Returns the value of attribute cmd_data.



929
930
931
# File 'documented/common/script.rb', line 929

def cmd_data
  @cmd_data
end

Class Method Details

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

Starts a new executable script with the given command data.

Parameters:

  • cmd_data (String) —

    the command data to execute.

  • options (Hash) (defaults to: {}) —

    options for script execution.

Returns:

  • (ExecScript) —

    the newly started executable script instance.



935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
# File 'documented/common/script.rb', line 935

def self.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
        respond "--- Lich error: #{$!}"
        respond $!.backtrace.first
        Lich.log "Exception: #{$!}\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_label ⇒ Object

rubocop:enable Lint/MissingSuper



1046
1047
1048
1049
# File 'documented/common/script.rb', line 1046

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