naksh 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. data/AUTHORS +1 -0
  2. data/COPYING +674 -0
  3. data/COPYING-DOCS +15 -0
  4. data/README.md +45 -0
  5. data/Rakefile +129 -0
  6. data/TODO +19 -0
  7. data/bin/naksh +2 -0
  8. data/data/default.mo +0 -0
  9. data/data/default.po +16 -0
  10. data/data/default2.mo +0 -0
  11. data/data/default2.po +27 -0
  12. data/data/starter.pot +29 -0
  13. data/docs/bash_flow.dia +0 -0
  14. data/docs/gtk_manual.txt +13 -0
  15. data/docs/master_plan.dia +0 -0
  16. data/docs/master_plan.png +0 -0
  17. data/docs/master_plan.svg +74 -0
  18. data/docs/master_plan.svgz +0 -0
  19. data/docs/userguide/en/about.xml +42 -0
  20. data/docs/userguide/en/gnu-fdl-1.2.xml +543 -0
  21. data/docs/userguide/en/naksh.xml +881 -0
  22. data/docs/userguide/en/naksh_start_window.png +0 -0
  23. data/docs/userguide/en/old_naksh.xml +180 -0
  24. data/docs/userguide/en/userguide-en.omf +37 -0
  25. data/lib/naksh.rb +165 -0
  26. data/lib/naksh/buffer.rb +153 -0
  27. data/lib/naksh/command_handling.rb +117 -0
  28. data/lib/naksh/commands/bash/README +2 -0
  29. data/lib/naksh/commands/bash/echo.nkc.rb +50 -0
  30. data/lib/naksh/commands/bsd.rb +25 -0
  31. data/lib/naksh/commands/dos.rb +25 -0
  32. data/lib/naksh/commands/fuse.rb +25 -0
  33. data/lib/naksh/commands/gnu.rb +30 -0
  34. data/lib/naksh/commands/gnu/README +2 -0
  35. data/lib/naksh/commands/gnu/cat.nkc.rb +50 -0
  36. data/lib/naksh/commands/gnu/cd.nkc.rb +11 -0
  37. data/lib/naksh/commands/gnu/echo.documentation.json +57 -0
  38. data/lib/naksh/commands/gnu/echo.documentation.rb +60 -0
  39. data/lib/naksh/commands/gnu/echo.nkc.rb +52 -0
  40. data/lib/naksh/commands/gnu/ls.nkc.rb +11 -0
  41. data/lib/naksh/commands/gnu/mv.nkc.rb +15 -0
  42. data/lib/naksh/commands/gnu/pwd.nkc.rb +11 -0
  43. data/lib/naksh/commands/naksh/addpath.nkc.rb +15 -0
  44. data/lib/naksh/commands/naksh/broken.nkc.rb +10 -0
  45. data/lib/naksh/commands/naksh/empty.nkc.rb +4 -0
  46. data/lib/naksh/commands/naksh/exit.nkc.rb +10 -0
  47. data/lib/naksh/commands/naksh/help.nkc.rb +21 -0
  48. data/lib/naksh/commands/naksh/man.nkc.rb +8 -0
  49. data/lib/naksh/commands/ruby.rb +31 -0
  50. data/lib/naksh/commands/unix.rb +131 -0
  51. data/lib/naksh/configuration/gconf.rb +51 -0
  52. data/lib/naksh/configuration/hash.rb +38 -0
  53. data/lib/naksh/defaults.rb +40 -0
  54. data/lib/naksh/history.rb +85 -0
  55. data/lib/naksh/interfaces.rb +59 -0
  56. data/lib/naksh/interfaces/fox.rb +49 -0
  57. data/lib/naksh/interfaces/gtk.rb +103 -0
  58. data/lib/naksh/interfaces/gtk/abtwin.rb +53 -0
  59. data/lib/naksh/interfaces/gtk/gnomeapp.rb +31 -0
  60. data/lib/naksh/interfaces/gtk/helpwin.rb +70 -0
  61. data/lib/naksh/interfaces/gtk/io.rb +94 -0
  62. data/lib/naksh/interfaces/gtk/mainwin.rb +56 -0
  63. data/lib/naksh/interfaces/gtk/mainwin/console.rb +121 -0
  64. data/lib/naksh/interfaces/gtk/mainwin/main_menu.rb +143 -0
  65. data/lib/naksh/interfaces/gtk/optwin.rb +78 -0
  66. data/lib/naksh/interfaces/tab.rb +58 -0
  67. data/lib/naksh/interfaces/wxw.rb +75 -0
  68. data/lib/naksh/interfaces/wxw/abtwin.rb +38 -0
  69. data/lib/naksh/interfaces/wxw/helpwin.rb +40 -0
  70. data/lib/naksh/interfaces/wxw/io.rb +61 -0
  71. data/lib/naksh/interfaces/wxw/mainwin.rb +42 -0
  72. data/lib/naksh/interfaces/wxw/mainwin/main_menu.rb +126 -0
  73. data/lib/naksh/interfaces/wxw/mainwin/sessions.rb +49 -0
  74. data/lib/naksh/interfaces/wxw/optwin.rb +37 -0
  75. data/lib/naksh/path_entry.rb +101 -0
  76. data/lib/naksh/regexp.rb +31 -0
  77. data/lib/naksh/session.rb +72 -0
  78. data/lib/naksh/stdlibext.rb +48 -0
  79. data/lib/naksh/syntax.rb +57 -0
  80. data/lib/naksh/syntax/bash.rb +219 -0
  81. data/lib/naksh/syntax/bash/antlr/bottomup.g +24 -0
  82. data/lib/naksh/syntax/bash/antlr/topdown.g +52 -0
  83. data/lib/naksh/syntax/bash/dhaka/evaluator.rb +353 -0
  84. data/lib/naksh/syntax/bash/dhaka/grammar.rb +71 -0
  85. data/lib/naksh/syntax/bash/dhaka/lexer.rb +52 -0
  86. data/lib/naksh/syntax/bash/treetop/bash.treetop +17 -0
  87. data/lib/old/abbr_call.rb +39 -0
  88. data/lib/old/autocomplete.rb +29 -0
  89. data/lib/old/command.rb +106 -0
  90. data/lib/old/command_holder.rb +75 -0
  91. data/lib/old/commands.rb +24 -0
  92. data/lib/old/old_executor.rb +95 -0
  93. data/lib/old/options.rb +31 -0
  94. data/lib/old/rush.rb +113 -0
  95. data/lib/old/sortissimo.rb +205 -0
  96. data/lib/old/systems.rb +25 -0
  97. data/lib/old/systems/ruby.rb +26 -0
  98. data/lib/old/un.rb +240 -0
  99. data/lib/rust/Rakefile +6 -0
  100. data/lib/rust/commands/builtins.rb +46 -0
  101. data/lib/rust/commands/builtins/cd.rb +17 -0
  102. data/lib/rust/commands/builtins/exit.rb +13 -0
  103. data/lib/rust/commands/builtins/help.rb +14 -0
  104. data/lib/rust/commands/builtins/parser.rb +13 -0
  105. data/lib/rust/commands/builtins/pwd.rb +13 -0
  106. data/lib/rust/commands/builtins/type.rb +13 -0
  107. data/lib/rust/commands/commands/ls.rb +13 -0
  108. data/lib/rust/commands/commands/read.rb +13 -0
  109. data/lib/rust/commands/commands/rm.rb +14 -0
  110. data/lib/rust/commands/commands/test.rb +20 -0
  111. data/lib/rust/helpers/array.rb +10 -0
  112. data/lib/rust/helpers/command_center.rb +78 -0
  113. data/lib/rust/helpers/constants.rb +58 -0
  114. data/lib/rust/helpers/io.rb +132 -0
  115. data/lib/rust/helpers/parser.rb +45 -0
  116. data/lib/rust/helpers/rush_control.rb +40 -0
  117. data/lib/rust/helpers/string.rb +50 -0
  118. data/lib/rust/helpers/trollop.rb +475 -0
  119. data/lib/rust/parsers/bash.rb +220 -0
  120. data/lib/rust/parsers/bash/stdlibext.rb +32 -0
  121. data/lib/rust/parsers/ren.rb +57 -0
  122. data/lib/rust/rust.rb +75 -0
  123. data/lib/rust/syntax/command.rb +23 -0
  124. data/lib/rust/syntax/paths.rb +31 -0
  125. data/lib/rust/syntax/pipes.rb +148 -0
  126. data/naksh.gemspec +55 -0
  127. data/test/naksh/arg_parser.rspec.rb +27 -0
  128. data/test/naksh/commands/bash/echo.rspec.rb +32 -0
  129. data/test/naksh/commands/sortlist +127 -0
  130. data/test/naksh/external_command.rspec.rb +46 -0
  131. data/test/naksh/send_error.rspec.rb +60 -0
  132. data/test/naksh/suggest.rspec.rb +38 -0
  133. data/test/naksh/syntax/bash.rspec.rb +69 -0
  134. data/test/naksh/syntax/bash/execute.rspec.rb +51 -0
  135. data/test/naksh/syntax/rspec.rb +63 -0
  136. data/test/run_rspecs.rb +20 -0
  137. metadata +217 -0
@@ -0,0 +1,31 @@
1
+ #!/usr/local/bin/ruby
2
+ # 2011 Mahmut Bulut
3
+
4
+ # This file is part of Naksh.
5
+ #
6
+ # Naksh is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Naksh is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Naksh. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ module Naksh
20
+ def options
21
+ @options
22
+ end
23
+ class OptionsClass<Hash
24
+ def method_missing(m)
25
+ p m
26
+ self[m]
27
+ end
28
+ end
29
+ @options=OptionsClass.new({
30
+ :suggest_on_failure?=>false}
31
+ end
data/lib/old/rush.rb ADDED
@@ -0,0 +1,113 @@
1
+ #!/usr/local/bin/ruby
2
+ # Copyright 20.2.09-27 Ari Brown
3
+ # All pwnage reserved.
4
+
5
+ #THIS FILE IS NO LONGER USED,
6
+ #IT IS KEPT SO YOU CAN STEAL CODE AND
7
+ #USE IT IN ANOTHER FILE
8
+
9
+ require "rubygems"
10
+
11
+ module Kernel
12
+ def interactive(m, &b)
13
+ system(m)
14
+ yield
15
+ system("exit")
16
+ end
17
+
18
+ def method_missing(m, *arr)
19
+ if arr[0].is_a? Proc
20
+ interactive(m, arr[0])
21
+ else
22
+ system(m + arr.join(" "))
23
+ end
24
+ end
25
+
26
+ alias_method(:old_system, :system)
27
+ def system(*arr)
28
+ puts *arr.join(" ")
29
+ old_system(*arr)
30
+ end
31
+ end
32
+
33
+ class Rush
34
+ attr_reader :env
35
+
36
+ class << self
37
+
38
+ def get(url, save_loco=Dir.pwd)
39
+ system("#{@env[:getter]} #{url}")
40
+ if url =~ %r(.+/([^/]+)$)
41
+ name = \1
42
+ File.new(\1, false, false)
43
+ end
44
+ end
45
+
46
+ def env(arr={})
47
+ @env[:getter] = arr[:getter] || "curl"
48
+ @env[:clean] = arr[:clean] || false
49
+ end
50
+
51
+ def clean(file)
52
+ system("rm -rf #{file}*")
53
+ end
54
+ end
55
+ end
56
+
57
+ class File
58
+
59
+ TYPES = [".zip",
60
+ ".tgz",
61
+ ".gz",
62
+ ".tar"]
63
+ UNCOMPRESS = {".zip" => "unzip",
64
+ ".tgz" => "tar zxvf",
65
+ ".tar" => "tar xvf",
66
+ ".gz" => "gunzip"}
67
+
68
+ def __clean(name)
69
+ if @clean
70
+ system("rm -rf #{name}")
71
+ end
72
+ end
73
+ private :clean
74
+
75
+ def initialize(file_name, clean, echo)
76
+ @type = TYPES.size.times do |i, type|
77
+ i = []
78
+
79
+ if file_name =~ %r((\.zip|\.tgz|\.tar|\.gz)$)
80
+ i << $1
81
+ end
82
+
83
+ i.reverse.join("")
84
+ end
85
+ @name = file_name - @type
86
+ @clean = clean
87
+ $echo = echo
88
+ end
89
+
90
+ def unpack
91
+ system(UNCOMPRESS[@type] + (@name + @type))
92
+ __clean(@name + @type)
93
+ end
94
+
95
+ def configure(*args)
96
+ system("cd #{@name}")
97
+ system("./configure" + args.join(" "))
98
+ system("cd ..")
99
+ end
100
+
101
+ def make
102
+ system("make")
103
+ end
104
+
105
+ def install
106
+ system("sudo make install")
107
+ end
108
+
109
+ def minstall
110
+ make
111
+ install
112
+ end
113
+ end
@@ -0,0 +1,205 @@
1
+ #!/usr/local/bin/ruby
2
+ # 2011 Mahmut Bulut
3
+
4
+ # This file is part of Naksh.
5
+ #
6
+ # Naksh is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Naksh is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Naksh. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ #this library is not capable of performing sorts using UNIX sort order
20
+ #because a<A<aa
21
+ #however, I am leaving it in until I figure out the right order.
22
+
23
+ #Need to replace this with proper method
24
+ #cant remember right now
25
+ class Array
26
+ alias_method :merge,:+
27
+ def merge!(other_arr)
28
+ self.replace(self.merge(other_arr))
29
+ end
30
+ end
31
+ #sortObjects(objects,order,options,&block)
32
+ #objects is an array of objects to be sorted
33
+ #order is an array in the order the objects are to be arranged or a hash with a pointer to a nonnegative integer
34
+ #others is either :beginning or :end, anything else raises an exception
35
+ #if unlisted==:beginning, unlisted objects are prepended in the order they are given in +objects+
36
+ #if unlisted==:end, unlisted objects are appended in the order they are given in +objects+
37
+ #If a block is given, it will be executed whenever an element of +objects+ is unlisted in +order+, it will be passed that object
38
+ def sort_objects(objects,order,unlisted=:end,&block)
39
+ raise ArgumentError,'3rd argument must be :beginning, :end, or :omit' if unlisted != :beginning and unlisted != :end and unlisted != :omit
40
+ begin
41
+ objects.replace(objects.to_ary)
42
+ rescue
43
+ raise ArgumentError,'1st argument must be an array'
44
+ end
45
+ begin
46
+ order.replace(order.to_ary)
47
+ rescue
48
+ begin
49
+ order.replace(order.to_hash)
50
+ rescue
51
+ raise ArgumentError,"2nd argument must respond to either to_ary or to_hsh #{order.inspect}"
52
+ end
53
+ raise ArgumentError,"second_argument.to_hash does not return a Hash #{order.inspect}" unless order.kind_of? Hash
54
+ end
55
+ #what's the proper error for object.to_ary is not an array?
56
+ raise ArgumentError,"second_argument.to_ary does not return an Array #{order.inspect}" unless order.kind_of? Array
57
+ result=[]
58
+ unlisted=[] if others
59
+ objects.each do |obj|
60
+ if order.class==Array
61
+ i=order.index(obj)
62
+ else
63
+ i=order[obj]
64
+ end
65
+ if i
66
+ #in case a hash points to something other than a nonnegative integer of class Fixnum
67
+ if order.class==Hash
68
+ begin
69
+ ni=i.to_i
70
+ #gets caught
71
+ raise ArgumentError unless i==ni
72
+ i=ni
73
+ ni=nil
74
+ rescue
75
+ raise ArgumentError,'2nd Argument (hashlike) has keys pointing to nonintegers'
76
+ end
77
+ end
78
+ if result[i]
79
+ result[i]<< obj
80
+ else
81
+ result[i]=[obj]
82
+ end
83
+ else
84
+ if block
85
+ #should blocks be allowed to modify obj?
86
+ yield(obj)
87
+ end
88
+ if unlisted != :omit
89
+ begin
90
+ unlisted<< obj
91
+ rescue
92
+ end
93
+ end
94
+ end
95
+ end
96
+ result.compact!
97
+ result.flatten!
98
+ if unlisted != :omit# or result.empty? #what is this?
99
+ if unlisted==:beginning
100
+ return unlisted.merge(result)
101
+ elsif unlisted==:end
102
+ return result.merge(unlisted)
103
+ else
104
+ raise RuntimeError,"internal problem with sortissimo; unlisted is not :omit, :beginning, or :end. This should have been checked earlier [#{unlisted.inspect}]"
105
+ end
106
+ end
107
+ result
108
+ end
109
+
110
+
111
+
112
+
113
+ #make nil allowed in order set
114
+ #[a,b,nil,c]
115
+ #this was a bad attempt to apply sort_objects to strings and arrays
116
+ def sort_ordered_objects(objects,order,others=:end,element=0,&block)
117
+ $stderr.puts('this does not appear to work.')
118
+ raise ArgumentError,'3rd argument must be falsy or :beginning or :end' if others and others != :beginning and others != :end
119
+ begin
120
+ objects.replace(objects.to_ary)
121
+ rescue
122
+ raise ArgumentError,'1st argument must be an array'
123
+ end
124
+ begin
125
+ order.replace(order.to_ary)
126
+ rescue
127
+ begin
128
+ order.replace(order.to_hash)
129
+ rescue
130
+ raise ArgumentError,"2nd argument must respond to either to_ary or to_hsh #{order.inspect}"
131
+ end
132
+ raise ArgumentError,"second_argument.to_hash does not return a Hash #{order.inspect}" unless order.kind_of? Hash
133
+ end
134
+ #what's the proper error for object.to_ary is not an array?
135
+ raise ArgumentError,"second_argument.to_ary does not return an Array #{order.inspect}" unless order.kind_of? Array
136
+ result=[]
137
+ unlisted=[] if others
138
+ objects.each do |obj|
139
+ if order.class==Array
140
+ i=order.index(obj[element])
141
+ else
142
+ i=order[obj[element]]
143
+ end
144
+ if i
145
+ #in case a hash points to something other than a nonnegative integer of class Fixnum
146
+ if order.class==Hash
147
+ begin
148
+ ni=i.to_i
149
+ #gets caught
150
+ raise ArgumentError unless i==ni
151
+ i=ni
152
+ ni=nil
153
+ rescue
154
+ raise ArgumentError,'2nd Argument (hashlike) has keys pointing to nonintegers'
155
+ end
156
+ end
157
+ if result[i]
158
+ result[i]<< obj
159
+ else
160
+ result[i]=[obj]
161
+ end
162
+ else
163
+ if block
164
+ #should blocks be allowed to modify obj?
165
+ yield(obj)
166
+ end
167
+ if others
168
+ begin
169
+ unlisted<< obj
170
+ rescue
171
+ end
172
+ end
173
+ end
174
+ end
175
+ result.compact!
176
+ result.collect do entry
177
+ entry.kind_of? Array ? entry=sort_ordered_objects(entry,order,others,element.next) : entry
178
+ end
179
+ result.flatten!
180
+ if others or result.empty?
181
+ if others==:beginning
182
+ return unlisted.merge(result)
183
+ else#if others==:end#argument checked earlier
184
+ return result.merge(unlisted)
185
+ end
186
+ end
187
+ result
188
+ end
189
+
190
+ a=[4,6,'a']
191
+ b={5=>a,:6=>'ahr'}
192
+ c=nil
193
+ d=3
194
+ e=:6
195
+ f=Object.new
196
+ class<<f
197
+ undef_method :==,:===,'=~'.intern,:args_and_options,:class,:clone,:dclone,:deep_clone,:display,:dup,:enum_for,:eql?,:equal?,:extend,:freeze,:frozen?,:hash,:id,:inspect,:instance_eval,:instance_of?,:instance_variable_defined?,:instance_variable_get,:instance_variable_set,:instance_variables,:is_a?,:is_one_of?,:kind_of?,:metaclass,:method,:methods,:nil?,:object_id,:private_methods,:protected_methods,:public_methods,:remove_instance_variable,:respond_to?,:rollback!,:send,:singleton_method_added,:singleton_method_removed,:singleton_method_undefined,:singleton_methods,:taint,:tainted?,:to_a,:to_enum,:to_s,:to_yaml,:to_yaml_properties,:to_yaml_style,:type,:untaint,'`'.intern,:abort,:at_exit,:autoload,:autoload?,:binding,:block_given?,:callcc,:caller,:catch,:chomp,:chomp!,:chop,:chop!,:context,:describe,:eval,:exec,:exit,:exit!,:fail,:fork,:format,:gem,:getc,:gets,:global_variables,:gsub,:gsub!,:iterator?,:lambda,:load,:local_variables,:loop,:method_missing,:open,:open_uri_original_open,:p,:pp,:pretty_inspect,:print,:printf,:proc,:putc,:puts,:raise,:rand,:readline,:readlines,:require,:require_gem,:respond_to,:scan,:scanf,:select,:set_trace_func,:sleep,:split,:sprintf,:srand,:sub,:sub!,:syscall,:system,:test,:throw,:trace_var,:trap,:untrace_var,:warn,:y,:pretty_print,:pretty_print_cycle,:pretty_print_inspect,:pretty_print_instance_variables
198
+ def method_missing(*args,&block)
199
+ p args,block
200
+ yield block
201
+ *args
202
+ end
203
+ end
204
+ p f.methods
205
+ sort_objects([a,b,c,d,e,f],[b,e,c,a,f,d],:end,0){|*args|p args;args}
@@ -0,0 +1,25 @@
1
+ #!/usr/local/bin/ruby
2
+ # 2011 Mahmut Bulut
3
+
4
+ # This file is part of Naksh.
5
+ #
6
+ # Naksh is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # Naksh is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with Naksh. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ module Naksh
20
+ #these should be really dumb
21
+ #only use when there isn't a cross-platform way
22
+ #good example is expanding '~/'
23
+ module Systems
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/ruby
2
+ # 2011 Mahmut Bulut
3
+ # GPLv3 License
4
+
5
+
6
+
7
+ module Naksh
8
+ module Systems
9
+ module Ruby
10
+ def Ruby.delete(file_path)
11
+ File.del(file_path)
12
+ end
13
+ def Ruby.make_dir(dir_name,folder=Naksh.current_dir)
14
+ File.mkdir(dir_name,folder=Naksh.current_dir)
15
+ end
16
+ def Ruby.make_file(file_name,folder=Naksh.current_dir)
17
+ end
18
+ def Ruby.move(file_path,destination_dir)
19
+ File.mv(file_path,destination_dir)
20
+ end
21
+ def Ruby.rename(file_path,new_name)
22
+ File.rename(file_path,new_name)
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/old/un.rb ADDED
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/ruby
2
+ # 2003 WATANABE Hirofumi
3
+ # Ruby License
4
+
5
+ #http://www.atdot.net/~ko1/w3ml/w3ml.cgi/yarv-diff/msg/152
6
+ #
7
+ # = un.rb
8
+ #
9
+ # Copyright (c) 2003 WATANABE Hirofumi <eban ruby-lang.org>
10
+ #
11
+ # This program is free software.
12
+ # You can distribute/modify this program under the same terms of Ruby.
13
+ #
14
+ # == Utilities to replace common UNIX commands in Makefiles etc
15
+ #
16
+ # == SYNOPSIS
17
+ #
18
+ # ruby -run -e cp -- [OPTION] SOURCE DEST
19
+ # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
20
+ # ruby -run -e mv -- [OPTION] SOURCE DEST
21
+ # ruby -run -e rm -- [OPTION] FILE
22
+ # ruby -run -e mkdir -- [OPTION] DIRS
23
+ # ruby -run -e rmdir -- [OPTION] DIRS
24
+ # ruby -run -e install -- [OPTION] SOURCE DEST
25
+ # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
26
+ # ruby -run -e touch -- [OPTION] FILE
27
+ # ruby -run -e help [COMMAND]
28
+
29
+ require "fileutils"
30
+ require "optparse"
31
+
32
+ module FileUtils
33
+ # @fileutils_label = ""
34
+ @fileutils_output = $stdout
35
+ end
36
+
37
+ def setup(options = "")
38
+ ARGV.map! do |x|
39
+ case x
40
+ when /^-/
41
+ x.delete "^-#{options}v"
42
+ when /[*?\[{]/
43
+ Dir[x]
44
+ else
45
+ x
46
+ end
47
+ end
48
+ ARGV.flatten!
49
+ ARGV.delete_if{|x| x == "-"}
50
+ opt_hash = {}
51
+ OptionParser.new do |o|
52
+ options.scan(/.:?/) do |s|
53
+ o.on("-" + s.tr(":", " ")) do |val|
54
+ opt_hash[s.delete(":").intern] = val
55
+ end
56
+ end
57
+ o.on("-v") do opt_hash[:verbose] = true end
58
+ o.parse!
59
+ end
60
+ yield ARGV, opt_hash
61
+ end
62
+
63
+ ##
64
+ # Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY
65
+ #
66
+ # ruby -run -e cp -- [OPTION] SOURCE DEST
67
+ #
68
+ # -p preserve file attributes if possible
69
+ # -r copy recursively
70
+ # -v verbose
71
+ #
72
+
73
+ def cp
74
+ setup("pr") do |argv, options|
75
+ cmd = "cp"
76
+ cmd += "_r" if options.delete :r
77
+ options[:preserve] = true if options.delete :p
78
+ dest = argv.pop
79
+ argv = argv[0] if argv.size == 1
80
+ FileUtils.send cmd, argv, dest, options
81
+ end
82
+ end
83
+
84
+ ##
85
+ # Create a link to the specified TARGET with LINK_NAME.
86
+ #
87
+ # ruby -run -e ln -- [OPTION] TARGET LINK_NAME
88
+ #
89
+ # -s make symbolic links instead of hard links
90
+ # -f remove existing destination files
91
+ # -v verbose
92
+ #
93
+
94
+ def ln
95
+ setup("sf") do |argv, options|
96
+ cmd = "ln"
97
+ cmd += "_s" if options.delete :s
98
+ options[:force] = true if options.delete :f
99
+ dest = argv.pop
100
+ argv = argv[0] if argv.size == 1
101
+ FileUtils.send cmd, argv, dest, options
102
+ end
103
+ end
104
+
105
+ ##
106
+ # Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.
107
+ #
108
+ # ruby -run -e mv -- [OPTION] SOURCE DEST
109
+ #
110
+ # -v verbose
111
+ #
112
+
113
+ def mv
114
+ setup do |argv, options|
115
+ dest = argv.pop
116
+ argv = argv[0] if argv.size == 1
117
+ FileUtils.mv argv, dest, options
118
+ end
119
+ end
120
+
121
+ ##
122
+ # Remove the FILE
123
+ #
124
+ # ruby -run -e rm -- [OPTION] FILE
125
+ #
126
+ # -f ignore nonexistent files
127
+ # -r remove the contents of directories recursively
128
+ # -v verbose
129
+ #
130
+
131
+ def rm
132
+ setup("fr") do |argv, options|
133
+ cmd = "rm"
134
+ cmd += "_r" if options.delete :r
135
+ options[:force] = true if options.delete :f
136
+ FileUtils.send cmd, argv, options
137
+ end
138
+ end
139
+
140
+ ##
141
+ # Create the DIR, if they do not already exist.
142
+ #
143
+ # ruby -run -e mkdir -- [OPTION] DIR
144
+ #
145
+ # -p no error if existing, make parent directories as needed
146
+ # -v verbose
147
+ #
148
+
149
+ def mkdir
150
+ setup("p") do |argv, options|
151
+ cmd = "mkdir"
152
+ cmd += "_p" if options.delete :p
153
+ FileUtils.send cmd, argv, options
154
+ end
155
+ end
156
+
157
+ ##
158
+ # Remove the DIR.
159
+ #
160
+ # ruby -run -e rmdir -- [OPTION] DIR
161
+ #
162
+ # -v verbose
163
+ #
164
+
165
+ def rmdir
166
+ setup do |argv, options|
167
+ FileUtils.rmdir argv, options
168
+ end
169
+ end
170
+
171
+ ##
172
+ # Copy SOURCE to DEST.
173
+ #
174
+ # ruby -run -e install -- [OPTION] SOURCE DEST
175
+ #
176
+ # -p apply access/modification times of SOURCE files to
177
+ # corresponding destination files
178
+ # -m set permission mode (as in chmod), instead of 0755
179
+ # -v verbose
180
+ #
181
+
182
+ def install
183
+ setup("pm:") do |argv, options|
184
+ options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
185
+ options[:preserve] = true if options.delete :p
186
+ dest = argv.pop
187
+ argv = argv[0] if argv.size == 1
188
+ FileUtils.install argv, dest, options
189
+ end
190
+ end
191
+
192
+ ##
193
+ # Change the mode of each FILE to OCTAL-MODE.
194
+ #
195
+ # ruby -run -e chmod -- [OPTION] OCTAL-MODE FILE
196
+ #
197
+ # -v verbose
198
+ #
199
+
200
+ def chmod
201
+ setup do |argv, options|
202
+ mode = argv.shift.oct
203
+ FileUtils.chmod mode, argv, options
204
+ end
205
+ end
206
+
207
+ ##
208
+ # Update the access and modification times of each FILE to the current time.
209
+ #
210
+ # ruby -run -e touch -- [OPTION] FILE
211
+ #
212
+ # -v verbose
213
+ #
214
+
215
+ def touch
216
+ setup do |argv, options|
217
+ FileUtils.touch argv, options
218
+ end
219
+ end
220
+
221
+ ##
222
+ # Display help message.
223
+ #
224
+ # ruby -run -e help [COMMAND]
225
+ #
226
+
227
+ def help
228
+ setup do |argv,|
229
+ all = argv.empty?
230
+ open(__FILE__) do |me|
231
+ while me.gets("##\n")
232
+ if help = me.gets("\n\n")
233
+ if all or argv.delete help[/-e \w+/].sub(/-e /, "")
234
+ print help.gsub(/^# ?/, "")
235
+ end
236
+ end
237
+ end
238
+ end
239
+ end
240
+ end