Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lit] Implement 'cat' command for internal shell
Fixes PR36449 Patch by Chamal de Silva Differential Revision: https://reviews.llvm.org/D43501 llvm-svn: 328563
- Loading branch information
Showing
8 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import getopt | ||
import sys | ||
try: | ||
from StringIO import StringIO | ||
except ImportError: | ||
from io import StringIO | ||
|
||
def convertToCaretAndMNotation(data): | ||
newdata = StringIO() | ||
for char in data: | ||
intval = ord(char) | ||
if intval == 9 or intval == 10: | ||
newdata.write(chr(intval)) | ||
continue | ||
if intval > 127: | ||
intval = intval -128 | ||
newdata.write("M-") | ||
if intval < 32: | ||
newdata.write("^") | ||
newdata.write(chr(intval+64)) | ||
elif intval == 127: | ||
newdata.write("^?") | ||
else: | ||
newdata.write(chr(intval)) | ||
|
||
return newdata.getvalue(); | ||
|
||
|
||
def main(argv): | ||
arguments = argv[1:] | ||
short_options = "v" | ||
long_options = ["show-nonprinting"] | ||
show_nonprinting = False; | ||
|
||
try: | ||
options, filenames = getopt.gnu_getopt(arguments, short_options, long_options) | ||
except getopt.GetoptError as err: | ||
sys.stderr.write("Unsupported: 'cat': %s\n" % str(err)) | ||
sys.exit(1) | ||
|
||
for option, value in options: | ||
if option == "-v" or option == "--show-nonprinting": | ||
show_nonprinting = True; | ||
|
||
for filename in filenames: | ||
try: | ||
fileToCat = open(filename,"rb") | ||
contents = fileToCat.read() | ||
if show_nonprinting: | ||
contents = convertToCaretAndMNotation(contents) | ||
sys.stdout.write(contents) | ||
sys.stdout.flush() | ||
fileToCat.close() | ||
except IOError as error: | ||
sys.stderr.write(str(error)) | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Check error on an unsupported option for cat . | ||
# | ||
# RUN: cat -b temp1.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Check error on a unsupported cat (Unable to find input file). | ||
# | ||
# RUN: cat temp1.txt |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters