chiark / gitweb /
fishdescriptor: before redo option parsing
[chiark-utils.git] / fishdescriptor / fishdescriptor
1 #!/usr/bin/python3
2
3 import sys
4 import fishdescriptor.fish
5 import re
6
7 donor = None
8
9 usage =
10 '''
11 fishdescriptor <pid> <action>... [-p|--pid <pid> <action>...]
12
13 <action>s
14   <there-fd>[=<here>]
15           fish the openfile referenced by descriptor <there-fd> in
16           (the most recent) <pid> and keep a descriptor onto it;
17           and, optionally, give it the number <here> for exec
18   exec <program> [<arg>...]
19           execute a process with each specified numeric <here>
20           as an actual fd
21   sockinfo
22           calls getsockname/getpeername on the most recent
23           <there-fd>
24
25   -p|-pid <pid>
26           now attach to <pid>, detaching from previous pid
27 '''
28
29 def set_donor(pid_arg):
30     pid = int(pid_arg)
31     if donor is not None: donor.detach()
32     donor = fishdescriptor.fish.Donor(pid)
33
34 def process_args():
35     def arg_matches(regexp):
36         nonlocal m
37         m = re.search(regexp, arg)
38         return m
39
40     arg_ix = 0
41     def next_arg():
42         sys.argv[arg_ix] = 
43
44     while arg_ix < len(sys.argv):
45         arg = next_arg()
46         if arg_matches(r'^-p(\d+)') or
47            arg_matches(r'^--pid=(\d+)$')
48             set_donor(m.groups(1)[0])
49             
50             pid = 
51 (?:-p|^--pid(?:=|$))(\d*)$'):
52             
53             pid = int(m.groups(1)[0])
54         elif arg_matches(r'^(?:-p|^--pid=)(\d+)$'):
55             
56         if expecting_pid:
57             set_donor(arg)
58         el
59         elif arg_matches(r'\d+
60             
61     parser = OptionParser(usage=usage, )
62
63     parser.add_option("-p", "--pid", type='int', callback=set_donor)
64     parser.add_option(
65
66 pid = int(sys.argv[1])
67 fds = [int(x) for x in sys.argv[2:]]
68
69 d = fishdescriptor.fish.Donor(pid)
70 r = d.fish(fds)
71 print(repr(r))