Class: Glaemscribe::API::Glaeml::Shellwords
- Defined in:
- lib/api/glaeml_shellwords.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- ESCAPE_MODE_UNICODE =
1
Instance Method Summary collapse
- #advance_inside_arg(l, i) ⇒ Object
- #advance_inside_escape(l, i) ⇒ Object
-
#initialize ⇒ Shellwords
constructor
A new instance of Shellwords.
- #parse(l) ⇒ Object
- #reset_state ⇒ Object
Constructor Details
#initialize ⇒ Shellwords
Returns a new instance of Shellwords.
31 32 |
# File 'lib/api/glaeml_shellwords.rb', line 31 def initialize end |
Instance Method Details
#advance_inside_arg(l, i) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/api/glaeml_shellwords.rb', line 48 def advance_inside_arg(l,i) if l[i] == "\\" @is_escaping = true @escape_mode = nil else @current_arg += l[i] end end |
#advance_inside_escape(l, i) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/api/glaeml_shellwords.rb', line 57 def advance_inside_escape(l,i) if !@escape_mode # We don't now yet what to do. case l[i] when 'n' @current_arg << "\n" @is_escaping = false return when "\\" @current_arg << "\\" @is_escaping = false when 't' @current_arg << "\t" @is_escaping = false when "\"" @current_arg << "\"" @is_escaping = false when "u" @escape_mode = ESCAPE_MODE_UNICODE @unicode_escape_counter = 0 @unicode_escape_str = '' else raise Error, "Unknown escapment : \\#{l[i]}" end else case @escape_mode when ESCAPE_MODE_UNICODE c = l[i].downcase if !(c =~ /[0-9a-f]/) raise Error, 'Wrong format for unicode escaping, should be \u with 4 hex digits' end @unicode_escape_counter += 1 @unicode_escape_str += c if @unicode_escape_counter == 4 @is_escaping = false @current_arg += [@unicode_escape_str.hex].pack("U") end else raise Error, "Unimplemented escape mode" end end end |
#parse(l) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/api/glaeml_shellwords.rb', line 104 def parse(l) reset_state i = 0 l.length.times{ |i| if !@is_eating_arg next if l[i] =~ /\s/ raise Error, "Glaeml strictly uses double quotes, not simple quotes for args" if l[i] == "'" @is_eating_arg = true @is_eating_arg_between_quotes = (l[i] == "\"") @current_arg << l[i] if !@is_eating_arg_between_quotes else # Eating arg if @is_escaping advance_inside_escape(l,i) else if !@is_eating_arg_between_quotes if l[i] =~ /[\s"]/ @args << @current_arg @current_arg = "" @is_eating_arg = (l[i] == "\"") # Starting a new arg directly @is_eating_arg_between_quotes = @is_eating_arg next else advance_inside_arg(l,i) end else if l[i] == "\"" @args << @current_arg @current_arg = "" @is_eating_arg = false else advance_inside_arg(l,i) end end end end } if @is_eating_arg && @is_eating_arg_between_quotes raise Error, "Unmatched quote." end @args << @current_arg if !@current_arg.empty? @args end |
#reset_state ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/api/glaeml_shellwords.rb', line 36 def reset_state @is_escaping = false @is_eating_arg = false @is_eating_arg_between_quotes = false @args = [] @current_arg = "" @escape_mode = nil @unicode_escape_counter = 0 @unicode_escape_str = '' end |