forked from Hyperion/HyperionOS
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
429a1489d2 | ||
|
|
05ca0f10a2 | ||
|
|
125baaebf5 | ||
|
|
211b7fc75b | ||
|
|
faf4f27987 | ||
|
|
d70328c224 | ||
|
|
4e7cec2ce1 | ||
|
|
d4f74e950c | ||
|
|
7ddc527e00 | ||
|
|
3b86113425 | ||
|
|
bb28d24674 | ||
|
|
3a33bd0264 | ||
|
|
4ed82a23a1 | ||
|
|
242762ab6b | ||
|
|
10aa0c62ff | ||
|
|
56646625c0 | ||
|
|
49c9e5e37c | ||
|
|
f7b64c11b7 | ||
|
|
59e09a5995 | ||
|
|
ab0a10105c | ||
|
|
8c18d89489 | ||
|
|
22b5021e9d | ||
|
|
462c74686a | ||
|
|
0072547beb | ||
|
|
041663e1e8 | ||
|
|
7deefc83ca | ||
|
|
fcac0ff8d8 | ||
|
|
df62414229 | ||
|
|
d98555328b | ||
|
|
055dd4e606 | ||
|
|
5755dd9cbe | ||
|
|
61cbb91db2 | ||
|
|
b79492dbb8 | ||
|
|
bc87c53427 | ||
|
|
8e11faf9ec | ||
|
|
0ea42f9454 | ||
|
|
d0f26a937f | ||
|
|
9b338328f0 | ||
|
|
030e5bfd96 | ||
|
|
099638c735 | ||
|
|
4f9eebade2 | ||
|
|
5172da1b5b | ||
|
|
d08935b68a | ||
|
|
45b46cf3c4 | ||
|
|
aeea68bc9b | ||
|
|
f983b13d56 | ||
|
|
03c5b106c4 | ||
|
|
08323e00ff | ||
|
|
5e3cdbe40c | ||
|
|
8762b8f022 | ||
|
|
677b2cccec | ||
|
|
a5e8624368 | ||
|
|
bbda3b3937 | ||
|
|
585d39bec2 | ||
|
|
b08b14763a | ||
|
|
de6696003b | ||
|
|
9220281365 | ||
|
|
813ddabd9d | ||
|
|
5177639d71 | ||
|
|
528b4f31bd | ||
|
|
60162c7c57 | ||
|
|
18f5c454bb | ||
|
|
849ecb7dd6 | ||
|
|
e41bd6bee7 | ||
|
|
359198c1ea | ||
|
|
beebf01223 | ||
|
|
2d4ea1bbf4 | ||
|
|
ea3a7e99a7 | ||
|
|
be0fe5dc5a | ||
|
|
12669d9f82 | ||
|
|
f12159bfb9 | ||
|
|
1590e1f3f7 | ||
|
|
a69f945b91 | ||
|
|
7da67899db | ||
|
|
62e032e4c5 | ||
|
|
6fefa2d9ff | ||
|
|
bb354cc706 | ||
|
|
fabc061731 | ||
|
|
82c3e2b346 | ||
|
|
e2e1d5b8a5 | ||
|
|
9342b9b2b3 | ||
|
|
9a7db6c243 | ||
|
|
c7545e6947 | ||
|
|
b7f52dd17b | ||
|
|
eb5bed0f09 | ||
|
|
1827a463eb | ||
|
|
b532a63fc6 | ||
|
|
4e5a4172bf | ||
|
|
31ce894fda | ||
|
|
16c900de84 | ||
|
|
413afd96de | ||
|
|
a0a0ac69d4 | ||
|
|
17453983ad | ||
|
|
a6550aa069 | ||
|
|
02e7b3897c | ||
|
|
34b89a8e34 | ||
|
|
0eabfebd0f | ||
|
|
5b2e5eac65 | ||
|
|
415064480a | ||
|
|
f00453f703 | ||
|
|
ab1e847d1c | ||
|
|
62a03bfe6b | ||
|
|
e77a8b3636 | ||
|
|
6bb7f03a3e | ||
|
|
8798a2f4fe | ||
|
|
a6d2f6dca7 | ||
|
|
b015d5880a | ||
|
|
6694711423 | ||
|
|
40c97ca000 | ||
|
|
dd2437d4af | ||
|
|
d026cfbb03 | ||
|
|
aad7efd055 |
Vendored
+6
-1
@@ -5,6 +5,11 @@
|
|||||||
"syscall",
|
"syscall",
|
||||||
"printf",
|
"printf",
|
||||||
"printInline",
|
"printInline",
|
||||||
"toHex"
|
"toHex",
|
||||||
|
"loadcstr",
|
||||||
|
"userinput"
|
||||||
|
],
|
||||||
|
"Lua.diagnostics.disable": [
|
||||||
|
"need-check-nil"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,202 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2025 Astronand
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
|
||||||
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
|
||||||
following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial
|
1. Definitions.
|
||||||
portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
[](https://pinestore.cc/projects/225/hyperionos)
|
||||||
# HyperionOS
|
# HyperionOS
|
||||||
|
|
||||||
HyperionOS is a modular, hybrid kernel operating system written entirely in Lua. It features a custom task scheduler, virtual filesystem, syscall interface, and separates core functionality from user-space services.
|
HyperionOS is a modular, hybrid kernel operating system written entirely in Lua. It features a custom task scheduler, virtual filesystem, syscall interface, and separates core functionality from user-space services.
|
||||||
@@ -14,8 +15,10 @@ See `building.md`.
|
|||||||
|
|
||||||
- Functionality is split into kernel modules (`.kmod`)
|
- Functionality is split into kernel modules (`.kmod`)
|
||||||
- Task-based lightweight thread/task preemptive scheduler with process isolation and IPC support
|
- Task-based lightweight thread/task preemptive scheduler with process isolation and IPC support
|
||||||
- Virtual filesystem unified interface for disk, RAM, and virtual filesystems
|
- Virtual filesystem
|
||||||
- TTY & Shell
|
- TTY & Shell
|
||||||
|
- Sockets
|
||||||
|
-
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -47,6 +50,12 @@ Add your name to `contributors.md` when your PR is merged.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Information
|
||||||
|
|
||||||
|
Trello: https://trello.com/b/8dryrlMi
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under the MIT License. See the LICENSE file for details.
|
This project is licensed under the MIT License. See the LICENSE file for details.
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
local args = {...}
|
|
||||||
local name = syscall.getTask(syscall.getpid()).name
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
local filePath = (args[1] or "")
|
|
||||||
if filePath:sub(1, 1) ~= "/" then
|
|
||||||
filePath = syscall.getcwd().."/"..filePath
|
|
||||||
end
|
|
||||||
|
|
||||||
if not fs.exists(filePath) and args[1] then
|
|
||||||
print(name..": Cannot access '"..args[1].."': No such file.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local file = 0
|
|
||||||
if args[1] then
|
|
||||||
file = syscall.open(filePath, "r")
|
|
||||||
end
|
|
||||||
local content=""
|
|
||||||
while content~=nil or file == 0 do
|
|
||||||
content=syscall.read(file, 1024)
|
|
||||||
printInline(content)
|
|
||||||
end
|
|
||||||
syscall.close(file)
|
|
||||||
print("")
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
syscall.devctl(1,"clear")
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
local args = {...}
|
|
||||||
print(table.concat(args, " "))
|
|
||||||
@@ -1,292 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
-- help: display command reference with paged scrolling
|
|
||||||
|
|
||||||
local COMMANDS = {
|
|
||||||
-- {name, usage, description, {flags...}}
|
|
||||||
-- flags: {flag, desc}
|
|
||||||
{
|
|
||||||
name = "cat",
|
|
||||||
usage = "cat [file]",
|
|
||||||
desc = "Print file contents to stdout. Reads from stdin if no file given.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "cd",
|
|
||||||
usage = "cd [dir]",
|
|
||||||
desc = "Change working directory. Use '-' to return to previous directory.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "clear",
|
|
||||||
usage = "clear",
|
|
||||||
desc = "Clear the terminal screen.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "echo",
|
|
||||||
usage = "echo [text...]",
|
|
||||||
desc = "Print arguments to stdout.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "hfetch",
|
|
||||||
usage = "hfetch",
|
|
||||||
desc = "Display system information in a neofetch-style layout.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "id",
|
|
||||||
usage = "id [username]",
|
|
||||||
desc = "Print user identity (uid, gid). Defaults to current user.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "login",
|
|
||||||
usage = "login",
|
|
||||||
desc = "System login prompt. Launched automatically at boot.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "ls",
|
|
||||||
usage = "ls [-alh] [dir]",
|
|
||||||
desc = "List directory contents.",
|
|
||||||
flags = {
|
|
||||||
{"-a", "Show hidden files (starting with .)"},
|
|
||||||
{"-l", "Long format: permissions, owner, size"},
|
|
||||||
{"-h", "Human-readable file sizes"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "lsusers",
|
|
||||||
usage = "lsusers",
|
|
||||||
desc = "List all user accounts with uid, gid, home, and shell.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "lua",
|
|
||||||
usage = "lua",
|
|
||||||
desc = "Interactive Lua REPL prompt.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "mkdir",
|
|
||||||
usage = "mkdir <dir>",
|
|
||||||
desc = "Create a directory.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "passwd",
|
|
||||||
usage = "passwd [username]",
|
|
||||||
desc = "Change a user password. Non-root must verify current password first.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "ps",
|
|
||||||
usage = "ps",
|
|
||||||
desc = "List running tasks with pid, user, name, and status.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "pwd",
|
|
||||||
usage = "pwd",
|
|
||||||
desc = "Print current working directory.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "su",
|
|
||||||
usage = "su [username]",
|
|
||||||
desc = "Switch user. Defaults to root. Root can switch without a password.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "sudo",
|
|
||||||
usage = "sudo [-u user] <command> [args...]",
|
|
||||||
desc = "Run a command as another user (default root). Authenticates as current user.",
|
|
||||||
flags = {
|
|
||||||
{"-u user", "Run as the specified user (name or uid)"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "sysdump",
|
|
||||||
usage = "sysdump",
|
|
||||||
desc = "List all registered kernel syscalls.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "useradd",
|
|
||||||
usage = "useradd [-p pw] [-g gid] [-d home] [-s shell] [-M] <username>",
|
|
||||||
desc = "Create a new user account.",
|
|
||||||
flags = {
|
|
||||||
{"-p pw", "Set password (prompted interactively if omitted)"},
|
|
||||||
{"-g gid", "Set primary group id"},
|
|
||||||
{"-d home", "Set home directory (default: /home/username)"},
|
|
||||||
{"-s shell", "Set login shell (default: /bin/hysh)"},
|
|
||||||
{"-M", "Do not create home directory"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "userdel",
|
|
||||||
usage = "userdel [-r] <username>",
|
|
||||||
desc = "Delete a user account.",
|
|
||||||
flags = {
|
|
||||||
{"-r", "Also recursively remove the user's home directory"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "usermod",
|
|
||||||
usage = "usermod [-l name] [-p pw] [-g gid] [-d home] [-s shell] [-L] [-U] <username>",
|
|
||||||
desc = "Modify an existing user account.",
|
|
||||||
flags = {
|
|
||||||
{"-l name", "Rename the user"},
|
|
||||||
{"-p pw", "Set new password"},
|
|
||||||
{"-g gid", "Change primary group id"},
|
|
||||||
{"-d home", "Change home directory"},
|
|
||||||
{"-s shell", "Change login shell"},
|
|
||||||
{"-L", "Lock the account (disable login)"},
|
|
||||||
{"-U", "Unlock the account"},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "whoami",
|
|
||||||
usage = "whoami",
|
|
||||||
desc = "Print the current username.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name = "yes",
|
|
||||||
usage = "yes [text]",
|
|
||||||
desc = "Repeatedly print 'y' (or given text) until interrupted with Ctrl+C.",
|
|
||||||
flags = {}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
-- Build lines to display
|
|
||||||
local C_HEAD = 7 -- yellow (section headers)
|
|
||||||
local C_CMD = 5 -- cyan (command name)
|
|
||||||
local C_USAGE = 1 -- white (usage line)
|
|
||||||
local C_DESC = 13 -- grey (description)
|
|
||||||
local C_FLAG = 3 -- green (flag name)
|
|
||||||
local C_DIM = 12 -- dark grey
|
|
||||||
|
|
||||||
-- Each entry is {text, color}
|
|
||||||
local lines = {}
|
|
||||||
local function push(text, col) lines[#lines+1] = {text, col or 1} end
|
|
||||||
|
|
||||||
push("HyperionOS Command Reference", C_HEAD)
|
|
||||||
push(string.rep("=", 50), C_DIM)
|
|
||||||
push("", 1)
|
|
||||||
|
|
||||||
local args = {...}
|
|
||||||
local filter = args[1] -- optional: help <command>
|
|
||||||
|
|
||||||
local function addCmd(cmd)
|
|
||||||
push(cmd.name, C_CMD)
|
|
||||||
push(" Usage: " .. cmd.usage, C_USAGE)
|
|
||||||
push(" " .. cmd.desc, C_DESC)
|
|
||||||
if #cmd.flags > 0 then
|
|
||||||
for _, f in ipairs(cmd.flags) do
|
|
||||||
push(" " .. f[1], C_FLAG)
|
|
||||||
push(" " .. f[2], C_DESC)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
push("", 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
if filter then
|
|
||||||
local found = false
|
|
||||||
for _, cmd in ipairs(COMMANDS) do
|
|
||||||
if cmd.name == filter then addCmd(cmd); found = true; break end
|
|
||||||
end
|
|
||||||
if not found then
|
|
||||||
push("help: unknown command '" .. filter .. "'", 2)
|
|
||||||
push("Run 'help' with no arguments for the full list.", C_DESC)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
push("Run 'help <command>' for details on a specific command.", C_DESC)
|
|
||||||
push("", 1)
|
|
||||||
for _, cmd in ipairs(COMMANDS) do addCmd(cmd) end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Pager
|
|
||||||
local sizeStr = syscall.devctl(1, "size")
|
|
||||||
local screenW = tonumber(sizeStr:sub(1, sizeStr:find(";")-1)) or 51
|
|
||||||
local screenH = tonumber(sizeStr:sub(sizeStr:find(";")+1)) or 19
|
|
||||||
local pageSize = screenH - 2 -- reserve bottom row for status bar
|
|
||||||
|
|
||||||
local scroll = 0
|
|
||||||
local totalLines = #lines
|
|
||||||
local dirty = true
|
|
||||||
|
|
||||||
local function render()
|
|
||||||
syscall.devctl(1, "clear")
|
|
||||||
syscall.devctl(1, "spos", 1, 1)
|
|
||||||
for row = 1, pageSize do
|
|
||||||
local li = scroll + row
|
|
||||||
if li <= totalLines then
|
|
||||||
local text, col = lines[li][1], lines[li][2]
|
|
||||||
syscall.devctl(1, "sfgc", col)
|
|
||||||
-- truncate to screen width
|
|
||||||
if #text > screenW then text = text:sub(1, screenW) end
|
|
||||||
syscall.write(1, text .. "\n")
|
|
||||||
else
|
|
||||||
syscall.write(1, "\n")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- status bar
|
|
||||||
syscall.devctl(1, "sfgc", 16)
|
|
||||||
syscall.devctl(1, "sbgc", 13)
|
|
||||||
local pct = math.floor(math.min(100, (scroll + pageSize) / totalLines * 100))
|
|
||||||
local status = string.format(" help -- line %d/%d (%d%%) [up/down: scroll q: quit] ",
|
|
||||||
scroll + 1, totalLines, pct)
|
|
||||||
if #status > screenW then status = status:sub(1, screenW) end
|
|
||||||
syscall.devctl(1, "spos", 1, screenH)
|
|
||||||
syscall.write(1, status .. string.rep(" ", screenW - #status))
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
syscall.devctl(1, "sbgc", 16)
|
|
||||||
dirty = false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- If output fits on one screen, just print without pager
|
|
||||||
if totalLines <= pageSize then
|
|
||||||
syscall.devctl(1, "clear")
|
|
||||||
syscall.devctl(1, "spos", 1, 1)
|
|
||||||
for _, line in ipairs(lines) do
|
|
||||||
syscall.devctl(1, "sfgc", line[2])
|
|
||||||
syscall.write(1, line[1] .. "\n")
|
|
||||||
end
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
render()
|
|
||||||
while true do
|
|
||||||
local ch = syscall.read(0)
|
|
||||||
if not ch or ch == "" then
|
|
||||||
-- idle
|
|
||||||
elseif ch == "q" or ch == "Q" then
|
|
||||||
break
|
|
||||||
elseif ch == "\17" then -- up arrow
|
|
||||||
if scroll > 0 then
|
|
||||||
scroll = scroll - 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif ch == "\18" then -- down arrow
|
|
||||||
if scroll + pageSize < totalLines then
|
|
||||||
scroll = scroll + 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif ch == "\19" then -- left / page up (reuse left arrow)
|
|
||||||
scroll = math.max(0, scroll - pageSize)
|
|
||||||
dirty = true
|
|
||||||
elseif ch == "\20" then -- right / page down
|
|
||||||
scroll = math.min(totalLines - pageSize, scroll + pageSize)
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
if dirty then render() end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Restore screen
|
|
||||||
syscall.devctl(1, "clear")
|
|
||||||
syscall.devctl(1, "spos", 1, 1)
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
syscall.devctl(1, "sbgc", 16)
|
|
||||||
@@ -1,299 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
syscall.open("/dev/tty/TTY1","r") --stdin (Device 0)
|
|
||||||
syscall.open("/dev/tty/TTY1","w") --stdout (Device 1)
|
|
||||||
syscall.open("/dev/null","w") --stderr (device 2)
|
|
||||||
|
|
||||||
local success, errorMsg = xpcall(function()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
|
|
||||||
syscall.devctl(1,"clear")
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.devctl(1,"spos",1,1)
|
|
||||||
print("HyperionOS hysh Shell")
|
|
||||||
|
|
||||||
local userhost = (syscall.getUsername() or "Unknown").."@"..(syscall.getHostname() or "Unknown")
|
|
||||||
local commandHistory = {}
|
|
||||||
local terminate = false
|
|
||||||
syscall.setEnviron("SHELL","rtbash")
|
|
||||||
syscall.setEnviron("PATH","/bin/")
|
|
||||||
syscall.chdir("/")
|
|
||||||
local oldWD = ""
|
|
||||||
|
|
||||||
for i = 1, 16 do
|
|
||||||
syscall.devctl(1,"sbgc",i)
|
|
||||||
printInline(" ");
|
|
||||||
end
|
|
||||||
print("\n")
|
|
||||||
|
|
||||||
syscall.sigcatch(function(sig)
|
|
||||||
if sig == 1 then
|
|
||||||
terminate = true
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
local builtinCmds = {}
|
|
||||||
|
|
||||||
builtinCmds.cd = function(path)
|
|
||||||
local cwd = syscall.getcwd()
|
|
||||||
local dirIn = (path or "")
|
|
||||||
if dirIn == "-" then
|
|
||||||
if oldWD == "" then
|
|
||||||
print("hysh-cd: No previous working directory set.")
|
|
||||||
else
|
|
||||||
print(oldWD)
|
|
||||||
syscall.chdir(oldWD)
|
|
||||||
oldWD = cwd
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local dirInMod = dirIn
|
|
||||||
if dirIn:sub(1, 1) ~= "/" then dirInMod = cwd .. "/" .. dirIn end
|
|
||||||
local parts = {}
|
|
||||||
for part in dirInMod:gmatch("[^/]+") do
|
|
||||||
if part == ".." then
|
|
||||||
if #parts > 0 then table.remove(parts) end
|
|
||||||
elseif part ~= "." and part ~= "" then
|
|
||||||
table.insert(parts, part)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local normDir = "/" .. table.concat(parts, "/")
|
|
||||||
if normDir:sub(#normDir, #normDir) ~= "/" then normDir = normDir .. "/" end
|
|
||||||
|
|
||||||
if not fs.isDir(normDir) then
|
|
||||||
print("hysh-cd: "..dirIn..": No such directory.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
oldWD = cwd
|
|
||||||
syscall.chdir(normDir)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local function getUserInput()
|
|
||||||
syscall.devctl(1,"sfgc",3)
|
|
||||||
syscall.write(1, userhost)
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.write(1, ":")
|
|
||||||
syscall.devctl(1,"sfgc",10)
|
|
||||||
syscall.write(1, syscall.getcwd())
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.write(1, "$ ")
|
|
||||||
local curOffsetStr = syscall.devctl(1, "gpos")
|
|
||||||
local curOffsetX = tonumber(curOffsetStr:sub(1, curOffsetStr:find(";")-1))
|
|
||||||
local curOffsetY = tonumber(curOffsetStr:sub(curOffsetStr:find(";")+1))
|
|
||||||
|
|
||||||
local input = ""
|
|
||||||
local blinkState = false
|
|
||||||
local cursorPos = 1
|
|
||||||
local history = 0
|
|
||||||
local dirty = true -- redraw on first iteration
|
|
||||||
|
|
||||||
local function redraw()
|
|
||||||
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
|
|
||||||
-- text before cursor
|
|
||||||
syscall.write(1, string.sub(input, 1, cursorPos-1))
|
|
||||||
-- cursor character (inverted if blinking on)
|
|
||||||
if blinkState then
|
|
||||||
syscall.devctl(1,"sfgc",16)
|
|
||||||
syscall.devctl(1,"sbgc",1)
|
|
||||||
end
|
|
||||||
if cursorPos > #input then
|
|
||||||
syscall.write(1, " ")
|
|
||||||
else
|
|
||||||
syscall.write(1, string.sub(input, cursorPos, cursorPos))
|
|
||||||
end
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.devctl(1,"sbgc",16)
|
|
||||||
-- text after cursor + trailing space to erase old chars
|
|
||||||
syscall.write(1, string.sub(input, cursorPos+1) .. " ")
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
local key = syscall.read(0)
|
|
||||||
if key and key ~= "" then
|
|
||||||
if key == "\19" then -- left arrow
|
|
||||||
if cursorPos > 1 then
|
|
||||||
cursorPos = cursorPos - 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif key == "\20" then -- right arrow
|
|
||||||
if cursorPos <= #input then
|
|
||||||
cursorPos = cursorPos + 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif key == "\17" then -- up arrow
|
|
||||||
if history < #commandHistory then
|
|
||||||
history = history + 1
|
|
||||||
input = commandHistory[#commandHistory - history + 1]
|
|
||||||
cursorPos = #input + 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif key == "\18" then -- down arrow
|
|
||||||
if history > 1 then
|
|
||||||
history = history - 1
|
|
||||||
input = commandHistory[#commandHistory - history + 1]
|
|
||||||
cursorPos = #input + 1
|
|
||||||
dirty = true
|
|
||||||
elseif history == 1 then
|
|
||||||
history = 0
|
|
||||||
input = ""
|
|
||||||
cursorPos = 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif key == "\b" then
|
|
||||||
if cursorPos > 1 then
|
|
||||||
input = string.sub(input, 1, cursorPos-2) .. string.sub(input, cursorPos)
|
|
||||||
cursorPos = cursorPos - 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
elseif key == "\n" then
|
|
||||||
-- redraw cleanly with no cursor highlight before committing
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.devctl(1,"sbgc",16)
|
|
||||||
syscall.devctl(1,"spos",curOffsetX,curOffsetY)
|
|
||||||
syscall.write(1, input .. " \n")
|
|
||||||
return input
|
|
||||||
else
|
|
||||||
input = string.sub(input, 1, cursorPos-1) .. key .. string.sub(input, cursorPos)
|
|
||||||
cursorPos = cursorPos + 1
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- cursor blink
|
|
||||||
local curBlink = ((math.floor(syscall.getUptime() / 500) % 2) == 0)
|
|
||||||
if curBlink ~= blinkState then
|
|
||||||
blinkState = curBlink
|
|
||||||
dirty = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if dirty then
|
|
||||||
redraw()
|
|
||||||
dirty = false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function runCommand(command)
|
|
||||||
do
|
|
||||||
local func = load("return " .. command, "@equation", "t", {})
|
|
||||||
if func then
|
|
||||||
local success, result = pcall(func)
|
|
||||||
if success and type(result) == "number" then
|
|
||||||
print(result)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
terminate = false
|
|
||||||
local args = string.split(command, " ")
|
|
||||||
if builtinCmds[args[1]] then
|
|
||||||
local success, msg = pcall(builtinCmds[args[1]], table.unpack(args, 2))
|
|
||||||
if not success then
|
|
||||||
local errSL = string.sub(msg, string.find(msg, "]") + 2)
|
|
||||||
syscall.devctl(1,"sfgc",2)
|
|
||||||
printInline(args[1]..": Program runtime error on line ")
|
|
||||||
print(string.sub(errSL, 1, string.find(errSL, ":") - 1))
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
print(string.sub(errSL, string.find(errSL, ":") + 1))
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local cmdPath = ""
|
|
||||||
if string.find(args[1], "/") then
|
|
||||||
if fs.exists(args[1]) then
|
|
||||||
cmdPath = args[1]
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local paths = string.split(syscall.getEnviron("PATH"), ":")
|
|
||||||
for _, path in pairs(paths) do
|
|
||||||
if fs.exists(path..args[1]) then
|
|
||||||
cmdPath = path..args[1]
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if cmdPath == "" then
|
|
||||||
print(args[1]..": Command not found")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local progName = string.sub(cmdPath, #cmdPath - string.find(string.reverse(cmdPath), "/") + 2)
|
|
||||||
|
|
||||||
local text = fs.readAllText(cmdPath)
|
|
||||||
local program, err = load(text, progName)
|
|
||||||
if not program then
|
|
||||||
local errSL = string.sub(err, string.find(err, ":") + 1)
|
|
||||||
syscall.devctl(1,"sfgc",2)
|
|
||||||
printInline(progName..": Program load error on line ")
|
|
||||||
print(string.sub(errSL, 1, string.find(errSL, ":") - 1))
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
print(string.sub(errSL, string.find(errSL, ":") + 1))
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local proc = syscall.spawn(function(...)
|
|
||||||
syscall.open("/dev/tty/TTY1","r")
|
|
||||||
syscall.open("/dev/tty/TTY1","w")
|
|
||||||
syscall.open("/dev/null","w")
|
|
||||||
local success, msg = pcall(program, ...)
|
|
||||||
if not success then
|
|
||||||
local errSL = string.sub(msg, string.find(msg, ":") + 1)
|
|
||||||
syscall.devctl(1,"sfgc",2)
|
|
||||||
printInline(progName..": Program runtime error on line ")
|
|
||||||
print(string.sub(errSL, 1, string.find(errSL, ":") - 1))
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
print(string.sub(errSL, string.find(errSL, ":") + 1))
|
|
||||||
end
|
|
||||||
end, progName, nil, {table.unpack(args, 2)})
|
|
||||||
|
|
||||||
while true do
|
|
||||||
local exited, code = syscall.collect(proc)
|
|
||||||
if exited then
|
|
||||||
if code then
|
|
||||||
print("\nTask exited with code:\n"..tostring(code))
|
|
||||||
end
|
|
||||||
return
|
|
||||||
end
|
|
||||||
if terminate then
|
|
||||||
local success, err = syscall.kill(proc)
|
|
||||||
if success then
|
|
||||||
syscall.devctl(1,"sbgc",16)
|
|
||||||
syscall.devctl(1,"sfgc",2)
|
|
||||||
print("\nProgram Terminated.")
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
end
|
|
||||||
terminate = false
|
|
||||||
break
|
|
||||||
end
|
|
||||||
sleep(0.05)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
local command = getUserInput()
|
|
||||||
if command ~= "" then
|
|
||||||
if command ~= commandHistory[#commandHistory] then
|
|
||||||
table.insert(commandHistory, command)
|
|
||||||
end
|
|
||||||
runCommand(command)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
--ERROR HANDLING
|
|
||||||
end, debug.traceback)
|
|
||||||
|
|
||||||
if not success then
|
|
||||||
syscall.log("Error running shell: "..errorMsg, "ERROR")
|
|
||||||
syscall.devctl(1,"sfgc",2)
|
|
||||||
syscall.devctl(1,"sbgc",16)
|
|
||||||
print()
|
|
||||||
print("Error running shell: ")
|
|
||||||
print(errorMsg)
|
|
||||||
end
|
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
syscall.open("/dev/tty/TTY1","r")
|
|
||||||
syscall.open("/dev/tty/TTY1","w")
|
|
||||||
syscall.open("/dev/null","r")
|
|
||||||
syscall.devctl(1,"clear")
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
syscall.devctl(1,"spos",1,1)
|
|
||||||
print("HyperionOS hysh Shell")
|
|
||||||
local str=""
|
|
||||||
local stopInput=false
|
|
||||||
local proc=0
|
|
||||||
local fs=require("sys.fs")
|
|
||||||
local timeout=false
|
|
||||||
syscall.setEnviron("SHELL","simpleshell")
|
|
||||||
printInline("> ")
|
|
||||||
syscall.sigcatch(function(sig)
|
|
||||||
if sig==1 then
|
|
||||||
syscall.kill(proc)
|
|
||||||
print("Terminated")
|
|
||||||
printInline("> ")
|
|
||||||
stopInput=false
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
while true do
|
|
||||||
if not stopInput then
|
|
||||||
local input=syscall.read(0)
|
|
||||||
if input then
|
|
||||||
if input=="\b" then
|
|
||||||
if #str>0 then
|
|
||||||
str=str:sub(1,#str-1)
|
|
||||||
printInline("\b")
|
|
||||||
end
|
|
||||||
elseif input=="\n" then
|
|
||||||
print("")
|
|
||||||
stopInput=true
|
|
||||||
if str == "" then
|
|
||||||
printInline("> ")
|
|
||||||
stopInput=false
|
|
||||||
else
|
|
||||||
local path=nil
|
|
||||||
local split=string.split(str, " ")
|
|
||||||
if fs.exists("/bin/"..split[1]) then
|
|
||||||
path="/bin/"..split[1]
|
|
||||||
elseif fs.exists("/bin/"..split[1]..".lua") then
|
|
||||||
path="/bin/"..split[1]..".lua"
|
|
||||||
end
|
|
||||||
if not path then
|
|
||||||
print("Program not found")
|
|
||||||
printInline("> ")
|
|
||||||
stopInput=false
|
|
||||||
else
|
|
||||||
local text = fs.readAllText(path)
|
|
||||||
local program, err = load(text, path)
|
|
||||||
if not program then
|
|
||||||
print(err)
|
|
||||||
printInline("> ")
|
|
||||||
end
|
|
||||||
proc = syscall.spawn(function(...)
|
|
||||||
syscall.open("/dev/tty/TTY1","r")
|
|
||||||
syscall.open("/dev/tty/TTY1","w")
|
|
||||||
syscall.open("/dev/null","w")
|
|
||||||
program(...)
|
|
||||||
end, path, nil, {table.unpack(split, 2)})
|
|
||||||
end
|
|
||||||
str=""
|
|
||||||
end
|
|
||||||
else
|
|
||||||
str=str..input
|
|
||||||
printInline(input)
|
|
||||||
end
|
|
||||||
timeout=false
|
|
||||||
else
|
|
||||||
timeout=true
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local exited, code = syscall.collect(proc)
|
|
||||||
if exited then
|
|
||||||
if code then
|
|
||||||
print("\nTask exited with code:\n"..tostring(code))
|
|
||||||
end
|
|
||||||
printInline("> ")
|
|
||||||
stopInput=false
|
|
||||||
end
|
|
||||||
timeout=true
|
|
||||||
end
|
|
||||||
if timeout then
|
|
||||||
if stopInput then
|
|
||||||
sleep(.5)
|
|
||||||
else
|
|
||||||
sleep(.05)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
syscall.open("/dev/tty/TTY1", "r") --stdin (fd 0)
|
|
||||||
syscall.open("/dev/tty/TTY1", "w") --stdout (fd 1)
|
|
||||||
syscall.open("/dev/null", "w") --stderr (fd 2)
|
|
||||||
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
|
|
||||||
local MAX_ATTEMPTS = 3
|
|
||||||
|
|
||||||
local function readLine(mask)
|
|
||||||
local input = ""
|
|
||||||
while true do
|
|
||||||
local ch = syscall.read(0)
|
|
||||||
if not ch or ch == "" then
|
|
||||||
-- buffer empty, spin
|
|
||||||
elseif ch == "\n" then
|
|
||||||
syscall.write(1, "\n")
|
|
||||||
return input
|
|
||||||
elseif ch == "\b" then
|
|
||||||
if #input > 0 then
|
|
||||||
input = input:sub(1, -2)
|
|
||||||
syscall.write(1, "\b \b")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
input = input .. ch
|
|
||||||
syscall.write(1, mask or ch)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function firstBoot()
|
|
||||||
local shadow = fs.readAllText("/etc/shadow") or ""
|
|
||||||
if shadow:match("%S") then return end
|
|
||||||
|
|
||||||
syscall.devctl(1, "clear")
|
|
||||||
syscall.devctl(1, "spos", 1, 1)
|
|
||||||
syscall.devctl(1, "sfgc", 3)
|
|
||||||
syscall.write(1, "HyperionOS First Boot Setup\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
syscall.write(1, "No root password is set. Please create one now.\n\n")
|
|
||||||
|
|
||||||
while true do
|
|
||||||
syscall.write(1, "New root password: ")
|
|
||||||
local pw1 = readLine("*")
|
|
||||||
syscall.write(1, "Confirm password: ")
|
|
||||||
local pw2 = readLine("*")
|
|
||||||
|
|
||||||
if pw1 ~= pw2 then
|
|
||||||
syscall.devctl(1, "sfgc", 2)
|
|
||||||
syscall.write(1, "Passwords do not match. Try again.\n\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
elseif #pw1 < 6 then
|
|
||||||
syscall.devctl(1, "sfgc", 2)
|
|
||||||
syscall.write(1, "Password too short (minimum 6 characters).\n\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
else
|
|
||||||
local ok, err = syscall.auth_setpassword(0, pw1)
|
|
||||||
if ok then
|
|
||||||
syscall.devctl(1, "sfgc", 3)
|
|
||||||
syscall.write(1, "Root password set.\n\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
sleep(0.5)
|
|
||||||
break
|
|
||||||
else
|
|
||||||
syscall.devctl(1, "sfgc", 2)
|
|
||||||
syscall.write(1, "Error: " .. tostring(err) .. "\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function spawnShell(username, uid, shell, homedir)
|
|
||||||
local shellText = fs.readAllText(shell)
|
|
||||||
if not shellText then
|
|
||||||
syscall.write(1, "login: shell not found: " .. shell .. "\n")
|
|
||||||
sleep(2)
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local errFifo = {}
|
|
||||||
|
|
||||||
local proc = syscall.spawn(function()
|
|
||||||
syscall.setuid(uid)
|
|
||||||
syscall.chdir(homedir)
|
|
||||||
syscall.setEnviron("HOME", homedir)
|
|
||||||
syscall.setEnviron("USER", username)
|
|
||||||
syscall.setEnviron("SHELL", shell)
|
|
||||||
syscall.setEnviron("PATH", "/bin/")
|
|
||||||
|
|
||||||
local shellFn, loadErr = load(shellText, "@" .. shell)
|
|
||||||
if not shellFn then
|
|
||||||
syscall.log("login: shell load error: " .. tostring(loadErr), "ERROR")
|
|
||||||
syscall.exit(-1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, runErr = xpcall(shellFn, debug.traceback)
|
|
||||||
if not ok then
|
|
||||||
syscall.log("login: shell runtime error: " .. tostring(runErr), "ERROR")
|
|
||||||
end
|
|
||||||
end, username .. ":shell")
|
|
||||||
syscall.exit(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function doLogin()
|
|
||||||
syscall.devctl(1, "clear")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
syscall.devctl(1, "sbgc", 16)
|
|
||||||
syscall.devctl(1, "spos", 1, 1)
|
|
||||||
|
|
||||||
local hostname = syscall.getHostname() or "hyperion"
|
|
||||||
syscall.write(1, "HyperionOS\n")
|
|
||||||
syscall.write(1, hostname .. " login\n\n")
|
|
||||||
|
|
||||||
local attempts = 0
|
|
||||||
while attempts < MAX_ATTEMPTS do
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
syscall.write(1, "Username: ")
|
|
||||||
local username = readLine(nil)
|
|
||||||
|
|
||||||
if username == "" then goto continue end
|
|
||||||
|
|
||||||
syscall.write(1, "Password: ")
|
|
||||||
local password = readLine("*")
|
|
||||||
|
|
||||||
local ok, err = syscall.auth_login(username, password)
|
|
||||||
if ok then
|
|
||||||
local uid = syscall.auth_getuid(username)
|
|
||||||
local pwent = uid and syscall.auth_getpasswd(uid)
|
|
||||||
local shell = (pwent and pwent.shell) or "/bin/hysh"
|
|
||||||
local homedir = (pwent and pwent.homedir) or "/"
|
|
||||||
|
|
||||||
syscall.devctl(1, "sfgc", 3)
|
|
||||||
syscall.write(1, "\nWelcome, " .. username .. "!\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
sleep(0.3)
|
|
||||||
|
|
||||||
spawnShell(username, uid, shell, homedir)
|
|
||||||
return -- back to login prompt
|
|
||||||
else
|
|
||||||
attempts = attempts + 1
|
|
||||||
sleep(1)
|
|
||||||
syscall.devctl(1, "sfgc", 2)
|
|
||||||
syscall.write(1, "Login incorrect.\n\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
|
|
||||||
syscall.devctl(1, "sfgc", 2)
|
|
||||||
syscall.write(1, "Maximum login attempts exceeded.\n")
|
|
||||||
syscall.devctl(1, "sfgc", 1)
|
|
||||||
sleep(5)
|
|
||||||
end
|
|
||||||
|
|
||||||
firstBoot()
|
|
||||||
while true do
|
|
||||||
doLogin()
|
|
||||||
end
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
local cloptions = {
|
|
||||||
a = false,
|
|
||||||
h = false,
|
|
||||||
l = false,
|
|
||||||
help = false,
|
|
||||||
}
|
|
||||||
|
|
||||||
local inpArgs = {...}
|
|
||||||
local args = {}
|
|
||||||
local name = syscall.getTask(syscall.getpid()).name
|
|
||||||
|
|
||||||
local optToSet = false
|
|
||||||
for _, v in pairs(inpArgs) do
|
|
||||||
if optToSet then
|
|
||||||
cloptions[optToSet] = v
|
|
||||||
optToSet = false
|
|
||||||
elseif v:sub(1, 2) == "--" then
|
|
||||||
local opt = v:sub(3)
|
|
||||||
if cloptions[opt] == nil then
|
|
||||||
print(name..": unrecognized option '"..v.."'.")
|
|
||||||
if cloptions.help ~= nil then
|
|
||||||
print("try '"..name.." --help' for more information.")
|
|
||||||
end
|
|
||||||
return
|
|
||||||
elseif cloptions[opt] == false then
|
|
||||||
cloptions[opt] = true
|
|
||||||
else
|
|
||||||
optToSet = opt
|
|
||||||
end
|
|
||||||
elseif v:sub(1, 1) == "-" then
|
|
||||||
for i = 2, #v do
|
|
||||||
local opt = v:sub(i, i)
|
|
||||||
if cloptions[opt] == nil then
|
|
||||||
print(name..": invalid option '-"..opt.."'.")
|
|
||||||
if cloptions.help ~= nil then
|
|
||||||
print("try '"..name.." --help' for more information.")
|
|
||||||
end
|
|
||||||
return
|
|
||||||
else
|
|
||||||
cloptions[opt] = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
table.insert(args, v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if cloptions.help then
|
|
||||||
print("Usage: "..name.." [OPTION]... [DIR]")
|
|
||||||
print("List all entries in the specified DIRectory, or cwd if not specified")
|
|
||||||
print("\nOptions:")
|
|
||||||
print(" -a Do not ignore entries starting with .")
|
|
||||||
print(" -h with -l, print sizes in a human readble format")
|
|
||||||
print(" -l Use a long listing format")
|
|
||||||
print(" --help Display this help and exit")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
local dir = (args[1] or "")
|
|
||||||
if dir:sub(1, 1) ~= "/" then
|
|
||||||
dir = syscall.getcwd().."/"..dir
|
|
||||||
end
|
|
||||||
|
|
||||||
if dir:sub(#dir, #dir) ~= "/" then
|
|
||||||
dir = dir.."/"
|
|
||||||
end
|
|
||||||
|
|
||||||
if not fs.isDir(dir) then
|
|
||||||
print(name..": Cannot access '"..args[1].."': No such directory.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local screenSizeStr = syscall.devctl(1, "size")
|
|
||||||
local sizeX = tonumber(screenSizeStr:sub(1, screenSizeStr:find(";")-1))
|
|
||||||
local sizeY = tonumber(screenSizeStr:sub(screenSizeStr:find(";")+1))
|
|
||||||
|
|
||||||
local list = fs.list(dir)
|
|
||||||
if not cloptions.a then
|
|
||||||
for i = #list, 1, -1 do
|
|
||||||
if list[i]:sub(1, 1) == "." then
|
|
||||||
table.remove(list, i)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local colWidth = 0
|
|
||||||
local numCols = 1
|
|
||||||
if not cloptions.l then
|
|
||||||
for _, item in pairs(list) do
|
|
||||||
if #item + 2 > colWidth then
|
|
||||||
colWidth = #item + 2
|
|
||||||
end
|
|
||||||
end
|
|
||||||
numCols = math.floor(sizeX / colWidth)
|
|
||||||
end
|
|
||||||
|
|
||||||
local sizePrefixes = {"K", "M", "G"}
|
|
||||||
|
|
||||||
for i,v in ipairs(list) do
|
|
||||||
local fileStats = syscall.stat(dir..v)
|
|
||||||
local isDir = fs.isDir(dir..v)
|
|
||||||
if cloptions.l then
|
|
||||||
if isDir then
|
|
||||||
printInline("d")
|
|
||||||
else
|
|
||||||
printInline("-")
|
|
||||||
end
|
|
||||||
printInline("------ ")
|
|
||||||
printInline(fileStats.owner.." ")
|
|
||||||
printInline(fileStats.group.." ")
|
|
||||||
local size = fileStats.size
|
|
||||||
if cloptions.h then
|
|
||||||
local scale = 0
|
|
||||||
while size > 1024 do
|
|
||||||
size = size / 1024
|
|
||||||
scale = scale + 1
|
|
||||||
end
|
|
||||||
if scale > 0 then
|
|
||||||
if size < 10 then
|
|
||||||
size = math.floor(size).."."..math.floor((size * 10) % 10)..sizePrefixes[scale]
|
|
||||||
else
|
|
||||||
size = math.floor(size)..sizePrefixes[scale]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
printInline(size.." ")
|
|
||||||
printInline(math.floor(fileStats.modified / 1000).." ")
|
|
||||||
end
|
|
||||||
if isDir then
|
|
||||||
syscall.devctl(1,"sfgc",4)
|
|
||||||
else
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
end
|
|
||||||
printInline(v)
|
|
||||||
printInline((" "):rep(colWidth - #v))
|
|
||||||
syscall.devctl(1,"sfgc",1)
|
|
||||||
if i % numCols == 0 then
|
|
||||||
print("")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if #list % numCols ~= 0 then
|
|
||||||
print("")
|
|
||||||
end
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
print("HyperionOS lua")
|
|
||||||
local str=""
|
|
||||||
local stopInput=false
|
|
||||||
local timeout=false
|
|
||||||
local luaEnv=setmetatable({},{__index=_ENV})
|
|
||||||
printInline("> ")
|
|
||||||
while true do
|
|
||||||
local input=syscall.read(0)
|
|
||||||
if input then
|
|
||||||
if input=="\b" then
|
|
||||||
if #str>0 then
|
|
||||||
str=str:sub(1,#str-1)
|
|
||||||
printInline("\b")
|
|
||||||
end
|
|
||||||
elseif input=="\n" then
|
|
||||||
print("")
|
|
||||||
stopInput=true
|
|
||||||
if str == "" then
|
|
||||||
printInline("> ")
|
|
||||||
stopInput=false
|
|
||||||
elseif str == "exit()" then
|
|
||||||
break
|
|
||||||
else
|
|
||||||
local func=load(str,"@Lua","t",luaEnv)
|
|
||||||
local ok,err = xpcall(func, debug.traceback)
|
|
||||||
if not ok then
|
|
||||||
print(err)
|
|
||||||
end
|
|
||||||
printInline("\n> ")
|
|
||||||
str=""
|
|
||||||
end
|
|
||||||
str=""
|
|
||||||
else
|
|
||||||
str=str..input
|
|
||||||
printInline(input)
|
|
||||||
end
|
|
||||||
timeout=false
|
|
||||||
else
|
|
||||||
timeout=true
|
|
||||||
end
|
|
||||||
|
|
||||||
if timeout then
|
|
||||||
if stopInput then
|
|
||||||
sleep(.5)
|
|
||||||
else
|
|
||||||
sleep(.05)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
local args = {...}
|
|
||||||
local name = syscall.getTask(syscall.getpid()).name
|
|
||||||
if #args == 0 then
|
|
||||||
print(name..": Missing operand.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
local newDir = args[1]
|
|
||||||
if newDir:sub(1, 1) ~= "/" then
|
|
||||||
newDir = syscall.getcwd().."/"..newDir
|
|
||||||
end
|
|
||||||
|
|
||||||
if newDir:sub(#newDir, #newDir) ~= "/" then
|
|
||||||
newDir = newDir.."/"
|
|
||||||
end
|
|
||||||
|
|
||||||
if fs.isDir(newDir) then
|
|
||||||
print(name..": Cannot create directory '"..args[1].."': Directory already exists.")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
fs.mkdir(newDir)
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
local userhost = (syscall.getUsername() or "Unknown").."@"..(syscall.getHostname() or "Unknown")
|
|
||||||
print(".. *. .. | "..userhost)
|
|
||||||
print(" *= +@* +* | "..string.rep("-",#userhost))
|
|
||||||
print(" .@#. -@@@= :#@. | OS: "..(syscall.version() or "Unknown"))
|
|
||||||
print(" =@@+ *@@@# +@@= | Host: "..(syscall.getHost() or "Unknown"))
|
|
||||||
print(" %@@%: *@@@# -%@@% | Uptime: "..(syscall.getUptime() or "Unknown"))
|
|
||||||
print(" :@@@@+ *@@@# .*@@@@: | Tasks: "..tostring((#syscall.getTasks() or "Unknown")))
|
|
||||||
print(" :*@@@%- *@@@# -@@@@*: | Packages: ".."Unknown")
|
|
||||||
print(" =%@@#. *@@@# .#@@%= | Shell: "..(syscall.getEnviron("SHELL") or "Unknown"))
|
|
||||||
print(" :=. :*@@= *@@@# =@@+: .=: | ")
|
|
||||||
print(" %@#=..*# +@@@# #*..=#@# | ")
|
|
||||||
print(" .@@@@+=# .%@%: #=+@@@@. | ")
|
|
||||||
print(" .....=# -@= *+...:. | ")
|
|
||||||
print(" -*%*-@= - =@-*%*- | ")
|
|
||||||
print(" -@*. -@%. :%@- :*@- | ")
|
|
||||||
print(" .#@#@* | ")
|
|
||||||
print(" -#- | ")
|
|
||||||
print(" | ")
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
print(syscall.getcwd())
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
syscall.chown("/bin", 0, 0)
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
local targetUser = ({ ... })[1] or "root"
|
|
||||||
|
|
||||||
local currentUid = syscall.getuid()
|
|
||||||
local currentUser = syscall.getUsername(currentUid) or tostring(currentUid)
|
|
||||||
|
|
||||||
local targetUid = syscall.auth_getuid(targetUser)
|
|
||||||
if not targetUid then
|
|
||||||
print("su: user '" .. targetUser .. "' does not exist")
|
|
||||||
syscall.exit(1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local ok, err
|
|
||||||
if currentUid == 0 then
|
|
||||||
ok = true
|
|
||||||
else
|
|
||||||
printInline("Password: ")
|
|
||||||
local pw = ""
|
|
||||||
while true do
|
|
||||||
local ch = syscall.read(0)
|
|
||||||
if not ch or ch == "" then
|
|
||||||
elseif ch == "\n" then
|
|
||||||
syscall.write(1, "\n")
|
|
||||||
break
|
|
||||||
elseif ch == "\b" then
|
|
||||||
if #pw > 0 then
|
|
||||||
pw = pw:sub(1, -2); syscall.write(1, "\b \b")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
pw = pw .. ch
|
|
||||||
syscall.write(1, "*")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
ok, err = syscall.auth_elevate(targetUser, pw)
|
|
||||||
if not ok then
|
|
||||||
sleep(1)
|
|
||||||
print("su: Authentication failure")
|
|
||||||
syscall.exit(1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if currentUid == 0 then
|
|
||||||
syscall.setuid(targetUid)
|
|
||||||
end
|
|
||||||
|
|
||||||
local pwent = syscall.auth_getpasswd(targetUid)
|
|
||||||
local shell = (pwent and pwent.shell) or "/bin/hysh"
|
|
||||||
local homedir = (pwent and pwent.homedir) or "/"
|
|
||||||
|
|
||||||
syscall.chdir(homedir)
|
|
||||||
syscall.setEnviron("HOME", homedir)
|
|
||||||
syscall.setEnviron("USER", targetUser)
|
|
||||||
syscall.setEnviron("SHELL", shell)
|
|
||||||
|
|
||||||
local shellText = fs.readAllText(shell)
|
|
||||||
if not shellText then
|
|
||||||
print("su: shell not found: " .. shell)
|
|
||||||
syscall.exit(1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local shellFn, loadErr = load(shellText, "@" .. shell)
|
|
||||||
|
|
||||||
if not shellFn then
|
|
||||||
print("su: cannot load shell: " .. tostring(loadErr))
|
|
||||||
syscall.exit(1)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local success, err = syscall.kill(syscall.getppid())
|
|
||||||
if success then
|
|
||||||
syscall.spawn(shellFn, targetUser .. ":" .. shell, syscall.getEnviron())
|
|
||||||
syscall.exit(0)
|
|
||||||
else
|
|
||||||
print("su: "..err)
|
|
||||||
end
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
local syscalls=syscall.sysdump()
|
|
||||||
for i=1, #syscalls do
|
|
||||||
print(syscalls[i])
|
|
||||||
end
|
|
||||||
print("Total # of syscalls: "..tostring(#syscalls))
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
print((syscall.getUsername() or "Unknown"))
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
--:Minify:--
|
||||||
|
return {
|
||||||
|
white=0xFFFFFF,
|
||||||
|
red=0xFF0000,
|
||||||
|
green=0x00FF00,
|
||||||
|
blue=0x0000FF,
|
||||||
|
cyan=0x00FFFF,
|
||||||
|
yellow=0xFFFF00,
|
||||||
|
purple=0xFF00FF,
|
||||||
|
black=0x000000,
|
||||||
|
gray=0x888888,
|
||||||
|
lightgrey=0xBBBBBB,
|
||||||
|
darkgrey=0x444444,
|
||||||
|
orange=0xFF8800,
|
||||||
|
mint=0x00FF88,
|
||||||
|
brown=0xa52a2a,
|
||||||
|
chocolate=0xd2691e
|
||||||
|
}
|
||||||
@@ -1,11 +1,6 @@
|
|||||||
--:Minify:--
|
--:Minify:--
|
||||||
local fs={}
|
local fs={}
|
||||||
|
|
||||||
-- "open" : open
|
|
||||||
-- "read" : read
|
|
||||||
-- "write" : write
|
|
||||||
-- "close" : close
|
|
||||||
|
|
||||||
function fs.open(path, mode)
|
function fs.open(path, mode)
|
||||||
local fd=syscall.open(path,mode)
|
local fd=syscall.open(path,mode)
|
||||||
local ret={
|
local ret={
|
||||||
@@ -140,4 +135,12 @@ function fs.isDir(path)
|
|||||||
return syscall.type(path) == "directory"
|
return syscall.type(path) == "directory"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function fs.symlink(target, path)
|
||||||
|
return syscall.syslink(target, path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function fs.readlink(path)
|
||||||
|
return syscall.readlink(path)
|
||||||
|
end
|
||||||
|
|
||||||
return fs
|
return fs
|
||||||
@@ -0,0 +1,182 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local http = {}
|
||||||
|
|
||||||
|
local syscall = syscall
|
||||||
|
|
||||||
|
local function parseUrl(url)
|
||||||
|
local proto, host, path =
|
||||||
|
url:match(
|
||||||
|
"^(https?://)([^/]+)(/.*)$"
|
||||||
|
)
|
||||||
|
|
||||||
|
if not proto then
|
||||||
|
proto, host =
|
||||||
|
url:match(
|
||||||
|
"^(https?://)([^/]+)$"
|
||||||
|
)
|
||||||
|
|
||||||
|
path = "/"
|
||||||
|
end
|
||||||
|
|
||||||
|
if not proto then
|
||||||
|
return nil, "EINVAL"
|
||||||
|
end
|
||||||
|
|
||||||
|
return host, path
|
||||||
|
end
|
||||||
|
|
||||||
|
local function buildRequest(req)
|
||||||
|
local host, path = parseUrl(req.url)
|
||||||
|
|
||||||
|
if not host then
|
||||||
|
return nil, path
|
||||||
|
end
|
||||||
|
|
||||||
|
local method =
|
||||||
|
req.method or "GET"
|
||||||
|
|
||||||
|
local body =
|
||||||
|
req.body or ""
|
||||||
|
|
||||||
|
local headers =
|
||||||
|
table.deepcopy(
|
||||||
|
req.headers or {}
|
||||||
|
)
|
||||||
|
|
||||||
|
headers.Host =
|
||||||
|
headers.Host or host
|
||||||
|
|
||||||
|
headers.Connection =
|
||||||
|
headers.Connection or "close"
|
||||||
|
|
||||||
|
if body ~= "" then
|
||||||
|
headers["Content-Length"] =
|
||||||
|
tostring(#body)
|
||||||
|
end
|
||||||
|
|
||||||
|
local out = {
|
||||||
|
method ..
|
||||||
|
" " ..
|
||||||
|
path ..
|
||||||
|
" HTTP/1.1"
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v in pairs(headers) do
|
||||||
|
out[#out + 1] =
|
||||||
|
tostring(k) ..
|
||||||
|
": " ..
|
||||||
|
tostring(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
out[#out + 1] = ""
|
||||||
|
out[#out + 1] = body
|
||||||
|
|
||||||
|
return table.concat(out, "\r\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parseResponse(response)
|
||||||
|
local headerBlock, body = response:match("^(.-)\r\n\r\n(.*)$")
|
||||||
|
if not headerBlock then
|
||||||
|
return nil, "invalid HTTP response"
|
||||||
|
end
|
||||||
|
|
||||||
|
local headers = {}
|
||||||
|
|
||||||
|
-- Parse status line
|
||||||
|
local statusLine, rest = headerBlock:match("^(.-)\r\n(.*)$")
|
||||||
|
if not statusLine then
|
||||||
|
statusLine = headerBlock
|
||||||
|
rest = ""
|
||||||
|
end
|
||||||
|
|
||||||
|
local version, code, message = statusLine:match("^(HTTP/%d+%.%d+)%s+(%d+)%s+(.*)$")
|
||||||
|
|
||||||
|
-- Parse headers
|
||||||
|
for line in (rest .. "\r\n"):gmatch("(.-)\r\n") do
|
||||||
|
local key, value = line:match("^([^:]+):%s*(.*)$")
|
||||||
|
if key then
|
||||||
|
headers[key:lower()] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
code=tonumber(code),
|
||||||
|
message=message,
|
||||||
|
headers=headers,
|
||||||
|
body=body
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function http.request(req)
|
||||||
|
local raw, err =
|
||||||
|
buildRequest(req)
|
||||||
|
|
||||||
|
if not raw then
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
|
||||||
|
local fd =
|
||||||
|
syscall.socket()
|
||||||
|
|
||||||
|
if not fd then
|
||||||
|
return nil, "ESOCKET"
|
||||||
|
end
|
||||||
|
|
||||||
|
local host, path = parseUrl(req.url)
|
||||||
|
|
||||||
|
if not host then
|
||||||
|
syscall.close(fd)
|
||||||
|
return nil, path
|
||||||
|
end
|
||||||
|
|
||||||
|
local proto = req.url:match("^(https?://)")
|
||||||
|
|
||||||
|
local base = proto .. host
|
||||||
|
|
||||||
|
local ok, err = syscall.connect(fd, base)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
syscall.close(fd)
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok2, err2 = syscall.write(fd, raw)
|
||||||
|
|
||||||
|
if not ok2 then
|
||||||
|
syscall.close(fd)
|
||||||
|
return nil, err2
|
||||||
|
end
|
||||||
|
|
||||||
|
local out = {}
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local chunk = syscall.read(fd, 4096)
|
||||||
|
if chunk == "" then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
out[#out+1] = chunk
|
||||||
|
end
|
||||||
|
|
||||||
|
syscall.close(fd)
|
||||||
|
return parseResponse(table.concat(out))
|
||||||
|
end
|
||||||
|
|
||||||
|
function http.get(url, headers)
|
||||||
|
return http.request({
|
||||||
|
url = url,
|
||||||
|
method = "GET",
|
||||||
|
headers = headers
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
function http.post(url, body, headers)
|
||||||
|
return http.request({
|
||||||
|
url = url,
|
||||||
|
method = "POST",
|
||||||
|
body = body,
|
||||||
|
headers = headers
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
return http
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
local io = {}
|
local io = {}
|
||||||
local fs = require("sys.fs")
|
local fs = require("fs")
|
||||||
|
|
||||||
function io.open(path, mode)
|
function io.open(path, mode)
|
||||||
return fs.open(path, mode)
|
return fs.open(path, mode)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
Core library for Hyperion
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
local sys = {}
|
|
||||||
local fs = require("sys.fs")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return sys
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
local sys = {}
|
|
||||||
sys.fs = require("sys.fs")
|
|
||||||
sys.hpv = require("sys.hpv")
|
|
||||||
sys.ipc = require("sys.ipc")
|
|
||||||
return sys
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
local ipc = {}
|
|
||||||
|
|
||||||
return ipc
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
local term = {}
|
|
||||||
|
|
||||||
function term.clear()
|
|
||||||
coroutine.yield("VFS_write", 1, "\27C\25")
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.setCursorPos(x, y)
|
|
||||||
coroutine.yield("VFS_write", 1, "\27cs"..tostring(y)..";"..tostring(x).."\25")
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.size()
|
|
||||||
coroutine.yield("VFS_write", 1, "\27ts\25")
|
|
||||||
local ok, data = coroutine.yield("VFS_read", 0, 16) -- read response
|
|
||||||
if not ok then error("Failed to get terminal size") end
|
|
||||||
local x, y = string.match(data, "%R(%d+);(%d+)\25")
|
|
||||||
return tonumber(x), tonumber(y)
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.getCursorPos()
|
|
||||||
coroutine.yield("VFS_write", 1, "\27gc\25")
|
|
||||||
local ok, data = coroutine.yield("VFS_read", 0, 16) -- read response
|
|
||||||
if not ok then error("Failed to get cursor position") end
|
|
||||||
local y, x = string.match(data, "%R(%d+);(%d+)\25")
|
|
||||||
return tonumber(x), tonumber(y)
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.write(data)
|
|
||||||
coroutine.yield("VFS_write", 1, data)
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.setTextColor(color)
|
|
||||||
local ok, err = coroutine.yield("VFS_type", 1)
|
|
||||||
if not ok then error(err) end
|
|
||||||
if ok ~= "tty" then return end
|
|
||||||
coroutine.yield("VFS_write", 1, "\27f"..tostring(color).."\25")
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.setBackgroundColor(color)
|
|
||||||
local ok, err = coroutine.yield("VFS_type", 1)
|
|
||||||
if not ok then error(err) end
|
|
||||||
if ok ~= "tty" then return end
|
|
||||||
coroutine.yield("VFS_write", 1, "\27b"..tostring(color).."\25")
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.isColor()
|
|
||||||
local ok, err = coroutine.yield("VFS_type", 1)
|
|
||||||
if not ok then error(err) end
|
|
||||||
return ok == "tty"
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.scroll(n)
|
|
||||||
coroutine.yield("VFS_write", 1, "\27S"..tostring(n).."\25")
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.setDefault(color, layer)
|
|
||||||
if layer then
|
|
||||||
coroutine.yield("VFS_write", 1, "\27F"..tostring(color).."\25")
|
|
||||||
else
|
|
||||||
coroutine.yield("VFS_write", 1, "\27B"..tostring(color).."\25")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function term.showCursor(show)
|
|
||||||
if show then
|
|
||||||
coroutine.yield("VFS_write", 1, "\27sc\25")
|
|
||||||
else
|
|
||||||
coroutine.yield("VFS_write", 1, "\27hc\25")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return term
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
local kernel=...
|
|
||||||
local fs=require("sys.fs")
|
|
||||||
|
|
||||||
for i,v in pairs(kernel.processes) do
|
|
||||||
kernel.log("Spawning kernel task "..i)
|
|
||||||
syscall.spawn(function()
|
|
||||||
local status, err = pcall(v)
|
|
||||||
if not status then
|
|
||||||
kernel.log("Error executing kernel task '" .. i .. "': " .. err, "ERROR")
|
|
||||||
else
|
|
||||||
kernel.log("Successfully executed kernel task: " .. i, "INFO")
|
|
||||||
end
|
|
||||||
end, i)
|
|
||||||
end
|
|
||||||
|
|
||||||
local files = fs.list("/bin/startup")
|
|
||||||
if not files then error("Failed to list /bin/startup") end
|
|
||||||
for i,v in ipairs(files) do
|
|
||||||
if v:sub(-4) == ".lua" then
|
|
||||||
local filepath = "/bin/startup/" .. v
|
|
||||||
kernel.log("Executing startup script: " .. filepath, "INFO")
|
|
||||||
local startupFunc, err = load(fs.readAllText(filepath), "@" .. filepath)
|
|
||||||
if not startupFunc then
|
|
||||||
kernel.log("Error loading startup script '" .. filepath .. "': " .. err, "ERROR")
|
|
||||||
else
|
|
||||||
syscall.spawn(function()
|
|
||||||
syscall.setuid(1)
|
|
||||||
local status, err = pcall(startupFunc)
|
|
||||||
if not status then
|
|
||||||
kernel.log("Error executing startup script '" .. filepath .. "': " .. err, "ERROR")
|
|
||||||
else
|
|
||||||
kernel.log("Successfully executed startup script: " .. filepath, "INFO")
|
|
||||||
end
|
|
||||||
end, "startup:" .. v)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
while true do
|
|
||||||
sleep(1)
|
|
||||||
kernel.saveLog()
|
|
||||||
end
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0.0
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
|
GHXX (Advanced Computers)
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
local cp,c=component,computer;local b_addr=c.getBootAddress();local b_fs=cp.proxy(b_addr)
|
||||||
|
local gpu=cp.list("gpu")() and cp.proxy(cp.list("gpu")())
|
||||||
|
local e_rom=cp.list("eeprom")() and cp.proxy(cp.list("eeprom")())
|
||||||
|
local screens={}
|
||||||
|
for addr in cp.list("screen") do
|
||||||
|
if gpu then
|
||||||
|
gpu.bind(addr)
|
||||||
|
local w,h=gpu.maxResolution()
|
||||||
|
gpu.setResolution(w,h)
|
||||||
|
gpu.fill(1,1,w,h," ")
|
||||||
|
cp.proxy(addr).turnOn()
|
||||||
|
screens[#screens+1] = {cx=1,cy=1,w=w,h=h,addr=addr}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function scroll(scr) gpu.copy(1,2,scr.w,scr.h-1,0,-1);gpu.fill(1,scr.h,scr.w,1," ") end
|
||||||
|
local function write(t)
|
||||||
|
if not gpu then return end
|
||||||
|
for s=1, #screens do
|
||||||
|
local scr=screens[s]
|
||||||
|
gpu.bind(scr.addr)
|
||||||
|
for i=1,#t do
|
||||||
|
local char=t:sub(i,i)
|
||||||
|
if char=="\n" then scr.cx=1;scr.cy=scr.cy+1
|
||||||
|
elseif char=="\t" then scr.cx=scr.cx+(4-((scr.cx-1)%4))
|
||||||
|
elseif char=="\b" then if scr.cx>1 then scr.cx=scr.cx-1;gpu.set(scr.cx,scr.cy," ") end
|
||||||
|
else gpu.set(scr.cx,scr.cy,char);scr.cx=scr.cx+1 end
|
||||||
|
if scr.cx>scr.w then scr.cx=1;scr.cy=scr.cy+1 end
|
||||||
|
if scr.cy>scr.h then scroll(scr);scr.cy=scr.cy-1 end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function throw(err)
|
||||||
|
if gpu then
|
||||||
|
gpu.setBackground(0x0000AA);
|
||||||
|
gpu.setForeground(0xFFFFFF);
|
||||||
|
for i=1, #screens do
|
||||||
|
gpu.fill(1,1,screens[i].w,screens[i].h," ");
|
||||||
|
screens[i].cx,screens[i].cy=1,1;
|
||||||
|
end
|
||||||
|
write("CRITICAL KERNEL ERROR:\n"..tostring(err))
|
||||||
|
end
|
||||||
|
while true do c.pullSignal() end
|
||||||
|
end
|
||||||
|
local ok,err=xpcall(function()
|
||||||
|
local apis={}
|
||||||
|
local _l={coroutine=1,debug=1,_VERSION=1,assert=1,collectgarbage=1,error=1,getmetatable=1,ipairs=1,load=1,math=1,next=1,pairs=1,pcall=1,rawequal=1,rawget=1,rawlen=1,rawset=1,select=1,setmetatable=1,string=1,table=1,tonumber=1,tostring=1,type=1,xpcall=1,_G=1}
|
||||||
|
for k,v in pairs(_G) do if not _l[k] then apis[k]=v;_G[k]=nil end end
|
||||||
|
function sleep(t) local s=c.uptime()+t;while c.uptime()<s do c.pullSignal(s-c.uptime()) end end
|
||||||
|
local function getFile(path)
|
||||||
|
local h,err=b_fs.open(path,"r")
|
||||||
|
if not h then throw("Cannot open: "..path) end
|
||||||
|
local buf,chk="",""
|
||||||
|
repeat chk=b_fs.read(h,math.huge);buf=buf..(chk or "") until not chk
|
||||||
|
b_fs.close(h)
|
||||||
|
return buf
|
||||||
|
end
|
||||||
|
local Kernel=load(getFile("/kernel.lua"),"@Kernel")
|
||||||
|
local initFs=load(getFile("/oc/initdisks"),"@Init_disks")
|
||||||
|
local fs=load(getFile("/initfs"),"@InitFs")
|
||||||
|
if not Kernel then throw("Kernel load failed.") end
|
||||||
|
if initFs then initFs=initFs(apis, b_fs, b_addr) end
|
||||||
|
if fs then fs=fs() end
|
||||||
|
local eQ={}
|
||||||
|
local function qEv(e,...) table.insert(eQ,{e,...}) end
|
||||||
|
local efi={
|
||||||
|
getEpochMs=function() return math.floor(c.uptime()*1000) end,
|
||||||
|
getUptime=function() return c.uptime()*1000 end,
|
||||||
|
date=function() return tostring(apis.os.date()) end,
|
||||||
|
getMachineEvent=function()
|
||||||
|
if #eQ > 0 then
|
||||||
|
return table.unpack(table.remove(eQ, 1))
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
getEEPROM=function() return e_rom and e_rom.get() or "" end,
|
||||||
|
setEEPROM=function(_,t) if e_rom then e_rom.set(t) end end,
|
||||||
|
getNvram=function() return e_rom and e_rom.getData() or "" end,
|
||||||
|
setNvram=function(_,t) if e_rom then e_rom.setData(t) end end,
|
||||||
|
beep=function(freq, timeMs) c.beep(freq, timeMs / 1000) end,
|
||||||
|
initfs=fs,
|
||||||
|
disks=initFs,
|
||||||
|
architecture="oc",
|
||||||
|
firmware=apis,
|
||||||
|
reboot=false,
|
||||||
|
yield=function() coroutine.yield() end,
|
||||||
|
screenCtl={
|
||||||
|
print=function(_,t) write(tostring(t).."\n") end,
|
||||||
|
printInline=function(_,t) write(tostring(t)) end,
|
||||||
|
clear=function() if gpu then for i=1, #screens do local scr=screens[i];gpu.bind(scr.addr);gpu.fill(1,1,scr.w,scr.h," ");scr.cx,scr.cy=1,1 end end end,
|
||||||
|
resetCursor=function() for i=1, #screens do local scr=screens[i];scr.cx,scr.cy=1,1 end end,
|
||||||
|
setBackgroundColor=function(_,cl) if gpu then gpu.setBackground(cl) end end,
|
||||||
|
setTextColor=function(_,cl) if gpu then gpu.setForeground(cl) end end,
|
||||||
|
enable=function() end,
|
||||||
|
disable=function() end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local kCo=coroutine.create(function()
|
||||||
|
local s,e=xpcall(Kernel,debug.traceback,efi)
|
||||||
|
if not s and not efi.reboot then throw(e) end
|
||||||
|
if efi.reboot then c.shutdown(true) else c.shutdown() end
|
||||||
|
end)
|
||||||
|
efi.screenCtl:print("Loaded in "..tostring(c.uptime()).."s")
|
||||||
|
while true do
|
||||||
|
local st,e=coroutine.resume(kCo)
|
||||||
|
c.pushSignal("NoSleep")
|
||||||
|
local ex=false
|
||||||
|
while not ex do
|
||||||
|
local ev={c.pullSignal(0)}
|
||||||
|
if ev[1]=="key_down" then qEv("keyPressed",1,ev[3]);qEv("keyTyped",1,string.char(ev[3]))
|
||||||
|
elseif ev[1]=="key_up" then qEv("keyReleased",1,ev[3])
|
||||||
|
end
|
||||||
|
if ev[1]=="NoSleep" then ex=true else qEv(table.unpack(ev)) end
|
||||||
|
end
|
||||||
|
if st=="error" or coroutine.status(kCo)=="dead" then
|
||||||
|
if efi.reboot then c.shutdown(true) end
|
||||||
|
throw("Kernel fault: "..tostring(e))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,debug.traceback)
|
||||||
|
if not ok then throw("Boot fault: "..err) end
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
local init
|
||||||
|
do
|
||||||
|
local component_invoke = component.invoke
|
||||||
|
local function boot_invoke(address, method, ...)
|
||||||
|
local result = table.pack(pcall(component_invoke, address, method, ...))
|
||||||
|
if not result[1] then
|
||||||
|
return nil, result[2]
|
||||||
|
else
|
||||||
|
return table.unpack(result, 2, result.n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- backwards compatibility, may remove later
|
||||||
|
local eeprom = component.list("eeprom")()
|
||||||
|
computer.getBootAddress = function()
|
||||||
|
return boot_invoke(eeprom, "getData")
|
||||||
|
end
|
||||||
|
computer.setBootAddress = function(address)
|
||||||
|
return boot_invoke(eeprom, "setData", address)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local screen = component.list("screen")()
|
||||||
|
local gpu = component.list("gpu")()
|
||||||
|
if gpu and screen then
|
||||||
|
boot_invoke(gpu, "bind", screen)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function tryLoadFrom(address)
|
||||||
|
local handle, reason = boot_invoke(address, "open", "/oc/boot.lua")
|
||||||
|
if not handle then
|
||||||
|
return nil, reason
|
||||||
|
end
|
||||||
|
local buffer = ""
|
||||||
|
repeat
|
||||||
|
local data, reason = boot_invoke(address, "read", handle, math.maxinteger or math.huge)
|
||||||
|
if not data and reason then
|
||||||
|
return nil, reason
|
||||||
|
end
|
||||||
|
buffer = buffer .. (data or "")
|
||||||
|
until not data
|
||||||
|
boot_invoke(address, "close", handle)
|
||||||
|
return load(buffer, "=init")
|
||||||
|
end
|
||||||
|
local reason
|
||||||
|
if computer.getBootAddress() then
|
||||||
|
init, reason = tryLoadFrom(computer.getBootAddress())
|
||||||
|
end
|
||||||
|
if not init then
|
||||||
|
computer.setBootAddress()
|
||||||
|
for address in component.list("filesystem") do
|
||||||
|
init, reason = tryLoadFrom(address)
|
||||||
|
if init then
|
||||||
|
computer.setBootAddress(address)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not init then
|
||||||
|
error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local ok, err = xpcall(init,debug.traceback)
|
||||||
|
if not ok then
|
||||||
|
error(err)
|
||||||
|
else
|
||||||
|
return err
|
||||||
|
end
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
local args = {...}
|
||||||
|
local apis = args[1]
|
||||||
|
local bootdrive = args[2]
|
||||||
|
local bootaddr = args[3]
|
||||||
|
local disks = {}
|
||||||
|
local internal = {}
|
||||||
|
|
||||||
|
local function createDisk(id, hw)
|
||||||
|
local disk = {
|
||||||
|
address = id,
|
||||||
|
isReadOnly = function() return hw.isReadOnly() end
|
||||||
|
}
|
||||||
|
|
||||||
|
function disk:spaceUsed() return hw.spaceUsed() end
|
||||||
|
function disk:spaceTotal() return hw.spaceTotal() end
|
||||||
|
|
||||||
|
function disk:list(path)
|
||||||
|
if not hw.exists(path) or not hw.isDirectory(path) then
|
||||||
|
return nil, "not directory"
|
||||||
|
end
|
||||||
|
return hw.list(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:fileExists(path)
|
||||||
|
return hw.exists(path) and not hw.isDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:directoryExists(path)
|
||||||
|
return hw.exists(path) and hw.isDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:type(path)
|
||||||
|
if not hw.exists(path) then
|
||||||
|
return nil
|
||||||
|
elseif hw.isDirectory(path) then
|
||||||
|
return "directory"
|
||||||
|
else
|
||||||
|
return "file"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:makeDirectory(path)
|
||||||
|
hw.makeDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:remove(path)
|
||||||
|
if hw.exists(path) then hw.remove(path) end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:setLabel(label) hw.setLabel(label) end
|
||||||
|
function disk:getLabel() return hw.getLabel() end
|
||||||
|
|
||||||
|
function disk:attributes(path)
|
||||||
|
return {
|
||||||
|
size = hw.size(path),
|
||||||
|
isDir = hw.isDirectory(path),
|
||||||
|
isReadOnly = hw.isReadOnly(),
|
||||||
|
modified = hw.lastModified(path),
|
||||||
|
created = hw.lastModified(path)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path, mode)
|
||||||
|
local fd = hw.open(path, mode)
|
||||||
|
|
||||||
|
if not fd then
|
||||||
|
local dir = path:match("^(.*)/[^/]+$")
|
||||||
|
|
||||||
|
if dir and not hw.exists(dir) then
|
||||||
|
local ok, err = hw.makeDirectory(dir)
|
||||||
|
if not ok then
|
||||||
|
return nil, err or "could not create parent directory"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fd = hw.open(path, mode)
|
||||||
|
if not fd then
|
||||||
|
return nil, "file not found or access denied"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
read = function(num) return hw.read(fd, num) end,
|
||||||
|
|
||||||
|
readAll = function()
|
||||||
|
local data = {}
|
||||||
|
while true do
|
||||||
|
local chunk = hw.read(fd, math.huge)
|
||||||
|
if not chunk then break end
|
||||||
|
data[#data + 1] = chunk
|
||||||
|
end
|
||||||
|
return table.concat(data)
|
||||||
|
end,
|
||||||
|
|
||||||
|
write = function(data) return hw.write(fd, data) end,
|
||||||
|
seek = function(whence, offset)
|
||||||
|
return hw.seek(fd, whence, offset)
|
||||||
|
end,
|
||||||
|
|
||||||
|
flush = function()
|
||||||
|
hw.close(fd)
|
||||||
|
fd = hw.open(path, mode)
|
||||||
|
end,
|
||||||
|
|
||||||
|
close = function()
|
||||||
|
hw.close(fd)
|
||||||
|
fd = nil
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
internal["$"] = createDisk("$", bootdrive)
|
||||||
|
|
||||||
|
local function refresh()
|
||||||
|
disks = {}
|
||||||
|
for addr in apis.component.list("filesystem") do
|
||||||
|
if addr ~= bootaddr then
|
||||||
|
disks[addr] = createDisk(addr, apis.component.proxy(addr))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function iter()
|
||||||
|
refresh()
|
||||||
|
local combined = {}
|
||||||
|
|
||||||
|
for id, obj in pairs(internal) do combined[id] = obj end
|
||||||
|
for id, obj in pairs(disks) do combined[id] = obj end
|
||||||
|
|
||||||
|
return pairs(combined)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {refresh = refresh, list = iter}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
kernel.oc={}
|
||||||
|
kernel.oc.component=kernel.apis.component
|
||||||
|
kernel.oc.computer=kernel.apis.computer
|
||||||
|
|
||||||
|
kernel.log("Installed components:")
|
||||||
|
for addr, typ in kernel.oc.component.list() do
|
||||||
|
local dev=kernel.oc.component.proxy(addr)
|
||||||
|
local tier=""
|
||||||
|
if dev.getTier then
|
||||||
|
tier=dev.getTier()
|
||||||
|
end
|
||||||
|
kernel.log(" "..addr.." : "..typ..tier)
|
||||||
|
end
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local component=kernel.oc.component
|
||||||
|
|
||||||
|
local function wrap(p)
|
||||||
|
return function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if kernel.uid~=0 then error("EACCES") end
|
||||||
|
return component.proxy(p)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for addr, typ in kernel.oc.component.list() do
|
||||||
|
local obj=wrap(addr)
|
||||||
|
kernel.devfs.data["raw"][addr]=obj
|
||||||
|
local typefolder=kernel.devfs.data["raw"]["by-type"][typ]
|
||||||
|
if not typefolder then typefolder={} end
|
||||||
|
typefolder[tostring(#table.keys(typefolder)+1)] = obj
|
||||||
|
kernel.devfs.data["raw"]["by-type"][typ]=typefolder
|
||||||
|
end
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
-- OpenComputers TTY driver
|
||||||
|
|
||||||
|
local kernel = ...
|
||||||
|
local component = kernel.oc.component
|
||||||
|
local computer = kernel.oc.computer
|
||||||
|
|
||||||
|
local plt = {
|
||||||
|
0x000000, 0x000040, 0x000080, 0x0000C0, 0x0000FF,
|
||||||
|
0x002400, 0x002440, 0x002480, 0x0024C0, 0x0024FF,
|
||||||
|
0x004900, 0x004940, 0x004980, 0x0049C0, 0x0049FF,
|
||||||
|
0x006D00, 0x006D40, 0x006D80, 0x006DC0, 0x006DFF,
|
||||||
|
0x009200, 0x009240, 0x009280, 0x0092C0, 0x0092FF,
|
||||||
|
0x00B600, 0x00B640, 0x00B680, 0x00B6C0, 0x00B6FF,
|
||||||
|
0x00DB00, 0x00DB40, 0x00DB80, 0x00DBC0, 0x00DBFF,
|
||||||
|
0x00FF00, 0x00FF40, 0x00FF80, 0x00FFC0, 0x00FFFF,
|
||||||
|
|
||||||
|
0x330000, 0x330040, 0x330080, 0x3300C0, 0x3300FF,
|
||||||
|
0x332400, 0x332440, 0x332480, 0x3324C0, 0x3324FF,
|
||||||
|
0x334900, 0x334940, 0x334980, 0x3349C0, 0x3349FF,
|
||||||
|
0x336D00, 0x336D40, 0x336D80, 0x336DC0, 0x336DFF,
|
||||||
|
0x339200, 0x339240, 0x339280, 0x3392C0, 0x3392FF,
|
||||||
|
0x33B600, 0x33B640, 0x33B680, 0x33B6C0, 0x33B6FF,
|
||||||
|
0x33DB00, 0x33DB40, 0x33DB80, 0x33DBC0, 0x33DBFF,
|
||||||
|
0x33FF00, 0x33FF40, 0x33FF80, 0x33FFC0, 0x33FFFF,
|
||||||
|
|
||||||
|
0x660000, 0x660040, 0x660080, 0x6600C0, 0x6600FF,
|
||||||
|
0x662400, 0x662440, 0x662480, 0x6624C0, 0x6624FF,
|
||||||
|
0x664900, 0x664940, 0x664980, 0x6649C0, 0x6649FF,
|
||||||
|
0x666D00, 0x666D40, 0x666D80, 0x666DC0, 0x666DFF,
|
||||||
|
0x669200, 0x669240, 0x669280, 0x6692C0, 0x6692FF,
|
||||||
|
0x66B600, 0x66B640, 0x66B680, 0x66B6C0, 0x66B6FF,
|
||||||
|
0x66DB00, 0x66DB40, 0x66DB80, 0x66DBC0, 0x66DBFF,
|
||||||
|
0x66FF00, 0x66FF40, 0x66FF80, 0x66FFC0, 0x66FFFF,
|
||||||
|
|
||||||
|
0x990000, 0x990040, 0x990080, 0x9900C0, 0x9900FF,
|
||||||
|
0x992400, 0x992440, 0x992480, 0x9924C0, 0x9924FF,
|
||||||
|
0x994900, 0x994940, 0x994980, 0x9949C0, 0x9949FF,
|
||||||
|
0x996D00, 0x996D40, 0x996D80, 0x996DC0, 0x996DFF,
|
||||||
|
0x999200, 0x999240, 0x999280, 0x9992C0, 0x9992FF,
|
||||||
|
0x99B600, 0x99B640, 0x99B680, 0x99B6C0, 0x99B6FF,
|
||||||
|
0x99DB00, 0x99DB40, 0x99DB80, 0x99DBC0, 0x99DBFF,
|
||||||
|
0x99FF00, 0x99FF40, 0x99FF80, 0x99FFC0, 0x99FFFF,
|
||||||
|
|
||||||
|
0xCC0000, 0xCC0040, 0xCC0080, 0xCC00C0, 0xCC00FF,
|
||||||
|
0xCC2400, 0xCC2440, 0xCC2480, 0xCC24C0, 0xCC24FF,
|
||||||
|
0xCC4900, 0xCC4940, 0xCC4980, 0xCC49C0, 0xCC49FF,
|
||||||
|
0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DC0, 0xCC6DFF,
|
||||||
|
0xCC9200, 0xCC9240, 0xCC9280, 0xCC92C0, 0xCC92FF,
|
||||||
|
0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6C0, 0xCCB6FF,
|
||||||
|
0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBC0, 0xCCDBFF,
|
||||||
|
0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFC0, 0xCCFFFF,
|
||||||
|
|
||||||
|
0xFF0000, 0xFF0040, 0xFF0080, 0xFF00C0, 0xFF00FF,
|
||||||
|
0xFF2400, 0xFF2440, 0xFF2480, 0xFF24C0, 0xFF24FF,
|
||||||
|
0xFF4900, 0xFF4940, 0xFF4980, 0xFF49C0, 0xFF49FF,
|
||||||
|
0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DC0, 0xFF6DFF,
|
||||||
|
0xFF9200, 0xFF9240, 0xFF9280, 0xFF92C0, 0xFF92FF,
|
||||||
|
0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6C0, 0xFFB6FF,
|
||||||
|
0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBC0, 0xFFDBFF,
|
||||||
|
0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFC0, 0xFFFFFF,
|
||||||
|
|
||||||
|
0x000000, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D,
|
||||||
|
0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x696969,
|
||||||
|
0x787878, 0x878787, 0x969696, 0xA5A5A5,
|
||||||
|
0xB4B4B4, 0xC3C3C3, 0xD2D2D2, 0xE1E1E1
|
||||||
|
}
|
||||||
|
|
||||||
|
local gpus,screens = {},{}
|
||||||
|
|
||||||
|
for addr, typ in component.list("gpu") do
|
||||||
|
if typ == "gpu" then
|
||||||
|
gpus[#gpus+1]=component.proxy(addr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #gpus>0 then
|
||||||
|
for addr, typ in component.list("screen") do
|
||||||
|
if typ == "screen" then
|
||||||
|
gpus[1].bind(addr)
|
||||||
|
local w,h = gpus[1].maxResolution()
|
||||||
|
gpus[1].setResolution(w,h)
|
||||||
|
screens[addr]={cx=1,cy=1,w=w,h=h,addr=addr,fg=0,bg=0,gpu=nil}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local scrnum = 0
|
||||||
|
for i,v in pairs(screens) do
|
||||||
|
scrnum=scrnum+1
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("initialized "..tostring(#gpus).." gpus and "..tostring(scrnum).." screens")
|
||||||
|
|
||||||
|
local binds,newid={},0
|
||||||
|
local curaddr=""
|
||||||
|
local function scroll(scr, gpu) gpu.copy(1,2,scr.w,scr.h-1,0,-1);gpu.fill(1,scr.h,scr.w,1," ") end
|
||||||
|
local function write(text, addr)
|
||||||
|
local g=gpus[1]
|
||||||
|
local scr=screens[addr]
|
||||||
|
if not scr then error("NODEV") end
|
||||||
|
if curaddr~=addr then g.bind(addr); g.setForeground(scr.fg); g.setBackground(scr.bg) end
|
||||||
|
for i=1,#text do
|
||||||
|
local char=text:sub(i,i)
|
||||||
|
if char=="\n" then scr.cx=1;scr.cy=scr.cy+1
|
||||||
|
elseif char=="\t" then scr.cx=scr.cx+(4-((scr.cx-1)%4))
|
||||||
|
elseif char=="\b" then if scr.cx>1 then scr.cx=scr.cx-1;g.set(scr.cx,scr.cy," ") end
|
||||||
|
else g.set(scr.cx,scr.cy,char);scr.cx=scr.cx+1 end
|
||||||
|
if scr.cx>scr.w then scr.cx=1;scr.cy=scr.cy+1 end
|
||||||
|
if scr.cy>scr.h then scroll(scr, g);scr.cy=scr.cy-1 end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function newtty(addr)
|
||||||
|
newid=newid+1
|
||||||
|
kernel.devfs.data["tty"][tostring(newid)]=function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Terminal"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount) end,
|
||||||
|
write=function(content)
|
||||||
|
write(content, addr)
|
||||||
|
end,
|
||||||
|
size=function()
|
||||||
|
return screens[addr].w, screens[addr].h
|
||||||
|
end,
|
||||||
|
clear=function()
|
||||||
|
local g=gpus[1]
|
||||||
|
local scr=screens[addr]
|
||||||
|
if addr~=curaddr then g.bind(addr) end
|
||||||
|
g.fill(1,1,scr.w,scr.h," ");
|
||||||
|
scr.cx,scr.cy=1,1
|
||||||
|
end,
|
||||||
|
gpos=function()
|
||||||
|
return screens[addr].cx, screens[addr].cy
|
||||||
|
end,
|
||||||
|
spos=function(x,y)
|
||||||
|
if not tonumber(x) or not tonumber(y) then error("Invailid arg") end
|
||||||
|
screens[addr].cx, screens[addr].cy = x,y
|
||||||
|
end,
|
||||||
|
sfgc=function(c)
|
||||||
|
screens[addr].fg=c
|
||||||
|
end,
|
||||||
|
sbgc=function(c)
|
||||||
|
screens[addr].bg=c
|
||||||
|
end,
|
||||||
|
gfgc=function()
|
||||||
|
return screens[addr].fg
|
||||||
|
end,
|
||||||
|
gbgc=function()
|
||||||
|
return screens[addr].bg
|
||||||
|
end,
|
||||||
|
isvirt=function()
|
||||||
|
return false
|
||||||
|
end,
|
||||||
|
gplt=function()
|
||||||
|
return plt
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #gpus>=1 and scrnum>=1 then
|
||||||
|
for addr,obj in pairs(screens) do
|
||||||
|
newtty(addr)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
kernel.oc.keyboards={}
|
||||||
|
local kbs=kernel.oc.keyboards
|
||||||
|
|
||||||
|
local kbidx=0
|
||||||
|
local keyboard = {}
|
||||||
|
|
||||||
|
keyboard.keys = {
|
||||||
|
back = 0x0E, -- backspace
|
||||||
|
delete = 0xD3,
|
||||||
|
down = 0xD0,
|
||||||
|
enter = 0x1C,
|
||||||
|
home = 0xC7,
|
||||||
|
lcontrol = 0x1D,
|
||||||
|
left = 0xCB,
|
||||||
|
lmenu = 0x38, -- left Alt
|
||||||
|
lshift = 0x2A,
|
||||||
|
pageDown = 0xD1,
|
||||||
|
rcontrol = 0x9D,
|
||||||
|
right = 0xCD,
|
||||||
|
rmenu = 0xB8, -- right Alt
|
||||||
|
rshift = 0x36,
|
||||||
|
space = 0x39,
|
||||||
|
tab = 0x0F,
|
||||||
|
up = 0xC8,
|
||||||
|
["end"] = 0xCF,
|
||||||
|
numpadenter = 0x9C,
|
||||||
|
}
|
||||||
|
kernel.oc.keys=keyboard.keys
|
||||||
|
|
||||||
|
for i=0, 255 do
|
||||||
|
kernel.oc.keys[string.char(i)]=i
|
||||||
|
end
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function keyboard.isAltDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lmenu] or kbs[addr].pressedCodes[keyboard.keys.rmenu]
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isControl(char)
|
||||||
|
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isControlDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lcontrol] or kbs[addr].pressedCodes[keyboard.keys.rcontrol]
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isKeyDown(addr,charOrCode)
|
||||||
|
if type(charOrCode) == "string" then
|
||||||
|
return kbs[addr].pressedChars[utf8 and utf8.codepoint(charOrCode) or charOrCode:byte()]
|
||||||
|
elseif type(charOrCode) == "number" then
|
||||||
|
return kbs[addr].pressedCodes[charOrCode]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isShiftDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lshift] or kbs[addr].pressedCodes[keyboard.keys.rshift]
|
||||||
|
end
|
||||||
|
kernel.oc.keyboard=keyboard
|
||||||
|
|
||||||
|
local function serializeBool(bool)
|
||||||
|
if bool then
|
||||||
|
return "T"
|
||||||
|
else
|
||||||
|
return "F"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for addr, typ in kernel.oc.component.list("keyboard") do
|
||||||
|
if typ=="keyboard" then
|
||||||
|
kbidx=kbidx+1
|
||||||
|
kbs[addr]={pressedChars = {}, pressedCodes = {}, events=kernel.newFifo()}
|
||||||
|
kernel.devfs.data["input"]["keyboard"..tostring(kbidx)]=function(op,mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Keyboard"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount)
|
||||||
|
local rv=""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
local event = {kernel.oc.keyboards[addr].events.pop()}
|
||||||
|
if event[1] then
|
||||||
|
rv=rv..event[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if rv=="" then rv=nil end
|
||||||
|
return rv
|
||||||
|
end,
|
||||||
|
write=function(content)
|
||||||
|
end,
|
||||||
|
gctrl=function()
|
||||||
|
return serializeBool(keyboard.isControlDown(addr))..";"..serializeBool(keyboard.isAltDown(addr))
|
||||||
|
end,
|
||||||
|
isKeyDown=function(char)
|
||||||
|
return keyboard.isKeyDown(addr,char)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local keys=kernel.oc.keys
|
||||||
|
|
||||||
|
kernel.processes.ocdaemon = function()
|
||||||
|
|
||||||
|
local timeout = false
|
||||||
|
|
||||||
|
kernel.log("OC daemon started")
|
||||||
|
|
||||||
|
local ctrlKeyMap = {
|
||||||
|
[keys.a]=1, [keys.b]=2, [keys.c]=3,
|
||||||
|
[keys.d]=4, [keys.e]=5, [keys.f]=6,
|
||||||
|
[keys.g]=7, [keys.h]=8, [keys.i]=9,
|
||||||
|
[keys.j]=10, [keys.k]=11, [keys.l]=12,
|
||||||
|
[keys.m]=13, [keys.n]=14, [keys.o]=15,
|
||||||
|
[keys.p]=16, [keys.q]=17, [keys.r]=18,
|
||||||
|
[keys.s]=19, [keys.t]=20, [keys.u]=21,
|
||||||
|
[keys.v]=22, [keys.w]=23, [keys.x]=24,
|
||||||
|
[keys.y]=25, [keys.z]=26,
|
||||||
|
}
|
||||||
|
|
||||||
|
local specialKeyMap = {
|
||||||
|
[keys.up] = "\27[A",
|
||||||
|
[keys.down] = "\27[B",
|
||||||
|
[keys.right] = "\27[C",
|
||||||
|
[keys.left] = "\27[D",
|
||||||
|
[keys.home] = "\27[H",
|
||||||
|
[keys["end"]] = "\27[F",
|
||||||
|
--[keys.pageUp] = "\27[5~",
|
||||||
|
--[keys.pageDown] = "\27[6~",
|
||||||
|
[keys.delete] = "\27[3~",
|
||||||
|
[keys.enter] = "\n",
|
||||||
|
[keys.back] = "\b",
|
||||||
|
[keys.tab] = "\t",
|
||||||
|
}
|
||||||
|
|
||||||
|
while true do
|
||||||
|
|
||||||
|
local event = {kernel.EFI.getMachineEvent()}
|
||||||
|
|
||||||
|
if event[1] then
|
||||||
|
|
||||||
|
local eventType = event[1]
|
||||||
|
local addr = event[2]
|
||||||
|
local char = event[3]
|
||||||
|
local keyCode = event[4]
|
||||||
|
|
||||||
|
if eventType == "key_down" then
|
||||||
|
|
||||||
|
local keyboard = kernel.oc.keyboards[addr]
|
||||||
|
|
||||||
|
if keyboard.ctrl then
|
||||||
|
|
||||||
|
local ctrlByte = ctrlKeyMap[keyCode]
|
||||||
|
|
||||||
|
if ctrlByte then
|
||||||
|
if ctrlByte == 3 then
|
||||||
|
|
||||||
|
for _, task in ipairs(syscall.getTasks()) do
|
||||||
|
syscall.sigsend(task, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
keyboard.events.push(string.char(ctrlByte))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
local special = specialKeyMap[keyCode]
|
||||||
|
|
||||||
|
if special then
|
||||||
|
keyboard.events.push(special)
|
||||||
|
|
||||||
|
elseif char and char > 0 then
|
||||||
|
keyboard.events.push(kernel.apis.unicode.char(char))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif eventType == "key_up" then
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
timeout = false
|
||||||
|
|
||||||
|
else
|
||||||
|
timeout = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if timeout then
|
||||||
|
sleep(0.05)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("OC daemon queued for execution")
|
||||||
@@ -0,0 +1,146 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local alpha = 0.85
|
||||||
|
local C_target = 0.01
|
||||||
|
local Tmin = 0.0005
|
||||||
|
local Tmax = 0.5
|
||||||
|
local lambda_budget = 0.08
|
||||||
|
local lambda_clamp = 0.03
|
||||||
|
local lambda_var = 0.02
|
||||||
|
local k_min = 0.5
|
||||||
|
local k_max = 0.5
|
||||||
|
local B = 0.01
|
||||||
|
|
||||||
|
function kernel.main()
|
||||||
|
kernel.log("Starting main loop...")
|
||||||
|
kernel.saveLog()
|
||||||
|
local stopLog=5
|
||||||
|
local logTasks=100
|
||||||
|
|
||||||
|
while not kernel.exitMain do
|
||||||
|
if kernel.config.logTasks then
|
||||||
|
if logTasks<0 then
|
||||||
|
kernel.log("Active Tasks:")
|
||||||
|
for i,v in pairs(kernel.tasks) do
|
||||||
|
kernel.log(v.name.." : "..v.status.." : "..tostring(v.pid).." : "..tostring(v.exit))
|
||||||
|
end
|
||||||
|
kernel.log("[END BLOCK]")
|
||||||
|
logTasks=100
|
||||||
|
end
|
||||||
|
logTasks=logTasks-1
|
||||||
|
end
|
||||||
|
local N = 0
|
||||||
|
local Tmin_hit = 0
|
||||||
|
local Tmax_hit = 0
|
||||||
|
local totalTaskTime = 0
|
||||||
|
local taskTimes = {}
|
||||||
|
|
||||||
|
for pid, task in pairs(kernel.tasks) do
|
||||||
|
if kernel.exitMain then break end
|
||||||
|
|
||||||
|
for _,list in ipairs(kernel.perTaskHooks) do
|
||||||
|
for id,hook in ipairs(list) do
|
||||||
|
local ok,err = xpcall(hook, debug.traceback, task, pid)
|
||||||
|
if not ok then
|
||||||
|
kernel.log("[MAIN HOOK ERR]: "..err, "ERROR", 0xFF0000)
|
||||||
|
list[id]=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if task.status == "R" then
|
||||||
|
N = N + 1
|
||||||
|
task.timeSlice = math.min(Tmax, math.max(Tmin, B / (N ^ alpha)))
|
||||||
|
if task.status == "R" then
|
||||||
|
local startTime = kernel.EFI:getEpochMs()
|
||||||
|
local ret
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Running task " .. tostring(task.pid) .. " (" .. task.name .. ") with time slice " .. string.format("%.3f", task.timeSlice) .. "s", "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
|
||||||
|
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||||
|
|
||||||
|
|
||||||
|
local elapsed = kernel.EFI:getEpochMs() - startTime
|
||||||
|
task.lastTime = elapsed
|
||||||
|
task.totalTime = (task.totalTime or 0) + elapsed
|
||||||
|
task.numRuns = (task.numRuns or 0) + 1
|
||||||
|
|
||||||
|
taskTimes[#taskTimes+1] = elapsed
|
||||||
|
totalTaskTime = totalTaskTime + elapsed
|
||||||
|
|
||||||
|
if elapsed <= Tmin then Tmin_hit = Tmin_hit + 1 end
|
||||||
|
if elapsed >= Tmax then Tmax_hit = Tmax_hit + 1 end
|
||||||
|
|
||||||
|
if ret[1] == "error" or ret[1] == false then
|
||||||
|
kernel.log("processHandlerException: " .. tostring(ret[2]), "ERROR", 0xFF0000)
|
||||||
|
task.status = "Z"
|
||||||
|
task.exit = "processHandlerException: " .. tostring(ret[2])
|
||||||
|
|
||||||
|
elseif ret[1] == "timeout" then
|
||||||
|
task.ivs = task.ivs + 1
|
||||||
|
task.syscallReturn = {}
|
||||||
|
|
||||||
|
elseif ret[1] == "success" or ret[1] == true then
|
||||||
|
task.vs = task.vs + 1
|
||||||
|
|
||||||
|
if ret[2] == "syscall" then
|
||||||
|
local scname = ret[3]
|
||||||
|
if kernel.syscalls[scname] then
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall: " .. scname, "DBUG", 0x00FFFF)
|
||||||
|
for i = 4, #ret do
|
||||||
|
kernel.log(" inval[" .. (i-3) .. "] = " .. tostring(ret[i]), "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local sysret = { xpcall(kernel.syscalls[scname], debug.traceback, table.unpack(ret, 4)) }
|
||||||
|
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
if not sysret[1] then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " failed: " .. tostring(sysret[2]), "ERROR", 0xFF0000)
|
||||||
|
else
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " ok, " .. (#sysret-1) .. " retvals", "DBUG", 0x00FFFF)
|
||||||
|
for i = 2, #sysret do
|
||||||
|
local v = type(sysret[i]) == "table" and table.serialize(sysret[i]) or tostring(sysret[i])
|
||||||
|
kernel.log(" retval[" .. (i-1) .. "] = " .. v, "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not sysret[1] then
|
||||||
|
task.syscallReturn = { false, sysret[2] }
|
||||||
|
else
|
||||||
|
task.syscallReturn = { true, table.unpack(sysret, 2) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
task.syscallReturn = { false, "Unknown syscall: " .. tostring(scname) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.EFI:yield()
|
||||||
|
end
|
||||||
|
|
||||||
|
local T_prev_avg = (N > 0) and (totalTaskTime / N) or 0
|
||||||
|
local T_prev_var = 0
|
||||||
|
for _, t in ipairs(taskTimes) do
|
||||||
|
T_prev_var = T_prev_var + (t - T_prev_avg) ^ 2
|
||||||
|
end
|
||||||
|
if N > 0 then T_prev_var = T_prev_var / N end
|
||||||
|
|
||||||
|
if N > 0 then
|
||||||
|
local f_clamp = k_min * (Tmin_hit / N) - k_max * (Tmax_hit / N)
|
||||||
|
local B_budget = (C_target * (N ^ (alpha - 1))) / math.max(T_prev_avg, 1e-8)
|
||||||
|
B = B + lambda_budget * (B_budget - B) + lambda_clamp * f_clamp - lambda_var * T_prev_var
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.hpv.reapDeadTasks()
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Executed " .. N .. " tasks, avg time: " .. string.format("%.2f", T_prev_avg) .. "ms, var: " .. string.format("%.2f", T_prev_var) .. ", B: " .. string.format("%.5f", B))
|
||||||
|
stopLog = stopLog - 1
|
||||||
|
kernel.saveLog()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
firmware for Advanced Computers
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0.0
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
|
||||||
|
local kbs = {kernel.cct.peripheral.find("tm_keyboard")}
|
||||||
|
|
||||||
|
|
||||||
|
kernel.cct.eventhooks["tm_keyboard_char"]=function(...)
|
||||||
|
|
||||||
|
end
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
+151
-73
@@ -1,4 +1,4 @@
|
|||||||
-- :Minify:--
|
--:Minify:--
|
||||||
local BOOT_DRIVE_PATH = ({...})[1] or "/$"
|
local BOOT_DRIVE_PATH = ({...})[1] or "/$"
|
||||||
---@diagnostic disable-next-line: undefined-global
|
---@diagnostic disable-next-line: undefined-global
|
||||||
local term = term
|
local term = term
|
||||||
@@ -71,7 +71,6 @@ local ok, err = xpcall(function()
|
|||||||
collectgarbage = true,
|
collectgarbage = true,
|
||||||
error = true,
|
error = true,
|
||||||
gcinfo = true,
|
gcinfo = true,
|
||||||
getfenv = true,
|
|
||||||
getmetatable = true,
|
getmetatable = true,
|
||||||
ipairs = true,
|
ipairs = true,
|
||||||
__inext = true,
|
__inext = true,
|
||||||
@@ -85,7 +84,6 @@ local ok, err = xpcall(function()
|
|||||||
rawlen = true,
|
rawlen = true,
|
||||||
rawset = true,
|
rawset = true,
|
||||||
select = true,
|
select = true,
|
||||||
setfenv = true,
|
|
||||||
setmetatable = true,
|
setmetatable = true,
|
||||||
string = true,
|
string = true,
|
||||||
table = true,
|
table = true,
|
||||||
@@ -154,17 +152,106 @@ local ok, err = xpcall(function()
|
|||||||
if not initFs then displaySuperBadError("Could not load initdisks.") end
|
if not initFs then displaySuperBadError("Could not load initdisks.") end
|
||||||
if not fs then displaySuperBadError("Could not load initfs.") end
|
if not fs then displaySuperBadError("Could not load initfs.") end
|
||||||
|
|
||||||
|
if not apis.fs.exists("/nvram.dat") then
|
||||||
|
local file = apis.fs.open("/nvram.dat", "w")
|
||||||
|
file.write("Hello, World!")
|
||||||
|
file.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local eeprom
|
||||||
|
if apis.fs.exists("/startup.lua") then
|
||||||
|
eeprom="/startup.lua"
|
||||||
|
elseif apis.fs.exists("/eeprom") then
|
||||||
|
eeprom="/eeprom"
|
||||||
|
end
|
||||||
|
|
||||||
local eventQueue = {}
|
local eventQueue = {}
|
||||||
|
|
||||||
local function queueEvent(event, ...)
|
local function queueEvent(event, ...)
|
||||||
table.insert(eventQueue, {event, ...})
|
table.insert(eventQueue, {event, ...})
|
||||||
end
|
end
|
||||||
|
|
||||||
local computer = {
|
local colors = {
|
||||||
time = function() return apis.os.epoch("utc") end,
|
[0x000000]=0x0001,
|
||||||
clock = function() return apis.os.clock() * 1000 end,
|
[0xFFFFFF]=0x0002,
|
||||||
shutdown = apis.os.shutdown,
|
[0xFF0000]=0x0004,
|
||||||
reboot = apis.os.reboot,
|
[0x00FF00]=0x0008,
|
||||||
|
[0x0000FF]=0x0010,
|
||||||
|
[0x00FFFF]=0x0020,
|
||||||
|
[0xFF00FF]=0x0040,
|
||||||
|
[0xFFFF00]=0x0080,
|
||||||
|
[0xFF6D00]=0x0100,
|
||||||
|
[0x6DFF55]=0x0200,
|
||||||
|
[0x24FFFF]=0x0400,
|
||||||
|
[0x924900]=0x0800,
|
||||||
|
[0x6D6D55]=0x1000,
|
||||||
|
[0xDBDBAA]=0x2000,
|
||||||
|
[0x6D00FF]=0x4000,
|
||||||
|
[0xB6FF00]=0x8000
|
||||||
|
}
|
||||||
|
|
||||||
|
local fg,bg=0x6D6D55,0x000000
|
||||||
|
local l1f,l1d,l2,ops={},{},{},0
|
||||||
|
|
||||||
|
local function findClosest(tbl, target)
|
||||||
|
local closest = nil
|
||||||
|
local smallestDiff = math.huge
|
||||||
|
|
||||||
|
for k, _ in pairs(tbl) do
|
||||||
|
local diff = math.abs(k - target)
|
||||||
|
if diff < smallestDiff then
|
||||||
|
smallestDiff = diff
|
||||||
|
closest = k
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return closest
|
||||||
|
end
|
||||||
|
|
||||||
|
local function aprox(c24)
|
||||||
|
ops = ops + 1
|
||||||
|
|
||||||
|
if ops % 1024 == 0 then
|
||||||
|
l1d = {}
|
||||||
|
l1f = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if ops % 8192 == 0 then
|
||||||
|
l2 = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if l2[c24] ~= nil then
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
if l1d[c24] ~= nil then
|
||||||
|
l1f[c24] = l1f[c24] + 1
|
||||||
|
|
||||||
|
if l1f[c24] >= 16 then
|
||||||
|
l2[c24] = l1d[c24]
|
||||||
|
l1d[c24] = nil
|
||||||
|
l1f[c24] = nil
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
return l1d[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
local closestKey = findClosest(colors, c24)
|
||||||
|
if not closestKey then return nil end
|
||||||
|
|
||||||
|
local value = colors[closestKey]
|
||||||
|
|
||||||
|
l1d[c24] = value
|
||||||
|
l1f[c24] = 1
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
local EFI = {
|
||||||
|
getEpochMs = function() return apis.os.epoch("utc") end,
|
||||||
|
getUptime = function() return apis.os.clock() * 1000 end,
|
||||||
|
date = function() return apis.os.date("!%Y-%m-%dT%H:%M:%SZ", apis.os.epoch("utc") / 1000) end,
|
||||||
getMachineEvent = function()
|
getMachineEvent = function()
|
||||||
if #eventQueue > 0 then
|
if #eventQueue > 0 then
|
||||||
return table.unpack(table.remove(eventQueue, 1))
|
return table.unpack(table.remove(eventQueue, 1))
|
||||||
@@ -172,61 +259,15 @@ local ok, err = xpcall(function()
|
|||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
getEEPROM = function() return getFile("/startup.lua") end,
|
getEEPROM = function() return getFile(eeprom) end,
|
||||||
setEEPROM = function(_, text)
|
setEEPROM = function(_, text)
|
||||||
local h = apis.fs.open("/startup.lua", "w")
|
local h = apis.fs.open(eeprom, "w")
|
||||||
h.write(text)
|
h.write(text)
|
||||||
h.close()
|
h.close()
|
||||||
end
|
end,
|
||||||
}
|
initfs=fs,
|
||||||
|
disks=initFs,
|
||||||
local icolors = {
|
screenCtl={
|
||||||
[0x1] = 1, -- #000000
|
|
||||||
[0x2] = 2, -- #FFFFFF
|
|
||||||
[0x4] = 3, -- #FF0000
|
|
||||||
[0x8] = 4, -- #00FF00
|
|
||||||
[0x10] = 5, -- #0000FF
|
|
||||||
[0x20] = 6, -- #00FFFF
|
|
||||||
[0x40] = 7, -- #FF00FF
|
|
||||||
[0x80] = 8, -- #FFFF00
|
|
||||||
[0x100] = 9, -- #FF6D00
|
|
||||||
[0x200] = 10, -- #6DFF55
|
|
||||||
[0x400] = 11, -- #24FFFF
|
|
||||||
[0x800] = 12, -- #924900
|
|
||||||
[0x1000] = 13, -- #6D6D55
|
|
||||||
[0x2000] = 14, -- #DBDBAA
|
|
||||||
[0x4000] = 15, -- #6D00FF
|
|
||||||
[0x8000] = 16 -- #B6FF00
|
|
||||||
}
|
|
||||||
|
|
||||||
local colors = {
|
|
||||||
0x0001, -- #000000
|
|
||||||
0x0002, -- #FFFFFF
|
|
||||||
0x0004, -- #FF0000
|
|
||||||
0x0008, -- #00FF00
|
|
||||||
0x0010, -- #0000FF
|
|
||||||
0x0020, -- #00FFFF
|
|
||||||
0x0040, -- #FF00FF
|
|
||||||
0x0080, -- #FFFF00
|
|
||||||
0x0100, -- #FF6D00
|
|
||||||
0x0200, -- #6DFF55
|
|
||||||
0x0400, -- #24FFFF
|
|
||||||
0x0800, -- #924900
|
|
||||||
0x1000, -- #6D6D55
|
|
||||||
0x2000, -- #DBDBAA
|
|
||||||
0x4000, -- #6D00FF
|
|
||||||
0x8000 -- #B6FF00
|
|
||||||
}
|
|
||||||
|
|
||||||
apis.term.setBackgroundColor(0x8000)
|
|
||||||
apis.term.setTextColor(0x1000)
|
|
||||||
apis.term.clear()
|
|
||||||
apis.term.setCursorPos(1, 1)
|
|
||||||
|
|
||||||
local kernelCoro = coroutine.create(function()
|
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
|
||||||
local ok, err = xpcall(Kernel, debug.traceback, apis, initFs, "cct", "/sbin/init",
|
|
||||||
{
|
|
||||||
print = function(_, text) write(text .. "\n") end,
|
print = function(_, text) write(text .. "\n") end,
|
||||||
printInline = function(_, text) write(text) end,
|
printInline = function(_, text) write(text) end,
|
||||||
clear = function()
|
clear = function()
|
||||||
@@ -239,26 +280,51 @@ local ok, err = xpcall(function()
|
|||||||
getCursorPos = function() return apis.term.getCursorPos() end,
|
getCursorPos = function() return apis.term.getCursorPos() end,
|
||||||
getSize = function() return apis.term.getSize() end,
|
getSize = function() return apis.term.getSize() end,
|
||||||
setBackgroundColor = function(_, color)
|
setBackgroundColor = function(_, color)
|
||||||
apis.term.setBackgroundColor(colors[color])
|
apis.term.setBackgroundColor(aprox(color))
|
||||||
end,
|
end,
|
||||||
setTextColor = function(_, color)
|
setTextColor = function(_, color)
|
||||||
apis.term.setTextColor(colors[color])
|
apis.term.setTextColor(aprox(color))
|
||||||
end,
|
end,
|
||||||
getBackgroundColor = function()
|
getBackgroundColor = function()
|
||||||
return icolors[apis.term.getBackgroundColor()]
|
return bg
|
||||||
end,
|
end,
|
||||||
getTextColor = function()
|
getTextColor = function()
|
||||||
return icolors[apis.term.getTextColor()]
|
return fg
|
||||||
end
|
end,
|
||||||
}, computer, fs, "$")
|
enable=function() end,
|
||||||
if not ok then displaySuperBadError(err) end
|
disable=function() end
|
||||||
|
},
|
||||||
|
architecture="cct",
|
||||||
|
getNvram = function() return getFile("/nvram.dat") end,
|
||||||
|
setNvram = function(_, text)
|
||||||
|
local h = apis.fs.open("/nvram.dat", "w")
|
||||||
|
h.write(text)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
firmware=apis,
|
||||||
|
reboot=false
|
||||||
|
}
|
||||||
|
|
||||||
|
apis.term.setBackgroundColor(0x8000)
|
||||||
|
apis.term.setTextColor(0x1000)
|
||||||
|
apis.term.clear()
|
||||||
|
apis.term.setCursorPos(1, 1)
|
||||||
|
|
||||||
|
local kernelCoro = coroutine.create(function()
|
||||||
|
---@diagnostic disable-next-line: param-type-mismatch
|
||||||
|
local ok, err = xpcall(Kernel, debug.traceback, EFI)
|
||||||
|
if not ok and not EFI.reboot then displaySuperBadError(err) end
|
||||||
|
if err then
|
||||||
|
apis.os.reboot()
|
||||||
|
else
|
||||||
|
apis.os.shutdown()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- time is in milliseconds
|
function EFI.resumeWithTimeout(co, timeout, ...)
|
||||||
function coroutine.resumeWithTimeout(co, timeout, ...)
|
local startTime = EFI.getEpochMs()
|
||||||
local startTime = computer.time()
|
|
||||||
debug.sethook(co, function()
|
debug.sethook(co, function()
|
||||||
if computer.time() > startTime + timeout then
|
if EFI.getEpochMs() > startTime + timeout then
|
||||||
return coroutine.yield("timeout")
|
return coroutine.yield("timeout")
|
||||||
end
|
end
|
||||||
end, "", 1000)
|
end, "", 1000)
|
||||||
@@ -276,7 +342,7 @@ local ok, err = xpcall(function()
|
|||||||
write("Loaded in " .. tostring(apis.os.clock()) .. " seconds.\n")
|
write("Loaded in " .. tostring(apis.os.clock()) .. " seconds.\n")
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local status, err = coroutine.resumeWithTimeout(kernelCoro, 50)
|
local status, err = EFI.resumeWithTimeout(kernelCoro, 50)
|
||||||
apis.os.queueEvent("NoSleep")
|
apis.os.queueEvent("NoSleep")
|
||||||
local exit = false
|
local exit = false
|
||||||
while not exit do
|
while not exit do
|
||||||
@@ -294,16 +360,28 @@ local ok, err = xpcall(function()
|
|||||||
queueEvent("componentAdded", "disk")
|
queueEvent("componentAdded", "disk")
|
||||||
elseif event[1] == "disk_eject" then
|
elseif event[1] == "disk_eject" then
|
||||||
queueEvent("componentRemoved", "disk")
|
queueEvent("componentRemoved", "disk")
|
||||||
|
elseif event[1] == "modem_message" then
|
||||||
|
queueEvent("modem_message", table.unpack(event, 2))
|
||||||
|
elseif event[1] == "rednet_message" then
|
||||||
|
queueEvent("rednet_message", table.unpack(event, 2))
|
||||||
|
elseif event[1] == "http_success" then
|
||||||
|
queueEvent("http_success", table.unpack(event, 2))
|
||||||
|
elseif event[1] == "http_failure" then
|
||||||
|
queueEvent("http_failure", table.unpack(event, 2))
|
||||||
elseif event[1] == "NoSleep" then
|
elseif event[1] == "NoSleep" then
|
||||||
exit = true
|
exit = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if status == "error" or coroutine.status(kernelCoro) == "dead" then
|
if status == "error" or coroutine.status(kernelCoro) == "dead" then
|
||||||
|
if EFI.reboot then
|
||||||
|
apis.os.reboot()
|
||||||
|
end
|
||||||
displaySuperBadError("Kernel error: " .. tostring(err))
|
displaySuperBadError("Kernel error: " .. tostring(err))
|
||||||
coroutine.yield("key")
|
coroutine.yield("key")
|
||||||
end
|
end
|
||||||
|
initFs:refresh()
|
||||||
end
|
end
|
||||||
end, debug.traceback)
|
end, debug.traceback)
|
||||||
|
|
||||||
if not ok then displaySuperBadError("Fatal error during boot: " .. err) end
|
if not ok then displaySuperBadError("Fatal error during boot: " .. err) end
|
||||||
while true do coroutine.yield() end
|
while true do coroutine.yield("key") end
|
||||||
@@ -0,0 +1,272 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local apis = ({...})[1]
|
||||||
|
local BOOT_DRIVE_PATH = apis.BOOT_DRIVE_PATH
|
||||||
|
local fs = apis.fs
|
||||||
|
local native = apis.peripheral
|
||||||
|
local peripheral = {}
|
||||||
|
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
||||||
|
|
||||||
|
function peripheral.getNames()
|
||||||
|
local results = {}
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.isPresent(side) then
|
||||||
|
table.insert(results, side)
|
||||||
|
if native.hasType(side, "peripheral_hub") then
|
||||||
|
local remote = native.call(side, "getNamesRemote")
|
||||||
|
for _, name in ipairs(remote) do
|
||||||
|
table.insert(results, name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.isPresent(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getType(peripheral)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.getType(peripheral)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "getTypeRemote", peripheral)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return table.unpack(mt.types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.hasType(peripheral, peripheral_type)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.hasType(peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "hasTypeRemote", peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.types[peripheral_type] ~= nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getMethods(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.getMethods(name)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "getMethodsRemote", name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getName(peripheral)
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.call(name, method, ...)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.call(name, method, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "callRemote", name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.wrap(name)
|
||||||
|
local methods = peripheral.getMethods(name)
|
||||||
|
if not methods then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local types = { peripheral.getType(name) }
|
||||||
|
for i = 1, #types do types[types[i]] = true end
|
||||||
|
local result = setmetatable({}, {
|
||||||
|
__name = "peripheral",
|
||||||
|
name = name,
|
||||||
|
type = types[1],
|
||||||
|
types = types,
|
||||||
|
})
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
result[method] = function(...)
|
||||||
|
return peripheral.call(name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.find(ty, filter)
|
||||||
|
local results = {}
|
||||||
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
|
if peripheral.hasType(name, ty) then
|
||||||
|
local wrapped = peripheral.wrap(name)
|
||||||
|
if filter == nil or filter(name, wrapped) then
|
||||||
|
table.insert(results, wrapped)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.unpack(results)
|
||||||
|
end
|
||||||
|
|
||||||
|
local disks = {}
|
||||||
|
local internal = {}
|
||||||
|
|
||||||
|
local function norm(path)
|
||||||
|
if not path or path == "" then return "/" end
|
||||||
|
return fs.combine("/", path)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function createDisk(id, basePath, readonly, periph)
|
||||||
|
basePath = norm(basePath)
|
||||||
|
|
||||||
|
local disk = {address = id, isReadOnly = function() return readonly end}
|
||||||
|
|
||||||
|
function disk:spaceUsed()
|
||||||
|
return fs.getCapacity(basePath) - fs.getFreeSpace(basePath)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:spaceTotal() return fs.getCapacity(basePath) end
|
||||||
|
|
||||||
|
function disk:list(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if not fs.exists(p) or not fs.isDir(p) then
|
||||||
|
return nil, "not directory"
|
||||||
|
end
|
||||||
|
return fs.list(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:fileExists(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.exists(p) and not fs.isDir(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:directoryExists(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.exists(p) and fs.isDir(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:type(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if not fs.exists(p) then
|
||||||
|
return nil
|
||||||
|
elseif fs.isDir(p) then
|
||||||
|
return "directory"
|
||||||
|
else
|
||||||
|
return "file"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:makeDirectory(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
fs.makeDir(p)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:remove(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if fs.exists(p) then fs.delete(p) end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:setLabel(label) periph.setLabel(label) end
|
||||||
|
|
||||||
|
function disk:getLabel(label) return periph.getLabel() end
|
||||||
|
|
||||||
|
function disk:attributes(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.attributes(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path, mode)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.open(p, mode)
|
||||||
|
end
|
||||||
|
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
internal["$"] = createDisk("$", BOOT_DRIVE_PATH, false, {
|
||||||
|
setLabel = function(label)
|
||||||
|
local h = fs.open("/.label", "w")
|
||||||
|
h.write(label)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
getLabel = function()
|
||||||
|
local h = fs.open("/.label", "r")
|
||||||
|
if not h then return "$" end
|
||||||
|
local label = h.readAll()
|
||||||
|
h.close()
|
||||||
|
return label
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
internal["rom"] = createDisk("rom", "/rom", true, {
|
||||||
|
setLabel = function(label)
|
||||||
|
error("Device is read-only")
|
||||||
|
end,
|
||||||
|
getLabel = function()
|
||||||
|
return "cctrom"
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
local function refresh()
|
||||||
|
disks={}
|
||||||
|
for _, disk in ipairs({peripheral.find("drive")}) do
|
||||||
|
if disk.isDiskPresent() then
|
||||||
|
disks[tostring(disk.getDiskID())]=createDisk("cctdisk"..tostring(disk.getDiskID()), disk.getMountPath(), false, fs)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function iter()
|
||||||
|
refresh()
|
||||||
|
local combined = {}
|
||||||
|
|
||||||
|
for id, obj in pairs(internal) do combined[id] = obj end
|
||||||
|
for id, obj in pairs(disks) do combined[id] = obj end
|
||||||
|
|
||||||
|
return pairs(combined)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {refresh = refresh, list = iter}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
|
||||||
|
if kernel.gfx then
|
||||||
|
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
firmware for CraftOS PC
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0.0
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
@@ -1,155 +0,0 @@
|
|||||||
-- :Minify:--
|
|
||||||
local apis = ({...})[1]
|
|
||||||
local BOOT_DRIVE_PATH = apis.BOOT_DRIVE_PATH or "/$"
|
|
||||||
local fs = apis.fs
|
|
||||||
local native = apis.peripheral
|
|
||||||
local peripheral = {}
|
|
||||||
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
|
||||||
|
|
||||||
function peripheral.getType(name)
|
|
||||||
if native.isPresent(name) then return native.getType(name) end
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and
|
|
||||||
native.call(side, "isPresentRemote", name) then
|
|
||||||
return native.call(side, "getTypeRemote", name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.getNames()
|
|
||||||
local names = {}
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.isPresent(side) then table.insert(names, side) end
|
|
||||||
if native.hasType(side, "peripheral_hub") then
|
|
||||||
local hubSides = native.call(side, "getConnectedSides")
|
|
||||||
for _, hubSide in ipairs(hubSides) do
|
|
||||||
table.insert(names, hubSide)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return names
|
|
||||||
end
|
|
||||||
|
|
||||||
local disks = {}
|
|
||||||
local internal = {}
|
|
||||||
|
|
||||||
local function norm(path)
|
|
||||||
if not path or path == "" then return "/" end
|
|
||||||
return fs.combine("/", path)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function createDisk(id, basePath, readonly, periph)
|
|
||||||
basePath = norm(basePath)
|
|
||||||
|
|
||||||
local disk = {address = id, isReadOnly = function() return readonly end}
|
|
||||||
|
|
||||||
function disk:spaceUsed()
|
|
||||||
return fs.getCapacity(basePath) - fs.getFreeSpace(basePath)
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:spaceTotal() return fs.getCapacity(basePath) end
|
|
||||||
|
|
||||||
function disk:list(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
if not fs.exists(p) or not fs.isDir(p) then
|
|
||||||
return nil, "not directory"
|
|
||||||
end
|
|
||||||
return fs.list(p)
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:fileExists(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
return fs.exists(p) and not fs.isDir(p)
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:directoryExists(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
return fs.exists(p) and fs.isDir(p)
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:type(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
if not fs.exists(p) then
|
|
||||||
return nil
|
|
||||||
elseif fs.isDir(p) then
|
|
||||||
return "directory"
|
|
||||||
else
|
|
||||||
return "file"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:makeDirectory(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
fs.makeDir(p)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:remove(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
if fs.exists(p) then fs.delete(p) end
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:setLabel(label) periph.setLabel(label) end
|
|
||||||
|
|
||||||
function disk:getLabel(label) return periph.getLabel() end
|
|
||||||
|
|
||||||
function disk:attributes(path)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
return fs.attributes(p)
|
|
||||||
end
|
|
||||||
|
|
||||||
function disk:open(path, mode)
|
|
||||||
local p = fs.combine(basePath, path)
|
|
||||||
return fs.open(p, mode)
|
|
||||||
end
|
|
||||||
|
|
||||||
return disk
|
|
||||||
end
|
|
||||||
|
|
||||||
internal["$"] = createDisk("$", BOOT_DRIVE_PATH, false, {
|
|
||||||
setLabel = function(label)
|
|
||||||
local h = fs.open("/.label", "w")
|
|
||||||
h.write(label)
|
|
||||||
h.close()
|
|
||||||
end,
|
|
||||||
getLabel = function()
|
|
||||||
local h = fs.open("/.label", "r")
|
|
||||||
if not h then return "$" end
|
|
||||||
local label = h.readAll()
|
|
||||||
h.close()
|
|
||||||
return label
|
|
||||||
end
|
|
||||||
})
|
|
||||||
|
|
||||||
local function refresh()
|
|
||||||
for id, _ in pairs(disks) do
|
|
||||||
if not peripheral.getType(id) then disks[id] = nil end
|
|
||||||
end
|
|
||||||
|
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
|
||||||
if peripheral.getType(name) == "disk" then
|
|
||||||
if not disks[name] then
|
|
||||||
local mount = disk.getMountPath(name)
|
|
||||||
if mount then
|
|
||||||
disks[name] = createDisk(name, mount, false, disk)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function iter()
|
|
||||||
refresh()
|
|
||||||
local combined = {}
|
|
||||||
|
|
||||||
for id, obj in pairs(internal) do combined[id] = obj end
|
|
||||||
for id, obj in pairs(disks) do combined[id] = obj end
|
|
||||||
|
|
||||||
return pairs(combined)
|
|
||||||
end
|
|
||||||
|
|
||||||
return {refresh = refresh, list = iter}
|
|
||||||
@@ -0,0 +1,589 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local BOOT_DRIVE_PATH = ({...})[1] or "/$"
|
||||||
|
---@diagnostic disable-next-line: undefined-global
|
||||||
|
local lterm = term
|
||||||
|
local function write(text, term)
|
||||||
|
local x, y = term.getCursorPos()
|
||||||
|
local w, h = term.getSize()
|
||||||
|
|
||||||
|
for i = 1, #text do
|
||||||
|
local c = text:sub(i, i)
|
||||||
|
|
||||||
|
if c == "\n" then
|
||||||
|
y = y + 1
|
||||||
|
x = 1
|
||||||
|
elseif c == "\t" then
|
||||||
|
local tabSize = 4
|
||||||
|
local spaces = tabSize - ((x - 1) % tabSize)
|
||||||
|
term.write(string.rep(" ", spaces))
|
||||||
|
x = x + spaces
|
||||||
|
elseif c == "\b" then
|
||||||
|
if x > 1 then
|
||||||
|
x = x - 1
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
term.write(" ")
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if x <= w and y <= h then
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
term.write(c)
|
||||||
|
x = x + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if x > w then
|
||||||
|
x = 1
|
||||||
|
y = y + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if y - 1 >= h then
|
||||||
|
term.scroll(1)
|
||||||
|
y = h
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function displaySuperBadError(err, noyield)
|
||||||
|
lterm.setBackgroundColor(0x1)
|
||||||
|
lterm.setTextColor(0x4)
|
||||||
|
lterm.clear()
|
||||||
|
lterm.setCursorPos(1, 1)
|
||||||
|
lterm.write("A critical error occurred while loading the system:")
|
||||||
|
lterm.setCursorPos(1, 3)
|
||||||
|
write(err, lterm)
|
||||||
|
coroutine.yield("key")
|
||||||
|
end
|
||||||
|
|
||||||
|
term.setCursorBlink(false)
|
||||||
|
local ok, err = xpcall(function()
|
||||||
|
--p
|
||||||
|
local apis = {BOOT_DRIVE_PATH = BOOT_DRIVE_PATH}
|
||||||
|
|
||||||
|
local lua = {
|
||||||
|
coroutine = true,
|
||||||
|
debug = true,
|
||||||
|
_VERSION = true,
|
||||||
|
assert = true,
|
||||||
|
collectgarbage = true,
|
||||||
|
error = true,
|
||||||
|
gcinfo = true,
|
||||||
|
getmetatable = true,
|
||||||
|
ipairs = true,
|
||||||
|
__inext = true,
|
||||||
|
load = true,
|
||||||
|
math = true,
|
||||||
|
next = true,
|
||||||
|
pairs = true,
|
||||||
|
pcall = true,
|
||||||
|
rawequal = true,
|
||||||
|
rawget = true,
|
||||||
|
rawlen = true,
|
||||||
|
rawset = true,
|
||||||
|
select = true,
|
||||||
|
setmetatable = true,
|
||||||
|
string = true,
|
||||||
|
table = true,
|
||||||
|
tonumber = true,
|
||||||
|
tostring = true,
|
||||||
|
type = true,
|
||||||
|
xpcall = true,
|
||||||
|
_G = true
|
||||||
|
}
|
||||||
|
|
||||||
|
local debug = debug
|
||||||
|
for i, v in pairs(_G) do
|
||||||
|
if not lua[i] or lua[i] == nil then
|
||||||
|
apis[i] = v
|
||||||
|
_G[i] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
--p
|
||||||
|
|
||||||
|
local acekeys={
|
||||||
|
[apis.keys.enter]="\n",
|
||||||
|
[apis.keys.tab]="\t",
|
||||||
|
[apis.keys.backspace]="\b",
|
||||||
|
[apis.keys.up]="\17",
|
||||||
|
[apis.keys.down]="\18",
|
||||||
|
[apis.keys.left]="\19",
|
||||||
|
[apis.keys.right]="\20",
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(time)
|
||||||
|
local stoptime = apis.os.clock() + (time)
|
||||||
|
while stoptime > apis.os.clock() do end
|
||||||
|
end
|
||||||
|
|
||||||
|
apis.term.setPaletteColor(0x1, 0xFFFFFF) -- #000000
|
||||||
|
apis.term.setPaletteColor(0x2, 0xFF0000) -- #FFFFFF
|
||||||
|
apis.term.setPaletteColor(0x4, 0x00FF00) -- #FF0000
|
||||||
|
apis.term.setPaletteColor(0x8, 0x0000FF) -- #00FF00
|
||||||
|
apis.term.setPaletteColor(0x10, 0x00FFFF) -- #0000FF
|
||||||
|
apis.term.setPaletteColor(0x20, 0xFF00FF) -- #00FFFF
|
||||||
|
apis.term.setPaletteColor(0x40, 0xFFFF00) -- #FF00FF
|
||||||
|
apis.term.setPaletteColor(0x80, 0xFF6D00) -- #FFFF00
|
||||||
|
apis.term.setPaletteColor(0x100, 0x6DFF55) -- #FF6D00
|
||||||
|
apis.term.setPaletteColor(0x200, 0x24FFFF) -- #6DFF55
|
||||||
|
apis.term.setPaletteColor(0x400, 0x924900) -- #24FFFF
|
||||||
|
apis.term.setPaletteColor(0x800, 0x6D6D55) -- #924900
|
||||||
|
apis.term.setPaletteColor(0x1000, 0xDBDBAA) -- #6D6D55
|
||||||
|
apis.term.setPaletteColor(0x2000, 0x6D00FF) -- #DBDBAA
|
||||||
|
apis.term.setPaletteColor(0x4000, 0xB6FF00) -- #6D00FF
|
||||||
|
apis.term.setPaletteColor(0x8000, 0x000000) -- #B6FF00
|
||||||
|
|
||||||
|
local function getFile(path)
|
||||||
|
local file = apis.fs.open(path, "r")
|
||||||
|
if not file then
|
||||||
|
displaySuperBadError("Could not open file: " .. path)
|
||||||
|
end
|
||||||
|
local content = file.readAll()
|
||||||
|
file.close()
|
||||||
|
return content
|
||||||
|
end
|
||||||
|
|
||||||
|
local Kernel = load(getFile(BOOT_DRIVE_PATH .. "/kernel.lua"),"@Kernel")
|
||||||
|
local initFs = load(getFile(BOOT_DRIVE_PATH .. "/cct/initdisks"),"@Init_disks")(apis)
|
||||||
|
local fs = load(getFile(BOOT_DRIVE_PATH .. "/initfs"), "@InitFs")()
|
||||||
|
|
||||||
|
if not Kernel then displaySuperBadError("Could not load kernel.") end
|
||||||
|
if not initFs then displaySuperBadError("Could not load initdisks.") end
|
||||||
|
if not fs then displaySuperBadError("Could not load initfs.") end
|
||||||
|
|
||||||
|
if not apis.fs.exists("/nvram.dat") then
|
||||||
|
local file = apis.fs.open("/nvram.dat", "w")
|
||||||
|
file.write("Hello, World!")
|
||||||
|
file.close()
|
||||||
|
end
|
||||||
|
|
||||||
|
local eeprom
|
||||||
|
if apis.fs.exists("/startup.lua") then
|
||||||
|
eeprom="/startup.lua"
|
||||||
|
elseif apis.fs.exists("/eeprom") then
|
||||||
|
eeprom="/eeprom"
|
||||||
|
end
|
||||||
|
|
||||||
|
local eventQueue = {}
|
||||||
|
|
||||||
|
local function queueEvent(event, ...)
|
||||||
|
table.insert(eventQueue, {event, ...})
|
||||||
|
end
|
||||||
|
|
||||||
|
local colors = {
|
||||||
|
[0xFFFFFF]=0x0001,
|
||||||
|
[0xFF0000]=0x0002,
|
||||||
|
[0x00FF00]=0x0004,
|
||||||
|
[0x0000FF]=0x0008,
|
||||||
|
[0x00FFFF]=0x0010,
|
||||||
|
[0xFF00FF]=0x0020,
|
||||||
|
[0xFFFF00]=0x0040,
|
||||||
|
[0xFF6D00]=0x0080,
|
||||||
|
[0x6DFF55]=0x0100,
|
||||||
|
[0x24FFFF]=0x0200,
|
||||||
|
[0x924900]=0x0400,
|
||||||
|
[0x6D6D55]=0x0800,
|
||||||
|
[0xDBDBAA]=0x1000,
|
||||||
|
[0x6D00FF]=0x2000,
|
||||||
|
[0xB6FF00]=0x4000,
|
||||||
|
[0x000000]=0x8000
|
||||||
|
}
|
||||||
|
|
||||||
|
local fg,bg=0x6D6D55,0x000000
|
||||||
|
local l1f,l1d,l2,ops={},{},{},0
|
||||||
|
|
||||||
|
local function findClosest(tbl, target)
|
||||||
|
local closest = nil
|
||||||
|
local smallestDiff = math.huge
|
||||||
|
|
||||||
|
for k, _ in pairs(tbl) do
|
||||||
|
if k==target then return k end
|
||||||
|
local diff = math.abs(k - target)
|
||||||
|
if diff < smallestDiff then
|
||||||
|
smallestDiff = diff
|
||||||
|
closest = k
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return closest
|
||||||
|
end
|
||||||
|
|
||||||
|
local function aprox(c24)
|
||||||
|
ops = ops + 1
|
||||||
|
|
||||||
|
if ops % 1024 == 0 then
|
||||||
|
l1d = {}
|
||||||
|
l1f = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if ops % 8192 == 0 then
|
||||||
|
l2 = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if l2[c24] ~= nil then
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
if l1d[c24] ~= nil then
|
||||||
|
l1f[c24] = l1f[c24] + 1
|
||||||
|
|
||||||
|
if l1f[c24] >= 16 then
|
||||||
|
l2[c24] = l1d[c24]
|
||||||
|
l1d[c24] = nil
|
||||||
|
l1f[c24] = nil
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
return l1d[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
local closestKey = findClosest(colors, c24)
|
||||||
|
if not closestKey then return nil end
|
||||||
|
|
||||||
|
local value = colors[closestKey]
|
||||||
|
|
||||||
|
l1d[c24] = value
|
||||||
|
l1f[c24] = 1
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
--p
|
||||||
|
local p={}
|
||||||
|
local native = apis.peripheral
|
||||||
|
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
||||||
|
|
||||||
|
function p.getNames()
|
||||||
|
local results = {}
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.isPresent(side) then
|
||||||
|
table.insert(results, side)
|
||||||
|
if native.hasType(side, "peripheral_hub") then
|
||||||
|
local remote = native.call(side, "getNamesRemote")
|
||||||
|
for _, name in ipairs(remote) do
|
||||||
|
table.insert(results, name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.isPresent(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.getType(peripheral)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.getType(peripheral)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "getTypeRemote", peripheral)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return table.unpack(mt.types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.hasType(peripheral, peripheral_type)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.hasType(peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "hasTypeRemote", peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.types[peripheral_type] ~= nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.getMethods(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.getMethods(name)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "getMethodsRemote", name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.getName(peripheral)
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.call(name, method, ...)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.call(name, method, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "callRemote", name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.wrap(name)
|
||||||
|
local methods = p.getMethods(name)
|
||||||
|
if not methods then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local types = { p.getType(name) }
|
||||||
|
for i = 1, #types do types[types[i]] = true end
|
||||||
|
local result = setmetatable({}, {
|
||||||
|
__name = "peripheral",
|
||||||
|
name = name,
|
||||||
|
type = types[1],
|
||||||
|
types = types,
|
||||||
|
})
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
result[method] = function(...)
|
||||||
|
return p.call(name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function p.find(ty, filter)
|
||||||
|
local results = {}
|
||||||
|
for _, name in ipairs(p.getNames()) do
|
||||||
|
if p.hasType(name, ty) then
|
||||||
|
local wrapped = p.wrap(name)
|
||||||
|
if filter == nil or filter(name, wrapped) then
|
||||||
|
table.insert(results, wrapped)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.unpack(results)
|
||||||
|
end
|
||||||
|
--p
|
||||||
|
local allscreens = {p.find("monitor")}
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].setTextScale(.5)
|
||||||
|
allscreens[i].clear()
|
||||||
|
allscreens[i].setCursorPos(1,1)
|
||||||
|
end
|
||||||
|
|
||||||
|
allscreens[#allscreens+1] = apis.term
|
||||||
|
|
||||||
|
local callAsyncQueue = {}
|
||||||
|
local callAsyncReturn = {}
|
||||||
|
apis.callAsyncReturn = callAsyncReturn
|
||||||
|
local callidx=0
|
||||||
|
|
||||||
|
function apis.callAsync(func, ...)
|
||||||
|
callidx=callidx+1
|
||||||
|
callAsyncQueue[#callAsyncQueue+1] = {callidx, func, ...}
|
||||||
|
return callidx
|
||||||
|
end
|
||||||
|
|
||||||
|
local EFI = {
|
||||||
|
yield=function() end,
|
||||||
|
getEpochMs = function() return apis.os.epoch("utc") end,
|
||||||
|
getUptime = function() return apis.os.clock() * 1000 end,
|
||||||
|
date = function() return apis.os.date("!%Y-%m-%dT%H:%M:%SZ", apis.os.epoch("utc") / 1000) end,
|
||||||
|
getMachineEvent = function()
|
||||||
|
if #eventQueue > 0 then
|
||||||
|
return table.unpack(table.remove(eventQueue, 1))
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
getEEPROM = function() return getFile(eeprom) end,
|
||||||
|
setEEPROM = function(_, text)
|
||||||
|
local h = apis.fs.open(eeprom, "w")
|
||||||
|
h.write(text)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
initfs=fs,
|
||||||
|
disks=initFs,
|
||||||
|
screenCtl={
|
||||||
|
print = function(_, text)
|
||||||
|
for i=1, #allscreens do
|
||||||
|
write(text.."\n", allscreens[i])
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
printInline = function(_, text)
|
||||||
|
for i=1, #allscreens do
|
||||||
|
write(text, allscreens[i])
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
clear = function()
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].clear()
|
||||||
|
allscreens[i].setCursorPos(1, 1)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
resetCursor = function()
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].setCursorPos(1, 1)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
setBackgroundColor = function(_, color)
|
||||||
|
bg=color
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].setBackgroundColor(aprox(color))
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
setTextColor = function(_, color)
|
||||||
|
fg=color
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].setTextColor(aprox(color))
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
getBackgroundColor = function()
|
||||||
|
return bg
|
||||||
|
end,
|
||||||
|
getTextColor = function()
|
||||||
|
return fg
|
||||||
|
end,
|
||||||
|
enable=function()
|
||||||
|
for i=1, #allscreens do
|
||||||
|
allscreens[i].clear()
|
||||||
|
allscreens[i].setCursorPos(1, 1)
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
disable=function() end
|
||||||
|
},
|
||||||
|
architecture="cct",
|
||||||
|
getNvram = function() return getFile("/nvram.dat") end,
|
||||||
|
setNvram = function(_, text)
|
||||||
|
local h = apis.fs.open("/nvram.dat", "w")
|
||||||
|
h.write(text)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
firmware=apis,
|
||||||
|
reboot=false,
|
||||||
|
beep=function() end
|
||||||
|
}
|
||||||
|
|
||||||
|
apis.term.setBackgroundColor(0x8000)
|
||||||
|
apis.term.setTextColor(0x1000)
|
||||||
|
apis.term.clear()
|
||||||
|
apis.term.setCursorPos(1, 1)
|
||||||
|
|
||||||
|
local kernelCoro = coroutine.create(function()
|
||||||
|
--pf
|
||||||
|
---@diagnostic disable-next-line: param-type-mismatch-
|
||||||
|
local ok, err = xpcall(Kernel, debug.traceback, EFI)
|
||||||
|
if not ok then
|
||||||
|
error(err)
|
||||||
|
else
|
||||||
|
apis.os.shutdown()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
function EFI.resumeWithTimeout(co, timeout, ...)
|
||||||
|
local startTime = EFI.getEpochMs()
|
||||||
|
debug.sethook(co, function()
|
||||||
|
if EFI.getEpochMs() > startTime + timeout then
|
||||||
|
return coroutine.yield("timeout")
|
||||||
|
end
|
||||||
|
end, "", 1000)
|
||||||
|
local ret = {coroutine.resume(co, ...)}
|
||||||
|
if ret[1] and ret[2] == "timeout" then
|
||||||
|
return "timeout"
|
||||||
|
elseif ret[1] == false then
|
||||||
|
return "error", ret[2]
|
||||||
|
else
|
||||||
|
debug.sethook(co)
|
||||||
|
return "success", table.unpack(ret, 2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
EFI.screenCtl:print("Loaded in " .. tostring(apis.os.clock()) .. " seconds.\n")
|
||||||
|
--p
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local status, err = EFI.resumeWithTimeout(kernelCoro, 50)
|
||||||
|
apis.os.queueEvent("NoSleep")
|
||||||
|
local exit = false
|
||||||
|
while not exit do
|
||||||
|
local event = {coroutine.yield()}
|
||||||
|
if event[1] == "key" then
|
||||||
|
queueEvent(table.unpack(event))
|
||||||
|
queueEvent("keyPressed", 1, event[2])
|
||||||
|
if acekeys[event[2]] then
|
||||||
|
queueEvent("keyTyped", 1, acekeys[event[2]])
|
||||||
|
end
|
||||||
|
elseif event[1] == "char" then
|
||||||
|
queueEvent(table.unpack(event))
|
||||||
|
queueEvent("keyTyped", 1, event[2])
|
||||||
|
elseif event[1] == "key_up" then
|
||||||
|
queueEvent(table.unpack(event))
|
||||||
|
queueEvent("keyReleased", 1, event[2])
|
||||||
|
elseif event[1] == "NoSleep" then
|
||||||
|
exit = true
|
||||||
|
else
|
||||||
|
queueEvent(table.unpack(event))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
while #callAsyncQueue>0 do
|
||||||
|
local bundle = table.remove(callAsyncQueue, 1)
|
||||||
|
local ret = {xpcall(bundle[2], debug.traceback, table.unpack(bundle, 3))}
|
||||||
|
if not ret[1] then
|
||||||
|
callAsyncReturn[bundle[1]]={false, ret[2]}
|
||||||
|
else
|
||||||
|
callAsyncReturn[bundle[1]]={true, table.unpack(ret,2)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if status == "error" or coroutine.status(kernelCoro) == "dead" then
|
||||||
|
if EFI.reboot then
|
||||||
|
apis.os.reboot()
|
||||||
|
end
|
||||||
|
displaySuperBadError("Kernel error: " .. tostring(err))
|
||||||
|
coroutine.yield("key")
|
||||||
|
elseif status == "success" then
|
||||||
|
if EFI.reboot then
|
||||||
|
apis.os.reboot()
|
||||||
|
end
|
||||||
|
displaySuperBadError("Kernel error: Attempted to yield main thread\n"..debug.traceback(kernelCoro, "Attempted to yield main thread"))
|
||||||
|
coroutine.yield("key")
|
||||||
|
end
|
||||||
|
initFs:refresh()
|
||||||
|
end
|
||||||
|
end, debug.traceback)
|
||||||
|
|
||||||
|
if not ok then displaySuperBadError("Fatal error during boot: " .. err) end
|
||||||
|
while true do coroutine.yield("key") end
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local BOOT_DRIVE_PATH=({...})[1] or "/$"
|
||||||
|
-- UnBIOS by JackMacWindows
|
||||||
|
-- This will undo most of the changes/additions made in the BIOS, but some things may remain wrapped if `debug` is unavailable
|
||||||
|
-- To use, just place a `bios.lua` in the root of the drive, and run this program
|
||||||
|
-- Here's a list of things that are irreversibly changed:
|
||||||
|
-- * both `bit` and `bit32` are kept for compatibility
|
||||||
|
-- * string metatable blocking (on old versions of CC)
|
||||||
|
-- In addition, if `debug` is not available these things are also irreversibly changed:
|
||||||
|
-- * old Lua 5.1 `load` function (for loading from a function)
|
||||||
|
-- * `loadstring` prefixing (before CC:T 1.96.0)
|
||||||
|
-- * `http.request`
|
||||||
|
-- * `os.shutdown` and `os.reboot`
|
||||||
|
-- * `peripheral`
|
||||||
|
-- * `turtle.equip[Left|Right]`
|
||||||
|
-- Licensed under the MIT license
|
||||||
|
local args = {...}
|
||||||
|
local keptAPIs = {keys=true, bit32 = true, bit = true, ccemux = true, config = true, coroutine = true, debug = true, fs = true, http = true, mounter = true, os = true, periphemu = true, peripheral = true, redstone = true, rs = true, term = true, utf8 = true, _HOST = true, _CC_DEFAULT_SETTINGS = true, _CC_DISABLE_LUA51_FEATURES = true, _VERSION = true, assert = true, collectgarbage = true, error = true, gcinfo = true, getfenv = true, getmetatable = true, ipairs = true, __inext = true,load = true, loadstring = true, math = true, newproxy = true, next = true, pairs = true, pcall = true, rawequal = true, rawget = true, rawlen = true, rawset = true, select = true, setfenv = true, setmetatable = true, string = true, table = true, tonumber = true, tostring = true, type = true, unpack = true, xpcall = true, turtle = true, pocket = true, commands = true, _G = true}
|
||||||
|
local t = {}
|
||||||
|
for k in pairs(_G) do if not keptAPIs[k] then table.insert(t, k) end end
|
||||||
|
for _,k in ipairs(t) do _G[k] = nil end
|
||||||
|
local native = _G.term.native()
|
||||||
|
for _, method in ipairs {"nativePaletteColor", "nativePaletteColour", "screenshot"} do native[method] = _G.term[method] end
|
||||||
|
_G.term = native
|
||||||
|
if _G.http then
|
||||||
|
_G.http.checkURL = _G.http.checkURLAsync
|
||||||
|
_G.http.websocket = _G.http.websocketAsync
|
||||||
|
end
|
||||||
|
if _G.commands then _G.commands = _G.commands.native end
|
||||||
|
if _G.turtle then _G.turtle.native, _G.turtle.craft = nil end
|
||||||
|
local delete = {os = {"version", "pullEventRaw", "pullEvent", "run", "loadAPI", "unloadAPI", "sleep"}, http = _G.http and {"get", "post", "put", "delete", "patch", "options", "head", "trace", "listen", "checkURLAsync", "websocketAsync"}, fs = {"complete", "isDriveRoot"}}
|
||||||
|
for k,v in pairs(delete) do for _,a in ipairs(v) do _G[k][a] = nil end end
|
||||||
|
-- Set up TLCO
|
||||||
|
-- This functions by crashing `rednet.run` by removing `os.pullEventRaw`. Normally
|
||||||
|
-- this would cause `parallel` to throw an error, but we replace `error` with an
|
||||||
|
-- empty placeholder to let it continue and return without throwing. This results
|
||||||
|
-- in the `pcall` returning successfully, preventing the error-displaying code
|
||||||
|
-- from running - essentially making it so that `os.shutdown` is called immediately
|
||||||
|
-- after the new BIOS exits.
|
||||||
|
--
|
||||||
|
-- From there, the setup code is placed in `term.native` since it's the first
|
||||||
|
-- thing called after `parallel` exits. This loads the new BIOS and prepares it
|
||||||
|
-- for execution. Finally, it overwrites `os.shutdown` with the new function to
|
||||||
|
-- allow it to be the last function called in the original BIOS, and returns.
|
||||||
|
-- From there execution continues, calling the `term.redirect` dummy, skipping
|
||||||
|
-- over the error-handling code (since `pcall` returned ok), and calling
|
||||||
|
-- `os.shutdown()`. The real `os.shutdown` is re-added, and the new BIOS is tail
|
||||||
|
-- called, which effectively makes it run as the main chunk.
|
||||||
|
local olderror = error
|
||||||
|
_G.error = function() end
|
||||||
|
_G.term.redirect = function() end
|
||||||
|
function _G.term.native()
|
||||||
|
_G.term.native = nil
|
||||||
|
_G.term.redirect = nil
|
||||||
|
_G.error = olderror
|
||||||
|
term.setBackgroundColor(32768)
|
||||||
|
term.setTextColor(1)
|
||||||
|
term.setCursorPos(1, 1)
|
||||||
|
term.setCursorBlink(true)
|
||||||
|
term.clear()
|
||||||
|
local file = fs.open(BOOT_DRIVE_PATH.."/cct/boot.lua", "r")
|
||||||
|
if file == nil then
|
||||||
|
term.setCursorBlink(false)
|
||||||
|
term.setTextColor(16384)
|
||||||
|
term.write("Could not find /cct/boot.lua. UnBIOS cannot continue.")
|
||||||
|
term.setCursorPos(1, 2)
|
||||||
|
term.write("Press any key to continue")
|
||||||
|
coroutine.yield("key")
|
||||||
|
os.shutdown()
|
||||||
|
end
|
||||||
|
local fn, err = loadstring(file.readAll(), "@bootloader")
|
||||||
|
file.close()
|
||||||
|
if fn == nil then
|
||||||
|
term.setCursorBlink(false)
|
||||||
|
term.setTextColor(16384)
|
||||||
|
term.write("Could not load /cc/boot.lua. UnBIOS cannot continue.")
|
||||||
|
term.setCursorPos(1, 2)
|
||||||
|
term.write(err)
|
||||||
|
term.setCursorPos(1, 3)
|
||||||
|
term.write("Press any key to continue")
|
||||||
|
coroutine.yield("key")
|
||||||
|
os.shutdown()
|
||||||
|
end
|
||||||
|
setfenv(fn, _G)
|
||||||
|
local oldshutdown = os.shutdown
|
||||||
|
os.shutdown = function()
|
||||||
|
os.shutdown = oldshutdown
|
||||||
|
return fn(BOOT_DRIVE_PATH)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if debug then
|
||||||
|
-- Restore functions that were overwritten in the BIOS
|
||||||
|
-- Apparently this has to be done *after* redefining term.native
|
||||||
|
local function restoreValue(tab, idx, name, hint)
|
||||||
|
local i, key, value = 1, debug.getupvalue(tab[idx], hint)
|
||||||
|
while key ~= name and key ~= nil do
|
||||||
|
key, value = debug.getupvalue(tab[idx], i)
|
||||||
|
i=i+1
|
||||||
|
end
|
||||||
|
tab[idx] = value or tab[idx]
|
||||||
|
end
|
||||||
|
restoreValue(_G, "loadstring", "nativeloadstring", 1)
|
||||||
|
restoreValue(_G, "load", "nativeload", 5)
|
||||||
|
if http then restoreValue(http, "request", "nativeHTTPRequest", 3) end
|
||||||
|
restoreValue(os, "shutdown", "nativeShutdown", 1)
|
||||||
|
restoreValue(os, "reboot", "nativeReboot", 1)
|
||||||
|
if turtle then
|
||||||
|
restoreValue(turtle, "equipLeft", "v", 1)
|
||||||
|
restoreValue(turtle, "equipRight", "v", 1)
|
||||||
|
end
|
||||||
|
do
|
||||||
|
local i, key, value = 1, debug.getupvalue(peripheral.isPresent, 2)
|
||||||
|
while key ~= "native" and key ~= nil do
|
||||||
|
key, value = debug.getupvalue(peripheral.isPresent, i)
|
||||||
|
i=i+1
|
||||||
|
end
|
||||||
|
_G.peripheral = value or peripheral
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,287 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local apis = ({...})[1]
|
||||||
|
local BOOT_DRIVE_PATH = apis.BOOT_DRIVE_PATH
|
||||||
|
local fs = apis.fs
|
||||||
|
local native = apis.peripheral
|
||||||
|
local peripheral = {}
|
||||||
|
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
||||||
|
|
||||||
|
function peripheral.getNames()
|
||||||
|
local results = {}
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.isPresent(side) then
|
||||||
|
table.insert(results, side)
|
||||||
|
if native.hasType(side, "peripheral_hub") then
|
||||||
|
local remote = native.call(side, "getNamesRemote")
|
||||||
|
for _, name in ipairs(remote) do
|
||||||
|
table.insert(results, name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.isPresent(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getType(peripheral)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.getType(peripheral)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "getTypeRemote", peripheral)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return table.unpack(mt.types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.hasType(peripheral, peripheral_type)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.hasType(peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "hasTypeRemote", peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.types[peripheral_type] ~= nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getMethods(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.getMethods(name)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "getMethodsRemote", name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getName(peripheral)
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.call(name, method, ...)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.call(name, method, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "callRemote", name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.wrap(name)
|
||||||
|
local methods = peripheral.getMethods(name)
|
||||||
|
if not methods then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local types = { peripheral.getType(name) }
|
||||||
|
for i = 1, #types do types[types[i]] = true end
|
||||||
|
local result = setmetatable({}, {
|
||||||
|
__name = "peripheral",
|
||||||
|
name = name,
|
||||||
|
type = types[1],
|
||||||
|
types = types,
|
||||||
|
})
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
result[method] = function(...)
|
||||||
|
return peripheral.call(name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.find(ty, filter)
|
||||||
|
local results = {}
|
||||||
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
|
if peripheral.hasType(name, ty) then
|
||||||
|
local wrapped = peripheral.wrap(name)
|
||||||
|
if filter == nil or filter(name, wrapped) then
|
||||||
|
table.insert(results, wrapped)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.unpack(results)
|
||||||
|
end
|
||||||
|
|
||||||
|
local disks = {}
|
||||||
|
local internal = {}
|
||||||
|
|
||||||
|
local function norm(path)
|
||||||
|
if not path or path == "" then return "/" end
|
||||||
|
return fs.combine("/", path)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function createDisk(id, basePath, readonly, periph)
|
||||||
|
basePath = norm(basePath)
|
||||||
|
|
||||||
|
local disk = {address = id, isReadOnly = function() return readonly end}
|
||||||
|
|
||||||
|
function disk:spaceUsed()
|
||||||
|
return fs.getCapacity(basePath) - fs.getFreeSpace(basePath)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:spaceTotal() return fs.getCapacity(basePath) end
|
||||||
|
|
||||||
|
function disk:list(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if not fs.exists(p) or not fs.isDir(p) then
|
||||||
|
return nil, "not directory"
|
||||||
|
end
|
||||||
|
return fs.list(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:fileExists(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.exists(p) and not fs.isDir(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:directoryExists(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.exists(p) and fs.isDir(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:type(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if not fs.exists(p) then
|
||||||
|
return nil
|
||||||
|
elseif fs.isDir(p) then
|
||||||
|
return "directory"
|
||||||
|
else
|
||||||
|
return "file"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:makeDirectory(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
fs.makeDir(p)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:remove(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
if fs.exists(p) then fs.delete(p) end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:setLabel(label) periph.setLabel(label) end
|
||||||
|
|
||||||
|
function disk:getLabel(label) return periph.getLabel() end
|
||||||
|
|
||||||
|
function disk:attributes(path)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.attributes(p)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path, mode)
|
||||||
|
local p = fs.combine(basePath, path)
|
||||||
|
return fs.open(p, mode)
|
||||||
|
end
|
||||||
|
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
internal["$"] = createDisk("$", BOOT_DRIVE_PATH, false, {
|
||||||
|
setLabel = function(label)
|
||||||
|
local h = fs.open("/.label", "w")
|
||||||
|
h.write(label)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
getLabel = function()
|
||||||
|
local h = fs.open("/.label", "r")
|
||||||
|
if not h then return "$" end
|
||||||
|
local label = h.readAll()
|
||||||
|
h.close()
|
||||||
|
return label
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
internal["rom"] = createDisk("rom", "/rom", true, {
|
||||||
|
setLabel = function(label)
|
||||||
|
error("Device is read-only")
|
||||||
|
end,
|
||||||
|
getLabel = function()
|
||||||
|
return "cctrom"
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
internal["root"] = createDisk("root", "/root", false, {
|
||||||
|
setLabel = function(label)
|
||||||
|
local h = fs.open("/.rootlabel", "w")
|
||||||
|
h.write(label)
|
||||||
|
h.close()
|
||||||
|
end,
|
||||||
|
getLabel = function()
|
||||||
|
local h = fs.open("/.rootlabel", "r")
|
||||||
|
if not h then return "$" end
|
||||||
|
local label = h.readAll()
|
||||||
|
h.close()
|
||||||
|
return label
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
local function refresh()
|
||||||
|
disks={}
|
||||||
|
for _, disk in ipairs({peripheral.find("drive")}) do
|
||||||
|
if disk.isDiskPresent() then
|
||||||
|
disks[tostring(disk.getDiskID())]=createDisk("cctdisk"..tostring(disk.getDiskID()), disk.getMountPath(), false, fs)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function iter()
|
||||||
|
refresh()
|
||||||
|
local combined = {}
|
||||||
|
|
||||||
|
for id, obj in pairs(internal) do combined[id] = obj end
|
||||||
|
for id, obj in pairs(disks) do combined[id] = obj end
|
||||||
|
|
||||||
|
return pairs(combined)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {refresh = refresh, list = iter}
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
--:Minify:--
|
||||||
|
-- CCT driver peripheral helper
|
||||||
|
local kernel=...
|
||||||
|
kernel.cct={}
|
||||||
|
kernel.cct.peripheral={}
|
||||||
|
kernel.cct.callAsync=kernel.apis.callAsync
|
||||||
|
local peripheral=kernel.cct.peripheral
|
||||||
|
local apis = kernel.apis
|
||||||
|
local native = apis.peripheral
|
||||||
|
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
||||||
|
|
||||||
|
function peripheral.getNames()
|
||||||
|
local results = {}
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.isPresent(side) then
|
||||||
|
table.insert(results, side)
|
||||||
|
if native.hasType(side, "peripheral_hub") then
|
||||||
|
local remote = native.call(side, "getNamesRemote")
|
||||||
|
for _, name in ipairs(remote) do
|
||||||
|
table.insert(results, name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.isPresent(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getType(peripheral)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.getType(peripheral)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "getTypeRemote", peripheral)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return table.unpack(mt.types)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.hasType(peripheral, peripheral_type)
|
||||||
|
if type(peripheral) == "string" then
|
||||||
|
if native.isPresent(peripheral) then
|
||||||
|
return native.hasType(peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
||||||
|
return native.call(side, "hasTypeRemote", peripheral, peripheral_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
else
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.types[peripheral_type] ~= nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getMethods(name)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.getMethods(name)
|
||||||
|
end
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "getMethodsRemote", name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.getName(peripheral)
|
||||||
|
local mt = getmetatable(peripheral)
|
||||||
|
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
|
||||||
|
error("bad argument #1 (table is not a peripheral)", 2)
|
||||||
|
end
|
||||||
|
return mt.name
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.call(name, method, ...)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
local id = kernel.cct.callAsync(native.call, name, method, ...)
|
||||||
|
kernel.currentTask.io=function()
|
||||||
|
if kernel.apis.callAsyncReturn[id] then
|
||||||
|
local r=kernel.apis.callAsyncReturn[id]
|
||||||
|
if not r[1] then error(r[2]) end
|
||||||
|
kernel.apis.callAsyncReturn[id]=nil
|
||||||
|
return table.unpack(r,2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.currentTask.status="D"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
local id = kernel.cct.callAsync(native.call, side, "callRemote", name, method, ...)
|
||||||
|
kernel.currentTask.io=function()
|
||||||
|
if kernel.apis.callAsyncReturn[id] then
|
||||||
|
local r=kernel.apis.callAsyncReturn[id]
|
||||||
|
if not r[1] then error(r[2]) end
|
||||||
|
kernel.apis.callAsyncReturn[id]=nil
|
||||||
|
return table.unpack(r,2)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.currentTask.status="D"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.callnative(name, method, ...)
|
||||||
|
if native.isPresent(name) then
|
||||||
|
return native.call(name, method, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
for n = 1, #sides do
|
||||||
|
local side = sides[n]
|
||||||
|
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
||||||
|
return native.call(side, "callRemote", name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.wrap(name, wrap)
|
||||||
|
local methods = peripheral.getMethods(name)
|
||||||
|
if not methods then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local types = { peripheral.getType(name) }
|
||||||
|
for i = 1, #types do types[types[i]] = true end
|
||||||
|
local result = setmetatable({}, {
|
||||||
|
__name = "peripheral",
|
||||||
|
name = name,
|
||||||
|
type = types[1],
|
||||||
|
types = types,
|
||||||
|
})
|
||||||
|
if wrap then
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
result[method] = function(...)
|
||||||
|
return peripheral.call(name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for _, method in ipairs(methods) do
|
||||||
|
result[method] = function(...)
|
||||||
|
return peripheral.callnative(name, method, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
function peripheral.find(ty, filter)
|
||||||
|
local results = {}
|
||||||
|
for _, name in ipairs(peripheral.getNames()) do
|
||||||
|
if peripheral.hasType(name, ty) then
|
||||||
|
local wrapped = peripheral.wrap(name)
|
||||||
|
if filter == nil or filter(name, wrapped) then
|
||||||
|
table.insert(results, wrapped)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return table.unpack(results)
|
||||||
|
end
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local peripheral=kernel.cct.peripheral
|
||||||
|
|
||||||
|
local function wrap(p)
|
||||||
|
return function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if kernel.uid~=0 then error("EACCES") end
|
||||||
|
return peripheral.wrap(p, true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local peripherals=peripheral.getNames()
|
||||||
|
for i=1, #peripherals do
|
||||||
|
local p=peripherals[i]
|
||||||
|
local typ=peripheral.getType(p)
|
||||||
|
local file=wrap(p)
|
||||||
|
kernel.devfs.data["raw"][p]=file
|
||||||
|
local typefolder=kernel.devfs.data["raw"]["by-type"][typ]
|
||||||
|
if not typefolder then typefolder={} end
|
||||||
|
typefolder[tostring(#table.keys(typefolder)+1)] = file
|
||||||
|
kernel.devfs.data["raw"]["by-type"][typ]=typefolder
|
||||||
|
end
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
|
||||||
|
local handler = {}
|
||||||
|
|
||||||
|
local http = kernel.apis.http
|
||||||
|
|
||||||
|
kernel.cct.httpqueue = kernel.cct.httpqueue or {}
|
||||||
|
kernel.cct.httpresponse = kernel.cct.httpresponse or {}
|
||||||
|
kernel.cct.httperror = kernel.cct.httperror or {}
|
||||||
|
|
||||||
|
local function parseRawRequest(raw, address)
|
||||||
|
local headerEnd = raw:find("\r\n\r\n", 1, true)
|
||||||
|
|
||||||
|
local headerPart
|
||||||
|
local body = ""
|
||||||
|
|
||||||
|
if headerEnd then
|
||||||
|
headerPart = raw:sub(1, headerEnd - 1)
|
||||||
|
body = raw:sub(headerEnd + 4)
|
||||||
|
else
|
||||||
|
headerPart = raw
|
||||||
|
end
|
||||||
|
|
||||||
|
local lines = {}
|
||||||
|
|
||||||
|
for line in headerPart:gmatch("[^\r\n]+") do
|
||||||
|
lines[#lines + 1] = line
|
||||||
|
end
|
||||||
|
|
||||||
|
if #lines == 0 then
|
||||||
|
return nil, "EINVAL"
|
||||||
|
end
|
||||||
|
|
||||||
|
local split=string.split(lines[1], " ")
|
||||||
|
if #split~=3 then
|
||||||
|
return nil, "EINVAL"
|
||||||
|
end
|
||||||
|
local method, path, ver = table.unpack(split)
|
||||||
|
|
||||||
|
local headers = {}
|
||||||
|
|
||||||
|
for i = 2, #lines do
|
||||||
|
local k, v = lines[i]:match("^([^:]+):%s*(.*)$")
|
||||||
|
if k then
|
||||||
|
headers[k] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if path:sub(1,1)~="/" then
|
||||||
|
return nil, "EINVAL"
|
||||||
|
end
|
||||||
|
local url = address..path
|
||||||
|
|
||||||
|
local req = {
|
||||||
|
url = url,
|
||||||
|
method = method,
|
||||||
|
headers = headers
|
||||||
|
}
|
||||||
|
|
||||||
|
if body ~= "" then
|
||||||
|
req.body = body
|
||||||
|
end
|
||||||
|
|
||||||
|
return req, url
|
||||||
|
end
|
||||||
|
|
||||||
|
local function buildResponse(resp)
|
||||||
|
if not resp then
|
||||||
|
return nil, "EINVAL"
|
||||||
|
end
|
||||||
|
|
||||||
|
local code, msg = resp.getResponseCode()
|
||||||
|
local headers = resp.getResponseHeaders()
|
||||||
|
local body = resp.readAll() or ""
|
||||||
|
resp.close()
|
||||||
|
|
||||||
|
local out = {
|
||||||
|
"HTTP/1.1 " ..
|
||||||
|
tostring(code) ..
|
||||||
|
" " ..
|
||||||
|
tostring(msg or "")
|
||||||
|
}
|
||||||
|
|
||||||
|
for i,v in pairs(headers) do
|
||||||
|
out[#out+1] = i..": "..v
|
||||||
|
end
|
||||||
|
|
||||||
|
out[#out + 1] = ""
|
||||||
|
out[#out + 1] = body
|
||||||
|
|
||||||
|
return table.concat(out, "\r\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.connect(fd, address)
|
||||||
|
local fdo = kernel.currentTask.fd[fd]
|
||||||
|
|
||||||
|
if not fdo then
|
||||||
|
return nil, "EBADF"
|
||||||
|
end
|
||||||
|
|
||||||
|
fdo.handle.write = function(raw)
|
||||||
|
local req, url = parseRawRequest(raw, address)
|
||||||
|
|
||||||
|
if not req then
|
||||||
|
return nil, url
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.cct.httpqueue[url] = true
|
||||||
|
local ok, err = http.request(req)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
kernel.cct.httpqueue[url] = nil
|
||||||
|
return nil, err
|
||||||
|
end
|
||||||
|
if fdo.socket.active then
|
||||||
|
fdo.socket.content=nil
|
||||||
|
fdo.socket.file.close()
|
||||||
|
fdo.socket.file=nil
|
||||||
|
end
|
||||||
|
fdo.socket.active=true
|
||||||
|
fdo.socket.url=url
|
||||||
|
fdo.socket.loaded=false
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
fdo.handle.read = function(count)
|
||||||
|
if not fdo.socket.active then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not kernel.cct.httpqueue[fdo.socket.url] and not fdo.socket.loaded then
|
||||||
|
fdo.socket.file=kernel.sfile(buildResponse(kernel.cct.httpresponse[fdo.socket.url]))
|
||||||
|
kernel.cct.httpqueue[fdo.socket.url]=nil
|
||||||
|
kernel.cct.httpresponse[fdo.socket.url]=nil
|
||||||
|
fdo.socket.loaded=true
|
||||||
|
end
|
||||||
|
|
||||||
|
if not fdo.socket.file then
|
||||||
|
kernel.currentTask.io=function()
|
||||||
|
if not kernel.cct.httpqueue[fdo.socket.url] then
|
||||||
|
fdo.socket.file=kernel.sfile(buildResponse(kernel.cct.httpresponse[fdo.socket.url]))
|
||||||
|
kernel.cct.httpqueue[fdo.socket.url]=nil
|
||||||
|
kernel.cct.httpresponse[fdo.socket.url]=nil
|
||||||
|
fdo.socket.loaded=true
|
||||||
|
return fdo.socket.file.read(count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.currentTask.status="D"
|
||||||
|
return
|
||||||
|
else
|
||||||
|
return fdo.socket.file.read(count)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fdo.handle.seek = function(whence, offset)
|
||||||
|
if not fdo.socket.active then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not kernel.cct.httpqueue[fdo.socket.url] and not fdo.socket.loaded then
|
||||||
|
fdo.socket.file=kernel.sfile(buildResponse(kernel.cct.httpresponse[fdo.socket.url]))
|
||||||
|
kernel.cct.httpqueue[fdo.socket.url]=nil
|
||||||
|
kernel.cct.httpresponse[fdo.socket.url]=nil
|
||||||
|
fdo.socket.loaded=true
|
||||||
|
end
|
||||||
|
|
||||||
|
if not fdo.socket.file then
|
||||||
|
kernel.currentTask.io=function()
|
||||||
|
if not kernel.cct.httpqueue[fdo.socket.url] then
|
||||||
|
fdo.socket.file=kernel.sfile(buildResponse(kernel.cct.httpresponse[fdo.socket.url]))
|
||||||
|
kernel.cct.httpqueue[fdo.socket.url]=nil
|
||||||
|
kernel.cct.httpresponse[fdo.socket.url]=nil
|
||||||
|
fdo.socket.loaded=true
|
||||||
|
return fdo.socket.file.seek(whence, offset)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.currentTask.status="D"
|
||||||
|
return
|
||||||
|
else
|
||||||
|
return fdo.socket.file.seek(whence, offset)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fdo.handle.close = function()
|
||||||
|
fdo.socket.file.close()
|
||||||
|
fdo.socket.active=false
|
||||||
|
kernel.cct.httpqueue[fdo.socket.url] = nil
|
||||||
|
kernel.cct.httpresponse[fdo.socket.url] = nil
|
||||||
|
kernel.cct.httperror[fdo.socket.url] = nil
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.socket.registerProtocal(
|
||||||
|
"http://",
|
||||||
|
handler
|
||||||
|
)
|
||||||
|
|
||||||
|
kernel.socket.registerProtocal(
|
||||||
|
"https://",
|
||||||
|
handler
|
||||||
|
)
|
||||||
@@ -0,0 +1,281 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
local peripheral=kernel.cct.peripheral
|
||||||
|
|
||||||
|
local colors = {
|
||||||
|
[0xFFFFFF]=0x0001,
|
||||||
|
[0xFF0000]=0x0002,
|
||||||
|
[0x00FF00]=0x0004,
|
||||||
|
[0x0000FF]=0x0008,
|
||||||
|
[0x00FFFF]=0x0010,
|
||||||
|
[0xFF00FF]=0x0020,
|
||||||
|
[0xFFFF00]=0x0040,
|
||||||
|
[0xFF6D00]=0x0080,
|
||||||
|
[0x6DFF55]=0x0100,
|
||||||
|
[0x24FFFF]=0x0200,
|
||||||
|
[0x924900]=0x0400,
|
||||||
|
[0x6D6D55]=0x0800,
|
||||||
|
[0xDBDBAA]=0x1000,
|
||||||
|
[0x6D00FF]=0x2000,
|
||||||
|
[0xB6FF00]=0x4000,
|
||||||
|
[0x000000]=0x8000
|
||||||
|
}
|
||||||
|
|
||||||
|
local plt = {
|
||||||
|
0xFFFFFF,
|
||||||
|
0xFF0000,
|
||||||
|
0x00FF00,
|
||||||
|
0x0000FF,
|
||||||
|
0x00FFFF,
|
||||||
|
0xFF00FF,
|
||||||
|
0xFFFF00,
|
||||||
|
0xFF6D00,
|
||||||
|
0x6DFF55,
|
||||||
|
0x24FFFF,
|
||||||
|
0x924900,
|
||||||
|
0x6D6D55,
|
||||||
|
0xDBDBAA,
|
||||||
|
0x6D00FF,
|
||||||
|
0xB6FF00,
|
||||||
|
0x000000
|
||||||
|
}
|
||||||
|
|
||||||
|
local fg,bg=0x6D6D55,0x000000
|
||||||
|
local l1f,l1d,l2,ops={},{},{},0
|
||||||
|
|
||||||
|
local function findClosest(tbl, target)
|
||||||
|
local closest = nil
|
||||||
|
local smallestDiff = math.huge
|
||||||
|
|
||||||
|
for k, _ in pairs(tbl) do
|
||||||
|
if k==target then return k end
|
||||||
|
local diff = math.abs(k - target)
|
||||||
|
if diff < smallestDiff then
|
||||||
|
smallestDiff = diff
|
||||||
|
closest = k
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return closest
|
||||||
|
end
|
||||||
|
|
||||||
|
local function aprox(c24)
|
||||||
|
ops = ops + 1
|
||||||
|
|
||||||
|
if ops % 1024 == 0 then
|
||||||
|
l1d = {}
|
||||||
|
l1f = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if ops % 8192 == 0 then
|
||||||
|
l2 = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
if l2[c24] ~= nil then
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
if l1d[c24] ~= nil then
|
||||||
|
l1f[c24] = l1f[c24] + 1
|
||||||
|
|
||||||
|
if l1f[c24] >= 16 then
|
||||||
|
l2[c24] = l1d[c24]
|
||||||
|
l1d[c24] = nil
|
||||||
|
l1f[c24] = nil
|
||||||
|
return l2[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
return l1d[c24]
|
||||||
|
end
|
||||||
|
|
||||||
|
local closestKey = findClosest(colors, c24)
|
||||||
|
if not closestKey then return nil end
|
||||||
|
|
||||||
|
local value = colors[closestKey]
|
||||||
|
|
||||||
|
l1d[c24] = value
|
||||||
|
l1f[c24] = 1
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
local function write(text, term)
|
||||||
|
local x, y = term.getCursorPos()
|
||||||
|
local w, h = term.getSize()
|
||||||
|
|
||||||
|
for i = 1, #text do
|
||||||
|
local c = text:sub(i, i)
|
||||||
|
|
||||||
|
if c == "\n" then
|
||||||
|
y = y + 1
|
||||||
|
x = 1
|
||||||
|
elseif c == "\t" then
|
||||||
|
local tabSize = 4
|
||||||
|
local spaces = tabSize - ((x - 1) % tabSize)
|
||||||
|
term.write(string.rep(" ", spaces))
|
||||||
|
x = x + spaces
|
||||||
|
elseif c == "\b" then
|
||||||
|
if x > 1 then
|
||||||
|
x = x - 1
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
term.write(" ")
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if x <= w and y <= h then
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
term.write(c)
|
||||||
|
x = x + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if x > w then
|
||||||
|
x = 1
|
||||||
|
y = y + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if y - 1 >= h then
|
||||||
|
term.scroll(1)
|
||||||
|
y = h
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
term.setCursorPos(x, y)
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.cct.ctrl,kernel.cct.alt = false, false
|
||||||
|
|
||||||
|
local function serializeBool(bool)
|
||||||
|
if bool then
|
||||||
|
return "T"
|
||||||
|
else
|
||||||
|
return "F"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function newtty(obj, id, ev)
|
||||||
|
obj.setPaletteColor(0x1, 0xFFFFFF) -- #000000
|
||||||
|
obj.setPaletteColor(0x2, 0xFF0000) -- #FFFFFF
|
||||||
|
obj.setPaletteColor(0x4, 0x00FF00) -- #FF0000
|
||||||
|
obj.setPaletteColor(0x8, 0x0000FF) -- #00FF00
|
||||||
|
obj.setPaletteColor(0x10, 0x00FFFF) -- #0000FF
|
||||||
|
obj.setPaletteColor(0x20, 0xFF00FF) -- #00FFFF
|
||||||
|
obj.setPaletteColor(0x40, 0xFFFF00) -- #FF00FF
|
||||||
|
obj.setPaletteColor(0x80, 0xFF6D00) -- #FFFF00
|
||||||
|
obj.setPaletteColor(0x100, 0x6DFF55) -- #FF6D00
|
||||||
|
obj.setPaletteColor(0x200, 0x24FFFF) -- #6DFF55
|
||||||
|
obj.setPaletteColor(0x400, 0x924900) -- #24FFFF
|
||||||
|
obj.setPaletteColor(0x800, 0x6D6D55) -- #924900
|
||||||
|
obj.setPaletteColor(0x1000, 0xDBDBAA) -- #6D6D55
|
||||||
|
obj.setPaletteColor(0x2000, 0x6D00FF) -- #DBDBAA
|
||||||
|
obj.setPaletteColor(0x4000, 0xB6FF00) -- #6D00FF
|
||||||
|
obj.setPaletteColor(0x8000, 0x000000) -- #B6FF00
|
||||||
|
kernel.devfs.data["tty"][id] = function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Terminal"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount)
|
||||||
|
local rv=""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
local event = {ev()}
|
||||||
|
if event[1] then
|
||||||
|
rv=rv..event[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if rv=="" then rv=nil end
|
||||||
|
return rv
|
||||||
|
end,
|
||||||
|
write=function(content)
|
||||||
|
write(content, obj)
|
||||||
|
end,
|
||||||
|
size=function()
|
||||||
|
return obj.getSize()
|
||||||
|
end,
|
||||||
|
clear=function()
|
||||||
|
obj.clear()
|
||||||
|
obj.setCursorPos(1,1)
|
||||||
|
end,
|
||||||
|
gpos=function()
|
||||||
|
return obj.getCursorPos()
|
||||||
|
end,
|
||||||
|
spos=function(x,y)
|
||||||
|
return obj.setCursorPos(x,y)
|
||||||
|
end,
|
||||||
|
sfgc=function(c)
|
||||||
|
fg=c
|
||||||
|
return obj.setTextColor(aprox(c))
|
||||||
|
end,
|
||||||
|
sbgc=function(c)
|
||||||
|
bg=c
|
||||||
|
return obj.setBackgroundColor(aprox(c))
|
||||||
|
end,
|
||||||
|
gfgc=function()
|
||||||
|
return fg
|
||||||
|
end,
|
||||||
|
gbgc=function()
|
||||||
|
return bg
|
||||||
|
end,
|
||||||
|
isvirt=function()
|
||||||
|
return false
|
||||||
|
end,
|
||||||
|
gplt=function()
|
||||||
|
return plt
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local fifo = kernel.newFifo()
|
||||||
|
kernel.cct.fifo=fifo
|
||||||
|
|
||||||
|
newtty(kernel.apis.term, "1", function() end)
|
||||||
|
|
||||||
|
for i,v in ipairs({peripheral.find("monitor")}) do
|
||||||
|
newtty(v,tostring(i+1),function () end)
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.devfs.data["input"]["keyboard1"] = function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Keyboard"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount)
|
||||||
|
local rv=""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
local event = {fifo.pop()}
|
||||||
|
if event[1] then
|
||||||
|
rv=rv..event[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if rv=="" then rv=nil end
|
||||||
|
return rv
|
||||||
|
end,
|
||||||
|
write=function(content)
|
||||||
|
end,
|
||||||
|
gctrl=function()
|
||||||
|
return serializeBool(kernel.cct.ctrl)..";"..serializeBool(kernel.cct.alt)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local keys=kernel.apis.keys
|
||||||
|
kernel.cct.eventhooks={}
|
||||||
|
|
||||||
|
kernel.processes.cctdaemon = function()
|
||||||
|
local timeout = false
|
||||||
|
kernel.log("CCT daemon started")
|
||||||
|
while true do
|
||||||
|
local event = {kernel.EFI:getMachineEvent()}
|
||||||
|
|
||||||
|
if event[1] then
|
||||||
|
local eventType = event[1]
|
||||||
|
local charOrKey = event[3]
|
||||||
|
|
||||||
|
local ctrlKeyMap = {
|
||||||
|
[keys.a]=1, [keys.b]=2, [keys.c]=3,
|
||||||
|
[keys.d]=4, [keys.e]=5, [keys.f]=6,
|
||||||
|
[keys.g]=7, [keys.h]=8, [keys.i]=9,
|
||||||
|
[keys.j]=10, [keys.k]=11, [keys.l]=12,
|
||||||
|
[keys.m]=13, [keys.n]=14, [keys.o]=15,
|
||||||
|
[keys.p]=16, [keys.q]=17, [keys.r]=18,
|
||||||
|
[keys.s]=19, [keys.t]=20, [keys.u]=21,
|
||||||
|
[keys.v]=22, [keys.w]=23, [keys.x]=24,
|
||||||
|
[keys.y]=25, [keys.z]=26,
|
||||||
|
}
|
||||||
|
|
||||||
|
if eventType == "keyPressed" then
|
||||||
|
if charOrKey == keys.leftCtrl or charOrKey == keys.rightCtrl then
|
||||||
|
kernel.cct.ctrl = true
|
||||||
|
elseif charOrKey == keys.leftAlt or charOrKey == keys.rightAlt then
|
||||||
|
kernel.cct.alt = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if kernel.cct.ctrl then
|
||||||
|
local ctrlByte = ctrlKeyMap[charOrKey]
|
||||||
|
if ctrlByte then
|
||||||
|
if ctrlByte == 3 then
|
||||||
|
for _, task in ipairs(syscall.getTasks()) do
|
||||||
|
syscall.sigsend(task, 1)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
kernel.cct.fifo.push(string.char(ctrlByte))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local specialKeyMap = {
|
||||||
|
[keys.up] = "[A",
|
||||||
|
[keys.down] = "[B",
|
||||||
|
[keys.right] = "[C",
|
||||||
|
[keys.left] = "[D",
|
||||||
|
[keys.home] = "[H",
|
||||||
|
[keys["end"]] = "[F",
|
||||||
|
[keys.pageUp] = "[5~",
|
||||||
|
[keys.pageDown] = "[6~",
|
||||||
|
[keys.delete] = "[3~",
|
||||||
|
}
|
||||||
|
local special = specialKeyMap[charOrKey]
|
||||||
|
if special then kernel.cct.fifo.push(special) end
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif eventType == "keyReleased" then
|
||||||
|
if charOrKey == keys.leftCtrl or charOrKey == keys.rightCtrl then
|
||||||
|
kernel.cct.ctrl = false
|
||||||
|
elseif charOrKey == keys.leftAlt or charOrKey == keys.rightAlt then
|
||||||
|
kernel.cct.alt = false
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif eventType == "keyTyped" then
|
||||||
|
if charOrKey then kernel.cct.fifo.push(charOrKey) end
|
||||||
|
elseif eventType == "mouse_scroll" then
|
||||||
|
if event[2] == 1 then
|
||||||
|
kernel.cct.fifo.push("[nS")
|
||||||
|
else
|
||||||
|
kernel.cct.fifo.push("[nT")
|
||||||
|
end
|
||||||
|
elseif eventType == "http_success" then
|
||||||
|
if kernel.cct.httpqueue[event[2]] then
|
||||||
|
kernel.cct.httpqueue[event[2]]=nil
|
||||||
|
kernel.cct.httpresponse[event[2]]=event[3]
|
||||||
|
end
|
||||||
|
elseif eventType == "http_failure" then
|
||||||
|
if kernel.cct.httpqueue[event[2]] then
|
||||||
|
kernel.cct.httpqueue[event[2]]=nil
|
||||||
|
kernel.cct.httperror[event[2]]=event[3]
|
||||||
|
kernel.cct.httpresponse[event[2]]=event[4]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if kernel.cct.eventhooks[eventType] then
|
||||||
|
local ok,err = xpcall(kernel.cct.eventhooks[eventType], debug.traceback, table.unpack(event))
|
||||||
|
if not ok then
|
||||||
|
kernel.log("Error on eventhook "..eventType.."\n"..err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
timeout = false
|
||||||
|
else
|
||||||
|
timeout = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if timeout then
|
||||||
|
sleep(0.05)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("CCT daemon queued for execution")
|
||||||
@@ -0,0 +1,151 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local alpha = 0.85
|
||||||
|
local C_target = 0.01
|
||||||
|
local Tmin = 0.0005
|
||||||
|
local Tmax = 0.5
|
||||||
|
local lambda_budget = 0.08
|
||||||
|
local lambda_clamp = 0.03
|
||||||
|
local lambda_var = 0.02
|
||||||
|
local k_min = 0.5
|
||||||
|
local k_max = 0.5
|
||||||
|
local B = 0.01
|
||||||
|
|
||||||
|
function kernel.main()
|
||||||
|
kernel.log("Starting main loop...")
|
||||||
|
kernel.saveLog()
|
||||||
|
local stopLog=5
|
||||||
|
local logTasks=100
|
||||||
|
|
||||||
|
while not kernel.exitMain do
|
||||||
|
if kernel.config.logTasks then
|
||||||
|
if logTasks<0 then
|
||||||
|
kernel.log("Active Tasks:")
|
||||||
|
for i,v in pairs(kernel.tasks) do
|
||||||
|
kernel.log(v.name.." : "..v.status.." : "..tostring(v.pid).." : "..tostring(v.exit))
|
||||||
|
end
|
||||||
|
kernel.log("[END BLOCK]")
|
||||||
|
logTasks=100
|
||||||
|
end
|
||||||
|
logTasks=logTasks-1
|
||||||
|
end
|
||||||
|
local N = 0
|
||||||
|
local Tmin_hit = 0
|
||||||
|
local Tmax_hit = 0
|
||||||
|
local totalTaskTime = 0
|
||||||
|
local taskTimes = {}
|
||||||
|
|
||||||
|
for pid, task in pairs(kernel.tasks) do
|
||||||
|
if kernel.exitMain then break end
|
||||||
|
|
||||||
|
for _,list in ipairs(kernel.perTaskHooks) do
|
||||||
|
for id,hook in ipairs(list) do
|
||||||
|
local ok,err = xpcall(hook, debug.traceback, task, pid)
|
||||||
|
if not ok then
|
||||||
|
kernel.log("[MAIN HOOK ERR]: "..err, "ERROR", 0xFF0000)
|
||||||
|
list[id]=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if task.status == "R" then
|
||||||
|
N = N + 1
|
||||||
|
task.timeSlice = math.min(Tmax, math.max(Tmin, B / (N ^ alpha)))
|
||||||
|
if task.status == "R" then
|
||||||
|
local startTime = kernel.EFI:getEpochMs()
|
||||||
|
local ret
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Running task " .. tostring(task.pid) .. " (" .. task.name .. ") with time slice " .. string.format("%.3f", task.timeSlice) .. "s", "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
if kernel.config.preempt then
|
||||||
|
if not task.debugger then
|
||||||
|
ret = { kernel.EFI.resumeWithTimeout(task.coro, task.timeSlice, table.unpack(task.syscallReturn)) }
|
||||||
|
else
|
||||||
|
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||||
|
end
|
||||||
|
|
||||||
|
local elapsed = kernel.EFI:getEpochMs() - startTime
|
||||||
|
task.lastTime = elapsed
|
||||||
|
task.totalTime = (task.totalTime or 0) + elapsed
|
||||||
|
task.numRuns = (task.numRuns or 0) + 1
|
||||||
|
|
||||||
|
taskTimes[#taskTimes+1] = elapsed
|
||||||
|
totalTaskTime = totalTaskTime + elapsed
|
||||||
|
|
||||||
|
if elapsed <= Tmin then Tmin_hit = Tmin_hit + 1 end
|
||||||
|
if elapsed >= Tmax then Tmax_hit = Tmax_hit + 1 end
|
||||||
|
|
||||||
|
if ret[1] == "error" or ret[1] == false then
|
||||||
|
kernel.log("processHandlerException: " .. tostring(ret[2]), "ERROR", 0xFF0000)
|
||||||
|
task.status = "Z"
|
||||||
|
task.exit = "processHandlerException: " .. tostring(ret[2])
|
||||||
|
|
||||||
|
elseif ret[1] == "timeout" then
|
||||||
|
task.ivs = task.ivs + 1
|
||||||
|
task.syscallReturn = {}
|
||||||
|
|
||||||
|
elseif ret[1] == "success" or ret[1] == true then
|
||||||
|
task.vs = task.vs + 1
|
||||||
|
|
||||||
|
if ret[2] == "syscall" then
|
||||||
|
local scname = ret[3]
|
||||||
|
if kernel.syscalls[scname] then
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall: " .. scname, "DBUG", 0x00FFFF)
|
||||||
|
for i = 4, #ret do
|
||||||
|
kernel.log(" inval[" .. (i-3) .. "] = " .. tostring(ret[i]), "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local sysret = { xpcall(kernel.syscalls[scname], debug.traceback, table.unpack(ret, 4)) }
|
||||||
|
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
if not sysret[1] then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " failed: " .. tostring(sysret[2]), "ERROR", 0xFF0000)
|
||||||
|
else
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " ok, " .. (#sysret-1) .. " retvals", "DBUG", 0x00FFFF)
|
||||||
|
for i = 2, #sysret do
|
||||||
|
local v = type(sysret[i]) == "table" and table.serialize(sysret[i]) or tostring(sysret[i])
|
||||||
|
kernel.log(" retval[" .. (i-1) .. "] = " .. v, "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not sysret[1] then
|
||||||
|
task.syscallReturn = { false, sysret[2] }
|
||||||
|
else
|
||||||
|
task.syscallReturn = { true, table.unpack(sysret, 2) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
task.syscallReturn = { false, "Unknown syscall: " .. tostring(scname) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local T_prev_avg = (N > 0) and (totalTaskTime / N) or 0
|
||||||
|
local T_prev_var = 0
|
||||||
|
for _, t in ipairs(taskTimes) do
|
||||||
|
T_prev_var = T_prev_var + (t - T_prev_avg) ^ 2
|
||||||
|
end
|
||||||
|
if N > 0 then T_prev_var = T_prev_var / N end
|
||||||
|
|
||||||
|
if N > 0 then
|
||||||
|
local f_clamp = k_min * (Tmin_hit / N) - k_max * (Tmax_hit / N)
|
||||||
|
local B_budget = (C_target * (N ^ (alpha - 1))) / math.max(T_prev_avg, 1e-8)
|
||||||
|
B = B + lambda_budget * (B_budget - B) + lambda_clamp * f_clamp - lambda_var * T_prev_var
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.hpv.reapDeadTasks()
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Executed " .. N .. " tasks, avg time: " .. string.format("%.2f", T_prev_avg) .. "ms, var: " .. string.format("%.2f", T_prev_var) .. ", B: " .. string.format("%.5f", B))
|
||||||
|
stopLog = stopLog - 1
|
||||||
|
kernel.saveLog()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local rs=kernel.apis.rs
|
||||||
|
local sides = {top=1, bottom=2, left=3, right=4, front=5, back=6}
|
||||||
|
local function newGPIO(side)
|
||||||
|
return function(mode, data)
|
||||||
|
if mode=="w" then
|
||||||
|
if type(data)~="boolean" then error("data: expected bool") end
|
||||||
|
rs.setOutput(side, data)
|
||||||
|
elseif mode=="wa" then
|
||||||
|
if type(data)~="number" then error("data: expected bool") end
|
||||||
|
rs.setAnalogOutput(side, data)
|
||||||
|
elseif mode=="r" then
|
||||||
|
return rs.getInput(side)
|
||||||
|
elseif mode=="ra" then
|
||||||
|
return rs.getAnalogInput(side)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for side, alt in pairs(sides) do
|
||||||
|
local func=newGPIO(side)
|
||||||
|
kernel.gpio[side]=func
|
||||||
|
kernel.gpio[alt]=func
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
firmware for ComputerCraft Tweaked
|
||||||
@@ -1,363 +0,0 @@
|
|||||||
-- :Minify:--
|
|
||||||
local kernel = ...
|
|
||||||
local apis = kernel.apis
|
|
||||||
local native = apis.peripheral
|
|
||||||
local sides = {"top", "bottom", "left", "right", "front", "back"}
|
|
||||||
local peripheral={}
|
|
||||||
|
|
||||||
function peripheral.getNames()
|
|
||||||
local results = {}
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.isPresent(side) then
|
|
||||||
table.insert(results, side)
|
|
||||||
if native.hasType(side, "peripheral_hub") then
|
|
||||||
local remote = native.call(side, "getNamesRemote")
|
|
||||||
for _, name in ipairs(remote) do
|
|
||||||
table.insert(results, name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return results
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.isPresent(name)
|
|
||||||
if native.isPresent(name) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.getType(peripheral)
|
|
||||||
if type(peripheral) == "string" then -- Peripheral name passed
|
|
||||||
if native.isPresent(peripheral) then
|
|
||||||
return native.getType(peripheral)
|
|
||||||
end
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
|
||||||
return native.call(side, "getTypeRemote", peripheral)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
local mt = getmetatable(peripheral)
|
|
||||||
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
|
||||||
error("bad argument #1 (table is not a peripheral)", 2)
|
|
||||||
end
|
|
||||||
return table.unpack(mt.types)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.hasType(peripheral, peripheral_type)
|
|
||||||
if type(peripheral) == "string" then -- Peripheral name passed
|
|
||||||
if native.isPresent(peripheral) then
|
|
||||||
return native.hasType(peripheral, peripheral_type)
|
|
||||||
end
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", peripheral) then
|
|
||||||
return native.call(side, "hasTypeRemote", peripheral, peripheral_type)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
local mt = getmetatable(peripheral)
|
|
||||||
if not mt or mt.__name ~= "peripheral" or type(mt.types) ~= "table" then
|
|
||||||
error("bad argument #1 (table is not a peripheral)", 2)
|
|
||||||
end
|
|
||||||
return mt.types[peripheral_type] ~= nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.getMethods(name)
|
|
||||||
if native.isPresent(name) then
|
|
||||||
return native.getMethods(name)
|
|
||||||
end
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
|
||||||
return native.call(side, "getMethodsRemote", name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.getName(peripheral)
|
|
||||||
local mt = getmetatable(peripheral)
|
|
||||||
if not mt or mt.__name ~= "peripheral" or type(mt.name) ~= "string" then
|
|
||||||
error("bad argument #1 (table is not a peripheral)", 2)
|
|
||||||
end
|
|
||||||
return mt.name
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.call(name, method, ...)
|
|
||||||
if native.isPresent(name) then
|
|
||||||
return native.call(name, method, ...)
|
|
||||||
end
|
|
||||||
|
|
||||||
for n = 1, #sides do
|
|
||||||
local side = sides[n]
|
|
||||||
if native.hasType(side, "peripheral_hub") and native.call(side, "isPresentRemote", name) then
|
|
||||||
return native.call(side, "callRemote", name, method, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.wrap(name)
|
|
||||||
local methods = peripheral.getMethods(name)
|
|
||||||
if not methods then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local types = { peripheral.getType(name) }
|
|
||||||
for i = 1, #types do types[types[i]] = true end
|
|
||||||
local result = setmetatable({}, {
|
|
||||||
__name = "peripheral",
|
|
||||||
name = name,
|
|
||||||
type = types[1],
|
|
||||||
types = types,
|
|
||||||
})
|
|
||||||
for _, method in ipairs(methods) do
|
|
||||||
result[method] = function(...)
|
|
||||||
return peripheral.call(name, method, ...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
|
|
||||||
function peripheral.find(ty, filter)
|
|
||||||
local results = {}
|
|
||||||
for _, name in ipairs(peripheral.getNames()) do
|
|
||||||
if peripheral.hasType(name, ty) then
|
|
||||||
local wrapped = peripheral.wrap(name)
|
|
||||||
if filter == nil or filter(name, wrapped) then
|
|
||||||
table.insert(results, wrapped)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return table.unpack(results)
|
|
||||||
end
|
|
||||||
|
|
||||||
local icolors = {
|
|
||||||
[0x1] = 1, -- #000000
|
|
||||||
[0x2] = 2, -- #FFFFFF
|
|
||||||
[0x4] = 3, -- #FF0000
|
|
||||||
[0x8] = 4, -- #00FF00
|
|
||||||
[0x10] = 5, -- #0000FF
|
|
||||||
[0x20] = 6, -- #00FFFF
|
|
||||||
[0x40] = 7, -- #FF00FF
|
|
||||||
[0x80] = 8, -- #FFFF00
|
|
||||||
[0x100] = 9, -- #FF6D00
|
|
||||||
[0x200] = 10, -- #6DFF55
|
|
||||||
[0x400] = 11, -- #24FFFF
|
|
||||||
[0x800] = 12, -- #924900
|
|
||||||
[0x1000] = 13, -- #6D6D55
|
|
||||||
[0x2000] = 14, -- #DBDBAA
|
|
||||||
[0x4000] = 15, -- #6D00FF
|
|
||||||
[0x8000] = 16 -- #B6FF00
|
|
||||||
}
|
|
||||||
|
|
||||||
local colors = {
|
|
||||||
0x0001, -- #000000
|
|
||||||
0x0002, -- #FFFFFF
|
|
||||||
0x0004, -- #FF0000
|
|
||||||
0x0008, -- #00FF00
|
|
||||||
0x0010, -- #0000FF
|
|
||||||
0x0020, -- #00FFFF
|
|
||||||
0x0040, -- #FF00FF
|
|
||||||
0x0080, -- #FFFF00
|
|
||||||
0x0100, -- #FF6D00
|
|
||||||
0x0200, -- #6DFF55
|
|
||||||
0x0400, -- #24FFFF
|
|
||||||
0x0800, -- #924900
|
|
||||||
0x1000, -- #6D6D55
|
|
||||||
0x2000, -- #DBDBAA
|
|
||||||
0x4000, -- #6D00FF
|
|
||||||
0x8000 -- #B6FF00
|
|
||||||
}
|
|
||||||
|
|
||||||
local function write(text, term)
|
|
||||||
local x, y = term.getCursorPos()
|
|
||||||
local w, h = term.getSize()
|
|
||||||
|
|
||||||
for i = 1, #text do
|
|
||||||
local c = text:sub(i, i)
|
|
||||||
|
|
||||||
if c == "\n" then
|
|
||||||
y = y + 1
|
|
||||||
x = 1
|
|
||||||
elseif c == "\t" then
|
|
||||||
local tabSize = 4
|
|
||||||
local spaces = tabSize - ((x - 1) % tabSize)
|
|
||||||
term.write(string.rep(" ", spaces))
|
|
||||||
x = x + spaces
|
|
||||||
elseif c == "\b" then
|
|
||||||
if x > 1 then
|
|
||||||
x = x - 1
|
|
||||||
term.setCursorPos(x, y)
|
|
||||||
term.write(" ")
|
|
||||||
term.setCursorPos(x, y)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if x <= w and y <= h then
|
|
||||||
term.setCursorPos(x, y)
|
|
||||||
term.write(c)
|
|
||||||
x = x + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if x > w then
|
|
||||||
x = 1
|
|
||||||
y = y + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if y - 1 >= h then
|
|
||||||
term.scroll(1)
|
|
||||||
y = h
|
|
||||||
term.setCursorPos(x, y)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
term.setCursorPos(x, y)
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.devfs.data.tty={}
|
|
||||||
local ctrl,alt = false, false
|
|
||||||
|
|
||||||
local function serializeBool(bool)
|
|
||||||
if bool then
|
|
||||||
return "T"
|
|
||||||
else
|
|
||||||
return "F"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function newtty(obj, id, ev)
|
|
||||||
kernel.devfs.data["tty"][id] = function(op, mode)
|
|
||||||
if op=="type" then
|
|
||||||
return "character device"
|
|
||||||
elseif op=="open" then
|
|
||||||
local h = {
|
|
||||||
read=function(amount)
|
|
||||||
local rv=""
|
|
||||||
for i=1, amount or 1 do
|
|
||||||
local event = {ev()}
|
|
||||||
if event[1] then
|
|
||||||
rv=rv..event[1]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if rv=="" then rv=nil end
|
|
||||||
return rv
|
|
||||||
end,
|
|
||||||
write=function(content)
|
|
||||||
write(content, obj)
|
|
||||||
end,
|
|
||||||
size=function()
|
|
||||||
local s={obj.getSize()}
|
|
||||||
return table.concat(s,";")
|
|
||||||
end,
|
|
||||||
clear=function()
|
|
||||||
obj.clear()
|
|
||||||
obj.setCursorPos(1,1)
|
|
||||||
end,
|
|
||||||
gpos=function()
|
|
||||||
local s={obj.getCursorPos()}
|
|
||||||
return table.concat(s,";")
|
|
||||||
end,
|
|
||||||
spos=function(x,y)
|
|
||||||
return obj.setCursorPos(x,y)
|
|
||||||
end,
|
|
||||||
sfgc=function(c)
|
|
||||||
return obj.setTextColor(colors[c])
|
|
||||||
end,
|
|
||||||
sbgc=function(c)
|
|
||||||
return obj.setBackgroundColor(colors[c])
|
|
||||||
end,
|
|
||||||
gfgc=function()
|
|
||||||
return icolors[obj.getTextColor()]
|
|
||||||
end,
|
|
||||||
gbgc=function()
|
|
||||||
return icolors[obj.getBackgroundColor()]
|
|
||||||
end,
|
|
||||||
gctrl=function()
|
|
||||||
return serializeBool(ctrl)..";"..serializeBool(alt)
|
|
||||||
end
|
|
||||||
}
|
|
||||||
if mode=="rw" then
|
|
||||||
return h
|
|
||||||
elseif mode=="r" then
|
|
||||||
h["write"]=nil
|
|
||||||
return h
|
|
||||||
elseif mode=="w" then
|
|
||||||
h["read"]=nil
|
|
||||||
return h
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local fifo = kernel.newFifo()
|
|
||||||
|
|
||||||
kernel.processes.cctmond = function()
|
|
||||||
local timeout = false
|
|
||||||
while true do
|
|
||||||
local event = {kernel.computer:getMachineEvent()}
|
|
||||||
|
|
||||||
if event[1] then
|
|
||||||
local eventType = event[1]
|
|
||||||
local charOrKey = event[3]
|
|
||||||
|
|
||||||
-- Update modifier keys
|
|
||||||
if eventType == "keyPressed" then
|
|
||||||
if charOrKey == apis.keys.leftCtrl or charOrKey == apis.keys.rightCtrl then
|
|
||||||
ctrl = true
|
|
||||||
elseif charOrKey == apis.keys.leftAlt or charOrKey == apis.keys.rightAlt then
|
|
||||||
alt = true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Handle Ctrl+C
|
|
||||||
if ctrl and charOrKey == apis.keys.c then
|
|
||||||
for _, task in ipairs(syscall.getTasks()) do
|
|
||||||
syscall.sigsend(task, 1) -- SIGINT
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif eventType == "keyReleased" then
|
|
||||||
if charOrKey == apis.keys.leftCtrl or charOrKey == apis.keys.rightCtrl then
|
|
||||||
ctrl = false
|
|
||||||
elseif charOrKey == apis.keys.leftAlt or charOrKey == apis.keys.rightAlt then
|
|
||||||
alt = false
|
|
||||||
end
|
|
||||||
|
|
||||||
elseif eventType == "keyTyped" then
|
|
||||||
if charOrKey then fifo.push(charOrKey) end
|
|
||||||
end
|
|
||||||
|
|
||||||
timeout = false
|
|
||||||
else
|
|
||||||
timeout = true
|
|
||||||
end
|
|
||||||
|
|
||||||
if timeout then
|
|
||||||
sleep(0.05)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
newtty(apis.term, "TTY1", fifo.pop)
|
|
||||||
|
|
||||||
for i,v in ipairs({peripheral.find("monitor")}) do
|
|
||||||
v.setTextScale(.5)
|
|
||||||
v.write("Initializing...")
|
|
||||||
newtty(v,"TTY"..tostring(i+1),function () end)
|
|
||||||
end
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
local args={...}
|
|
||||||
local kernel=args[1]
|
|
||||||
local driver={}
|
|
||||||
|
|
||||||
driver.name="CCT Term Module"
|
|
||||||
driver.version="0.1.0"
|
|
||||||
driver.type="gpio"
|
|
||||||
driver.description="CCT redstone Module Kernel Module"
|
|
||||||
driver.arch="cct"
|
|
||||||
driver.author="HyperionOS Dev Team"
|
|
||||||
driver.license="MIT"
|
|
||||||
driver.api={}
|
|
||||||
|
|
||||||
function driver.load()
|
|
||||||
-- will
|
|
||||||
end
|
|
||||||
|
|
||||||
function driver.unload()
|
|
||||||
-- Nothing to unload
|
|
||||||
end
|
|
||||||
|
|
||||||
function driver.main()
|
|
||||||
-- Nothing to run
|
|
||||||
end
|
|
||||||
|
|
||||||
-- kernel.drivers.register(driver)
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.3.0
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
|
Xtimhedae
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
local lua = {
|
|
||||||
coroutine = true,
|
|
||||||
debug = true,
|
|
||||||
_HOST = true,
|
|
||||||
_VERSION = true,
|
|
||||||
assert = true,
|
|
||||||
collectgarbage = true,
|
|
||||||
error = true,
|
|
||||||
gcinfo = true,
|
|
||||||
getfenv = true,
|
|
||||||
getmetatable = true,
|
|
||||||
ipairs = true,
|
|
||||||
__inext = true,
|
|
||||||
load = true,
|
|
||||||
math = true,
|
|
||||||
next = true,
|
|
||||||
pairs = true,
|
|
||||||
pcall = true,
|
|
||||||
rawequal = true,
|
|
||||||
rawget = true,
|
|
||||||
rawlen = true,
|
|
||||||
rawset = true,
|
|
||||||
select = true,
|
|
||||||
setfenv = true,
|
|
||||||
setmetatable = true,
|
|
||||||
string = true,
|
|
||||||
table = true,
|
|
||||||
tonumber = true,
|
|
||||||
tostring = true,
|
|
||||||
type = true,
|
|
||||||
xpcall = true,
|
|
||||||
_G=true
|
|
||||||
}
|
|
||||||
|
|
||||||
local apis={}
|
|
||||||
for i,v in pairs(_G) do
|
|
||||||
if not lua[i] or lua[i]==nil then
|
|
||||||
apis[i]=v
|
|
||||||
_G[i]=nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
checkArg=nil
|
|
||||||
local oldcomputer=computer
|
|
||||||
_G.computer=nil
|
|
||||||
local os=os
|
|
||||||
_G.os=nil
|
|
||||||
|
|
||||||
function component.wrap(address)
|
|
||||||
local methods=oldcomponent.methods(address)
|
|
||||||
local object={}
|
|
||||||
for _,method in ipairs(methods) do
|
|
||||||
object[method]=function(_,...)
|
|
||||||
return oldcomponent.invoke(address,method,...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return object
|
|
||||||
end
|
|
||||||
|
|
||||||
local
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
local fs={}
|
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
local cp,c=component,computer;local b_addr=c.getBootAddress();local b_fs=cp.proxy(b_addr)
|
||||||
|
local gpu=cp.list("gpu")() and cp.proxy(cp.list("gpu")())
|
||||||
|
local e_rom=cp.list("eeprom")() and cp.proxy(cp.list("eeprom")())
|
||||||
|
local screens={}
|
||||||
|
for addr in cp.list("screen") do
|
||||||
|
if gpu then
|
||||||
|
gpu.bind(addr)
|
||||||
|
local w,h=gpu.maxResolution()
|
||||||
|
gpu.setResolution(w,h)
|
||||||
|
gpu.fill(1,1,w,h," ")
|
||||||
|
cp.proxy(addr).turnOn()
|
||||||
|
screens[#screens+1] = {cx=1,cy=1,w=w,h=h,addr=addr}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function scroll(scr) gpu.copy(1,2,scr.w,scr.h-1,0,-1);gpu.fill(1,scr.h,scr.w,1," ") end
|
||||||
|
local function write(t)
|
||||||
|
if not gpu then return end
|
||||||
|
for s=1, #screens do
|
||||||
|
local scr=screens[s]
|
||||||
|
gpu.bind(scr.addr)
|
||||||
|
for i=1,#t do
|
||||||
|
local char=t:sub(i,i)
|
||||||
|
if char=="\n" then scr.cx=1;scr.cy=scr.cy+1
|
||||||
|
elseif char=="\t" then scr.cx=scr.cx+(4-((scr.cx-1)%4))
|
||||||
|
elseif char=="\b" then if scr.cx>1 then scr.cx=scr.cx-1;gpu.set(scr.cx,scr.cy," ") end
|
||||||
|
else gpu.set(scr.cx,scr.cy,char);scr.cx=scr.cx+1 end
|
||||||
|
if scr.cx>scr.w then scr.cx=1;scr.cy=scr.cy+1 end
|
||||||
|
if scr.cy>scr.h then scroll(scr);scr.cy=scr.cy-1 end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function throw(err)
|
||||||
|
if gpu then
|
||||||
|
gpu.setBackground(0x0000AA);
|
||||||
|
gpu.setForeground(0xFFFFFF);
|
||||||
|
for i=1, #screens do
|
||||||
|
gpu.fill(1,1,screens[i].w,screens[i].h," ");
|
||||||
|
screens[i].cx,screens[i].cy=1,1;
|
||||||
|
end
|
||||||
|
write("CRITICAL KERNEL ERROR:\n"..tostring(err))
|
||||||
|
end
|
||||||
|
while true do c.pullSignal() end
|
||||||
|
end
|
||||||
|
local ok,err=xpcall(function()
|
||||||
|
local apis={}
|
||||||
|
local _l={coroutine=1,debug=1,_VERSION=1,assert=1,collectgarbage=1,error=1,getmetatable=1,ipairs=1,load=1,math=1,next=1,pairs=1,pcall=1,rawequal=1,rawget=1,rawlen=1,rawset=1,select=1,setmetatable=1,string=1,table=1,tonumber=1,tostring=1,type=1,xpcall=1,_G=1}
|
||||||
|
for k,v in pairs(_G) do if not _l[k] then apis[k]=v;_G[k]=nil end end
|
||||||
|
function sleep(t) local s=c.uptime()+t;while c.uptime()<s do c.pullSignal(s-c.uptime()) end end
|
||||||
|
local function getFile(path)
|
||||||
|
local h,err=b_fs.open(path,"r")
|
||||||
|
if not h then throw("Cannot open: "..path) end
|
||||||
|
local buf,chk="",""
|
||||||
|
repeat chk=b_fs.read(h,math.huge);buf=buf..(chk or "") until not chk
|
||||||
|
b_fs.close(h)
|
||||||
|
return buf
|
||||||
|
end
|
||||||
|
local Kernel=load(getFile("/kernel.lua"),"@Kernel")
|
||||||
|
local initFs=load(getFile("/oc/initdisks"),"@Init_disks")
|
||||||
|
local fs=load(getFile("/initfs"),"@InitFs")
|
||||||
|
if not Kernel then throw("Kernel load failed.") end
|
||||||
|
if initFs then initFs=initFs(apis, b_fs, b_addr) end
|
||||||
|
if fs then fs=fs() end
|
||||||
|
local eQ={}
|
||||||
|
local function qEv(e,...) table.insert(eQ,{e,...}) end
|
||||||
|
local efi={
|
||||||
|
getEpochMs=function() return math.floor(c.uptime()*1000) end,
|
||||||
|
getUptime=function() return c.uptime()*1000 end,
|
||||||
|
date=function() return tostring(apis.os.date()) end,
|
||||||
|
getMachineEvent=function()
|
||||||
|
if #eQ > 0 then
|
||||||
|
return table.unpack(table.remove(eQ, 1))
|
||||||
|
else
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
getEEPROM=function() return e_rom and e_rom.get() or "" end,
|
||||||
|
setEEPROM=function(_,t) if e_rom then e_rom.set(t) end end,
|
||||||
|
getNvram=function() return e_rom and e_rom.getData() or "" end,
|
||||||
|
setNvram=function(_,t) if e_rom then e_rom.setData(t) end end,
|
||||||
|
beep=function(freq, timeMs) c.beep(freq, timeMs / 1000) end,
|
||||||
|
initfs=fs,
|
||||||
|
disks=initFs,
|
||||||
|
architecture="oc",
|
||||||
|
firmware=apis,
|
||||||
|
reboot=false,
|
||||||
|
yield=function() coroutine.yield() end,
|
||||||
|
screenCtl={
|
||||||
|
print=function(_,t) write(tostring(t).."\n") end,
|
||||||
|
printInline=function(_,t) write(tostring(t)) end,
|
||||||
|
clear=function() if gpu then for i=1, #screens do local scr=screens[i];gpu.bind(scr.addr);gpu.fill(1,1,scr.w,scr.h," ");scr.cx,scr.cy=1,1 end end end,
|
||||||
|
resetCursor=function() for i=1, #screens do local scr=screens[i];scr.cx,scr.cy=1,1 end end,
|
||||||
|
setBackgroundColor=function(_,cl) if gpu then gpu.setBackground(cl) end end,
|
||||||
|
setTextColor=function(_,cl) if gpu then gpu.setForeground(cl) end end,
|
||||||
|
enable=function() end,
|
||||||
|
disable=function() end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
local kCo=coroutine.create(function()
|
||||||
|
local s,e=xpcall(Kernel,debug.traceback,efi)
|
||||||
|
if not s and not efi.reboot then throw(e) end
|
||||||
|
if efi.reboot then c.shutdown(true) else c.shutdown() end
|
||||||
|
end)
|
||||||
|
efi.screenCtl:print("Loaded in "..tostring(c.uptime()).."s")
|
||||||
|
while true do
|
||||||
|
local st,e=coroutine.resume(kCo)
|
||||||
|
c.pushSignal("NoSleep")
|
||||||
|
local ex=false
|
||||||
|
while not ex do
|
||||||
|
local ev={c.pullSignal(0)}
|
||||||
|
if ev[1]=="key_down" then qEv("keyPressed",1,ev[3]);qEv("keyTyped",1,string.char(ev[3]))
|
||||||
|
elseif ev[1]=="key_up" then qEv("keyReleased",1,ev[3])
|
||||||
|
end
|
||||||
|
if ev[1]=="NoSleep" then ex=true else qEv(table.unpack(ev)) end
|
||||||
|
end
|
||||||
|
if st=="error" or coroutine.status(kCo)=="dead" then
|
||||||
|
if efi.reboot then c.shutdown(true) end
|
||||||
|
throw("Kernel fault: "..tostring(e))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,debug.traceback)
|
||||||
|
if not ok then throw("Boot fault: "..err) end
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
local init
|
||||||
|
do
|
||||||
|
local component_invoke = component.invoke
|
||||||
|
local function boot_invoke(address, method, ...)
|
||||||
|
local result = table.pack(pcall(component_invoke, address, method, ...))
|
||||||
|
if not result[1] then
|
||||||
|
return nil, result[2]
|
||||||
|
else
|
||||||
|
return table.unpack(result, 2, result.n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- backwards compatibility, may remove later
|
||||||
|
local eeprom = component.list("eeprom")()
|
||||||
|
computer.getBootAddress = function()
|
||||||
|
return boot_invoke(eeprom, "getData")
|
||||||
|
end
|
||||||
|
computer.setBootAddress = function(address)
|
||||||
|
return boot_invoke(eeprom, "setData", address)
|
||||||
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local screen = component.list("screen")()
|
||||||
|
local gpu = component.list("gpu")()
|
||||||
|
if gpu and screen then
|
||||||
|
boot_invoke(gpu, "bind", screen)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function tryLoadFrom(address)
|
||||||
|
local handle, reason = boot_invoke(address, "open", "/oc/boot.lua")
|
||||||
|
if not handle then
|
||||||
|
return nil, reason
|
||||||
|
end
|
||||||
|
local buffer = ""
|
||||||
|
repeat
|
||||||
|
local data, reason = boot_invoke(address, "read", handle, math.maxinteger or math.huge)
|
||||||
|
if not data and reason then
|
||||||
|
return nil, reason
|
||||||
|
end
|
||||||
|
buffer = buffer .. (data or "")
|
||||||
|
until not data
|
||||||
|
boot_invoke(address, "close", handle)
|
||||||
|
return load(buffer, "=init")
|
||||||
|
end
|
||||||
|
local reason
|
||||||
|
if computer.getBootAddress() then
|
||||||
|
init, reason = tryLoadFrom(computer.getBootAddress())
|
||||||
|
end
|
||||||
|
if not init then
|
||||||
|
computer.setBootAddress()
|
||||||
|
for address in component.list("filesystem") do
|
||||||
|
init, reason = tryLoadFrom(address)
|
||||||
|
if init then
|
||||||
|
computer.setBootAddress(address)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not init then
|
||||||
|
error("no bootable medium found" .. (reason and (": " .. tostring(reason)) or ""), 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local ok, err = xpcall(init,debug.traceback)
|
||||||
|
if not ok then
|
||||||
|
error(err)
|
||||||
|
else
|
||||||
|
return err
|
||||||
|
end
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
local args = {...}
|
||||||
|
local apis = args[1]
|
||||||
|
local bootdrive = args[2]
|
||||||
|
local bootaddr = args[3]
|
||||||
|
local disks = {}
|
||||||
|
local internal = {}
|
||||||
|
|
||||||
|
local function createDisk(id, hw)
|
||||||
|
local disk = {
|
||||||
|
address = id,
|
||||||
|
isReadOnly = function() return hw.isReadOnly() end
|
||||||
|
}
|
||||||
|
|
||||||
|
function disk:spaceUsed() return hw.spaceUsed() end
|
||||||
|
function disk:spaceTotal() return hw.spaceTotal() end
|
||||||
|
|
||||||
|
function disk:list(path)
|
||||||
|
if not hw.exists(path) or not hw.isDirectory(path) then
|
||||||
|
return nil, "not directory"
|
||||||
|
end
|
||||||
|
return hw.list(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:fileExists(path)
|
||||||
|
return hw.exists(path) and not hw.isDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:directoryExists(path)
|
||||||
|
return hw.exists(path) and hw.isDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:type(path)
|
||||||
|
if not hw.exists(path) then
|
||||||
|
return nil
|
||||||
|
elseif hw.isDirectory(path) then
|
||||||
|
return "directory"
|
||||||
|
else
|
||||||
|
return "file"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:makeDirectory(path)
|
||||||
|
hw.makeDirectory(path)
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:remove(path)
|
||||||
|
if hw.exists(path) then hw.remove(path) end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:setLabel(label) hw.setLabel(label) end
|
||||||
|
function disk:getLabel() return hw.getLabel() end
|
||||||
|
|
||||||
|
function disk:attributes(path)
|
||||||
|
return {
|
||||||
|
size = hw.size(path),
|
||||||
|
isDir = hw.isDirectory(path),
|
||||||
|
isReadOnly = hw.isReadOnly(),
|
||||||
|
modified = hw.lastModified(path),
|
||||||
|
created = hw.lastModified(path)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path, mode)
|
||||||
|
local fd = hw.open(path, mode)
|
||||||
|
|
||||||
|
if not fd then
|
||||||
|
local dir = path:match("^(.*)/[^/]+$")
|
||||||
|
|
||||||
|
if dir and not hw.exists(dir) then
|
||||||
|
local ok, err = hw.makeDirectory(dir)
|
||||||
|
if not ok then
|
||||||
|
return nil, err or "could not create parent directory"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fd = hw.open(path, mode)
|
||||||
|
if not fd then
|
||||||
|
return nil, "file not found or access denied"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
read = function(num) return hw.read(fd, num) end,
|
||||||
|
|
||||||
|
readAll = function()
|
||||||
|
local data = {}
|
||||||
|
while true do
|
||||||
|
local chunk = hw.read(fd, math.huge)
|
||||||
|
if not chunk then break end
|
||||||
|
data[#data + 1] = chunk
|
||||||
|
end
|
||||||
|
return table.concat(data)
|
||||||
|
end,
|
||||||
|
|
||||||
|
write = function(data) return hw.write(fd, data) end,
|
||||||
|
seek = function(whence, offset)
|
||||||
|
return hw.seek(fd, whence, offset)
|
||||||
|
end,
|
||||||
|
|
||||||
|
flush = function()
|
||||||
|
hw.close(fd)
|
||||||
|
fd = hw.open(path, mode)
|
||||||
|
end,
|
||||||
|
|
||||||
|
close = function()
|
||||||
|
hw.close(fd)
|
||||||
|
fd = nil
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
internal["$"] = createDisk("$", bootdrive)
|
||||||
|
|
||||||
|
local function refresh()
|
||||||
|
disks = {}
|
||||||
|
for addr in apis.component.list("filesystem") do
|
||||||
|
if addr ~= bootaddr then
|
||||||
|
disks[addr] = createDisk(addr, apis.component.proxy(addr))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function iter()
|
||||||
|
refresh()
|
||||||
|
local combined = {}
|
||||||
|
|
||||||
|
for id, obj in pairs(internal) do combined[id] = obj end
|
||||||
|
for id, obj in pairs(disks) do combined[id] = obj end
|
||||||
|
|
||||||
|
return pairs(combined)
|
||||||
|
end
|
||||||
|
|
||||||
|
return {refresh = refresh, list = iter}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
kernel.oc={}
|
||||||
|
kernel.oc.component=kernel.apis.component
|
||||||
|
kernel.oc.computer=kernel.apis.computer
|
||||||
|
|
||||||
|
kernel.log("Installed components:")
|
||||||
|
for addr, typ in kernel.oc.component.list() do
|
||||||
|
local dev=kernel.oc.component.proxy(addr)
|
||||||
|
local tier=""
|
||||||
|
if dev.getTier then
|
||||||
|
tier=dev.getTier()
|
||||||
|
end
|
||||||
|
kernel.log(" "..addr.." : "..typ..tier)
|
||||||
|
end
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local component=kernel.oc.component
|
||||||
|
|
||||||
|
local function wrap(p)
|
||||||
|
return function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if kernel.uid~=0 then error("EACCES") end
|
||||||
|
return component.proxy(p)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for addr, typ in kernel.oc.component.list() do
|
||||||
|
local obj=wrap(addr)
|
||||||
|
kernel.devfs.data["raw"][addr]=obj
|
||||||
|
local typefolder=kernel.devfs.data["raw"]["by-type"][typ]
|
||||||
|
if not typefolder then typefolder={} end
|
||||||
|
typefolder[tostring(#table.keys(typefolder)+1)] = obj
|
||||||
|
kernel.devfs.data["raw"]["by-type"][typ]=typefolder
|
||||||
|
end
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
-- OpenComputers TTY driver
|
||||||
|
|
||||||
|
local kernel = ...
|
||||||
|
local component = kernel.oc.component
|
||||||
|
local computer = kernel.oc.computer
|
||||||
|
|
||||||
|
local plt = {
|
||||||
|
0x000000, 0x000040, 0x000080, 0x0000C0, 0x0000FF,
|
||||||
|
0x002400, 0x002440, 0x002480, 0x0024C0, 0x0024FF,
|
||||||
|
0x004900, 0x004940, 0x004980, 0x0049C0, 0x0049FF,
|
||||||
|
0x006D00, 0x006D40, 0x006D80, 0x006DC0, 0x006DFF,
|
||||||
|
0x009200, 0x009240, 0x009280, 0x0092C0, 0x0092FF,
|
||||||
|
0x00B600, 0x00B640, 0x00B680, 0x00B6C0, 0x00B6FF,
|
||||||
|
0x00DB00, 0x00DB40, 0x00DB80, 0x00DBC0, 0x00DBFF,
|
||||||
|
0x00FF00, 0x00FF40, 0x00FF80, 0x00FFC0, 0x00FFFF,
|
||||||
|
|
||||||
|
0x330000, 0x330040, 0x330080, 0x3300C0, 0x3300FF,
|
||||||
|
0x332400, 0x332440, 0x332480, 0x3324C0, 0x3324FF,
|
||||||
|
0x334900, 0x334940, 0x334980, 0x3349C0, 0x3349FF,
|
||||||
|
0x336D00, 0x336D40, 0x336D80, 0x336DC0, 0x336DFF,
|
||||||
|
0x339200, 0x339240, 0x339280, 0x3392C0, 0x3392FF,
|
||||||
|
0x33B600, 0x33B640, 0x33B680, 0x33B6C0, 0x33B6FF,
|
||||||
|
0x33DB00, 0x33DB40, 0x33DB80, 0x33DBC0, 0x33DBFF,
|
||||||
|
0x33FF00, 0x33FF40, 0x33FF80, 0x33FFC0, 0x33FFFF,
|
||||||
|
|
||||||
|
0x660000, 0x660040, 0x660080, 0x6600C0, 0x6600FF,
|
||||||
|
0x662400, 0x662440, 0x662480, 0x6624C0, 0x6624FF,
|
||||||
|
0x664900, 0x664940, 0x664980, 0x6649C0, 0x6649FF,
|
||||||
|
0x666D00, 0x666D40, 0x666D80, 0x666DC0, 0x666DFF,
|
||||||
|
0x669200, 0x669240, 0x669280, 0x6692C0, 0x6692FF,
|
||||||
|
0x66B600, 0x66B640, 0x66B680, 0x66B6C0, 0x66B6FF,
|
||||||
|
0x66DB00, 0x66DB40, 0x66DB80, 0x66DBC0, 0x66DBFF,
|
||||||
|
0x66FF00, 0x66FF40, 0x66FF80, 0x66FFC0, 0x66FFFF,
|
||||||
|
|
||||||
|
0x990000, 0x990040, 0x990080, 0x9900C0, 0x9900FF,
|
||||||
|
0x992400, 0x992440, 0x992480, 0x9924C0, 0x9924FF,
|
||||||
|
0x994900, 0x994940, 0x994980, 0x9949C0, 0x9949FF,
|
||||||
|
0x996D00, 0x996D40, 0x996D80, 0x996DC0, 0x996DFF,
|
||||||
|
0x999200, 0x999240, 0x999280, 0x9992C0, 0x9992FF,
|
||||||
|
0x99B600, 0x99B640, 0x99B680, 0x99B6C0, 0x99B6FF,
|
||||||
|
0x99DB00, 0x99DB40, 0x99DB80, 0x99DBC0, 0x99DBFF,
|
||||||
|
0x99FF00, 0x99FF40, 0x99FF80, 0x99FFC0, 0x99FFFF,
|
||||||
|
|
||||||
|
0xCC0000, 0xCC0040, 0xCC0080, 0xCC00C0, 0xCC00FF,
|
||||||
|
0xCC2400, 0xCC2440, 0xCC2480, 0xCC24C0, 0xCC24FF,
|
||||||
|
0xCC4900, 0xCC4940, 0xCC4980, 0xCC49C0, 0xCC49FF,
|
||||||
|
0xCC6D00, 0xCC6D40, 0xCC6D80, 0xCC6DC0, 0xCC6DFF,
|
||||||
|
0xCC9200, 0xCC9240, 0xCC9280, 0xCC92C0, 0xCC92FF,
|
||||||
|
0xCCB600, 0xCCB640, 0xCCB680, 0xCCB6C0, 0xCCB6FF,
|
||||||
|
0xCCDB00, 0xCCDB40, 0xCCDB80, 0xCCDBC0, 0xCCDBFF,
|
||||||
|
0xCCFF00, 0xCCFF40, 0xCCFF80, 0xCCFFC0, 0xCCFFFF,
|
||||||
|
|
||||||
|
0xFF0000, 0xFF0040, 0xFF0080, 0xFF00C0, 0xFF00FF,
|
||||||
|
0xFF2400, 0xFF2440, 0xFF2480, 0xFF24C0, 0xFF24FF,
|
||||||
|
0xFF4900, 0xFF4940, 0xFF4980, 0xFF49C0, 0xFF49FF,
|
||||||
|
0xFF6D00, 0xFF6D40, 0xFF6D80, 0xFF6DC0, 0xFF6DFF,
|
||||||
|
0xFF9200, 0xFF9240, 0xFF9280, 0xFF92C0, 0xFF92FF,
|
||||||
|
0xFFB600, 0xFFB640, 0xFFB680, 0xFFB6C0, 0xFFB6FF,
|
||||||
|
0xFFDB00, 0xFFDB40, 0xFFDB80, 0xFFDBC0, 0xFFDBFF,
|
||||||
|
0xFFFF00, 0xFFFF40, 0xFFFF80, 0xFFFFC0, 0xFFFFFF,
|
||||||
|
|
||||||
|
0x000000, 0x0F0F0F, 0x1E1E1E, 0x2D2D2D,
|
||||||
|
0x3C3C3C, 0x4B4B4B, 0x5A5A5A, 0x696969,
|
||||||
|
0x787878, 0x878787, 0x969696, 0xA5A5A5,
|
||||||
|
0xB4B4B4, 0xC3C3C3, 0xD2D2D2, 0xE1E1E1
|
||||||
|
}
|
||||||
|
|
||||||
|
local gpus,screens = {},{}
|
||||||
|
|
||||||
|
for addr, typ in component.list("gpu") do
|
||||||
|
if typ == "gpu" then
|
||||||
|
gpus[#gpus+1]=component.proxy(addr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #gpus>0 then
|
||||||
|
for addr, typ in component.list("screen") do
|
||||||
|
if typ == "screen" then
|
||||||
|
gpus[1].bind(addr)
|
||||||
|
local w,h = gpus[1].maxResolution()
|
||||||
|
gpus[1].setResolution(w,h)
|
||||||
|
screens[addr]={cx=1,cy=1,w=w,h=h,addr=addr,fg=0,bg=0,gpu=nil}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local scrnum = 0
|
||||||
|
for i,v in pairs(screens) do
|
||||||
|
scrnum=scrnum+1
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("initialized "..tostring(#gpus).." gpus and "..tostring(scrnum).." screens")
|
||||||
|
|
||||||
|
local binds,newid={},0
|
||||||
|
local curaddr=""
|
||||||
|
local function scroll(scr, gpu) gpu.copy(1,2,scr.w,scr.h-1,0,-1);gpu.fill(1,scr.h,scr.w,1," ") end
|
||||||
|
local function write(text, addr)
|
||||||
|
local g=gpus[1]
|
||||||
|
local scr=screens[addr]
|
||||||
|
if not scr then error("NODEV") end
|
||||||
|
if curaddr~=addr then g.bind(addr); g.setForeground(scr.fg); g.setBackground(scr.bg) end
|
||||||
|
for i=1,#text do
|
||||||
|
local char=text:sub(i,i)
|
||||||
|
if char=="\n" then scr.cx=1;scr.cy=scr.cy+1
|
||||||
|
elseif char=="\t" then scr.cx=scr.cx+(4-((scr.cx-1)%4))
|
||||||
|
elseif char=="\b" then if scr.cx>1 then scr.cx=scr.cx-1;g.set(scr.cx,scr.cy," ") end
|
||||||
|
else g.set(scr.cx,scr.cy,char);scr.cx=scr.cx+1 end
|
||||||
|
if scr.cx>scr.w then scr.cx=1;scr.cy=scr.cy+1 end
|
||||||
|
if scr.cy>scr.h then scroll(scr, g);scr.cy=scr.cy-1 end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function newtty(addr)
|
||||||
|
newid=newid+1
|
||||||
|
kernel.devfs.data["tty"][tostring(newid)]=function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Terminal"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount) end,
|
||||||
|
write=function(content)
|
||||||
|
write(content, addr)
|
||||||
|
end,
|
||||||
|
size=function()
|
||||||
|
return screens[addr].w, screens[addr].h
|
||||||
|
end,
|
||||||
|
clear=function()
|
||||||
|
local g=gpus[1]
|
||||||
|
local scr=screens[addr]
|
||||||
|
if addr~=curaddr then g.bind(addr) end
|
||||||
|
g.fill(1,1,scr.w,scr.h," ");
|
||||||
|
scr.cx,scr.cy=1,1
|
||||||
|
end,
|
||||||
|
gpos=function()
|
||||||
|
return screens[addr].cx, screens[addr].cy
|
||||||
|
end,
|
||||||
|
spos=function(x,y)
|
||||||
|
if not tonumber(x) or not tonumber(y) then error("Invailid arg") end
|
||||||
|
screens[addr].cx, screens[addr].cy = x,y
|
||||||
|
end,
|
||||||
|
sfgc=function(c)
|
||||||
|
screens[addr].fg=c
|
||||||
|
end,
|
||||||
|
sbgc=function(c)
|
||||||
|
screens[addr].bg=c
|
||||||
|
end,
|
||||||
|
gfgc=function()
|
||||||
|
return screens[addr].fg
|
||||||
|
end,
|
||||||
|
gbgc=function()
|
||||||
|
return screens[addr].bg
|
||||||
|
end,
|
||||||
|
isvirt=function()
|
||||||
|
return false
|
||||||
|
end,
|
||||||
|
gplt=function()
|
||||||
|
return plt
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #gpus>=1 and scrnum>=1 then
|
||||||
|
for addr,obj in pairs(screens) do
|
||||||
|
newtty(addr)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,113 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
kernel.oc.keyboards={}
|
||||||
|
local kbs=kernel.oc.keyboards
|
||||||
|
|
||||||
|
local kbidx=0
|
||||||
|
local keyboard = {}
|
||||||
|
|
||||||
|
keyboard.keys = {
|
||||||
|
back = 0x0E, -- backspace
|
||||||
|
delete = 0xD3,
|
||||||
|
down = 0xD0,
|
||||||
|
enter = 0x1C,
|
||||||
|
home = 0xC7,
|
||||||
|
lcontrol = 0x1D,
|
||||||
|
left = 0xCB,
|
||||||
|
lmenu = 0x38, -- left Alt
|
||||||
|
lshift = 0x2A,
|
||||||
|
pageDown = 0xD1,
|
||||||
|
rcontrol = 0x9D,
|
||||||
|
right = 0xCD,
|
||||||
|
rmenu = 0xB8, -- right Alt
|
||||||
|
rshift = 0x36,
|
||||||
|
space = 0x39,
|
||||||
|
tab = 0x0F,
|
||||||
|
up = 0xC8,
|
||||||
|
["end"] = 0xCF,
|
||||||
|
numpadenter = 0x9C,
|
||||||
|
}
|
||||||
|
kernel.oc.keys=keyboard.keys
|
||||||
|
|
||||||
|
for i=0, 255 do
|
||||||
|
kernel.oc.keys[string.char(i)]=i
|
||||||
|
end
|
||||||
|
|
||||||
|
-------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function keyboard.isAltDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lmenu] or kbs[addr].pressedCodes[keyboard.keys.rmenu]
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isControl(char)
|
||||||
|
return type(char) == "number" and (char < 0x20 or (char >= 0x7F and char <= 0x9F))
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isControlDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lcontrol] or kbs[addr].pressedCodes[keyboard.keys.rcontrol]
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isKeyDown(addr,charOrCode)
|
||||||
|
if type(charOrCode) == "string" then
|
||||||
|
return kbs[addr].pressedChars[utf8 and utf8.codepoint(charOrCode) or charOrCode:byte()]
|
||||||
|
elseif type(charOrCode) == "number" then
|
||||||
|
return kbs[addr].pressedCodes[charOrCode]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function keyboard.isShiftDown(addr)
|
||||||
|
return kbs[addr].pressedCodes[keyboard.keys.lshift] or kbs[addr].pressedCodes[keyboard.keys.rshift]
|
||||||
|
end
|
||||||
|
kernel.oc.keyboard=keyboard
|
||||||
|
|
||||||
|
local function serializeBool(bool)
|
||||||
|
if bool then
|
||||||
|
return "T"
|
||||||
|
else
|
||||||
|
return "F"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for addr, typ in kernel.oc.component.list("keyboard") do
|
||||||
|
if typ=="keyboard" then
|
||||||
|
kbidx=kbidx+1
|
||||||
|
kbs[addr]={pressedChars = {}, pressedCodes = {}, events=kernel.newFifo()}
|
||||||
|
kernel.devfs.data["input"]["keyboard"..tostring(kbidx)]=function(op,mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "Keyboard"
|
||||||
|
elseif op=="open" then
|
||||||
|
local h = {
|
||||||
|
read=function(amount)
|
||||||
|
local rv=""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
local event = {kernel.oc.keyboards[addr].events.pop()}
|
||||||
|
if event[1] then
|
||||||
|
rv=rv..event[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if rv=="" then rv=nil end
|
||||||
|
return rv
|
||||||
|
end,
|
||||||
|
write=function(content)
|
||||||
|
end,
|
||||||
|
gctrl=function()
|
||||||
|
return serializeBool(keyboard.isControlDown(addr))..";"..serializeBool(keyboard.isAltDown(addr))
|
||||||
|
end,
|
||||||
|
isKeyDown=function(char)
|
||||||
|
return keyboard.isKeyDown(addr,char)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
if mode=="rw" then
|
||||||
|
return h
|
||||||
|
elseif mode=="r" then
|
||||||
|
h["write"]=nil
|
||||||
|
return h
|
||||||
|
elseif mode=="w" then
|
||||||
|
h["read"]=nil
|
||||||
|
return h
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local keys=kernel.oc.keys
|
||||||
|
|
||||||
|
kernel.processes.ocdaemon = function()
|
||||||
|
|
||||||
|
local timeout = false
|
||||||
|
|
||||||
|
kernel.log("OC daemon started")
|
||||||
|
|
||||||
|
local ctrlKeyMap = {
|
||||||
|
[keys.a]=1, [keys.b]=2, [keys.c]=3,
|
||||||
|
[keys.d]=4, [keys.e]=5, [keys.f]=6,
|
||||||
|
[keys.g]=7, [keys.h]=8, [keys.i]=9,
|
||||||
|
[keys.j]=10, [keys.k]=11, [keys.l]=12,
|
||||||
|
[keys.m]=13, [keys.n]=14, [keys.o]=15,
|
||||||
|
[keys.p]=16, [keys.q]=17, [keys.r]=18,
|
||||||
|
[keys.s]=19, [keys.t]=20, [keys.u]=21,
|
||||||
|
[keys.v]=22, [keys.w]=23, [keys.x]=24,
|
||||||
|
[keys.y]=25, [keys.z]=26,
|
||||||
|
}
|
||||||
|
|
||||||
|
local specialKeyMap = {
|
||||||
|
[keys.up] = "\27[A",
|
||||||
|
[keys.down] = "\27[B",
|
||||||
|
[keys.right] = "\27[C",
|
||||||
|
[keys.left] = "\27[D",
|
||||||
|
[keys.home] = "\27[H",
|
||||||
|
[keys["end"]] = "\27[F",
|
||||||
|
--[keys.pageUp] = "\27[5~",
|
||||||
|
--[keys.pageDown] = "\27[6~",
|
||||||
|
[keys.delete] = "\27[3~",
|
||||||
|
[keys.enter] = "\n",
|
||||||
|
[keys.back] = "\b",
|
||||||
|
[keys.tab] = "\t",
|
||||||
|
}
|
||||||
|
|
||||||
|
while true do
|
||||||
|
|
||||||
|
local event = {kernel.EFI.getMachineEvent()}
|
||||||
|
|
||||||
|
if event[1] then
|
||||||
|
|
||||||
|
local eventType = event[1]
|
||||||
|
local addr = event[2]
|
||||||
|
local char = event[3]
|
||||||
|
local keyCode = event[4]
|
||||||
|
|
||||||
|
if eventType == "key_down" then
|
||||||
|
|
||||||
|
local keyboard = kernel.oc.keyboards[addr]
|
||||||
|
|
||||||
|
if keyboard.ctrl then
|
||||||
|
|
||||||
|
local ctrlByte = ctrlKeyMap[keyCode]
|
||||||
|
|
||||||
|
if ctrlByte then
|
||||||
|
if ctrlByte == 3 then
|
||||||
|
|
||||||
|
for _, task in ipairs(syscall.getTasks()) do
|
||||||
|
syscall.sigsend(task, 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
keyboard.events.push(string.char(ctrlByte))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
local special = specialKeyMap[keyCode]
|
||||||
|
|
||||||
|
if special then
|
||||||
|
keyboard.events.push(special)
|
||||||
|
|
||||||
|
elseif char and char > 0 then
|
||||||
|
keyboard.events.push(kernel.apis.unicode.char(char))
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif eventType == "key_up" then
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
timeout = false
|
||||||
|
|
||||||
|
else
|
||||||
|
timeout = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if timeout then
|
||||||
|
sleep(0.05)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("OC daemon queued for execution")
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel=...
|
||||||
|
local alpha = 0.85
|
||||||
|
local C_target = 0.01
|
||||||
|
local Tmin = 0.0005
|
||||||
|
local Tmax = 0.5
|
||||||
|
local lambda_budget = 0.08
|
||||||
|
local lambda_clamp = 0.03
|
||||||
|
local lambda_var = 0.02
|
||||||
|
local k_min = 0.5
|
||||||
|
local k_max = 0.5
|
||||||
|
local B = 0.01
|
||||||
|
local nextyield=0
|
||||||
|
|
||||||
|
function kernel.main()
|
||||||
|
kernel.log("Starting main loop...")
|
||||||
|
kernel.saveLog()
|
||||||
|
local stopLog=5
|
||||||
|
local logTasks=100
|
||||||
|
|
||||||
|
while not kernel.exitMain do
|
||||||
|
if kernel.config.logTasks then
|
||||||
|
if logTasks<0 then
|
||||||
|
kernel.log("Active Tasks:")
|
||||||
|
for i,v in pairs(kernel.tasks) do
|
||||||
|
kernel.log(v.name.." : "..v.status.." : "..tostring(v.pid).." : "..tostring(v.exit))
|
||||||
|
end
|
||||||
|
kernel.log("[END BLOCK]")
|
||||||
|
logTasks=100
|
||||||
|
end
|
||||||
|
logTasks=logTasks-1
|
||||||
|
end
|
||||||
|
local N = 0
|
||||||
|
local Tmin_hit = 0
|
||||||
|
local Tmax_hit = 0
|
||||||
|
local totalTaskTime = 0
|
||||||
|
local taskTimes = {}
|
||||||
|
|
||||||
|
for pid, task in pairs(kernel.tasks) do
|
||||||
|
if kernel.exitMain then break end
|
||||||
|
|
||||||
|
for _,list in ipairs(kernel.perTaskHooks) do
|
||||||
|
for id,hook in ipairs(list) do
|
||||||
|
local ok,err = xpcall(hook, debug.traceback, task, pid)
|
||||||
|
if not ok then
|
||||||
|
kernel.log("[MAIN HOOK ERR]: "..err, "ERROR", 0xFF0000)
|
||||||
|
list[id]=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if task.status == "R" then
|
||||||
|
N = N + 1
|
||||||
|
task.timeSlice = math.min(Tmax, math.max(Tmin, B / (N ^ alpha)))
|
||||||
|
if task.status == "R" then
|
||||||
|
local startTime = kernel.EFI:getEpochMs()
|
||||||
|
local ret
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Running task " .. tostring(task.pid) .. " (" .. task.name .. ") with time slice " .. string.format("%.3f", task.timeSlice) .. "s", "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
|
||||||
|
ret = { coroutine.resume(task.coro, table.unpack(task.syscallReturn)) }
|
||||||
|
|
||||||
|
|
||||||
|
local elapsed = kernel.EFI:getEpochMs() - startTime
|
||||||
|
task.lastTime = elapsed
|
||||||
|
task.totalTime = (task.totalTime or 0) + elapsed
|
||||||
|
task.numRuns = (task.numRuns or 0) + 1
|
||||||
|
|
||||||
|
taskTimes[#taskTimes+1] = elapsed
|
||||||
|
totalTaskTime = totalTaskTime + elapsed
|
||||||
|
|
||||||
|
if elapsed <= Tmin then Tmin_hit = Tmin_hit + 1 end
|
||||||
|
if elapsed >= Tmax then Tmax_hit = Tmax_hit + 1 end
|
||||||
|
|
||||||
|
if ret[1] == "error" or ret[1] == false then
|
||||||
|
kernel.log("processHandlerException: " .. tostring(ret[2]), "ERROR", 0xFF0000)
|
||||||
|
task.status = "Z"
|
||||||
|
task.exit = "processHandlerException: " .. tostring(ret[2])
|
||||||
|
|
||||||
|
elseif ret[1] == "timeout" then
|
||||||
|
task.ivs = task.ivs + 1
|
||||||
|
task.syscallReturn = {}
|
||||||
|
|
||||||
|
elseif ret[1] == "success" or ret[1] == true then
|
||||||
|
task.vs = task.vs + 1
|
||||||
|
|
||||||
|
if ret[2] == "syscall" then
|
||||||
|
local scname = ret[3]
|
||||||
|
if kernel.syscalls[scname] then
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall: " .. scname, "DBUG", 0x00FFFF)
|
||||||
|
for i = 4, #ret do
|
||||||
|
kernel.log(" inval[" .. (i-3) .. "] = " .. tostring(ret[i]), "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local sysret = { xpcall(kernel.syscalls[scname], debug.traceback, table.unpack(ret, 4)) }
|
||||||
|
|
||||||
|
if kernel.config.debugSyscalls then
|
||||||
|
if not sysret[1] then
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " failed: " .. tostring(sysret[2]), "ERROR", 0xFF0000)
|
||||||
|
else
|
||||||
|
kernel.log("Task " .. task.pid .. " syscall " .. scname .. " ok, " .. (#sysret-1) .. " retvals", "DBUG", 0x00FFFF)
|
||||||
|
for i = 2, #sysret do
|
||||||
|
local v = type(sysret[i]) == "table" and table.serialize(sysret[i]) or tostring(sysret[i])
|
||||||
|
kernel.log(" retval[" .. (i-1) .. "] = " .. v, "DBUG", 0x00FFFF)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not sysret[1] then
|
||||||
|
task.syscallReturn = { false, sysret[2] }
|
||||||
|
else
|
||||||
|
task.syscallReturn = { true, table.unpack(sysret, 2) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
task.syscallReturn = { false, "Unknown syscall: " .. tostring(scname) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if kernel.EFI:getEpochMs()>=nextyield then
|
||||||
|
kernel.EFI:yield()
|
||||||
|
nextyield=kernel.EFI:getEpochMs()+5000
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local T_prev_avg = (N > 0) and (totalTaskTime / N) or 0
|
||||||
|
local T_prev_var = 0
|
||||||
|
for _, t in ipairs(taskTimes) do
|
||||||
|
T_prev_var = T_prev_var + (t - T_prev_avg) ^ 2
|
||||||
|
end
|
||||||
|
if N > 0 then T_prev_var = T_prev_var / N end
|
||||||
|
|
||||||
|
if N > 0 then
|
||||||
|
local f_clamp = k_min * (Tmin_hit / N) - k_max * (Tmax_hit / N)
|
||||||
|
local B_budget = (C_target * (N ^ (alpha - 1))) / math.max(T_prev_avg, 1e-8)
|
||||||
|
B = B + lambda_budget * (B_budget - B) + lambda_clamp * f_clamp - lambda_var * T_prev_var
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.hpv.reapDeadTasks()
|
||||||
|
if stopLog > 0 then
|
||||||
|
kernel.log("Executed " .. N .. " tasks, avg time: " .. string.format("%.2f", T_prev_avg) .. "ms, var: " .. string.format("%.2f", T_prev_var) .. ", B: " .. string.format("%.5f", B))
|
||||||
|
stopLog = stopLog - 1
|
||||||
|
kernel.saveLog()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
firmware for Opencomputers mod
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0.0
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Hyperion
|
||||||
|
Astronand
|
||||||
|
Spsf
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
U $;/
|
|
||||||
U devfs0000;/dev/
|
|
||||||
U tmpfs0000;/tmp/
|
|
||||||
@@ -1,273 +0,0 @@
|
|||||||
--:Minify:--
|
|
||||||
local args = {...}
|
|
||||||
local apis = args[1]
|
|
||||||
local disks = args[2]
|
|
||||||
local arch = args[3]
|
|
||||||
local screen = args[5]
|
|
||||||
local computer = args[6]
|
|
||||||
local ifs = args[7]
|
|
||||||
local kernel = {}
|
|
||||||
kernel.LOG_Text=""
|
|
||||||
kernel.version="HyperionOS V1.0.0"
|
|
||||||
kernel.process = "Kernel"
|
|
||||||
kernel.users={[0]="root",[1]="User"}
|
|
||||||
kernel.hostname = "hyperion"
|
|
||||||
kernel.groups = {}
|
|
||||||
kernel.uid = 0
|
|
||||||
kernel.gid = 0
|
|
||||||
kernel.status = "start"
|
|
||||||
kernel.key = {}
|
|
||||||
kernel.cache = {}
|
|
||||||
kernel.cache.preload = {}
|
|
||||||
kernel._G=_G
|
|
||||||
kernel.sleep=sleep
|
|
||||||
|
|
||||||
_G.sleep=nil
|
|
||||||
local windowsExp = false
|
|
||||||
|
|
||||||
function kernel.log(msg, level, c)
|
|
||||||
c=c or 12
|
|
||||||
kernel.LOG_Text = kernel.LOG_Text..tostring(computer:time()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg.."\n"
|
|
||||||
if kernel.status == "start" then
|
|
||||||
screen:setTextColor(c)
|
|
||||||
screen:print(string.format("%X",c-1).." "..tostring(computer:time()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg)
|
|
||||||
elseif kernel.status == "term" then
|
|
||||||
kernel.standbyTask=kernel.currentTask
|
|
||||||
kernel.currentTask=kernel.kernelTask
|
|
||||||
kernel.vfs.devctl(1,"sfgc",c)
|
|
||||||
kernel.vfs.write(1,string.format("%X",c-1).." "..tostring(computer:time()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg.."\n")
|
|
||||||
kernel.currentTask=kernel.standbyTask
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function kernel.PANIC(msg)
|
|
||||||
if kernel.status~="Panic" then
|
|
||||||
kernel.log("PANIC: "..msg, "PANIC")
|
|
||||||
pcall(kernel["saveLog"])
|
|
||||||
kernel.status="Panic"
|
|
||||||
kernel.reason=msg
|
|
||||||
screen:setTextColor(2)
|
|
||||||
screen:setBackgroundColor(16)
|
|
||||||
screen:clear()
|
|
||||||
screen:setCursorPos(1,1)
|
|
||||||
screen:print(kernel.LOG_Text)
|
|
||||||
screen:print("KERNEL PANIC!\n"..msg.."\nSystem halted.")
|
|
||||||
screen:print("Press any key to continue...")
|
|
||||||
kernel.exitMain = true
|
|
||||||
end
|
|
||||||
while true do
|
|
||||||
local event={computer:getMachineEvent()}
|
|
||||||
if event[1]=="keyPressed" then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
computer:reboot()
|
|
||||||
end
|
|
||||||
kernel.panic=kernel.PANIC
|
|
||||||
|
|
||||||
if windowsExp then
|
|
||||||
screen:setTextColor(1)
|
|
||||||
screen:setBackgroundColor(4)
|
|
||||||
screen:clear()
|
|
||||||
local w,h = screen:getSize()
|
|
||||||
screen:setCursorPos(3,5)
|
|
||||||
screen:print(":(")
|
|
||||||
screen:setCursorPos(3,7)
|
|
||||||
screen:print("Your PC ran into a problem and needs to restart. We're just collecting some error")
|
|
||||||
screen:setCursorPos(3,8)
|
|
||||||
screen:print("info, and then we'll restart for you.\n")
|
|
||||||
screen:setCursorPos(3,h-5)
|
|
||||||
screen:print("Stop code: average windows experience")
|
|
||||||
screen:setCursorPos(1,h)
|
|
||||||
screen:print("Press any key to continue... jk reboot it yourself lazy")
|
|
||||||
while true do end
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.log("Kernel loaded.")
|
|
||||||
kernel.log("Mounting init disks...")
|
|
||||||
disks.refresh()
|
|
||||||
ifs.update(disks)
|
|
||||||
kernel.disks={}
|
|
||||||
for _,v in disks.list() do
|
|
||||||
kernel.disks[v.address] = v
|
|
||||||
end
|
|
||||||
ifs.mount("$", "/")
|
|
||||||
|
|
||||||
local fstab=ifs.readAllText("/boot/fstab")
|
|
||||||
local split = function(str, delim, maxResultCountOrNil)
|
|
||||||
assert(#delim == 1, "only delim len 1 supported for now")
|
|
||||||
maxResultCountOrNil = (maxResultCountOrNil or 0)-1
|
|
||||||
local rv = {}
|
|
||||||
local buf = ""
|
|
||||||
for i = 1, #str do
|
|
||||||
local c = string.sub(str,i,i)
|
|
||||||
if #rv ~= maxResultCountOrNil and c == delim then
|
|
||||||
table.insert(rv, buf)
|
|
||||||
buf = ""
|
|
||||||
else
|
|
||||||
buf = buf..c
|
|
||||||
end
|
|
||||||
end
|
|
||||||
table.insert(rv, buf)
|
|
||||||
return rv
|
|
||||||
end
|
|
||||||
|
|
||||||
if not ifs.isFile("/boot/boot.cfg") then
|
|
||||||
kernel.log("boot.cfg missing or corrupted!, Attempting to write recovery boot.cfg", "ERROR", 2)
|
|
||||||
ifs.writeAllText("/boot/boot.cfg",ifs.readAllText("/boot/safeboot.cfg"))
|
|
||||||
end
|
|
||||||
|
|
||||||
local initCfgFunc, err = load(ifs.readAllText("/boot/boot.cfg"), "@boot.cfg")
|
|
||||||
if not initCfgFunc then
|
|
||||||
kernel.PANIC("Failed to load /boot/boot.cfg: "..tostring(err))
|
|
||||||
end
|
|
||||||
|
|
||||||
---@diagnostic disable-next-line: param-type-mismatch
|
|
||||||
local initCfgStatus, config = pcall(initCfgFunc)
|
|
||||||
if not initCfgStatus then
|
|
||||||
kernel.PANIC("Error in /boot/boot.cfg: "..tostring(config))
|
|
||||||
end
|
|
||||||
kernel.config = config
|
|
||||||
|
|
||||||
for i,v in ipairs(split(fstab,"\n")) do
|
|
||||||
if v:sub(1,1)=="U" then
|
|
||||||
local id=""
|
|
||||||
for i=3,#v do
|
|
||||||
if v:sub(i,i)==";" then
|
|
||||||
if i==3 then kernel.log("Invalid fstab line... Skipping.","WARN") goto endline end
|
|
||||||
id=v:sub(3,i-1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local path=v:sub(#id+4)
|
|
||||||
ifs.mount(id,path)
|
|
||||||
::endline::
|
|
||||||
end
|
|
||||||
end
|
|
||||||
kernel.log("Disks initialized")
|
|
||||||
|
|
||||||
function kernel.saveLog()
|
|
||||||
ifs.writeAllText("/var/log/syslog.log", kernel.LOG_Text)
|
|
||||||
end
|
|
||||||
|
|
||||||
function kernel.newFifo()
|
|
||||||
local fifo = {}
|
|
||||||
fifo.push=function(data)
|
|
||||||
table.insert(fifo, data)
|
|
||||||
end
|
|
||||||
fifo.pop=function()
|
|
||||||
return table.remove(fifo,1)
|
|
||||||
end
|
|
||||||
return fifo
|
|
||||||
end
|
|
||||||
|
|
||||||
function kernel.newUUID()
|
|
||||||
local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
|
||||||
local uuid = ""
|
|
||||||
for i = 1, #template do
|
|
||||||
local c = template:sub(i,i)
|
|
||||||
if c == "x" then
|
|
||||||
uuid = uuid .. string.format("%x", math.random(0, 15))
|
|
||||||
elseif c == "y" then
|
|
||||||
uuid = uuid .. string.format("%x", math.random(8, 11))
|
|
||||||
else
|
|
||||||
uuid = uuid .. c
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return uuid
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.syscalls={}
|
|
||||||
local modules={[0]={}}
|
|
||||||
for i=0, 100 do
|
|
||||||
modules[i]={}
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.log("Gathering modules")
|
|
||||||
for _, i in ipairs(ifs.list("/lib/modules")) do
|
|
||||||
for _,v in ipairs(ifs.list("/lib/modules/"..i)) do
|
|
||||||
local prior=tonumber(v:sub(1,2))
|
|
||||||
modules[prior+1][#modules[prior+1]+1]="/lib/modules/"..i.."/"..v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.ifs=ifs
|
|
||||||
kernel.apis=apis
|
|
||||||
kernel.computer=computer
|
|
||||||
kernel.arch=arch
|
|
||||||
kernel.initdisks=disks
|
|
||||||
kernel.screen=screen
|
|
||||||
kernel.processes={}
|
|
||||||
kernel.fstab=fstab
|
|
||||||
|
|
||||||
kernel.kernelTask = {
|
|
||||||
name="kernel",
|
|
||||||
status="R",
|
|
||||||
pid=0,
|
|
||||||
tgid=0,
|
|
||||||
uid=0,
|
|
||||||
fd={},
|
|
||||||
exit="",
|
|
||||||
sleep=0,
|
|
||||||
ivs=0,
|
|
||||||
vs=0,
|
|
||||||
children={},
|
|
||||||
syscallReturn={},
|
|
||||||
cwd="/",
|
|
||||||
timeSlice=0,
|
|
||||||
lastTime=0,
|
|
||||||
totalTime=0,
|
|
||||||
numRuns=0
|
|
||||||
}
|
|
||||||
kernel.currentTask = kernel.kernelTask
|
|
||||||
|
|
||||||
function kernel.shutdown()
|
|
||||||
kernel.computer:shutdown()
|
|
||||||
end
|
|
||||||
|
|
||||||
function kernel.reboot()
|
|
||||||
kernel.computer:reboot()
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.syscalls["time"]=function() return kernel.computer:time() end
|
|
||||||
kernel.syscalls["log"]=kernel.log
|
|
||||||
kernel.syscalls["getUptime"]=function() return kernel.computer:clock() end
|
|
||||||
kernel.syscalls["getUsername"]=function(uid) return kernel.users[uid or kernel.uid] end
|
|
||||||
kernel.syscalls["getHostname"]=function() return kernel.hostname end
|
|
||||||
kernel.syscalls["getHost"]=function() return kernel.apis._HOST end
|
|
||||||
kernel.syscalls["version"]=function() return kernel.version end
|
|
||||||
kernel.syscalls["setHostname"]=function(name) if kernel.uid~=0 then error("Permission denied") end kernel.hostname=name end
|
|
||||||
kernel.syscalls["arch"]=function() return arch end
|
|
||||||
kernel.syscalls["sysdump"]=function()
|
|
||||||
local rv={}
|
|
||||||
for i,v in pairs(kernel.syscalls) do
|
|
||||||
rv[#rv+1] = i
|
|
||||||
end
|
|
||||||
return rv
|
|
||||||
end
|
|
||||||
kernel.syscalls["test"]=function() return true end
|
|
||||||
|
|
||||||
kernel.log("Running modules")
|
|
||||||
for _,p in ipairs(modules) do
|
|
||||||
for _,v in ipairs(p) do
|
|
||||||
if kernel.config.showModLoad then kernel.log("Loading module "..v, "DBUG", 5) end
|
|
||||||
local code=ifs.readAllText(v)
|
|
||||||
if not code then
|
|
||||||
kernel.log("ModuReadErr: "..v, "WARN", 8)
|
|
||||||
goto skip
|
|
||||||
end
|
|
||||||
local func,err=load(code,"@"..v)
|
|
||||||
if not func then kernel.panic("ModuLoadErr: "..tostring(err)) goto skip end
|
|
||||||
local status, err = xpcall(func,debug.traceback, kernel)
|
|
||||||
if not status then kernel.panic("ModuRunErr: "..tostring(err)) end
|
|
||||||
if kernel.config.showModLoad then kernel.log("Loaded module "..v, "DBUG", 5) end
|
|
||||||
::skip::
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
kernel.log("Kernel initialized successfully.")
|
|
||||||
kernel.status="running"
|
|
||||||
kernel.main()
|
|
||||||
if kernel.status=="panic" then
|
|
||||||
kernel.panic()
|
|
||||||
end
|
|
||||||
kernel.PANIC("Execution complete")
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
U $;/boot/
|
||||||
|
V devfs0000;/dev/
|
||||||
|
V tmpfs0000;/tmp/
|
||||||
|
V procfs0000;/proc/
|
||||||
@@ -77,7 +77,7 @@ end
|
|||||||
function fs.load(path) return load(fs.readAllText(path), path) end
|
function fs.load(path) return load(fs.readAllText(path), path) end
|
||||||
|
|
||||||
function fs.mount(disk, mountPoint)
|
function fs.mount(disk, mountPoint)
|
||||||
if not disks[disk] then return end
|
if not disks[disk] then error("NODISK : "..disk) end
|
||||||
mounts[mountPoint] = disk
|
mounts[mountPoint] = disk
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -0,0 +1,511 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local EFI=...
|
||||||
|
--pf
|
||||||
|
EFI.beep(440, 500)
|
||||||
|
local screen=EFI.screenCtl
|
||||||
|
local ifs=EFI.initfs
|
||||||
|
local disks=EFI.disks
|
||||||
|
local arch=EFI.architecture
|
||||||
|
local kernel = {}
|
||||||
|
kernel.LOG_Text=""
|
||||||
|
kernel.version="HyperionOS V1.2.4"
|
||||||
|
kernel.tag="1.2.4"
|
||||||
|
kernel.process = "Kernel"
|
||||||
|
kernel.users={[0]="root",[1000]="User"}
|
||||||
|
kernel.hostname = "hyperion"
|
||||||
|
kernel.groups = {}
|
||||||
|
kernel.uid = 0
|
||||||
|
kernel.gid = 0
|
||||||
|
kernel.status = "start"
|
||||||
|
kernel.key = {}
|
||||||
|
kernel.cache = {}
|
||||||
|
kernel.cache.preload = {}
|
||||||
|
kernel.unixSockets={}
|
||||||
|
kernel._G=_G
|
||||||
|
kernel.sleep=sleep
|
||||||
|
|
||||||
|
_G.sleep=nil
|
||||||
|
local windowsExp = false
|
||||||
|
|
||||||
|
function kernel.log(msg, level, c, nopanic)
|
||||||
|
local f=function()
|
||||||
|
c=c or 0x6D6D6D
|
||||||
|
kernel.LOG_Text = kernel.LOG_Text..tostring(EFI:date()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg.."\n"
|
||||||
|
if kernel.status == "start" then
|
||||||
|
screen:setTextColor(c)
|
||||||
|
screen:print(tostring(EFI:date()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg)
|
||||||
|
elseif kernel.status == "term" then
|
||||||
|
kernel.standbyTask=kernel.currentTask
|
||||||
|
kernel.currentTask=kernel.kernelTask
|
||||||
|
local file=kernel.vfs.open("/dev/console", "w")
|
||||||
|
kernel.vfs.devctl(file,"sfgc",c)
|
||||||
|
kernel.vfs.write(file,tostring(EFI:date()).." "..kernel.users[kernel.uid].." "..kernel.process.."["..tostring(level or "INFO").."]: "..msg.."\n")
|
||||||
|
kernel.vfs.close(file)
|
||||||
|
kernel.currentTask=kernel.standbyTask
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok,err = xpcall(f,debug.traceback)
|
||||||
|
if not ok and not nopanic then
|
||||||
|
kernel.panic(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.PANIC(msg)
|
||||||
|
if kernel.status~="Panic" then
|
||||||
|
kernel.log("PANIC: "..msg, "PANIC", 0xFF0000)
|
||||||
|
pcall(kernel["saveLog"])
|
||||||
|
kernel.status="Panic"
|
||||||
|
kernel.reason=msg
|
||||||
|
screen:enable()
|
||||||
|
screen:setTextColor(0xFF0000)
|
||||||
|
screen:setBackgroundColor(0x000000)
|
||||||
|
screen:clear()
|
||||||
|
screen:resetCursor()
|
||||||
|
screen:print(kernel.LOG_Text)
|
||||||
|
screen:print("KERNEL PANIC!\n"..msg.."\nSystem halted.")
|
||||||
|
screen:print("Press any key to continue...")
|
||||||
|
kernel.exitMain = true
|
||||||
|
end
|
||||||
|
while true do
|
||||||
|
EFI:yield()
|
||||||
|
local event={EFI:getMachineEvent()}
|
||||||
|
if event[1]=="keyPressed" then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
EFI.reboot=true
|
||||||
|
error("KERNEL PANIC")
|
||||||
|
end
|
||||||
|
kernel.panic=kernel.PANIC
|
||||||
|
|
||||||
|
if windowsExp then
|
||||||
|
screen:setTextColor(0xFFFFFF)
|
||||||
|
screen:setBackgroundColor(0x0000FF)
|
||||||
|
screen:clear()
|
||||||
|
screen:resetCursor()
|
||||||
|
screen:print("\n\n\n\n\n :(")
|
||||||
|
screen:print("\n Your PC ran into a problem and needs to restart. We're just collecting some error")
|
||||||
|
screen:print(" info, and then we'll restart for you.\n")
|
||||||
|
screen:print("\n\n\n Stop code: average windows experience")
|
||||||
|
while true do end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("Kernel loaded.")
|
||||||
|
kernel.log("Mounting init disks...")
|
||||||
|
disks.refresh()
|
||||||
|
ifs.update(disks)
|
||||||
|
kernel.disks={}
|
||||||
|
for _,v in disks.list() do
|
||||||
|
kernel.disks[v.address] = v
|
||||||
|
end
|
||||||
|
ifs.mount("$", "/boot/")
|
||||||
|
|
||||||
|
local fstab=ifs.readAllText("/boot/fstab")
|
||||||
|
local split = function(str, delim, maxResultCountOrNil)
|
||||||
|
assert(#delim == 1, "only delim len 1 supported for now")
|
||||||
|
maxResultCountOrNil = (maxResultCountOrNil or 0)-1
|
||||||
|
local rv = {}
|
||||||
|
local buf = ""
|
||||||
|
for i = 1, #str do
|
||||||
|
local c = string.sub(str,i,i)
|
||||||
|
if #rv ~= maxResultCountOrNil and c == delim then
|
||||||
|
table.insert(rv, buf)
|
||||||
|
buf = ""
|
||||||
|
else
|
||||||
|
buf = buf..c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.insert(rv, buf)
|
||||||
|
return rv
|
||||||
|
end
|
||||||
|
|
||||||
|
if not ifs.isFile("/boot/boot.cfg") then
|
||||||
|
kernel.log("First boot detected writing boot.cfg", "INFO", 0x00FF00)
|
||||||
|
ifs.writeAllText("/boot/boot.cfg",ifs.readAllText("/boot/safeboot.cfg"))
|
||||||
|
kernel.firstBoot=true
|
||||||
|
end
|
||||||
|
|
||||||
|
local initCfgFunc, err = load(ifs.readAllText("/boot/boot.cfg"), "@boot.cfg")
|
||||||
|
if not initCfgFunc then
|
||||||
|
kernel.PANIC("Failed to load /boot/boot.cfg: "..tostring(err))
|
||||||
|
end
|
||||||
|
|
||||||
|
---@diagnostic disable-next-line: param-type-mismatch
|
||||||
|
local initCfgStatus, config = pcall(initCfgFunc)
|
||||||
|
if not initCfgStatus then
|
||||||
|
kernel.PANIC("Error in /boot/boot.cfg: "..tostring(config))
|
||||||
|
end
|
||||||
|
kernel.config = config
|
||||||
|
|
||||||
|
local skip=false
|
||||||
|
local mounted={}
|
||||||
|
local root=false
|
||||||
|
for i,v in ipairs(split(fstab,"\n")) do
|
||||||
|
if v:sub(1,1)=="U" or v:sub(1,1)=="F" then
|
||||||
|
local id=""
|
||||||
|
for i=3,#v do
|
||||||
|
if v:sub(i,i)==";" then
|
||||||
|
if i==3 then kernel.log("Invalid fstab line... Skipping.","WARN", 0xFF8800) skip = true break end
|
||||||
|
id=v:sub(3,i-1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not skip then
|
||||||
|
local path=v:sub(#id+4)
|
||||||
|
if path=="/" then root=true end
|
||||||
|
mounted[id]=true
|
||||||
|
ifs.mount(id,path)
|
||||||
|
else
|
||||||
|
skip=false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not root then
|
||||||
|
kernel.log("No rootdisk found, prompting user...", "WARN", 0xFF8800)
|
||||||
|
screen:setTextColor(0xDDDDDD)
|
||||||
|
screen:print("Disk to mount to /:")
|
||||||
|
local idx=1
|
||||||
|
local canadates={}
|
||||||
|
for i,v in pairs(kernel.disks) do
|
||||||
|
if not mounted[i] then
|
||||||
|
screen:print("("..idx..") : "..i)
|
||||||
|
canadates[idx]=i
|
||||||
|
idx=idx+1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
while true do
|
||||||
|
EFI:yield()
|
||||||
|
local event={EFI:getMachineEvent()}
|
||||||
|
if event[1]=="keyTyped" then
|
||||||
|
if tonumber(event[3]) then
|
||||||
|
if canadates[tonumber(event[3])] then
|
||||||
|
ifs.writeAllText("/boot/fstab", "U "..canadates[tonumber(event[3])]..";/\n"..fstab)
|
||||||
|
fstab="U "..canadates[tonumber(event[3])]..";/\n"..fstab
|
||||||
|
ifs.mount(canadates[tonumber(event[3])], "/")
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
screen:print(event[1], event[3])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.log("Disks initialized")
|
||||||
|
|
||||||
|
function kernel.saveLog()
|
||||||
|
if kernel.status=="running" then
|
||||||
|
local file = kernel.vfs.open("/var/log/syslog.log", "w")
|
||||||
|
kernel.vfs.write(file, kernel.LOG_Text)
|
||||||
|
kernel.vfs.close(file)
|
||||||
|
else
|
||||||
|
ifs.writeAllText("/var/log/syslog.log", kernel.LOG_Text)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.newFifo()
|
||||||
|
local fifo = {}
|
||||||
|
fifo.push=function(data)
|
||||||
|
table.insert(fifo, data)
|
||||||
|
end
|
||||||
|
fifo.pop=function()
|
||||||
|
return table.remove(fifo,1)
|
||||||
|
end
|
||||||
|
return fifo
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.newUUID()
|
||||||
|
local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
|
||||||
|
local uuid = ""
|
||||||
|
for i = 1, #template do
|
||||||
|
local c = template:sub(i,i)
|
||||||
|
if c == "x" then
|
||||||
|
uuid = uuid .. string.format("%x", math.random(0, 15))
|
||||||
|
elseif c == "y" then
|
||||||
|
uuid = uuid .. string.format("%x", math.random(8, 11))
|
||||||
|
else
|
||||||
|
uuid = uuid .. c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return uuid
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.sfile(str, writeable)
|
||||||
|
local buf = {str or ""}
|
||||||
|
local pos = 1
|
||||||
|
local closed = false
|
||||||
|
|
||||||
|
local function content()
|
||||||
|
return table.concat(buf)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function set_content(s)
|
||||||
|
buf = {s}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function flush()
|
||||||
|
str=content()
|
||||||
|
end
|
||||||
|
|
||||||
|
local file = {}
|
||||||
|
|
||||||
|
function file.read(n)
|
||||||
|
assert(not closed, "file is closed")
|
||||||
|
|
||||||
|
local s = content()
|
||||||
|
local len = #s
|
||||||
|
|
||||||
|
if not n then
|
||||||
|
local out = s:sub(pos)
|
||||||
|
pos = len + 1
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
local out = s:sub(pos, pos + n - 1)
|
||||||
|
pos = pos + #out
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
if writeable then
|
||||||
|
function file.write(data)
|
||||||
|
assert(not closed, "file is closed")
|
||||||
|
|
||||||
|
local s = content()
|
||||||
|
local before = s:sub(1, pos - 1)
|
||||||
|
local after = s:sub(pos + #data)
|
||||||
|
|
||||||
|
set_content(before .. data .. after)
|
||||||
|
pos = pos + #data
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function file.seek(whence, offset)
|
||||||
|
assert(not closed, "file is closed")
|
||||||
|
|
||||||
|
local s = content()
|
||||||
|
local len = #s
|
||||||
|
|
||||||
|
whence = whence or "cur"
|
||||||
|
offset = offset or 0
|
||||||
|
|
||||||
|
if whence == "set" then
|
||||||
|
pos = offset + 1
|
||||||
|
elseif whence == "cur" then
|
||||||
|
pos = pos + offset
|
||||||
|
elseif whence == "end" then
|
||||||
|
pos = len + offset + 1
|
||||||
|
else
|
||||||
|
error("invalid whence")
|
||||||
|
end
|
||||||
|
|
||||||
|
if pos < 1 then pos = 1 end
|
||||||
|
if pos > len + 1 then pos = len + 1 end
|
||||||
|
|
||||||
|
return pos - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
function file.close()
|
||||||
|
assert(not closed, "file is closed")
|
||||||
|
flush()
|
||||||
|
closed = true
|
||||||
|
end
|
||||||
|
|
||||||
|
function file.flush()
|
||||||
|
assert(not closed, "file is closed")
|
||||||
|
flush()
|
||||||
|
end
|
||||||
|
|
||||||
|
function file.content()
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
|
||||||
|
return file
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls={}
|
||||||
|
local modules={[0]={}}
|
||||||
|
for i=0, 100 do
|
||||||
|
modules[i]={}
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("Gathering modules")
|
||||||
|
for _, i in ipairs(ifs.list("/lib/modules")) do
|
||||||
|
local modlist = ifs.list("/lib/modules/"..i)
|
||||||
|
if not modlist then
|
||||||
|
kernel.log("WARNING: could not list /lib/modules/"..i.." (skipping)", "WARN", 0xFF8800)
|
||||||
|
else
|
||||||
|
for _,v in ipairs(modlist) do
|
||||||
|
local prior=tonumber(v:sub(1,2))
|
||||||
|
if prior then
|
||||||
|
modules[prior+1][#modules[prior+1]+1]=i.."/"..v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.ifs=ifs
|
||||||
|
--pf
|
||||||
|
kernel.apis=EFI.firmware
|
||||||
|
--pf
|
||||||
|
kernel.EFI=EFI
|
||||||
|
kernel.arch=arch
|
||||||
|
kernel.initdisks=disks
|
||||||
|
kernel.screen=screen
|
||||||
|
kernel.processes={}
|
||||||
|
kernel.fstab=fstab
|
||||||
|
kernel.denied={}
|
||||||
|
kernel.loadingModule="kernel"
|
||||||
|
kernel.perTaskHooks={}
|
||||||
|
kernel.mainHooks={}
|
||||||
|
kernel.runhooks={}
|
||||||
|
|
||||||
|
-- args {taskobj}
|
||||||
|
function kernel.execPerTask(func, prior)
|
||||||
|
if not kernel.perTaskHooks[prior] then kernel.perTaskHooks[prior]={} end
|
||||||
|
kernel.perTaskHooks[prior][#kernel.perTaskHooks[prior]+1] = func
|
||||||
|
return #kernel.perTaskHooks[prior]
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.execOnMain(func)
|
||||||
|
kernel.mainHooks[#kernel.mainHooks+1] = func
|
||||||
|
return #kernel.mainHooks
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.runWhenLoaded(func)
|
||||||
|
kernel.runhooks[#kernel.runhooks+1] = func
|
||||||
|
return #kernel.runhooks
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.kernelTask = {
|
||||||
|
name="kernel",
|
||||||
|
status="R",
|
||||||
|
pid=0,
|
||||||
|
tgid=0,
|
||||||
|
uid=0,
|
||||||
|
fd={},
|
||||||
|
exit="",
|
||||||
|
sleep=0,
|
||||||
|
ivs=0,
|
||||||
|
vs=0,
|
||||||
|
children={},
|
||||||
|
syscallReturn={},
|
||||||
|
cwd="/",
|
||||||
|
timeSlice=0,
|
||||||
|
lastTime=0,
|
||||||
|
totalTime=0,
|
||||||
|
numRuns=0
|
||||||
|
}
|
||||||
|
kernel.currentTask = kernel.kernelTask
|
||||||
|
|
||||||
|
function kernel.shutdown()
|
||||||
|
kernel.exitMain=true
|
||||||
|
kernel.status="shutdown"
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.reboot()
|
||||||
|
kernel.exitMain=true
|
||||||
|
kernel.status="reboot"
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.halt()
|
||||||
|
kernel.exitMain=true
|
||||||
|
kernel.status="halt"
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.asyncReturn(...)
|
||||||
|
kernel.currentTask.syscallReturn = {...}
|
||||||
|
end
|
||||||
|
|
||||||
|
function kernel.deny(moduleid)
|
||||||
|
kernel.denied[moduleid]=true
|
||||||
|
if kernel.config.showModLoad then kernel.log("Module "..kernel.loadingModule.." denied "..moduleid, "DBUG", 0xFF0000) end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["time"]=function() return kernel.EFI:getEpochMs() end
|
||||||
|
kernel.syscalls["date"]=function() return kernel.EFI:date() end
|
||||||
|
kernel.syscalls["log"]=kernel.log
|
||||||
|
kernel.syscalls["tag"]=function() return kernel.tag end
|
||||||
|
kernel.syscalls["getUptime"]=function() return kernel.EFI:getUptime() end
|
||||||
|
kernel.syscalls["getUsername"]=function(uid) return kernel.users[uid or kernel.uid] end
|
||||||
|
kernel.syscalls["getHostname"]=function() return kernel.hostname end
|
||||||
|
kernel.syscalls["getHost"]=function() return kernel.apis._HOST end
|
||||||
|
kernel.syscalls["version"]=function() return kernel.version end
|
||||||
|
kernel.syscalls["setHostname"]=function(name) if kernel.uid~=0 then error("Permission denied") end kernel.hostname=name end
|
||||||
|
kernel.syscalls["arch"]=function() return arch end
|
||||||
|
kernel.syscalls["sysdump"]=function()
|
||||||
|
local rv={}
|
||||||
|
for i,v in pairs(kernel.syscalls) do
|
||||||
|
rv[#rv+1] = i
|
||||||
|
end
|
||||||
|
return rv
|
||||||
|
end
|
||||||
|
kernel.syscalls["reboot"]=function()
|
||||||
|
if kernel.uid==0 or kernel.groups.wheel then
|
||||||
|
kernel.reboot()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.syscalls["shutdown"]=function()
|
||||||
|
if kernel.uid==0 or kernel.groups.wheel then
|
||||||
|
kernel.shutdown()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.syscalls["halt"]=function()
|
||||||
|
if kernel.uid==0 or kernel.groups.wheel then
|
||||||
|
kernel.halt()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
kernel.syscalls["saveLog"]=function()
|
||||||
|
if kernel.uid==0 or kernel.groups.wheel then
|
||||||
|
kernel.saveLog()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.saveLog()
|
||||||
|
kernel.log("Running modules")
|
||||||
|
for _,p in ipairs(modules) do
|
||||||
|
for _,v in ipairs(p) do
|
||||||
|
kernel.loadingModule=v
|
||||||
|
if not kernel.denied[v] then
|
||||||
|
if kernel.config.showModLoad then kernel.log("Loading module "..v, "DBUG", 0x00FFFF) end
|
||||||
|
local code=ifs.readAllText("/lib/modules/"..v)
|
||||||
|
if not code then
|
||||||
|
kernel.panic("Failed to read module "..v)
|
||||||
|
end
|
||||||
|
local func,err=load(code,"@"..v)
|
||||||
|
if not func then kernel.panic("ModuLoadErr: "..tostring(err)) end
|
||||||
|
local status, err = xpcall(func,debug.traceback, kernel)
|
||||||
|
if not status then kernel.panic("ModuRunErr: "..tostring(err)) end
|
||||||
|
if kernel.config.showModLoad then kernel.log("Loaded module "..v, "DBUG", 0x00FFFF) end
|
||||||
|
if kernel.config.moreLogsaves then kernel.saveLog() end
|
||||||
|
else
|
||||||
|
if kernel.config.showModLoad then kernel.log("Denied module "..v, "DBUG", 0xFF0000) end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i=1, #kernel.runhooks do
|
||||||
|
local ok,err = xpcall(kernel.runhooks[i], debug.traceback)
|
||||||
|
if not ok then kernel.panic(err) end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("Kernel initialized successfully.")
|
||||||
|
kernel.saveLog()
|
||||||
|
kernel.status="running"
|
||||||
|
kernel.loadingModule=nil
|
||||||
|
screen:disable()
|
||||||
|
if not kernel.main then kernel.panic("No scheduler implemented from firmware") end
|
||||||
|
local ok,err = xpcall(kernel.main, debug.traceback)
|
||||||
|
if not ok then
|
||||||
|
kernel.panic(err)
|
||||||
|
end
|
||||||
|
if kernel.status=="panic" then
|
||||||
|
kernel.panic(kernel.reason)
|
||||||
|
end
|
||||||
|
if kernel.status=="reboot" then
|
||||||
|
EFI.reboot=true
|
||||||
|
return true
|
||||||
|
elseif kernel.status=="halt" then
|
||||||
|
kernel.log("System halted.")
|
||||||
|
kernel.saveLog()
|
||||||
|
while true do end
|
||||||
|
end
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
-- This file is auto-generated during the build process.
|
-- This file is auto-generated during the build process.
|
||||||
-- DEFAULT BOOT CONFIGURATION FILE
|
-- DEFAULT BOOT CONFIGURATION FILE
|
||||||
return {
|
return {
|
||||||
initPath = "/sbin/init.lua",
|
initPath = "/sbin/init",
|
||||||
maxOpenFiles = 128,
|
maxOpenFiles = 128,
|
||||||
maxFilesPerTask = 16,
|
maxFilesPerTask = 16,
|
||||||
preempt=true
|
preempt = true,
|
||||||
}
|
}
|
||||||
+68
-18
@@ -1,4 +1,8 @@
|
|||||||
-- :Minify:--
|
--:Minify:--
|
||||||
|
--- @diagnostic disable: duplicate-set-field
|
||||||
|
local kernel = ...
|
||||||
|
kernel.allowGlobalOverwrites = true
|
||||||
|
|
||||||
function string.hasSuffix(str, suffix)
|
function string.hasSuffix(str, suffix)
|
||||||
return string.sub(str, #suffix + 1) == suffix
|
return string.sub(str, #suffix + 1) == suffix
|
||||||
end
|
end
|
||||||
@@ -19,6 +23,7 @@ function string.split(str, delim, maxResultCountOrNil)
|
|||||||
assert(#delim == 1, "only delim len 1 supported for now")
|
assert(#delim == 1, "only delim len 1 supported for now")
|
||||||
if not str then return false end
|
if not str then return false end
|
||||||
maxResultCountOrNil = (maxResultCountOrNil or 0) - 1
|
maxResultCountOrNil = (maxResultCountOrNil or 0) - 1
|
||||||
|
if str == "" then return {} end
|
||||||
local rv = {}
|
local rv = {}
|
||||||
local buf = ""
|
local buf = ""
|
||||||
for i = 1, #str do
|
for i = 1, #str do
|
||||||
@@ -68,30 +73,24 @@ end
|
|||||||
local function serialize(tbl, seen)
|
local function serialize(tbl, seen)
|
||||||
seen = seen or {}
|
seen = seen or {}
|
||||||
|
|
||||||
-- If we've seen this table before, return a placeholder to prevent infinite loops
|
|
||||||
if seen[tbl] then return '"[Circular Reference]"' end
|
if seen[tbl] then return '"[Circular Reference]"' end
|
||||||
|
|
||||||
-- Mark this table as seen
|
|
||||||
seen[tbl] = true
|
seen[tbl] = true
|
||||||
|
|
||||||
local output = "{"
|
local output = "{"
|
||||||
local first = true
|
local first = true
|
||||||
|
|
||||||
for i, v in pairs(tbl) do
|
for i, v in pairs(tbl) do
|
||||||
-- Handle comma placement more cleanly
|
|
||||||
if not first then output = output .. "," end
|
if not first then output = output .. "," end
|
||||||
first = false
|
first = false
|
||||||
|
|
||||||
-- Serialize Key
|
|
||||||
if type(i) == "string" then
|
if type(i) == "string" then
|
||||||
output = output .. "[\"" .. i .. "\"]="
|
output = output .. "[\"" .. i .. "\"]="
|
||||||
elseif type(i) == "number" then
|
elseif type(i) == "number" then
|
||||||
output = output .. "[" .. tostring(i) .. "]="
|
output = output .. "[" .. tostring(i) .. "]="
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Serialize Value
|
|
||||||
if type(v) == "table" then
|
if type(v) == "table" then
|
||||||
-- Pass the 'seen' table down to the recursive call
|
|
||||||
output = output .. serialize(v, seen)
|
output = output .. serialize(v, seen)
|
||||||
elseif type(v) == "string" then
|
elseif type(v) == "string" then
|
||||||
output = output .. "[=[" .. v .. "]=]"
|
output = output .. "[=[" .. v .. "]=]"
|
||||||
@@ -175,6 +174,27 @@ function table.indexOf(t, value)
|
|||||||
return -1
|
return -1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function table.merge(...)
|
||||||
|
local args={...}
|
||||||
|
local out = {}
|
||||||
|
local outi = {}
|
||||||
|
|
||||||
|
for _,t in ipairs(args) do
|
||||||
|
for i,v in pairs(t) do
|
||||||
|
out[i]=v
|
||||||
|
end
|
||||||
|
for i,v in ipairs(t) do
|
||||||
|
outi[#outi+1]=v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for i,v in ipairs(outi) do
|
||||||
|
out[i]=v
|
||||||
|
end
|
||||||
|
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
function string.replace(s, target, repl)
|
function string.replace(s, target, repl)
|
||||||
local result = {}
|
local result = {}
|
||||||
local i = 1
|
local i = 1
|
||||||
@@ -210,17 +230,47 @@ function toHex(num)
|
|||||||
return string.format("%X", num)
|
return string.format("%X", num)
|
||||||
end
|
end
|
||||||
|
|
||||||
syscall = setmetatable({}, {
|
local st={
|
||||||
__index = function(self, name)
|
push=function(self,...) table.insert(self.contents,{...}) end,
|
||||||
return function(...)
|
pop=function(self) return table.unpack(table.remove(self.contents)) end,
|
||||||
local res = table.pack(coroutine.yield("syscall", name, ...))
|
}
|
||||||
if res[1] then
|
local q={
|
||||||
return table.unpack(res, 2, res.n)
|
push=function(self,...) table.insert(self.contents,{...}) end,
|
||||||
else
|
pop=function(self) return table.unpack(table.remove(self.contents,1)) end,
|
||||||
error(res[2], 2)
|
}
|
||||||
|
|
||||||
|
function stack()
|
||||||
|
return setmetatable({contents={}}, {__index=st})
|
||||||
|
end
|
||||||
|
|
||||||
|
function queue()
|
||||||
|
return setmetatable({contents={}}, {__index=q})
|
||||||
|
end
|
||||||
|
|
||||||
|
local function makeSyscallProxy()
|
||||||
|
local backing = {}
|
||||||
|
return setmetatable(backing, {
|
||||||
|
__index = function(self, name)
|
||||||
|
local raw = rawget(self, name)
|
||||||
|
if raw ~= nil then return raw end
|
||||||
|
return function(...)
|
||||||
|
local res = table.pack(coroutine.yield("syscall", name, ...))
|
||||||
|
if res[1] then
|
||||||
|
return table.unpack(res, 2, res.n)
|
||||||
|
else
|
||||||
|
error(res[2], 2)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
end
|
__newindex = function(self, k, v)
|
||||||
})
|
rawset(self, k, v)
|
||||||
|
end,
|
||||||
|
__metatable=false
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
|
syscall = makeSyscallProxy()
|
||||||
|
|
||||||
|
_makeSyscallProxy = makeSyscallProxy
|
||||||
|
|
||||||
table.serialize = serialize
|
table.serialize = serialize
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,47 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
local cache = {}
|
||||||
|
kernel.searchpaths = {
|
||||||
|
"?", "?.lua", "/lib/?", "/lib/?.lua", "/usr/lib/?", "/usr/lib/?.lua",
|
||||||
|
"/usr/local/lib/?", "/usr/local/lib/?.lua"
|
||||||
|
}
|
||||||
|
kernel.reqcache = cache
|
||||||
|
|
||||||
|
local function require(module, ...)
|
||||||
|
if cache[module] then return cache[module].ret end
|
||||||
|
local args = {...}
|
||||||
|
for _, path in ipairs(kernel.searchpaths) do
|
||||||
|
local filepath = path:gsub("?", module)
|
||||||
|
if kernel.vfs.exists(filepath) then
|
||||||
|
if kernel.vfs.type(filepath) == "directory" then
|
||||||
|
filepath = filepath .. "/init.lua"
|
||||||
|
if kernel.vfs.type(filepath) == "directory" then
|
||||||
|
error("Module not found: "..module)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local fd = kernel.vfs.open(filepath, "r")
|
||||||
|
local chunks = {}
|
||||||
|
while true do
|
||||||
|
local chunk = kernel.vfs.read(fd, 4096)
|
||||||
|
if not chunk or chunk == "" then break end
|
||||||
|
chunks[#chunks + 1] = chunk
|
||||||
|
end
|
||||||
|
kernel.vfs.close(fd)
|
||||||
|
local data = table.concat(chunks)
|
||||||
|
local func, err = load(data, "@"..module, "t", kernel._U)
|
||||||
|
if not func then
|
||||||
|
error("Error loading module "..module..": "..tostring(err))
|
||||||
|
end
|
||||||
|
local ok, ret = xpcall(func, debug.traceback, table.unpack(args))
|
||||||
|
if not ok then
|
||||||
|
error("Error running module "..module..": "..tostring(ret))
|
||||||
|
end
|
||||||
|
cache[module] = {ret = ret, expires = kernel.EFI:getEpochMs() + 120000}
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
coroutine.yield()
|
||||||
|
end
|
||||||
|
error("Module not found: "..module)
|
||||||
|
end
|
||||||
|
|
||||||
|
_G.require = function(...) return require(...) end
|
||||||
@@ -0,0 +1,309 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
|
||||||
|
local proxy = {}
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
proxy.address = "devfs0000"
|
||||||
|
proxy.isvirt = true
|
||||||
|
proxy.isReadOnly = function() return false end
|
||||||
|
proxy.spaceUsed = function() return 0 end
|
||||||
|
proxy.spaceTotal = function() return 0 end
|
||||||
|
proxy.makeDirectory = function() error("EACCES") end
|
||||||
|
proxy.remove = function() error("EACCES") end
|
||||||
|
proxy.setLabel = function() error("EACCES") end
|
||||||
|
proxy.getLabel = function() return "devfs" end
|
||||||
|
proxy.attributes = function(path) return {
|
||||||
|
size = 0,
|
||||||
|
modified = 0,
|
||||||
|
created = 0
|
||||||
|
} end
|
||||||
|
|
||||||
|
function proxy:open(path, mode)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("open", mode)
|
||||||
|
end
|
||||||
|
error("ENFILE")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:type(path, mode)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if #steps == 0 then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("type", mode)
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
error("ENOENT")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:list(path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if #steps == 0 then
|
||||||
|
return table.keys(data)
|
||||||
|
end
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENOENT") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return table.keys(step[steps[#steps]])
|
||||||
|
end
|
||||||
|
error("ENOENT")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:fileExists(path)
|
||||||
|
local ok = pcall(function()
|
||||||
|
return self:type(path)
|
||||||
|
end)
|
||||||
|
return ok
|
||||||
|
end
|
||||||
|
|
||||||
|
function data.random(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if mode=="r" then
|
||||||
|
return {
|
||||||
|
read=function(amount)
|
||||||
|
local str = ""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
str=str..string.char(math.random(0, 255))
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="w" or mode=="a" then
|
||||||
|
return {
|
||||||
|
write=function() end
|
||||||
|
}
|
||||||
|
else error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function data.null(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if mode=="r" then
|
||||||
|
return {
|
||||||
|
read=function(amount) end
|
||||||
|
}
|
||||||
|
elseif mode=="w" or mode=="a" then
|
||||||
|
return {
|
||||||
|
write=function() end
|
||||||
|
}
|
||||||
|
else error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function data.zero(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if mode=="r" then
|
||||||
|
return {
|
||||||
|
read=function(amount)
|
||||||
|
local str = ""
|
||||||
|
for i=1, amount or 1 do
|
||||||
|
str=str..string.char(0)
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="w" or mode=="a" then
|
||||||
|
return {
|
||||||
|
write=function() end
|
||||||
|
}
|
||||||
|
else error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function buildMeta(entries, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
local uid = opts.uid or 0
|
||||||
|
local gid = opts.gid or 0
|
||||||
|
local perms = opts.perms or 0x3F -- default read/write for owner/group/world
|
||||||
|
|
||||||
|
local chunks = {}
|
||||||
|
table.insert(chunks, string.char(0x02)) -- version header
|
||||||
|
|
||||||
|
for path, target in pairs(entries) do
|
||||||
|
local name = path
|
||||||
|
local nameLen = #name
|
||||||
|
if nameLen > 255 then
|
||||||
|
error("Filename too long (>255 bytes): "..name)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Determine entry type: 0x01 = symlink if target ~= nil and target ~= ""
|
||||||
|
local entryType = 0x00
|
||||||
|
local cmeta = ""
|
||||||
|
if target and target ~= "" then
|
||||||
|
entryType = 0x01
|
||||||
|
cmeta = target
|
||||||
|
end
|
||||||
|
local cmetaLen = #cmeta
|
||||||
|
if cmetaLen > 255 then
|
||||||
|
error("cmeta too long (>255 bytes) for "..name)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Build entry as bytes
|
||||||
|
table.insert(chunks, string.char(nameLen)) -- name length
|
||||||
|
table.insert(chunks, name) -- name
|
||||||
|
table.insert(chunks, string.char(entryType)) -- entry type
|
||||||
|
table.insert(chunks, string.char(uid % 256, math.floor(uid/256) % 256)) -- uid
|
||||||
|
table.insert(chunks, string.char(gid % 256, math.floor(gid/256) % 256)) -- gid
|
||||||
|
table.insert(chunks, string.char(perms % 256, math.floor(perms/256) % 256)) -- perms
|
||||||
|
table.insert(chunks, string.char(cmetaLen)) -- cmeta length
|
||||||
|
if cmetaLen > 0 then
|
||||||
|
table.insert(chunks, cmeta)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return table.concat(chunks)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function simpleFile(r,w)
|
||||||
|
return function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "file"
|
||||||
|
elseif op=="open" then
|
||||||
|
if mode=="r" then
|
||||||
|
return {
|
||||||
|
read=r
|
||||||
|
}
|
||||||
|
elseif mode=="w" then
|
||||||
|
return {
|
||||||
|
write=w
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function strFile(str)
|
||||||
|
local dat=tostring(str)
|
||||||
|
local pos=1
|
||||||
|
return simpleFile(function(amount)
|
||||||
|
pos=pos+amount
|
||||||
|
return dat:sub(pos-amount, pos)
|
||||||
|
end,function() error("EACCES") end)
|
||||||
|
end
|
||||||
|
|
||||||
|
data[".meta"]=strFile(buildMeta({
|
||||||
|
stdin="/proc/self/fd/0",
|
||||||
|
stdout="/proc/self/fd/1",
|
||||||
|
stderr="/proc/self/2",
|
||||||
|
fd="/proc/self/fd",
|
||||||
|
log="/var/log/syslog.log"
|
||||||
|
}))
|
||||||
|
|
||||||
|
if kernel.EFI.getEEPROM then
|
||||||
|
function data.eeprom(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if kernel.uid~=0 then error("EACCES") end
|
||||||
|
if mode=="r" then
|
||||||
|
local ptr,eeprom=1,kernel.EFI:getEEPROM()
|
||||||
|
return {
|
||||||
|
read=function(amount)
|
||||||
|
ptr=ptr+amount
|
||||||
|
return eeprom:sub(ptr-amount, ptr)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="w" then
|
||||||
|
local firstwrite=true
|
||||||
|
return {
|
||||||
|
write=function(data)
|
||||||
|
if firstwrite then
|
||||||
|
kernel.EFI:setEEPROM(data)
|
||||||
|
else
|
||||||
|
kernel.EFI:setEEPROM(kernel.EFI:getEEPROM()..data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="a" then
|
||||||
|
return {
|
||||||
|
write=function(data)
|
||||||
|
kernel.EFI:setEEPROM(kernel.EFI:getEEPROM()..data)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
else error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if kernel.EFI.getNvram then
|
||||||
|
function data.nvram(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if kernel.uid~=0 then error("EACCES") end
|
||||||
|
if mode=="r" then
|
||||||
|
local ptr,nvram=1,kernel.EFI:getNvram()
|
||||||
|
return {
|
||||||
|
read=function(amount)
|
||||||
|
ptr=ptr+amount
|
||||||
|
return nvram:sub(ptr-amount, ptr)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="w" then
|
||||||
|
local firstwrite=true
|
||||||
|
return {
|
||||||
|
write=function(data)
|
||||||
|
if firstwrite then
|
||||||
|
kernel.EFI:setNvram(data)
|
||||||
|
else
|
||||||
|
kernel.EFI:setNvram(kernel.EFI:getNvram()..data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
elseif mode=="a" then
|
||||||
|
return {
|
||||||
|
write=function(data)
|
||||||
|
kernel.EFI:setNvram(kernel.EFI:getNvram()..data)
|
||||||
|
end
|
||||||
|
}
|
||||||
|
else error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
data["disk"]={["by-id"]={}, ["by-path"]={}, ["by-label"]={}}
|
||||||
|
-- disks
|
||||||
|
for i,v in pairs(kernel.vfs.mounts) do
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
data["raw"]={["by-type"]={}}
|
||||||
|
data["gfx"]={}
|
||||||
|
data["input"]={}
|
||||||
|
data["tty"]={}
|
||||||
|
kernel.devfs={}
|
||||||
|
kernel.devfs.data=data
|
||||||
|
kernel.devfs.proxy=proxy
|
||||||
|
kernel.disks["devfs0000"]=proxy
|
||||||
@@ -0,0 +1,266 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
|
||||||
|
local proxy = {}
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
proxy.address = "procfs0000"
|
||||||
|
proxy.isvirt = true
|
||||||
|
proxy.isReadOnly = function() return true end
|
||||||
|
proxy.spaceUsed = function() return 0 end
|
||||||
|
proxy.spaceTotal = function() return 0 end
|
||||||
|
proxy.makeDirectory = function() error("EACCES") end
|
||||||
|
proxy.remove = function() error("EACCES") end
|
||||||
|
proxy.setLabel = function() error("EACCES") end
|
||||||
|
proxy.getLabel = function() return "procfs" end
|
||||||
|
proxy.attributes = function(path) return {
|
||||||
|
size = 0,
|
||||||
|
modified = 0,
|
||||||
|
created = 0
|
||||||
|
} end
|
||||||
|
|
||||||
|
local function buildMeta(entries, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
local uid = opts.uid or 0
|
||||||
|
local gid = opts.gid or 0
|
||||||
|
local perms = opts.perms or 0x3F -- default read/write for owner/group/world
|
||||||
|
|
||||||
|
local chunks = {}
|
||||||
|
table.insert(chunks, string.char(0x02)) -- version header
|
||||||
|
|
||||||
|
for path, target in pairs(entries) do
|
||||||
|
local name = path
|
||||||
|
local nameLen = #name
|
||||||
|
if nameLen > 255 then
|
||||||
|
error("Filename too long (>255 bytes): "..name)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Determine entry type: 0x01 = symlink if target ~= nil and target ~= ""
|
||||||
|
local entryType = 0x00
|
||||||
|
local cmeta = ""
|
||||||
|
if target and target ~= "" then
|
||||||
|
entryType = 0x01
|
||||||
|
cmeta = target
|
||||||
|
end
|
||||||
|
local cmetaLen = #cmeta
|
||||||
|
if cmetaLen > 255 then
|
||||||
|
error("cmeta too long (>255 bytes) for "..name)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Build entry as bytes
|
||||||
|
table.insert(chunks, string.char(nameLen)) -- name length
|
||||||
|
table.insert(chunks, name) -- name
|
||||||
|
table.insert(chunks, string.char(entryType)) -- entry type
|
||||||
|
table.insert(chunks, string.char(uid % 256, math.floor(uid/256) % 256)) -- uid
|
||||||
|
table.insert(chunks, string.char(gid % 256, math.floor(gid/256) % 256)) -- gid
|
||||||
|
table.insert(chunks, string.char(perms % 256, math.floor(perms/256) % 256)) -- perms
|
||||||
|
table.insert(chunks, string.char(cmetaLen)) -- cmeta length
|
||||||
|
if cmetaLen > 0 then
|
||||||
|
table.insert(chunks, cmeta)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return table.concat(chunks)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function simpleFile(r,w)
|
||||||
|
return function(op, mode)
|
||||||
|
if op=="type" then
|
||||||
|
return "character device"
|
||||||
|
elseif op=="open" then
|
||||||
|
if mode=="r" then
|
||||||
|
return {
|
||||||
|
read=r
|
||||||
|
}
|
||||||
|
elseif mode=="w" then
|
||||||
|
return {
|
||||||
|
write=w
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function strFile(str)
|
||||||
|
local dat=tostring(str)
|
||||||
|
local pos=1
|
||||||
|
return simpleFile(function(amount)
|
||||||
|
pos=pos+amount
|
||||||
|
return dat:sub(pos-amount, pos)
|
||||||
|
end,function() error("EACCES") end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function newtaskproxy(task)
|
||||||
|
local files,siblings,children={},{},{}
|
||||||
|
if task.fd[0] then files["0"]=task.fd[0].path end
|
||||||
|
for i,v in ipairs(task.fd) do
|
||||||
|
files[tostring(i)]=tostring(v.path)
|
||||||
|
end
|
||||||
|
for i,v in ipairs(task.siblings) do
|
||||||
|
siblings[tostring(v.pid)]="/proc/"..tostring(v.pid)
|
||||||
|
end
|
||||||
|
for i,v in ipairs(task.children) do
|
||||||
|
children[tostring(v.pid)]="/proc/"..tostring(v.pid)
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
[".meta"]=strFile(buildMeta({cwd=task.cwd,parent="/proc/"..tostring(task.parent.pid)})),
|
||||||
|
uid=strFile(task.uid),
|
||||||
|
comm=strFile(task.name),
|
||||||
|
fd={
|
||||||
|
[".meta"]=strFile(buildMeta(files))
|
||||||
|
},
|
||||||
|
siblings={
|
||||||
|
[".meta"]=strFile(buildMeta(siblings))
|
||||||
|
},
|
||||||
|
children={
|
||||||
|
[".meta"]=strFile(buildMeta(children))
|
||||||
|
},
|
||||||
|
pid=strFile(task.pid)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:open(path, mode)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if tonumber(steps[1]) then
|
||||||
|
local task=kernel.tasks[tostring(steps[1])]
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("open", mode)
|
||||||
|
end
|
||||||
|
elseif tostring(steps[1])=="self" then
|
||||||
|
local task=kernel.currentTask
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("open", mode)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("open", mode)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
error("ENFILE")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:type(path, mode)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if #steps == 0 then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
if tonumber(steps[1]) then
|
||||||
|
local task=kernel.tasks[steps[1]]
|
||||||
|
if #steps==1 then return "directory" end
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("type", mode)
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
elseif tostring(steps[1])=="self" then
|
||||||
|
local task=kernel.currentTask
|
||||||
|
if #steps==1 then return "directory" end
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("type", mode)
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENFILE") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "function" then
|
||||||
|
return step[steps[#steps]]("type", mode)
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return "directory"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
error("ENOENT")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:list(path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if #steps == 0 then
|
||||||
|
return table.merge(table.keys(data),table.keys(kernel.tasks),{"self"})
|
||||||
|
end
|
||||||
|
if tonumber(steps[1]) then
|
||||||
|
local task=kernel.tasks[steps[1]]
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
if #steps==1 then return table.keys(step) end
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENOENT") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return table.keys(step[steps[#steps]])
|
||||||
|
end
|
||||||
|
elseif tostring(steps[1])=="self" then
|
||||||
|
local task=kernel.currentTask
|
||||||
|
local step = newtaskproxy(task)
|
||||||
|
if #steps==1 then return table.keys(step) end
|
||||||
|
for i=2, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENOENT") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return table.keys(step[steps[#steps]])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i=1, #steps-1 do
|
||||||
|
local dat = step[steps[i]]
|
||||||
|
if type(dat) ~= "table" then error("ENOENT") end
|
||||||
|
step=dat
|
||||||
|
end
|
||||||
|
if type(step[steps[#steps]]) == "table" then
|
||||||
|
return table.keys(step[steps[#steps]])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
error("ENOENT")
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:fileExists(path)
|
||||||
|
local ok = pcall(function()
|
||||||
|
return self:type(path)
|
||||||
|
end)
|
||||||
|
return ok
|
||||||
|
end
|
||||||
|
|
||||||
|
data.uptime=simpleFile(function()return tostring(kernel.EFI:getUptime())end,function()error("EACCES")end)
|
||||||
|
kernel.procfs={}
|
||||||
|
kernel.procfs.data=data
|
||||||
|
kernel.procfs.proxy=proxy
|
||||||
|
kernel.disks["procfs0000"]=proxy
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
|
||||||
|
if not kernel.config.tmpToDisk then
|
||||||
|
local proxy = {}
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
proxy.address = "tmpfs0000"
|
||||||
|
proxy.isvirt = true
|
||||||
|
proxy.isReadOnly = function() return false end
|
||||||
|
|
||||||
|
proxy.spaceUsed = function() return 0 end
|
||||||
|
proxy.spaceTotal = function() return 0 end
|
||||||
|
|
||||||
|
proxy.makeDirectory = function(_, path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1,#steps do
|
||||||
|
if not step[steps[i]] then
|
||||||
|
step[steps[i]] = {}
|
||||||
|
elseif type(step[steps[i]]) ~= "table" then
|
||||||
|
error("ENOTDIR")
|
||||||
|
end
|
||||||
|
step = step[steps[i]]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
proxy.remove = function(_, path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1,#steps-1 do
|
||||||
|
step = step[steps[i]]
|
||||||
|
if not step then error("ENOENT") end
|
||||||
|
end
|
||||||
|
step[steps[#steps]] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
proxy.setLabel = function(_, label) end
|
||||||
|
proxy.getLabel = function() return "tmpfs" end
|
||||||
|
|
||||||
|
proxy.attributes = function(_, path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1,#steps do
|
||||||
|
step = step[steps[i]]
|
||||||
|
if not step then error("ENOENT") end
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
size = type(step) == "string" and #step or 0,
|
||||||
|
modified = 0,
|
||||||
|
created = 0,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:open(path, mode)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1,#steps-1 do
|
||||||
|
if not step[steps[i]] then
|
||||||
|
if mode == "w" then step[steps[i]] = {} else error("ENOENT") end
|
||||||
|
elseif type(step[steps[i]]) ~= "table" then
|
||||||
|
error("ENOTDIR")
|
||||||
|
end
|
||||||
|
step = step[steps[i]]
|
||||||
|
end
|
||||||
|
local filename = steps[#steps]
|
||||||
|
|
||||||
|
if mode == "r" then
|
||||||
|
if type(step[filename]) ~= "string" then error("ENOENT") end
|
||||||
|
local content = step[filename]
|
||||||
|
local pos = 1
|
||||||
|
return {
|
||||||
|
read = function(amount)
|
||||||
|
amount = amount or #content
|
||||||
|
local chunk = content:sub(pos, pos+amount-1)
|
||||||
|
pos = pos + #chunk
|
||||||
|
return chunk
|
||||||
|
end,
|
||||||
|
close = function() end,
|
||||||
|
}
|
||||||
|
elseif mode == "w" then
|
||||||
|
step[filename] = ""
|
||||||
|
local buf = {}
|
||||||
|
return {
|
||||||
|
write = function(str)
|
||||||
|
buf[#buf + 1] = str
|
||||||
|
end,
|
||||||
|
close = function()
|
||||||
|
step[filename] = table.concat(buf)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
elseif mode == "a" then
|
||||||
|
if type(step[filename]) ~= "string" then step[filename] = "" end
|
||||||
|
return {
|
||||||
|
write = function(str)
|
||||||
|
step[filename] = step[filename] .. str
|
||||||
|
end,
|
||||||
|
close = function() end,
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error("EACCES")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:type(path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
if #steps == 0 then return "directory" end
|
||||||
|
for i=1,#steps do
|
||||||
|
step = step[steps[i]]
|
||||||
|
if not step then return false end
|
||||||
|
end
|
||||||
|
if type(step) == "table" then return "directory" end
|
||||||
|
if type(step) == "string" then return "file" end
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:list(path)
|
||||||
|
local steps = kernel.vfs.splitPath(path)
|
||||||
|
local step = data
|
||||||
|
for i=1,#steps do
|
||||||
|
step = step[steps[i]]
|
||||||
|
if not step then error("ENOENT") end
|
||||||
|
end
|
||||||
|
if type(step) ~= "table" then error("ENOTDIR") end
|
||||||
|
local keys = {}
|
||||||
|
for k,_ in pairs(step) do table.insert(keys, k) end
|
||||||
|
return keys
|
||||||
|
end
|
||||||
|
|
||||||
|
function proxy:fileExists(path)
|
||||||
|
local t = self:type(path)
|
||||||
|
return t == "file" or t == "directory"
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.disks["tmpfs0000"] = proxy
|
||||||
|
else
|
||||||
|
kernel.log("tmpToDisk enabled, skipping tmpfs module")
|
||||||
|
kernel.log("tmpfs0000 will passthrough to /tmp on the disk")
|
||||||
|
kernel.runWhenLoaded(function()
|
||||||
|
kernel.vfs.remove("/tmp")
|
||||||
|
kernel.vfs.mkdir("/tmp")
|
||||||
|
end)
|
||||||
|
end
|
||||||
@@ -0,0 +1,540 @@
|
|||||||
|
--:Minify:--
|
||||||
|
-- Loop device driver:
|
||||||
|
--
|
||||||
|
-- BIND (directory) - re-routes VFS calls into a host directory subtree.
|
||||||
|
-- Identical to the original behaviour.
|
||||||
|
--
|
||||||
|
-- IMAGE (*.hfs file) - mounts a Hyperion Filesystem Image. The image is
|
||||||
|
-- loaded entirely into memory; reads and writes operate
|
||||||
|
-- on the in-memory tree, so the image file is only
|
||||||
|
-- touched on attach/detach.
|
||||||
|
--
|
||||||
|
-- BHFS v1 - Binary Hyperion Filesystem Image format:
|
||||||
|
--
|
||||||
|
-- File header (8 bytes):
|
||||||
|
-- [0-3] magic: 0x42 0x48 0x46 0x53 ("BHFS")
|
||||||
|
-- [4] version: 0x01
|
||||||
|
-- [5] flags: bit0 = per-file deflate compression enabled
|
||||||
|
-- [6-7] reserved: 0x00 0x00
|
||||||
|
--
|
||||||
|
-- Records (repeated until END record):
|
||||||
|
-- [0] type: 0x01=file 0x02=dir 0x03=symlink 0xFF=end
|
||||||
|
-- [1-4] path_len (uint32 LE) - byte length of the path string
|
||||||
|
-- [5-8] raw_size (uint32 LE) - original uncompressed data size (0 for dirs)
|
||||||
|
-- [9-12] stored_size (uint32 LE) - bytes that follow in stream
|
||||||
|
-- (< raw_size means deflate-compressed;
|
||||||
|
-- = raw_size means stored as-is)
|
||||||
|
-- [13 .. 13+path_len-1] path bytes (no null terminator)
|
||||||
|
-- [.. +stored_size] data bytes
|
||||||
|
--
|
||||||
|
-- Dirs have raw_size=0, stored_size=0, zero data bytes.
|
||||||
|
-- Symlinks store the target path as data; stored_size == raw_size (no compression).
|
||||||
|
--
|
||||||
|
-- Syscalls:
|
||||||
|
-- id = syscall.losetup(path) attach dir OR .hfs image
|
||||||
|
-- id = syscall.losetup(path, true) force image mode
|
||||||
|
-- syscall.lodetach(id) detach (must be unmounted first)
|
||||||
|
-- tbl = syscall.lolist() {id -> {path,mode}, ...}
|
||||||
|
-- str = syscall.loimgcreate(srcdir) serialise VFS dir -> BHFS binary string
|
||||||
|
-- syscall.loimgwrite(str, dest) write BHFS string to a file (binary)
|
||||||
|
|
||||||
|
local kernel = ...
|
||||||
|
|
||||||
|
local _deflate = nil
|
||||||
|
local function getDeflate()
|
||||||
|
if _deflate == nil then
|
||||||
|
local ok, lib = pcall(require, "store.deflate")
|
||||||
|
_deflate = ok and lib or false
|
||||||
|
end
|
||||||
|
return _deflate or nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function pack32(n)
|
||||||
|
n = math.floor(n) % 4294967296
|
||||||
|
return string.char(
|
||||||
|
n % 256,
|
||||||
|
math.floor(n / 256) % 256,
|
||||||
|
math.floor(n / 65536) % 256,
|
||||||
|
math.floor(n / 16777216) % 256
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function unpack32(s, i)
|
||||||
|
local a, b, c, d = s:byte(i, i + 3)
|
||||||
|
return (a or 0)
|
||||||
|
+ (b or 0) * 256
|
||||||
|
+ (c or 0) * 65536
|
||||||
|
+ (d or 0) * 16777216
|
||||||
|
end
|
||||||
|
|
||||||
|
local BHFS_MAGIC = "BHFS"
|
||||||
|
local BHFS_VERSION = "\001"
|
||||||
|
local BHFS_FLAG_COMPRESS = 1
|
||||||
|
|
||||||
|
local TYPE_FILE = "\001"
|
||||||
|
local TYPE_DIR = "\002"
|
||||||
|
local TYPE_LINK = "\003"
|
||||||
|
local TYPE_END = "\255"
|
||||||
|
|
||||||
|
local B64D = {}
|
||||||
|
do
|
||||||
|
local a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
||||||
|
for i = 1, #a do B64D[a:sub(i, i)] = i - 1 end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function b64dec(s)
|
||||||
|
s = s:gsub("[^A-Za-z0-9+/=]", "")
|
||||||
|
local t, i = {}, 1
|
||||||
|
while i <= #s do
|
||||||
|
local c1 = B64D[s:sub(i, i )] or 0
|
||||||
|
local c2 = B64D[s:sub(i+1, i+1)] or 0
|
||||||
|
local c3 = B64D[s:sub(i+2, i+2)] or 0
|
||||||
|
local c4 = B64D[s:sub(i+3, i+3)] or 0
|
||||||
|
local n = c1*262144 + c2*4096 + c3*64 + c4
|
||||||
|
t[#t+1] = string.char(math.floor(n/65536) % 256)
|
||||||
|
if s:sub(i+2, i+2) ~= "=" then t[#t+1] = string.char(math.floor(n/256) % 256) end
|
||||||
|
if s:sub(i+3, i+3) ~= "=" then t[#t+1] = string.char(n % 256) end
|
||||||
|
i = i + 4
|
||||||
|
end
|
||||||
|
return table.concat(t)
|
||||||
|
end
|
||||||
|
|
||||||
|
local loopDevs = {}
|
||||||
|
local nextLoop = 0
|
||||||
|
|
||||||
|
local function makeBindDisk(id, dirPath)
|
||||||
|
local disk = { address = id, isvirt = false }
|
||||||
|
disk.isReadOnly = function() return false end
|
||||||
|
disk.spaceUsed = function() return 0 end
|
||||||
|
disk.spaceTotal = function() return 0 end
|
||||||
|
disk.setLabel = function() end
|
||||||
|
disk.getLabel = function() return id end
|
||||||
|
|
||||||
|
local function resolveBase()
|
||||||
|
local mp, mid = "/", "$"
|
||||||
|
for id2, m in pairs(kernel.vfs.mounts) do
|
||||||
|
if dirPath == m or (m == "/" and dirPath:sub(1,1) == "/")
|
||||||
|
or dirPath:sub(1, #m+1) == m.."/" then
|
||||||
|
if #m > #mp then mp = m; mid = id2 end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return kernel.vfs.disks[mid], dirPath:sub(#mp+1)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dp(path)
|
||||||
|
local hd, base = resolveBase()
|
||||||
|
local b = (base == "" or base == "/") and "" or base:gsub("^/+","")
|
||||||
|
local p = path:gsub("^/+","")
|
||||||
|
local c = ((b=="") and "/"..p or "/"..b.."/"..p):gsub("//+","/")
|
||||||
|
local r = c:sub(2); if r == "" then r = "/" end
|
||||||
|
return hd, r
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path,mode) local h,r=dp(path); return h:open(r,mode) end
|
||||||
|
function disk:type(path) local h,r=dp(path); return h:type(r) end
|
||||||
|
function disk:list(path) local h,r=dp(path); return h:list(r) end
|
||||||
|
function disk:fileExists(path) local h,r=dp(path); return h:fileExists(r) end
|
||||||
|
function disk:attributes(path) local h,r=dp(path); return h:attributes(r) end
|
||||||
|
function disk:makeDirectory(path) local h,r=dp(path); return h:makeDirectory(r) end
|
||||||
|
function disk:remove(path) local h,r=dp(path); return h:remove(r) end
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
local function makeImageDisk(id, imageStr)
|
||||||
|
local root = { kind="dir", children={} }
|
||||||
|
|
||||||
|
local function getNode(path, create)
|
||||||
|
local parts = {}
|
||||||
|
for p in path:gmatch("[^/]+") do parts[#parts+1] = p end
|
||||||
|
local node = root
|
||||||
|
for i = 1, #parts do
|
||||||
|
local name = parts[i]
|
||||||
|
if not node.children then
|
||||||
|
if not create then return nil end
|
||||||
|
node.children = {}
|
||||||
|
end
|
||||||
|
if not node.children[name] then
|
||||||
|
if not create then return nil end
|
||||||
|
node.children[name] = { kind="dir", children={} }
|
||||||
|
end
|
||||||
|
node = node.children[name]
|
||||||
|
end
|
||||||
|
return node
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ensureParent(path)
|
||||||
|
local par = path:match("^(.*)/[^/]+$") or ""
|
||||||
|
if par ~= "" then
|
||||||
|
local n = getNode(par, true)
|
||||||
|
if not n.children then n.children = {} end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if imageStr:sub(1, 4) == BHFS_MAGIC then
|
||||||
|
local pos = 9
|
||||||
|
|
||||||
|
while pos <= #imageStr do
|
||||||
|
local rtype = imageStr:sub(pos, pos)
|
||||||
|
pos = pos + 1
|
||||||
|
|
||||||
|
if rtype == TYPE_END then break end
|
||||||
|
|
||||||
|
local path_len = unpack32(imageStr, pos); pos = pos + 4
|
||||||
|
local raw_size = unpack32(imageStr, pos); pos = pos + 4
|
||||||
|
local stored_size = unpack32(imageStr, pos); pos = pos + 4
|
||||||
|
|
||||||
|
local path = imageStr:sub(pos, pos + path_len - 1)
|
||||||
|
pos = pos + path_len
|
||||||
|
|
||||||
|
local stored_data = imageStr:sub(pos, pos + stored_size - 1)
|
||||||
|
pos = pos + stored_size
|
||||||
|
local data = stored_data
|
||||||
|
if stored_size < raw_size then
|
||||||
|
local deflate = getDeflate()
|
||||||
|
if deflate then
|
||||||
|
data = deflate.decompress(stored_data) or stored_data
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if rtype == TYPE_DIR then
|
||||||
|
if path ~= "" and path ~= "/" then
|
||||||
|
ensureParent(path)
|
||||||
|
local n = getNode(path, true)
|
||||||
|
n.kind = "dir"; n.children = n.children or {}
|
||||||
|
end
|
||||||
|
elseif rtype == TYPE_FILE then
|
||||||
|
ensureParent(path)
|
||||||
|
local n = getNode(path, true)
|
||||||
|
n.kind="file"; n.data=data; n.size=#data; n.children=nil
|
||||||
|
elseif rtype == TYPE_LINK then
|
||||||
|
ensureParent(path)
|
||||||
|
local n = getNode(path, true)
|
||||||
|
n.kind="link"; n.target=data; n.children=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for line in (imageStr.."\n"):gmatch("([^\n]*)\n") do
|
||||||
|
if line == "END" then
|
||||||
|
break
|
||||||
|
elseif line:sub(1,4) == "DIR " then
|
||||||
|
local p = line:sub(5):match("^%s*(.-)%s*$")
|
||||||
|
if p and p ~= "" and p ~= "/" then
|
||||||
|
ensureParent(p)
|
||||||
|
local n = getNode(p, true)
|
||||||
|
n.kind = "dir"; n.children = n.children or {}
|
||||||
|
end
|
||||||
|
elseif line:sub(1,5) == "FILE " then
|
||||||
|
local p, sz, body = line:sub(6):match("^(%S+)%s+(%d+)%s*(.-)%s*$")
|
||||||
|
if p then
|
||||||
|
ensureParent(p)
|
||||||
|
local data = (tonumber(sz) or 0) > 0 and b64dec(body) or ""
|
||||||
|
local n = getNode(p, true)
|
||||||
|
n.kind="file"; n.data=data; n.size=#data; n.children=nil
|
||||||
|
end
|
||||||
|
elseif line:sub(1,5) == "LINK " then
|
||||||
|
local p, tgt = line:sub(6):match("^(%S+)%s+(.+)$")
|
||||||
|
if p then
|
||||||
|
ensureParent(p)
|
||||||
|
local n = getNode(p, true)
|
||||||
|
n.kind="link"; n.target=tgt; n.children=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local disk = { address=id, isvirt=false }
|
||||||
|
disk.isReadOnly = function() return false end
|
||||||
|
disk.spaceTotal = function() return 1024*1024*64 end
|
||||||
|
disk.spaceUsed = function()
|
||||||
|
local tot = 0
|
||||||
|
local function w(n)
|
||||||
|
if n.kind=="file" then tot = tot + (n.size or 0)
|
||||||
|
elseif n.kind=="dir" then for _,c in pairs(n.children or {}) do w(c) end end
|
||||||
|
end
|
||||||
|
w(root); return tot
|
||||||
|
end
|
||||||
|
disk.setLabel = function() end
|
||||||
|
disk.getLabel = function() return id end
|
||||||
|
|
||||||
|
local function norm(path)
|
||||||
|
return path:gsub("^/+",""):gsub("/+$","")
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:type(path)
|
||||||
|
local p = norm(path)
|
||||||
|
if p == "" then return "directory" end
|
||||||
|
local n = getNode(p)
|
||||||
|
if not n then return nil end
|
||||||
|
if n.kind == "dir" then return "directory" end
|
||||||
|
return "file"
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:fileExists(path)
|
||||||
|
local p = norm(path)
|
||||||
|
if p == "" then return true end
|
||||||
|
return getNode(p) ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:list(path)
|
||||||
|
local p = norm(path)
|
||||||
|
local node = (p=="") and root or getNode(p)
|
||||||
|
if not node or node.kind ~= "dir" then return {} end
|
||||||
|
local out = {}
|
||||||
|
for name in pairs(node.children or {}) do out[#out+1] = name end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:attributes(path)
|
||||||
|
local p = norm(path)
|
||||||
|
local node = (p=="") and root or getNode(p)
|
||||||
|
if not node then return nil end
|
||||||
|
return {
|
||||||
|
size = node.kind=="file" and (node.size or 0) or 0,
|
||||||
|
isDir = node.kind=="dir",
|
||||||
|
isReadOnly = false,
|
||||||
|
created = 0,
|
||||||
|
modified = 0,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:open(path, mode)
|
||||||
|
local p = norm(path)
|
||||||
|
local node = getNode(p)
|
||||||
|
|
||||||
|
if mode == "r" then
|
||||||
|
if not node or node.kind ~= "file" then error("ENOENT: "..path) end
|
||||||
|
local data, pos = node.data or "", 1
|
||||||
|
return {
|
||||||
|
read = function(n)
|
||||||
|
if pos > #data then return nil end
|
||||||
|
local chunk = data:sub(pos, pos + (n or 1) - 1)
|
||||||
|
pos = pos + #chunk; return chunk
|
||||||
|
end,
|
||||||
|
readAll = function()
|
||||||
|
local all = data:sub(pos); pos = #data + 1; return all
|
||||||
|
end,
|
||||||
|
readLine = function()
|
||||||
|
if pos > #data then return nil end
|
||||||
|
local nl = data:find("\n", pos, true)
|
||||||
|
local line
|
||||||
|
if nl then line=data:sub(pos, nl-1); pos=nl+1
|
||||||
|
else line=data:sub(pos); pos=#data+1 end
|
||||||
|
return line
|
||||||
|
end,
|
||||||
|
seek = function(w, o)
|
||||||
|
o = o or 0
|
||||||
|
if w == "set" then pos = o + 1
|
||||||
|
elseif w == "cur" then pos = pos + o
|
||||||
|
elseif w == "end" then pos = #data + 1 + o end
|
||||||
|
return pos - 1
|
||||||
|
end,
|
||||||
|
close = function() end,
|
||||||
|
}
|
||||||
|
|
||||||
|
elseif mode == "w" or mode == "a" then
|
||||||
|
local buf = (mode=="a" and node and node.kind=="file")
|
||||||
|
and {node.data or ""} or {}
|
||||||
|
local done = false
|
||||||
|
local function commit()
|
||||||
|
if done then return end; done = true
|
||||||
|
local data = table.concat(buf)
|
||||||
|
if not node then ensureParent(p); node = getNode(p, true) end
|
||||||
|
node.kind="file"; node.data=data; node.size=#data; node.children=nil
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
write = function(s) buf[#buf+1] = tostring(s) end,
|
||||||
|
writeLine = function(s) buf[#buf+1] = tostring(s).."\n" end,
|
||||||
|
flush = function() end,
|
||||||
|
close = commit,
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error("EINVAL: unknown mode: "..tostring(mode))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:makeDirectory(path)
|
||||||
|
local p = norm(path)
|
||||||
|
if p == "" then return end
|
||||||
|
ensureParent(p)
|
||||||
|
local n = getNode(p, true)
|
||||||
|
n.kind="dir"; n.children=n.children or {}; n.data=nil; n.size=nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function disk:remove(path)
|
||||||
|
local p = norm(path)
|
||||||
|
if p == "" then error("EBUSY: cannot remove root") end
|
||||||
|
local par = p:match("^(.*)/[^/]+$") or ""
|
||||||
|
local name = p:match("([^/]+)$")
|
||||||
|
local pn = (par=="") and root or getNode(par)
|
||||||
|
if pn and pn.children then pn.children[name] = nil end
|
||||||
|
end
|
||||||
|
|
||||||
|
disk._root = root
|
||||||
|
return disk
|
||||||
|
end
|
||||||
|
|
||||||
|
local function serializeDir(srcPath)
|
||||||
|
local deflate = getDeflate()
|
||||||
|
local useCompress = deflate ~= nil
|
||||||
|
|
||||||
|
local flags = useCompress and BHFS_FLAG_COMPRESS or 0
|
||||||
|
local parts = {
|
||||||
|
BHFS_MAGIC,
|
||||||
|
BHFS_VERSION,
|
||||||
|
string.char(flags),
|
||||||
|
"\0\0",
|
||||||
|
}
|
||||||
|
|
||||||
|
srcPath = srcPath:gsub("/$", "")
|
||||||
|
|
||||||
|
local MIN_COMPRESS = 64
|
||||||
|
|
||||||
|
local function walk(vpath)
|
||||||
|
local ftype = kernel.vfs.type(vpath)
|
||||||
|
|
||||||
|
if ftype == "directory" then
|
||||||
|
if vpath ~= srcPath then
|
||||||
|
local relPath = vpath:sub(#srcPath + 1)
|
||||||
|
parts[#parts+1] = TYPE_DIR
|
||||||
|
parts[#parts+1] = pack32(#relPath)
|
||||||
|
parts[#parts+1] = pack32(0)
|
||||||
|
parts[#parts+1] = pack32(0)
|
||||||
|
parts[#parts+1] = relPath
|
||||||
|
end
|
||||||
|
local ok, entries = pcall(kernel.vfs.listdir, vpath)
|
||||||
|
if ok and entries then
|
||||||
|
table.sort(entries)
|
||||||
|
for _, name in ipairs(entries) do
|
||||||
|
walk(vpath:gsub("/$","").."/"..name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
elseif ftype == "file" then
|
||||||
|
local relPath = vpath:sub(#srcPath + 1)
|
||||||
|
local ok, fd = pcall(kernel.vfs.open, vpath, "r")
|
||||||
|
if ok then
|
||||||
|
local rawData = ""
|
||||||
|
local ok2, content = pcall(kernel.vfs.read, fd, 1024*1024)
|
||||||
|
if ok2 then rawData = content or "" end
|
||||||
|
pcall(kernel.vfs.close, fd)
|
||||||
|
|
||||||
|
local storedData = rawData
|
||||||
|
if useCompress and #rawData >= MIN_COMPRESS then
|
||||||
|
local compressed = deflate.compress(rawData)
|
||||||
|
if compressed and #compressed < #rawData then
|
||||||
|
storedData = compressed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
parts[#parts+1] = TYPE_FILE
|
||||||
|
parts[#parts+1] = pack32(#relPath)
|
||||||
|
parts[#parts+1] = pack32(#rawData)
|
||||||
|
parts[#parts+1] = pack32(#storedData)
|
||||||
|
parts[#parts+1] = relPath
|
||||||
|
parts[#parts+1] = storedData
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
walk(srcPath)
|
||||||
|
|
||||||
|
parts[#parts+1] = TYPE_END
|
||||||
|
|
||||||
|
return table.concat(parts)
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["losetup"] = function(filePath, forceImage)
|
||||||
|
if not filePath then error("EINVAL") end
|
||||||
|
local task = kernel.currentTask
|
||||||
|
local euid = (task and (task.euid or task.uid)) or kernel.uid
|
||||||
|
if euid ~= 0 then error("EPERM") end
|
||||||
|
|
||||||
|
filePath = filePath:gsub("/$", "")
|
||||||
|
local id = "loop" .. tostring(nextLoop)
|
||||||
|
nextLoop = nextLoop + 1
|
||||||
|
|
||||||
|
local ftype = kernel.vfs.type(filePath)
|
||||||
|
local disk, mode
|
||||||
|
|
||||||
|
if not forceImage and ftype == "directory" then
|
||||||
|
disk = makeBindDisk(id, filePath)
|
||||||
|
mode = "bind"
|
||||||
|
elseif ftype == "file" or forceImage then
|
||||||
|
if ftype ~= "file" then error("ENOENT: not a file: "..filePath) end
|
||||||
|
|
||||||
|
local img
|
||||||
|
local ok, fd = pcall(kernel.vfs.open, filePath, "rb")
|
||||||
|
if ok then
|
||||||
|
local ok2, data = pcall(kernel.vfs.read, fd, 1024*1024*16)
|
||||||
|
pcall(kernel.vfs.close, fd)
|
||||||
|
if ok2 and data then img = data end
|
||||||
|
end
|
||||||
|
if not img then
|
||||||
|
local ok2, fd2 = pcall(kernel.vfs.open, filePath, "r")
|
||||||
|
if not ok2 then error("EIO: cannot open image: "..filePath) end
|
||||||
|
local ok3, data = pcall(kernel.vfs.read, fd2, 1024*1024*16)
|
||||||
|
pcall(kernel.vfs.close, fd2)
|
||||||
|
if not ok3 or not data then error("EIO: cannot read image: "..filePath) end
|
||||||
|
img = data
|
||||||
|
end
|
||||||
|
|
||||||
|
disk = makeImageDisk(id, img)
|
||||||
|
mode = "image"
|
||||||
|
else
|
||||||
|
error("EINVAL: path must be a directory or .hfs image file")
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.vfs.disks[id] = disk
|
||||||
|
loopDevs[id] = { path=filePath, disk=disk, mode=mode }
|
||||||
|
kernel.log("losetup: attached "..id.." ("..mode..") -> "..filePath, "INFO")
|
||||||
|
return id
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["lodetach"] = function(id)
|
||||||
|
local task = kernel.currentTask
|
||||||
|
local euid = (task and (task.euid or task.uid)) or kernel.uid
|
||||||
|
if euid ~= 0 then error("EPERM") end
|
||||||
|
|
||||||
|
if not loopDevs[id] then error("ENXIO") end
|
||||||
|
for mid in pairs(kernel.vfs.mounts) do
|
||||||
|
if mid == id then error("EBUSY: loop device is still mounted") end
|
||||||
|
end
|
||||||
|
kernel.vfs.disks[id] = nil
|
||||||
|
loopDevs[id] = nil
|
||||||
|
kernel.log("lodetach: detached "..id, "INFO")
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["lolist"] = function()
|
||||||
|
local rv = {}
|
||||||
|
for id, info in pairs(loopDevs) do
|
||||||
|
rv[id] = { path=info.path, mode=info.mode }
|
||||||
|
end
|
||||||
|
return rv
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["loimgcreate"] = function(srcPath)
|
||||||
|
local task = kernel.currentTask
|
||||||
|
local euid = (task and (task.euid or task.uid)) or kernel.uid
|
||||||
|
if euid ~= 0 then error("EPERM") end
|
||||||
|
if not srcPath then error("EINVAL") end
|
||||||
|
if kernel.vfs.type(srcPath) ~= "directory" then error("ENOTDIR: "..srcPath) end
|
||||||
|
return serializeDir(srcPath)
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.syscalls["loimgwrite"] = function(imgStr, destPath)
|
||||||
|
local task = kernel.currentTask
|
||||||
|
local euid = (task and (task.euid or task.uid)) or kernel.uid
|
||||||
|
if euid ~= 0 then error("EPERM") end
|
||||||
|
if not imgStr or not destPath then error("EINVAL") end
|
||||||
|
|
||||||
|
local ok, fd = pcall(kernel.vfs.open, destPath, "wb")
|
||||||
|
if not ok then
|
||||||
|
ok, fd = pcall(kernel.vfs.open, destPath, "w")
|
||||||
|
if not ok then error("EIO: cannot write: "..tostring(destPath)) end
|
||||||
|
end
|
||||||
|
local ok2, werr = pcall(kernel.vfs.write, fd, imgStr)
|
||||||
|
pcall(kernel.vfs.close, fd)
|
||||||
|
if not ok2 then error("EIO: write failed: "..tostring(werr)) end
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.log("Loop device driver loaded (bind + BHFS binary image + legacy HFS compat)")
|
||||||
+4
-5
@@ -11,7 +11,7 @@ end
|
|||||||
|
|
||||||
for _, line in ipairs(string.split(kernel.fstab, "\n")) do
|
for _, line in ipairs(string.split(kernel.fstab, "\n")) do
|
||||||
line = trim(line)
|
line = trim(line)
|
||||||
if line ~= "" and line:sub(1,1) == "U" then
|
if line:sub(1,1) == "U" or line:sub(1,1) == "V" or line:sub(1,1) == "F" then
|
||||||
local semicolon_pos
|
local semicolon_pos
|
||||||
for i = 3, #line do
|
for i = 3, #line do
|
||||||
if line:sub(i,i) == ";" then
|
if line:sub(i,i) == ";" then
|
||||||
@@ -21,14 +21,13 @@ for _, line in ipairs(string.split(kernel.fstab, "\n")) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not semicolon_pos or semicolon_pos == 3 then
|
if not semicolon_pos or semicolon_pos == 3 then
|
||||||
kernel.log("Invalid fstab line: "..line.." ... Skipping.", "WARN", 8)
|
kernel.log("Invalid fstab line: "..line.." ... Skipping.", "WARN", 0xFF8800)
|
||||||
else
|
else
|
||||||
local id = line:sub(3, semicolon_pos - 1)
|
local id = line:sub(3, semicolon_pos - 1)
|
||||||
local path = trim(line:sub(semicolon_pos + 1))
|
local path = trim(line:sub(semicolon_pos + 1))
|
||||||
|
kernel.log("Mounting '"..id.."' to '"..path.."'")
|
||||||
|
kernel.vfs.mount(path, id, nil, line:sub(1,1) == "F")
|
||||||
kernel.log("Mounted "..id.." to "..path)
|
kernel.log("Mounted "..id.." to "..path)
|
||||||
if id ~= "$" then
|
|
||||||
kernel.vfs.mount(path, id)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
--:Minify:--
|
||||||
|
local kernel = ...
|
||||||
|
local signal = {}
|
||||||
|
kernel.signal=signal
|
||||||
|
|
||||||
|
function signal.sigsend(pid, sig)
|
||||||
|
if sig<0 or sig>256 then error("EINVAL") end
|
||||||
|
local task = kernel.tasks[tostring(pid)]
|
||||||
|
if not task then error("ENOENT") end
|
||||||
|
if not task.sigq then return end
|
||||||
|
task.sigq[#task.sigq+1] = sig
|
||||||
|
end
|
||||||
|
|
||||||
|
function signal.sigcatch(handler)
|
||||||
|
local task=kernel.currentTask
|
||||||
|
task.sigh=handler
|
||||||
|
if not task.sigq then task.sigq={} end
|
||||||
|
local handle={
|
||||||
|
error="",
|
||||||
|
active=true
|
||||||
|
}
|
||||||
|
if task.sigd then task.sigd.active=false; end
|
||||||
|
task.sigd=handle
|
||||||
|
return handle
|
||||||
|
end
|
||||||
|
|
||||||
|
function signal.sigignore()
|
||||||
|
local task=kernel.currentTask
|
||||||
|
task.sigh=nil
|
||||||
|
task.sigq=nil
|
||||||
|
if task.sigd then task.sigd.active=false end
|
||||||
|
task.sigd=nil
|
||||||
|
end
|
||||||
|
|
||||||
|
kernel.execPerTask(function(task)
|
||||||
|
if task.status=="R" then
|
||||||
|
if task.sigq and #task.sigq ~= 0 and task.sigh then
|
||||||
|
local coro = coroutine.create(task.sigh)
|
||||||
|
local status,err=coroutine.resume(coro, table.remove(task.sigq, 1))
|
||||||
|
EFI:yeild()
|
||||||
|
if status=="error" or status==false then
|
||||||
|
task.sigd.error=err or "Unknown"
|
||||||
|
task.sigd.active=false
|
||||||
|
task.sigh=nil
|
||||||
|
task.sigq=nil
|
||||||
|
task.sigd=nil
|
||||||
|
elseif status=="success" or status==true then
|
||||||
|
if err=="syscall" then
|
||||||
|
task.sigd.error="Cannot execute syscalls from signals"
|
||||||
|
task.sigd.active=false
|
||||||
|
task.sigh=nil
|
||||||
|
task.sigq=nil
|
||||||
|
task.sigd=nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,2)
|
||||||
|
|
||||||
|
local s=kernel.syscalls
|
||||||
|
s["sigsend"] = signal.sigsend
|
||||||
|
s["sigcatch"] = signal.sigcatch
|
||||||
|
s["sigignore"] = signal.sigignore
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user