2021-03-21 16:41:23 +00:00
|
|
|
-- The preload file initializes everything else for our shell
|
|
|
|
|
|
|
|
local fs = require 'fs'
|
|
|
|
local commander = require 'commander'
|
2021-03-31 03:58:12 +00:00
|
|
|
local bait = require 'bait'
|
2021-06-10 10:29:35 +00:00
|
|
|
local oldDir = hilbish.cwd()
|
2021-03-21 16:41:23 +00:00
|
|
|
|
2021-05-29 03:10:23 +00:00
|
|
|
local shlvl = tonumber(os.getenv 'SHLVL')
|
|
|
|
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
|
|
|
|
|
2021-05-19 01:04:49 +00:00
|
|
|
-- Builtins
|
2021-03-31 17:46:22 +00:00
|
|
|
commander.register('cd', function (args)
|
|
|
|
if #args > 0 then
|
|
|
|
local path = ''
|
|
|
|
for i = 1, #args do
|
|
|
|
path = path .. tostring(args[i]) .. ' '
|
|
|
|
end
|
2021-05-01 17:31:51 +00:00
|
|
|
path = path:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
|
2021-05-27 22:23:15 +00:00
|
|
|
:gsub('$([%w_]+)', os.getenv):gsub('%z','$'):gsub("%s+", "")
|
2021-03-31 17:46:22 +00:00
|
|
|
|
2021-05-27 22:23:15 +00:00
|
|
|
if path == '-' then
|
2021-06-10 10:29:35 +00:00
|
|
|
path = oldDir
|
2021-05-27 23:06:45 +00:00
|
|
|
print(path)
|
2021-05-27 22:23:15 +00:00
|
|
|
end
|
2021-06-10 10:29:35 +00:00
|
|
|
oldDir = hilbish.cwd()
|
2021-05-27 23:06:45 +00:00
|
|
|
|
2021-03-31 17:46:22 +00:00
|
|
|
local ok, err = pcall(function() fs.cd(path) end)
|
2021-03-31 03:58:12 +00:00
|
|
|
if not ok then
|
|
|
|
if err == 1 then
|
2021-03-31 03:59:59 +00:00
|
|
|
print('directory does not exist')
|
2021-03-31 03:58:12 +00:00
|
|
|
end
|
2021-05-11 22:57:44 +00:00
|
|
|
return err
|
2021-05-11 22:55:22 +00:00
|
|
|
end
|
2021-06-14 22:11:07 +00:00
|
|
|
bait.throw('cd', path)
|
2021-03-31 03:59:59 +00:00
|
|
|
return
|
2021-03-21 16:41:23 +00:00
|
|
|
end
|
2021-06-14 22:12:12 +00:00
|
|
|
fs.cd(hilbish.home)
|
|
|
|
bait.throw('cd', hilbish.home)
|
2021-06-10 00:30:12 +00:00
|
|
|
|
|
|
|
return
|
2021-03-21 16:41:23 +00:00
|
|
|
end)
|
2021-04-03 20:08:04 +00:00
|
|
|
|
2021-04-05 19:21:44 +00:00
|
|
|
commander.register('exit', function()
|
|
|
|
os.exit(0)
|
|
|
|
end)
|
|
|
|
|
2021-04-03 20:08:04 +00:00
|
|
|
do
|
|
|
|
local virt_G = { }
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-04-03 20:08:04 +00:00
|
|
|
setmetatable(_G, {
|
2021-06-09 22:41:37 +00:00
|
|
|
__index = function (_, key)
|
2021-04-03 20:08:04 +00:00
|
|
|
local got_virt = virt_G[key]
|
|
|
|
if got_virt ~= nil then
|
|
|
|
return got_virt
|
|
|
|
end
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-04-03 20:08:04 +00:00
|
|
|
virt_G[key] = os.getenv(key)
|
|
|
|
return virt_G[key]
|
|
|
|
end,
|
2021-06-09 22:41:37 +00:00
|
|
|
|
|
|
|
__newindex = function (_, key, value)
|
2021-04-03 20:08:04 +00:00
|
|
|
if type(value) == 'string' then
|
|
|
|
os.setenv(key, value)
|
|
|
|
virt_G[key] = value
|
|
|
|
else
|
|
|
|
if type(virt_G[key]) == 'string' then
|
|
|
|
os.setenv(key, '')
|
|
|
|
end
|
|
|
|
virt_G[key] = value
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-04-03 20:08:04 +00:00
|
|
|
bait.catch('command.exit', function ()
|
|
|
|
for key, value in pairs(virt_G) do
|
|
|
|
if type(value) == 'string' then
|
|
|
|
virt_G[key] = os.getenv(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2021-05-01 05:08:20 +00:00
|
|
|
|
|
|
|
-- Function additions to Lua standard library
|
|
|
|
function string.split(str, delimiter)
|
|
|
|
local result = {}
|
|
|
|
local from = 1
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-05-01 05:08:20 +00:00
|
|
|
local delim_from, delim_to = string.find(str, delimiter, from)
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-05-01 05:08:20 +00:00
|
|
|
while delim_from do
|
|
|
|
table.insert(result, string.sub(str, from, delim_from - 1))
|
|
|
|
from = delim_to + 1
|
|
|
|
delim_from, delim_to = string.find(str, delimiter, from)
|
|
|
|
end
|
2021-06-09 22:41:37 +00:00
|
|
|
|
2021-05-01 05:08:20 +00:00
|
|
|
table.insert(result, string.sub(str, from))
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-05-19 01:04:49 +00:00
|
|
|
-- Hook handles
|
|
|
|
bait.catch('command.not-found', function(cmd)
|
|
|
|
print(string.format('hilbish: %s not found', cmd))
|
|
|
|
end)
|
|
|
|
|