Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 1 | #!/usr/bin/wish |
| 2 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 3 | # This file provides many valuable quote and metachar escape processing procedures. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 4 | |
| 5 | |
| 6 | proc escape_bash_quotes { buffer } { |
| 7 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 8 | # Do a bash-style escape of all single quotes in the buffer and return the result. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 9 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 10 | # In bash, if you wish to have a single quote (i.e. apostrophe) inside single quotes, you must escape it. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 23 | # buffer The string whose single quotes are to be escaped. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 24 | |
| 25 | regsub -all {'} $buffer {'\''} new_buffer |
| 26 | return $new_buffer |
| 27 | |
| 28 | } |
| 29 | |
| 30 | |
| 31 | proc quotes_to_curly_braces { buffer } { |
| 32 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 33 | # Convert a single-quoted string to a curly brace-quoted string and return the result. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 34 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 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: '\'' |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 38 | |
| 39 | # Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 40 | # buffer The string whose quotes are to be converted to curly braces. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 59 | # In a bash string "'\''" is an escaped quote which we wish to convert to a single quote. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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 } { |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 67 | # Processing a quote. inside_quotes will tell us whether we've come across a left quote or a right |
| 68 | # quote. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 69 | if { $inside_quotes == 0 } { |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 70 | # Processing closing quote. Add a left curly brace to return_buffer and discard the quote char. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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 { |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 75 | # Processing opening quote. Add a right curly brace to return_buffer and discard the quote char. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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 | |
| 93 | proc curly_braces_to_quotes { buffer } { |
| 94 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 95 | # Convert a curly brace-quoted string to a single-quoted string and return the result. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 96 | |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 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: '\'' |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 100 | |
| 101 | # Description of argument(s): |
Michael Walsh | 410b178 | 2019-10-22 15:56:18 -0500 | [diff] [blame] | 102 | # buffer The string whose curly braces are to be converted to single quotes. |
Michael Walsh | 9a9c835 | 2018-02-15 16:32:48 -0600 | [diff] [blame] | 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 | |
David Shaw | 721f970 | 2020-06-25 14:55:13 -0500 | [diff] [blame] | 115 | proc escape_regex_metachars { buffer } { |
| 116 | |
| 117 | # Escape every regex metacharacter found in buffer and return the result. |
| 118 | |
| 119 | # Example code: |
| 120 | |
| 121 | # set var1 {john*sm(]ith} |
| 122 | # print_vars var1 |
| 123 | # set var1 [escape_regex_metachars $var1] |
| 124 | # print_vars var1 |
| 125 | |
| 126 | # Example output: |
| 127 | |
| 128 | # var1: john*sm(]ith |
| 129 | # var1: john\*sm\(\]ith |
| 130 | |
| 131 | # Description of argument(s): |
| 132 | # buffer The string whose regex metacharacters are to be escaped. |
| 133 | |
| 134 | set escape_chars_regex {[\\\^\$\/\(\)\|\?\+\*\[\]\{\}\,\.]} |
| 135 | regsub -all ${escape_chars_regex} ${buffer} {\\\0} buffer |
| 136 | return ${buffer} |
| 137 | |
| 138 | } |