Exemple #1
0
static void	load_regexes_from_file(const char *filename)
{
	char *line;
	FILE *f = xfopen(filename, "r");
	while ((line = get_line_from_file(f)) != NULL) {
		chomp(line);
		add_regex(line);
		free(line);
	}
}
Exemple #2
0
int MOD_CLS_NAME::preload() {
   AmConfigReader cfg;
   if(cfg.loadPluginConf(MOD_NAME)) {
     INFO("no module configuration for '%s' found, not preloading regular expressions\n",
	  MOD_NAME);
     return 0;
   }

   bool failed = false;
   for (std::map<string,string>::const_iterator it =
	  cfg.begin(); it != cfg.end(); it++) {
     if (add_regex(it->first, it->second)) {
       ERROR("compiling regex '%s' for '%s'\n",
	     it->second.c_str(), it->first.c_str());
       failed = true;
     } else {
       DBG("compiled regex '%s' as '%s'\n", it->second.c_str(), it->first.c_str());
     }
   }

   return failed? -1 : 0;
}
Exemple #3
0
extern int grep_main(int argc, char **argv)
{
	int opt;
#if defined BB_FEATURE_GREP_CONTEXT || defined BB_FEATURE_GREP_EGREP_ALIAS
	char *junk;
#endif

#ifdef BB_FEATURE_CLEAN_UP
	/* destroy command strings on exit */
	atexit(destroy_regexes);
#endif

#ifdef BB_FEATURE_GREP_EGREP_ALIAS
	junk = get_last_path_component(argv[0]);
	if (junk && strcmp(junk, "egrep") == 0)
		reflags |= REG_EXTENDED;
#endif

	/* do normal option parsing */
	while ((opt = getopt(argc, argv, "iHhlnqvsce:f:"
#ifdef BB_FEATURE_GREP_CONTEXT
"A:B:C:"
#endif
#ifdef BB_FEATURE_GREP_EGREP_ALIAS
"E"
#endif
)) > 0) {
		switch (opt) {
			case 'i':
				ignore_case++;
				break;
			case 'l':
				print_files_with_matches++;
				break;
			case 'H':
				print_filename++;
				break;
			case 'h':
				print_filename--;
				break;
			case 'n':
				print_line_num++;
				break;
			case 'q':
				be_quiet++;
				break;
			case 'v':
				invert_search++;
				break;
			case 's':
				suppress_err_msgs++;
				break;
			case 'c':
				print_match_counts++;
				break;
			case 'e':
				add_regex(optarg);
				break;
#ifdef BB_FEATURE_GREP_EGREP_ALIAS
			case 'E':
				reflags |= REG_EXTENDED;
				break;
#endif
			case 'f':
				load_regexes_from_file(optarg);
				break;
#ifdef BB_FEATURE_GREP_CONTEXT
			case 'A':
				lines_after = strtoul(optarg, &junk, 10);
				if(*junk != '\0')
					error_msg_and_die("invalid context length argument");
				break;
			case 'B':
				lines_before = strtoul(optarg, &junk, 10);
				if(*junk != '\0')
					error_msg_and_die("invalid context length argument");
				before_buf = (char **)xcalloc(lines_before, sizeof(char *));
				break;
			case 'C':
				lines_after = lines_before = strtoul(optarg, &junk, 10);
				if(*junk != '\0')
					error_msg_and_die("invalid context length argument");
				before_buf = (char **)xcalloc(lines_before, sizeof(char *));
				break;
#endif /* BB_FEATURE_GREP_CONTEXT */
			default:
				show_usage();
		}
	}

	/* if we didn't get a pattern from a -e and no command file was specified,
	 * argv[optind] should be the pattern. no pattern, no worky */
	if (nregexes == 0) {
		if (argv[optind] == NULL)
			show_usage();
		else {
			add_regex(argv[optind]);
			optind++;
		}
	}

	/* sanity checks */
	if (print_match_counts || be_quiet || print_files_with_matches) {
		print_line_num = 0;
#ifdef BB_FEATURE_GREP_CONTEXT
		lines_before = 0;
		lines_after = 0;
#endif
	}

	/* argv[(optind)..(argc-1)] should be names of file to grep through. If
	 * there is more than one file to grep, we will print the filenames */
	if ((argc-1) - (optind) > 0)
		print_filename++;

	/* If no files were specified, or '-' was specified, take input from
	 * stdin. Otherwise, we grep through all the files specified. */
	if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
		grep_file(stdin);
	}
	else {
		int i;
		FILE *file;
		for (i = optind; i < argc; i++) {
			cur_file = argv[i];
			file = fopen(cur_file, "r");
			if (file == NULL) {
				if (!suppress_err_msgs)
					perror_msg("%s", cur_file);
			}
			else {
				grep_file(file);
				fclose(file);
			}
		}
	}

	return !matched; /* invert return value 0 = success, 1 = failed */
}