#!/usr/bin/ruby require "cgi" require "P4" def create_ref (cgi, cmd, text) ref = "http://#{cgi.host}#{cgi.script_name}" if (cmd != "") ref += "?cmd="+cmd end cgi.a( "href" => ref ) do text end end def process_help(p4, cgi) array = Array.new commands = p4.run_help[0].split("\n") commands.each { |line| line.sub(/p4 help ([a-z]*)\s*(.*)/) { array << [$1, $2] } } # delete "command" => there is no "p4 help command" array.delete_if {|x| x[0] == "command" } # add undoc => the "killer" app array << [ "undoc", "unsupported or obsolete Perforce commands and options" ] display_array_as_table(cgi, array) end def process_list(p4, cgi, cmd) array = Array.new commands = p4.run_help(cmd)[0].split("\n")[3..-1] commands.each { |line| line.sub(/\t([a-z0-9]*)\w*(.*)/) { array << [$1, $2] } } display_array_as_table(cgi, array) end def display_array_as_table(cgi, array) cgi.table ("border" => "1", "cellpadding" => "10") do array.collect do |entry| cgi.tr do cgi.td do # replace the first entry by a link create_ref(cgi, entry[0], "p4 help " + entry[0]) end + cgi.td do entry[1] end end end.join("\n") end end =begin process_undoc Replaces every instance of 'p4 command' or 'p4 help command' with a reference to that command =end def process_references(p4, cgi, cmd) result = p4.run_help(cmd)[0] a = result.split("\n").each do |line| line.gsub!(/\'p4\s+(help\s+)?(\w*)\'/) do |s| s = create_ref( cgi,$2, "p4 #{$1}#{$2}"); end end cgi.pre do a.join("\n") end end def gen_page( cgi ) p4 = P4.new # use the environment settings instead p4.port = "1666" p4.connect cmd = cgi.has_key?('cmd') ? cgi['cmd'] : "" body = case cmd when "commands" then process_list(p4, cgi, cmd) when "simple" then process_list(p4, cgi, cmd) when "undoc" then process_references(p4, cgi, "undoc") when "" then process_help(p4, cgi) else process_references(p4, cgi, cmd) end cgi.out do cgi.html do cgi.body() do cgi.table( "border" => "1", "cellpadding" => "10") do cgi.tr do cgi.td do create_ref(cgi, "", "Overview") end + cgi.td do create_ref(cgi, "simple", "Simple Commands") end + cgi.td do create_ref(cgi, "commands", "All Commands") end + cgi.td do create_ref(cgi, "undoc", "Undocumented Features") end end end + cgi.hr + cgi.h1 do "P4 Help : " + cmd end + cgi.hr + body + cgi.hr+ cgi.pre do info = p4.run_info[0] "Server Version: " + info["serverVersion"] + "\t" + "Server: " + info["serverAddress"] end end end end end cgi = CGI.new( 'html4' ) begin gen_page( cgi ) rescue cgi.out do cgi.html do cgi.body do cgi.h1 { "Error" } + cgi.p { "Errors during script execution" } + cgi.pre { $!.message } + cgi.pre do $!.backtrace.join( "\n" ) end end end end end