aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2013-10-11 13:27:09 +0200
committerMattias Andrée <maandree@operamail.com>2013-10-11 13:27:09 +0200
commit4def369b43fb35806e7f3d484f2b924a32392eb9 (patch)
treeebf205ea42246aa3b0d292b5b6b673682b48fdbc
parentimplement --iterations (diff)
downloadgpp-4def369b43fb35806e7f3d484f2b924a32392eb9.tar.gz
gpp-4def369b43fb35806e7f3d484f2b924a32392eb9.tar.bz2
gpp-4def369b43fb35806e7f3d484f2b924a32392eb9.tar.xz
add shebang support
-rwxr-xr-xsrc/gpp.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/gpp.py b/src/gpp.py
index 9ba6d42..f40dbb8 100755
--- a/src/gpp.py
+++ b/src/gpp.py
@@ -1,6 +1,8 @@
#!/usr/bin/env python3
+import os
import sys
+import shlex
from subprocess import Popen, PIPE
symbol = '@'
@@ -8,13 +10,24 @@ encoding = 'utf-8'
iterations = 1
input_file = '/dev/stdin'
output_file = '/dev/stdout'
+unshebang = False
-for i in range(1, len(sys.argv)):
- arg = sys.argv[i]
+args = sys.argv
+
+# If the file is executed, the first command line argument will be the first argument in the shebang,
+# the second will be the rest of the arguments in the command line as is and the third and final will
+# be the executed file.
+if len(args) == 3:
+ if os.path.exists(args[2]) and os.path.isfile(args[2]) and ((os.stat(args[2]).st_mode & 0o111) != 0):
+ args[1 : 2] = shlex.split(args[1])
+
+for i in range(1, len(args)):
+ arg = args[i]
i += 1
if arg in ('-s', '--symbol'): symbol = sys.argv[i]
elif arg in ('-e', '--encoding'): encoding = sys.argv[i]
elif arg in ('-n', '--iterations'): iterations = int(sys.argv[i])
+ elif arg in ('-u', '--unshebang'): unshebang = True; continue
elif arg in ('-i', '--input'): input_file = sys.argv[i]
elif arg in ('-o', '--output'): output_file = sys.argv[i]
elif arg in ('-f', '--file'):
@@ -41,6 +54,10 @@ data = None
with open(input_file, 'rb') as file:
data = file.read().decode(encoding, 'error').split('\n')
+if unshebang:
+ if data[0][:2] == '#!':
+ data[:] = data[1:]
+
def pp(line):
rc = ''
symb = False