Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- # This is a simple shell. Part of KitOS
- # Made by Sakireth
- #
- # This is licensed under the GNU GPL, available in COPYING
- # Initialization
- # Includes
- require 'readline'
- include Readline
- conf = {}
- conf['dirs'] = {}
- conf['files'] = {}
- conf['vars'] = {}
- # Configuration
- # Directories
- #conf['dirs']['home'] = ENV['HOME']
- conf['dirs']['home'] = '/home/robin/programmas/ruby/projects/kitos/shell'
- conf['dirs']['kitdir'] = conf['dirs']['home'] + '/.kit'
- # Files
- conf['files']['rc'] = conf['dirs']['kitdir'] + '/kitshellrc'
- # Default configuration
- conf['vars']['prompt'] = '>> '
- conf['vars']['debug'] = true
- # Check if the kit main directory exists. If not, create it.
- if File::directory?(conf['dirs']['kitdir']) == false
- Dir::mkdir(conf['dirs']['kitdir'])
- end
- # Now, load the RC file, containing handy user settings. (If it exists. If not, create it.)
- if File::file?(conf['files']['rc'])
- eval(File::read(conf['files']['rc']))
- else
- File::open(conf['files']['rc'], 'w').close
- end
- # Start the main loop, asking for the input.
- while true
- input = readline(conf['vars']['prompt'], TRUE).strip
- # To get arguments, we may want to split the input.
- input_splitted = input.split
- if input_splitted.length > 1
- args = input_splitted[1..-1].join(' ')
- end
- # If debugging is on, print the arguments.
- p args if conf['vars']['debug']
- # The commands which don't need arguments
- case input_splitted[0]
- when 'exit'
- exit
- when 'kitosrc'
- puts eval(File::read(conf['files']['rc']))
- else
- system(input)
- end
- # Here begin all the commands which need arguments.
- if input_splitted.length > 1
- case input_splitted[0]
- when 'cd'
- if args != ''
- if File::directory?(args)
- Dir::chdir(args)
- else
- puts 'Directory doesn\'t exist.'
- end
- else
- puts 'Switching to home directory' if conf['vars']['debug']
- Dir::chdir(conf['dirs']['home'])
- end
- when 'set'
- eval("#{input_splitted[1]} = #{input_splitted[2]}")
- when 'eval'
- eval(args)
- end
- end
- input = nil
- input_splitted = nil
- end
Add Comment
Please, Sign In to add comment