func check() error { verbose := flag.Bool("v", false, "print rejected rules") dump := flag.Bool("dump", false, "print parsed rules") flag.Parse() args := flag.Args() if len(args) != 1 { return fmt.Errorf("one input rule file expected") } fp, err := os.Open(args[0]) if err != nil { return err } defer fp.Close() ok := true rules := adblock.NewMatcher() scanner := bufio.NewScanner(fp) for scanner.Scan() { rule, err := adblock.ParseRule(scanner.Text()) if err != nil { fmt.Fprintf(os.Stderr, "error: could not parse rule:\n %s\n %s\n", scanner.Text(), err) ok = false continue } if rule == nil { continue } err = rules.AddRule(rule, 0) if *verbose && err != nil { fmt.Fprintf(os.Stderr, "error: could not add rule:\n %s\n %s\n", scanner.Text(), err) ok = false } } if !ok { return fmt.Errorf("some rules could not be parsed") } if *dump { fmt.Printf("%s\n", rules) } return nil }
// Add the rules in supplied reader to the matcher. Returns the list of added // rules (for debugging or tracing purposes) and the total number of read rules. // Some rules could not have been parsed. func buildOne(r io.Reader, matcher *adblock.RuleMatcher) ([]string, int, error) { read := 0 rules := []string{} scanner := bufio.NewScanner(r) for scanner.Scan() { s := scanner.Text() rule, err := adblock.ParseRule(s) if err != nil { log.Printf("error: could not parse rule:\n %s\n %s\n", scanner.Text(), err) continue } if rule == nil { continue } err = matcher.AddRule(rule, len(rules)) read += 1 if err == nil { rules = append(rules, s) } } return rules, read, scanner.Err() }