1#! /usr/bin/env python
2###
3#
4#       build_glob.py : Build the global_functions.h and global_functions.c
5#                       files which are required to implement the user
6#                       interface to global variables now that thread specific
7#                       data (TSD) is used to emulate global state.
8#
9#       See Copyright for the status of this software.
10#       Gary.Pennington@sun.com
11###
12import os, string
13
14class globvar:
15    def __init__(self, type, name):
16        self.type=type
17        self.name=name
18
19def striplinesep(line):
20    while line and line[-1] in ('\r','\n'):
21        line = line[:-1]
22    return line
23
24def writeline(file, line=None):
25    if line:
26        file.write(line)
27    file.write("\n")
28
29if __name__ == "__main__":
30    globals={}
31    global_data=open("global.data").readlines()
32    global_code=open("globals.c").readlines()
33    global_hdr=open("include/libxml/globals.h").readlines()
34    global_functions_hdr=open("include/libxml/globals.h", "w+")
35    global_functions_impl=open("globals.c", "w+")
36
37    #
38    # Rebuild the beginning of the file up to the
39    # Automatically generated string
40    #
41    for line in global_hdr:
42        line = striplinesep(line)
43        if line == " * Automatically generated by build_glob.py.":
44	    break
45	writeline(global_functions_hdr, line)
46
47    writeline(global_functions_hdr, " * Automatically generated by build_glob.py.")
48    writeline(global_functions_hdr, " * Do not modify the previous line.")
49    writeline(global_functions_hdr, " */")
50    writeline(global_functions_hdr)
51
52    for line in global_code:
53        line = striplinesep(line)
54        if line == " * Automatically generated by build_glob.py.":
55	    break
56	writeline(global_functions_impl, line)
57
58    writeline(global_functions_impl, " * Automatically generated by build_glob.py.")
59    writeline(global_functions_impl, " * Do not modify the previous line.")
60    writeline(global_functions_impl, " */")
61    writeline(global_functions_impl)
62
63    # Now process the data and write it to the appropriate output file
64    for line in global_data:
65        if line[0]=='#':
66            continue
67        line = striplinesep(line)
68        fields = string.split(line, ",")
69        # Update the header file
70        writeline(global_functions_hdr)
71        global_functions_hdr.write("extern "+fields[0]+" *")
72        if fields[2]:
73            global_functions_hdr.write("(*")
74        global_functions_hdr.write("__"+fields[1]+"(void)")
75        if fields[2]:
76            global_functions_hdr.write(")"+fields[2])
77        writeline(global_functions_hdr,";")
78        writeline(global_functions_hdr, "#ifdef LIBXML_THREAD_ENABLED")
79        writeline(global_functions_hdr,"#define "+fields[1]+" \\")
80        writeline(global_functions_hdr,"(*(__"+fields[1]+"()))")
81        writeline(global_functions_hdr,"#else")
82        if fields[2]:
83            writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+fields[2]+";")
84        else:
85            writeline(global_functions_hdr,"LIBXML_DLL_IMPORT extern "+fields[0]+" "+fields[1]+";")
86        writeline(global_functions_hdr,"#endif")
87        # set/get for per-thread global defaults
88        if fields[3]:
89            writeline(global_functions_hdr,fields[0]+" "+fields[1][:3]+"ThrDef"+fields[1][3:]+"("+fields[0]+" v);")
90        # Update the implementation file
91        writeline(global_functions_impl)
92#        writeline(global_functions_impl, "extern "+fields[0]+" "+fields[1]+";")
93        writeline(global_functions_impl, "#undef\t"+fields[1])
94        writeline(global_functions_impl, fields[0]+" *")
95        if fields[2]:
96            global_functions_impl.write("(*")
97        global_functions_impl.write("__"+fields[1]+"(void)")
98        if fields[2]:
99            writeline(global_functions_impl, ")[]")
100        writeline(global_functions_impl, " {")
101        writeline(global_functions_impl, "    if (IS_MAIN_THREAD)")
102        writeline(global_functions_impl, "\treturn (&"+fields[1]+");")
103        writeline(global_functions_impl, "    else")
104        writeline(global_functions_impl, "\treturn (&xmlGetGlobalState()->"+fields[1]+");")
105        writeline(global_functions_impl, "}")
106        # set/get for per-thread global defaults
107        if fields[3]:
108            writeline(global_functions_impl,fields[0]+" "+fields[1][:3]+"ThrDef"+fields[1][3:]+"("+fields[0]+" v) {")
109            writeline(global_functions_impl,"    "+fields[0]+" ret;");
110            writeline(global_functions_impl,"    xmlMutexLock(xmlThrDefMutex);")
111            writeline(global_functions_impl,"    ret = "+fields[1][:3]+fields[1][3:]+"ThrDef;")
112            writeline(global_functions_impl,"    "+fields[1][:3]+fields[1][3:]+"ThrDef = v;")
113            writeline(global_functions_impl,"    xmlMutexUnlock(xmlThrDefMutex);")
114            writeline(global_functions_impl,"    return ret;")
115            writeline(global_functions_impl,"}")
116    # Terminate the header file with appropriate boilerplate
117    writeline(global_functions_hdr)
118    writeline(global_functions_hdr, "#ifdef __cplusplus")
119    writeline(global_functions_hdr, "}")
120    writeline(global_functions_hdr, "#endif")
121    writeline(global_functions_hdr)
122    writeline(global_functions_hdr, "#endif /* __XML_GLOBALS_H */")
123