Sirshark10

testbios

Jun 24th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.74 KB | None | 0 0
  1.  
  2. backup = {getfenv,setfenv,}
  3. local nativegetfenv = getfenv
  4. if _VERSION == "Lua 5.1" then
  5. -- If we're on Lua 5.1, install parts of the Lua 5.2/5.3 API so that programs can be written against it
  6. local nativeload = load
  7. local nativeloadstring = loadstring
  8. local nativesetfenv = setfenv
  9. function load( x, name, mode, env )
  10. if type( x ) ~= "string" and type( x ) ~= "function" then
  11. error( "bad argument #1 (expected string or function, got " .. type( x ) .. ")", 2 )
  12. end
  13. if name ~= nil and type( name ) ~= "string" then
  14. error( "bad argument #2 (expected string, got " .. type( name ) .. ")", 2 )
  15. end
  16. if mode ~= nil and type( mode ) ~= "string" then
  17. error( "bad argument #3 (expected string, got " .. type( mode ) .. ")", 2 )
  18. end
  19. if env ~= nil and type( env) ~= "table" then
  20. error( "bad argument #4 (expected table, got " .. type( env ) .. ")", 2 )
  21. end
  22. if mode ~= nil and mode ~= "t" then
  23. error( "Binary chunk loading prohibited", 2 )
  24. end
  25. local ok, p1, p2 = pcall( function()
  26. if type(x) == "string" then
  27. local result, err = nativeloadstring( x, name )
  28. if result then
  29. if env then
  30. env._ENV = env
  31. nativesetfenv( result, env )
  32. end
  33. return result
  34. else
  35. return nil, err
  36. end
  37. else
  38. local result, err = nativeload( x, name )
  39. if result then
  40. if env then
  41. env._ENV = env
  42. nativesetfenv( result, env )
  43. end
  44. return result
  45. else
  46. return nil, err
  47. end
  48. end
  49. end )
  50. if ok then
  51. return p1, p2
  52. else
  53. error( p1, 2 )
  54. end
  55. end
  56. table.unpack = unpack
  57. table.pack = function( ... ) return { n = select( "#", ... ), ... } end
  58.  
  59. if _CC_DISABLE_LUA51_FEATURES then
  60. -- Remove the Lua 5.1 features that will be removed when we update to Lua 5.2, for compatibility testing.
  61. -- See "disable_lua51_functions" in ComputerCraft.cfg
  62. setfenv = nil
  63. getfenv = nil
  64. loadstring = nil
  65. unpack = nil
  66. math.log10 = nil
  67. table.maxn = nil
  68. else
  69. -- Inject a stub for the old bit library
  70. _G.bit = {
  71. bnot = bit32.bnot,
  72. band = bit32.band,
  73. bor = bit32.bor,
  74. bxor = bit32.bxor,
  75. brshift = bit32.arshift,
  76. blshift = bit32.lshift,
  77. blogic_rshift = bit32.rshift
  78. }
  79. end
  80. end
  81.  
  82. if _VERSION == "Lua 5.3" and not bit32 then
  83. -- If we're on Lua 5.3, install the bit32 api from Lua 5.2
  84. -- (Loaded from a string so this file will still parse on <5.3 lua)
  85. load( [[
  86. bit32 = {}
  87.  
  88. function bit32.arshift( n, bits )
  89. if type(n) ~= "number" or type(bits) ~= "number" then
  90. error( "Expected number, number", 2 )
  91. end
  92. return n >> bits
  93. end
  94.  
  95. function bit32.band( m, n )
  96. if type(m) ~= "number" or type(n) ~= "number" then
  97. error( "Expected number, number", 2 )
  98. end
  99. return m & n
  100. end
  101.  
  102. function bit32.bnot( n )
  103. if type(n) ~= "number" then
  104. error( "Expected number", 2 )
  105. end
  106. return ~n
  107. end
  108.  
  109. function bit32.bor( m, n )
  110. if type(m) ~= "number" or type(n) ~= "number" then
  111. error( "Expected number, number", 2 )
  112. end
  113. return m | n
  114. end
  115.  
  116. function bit32.btest( m, n )
  117. if type(m) ~= "number" or type(n) ~= "number" then
  118. error( "Expected number, number", 2 )
  119. end
  120. return (m & n) ~= 0
  121. end
  122.  
  123. function bit32.bxor( m, n )
  124. if type(m) ~= "number" or type(n) ~= "number" then
  125. error( "Expected number, number", 2 )
  126. end
  127. return m ~ n
  128. end
  129.  
  130. function bit32.lshift( n, bits )
  131. if type(n) ~= "number" or type(bits) ~= "number" then
  132. error( "Expected number, number", 2 )
  133. end
  134. return n << bits
  135. end
  136.  
  137. function bit32.rshift( n, bits )
  138. if type(n) ~= "number" or type(bits) ~= "number" then
  139. error( "Expected number, number", 2 )
  140. end
  141. return n >> bits
  142. end
  143. ]] )()
  144. end
  145.  
  146. -- Install lua parts of the os api
  147. function os.version()
  148. return "CraftOS 1.8"
  149. end
  150.  
  151. function os.pullEventRaw( sFilter )
  152. return coroutine.yield( sFilter )
  153. end
  154.  
  155. function os.pullEvent( sFilter )
  156. local eventData = table.pack( os.pullEventRaw( sFilter ) )
  157. if eventData[1] == "terminate" then
  158. error( "Terminated", 0 )
  159. end
  160. return table.unpack( eventData, 1, eventData.n )
  161. end
  162.  
  163. -- Install globals
  164. function sleep( nTime )
  165. if nTime ~= nil and type( nTime ) ~= "number" then
  166. error( "bad argument #1 (expected number, got " .. type( nTime ) .. ")", 2 )
  167. end
  168. local timer = os.startTimer( nTime or 0 )
  169. repeat
  170. local sEvent, param = os.pullEvent( "timer" )
  171. until param == timer
  172. end
  173.  
  174. function write( sText )
  175. if type( sText ) ~= "string" and type( sText ) ~= "number" then
  176. error( "bad argument #1 (expected string or number, got " .. type( sText ) .. ")", 2 )
  177. end
  178.  
  179. local w,h = term.getSize()
  180. local x,y = term.getCursorPos()
  181.  
  182. local nLinesPrinted = 0
  183. local function newLine()
  184. if y + 1 <= h then
  185. term.setCursorPos(1, y + 1)
  186. else
  187. term.setCursorPos(1, h)
  188. term.scroll(1)
  189. end
  190. x, y = term.getCursorPos()
  191. nLinesPrinted = nLinesPrinted + 1
  192. end
  193.  
  194. -- Print the line with proper word wrapping
  195. while string.len(sText) > 0 do
  196. local whitespace = string.match( sText, "^[ \t]+" )
  197. if whitespace then
  198. -- Print whitespace
  199. term.write( whitespace )
  200. x,y = term.getCursorPos()
  201. sText = string.sub( sText, string.len(whitespace) + 1 )
  202. end
  203.  
  204. local newline = string.match( sText, "^\n" )
  205. if newline then
  206. -- Print newlines
  207. newLine()
  208. sText = string.sub( sText, 2 )
  209. end
  210.  
  211. local text = string.match( sText, "^[^ \t\n]+" )
  212. if text then
  213. sText = string.sub( sText, string.len(text) + 1 )
  214. if string.len(text) > w then
  215. -- Print a multiline word
  216. while string.len( text ) > 0 do
  217. if x > w then
  218. newLine()
  219. end
  220. term.write( text )
  221. text = string.sub( text, (w-x) + 2 )
  222. x,y = term.getCursorPos()
  223. end
  224. else
  225. -- Print a word normally
  226. if x + string.len(text) - 1 > w then
  227. newLine()
  228. end
  229. term.write( text )
  230. x,y = term.getCursorPos()
  231. end
  232. end
  233. end
  234.  
  235. return nLinesPrinted
  236. end
  237.  
  238. function print( ... )
  239. local nLinesPrinted = 0
  240. local nLimit = select("#", ... )
  241. for n = 1, nLimit do
  242. local s = tostring( select( n, ... ) )
  243. if n < nLimit then
  244. s = s .. "\t"
  245. end
  246. nLinesPrinted = nLinesPrinted + write( s )
  247. end
  248. nLinesPrinted = nLinesPrinted + write( "\n" )
  249. return nLinesPrinted
  250. end
  251.  
  252. function printError( ... )
  253. local oldColour
  254. if term.isColour() then
  255. oldColour = term.getTextColour()
  256. term.setTextColour( colors.red )
  257. end
  258. print( ... )
  259. if term.isColour() then
  260. term.setTextColour( oldColour )
  261. end
  262. end
  263.  
  264. function read( _sReplaceChar, _tHistory, _fnComplete, _sDefault )
  265. if _sReplaceChar ~= nil and type( _sReplaceChar ) ~= "string" then
  266. error( "bad argument #1 (expected string, got " .. type( _sReplaceChar ) .. ")", 2 )
  267. end
  268. if _tHistory ~= nil and type( _tHistory ) ~= "table" then
  269. error( "bad argument #2 (expected table, got " .. type( _tHistory ) .. ")", 2 )
  270. end
  271. if _fnComplete ~= nil and type( _fnComplete ) ~= "function" then
  272. error( "bad argument #3 (expected function, got " .. type( _fnComplete ) .. ")", 2 )
  273. end
  274. if _sDefault ~= nil and type( _sDefault ) ~= "string" then
  275. error( "bad argument #4 (expected string, got " .. type( _sDefault ) .. ")", 2 )
  276. end
  277. term.setCursorBlink( true )
  278.  
  279. local sLine
  280. if type( _sDefault ) == "string" then
  281. sLine = _sDefault
  282. else
  283. sLine = ""
  284. end
  285. local nHistoryPos
  286. local nPos = #sLine
  287. if _sReplaceChar then
  288. _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  289. end
  290.  
  291. local tCompletions
  292. local nCompletion
  293. local function recomplete()
  294. if _fnComplete and nPos == string.len(sLine) then
  295. tCompletions = _fnComplete( sLine )
  296. if tCompletions and #tCompletions > 0 then
  297. nCompletion = 1
  298. else
  299. nCompletion = nil
  300. end
  301. else
  302. tCompletions = nil
  303. nCompletion = nil
  304. end
  305. end
  306.  
  307. local function uncomplete()
  308. tCompletions = nil
  309. nCompletion = nil
  310. end
  311.  
  312. local w = term.getSize()
  313. local sx = term.getCursorPos()
  314.  
  315. local function redraw( _bClear )
  316. local nScroll = 0
  317. if sx + nPos >= w then
  318. nScroll = (sx + nPos) - w
  319. end
  320.  
  321. local cx,cy = term.getCursorPos()
  322. term.setCursorPos( sx, cy )
  323. local sReplace = (_bClear and " ") or _sReplaceChar
  324. if sReplace then
  325. term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) )
  326. else
  327. term.write( string.sub( sLine, nScroll + 1 ) )
  328. end
  329.  
  330. if nCompletion then
  331. local sCompletion = tCompletions[ nCompletion ]
  332. local oldText, oldBg
  333. if not _bClear then
  334. oldText = term.getTextColor()
  335. oldBg = term.getBackgroundColor()
  336. term.setTextColor( colors.white )
  337. term.setBackgroundColor( colors.gray )
  338. end
  339. if sReplace then
  340. term.write( string.rep( sReplace, string.len( sCompletion ) ) )
  341. else
  342. term.write( sCompletion )
  343. end
  344. if not _bClear then
  345. term.setTextColor( oldText )
  346. term.setBackgroundColor( oldBg )
  347. end
  348. end
  349.  
  350. term.setCursorPos( sx + nPos - nScroll, cy )
  351. end
  352.  
  353. local function clear()
  354. redraw( true )
  355. end
  356.  
  357. recomplete()
  358. redraw()
  359.  
  360. local function acceptCompletion()
  361. if nCompletion then
  362. -- Clear
  363. clear()
  364.  
  365. -- Find the common prefix of all the other suggestions which start with the same letter as the current one
  366. local sCompletion = tCompletions[ nCompletion ]
  367. sLine = sLine .. sCompletion
  368. nPos = string.len( sLine )
  369.  
  370. -- Redraw
  371. recomplete()
  372. redraw()
  373. end
  374. end
  375. while true do
  376. local sEvent, param = os.pullEvent()
  377. if sEvent == "char" then
  378. -- Typed key
  379. clear()
  380. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  381. nPos = nPos + 1
  382. recomplete()
  383. redraw()
  384.  
  385. elseif sEvent == "paste" then
  386. -- Pasted text
  387. clear()
  388. sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  389. nPos = nPos + string.len( param )
  390. recomplete()
  391. redraw()
  392.  
  393. elseif sEvent == "key" then
  394. if param == keys.enter then
  395. -- Enter
  396. if nCompletion then
  397. clear()
  398. uncomplete()
  399. redraw()
  400. end
  401. break
  402.  
  403. elseif param == keys.left then
  404. -- Left
  405. if nPos > 0 then
  406. clear()
  407. nPos = nPos - 1
  408. recomplete()
  409. redraw()
  410. end
  411.  
  412. elseif param == keys.right then
  413. -- Right
  414. if nPos < string.len(sLine) then
  415. -- Move right
  416. clear()
  417. nPos = nPos + 1
  418. recomplete()
  419. redraw()
  420. else
  421. -- Accept autocomplete
  422. acceptCompletion()
  423. end
  424.  
  425. elseif param == keys.up or param == keys.down then
  426. -- Up or down
  427. if nCompletion then
  428. -- Cycle completions
  429. clear()
  430. if param == keys.up then
  431. nCompletion = nCompletion - 1
  432. if nCompletion < 1 then
  433. nCompletion = #tCompletions
  434. end
  435. elseif param == keys.down then
  436. nCompletion = nCompletion + 1
  437. if nCompletion > #tCompletions then
  438. nCompletion = 1
  439. end
  440. end
  441. redraw()
  442.  
  443. elseif _tHistory then
  444. -- Cycle history
  445. clear()
  446. if param == keys.up then
  447. -- Up
  448. if nHistoryPos == nil then
  449. if #_tHistory > 0 then
  450. nHistoryPos = #_tHistory
  451. end
  452. elseif nHistoryPos > 1 then
  453. nHistoryPos = nHistoryPos - 1
  454. end
  455. else
  456. -- Down
  457. if nHistoryPos == #_tHistory then
  458. nHistoryPos = nil
  459. elseif nHistoryPos ~= nil then
  460. nHistoryPos = nHistoryPos + 1
  461. end
  462. end
  463. if nHistoryPos then
  464. sLine = _tHistory[nHistoryPos]
  465. nPos = string.len( sLine )
  466. else
  467. sLine = ""
  468. nPos = 0
  469. end
  470. uncomplete()
  471. redraw()
  472.  
  473. end
  474.  
  475. elseif param == keys.backspace then
  476. -- Backspace
  477. if nPos > 0 then
  478. clear()
  479. sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  480. nPos = nPos - 1
  481. recomplete()
  482. redraw()
  483. end
  484.  
  485. elseif param == keys.home then
  486. -- Home
  487. if nPos > 0 then
  488. clear()
  489. nPos = 0
  490. recomplete()
  491. redraw()
  492. end
  493.  
  494. elseif param == keys.delete then
  495. -- Delete
  496. if nPos < string.len(sLine) then
  497. clear()
  498. sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
  499. recomplete()
  500. redraw()
  501. end
  502.  
  503. elseif param == keys["end"] then
  504. -- End
  505. if nPos < string.len(sLine ) then
  506. clear()
  507. nPos = string.len(sLine)
  508. recomplete()
  509. redraw()
  510. end
  511.  
  512. elseif param == keys.tab then
  513. -- Tab (accept autocomplete)
  514. acceptCompletion()
  515.  
  516. end
  517.  
  518. elseif sEvent == "term_resize" then
  519. -- Terminal resized
  520. w = term.getSize()
  521. redraw()
  522.  
  523. end
  524. end
  525.  
  526. local cx, cy = term.getCursorPos()
  527. term.setCursorBlink( false )
  528. term.setCursorPos( w + 1, cy )
  529. print()
  530.  
  531. return sLine
  532. end
  533.  
  534. loadfile = function( _sFile, _tEnv )
  535. if type( _sFile ) ~= "string" then
  536. error( "bad argument #1 (expected string, got " .. type( _sFile ) .. ")", 2 )
  537. end
  538. if _tEnv ~= nil and type( _tEnv ) ~= "table" then
  539. error( "bad argument #2 (expected table, got " .. type( _tEnv ) .. ")", 2 )
  540. end
  541. local file = fs.open( _sFile, "r" )
  542. if file then
  543. local func, err = load( file.readAll(), "@" .. fs.getName( _sFile ), "t", _tEnv )
  544. file.close()
  545. return func, err
  546. end
  547. return nil, "File not found"
  548. end
  549.  
  550. dofile = function( _sFile )
  551. if type( _sFile ) ~= "string" then
  552. error( "bad argument #1 (expected string, got " .. type( _sFile ) .. ")", 2 )
  553. end
  554. local fnFile, e = loadfile( _sFile, _G )
  555. if fnFile then
  556. return fnFile()
  557. else
  558. error( e, 2 )
  559. end
  560. end
  561.  
  562. -- Install the rest of the OS api
  563. function os.run( _tEnv, _sPath, ... )
  564. if type( _tEnv ) ~= "table" then
  565. error( "bad argument #1 (expected table, got " .. type( _tEnv ) .. ")", 2 )
  566. end
  567. if type( _sPath ) ~= "string" then
  568. error( "bad argument #2 (expected string, got " .. type( _sPath ) .. ")", 2 )
  569. end
  570. local tArgs = table.pack( ... )
  571. local tEnv = _tEnv
  572. setmetatable( tEnv, { __index = _G } )
  573. local fnFile, err = loadfile( _sPath, tEnv )
  574. if fnFile then
  575. local ok, err = pcall( function()
  576. fnFile( table.unpack( tArgs, 1, tArgs.n ) )
  577. end )
  578. if not ok then
  579. if err and err ~= "" then
  580. printError( err )
  581. end
  582. return false
  583. end
  584. return true
  585. end
  586. if err and err ~= "" then
  587. printError( err )
  588. end
  589. return false
  590. end
  591.  
  592. local tAPIsLoading = {}
  593. function os.loadAPI( _sPath )
  594. if type( _sPath ) ~= "string" then
  595. error( "bad argument #1 (expected string, got " .. type( _sPath ) .. ")", 2 )
  596. end
  597. local sName = fs.getName( _sPath )
  598. if sName:sub(-4) == ".lua" then
  599. sName = sName:sub(1,-5)
  600. end
  601. if tAPIsLoading[sName] == true then
  602. printError( "API "..sName.." is already being loaded" )
  603. return false
  604. end
  605. tAPIsLoading[sName] = true
  606.  
  607. local tEnv = {}
  608. setmetatable( tEnv, { __index = _G } )
  609. local fnAPI, err = loadfile( _sPath, tEnv )
  610. if fnAPI then
  611. local ok, err = pcall( fnAPI )
  612. if not ok then
  613. tAPIsLoading[sName] = nil
  614. return error( "Failed to load API " .. sName .. " due to " .. err, 1 )
  615. end
  616. else
  617. tAPIsLoading[sName] = nil
  618. return error( "Failed to load API " .. sName .. " due to " .. err, 1 )
  619. end
  620.  
  621. local tAPI = {}
  622. for k,v in pairs( tEnv ) do
  623. if k ~= "_ENV" then
  624. tAPI[k] = v
  625. end
  626. end
  627.  
  628. _G[sName] = tAPI
  629. tAPIsLoading[sName] = nil
  630. return true
  631. end
  632.  
  633. function os.unloadAPI( _sName )
  634. if type( _sName ) ~= "string" then
  635. error( "bad argument #1 (expected string, got " .. type( _sName ) .. ")", 2 )
  636. end
  637. if _sName ~= "_G" and type(_G[_sName]) == "table" then
  638. _G[_sName] = nil
  639. end
  640. end
  641.  
  642. function os.sleep( nTime )
  643. sleep( nTime )
  644. end
  645.  
  646. local nativeShutdown = os.shutdown
  647. function os.shutdown()
  648. nativeShutdown()
  649. while true do
  650. coroutine.yield()
  651. end
  652. end
  653.  
  654. local nativeReboot = os.reboot
  655. function os.reboot()
  656. nativeReboot()
  657. while true do
  658. coroutine.yield()
  659. end
  660. end
  661.  
  662. -- Install the lua part of the HTTP api (if enabled)
  663. if http then
  664. local nativeHTTPRequest = http.request
  665.  
  666. local methods = {
  667. GET = true, POST = true, HEAD = true,
  668. OPTIONS = true, PUT = true, DELETE = true
  669. }
  670.  
  671. local function checkKey( options, key, ty, opt )
  672. local value = options[key]
  673. local valueTy = type(value)
  674.  
  675. if (value ~= nil or not opt) and valueTy ~= ty then
  676. error(("bad field '%s' (expected %s, got %s"):format(key, ty, valueTy), 4)
  677. end
  678. end
  679.  
  680. local function checkOptions( options, body )
  681. checkKey( options, "url", "string")
  682. if body == false
  683. then checkKey( options, "body", "nil" )
  684. else checkKey( options, "body", "string", not body ) end
  685. checkKey( options, "headers", "table", true )
  686. checkKey( options, "method", "string", true )
  687. checkKey( options, "redirect", "boolean", true )
  688.  
  689. if options.method and not methods[options.method] then
  690. error( "Unsupported HTTP method", 3 )
  691. end
  692. end
  693.  
  694. local function wrapRequest( _url, ... )
  695. local ok, err = nativeHTTPRequest( ... )
  696. if ok then
  697. while true do
  698. local event, param1, param2, param3 = os.pullEvent()
  699. if event == "http_success" and param1 == _url then
  700. return param2
  701. elseif event == "http_failure" and param1 == _url then
  702. return nil, param2, param3
  703. end
  704. end
  705. end
  706. return nil, err
  707. end
  708.  
  709. http.get = function( _url, _headers, _binary)
  710. if type( _url ) == "table" then
  711. checkOptions( _url, false )
  712. return wrapRequest( _url.url, _url )
  713. end
  714.  
  715. if type( _url ) ~= "string" then
  716. error( "bad argument #1 (expected string, got " .. type( _url ) .. ")", 2 )
  717. end
  718. if _headers ~= nil and type( _headers ) ~= "table" then
  719. error( "bad argument #2 (expected table, got " .. type( _headers ) .. ")", 2 )
  720. end
  721. if _binary ~= nil and type( _binary ) ~= "boolean" then
  722. error( "bad argument #3 (expected boolean, got " .. type( _binary ) .. ")", 2 )
  723. end
  724. return wrapRequest( _url, _url, nil, _headers, _binary )
  725. end
  726.  
  727. http.post = function( _url, _post, _headers, _binary)
  728. if type( _url ) == "table" then
  729. checkOptions( _url, true )
  730. return wrapRequest( _url.url, _url )
  731. end
  732.  
  733. if type( _url ) ~= "string" then
  734. error( "bad argument #1 (expected string, got " .. type( _url ) .. ")", 2 )
  735. end
  736. if type( _post ) ~= "string" then
  737. error( "bad argument #2 (expected string, got " .. type( _post ) .. ")", 2 )
  738. end
  739. if _headers ~= nil and type( _headers ) ~= "table" then
  740. error( "bad argument #3 (expected table, got " .. type( _headers ) .. ")", 2 )
  741. end
  742. if _binary ~= nil and type( _binary ) ~= "boolean" then
  743. error( "bad argument #4 (expected boolean, got " .. type( _binary ) .. ")", 2 )
  744. end
  745. return wrapRequest( _url, _url, _post, _headers, _binary )
  746. end
  747.  
  748. http.request = function( _url, _post, _headers, _binary )
  749. local url
  750. if type( _url ) == "table" then
  751. checkOptions( _url )
  752. url = _url.url
  753. else
  754. if type( _url ) ~= "string" then
  755. error( "bad argument #1 (expected string, got " .. type( _url ) .. ")", 2 )
  756. end
  757. if _post ~= nil and type( _post ) ~= "string" then
  758. error( "bad argument #2 (expected string, got " .. type( _post ) .. ")", 2 )
  759. end
  760. if _headers ~= nil and type( _headers ) ~= "table" then
  761. error( "bad argument #3 (expected table, got " .. type( _headers ) .. ")", 2 )
  762. end
  763. if _binary ~= nil and type( _binary ) ~= "boolean" then
  764. error( "bad argument #4 (expected boolean, got " .. type( _binary ) .. ")", 2 )
  765. end
  766.  
  767. url = _url.url
  768. end
  769.  
  770. local ok, err = nativeHTTPRequest( _url, _post, _headers, _binary )
  771. if not ok then
  772. os.queueEvent( "http_failure", url, err )
  773. end
  774. return ok, err
  775. end
  776.  
  777. local nativeCheckURL = http.checkURL
  778. http.checkURLAsync = nativeCheckURL
  779. http.checkURL = function( _url )
  780. local ok, err = nativeCheckURL( _url )
  781. if not ok then return ok, err end
  782.  
  783. while true do
  784. local event, url, ok, err = os.pullEvent( "http_check" )
  785. if url == _url then return ok, err end
  786. end
  787. end
  788.  
  789. local nativeWebsocket = http.websocket
  790. http.websocketAsync = nativeWebsocket
  791. http.websocket = function( _url, _headers )
  792. if type( _url ) ~= "string" then
  793. error( "bad argument #1 (expected string, got " .. type( _url ) .. ")", 2 )
  794. end
  795. if _headers ~= nil and type( _headers ) ~= "table" then
  796. error( "bad argument #2 (expected table, got " .. type( _headers ) .. ")", 2 )
  797. end
  798. local ok, err = nativeWebsocket( _url, _headers )
  799. if not ok then return ok, err end
  800.  
  801. while true do
  802. local event, url, param = os.pullEvent( )
  803. if event == "websocket_success" and url == _url then
  804. return param
  805. elseif event == "websocket_failure" and url == _url then
  806. return false, param
  807. end
  808. end
  809. end
  810. end
  811.  
  812. -- Install the lua part of the FS api
  813. local tEmpty = {}
  814. function fs.complete( sPath, sLocation, bIncludeFiles, bIncludeDirs )
  815. if type( sPath ) ~= "string" then
  816. error( "bad argument #1 (expected string, got " .. type( sPath ) .. ")", 2 )
  817. end
  818. if type( sLocation ) ~= "string" then
  819. error( "bad argument #2 (expected string, got " .. type( sLocation ) .. ")", 2 )
  820. end
  821. if bIncludeFiles ~= nil and type( bIncludeFiles ) ~= "boolean" then
  822. error( "bad argument #3 (expected boolean, got " .. type( bIncludeFiles ) .. ")", 2 )
  823. end
  824. if bIncludeDirs ~= nil and type( bIncludeDirs ) ~= "boolean" then
  825. error( "bad argument #4 (expected boolean, got " .. type( bIncludeDirs ) .. ")", 2 )
  826. end
  827. bIncludeFiles = (bIncludeFiles ~= false)
  828. bIncludeDirs = (bIncludeDirs ~= false)
  829. local sDir = sLocation
  830. local nStart = 1
  831. local nSlash = string.find( sPath, "[/\\]", nStart )
  832. if nSlash == 1 then
  833. sDir = ""
  834. nStart = 2
  835. end
  836. local sName
  837. while not sName do
  838. local nSlash = string.find( sPath, "[/\\]", nStart )
  839. if nSlash then
  840. local sPart = string.sub( sPath, nStart, nSlash - 1 )
  841. sDir = fs.combine( sDir, sPart )
  842. nStart = nSlash + 1
  843. else
  844. sName = string.sub( sPath, nStart )
  845. end
  846. end
  847.  
  848. if fs.isDir( sDir ) then
  849. local tResults = {}
  850. if bIncludeDirs and sPath == "" then
  851. table.insert( tResults, "." )
  852. end
  853. if sDir ~= "" then
  854. if sPath == "" then
  855. table.insert( tResults, (bIncludeDirs and "..") or "../" )
  856. elseif sPath == "." then
  857. table.insert( tResults, (bIncludeDirs and ".") or "./" )
  858. end
  859. end
  860. local tFiles = fs.list( sDir )
  861. for n=1,#tFiles do
  862. local sFile = tFiles[n]
  863. if #sFile >= #sName and string.sub( sFile, 1, #sName ) == sName then
  864. local bIsDir = fs.isDir( fs.combine( sDir, sFile ) )
  865. local sResult = string.sub( sFile, #sName + 1 )
  866. if bIsDir then
  867. table.insert( tResults, sResult .. "/" )
  868. if bIncludeDirs and #sResult > 0 then
  869. table.insert( tResults, sResult )
  870. end
  871. else
  872. if bIncludeFiles and #sResult > 0 then
  873. table.insert( tResults, sResult )
  874. end
  875. end
  876. end
  877. end
  878. return tResults
  879. end
  880. return tEmpty
  881. end
  882.  
  883. -- Load APIs
  884. local bAPIError = false
  885. local tApis = fs.list( "rom/apis" )
  886. for n,sFile in ipairs( tApis ) do
  887. if string.sub( sFile, 1, 1 ) ~= "." then
  888. local sPath = fs.combine( "rom/apis", sFile )
  889. if not fs.isDir( sPath ) then
  890. if not os.loadAPI( sPath ) then
  891. bAPIError = true
  892. end
  893. end
  894. end
  895. end
  896.  
  897. if turtle and fs.isDir( "rom/apis/turtle" ) then
  898. -- Load turtle APIs
  899. local tApis = fs.list( "rom/apis/turtle" )
  900. for n,sFile in ipairs( tApis ) do
  901. if string.sub( sFile, 1, 1 ) ~= "." then
  902. local sPath = fs.combine( "rom/apis/turtle", sFile )
  903. if not fs.isDir( sPath ) then
  904. if not os.loadAPI( sPath ) then
  905. bAPIError = true
  906. end
  907. end
  908. end
  909. end
  910. end
  911.  
  912. if pocket and fs.isDir( "rom/apis/pocket" ) then
  913. -- Load pocket APIs
  914. local tApis = fs.list( "rom/apis/pocket" )
  915. for n,sFile in ipairs( tApis ) do
  916. if string.sub( sFile, 1, 1 ) ~= "." then
  917. local sPath = fs.combine( "rom/apis/pocket", sFile )
  918. if not fs.isDir( sPath ) then
  919. if not os.loadAPI( sPath ) then
  920. bAPIError = true
  921. end
  922. end
  923. end
  924. end
  925. end
  926.  
  927. if commands and fs.isDir( "rom/apis/command" ) then
  928. -- Load command APIs
  929. if os.loadAPI( "rom/apis/command/commands.lua" ) then
  930. -- Add a special case-insensitive metatable to the commands api
  931. local tCaseInsensitiveMetatable = {
  932. __index = function( table, key )
  933. local value = rawget( table, key )
  934. if value ~= nil then
  935. return value
  936. end
  937. if type(key) == "string" then
  938. local value = rawget( table, string.lower(key) )
  939. if value ~= nil then
  940. return value
  941. end
  942. end
  943. return nil
  944. end
  945. }
  946. setmetatable( commands, tCaseInsensitiveMetatable )
  947. setmetatable( commands.async, tCaseInsensitiveMetatable )
  948.  
  949. -- Add global "exec" function
  950. exec = commands.exec
  951. else
  952. bAPIError = true
  953. end
  954. end
  955.  
  956. if bAPIError then
  957. print( "Press any key to continue" )
  958. os.pullEvent( "key" )
  959. term.clear()
  960. term.setCursorPos( 1,1 )
  961. end
  962.  
  963. -- Set default settings
  964. settings.set( "shell.allow_startup", true )
  965. settings.set( "shell.allow_disk_startup", (commands == nil) )
  966. settings.set( "shell.autocomplete", true )
  967. settings.set( "edit.autocomplete", true )
  968. settings.set( "edit.default_extension", "lua" )
  969. settings.set( "paint.default_extension", "nfp" )
  970. settings.set( "lua.autocomplete", true )
  971. settings.set( "list.show_hidden", false )
  972. if term.isColour() then
  973. settings.set( "bios.use_multishell", true )
  974. end
  975. if _CC_DEFAULT_SETTINGS then
  976. for sPair in string.gmatch( _CC_DEFAULT_SETTINGS, "[^,]+" ) do
  977. local sName, sValue = string.match( sPair, "([^=]*)=(.*)" )
  978. if sName and sValue then
  979. local value
  980. if sValue == "true" then
  981. value = true
  982. elseif sValue == "false" then
  983. value = false
  984. elseif sValue == "nil" then
  985. value = nil
  986. elseif tonumber(sValue) then
  987. value = tonumber(sValue)
  988. else
  989. value = sValue
  990. end
  991. if value ~= nil then
  992. settings.set( sName, value )
  993. else
  994. settings.unset( sName )
  995. end
  996. end
  997. end
  998. end
  999.  
  1000. -- Load user settings
  1001. if fs.exists( ".settings" ) then
  1002. settings.load( ".settings" )
  1003. end
  1004.  
  1005. -- Run the shell
  1006. local ok, err = pcall( function()
  1007. parallel.waitForAny(
  1008. function()
  1009. local sShell
  1010. if term.isColour() and settings.get( "bios.use_multishell" ) then
  1011. sShell = "rom/programs/advanced/multishell.lua"
  1012. else
  1013. sShell = "rom/programs/shell.lua"
  1014. end
  1015. os.run( {}, sShell )
  1016. os.run( {}, "rom/programs/shutdown.lua" )
  1017. end,
  1018. function()
  1019. rednet.run()
  1020. end )
  1021. end )
  1022.  
  1023. -- If the shell errored, let the user read it.
  1024. term.redirect( term.native() )
  1025. if not ok then
  1026. printError( err )
  1027. pcall( function()
  1028. term.setCursorBlink( false )
  1029. print( "Press any key to continue" )
  1030. os.pullEvent( "key" )
  1031. end )
  1032. end
  1033.  
  1034. -- End
  1035. os.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment