blob: 493d943e33bc71351bb1f3c6d8c05de3ac0dec9a [file] [log] [blame]
Michael Walsh9a9c8352018-02-15 16:32:48 -06001#!/usr/bin/wish
2
3# This file provides many valuable quote and metachar escape processing
4# procedures.
5
6
7proc escape_bash_quotes { buffer } {
8
9 # Do a bash-style escape of all single quotes in the buffer and return the
10 # result.
11
12 # In bash, if you wish to have a single quote (i.e. apostrophe) inside
13 # single quotes, you must escape it.
14
15 # For example, the following bash command:
16 # echo 'Mike'\''s dog'
17 # Will produce the following output.
18 # Mike's dog
19
20 # So, if you pass the following string to this procedure:
21 # Mike's dog
22 # This procedure will return the following:
23 # Mike'\''s dog
24
25 # Description of argument(s):
26 # buffer The string whose single quotes are to be
27 # escaped.
28
29 regsub -all {'} $buffer {'\''} new_buffer
30 return $new_buffer
31
32}
33
34
35proc quotes_to_curly_braces { buffer } {
36
37 # Convert a single-quoted string to a curly brace-quoted string and return
38 # the result.
39
40 # This procedure can help in converting bash expressions, which are quoted
41 # with single quotes, to equivalent TCL expressions which are quoted with
42 # curly braces. This procedure will recognize and preserve a bash single
43 # quote escape sequence: '\''
44
45 # Description of argument(s):
46 # buffer The string whose quotes are to be
47 # converted to curly braces.
48
49 # For example, the following code...
50
51 # set buffer {'Mike'\''s dog'}
52 # print_var buffer
53 # set buffer [quotes_to_curly_braces $buffer]
54 # print_var buffer
55
56 # Would produce the following result:
57 # buffer: 'Mike'\''s dog'
58 # buffer: {Mike's dog}
59
60 set quote {'}
61
62 set return_buffer {}
63
64 set inside_quotes 0
65
66 # In a bash string "'\''" is an escaped quote which we wish to convert to a
67 # single quote.
68 set place_holder {supercaliforniaplace_holder}
69 regsub -all {'\\''} $buffer ${place_holder} buffer
70
71 # Walk the string one character at a time.
72 for {set ix 0} {$ix < [string length $buffer]} {incr ix} {
73 set char [string index $buffer $ix]
74 if { $char == $quote } {
75 # Processing a quote. inside_quotes will tell us whether we've come
76 # across a left quote or a right quote.
77 if { $inside_quotes == 0 } {
78 # Processing closing quote. Add a left curly brace to return_buffer
79 # and discard the quote char.
80 set return_buffer "${return_buffer}\{"
81 # Set inside_quotes to indicate we are now waiting for a closing quote.
82 set inside_quotes 1
83 } else {
84 # Processing opening quote. Add a right curly brace to return_buffer
85 # and discard the quote char.
86 set return_buffer "${return_buffer}\}"
87 # Clear inside_quotes to indicate we have found our closing quote.
88 set inside_quotes 0
89 }
90 } else {
91 # For non-quote character, simply add it to the return buffer/
92 set return_buffer "${return_buffer}${char}"
93 }
94 }
95
96 regsub -all ${place_holder} $return_buffer {'} return_buffer
97
98 return $return_buffer
99
100}
101
102
103proc curly_braces_to_quotes { buffer } {
104
105 # Convert a curly brace-quoted string to a single-quoted string and return
106 # the result.
107
108 # This procedure can help in converting TCL expressions, which are quoted
109 # with curly braces, to equivalent bash expressions which are quoted with
110 # single quotes. This procedure will first convert single quotes to the
111 # bash escaped single quote sequence: '\''
112
113 # Description of argument(s):
114 # buffer The string whose curly braces are to be
115 # converted to single quotes.
116
117 # For example, the following buffer value:
118 # echo {Mike's dog}
119 # Will be changed to this:
120 # echo 'Mike'\''s dog'
121
122 regsub -all {[\{\}]} [escape_bash_quotes $buffer] {'} new_buffer
123 return $new_buffer
124
125}
126
127