Advertisement
Alakazard12

newBIOS

Jan 28th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1.  
  2. --[[
  3. -- Install safe versions of various library functions
  4. -- These will not put cfunctions on the stack, so don't break serialisation
  5. xpcall = function( _fn, _fnErrorHandler )
  6. local typeT = type( _fn )
  7. assert( typeT == "function", "bad argument #1 to xpcall (function expected, got "..typeT..")" )
  8. local co = coroutine.create( _fn )
  9. local tResults = { coroutine.resume( co ) }
  10. while coroutine.status( co ) ~= "dead" do
  11. tResults = { coroutine.resume( co, coroutine.yield() ) }
  12. end
  13. if tResults[1] == true then
  14. return true, unpack( tResults, 2 )
  15. else
  16. return false, _fnErrorHandler( tResults[2] )
  17. end
  18. end
  19.  
  20. pcall = function( _fn, ... )
  21. local typeT = type( _fn )
  22. assert( typeT == "function", "bad argument #1 to pcall (function expected, got "..typeT..")" )
  23. local tArgs = { ... }
  24. return xpcall(
  25. function()
  26. return _fn( unpack( tArgs ) )
  27. end,
  28. function( _error )
  29. return _error
  30. end
  31. )
  32. end
  33.  
  34. function pairs( _t )
  35. local typeT = type( _t )
  36. if typeT ~= "table" then
  37. error( "bad argument #1 to pairs (table expected, got "..typeT..")", 2 )
  38. end
  39. return next, _t, nil
  40. end
  41.  
  42. function ipairs( _t )
  43. local typeT = type( _t )
  44. if typeT ~= "table" then
  45. error( "bad argument #1 to ipairs (table expected, got "..typeT..")", 2 )
  46. end
  47. return function( t, var )
  48. var = var + 1
  49. local value = t[var]
  50. if value == nil then
  51. return
  52. end
  53. return var, value
  54. end, _t, 0
  55. end
  56.  
  57. function coroutine.wrap( _fn )
  58. local typeT = type( _fn )
  59. if typeT ~= "function" then
  60. error( "bad argument #1 to coroutine.wrap (function expected, got "..typeT..")", 2 )
  61. end
  62. local co = coroutine.create( _fn )
  63. return function( ... )
  64. local tResults = { coroutine.resume( co, ... ) }
  65. if tResults[1] then
  66. return unpack( tResults, 2 )
  67. else
  68. error( tResults[2], 2 )
  69. end
  70. end
  71. end
  72.  
  73. function string.gmatch( _s, _pattern )
  74. local type1 = type( _s )
  75. if type1 ~= "string" then
  76. error( "bad argument #1 to string.gmatch (string expected, got "..type1..")", 2 )
  77. end
  78. local type2 = type( _pattern )
  79. if type2 ~= "string" then
  80. error( "bad argument #2 to string.gmatch (string expected, got "..type2..")", 2 )
  81. end
  82.  
  83. local nPos = 1
  84. return function()
  85. local nFirst, nLast = string.find( _s, _pattern, nPos )
  86. if nFirst == nil then
  87. return
  88. end
  89. nPos = nLast + 1
  90. return string.match( _s, _pattern, nFirst )
  91. end
  92. end
  93.  
  94. local nativesetmetatable = setmetatable
  95. function setmetatable( _o, _t )
  96. if _t and type(_t) == "table" then
  97. local idx = rawget( _t, "__index" )
  98. if idx and type( idx ) == "table" then
  99. rawset( _t, "__index", function( t, k ) return idx[k] end )
  100. end
  101. local newidx = rawget( _t, "__newindex" )
  102. if newidx and type( newidx ) == "table" then
  103. rawset( _t, "__newindex", function( t, k, v ) newidx[k] = v end )
  104. end
  105. end
  106. return nativesetmetatable( _o, _t )
  107. end
  108. ]]
  109.  
  110. -- Install fix for luaj's broken string.sub/string.find
  111. local nativestringfind = string.find
  112. local nativestringsub = string.sub
  113. function string.sub( ... )
  114. local r = nativestringsub( ... )
  115. if r then
  116. return r .. ""
  117. end
  118. return nil
  119. end
  120. function string.find( s, ... )
  121. return nativestringfind( s .. "", ... );
  122. end
  123.  
  124. -- Install lua parts of the os api
  125. function os.version()
  126. return "CraftOS 1.6 (Beta)"
  127. end
  128.  
  129. function os.pullEventRaw( sFilter )
  130. return coroutine.yield( sFilter )
  131. end
  132.  
  133. function os.pullEvent( sFilter )
  134. local eventData = { os.pullEventRaw( sFilter ) }
  135. if eventData[1] == "terminate" then
  136. error( "Terminated", 0 )
  137. end
  138. return unpack( eventData )
  139. end
  140.  
  141. -- Install globals
  142. function sleep( nTime )
  143. local timer = os.startTimer( nTime )
  144. repeat
  145. local sEvent, param = os.pullEvent( "timer" )
  146. until param == timer
  147. end
  148.  
  149. function write( sText )
  150. local w,h = term.getSize()
  151. local x,y = term.getCursorPos()
  152.  
  153. local nLinesPrinted = 0
  154. local function newLine()
  155. if y + 1 <= h then
  156. term.setCursorPos(1, y + 1)
  157. else
  158. term.setCursorPos(1, h)
  159. term.scroll(1)
  160. end
  161. x, y = term.getCursorPos()
  162. nLinesPrinted = nLinesPrinted + 1
  163. end
  164.  
  165. -- Print the line with proper word wrapping
  166. while string.len(sText) > 0 do
  167. local whitespace = string.match( sText, "^[ \t]+" )
  168. if whitespace then
  169. -- Print whitespace
  170. term.write( whitespace )
  171. x,y = term.getCursorPos()
  172. sText = string.sub( sText, string.len(whitespace) + 1 )
  173. end
  174.  
  175. local newline = string.match( sText, "^\n" )
  176. if newline then
  177. -- Print newlines
  178. newLine()
  179. sText = string.sub( sText, 2 )
  180. end
  181.  
  182. local text = string.match( sText, "^[^ \t\n]+" )
  183. if text then
  184. sText = string.sub( sText, string.len(text) + 1 )
  185. if string.len(text) > w then
  186. -- Print a multiline word
  187. while string.len( text ) > 0 do
  188. if x > w then
  189. newLine()
  190. end
  191. term.write( text )
  192. text = string.sub( text, (w-x) + 2 )
  193. x,y = term.getCursorPos()
  194. end
  195. else
  196. -- Print a word normally
  197. if x + string.len(text) - 1 > w then
  198. newLine()
  199. end
  200. term.write( text )
  201. x,y = term.getCursorPos()
  202. end
  203. end
  204. end
  205.  
  206. return nLinesPrinted
  207. end
  208.  
  209. function print( ... )
  210. local nLinesPrinted = 0
  211. for n,v in ipairs( { ... } ) do
  212. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  213. end
  214. nLinesPrinted = nLinesPrinted + write( "\n" )
  215. return nLinesPrinted
  216. end
  217.  
  218. function printError( ... )
  219. if term.isColour() then
  220. term.setTextColour( colours.red )
  221. end
  222. print( ... )
  223. term.setTextColour( colours.white )
  224. end
  225.  
  226. function read( _sReplaceChar, _tHistory )
  227. term.setCursorBlink( true )
  228.  
  229. local sLine = ""
  230. local nHistoryPos
  231. local nPos = 0
  232. if _sReplaceChar then
  233. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  234. end
  235.  
  236. local w, h = term.getSize()
  237. local sx, sy = term.getCursorPos()
  238.  
  239. local function redraw( _sCustomReplaceChar )
  240. local nScroll = 0
  241. if sx + nPos >= w then
  242. nScroll = (sx + nPos) - w
  243. end
  244.  
  245. term.setCursorPos( sx, sy )
  246. local sReplace = _sCustomReplaceChar or _sReplaceChar
  247. if sReplace then
  248. term.write( string.rep( sReplace, string.len(sLine) - nScroll ) )
  249. else
  250. term.write( string.sub( sLine, nScroll + 1 ) )
  251. end
  252. term.setCursorPos( sx + nPos - nScroll, sy )
  253. end
  254.  
  255. while true do
  256. local sEvent, param = os.pullEvent()
  257. if sEvent == "char" then
  258. -- Typed key
  259. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  260. nPos = nPos + 1
  261. redraw()
  262.  
  263. elseif sEvent == "paste" then
  264. -- Pasted text
  265. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  266. nPos = nPos + string.len( param )
  267. redraw()
  268.  
  269. elseif sEvent == "key" then
  270. if param == keys.enter then
  271. -- Enter
  272. break
  273.  
  274. elseif param == keys.left then
  275. -- Left
  276. if nPos > 0 then
  277. nPos = nPos - 1
  278. redraw()
  279. end
  280.  
  281. elseif param == keys.right then
  282. -- Right
  283. if nPos < string.len(sLine) then
  284. redraw(" ")
  285. nPos = nPos + 1
  286. redraw()
  287. end
  288.  
  289. elseif param == keys.up or param == keys.down then
  290. -- Up or down
  291. if _tHistory then
  292. redraw(" ")
  293. if param == keys.up then
  294. -- Up
  295. if nHistoryPos == nil then
  296. if #_tHistory > 0 then
  297. nHistoryPos = #_tHistory
  298. end
  299. elseif nHistoryPos > 1 then
  300. nHistoryPos = nHistoryPos - 1
  301. end
  302. else
  303. -- Down
  304. if nHistoryPos == #_tHistory then
  305. nHistoryPos = nil
  306. elseif nHistoryPos ~= nil then
  307. nHistoryPos = nHistoryPos + 1
  308. end
  309. end
  310. if nHistoryPos then
  311. sLine = _tHistory[nHistoryPos]
  312. nPos = string.len( sLine )
  313. else
  314. sLine = ""
  315. nPos = 0
  316. end
  317. redraw()
  318. end
  319. elseif param == keys.backspace then
  320. -- Backspace
  321. if nPos > 0 then
  322. redraw(" ")
  323. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  324. nPos = nPos - 1
  325. redraw()
  326. end
  327. elseif param == keys.home then
  328. -- Home
  329. redraw(" ")
  330. nPos = 0
  331. redraw()
  332. elseif param == keys.delete then
  333. -- Delete
  334. if nPos < string.len(sLine) then
  335. redraw(" ")
  336. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  337. redraw()
  338. end
  339. elseif param == keys["end"] then
  340. -- End
  341. redraw(" ")
  342. nPos = string.len(sLine)
  343. redraw()
  344. end
  345. end
  346. end
  347.  
  348. term.setCursorBlink( false )
  349. term.setCursorPos( w + 1, sy )
  350. print()
  351.  
  352. return sLine
  353. end
  354.  
  355. loadfile = function( _sFile )
  356. local file = fs.open( _sFile, "r" )
  357. if file then
  358. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  359. file.close()
  360. return func, err
  361. end
  362. return nil, "File not found"
  363. end
  364.  
  365. dofile = function( _sFile )
  366. local fnFile, e = loadfile( _sFile )
  367. if fnFile then
  368. setfenv( fnFile, getfenv(2) )
  369. return fnFile()
  370. else
  371. error( e, 2 )
  372. end
  373. end
  374.  
  375. -- Install the rest of the OS api
  376. function os.run( _tEnv, _sPath, ... )
  377. local tArgs = { ... }
  378. local fnFile, err = loadfile( _sPath )
  379. if fnFile then
  380. local tEnv = _tEnv
  381. --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  382. setmetatable( tEnv, { __index = _G } )
  383. setfenv( fnFile, tEnv )
  384. local ok, err = pcall( function()
  385. fnFile( unpack( tArgs ) )
  386. end )
  387. if not ok then
  388. if err and err ~= "" then
  389. printError( err )
  390. end
  391. return false
  392. end
  393. return true
  394. end
  395. if err and err ~= "" then
  396. printError( err )
  397. printError( "bar" )
  398. end
  399. return false
  400. end
  401.  
  402. local nativegetmetatable = getmetatable
  403. local nativetype = type
  404. local nativeerror = error
  405. function getmetatable( _t )
  406. if nativetype( _t ) == "string" then
  407. nativeerror( "Attempt to access string metatable", 2 )
  408. return nil
  409. end
  410. return nativegetmetatable( _t )
  411. end
  412.  
  413. local tAPIsLoading = {}
  414. function os.loadAPI( _sPath )
  415. local sName = fs.getName( _sPath )
  416. if tAPIsLoading[sName] == true then
  417. printError( "API "..sName.." is already being loaded" )
  418. return false
  419. end
  420. tAPIsLoading[sName] = true
  421.  
  422. local tEnv = {}
  423. setmetatable( tEnv, { __index = _G } )
  424. local fnAPI, err = loadfile( _sPath )
  425. if fnAPI then
  426. setfenv( fnAPI, tEnv )
  427. fnAPI()
  428. else
  429. printError( err )
  430. tAPIsLoading[sName] = nil
  431. return false
  432. end
  433.  
  434. local tAPI = {}
  435. for k,v in pairs( tEnv ) do
  436. tAPI[k] = v
  437. end
  438.  
  439. _G[sName] = tAPI
  440. tAPIsLoading[sName] = nil
  441. return true
  442. end
  443.  
  444. function os.unloadAPI( _sName )
  445. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  446. _G[_sName] = nil
  447. end
  448. end
  449.  
  450. function os.sleep( nTime )
  451. sleep( nTime )
  452. end
  453.  
  454. local nativeShutdown = os.shutdown
  455. function os.shutdown()
  456. nativeShutdown()
  457. while true do
  458. coroutine.yield()
  459. end
  460. end
  461.  
  462. local nativeReboot = os.reboot
  463. function os.reboot()
  464. nativeReboot()
  465. while true do
  466. coroutine.yield()
  467. end
  468. end
  469.  
  470. -- Install the lua part of the HTTP api (if enabled)
  471. if http then
  472. local function wrapRequest( _url, _post )
  473. local requestID = http.request( _url, _post )
  474. while true do
  475. local event, param1, param2 = os.pullEvent()
  476. if event == "http_success" and param1 == _url then
  477. return param2
  478. elseif event == "http_failure" and param1 == _url then
  479. return nil
  480. end
  481. end
  482. end
  483.  
  484. http.get = function( _url )
  485. return wrapRequest( _url, nil )
  486. end
  487.  
  488. http.post = function( _url, _post )
  489. return wrapRequest( _url, _post or "" )
  490. end
  491. end
  492.  
  493. -- Load APIs
  494. local tApis = fs.list( "rom/apis" )
  495. for n,sFile in ipairs( tApis ) do
  496. if string.sub( sFile, 1, 1 ) ~= "." then
  497. local sPath = fs.combine( "rom/apis", sFile )
  498. if not fs.isDir( sPath ) then
  499. os.loadAPI( sPath )
  500. end
  501. end
  502. end
  503.  
  504. if turtle then
  505. local tApis = fs.list( "rom/apis/turtle" )
  506. for n,sFile in ipairs( tApis ) do
  507. if string.sub( sFile, 1, 1 ) ~= "." then
  508. local sPath = fs.combine( "rom/apis/turtle", sFile )
  509. if not fs.isDir( sPath ) then
  510. os.loadAPI( sPath )
  511. end
  512. end
  513. end
  514. end
  515.  
  516. -- Run the shell
  517. local ok, err = pcall( function()
  518. parallel.waitForAny(
  519. function()
  520. if term.isColour() then
  521. os.run( {}, "rom/programs/advanced/multishell" )
  522. else
  523. os.run( {}, "rom/programs/shell" )
  524. end
  525. os.run( {}, "rom/programs/shutdown" )
  526. end,
  527. function()
  528. rednet.run()
  529. end )
  530. end )
  531.  
  532. -- If the shell errored, let the user read it.
  533. term.redirect( term.native() )
  534. if not ok then
  535. printError( err )
  536. pcall( function()
  537. term.setCursorBlink( false )
  538. print( "Press any key to continue" )
  539. os.pullEvent( "key" )
  540. end )
  541. end
  542.  
  543. -- End
  544. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement