Alakazard12

CC Bios

Feb 23rd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 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 lua parts of the os api
  111. function os.version()
  112. if turtle then
  113. return "TurtleOS 1.5"
  114. end
  115. return "CraftOS 1.5"
  116. end
  117.  
  118. function os.pullEventRaw( _sFilter )
  119. return coroutine.yield( _sFilter )
  120. end
  121.  
  122. function os.pullEvent( _sFilter )
  123. local eventData = { os.pullEventRaw( _sFilter ) }
  124. if eventData[1] == "terminate" then
  125. error( "Terminated", 0 )
  126. end
  127. return unpack( eventData )
  128. end
  129.  
  130. -- Install globals
  131. function sleep( _nTime )
  132. local timer = os.startTimer( _nTime )
  133. repeat
  134. local sEvent, param = os.pullEvent( "timer" )
  135. until param == timer
  136. end
  137.  
  138. function write( sText )
  139. local w,h = term.getSize()
  140. local x,y = term.getCursorPos()
  141.  
  142. local nLinesPrinted = 0
  143. local function newLine()
  144. if y + 1 <= h then
  145. term.setCursorPos(1, y + 1)
  146. else
  147. term.setCursorPos(1, h)
  148. term.scroll(1)
  149. end
  150. x, y = term.getCursorPos()
  151. nLinesPrinted = nLinesPrinted + 1
  152. end
  153.  
  154. -- Print the line with proper word wrapping
  155. while string.len(sText) > 0 do
  156. local whitespace = string.match( sText, "^[ \t]+" )
  157. if whitespace then
  158. -- Print whitespace
  159. term.write( whitespace )
  160. x,y = term.getCursorPos()
  161. sText = string.sub( sText, string.len(whitespace) + 1 )
  162. end
  163.  
  164. local newline = string.match( sText, "^\n" )
  165. if newline then
  166. -- Print newlines
  167. newLine()
  168. sText = string.sub( sText, 2 )
  169. end
  170.  
  171. local text = string.match( sText, "^[^ \t\n]+" )
  172. if text then
  173. sText = string.sub( sText, string.len(text) + 1 )
  174. if string.len(text) > w then
  175. -- Print a multiline word
  176. while string.len( text ) > 0 do
  177. if x > w then
  178. newLine()
  179. end
  180. term.write( text )
  181. text = string.sub( text, (w-x) + 2 )
  182. x,y = term.getCursorPos()
  183. end
  184. else
  185. -- Print a word normally
  186. if x + string.len(text) - 1 > w then
  187. newLine()
  188. end
  189. term.write( text )
  190. x,y = term.getCursorPos()
  191. end
  192. end
  193. end
  194.  
  195. return nLinesPrinted
  196. end
  197.  
  198. function print( ... )
  199. local nLinesPrinted = 0
  200. for n,v in ipairs( { ... } ) do
  201. nLinesPrinted = nLinesPrinted + write( tostring( v ) )
  202. end
  203. nLinesPrinted = nLinesPrinted + write( "\n" )
  204. return nLinesPrinted
  205. end
  206.  
  207. function printError( ... )
  208. if term.isColour() then
  209. term.setTextColour( colours.red )
  210. end
  211. print( ... )
  212. term.setTextColour( colours.white )
  213. end
  214.  
  215. function read( _sReplaceChar, _tHistory )
  216. term.setCursorBlink( true )
  217.  
  218. local sLine = ""
  219. local nHistoryPos = nil
  220. local nPos = 0
  221. if _sReplaceChar then
  222. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  223. end
  224.  
  225. local w, h = term.getSize()
  226. local sx, sy = term.getCursorPos()
  227.  
  228. local function redraw( _sCustomReplaceChar )
  229. local nScroll = 0
  230. if sx + nPos >= w then
  231. nScroll = (sx + nPos) - w
  232. end
  233.  
  234. term.setCursorPos( sx, sy )
  235. local sReplace = _sCustomReplaceChar or _sReplaceChar
  236. if sReplace then
  237. term.write( string.rep( sReplace, string.len(sLine) - nScroll ) )
  238. else
  239. term.write( string.sub( sLine, nScroll + 1 ) )
  240. end
  241. term.setCursorPos( sx + nPos - nScroll, sy )
  242. end
  243.  
  244. while true do
  245. local sEvent, param = os.pullEvent()
  246. if sEvent == "char" then
  247. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  248. nPos = nPos + 1
  249. redraw()
  250.  
  251. elseif sEvent == "key" then
  252. if param == keys.enter then
  253. -- Enter
  254. break
  255.  
  256. elseif param == keys.left then
  257. -- Left
  258. if nPos > 0 then
  259. nPos = nPos - 1
  260. redraw()
  261. end
  262.  
  263. elseif param == keys.right then
  264. -- Right
  265. if nPos < string.len(sLine) then
  266. redraw(" ")
  267. nPos = nPos + 1
  268. redraw()
  269. end
  270.  
  271. elseif param == keys.up or param == keys.down then
  272. -- Up or down
  273. if _tHistory then
  274. redraw(" ")
  275. if param == keys.up then
  276. -- Up
  277. if nHistoryPos == nil then
  278. if #_tHistory > 0 then
  279. nHistoryPos = #_tHistory
  280. end
  281. elseif nHistoryPos > 1 then
  282. nHistoryPos = nHistoryPos - 1
  283. end
  284. else
  285. -- Down
  286. if nHistoryPos == #_tHistory then
  287. nHistoryPos = nil
  288. elseif nHistoryPos ~= nil then
  289. nHistoryPos = nHistoryPos + 1
  290. end
  291. end
  292. if nHistoryPos then
  293. sLine = _tHistory[nHistoryPos]
  294. nPos = string.len( sLine )
  295. else
  296. sLine = ""
  297. nPos = 0
  298. end
  299. redraw()
  300. end
  301. elseif param == keys.backspace then
  302. -- Backspace
  303. if nPos > 0 then
  304. redraw(" ")
  305. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  306. nPos = nPos - 1
  307. redraw()
  308. end
  309. elseif param == keys.home then
  310. -- Home
  311. redraw(" ")
  312. nPos = 0
  313. redraw()
  314. elseif param == keys.delete then
  315. if nPos < string.len(sLine) then
  316. redraw(" ")
  317. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  318. redraw()
  319. end
  320. elseif param == keys["end"] then
  321. -- End
  322. redraw(" ")
  323. nPos = string.len(sLine)
  324. redraw()
  325. end
  326. end
  327. end
  328.  
  329. term.setCursorBlink( false )
  330. term.setCursorPos( w + 1, sy )
  331. print()
  332.  
  333. return sLine
  334. end
  335.  
  336. loadfile = function( _sFile )
  337. local file = fs.open( _sFile, "r" )
  338. if file then
  339. local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  340. file.close()
  341. return func, err
  342. end
  343. return nil, "File not found"
  344. end
  345.  
  346. dofile = function( _sFile )
  347. local fnFile, e = loadfile( _sFile )
  348. if fnFile then
  349. setfenv( fnFile, getfenv(2) )
  350. return fnFile()
  351. else
  352. error( e, 2 )
  353. end
  354. end
  355.  
  356. -- Install the rest of the OS api
  357. function os.run( _tEnv, _sPath, ... )
  358. local tArgs = { ... }
  359. local fnFile, err = loadfile( _sPath )
  360. if fnFile then
  361. local tEnv = _tEnv
  362. --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
  363. setmetatable( tEnv, { __index = _G } )
  364. setfenv( fnFile, tEnv )
  365. local ok, err = pcall( function()
  366. fnFile( unpack( tArgs ) )
  367. end )
  368. if not ok then
  369. if err and err ~= "" then
  370. printError( err )
  371. end
  372. return false
  373. end
  374. return true
  375. end
  376. if err and err ~= "" then
  377. printError( err )
  378. end
  379. return false
  380. end
  381.  
  382. local nativegetmetatable = getmetatable
  383. local nativetype = type
  384. local nativeerror = error
  385. function getmetatable( _t )
  386. if nativetype( _t ) == "string" then
  387. nativeerror( "Attempt to access string metatable", 2 )
  388. return nil
  389. end
  390. return nativegetmetatable( _t )
  391. end
  392.  
  393. local tAPIsLoading = {}
  394. function os.loadAPI( _sPath )
  395. local sName = fs.getName( _sPath )
  396. if tAPIsLoading[sName] == true then
  397. printError( "API "..sName.." is already being loaded" )
  398. return false
  399. end
  400. tAPIsLoading[sName] = true
  401.  
  402. local tEnv = {}
  403. setmetatable( tEnv, { __index = _G } )
  404. local fnAPI, err = loadfile( _sPath )
  405. if fnAPI then
  406. setfenv( fnAPI, tEnv )
  407. fnAPI()
  408. else
  409. printError( err )
  410. tAPIsLoading[sName] = nil
  411. return false
  412. end
  413.  
  414. local tAPI = {}
  415. for k,v in pairs( tEnv ) do
  416. tAPI[k] = v
  417. end
  418.  
  419. _G[sName] = tAPI
  420. tAPIsLoading[sName] = nil
  421. return true
  422. end
  423.  
  424. function os.unloadAPI( _sName )
  425. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  426. _G[_sName] = nil
  427. end
  428. end
  429.  
  430. function os.sleep( _nTime )
  431. sleep( _nTime )
  432. end
  433.  
  434. local nativeShutdown = os.shutdown
  435. function os.shutdown()
  436. nativeShutdown()
  437. while true do
  438. coroutine.yield()
  439. end
  440. end
  441.  
  442. local nativeReboot = os.reboot
  443. function os.reboot()
  444. nativeReboot()
  445. while true do
  446. coroutine.yield()
  447. end
  448. end
  449.  
  450. -- Install the lua part of the HTTP api (if enabled)
  451. if http then
  452. local function wrapRequest( _url, _post )
  453. local requestID = http.request( _url, _post )
  454. while true do
  455. local event, param1, param2 = os.pullEvent()
  456. if event == "http_success" and param1 == _url then
  457. return param2
  458. elseif event == "http_failure" and param1 == _url then
  459. return nil
  460. end
  461. end
  462. end
  463.  
  464. http.get = function( _url )
  465. return wrapRequest( _url, nil )
  466. end
  467.  
  468. http.post = function( _url, _post )
  469. return wrapRequest( _url, _post or "" )
  470. end
  471. end
  472.  
  473. -- Load APIs
  474. local tApis = fs.list( "rom/apis" )
  475. for n,sFile in ipairs( tApis ) do
  476. if string.sub( sFile, 1, 1 ) ~= "." then
  477. local sPath = fs.combine( "rom/apis", sFile )
  478. if not fs.isDir( sPath ) then
  479. os.loadAPI( sPath )
  480. end
  481. end
  482. end
  483.  
  484. if turtle then
  485. local tApis = fs.list( "rom/apis/turtle" )
  486. for n,sFile in ipairs( tApis ) do
  487. if string.sub( sFile, 1, 1 ) ~= "." then
  488. local sPath = fs.combine( "rom/apis/turtle", sFile )
  489. if not fs.isDir( sPath ) then
  490. os.loadAPI( sPath )
  491. end
  492. end
  493. end
  494. end
  495.  
  496. -- Run the shell
  497. local ok, err = pcall( function()
  498. parallel.waitForAny(
  499. function()
  500. os.run( {}, "rom/programs/shell" )
  501. end,
  502. function()
  503. rednet.run()
  504. end )
  505. end )
  506.  
  507. -- If the shell errored, let the user read it.
  508. if not ok then
  509. printError( err )
  510. end
  511.  
  512. pcall( function()
  513. term.setCursorBlink( false )
  514. print( "Press any key to continue" )
  515. os.pullEvent( "key" )
  516. end )
  517. os.shutdown()
Add Comment
Please, Sign In to add comment