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/proto/pjl/client.rb
Views: 11702
# -*- coding: binary -*-12# https://en.wikipedia.org/wiki/Printer_Job_Language3# See external links for PJL spec45module Rex::Proto::PJL6class Client78def initialize(sock)9@sock = sock10end1112# Begin a PJL job13#14# @return [void]15def begin_job16@sock.put("#{UEL}#{PREFIX}\n")17end1819# End a PJL job20#21# @return [void]22def end_job23@sock.put(UEL)24end2526# Send an INFO request and read the response27#28# @param category [String] INFO category29# @return [String] INFO response30def info(category)31categories = {32:id => Info::ID,33:status => Info::STATUS,34:variables => Info::VARIABLES,35:filesys => Info::FILESYS36}3738unless categories.has_key?(category)39raise ArgumentError, "Unknown INFO category"40end4142@sock.put("#{categories[category]}\n")43@sock.get(DEFAULT_TIMEOUT)44end4546# Get version information47#48# @return [String] Version information49def info_id50id = nil5152if info(:id) =~ /"(.*?)"/m53id = $154end5556id57end5859# Get environment variables60#61# @return [String] Environment variables62def info_variables63env_vars = nil6465if info(:variables) =~ /#{Info::VARIABLES}\r?\n(.*?)\f/m66env_vars = $167end6869env_vars70end7172# List volumes73#74# @return [String] Volume listing75def info_filesys76filesys = nil7778if info(:filesys) =~ /\[\d+ TABLE\]\r?\n(.*?)\f/m79filesys = $180end8182filesys83end8485# Get the ready message86#87# @return [String] Ready message88def get_rdymsg89rdymsg = nil9091if info(:status) =~ /DISPLAY="(.*?)"/m92rdymsg = $193end9495rdymsg96end9798# Set the ready message99#100# @param message [String] Ready message101# @return [void]102def set_rdymsg(message)103@sock.put(%Q{#{RDYMSG} DISPLAY = "#{message}"\n})104end105106# Initialize a volume107#108# @param volume [String] Volume109# @return [void]110def fsinit(volume)111if volume !~ /^[0-2]:$/112raise ArgumentError, "Volume must be 0:, 1:, or 2:"113end114115@sock.put(%Q{#{FSINIT} VOLUME = "#{volume}"\n})116end117118# Query a file119#120# @param path [String] Remote path121# @return [Boolean] True if file exists122def fsquery(path)123if path !~ /^[0-2]:/124raise ArgumentError, "Path must begin with 0:, 1:, or 2:"125end126127file = false128129@sock.put(%Q{#{FSQUERY} NAME = "#{path}"\n})130131if @sock.get(DEFAULT_TIMEOUT) =~ /TYPE=(FILE|DIR)/m132file = true133end134135file136end137138# List a directory139#140# @param path [String] Remote path141# @param count [Integer] Number of entries to list142# @return [String] Directory listing143def fsdirlist(path, count = COUNT_MAX)144if path !~ /^[0-2]:/145raise ArgumentError, "Path must begin with 0:, 1:, or 2:"146end147148listing = nil149150@sock.put(%Q{#{FSDIRLIST} NAME = "#{path}" ENTRY=1 COUNT=#{count}\n})151152if @sock.get(DEFAULT_TIMEOUT) =~ /ENTRY=1\r?\n(.*?)\f/m153listing = $1154end155156listing157end158159# Download a file160#161# @param path [String] Remote path162# @return [String] File as a string163def fsupload(path)164if path !~ /^[0-2]:/165raise ArgumentError, "Path must begin with 0:, 1:, or 2:"166end167168file = nil169170@sock.put(%Q{#{FSUPLOAD} NAME = "#{path}" OFFSET=0 SIZE=#{SIZE_MAX}\n})171172if @sock.get(DEFAULT_TIMEOUT) =~ /SIZE=\d+\r?\n(.*)\f/m173file = $1174end175176file177end178179# Upload a file or write string data to remote path180#181# @param data_or_lpath [String] data or local path182# @param rpath [String] Remote path183# @param is_file [Boolean] True if data_or_lpath is a local file path184# @return [Boolean] True if the file was uploaded185def fsdownload(data_or_lpath, rpath, is_file: true)186if rpath !~ /^[0-2]:/187raise ArgumentError, "Path must begin with 0:, 1:, or 2:"188end189190file = is_file ? File.read(data_or_lpath) : data_or_lpath191192@sock.put(193%Q{#{FSDOWNLOAD} FORMAT:BINARY SIZE=#{file.length} NAME = "#{rpath}"\n}194)195196@sock.put(file)197@sock.put(UEL)198199fsquery(rpath)200end201202# Delete a file203#204# @param path [String] Remote path205# @return [Boolean] True if the file was deleted206def fsdelete(path)207if path !~ /^[0-2]:/208raise ArgumentError, "Path must begin with 0:, 1:, or 2:"209end210211@sock.put(%Q{#{FSDELETE} NAME = "#{path}"\n})212213!fsquery(path)214end215216end217end218219220