Class: Lich::Common::ExecScript

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

Overview

Class for executing scripts This class extends Script to handle execution of command scripts.

Examples:

Starting an exec script

ExecScript.start("command_data")

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, #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?, version

Constructor Details

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

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



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
# File 'documented/common/script.rb', line 959

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_dataObject (readonly)

Returns the value of attribute cmd_data.



882
883
884
# File 'documented/common/script.rb', line 882

def cmd_data
  @cmd_data
end

Class Method Details

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

Starts a new exec script

Parameters:

  • cmd_data (String)

    The command data to execute

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

    Options for the script execution

Returns:

  • (ExecScript, false)

    The started exec script or false if failed



888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# File 'documented/common/script.rb', line 888

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
        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_labelObject

rubocop:enable Lint/MissingSuper



1000
1001
1002
1003
# File 'documented/common/script.rb', line 1000

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