Reverted character set back to ISO8859.
[tangerine.git] / scripts / config2c.py
blob4a4b77b23089a27970ba92bb588a8450b2c9bbf0
1 #!/usr/bin/python
3 # $Id$
5 # Converts config file for shared libraries to a C file
6 # with function prototypes.
7 # A *.conf file is searched in the current directory.
8 # Result is print to STDOUT.
10 import re
11 import glob
12 import sys
14 lvo = 5 # 1st functions has always LVO of 5
15 libtype = "struct Library" # default
16 libvar = "library" # default
17 mode = ""
18 tab = " " # tabulator for identation
19 wrapped = False # set to True if you want wrapping by "#ifdef __AROS__"
21 # regex for splitting line into rettype, retval, args, regs
22 linepatt = re.compile('(.+?)\s*(\w*)\s*\((.*?)\)\s*\((.*?)\)')
24 # regex for splitting arg into rettype and argname
25 argpatt = re.compile('\s*(.*?)\s*(\w+)\s*$')
27 # regex for splitting line into two parts
28 splitpatt = re.compile('([#\w]*?)\s+(.*)\s*$')
30 infiles = glob.glob("*.conf")
31 if len(infiles) != 1:
32 sys.stderr.write("There must be one *.conf in current directory")
33 sys.exit(1)
35 libname = infiles[0].rsplit(".")
36 libname = libname[0].capitalize()
38 infile = open(infiles[0], "r")
40 for line in infile:
41 parts = splitpatt.match(line)
42 if parts and parts.group(1) == "##begin":
43 mode = parts.group(2)
44 elif parts and parts.group(1) == "##end":
45 mode = ""
46 elif mode == "config":
47 if parts and parts.group(1) == "libbasetype":
48 libtype = parts.group(2)
49 elif mode == "functionlist":
50 res = linepatt.match(line)
51 if res:
52 rettype = res.group(1)
53 funcname = res.group(2)
54 args = res.group(3).split(",")
55 regs = res.group(4).split(",")
56 argcnt = len(args)
57 if wrapped:
58 print "#ifdef __AROS__"
59 print "AROS_LH%d(%s, %s, " %(argcnt, rettype, funcname)
60 for i in range(argcnt):
61 argres = argpatt.match(args[i])
62 print "%sAROS_LHA(%s, %s, %s)," %(tab, argres.group(1), argres.group(2), regs[i])
63 print "%s%s *, %s, %d, %s" %(tab, libtype, libvar, lvo, libname)
64 print ")\n{"
65 print "%sAROS_LIBFUNC_INIT" %(tab)
66 if wrapped:
67 print "#else"
68 print "#endif\n"
69 print "#ifdef __AROS__"
70 print "%sAROS_LIBFUNC_EXIT" %(tab)
71 if wrapped:
72 print "#endif"
73 print "}\n"
75 # even empty line increase LVO
76 lvo = lvo + 1
79 infile.close()