File: shlex.go

package info (click to toggle)
golang-sourcehut-rjarry-go-opt 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (163 lines) | stat: -rw-r--r-- 3,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
103
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
154
155
156
157
158
159
160
161
162
163
// This code has been inspired from https://github.com/google/shlex
//
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// The following changes are published under the MIT license:
//
//  * Internal variables renamed
//  * Recording of the original argument start position in the string
//  * Reformatting of some code parts.
//
// SPDX-License-Identifier: MIT
// Copyright (c) 2023 Robin Jarry

package opt

// runeClass is the type of a UTF-8 character classification: A quote, space,
// escape.
type runeClass int

const (
	other runeClass = iota
	space
	doubleQuote
	singleQuote
	backslash
)

var runeClasses = map[rune]runeClass{
	' ':  space,
	'\t': space,
	'\r': space,
	'\n': space,
	'"':  doubleQuote,
	'\'': singleQuote,
	'\\': backslash,
}

// the internal state used by the lexer state machine
type lexerState int

// Lexer state machine states
const (
	// no runes have been seen
	start lexerState = iota
	// processing regular runes in a word
	inWord
	// we have just consumed an escape rune; the next rune is literal
	escaping
	// we have just consumed an escape rune within a quoted string
	escapingQuoted
	// we are within a quoted string that supports escaping ("...")
	inDoubleQuote
	// we are within a string that does not support escaping ('...')
	inSingleQuote
)

// Each argument info contains the start offset of the raw argument in the
// command line (including shell escapes, quotes, etc.), and its "unquoted"
// value after interpreting shell quotes and escapes.
type argInfo struct {
	start    int
	end      int
	unquoted string
}

// Parse a raw command line and return a list of argument info structs
func lexCmdline(raw []rune) []argInfo {
	var state lexerState
	var unquoted []rune
	var argstart int
	var infos []argInfo

	state = start

	for i, char := range raw {
		class := runeClasses[char]

		switch state {
		case start:
			// no runes read yet
			switch class {
			case space:
				break
			case doubleQuote:
				state = inDoubleQuote
			case singleQuote:
				state = inSingleQuote
			case backslash:
				state = escaping
			default:
				// start a new word
				unquoted = []rune{char}
				state = inWord
			}
			argstart = i
		case inWord:
			switch class {
			case space:
				infos = append(infos, argInfo{
					start:    argstart,
					end:      i,
					unquoted: string(unquoted),
				})
				unquoted = nil
				state = start
			case doubleQuote:
				state = inDoubleQuote
			case singleQuote:
				state = inSingleQuote
			case backslash:
				state = escaping
			default:
				unquoted = append(unquoted, char)
			}
		case escaping:
			// the rune after an escape character
			state = inWord
			unquoted = append(unquoted, char)
		case escapingQuoted:
			// the next rune after an escape character, in double quotes
			state = inDoubleQuote
			unquoted = append(unquoted, char)
		case inDoubleQuote:
			switch class {
			case doubleQuote:
				state = inWord
			case backslash:
				state = escapingQuoted
			default:
				unquoted = append(unquoted, char)
			}
		case inSingleQuote:
			switch class {
			case singleQuote:
				state = inWord
			default:
				unquoted = append(unquoted, char)
			}
		}
	}

	if unquoted != nil {
		infos = append(infos, argInfo{
			start:    argstart,
			end:      len(raw),
			unquoted: string(unquoted),
		})
	}

	return infos
}