mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os: basic proof of concept stdout capture; autofree: small fixes
This commit is contained in:
@@ -159,6 +159,54 @@ pub fn exec(cmd string) ?Result {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Command {
|
||||
mut:
|
||||
f voidptr
|
||||
pub mut:
|
||||
eof bool
|
||||
pub:
|
||||
path string
|
||||
redirect_stdout bool
|
||||
}
|
||||
|
||||
//pub fn command(cmd Command) Command {
|
||||
//}
|
||||
|
||||
pub fn (mut c Command) start()? {
|
||||
pcmd := '$c.path 2>&1'
|
||||
c.f = vpopen(pcmd)
|
||||
if isnil(c.f) {
|
||||
return error('exec("$c.path") failed')
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut c Command) read_line() string {
|
||||
buf := [4096]byte{}
|
||||
mut res := strings.new_builder(1024)
|
||||
unsafe {
|
||||
for C.fgets(charptr(buf), 4096, c.f) != 0 {
|
||||
bufbp := byteptr(buf)
|
||||
len := vstrlen(bufbp)
|
||||
for i in 0..len {
|
||||
if int(bufbp[i]) == `\n` {
|
||||
res.write_bytes(bufbp, i)
|
||||
return res.str()
|
||||
}
|
||||
}
|
||||
res.write_bytes(bufbp, len)
|
||||
}
|
||||
}
|
||||
c.eof = true
|
||||
return res.str()
|
||||
}
|
||||
|
||||
pub fn (c &Command) close()? {
|
||||
exit_code := vpclose(c.f)
|
||||
if exit_code == 127 {
|
||||
return error_with_code('error', 127)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn symlink(origin string, target string) ?bool {
|
||||
res := C.symlink(charptr(origin.str), charptr(target.str))
|
||||
if res == 0 {
|
||||
|
||||
@@ -503,3 +503,18 @@ fn test_write_file_array_structs() {
|
||||
assert rarr.len == maxn
|
||||
// eprintln( rarr.str().replace('\n', ' ').replace('},', '},\n'))
|
||||
}
|
||||
|
||||
fn test_stdout_capture() {
|
||||
/*
|
||||
mut cmd := os.Command{
|
||||
path:'cat'
|
||||
redirect_stdout: true
|
||||
}
|
||||
cmd.start()
|
||||
for !cmd.eof {
|
||||
line := cmd.read_line()
|
||||
println('line="$line"')
|
||||
}
|
||||
cmd.close()
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user