#!/usr/bin/python

import sys, re


header = """/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#ifndef %s
#define %s

#include <iostypes.h>
#if defined(_LANGUAGE_C) 
#include <iosiobuf.h> 

"""

footer = """
#endif
#endif
#endif /* %s */
"""

def fileName(name):
  return '__' + name.upper().replace('.', '_') + '__'
 
def parse_syscalls(name):
  f = open(name)
  calls = []
  for l in f.readlines():
    l = l.strip()
    if l != "":
      result = re.match('([a-zA-Z0-9_]*\s*\**)([^(]*)\((.*)\)', l)
      ret = result.group(1)
      func = result.group(2)
      params = []
      for p in result.group(3).split(','):
        result = re.match('([a-zA-Z0-9_]*\s*\**)(.*)', p.strip())
        params.append((result.group(1), result.group(2)))
      calls.append((ret, func, params));
  return calls

if __name__ == "__main__":
  calls = parse_syscalls(sys.argv[1])
  out = open(sys.argv[3], "w")
  mod = sys.argv[2]
  f = fileName(sys.argv[3])
  out.write(header % (f, f))
  if mod == "IOSC":
    out.write("#include <iosctypes.h>\n")
  out.write("\n#if !defined(ASSEMBLER)\n\n")
  for call in calls:
    s = "%s%s_%s" %  (call[0], mod, call[1])
    s = s + "("
    prefix = ""
    for p in call[2]:
      s = s + "%s%s%s" % (prefix, p[0], p[1])
      prefix = ", "
    s = s + ");"
    out.write(s + '\n')
  out.write(footer % (f))
  out.close()
