Stefan Schuermans commited on 2020-10-18 11:38:44
Showing 1 changed files, with 21 additions and 1 deletions.
Fix environment variable assignment syntax when copying to clipboard. Assignment like X='a b' were copied to clipboard as 'X=a b', which does not work.
... | ... |
@@ -6,6 +6,7 @@ Graphical user interface of UProcTrace. |
6 | 6 |
""" |
7 | 7 |
|
8 | 8 |
import functools |
9 |
+import re |
|
9 | 10 |
import shlex |
10 | 11 |
import signal |
11 | 12 |
import time |
... | ... |
@@ -22,6 +23,9 @@ from gi.repository import Gdk, Gtk, GLib |
22 | 23 |
|
23 | 24 |
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit) |
24 | 25 |
|
26 |
+# regular expression for an environment variable assignment |
|
27 |
+re_env_var = re.compile(r'^(?P<name>[A-Za-z_][A-Za-z0-9_]*)=(?P<value>.*)$') |
|
28 |
+ |
|
25 | 29 |
|
26 | 30 |
def add_none(val_a: int, val_b: int) -> int: |
27 | 31 |
""" |
... | ... |
@@ -38,7 +42,23 @@ def cmdline2str(cmdline: list) -> str: |
38 | 42 |
""" |
39 | 43 |
if cmdline is None: |
40 | 44 |
return '???' |
41 |
- return ' '.join([shlex.quote(s) for s in cmdline]) |
|
45 |
+ return ' '.join([cmdline_str_escape(s) for s in cmdline]) |
|
46 |
+ |
|
47 |
+ |
|
48 |
+def cmdline_str_escape(s: str) -> str: |
|
49 |
+ """ |
|
50 |
+ Escape a command line string for shell use in a way that also works for |
|
51 |
+ environment variables (i.e., not escaping the variable name). |
|
52 |
+ """ |
|
53 |
+ m = re_env_var.match(s) |
|
54 |
+ if not m: |
|
55 |
+ # not a variable assignment -> escape entire string |
|
56 |
+ return shlex.quote(s) |
|
57 |
+ # variable assignment -> escape only value part |
|
58 |
+ # (also works if it only looks like a variable assignment) |
|
59 |
+ name = m.group('name') |
|
60 |
+ value = shlex.quote(m.group('value')) |
|
61 |
+ return f'{name:s}={value:s}' |
|
42 | 62 |
|
43 | 63 |
|
44 | 64 |
def duration2str(duration: float) -> str: |
45 | 65 |