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

Sudo Bash

Started by Lemur, 22 December 2013 - 04:09 PM
Lemur #1
Posted 22 December 2013 - 05:09 PM
While I have no qualms with LUA itself, it would be interesting to try and emulate some of the functionality of typical unix programs inside the terminal, including piping. Does anyone know if this has been done before? If not I'll look into how to pull it off. (Well, as long as I can *args or something to that effect, simple enough.)

I come from a heavy Ruby and Unix background, and work as a Software Engineer. Experience wise I'm not far from a Senior level. How difficult would you think something like this would be to implement?

Current things I'd want to get made:
  • grep / ag
  • sed
  • cut
  • split
  • pipes
  • regex
  • ping (find if another computer is up / port is up)
  • Telnet / SSH (connect to another computer)
  • Vim
  • Git integration

Of course I intend to do a fair amount of other research around this and find out more, but if someone already knows of something to this effect that I can contribute to I love to hack about a bit. This seems like a fun one to try and make happen either way.
Lyqyd #2
Posted 23 December 2013 - 04:07 PM
For something ssh-like, my nsh may prove to be a useful reference.

As for piping, there are two sticking points: proper command-line interpretation and coroutine management. Piping and IO redirection on the command line are more complex than most people realize. None of the built-in programs make any effort to support IO redirection, so you'd be on your own to create all of them again, as well as coming up with a coroutine model that makes sense for piping.

Git integration is read-only, essentially. You could probably set up git to work within ComputerCraft if you had the dedication, and you could fetch from remote repositories. However, unless you can find a git hosting service that allows unauthenticated (!) access to the repos, you could not upload to another repo from within ComputerCraft due limitations in the http API.
awsmazinggenius #3
Posted 23 December 2013 - 04:56 PM
For something ssh-like, my nsh may prove to be a useful reference.

As for piping, there are two sticking points: proper command-line interpretation and coroutine management. Piping and IO redirection on the command line are more complex than most people realize. None of the built-in programs make any effort to support IO redirection, so you'd be on your own to create all of them again, as well as coming up with a coroutine model that makes sense for piping.

Git integration is read-only, essentially. You could probably set up git to work within ComputerCraft if you had the dedication, and you could fetch from remote repositories. However, unless you can find a git hosting service that allows unauthenticated (!) access to the repos, you could not upload to another repo from within ComputerCraft due limitations in the http API.

I thought something like this could work if you willing to host your own server. You could display a link in your app to a website where you could use the GitHub API's OAuth authentication to gain access to the account, then display a key to type into the CC app. This key would identify the app to the server, and would be stored securely (the site could use a hashing mechanism) on the ComputerCraft computer (both server and client (CC) would hash said key). The CC client could then send encrypted requests to the server (with a common key) which would handle said requests through GitHub's API. This method would be restricted to GitHub, however, and porting it to another code host (say, BitBucket) would not be the world's easiest task. Also, this was a very brief idea I had, so it would probably require some more thought before actually used.
Edited on 23 December 2013 - 03:57 PM
Lemur #4
Posted 24 December 2013 - 01:11 AM
SSH Key authentication may be able to do something to that effect. I'll read through NSH to get an idea.

Do we have the ability to require libraries to split up files?

Essentially Piping would be syntactic sugar on command chaining.
v
Let's say we use Ruby for this:

Program = Struct.new(:name, :args)

command = "echo 'one' | grep 'one'"

def prog(commands)
  commands.split('|').reverse.map { |program| args = program.split ' '; Program.new(args[0], args[1..-1].join(',')) }
end

def chainify(list)
  head, tail = list[0], list[1..-1]
  tail.empty? ? "#{head.name}(#{head.args})" : "#{head.name}(#{head.args}, #{chainify(tail)})"
end

puts chainify(prog("echo 'one' | grep 'one'")) # => grep('one', echo('one'))
puts chainify prog("try 'one' 'two' 'three' | grep 'one'") # => "grep('one', try('one','two','three'))"

This necessitates you making the last arg optional, and first class functions of some sort. Of course, it's probably prone to a few bugs as this was a 5 minute one-shot. Of course it's hacky ruby considering everything in Ruby is an object, but I'm mapping more towards something Lua would be more likely to do.

Greenspun would be proud, I just hacked together a slight macro-eval system. Switch puts with eval, and it'd work. What'd suck is if Lua didn't have eval or first class functions.
Wobbo #5
Posted 24 December 2013 - 06:28 AM
There are some shells/OSes that allow for piping, like Leadlined OS and Project NewLife, so it is possible to do piping.

Also, I created a small grep utility here. It uses Lua regex and supports almost all POSIX options(and silently accepts the rest)

​EDIT: found lik to project newlife.
Edited on 24 December 2013 - 05:29 AM