aboutsummaryrefslogtreecommitdiffstats
path: root/main.py
diff options
context:
space:
mode:
authorbloodstalker <thabogre@gmail.com>2018-07-02 12:20:17 +0000
committerbloodstalker <thabogre@gmail.com>2018-07-02 12:20:17 +0000
commite83ae1b59160627bef19c519ba13817dd5ee88b6 (patch)
tree1e9af360fbfca0d27594474b432126a67eba38c6 /main.py
parentupdate (diff)
downloadfaultreiber-e83ae1b59160627bef19c519ba13817dd5ee88b6.tar.gz
faultreiber-e83ae1b59160627bef19c519ba13817dd5ee88b6.zip
update
Diffstat (limited to 'main.py')
-rwxr-xr-xmain.py42
1 files changed, 39 insertions, 3 deletions
diff --git a/main.py b/main.py
index f848cbc..6d76678 100755
--- a/main.py
+++ b/main.py
@@ -9,25 +9,31 @@ import readline
from shutil import copy
import signal
import sys
+from text import text
+import datetime
def SigHandler_SIGINT(signum, frame):
print()
sys.exit(0)
+def get_full_path(path, name):
+ if path[-1] == "/": return path + name
+ else: return path + "/" + name
+
class Argparser(object):
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument("--targetname", type=str, help="main target name")
parser.add_argument("--outdir", type=str, help="path to output dir")
parser.add_argument("--structs", type=str, help="the structs json file")
+ parser.add_argument("--structsinclude", type=str, help="the path to the header that's going to be included by structs.h before structure declarations.")
parser.add_argument("--dbg", action="store_true", help="debug", default=False)
+ parser.add_argument("--datetime", action="store_true", help="print date and time in autogen files", default=False)
self.args = parser.parse_args()
def dupemake(path, main_name):
copy("./resources/makefile", path)
- makefile_path = str()
- if path[-1] == "/": makefile_path = path + "makefile"
- else: makefile_path = path + "/makefile"
+ makefile_path = get_full_path(path, "makefile")
for line in fileinput.input(makefile_path, inplace=True):
if "XXX" in line:
line = line.replace("XXX", main_name)
@@ -37,6 +43,7 @@ class CodeGen(object):
def __init__(self, argparser):
self.argparser = argparser
self.struct_json = json.load(open(self.argparser.args.structs))
+ self.dnt = datetime.datetime.now().isoformat()
def init_hook(self):
pass
@@ -44,9 +51,38 @@ class CodeGen(object):
def init(self):
dupemake(self.argparser.args.outdir, self.argparser.args.targetname)
+ def gen_reader_funcs(self):
+ pass
+
+ def gen_struct_header(self):
+ struct_source = open(get_full_path(self.argparser.args.outdir, "structs.h"), "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)
+ struct_source.write(text.autogen_warning)
+ if self.argparser.args.datetime: struct_source.write("// " + self.dnt + "\n")
+ struct_source.write(text.header_guard_begin.replace("XXX", "structs".upper()))
+ struct_source.write(text.header_inttype)
+ if self.argparser.args.structsinclude:
+ copy(self.argparser.args.structsinclude, self.argparser.args.outdir)
+ pos = self.argparser.args.structsinclude.rfind("/")
+ sub = self.argparser.args.structsinclude[pos+1:]
+ struct_source.write('#include "' + sub + '"\n')
+ for k,v in self.struct_json.items():
+ struct_name = k
+ field_names = v["field_name"]
+ field_typess = v["field_type"]
+ struct_source.write("typedef struct {\n")
+ for i, j in zip(field_names, field_typess):
+ struct_source.write("\t" + j + " " + i + ";\n")
+ struct_source.write("}" + struct_name + ";\n\n")
+ struct_source.write(text.pragma_endif)
+ struct_source.write(text.last_comment)
+
def run(self):
self.init()
self.init_hook()
+ self.gen_struct_header()
# write code here
def premain(argparser):