blob: e854792f9eacd0e2eb0917bec5ed227ba67b926c [file] [log] [blame]
Michael Walsh9a9c8352018-02-15 16:32:48 -06001#!/usr/bin/wish
2
Michael Walsh410b1782019-10-22 15:56:18 -05003# This file provides many valuable quote and metachar escape processing procedures.
Michael Walsh9a9c8352018-02-15 16:32:48 -06004
5
6proc escape_bash_quotes { buffer } {
7
Michael Walsh410b1782019-10-22 15:56:18 -05008 # Do a bash-style escape of all single quotes in the buffer and return the result.
Michael Walsh9a9c8352018-02-15 16:32:48 -06009
Michael Walsh410b1782019-10-22 15:56:18 -050010 # In bash, if you wish to have a single quote (i.e. apostrophe) inside single quotes, you must escape it.
Michael Walsh9a9c8352018-02-15 16:32:48 -060011
12 # For example, the following bash command:
13 # echo 'Mike'\''s dog'
14 # Will produce the following output.
15 # Mike's dog
16
17 # So, if you pass the following string to this procedure:
18 # Mike's dog
19 # This procedure will return the following:
20 # Mike'\''s dog
21
22 # Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050023 # buffer The string whose single quotes are to be escaped.
Michael Walsh9a9c8352018-02-15 16:32:48 -060024
25 regsub -all {'} $buffer {'\''} new_buffer
26 return $new_buffer
27
28}
29
30
31proc quotes_to_curly_braces { buffer } {
32
Michael Walsh410b1782019-10-22 15:56:18 -050033 # Convert a single-quoted string to a curly brace-quoted string and return the result.
Michael Walsh9a9c8352018-02-15 16:32:48 -060034
Michael Walsh410b1782019-10-22 15:56:18 -050035 # This procedure can help in converting bash expressions, which are quoted with single quotes, to
36 # equivalent TCL expressions which are quoted with curly braces. This procedure will recognize and
37 # preserve a bash single quote escape sequence: '\''
Michael Walsh9a9c8352018-02-15 16:32:48 -060038
39 # Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -050040 # buffer The string whose quotes are to be converted to curly braces.
Michael Walsh9a9c8352018-02-15 16:32:48 -060041
42 # For example, the following code...
43
44 # set buffer {'Mike'\''s dog'}
45 # print_var buffer
46 # set buffer [quotes_to_curly_braces $buffer]
47 # print_var buffer
48
49 # Would produce the following result:
50 # buffer: 'Mike'\''s dog'
51 # buffer: {Mike's dog}
52
53 set quote {'}
54
55 set return_buffer {}
56
57 set inside_quotes 0
58
Michael Walsh410b1782019-10-22 15:56:18 -050059 # In a bash string "'\''" is an escaped quote which we wish to convert to a single quote.
Michael Walsh9a9c8352018-02-15 16:32:48 -060060 set place_holder {supercaliforniaplace_holder}
61 regsub -all {'\\''} $buffer ${place_holder} buffer
62
63 # Walk the string one character at a time.
64 for {set ix 0} {$ix < [string length $buffer]} {incr ix} {
65 set char [string index $buffer $ix]
66 if { $char == $quote } {
Michael Walsh410b1782019-10-22 15:56:18 -050067 # Processing a quote. inside_quotes will tell us whether we've come across a left quote or a right
68 # quote.
Michael Walsh9a9c8352018-02-15 16:32:48 -060069 if { $inside_quotes == 0 } {
Michael Walsh410b1782019-10-22 15:56:18 -050070 # Processing closing quote. Add a left curly brace to return_buffer and discard the quote char.
Michael Walsh9a9c8352018-02-15 16:32:48 -060071 set return_buffer "${return_buffer}\{"
72 # Set inside_quotes to indicate we are now waiting for a closing quote.
73 set inside_quotes 1
74 } else {
Michael Walsh410b1782019-10-22 15:56:18 -050075 # Processing opening quote. Add a right curly brace to return_buffer and discard the quote char.
Michael Walsh9a9c8352018-02-15 16:32:48 -060076 set return_buffer "${return_buffer}\}"
77 # Clear inside_quotes to indicate we have found our closing quote.
78 set inside_quotes 0
79 }
80 } else {
81 # For non-quote character, simply add it to the return buffer/
82 set return_buffer "${return_buffer}${char}"
83 }
84 }
85
86 regsub -all ${place_holder} $return_buffer {'} return_buffer
87
88 return $return_buffer
89
90}
91
92
93proc curly_braces_to_quotes { buffer } {
94
Michael Walsh410b1782019-10-22 15:56:18 -050095 # Convert a curly brace-quoted string to a single-quoted string and return the result.
Michael Walsh9a9c8352018-02-15 16:32:48 -060096
Michael Walsh410b1782019-10-22 15:56:18 -050097 # This procedure can help in converting TCL expressions, which are quoted with curly braces, to equivalent
98 # bash expressions which are quoted with single quotes. This procedure will first convert single quotes to
99 # the bash escaped single quote sequence: '\''
Michael Walsh9a9c8352018-02-15 16:32:48 -0600100
101 # Description of argument(s):
Michael Walsh410b1782019-10-22 15:56:18 -0500102 # buffer The string whose curly braces are to be converted to single quotes.
Michael Walsh9a9c8352018-02-15 16:32:48 -0600103
104 # For example, the following buffer value:
105 # echo {Mike's dog}
106 # Will be changed to this:
107 # echo 'Mike'\''s dog'
108
109 regsub -all {[\{\}]} [escape_bash_quotes $buffer] {'} new_buffer
110 return $new_buffer
111
112}
113
114