-
Notifications
You must be signed in to change notification settings - Fork 10
/
rawcat
executable file
·137 lines (120 loc) · 4.64 KB
/
rawcat
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
#!/usr/bin/python
"""\
USAGE: %prog [-b BYTES] [-r] [-s] [-w MSEC] FILE...
Print the given file(s) to standard output without converting LF to CRLF.
To pause after each byte output, run with ``-w 100``.
"""
__license__ = """
Copyright (c) 2009 Mark Lodato
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import re
import sys
import termios
import time
from optparse import OptionParser
__metaclass__ = type
OFLAG = 1
class RawMode:
def __init__(self, f):
self.f = f
def __enter__(self):
self.saved = termios.tcgetattr(self.f)
mode = list(self.saved)
mode[OFLAG] &= ~termios.OPOST
termios.tcsetattr(self.f, termios.TCSAFLUSH, mode)
return self.f
def __exit__(self, exc_type, exc_val, exc_tb):
termios.tcsetattr(self.f, termios.TCSAFLUSH, self.saved)
class Status (object):
textify = [ 'NUL', 'SOH', 'STX', 'ETX', 'EOT', 'ENQ', 'ACK', 'BEL',
'BS', 'HT', 'LF', 'VT', 'FF', 'CR', 'SO', 'SI',
'DLE', 'DC1', 'DC2', 'DC3', 'DC4', 'NAK', 'SYN', 'ETB',
'CAN', 'EM', 'SUB', 'ESC', 'FS', 'GS', 'RS', 'US', 'SP' ]
textify += [ chr(x) for x in range(len(textify), 127) ]
textify += [ 'DEL' ]
textify += [ '\\x%02d' % x for x in range(128,256) ]
def __init__(self, filename):
if filename:
self.opened = True
self.f = open(filename, 'w', 0)
else:
self.opened = False
def write(self, s):
if self.opened:
textify = self.textify
t = ' '.join(textify[ord(x)] for x in s) + '\n'
self.f.write(t)
self.f.flush()
def close(self):
if self.opened:
self.opened = False
self.f.close()
def __del__(self):
self.close()
def rawcat(files, outfile, bufsiz=None, wait=None, reset=False,
status_file=None):
if wait is None:
if not bufsiz:
bufsiz = 4096
wait = 0.0
else:
if not bufsiz:
bufsiz = 1
wait /= 1000.0
if not wait:
status_file = None
status = Status(status_file)
with RawMode(outfile):
if reset:
outfile.write('\x1bc')
for filename in files:
if filename == '-':
f = sys.stdin
else:
f = open(filename, 'rb')
while True:
buf = f.read(bufsiz)
if not buf:
break
outfile.write(buf)
if wait:
status.write(buf)
outfile.flush()
time.sleep(wait)
if filename != '-':
f.close()
status.close()
if __name__ == "__main__":
doc = globals()['__doc__']
usage, desc = re.match(r'USAGE:\s*(.*?)\n{2,}(.*)', doc,
re.DOTALL | re.IGNORECASE).groups()
parser = OptionParser(usage=usage, description=desc)
parser.add_option('-b', '--buffer-size', type='int', metavar='BYTES',
help='Read this many bytes at at time (default: 1 if -w, else 4096)')
parser.add_option('-r', '--reset', action='store_true',
help='Issue a terminal reset before starting output')
parser.add_option('-s', '--status', metavar='FILE',
help='When run with `-w`, print status to this file')
parser.add_option('-w', '--wait', type='float', metavar='MSEC',
help='Milliseconds to wait between buffers (default: no wait)')
options, args = parser.parse_args()
if not args:
parser.print_usage()
sys.exit(1)
rawcat(args, sys.stdout, bufsiz=options.buffer_size, wait=options.wait,
reset=options.reset, status_file=options.status)