File: grc

package info (click to toggle)
grc 1.11.3-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 556 kB
  • sloc: python: 400; sh: 70; makefile: 39; sql: 31
file content (207 lines) | stat: -rwxr-xr-x 5,267 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
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
#! /usr/bin/env python3

from __future__ import print_function

import os, re, string, sys, getopt, signal


def version():
    print ("Generic Colouriser 1.11.3")
    sys.exit()

def help():
    print("""Generic Colouriser 1.11.3
grc [options] command [args]
Options:
-e --stderr    redirect stderr. If this option is selected,
               do not automatically redirect stdout
-s --stdout    redirect stdout, even if -e is selected
-c name --config=name    use name as configuration file for grcat
--colour=word  word is one of: on, off, auto
--pty          run command in pseudoterminal (experimental)
""")
    sys.exit()


def catch_signal(signum, frame):
    "catch signal sent to grc and forward it to the original application"
    global pidp
#    print('signal')
    try:
        os.kill(pidp, signum)
    except OSError: # if the subprocess already died
        pass

try:
    optlist, args = getopt.getopt(sys.argv[1:], "sec:", ["stdout", "stderr", "config=", "colour=", "pty"] )
except:
    help()

if not args:
    help()

stdoutf = 0
stderrf = 0

# configure file for grcat
cfile = ""

colour = sys.stdout.isatty()
use_pty = 0

for i in optlist:
    if i[0] in ["--stderr", "-e"]:
        # redirect stderr
        stderrf = 1
    elif i[0] in ["--stdout", "-s"]:
        # redirect stdout
        stdoutf = 1
    elif i[0] in ["--config", "-c"]:
        cfile = i[1]
    elif i[0] == "--colour":
        if i[1] == "on":
            colour = 1
        elif i[1] == "off":
            colour = 0
        elif i[1] == "auto":
            colour = sys.stdout.isatty()
        else:
            help()
    elif i[0] == '--pty':
        use_pty = 1

stdoutff = 1
stderrff = 0
if stderrf == 1:
    stdoutff = 0
    stderrff = 1
if stdoutf == 1:
    stdoutff = 1

if use_pty:
    import pty

conffile = None
if cfile == "":
    home = os.environ.get('HOME')
    xdg  = os.environ.get('XDG_CONFIG_HOME')
    if not xdg and home:
        xdg = home + '/.config'

    conffilenames = []
    if xdg:
        conffilenames += [xdg + '/grc/grc.conf']
    if home:
        conffilenames += [home + '/.grc/grc.conf']
    conffilenames += ['/usr/local/etc/grc.conf', '/etc/grc.conf']
    for i in conffilenames:
        # test if conffile exists, it can be also a pipe
        if os.path.exists(i) and not os.path.isdir(i):
            conffile = i
            break
    regexplist = []

    if conffile:
        f = open(conffile, "r")
        while 1:
            l = f.readline()
            if l == "":
                break
            if l[0] == "#" or l[0] == '\012':
                continue
            regexp = l.strip()
            if re.search(regexp, ' '.join(args)):
                cfile = f.readline().strip()
                break

signal.signal(signal.SIGINT, catch_signal)


if cfile != "" and colour:
    if stdoutff:
        choo, chio = os.pipe()
    if stderrff:
        choe, chie = os.pipe()
    if use_pty:
        pidp, pty_fd = pty.fork()
    else:
        pidp = os.fork()
    if pidp == 0: # child, command to run
        if stdoutff:
            # connect child (this) stdout to pipe write end
            if not use_pty:
                os.dup2(chio, 1)
                os.close(choo)
                os.close(chio)
        if stderrff:
                os.dup2(chie, 2)
                os.close(choe)
                os.close(chie)
        try:
            os.execvp(args[0], args)
        except OSError as e:
            sys.stderr.write('grc: %s: %s\n' % (args[0], e.strerror))
            sys.exit(1)


    if stdoutff:
        pido = os.fork()
        if pido == 0: # child, grcat
            # connect grcat's stdin to pipe read end, or pty master
            if use_pty:
                os.dup2(pty_fd, 0)
            else:
                os.dup2(choo, 0)
            os.close(choo)
            os.close(chio)
            if stderrff:
                os.close(choe)
                os.close(chie)
            os.execvp("grcat", ["grcat", cfile])

    if stderrff:
        pide = os.fork()
        if pide == 0: # child
            os.dup2(choe, 0)
            os.dup2(2, 1)
            os.close(choe)
            os.close(chie)
            if stdoutff:
                os.close(choo)
                os.close(chio)
            os.execvp("grcat", ["grcat", cfile])
    try:
        status = os.waitpid(pidp, 0)[1]
    except OSError: # interrupted system call
        status = None
        pass # this is probably not correct
#    except KeyboardInterrupt: # catching SIGINT does not work when using pty...
#        status = None
#        os.kill(pidp, signal.SIGINT)
#        pass
    if stderrff:
        os.close(chie)
        os.waitpid(pide, 0)
        os.close(choe)
    if stdoutff:
        os.close(chio)
        os.waitpid(pido, 0)
        os.close(choo)
    sys.exit(status and os.WEXITSTATUS(status))

else:
    pidp = os.fork()
    if pidp == 0:
        try:
            os.execvp(args[0], args)
        except OSError as e:
            sys.stderr.write('grc: %s: %s\n' % (args[0], e.strerror))
            sys.exit(1)
    try:
        status = os.wait()[1]
    except OSError: # interrupted system call
        status = None
        pass # this is probably not correct

sys.exit(status and os.WEXITSTATUS(status))