File: collect-response.py

package info (click to toggle)
termpaint 0.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,760 kB
  • sloc: cpp: 40,344; ansic: 10,323; python: 402; sh: 36; makefile: 14
file content (71 lines) | stat: -rwxr-xr-x 2,327 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/python3

import os
import termios
import tty
import time
import re
import hashlib

def read_timed():
    time.sleep(1)
    return os.read(0, 100)

def get_pos():
    os.write(1, b'\033[6n')
    r = read_timed()
    m = re.fullmatch(b'\033\\[(?P<row>[0-9]*);(?P<col>[0-9]*)R', r)
    if not m:
        raise RuntimeError('Terminal reply to cursor position report is invalid')
    return int(m['row'].decode()), int(m['col'].decode())

LFLAG = 3
CC = 6

print('working... Do not press any key, this will take about a minute')
print()

orig = termios.tcgetattr(0)

result = []

try:
    mode = termios.tcgetattr(0)
    mode[LFLAG] = mode[LFLAG] & ~(termios.ECHO | termios.ICANON)
    mode[CC][termios.VMIN] = 0
    mode[CC][termios.VTIME] = 0
    termios.tcsetattr(0, termios.TCSAFLUSH, mode)
    pos = get_pos()
    for i in [r'\033[>c', r'\033[>1c', r'\033[>0;1c',r'\033[=c', r'\033[5n', r'\033[6n', r'\033[?6n', r'\033[>q', r'\033[1x', r'\033]4;255;?\007', r'\033P+q544e\033\\']:
        send = i.replace('\\033', '\033').replace('\\007', '\007').replace(r'\\', '\\')
        os.write(1, send.encode())
        ret = read_timed()
        new_pos = get_pos()
        if pos == new_pos:
            result.append((i, ret.replace(b'\\', b'\\\\').replace(b'\033', b'\\033')
                                 .replace(b'\007', b'\\007').replace(b'\x90', b'\\x90')
                                 .replace(b'\x9c', b'\\x9c').decode(), None))
        else:
            if pos[0] == new_pos[0]:
                glitch = 'X' * (new_pos[1] - pos[1])
            else:
                glitch = 'fix this manually'
            print()
            pos = get_pos()
            result.append((i, ret.replace(b'\\', b'\\\\').replace(b'\033', b'\\033')
                                 .replace(b'\007', b'\\007').replace(b'\x90', b'\\x90')
                                 .replace(b'\x9c', b'\\x9c').decode(), glitch))
finally:
    termios.tcsetattr(0, termios.TCSAFLUSH, orig)

fphash = hashlib.new('md5')
for (query, response, glitch) in result:
    if not glitch:
        line = '            { %-19s { "%s" }},' % ('"' + query + '",', response)
    else:
        line = '            { %-19s { "%s", "%s" }},' % ('"' + query + '",', response, glitch)
    print(line)
    fphash.update(line.encode())
print(fphash.hexdigest())