Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
880 B

  1. ;# shellwords.pl
  2. ;#
  3. ;# Usage:
  4. ;# require 'shellwords.pl';
  5. ;# @words = &shellwords($line);
  6. ;# or
  7. ;# @words = &shellwords(@lines);
  8. ;# or
  9. ;# @words = &shellwords; # defaults to $_ (and clobbers it)
  10. sub shellwords {
  11. package shellwords;
  12. local($_) = join('', @_) if @_;
  13. local(@words,$snippet,$field);
  14. s/^\s+//;
  15. while ($_ ne '') {
  16. $field = '';
  17. for (;;) {
  18. if (s/^"(([^"\\]|\\.)*)"//) {
  19. ($snippet = $1) =~ s#\\(.)#$1#g;
  20. }
  21. elsif (/^"/) {
  22. die "Unmatched double quote: $_\n";
  23. }
  24. elsif (s/^'(([^'\\]|\\.)*)'//) {
  25. ($snippet = $1) =~ s#\\(.)#$1#g;
  26. }
  27. elsif (/^'/) {
  28. die "Unmatched single quote: $_\n";
  29. }
  30. elsif (s/^\\(.)//) {
  31. $snippet = $1;
  32. }
  33. elsif (s/^([^\s\\'"]+)//) {
  34. $snippet = $1;
  35. }
  36. else {
  37. s/^\s+//;
  38. last;
  39. }
  40. $field .= $snippet;
  41. }
  42. push(@words, $field);
  43. }
  44. @words;
  45. }
  46. 1;