From 8f9eabc84e8dbdeb98c8ee2e7af85f00add91677 Mon Sep 17 00:00:00 2001 From: payonel Date: Sun, 14 May 2017 18:18:07 -0700 Subject: [PATCH] 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 --- .../loot/openos/bin/hostname.lua | 30 ++++++++++++------- .../loot/openos/boot/93_term.lua | 7 ++--- .../loot/openos/boot/94_shell.lua | 13 ++------ .../opencomputers/loot/openos/etc/profile | 2 +- .../loot/openos/usr/man/hostname | 5 +++- 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/bin/hostname.lua b/src/main/resources/assets/opencomputers/loot/openos/bin/hostname.lua index 8ddc55874..072dd720b 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/bin/hostname.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/bin/hostname.lua @@ -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() - else - io.stderr:write("Hostname not set\n") - return 1 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 diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/93_term.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/93_term.lua index 8382903d3..7b4036dc7 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/93_term.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/93_term.lua @@ -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) diff --git a/src/main/resources/assets/opencomputers/loot/openos/boot/94_shell.lua b/src/main/resources/assets/opencomputers/loot/openos/boot/94_shell.lua index 3074ce6ee..e8847a8cf 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/boot/94_shell.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/boot/94_shell.lua @@ -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") diff --git a/src/main/resources/assets/opencomputers/loot/openos/etc/profile b/src/main/resources/assets/opencomputers/loot/openos/etc/profile index d8c6a985d..dcbe584b5 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/etc/profile +++ b/src/main/resources/assets/opencomputers/loot/openos/etc/profile @@ -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}" diff --git a/src/main/resources/assets/opencomputers/loot/openos/usr/man/hostname b/src/main/resources/assets/opencomputers/loot/openos/usr/man/hostname index ae853e58a..931a94365 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/usr/man/hostname +++ b/src/main/resources/assets/opencomputers/loot/openos/usr/man/hostname @@ -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