Exemplo n.º 1
0
func grep(re *regexp.Regexp, path string, r io.Reader) error {
	in := bufio.NewReader(r)
	lineNo := 0
	for {
		switch line, prefix, err := in.ReadLine(); {
		case prefix:
			return errors.New("Line is too long")
		case err == io.EOF:
			return nil
		case err != nil:
			return err
		default:
			lineNo++
			match := re.Match(line)
			if (match && !*vFlag) || (!match && *vFlag) {
				if *nFlag && path != "" {
					os.Stdout.WriteString(path + ":")
				}
				if *nFlag {
					fmt.Print(lineNo, ":")
				}
				os.Stdout.WriteString(string(line) + "\n")
			}
		}
	}
	panic("Unreachable")
}
Exemplo n.º 2
0
func ReaderWaitFor(r io.Reader, re *regexp.Regexp, duration time.Duration) ([]byte, bool, error) {
	res := make(chan *readWaitResult, 1)
	quit := make(chan bool, 1)
	go func() {
		out := &bytes.Buffer{}
		var err error
		found := false
		var n int
		buf := make([]byte, 1024)
		for err == nil && !found {
			select {
			case <-quit:
				break
			default:
				n, err = r.Read(buf)
				if n > 0 {
					out.Write(buf[0:n])
				}
				found = re.Match(out.Bytes())
			}
		}
		res <- &readWaitResult{out.Bytes(), found, err}
	}()
	select {
	case result := <-res:
		return result.byts, result.found, result.err
	case <-time.After(duration):
		quit <- true
		return nil, false, ErrTimeout
	}
}
Exemplo n.º 3
0
func grep(rx *regexp.Regexp, r io.Reader) bool {
	matched := false
	s := bufio.NewScanner(r)
	for {
		if !s.Scan() {
			break
		}
		b := s.Bytes()

		m := rx.Match(b)
		if invert && !m {
			matched = true
			if _, err := os.Stdout.Write(b); err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
			if _, err := fmt.Fprintln(os.Stdout); err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
		} else if !invert && m {
			matched = true
			if _, err := os.Stdout.Write(b); err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
			if _, err := fmt.Fprintln(os.Stdout); err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
		}
	}
	return matched
}
Exemplo n.º 4
0
// 载入所有Page
func LoadPages(root string, exclude string) (pages map[string]Mapper, err error) {
	pages = make(map[string]Mapper)
	err = nil
	var _exclude *regexp.Regexp
	if exclude != "" {
		_exclude, err = regexp.Compile(exclude)
		if err != nil {
			errMsg := fmt.Sprintf("BAD pages exclude regexp %q >> ", exclude, err.Error())
			err = errors.New(errMsg)
			return
		}
	}
	err = filepath.Walk(filepath.Join(root, "pages/"), func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() || strings.HasPrefix(filepath.Base(path), ".") {
			return nil
		}
		if _exclude != nil && _exclude.Match([]byte(path[len(root+"pages/"):])) {
			return nil
		}
		page, err := LoadPage(root, path)
		if err != nil {
			return err
		}
		pages[page.Id()] = page
		return nil
	})
	return
}
Exemplo n.º 5
0
func read(name string, wg *sync.WaitGroup, reg *regexp.Regexp) {
	defer wg.Done()
	f, err := os.Open(name)
	if err != nil {
		errlog.Println(err)
		return
	}
	defer f.Close()
	var r io.Reader
	ext := filepath.Ext(name)
	switch ext {
	case ".gz":
		r, err = gzip.NewReader(f)
		if err != nil {
			log.Println(err)
			return
		}
	case ".bz":
		r = bzip2.NewReader(f)
	default:
		errlog.Println("Unknown extension:", ext)
		return
	}
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		b := scanner.Bytes()
		if reg.Match(b) {
			log.Printf("%s: %s", name, b)
		}
	}
	if err := scanner.Err(); err != nil {
		errlog.Printf("Error while reading %s: %s", name, err)
	}
}
Exemplo n.º 6
0
Arquivo: payload.go Projeto: 6wei/gor
// 载入所有的Post
func LoadPosts(root string, exclude string) (posts map[string]Mapper, err error) {
	posts = make(map[string]Mapper)
	err = nil
	var _exclude *regexp.Regexp
	if exclude != "" {
		_exclude, err = regexp.Compile(exclude)
		if err != nil {
			err = errors.New("BAD pages exclude regexp : " + exclude + "\t" + err.Error())
			return
		}
	}
	err = filepath.Walk(root+"posts/", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.IsDir() || strings.HasPrefix(filepath.Base(path), ".") {
			return nil
		}
		if _exclude != nil && _exclude.Match([]byte(path[len(root+"posts/"):])) {
			return nil
		}
		post, err := LoadPost(root, path)
		if err != nil {
			return err
		}
		posts[post.Id()] = post
		return nil
	})
	return
}
Exemplo n.º 7
0
func (collection *exampleCollection) applyRegExpFocus(focusFilter *regexp.Regexp) {
	for _, example := range collection.examples {
		if !focusFilter.Match([]byte(example.concatenatedString())) {
			example.skip()
		}
	}
}
Exemplo n.º 8
0
func ValidateCSVData(fn string, re *regexp.Regexp) (err error) {
	fin, err := os.Open(fn)
	if err != nil {
		// do not return the error, the file might be not needed
		return nil
	}
	defer fin.Close()
	r := bufio.NewReader(fin)
	line_number := 1
	for {
		line, truncated, err := r.ReadLine()
		if err != nil {
			break
		}
		if truncated {
			return errors.New("line too long")
		}
		// skip the header line
		if line_number > 1 {
			if !re.Match(line) {
				return errors.New(fmt.Sprintf("%s: error on line %d: %s", fn, line_number, line))
			}
		}
		line_number++
	}
	return
}
Exemplo n.º 9
0
// find the directories and files which matches the regular expression
func findRecursively(dir *os.File, reg *regexp.Regexp, currentwd string) (dirs, files []string) {
	dirs = make([]string, 0)
	files = make([]string, 0)

	fileInfos, err := dir.Readdir(0)
	if err != nil {
		return
	}
	for i := 0; i < len(fileInfos); i++ {
		name := fileInfos[i].Name()
		if reg.Match([]byte(name)) == true {
			if fileInfos[i].IsDir() == true {
				dirs = append(dirs, currentwd+"/"+name)
			} else {
				files = append(dirs, currentwd+"/"+name)
			}
		}
		if fileInfos[i].IsDir() == true {
			redir, err := os.Open(currentwd + "/" + name)
			if err != nil {
				continue
			}
			newDirs, newFiles := findRecursively(redir, reg, currentwd+"/"+name)
			dirs = append(dirs, newDirs...)
			files = append(files, newFiles...)
			redir.Close()
		}
	}
	return
}
Exemplo n.º 10
0
func (expect *ExpectSubprocess) ExpectRegex(re *regexp.Regexp) (buf []byte, err error) {
	join := make([][]byte, 0, 1024)
	chunk := make([]byte, 255)
	line := make([]byte, 0, 1024)

	var n int
	for {
		n, err = expect.f.Read(chunk)
		if err != nil {
			return
		}

		for i := 0; i < n; i++ {
			if chunk[i] != '\n' {
				line = append(line, chunk[i])
			}
			success := re.Match(line)
			if success {
				buf = flatten(join)
				return
			}
			if chunk[i] == '\n' {
				line = append(line, chunk[i])
				join = append(join, line)
				line = make([]byte, 0, 1024)
			}
		}

	}
}
Exemplo n.º 11
0
// Shell worker.
func shell(check *Check, name *string, sleep *time.Duration) {
	// Execute with shell in N attemps.
	var out []byte
	var err error
	for i := 0; i < check.Tries; {
		out, err = exec.Command("sh", "-c", check.Shell).CombinedOutput()
		if err == nil {
			if check.Match != "" { // Match regexp.
				var regex *regexp.Regexp
				regex, err = regexp.Compile(check.Match)
				if err == nil && !regex.Match(out) {
					err = errors.New("Expected:\n" + check.Match + "\n\nGot:\n" + string(out))
				}
			}
			break
		}
		i++
		if i < check.Tries {
			time.Sleep(*sleep)
		}
	}
	// Process results.
	if err == nil {
		if check.Failed {
			ts := time.Now()
			mutex.Lock()
			check.Failed = false
			check.Since = ts.Format(time.RFC3339)
			modified = etag(ts)
			mutex.Unlock()
			subject := "Fixed: " + *name
			log(5, subject)
			if check.Notify != "" {
				notify(check, &subject, nil)
			}
			if check.Alert != "" {
				alert(check, name, nil, false)
			}
		}
	} else {
		if !check.Failed {
			ts := time.Now()
			mutex.Lock()
			check.Failed = true
			check.Since = ts.Format(time.RFC3339)
			modified = etag(ts)
			mutex.Unlock()
			msg := string(out) + err.Error()
			subject := "Failed: " + *name
			log(5, subject+"\n"+msg)
			if check.Notify != "" {
				notify(check, &subject, &msg)
			}
			if check.Alert != "" {
				alert(check, name, &msg, true)
			}
		}
	}
}
Exemplo n.º 12
0
Arquivo: main.go Projeto: rsc/go-misc
func grepFailures(revs []*Revision, re *regexp.Regexp) []*failure {
	return processFailureLogs(revs, func(build *Build, data []byte) []*failure {
		if !re.Match(data) {
			return nil
		}
		return []*failure{new(failure)}
	})
}
Exemplo n.º 13
0
// Find takes a compiled regexp and starting at the top of the file
// returns the line number of all lines that match.
func (traveler *FileTraveler) Find(rx *regexp.Regexp) (matches []int) {
	traveler.ForEach(func(pos int, line []byte) {
		if rx.Match(line) {
			matches = append(matches, pos)
		}
	})
	return
}
Exemplo n.º 14
0
//ConstraintRegexp limits request item by regexp
func ConstraintRegexp(item string, reg *regexp.Regexp) func(map[string]string) bool {
	return func(m map[string]string) bool {
		if value, ok := m[item]; ok {
			return reg.Match([]byte(value))
		}
		return false
	}
}
Exemplo n.º 15
0
// ResponseMatchesGeneral is an abstraction of ResponseMatches and
// ResponseMatchesInsecure that simply varies in the security of the connection
func ResponseMatchesGeneral(urlstr string, re *regexp.Regexp, secure bool) (int, string, error) {
	body := chkutil.URLToBytes(urlstr, secure)
	if re.Match(body) {
		return errutil.Success()
	}
	msg := "Response didn't match regexp"
	return errutil.GenericError(msg, re.String(), []string{string(body)})
}
Exemplo n.º 16
0
Arquivo: rollbar.go Projeto: cyx/busl
// filterParams filters sensitive information like passwords from being sent to
// Rollbar.
func filterParams(pattern *regexp.Regexp, values map[string][]string) map[string][]string {
	for key, _ := range values {
		if pattern.Match([]byte(key)) {
			values[key] = []string{FILTERED}
		}
	}

	return values
}
Exemplo n.º 17
0
func filterVariables(vars []api.Variable, filter *regexp.Regexp) []string {
	data := make([]string, 0, len(vars))
	for _, v := range vars {
		if filter == nil || filter.Match([]byte(v.Name)) {
			data = append(data, fmt.Sprintf("%s = %s", v.Name, v.Value))
		}
	}
	return data
}
Exemplo n.º 18
0
func list(context *config.Context, p string, hidden bool, ignore *regexp.Regexp) (fileChan chan *File, err error) {
	absPath := context.AbsPathOf(p)
	var f []os.FileInfo
	f, err = ioutil.ReadDir(absPath)
	fileChan = make(chan *File)
	if err != nil {
		close(fileChan)
		return
	}

	go func() {
		for _, file := range f {
			fileName := file.Name()
			if fileName == config.GDDirSuffix {
				continue
			}
			if ignore != nil && ignore.Match([]byte(fileName)) {
				continue
			}
			if isHidden(fileName, hidden) {
				continue
			}

			resPath := gopath.Join(absPath, fileName)

			// TODO: (@odeke-em) decide on how to deal with isFifo
			if namedPipe(file.Mode()) {
				fmt.Fprintf(os.Stderr, "%s (%s) is a named pipe, not reading from it\n", p, resPath)
				continue
			}

			if !symlink(file.Mode()) {
				fileChan <- NewLocalFile(resPath, file)
			} else {
				var symResolvPath string
				symResolvPath, err = filepath.EvalSymlinks(resPath)
				if err != nil {
					continue
				}

				var symInfo os.FileInfo
				symInfo, err = os.Stat(symResolvPath)
				if err != nil {
					continue
				}

				lf := NewLocalFile(symResolvPath, symInfo)
				// Retain the original name as appeared in
				// the manifest instead of the resolved one
				lf.Name = fileName
				fileChan <- lf
			}
		}
		close(fileChan)
	}()
	return
}
Exemplo n.º 19
0
Arquivo: scan.go Projeto: lunny/nodb
func (db *DB) scan(dataType byte, key []byte, count int, inclusive bool, match string) ([][]byte, error) {
	var minKey, maxKey []byte
	var err error
	var r *regexp.Regexp

	if len(match) > 0 {
		if r, err = regexp.Compile(match); err != nil {
			return nil, err
		}
	}

	if len(key) > 0 {
		if err = checkKeySize(key); err != nil {
			return nil, err
		}
		if minKey, err = db.encodeMetaKey(dataType, key); err != nil {
			return nil, err
		}

	} else {
		if minKey, err = db.encodeMinKey(dataType); err != nil {
			return nil, err
		}
	}

	if maxKey, err = db.encodeMaxKey(dataType); err != nil {
		return nil, err
	}

	if count <= 0 {
		count = defaultScanCount
	}

	v := make([][]byte, 0, count)

	it := db.bucket.NewIterator()
	it.Seek(minKey)

	if !inclusive {
		if it.Valid() && bytes.Equal(it.RawKey(), minKey) {
			it.Next()
		}
	}

	for i := 0; it.Valid() && i < count && bytes.Compare(it.RawKey(), maxKey) < 0; it.Next() {
		if k, err := db.decodeMetaKey(dataType, it.Key()); err != nil {
			continue
		} else if r != nil && !r.Match(k) {
			continue
		} else {
			v = append(v, k)
			i++
		}
	}
	it.Close()
	return v, nil
}
Exemplo n.º 20
0
func genPatternMatcher(
	regex *regexp.Regexp,
	sel func(last, current []byte) []byte,
) (matcher, error) {
	matcher := func(last, current []byte) bool {
		line := sel(last, current)
		return regex.Match(line)
	}
	return matcher, nil
}
Exemplo n.º 21
0
Arquivo: uni.go Projeto: gaal/shstat
func scan(re *regexp.Regexp) {
	s := &ucd.Scanner{}
	for ; !s.Done(); s.Next() {
		rec := s.Record()
		r := rec.Rune()
		nam := rec.Name()
		if re.Match(nam) {
			fmt.Printf("U+%04X [%c] %s\n", r, r, nam)
		}
	}
}
Exemplo n.º 22
0
func anyMatch(pat *regexp.Regexp, args ...string) bool {
	if pat == nil {
		return false
	}

	for _, arg := range args {
		if pat.Match([]byte(arg)) {
			return true
		}
	}
	return false
}
Exemplo n.º 23
0
// 判断val是否能正确匹配exp中的正则表达式。
// val可以是[]byte, []rune, string类型。
func isMatch(exp *regexp.Regexp, val interface{}) bool {
	switch v := val.(type) {
	case []rune:
		return exp.MatchString(string(v))
	case []byte:
		return exp.Match(v)
	case string:
		return exp.MatchString(v)
	default:
		return false
	}
}
Exemplo n.º 24
0
func (msg *LogMsg) hasPattern(arg interface{}) bool {
	var re *regexp.Regexp
	var err error
	if s, ok := arg.(string); ok {
		if re, err = regexp.Compile(s); err != nil {
			log.Fatal(err)
		}
	} else if re, ok = arg.(*regexp.Regexp); !ok {
		log.Fatalf("unexpected arg %T %v\n", arg, arg)
	}
	return re.Match([]byte(msg.msg))
}
Exemplo n.º 25
0
func isMatched(v interface{}, with *regexp.Regexp) bool {
	switch v.(type) {
	case string:
		if with.MatchString(v.(string)) {
			return true
		}
	case []byte:
		if with.Match(v.([]byte)) {
			return true
		}
	}
	return false
}
Exemplo n.º 26
0
func matchesFilters(raw []byte, match *regexp.Regexp, exclude *regexp.Regexp) bool {
	matches := true

	if match != nil {
		matches = match.Match(raw)
	}

	if exclude != nil {
		matches = matches && !exclude.Match(raw)
	}

	return matches
}
Exemplo n.º 27
0
Arquivo: main.go Projeto: bjarneh/cmd
func addRegexpFilter() {

	var reg *regexp.Regexp

	if posix {
		reg = regexp.MustCompilePOSIX(pattern)
	} else {
		reg = regexp.MustCompile(pattern)
	}

	matchOK = func(s string) bool {
		b := slurpStripNullByte(s)
		return reg.Match(b)
	}
}
Exemplo n.º 28
0
func requestOutputInterpreter(result []byte, find *regexp.Regexp, ssl bool, sslwork bool) int {
	result_s := string(result)
	if strings.Contains(result_s, "HTTP/1.1 200") || strings.Contains(result_s, "HTTP/1.1 30") {
		if find.Match(result) {
			return 1
		} else {
			if strings.Contains(result_s, "HTTP/1.1 200") {
				return 200
			} else {
				return 300
			}
		}
	}
	return -1
}
Exemplo n.º 29
0
func (inp *Input) ValidateMatchRegexp(re *regexp.Regexp) {
	inp.Validators = append(inp.Validators, func(inp *Input, r *http.Request) bool {
		name := inp.Name
		value, ok := r.Form[name]
		ret := false
		if ok {
			if 0 != len(value) && re.Match([]byte(value[0])) {
				ret = true
			}
		}
		if !ret {
			inp.Errors = append(inp.Errors, fmt.Sprintf("%s is not valid", inp.Label))
		}
		return ret
	})
}
Exemplo n.º 30
0
// Test result from server against regexp
func expectResult(t tLogger, c net.Conn, re *regexp.Regexp) []byte {
	// Wait for commands to be processed and results queued for read
	c.SetReadDeadline(time.Now().Add(1 * time.Second))
	n, err := c.Read(expBuf)
	c.SetReadDeadline(time.Time{})

	if n <= 0 && err != nil {
		stackFatalf(t, "Error reading from conn: %v\n", err)
	}
	buf := expBuf[:n]

	if !re.Match(buf) {
		stackFatalf(t, "Response did not match expected: \n\tReceived:'%q'\n\tExpected:'%s'\n", buf, re)
	}
	return buf
}