Write into stdin, read from stdout Subprocess/Process in Glib/GTK Vala

50 views Asked by At

I have a test bash/sh script. It simply shows a question and reads answer from console, then it shows output:

#!/bin/bash
# Ask the user for their name
echo Hello, who am I talking to?
read varname
echo It\'s nice to meet you $varname

I use it to test more comples sh script which literally does the same but executed in Go, Python and Vala. Python looks like:

user_input = entry1.get_text()
p = Popen(['/usr/share/bin/bla-bla'], shell=True, stdin=PIPE, stdout=PIPE)
script_output = p.communicate(input=user_input.encode('utf-8'))[0]
if script_output:
    bla-bla
else:
    Gtk.main_quit()

In Go it looks like this:

    user_input, _ := entry1.GetText()
    cmd := exec.Command(
        "/usr/share/bla-bla",
    )
  
    var outb bytes.Buffer
    var inb bytes.Buffer
    var errb bytes.Buffer
    cmd.Stdout = &outb
    cmd.Stderr = &errb
    cmd.Stdin = &inb
    
    inb.Write([]byte(user_input))
    cmd.Run()

    if output_line := strings.TrimSuffix(outb.String(), "\n"); output_line != "" {
        lab4.SetText(output_line)
    } else {
        but2.SetSensitive(false)
    }

I need to get the same functionality in Vala GTK/Glib. I have searched and found few solutions but none of them work as I expected. The main issue is script does not get data from stdin. For example this doesn't work: Read/write file pipes in vala/glib

Please help me with it.

0

There are 0 answers