Exemplo n.º 1
0
static void grep_error(int errcode, regex_t *matcher, GtError *err)
{
  char sbuf[BUFSIZ], *buf;
  size_t bufsize;
  gt_error_check(err);
  bufsize = tre_regerror(errcode, matcher, NULL, 0);
  buf = gt_malloc(bufsize);
  (void) tre_regerror(errcode, matcher, buf ? buf : sbuf,
                      buf ? bufsize : BUFSIZ);
  gt_error_set(err, "grep(): %s", buf ? buf : sbuf);
  gt_free(buf);
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
   regex_t preg;
   regmatch_t *pmatch = NULL;
   int i, subject_len, match_len,
       sub_len, errcode, ret = 1,
       match_count, done;
   char errbuf[256], *subject, *regex, *match_start,
        *sub;

   if( argc < 3 )
   {
      printf("Usage: %s [subject] [regex]\n", argv[0]);
      return 0;
   }

   subject = strdup(argv[1]);
   subject_len = strlen(subject);
   regex = strdup(argv[2]);

   /* compile the regex */
   errcode = tre_regcomp(&preg, regex, REG_EXTENDED);
   if(errcode)
   {
      tre_regerror(errcode, &preg, errbuf, sizeof(errbuf));
      fprintf(stderr, "Error: regcomp() %s\n", errbuf);
      goto err;
   }

   /* allocate space for match and submatches */
   pmatch = (regmatch_t*)calloc(sizeof(regmatch_t),
                                preg.re_nsub+1);
   if(pmatch == NULL)
   {
      perror("Error calloc()");
      goto err;
   }

   /* find all matches */
   sub = subject;
   sub_len = subject_len;
   match_count = 0;
   done = 0;

   while(!done)
   {
      /* execute regex */
      errcode = tre_regnexec(&preg, sub, sub_len,
                             preg.re_nsub+1,
                             pmatch, 0);

      switch(errcode)
      {
         case REG_OK:
            match_count++;

            /* loop through subpattern matches */
            for(i=0; i < preg.re_nsub+1; i++)
            {
               match_len = pmatch[i].rm_eo - pmatch[i].rm_so;
               match_start = sub + pmatch[i].rm_so;

               if(i == 0)
                  printf("Match [%d] %.*s\n", match_count,
                         match_len, match_start);
               else
                  printf(":: group [%d] %.*s\n", i, match_len,
                         match_start);
            }

            /* set up for next regexec call */
            sub_len -= pmatch[0].rm_eo;
            assert(sub_len >= 0);

            /* entire subject consumed */
            if(sub_len == 0)
            {
               done = 1;
               break;
            }

            /* start next match were we left off */
            sub += pmatch[0].rm_eo;
            break;

         case REG_NOMATCH:
            if(match_count == 0)
               puts("No matches found.");
            done = 1;
            break;

         case REG_ESPACE:
            fprintf(stderr,"Error: Out of memory.\n");
            goto err;

         default:
            /* should not be here, abort */
            assert(0);
      }
   }

   /* success */
   ret = 0;

err:
   /* cleanup */
   if(regex)
      free(regex);
   if(subject)
      free(subject);
   if(&preg)
      tre_regfree(&preg);
   if(pmatch)
      free(pmatch);

   return ret;
}
Exemplo n.º 3
0
/* M A I N *******************************************************************/
int main(int argc, char *argv[]) {
    int opt_index;

    opt_index = process_options(argc, argv);

    if (opt_index >= argc) {
        fprintf(stderr, "%s : %s\n",
                        PRG_NAME, "[err] specify a record string to process!");
        exit(1);
    }

    if (argv[1])
      strncpy (record, argv[opt_index], MAX_CHAR);

    if ( strlen(record) == 0 ) {
        fprintf(stdout, "Please enter a valid string to search pattern for!\n");
        fprintf(stdout, "Usage: %s %s %s %s\n", PRG_NAME, "[COSTS]", "[PATTERN]", "[STRING]");
        exit(1);
    }

/* Testing patterns */
//    char record[] = "ATTTACTATGTAAAGATAGAAGGAATAAGGTGAAG";
//    char regexp[]   = "GATTT";
//    char regexp[]   = "GTAAAGA";
//    char regexp[]   = "(G|T)A*GAT";

    int comp_flags  = REG_EXTENDED | REG_ICASE ;

    static regex_t preg;	      /* Compiled pattern to search for. */
    static regaparams_t match_params; /* regexp matching parameters */
    int errcode;

    regmatch_t pmatch = { 0, 0 };     /* matched pattern structure */
    regamatch_t match;                /* overall match structure */

    memset(&match, 0, sizeof(match)); /* initialize the overall match struct */
    match.pmatch = &pmatch;           /* assign default pattern structure */
    match.nmatch = 1;                 /* initialization of pmatch array */

    /* setup the default match parameters */
    tre_regaparams_default(&match_params);
    /* Set the maximum number of errors allowed for a record to match. */
    match_params.max_cost   = max_cost;
    match_params.cost_ins   = cost_ins;
    match_params.cost_del   = cost_del;
    match_params.cost_subst = cost_subs;

    fprintf(stdout, "Default regex params set by TRE:\n");
    fprintf(stdout, "\t%-12s : %4d\n", "cost_ins", match_params.cost_ins);
    fprintf(stdout, "\t%-12s : %4d\n", "cost_del", match_params.cost_del);
    fprintf(stdout, "\t%-12s : %4d\n", "cost_substr", match_params.cost_subst);
    fprintf(stdout, "\t%-12s : %4d\n", "max_cost", match_params.max_cost);
    fprintf(stdout, "\t%-12s : %4d\n", "max_ins", match_params.max_ins);
    fprintf(stdout, "\t%-12s : %4d\n", "max_del", match_params.max_del);
    fprintf(stdout, "\t%-12s : %4d\n", "max_subst", match_params.max_subst);
    fprintf(stdout, "\t%-12s : %4d\n", "max_err", match_params.max_err);
    fprintf(stdout, "\n\n");

    /* Step 1: compile the regex */
    errcode = tre_regcomp(&preg, regexp, comp_flags);
    if (errcode)
      {
        char errbuf[256];
        tre_regerror(errcode, &preg, errbuf, sizeof(errbuf));
        fprintf(stderr, "%s: %s: %s\n",
  	      PRG_NAME, "Error in search pattern", errbuf);
        exit(1);
      }

//    if (tre_regexec(&delim, "", 0, NULL, 0) == REG_OK)
//    {
//      fprintf(stderr, "%s: %s\n", PRG_NAME,
//	      "Record delimiter pattern must not match an empty string");
//      exit(1);
//    }

    /* Step 2: search for the pattern in the haystack */

    errcode = tre_regaexec(&preg, record, &match, match_params, 0);
    if (errcode == REG_OK) {
        fprintf(stdout, "Found match!\n");
        fprintf(stdout, "\t%10s : %s\n", "record", record);
        fprintf(stdout, "\t%10s : %s\n", "pattern", regexp);
        fprintf(stdout, "\t%10s : %4d\n", "cost", match.cost);
        fprintf(stdout, "\t%10s : %4d\n", "num_ins", match.num_ins);
        fprintf(stdout, "\t%10s : %4d\n", "num_del", match.num_del);
        fprintf(stdout, "\t%10s : %4d\n", "num_subst", match.num_subst);
        fprintf(stdout, "\t%10s : %2d - %2d\n", "char pos", 
                                                pmatch.rm_so, pmatch.rm_eo);
    }
    else {
        fprintf(stdout, "Found no matches!\n");
        fprintf(stdout, "%6s : %s\n", "regexp", regexp);
        fprintf(stdout, "%6s : %s\n", "record", record);

    }

    return 0;
}
Exemplo n.º 4
0
static int generate_error (lua_State *L, const TPosix *ud, int errcode) {
  char errbuf[80];
  tre_regerror (errcode, &ud->r, errbuf, sizeof (errbuf));
  return luaL_error (L, "%s", errbuf);
}
Exemplo n.º 5
0
size_t crm114__regerror(int errcode, const regex_t *preg, char *errbuf,
		       size_t errbuf_size)
{
  return tre_regerror(errcode, preg, errbuf, errbuf_size);
}