func Shell(call []string) error { var in *common.BufferedReader interactive := true if len(call) > 2 { call = call[0:1] } if len(call) == 2 { f, err := os.Open(call[1]) if err != nil { return err } defer f.Close() in = common.NewBufferedReader(f) interactive = false } else { in = common.NewBufferedReader(os.Stdin) } var err error var line string for err == nil { if interactive { fmt.Print("> ") } line, err = in.ReadWholeLine() if err != nil { return err } if isComment(line) { continue } params, ce := common.Parameterize(line) if ce != nil { common.DumpError(ce) continue } ce = execute(params) if ce != nil { common.DumpError(ce) continue } } return nil }
func dumpFile(path string) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() r := common.NewBufferedReader(f) line, err := r.ReadWholeLine() for i := uint(0); i < *numLines && err == nil; i++ { fmt.Println(line) line, err = r.ReadWholeLine() } if err == io.EOF { err = nil } return err }
func doGrep(pattern *regexp.Regexp, fh io.Reader, fn string, print_fn bool) { buf := common.NewBufferedReader(fh) for { line, err := buf.ReadWholeLine() if err != nil { fmt.Fprintf(os.Stderr, "Error while reading from %s: %v\n", fn, err) return } if line == "" { break } if pattern.MatchString(line) { if print_fn { fmt.Printf("%s:", fn) } fmt.Printf("%s\n", line) } } }