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