#!/usr/bin/python3 # The MIT License (MIT) # # Copyright © 2024 pacman64 # # 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. from shutil import copyfileobj from sys import argv, exit, stderr, stdin, stdout from typing import List info = ''' dog [options...] [filepaths/URIs...] This script reads/fetches all named sources given to it, which can be a mix of filepaths and URIs. Its name suggests that, unlike `cat`, it can also fetch URIs. Any of `-l`, `--l`, `-lines`, or `--lines` enables line-mode, which ensures each non-empty input ends with a line feed, even when the original data do not; this mode also ignores leading UTF-8 BOM on first lines, and turns all CRLF byte-pairs into simple line-feeds. In other words, line-mode ensures unix-style output lines. The help option is `-h`, `--h`, `-help`, or `--help`. ''' # a leading help-option arg means show the help message and quit if len(argv) == 2 and argv[1] in ('-h', '--h', '-help', '--help'): print(info.strip(), file=stderr) exit(0) def seems_url(s: str) -> bool: protocols = ('https://', 'http://', 'file://', 'ftp://', 'data:') return any(s.startswith(p) for p in protocols) def bytes_mode(args: List[str]) -> None: dashes = 0 data = bytes() reuse_stdin = args.count('-') > 1 if any(seems_url(e) for e in args): from urllib.request import urlopen for path in args: if path == '-': dashes += 1 if reuse_stdin and dashes == 1: data = stdin.buffer.read() if reuse_stdin: stdout.buffer.write(data) stdout.flush() continue copyfileobj(stdin.buffer, stdout.buffer) stdout.flush() continue if seems_url(path): with urlopen(path) as inp: copyfileobj(inp, stdout.buffer) stdout.flush() continue with open(path, mode='rb') as inp: copyfileobj(inp, stdout.buffer) stdout.flush() if len(args) == 0: copyfileobj(stdin.buffer, stdout.buffer) stdout.flush() def lines_mode(args: List[str]) -> None: dashes = 0 lines = [] reuse_stdin = args.count('-') > 1 if any(seems_url(e) for e in args): from urllib.request import urlopen for path in args: if path == '-': dashes += 1 if reuse_stdin and dashes == 1: lines = load_lines(stdin, '\xef\xbb\xbf', '\r\n', '\n') if reuse_stdin: for line in lines: stdout.write(line) stdout.write('\n') stdout.flush() continue emit_lines(stdout, stdin, '\xef\xbb\xbf', '\r\n', '\n') stdout.flush() continue if seems_url(path): with urlopen(path) as inp: w = stdout.buffer emit_lines(w, inp, b'\xef\xbb\xbf', b'\r\n', b'\n') stdout.flush() continue with open(path, encoding='utf-8') as inp: emit_lines(stdout, inp, '\xef\xbb\xbf', '\r\n', '\n') stdout.flush() if len(args) == 0: emit_lines(stdout, stdin, '\xef\xbb\xbf', '\r\n', '\n') stdout.flush() def load_lines(src, utf8bom, crlf, lf) -> List[str]: lines = [] first = True for line in src: line = line.lstrip(utf8bom) if first else line # ignore trailing carriage-returns and line-feeds in lines lines.append(line.rstrip(crlf).rstrip(lf)) first = False return lines def emit_lines(w, src, utf8bom, crlf, lf) -> None: first = True for line in src: line = line.lstrip(utf8bom) if first else line # ignore trailing carriage-returns and line-feeds in lines w.write(line.rstrip(crlf).rstrip(lf)) w.write(lf) first = False try: if len(argv) > 1 and argv[1] in ('-l', '--l', '-lines', '--lines'): lines_mode(argv[2:]) else: bytes_mode(argv[1:]) except BrokenPipeError: # quit quietly, instead of showing a confusing error message stderr.close() except KeyboardInterrupt: exit(2) except Exception as e: print(f'\x1b[31m{e}\x1b[0m', file=stderr) exit(1)