1#!/usr/bin/wish 2 3# This file provides many valuable quote and metachar escape processing procedures. 4 5 6proc escape_bash_quotes { buffer } { 7 8 # Do a bash-style escape of all single quotes in the buffer and return the result. 9 10 # In bash, if you wish to have a single quote (i.e. apostrophe) inside single quotes, you must escape it. 11 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): 23 # buffer The string whose single quotes are to be escaped. 24 25 regsub -all {'} $buffer {'\''} new_buffer 26 return $new_buffer 27 28} 29 30 31proc quotes_to_curly_braces { buffer } { 32 33 # Convert a single-quoted string to a curly brace-quoted string and return the result. 34 35 # 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: '\'' 38 39 # Description of argument(s): 40 # buffer The string whose quotes are to be converted to curly braces. 41 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 59 # In a bash string "'\''" is an escaped quote which we wish to convert to a single quote. 60 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 } { 67 # Processing a quote. inside_quotes will tell us whether we've come across a left quote or a right 68 # quote. 69 if { $inside_quotes == 0 } { 70 # Processing closing quote. Add a left curly brace to return_buffer and discard the quote char. 71 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 { 75 # Processing opening quote. Add a right curly brace to return_buffer and discard the quote char. 76 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 95 # Convert a curly brace-quoted string to a single-quoted string and return the result. 96 97 # 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: '\'' 100 101 # Description of argument(s): 102 # buffer The string whose curly braces are to be converted to single quotes. 103 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 115