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

[Error] Weird error after running program.

Started by Zoinky, 14 October 2012 - 02:53 AM
Zoinky #1
Posted 14 October 2012 - 04:53 AM
Hey, I was trying to make a antivirus (And did it) but the code is somewhat weird and messy. I was wondering if anyone had any suggestions on how to make the code more efficient or cleaner. Here it is:

Spoiler

local tArgs = {...}
local virusDB = {'fs.open((%p.)startup(%p.))', 'os.pullEvent = os.pullEventRaw', 'fs.delete((%p.)scan(%p.))'}
-- Add virus code to virusDB (line above)
percent = 0
if #tArgs < 1 then
print("Usage:")
print("scan <program>")
print("")
error()
end
path = shell.resolve(tArgs[1])
if fs.exists(path) == false then
print("File does not exist.")
error()
end

function status(lines)
add = (100/lines)/#virusDB
write(math.floor(percent))
print("%")
percent = percent + add
end


function scan(file, string)
for w in string.gmatch(file, string) do
  return true
end
return false
end


function ready(tempFile)
lines = #tempFile
if #tempFile > 65 then
quick = true
else
quick = false
end
for virNum = 1, #virusDB do
for fileNum = 1, #tempFile do
  term.clear()
  term.setCursorPos(1,1)
  print("Searching for virus #"..virNum.." in "..tArgs[1])
  write("Results: ")
  val = scan(tempFile[fileNum], virusDB[virNum])
  print(val)
  write("Status: ")
  status(lines)
  if val == true then
   print("")
   print("************************")
   print("	   Virus Info	   ")
   print("")
   print("Virus code: "..virusDB[virNum])
   print("Line number: "..fileNum)
   print("************************")
   print("")
   print("Would you like to delete this program? (Y/N)")
   act, key = os.pullEvent("key")
   if key == 21 then
	path = shell.resolve(tArgs[1])
	fs.delete(path)
	term.clear()
	term.setCursorPos(1,1)
	print("File removed.")
	error()
   end
  end
if quick == false then
   sleep(.1)
end
end
end
end

function read(file)
f = fs.open(file, "r")
fileData = {}
nilLine = 0
repeat
  table.insert(fileData, line)
  line = f.readLine()
  if line == nil then
   nilLine = nilLine + 1
  else
   nilLine = 0
  end
  until nilLine > 1
f.close()
ready(fileData)
end

read(tArgs[1])
term.clear()
term.setCursorPos(1,1)
print("File scan complete. Scanned "..lines.." lines total.")
if quick == true then
print("File was scanned in hyper mode to save time.")
end
sleep(2)
os.reboot()

Oh and I added the 'os.reboot()' line because I was getting a weird error (scan:78:bad argument: string expected, got nil) when I tried to run the Lua interface after I scanned a program. If anyone could figure that out I'd be grateful. Thanks in advance! :)/>/>
PixelToast #2
Posted 14 October 2012 - 05:21 AM
your overwriting the read function, a global, stahp
Zoinky #3
Posted 14 October 2012 - 06:47 AM
Oh right! Thanks :)/>/> I'll fix it up once I get home. :)/>/>