Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/ui/text/resource.rb
Views: 11655
# -*- coding: binary -*-1require 'erb'23module Rex4module Ui5module Text67module Resource89# Processes a resource script file for the console.10#11# @param path [String] Path to a resource file to run12# @return [void]13def load_resource(path)14if path == '-'15resource_file = $stdin.read16path = 'stdin'17elsif ::File.exist?(path)18resource_file = ::File.read(path)19else20print_error("Cannot find resource script: #{path}")21return22end2324# Process ERB directives first25print_status "Processing #{path} for ERB directives."26erb = ERB.new(resource_file)27processed_resource = erb.result(binding)2829lines = processed_resource.each_line.to_a30bindings = {}31while lines.length > 03233line = lines.shift34break if not line35line.strip!36next if line.length == 037next if line =~ /^#/3839# Pretty soon, this is going to need an XML parser :)40# TODO: case matters for the tag and for binding names41if line =~ /<ruby/42if line =~ /\s+binding=(?:'(\w+)'|"(\w+)")(>|\s+)/43bin = ($~[1] || $~[2])44bindings[bin] = binding unless bindings.has_key? bin45bin = bindings[bin]46else47bin = binding48end49buff = ''50while lines.length > 051line = lines.shift52break if not line53break if line =~ /<\/ruby>/54buff << line55end56if ! buff.empty?57print_status("resource (#{path})> Ruby Code (#{buff.length} bytes)")58begin59eval(buff, bin)60rescue ::Interrupt61raise $!62rescue ::Exception => e63print_error("resource (#{path})> Ruby Error: #{e.class} #{e} #{e.backtrace}")64end65end66else67print_line("resource (#{path})> #{line}")68run_single(line)69end70end71end7273end7475end76end77end787980