| 1 | // This code has been inspired from https://github.com/google/shlex |
| 2 | // |
| 3 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // The following changes are published under the MIT license: |
| 18 | // |
| 19 | // * Internal variables renamed |
| 20 | // * Recording of the original argument start position in the string |
| 21 | // * Reformatting of some code parts. |
| 22 | // |
| 23 | // SPDX-License-Identifier: MIT |
| 24 | // Copyright (c) 2023 Robin Jarry |
| 25 | |
| 26 | package optscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/ |
| 27 | |
| 28 | // runeClass is the type of a UTF-8 character classification: A quote, space, |
| 29 | // escape. |
| 30 | type runeClassscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/runeClass# int |
| 31 | |
| 32 | const ( |
| 33 | otherscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/other. runeClassscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/runeClass# = iota |
| 34 | spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space. |
| 35 | doubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/doubleQuote. |
| 36 | singleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/singleQuote. |
| 37 | backslashscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/backslash. |
| 38 | ) |
| 39 | |
| 40 | var runeClassesscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/runeClasses. = map[rune]runeClassscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/runeClass#{ |
| 41 | ' ': spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space., |
| 42 | '\t': spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space., |
| 43 | '\r': spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space., |
| 44 | '\n': spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space., |
| 45 | '"': doubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/doubleQuote., |
| 46 | '\'': singleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/singleQuote., |
| 47 | '\\': backslashscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/backslash., |
| 48 | } |
| 49 | |
| 50 | // the internal state used by the lexer state machine |
| 51 | type lexerStatescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/lexerState# int |
| 52 | |
| 53 | // Lexer state machine states |
| 54 | const ( |
| 55 | // no runes have been seen |
| 56 | startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/start. lexerStatescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/lexerState# = iota |
| 57 | // processing regular runes in a word |
| 58 | inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord. |
| 59 | // we have just consumed an escape rune; the next rune is literal |
| 60 | escapingscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escaping. |
| 61 | // we have just consumed an escape rune within a quoted string |
| 62 | escapingQuotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escapingQuoted. |
| 63 | // we are within a quoted string that supports escaping ("...") |
| 64 | inDoubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inDoubleQuote. |
| 65 | // we are within a string that does not support escaping ('...') |
| 66 | inSingleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inSingleQuote. |
| 67 | ) |
| 68 | |
| 69 | // Each argument info contains the start offset of the raw argument in the |
| 70 | // command line (including shell escapes, quotes, etc.), and its "unquoted" |
| 71 | // value after interpreting shell quotes and escapes. |
| 72 | type argInfoscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo# struct { |
| 73 | startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#start. int |
| 74 | endscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#end. int |
| 75 | unquotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#unquoted. string |
| 76 | } |
| 77 | |
| 78 | // Parse a raw command line and return a list of argument info structs |
| 79 | func lexCmdlinescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/lexCmdline().(raw []rune) []argInfoscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo# { |
| 80 | var state lexerStatescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/lexerState# |
| 81 | var unquoted []rune |
| 82 | var argstart int |
| 83 | var infos []argInfoscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo# |
| 84 | |
| 85 | state = startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/start. |
| 86 | |
| 87 | for i, char := range raw { |
| 88 | class := runeClassesscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/runeClasses.[char] |
| 89 | |
| 90 | switch state { |
| 91 | case startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/start.: |
| 92 | // no runes read yet |
| 93 | switch class { |
| 94 | case spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space.: |
| 95 | break |
| 96 | case doubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/doubleQuote.: |
| 97 | state = inDoubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inDoubleQuote. |
| 98 | case singleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/singleQuote.: |
| 99 | state = inSingleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inSingleQuote. |
| 100 | case backslashscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/backslash.: |
| 101 | state = escapingscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escaping. |
| 102 | default: |
| 103 | // start a new word |
| 104 | unquoted = []rune{char} |
| 105 | state = inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord. |
| 106 | } |
| 107 | argstart = i |
| 108 | case inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord.: |
| 109 | switch class { |
| 110 | case spacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/space.: |
| 111 | infos = append(infos, argInfoscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#{ |
| 112 | startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#start.: argstart, |
| 113 | endscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#end.: i, |
| 114 | unquotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#unquoted.: string(unquoted), |
| 115 | }) |
| 116 | unquoted = nil |
| 117 | state = startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/start. |
| 118 | case doubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/doubleQuote.: |
| 119 | state = inDoubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inDoubleQuote. |
| 120 | case singleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/singleQuote.: |
| 121 | state = inSingleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inSingleQuote. |
| 122 | case backslashscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/backslash.: |
| 123 | state = escapingscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escaping. |
| 124 | default: |
| 125 | unquoted = append(unquoted, char) |
| 126 | } |
| 127 | case escapingscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escaping.: |
| 128 | // the rune after an escape character |
| 129 | state = inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord. |
| 130 | unquoted = append(unquoted, char) |
| 131 | case escapingQuotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escapingQuoted.: |
| 132 | // the next rune after an escape character, in double quotes |
| 133 | state = inDoubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inDoubleQuote. |
| 134 | unquoted = append(unquoted, char) |
| 135 | case inDoubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inDoubleQuote.: |
| 136 | switch class { |
| 137 | case doubleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/doubleQuote.: |
| 138 | state = inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord. |
| 139 | case backslashscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/backslash.: |
| 140 | state = escapingQuotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/escapingQuoted. |
| 141 | default: |
| 142 | unquoted = append(unquoted, char) |
| 143 | } |
| 144 | case inSingleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inSingleQuote.: |
| 145 | switch class { |
| 146 | case singleQuotescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/singleQuote.: |
| 147 | state = inWordscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/inWord. |
| 148 | default: |
| 149 | unquoted = append(unquoted, char) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if unquoted != nil { |
| 155 | infos = append(infos, argInfoscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#{ |
| 156 | startscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#start.: argstart, |
| 157 | endscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#end.: len(raw), |
| 158 | unquotedscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/argInfo#unquoted.: string(unquoted), |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | return infos |
| 163 | } |