# Given the Postscript for TR UCB//CSD-98-1029, extract the syscall
# appendix

import sys

ps = file(sys.argv[1])
for l in ps:
    if "Appendix: System Calls" in l:
        break
else:
    raise RuntimeError, "Couldn't find appendix"
ps.next()
ps.next()
for l in ps:
    if l.strip() == "0 F":
        break
else:
    raise RuntimeError, "Couldn't find appendix"

# Get the strings in each cell of the table
strings = []
for l in ps:
    if l.startswith("("):
        strings.append(l[1:l.find(")")])
    else:
        break
ps.close()

# Undo ligatures
strings = [s.replace("\\336", "fi").replace("\\337", "fl") for s in strings]
# Fix numbers and text that got split into multiple strings
s2 = []
while strings:
    n = strings.pop(0)
    if len(s2) and s2[-1].isdigit() and n.isdigit():
        s2[-1] += n
    elif len(s2) and s2[-1].endswith("of") and n.startswith("fset"):
        s2[-1] += n
    else:
        s2.append(n)
strings = s2

while strings:
    syscall = strings.pop(0)
    num = int(strings.pop(0))
    args = strings.pop(0)
    if args == "none" or args == "removed from processed traces":
        args = []
    else:
        args = args.split()
    print num, syscall, args
