예제 #1
0
func bflag(c cmd.Ctx) error {
	args := c.Args
	xprintf("bflag...\n")
	if len(args) != 2 || len(args[1]) == 0 || args[1][0] != '-' && args[1][0] != '+' {
		return errors.New("usage: flag ±flags")
	}
	flg := args[1]
	set := flg[0] == '+'
	for _, r := range flg[1:] {
		switch r {
		case 'D':
			debug = set
		case 'V':
			verb = set
			debug = set
		case 'N':
			debugNs = set
		default:
			c.Warn("unknown flag '%c'", r)
			return errors.New("unknown flag")
		}
	}
	debugYacc = debug
	debugExec = verb
	cmd.Debug = debugNs
	return nil
}
예제 #2
0
func btype(c cmd.Ctx) error {
	args := c.Args
	if len(args) == 1 {
		return errors.New("usage: type name...")
	}
	for _, nm := range args[1:] {
		c.Printf("%s: %s\n", nm, ntype(nm))
	}
	return nil
}
예제 #3
0
func becho(c cmd.Ctx) error {
	args := c.Args[1:]
	nl := "\n"
	if len(args) >= 1 && args[0] == "-n" {
		nl = ""
		args = args[1:]
	}
	str := strings.Join(args, " ")
	c.Printf("%s%s", str, nl)
	return nil
}
예제 #4
0
func bpwd(c cmd.Ctx) error {
	args := c.Args
	if len(args) > 1 {
		return errors.New("extra args to pwd")
	}
	s, err := os.Getwd()
	if err != nil {
		return err
	}
	c.Printf("%s\n", s)
	return nil
}
예제 #5
0
func (nd *Nd) xExec() error {
	xprintf("x %s\n", nd)
	err := nd.redirs()
	if err != nil {
		xprintf("x %s done\n", nd)
		nd.closeAll()
		nd.waitc <- err
		return err
	}
	args := nd.names()
	if len(args) == 0 {
		xprintf("x %s done\n", nd)
		err = errors.New("no name for command")
		nd.closeAll()
		nd.waitc <- err
		return err
	}
	fname := LookCmd(args[0])
	name := args[0]

	fn := bltin[name]
	if fn != nil {
		ctx := cmd.Ctx{
			Stdin:  nd.Stdin,
			Stdout: nd.Stdout,
			Stderr: nd.Stderr,
			Intrc:  nd.intrc,
			Args:   args,
		}
		go func() {
			err := fn(ctx)
			if err != nil {
				ctx.Warn("%s", err)
			}
			nd.closeAll()
			xprintf("x %s done\n", nd)
			nd.waitc <- err
			xprintf("x %s waitc done\n", nd)
		}()
		return nil
	}

	fns := funcs[name]
	if fns != nil {
		bdy := fns.Child[0]
		bdy.mkExec(nd.Stdin, nd.Stdout, nd.Stderr, args[1:]...)
		err = bdy.xCmds()
		if err != nil {
			nd.closeAll()
			nd.waitc <- err
			return err
		}
		go func() {
			err := bdy.wait()
			nd.closeAll()
			xprintf("x %s done\n", nd)
			nd.waitc <- err
		}()
		return nil
	}

	if nd.interrupted() {
		err = errors.New("interrupted")
		nd.closeAll()
		xprintf("x %s done\n", nd)
		nd.waitc <- err
		return err
	}
	nd.x = exec.Command(fname, args[1:]...)
	nd.x.Stdin, nd.x.Stdout, nd.x.Stderr = nd.Stdin, nd.Stdout, nd.Stderr
	nd.x.ExtraFiles = append(nd.x.ExtraFiles, nd.extrafds...)
	err = nd.x.Start()
	if err != nil {
		dbg.Warn("%s: %s", name, err)
		xprintf("x %s done\n", nd)
		nd.closeAll()
		nd.waitc <- err
		return err
	}
	go func() {
		xprintf("x %s done\n", nd)
		errc := make(chan error, 1)
		go func() {
			errc <- nd.x.Wait()
		}()
		var err error
	Loop:
		for {
			select {
			case err = <-errc:
				break Loop
			case <-nd.intrc:
				err = errors.New("interrupted")
				proc := nd.x.Process
				if proc != nil {
					proc.Signal(os.Interrupt)
				} else {
					break Loop
				}
			}
		}
		nd.closeAll()
		nd.waitc <- err
	}()
	return nil
}