github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/core/pipe/namedpipe.go (about)

     1  package cmdpipe
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"github.com/lmorg/murex/lang"
     8  	"github.com/lmorg/murex/lang/stdio"
     9  	"github.com/lmorg/murex/lang/types"
    10  	"github.com/lmorg/murex/utils/consts"
    11  )
    12  
    13  func init() {
    14  	lang.DefineMethod("(murex named pipe)", cmdMurexNamedPipe, types.Null, types.Any)
    15  	lang.DefineMethod(consts.NamedPipeProcName, cmdMurexNamedPipe, types.Any, types.Any)
    16  }
    17  
    18  func cmdMurexNamedPipe(p *lang.Process) error {
    19  	name, err := p.Parameters.String(0)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	var pipe stdio.Io
    25  
    26  	if name == "stdin" {
    27  		pipe = p.Scope.Stdin
    28  
    29  	} else {
    30  		pipe, err = lang.GlobalPipes.Get(name)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	}
    35  
    36  	if pipe == nil {
    37  		return errors.New("STDIN is null")
    38  	}
    39  
    40  	if p.IsMethod {
    41  		pipe.SetDataType(p.Stdin.GetDataType())
    42  		_, err = io.Copy(pipe, p.Stdin)
    43  		return err
    44  	}
    45  
    46  	p.Stdout.SetDataType(pipe.GetDataType())
    47  	_, err = io.Copy(p.Stdout, pipe)
    48  	return err
    49  }