Ejemplo n.º 1
0
func Run(c cmd.Ctx) (err error) {
	argv := c.Args
	x := &xCmd{Ctx: &c}
	x.Flags = opt.New("expr")
	debugLex = false
	bhelp := false
	x.NewFlag("F", "report known functions and exit", &bhelp)
	x.NewFlag("D", "debug", &debugLex)
	x.NewFlag("q", "do not print values as they are evaluated", &x.quiet)
	x.Argv0 = argv[0]
	args, err := x.Parse(argv)
	if err != nil {
		x.Usage(x.Stderr)
		return err
	}
	cmd.MkNS()
	if bhelp {
		fns := []string{}
		for k := range funcs {
			fns = append(fns, k)
		}
		sort.Sort(sort.StringSlice(fns))
		for _, b := range fns {
			x.Printf("%s\n", b)
		}
		return nil
	}
	if len(args) != 0 {
		expr := strings.Join(args, " ")
		return x.expr(expr)
	}
	var sts error
	scn := bufio.NewScanner(x.Stdin)
	for scn.Scan() {
		ln := scn.Text()
		if ln == "" {
			continue
		}
		if err := x.expr(ln); err != nil {
			sts = errors.New("errors")
			x.Warn("'%s': %s", ln, err)
		}
	}
	if err := scn.Err(); err != nil {
		x.Warn("%s", err)
		sts = errors.New("errors")
	}
	if x, ok := result.(bool); sts==nil && ok {
		if x {
			return nil
		}
		return errors.New("false")
	}
	return sts
}
Ejemplo n.º 2
0
func main() {
	defer dbg.Exits("errors")
	os.Args[0] = "ql"
	opts.NewFlag("c", "cmd: execute this command and exit", &cmdarg)
	opts.NewFlag("D", "debug", &debug)
	opts.NewFlag("V", "verbose debug", &verb)
	opts.NewFlag("N", "debug ns", &debugNs)
	args, err := opts.Parse(os.Args)
	if err != nil {
		opts.Usage(os.Stderr)
		dbg.Fatal(err)
	}

	cmd.Debug = debugNs
	debugYacc = debug
	debugExec = verb
	cmd.MkNS()

	var in inText
	Interactive = cmd.IsTTY(os.Stdin)
	if Interactive {
		dprintf("interactive\n")
		if xprompt := os.Getenv("prompt"); xprompt != "" {
			prompt = xprompt
		} else {
			os.Setenv("prompt", prompt)
		}
		if xprompt2 := os.Getenv("prompt2"); xprompt2 != "" {
			prompt2 = xprompt2
		} else {
			os.Setenv("prompt2", prompt2)
		}
	} else {
		dprintf("script\n")
		IntrExits = true
	}
	var argv0, iname string
	var argv []string
	if cmdarg != "" {
		in = bytes.NewBufferString(cmdarg + "\n")
		argv0 = "ql"
		iname = "flag-c"
		argv = args
	} else if len(args) == 0 {
		if Interactive {
			SetEnvList("argv0", "ql")
			SetEnvList("argv", args...)
			Argv = args
			rdr := cmd.NewReader(os.Stdin, os.Stdout, prompt)
			Prompter = rdr
			for interactive(bufio.NewReader(rdr)) {
				dprintf("*** interrupted\n")
				rdr.Flush()
			}
			dbg.Exits("")
		}
		iname = "stdin"
		in = bufio.NewReader(os.Stdin)
		argv0 = "ql"
	} else {
		argv0 = args[0]
		iname = args[0]
		argv = args[1:]
		idata, err := ioutil.ReadFile(iname)
		if err != nil {
			dbg.Fatal("open: %s: %s", iname, err)
		}
		in = bytes.NewBuffer(idata)
	}

	SetEnvList("argv0", argv0)
	SetEnvList("argv", argv...)
	Argv = argv
	lexer = newLex(iname, in)
	yyParse(lexer)
	dbg.Exits(os.Getenv("status"))
}