forked from fvwmorg/fvwm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFvwmCommand.in
156 lines (140 loc) · 5.24 KB
/
FvwmCommand.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!@PYTHON@
import socket
import getopt
import sys
import os
import stat
import json
# Fvwm3 - socket API and MFL UNIX socket
# Simple Python 3 FvwmMFL client which purpose is to act as FVWM2 FvwmCommand
# replacement in FVWM3. It uses FvwmMFL unix domain socket to send FVWM commands
# at this moment, that is all. It doesn't parse json structures yet, or anything
# fancy and new from FVWM3 new command processing interface.
def mflclnt(verbose, read_from_stdin, info, fvwm_socket, args):
# Create a unix domain socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
# Connect the socket to the "port" where the server is listening
# FvwmMFL is setting this in environment. This is done by using
# the explicit -f <sockname> flag if it is provided, otherwise
# use the environment FVWMMFL_SOCKET if it is defined, then fall
# back to TMPDIR/fvwm_mfl.sock if TMPDIR is defined, and last use
# the default location /tmp/fvwm_mfl.sock.
if not fvwm_socket:
if 'FVWMMFL_SOCKET' in os.environ:
fvwm_socket = str(os.environ.get('FVWMMFL_SOCKET'))
elif 'TMPDIR' in os.environ:
fvwm_socket = str(os.environ.get('TMPDIR')) + '/fvwm_mfl.sock'
else:
fvwm_socket = '/tmp/fvwm_mfl.sock'
def_socket = True
else:
def_socket = False
try:
if verbose:
print ("Trying to determine if", fvwm_socket, "exists as MFL socket ...")
mode = os.stat(fvwm_socket).st_mode
if stat.S_ISSOCK(mode):
server_address = fvwm_socket
if verbose:
print ("Using", fvwm_socket, "as MFL socket.")
except FileNotFoundError:
print ("Cannot find FvwmMFL socket " + fvwm_socket + ".")
if def_socket:
print ("Please start FvwmMFL module from your fvwm configuration.")
else:
print ("FvwmMFL socket " + fvwm_socket + " does not exist or it is not active.")
sys.exit(1)
if verbose:
print('Connecting to {}'.format(server_address))
try:
sock.connect(server_address)
except socket.error as msg:
print(msg)
sys.exit(1)
try:
if verbose:
print ("Sending data to MFL socket", server_address)
# Send data, either multiple commands from pipe, arguments
if read_from_stdin:
for stdline in sys.stdin:
message = stdline.encode()
sock.sendall(message)
else:
message = " ".join(args).encode()
if len(args) > 0:
sock.sendall(message)
if verbose:
print('Sent {!r}'.format(message) + " to FvwmMFL")
amount_received = 0
data = sock.recv(32768)
# amount_received += len(data)
# print (amount_received)
if verbose:
print('Received {!r}'.format(data))
if info:
jparsed = json.loads(data.decode('utf-8'))
print (json.dumps(jparsed, sort_keys=True, indent=4))
# Close unix domain socket
finally:
if verbose:
print('Closing connection to socket', server_address)
sock.close()
def usage():
sockname = str(os.environ.get('FVWMMFL_SOCKET'))
text = """Usage: FvwmCommand [OPTION] [COMMAND]...
Send commands to fvwm3 via FvwmMFL
-c read and send commands from stdin
The COMMAND if any is ignored.
-f <file name> use unix socket <file name> to connect to FvwmMFL
-i <info level> 0 - default
>0 - print FvwmMFL feedback
-d print detailed diagnostic messages
-v print version number
Default unix socket name is:"""
print (text, sockname)
def main():
# Defaults for mflclnt function
verbose = False
info = False
read_from_stdin = False
fvwm_socket = False
versioninfo = "@VERSION@-@VERSIONINFO@"
# Getopt loop
try:
opts, args = getopt.getopt(sys.argv[1:], "hvcdi:f:FmrSw:", ["help", "verbose", "cmds", "detailed", "info=", "socket="])
except getopt.GetoptError as err:
# print help and usage informations and exit
print(err)
usage()
sys.exit(2)
for opt, arg in opts:
# Print version
if opt in ("-v", "--version"):
print ("FvwmCommand " + versioninfo)
sys.exit()
# Read one or multiple commands from stdin
elif opt in ("-c", "--cmds"):
read_from_stdin = True
# Provide FvwmMFL feedback on stdout
elif opt in ("-i", "--info"):
if arg != "0":
info = True
# Specify FvwmMFL socket explicitly
elif opt in ("-f", "--socket"):
fvwm_socket = arg
# Print diagnostic messages
elif opt in ("-d", "--detailed"):
verbose = True
# Print help usage message
elif opt in ("-h", "--help"):
usage()
sys.exit()
# Silently ignore obsolete options from FVWM2 FvwmCommand
elif opt in ("-F", "-m", "-r", "-S", "-w"):
pass
else:
assert False, "unhandled option"
# Call out socket function finally.
mflclnt(verbose, read_from_stdin, info, fvwm_socket, args)
if __name__ == "__main__":
main()