source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/1.8/shellwords.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

File size: 1.3 KB
Line 
1#
2# shellwords.rb: Split text into an array of tokens a la UNIX shell
3#
4
5#
6# This module is originally a port of shellwords.pl, but modified to
7# conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
8#
9# Examples:
10#
11# require 'shellwords'
12# words = Shellwords.shellwords(line)
13#
14# or
15#
16# require 'shellwords'
17# include Shellwords
18# words = shellwords(line)
19#
20module Shellwords
21
22 #
23 # Split text into an array of tokens in the same way the UNIX Bourne
24 # shell does.
25 #
26 # See the +Shellwords+ module documentation for an example.
27 #
28 def shellwords(line)
29 line = String.new(line) rescue
30 raise(ArgumentError, "Argument must be a string")
31 line.lstrip!
32 words = []
33 until line.empty?
34 field = ''
35 loop do
36 if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then
37 snippet = $1.gsub(/\\(.)/, '\1')
38 elsif line =~ /\A"/ then
39 raise ArgumentError, "Unmatched double quote: #{line}"
40 elsif line.sub!(/\A'([^']*)'/, '') then
41 snippet = $1
42 elsif line =~ /\A'/ then
43 raise ArgumentError, "Unmatched single quote: #{line}"
44 elsif line.sub!(/\A\\(.)?/, '') then
45 snippet = $1 || '\\'
46 elsif line.sub!(/\A([^\s\\'"]+)/, '') then
47 snippet = $1
48 else
49 line.lstrip!
50 break
51 end
52 field.concat(snippet)
53 end
54 words.push(field)
55 end
56 words
57 end
58
59 module_function :shellwords
60end
Note: See TracBrowser for help on using the repository browser.