Пример #1
0
// runs here
func runFlag(x *xEnv, argv ...string) error {
	c := app.AppCtx()
	app.Dprintf("flag %v\n", argv)
	switch len(argv) {
	case 1:
		flgs := ""
		if c.Debug {
			flgs += "D"
		}
		if x.debugX {
			flgs += "X"
		}
		if x.debugL {
			flgs += "L"
		}
		if x.iflag {
			flgs += "i"
		}
		app.Printf("flags %s\n", flgs)
		return nil
	case 2:
	default:
		app.Eprintf("usage: flags [±]flags\n")
		return dbg.ErrUsage
	}
	flg := argv[1]
	set := flg[0] == '+'
	clear := flg[0] != '-'
	if !set && !clear {
		// clear all flags
		c.Debug = false
		x.debugX = false
		x.debugL = false
		// then set those named
		set = true
	} else {
		flg = flg[1:]
	}
	for _, r := range flg {
		switch r {
		case 'D':
			c.Debug = (c.Debug && !clear) || set
		case 'X':
			x.debugX = (x.debugX && !clear) || set
		case 'L':
			x.debugL = (x.debugL && !clear) || set
		case 'i':
			app.Warn("'-i' cannot be changed")
		default:
			app.Warn("unknown flag '%c'", r)
		}
	}
	return nil
}
Пример #2
0
func main() {
	defer app.Exiting()
	x := app.New()
	stacks := false
	opts.NewFlag("F", "debug fuse requests", &zxfs.Debug)
	opts.NewFlag("D", "debug", &x.Debug)
	opts.NewFlag("s", "statistics", &sflag)
	opts.NewFlag("x", "addr: re-export locally the ql tree to this address", &xaddr)
	opts.NewFlag("S", "dump stacks on unmount for debugging", &stacks)
	args, err := opts.Parse(x.Args)
	if err != nil {
		app.Warn("%s", err)
		opts.Usage()
		app.Exits(err)
	}
	switch len(args) {
	case 0:
	case 1:
		mntdir = args[1]
	default:
		app.Warn("wrong number of arguments")
		opts.Usage()
		app.Exits("usage")
	}
	dprintf("debug on\n")
	qfs, err := qlfs.New("qlfs")
	if err != nil {
		app.Fatal(err)
	}
	qfs.Dbg = x.Debug
	qfs.Flags.Add("fdebug", &zxfs.Debug)
	st := &zx.IOstats{}
	qfs.IOstats = st
	if xaddr != "" {
		rfs.Server(qfs, xaddr)
	}
	err = zxfs.MountServer(qfs, mntdir)
	if sflag {
		st.Averages()
		app.Warn("%s iostats:\n%s\n", qfs.Name(), st)
	}
	if stacks {
		app.Warn("*** PANICING ON USER REQUEST (-S) ***")
		panic("stack dump")
	}
	if err != nil {
		app.Fatal("%s", err)
	}
	app.Warn("unmounted: exiting")
}
Пример #3
0
// html writer
func wrhtml(t *Text, wid int, out io.Writer, outfig string) {
	f := &htmlFmt{
		par: &par{fn: escHtml, out: out, wid: wid, tab: "    "},
		outfig: outfig,
	}
	var tmpl []string
	if cliveMan {
		dat, err := nsutil.GetAll(TEMPLATE)
		if err != nil {
			app.Warn("%s", err)
		} else {
			tmpl = strings.Split(string(dat), "\n")
		}
		for len(tmpl) > 0 {
			ln := tmpl[0]
			tmpl = tmpl[1:]
			fmt.Fprintf(out, "%s\n", ln)
			if strings.Contains(ln, `div id="content" class="Content"`) {
				break
			}
		}
	}
	f.run(t)
	for _, ln := range tmpl {
		fmt.Fprintf(out, "%s\n", ln)
	}
}
Пример #4
0
func (e *Elem) Warn(fmts string, args ...interface{}) {
	if e != nil && e.fname != "" {
		fmts = "%s:%d: " + fmts
		args = append([]interface{}{e.fname, e.lno}, args...)
	}
	app.Warn(fmts, args...)
}
Пример #5
0
func (l *lex) source(what string) {
	dat, err := nsutil.GetAll(what)
	if err != nil {
		app.Warn("open: %s: %s", what, err)
		return
	}
	l.in = append([]inText{bytes.NewBuffer(dat)}, l.in...)
	l.saddr = append([]Addr{l.Addr}, l.saddr...)
	l.Addr = Addr{what, 1}
}
Пример #6
0
// runs here
func runCd(x *xEnv, argv ...string) error {
	opts := opt.New("")
	app.Dprintf("cd %v\n", argv)
	args, err := opts.Parse(argv)
	if err != nil {
		opts.Usage()
		return dbg.ErrUsage
	}
	if len(args) != 0 {
		opts.Usage()
		return dbg.ErrUsage
	}
	m, ok := <-app.In()
	if !ok {
		err = cerror(app.In())
		app.Warn("%s", err)
		return err
	}
	d, ok := m.(zx.Dir)
	if !ok {
		b, ok := m.([]byte)
		if !ok {
			app.Warn("%s", dbg.ErrNotDir)
			return dbg.ErrNotDir
		}
		s := string(b)
		d, err = nsutil.Stat(s)
		if err != nil {
			app.Warn("%s: %s", s, err)
			return err
		}
	}
	if d["type"] != "d" {
		app.Warn("%s: %s", d["path"], dbg.ErrNotDir)
		return dbg.ErrNotDir
	}
	app.Cd(d["path"])
	out := app.Out()
	if out != nil {
		out <- d
	}
	return nil
}
Пример #7
0
func main() {
	defer app.Exiting()
	os.Args[0] = "auth"
	app.New()
	dfltdir := auth.KeyDir()
	dir = dfltdir
	opts.NewFlag("d", "adir: clive auth dir", &dir)
	opts.NewFlag("f", "force write of key file when file already exists", &force)
	args, err := opts.Parse(os.Args)
	if err != nil {
		app.Warn("%s", err)
		opts.Usage()
		app.Exits(err)
	}
	if len(args) < 3 {
		opts.Usage()
		app.Exits("usage")
	}
	name, user, secret := args[0], args[1], args[2]
	groups := args[3:]
	file := auth.KeyFile(dir, name)
	fi, _ := os.Stat(file)
	if fi != nil && !force {
		app.Fatal("key file already exists")
	}
	err = auth.SaveKey(dir, name, user, secret, groups...)
	if err != nil {
		app.Fatal("%s: %s", file, err)
	}
	ks, err := auth.LoadKey(dir, name)
	if err != nil {
		app.Fatal("can't load key: %s", err)
	}
	for _, k := range ks {
		if k.Uid == user {
			app.Warn("%s", file)
			return
		}
	}
	app.Fatal("bad user")
}
Пример #8
0
func (t *Fs) getdir(f File, d zx.Dir, off, count int64, c chan<- []byte) error {
	gf, ok := f.(Walker)
	if !ok {
		return nil
	}
	ns, err := gf.Getdir()
	if err != nil {
		return err
	}
	sort.Sort(sort.StringSlice(ns))
	if d["name"] == "/" {
		ns = append([]string{"Ctl"}, ns...)
	}
Dloop:	for _, n := range ns {
		if n == "" {
			err = fmt.Errorf("%s: empty name in getdir", d["path"], n)
			app.Warn("fs bug: %s", err)
			return err
		}
		if off > 0 {
			off--
			continue
		}
		switch count {
		case zx.All:
			break
		case 0:
			break Dloop
		default:
			count--
		}
		if d["name"]=="/" && n == "Ctl" {
			cd := ctldir.Dup()
			cd["tpath"] = t.path
			cd.Send(c)
			continue
		}
		cf, err := gf.Walk(n)
		if err != nil {
			return err
		}
		cp := zx.Path(d["path"], n)
		cd, err := t.statf(cf, cp)
		if err != nil {
			return err
		}
		t.Dprintf("getdir %s: %s\n", gf, cf)
		if _, err := cd.Send(c); err != nil {
			return err
		}
	}
	return nil
}
Пример #9
0
func main() {
	defer app.Exiting()
	app.New()
	opts.NewFlag("p", "port: port used (8080 by default)", &port)
	args, err := opts.Parse(os.Args)
	if err != nil {
		app.Warn("%s", err)
		opts.Usage()
		app.Exits(err)
	}
	switch len(args) {
	case 0:
	case 1:
		dir = args[0]
	default:
		app.Warn("too many arguments")
		opts.Usage()
		app.Exits("usage")
	}
	err = http.ListenAndServe(":"+port, http.FileServer(http.Dir(dir)))
	app.Exits(err)
}
Пример #10
0
func pspdf(t *Text, wid int, out io.Writer, cline, outfig string) {
	// pipe the roff writer into a command to output ps and pdf
	cmd := exec.Command("sh", "-c", cline)
	cmd.Stdout = out
	stdin, err := cmd.StdinPipe()
	if err != nil {
		app.Fatal("pipe to sh: %s", err)
	}
	if err := cmd.Start(); err != nil {
		app.Fatal("pipe to sh: %s", err)
	}
	wrroff(t, wid, stdin, outfig)
	stdin.Close()
	if err := cmd.Wait(); err != nil {
		app.Warn("pspdf: sh: %s", err)
	}
}
Пример #11
0
// runs here
func runNew(x *xEnv, argv ...string) error {
	opts := opt.New("dup")
	app.Dprintf("dup %v\n", argv)
	args, err := opts.Parse(argv)
	if err != nil {
		opts.Usage()
		return dbg.ErrUsage
	}
	for _, arg := range args {
		switch arg {
		case "ns":
			app.NewNS(nil)
		case "io":
			app.NewIO(nil)
		case "env":
			app.NewEnv(nil)
		case "dot":
			app.NewDot("")
		default:
			app.Warn("unknown resource '%s'", arg)
		}
	}
	return nil
}
Пример #12
0
func (l *lex) Error(s string) {
	l.nerrors++
	app.Warn("%s: %s", l.Addr, s)
}
Пример #13
0
func main() {
	defer app.Exiting()
	os.Args[0] = "Q"
	c := app.New()
	opts.NewFlag("c", "ignored for compatibility", &dummy)
	opts.NewFlag("D", "debug", &c.Debug)
	edir := dbg.Usr
	opts.NewFlag("e", "env: qlfs environment name (defaults to uid)", &edir)
	opts.NewFlag("q", "qldir: qlfs root dir (defaults to /n/ql)", &ql)
	args, err := opts.Parse(os.Args)
	if err != nil {
		app.Warn("%s", err)
		opts.Usage()
		app.Exits(err)
	}
	cmd := ""
	if len(args) == 0 {
		app.Warn("no command given")
		opts.Usage()
		app.Exits(err)
	}
	cmd = strings.Join(args, " ")
	app.Dprintf("run %s\n", cmd)
	_, err = os.Stat(path.Join(ql, "Ctl"))
	if err != nil {
		app.Fatal("qlfs: %s", err)
	}
	env := path.Join(ql, edir)
	_, err = os.Stat(env)
	if err != nil {
		err = os.Mkdir(env, 0775)
	}
	if err != nil {
		app.Fatal("%s", err)
	}

	cdir := fmt.Sprintf("%s/%d", env, os.Getpid())
	if err := os.Mkdir(cdir, 0755); err != nil {
		app.Fatal("%s", err)
	}
	var b bytes.Buffer
	io.Copy(&b, os.Stdin)
	if err := ioutil.WriteFile(path.Join(cdir, "in"), b.Bytes(), 0644); err != nil {
		os.RemoveAll(cdir)
		app.Fatal("writing in: %s", err)
	}
	if err := ioutil.WriteFile(path.Join(cdir, "cmd"), []byte(cmd), 0644); err != nil {
		os.RemoveAll(cdir)
		app.Fatal("writing cmd: %s", err)
	}
	oc := make(chan bool, 2)
	pfd, err := os.Open(path.Join(cdir, "pout"))
	if err != nil {
		app.Warn("out: %s", err)
		oc <- true
	} else {
		go func() {
			io.Copy(os.Stdout, pfd)
			pfd.Close()
			oc <- true
		}()
	}
	efd, err := os.Open(path.Join(cdir, "perr"))
	if err != nil {
		app.Warn("err: %s", err)
		oc <- true
	} else {
		go func() {
			io.Copy(os.Stderr, efd)
			efd.Close()
			oc <- true
		}()
	}
	<-oc
	<-oc
	sts, err := ioutil.ReadFile(path.Join(cdir, "wait"))
	os.RemoveAll(cdir)
	if err != nil {
		app.Exits(err)
	}
	s := string(sts)
	if s == "success" {
		s = ""
	}
	app.Exits(s)
}