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

[LUA][ERROR][QUESTION] Output from startup program ends up in the lua prompt?

Started by arbot, 30 May 2012 - 04:44 PM
arbot #1
Posted 30 May 2012 - 06:44 PM
Hi,

So, today, I wrote my first LUA program. It's a news aggregator, which pulls news items from my server's website using the HTTP API.

It appears to work perfecltly. However, when I run the "lua" program on any machine, the output from my program (Which runs at startup) ends up being execurted by the LUA prompt.

EDIT: Added screenshots

This is the output of my program.



And this is what happens when I run "lua".



Here's my code..

Snippet added to serverwide "startup"


if not fs.exists("/NewsCache/NoStartup") then
		shell.run("news", "update")
		shell.run("news", "read")
end

News program


appver = "0.1"

term.clear() -- Clear the terminal
term.setCursorPos(1, 1) -- Set the cursor position to the top left

local tArgs = { ... } -- Get the shell args

local x, y = term.getSize() -- Get the size of the terminal (For == NEWS == headers)

local headerNum = ( x / 2 ) - 4
local headerString = string.rep("=", headerNum)

function outputHeader()
		print ( headerString .. " NEWS " .. headerString)
end

function outputFooter()
		print ( string.rep("=", (x - 2) ) )
end

function update(silent)

		if silent == nil then
				silent = false
		end

		if not silent then
				print ( "Checking for a news update.." )
		end

		local h = http.get("http://archivesmc.com/TekkitNewsVersion.txt")

		if h == nil then
				print ( "There was a problem checking for updates." )
				return
		end

		latestVersion = tonumber(h.readAll())
		h.close()

		if localVersion < latestVersion then
				local updateNum = latestVersion - localVersion
				print ("Downloading ".. updateNum .." update(s).")

				i = 0
				curUpdate = (localVersion + 1)

				while i < updateNum do
						h = http.get("http://archivesmc.com/TekkitNews/"..curUpdate)
						if h == nil then
								print ("Unable to download update #" .. curUpdate)
						else
								local filename = "NewsCache/news/"..curUpdate
								local data = h.readAll()
								h.close()


								if not fs.exists(filename) then
										h = fs.open(filename, "w")
										h.write(data)
										h.close()
										if not silent then
												print ( "Downloaded update #"..curUpdate..".")
										end
								else
										if not silent then
												print ( "Update #" .. curUpdate .. " already downloaded, skipping.." )
										end
								end
						end

						i = (i + 1)
						curUpdate = (curUpdate + 1)
				end

				h = fs.open("NewsCache/version", "w")
				h.write(latestVersion)
				h.close()

				print ( "Updated to version " .. latestVersion .. "." )
				localVersion = latestVersion

		else
				print ("Nothing to update.")
		end
end



function read(num)
		if num == nil then
				num = localVersion -- Just get the latest news item.
		end

		local filepath = "NewsCache/news/" .. num

		if not fs.exists( filepath ) then
				print ( "Update "..num.." cannot be found." )
				print ( "The latest stored update is "..localVersion.."." )
				return
		end

		h = fs.open( filepath, "r" )
		data = h.readAll()
		h.close()

		outputHeader()
		print ( data )
		outputFooter()
end


if not fs.exists( "NewsCache" ) then -- If the News cache folder doesn't exist..
		fs.makeDir( "NewsCache" ) -- Create it
end

if not fs.exists( "NewsCache/news" ) then -- If the News cache is missing its news folder..
		fs.makeDir( "NewsCache/news" ) -- Create it
end

localVersion = 0


if not fs.exists( "NewsCache" ) then -- If the News cache folder doesn't exist..
		fs.makeDir( "NewsCache" ) -- Create it
end

if not fs.exists( "NewsCache/news" ) then -- If the News cache is missing its news folder..
		fs.makeDir( "NewsCache/news" ) -- Create it
end

localVersion = 0

if fs.exists( "NewsCache/version" ) then -- If we have a version file, load it
		h = fs.open( "NewsCache/version", "r" )
		localVersion = tonumber(h.readAll())
		h.close()
else -- If not, create it and update
		h = fs.open( "NewsCache/version", "w")
		h.write("0")
		h.close()
		update()
		term.clear()
		term.setCursorPos(1, 1)
		read()
		return
end


if #tArgs == 0 then -- No args?
		shell.run("news", "help")
		return
elseif tArgs[1] == "update" then -- Update?
		update()
		return
elseif tArgs[1] == "read" then -- Read?
		read(tArgs[2])
		return
elseif tArgs[1] == "startup" then -- Startup check?
		if fs.exists("NewsCache/NoStartup") then
				fs.delete("NewsCache/NoStartup")
				print ( "Startup check enabled.")
		else
				fs.open("NewsCache/NoStartup", "w")
				print ( "Startup check disabled." )
		end
elseif tArgs[1] == "help" then -- Help?
		print ( "News V " .. appver .. " by g/arbot" )
		print ( "" )
		print ( "-- Command listing --" )
		print ( "" )
		print ( "news help: This information" )
		print ( "news update: Check for news" )
		print ( "  updates." )
		print ( "news read [entry]: Read the" )
		print ( "  news (entry optional)" )
		print ( "news startup: Enable or disable")
		print ( "  startup check" )
else -- Not recognized?
		print (tArgs[1] .. " is not a recognized command. Try help for more info.")
end
MysticT #2
Posted 30 May 2012 - 07:01 PM
The problem is that you are overwriting the read function with yours. So each time the function is called in any program, it will run your program (actually only the read function). Don't use global variables and functions, they can mess up your code and others.
arbot #3
Posted 30 May 2012 - 07:13 PM
The problem is that you are overwriting the read function with yours. So each time the function is called in any program, it will run your program (actually only the read function). Don't use global variables and functions, they can mess up your code and others.

Ahhh, balls, I forgot about that.

Thanks a bunch, I'll go test it.

EDIT: Yep, that fixed it. Thanks a bunch. <3
Lolgast #4
Posted 31 May 2012 - 09:54 AM
Now I've heared several times about the HTTP api. I was wondering where I could find more information about this API, since I can't find it here in the forums? Isn't it here in the forums, or am I just not looking well enough?
BigSHinyToys #5
Posted 31 May 2012 - 10:02 AM
Now I've heared several times about the HTTP api. I was wondering where I could find more information about this API, since I can't find it here in the forums? Isn't it here in the forums, or am I just not looking well enough?
http://www.computercraft.info/forums2/index.php?/topic/82-121-httptest-v13/
Lolgast #6
Posted 31 May 2012 - 02:43 PM
Thanks. Also explained why I couldn't find it, I was looking in the API's and utilities forum instead of tutorials.
MysticT #7
Posted 31 May 2012 - 05:59 PM
Thanks. Also explained why I couldn't find it, I was looking in the API's and utilities forum instead of tutorials.
That's what the wiki is for :)/>/>
You have the description and methods of every api. Here's the http api on the wiki.