hostname to PS1 fix

hostname in PS1 cannot be set by the init signal as that occurs before /etc/profile, which overrides PS1

Also, clean up some code to not try to change PS1, but just change HOSTNAME

Added `hostname --update` to update $HOSTNAME by reading /etc/hostname again
This commit is contained in:
payonel 2017-05-14 18:18:07 -07:00
parent 7f1739614d
commit 8f9eabc84e
5 changed files with 30 additions and 27 deletions

View File

@ -1,22 +1,30 @@
local args = {...}
if args[1] then
local shell = require("shell")
local args, ops = shell.parse(...)
local hostname = args[1]
if hostname then
local file, reason = io.open("/etc/hostname", "w")
if not file then
io.stderr:write("failed to open for writing: ", reason, "\n")
return 1
else
file:write(args[1])
file:close()
os.setenv("HOSTNAME", args[1])
os.setenv("PS1", "$HOSTNAME:$PWD# ")
end
file:write(hostname)
file:close()
ops.update = true
else
local file = io.open("/etc/hostname")
if file then
io.write(file:read("*l"), "\n")
hostname = file:read("*l")
file:close()
end
end
if ops.update then
os.setenv("HOSTNAME_SEPARATOR", hostname and #hostname > 0 and ":" or "")
os.setenv("HOSTNAME", hostname)
elseif hostname then
print(hostname)
else
io.stderr:write("Hostname not set\n")
return 1
end
end

View File

@ -32,6 +32,9 @@ local function components_changed(ename, address, type)
-- recheck what kb to use
window.keyboard = nil
end
if (type == "screen" or type == "gpu") and not tty.isAvailable() then
computer.pushSignal("term_unavailable")
end
elseif (ename == "component_added" or ename == "component_available") and type == "keyboard" then
-- we need to clear the current terminals cached keyboard (if any) when
-- a new keyboard becomes available. This is in case the new keyboard was
@ -43,10 +46,6 @@ local function components_changed(ename, address, type)
-- primary keyboard is a valid keyboard (weird, in my opinion)
window.keyboard = nil
end
if (type == "screen" or type == "gpu") and not tty.isAvailable() then
computer.pushSignal("term_unavailable")
end
end
event.listen("component_removed", components_changed)

View File

@ -1,10 +1,3 @@
local shell = require("shell")
require("event").listen("init", function()
local file = io.open("/etc/hostname")
if file then
os.setenv("HOSTNAME", file:read("*l"))
os.setenv("PS1", "$HOSTNAME:$PWD# ")
file:close()
end
end)
-- there doesn't seem to be a reason to update $HOSTNAME after the init signal
-- as user space /etc/profile comes after this point anyways
loadfile("/bin/hostname.lua")("--update")

View File

@ -16,7 +16,7 @@ set HOME=/home
set IFS=\
set MANPATH=/usr/man:.
set PAGER=/bin/more
set PS1='$PWD # '
set PS1='$HOSTNAME$HOSTNAME_SEPARATOR$PWD # '
set PWD=/
set SHELL=/bin/sh
set LS_COLORS="{FILE=0xFFFFFF,DIR=0x66CCFF,LINK=0xFFAA00,['*.lua']=0x00FF00}"

View File

@ -2,7 +2,7 @@ NAME
hostname - Display and modify hostname
SYNOPIS
hostname [NEW NAME]
hostname [NEW NAME] [--update]
EXAMPLES
hostname
@ -10,3 +10,6 @@ EXAMPLES
hostname test
Sets hostname of this computer to test
hostname --update
Updates $HOSTNAME by reading /etc/hostname in case it was set manually. Does not print to stdout