1 | #!/research/code/gsdl-video-import/trunk/installed/cmdline/bin/ruby
|
---|
2 | # Tiny eRuby --- ERB2
|
---|
3 | # Copyright (c) 1999-2000,2002 Masatoshi SEKI
|
---|
4 | # You can redistribute it and/or modify it under the same terms as Ruby.
|
---|
5 |
|
---|
6 | require 'erb'
|
---|
7 |
|
---|
8 | class ERB
|
---|
9 | module Main
|
---|
10 | def ARGV.switch
|
---|
11 | return nil if self.empty?
|
---|
12 | arg = self.shift
|
---|
13 | return nil if arg == '--'
|
---|
14 | if arg =~ /^-(.)(.*)/
|
---|
15 | return arg if $1 == '-'
|
---|
16 | raise 'unknown switch "-"' if $2.index('-')
|
---|
17 | self.unshift "-#{$2}" if $2.size > 0
|
---|
18 | "-#{$1}"
|
---|
19 | else
|
---|
20 | self.unshift arg
|
---|
21 | nil
|
---|
22 | end
|
---|
23 | end
|
---|
24 |
|
---|
25 | def ARGV.req_arg
|
---|
26 | self.shift || raise('missing argument')
|
---|
27 | end
|
---|
28 |
|
---|
29 | def trim_mode_opt(trim_mode, disable_percent)
|
---|
30 | return trim_mode if disable_percent
|
---|
31 | case trim_mode
|
---|
32 | when 0
|
---|
33 | return '%'
|
---|
34 | when 1
|
---|
35 | return '%>'
|
---|
36 | when 2
|
---|
37 | return '%<>'
|
---|
38 | when '-'
|
---|
39 | return '%-'
|
---|
40 | end
|
---|
41 | end
|
---|
42 | module_function :trim_mode_opt
|
---|
43 |
|
---|
44 | def run(factory=ERB)
|
---|
45 | trim_mode = 0
|
---|
46 | disable_percent = false
|
---|
47 | begin
|
---|
48 | while switch = ARGV.switch
|
---|
49 | case switch
|
---|
50 | when '-x' # ruby source
|
---|
51 | output = true
|
---|
52 | when '-n' # line number
|
---|
53 | number = true
|
---|
54 | when '-v' # verbose
|
---|
55 | $VERBOSE = true
|
---|
56 | when '--version' # version
|
---|
57 | STDERR.puts factory.version
|
---|
58 | exit
|
---|
59 | when '-d', '--debug' # debug
|
---|
60 | $DEBUG = true
|
---|
61 | when '-r' # require
|
---|
62 | require ARGV.req_arg
|
---|
63 | when '-S' # security level
|
---|
64 | arg = ARGV.req_arg
|
---|
65 | raise "invalid safe_level #{arg.dump}" unless arg =~ /^[0-4]$/
|
---|
66 | safe_level = arg.to_i
|
---|
67 | when '-T' # trim mode
|
---|
68 | arg = ARGV.req_arg
|
---|
69 | if arg == '-'
|
---|
70 | trim_mode = arg
|
---|
71 | next
|
---|
72 | end
|
---|
73 | raise "invalid trim mode #{arg.dump}" unless arg =~ /^[0-2]$/
|
---|
74 | trim_mode = arg.to_i
|
---|
75 | when '-K' # KCODE
|
---|
76 | arg = ARGV.req_arg
|
---|
77 | case arg.downcase
|
---|
78 | when 'e', '-e', 'euc'
|
---|
79 | $KCODE = 'EUC'
|
---|
80 | when 's', '-s', 'sjis'
|
---|
81 | $KCODE = 'SJIS'
|
---|
82 | when 'u', '-u', 'utf8'
|
---|
83 | $KCODE = 'UTF8'
|
---|
84 | when 'n', '-n', 'none'
|
---|
85 | $KCODE = 'NONE'
|
---|
86 | else
|
---|
87 | raise "invalid KCODE #{arg.dump}"
|
---|
88 | end
|
---|
89 | when '-P'
|
---|
90 | disable_percent = true
|
---|
91 | when '--help'
|
---|
92 | raise "print this help"
|
---|
93 | else
|
---|
94 | raise "unknown switch #{switch.dump}"
|
---|
95 | end
|
---|
96 | end
|
---|
97 | rescue # usage
|
---|
98 | STDERR.puts $!.to_s
|
---|
99 | STDERR.puts File.basename($0) +
|
---|
100 | " [switches] [inputfile]"
|
---|
101 | STDERR.puts <<EOU
|
---|
102 | -x print ruby script
|
---|
103 | -n print ruby script with line number
|
---|
104 | -v enable verbose mode
|
---|
105 | -d set $DEBUG to true
|
---|
106 | -r [library] load a library
|
---|
107 | -K [kcode] specify KANJI code-set
|
---|
108 | -S [safe_level] set $SAFE (0..4)
|
---|
109 | -T [trim_mode] specify trim_mode (0..2, -)
|
---|
110 | -P ignore lines which start with "%"
|
---|
111 | EOU
|
---|
112 | exit 1
|
---|
113 | end
|
---|
114 |
|
---|
115 | src = $<.read
|
---|
116 | filename = $FILENAME
|
---|
117 | exit 2 unless src
|
---|
118 | trim = trim_mode_opt(trim_mode, disable_percent)
|
---|
119 | erb = factory.new(src.untaint, safe_level, trim)
|
---|
120 | erb.filename = filename
|
---|
121 | if output
|
---|
122 | if number
|
---|
123 | l = 1
|
---|
124 | for line in erb.src
|
---|
125 | puts "%3d %s"%[l, line]
|
---|
126 | l += 1
|
---|
127 | end
|
---|
128 | else
|
---|
129 | puts erb.src
|
---|
130 | end
|
---|
131 | else
|
---|
132 | erb.run(TOPLEVEL_BINDING.taint)
|
---|
133 | end
|
---|
134 | end
|
---|
135 | module_function :run
|
---|
136 | end
|
---|
137 | end
|
---|
138 |
|
---|
139 | if __FILE__ == $0
|
---|
140 | ERB::Main.run
|
---|
141 | end
|
---|