| 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 opt |
| 27 | |
| 28 | // runeClass is the type of a UTF-8 character classification: A quote, space, |
| 29 | // escape. |
| 30 | type runeClass int |
| 31 | |
| 32 | const ( |
| 33 | other runeClass = iota |
| 34 | space |
| 35 | doubleQuote |
| 36 | singleQuote |
| 37 | backslash |
| 38 | ) |
| 39 | |
| 40 | var runeClasses = map[rune]runeClass{ |
| 41 | ' ': space, |
| 42 | '\t': space, |
| 43 | '\r': space, |
| 44 | '\n': space, |
| 45 | '"': doubleQuote, |
| 46 | '\'': singleQuote, |
| 47 | '\\': backslash, |
| 48 | } |
| 49 | |
| 50 | // the internal state used by the lexer state machine |
| 51 | type lexerState int |
| 52 | |
| 53 | // Lexer state machine states |
| 54 | const ( |
| 55 | // no runes have been seen |
| 56 | start lexerState = iota |
| 57 | // processing regular runes in a word |
| 58 | inWord |
| 59 | // we have just consumed an escape rune; the next rune is literal |
| 60 | escaping |
| 61 | // we have just consumed an escape rune within a quoted string |
| 62 | escapingQuoted |
| 63 | // we are within a quoted string that supports escaping ("...") |
| 64 | inDoubleQuote |
| 65 | // we are within a string that does not support escaping ('...') |
| 66 | 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 argInfo struct { |
| 73 | start int |
| 74 | end int |
| 75 | unquoted string |
| 76 | } |
| 77 | |
| 78 | // Parse a raw command line and return a list of argument info structs |
| 79 | func lexCmdline(raw []rune) []argInfo { |
| 80 | var state lexerState |
| 81 | var unquoted []rune |
| 82 | var argstart int |
| 83 | var infos []argInfo |
| 84 | |
| 85 | state = start |
| 86 | |
| 87 | for i, char := range raw { |
| 88 | class := runeClasses[char] |
| 89 | |
| 90 | switch state { |
| 91 | case start: |
| 92 | // no runes read yet |
| 93 | switch class { |
| 94 | case space: |
| 95 | break |
| 96 | case doubleQuote: |
| 97 | state = inDoubleQuote |
| 98 | case singleQuote: |
| 99 | state = inSingleQuote |
| 100 | case backslash: |
| 101 | state = escaping |
| 102 | default: |
| 103 | // start a new word |
| 104 | unquoted = []rune{char} |
| 105 | state = inWord |
| 106 | } |
| 107 | argstart = i |
| 108 | case inWord: |
| 109 | switch class { |
| 110 | case space: |
| 111 | infos = append(infos, argInfo{ |
| 112 | start: argstart, |
| 113 | end: i, |
| 114 | unquoted: string(unquoted), |
| 115 | }) |
| 116 | unquoted = nil |
| 117 | state = start |
| 118 | case doubleQuote: |
| 119 | state = inDoubleQuote |
| 120 | case singleQuote: |
| 121 | state = inSingleQuote |
| 122 | case backslash: |
| 123 | state = escaping |
| 124 | default: |
| 125 | unquoted = append(unquoted, char) |
| 126 | } |
| 127 | case escaping: |
| 128 | // the rune after an escape character |
| 129 | state = inWord |
| 130 | unquoted = append(unquoted, char) |
| 131 | case escapingQuoted: |
| 132 | // the next rune after an escape character, in double quotes |
| 133 | state = inDoubleQuote |
| 134 | unquoted = append(unquoted, char) |
| 135 | case inDoubleQuote: |
| 136 | switch class { |
| 137 | case doubleQuote: |
| 138 | state = inWord |
| 139 | case backslash: |
| 140 | state = escapingQuoted |
| 141 | default: |
| 142 | unquoted = append(unquoted, char) |
| 143 | } |
| 144 | case inSingleQuote: |
| 145 | switch class { |
| 146 | case singleQuote: |
| 147 | state = inWord |
| 148 | default: |
| 149 | unquoted = append(unquoted, char) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if unquoted != nil { |
| 155 | infos = append(infos, argInfo{ |
| 156 | start: argstart, |
| 157 | end: len(raw), |
| 158 | unquoted: string(unquoted), |
| 159 | }) |
| 160 | } |
| 161 | |
| 162 | return infos |
| 163 | } |