Beispiel #1
0
/* actually compiles individual regex etc. */
void add_to_patterns(char *pattern, struct pattern_item **list)
{
    char first[MAX_BUFF];
    char second[MAX_BUFF];
    char type[MAX_BUFF];
    char accel[MAX_BUFF];
    regex_t compiled;
    struct REGEX_pattern rpattern;
    int abort_type = 0;
    int abort_regex_type = 0;
    int parenthesis;
    int stored;

    /*  The regex_flags that we use are:
        REG_EXTENDED
        REG_NOSUB
        REG_ICASE; */

    int regex_flags = REG_NOSUB;

    rpattern.type = NORMAL;
    rpattern.case_sensitive = 1;

    stored = sscanf(pattern, "%s %s %s %s", type, first, second, accel);


    if((stored < 2) || (stored > 4)) {
        logprint(LOG_ERROR,
                 "unable to get a pair of patterns in add_to_patterns() "
                 "for [%s]\n", pattern);
        dodo_mode = 1;
        return;
    }

    if(stored == 2)
        strcpy(second, "");

    /* type to lower case so as to be case insensitive */
    lower_case(type);

    if(strcmp(type, "abort") == 0) {
        rpattern.type = ABORT;
        abort_type = 1;
    }

    if(strcmp(type, "abortregexi") == 0) {
        abort_regex_type = 1;
        regex_flags |= REG_ICASE;
        rpattern.case_sensitive = 0;
        rpattern.type = ABORT_NORMAL;
    }

    if(strcmp(type, "abortregex") == 0) {
        abort_regex_type = 1;
        rpattern.type = ABORT_NORMAL;
    }

    if (strcmp(type, "abort_on_match") == 0) {
        rpattern.type = ABORT_ON_MATCH;
        abort_type = 1;
        /* make sure on or off is in the correct state */
        lower_case (first);
    }

    if(strcmp(type, "regexi") == 0) {
        regex_flags |= REG_ICASE;
        rpattern.case_sensitive = 0;
    }

    if(!abort_type) {

        parenthesis = count_parenthesis (first);

        if (parenthesis < 0) {

            /* The function returned an invalid result,
            indicating an invalid string */

            logprint(LOG_ERROR, "count_parenthesis() returned "
                     "left count did not match right count for line: [%s]\n",
                     pattern);
            dodo_mode = 1;
            return;

        } else if (parenthesis > 0) {
            regex_flags |= REG_EXTENDED;
            regex_flags ^= REG_NOSUB;
            if (!abort_regex_type) {
                rpattern.type = EXTENDED;
            } else {
                rpattern.type = ABORT_EXTENDED;
            }
        }
    }


    /* compile the regex */
    if(regcomp(&compiled, first, regex_flags)) {
        logprint(LOG_ERROR, "Invalid regex [%s] in pattern file\n", first);
        dodo_mode = 1;
        return;
    }


    /* put the compiled regex into the structure, along with replacement etc. */
    rpattern.cpattern = compiled;
    rpattern.pattern = (char *)malloc(sizeof(char) * (strlen(first) +1));
    if(rpattern.pattern == NULL) {
        logprint(LOG_ERROR, "unable to allocate memory in add_to_patterns()\n");
        dodo_mode = 1;
        return;
    }
    strcpy(rpattern.pattern, first);


    rpattern.replacement = (char *)malloc(sizeof(char) * (strlen(second) +1));
    if(rpattern.replacement == NULL) {
        logprint(LOG_ERROR, "unable to allocate memory in add_to_patterns()\n");
        dodo_mode = 1;
        return;
    }
    strcpy(rpattern.replacement, second);


    /* make up an accel string if one was not supplied */
    /* if we have a regex type */
    if (!abort_type) {
        /* if it is a abort regex, 3 fields means that we have accelerator */
        if (abort_regex_type && (stored != 3)) {
            makeup_accel(accel, first);
            stored = 3;
        }

        /* if it normal replacement regex, 4 fields means we have an accelerator already */
        if (!abort_regex_type && (stored != 4)) {
            makeup_accel(accel, first);
            stored = 4;
        }
    }


    if (accel != NULL) {
        rpattern.has_accel = 1;
        rpattern.accel = get_accel(accel, &rpattern.accel_type,
                                   rpattern.case_sensitive);

        if(rpattern.accel == NULL) {
            logprint(LOG_ERROR, "unable to allocate memory from get_accel()\n");
            dodo_mode = 1;
            return;
        }
    } else {
        rpattern.has_accel = 0;
        rpattern.accel = NULL;
    }

    /* finally, add it to the end of the list */
    add_to_plist(rpattern, list);
}
Beispiel #2
0
void add_to_patterns(char *pattern)
{
  char first[MAX_BUFF];
  char second[MAX_BUFF];
  char type[MAX_BUFF];
  char accel[MAX_BUFF];
  regex_t compiled;
  struct REGEX_pattern rpattern;
  int abort_type = 0;
  int parenthesis;
  int stored;
  
  /*  The regex_flags that we use are:
      REG_EXTENDED 
      REG_NOSUB 
      REG_ICASE; */

  int regex_flags = REG_NOSUB;
  
  rpattern.type = NORMAL;
  rpattern.case_sensitive = 1;
  
  stored = sscanf(pattern, "%s %s %s %s", type, first, second, accel);
  
  
  if((stored < 2) || (stored > 4)) {
    log(LOG_ERROR, 
	"unable to get a pair of patterns in add_to_patterns() "
	"for [%s]\n", pattern);
    dodo_mode = 1;
    return;
  }
  
  if(stored == 2)
    strcpy(second, "");
  
  if(strcmp(type, "abort") == 0) {
    rpattern.type = ABORT;
    abort_type = 1;
  }
  
  if(strcmp(type, "regexi") == 0) {
    regex_flags |= REG_ICASE;
    rpattern.case_sensitive = 0;
  }

  
  if(!abort_type) {

    parenthesis = count_parenthesis (first);

    if (parenthesis < 0) {
      
      /* The function returned an invalid result, 
	 indicating an invalid string */
      
      log (LOG_ERROR, "count_parenthesis() returned "
	   "left count did not match right count for line: [%s]\n",
	   pattern);
      dodo_mode = 1;
      return;

    } else if (parenthesis > 0) {

      regex_flags |= REG_EXTENDED;
      rpattern.type = EXTENDED;
      regex_flags ^= REG_NOSUB;

    }
  }
  
  
  if(regcomp(&compiled, first, regex_flags)) {
    log(LOG_ERROR, "Invalid regex [%s] in pattern file\n", first);
    dodo_mode = 1;
    return;
  }
  
  
  rpattern.cpattern = compiled;

  
  rpattern.pattern = (char *)malloc(sizeof(char) * (strlen(first) +1));
  if(rpattern.pattern == NULL) {
    log(LOG_ERROR, "unable to allocate memory in add_to_patterns()\n");
    dodo_mode = 1;
    return;
  }
  strcpy(rpattern.pattern, first);
  

  rpattern.replacement = (char *)malloc(sizeof(char) * (strlen(second) +1));
  if(rpattern.replacement == NULL) {
    log(LOG_ERROR, "unable to allocate memory in add_to_patterns()\n");
    dodo_mode = 1;
    return;
  }
  strcpy(rpattern.replacement, second);
  

  /* use accelerator string if it exists */
  if(stored == 4) {

    rpattern.has_accel = 1;
    rpattern.accel = get_accel(accel, &rpattern.accel_type, 
			       rpattern.case_sensitive);
  }


  /* use accelerator string if it exists */
  if(stored == 4) {

    rpattern.has_accel = 1;
    rpattern.accel = get_accel(accel, &rpattern.accel_type, 
			       rpattern.case_sensitive);


    if(rpattern.accel == NULL) {
      log(LOG_ERROR, "unable to allocate memory from get_accel()\n");
      dodo_mode = 1;
      return;
    }

  } else {
    rpattern.has_accel = 0;
    rpattern.accel = NULL;
  }
  
  add_to_plist(rpattern);
}