mirror of
https://github.com/gechandesu/runcmd.git
synced 2026-07-15 00:50:01 +03:00
Compare commits
6
Commits
v0.3.0
..
7390af4699
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7390af4699 | ||
|
|
8286af90eb | ||
|
|
14864ba992 | ||
|
|
c62722cba9 | ||
|
|
7acab2165e | ||
|
|
8e7d757b3b |
@@ -9,7 +9,7 @@ but I don't like any of them. So let's overview.
|
||||
|
||||
* `os.execvp()`, `os.execve()` — cross-platform versions of the C functions of the same name.
|
||||
|
||||
* `os.execute()`, `os.execute_opt()`, `os.execute_or_exit()`, `os.execute_or_panic()` — starts and waits for a command to completed. Under the hood, they perform a dirty hack by calling shell with stream redirection `'exec 2>&1;${cmd}'`.
|
||||
* `os.execute()`, `os.execute_opt()`, `os.execute_or_exit()`, `os.execute_or_panic()`, `os.raw_execute()` — starts and waits for a command to completed. Under the hood, they perform a dirty hack by calling shell with stream redirection `'exec 2>&1;${cmd}'`.
|
||||
|
||||
* `util.execute_with_timeout()` (from `os.util`) — just an `os.execute()` wrapper.
|
||||
|
||||
@@ -55,7 +55,6 @@ directory. **Examples are very important**.
|
||||
|
||||
- [x] Basic implementation.
|
||||
- [x] Contexts support for creating cancelable commands, commands with timeouts, etc.
|
||||
- [ ] Process groups support, pgkill().
|
||||
- [ ] Better error handling and more tests...
|
||||
|
||||
Send pull requests for additional features/bugfixes.
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
module runcmd
|
||||
|
||||
import io
|
||||
|
||||
// buffer creates simple bytes buffer that can be read through `io.Reader` interface.
|
||||
pub fn buffer(data []u8) ByteBuffer {
|
||||
return ByteBuffer{
|
||||
bytes: data
|
||||
}
|
||||
}
|
||||
|
||||
struct ByteBuffer {
|
||||
bytes []u8
|
||||
mut:
|
||||
pos int
|
||||
}
|
||||
|
||||
// read reads `buf.len` bytes from internal bytes buffer and returns number of bytes read.
|
||||
pub fn (mut b ByteBuffer) read(mut buf []u8) !int {
|
||||
if b.pos >= b.bytes.len {
|
||||
return io.Eof{}
|
||||
}
|
||||
n := copy(mut buf, b.bytes[b.pos..])
|
||||
b.pos += n
|
||||
return n
|
||||
}
|
||||
@@ -268,7 +268,7 @@ pub fn (mut c Command) start() !int {
|
||||
path: path
|
||||
argv: c.args
|
||||
env: if c.env.len == 0 { os.environ() } else { c.env }
|
||||
dir: c.dir
|
||||
dir: os.abs_path(c.dir)
|
||||
post_fork: [parent_pipes_hook]
|
||||
pre_exec: pre_exec_hooks
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import io.string_reader
|
||||
import io
|
||||
import rand
|
||||
import runcmd
|
||||
import time
|
||||
@@ -20,7 +20,7 @@ fn main() {
|
||||
mut child_stdout := cmd.stdout()!
|
||||
|
||||
// Prepare reader to store command output.
|
||||
mut output := string_reader.StringReader.new(reader: child_stdout)
|
||||
mut output := io.new_buffered_reader(reader: child_stdout)
|
||||
|
||||
// Start stdout reading in a coroutine.
|
||||
//
|
||||
@@ -56,4 +56,6 @@ fn main() {
|
||||
|
||||
// wait() will close the child stdout file desciptor by itself.
|
||||
cmd.wait()!
|
||||
|
||||
println('Child state: ${cmd.state}')
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import io.string_reader
|
||||
import io
|
||||
import runcmd
|
||||
|
||||
fn main() {
|
||||
@@ -14,7 +14,7 @@ fn main() {
|
||||
|
||||
// Setup StringReader with stdout input. Note the cmd.stdout()! call, it
|
||||
// returns the io.Reader interface and reads child process stdout file descriptor.
|
||||
mut reader := string_reader.StringReader.new(reader: cmd.stdout()!)
|
||||
mut reader := io.new_buffered_reader(reader: cmd.stdout()!)
|
||||
|
||||
// Read sdtout line by line until EOF.
|
||||
for {
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
import io
|
||||
import strings
|
||||
import runcmd
|
||||
|
||||
struct ByteBuffer {
|
||||
bytes []u8
|
||||
mut:
|
||||
pos int
|
||||
}
|
||||
|
||||
// read reads `buf.len` bytes from internal bytes buffer and returns number of bytes read.
|
||||
pub fn (mut b ByteBuffer) read(mut buf []u8) !int {
|
||||
if b.pos >= b.bytes.len {
|
||||
return io.Eof{}
|
||||
}
|
||||
n := copy(mut buf, b.bytes[b.pos..])
|
||||
b.pos += n
|
||||
return n
|
||||
}
|
||||
|
||||
fn main() {
|
||||
input := 'Hello from parent process!'
|
||||
|
||||
@@ -8,8 +25,10 @@ fn main() {
|
||||
// * `reader` reads input from the parent process; it will be copied to the
|
||||
// standard input of the child process.
|
||||
// * `writer` accepts data from the child process; it will be copied from the
|
||||
// standard output of the child process.
|
||||
mut reader := runcmd.buffer(input.bytes())
|
||||
// standard output of the child process. This is optinal.
|
||||
mut reader := ByteBuffer{
|
||||
bytes: input.bytes()
|
||||
}
|
||||
mut writer := strings.new_builder(4096)
|
||||
|
||||
// Prepare the command.
|
||||
|
||||
@@ -76,19 +76,20 @@ pub fn (s ProcessState) str() string {
|
||||
sig_str := os.sigint_to_signal_name(sig)
|
||||
str = 'signal: ${sig} (${sig_str})'
|
||||
}
|
||||
s.status.stopped() {
|
||||
str = 'stop signal: ${s.status.stop_signal()}'
|
||||
}
|
||||
s.status.continued() {
|
||||
str = 'continued'
|
||||
}
|
||||
// s.status.stopped() {
|
||||
// str = 'stop signal: ${s.status.stop_signal()}'
|
||||
// }
|
||||
// s.status.continued() {
|
||||
// str = 'continued'
|
||||
// }
|
||||
else {
|
||||
str = 'unknown'
|
||||
}
|
||||
}
|
||||
if s.status.coredump() {
|
||||
str += ' (core dumped)'
|
||||
}
|
||||
|
||||
// if s.status.coredump() {
|
||||
// str += ' (core dumped)'
|
||||
// }
|
||||
return str
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Module {
|
||||
name: 'runcmd'
|
||||
description: 'Run external commands'
|
||||
version: '0.3.0'
|
||||
version: '0.4.0'
|
||||
license: 'Unlicense'
|
||||
repo_url: 'https://github.com/gechandesu/runcmd'
|
||||
dependencies: []
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
module runcmd
|
||||
|
||||
@[trusted]
|
||||
fn C.WIFSTOPPED(int) bool
|
||||
// fn C.WIFSTOPPED(i32) bool
|
||||
|
||||
@[trusted]
|
||||
fn C.WCOREDUMP(int) bool
|
||||
// fn C.WCOREDUMP(i32) bool
|
||||
|
||||
@[trusted]
|
||||
fn C.WIFCONTINUED(int) bool
|
||||
// fn C.WIFCONTINUED(i32) bool
|
||||
|
||||
@[trusted]
|
||||
fn C.WSTOPSIG(int) int
|
||||
// fn C.WSTOPSIG(i32) int
|
||||
|
||||
// WaitStatus stores the result value of [wait(2)](https://www.man7.org/linux/man-pages/man2/wait.2.html) syscall.
|
||||
pub type WaitStatus = u32
|
||||
pub type WaitStatus = int
|
||||
|
||||
// exited returns true if process is exited.
|
||||
pub fn (w WaitStatus) exited() bool {
|
||||
@@ -41,29 +37,29 @@ pub fn (w WaitStatus) term_signal() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
// stopped returns true if the child process was stopped by delivery of a signal.
|
||||
pub fn (w WaitStatus) stopped() bool {
|
||||
return C.WIFSTOPPED(w)
|
||||
}
|
||||
// // stopped returns true if the child process was stopped by delivery of a signal.
|
||||
// pub fn (w WaitStatus) stopped() bool {
|
||||
// return C.WIFSTOPPED(w)
|
||||
// }
|
||||
|
||||
// stop_signal returns the number of the signal which caused the child to stop.
|
||||
pub fn (w WaitStatus) stop_signal() int {
|
||||
if w.stopped() {
|
||||
return C.WSTOPSIG(w)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
// // stop_signal returns the number of the signal which caused the child to stop.
|
||||
// pub fn (w WaitStatus) stop_signal() int {
|
||||
// if w.stopped() {
|
||||
// return C.WSTOPSIG(w)
|
||||
// }
|
||||
// return -1
|
||||
// }
|
||||
|
||||
// continued returns true if the child process was resumed by delivery of SIGCONT.
|
||||
pub fn (w WaitStatus) continued() bool {
|
||||
return C.WIFCONTINUED(w)
|
||||
}
|
||||
// // continued returns true if the child process was resumed by delivery of SIGCONT.
|
||||
// pub fn (w WaitStatus) continued() bool {
|
||||
// return C.WIFCONTINUED(w)
|
||||
// }
|
||||
|
||||
// coredump returns true if the child produced a core dump.
|
||||
// See [core(5)](https://man7.org/linux/man-pages/man5/core.5.html).
|
||||
pub fn (w WaitStatus) coredump() bool {
|
||||
if w.signaled() {
|
||||
return C.WCOREDUMP(w)
|
||||
}
|
||||
return false
|
||||
}
|
||||
// // coredump returns true if the child produced a core dump.
|
||||
// // See [core(5)](https://man7.org/linux/man-pages/man5/core.5.html).
|
||||
// pub fn (w WaitStatus) coredump() bool {
|
||||
// if w.signaled() {
|
||||
// return C.WCOREDUMP(w)
|
||||
// }
|
||||
// return false
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user