def readLabel(self): state, label, lno = 'newline', b'', 0 label_info = [] for i, c in enumerate(self.buf): if py3k: c = bytechr(c) if state == 'label': c = c.upper() if c.isalnum() or c == b'_': label += c else: assert self.buf[i-len(label)-1:i-len(label)] == b'*' state = 'newline' if c == b'\n' else 'ready' label_info.append(ONScripterLabel( name=label, start_line=lno, start_address=i-len(label)-1, )) label = b'' if c == b'\n': lno += 1 continue elif state == 'newline': if c in b'\t ': continue if c == b'*': state, label = 'label', b'' continue state = 'ready' if c == b'\n': state, lno = 'newline', lno + 1 self.label_info = label_info
def read(self, file_=None): buf = '' if file_: buf = open(file_, 'rb').read() else: for start in ('0.txt', '00.txt', 'nscript.dat'): try: buf = open(start, 'rb').read() break except IOError: pass else: raise ONSHandlerError('No script found!') if start == 'nscript.dat': buf = b''.join(bytechr(ord(c) ^ 0x84) for c in buf) else: bufs = [buf] for i in range(1, 100): try: text = open('%d.txt' % i, 'rb').read() except IOError: if i >= 10: continue try: text = open('%02d.txt' % i, 'rb').read() except IOError: continue bufs.append(text) buf = b'\n'.join(bufs) buf = buf.replace(b'\r\n', b'\n').replace(b'\r', b'\n') # try: # buf = buf.decode(self.encoding) # except UnicodeDecodeError: # raise ONSHandlerError( # 'Decode script with %s failed!' % self.encoding) p = 1 while buf[0:1] == b';': if buf[p:p+4] == b'mode': try: self.mode = int(buf[p+4:p+7]) except ValueError: pass p += 7 elif buf[p:p+5] == b'value': p += 5 while buf[p:p+1] in b'\t \n': p += 1 q = p while buf[p:p+1].isdigit(): p += 1 self.global_variable_border = int(buf[q:p]) else: break if buf[p:p+1] != b',': break p += 1 self.buf = buf #open('resultbuf.txt', 'wb').write(buf) self.readLabel()