<commit_before>require 'ruby-debug/interface'
require 'ruby-debug/command'

module Debugger

  annotate = 0
  
  class CommandProcessor # :nodoc:
    attr_accessor :interface
    attr_reader   :display
    
    @@Show_breakpoints_postcmd = ["break", "tbreak", "disable", "enable",
                                  "condition", "clear", "delete"]
    @@Show_annotations_preloop = ["step", "continue", "next", "finish"]
    @@Show_annotations_postcmd = ["down", "frame", "up"]
    
    def initialize(interface = LocalInterface.new)
      @interface = interface
      @display = []
      
      @mutex = Mutex.new
      @last_cmd = nil
      @actions = []
      @last_file = nil   # Filename the last time we stopped
      @last_line = nil   # line number the last time we stopped
      @output_annotation_in_progress = false
    end
    
    def interface=(interface)
      @mutex.synchronize do
        @interface.close if @interface
        @interface = interface
      end
    end
    
    require 'pathname'  # For cleanpath
    
    # Regularize file name. 
    # This is also used as a common funnel place if basename is 
    # desired or if we are working remotely and want to change the 
    # basename. Or we are eliding filenames.
    def self.canonic_file(filename)
      # For now we want resolved filenames 
      if Command.settings[:basename]
        File.basename(filename)
      else
        # Cache this?
        Pathname.new(filename).cleanpath.to_s
      end
    end

    def self.print_location_and_text(file, line)
      print "#{"\032\032" if ENV['EMACS']}#{canonic_file(file)}:#{line}\n" +
        "#{Debugger.line_at(file, line)}"
    end
  
    def self.protect(mname)
      alias_method "__#{mname}", mname
      module_eval %{
        def #{mname}(*args)
          @mutex.synchronize do
            return unless @interface
            __#{mname}(*args)
          end
        rescue IOError, Errno::EPIPE
          self.interface = nil
        rescue Exception
          print "INTERNAL ERROR!!! #\{$!\}\n" rescue nil
          print $!.backtrace.map{|l| "\t#\{l\}"}.join("\n") rescue nil
        end
      }
    end
    
    def at_breakpoint(context, breakpoint)
      n = Debugger.breakpoints.index(breakpoint) + 1
      print "\032\032%s:%d\n", 
      CommandProcessor.canonic_file(breakpoint.source), 
      breakpoint.pos if ENV['EMACS']
      print "Breakpoint %d at %s:%s\n", n, breakpoint.source, breakpoint.pos
    end
    protect :at_breakpoint
    
    def at_catchpoint(context, excpt)
      print "\032\032%s:%d\n", 
      CommandProcessor.canonic_file(context.frame_file(1)), 
      context.frame_line(1) if ENV['EMACS']
      print "Catchpoint at %s:%d: `%s' (%s)\n", 
      CommandProcessor.canonic_file(context.frame_file(1)), 
      context.frame_line(1), excpt, excpt.class
      fs = context.stack_size
      tb = caller(0)[-fs..-1]
      if tb
        for i in tb
          print "\tfrom %s\n", i
        end
      end
    end
    protect :at_catchpoint
    
    def at_tracing(context, file, line)
      @last_file = CommandProcessor.canonic_file(file)
      file = CommandProcessor.canonic_file(file)
      unless file == @last_file and @last_line == line and 
          Command.settings[:tracing_plus]
        print "Tracing(%d):%s:%s %s",
        context.thnum, file, line, Debugger.line_at(file, line)
        @last_file = file
        @last_line = line
      end
      always_run(context, file, line, 2)
    end
    protect :at_tracing

    def at_line(context, file, line)
      process_commands(context, file, line)
    end
    protect :at_line
    
    private

    # Callers of this routine should make sure to use comma to
    # separate format argments rather than %. Otherwise it seems that
    # if the string you want to print has format specifier, which
    # could happen if you are trying to show say a source-code line
    # with "puts" or "print" in it, this print routine will give an<commit_msg>Fixed problem where debugger messages ended up in the 'output' buffer.<commit_after>require 'ruby-debug/interface'
require 'ruby-debug/command'

module Debugger

  annotate = 0
  
  class CommandProcessor # :nodoc:
    attr_accessor :interface
    attr_reader   :display
    
    @@Show_breakpoints_postcmd = ["break", "tbreak", "disable", "enable",
                                  "condition", "clear", "delete"]
    @@Show_annotations_preloop = ["step", "continue", "next", "finish"]
    @@Show_annotations_postcmd = ["down", "frame", "up"]
    
    def initialize(interface = LocalInterface.new)
      @interface = interface
      @display = []
      
      @mutex = Mutex.new
      @last_cmd = nil
      @actions = []
      @last_file = nil   # Filename the last time we stopped
      @last_line = nil   # line number the last time we stopped
      @output_annotation_in_progress = false
    end
    
    def interface=(interface)
      @mutex.synchronize do
        @interface.close if @interface
        @interface = interface
      end
    end
    
    require 'pathname'  # For cleanpath
    
    # Regularize file name. 
    # This is also used as a common funnel place if basename is 
    # desired or if we are working remotely and want to change the 
    # basename. Or we are eliding filenames.
    def self.canonic_file(filename)
      # For now we want resolved filenames 
      if Command.settings[:basename]
        File.basename(filename)
      else
        # Cache this?
        Pathname.new(filename).cleanpath.to_s
      end
    end

    def self.print_location_and_text(file, line)
      print "#{"\032\032" if ENV['EMACS']}#{canonic_file(file)}:#{line}\n" +
        "#{Debugger.line_at(file, line)}"
    end
  
    def self.protect(mname)
      alias_method "__#{mname}", mname
      module_eval %{
        def #{mname}(*args)
          @mutex.synchronize do
            return unless @interface
            __#{mname}(*args)
          end
        rescue IOError, Errno::EPIPE
          self.interface = nil
        rescue Exception
          print "INTERNAL ERROR!!! #\{$!\}\n" rescue nil
          print $!.backtrace.map{|l| "\t#\{l\}"}.join("\n") rescue nil
        end
      }
    end
    
    def at_breakpoint(context, breakpoint)
      if @output_annotation_in_progress
        print "\032\032\n"
        @output_annotation_in_progress = false
      end

      n = Debugger.breakpoints.index(breakpoint) + 1
      print "\032\032%s:%d\n", 
      CommandProcessor.canonic_file(breakpoint.source), 
      breakpoint.pos if ENV['EMACS']
      print "Breakpoint %d at %s:%s\n", n, breakpoint.source, breakpoint.pos
    end
    protect :at_breakpoint
    
    def at_catchpoint(context, excpt)
      if @output_annotation_in_progress
        print "\032\032\n"
        @output_annotation_in_progress = false
      end

      print "\032\032%s:%d\n", 
      CommandProcessor.canonic_file(context.frame_file(1)), 
      context.frame_line(1) if ENV['EMACS']
      print "Catchpoint at %s:%d: `%s' (%s)\n", 
      CommandProcessor.canonic_file(context.frame_file(1)), 
      context.frame_line(1), excpt, excpt.class
      fs = context.stack_size
      tb = caller(0)[-fs..-1]
      if tb
        for i in tb
          print "\tfrom %s\n", i
        end
      end
    end
    protect :at_catchpoint
    
    def at_tracing(context, file, line)
      if @output_annotation_in_progress
        print "\032\032\n"
        @output_annotation_in_progress = false
      end

      @last_file = CommandProcessor.canonic_file(file)
      file = CommandProcessor.canonic_file(file)
      unless file == @last_file and @last_line == line and 
          Command.settings[:tracing_plus]
        print "Tracing(%d):%s:%s %s",
        context.thnum, file, line, Debugger.line_at(file, line)
        @last_file = file
        @last_line = line
      end
      always_run(context, file, line, 2)
    end
    protect :at_tracing

    def at_line(context, file, line)
      process_commands(context, file, line)
    end
    protect :at_line
    
    private

    # Callers of this routine should make sure to use comma to
    # separate format argments rather than %. Otherwise it seems that
    # if the string you want to print has format specifier, which
    # could happen if you are trying to show say a source-code line
    # with "puts" or "print" in it, this print routine will give an
{'fraction_non_alphanumeric': 0.10530719579249342, 'fraction_numerical': 0.008606263447286636, 'mean_word_length': 2.360240963855422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5079891', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2555, 'n_words': 702}