#!/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 itertools import islice, chain from subprocess import call from sys import argv, exit, stderr, stdin, stdout info = ''' xnargs [args...] Similar to `xargs`, eXtra Null-separated ARGumentS takes such strings from standard-input and uses them to extend the cmd/arg list given as cmd-line arguments, running the resulting extended command. This tool is a very convenient way to pass filepaths which may have any allowed (null-bytes aren't allowed) symbol in them to another app/tool. ''' if len(argv) < 2 or argv[1] in ('-h', '--h', '-help', '--help'): print(info.strip()) exit(0) try: # extras = stdin.buffer.read() # extras = extras.lstrip(b'\xef\xbb\xbf').rstrip(b'\x00') # extras = (str(e, encoding='utf-8') for e in extras.split(b'\x00')) extras = stdin.read() extras = extras.lstrip('\xef\xbb\xbf').rstrip('\x00') extras = (e for e in extras.split('\x00')) args = chain(islice(argv, 1, None), extras) exit(call(args, stdin=stdin, stdout=stdout, stderr=stderr)) except BrokenPipeError: stderr.close() exit(0) except KeyboardInterrupt: stderr.close() exit(1) except Exception as e: print(f'\x1b[31m{e}\x1b[0m', file=stderr) exit(1)