| ... | ... | @@ -203,6 +203,8 @@ pub const ArgIteratorPosix = struct { |
| 203 | 203 | index: usize, |
| 204 | 204 | count: usize, |
| 205 | 205 | |
| 206 | pub const InitError = error{}; |
| 207 | |
| 206 | 208 | pub fn init() ArgIteratorPosix { |
| 207 | 209 | return ArgIteratorPosix{ |
| 208 | 210 | .index = 0, |
| ... | ... | @@ -299,195 +301,268 @@ pub const ArgIteratorWasi = struct { |
| 299 | 301 | } |
| 300 | 302 | }; |
| 301 | 303 | |
| 302 | | pub const ArgIteratorWindows = struct { |
| 303 | | index: usize, |
| 304 | | cmd_line: [*]const u16, |
| 305 | | |
| 306 | | pub const NextError = error{ OutOfMemory, InvalidCmdLine }; |
| 304 | /// Optional parameters for `ArgIteratorGeneral` |
| 305 | pub const ArgIteratorGeneralOptions = struct { |
| 306 | comments_supported: bool = false, |
| 307 | }; |
| 307 | 308 | |
| 308 | | pub fn init() ArgIteratorWindows { |
| 309 | | return initWithCmdLine(os.windows.kernel32.GetCommandLineW()); |
| 310 | | } |
| 309 | /// A general Iterator to parse a string into a set of arguments |
| 310 | pub fn ArgIteratorGeneral(comptime options: ArgIteratorGeneralOptions) type { |
| 311 | return struct { |
| 312 | allocator: Allocator, |
| 313 | index: usize = 0, |
| 314 | cmd_line: []const u8, |
| 315 | |
| 316 | /// Should the cmd_line field be free'd (using the allocator) on deinit()? |
| 317 | free_cmd_line_on_deinit: bool, |
| 318 | |
| 319 | /// buffer MUST be long enough to hold the cmd_line plus a null terminator. |
| 320 | /// buffer will we free'd (using the allocator) on deinit() |
| 321 | buffer: []u8, |
| 322 | start: usize = 0, |
| 323 | end: usize = 0, |
| 324 | |
| 325 | pub const Self = @This(); |
| 326 | |
| 327 | pub const InitError = error{OutOfMemory}; |
| 328 | pub const InitUtf16leError = error{ OutOfMemory, InvalidCmdLine }; |
| 329 | |
| 330 | /// cmd_line_utf8 MUST remain valid and constant while using this instance |
| 331 | pub fn init(allocator: Allocator, cmd_line_utf8: []const u8) InitError!Self { |
| 332 | var buffer = try allocator.alloc(u8, cmd_line_utf8.len + 1); |
| 333 | errdefer allocator.free(buffer); |
| 334 | |
| 335 | return Self{ |
| 336 | .allocator = allocator, |
| 337 | .cmd_line = cmd_line_utf8, |
| 338 | .free_cmd_line_on_deinit = false, |
| 339 | .buffer = buffer, |
| 340 | }; |
| 341 | } |
| 311 | 342 | |
| 312 | | pub fn initWithCmdLine(cmd_line: [*]const u16) ArgIteratorWindows { |
| 313 | | return ArgIteratorWindows{ |
| 314 | | .index = 0, |
| 315 | | .cmd_line = cmd_line, |
| 316 | | }; |
| 317 | | } |
| 343 | /// cmd_line_utf8 will be free'd (with the allocator) on deinit() |
| 344 | pub fn initTakeOwnership(allocator: Allocator, cmd_line_utf8: []const u8) InitError!Self { |
| 345 | var buffer = try allocator.alloc(u8, cmd_line_utf8.len + 1); |
| 346 | errdefer allocator.free(buffer); |
| 347 | |
| 348 | return Self{ |
| 349 | .allocator = allocator, |
| 350 | .cmd_line = cmd_line_utf8, |
| 351 | .free_cmd_line_on_deinit = true, |
| 352 | .buffer = buffer, |
| 353 | }; |
| 354 | } |
| 318 | 355 | |
| 319 | | fn getPointAtIndex(self: *ArgIteratorWindows) u16 { |
| 320 | | // According to |
| 321 | | // https://docs.microsoft.com/en-us/windows/win32/intl/using-byte-order-marks |
| 322 | | // Microsoft uses UTF16-LE. So we just read assuming it's little |
| 323 | | // endian. |
| 324 | | return std.mem.littleToNative(u16, self.cmd_line[self.index]); |
| 325 | | } |
| 356 | /// cmd_line_utf16le MUST be encoded UTF16-LE, and is converted to UTF-8 in an internal buffer |
| 357 | pub fn initUtf16le(allocator: Allocator, cmd_line_utf16le: [*:0]const u16) InitUtf16leError!Self { |
| 358 | var utf16le_slice = mem.sliceTo(cmd_line_utf16le, 0); |
| 359 | var cmd_line = std.unicode.utf16leToUtf8Alloc(allocator, utf16le_slice) catch |err| switch (err) { |
| 360 | error.ExpectedSecondSurrogateHalf, |
| 361 | error.DanglingSurrogateHalf, |
| 362 | error.UnexpectedSecondSurrogateHalf, |
| 363 | => return error.InvalidCmdLine, |
| 364 | |
| 365 | error.OutOfMemory => return error.OutOfMemory, |
| 366 | }; |
| 367 | errdefer allocator.free(cmd_line); |
| 368 | |
| 369 | var buffer = try allocator.alloc(u8, cmd_line.len + 1); |
| 370 | errdefer allocator.free(buffer); |
| 371 | |
| 372 | return Self{ |
| 373 | .allocator = allocator, |
| 374 | .cmd_line = cmd_line, |
| 375 | .free_cmd_line_on_deinit = true, |
| 376 | .buffer = buffer, |
| 377 | }; |
| 378 | } |
| 326 | 379 | |
| 327 | | /// You must free the returned memory when done. |
| 328 | | pub fn next(self: *ArgIteratorWindows, allocator: Allocator) NextError!?[:0]u8 { |
| 329 | | // march forward over whitespace |
| 330 | | while (true) : (self.index += 1) { |
| 331 | | const character = self.getPointAtIndex(); |
| 332 | | switch (character) { |
| 333 | | 0 => return null, |
| 334 | | ' ', '\t' => continue, |
| 335 | | else => break, |
| 380 | // Skips over whitespace in the cmd_line. |
| 381 | // Returns false if the terminating sentinel is reached, true otherwise. |
| 382 | // Also skips over comments (if supported). |
| 383 | fn skipWhitespace(self: *Self) bool { |
| 384 | while (true) : (self.index += 1) { |
| 385 | const character = if (self.index != self.cmd_line.len) self.cmd_line[self.index] else 0; |
| 386 | switch (character) { |
| 387 | 0 => return false, |
| 388 | ' ', '\t', '\r', '\n' => continue, |
| 389 | '#' => { |
| 390 | if (options.comments_supported) { |
| 391 | while (true) : (self.index += 1) { |
| 392 | switch (self.cmd_line[self.index]) { |
| 393 | '\n' => break, |
| 394 | 0 => return false, |
| 395 | else => continue, |
| 396 | } |
| 397 | } |
| 398 | continue; |
| 399 | } else { |
| 400 | break; |
| 401 | } |
| 402 | }, |
| 403 | else => break, |
| 404 | } |
| 336 | 405 | } |
| 406 | return true; |
| 337 | 407 | } |
| 338 | 408 | |
| 339 | | return try self.internalNext(allocator); |
| 340 | | } |
| 409 | pub fn skip(self: *Self) bool { |
| 410 | if (!self.skipWhitespace()) { |
| 411 | return false; |
| 412 | } |
| 341 | 413 | |
| 342 | | pub fn skip(self: *ArgIteratorWindows) bool { |
| 343 | | // march forward over whitespace |
| 344 | | while (true) : (self.index += 1) { |
| 345 | | const character = self.getPointAtIndex(); |
| 346 | | switch (character) { |
| 347 | | 0 => return false, |
| 348 | | ' ', '\t' => continue, |
| 349 | | else => break, |
| 414 | var backslash_count: usize = 0; |
| 415 | var in_quote = false; |
| 416 | while (true) : (self.index += 1) { |
| 417 | const character = if (self.index != self.cmd_line.len) self.cmd_line[self.index] else 0; |
| 418 | switch (character) { |
| 419 | 0 => return true, |
| 420 | '"' => { |
| 421 | const quote_is_real = backslash_count % 2 == 0; |
| 422 | if (quote_is_real) { |
| 423 | in_quote = !in_quote; |
| 424 | } |
| 425 | }, |
| 426 | '\\' => { |
| 427 | backslash_count += 1; |
| 428 | }, |
| 429 | ' ', '\t', '\r', '\n' => { |
| 430 | if (!in_quote) { |
| 431 | return true; |
| 432 | } |
| 433 | backslash_count = 0; |
| 434 | }, |
| 435 | else => { |
| 436 | backslash_count = 0; |
| 437 | continue; |
| 438 | }, |
| 439 | } |
| 350 | 440 | } |
| 351 | 441 | } |
| 352 | 442 | |
| 353 | | var backslash_count: usize = 0; |
| 354 | | var in_quote = false; |
| 355 | | while (true) : (self.index += 1) { |
| 356 | | const character = self.getPointAtIndex(); |
| 357 | | switch (character) { |
| 358 | | 0 => return true, |
| 359 | | '"' => { |
| 360 | | const quote_is_real = backslash_count % 2 == 0; |
| 361 | | if (quote_is_real) { |
| 362 | | in_quote = !in_quote; |
| 363 | | } |
| 364 | | }, |
| 365 | | '\\' => { |
| 366 | | backslash_count += 1; |
| 367 | | }, |
| 368 | | ' ', '\t' => { |
| 369 | | if (!in_quote) { |
| 370 | | return true; |
| 371 | | } |
| 372 | | backslash_count = 0; |
| 373 | | }, |
| 374 | | else => { |
| 375 | | backslash_count = 0; |
| 376 | | continue; |
| 377 | | }, |
| 443 | /// Returns a slice of the internal buffer that contains the next argument. |
| 444 | /// Returns null when it reaches the end. |
| 445 | pub fn next(self: *Self) ?[:0]const u8 { |
| 446 | if (!self.skipWhitespace()) { |
| 447 | return null; |
| 448 | } |
| 449 | |
| 450 | var backslash_count: usize = 0; |
| 451 | var in_quote = false; |
| 452 | while (true) : (self.index += 1) { |
| 453 | const character = if (self.index != self.cmd_line.len) self.cmd_line[self.index] else 0; |
| 454 | switch (character) { |
| 455 | 0 => { |
| 456 | self.emitBackslashes(backslash_count); |
| 457 | self.buffer[self.end] = 0; |
| 458 | var token = self.buffer[self.start..self.end :0]; |
| 459 | self.end += 1; |
| 460 | self.start = self.end; |
| 461 | return token; |
| 462 | }, |
| 463 | '"' => { |
| 464 | const quote_is_real = backslash_count % 2 == 0; |
| 465 | self.emitBackslashes(backslash_count / 2); |
| 466 | backslash_count = 0; |
| 467 | |
| 468 | if (quote_is_real) { |
| 469 | in_quote = !in_quote; |
| 470 | } else { |
| 471 | self.emitCharacter('"'); |
| 472 | } |
| 473 | }, |
| 474 | '\\' => { |
| 475 | backslash_count += 1; |
| 476 | }, |
| 477 | ' ', '\t', '\r', '\n' => { |
| 478 | self.emitBackslashes(backslash_count); |
| 479 | backslash_count = 0; |
| 480 | if (in_quote) { |
| 481 | self.emitCharacter(character); |
| 482 | } else { |
| 483 | self.buffer[self.end] = 0; |
| 484 | var token = self.buffer[self.start..self.end :0]; |
| 485 | self.end += 1; |
| 486 | self.start = self.end; |
| 487 | return token; |
| 488 | } |
| 489 | }, |
| 490 | else => { |
| 491 | self.emitBackslashes(backslash_count); |
| 492 | backslash_count = 0; |
| 493 | self.emitCharacter(character); |
| 494 | }, |
| 495 | } |
| 378 | 496 | } |
| 379 | 497 | } |
| 380 | | } |
| 381 | 498 | |
| 382 | | fn internalNext(self: *ArgIteratorWindows, allocator: Allocator) NextError![:0]u8 { |
| 383 | | var buf = std.ArrayList(u16).init(allocator); |
| 384 | | defer buf.deinit(); |
| 385 | | |
| 386 | | var backslash_count: usize = 0; |
| 387 | | var in_quote = false; |
| 388 | | while (true) : (self.index += 1) { |
| 389 | | const character = self.getPointAtIndex(); |
| 390 | | switch (character) { |
| 391 | | 0 => { |
| 392 | | return convertFromWindowsCmdLineToUTF8(allocator, buf.items); |
| 393 | | }, |
| 394 | | '"' => { |
| 395 | | const quote_is_real = backslash_count % 2 == 0; |
| 396 | | try emitBackslashes(&buf, backslash_count / 2); |
| 397 | | backslash_count = 0; |
| 398 | | |
| 399 | | if (quote_is_real) { |
| 400 | | in_quote = !in_quote; |
| 401 | | } else { |
| 402 | | try buf.append(std.mem.nativeToLittle(u16, '"')); |
| 403 | | } |
| 404 | | }, |
| 405 | | '\\' => { |
| 406 | | backslash_count += 1; |
| 407 | | }, |
| 408 | | ' ', '\t' => { |
| 409 | | try emitBackslashes(&buf, backslash_count); |
| 410 | | backslash_count = 0; |
| 411 | | if (in_quote) { |
| 412 | | try buf.append(std.mem.nativeToLittle(u16, character)); |
| 413 | | } else { |
| 414 | | return convertFromWindowsCmdLineToUTF8(allocator, buf.items); |
| 415 | | } |
| 416 | | }, |
| 417 | | else => { |
| 418 | | try emitBackslashes(&buf, backslash_count); |
| 419 | | backslash_count = 0; |
| 420 | | try buf.append(std.mem.nativeToLittle(u16, character)); |
| 421 | | }, |
| 499 | fn emitBackslashes(self: *Self, emit_count: usize) void { |
| 500 | var i: usize = 0; |
| 501 | while (i < emit_count) : (i += 1) { |
| 502 | self.emitCharacter('\\'); |
| 422 | 503 | } |
| 423 | 504 | } |
| 424 | | } |
| 425 | 505 | |
| 426 | | fn convertFromWindowsCmdLineToUTF8(allocator: Allocator, buf: []u16) NextError![:0]u8 { |
| 427 | | return std.unicode.utf16leToUtf8AllocZ(allocator, buf) catch |err| switch (err) { |
| 428 | | error.ExpectedSecondSurrogateHalf, |
| 429 | | error.DanglingSurrogateHalf, |
| 430 | | error.UnexpectedSecondSurrogateHalf, |
| 431 | | => return error.InvalidCmdLine, |
| 506 | fn emitCharacter(self: *Self, char: u8) void { |
| 507 | self.buffer[self.end] = char; |
| 508 | self.end += 1; |
| 509 | } |
| 432 | 510 | |
| 433 | | error.OutOfMemory => return error.OutOfMemory, |
| 434 | | }; |
| 435 | | } |
| 436 | | fn emitBackslashes(buf: *std.ArrayList(u16), emit_count: usize) !void { |
| 437 | | var i: usize = 0; |
| 438 | | while (i < emit_count) : (i += 1) { |
| 439 | | try buf.append(std.mem.nativeToLittle(u16, '\\')); |
| 511 | /// Call to free the internal buffer of the iterator. |
| 512 | pub fn deinit(self: *Self) void { |
| 513 | self.allocator.free(self.buffer); |
| 514 | |
| 515 | if (self.free_cmd_line_on_deinit) { |
| 516 | self.allocator.free(self.cmd_line); |
| 517 | } |
| 440 | 518 | } |
| 441 | | } |
| 442 | | }; |
| 519 | }; |
| 520 | } |
| 443 | 521 | |
| 522 | /// Cross-platform command line argument iterator. |
| 444 | 523 | pub const ArgIterator = struct { |
| 445 | 524 | const InnerType = switch (builtin.os.tag) { |
| 446 | | .windows => ArgIteratorWindows, |
| 525 | .windows => ArgIteratorGeneral(.{ .comments_supported = false }), |
| 447 | 526 | .wasi => if (builtin.link_libc) ArgIteratorPosix else ArgIteratorWasi, |
| 448 | 527 | else => ArgIteratorPosix, |
| 449 | 528 | }; |
| 450 | 529 | |
| 451 | 530 | inner: InnerType, |
| 452 | 531 | |
| 453 | | /// Initialize the args iterator. |
| 532 | /// Initialize the args iterator. Consider using initWithAllocator() instead |
| 533 | /// for cross-platform compatibility. |
| 454 | 534 | pub fn init() ArgIterator { |
| 455 | 535 | if (builtin.os.tag == .wasi) { |
| 456 | 536 | @compileError("In WASI, use initWithAllocator instead."); |
| 457 | 537 | } |
| 538 | if (builtin.os.tag == .windows) { |
| 539 | @compileError("In Windows, use initWithAllocator instead."); |
| 540 | } |
| 458 | 541 | |
| 459 | 542 | return ArgIterator{ .inner = InnerType.init() }; |
| 460 | 543 | } |
| 461 | 544 | |
| 462 | | pub const InitError = ArgIteratorWasi.InitError; |
| 545 | pub const InitError = switch (builtin.os.tag) { |
| 546 | .windows => InnerType.InitUtf16leError, |
| 547 | else => InnerType.InitError, |
| 548 | }; |
| 463 | 549 | |
| 464 | 550 | /// You must deinitialize iterator's internal buffers by calling `deinit` when done. |
| 465 | 551 | pub fn initWithAllocator(allocator: mem.Allocator) InitError!ArgIterator { |
| 466 | 552 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 467 | 553 | return ArgIterator{ .inner = try InnerType.init(allocator) }; |
| 468 | 554 | } |
| 469 | | |
| 470 | | return ArgIterator{ .inner = InnerType.init() }; |
| 471 | | } |
| 472 | | |
| 473 | | pub const NextError = ArgIteratorWindows.NextError; |
| 474 | | |
| 475 | | /// You must free the returned memory when done. |
| 476 | | pub fn next(self: *ArgIterator, allocator: Allocator) NextError!?[:0]u8 { |
| 477 | 555 | if (builtin.os.tag == .windows) { |
| 478 | | return self.inner.next(allocator); |
| 479 | | } else { |
| 480 | | return try allocator.dupeZ(u8, self.inner.next() orelse return null); |
| 556 | const cmd_line_w = os.windows.kernel32.GetCommandLineW(); |
| 557 | return ArgIterator{ .inner = try InnerType.initUtf16le(allocator, cmd_line_w) }; |
| 481 | 558 | } |
| 482 | | } |
| 483 | 559 | |
| 484 | | /// If you only are targeting posix you can call this and not need an allocator. |
| 485 | | pub fn nextPosix(self: *ArgIterator) ?[:0]const u8 { |
| 486 | | return self.inner.next(); |
| 560 | return ArgIterator{ .inner = InnerType.init() }; |
| 487 | 561 | } |
| 488 | 562 | |
| 489 | | /// If you only are targeting WASI, you can call this and not need an allocator. |
| 490 | | pub fn nextWasi(self: *ArgIterator) ?[:0]const u8 { |
| 563 | /// Get the next argument. Returns 'null' if we are at the end. |
| 564 | /// Returned slice is pointing to the iterator's internal buffer. |
| 565 | pub fn next(self: *ArgIterator) ?([:0]const u8) { |
| 491 | 566 | return self.inner.next(); |
| 492 | 567 | } |
| 493 | 568 | |
| ... | ... | @@ -500,13 +575,18 @@ pub const ArgIterator = struct { |
| 500 | 575 | /// Call this to free the iterator's internal buffer if the iterator |
| 501 | 576 | /// was created with `initWithAllocator` function. |
| 502 | 577 | pub fn deinit(self: *ArgIterator) void { |
| 503 | | // Unless we're targeting WASI, this is a no-op. |
| 578 | // Unless we're targeting WASI or Windows, this is a no-op. |
| 504 | 579 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 505 | 580 | self.inner.deinit(); |
| 506 | 581 | } |
| 582 | |
| 583 | if (builtin.os.tag == .windows) { |
| 584 | self.inner.deinit(); |
| 585 | } |
| 507 | 586 | } |
| 508 | 587 | }; |
| 509 | 588 | |
| 589 | /// Use argsWithAllocator() for cross-platform code |
| 510 | 590 | pub fn args() ArgIterator { |
| 511 | 591 | return ArgIterator.init(); |
| 512 | 592 | } |
| ... | ... | @@ -518,12 +598,10 @@ pub fn argsWithAllocator(allocator: mem.Allocator) ArgIterator.InitError!ArgIter |
| 518 | 598 | |
| 519 | 599 | test "args iterator" { |
| 520 | 600 | var ga = std.testing.allocator; |
| 521 | | var it = if (builtin.os.tag == .wasi) try argsWithAllocator(ga) else args(); |
| 522 | | defer it.deinit(); // no-op unless WASI |
| 523 | | |
| 524 | | const prog_name = (try it.next(ga)) orelse unreachable; |
| 525 | | defer ga.free(prog_name); |
| 601 | var it = try argsWithAllocator(ga); |
| 602 | defer it.deinit(); // no-op unless WASI or Windows |
| 526 | 603 | |
| 604 | const prog_name = it.next() orelse unreachable; |
| 527 | 605 | const expected_suffix = switch (builtin.os.tag) { |
| 528 | 606 | .wasi => "test.wasm", |
| 529 | 607 | .windows => "test.exe", |
| ... | ... | @@ -533,14 +611,14 @@ test "args iterator" { |
| 533 | 611 | |
| 534 | 612 | try testing.expect(mem.eql(u8, expected_suffix, given_suffix)); |
| 535 | 613 | try testing.expect(it.skip()); // Skip over zig_exe_path, passed to the test runner |
| 536 | | try testing.expect((try it.next(ga)) == null); |
| 614 | try testing.expect(it.next() == null); |
| 537 | 615 | try testing.expect(!it.skip()); |
| 538 | 616 | } |
| 539 | 617 | |
| 540 | 618 | /// Caller must call argsFree on result. |
| 541 | 619 | pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 { |
| 542 | 620 | // TODO refactor to only make 1 allocation. |
| 543 | | var it = if (builtin.os.tag == .wasi) try argsWithAllocator(allocator) else args(); |
| 621 | var it = try argsWithAllocator(allocator); |
| 544 | 622 | defer it.deinit(); |
| 545 | 623 | |
| 546 | 624 | var contents = std.ArrayList(u8).init(allocator); |
| ... | ... | @@ -549,8 +627,7 @@ pub fn argsAlloc(allocator: mem.Allocator) ![][:0]u8 { |
| 549 | 627 | var slice_list = std.ArrayList(usize).init(allocator); |
| 550 | 628 | defer slice_list.deinit(); |
| 551 | 629 | |
| 552 | | while (try it.next(allocator)) |arg| { |
| 553 | | defer allocator.free(arg); |
| 630 | while (it.next()) |arg| { |
| 554 | 631 | try contents.appendSlice(arg[0 .. arg.len + 1]); |
| 555 | 632 | try slice_list.append(arg.len); |
| 556 | 633 | } |
| ... | ... | @@ -586,16 +663,17 @@ pub fn argsFree(allocator: mem.Allocator, args_alloc: []const [:0]u8) void { |
| 586 | 663 | return allocator.free(aligned_allocated_buf); |
| 587 | 664 | } |
| 588 | 665 | |
| 589 | | test "windows arg parsing" { |
| 590 | | const utf16Literal = std.unicode.utf8ToUtf16LeStringLiteral; |
| 591 | | try testWindowsCmdLine(utf16Literal("a b\tc d"), &[_][]const u8{ "a", "b", "c", "d" }); |
| 592 | | try testWindowsCmdLine(utf16Literal("\"abc\" d e"), &[_][]const u8{ "abc", "d", "e" }); |
| 593 | | try testWindowsCmdLine(utf16Literal("a\\\\\\b d\"e f\"g h"), &[_][]const u8{ "a\\\\\\b", "de fg", "h" }); |
| 594 | | try testWindowsCmdLine(utf16Literal("a\\\\\\\"b c d"), &[_][]const u8{ "a\\\"b", "c", "d" }); |
| 595 | | try testWindowsCmdLine(utf16Literal("a\\\\\\\\\"b c\" d e"), &[_][]const u8{ "a\\\\b c", "d", "e" }); |
| 596 | | try testWindowsCmdLine(utf16Literal("a b\tc \"d f"), &[_][]const u8{ "a", "b", "c", "d f" }); |
| 597 | | |
| 598 | | try testWindowsCmdLine(utf16Literal("\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\""), &[_][]const u8{ |
| 666 | test "general arg parsing" { |
| 667 | try testGeneralCmdLine("a b\tc d", &[_][]const u8{ "a", "b", "c", "d" }); |
| 668 | try testGeneralCmdLine("\"abc\" d e", &[_][]const u8{ "abc", "d", "e" }); |
| 669 | try testGeneralCmdLine("a\\\\\\b d\"e f\"g h", &[_][]const u8{ "a\\\\\\b", "de fg", "h" }); |
| 670 | try testGeneralCmdLine("a\\\\\\\"b c d", &[_][]const u8{ "a\\\"b", "c", "d" }); |
| 671 | try testGeneralCmdLine("a\\\\\\\\\"b c\" d e", &[_][]const u8{ "a\\\\b c", "d", "e" }); |
| 672 | try testGeneralCmdLine("a b\tc \"d f", &[_][]const u8{ "a", "b", "c", "d f" }); |
| 673 | try testGeneralCmdLine("j k l\\", &[_][]const u8{ "j", "k", "l\\" }); |
| 674 | try testGeneralCmdLine("\"\" x y z\\\\", &[_][]const u8{ "", "x", "y", "z\\\\" }); |
| 675 | |
| 676 | try testGeneralCmdLine("\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", &[_][]const u8{ |
| 599 | 677 | ".\\..\\zig-cache\\build", |
| 600 | 678 | "bin\\zig.exe", |
| 601 | 679 | ".\\..", |
| ... | ... | @@ -604,14 +682,52 @@ test "windows arg parsing" { |
| 604 | 682 | }); |
| 605 | 683 | } |
| 606 | 684 | |
| 607 | | fn testWindowsCmdLine(input_cmd_line: [*]const u16, expected_args: []const []const u8) !void { |
| 608 | | var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line); |
| 685 | fn testGeneralCmdLine(input_cmd_line: []const u8, expected_args: []const []const u8) !void { |
| 686 | var it = try ArgIteratorGeneral(.{ .comments_supported = false }) |
| 687 | .init(std.testing.allocator, input_cmd_line); |
| 688 | defer it.deinit(); |
| 689 | for (expected_args) |expected_arg| { |
| 690 | const arg = it.next().?; |
| 691 | try testing.expectEqualStrings(expected_arg, arg); |
| 692 | } |
| 693 | try testing.expect(it.next() == null); |
| 694 | } |
| 695 | |
| 696 | test "response file arg parsing" { |
| 697 | try testResponseFileCmdLine( |
| 698 | \\a b |
| 699 | \\c d\ |
| 700 | , &[_][]const u8{ "a", "b", "c", "d\\" }); |
| 701 | try testResponseFileCmdLine("a b c d\\", &[_][]const u8{ "a", "b", "c", "d\\" }); |
| 702 | |
| 703 | try testResponseFileCmdLine( |
| 704 | \\j |
| 705 | \\ k l # this is a comment \\ \\\ \\\\ "none" "\\" "\\\" |
| 706 | \\ "m" #another comment |
| 707 | \\ |
| 708 | , &[_][]const u8{ "j", "k", "l", "m" }); |
| 709 | |
| 710 | try testResponseFileCmdLine( |
| 711 | \\ "" q "" |
| 712 | \\ "r s # t" "u\" v" #another comment |
| 713 | \\ |
| 714 | , &[_][]const u8{ "", "q", "", "r s # t", "u\" v" }); |
| 715 | |
| 716 | try testResponseFileCmdLine( |
| 717 | \\ -l"advapi32" a# b#c d# |
| 718 | \\e\\\ |
| 719 | , &[_][]const u8{ "-ladvapi32", "a#", "b#c", "d#", "e\\\\\\" }); |
| 720 | } |
| 721 | |
| 722 | fn testResponseFileCmdLine(input_cmd_line: []const u8, expected_args: []const []const u8) !void { |
| 723 | var it = try ArgIteratorGeneral(.{ .comments_supported = true }) |
| 724 | .init(std.testing.allocator, input_cmd_line); |
| 725 | defer it.deinit(); |
| 609 | 726 | for (expected_args) |expected_arg| { |
| 610 | | const arg = (it.next(std.testing.allocator) catch unreachable).?; |
| 611 | | defer std.testing.allocator.free(arg); |
| 727 | const arg = it.next().?; |
| 612 | 728 | try testing.expectEqualStrings(expected_arg, arg); |
| 613 | 729 | } |
| 614 | | try testing.expect((try it.next(std.testing.allocator)) == null); |
| 730 | try testing.expect(it.next() == null); |
| 615 | 731 | } |
| 616 | 732 | |
| 617 | 733 | pub const UserInfo = struct { |