Skip to content

Instantly share code, notes, and snippets.

@enukane
Created February 14, 2015 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enukane/1ac64bfe21e5f08740f6 to your computer and use it in GitHub Desktop.
Save enukane/1ac64bfe21e5f08740f6 to your computer and use it in GitHub Desktop.
struct2packetdiag.rb
#!/usr/bin/env ruby
#
def dp str
if ENV['DEBUG'] == "1"
print ">> #{str}\n"
end
end
def type2size type
case type
when "u_int64_t"
return 64
when "u_int32_t"
return 32
when "u_int8_t"
return 8
when "time_t"
return 64
else
raise "unsupported type #{type}"
end
end
def parse_file f, basewidth, fontsize
diag = "{\n"
diag += "colwidth = #{basewidth}\n"
diag += "node_height = 32\n"
start = false
current = 0
while line = f.gets
dp ">> #{line}"
if line.match(/^struct\s+(.+)\s+\{$/)
dp "starts"
start = true
next
end
if line.match(/^\};$/)
dp "end"
break
end
ary = line.split
dp ary
type = ary[0]
name = ary[1].gsub(";", "")
size = type2size(type)
if size <= basewidth
diag += "#{current}-#{current+size-1}: #{name} (#{type}) [fontsize=#{fontsize}]\n"
else
num = size/basewidth
left = size % basewidth
diag += "#{current}-#{current + basewidth + left - 1}: #{name} (#{type}) [fontsize=#{fontsize}, colheight = #{num}]\n"
end
current += size
end
diag += "}\n"
return diag
end
# main
fname = ARGV.shift
if fname == nil
raise "no file name specified"
exit
end
basename = File.basename(fname)
outputdiag = basename + ".diag"
outputpng = basename + ".png"
diag = nil
File.open(fname) do |f|
diag = parse_file(f, 32, 18)
end
File.open(outputdiag, "w") do |f|
f.write(diag)
end
system("packetdiag #{outputdiag} -o #{outputpng}")
print "DONE: output to #{outputpng}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment