Fix abspath() shell function in doclib/doxygen.sh
[senf.git] / doclib / pkgdraw
index 35a6b92..895330f 100755 (executable)
@@ -1,4 +1,33 @@
 #!/usr/bin/python
+#
+# Usage:
+#    pkgdraw <header> <outfile> [<parser names>...] [-- <cpp options>...]
+#
+# Extract packet structure from <header>. Write generated PNG diagram
+# to <outfile>.  If <parser names> is given, it is a list names of
+# parsers to generate diagrams for. All other parsers from the same
+# header file will be skipped.
+#
+# <cpp options> are parsed to the C preprocessor while processing the
+# header file.
+#
+# pkgdraw will interpret most SENF_PARSER statements, it does however
+# *not* understand GOTO statements. Special comments may be added
+# directly before or after a field to pass parameters to pkgdraw
+#
+#    SENF_PARSER_FIELD( id, senf::UInt16Parser ); //<pkgdraw: <option>, <option>...
+#
+# or
+#
+#    //>pkgdraw: <option>, <option>...
+#    SENF_PARSER_FIELD( id, senf::UInt16Parser );
+#
+# <option> is any valid option:
+#
+#   hide                Completely skip this field (Helps with GOTO)
+#   name=<name>         Sets the field name to <name>
+#   size=<min>[-<max>]  Sets the field size in bits.
+#
 
 import sys, re, signal, tempfile, os, os.path, shutil, atexit
 
@@ -11,7 +40,6 @@ TEX_HEADER = r"""\documentclass{scrartcl}
 \usepackage[latin1]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{ae,aecompl}
-\usepackage[active]{srcltx}
 
 \usepackage{color}
 \usepackage{bytefield}
@@ -63,7 +91,7 @@ def formatSimpleField(width, start, field):
             namesz = areas[i]['size']
             nameix = i
     areas[nameix]['name'] = field['name'][:int(areas[nameix]['size'] * charsPerBit)]
-    if len(areas) == 2 and areas[0].get('bottom',True):
+    if field['name'] and len(areas) == 2 and areas[0].get('bottom',True):
         if areas[0].get('name','') : ix = 1
         else                       : ix = 0
         if 6 <= int(areas[ix]['size'] * charsPerBit):
@@ -158,7 +186,7 @@ def makeTex(rows):
             if sides == "lrtb" : sides = ""
             else               : sides = "[%s]" % sides
             if area.get('filled', False):
-                line.append(r"\bitbox%s{%s}{\color[gray]{0.7}\rule{\width}{\height}}" % (sides, area['size']))
+                line.append(r"\bitbox%s{%s}{\color[gray]{0.93}\rule{\width}{\height}}" % (sides, area['size']))
             elif area.get('skip', False):
                 line.append(r"\skippedwords")
             elif area.get('dots', False):
@@ -355,17 +383,42 @@ PARSER_END_RE = re.compile(r"PKGDRAW_PARSER_FINALIZE\s*\(([^)]*)\)\s*;")
 PARSER_FIELD_RE = re.compile(r"(?:@@>pkgdraw:(.*)$\s*)?PKGDRAW_PARSER_([A-Z_]+)\s*\(([^;]*)\)\s*;(?:\s*@@<pkgdraw:(.*)$)?", re.M)
 
 def scanPackets(data):
+    global FIELD_TYPES
+    
     packets = {}
+    packetOrder = []
     end = 0
     while True:
         start =  PARSER_START_RE.search(data, end)
-        if not start: return packets
+        if not start: return (packets, packetOrder)
         start = start.end(0)
         end = PARSER_END_RE.search(data, start)
-        if not end: return packets
+        if not end: return (packets, packetOrder)
         name=end.group(1).strip()
         end = end.start(0)
         packets[name] = scanFields(data[start:end])
+        packetOrder.append(name)
+        minsize = maxsize = 0
+        for field in packets[name]:
+            if maxsize is not None:
+                if field.get('repeat', False):
+                    maxsize = None
+                elif field.get('size', None) is not None:
+                    maxsize += field['size']
+                elif field.get('minsize', None) is not None:
+                    maxsize += field['maxsize']
+                else:
+                    maxsize = None
+            if not field.get('optional', False):
+                if field.get('size', None) is not None:
+                    minsize += field['size']
+                elif field.get('minsize', None) is not None:
+                    minsize += field['minsize']
+        if minsize is not None and maxsize is not None:
+            if minsize == maxsize:
+                FIELD_TYPES[name] = { 'size' : minsize }
+            else:
+                FIELD_TYPES[name] = { 'minsize' : minsize, 'maxsize' : maxsize }
 
 def scanFields(data):
     fields = []
@@ -411,7 +464,8 @@ names = []
 gccopts = []
 
 if len(args)<2 or args[0] == '--' or args[1] == '--':
-    sys.stderr.write("Usage: %s <header> <outfile> [<parser name>...] [ -- <cpp options>...]\n")
+    sys.stderr.write("Usage: %s <header> <outfile> [<parser names>...] [-- <cpp options>...]\n"
+                     % sys.argv[0])
     sys.exit(1)
 
 source = args.pop(0)
@@ -420,15 +474,15 @@ target = args.pop(0)
 while args and args[0] != '--' : names.append(args.pop(0))
 if args : gccopts = args[1:]
 
-data = scanPackets(cppExpand(quoteMacros(stripComments(file(source).read())),
-                             gccopts, os.path.dirname(source)))
+data, order = scanPackets(cppExpand(quoteMacros(stripComments(file(source).read())),
+                                    gccopts, os.path.dirname(source)))
 
 texf = file(os.path.join(tmpdir, "fields.tex"),"w")
 texf.write(TEX_HEADER)
 
 if not names:
-    names = data.keys()
-    names.sort()
+    order.reverse()
+    names = order
 
 for name in names:
     texf.write("\\textbf{%s}\n\\bigskip\\par\n" % texquote(name))