Module: Naksh::CommandSets::Unix

Defined in:
lib/naksh/commands/unix.rb

Overview

only what is written in the UNIX specification

Class Method Summary collapse

Class Method Details

.cd(new_working_dir = Naksh::user_home_directory, options = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/naksh/commands/unix.rb', line 70

def Unix.cd(new_working_dir=Naksh::user_home_directory,options=nil)
  new_working_dir=Pathname.new(new_working_dir)
  if options
    nyp :options,__LINE__
  else
    Windows::Directory.SetCurrentDirectory(new_working_dir.to_s) if $OS.contains('Windows')
    #neither system('cd') nor Dir.chdir changes WinXP pwd outside this program
    Dir.chdir(new_working_dir.to_s)
    return Naksh::NO_RETURN
  end
end

.echo(str) ⇒ Object

Raises:

  • (RuntimeError)


89
90
91
92
# File 'lib/naksh/commands/unix.rb', line 89

def Unix.echo(str)
  raise RuntimeError,"Unix.ls received nonstring #{working_dir.inspect}" unless working_dir.kind_of? String
  str
end

.hash(args = []) ⇒ Object

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/naksh/commands/unix.rb', line 23

def Unix.hash(args=[])
  raise NotImplementedError
end

.ls(args = []) ⇒ Object

responds to -a,-A accepts only strings

Raises:

  • (RuntimeError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/naksh/commands/unix.rb', line 49

def Unix.ls(args=[])
  #not ArgumentError because parameters are passed internally, external input is always a string
  raise RuntimeError,"Unix.ls received nonstring #{working_dir.inspect}" unless working_dir.kind_of? String
  working_dir=Pathname.new(working_dir)
  unless working_dir.exist?
    Naksh::Output.err.puts "ls: #{working_dir}: No such file or directory"
    return Naksh::STOP_VALUE
  end
  return working_dir.to_s unless working_dir.directory?
  if not options.empty?
    return Unix.sort(working_dir.children.join("\n")) if options.keys==[:A]
    return Unix.sort(working_dir.entries.join("\n")) if options.keys==[:a]
    nyp :options,__LINE__
  else
    res=[]
    working_dir.entries.each do |entry|
      res<< entry unless /\A\./.match(entry)
    end
    return Unix.sort(res.join("\n"))
  end
end

.mkdir(new_directories, options = []) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/naksh/commands/unix.rb', line 93

def Unix.mkdir(new_directories,options=[])
  new_directories.each do |new_directory_location|
    if options.has_key?(:p)
      Dir.mkpath(new_directory_location)
    else
      begin
        Dir.mkdir(new_directory_location)
      rescue
        Naksh::Output::err.puts("mkdir: cannot create directory `#{new_directory_location}': parent directory do not exist")
        return Naksh::STOP_VALUE
      end
    end
  end
  Naksh::NO_RETURN
end

.mv(file_to_be_moved, destination, options = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/naksh/commands/unix.rb', line 108

def Unix.mv(file_to_be_moved,destination,options=nil)
  if options
    nyp :options,__LINE__
  elsif not File.exists?(file_to_be_moved)
    nyp :error,__LINE__
  else
    file_to_be_moved=Pathname.new(file_to_be_moved)
    destination=Pathname.new(destination)
    if destination.exists? and destination.directory?
      if file_to_be_moved.dirname==destination.dirname
        File.rename(file_to_be_moved.basename,destination.basename)
      else
        File.move(file_to_be_moved,destination)
      end
    else
      File.move(file_to_be_moved,destination.dirname)
      File.rename(destination.dirname+file_to_be_moved.basename,destination.basename)
    end
  end
  Naksh::NO_RETURN
end

.pwd(options = false) ⇒ Object

finished



82
83
84
85
86
87
88
# File 'lib/naksh/commands/unix.rb', line 82

def Unix.pwd(options=false)
  if options
    Naksh::Output::err.puts('naksh: pwd accepts no arguments')
    return Naksh::STOP_VALUE
  end
  Dir::pwd
end

.sort(args = []) ⇒ Object

is it better to do octal excapes or literal characters? Nautilus (Gnome File Browser) uses different order



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/naksh/commands/unix.rb', line 31

def Unix.sort(args=[])

  unless options.empty? or options.keys==[:r]
    nyp :option,__LINE__
  end
  begin
    args.to_str
  rescue
    raise RuntimeError,"Unix.sort received nonstring #{args.inspect}"
  end
  #shy hyphen is before hard hyphen (doesn't show, but it's there)
  #there's a lot of obscure characters here which no one will ever use, but I'm trying to understand the ordering system, so I won't need a list of every character'
  result = sort_ordered_objects(args.split("\n"),((%w[´ ` ^ ¨ ~ ÷ < = > ¬ ¦ ° µ]<<' ').merge!(%w< _ ¯ ­ - , ; : ! ¡ ? ¿ . · ¸ ' " « » ( ) [ >)<<']').merge!(%w[{ } §  © ® @ ¤ ¢ $ £ ¥ * & # % + ±                                 ¼ ½ ¾ 0 1 ¹ 2 ² 3 ³ 4 5 6 7 8 9 Á À Â Å Ä Ã a A ª æ Æ b B c C d D e E f F g G h H i I j J k K l L m M n N o O º p P q Q r R s S ß t T u U v V w W x X y Y z Z]),:end).join("\n")
  result.reverse! if options.has_key? :r
  result
end

.sort(args = []) ⇒ Object

Raises:

  • (NotImplementedError)


23
24
25
26
27
# File 'lib/naksh/commands/gnu.rb', line 23

def Unix.sort(args=[])
  a=Naksh.argument_parser(args,1..1/0,:b=>:ignore_leading_blanks,:d=>:dictionary_order,:f=>:ignore_case,:g=>:general_numeric_sort,:i=>:ignore_nonprinting,:M=>:month_sort,:n=>:numeric_sort,:r=>:reverse,:C=>:check,:k=>:key,:m=>:merge,:o=>:output,:s=>:stable,:S=>:buffer_size,:t=>:field_separator,:T=>:temporary_directory,:u=>:unique,:a=>:zero_terminated)
  p a
  raise NotImplementedError
end

.type(args = []) ⇒ Object

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/naksh/commands/unix.rb', line 26

def Unix.type(args=[])
  raise NotImplementedError
end