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 = {...} local shell = require("shell")
if args[1] then local args, ops = shell.parse(...)
local hostname = args[1]
if hostname then
local file, reason = io.open("/etc/hostname", "w") local file, reason = io.open("/etc/hostname", "w")
if not file then if not file then
io.stderr:write("failed to open for writing: ", reason, "\n") io.stderr:write("failed to open for writing: ", reason, "\n")
return 1 return 1
else
file:write(args[1])
file:close()
os.setenv("HOSTNAME", args[1])
os.setenv("PS1", "$HOSTNAME:$PWD# ")
end end
file:write(hostname)
file:close()
ops.update = true
else else
local file = io.open("/etc/hostname") local file = io.open("/etc/hostname")
if file then if file then
io.write(file:read("*l"), "\n") hostname = file:read("*l")
file:close() file:close()
else
io.stderr:write("Hostname not set\n")
return 1
end end
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

View File

@ -32,6 +32,9 @@ local function components_changed(ename, address, type)
-- recheck what kb to use -- recheck what kb to use
window.keyboard = nil window.keyboard = nil
end 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 elseif (ename == "component_added" or ename == "component_available") and type == "keyboard" then
-- we need to clear the current terminals cached keyboard (if any) when -- 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 -- 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) -- primary keyboard is a valid keyboard (weird, in my opinion)
window.keyboard = nil window.keyboard = nil
end end
if (type == "screen" or type == "gpu") and not tty.isAvailable() then
computer.pushSignal("term_unavailable")
end
end end
event.listen("component_removed", components_changed) event.listen("component_removed", components_changed)

View File

@ -1,10 +1,3 @@
local shell = require("shell") -- 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
require("event").listen("init", function() loadfile("/bin/hostname.lua")("--update")
local file = io.open("/etc/hostname")
if file then
os.setenv("HOSTNAME", file:read("*l"))
os.setenv("PS1", "$HOSTNAME:$PWD# ")
file:close()
end
end)

View File

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

View File

@ -2,7 +2,7 @@ NAME
hostname - Display and modify hostname hostname - Display and modify hostname
SYNOPIS SYNOPIS
hostname [NEW NAME] hostname [NEW NAME] [--update]
EXAMPLES EXAMPLES
hostname hostname
@ -10,3 +10,6 @@ EXAMPLES
hostname test hostname test
Sets hostname of this computer to 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