示例#1
0
match_t* scan_try(scanner_t* sc) {
   assert(ptable);
   assert(sc);
   range_t* groups = core_match(sc->pattern->core, sc->curr, NULL,
                                     NULL, NULL, 0, &trash, sc->start);
   if (groups)
      return match_new(groups, sc->pattern->names,
                       range_group(groups, 0)->begin - sc->start);
   return NULL;
}
示例#2
0
match_t* shre_entire(pattern_t* pattern, char* str) {
   assert(ptable);
   assert(pattern);
   assert(str);
   range_t* groups =
          core_match(pattern->core, str, NULL, NULL,
                                  NULL, 0, &trash, str);
   if (!groups)
      return NULL;
   if (*(range_group(groups, 0)->end) == '\0') {
      return match_new(groups, pattern->names,
                          range_group(groups, 0)->begin - str);
   }
   return NULL;
}
示例#3
0
match_t* shre_search(pattern_t* pattern, char* str) {
   assert(ptable);
   assert(pattern);
   assert(str);
   char* start = str;
   for (;; ++str) {
      range_t* groups = core_match(pattern->core, str,
                              NULL, NULL, NULL, 0, &trash, start);
      if (groups)
         return match_new(groups, pattern->names,
                          range_group(groups, 0)->begin - start);
      if (*str == '\0')
         break;
   }
   return NULL;
}
示例#4
0
match_t* scan_next(scanner_t* sc) {
   assert(ptable);
   assert(sc);
   for (;; ++sc->curr) {
      char* pos = sc->curr;
      range_t* groups = core_match(sc->pattern->core, sc->curr,
                           NULL, NULL, NULL, 0, &sc->curr, sc->start);
      if (groups) {
         if (pos == sc->curr)
            scan_increment(sc);
         return match_new(groups, sc->pattern->names,
                          range_group(groups, 0)->begin - sc->start);
      }
      if (*sc->curr == '\0')
         break;
   }
   return NULL;
}
示例#5
0
文件: match.c 项目: dleonard0/state
struct matcher *
matcher_new(const struct globs *globs,
	    const struct generator *generator,
	    void *context)
{
	struct matcher *matcher;
	struct match *m;

	/* The initial match list contains the deferred empty string */
	m = match_new(0);
	m->next = 0;
	m->stri = stri_str(0);
	m->flags = MATCH_DEFERRED;
	m->state = 0;

	matcher = malloc(sizeof *matcher);
	matcher->globs = globs;
	matcher->generator = generator;
	matcher->gcontext = context;
	matcher->matches = m;

	return matcher;
}
示例#6
0
static int driver_add_match(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {

        struct bus_match_component *components = NULL;
        Context *context = userdata;
        unsigned n_components = 0;
        Match *m = NULL;
        Client *c = NULL;
        char *arg0;
        uint64_t id;
        int r;

        assert(bus);
        assert(message);
        assert(context);

        r = sd_bus_message_read(message, "s", &arg0);
        if (r < 0)
                return r;

        r = bus_kernel_parse_unique_name(message->sender, &id);
        if (r < 0)
                return r;

        r = client_acquire(context, id, &c);
        if (r == -ENOBUFS)
                return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u clients", CLIENTS_MAX);
        if (r < 0)
                return r;

        if (c->n_matches >= MATCHES_MAX) {
                r = sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Reached limit of %u matches per client", MATCHES_MAX);
                goto fail;
        }

        r = bus_match_parse(arg0, &components, &n_components);
        if (r < 0) {
                r = sd_bus_error_setf(error, SD_BUS_ERROR_MATCH_RULE_INVALID, "Match rule \"%s\" is not valid", arg0);
                goto fail;
        }

        r = match_new(c, components, n_components, &m);
        if (r < 0)
                goto fail;

        r = bus_add_match_internal_kernel(bus, id, components, n_components, m->cookie);
        if (r < 0)
                goto fail;

        bus_match_parse_free(components, n_components);

        return sd_bus_reply_method_return(message, NULL);

fail:
        bus_match_parse_free(components, n_components);

        match_free(m);

        if (c->n_matches <= 0)
                client_free(c);

        return r;
}
示例#7
0
/* this needs to be called with the indexes in
 * reverse order */
const_matchp matches_calc(match_ctx ctx,        /* IN/OUT */
                          int index)     /* IN */
{
    const unsigned char *buf;

    matchp matches;
    matchp mp;
    struct match_node *np;

    buf = ctx->buf;
    matches = NULL;

    LOG(LOG_DUMP, ("index %d, char '%c', rle %d, rle_r %d\n",
                   index, buf[index], ctx->rle[index],
                   ctx->rle_r[index]));

    /* proces the literal match and add it to matches */
    mp = match_new(ctx, &matches, 1, 0);

    /* get possible match */
    np = ctx->info[index]->single;
    if(np != NULL)
    {
        np = np->next;
    }
    for (; np != NULL; np = np->next)
    {
        int mp_len;
        int len;
        int pos;
        int offset;

        /* limit according to max offset */
        if(np->index > index + ctx->max_offset)
        {
            break;
        }

        LOG(LOG_DUMP, ("find lengths for index %d to index %d\n",
                        index, np->index));

        /* get match len */
        mp_len = mp->offset > 0 ? mp->len : 0;
        LOG(LOG_DUMP, ("0) comparing with current best [%d] off %d len %d\n",
                        index, mp->offset, mp_len));

        offset = np->index - index;
        len = mp_len;
        pos = index + 1 - len;
        /* Compare the first <previous len> bytes backwards. We can
         * skip some comparisons by increasing by the rle count. We
         * don't need to compare the first byte, hence > 1 instead of
         * > 0 */
        while(len > 1 && buf[pos] == buf[pos + offset])
        {
#if 1
            int offset1 = ctx->rle_r[pos];
            int offset2 = ctx->rle_r[pos + offset];
            int offset = offset1 < offset2 ? offset1 : offset2;

            LOG(LOG_DUMP, ("1) compared sucesssfully [%d] %d %d\n",
                            index, pos, pos + offset));

            len -= 1 + offset;
            pos += 1 + offset;
#else
            --len;
            ++pos;
#endif
        }
        if(len > 1)
        {
            /* sequence length too short, skip this match */
            continue;
        }

        if(offset < 17)
        {
            /* allocate match struct and add it to matches */
            mp = match_new(ctx, &matches, 1, offset);
        }

        /* Here we know that the current match is atleast as long as
         * the previuos one. let's compare further. */
        len = mp_len;
        pos = index - len;
        while(pos >= 0 && buf[pos] == buf[pos + offset])
        {
            LOG(LOG_DUMP, ("2) compared sucesssfully [%d] %d %d\n",
                            index, pos, pos + offset));
            ++len;
            --pos;
        }
        if(len > mp_len)
        {
            /* allocate match struct and add it to matches */
            mp = match_new(ctx, &matches, index - pos, offset);
        }
        if(pos < 0)
        {
            /* we have reached the eof, no better matches can be found */
            break;
        }
    }
    LOG(LOG_DEBUG, ("adding matches for index %d to cache\n", index));
    dump_matches(LOG_DEBUG, matches);

    return matches;
}