aboutsummaryrefslogtreecommitdiffstats
path: root/gpp.py
blob: 68a317fe798c6e955d0edf37690d4ccdfe8ef24b (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!@{SHEBANG}
# -*- coding: utf-8 -*-

import os
import sys
import shlex
from subprocess import Popen, PIPE

if sys.version_info.major < 3:
    def bytes(string):
        r = bytearray(len(string))
        b = buffer(r)
        r[:] = string
        return r

if sys.version_info.major < 3:
    def bytelist(string):
        return [ord(c) for c in string]
else:
    bytelist = list

symbol = '@'
encoding = sys.getdefaultencoding()
iterations = 1
input_file = '/dev/stdin'
output_file = '/dev/stdout'
unshebang = 0

args = sys.argv

# If the file is executed, the first command line argument will be the first argument in the shebang,
# the second will be the rest of the arguments in the command line as is and the third and final will
# be the executed file.
if len(args) == 3:
    if os.path.exists(args[2]) and os.path.isfile(args[2]) and ((os.stat(args[2]).st_mode & 0o111) != 0):
        args[1 : 2] = shlex.split(args[1])

for i in range(1, len(args)):
    arg = args[i]
    i += 1
    if   arg in ('-s', '--symbol'):      symbol      = sys.argv[i]
    elif arg in ('-e', '--encoding'):    encoding    = sys.argv[i]
    elif arg in ('-n', '--iterations'):  iterations  = int(sys.argv[i])
    elif arg in ('-u', '--unshebang'):   unshebang   += 1; continue
    elif arg in ('-i', '--input'):       input_file  = sys.argv[i]
    elif arg in ('-o', '--output'):      output_file = sys.argv[i]
    elif arg in ('-f', '--file'):
        input_file  = sys.argv[i]
        output_file = sys.argv[i]
    elif arg in ('-D', '--export'):
        export = sys.argv[i]
        if '=' not in export:
            export += '=1'
        export = (export.split('=')[0], '='.join(export.split('=')[1:]))
        os.putenv(export[0], export[1])
    else:
        continue
    i += 1

if input_file == '-':   input_file = '/dev/stdin'
if output_file == '-':  output_file = '/dev/stdout'

symbol = bytelist(symbol.encode(encoding))
symlen = len(symbol)

if iterations < 1:
    if input_file != output_file:
        data = None
        with open(input_file, 'rb') as file:
            data = file.read()
        with open(output_file, 'wb') as file:
            file.write(data)
            file.flush()
    sys.exit(0)

def linesplit(bs):
    rc = []
    elem = []
    for b in bs:
        if b == 10:
            rc.append(elem)
            elem = []
        else:
            elem.append(b)
    rc.append(elem)
    return rc

def linejoin(bss):
    rc = []
    if len(bss) > 0:
        rc += bss[0]
    for bs in bss[1:]:
        rc.append(10)
        rc += bs
    return rc

data = None
with open(input_file, 'rb') as file:
    data = file.read()
data = linesplit(bytelist(data))

if unshebang == 1:
    if (len(data[0]) >= 2) and (data[0][0] == ord('#')) and (data[0][1] == ord('!')):
        data[0] = []

if unshebang >= 2:
    if (len(data[0]) >= 2) and (data[0][0] == ord('#')) and (data[0][1] == ord('!')):
        data[0] = data[1]
        data[1] = []

def pp(line):
    rc = []
    symb = False
    brackets = 0
    esc = False
    dollar = False
    quote = []
    n = len(line)
    i = 0
    rc.append(ord('\''))
    while i < n:
        c = line[i]
        i += 1
        if brackets > 0:
            if esc:
                esc = False
            elif len(quote) > 0:
                if dollar:
                    dollar = False
                    if c == ord('('):
                        quote.append(ord(')'))
                    elif c == ord('{'):
                        quote.append(ord('}'))
                elif c == quote[-1]:
                    quote[:] = quote[:-1]
                elif (quote[-1] in (ord(')'), ord('}'))) and (c in (ord('"'), ord('\''), ord('`'))):
                    quote.append(c)
                elif (c == ord('\\')) and (quote[-1] != ord('\'')):
                    esc = True
                elif c == ord('$'):
                    dollar = True
            elif c in (ord('"'), ord('\''), ord('`')):
                quote.append(c)
            elif c in (ord(')'), ord('}')):
                brackets -= 1
                if brackets == 0:
                    rc.append(c)
                    rc.append(ord('"'))
                    rc.append(ord('\''))
                    continue
            elif c in (ord('('), ord('{')):
                brackets += 1
            elif c == ord('\\'):
                esc = True
            rc.append(c)
        elif line[i - 1 : i + symlen - 1] == symbol:
            if symb:
                rc += symbol
            symb = not symb
            i += symlen - 1
        elif symb:
            symb = False
            if c in (ord('('), ord('{')):
                brackets += 1
                rc.append(ord('\''))
                rc.append(ord('"'))
                rc.append(ord('$'))
            else:
                rc += symbol
            if c == ord('\''):
                rc.append(c)
                rc.append(ord('\\'))
                rc.append(c)
            rc.append(c)
        elif c == ord('\''):
            rc.append(c)
            rc.append(ord('\\'))
            rc.append(c)
            rc.append(c)
        else:
            rc.append(c)
    rc.append(ord('\''))
    return rc

for _ in range(iterations):
    entered = False
    bashed = []
    
    for lineno in range(len(data)):
        line = data[lineno]
        if (len(line) > symlen) and (line[:symlen] == symbol) and (line[symlen] in (ord('<'), ord('>'))):
            bashed.append(line[symlen + 1:])
            entered = line[symlen] == ord('<')
        elif entered:
            bashed.append(line)
        else:
            buf = bytelist(('echo $\'\\e%i\\e\'' % lineno).encode())
            bashed.append(buf + pp(line))
    
    bashed = bytes(linejoin(bashed))
    bash = Popen(["bash"], stdin = PIPE, stdout = PIPE, stderr = sys.stderr)
    bashed = bash.communicate(bashed)[0]
    
    if bash.returncode != 0:
        sys.exit(bash.returncode)
    
    bashed = linesplit(bytelist(bashed))
    data = []
    lineno = -1
    
    for line in bashed:
        no = -1
        if (len(line) > 0) and (line[0] == 0o33):
            no = 0
            for i in range(1, len(line)):
                if line[i] == 0o33:
                    line = line[i + 1:]
                    break
                no = no * 10 + (line[i] - ord('0'))
        if no > lineno:
            while no != lineno + 1:
                data.append([])
                lineno += 1
        data.append(line)
        lineno += 1

data = bytes(linejoin(data))
with open(output_file, 'wb') as file:
    file.write(data)
    file.flush()