-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes.rs
More file actions
479 lines (422 loc) · 15.2 KB
/
Copy pathquotes.rs
File metadata and controls
479 lines (422 loc) · 15.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
//! Quote Processing (Phase 6 M13)
//!
//! Implements POSIX-compliant quote processing for shell arguments:
//! - Single quotes (') - preserve all characters literally
//! - Double quotes (") - allow variable/command expansion, prevent glob
//! - Backslash (\) - escape next character
//!
//! # POSIX Semantics
//!
//! **Single Quotes**:
//! - All characters literal (no expansion whatsoever)
//! - Cannot contain single quote (even escaped)
//! - Whitespace preserved
//!
//! **Double Quotes**:
//! - Variable expansion: $VAR, ${VAR}
//! - Command substitution: $(cmd), `cmd`
//! - Arithmetic expansion: $((expr))
//! - NO glob expansion
//! - Backslash escapes: \$, \`, \", \\, \newline
//! - Whitespace preserved
//!
//! **Backslash (unquoted)**:
//! - Escapes next character literally
//! - At end of line: line continuation
//!
//! # Examples
//!
//! ```no_run
//! use vsh::quotes::{parse_quotes, QuoteState};
//!
//! // Single quotes - no expansion
//! let segments = parse_quotes("'$HOME'").unwrap();
//! assert_eq!(segments[0].state, QuoteState::SingleQuoted);
//!
//! // Double quotes - expansion allowed
//! let segments = parse_quotes("\"$HOME\"").unwrap();
//! assert_eq!(segments[0].state, QuoteState::DoubleQuoted);
//!
//! // Backslash escape
//! let segments = parse_quotes("hello\\ world").unwrap();
//! // Content will be "hello world"
//! ```
use anyhow::Result;
/// Quote state for a segment of text
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuoteState {
/// No quotes - full expansion (variables, globs, command substitution)
Unquoted,
/// Single-quoted - literal text (no expansion)
SingleQuoted,
/// Double-quoted - variable/command expansion only (no globs)
DoubleQuoted,
}
/// A segment of text with its quote state
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuotedSegment {
/// The text content (quotes removed)
pub content: String,
/// The quote state of this segment
pub state: QuoteState,
}
impl QuotedSegment {
/// Create a new quoted segment
pub fn new(content: String, state: QuoteState) -> Self {
Self { content, state }
}
/// Check if this segment allows variable expansion
pub fn allows_variable_expansion(&self) -> bool {
matches!(self.state, QuoteState::Unquoted | QuoteState::DoubleQuoted)
}
/// Check if this segment allows glob expansion
pub fn allows_glob_expansion(&self) -> bool {
self.state == QuoteState::Unquoted
}
}
/// Parse a string into quoted segments
///
/// Processes quotes and backslash escapes according to POSIX rules.
/// Returns a vector of segments, each with its quote state.
///
/// # POSIX Quote Rules
///
/// - `'...'` - Single quotes: all characters literal
/// - `"..."` - Double quotes: expansion allowed except globs
/// - `\x` - Backslash: escapes next character (outside quotes)
/// - `\x` in `"..."` - Escapes only: $ ` " \ newline
/// - `\x` in `'...'` - Literal backslash (no escaping)
///
/// # Examples
///
/// ```no_run
/// use vsh::quotes::parse_quotes;
///
/// // Single quotes
/// let segments = parse_quotes("'hello world'").unwrap();
/// assert_eq!(segments.len(), 1);
/// assert_eq!(segments[0].content, "hello world");
///
/// // Double quotes
/// let segments = parse_quotes("\"$HOME/.config\"").unwrap();
/// assert_eq!(segments[0].content, "$HOME/.config");
///
/// // Mixed quotes
/// let segments = parse_quotes("'hello'\" world\"").unwrap();
/// assert_eq!(segments.len(), 2);
/// ```
///
/// # Errors
///
/// Returns error if:
/// - Quote is unclosed (missing closing quote)
pub fn parse_quotes(input: &str) -> Result<Vec<QuotedSegment>> {
let mut segments = Vec::new();
let mut current = String::new();
let mut state = QuoteState::Unquoted;
let mut chars = input.chars().peekable();
while let Some(ch) = chars.next() {
match (state.clone(), ch) {
// === UNQUOTED STATE ===
// Unquoted → single quote
(QuoteState::Unquoted, '\'') => {
if !current.is_empty() {
segments.push(QuotedSegment::new(current.clone(), QuoteState::Unquoted));
current.clear();
}
state = QuoteState::SingleQuoted;
}
// Unquoted → double quote
(QuoteState::Unquoted, '"') => {
if !current.is_empty() {
segments.push(QuotedSegment::new(current.clone(), QuoteState::Unquoted));
current.clear();
}
state = QuoteState::DoubleQuoted;
}
// Unquoted backslash escape
(QuoteState::Unquoted, '\\') => {
if let Some(next) = chars.next() {
// Backslash-newline removed (line continuation)
if next != '\n' {
current.push(next);
}
} else {
// Backslash at end of input - keep it
current.push('\\');
}
}
// === SINGLE QUOTE STATE ===
// Single quote → close
(QuoteState::SingleQuoted, '\'') => {
segments.push(QuotedSegment::new(
current.clone(),
QuoteState::SingleQuoted,
));
current.clear();
state = QuoteState::Unquoted;
}
// Single quote - all characters literal
(QuoteState::SingleQuoted, ch) => {
current.push(ch);
}
// === DOUBLE QUOTE STATE ===
// Double quote → close
(QuoteState::DoubleQuoted, '"') => {
segments.push(QuotedSegment::new(
current.clone(),
QuoteState::DoubleQuoted,
));
current.clear();
state = QuoteState::Unquoted;
}
// Double quote backslash (limited escaping)
(QuoteState::DoubleQuoted, '\\') => {
if let Some(&next) = chars.peek() {
// Only escape these special characters in double quotes
if matches!(next, '$' | '`' | '"' | '\\' | '\n') {
chars.next(); // Consume the next character
if next != '\n' {
// Backslash-newline removed
current.push(next);
}
} else {
// Other characters: keep the backslash
current.push('\\');
}
} else {
// Backslash at end of input
current.push('\\');
}
}
// Double quote - regular character
(QuoteState::DoubleQuoted, ch) => {
current.push(ch);
}
// === UNQUOTED REGULAR CHARACTER ===
(QuoteState::Unquoted, ch) => {
current.push(ch);
}
}
}
// Check for unclosed quotes
if state != QuoteState::Unquoted {
anyhow::bail!(
"Unclosed quote (expected closing {})",
match state {
QuoteState::SingleQuoted => "'",
QuoteState::DoubleQuoted => "\"",
QuoteState::Unquoted => unreachable!(),
}
);
}
// Push final segment if any
if !current.is_empty() {
segments.push(QuotedSegment::new(current, state));
}
Ok(segments)
}
/// Check if quoted segments should undergo glob expansion
///
/// Returns true if any unquoted segment contains glob patterns.
/// Quoted segments (single or double) never expand globs.
///
/// # Examples
///
/// ```no_run
/// use vsh::quotes::{parse_quotes, should_expand_glob};
///
/// // Unquoted glob pattern → expand
/// let segments = parse_quotes("*.txt").unwrap();
/// assert!(should_expand_glob(&segments));
///
/// // Single-quoted glob → no expansion
/// let segments = parse_quotes("'*.txt'").unwrap();
/// assert!(!should_expand_glob(&segments));
///
/// // Double-quoted glob → no expansion
/// let segments = parse_quotes("\"*.txt\"").unwrap();
/// assert!(!should_expand_glob(&segments));
/// ```
pub fn should_expand_glob(segments: &[QuotedSegment]) -> bool {
segments
.iter()
.any(|seg| seg.allows_glob_expansion() && crate::glob::contains_glob_pattern(&seg.content))
}
/// Reconstruct the expanded string from quoted segments
///
/// For each segment:
/// - Unquoted/DoubleQuoted: content as-is (caller handles expansion)
/// - SingleQuoted: content as-is (literal)
///
/// This function does NOT perform variable/command expansion.
/// Use `expand_segments_with_state()` for full expansion.
///
/// # Examples
///
/// ```no_run
/// use vsh::quotes::{parse_quotes, reconstruct_string};
///
/// let segments = parse_quotes("'hello'\" world\"").unwrap();
/// let result = reconstruct_string(&segments);
/// assert_eq!(result, "hello world");
/// ```
pub fn reconstruct_string(segments: &[QuotedSegment]) -> String {
segments
.iter()
.map(|seg| seg.content.as_str())
.collect::<Vec<_>>()
.join("")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_single_quotes_literal() {
let segments = parse_quotes("'$HOME'").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "$HOME");
assert_eq!(segments[0].state, QuoteState::SingleQuoted);
}
#[test]
fn test_double_quotes() {
let segments = parse_quotes("\"$HOME\"").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "$HOME");
assert_eq!(segments[0].state, QuoteState::DoubleQuoted);
}
#[test]
fn test_unquoted() {
let segments = parse_quotes("hello").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "hello");
assert_eq!(segments[0].state, QuoteState::Unquoted);
}
#[test]
fn test_adjacent_quotes() {
let segments = parse_quotes("'hello'\"world\"").unwrap();
assert_eq!(segments.len(), 2);
assert_eq!(segments[0].content, "hello");
assert_eq!(segments[0].state, QuoteState::SingleQuoted);
assert_eq!(segments[1].content, "world");
assert_eq!(segments[1].state, QuoteState::DoubleQuoted);
}
#[test]
fn test_mixed_quotes() {
let segments = parse_quotes("pre'quoted'post").unwrap();
assert_eq!(segments.len(), 3);
assert_eq!(segments[0].content, "pre");
assert_eq!(segments[0].state, QuoteState::Unquoted);
assert_eq!(segments[1].content, "quoted");
assert_eq!(segments[1].state, QuoteState::SingleQuoted);
assert_eq!(segments[2].content, "post");
assert_eq!(segments[2].state, QuoteState::Unquoted);
}
#[test]
fn test_backslash_escape_unquoted() {
let segments = parse_quotes("hello\\ world").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "hello world");
assert_eq!(segments[0].state, QuoteState::Unquoted);
}
#[test]
fn test_backslash_in_single_quotes() {
let segments = parse_quotes("'back\\slash'").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "back\\slash");
assert_eq!(segments[0].state, QuoteState::SingleQuoted);
}
#[test]
fn test_backslash_in_double_quotes() {
let segments = parse_quotes("\"\\$HOME\"").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "$HOME");
assert_eq!(segments[0].state, QuoteState::DoubleQuoted);
// Backslash before non-special character stays
let segments = parse_quotes("\"\\a\"").unwrap();
assert_eq!(segments[0].content, "\\a");
}
#[test]
fn test_empty_quotes() {
let segments = parse_quotes("''").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "");
assert_eq!(segments[0].state, QuoteState::SingleQuoted);
let segments = parse_quotes("\"\"").unwrap();
assert_eq!(segments.len(), 1);
assert_eq!(segments[0].content, "");
assert_eq!(segments[0].state, QuoteState::DoubleQuoted);
}
#[test]
fn test_unclosed_single_quote_error() {
let result = parse_quotes("'unclosed");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Unclosed quote"));
}
#[test]
fn test_unclosed_double_quote_error() {
let result = parse_quotes("\"unclosed");
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Unclosed quote"));
}
#[test]
fn test_glob_no_expansion_in_quotes() {
// Unquoted - should expand
let segments = parse_quotes("*.txt").unwrap();
assert!(should_expand_glob(&segments));
// Single quoted - no expansion
let segments = parse_quotes("'*.txt'").unwrap();
assert!(!should_expand_glob(&segments));
// Double quoted - no expansion
let segments = parse_quotes("\"*.txt\"").unwrap();
assert!(!should_expand_glob(&segments));
}
#[test]
fn test_reconstruct_string() {
let segments = parse_quotes("'hello'\" world\"").unwrap();
let result = reconstruct_string(&segments);
assert_eq!(result, "hello world");
let segments = parse_quotes("one'two'three").unwrap();
let result = reconstruct_string(&segments);
assert_eq!(result, "onetwothree");
}
#[test]
fn test_allows_variable_expansion() {
let segments = parse_quotes("'$VAR'").unwrap();
assert!(!segments[0].allows_variable_expansion());
let segments = parse_quotes("\"$VAR\"").unwrap();
assert!(segments[0].allows_variable_expansion());
let segments = parse_quotes("$VAR").unwrap();
assert!(segments[0].allows_variable_expansion());
}
#[test]
fn test_allows_glob_expansion() {
let segments = parse_quotes("'*.txt'").unwrap();
assert!(!segments[0].allows_glob_expansion());
let segments = parse_quotes("\"*.txt\"").unwrap();
assert!(!segments[0].allows_glob_expansion());
let segments = parse_quotes("*.txt").unwrap();
assert!(segments[0].allows_glob_expansion());
}
#[test]
fn test_line_continuation() {
// Backslash-newline in unquoted context
let segments = parse_quotes("hello\\\nworld").unwrap();
assert_eq!(segments[0].content, "helloworld");
// Backslash-newline in double quotes
let segments = parse_quotes("\"hello\\\nworld\"").unwrap();
assert_eq!(segments[0].content, "helloworld");
// Backslash-newline in single quotes (literal)
let segments = parse_quotes("'hello\\\nworld'").unwrap();
assert_eq!(segments[0].content, "hello\\\nworld");
}
#[test]
fn test_whitespace_preservation() {
let segments = parse_quotes("'hello world'").unwrap();
assert_eq!(segments[0].content, "hello world");
let segments = parse_quotes("\"hello world\"").unwrap();
assert_eq!(segments[0].content, "hello world");
}
}