blob: 5828a7bf106ff9b8b70dc04971d1422487696135 (
plain) (
blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
#!/usr/bin/env python3
import sys
from subprocess import Popen, PIPE
symbol = '@'
encoding = 'utf-8'
input_file = '/dev/stdin'
output_file = '/dev/stdout'
data = None
with open(input_file, 'rb') as file:
data = file.read().decode(encoding, 'error').split('\n')
entered = False
bashed = []
def pp(line):
rc = ''
symb = False
brackets = 0
esc = False
dollar = False
quote = []
for c in line:
if brackets > 0:
if esc:
esc = False
elif (c == ')') or (c == '}'):
brackets -= 1
if brackets == 0:
rc += c + '"\''
continue
elif (c == '(') or (c == '{'):
brackets += 1
elif c == '\\':
esc = True
rc += c
elif symb:
symb = False
if (c == '(') or (c == '{'):
brackets += 1
rc += '\'"$' + c
else:
rc += c
elif c == symbol:
symb = True
elif len(quote) > 0:
if esc:
esc = False
elif dollar:
dollar = False
if c == '(':
quote.append(')')
elif c == '{':
quote.append('}')
elif c == quote[-1]:
quote[:] = quote[:-1]
elif (quote[-1] in ')}') and (c in '\'"`'):
quote.append(c)
elif (c == '\\') and (quote[-1] != "'"):
esc = True
elif c == '$':
dollar = True
rc += c
elif (c == '"') or (c == "'") or (c == '`'):
quote.append(c)
rc += c
else:
rc += c
return rc
for lineno in range(len(data)):
line = data[lineno]
if line.startswith(symbol + '<'):
bashed.append(line[2:])
entered = True
elif line.startswith(symbol + '>'):
bashed.append(line[2:])
entered = False
elif entered:
bashed.append(line)
else:
line = '\'%s\'' % line.replace('\'', '\'\\\'\'')
bashed.append('echo $\'\\e%i\\e\'%s' % (lineno, pp(line)))
bashed = '\n'.join(bashed).encode(encoding)
bash = Popen(["bash"], stdin = PIPE, stdout = PIPE, stderr = sys.stderr)
bashed = bash.communicate(bashed)[0]
if bash.returncode != 0:
sys.exit(bash.returncode)
bashed = bashed.decode(encoding, 'error').split('\n')
pped = []
lineno = -1
for line in bashed:
no = -1
if line.startswith('\033'):
no = int(line[1:].split('\033')[0])
line = '\033'.join(line[1:].split('\033')[1:])
if no > lineno:
while no != lineno + 1:
pped.append('')
lineno += 1
pped.append(line)
lineno += 1
with open(output_file, 'wb') as file:
file.write('\n'.join(pped).encode(encoding))
file.flush()
|