aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
authorbloodstalker <thabogre@gmail.com>2018-08-04 10:01:15 +0000
committerbloodstalker <thabogre@gmail.com>2018-08-04 10:01:15 +0000
commit35d02ccd72a09db1839a4aed6fc65de57c9a0809 (patch)
treeb4959ca73ac37c884c8a14cab2748e3c1ce007e6 /main.py
parentupdate (diff)
downloadfaultreiber-35d02ccd72a09db1839a4aed6fc65de57c9a0809.tar.gz
faultreiber-35d02ccd72a09db1839a4aed6fc65de57c9a0809.zip
update
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py71
1 files changed, 52 insertions, 19 deletions
diff --git a/main.py b/main.py
index fbf317e..b39aa83 100755
--- a/main.py
+++ b/main.py
@@ -177,12 +177,13 @@ class Argparser(object):
parser.add_argument("--static", action="store_true", help="statics reader funcs", default=False)
parser.add_argument("--verbose", action="store_true", help="verbose", default=False)
# TODO
- parser.add_argument("--forcenullterm", action="store_true", help="terminate all strings with null even if they are not null-terminated", default=False)
+ parser.add_argument("--forcenullterm", action="store_true", help="terminate all strings with null even if they are not originally null-terminated", default=False)
parser.add_argument("--strbuffersize", type=int, help="the size of the buffer for string reads", default=100)
parser.add_argument("--strbuffgrowfactor", type=float, help="the factor by which the strbuffer will grow", default=1.6)
parser.add_argument("--voidbuffersize", type=int, help="the size of the buffer for void* buffer", default=100)
parser.add_argument("--voidbuffgrowfactor", type=float, help="the factor by which the voidbuffer will grow", default=1.6)
parser.add_argument("--singlefile", action="store_true", help="the generated code will be put in a single file", default=False)
+ parser.add_argument("--singlefilename", type=str, help="name of the single file")
self.args = parser.parse_args()
def dupemake(path, main_name):
@@ -206,6 +207,26 @@ class CodeGen(object):
self.mem_size = {}
self.tree = xml.etree.ElementTree.parse(self.argparser.args.xml)
self.root = self.tree.getroot()
+ self.aggregate_source = ""
+ self.aggregate_source_h = ""
+ self.aggregate_flags = ""
+ self.read_source = ""
+ self.read_flags = ""
+ self.struct_source = ""
+ self.struct_flags = ""
+
+ def file_manager(self):
+ if self.argparser.args.singlefile:
+ name = self.argparser.args.singlefilename
+ self.read_source = self.argparser.args.outdir + "/" + name
+ self.aggregate_source = self.argparser.args.outdir + "/" + name
+ self.struct_source = self.argparser.args.outdir + "/" + name
+ else:
+ self.read_source = self.argparser.args.outdir + "/read.c"
+ self.aggregate_source = self.argparser.args.outdir + "/aggregate.c"
+ self.aggregate_source_h = self.argparser.args.outdir + "/aggregate.h"
+ self.struct_source_h = self.argparser.args.outdir + "/structs.h"
+ self.struct_source = self.argparser.args.outdir + "/structs.c"
def init_hook(self):
pass
@@ -238,12 +259,12 @@ class CodeGen(object):
def gen_reader_funcs(self):
temp_dec_list = []
- read_source = open(self.argparser.args.outdir + "/read.c", "w")
+
+ read_source = open(self.read_source, "w")
read_source.write("\n// automatically generated by faultrieber\n")
read_source.write("// " + self.dnt + "\n\n")
- read_source.write("#ifndef FT_READ_H\n#define FT_READ_H\n")
- read_source.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
read_source.write(text.header_list)
+ read_source.write('#include "./read.h"\n')
read_source.write('#include "./structs.h"\n\n')
inline = "inline " if self.argparser.args.inline else ""
static = "static " if self.argparser.args.static else ""
@@ -319,20 +340,27 @@ class CodeGen(object):
read_source.write(text.c_read_gen.replace("XXX", "dummy->" + elem.attrib["name"]).replace("YYY", type_resolver(elem, self.def_elems)))
#read_source.write(text.c_function_return_type)
read_source.write(text.c_function_close + "\n")
- read_source.write('#ifdef __cplusplus\n}#endif\n')
- read_source.write("#endif //end of header guard\n\n")
+ read_source_header = open(self.argparser.args.outdir + "/read.h", "w")
+ read_source_header.write("#ifndef FT_READ_H\n#define FT_READ_H\n")
+ read_source_header.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
+ read_source_header.write('#include "./structs.h"\n')
+ for elem in self.def_elems + self.read_elems:
+ read_source_header.write(static + inline + text.c_read_elem_sig_h.replace("YYY", elem.attrib["name"]).replace("XXX", elem.attrib["name"]))
+ read_source_header.write('#ifdef __cplusplus\n}\n#endif\n')
+ read_source_header.write("#endif //end of header guard\n\n")
def gen_void_train(self):
#void_source = open(self.argparser.args.outdir + "/void.h", "w")
- void_source = open(self.argparser.args.outdir + "/aggregate.h", "w")
+ void_source = open(self.aggregate_source, "w")
+ void_source_h = open(self.aggregate_source_h, "w")
void_source.write("\n// automatically generated by faultreiber\n")
void_source.write("// " + self.dnt + "\n")
void_source.write('#include "./structs.h"\n')
void_source.write('#include "./read.c"\n')
void_source.write("#include <stdlib.h>\n")
#void_source.write("void** void_train(void) {\n")
- void_source.write('#ifndef FT_AGGREGATE_H\n#define FT_AGGREGATE_H\n')
- void_source.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
+ void_source_h.write('#ifndef FT_AGGREGATE_H\n#define FT_AGGREGATE_H\n')
+ void_source_h.write('#ifdef __cplusplus\nextern "C" {\n#endif\n')
for elem in self.read_elems:
count = get_elem_count(elem)
size = get_elem_size(elem)
@@ -351,6 +379,7 @@ class CodeGen(object):
else:
void_source.write(ref_node.attrib["name"] + "* " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container;\n")
void_source.write("void malloc_all(void) {\n")
+ void_source_h.write("void malloc_all(void);\n")
count_int = int()
count_void = int()
read_count = len(self.read_elems)
@@ -408,31 +437,33 @@ class CodeGen(object):
void_source.write(elem.attrib["name"] + "_container->" + child.attrib["name"] + " = " + elem.attrib["name"] + "_" + child.attrib["name"] + "_container" + ";\n")
def gen_aggregate_read(self):
- agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a")
- #agg_source.write("\n// automatically generated by faultreiber\n")
- #agg_source.write("// " + self.dnt + "\n")
- #agg_source.write('#include "./structs.h"\n')
- #agg_source.write('#include "./read.c"\n')
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
+ agg_source_h.write("void read_aggr(int _fd);\n")
for elem in self.read_elems:
agg_source.write("ft_read_" + elem.attrib["name"] + "(_fd," + elem.attrib["name"] + "_container" + ");\n")
agg_source.write("}\n")
#FIXME-not handling double pointers
def gen_release(self):
- agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a")
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
+ agg_source_h.write("void release_all(void);\n")
agg_source.write("void release_all(void) {\n")
for elem in self.read_elems:
agg_source.write("free(" + elem.attrib["name"] + "_container);\n")
agg_source.write("}\n")
def gen_return(self):
- agg_source = open(self.argparser.args.outdir + "/aggregate.h", "a")
+ agg_source = open(self.aggregate_source, "a")
+ agg_source_h = open(self.aggregate_source_h, "a")
for elem in self.read_elems:
agg_source.write(elem.attrib["name"] + "* ft_ret_" + elem.attrib["name"] + "(void) {\n")
agg_source.write("return " + elem.attrib["name"] + "_container"+ ";\n")
agg_source.write("}\n")
- agg_source.write('#ifdef __cplusplus\n}\n#endif\n')
- agg_source.write("#endif //end of header guard\n\n")
+ agg_source_h.write(elem.attrib["name"] + "* ft_ret_" + elem.attrib["name"] + "(void);\n")
+ agg_source_h.write('#ifdef __cplusplus\n}\n#endif\n')
+ agg_source_h.write("#endif //end of header guard\n\n")
def read_xml(self):
if self.argparser.args.xml:
@@ -475,7 +506,7 @@ class CodeGen(object):
def_header.write("}" + child.attrib["name"] + ";\n\n")
def gen_struct_header_xml(self):
- struct_source = open(get_full_path(self.argparser.args.outdir, "structs.h"), "w")
+ struct_source = open(self.struct_source, "w")
struct_source_c = open(get_full_path(self.argparser.args.outdir, "structs.c"), "w")
struct_source_c.write('#include "structs.h"')
struct_source.write(text.pre_header_guard)
@@ -547,6 +578,7 @@ class CodeGen(object):
def run(self):
self.init()
self.init_hook()
+ self.file_manager()
#self.gen_struct_header()
self.read_xml()
self.gen_reader_funcs()
@@ -560,6 +592,7 @@ class CodeGen(object):
self.gen_release()
self.gen_return()
+
# write code here
def premain(argparser):
signal.signal(signal.SIGINT, SigHandler_SIGINT)