daily pastebin goal
93%
SHARE
TWEET

Untitled

a guest Feb 20th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2.  
  3. # This is a simple shell. Part of KitOS
  4. # Made by Sakireth
  5. #
  6. # This is licensed under the GNU GPL, available in COPYING
  7.  
  8. # Initialization
  9. # Includes
  10. require 'readline'
  11. include Readline
  12.  
  13. conf = {}
  14. conf['dirs'] = {}
  15. conf['files'] = {}
  16. conf['vars'] = {}
  17.  
  18. # Configuration
  19. # Directories
  20. #conf['dirs']['home'] = ENV['HOME']
  21. conf['dirs']['home'] = '/home/robin/programmas/ruby/projects/kitos/shell'
  22. conf['dirs']['kitdir'] = conf['dirs']['home'] + '/.kit'
  23. # Files
  24. conf['files']['rc'] = conf['dirs']['kitdir'] + '/kitshellrc'
  25.  
  26. # Default configuration
  27. conf['vars']['prompt'] = '>> '
  28. conf['vars']['debug'] = true
  29.  
  30. # Check if the kit main directory exists. If not, create it.
  31. if File::directory?(conf['dirs']['kitdir']) == false
  32.   Dir::mkdir(conf['dirs']['kitdir'])
  33. end
  34.  
  35. # Now, load the RC file, containing handy user settings. (If it exists. If not, create it.)
  36. if File::file?(conf['files']['rc'])
  37.   eval(File::read(conf['files']['rc']))
  38. else
  39.   File::open(conf['files']['rc'], 'w').close
  40. end
  41.  
  42. # Start the main loop, asking for the input.
  43. while true
  44.   input = readline(conf['vars']['prompt'], TRUE).strip
  45.  
  46.   # To get arguments, we may want to split the input.
  47.   input_splitted = input.split
  48.   if input_splitted.length > 1
  49.     args = input_splitted[1..-1].join(' ')
  50.   end
  51.  
  52.   # If debugging is on, print the arguments.
  53.   p args if conf['vars']['debug']
  54.  
  55.   # The commands which don't need arguments
  56.   case input_splitted[0]
  57.     when 'exit'
  58.       exit
  59.     when 'kitosrc'
  60.       puts eval(File::read(conf['files']['rc']))
  61.     else
  62.       system(input)
  63.   end
  64.  
  65.   # Here begin all the commands which need arguments.
  66.   if input_splitted.length > 1
  67.     case input_splitted[0]
  68.       when 'cd'
  69.         if args != ''
  70.           if File::directory?(args)
  71.             Dir::chdir(args)
  72.           else
  73.             puts 'Directory doesn\'t exist.'
  74.           end
  75.         else
  76.           puts 'Switching to home directory' if conf['vars']['debug']
  77.           Dir::chdir(conf['dirs']['home'])
  78.         end
  79.       when 'set'
  80.         eval("#{input_splitted[1]} = #{input_splitted[2]}")
  81.       when 'eval'
  82.         eval(args)
  83.     end
  84.   end
  85.  
  86.   input = nil
  87.   input_splitted = nil
  88. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top