Example #1
0
/* Read the regular expressions under the <iso> node.  libosinfo
 * itself uses the glib function 'g_regex_match_simple'.  That appears
 * to implement PCRE, however I have not checked in detail.
 */
static int
read_iso_node (guestfs_h *g, xmlNodePtr iso_node, struct osinfo *osinfo)
{
  xmlNodePtr child;

  for (child = iso_node->children; child; child = child->next) {
    if (STREQ ((const char *) child->name, "system-id")) {
      if (compile_re (g, child, &osinfo->re_system_id) == -1)
        return -1;
    }
    else if (STREQ ((const char *) child->name, "volume-id")) {
      if (compile_re (g, child, &osinfo->re_volume_id) == -1)
        return -1;
    }
    else if (STREQ ((const char *) child->name, "publisher-id")) {
      if (compile_re (g, child, &osinfo->re_publisher_id) == -1)
        return -1;
    }
    else if (STREQ ((const char *) child->name, "application-id")) {
      if (compile_re (g, child, &osinfo->re_application_id) == -1)
        return -1;
    }
  }

  return 0;
}
Example #2
0
/* * * * * * * * * * * * *
 *
 *  run_compiles   run all the regexp compiles for all the fixes once.
 */
void
run_compiles (void)
{
  tFixDesc *p_fixd = fixDescList;
  int fix_ct = FIX_COUNT;
  regex_t *p_re = XCNEWVEC (regex_t, REGEX_COUNT);

  /*  Make sure compile_re does not stumble across invalid data */

  memset (&incl_quote_re, '\0', sizeof (regex_t));

  compile_re (incl_quote_pat, &incl_quote_re, 1,
              "quoted include", "run_compiles");

  /*  Allow machine name tests to be ignored (testing, mainly) */

  if (pz_machine && ((*pz_machine == '\0') || (*pz_machine == '*')))
    pz_machine = (char*)NULL;

  /* FOR every fixup, ...  */
  do
    {
      tTestDesc *p_test = p_fixd->p_test_desc;
      int test_ct = p_fixd->test_ct;

      /*  IF the machine type pointer is not NULL (we are not in test mode)
             AND this test is for or not done on particular machines
          THEN ...   */

      if (  (pz_machine != NULL)
         && (p_fixd->papz_machs != (const char**) NULL)
         && ! machine_matches (p_fixd) )
        continue;

      /* FOR every test for the fixup, ...  */

      while (--test_ct >= 0)
        {
          switch (p_test->type)
            {
            case TT_EGREP:
            case TT_NEGREP:
              p_test->p_test_regex = p_re++;
              compile_re (p_test->pz_test_text, p_test->p_test_regex, 0,
                          "select test", p_fixd->fix_name);
            default: break;
            }
          p_test++;
        }
    }
  while (p_fixd++, --fix_ct > 0);
}
Example #3
0
void duplicate_es_array(strip_t *pes_in, int n_esin, strip_t **pes_out, int *n_esout)
{
	int loop, old_n = *n_esout;

	assert(n_esin >= 0);

	*n_esout += n_esin;
	*pes_out = (strip_t *)myrealloc(*pes_out, (*n_esout) * sizeof(strip_t));

	for(loop=0; loop<n_esin; loop++)
	{
		memcpy(&(*pes_out)[loop + old_n], &pes_in[loop], sizeof(strip_t));

		memset(&(*pes_out)[loop + old_n].regex, 0x00, sizeof(regex_t));
		if (pes_in[loop].type != STRIP_TYPE_COLUMN && pes_in[loop].type != STRIP_TYPE_RANGE)
		{
			(*pes_out)[loop + old_n].regex_str = mystrdup(pes_in[loop].regex_str);

			compile_re(&(*pes_out)[loop + old_n].regex, (*pes_out)[loop + old_n].regex_str);
		}

		if (pes_in[loop].del)
			(*pes_out)[loop + old_n].del = mystrdup(pes_in[loop].del);
	}
}
Example #4
0
/*=gfunc string_eqv_match_p
 *
 * what:   caseless regex match
 * general_use:
 *
 * exparg: text, text to test for pattern
 * exparg: match, pattern/substring to search for
 *
 * string: "~"
 *
 * doc:  Test to see if a string fully matches a pattern.
 *       Case is not significant, but any character equivalences
 *       must be expressed in your regular expression.
=*/
static tSuccess
Select_Match_Full(char const * sample, char const * pattern)
{
    regmatch_t m[2];

    /*
     *  On the first call for this macro, compile the expression
     */
    if (pCurMacro->funcPrivate == NULL) {
        void *    mat = (void *)pattern;
        regex_t*  pRe = AGALOC(sizeof(*pRe), "select match full re");

        if (OPT_VALUE_TRACE > TRACE_EXPRESSIONS) {
            fprintf(pfTrace, "Compiling ``%s'' with bits 0x%lX\n",
                     pattern, pCurMacro->res);
        }
        compile_re(pRe, mat, (int)pCurMacro->res);
        pCurMacro->funcPrivate = pRe;
    }

    if (regexec((regex_t*)pCurMacro->funcPrivate, sample, (size_t)2, m, 0)
        != 0)
        return FAILURE;

    if (  (m[0].rm_eo != strlen( sample ))
       || (m[0].rm_so != 0))
        return FAILURE;
    return SUCCESS;
}
Example #5
0
FrRegExElt *FrRegExp::compile(const char *re)
{
   if (!re || !*re)
      return 0 ;
   size_t num_groups = 0 ;
   return compile_re(re,false,num_groups,_classes) ;
}
Example #6
0
void duplicate_re_array(re *pre_in, int n_rein, re **pre_out, int *n_reout)
{
	int loop, old_n = *n_reout;

	assert(n_rein >= 0);

	*n_reout += n_rein;
	if (*n_reout)
	{
		*pre_out = (re *)myrealloc(*pre_out, (*n_reout) * sizeof(re));

		for(loop=0; loop<n_rein; loop++)
		{
			memcpy(&(*pre_out)[loop + old_n], &pre_in[loop], sizeof(re));

			memset(&(*pre_out)[loop + old_n].regex, 0x00, sizeof(regex_t));
			(*pre_out)[loop + old_n].regex_str = mystrdup(pre_in[loop].regex_str);
			compile_re(&(*pre_out)[loop + old_n].regex, (*pre_out)[loop + old_n].regex_str);

			if (pre_in[loop].cmd)
				(*pre_out)[loop + old_n].cmd = mystrdup(pre_in[loop].cmd);
		}
	}
	else
	{
		*pre_out = NULL;
	}
}
Example #7
0
void add_editrule(int linenr, char *cmd, char *par)
{
    char *type_str = par;
    char *par1 = find_next_par(type_str);
    char *par2 = NULL;
    striptype_t type = STRIP_TYPE_REGEXP;
    int rule_index = -1;

    if (!par1)
        config_error_exit(linenr, "editrule:%s requires a parameter.\n", type_str);

    if (strcmp(type_str, "kr") == 0)
        type = STRIP_TYPE_RANGE;
    else if (strcmp(type_str, "ke") == 0)
        type = STRIP_TYPE_REGEXP;
    else if (strcmp(type_str, "kc") == 0)
        type = STRIP_TYPE_COLUMN;
    else if (strcmp(type_str, "kS") == 0)
        type = STRIP_KEEP_SUBSTR;
    else
        config_error_exit(linenr, "editrule requirs either ke, kr, kS or kc.\n");

    if (type == STRIP_TYPE_RANGE || type == STRIP_TYPE_COLUMN)
    {
        par2 = find_next_par(par1);
        if (!par2)
            config_error_exit(linenr, "editrule:%s requires another parameter.\n", type_str);
    }

    rule_index = pes[cur_editscheme_nr].n_strips;
    pes[cur_editscheme_nr].strips = (strip_t *)myrealloc(pes[cur_editscheme_nr].strips, (pes[cur_editscheme_nr].n_strips + 1) * sizeof(strip_t));
    memset(&pes[cur_editscheme_nr].strips[pes[cur_editscheme_nr].n_strips], 0x00, sizeof(strip_t));
    pes[cur_editscheme_nr].n_strips++;

    pes[cur_editscheme_nr].strips[rule_index].type = type;

    if (type == STRIP_TYPE_RANGE)
    {
        pes[cur_editscheme_nr].strips[rule_index].start = atoi(par1);
        pes[cur_editscheme_nr].strips[rule_index].end   = atoi(par2);
    }
    else if (type == STRIP_TYPE_REGEXP || type == STRIP_KEEP_SUBSTR)
    {
        pes[cur_editscheme_nr].strips[rule_index].regex_str = mystrdup(par1);
        compile_re(&pes[cur_editscheme_nr].strips[rule_index].regex, par1);
    }
    else if (type == STRIP_TYPE_COLUMN)
    {
        pes[cur_editscheme_nr].strips[rule_index].del = mystrdup(par1);
        pes[cur_editscheme_nr].strips[rule_index].col_nr = atoi(par2);
    }
}
Example #8
0
SCM
ag_scm_string_has_match_p(SCM text, SCM substr)
{
    SCM      res;
    regex_t  re;

    compile_re(&re, ag_scm2zchars( substr, "match expr" ), REG_EXTENDED);

    if (regexec(&re, ag_scm2zchars(text, "text to match"), (size_t)0,
                 NULL, 0) == 0)
         res = SCM_BOOL_T;
    else res = SCM_BOOL_F;
    regfree(&re);

    return res;
}
Example #9
0
SCM
ag_scm_string_has_eqv_match_p(SCM text, SCM substr)
{
    char* pzText   = ag_scm2zchars(text, "text to match");
    char* pzSubstr = ag_scm2zchars(substr, "match expr");
    SCM      res;
    regex_t  re;

    compile_re(&re, pzSubstr, REG_EXTENDED | REG_ICASE);

    if (regexec(&re, pzText, (size_t)0, NULL, 0) == 0)
         res = SCM_BOOL_T;
    else res = SCM_BOOL_F;
    regfree(&re);

    return res;
}
Example #10
0
/*=gfunc string_has_eqv_match_p
 *
 * what:   caseless regex contains
 * general_use:
 *
 * exparg: text, text to test for pattern
 * exparg: match, pattern/substring to search for
 *
 * string: "*~*"
 *
 * doc:  Test to see if a string contains a pattern.
 *       Case is not significant.
=*/
static tSuccess
Select_Match(char const * sample, char const * pattern)
{
    /*
     *  On the first call for this macro, compile the expression
     */
    if (pCurMacro->funcPrivate == NULL) {
        void *    mat = (void *)pattern;
        regex_t*  pRe = AGALOC(sizeof(*pRe), "select match re");
        compile_re(pRe, mat, (int)pCurMacro->res);
        pCurMacro->funcPrivate = (void*)pRe;
    }

    if (regexec((regex_t*)pCurMacro->funcPrivate, sample, (size_t)0,
                 NULL, 0) != 0)
        return FAILURE;
    return SUCCESS;
}
Example #11
0
void add_filterscheme_rule(int linenr, char *cmd, char *par)
{
    char *type = par;
    int use_regex = 0x00;
    char *re_cmd = NULL;
    char *re_str = find_next_par(par);
    if (!re_str)
        config_error_exit(linenr, "Missing regular expression in rule-line for scheme %s.\n", pfs[cur_filterscheme_nr].fs_name);

    if (type[0] != 'e')
        config_error_exit(linenr, "Regular expression type '%s' is not recognized.\n", type);

    if (type[1] == 0x00 || type[1] == 'm')
        use_regex = 'm';
    else if (type[1] == 'v')
        use_regex = 'v';
    else if (type[1] == 'c')
        use_regex = 'c';
    else if (type[1] == 'C')
        use_regex = 'C';
    else if (toupper(type[1]) == 'X')
    {
        char *dummy = find_next_par(re_str);
        if (!dummy)
            config_error_exit(linenr, "Missing command for rule of type 'e%c' for scheme %s.\n", type[1], pfs[cur_filterscheme_nr].fs_name);

        re_cmd = mystrdup(dummy);
        use_regex = type[1];

        if (use_regex == 'X' && (strchr(re_str, '(') == NULL || strchr(re_str, ')') == NULL))
            config_error_exit(linenr, "Filter scheme rule: -eX requires a regular expression which selects a substring using '(' and ')'.\n");
    }

    pfs[cur_filterscheme_nr].pre = (re *)myrealloc(pfs[cur_filterscheme_nr].pre, (pfs[cur_filterscheme_nr].n_re + 1) * sizeof(re));
    memset(&pfs[cur_filterscheme_nr].pre[pfs[cur_filterscheme_nr].n_re], 0x00, sizeof(re));
    pfs[cur_filterscheme_nr].pre[pfs[cur_filterscheme_nr].n_re].use_regex = use_regex;
    pfs[cur_filterscheme_nr].pre[pfs[cur_filterscheme_nr].n_re].regex_str = mystrdup(re_str);
    compile_re(&pfs[cur_filterscheme_nr].pre[pfs[cur_filterscheme_nr].n_re].regex, re_str);
    pfs[cur_filterscheme_nr].pre[pfs[cur_filterscheme_nr].n_re].cmd = re_cmd;
    pfs[cur_filterscheme_nr].n_re++;
}
Example #12
0
SCM
ag_scm_string_start_eqv_match_p(SCM text, SCM substr)
{
    char* pzText   = ag_scm2zchars(text, "text to match");
    char* pzSubstr = ag_scm2zchars(substr, "match expr");
    SCM        res;
    regex_t    re;
    regmatch_t m[2];

    compile_re(&re, pzSubstr, REG_EXTENDED | REG_ICASE);

    if (regexec(&re, pzText, (size_t)2, m, 0) != 0)
         res = SCM_BOOL_F;
    else if (m[0].rm_so != 0)
         res = SCM_BOOL_F;
    else res = SCM_BOOL_T;

    regfree(&re);

    return res;
}
Example #13
0
/*=gfunc string_end_eqv_match_p
 *
 * what:   caseless regex ending
 * general_use:
 *
 * exparg: text, text to test for pattern
 * exparg: match, pattern/substring to search for
 *
 * string: "*~"
 *
 * doc:  Test to see if a string ends with a pattern.
 *       Case is not significant.
=*/
static tSuccess
Select_Match_End(char const * sample, char const * pattern)
{
    regmatch_t m[2];
    /*
     *  On the first call for this macro, compile the expression
     */
    if (pCurMacro->funcPrivate == NULL) {
        void *    mat = (void *)pattern;
        regex_t*  pRe = AGALOC(sizeof(*pRe), "select match end re");
        compile_re(pRe, mat, (int)pCurMacro->res);
        pCurMacro->funcPrivate = (void*)pRe;
    }

    if (regexec((regex_t*)pCurMacro->funcPrivate, sample, (size_t)2, m, 0)
        != 0)
        return FAILURE;
    if (m[0].rm_eo != strlen(sample))
        return FAILURE;
    return SUCCESS;
}
Example #14
0
static FrRegExElt *compile_alternation(const char *&re, size_t &num_groups,
				       FrList *&classes)
{
   assertq(*re == FrRE_ALT_BEG) ;
   const char *regex = re+1 ;
   FrRegExElt **elts = FrNewN(FrRegExElt*,2) ;
   if (!elts)
      return 0 ;
   elts[0] = 0 ;
   size_t num_elts = 0 ;
   size_t groupnum = ++num_groups ;
   while (*regex)
      {
      FrRegExElt *elt = compile_re(regex,true,num_groups,classes) ;
      FrRegExElt **newelts = FrNewR(FrRegExElt*,elts,num_elts+2) ;
      if (!newelts)
	 {
	 FrNoMemory("while compiling regex grouping/alternation") ;
	 break ;
	 }
      elts = newelts ;
      elts[num_elts++] = elt ;
      elts[num_elts] = 0 ;
      if (elt)
	 elt->setGroup(groupnum) ;
      if (*regex == FrRE_ALT_SEP)
	 regex++ ;
      else if (*regex == FrRE_ALT_END)
	 break ;
      }
   if (*regex == FrRE_ALT_END)
      regex++ ;
   else
      FrWarningVA("unterminated regular expression grouping: %s",re) ;
   re = regex ;
   return new FrRegExElt(elts,groupnum) ;
}
Example #15
0
void add_cs_re(int linenr, char *incmd, char *par)
{
    if (use_colors)
    {
        char *re = NULL, *val = NULL;
        char *cmd = &incmd[5];
        char *colon;

        if (strncmp(cmd, "_val", 4) == 0)
        {
            val = find_next_par(par);
            if (!val)
                config_error_exit(linenr, "cs_re_val...-entry malformed: value missing.\n");

            re = find_next_par(val);
        }
        else
            re = find_next_par(par);

        if (re == NULL)
            config_error_exit(linenr, "cs_re-entry malformed: color or regular expression missing.\n");

        if (cur_colorscheme_nr == -1)
            config_error_exit(linenr, "For cs_re one needs to define a color scheme name first.\n");

        /* find colorscheme */
        if (cschemes[cur_colorscheme_nr].color_script.script)
            config_error_exit(linenr, "One cannot let a color script have the same name has a color scheme.");

        /* grow/create list */
        cschemes[cur_colorscheme_nr].pentries = (color_scheme_entry *)myrealloc(cschemes[cur_colorscheme_nr].pentries, (cschemes[cur_colorscheme_nr].n + 1) * sizeof(color_scheme_entry));

        /* add to list */
        if (cmd[0] == 0x00)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = 0;
        else if (strcmp(cmd, "_s") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_SUB;
        else if (strcmp(cmd, "_val_less") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_LESS;
        else if (strcmp(cmd, "_val_bigger") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_BIGGER;
        else if (strcmp(cmd, "_val_equal") == 0)
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].flags = CSREFLAG_CMP_VAL_EQUAL;

        /* sanity check */
        if (cmd[0] != 0x00 && strchr(re, '(') == NULL)
            config_error_exit(linenr, "%s is missing substring selections! ('(' and ')')\n", cmd);

        if (val) cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cmp_value = atof(val);
        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].merge_color = incmd[0] == 'm' ? MY_TRUE : MY_FALSE;

        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.ac_index = 0;
        colon = strchr(par, '|');
        if (colon)
        {
            *colon = 0x00;
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.use_alternating_colors = MY_TRUE;
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.attrs2 = parse_attributes(colon + 1);
        }
        else
        {
            cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.use_alternating_colors = MY_FALSE;
        }
        cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].cdef.attrs1 = parse_attributes(par);

        /* compile regular expression */
        compile_re(&cschemes[cur_colorscheme_nr].pentries[cschemes[cur_colorscheme_nr].n].regex, re);

        cschemes[cur_colorscheme_nr].n++;
    }
}
Example #16
0
// Construct an object based on URL parameters
FunQuery::FunQuery(FILE *of, bool icase, Attributes::size_type cp, bool e, bool r) :
    Query(!e, r, true),
    match_fid(false),
    id_ec(NULL),
    call(NULL),
    current_project(cp)
{
    if (lazy)
        return;

    valid = true;

    // Query name
    char *qname = swill_getvar("n");
    if (qname && *qname)
        name = qname;

    // Match specific file
    int ifid;
    if (swill_getargs("i(fid)", &ifid)) {
        match_fid = true;
        fid = Fileid(ifid);
    }

    // Function call declaration direct match
    if (!swill_getargs("p(call)", &call))
        call = NULL;

    // Identifier EC match
    if (!swill_getargs("p(ec)", &id_ec)) {
        id_ec = NULL;

        // Type of boolean match
        char *m;
        if (!(m = swill_getvar("match"))) {
            fprintf(of, "Missing value: match");
            valid = return_val = false;
            lazy = true;
            return;
        }
        match_type = *m;
    }
    mquery.set_match_type(match_type);

    cfun = !!swill_getvar("cfun");
    macro = !!swill_getvar("macro");
    writable = !!swill_getvar("writable");
    ro = !!swill_getvar("ro");
    pscope = !!swill_getvar("pscope");
    fscope = !!swill_getvar("fscope");
    defined = !!swill_getvar("defined");
    if (!swill_getargs("i(ncallers)|i(ncallerop)", &ncallers, &ncallerop))
        ncallerop = ec_ignore;

    exclude_fnre = !!swill_getvar("xfnre");
    exclude_fure = !!swill_getvar("xfure");
    exclude_fdre = !!swill_getvar("xfdre");
    exclude_fre = !!swill_getvar("xfre");

    // Compile regular expression specs
    if (!compile_re(of, "Function name", "fnre", fnre, match_fnre, str_fnre) ||
            !compile_re(of, "Calling function name", "fure", fure, match_fure, str_fure) ||
            !compile_re(of, "Called function name", "fdre", fdre, match_fdre, str_fdre) ||
            !compile_re(of, "Filename", "fre", fre, match_fre, str_fre, (icase ? REG_ICASE : 0)))
        return;
    specified_order::set_order(mquery.get_sort_order(), mquery.get_reverse());
}
Example #17
0
void noit_http_rest_load_rules() {
  int ai, cnt = 0;
  noit_conf_section_t *acls;
  char path[256];
  struct noit_rest_acl *newhead = NULL, *oldacls, *remove_acl;
  struct noit_rest_acl_rule *remove_rule;

  snprintf(path, sizeof(path), "//rest//acl");
  acls = noit_conf_get_sections(NULL, path, &cnt);
  noitL(noit_debug, "Found %d acl stanzas\n", cnt);
  for(ai = cnt-1; ai>=0; ai--) {
    char tbuff[32];
    struct noit_rest_acl *newacl;
    int ri, rcnt = 0;
    noit_boolean default_allow = noit_false;
    noit_conf_section_t *rules;

    newacl = calloc(1, sizeof(*newacl));
    newacl->next = newhead;
    newhead = newacl;
    if(noit_conf_get_stringbuf(acls[ai], "@type", tbuff, sizeof(tbuff)) &&
       !strcmp(tbuff, "allow"))
      newacl->allow = noit_true;

#define compile_re(node, cont, name) do { \
  char buff[256]; \
  if(noit_conf_get_stringbuf(node, "@" #name, buff, sizeof(buff))) { \
    const char *error; \
    int erroffset; \
    cont->name = pcre_compile(buff, 0, &error, &erroffset, NULL); \
  } \
} while(0)

    newacl->allow = default_allow;
    compile_re(acls[ai], newacl, cn);
    compile_re(acls[ai], newacl, url);
    rules = noit_conf_get_sections(acls[ai], "rule", &rcnt);
    for(ri = rcnt - 1; ri >= 0; ri--) {
      struct noit_rest_acl_rule *newacl_rule;
      newacl_rule = calloc(1, sizeof(*newacl_rule));
      newacl_rule->next = newacl->rules;
      newacl->rules = newacl_rule;
      if(noit_conf_get_stringbuf(rules[ri], "@type", tbuff, sizeof(tbuff)) &&
         !strcmp(tbuff, "allow"))
        newacl_rule->allow = noit_true;
      compile_re(rules[ri], newacl_rule, cn);
      compile_re(rules[ri], newacl_rule, url);
    }
    free(rules);
  }
  free(acls);

  oldacls = global_rest_acls;
  global_rest_acls = newhead;

  while(oldacls) {
    remove_acl = oldacls->next;
    while(oldacls->rules) {
      remove_rule = oldacls->rules->next;
      if(oldacls->rules->cn) pcre_free(oldacls->rules->cn);
      if(oldacls->rules->url) pcre_free(oldacls->rules->url);
      free(oldacls->rules);
      oldacls->rules = remove_rule;
    }
    if(oldacls->cn) pcre_free(oldacls->cn);
    if(oldacls->url) pcre_free(oldacls->url);
    free(oldacls);
    oldacls = remove_acl;
  }
}
Example #18
0
static FrRegExElt *compile_re(const char *&re, bool in_alt,
			      size_t &num_groups, FrList *&classes)
{
   FrRegExElt *elt = 0 ;
   switch (*re)
      {
      case '\0':
	 elt = new FrRegExElt() ;
	 break ;
      case FrRE_ALT_BEG:
	 elt = compile_alternation(re,num_groups,classes) ;
	 break ;
      case FrRE_CLASS_BEG:
	 elt = compile_class(re,classes) ;
	 break ;
      case FrRE_CHARSET_BEG:
	 elt = compile_charset(re) ;
	 break ;
      case '.':
	 elt = compile_wildcard(re) ;
	 break ;
      case FrRE_ALT_SEP:
      case FrRE_ALT_END:
	 if (in_alt)
	    {
	    elt = new FrRegExElt((char*)0,0,(char*)0,0) ;
	    break ;
	    }
	 // fall through to default case
      default:
	 elt = compile_simple(re,in_alt) ;
	 break ;
      }
   if (re && *re)
      {
      FrRegExElt *next_elt ;
      char option = *re ;
      if (option == FrRE_MULTIPLE || option == FrRE_KLEENE ||
	  option == FrRE_OPTIONAL)
	 re++ ;
      else if (option == FrRE_COUNT_BEG)
	 {
	 size_t low, high ;
	 compile_count(re,low,high) ;
	 elt->setReps(low,high) ;
	 }
      if (in_alt && (option == FrRE_ALT_SEP || option == FrRE_ALT_END))
	 next_elt = 0 ;
      else
	 next_elt = compile_re(re,in_alt,num_groups,classes) ;
      if (elt)
	 elt->setNext(next_elt) ;
      else
	 elt = next_elt ;
      if (elt)
	 {
	 if (option == FrRE_MULTIPLE)
	    elt->setReps(1,UINT_MAX) ;
	 else if (option == FrRE_KLEENE)
	    elt->setReps(0,UINT_MAX) ;
	 else if (option == FrRE_OPTIONAL)
	    elt->setReps(0,1) ;
	 }
      }
   return elt ;
}
Example #19
0
void add_convert(int linenr, char *cmd, char *par)
{
    char *conv_name = par;
    char *conv_type = NULL;
    conversion_t type = 0;
    char *conv_script = NULL;
    char *conv_re = NULL;
    int loop;

    /* parse type */
    conv_type = find_next_par(conv_name);
    if (!conv_type)
        config_error_exit(linenr, "'convert'-entry malformed: conversion type missing.\n");

    if (strncmp(conv_type, "script:", 7) == 0)
    {
        conv_script = find_next_par(conv_type);
        if (conv_script)
            conv_re = find_next_par(conv_script);
        else
            config_error_exit(linenr, "Convert: script filename missing.\n");
    }
    else
        conv_re = find_next_par(conv_type);

    if (!conv_re)
        config_error_exit(linenr, "'convert'-entry malformed: type or regular expression missing.\n");

    /* find this conversion: is it from an already known group? */
    for(loop=0; loop<n_conversions; loop++)
    {
        if (strcmp(conversions[loop].name, conv_name) == 0)
            break;
    }
    /* create new group */
    if (loop == n_conversions)
    {
        n_conversions++;
        conversions = (conversion *)myrealloc(conversions, sizeof(conversion) * n_conversions);
        memset(&conversions[loop], 0x00, sizeof(conversion));
        conversions[loop].name = mystrdup(conv_name);
    }

    if (strcmp(conv_type, "ip4tohost") == 0)
        type = CONVTYPE_IP4TOHOST;
    else if (strcmp(conv_type, "epochtodate") == 0)
        type = CONVTYPE_EPOCHTODATE;
    else if (strcmp(conv_type, "errnotostr") == 0)
        type = CONVTYPE_ERRNO;
    else if (strcmp(conv_type, "hextodec") == 0)
        type = CONVTYPE_HEXTODEC;
    else if (strcmp(conv_type, "dectohex") == 0)
        type = CONVTYPE_DECTOHEX;
    else if (strcmp(conv_type, "tai64todate") == 0)
        type = CONVTYPE_TAI64NTODATE;
    else if (strcmp(conv_type, "script") == 0)
        type = CONVTYPE_SCRIPT;
    else if (strcmp(conv_type, "abbrtok") == 0)
        type = CONVTYPE_ABBRTOK;
    else if (strcmp(conv_type, "signrtostring") == 0)
        type = CONVTYPE_SIGNRTOSTRING;
    else
        config_error_exit(linenr, "Convert %s: '%s' is a not recognized conversion type.\n", conv_name, conv_type);

    conversions[loop].pcb = (conversion_bundle_t *)myrealloc(conversions[loop].pcb, sizeof(conversion_bundle_t) * (conversions[loop].n + 1));
    conversions[loop].pcs = (script *)myrealloc(conversions[loop].pcs, sizeof(script) * (conversions[loop].n + 1));

    conversions[loop].pcb[conversions[loop].n].type = type;


    memset(&conversions[loop].pcs[conversions[loop].n], 0x00, sizeof(script));
    conversions[loop].pcs[conversions[loop].n].script = conv_script?mystrdup(conv_script):NULL;

    compile_re(&conversions[loop].pcb[conversions[loop].n].regex, conv_re);
    conversions[loop].pcb[conversions[loop].n].match_count = 0;
    conversions[loop].n++;
}
Example #20
0
static int check_window_match(const xdo_t *xdo, Window wid, const xdo_search_t *search) {
  regex_t title_re;
  regex_t class_re;
  regex_t classname_re;
  regex_t name_re;

  if (!compile_re(search->title, &title_re) \
      || !compile_re(search->winclass, &class_re) \
      || !compile_re(search->winclassname, &classname_re) \
      || !compile_re(search->winname, &name_re)) {
    return False;
  }

  /* Set this to 1 for dev debugging */
  static const int debug = 0;

  int visible_ok, pid_ok, title_ok, name_ok, class_ok, classname_ok;
  int visible_want, pid_want, title_want, name_want, class_want, classname_want;

  visible_ok = pid_ok = title_ok = name_ok = class_ok = classname_ok = True;
    //(search->require == SEARCH_ANY ? False : True);

  visible_want = search->searchmask & SEARCH_ONLYVISIBLE;
  pid_want = search->searchmask & SEARCH_PID;
  title_want = search->searchmask & SEARCH_TITLE;
  name_want = search->searchmask & SEARCH_NAME;
  class_want = search->searchmask & SEARCH_CLASS;
  classname_want = search->searchmask & SEARCH_CLASSNAME;

  do {
    /* Visibility is a hard condition, fail always if we wanted 
     * only visible windows and this one isn't */
    if (visible_want && !_xdo_is_window_visible(xdo, wid)) {
      if (debug) fprintf(stderr, "skip %ld visible\n", wid); 
      visible_ok = False;
      break;
    }

    if (pid_want && !_xdo_window_match_pid(xdo, wid, search->pid)) {
      if (debug) fprintf(stderr, "skip %ld pid\n", wid); 
      pid_ok = False;
    }

    if (title_want && !_xdo_window_match_title(xdo, wid, &title_re)) {
      if (debug) fprintf(stderr, "skip %ld title\n", wid);
      title_ok = False;
    }

    if (name_want && !_xdo_window_match_name(xdo, wid, &name_re)) {
      if (debug) fprintf(stderr, "skip %ld winname\n", wid);
      name_ok = False;
    }

    if (class_want && !_xdo_window_match_class(xdo, wid, &class_re)) {
      if (debug) fprintf(stderr, "skip %ld winclass\n", wid);
      class_ok = False;
    }

    if (classname_want && !_xdo_window_match_classname(xdo, wid, &classname_re)) {
      if (debug) fprintf(stderr, "skip %ld winclassname\n", wid);
      classname_ok = False;
    }
  } while (0);

  if (search->title) 
    regfree(&title_re);
  if (search->winclass) 
    regfree(&class_re);
  if (search->winclassname) 
    regfree(&classname_re);
  if (search->winname) 
    regfree(&name_re);

  if (debug) {
    fprintf(stderr, "win: %ld, pid:%d, title:%d, name:%d, class:%d, visible:%d\n",
            wid, pid_ok, title_ok, name_ok, class_ok, visible_ok);
  }

  switch (search->require) {
    case SEARCH_ALL:
      return visible_ok && pid_ok && title_ok && name_ok && class_ok && classname_ok;
      break;
    case SEARCH_ANY:
      return visible_ok && ((pid_want && pid_ok) || (title_want && title_ok) \
                            || (name_want && name_ok) \
                            || (class_want && class_ok) \
                            || (classname_want && classname_ok));
      break;
  }
  
  fprintf(stderr, 
          "Unexpected code reached. search->require is not valid? (%d); "
          "this may be a bug?\n",
          search->require);
  return False;
}