]> thyrsus.com Git - mad.git/commitdiff
More careful error checking of print format codes.
authorEric S. Raymond <esr@thyrsus,com>
Wed, 18 Feb 2004 20:35:45 +0000 (20:35 +0000)
committerEric S. Raymond <esr@thyrsus,com>
Wed, 18 Feb 2004 20:35:45 +0000 (20:35 +0000)
mad

diff --git a/mad b/mad
index d71c52cdaf9fd04d35352b79d087aa5bebb9a1c4..ea5b5bd7d9735053b773a2f90b90e2a334c580b1 100755 (executable)
--- a/mad
+++ b/mad
@@ -783,7 +783,7 @@ def print_C_expr(t):
         res = res[1:-1]
     return res
 
-def fmt_convert(str, write=True):
+def fmt_convert(str):
     "Convert MAD format string to C printf/scanf"
     #print "->fmt_convert(%s)" % str
     fmt = str
@@ -820,7 +820,7 @@ def fmt_convert(str, write=True):
         # Now process the actual format item
         if str[0] == '(':
             i = str.find(')')
-            (innerfmt, innertypes) = fmt_convert(str[1:i]+"*", write=False)
+            (innerfmt, innertypes) = fmt_convert(str[1:i]+"*")
             res += innerfmt * count
             str = str[i+1:]
             for i in range(count):
@@ -840,8 +840,7 @@ def fmt_convert(str, write=True):
             str = str[i:]
             types += ['double'] * count
         elif str[0] == 'H':
-            if write:
-                res += str[1:count+1]
+            res += str[1:count+1]
             str = str[count+1:]
             types += ['char']
         elif str[0] == 'I':
@@ -857,13 +856,29 @@ def fmt_convert(str, write=True):
         else:
             sys.stderr.write('"%s", line %d: invalid field code %s at %d in format string %s!\n' % (sourcefile, sourceline, `str[0]`, len(fmt)-len(str), `fmt`))
             sys.exit(1)
-    # Clip off format character
-    if write:
-        res = res[1:]
-        res = res + '\\n'
     #print "<-fmt_convert() = %s" % res
     return (res, types)
 
+def lptcodes(res):
+    "Interpret format character"
+    if res[0] == ' ':          # Single space
+        res = res[1:]
+    elif res[0] == '0':                # Double space
+        res = res + r"\n" + res[1:]
+    elif res[0] == '+':                # No space
+        res = res[1:]
+    elif res[0] == '1':                # Skip to next page
+        res = res + r"\f" + res[1:]
+    elif res[0] == '2':                # Skip to next half page
+        res = res + r"\v\v" + res[1:]
+    elif res[0] == '4':                # Skip to next quarter page
+        res = res + r"\v" + res[1:]
+    else:                              # Anything else falls through 
+        sys.stderr.write('unknown print code %s ignored\n' % res[0])
+        res = res[1:]
+    res = res + '\\n'
+    return res
+
 def print_C_statement(statement):
     global currentfunc
     if statement[0] in ('boolean',
@@ -947,12 +962,13 @@ def print_C_statement(statement):
         return('printf("%o", ' + print_C_expr(statement[1]) + '); getchar();')
     elif statement[0] == 'print':
         (fmt, types) = fmt_convert(format[statement[1]])
+        fmt = lptcodes(fmt)
         if len(statement) < 3:
             return 'printf("' + fmt + '");'
         else:
             return 'printf("' + fmt + '", ' + ', '.join(map(print_C_expr, statement[2:])) + ');'
     elif statement[0] == 'read':
-        (fmt, types) = fmt_convert(format[statement[1]], write=False)
+        (fmt, types) = fmt_convert(format[statement[1]])
         inlist = []
         for (ispec, itype) in zip(statement[2:], types):
             if type(ispec) == type(""):