def do_else(args): if (not s.fn_cur.flow_n) or (s.fn_cur.flow_cur[s.fn_cur.flow_n - 1][0] != e.FLOW_IF): out.error("outside flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_ELSE, None) return res_
def do_end(args): if s.fn_cur.flow_n: out.error("unclosed flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_END, "") s.fn_cur = None return res_
def load(): with open(CHECK_CONF_FILE) as con: try: conf = json.load(con) return conf except: out.error( "conf.json error, please download another one and replace it.") exit()
def do_wend(args): if (not s.fn_cur.flow_n) or (s.fn_cur.flow_cur[s.fn_cur.flow_n - 1][0] != e.FLOW_WHILE): out.error("outside flow control") return False res_ = emit.emit(s.fn_cur, e.EMIT_WEND, None) s.fn_cur.flow_cur.pop() s.fn_cur.flow_n -= 1 return res_
def do_def(args): if s.fn_cur: out.error("nested function") return False _fn = fn.Fn(args[0], args[1:], emit.s.long_len) if not _fn: return False s.fn_cur = _fn res_ = emit.emit(s.fn_cur, e.EMIT_DEF, args[0]) return res_
def get_or_def_var (self, name_s): if self.get_arg (name_s): out.error ('name "' + name_s + '" taken by function argument') return None for var in self._vars: if var.name_s == name_s: return var datum = self.def_data (name_s, e.DATA_LONG, []) var = Var (name_s, datum) self._vars += [var] return var
def get_val (fn_cur, val, reg): val_type = get_val_type (val) if not val_type: out.error ('unknown val type "' + val + '"') return False elif val_type == e.VAL_LARRAY: datum = fn_cur.def_data ('.l' + str(fn_cur.data_larray_n),\ e.DATA_LARRAY, val) out.put ('mov ' + '$' + datum.name_s + ', ' + reg) elif val_type == e.VAL_STR: datum = fn_cur.def_data ('.s' + str(fn_cur.data_str_n),\ e.DATA_STR, val) out.put ('mov ' + '$' + datum.name_s + ', ' + reg) elif val_type == e.VAL_LONG: out.put ('mov $' + val + ', ' + reg) elif val_type == e.VAL_VAR: arg_i = fn_cur.get_arg (val) if arg_i: arg_i -= 1 if arg_i < s.arg_regs_n: _s = s.arg_regs [arg_i] else: _s = str((arg_i + 1) * s.long_len) + '(' + s.stack_regs [1] +\ ')' out.put ('mov ' + _s + ', ' + reg) else: var = fn_cur.get_or_def_var (val) if not var: return False out.put ('mov ' + var.datum.name_s + ', ' + reg) elif val_type == e.VAL_VAR_DEREF: _n = val [1:] arg_i = fn_cur.get_arg (_n) if arg_i: # TODO support this out.error ('dereferencing arg') return False else: var = fn_cur.get_or_def_var (_n) if not var: return False out.put ('mov ' + var.datum.name_s + ', ' + reg) out.put ('mov (' + reg + '), ' + reg) return True
def test_levels(full=True): out('no explicit level, should be info.') out.trace('trace msg: %s', 'Absurdly voluminous details…') out.debug('debug message') out.info('info message - Normal feedback') out.note('note message - Important positive feedback to remember.') out.warn('warn message - Something to worry about.') if full: out.critical('critical message - *flatline*') out.fatal('fatal message - *flatline*') try: 1 / 0 except Exception: out.error('error message - Pow!') #~ out.exception('exception message - Kerblooey!') out.exc('exc message - Kerblooey!')
def packKeyword(keyword, length, data, format): # We'll return a bytearray of the packed data keywordPack = bytearray() # Write the keyword keywordPack += bytearray(keyword.encode()) # Write the length # The < at the front says to pack it little endian if (keyword[0] == "#"): # Keywords that start with pound have a 2 byte length # H is 2 bytes keywordPack += struct.pack("<H", length) else: # B is 1 byte keywordPack += struct.pack("<B", length) # Write the data # If the user didn't provide data = length given, we'll pad the end with 0's if (format == "ascii"): # Pad if necessary data = data.ljust(length, '\0') # Write it keywordPack += bytearray(data.encode()) elif (format == "hex"): # fromhex will deal with spacing in the data, but not carriage returns # Remove those before we get to fromhex data = data.replace("\n","") # Pad if necessary (* 2 to convert nibble data to byte length) data = data.ljust((length * 2), '0') # Write it keywordPack += bytearray.fromhex(data) elif (format == "bin"): # Pad if necessary data = bytearray(data) for i in range (len(data), length): data.append(0) # Stick the binary data we have right back into the record keywordPack += data else: out.error("Unknown format type %s passed into packKeyword" % format) return None # The keyword is packed, send it back return keywordPack
def init (long_len): s.long_len = long_len if long_len == 8: s.arg_regs = ['%rdi', '%rsi', '%rdx', '%rcx', 'r8', 'r9'] s.arg_regs_n = len(s.arg_regs) s.regs = ['%rax', '%rbx', '%r10'] s.stack_regs = ['%rsp', '%rbp'] elif long_len == 4: s.arg_regs = [] s.arg_regs_n = 0 s.regs = ['%eax', '%ebx', '%ecx'] s.stack_regs = ['%esp', '%ebp'] else: out.error ('what year is this???') return False return True
def set_val (fn_cur, val): reg0 = s.regs [0] reg2 = s.regs [2] val_type = get_val_type (val) if \ val_type == e.VAL_STR or\ val_type == e.VAL_LARRAY or\ val_type == e.VAL_LONG: out.error ('can\'t assign to this type') return False elif val_type == e.VAL_VAR: arg_i = fn_cur.get_arg (val) if arg_i: arg_i -= 1 if arg_i < s.arg_regs_n: _s = s.arg_regs [arg_i] else: _s = str((arg_i + 1) * s.long_len) + '(' + s.stack_regs [1] +\ ')' out.put ('mov ' + reg0 + ', ' + _s) else: var = fn_cur.get_or_def_var (val) if not var: return False out.put ('mov ' + reg0 + ', ' + var.datum.name_s) elif val_type == e.VAL_VAR_DEREF: _n = val [1:] arg_i = fn_cur.get_arg (_n) if arg_i: out.error ('can\'t modify function arg') return False else: var = fn_cur.get_or_def_var (_n) if not var: return False out.put ('mov ' + var.datum.name_s + ', ' + reg2) out.put ('mov ' + reg0 + ', (' + reg2 + ')') return True
def main(): if len(sys.argv) > 1: res_ = init(True, int(sys.argv[1])) else: res_ = init(True) if not res_: out.error("init failed") return -1 lines = sys.stdin.readlines() res_ = process(lines) if not res_: out.error("process (pass 1) failed at line " + str(s.line_n + 1)) return -2 init(False) out.s.output_ = True if len(sys.argv) > 1: res_ = init(True, int(sys.argv[1])) else: res_ = init(True) res_ = process(lines) if not res_: out.error("process (pass 2) failed at line " + str(s.line_n + 1)) return -3 init(False) return 0
optgroup.add_argument('-r', '--create-records', help="Create tvpd files for each record in the vpd", action="store_true") # We've got everything we want loaded up, now look for it args = parser.parse_args() # Get the manifest file and get this party started clVpdFile = args.vpdfile # Look for output path clOutputPath = args.outpath # Make sure the path exists, we aren't going to create it if (os.path.exists(clOutputPath) != True): out.error("The given output path %s does not exist!" % clOutputPath) out.error("Please create the output directory and run again") exit(1) # Debug printing clDebug = args.debug # Create separate tvpd files for each record clCreateRecords = args.create_records ################################################ # Read in the VPD file and break it apart out.setIndent(0) out.msg("==== Stage 1: Parsing the VPD file") out.setIndent(2)
def parseTvpd(tvpdFile, topLevel): # Accumulate errors and return the total at the end # This allows the user to see all mistakes at once instead of iteratively running errorsFound = 0 # Let the user know what file we are reading # We could make this optional with a new function param in the future out.msg("Parsing tvpd %s" % tvpdFile) # Read in the file # If there are tag mismatch errors or other general gross format problems, it will get caught here # Once we return from this function, then we'll check to make sure only supported tags were given, etc.. tvpdRoot = ET.parse(tvpdFile).getroot() # Print the top level tags from the parsing if (clDebug): out.debug("Top level tag/attrib found") for child in tvpdRoot: out.debug("%s %s" % (child.tag, child.attrib)) # Do some basic error checking of what we've read in # Make sure the root starts with the vpd tag # If it doesn't, it's not worth syntax checking any further # This is the only time we'll just bail instead of accumulating if (tvpdRoot.tag != "vpd"): out.error("%s does not start with a <vpd> tag. No further checking will be done until fixed!" % tvpdFile) return(1, None) # We at least have a proper top level vpd tag, so loop thru the rest of the levels and check for any unknown tags # This will also be a good place to check for the required tags # Define the expected tags at this level vpdTags = {"name" : 0, "size" : 0, "VD" : 0, "record" : 0} # Go thru the tags at this level for vpd in tvpdRoot: # See if this is a tag we even expect if vpd.tag not in vpdTags: out.error("Unsupported tag <%s> found while parsing the <vpd> level" % vpd.tag) errorsFound+=1 # We continue here because we don't want to parse down this hierarcy path when we don't know what it is continue # It was a supported tag else: vpdTags[vpd.tag]+=1 # Do the record level checks if (vpd.tag == "record"): # Define the expected tags at this level recordTags = {"rdesc" : 0, "keyword" : 0, "rtvpdfile" : 0, "rbinfile" : 0} # Make sure the record has a name attrib, save for later use recordName = vpd.attrib.get("name") if (recordName == None): out.error("A <record> tag is missing the name attribute") errorsFound+=1 recordName = "INVALID" # Set the invalid name so the code below can use it without issue # Loop thru the tags defined for this record for record in vpd: # See if this is a tag we even expect if record.tag not in recordTags: out.error("Unsupported tag <%s> found while parsing the <record> level for record %s" % (record.tag, recordName)) errorsFound+=1 # We continue here because we don't want to parse down this hierarcy path when we don't know what it is continue # It was a supported tag else: recordTags[record.tag]+=1 # Do the keyword level checks if (record.tag == "keyword"): # Define the expected tags at this level keywordTags = {"kwdesc" : 0, "kwformat" : 0, "kwlen" : 0, "kwdata" : 0} # Make sure the keyword has a name attrib, save for later use keywordName = record.attrib.get("name") if (keywordName == None): out.error("<keyword> tag in record %s is missing the name attribute" % (recordName)) errorsFound+=1 keywordName = "INVALID" # Set the invalid name so the code below can use it without issue # Loop thru the tags defined for this keyword for keyword in record: # See if this is a tag we even expect if keyword.tag not in keywordTags: out.error("Unsupported tag <%s> found while parsing the <keyword> level for keyword %s in record %s" % (keyword.tag, keywordName, recordName)) errorsFound+=1 # We continue here because we don't want to parse down this hierarcy path when we don't know what it is continue # It was a supported tag else: keywordTags[keyword.tag] +=1 # We've checked for unknown keyword tags, now make sure we have the right number of each # This is a simple one, we can only have 1 of each for tag in keywordTags: if (keywordTags[tag] != 1): out.error("The tag <%s> was expected to have a count of 1, but was found with a count of %d for keyword %s in record %s" % (tag, keywordTags[tag], keywordName, recordName)) errorsFound+=1 # We've checked for unknown record tags, now make sure we've got the right number, they don't conflict, etc.. recordTagTotal = bool(recordTags["keyword"]) + bool(recordTags["rbinfile"]) + bool(recordTags["rtvpdfile"]) # keyword, rbinfile and rtvpdfile are mutually exclusive. Make sure we have only one if (recordTagTotal > 1): out.error("For record %s, more than one tag of type keyword, rbinfile or rtvpdfile was given!" % (recordName)) out.error("Use of only 1 at a time is supported for a given record!") errorsFound+=1 # We checked if we had more than 1, let's make sure we have at least 1 if (recordTagTotal < 1): out.error("For record %s, 0 tags of type keyword, rbinfile or rtvpdfile were given!" % (recordName)) out.error("1 tag of the 3 must be in use for the record to be valid!") errorsFound+=1 # Make sure the rdesc is available if (recordTags["keyword"] and recordTags["rdesc"] != 1): out.error("The tag <rdesc> was expected to have a count of 1, but was found with a count of %d for record %s" % (recordTags["rdesc"], recordName)) errorsFound+=1 # Do some checking of what we found at the vpd level # Top level is the manifest passed in on the command line # When false, it's just a record description and doesn't require the high level descriptors if (topLevel == True): comparer = 1 else: comparer = 0 # Don't go thru all of them, "record" has special handling below for tag in ["name", "size", "VD"]: if (vpdTags[tag] != comparer): out.error("The tag <%s> was expected to have a count of %d, but was found with a count of %d" % (tag, comparer, vpdTags[tag])) errorsFound+=1 # Make sure at least one record tag was found if (vpdTags["record"] == 0): out.error("At least one <record> must be defined for the file to be valid!") errorsFound+=1 # If this is an included tvpd, it can only have 1 record in it # This check is just by convention. If a compelling case to change it was provided, it could be done if (topLevel == False): if (vpdTags["record"] > 1): out.error("More than 1 record entry found in %s. Only 1 record is allowed!" % (tvpdFile)) errorsFound+=1 ###### # All done, vary our return based upon the errorsFound if (errorsFound): return (errorsFound, None) else: return(0, tvpdRoot)
optgroup.add_argument('-d', '--debug', help="Enables debug printing",action="store_true") optgroup.add_argument('-r', '--binary-records', help="Create binary files for each record in the template",action="store_true") optgroup.add_argument('-k', '--binary-keywords', help="Create binary files for each keyword in the template",action="store_true") optgroup.add_argument('-i', '--inpath', help="The search path to use for the files referenced in the manifest") # We've got everything we want loaded up, now look for it args = parser.parse_args() # Get the manifest file and get this party started clManifestFile = args.manifest # Look for output path clOutputPath = args.outpath # Make sure the path exists, we aren't going to create it if (os.path.exists(clOutputPath) != True): out.error("The given output path %s does not exist!" % clOutputPath) out.error("Please create the output directory and run again") exit(1) # Look for input path clInputPath = args.inpath # Make sure the path exists if (clInputPath != None): # Add the CWD onto the path so the local directory is always looked at clInputPath += ":." else: # Set it the CWD since it will be used throughout the program and having it set to None breaks things clInputPath = "." # Debug printing clDebug = args.debug
def process(ls): cmdd = { "def": (1, 0, do_def), "ret": (0, 0, do_ret), "end": (0, 0, do_end), "call": (1, 0, do_call), "if": (1, 1, do_if), "else": (0, 0, do_else), "endif": (0, 0, do_endif), "while": (1, 1, do_while), "wend": (0, 0, do_wend), "add": (1, 1, do_add), "sub": (1, 1, do_sub), "mul": (1, 1, do_mul), "div": (1, 1, do_div), "res": (1, 1, do_res), "set": (2, 2, do_set), "addto": (2, 2, do_addto), "subfrom": (2, 2, do_subfrom), "multo": (2, 2, do_multo), "divfrom": (2, 2, do_divfrom), } for s.line_n, s_raw in enumerate(ls): if not s_raw: continue # strip beginning and ending whitespaces s_stp = s_raw.strip() if not s_stp: continue # split at unquoted spaces s_spl = util.q_split(s_stp) keyword1 = s_spl[0] args = s_spl[1:] args_n = len(args) # ignore if keyword1[0] == "#": continue else: try: _c = cmdd[keyword1] except: out.error('"' + keyword1 + '" keyword unknown') return False if args_n < _c[0]: out.arg_n_error(keyword1, _c[0], _c[1]) return False elif _c[1] and args_n > _c[1]: out.arg_n_error(keyword1, _c[0], _c[1]) return False if (not s.fn_cur) and keyword1 != "def": out.error("outside function") return False res_ = _c[2](args) if not res_: return False return True
optgroup = parser.add_argument_group('Optional Arguments') optgroup.add_argument('-h', '--help', action="help", help="Show this help message and exit") optgroup.add_argument('-d', '--debug', help="Enables debug printing",action="store_true") optgroup.add_argument('-r', '--create-records', help="Create tvpd files for each record in the vpd",action="store_true") # We've got everything we want loaded up, now look for it args = parser.parse_args() # Get the manifest file and get this party started clVpdFile = args.vpdfile # Look for output path clOutputPath = args.outpath # Make sure the path exists, we aren't going to create it if (os.path.exists(clOutputPath) != True): out.error("The given output path %s does not exist!" % clOutputPath) out.error("Please create the output directory and run again") exit(1) # Debug printing clDebug = args.debug # Create separate tvpd files for each record clCreateRecords = args.create_records ################################################ # Read in the VPD file and break it apart out.setIndent(0) out.msg("==== Stage 1: Parsing the VPD file") out.setIndent(2)