int posix_regex_search(Fxstream& stream, regex_t * pxr) { ReadPair pair; while(stream.read(pair) == 0) { std::string * data = prepare_data(opts, pair.first); int ret = regexec(pxr, data->c_str(), 0, NULL, 0); if(ret == REG_NOMATCH){ // read one did not have a match check read 2 if it exists if(!pair.second.empty()) { data = prepare_data(opts, pair.second); ret = regexec(pxr, data->c_str(), 0, NULL, 0); if(ret == REG_NOMATCH && opts.v_flag) { on_match(stdout, pair); } if(ret != (REG_NOMATCH | 0)) { char * errorbuf = get_regerror(ret, pxr); fprintf(stderr, "%s\n", errorbuf); free(errorbuf); return 1; } else if(ret == 0) { if(on_match(stdout, pair) == 1) { // the l flag has been set, print the file name printf("%s\n", stream.getFile2().c_str()); return 0; } } } else if (opts.v_flag) { on_match(stdout, pair); } } else if(ret != 0) { char * errorbuf = get_regerror(ret, pxr); fprintf(stderr, "%s\n", errorbuf); free(errorbuf); return 1; } else if(! opts.v_flag ){ if(on_match(stdout, pair) == 1) { // the l flag has been set, print the file name printf("%s\n", stream.getFile1().c_str()); return 0; } } pair.clear(); } return 0; }
static void player_pattern_compile (struct player_pattern *pp) { int res; free_player_pattern_compiled_data (pp); if (pp->pattern && pp->pattern[0]) { switch (pp->mode) { case PATTERN_MODE_REGEXP: pp->data = g_malloc (sizeof (regex_t)); res = regcomp ((regex_t *) pp->data, pp->pattern, REGCOMP_FLAGS); if (res) { pp->error = get_regerror (res, (regex_t *) pp->data); regfree ((regex_t *) pp->data); g_free (pp->data); pp->data = NULL; } break; case PATTERN_MODE_STRING: case PATTERN_MODE_SUBSTR: default: pp->data = g_strdup (pp->pattern); g_strdown (pp->data); break; } } }
void remote_process(char *buf) { regmatch_t matches[6]; char *command; char *arg1 = NULL; char *arg2 = NULL; int e; if ((e = regexec(®ex, buf, 6, matches, 0)) != 0) { char *err = get_regerror(e, ®ex); printf("%s: regexec() failed: %s\n", GGadu_PLUGIN_NAME, err); free(err); return; } command = buf + matches[1].rm_so; buf[matches[1].rm_eo] = '\0'; arg1 = matches[3].rm_so == -1 ? NULL : buf + matches[3].rm_so; if (arg1) { buf[matches[3].rm_eo] = '\0'; arg2 = matches[5].rm_so == -1 ? NULL : buf + matches[5].rm_so; if (arg2) buf[matches[5].rm_eo] = '\0'; } print_debug("%s: %s (%s, %s)\n", GGadu_PLUGIN_NAME, command, arg1 == NULL ? "<void>" : arg1, arg2 == NULL ? "<void>" : arg2); for (e = 0; remote_signals[e].command != NULL; e++) { if (strcasecmp(command, remote_signals[e].command) == 0) { if (remote_signals[e].arg1 != NULL) { if (strcasecmp(arg1, remote_signals[e].arg1) == 0) { remote_signals[e].func(arg2); } } else { remote_signals[e].func(arg1); } } } }
void atom_finish(struct atom * atom) { char * regex_pat = NULL; int regex_patlen = atom->patlen + strlen(RE_PKG_BEGIN) + strlen(RE_PKG_END) + 1; debug_message("predicate_finish_atom", 0); if (atom->field_name != 0) { char * repl = strchr(atom->field_name, ':'); if (repl != NULL) { *repl++ = '\0'; atom->repl_inx = fieldtrie_insert(repl)->inx; } else { atom->repl_inx = -1; } atom->field_inx = fieldtrie_insert(atom->field_name)->inx; } if (atom->mode == M_REGEX || atom->mode == M_EREGEX) { regex_pat = calloc(1, regex_patlen); /* rely on mem 0-ing */ if (regex_pat == 0) fatal_enomem(0); if (atom->whole_pkg) strncat(regex_pat, RE_PKG_BEGIN, strlen(RE_PKG_BEGIN)); strncat(regex_pat, atom->pat, atom->patlen); if (atom->whole_pkg) strncat(regex_pat, RE_PKG_END, strlen(RE_PKG_END)); debug_message("compiling:", 0); debug_message(regex_pat, 0); int rerr = regcomp(&atom->regex, regex_pat, (atom->mode == M_EREGEX ? REG_EXTENDED : 0) | REG_NOSUB | (atom->ignore_case ? REG_ICASE : 0)); free(regex_pat); if (rerr != 0) { char * s; s = get_regerror(rerr, &atom->regex); if (s == 0) fatal_enomem(0); message(L_FATAL, 0, "%s", s); free(s); fail(); } } }
/* Check whether string macthes any of the pattern in PATARRAY and returns the matching pattern item or NULL. */ static pattern_t * match_p (const char *string, pattern_t *patarray) { pattern_t *pat; if (!*string) { if (opt.verbose) log_info ("zero length input line - ignored\n"); return NULL; } for (pat = patarray; pat->type != PAT_NULL; pat++) { if (pat->type == PAT_STRING) { if (!strcasecmp (pat->u.s.string, string)) return pat; } else if (pat->type == PAT_REGEX) { int rerr; rerr = regexec (pat->u.r.regex, string, 0, NULL, 0); if (!rerr) return pat; else if (rerr != REG_NOMATCH) { char *rerrbuf = get_regerror (rerr, pat->u.r.regex); log_error ("matching r.e. failed: %s\n", rerrbuf); xfree (rerrbuf); return pat; /* Better indicate a match on error. */ } } else BUG (); } return NULL; }
void start_plugin() { int e; if ((e = regcomp(®ex, "^([^ ]+)( ([^ ]+)( (.*))?)?$", REG_EXTENDED)) != 0) { char *err = get_regerror(e, ®ex); print_debug("%s: regcomp() failed: %s\n", GGadu_PLUGIN_NAME, err); print_debug("%s: plugin disabled\n", GGadu_PLUGIN_NAME); free(err); return; } if (remote_init() == -1) return; register_signal(handler, "update config"); /* perfidnie podpinamy siê pod sygna³ dockleta */ register_signal(handler, "docklet set icon"); register_signal(handler, "docklet set default icon"); menu_remotemenu = remote_menu(); signal_emit(GGadu_PLUGIN_NAME, "gui register menu", menu_remotemenu, "main-gui"); }
int main() { char *regex = "*.bb.cc/*"; char *nstr = NULL; char *str = "aa.bb.cc/ddx/cc"; char *err_str = NULL; int status = 0; regex_t *comp_regex = malloc(sizeof(regex_t)); regmatch_t matches[1]; nstr = format_str(regex); printf("str %s", nstr); if (!(status = regcomp(comp_regex, nstr, REG_EXTENDED))) { if (!(regexec (comp_regex, str, 1, matches, 0))) { printf("Match"); } } if (status) { err_str = get_regerror(status, comp_regex); printf("ERROR: %s", err_str); } }
/* Parse the pattern given in the memory aread DATA/DATALEN and return a new pattern array. The end of the array is indicated by a NULL entry. On error an error message is printed and the fucntion returns NULL. Note that the function modifies DATA and assumes that data is nul terminated (even if this is one byte past DATALEN). */ static pattern_t * parse_pattern_file (char *data, size_t datalen) { char *p, *p2; size_t n; pattern_t *array; size_t arraysize, arrayidx; unsigned int lineno = 0; /* Estimate the number of entries by counting the non-comment lines. */ arraysize = 0; p = data; for (n = datalen; n && (p2 = memchr (p, '\n', n)); p2++, n -= p2 - p, p = p2) if (*p != '#') arraysize++; arraysize += 2; /* For the terminating NULL and a last line w/o a LF. */ array = xcalloc (arraysize, sizeof *array); arrayidx = 0; /* Loop over all lines. */ while (datalen && data) { lineno++; p = data; p2 = data = memchr (p, '\n', datalen); if (p2) { *data++ = 0; datalen -= data - p; } else p2 = p + datalen; assert (!*p2); p2--; while (isascii (*p) && isspace (*p)) p++; if (*p == '#') continue; while (p2 > p && isascii (*p2) && isspace (*p2)) *p2-- = 0; if (!*p) continue; assert (arrayidx < arraysize); array[arrayidx].lineno = lineno; if (*p == '/') { int rerr; p++; array[arrayidx].type = PAT_REGEX; if (*p && p[strlen(p)-1] == '/') p[strlen(p)-1] = 0; /* Remove optional delimiter. */ array[arrayidx].u.r.regex = xcalloc (1, sizeof (regex_t)); rerr = regcomp (array[arrayidx].u.r.regex, p, REG_ICASE|REG_NOSUB|REG_EXTENDED); if (rerr) { char *rerrbuf = get_regerror (rerr, array[arrayidx].u.r.regex); log_error ("invalid r.e. at line %u: %s\n", lineno, rerrbuf); xfree (rerrbuf); if (!opt.checkonly) exit (1); } } else { array[arrayidx].type = PAT_STRING; array[arrayidx].u.s.string = p; array[arrayidx].u.s.length = strlen (p); } arrayidx++; } assert (arrayidx < arraysize); array[arrayidx].type = PAT_NULL; return array; }
/* map rigid onto FEM state */ int dom_rigid_to_fem (DOM *dom, PBF *bf, SET *subset) { for (; bf; bf = bf->next) { if (PBF_Label (bf, "DOM")) { /* read iover */ int iover = 2; if (PBF_Label (bf, "IOVER")) { PBF_Int (bf, &iover, 1); } ASSERT_TEXT (iover >= 3, "Output files are too old for RIGID_TO_FEM to work"); /* read body states */ if (subset) { #if POSIX for (SET *item = SET_First (subset); item; item = SET_Next (item)) { regex_t xp; char *pattern = item->data; int error = regcomp (&xp, pattern, 0); if (error != 0) { char *message = get_regerror (error, &xp); fprintf (stderr, "-->\n"); fprintf (stderr, "Regular expression ERROR --> %s\n", message); fprintf (stderr, "<--\n"); regfree (&xp); free (message); return 0; } for (BODY *bod = dom->bod; bod; bod = bod->next) { if (bod->label && regexec (&xp, bod->label, 0, NULL, 0) == 0) { if (PBF_Label (bf, bod->label)) { double conf [12], velo [6], energy [4]; int rkind; int rconf; int rdofs; PBF_Int (bf, &rkind, 1); PBF_Int (bf, &rconf, 1); PBF_Int (bf, &rdofs, 1); if (bod->kind == FEM && rkind == RIG) { PBF_Double (bf, conf, 12); PBF_Double (bf, velo, 6); PBF_Double (bf, energy, 4); BODY_From_Rigid (bod, conf, conf+9, velo, velo+3); } else { ASSERT_TEXT (((bod->kind == RIG || bod->kind == OBS) && (rkind == RIG || rkind == OBS)) || bod->kind == (unsigned)rkind, "Body kind mismatch when reading state"); ASSERT_TEXT (BODY_Conf_Size (bod) == rconf, "Body configuration size mismatch when reading state"); ASSERT_TEXT (bod->dofs == rdofs, "Body dofs size mismatch when reading state"); BODY_Read_State (bod, bf, 0); /* use 0 state to skip reading of rkind, rnconf, rdofs */ } } } } regfree (&xp); } #else ASSERT_TEXT (0, "Regular expressions require POSIX support --> recompile Solfec with POSIX=yes"); return 0; #endif } else { ASSERT (PBF_Label (bf, "BODS"), ERR_FILE_FORMAT); int nbod; PBF_Int (bf, &nbod, 1); for (int n = 0; n < nbod; n ++) { double conf [12], velo [6], energy [4]; unsigned int id; BODY *bod; int rank; PBF_Uint (bf, &id, 1); bod = MAP_Find (dom->idb, (void*) (long) id, NULL); if (bod) /* update state of existing bodies only */ { int rkind; int rconf; int rdofs; PBF_Int (bf, &rkind, 1); PBF_Int (bf, &rconf, 1); PBF_Int (bf, &rdofs, 1); if (bod->kind == FEM && rkind == RIG) { PBF_Double (bf, conf, 12); PBF_Double (bf, velo, 6); PBF_Double (bf, energy, 4); if (bf->parallel == PBF_ON) { PBF_Int (bf, &rank, 1); } BODY_From_Rigid (bod, conf, conf+9, velo, velo+3); } else { ASSERT_TEXT (((bod->kind == RIG || bod->kind == OBS) && (rkind == RIG || rkind == OBS)) || bod->kind == (unsigned)rkind, "Body kind mismatch when reading state"); ASSERT_TEXT (BODY_Conf_Size (bod) == rconf, "Body configuration size mismatch when reading state"); ASSERT_TEXT (bod->dofs == rdofs, "Body dofs size mismatch when reading state"); BODY_Read_State (bod, bf, 0); /* use 0 state to skip reading of rkind, rnconf, rdofs */ } } else /* mock read */ { int rkind; int rconf; int rdofs; PBF_Int (bf, &rkind, 1); PBF_Int (bf, &rconf, 1); PBF_Int (bf, &rdofs, 1); double *conf; double *velo; double energy[10]; int rank; ERRMEM (conf = malloc (sizeof(double) * rconf)); ERRMEM (velo = malloc (sizeof(double) * rdofs)); PBF_Double (bf, conf, rconf); PBF_Double (bf, velo, rdofs); PBF_Double (bf, energy, BODY_ENERGY_SIZE(rkind)); if (bf->parallel == PBF_ON) { PBF_Int (bf, &rank, 1); } free (conf); free (velo); } } } } } return 1; }
/* initialize domain state */ int dom_init_state (DOM *dom, PBF *bf, SET *subset) { for (; bf; bf = bf->next) { if (PBF_Label (bf, "DOM")) { /* read iover */ int iover = 2; if (PBF_Label (bf, "IOVER")) { PBF_Int (bf, &iover, 1); } ASSERT_TEXT (iover >= 3, "Output files are too old for INITIALISE_STATE to work"); /* read body states */ if (subset) { #if POSIX for (SET *item = SET_First (subset); item; item = SET_Next (item)) { regex_t xp; char *pattern = item->data; int error = regcomp (&xp, pattern, 0); if (error != 0) { char *message = get_regerror (error, &xp); fprintf (stderr, "-->\n"); fprintf (stderr, "Regular expression ERROR --> %s\n", message); fprintf (stderr, "<--\n"); regfree (&xp); free (message); return 0; } for (BODY *bod = dom->bod; bod; bod = bod->next) { if (bod->label && regexec (&xp, bod->label, 0, NULL, 0) == 0) { if (PBF_Label (bf, bod->label)) { BODY_Read_State (bod, bf, iover); } } } regfree (&xp); } #else ASSERT_TEXT (0, "Regular expressions require POSIX support --> recompile Solfec with POSIX=yes"); return 0; #endif } else { ASSERT (PBF_Label (bf, "BODS"), ERR_FILE_FORMAT); int nbod; PBF_Int (bf, &nbod, 1); for (int n = 0; n < nbod; n ++) { unsigned int id; BODY *bod; PBF_Uint (bf, &id, 1); bod = MAP_Find (dom->idb, (void*) (long) id, NULL); if (bod) /* update state of existing bodies only */ { BODY_Read_State (bod, bf, iover); } else /* mock read */ { int rkind; int rconf; int rdofs; PBF_Int (bf, &rkind, 1); PBF_Int (bf, &rconf, 1); PBF_Int (bf, &rdofs, 1); double *conf; double *velo; double energy[10]; int rank; ERRMEM (conf = malloc (sizeof(double) * rconf)); ERRMEM (velo = malloc (sizeof(double) * rdofs)); PBF_Double (bf, conf, rconf); PBF_Double (bf, velo, rdofs); PBF_Double (bf, energy, BODY_ENERGY_SIZE(rkind)); if (bf->parallel == PBF_ON) { PBF_Int (bf, &rank, 1); } free (conf); free (velo); } } } } } return 1; }
int main(int argc, char * argv[]) { int opt_idx = parseOptions(argc, argv); std::string pattern; if(opts.f_flag == NULL) { if( opt_idx >= argc) { puts("Please provide a pattern (or pattern file) and at least one input file"); usage(usage_msg); } else if (opt_idx >= argc - 1) { puts("Please provide an input file"); usage(usage_msg); } pattern = argv[opt_idx]; ++opt_idx; manager.add(pattern); if(opts.r_flag) { std::string rcpattern = pattern; reverseComplement(rcpattern); manager.add(rcpattern); } } else { if (opt_idx > argc - 1) { puts("Please provide an input file"); usage(usage_msg); } std::ifstream in (opts.f_flag); if(!in.good()) { fprintf(stderr, "problem opening pattern file\n"); exit(1); } else { tokenizePatternFile(in); } } while(opt_idx < argc) { Fxstream stream; int stream_state = 1; if(! opts.S_flag && ! opts.I_flag) { // two read files stream_state = stream.open(argv[opt_idx], argv[opt_idx+1], opts.I_flag); } else { // one read file stream_state = stream.open(argv[opt_idx], NULL, opts.I_flag); } if(stream_state != 0) { fprintf(stderr, "Failed to open stream\n"); return 1; } if(opts.E_flag | opts.G_flag) { // Posix regex regex_t px_regex; int flags = REG_NOSUB; //only need to report success or failure if(opts.E_flag) { flags |= REG_EXTENDED; } else { flags |= REG_BASIC; } int ret = regcomp(&px_regex, pattern.c_str(), flags); if(ret) { char * errorbuf = get_regerror(ret, &px_regex); fprintf(stderr, "%s\n", errorbuf); free(errorbuf); return 1; } ret = posix_regex_search(stream, &px_regex); regfree(&px_regex); } #ifdef HAVE_PCRE else if (opts.P_flag) { // PCRE regex pcre *re; const char *error; int erroffset; re = pcre_compile( pattern.c_str(), /* the pattern */ 0, /* default options */ &error, /* for error message */ &erroffset, /* for error offset */ NULL); /* use default character tables */ if (re == NULL) { printf("PCRE compilation failed at offset %d: %s\n", erroffset, error); return 1; } pcre_search(stream, re); pcre_free(re); } #endif else if(opts.X_flag) { hash_search(stream); } else if(opts.f_flag) { multipattern_search(stream); } else { simple_string_search(stream, pattern.c_str()); } if(opts.c_flag) { printf("%d\n", matched_records); } if(! opts.S_flag && ! opts.I_flag) { opt_idx+=2; } else { opt_idx++; } } return 0; }
static bool atom_field_verify(struct atom * atom, FSAF * fp, size_t start, size_t end) { size_t len = end - start; struct fsaf_read_rv r = fsaf_read(fp, start, len); assert(r.len == len); switch (atom->mode) { case M_EXACT: if (len != atom->patlen) return false; if (atom->ignore_case) { return strncasecmp(atom->pat, r.b, len) == 0; } else { return strncmp(atom->pat, r.b, len) == 0; } case M_SUBSTR: { #if 0 if (atom->ignore_case) { return strncasestr(r.b, atom->pat, len); } else { return strnstr(r.b, atom->pat, len); } #else bool rv; char * s = strndup(r.b, len); if (s == 0) fatal_enomem(0); if (atom->ignore_case) { rv = strcasestr(s, atom->pat) != 0; } else { rv = strstr(s, atom->pat) != 0; } free(s); return rv; #endif } case M_REGEX: case M_EREGEX: { char * s = strndup(r.b, len); if (s == 0) fatal_enomem(0); int regex_errcode = regexec(&atom->regex, s, 0, 0, 0); free(s); if (regex_errcode == 0 || regex_errcode == REG_NOMATCH) { return (regex_errcode == 0); } /* Error handling be here. */ assert(regex_errcode != 0 && regex_errcode != REG_NOMATCH); s = get_regerror (regex_errcode, &atom->regex); if (s == 0) { enomem (0); return false; } message(L_IMPORTANT, 0, "%s", s); free(s); return false; } case M_VER_EQ:case M_VER_LT:case M_VER_LE:case M_VER_GT:case M_VER_GE: ; char *pats = strndup(atom->pat, atom->patlen); char *cands = strndup(r.b, len); struct versionrevision pat, cand; if (!parse_version(&pat, pats, atom->patlen)) { free(pats); free(cands); return false; } if (!parse_version(&cand, cands, len)) { free(pats); free(cands); return false; } int res = versioncompare(&cand, &pat); free(pats); free(cands); switch (atom->mode) { case M_VER_EQ: return res == 0; case M_VER_LT: return res < 0; case M_VER_LE: return res <= 0; case M_VER_GT: return res > 0; case M_VER_GE: return res >= 0; default: assert(0); } } assert(0); }