Leaked source code of windows server 2003
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.

262 lines
6.6 KiB

  1. package Text::ParseWords;
  2. use vars qw($VERSION @ISA @EXPORT $PERL_SINGLE_QUOTE);
  3. $VERSION = "3.2";
  4. require 5.000;
  5. use Exporter;
  6. @ISA = qw(Exporter);
  7. @EXPORT = qw(shellwords quotewords nested_quotewords parse_line);
  8. @EXPORT_OK = qw(old_shellwords);
  9. sub shellwords {
  10. local(@lines) = @_;
  11. $lines[$#lines] =~ s/\s+$//;
  12. return(quotewords('\s+', 0, @lines));
  13. }
  14. sub quotewords {
  15. my($delim, $keep, @lines) = @_;
  16. my($line, @words, @allwords);
  17. foreach $line (@lines) {
  18. @words = parse_line($delim, $keep, $line);
  19. return() unless (@words || !length($line));
  20. push(@allwords, @words);
  21. }
  22. return(@allwords);
  23. }
  24. sub nested_quotewords {
  25. my($delim, $keep, @lines) = @_;
  26. my($i, @allwords);
  27. for ($i = 0; $i < @lines; $i++) {
  28. @{$allwords[$i]} = parse_line($delim, $keep, $lines[$i]);
  29. return() unless (@{$allwords[$i]} || !length($lines[$i]));
  30. }
  31. return(@allwords);
  32. }
  33. sub parse_line {
  34. # We will be testing undef strings
  35. no warnings;
  36. my($delimiter, $keep, $line) = @_;
  37. my($quote, $quoted, $unquoted, $delim, $word, @pieces);
  38. while (length($line)) {
  39. ($quote, $quoted, undef, $unquoted, $delim, undef) =
  40. $line =~ m/^(["']) # a $quote
  41. ((?:\\.|(?!\1)[^\\])*) # and $quoted text
  42. \1 # followed by the same quote
  43. ([\000-\377]*) # and the rest
  44. | # --OR--
  45. ^((?:\\.|[^\\"'])*?) # an $unquoted text
  46. (\Z(?!\n)|(?-x:$delimiter)|(?!^)(?=["']))
  47. # plus EOL, delimiter, or quote
  48. ([\000-\377]*) # the rest
  49. /x; # extended layout
  50. return() unless( $quote || length($unquoted) || length($delim));
  51. $line = $+;
  52. if ($keep) {
  53. $quoted = "$quote$quoted$quote";
  54. }
  55. else {
  56. $unquoted =~ s/\\(.)/$1/g;
  57. if (defined $quote) {
  58. $quoted =~ s/\\(.)/$1/g if ($quote eq '"');
  59. $quoted =~ s/\\([\\'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'");
  60. }
  61. }
  62. $word .= defined $quote ? $quoted : $unquoted;
  63. if (length($delim)) {
  64. push(@pieces, $word);
  65. push(@pieces, $delim) if ($keep eq 'delimiters');
  66. undef $word;
  67. }
  68. if (!length($line)) {
  69. push(@pieces, $word);
  70. }
  71. }
  72. return(@pieces);
  73. }
  74. sub old_shellwords {
  75. # Usage:
  76. # use ParseWords;
  77. # @words = old_shellwords($line);
  78. # or
  79. # @words = old_shellwords(@lines);
  80. local($_) = join('', @_);
  81. my(@words,$snippet,$field);
  82. s/^\s+//;
  83. while ($_ ne '') {
  84. $field = '';
  85. for (;;) {
  86. if (s/^"(([^"\\]|\\.)*)"//) {
  87. ($snippet = $1) =~ s#\\(.)#$1#g;
  88. }
  89. elsif (/^"/) {
  90. return();
  91. }
  92. elsif (s/^'(([^'\\]|\\.)*)'//) {
  93. ($snippet = $1) =~ s#\\(.)#$1#g;
  94. }
  95. elsif (/^'/) {
  96. return();
  97. }
  98. elsif (s/^\\(.)//) {
  99. $snippet = $1;
  100. }
  101. elsif (s/^([^\s\\'"]+)//) {
  102. $snippet = $1;
  103. }
  104. else {
  105. s/^\s+//;
  106. last;
  107. }
  108. $field .= $snippet;
  109. }
  110. push(@words, $field);
  111. }
  112. @words;
  113. }
  114. 1;
  115. __END__
  116. =head1 NAME
  117. Text::ParseWords - parse text into an array of tokens or array of arrays
  118. =head1 SYNOPSIS
  119. use Text::ParseWords;
  120. @lists = &nested_quotewords($delim, $keep, @lines);
  121. @words = &quotewords($delim, $keep, @lines);
  122. @words = &shellwords(@lines);
  123. @words = &parse_line($delim, $keep, $line);
  124. @words = &old_shellwords(@lines); # DEPRECATED!
  125. =head1 DESCRIPTION
  126. The &nested_quotewords() and &quotewords() functions accept a delimiter
  127. (which can be a regular expression)
  128. and a list of lines and then breaks those lines up into a list of
  129. words ignoring delimiters that appear inside quotes. &quotewords()
  130. returns all of the tokens in a single long list, while &nested_quotewords()
  131. returns a list of token lists corresponding to the elements of @lines.
  132. &parse_line() does tokenizing on a single string. The &*quotewords()
  133. functions simply call &parse_lines(), so if you're only splitting
  134. one line you can call &parse_lines() directly and save a function
  135. call.
  136. The $keep argument is a boolean flag. If true, then the tokens are
  137. split on the specified delimiter, but all other characters (quotes,
  138. backslashes, etc.) are kept in the tokens. If $keep is false then the
  139. &*quotewords() functions remove all quotes and backslashes that are
  140. not themselves backslash-escaped or inside of single quotes (i.e.,
  141. &quotewords() tries to interpret these characters just like the Bourne
  142. shell). NB: these semantics are significantly different from the
  143. original version of this module shipped with Perl 5.000 through 5.004.
  144. As an additional feature, $keep may be the keyword "delimiters" which
  145. causes the functions to preserve the delimiters in each string as
  146. tokens in the token lists, in addition to preserving quote and
  147. backslash characters.
  148. &shellwords() is written as a special case of &quotewords(), and it
  149. does token parsing with whitespace as a delimiter-- similar to most
  150. Unix shells.
  151. =head1 EXAMPLES
  152. The sample program:
  153. use Text::ParseWords;
  154. @words = &quotewords('\s+', 0, q{this is "a test" of\ quotewords \"for you});
  155. $i = 0;
  156. foreach (@words) {
  157. print "$i: <$_>\n";
  158. $i++;
  159. }
  160. produces:
  161. 0: <this>
  162. 1: <is>
  163. 2: <a test>
  164. 3: <of quotewords>
  165. 4: <"for>
  166. 5: <you>
  167. demonstrating:
  168. =over 4
  169. =item 0
  170. a simple word
  171. =item 1
  172. multiple spaces are skipped because of our $delim
  173. =item 2
  174. use of quotes to include a space in a word
  175. =item 3
  176. use of a backslash to include a space in a word
  177. =item 4
  178. use of a backslash to remove the special meaning of a double-quote
  179. =item 5
  180. another simple word (note the lack of effect of the
  181. backslashed double-quote)
  182. =back
  183. Replacing C<&quotewords('\s+', 0, q{this is...})>
  184. with C<&shellwords(q{this is...})>
  185. is a simpler way to accomplish the same thing.
  186. =head1 AUTHORS
  187. Maintainer is Hal Pomeranz <[email protected]>, 1994-1997 (Original
  188. author unknown). Much of the code for &parse_line() (including the
  189. primary regexp) from Joerk Behrends <[email protected]>.
  190. Examples section another documentation provided by John Heidemann
  191. <[email protected]>
  192. Bug reports, patches, and nagging provided by lots of folks-- thanks
  193. everybody! Special thanks to Michael Schwern <[email protected]>
  194. for assuring me that a &nested_quotewords() would be useful, and to
  195. Jeff Friedl <[email protected]> for telling me not to worry about
  196. error-checking (sort of-- you had to be there).
  197. =cut