github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/ed/system.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // system.go implements the "System" wrapper class to exec.Cmd
     6  
     7  package main
     8  
     9  import (
    10  	"io"
    11  	"os/exec"
    12  )
    13  
    14  const (
    15  	shellpath = "/bin/sh"
    16  	shellopts = "-c"
    17  )
    18  
    19  // System is a wrapper around exec.Cmd to run things in the Ed way
    20  type System struct {
    21  	Cmd    string
    22  	Stdin  io.Reader
    23  	Stdout io.Writer
    24  	Stderr io.Writer
    25  
    26  	cmdSane string
    27  }
    28  
    29  // Run a command (using the shell for arg processing)
    30  func (s *System) Run() (e error) {
    31  	s.cmdSane = rxSanitize.ReplaceAllString(s.Cmd, "..")
    32  	idx := rxCmdSub.FindAllStringIndex(s.cmdSane, -1)
    33  	fCmd := ""
    34  	oCmd := 0
    35  	for _, m := range idx {
    36  		fCmd += s.Cmd[oCmd:m[0]]
    37  		fCmd += state.fileName
    38  		oCmd = m[1]
    39  	}
    40  	fCmd += s.Cmd[oCmd:]
    41  
    42  	cmd := exec.Command(shellpath, shellopts, fCmd)
    43  	cmd.Stdin = s.Stdin
    44  	cmd.Stdout = s.Stdout
    45  	cmd.Stderr = s.Stderr
    46  	return cmd.Run()
    47  }