21 #define CALL_IF_DEBUG_MODE(x) 23 #define CALL_IF_DEBUG_MODE(x) x 26 #include "nomos_regex.h" 27 #include "nomos_gap.h" 28 #include "nomos_utils.h" 39 regex_t idx_regc[NFOOTPRINTS];
52 traceFunc(
"== regexError(%d, %p, %s)\n", ret, regc, regex);
56 Msg(
"regex = \"%s\"\n", regex);
69 int slen = (int) strlen(s);
70 int sufflen = (int) strlen(suffix);
76 traceFunc(
"== endsIn(%s, %s)\n", s, suffix);
79 if (strncasecmp(s + slen - sufflen, suffix, (
size_t) sufflen) == 0)
97 traceFunc(
"== lineInFile(%s, \"%s\")\n", pathname, regex);
100 (void) sprintf(buf,
"^%s$", regex);
101 return (
textInFile(pathname, buf, REG_NEWLINE));
117 traceFunc(
"== textInFile(%s, \"%s\", 0x%x)\n", pathname, regex, flags);
125 Assert(
NO,
"textInFile: NULL pathname");
129 Assert(
NO,
"textInFile: NULL regex");
138 ret =
strGrep(regex, textp, flags);
150 int strGrep(
char *regex,
char *data,
int flags)
159 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG) 160 traceFunc(
"== strGrep(\"%s\", %p, 0x%x)\n", regex, data, flags);
168 if ((ret = regcomp(®c, regex, flags)) != 0)
179 ret = regexec(®c, data, 1, &cur.regm, 0);
186 if (cur.regm.rm_so == cur.regm.rm_eo)
188 Assert(
NO,
"start/end offsets are identical in strGrep()");
192 printf(
"strGrep MATCH(%s) @ %d! = {", regex, cur.regm.rm_so);
193 for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
195 printf(
"%c", data[i]);
201 cur.matchBase = data;
293 int matchOnce(
int isPlain,
char *data,
char* regex, regex_t *rp,
294 regmatch_t* regmatch)
300 return regexec(rp, data, 1, regmatch, 0);
312 int storeOneMatch(regmatch_t currentRegMatch,
int lastmatch, GArray* allmatches,
313 char** tmpData,
char* data)
315 regmatch_t storeRegMatch;
316 storeRegMatch.rm_so = currentRegMatch.rm_so + lastmatch;
317 storeRegMatch.rm_eo = currentRegMatch.rm_eo + lastmatch;
318 g_array_append_val(allmatches, storeRegMatch);
319 lastmatch += currentRegMatch.rm_eo;
320 *tmpData = data + lastmatch;
340 int show = flags & FL_SHOWMATCH;
346 regex_t *rp = idx_regc + index;
348 CALL_IF_DEBUG_MODE(printf(
" %i %i \"", index, ltp->plain);)
350 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG) 351 traceFunc(
"== idxGrep(%d, %p, 0x%x)\n... regex \"%s\"\n", index, data,
355 if (index > NFOOTPRINTS)
357 LOG_FATAL(
"idxGrep: index %d out of range", index)
363 printf(
"idxGrep: NULL pointer to file data!\n");
371 if(ret == 0)
return (ret);
374 if ((ret = regcomp(rp, ltp->
regex, flags)))
376 fprintf(stderr,
"Compile failed, regex #%d\n", index);
379 printf(
"Compile error \n");
383 if (regexec(rp, data, 1, &cur.regm, 0))
391 if (cur.regm.rm_so == cur.regm.rm_eo)
394 Assert(
NO,
"start/end offsets are identical in idxGrep(%d)",
402 printf(
"REGEX(%d) \"%s\"\n", index, ltp->
regex);
404 printf(
"MATCH @ %d! = {", cur.regm.rm_so);
405 for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
407 printf(
"%c", data[i]);
413 cur.matchBase = data;
422 else if (mode==1 || mode == 2)
424 CALL_IF_DEBUG_MODE(printf(
"MATCH!\n");)
427 CALL_IF_DEBUG_MODE(printf(
"%s", data);)
430 GArray* allmatches = g_array_new(FALSE, FALSE,
sizeof(regmatch_t));
431 regmatch_t currentRegMatch;
434 char* tmpData = data;
436 lastmatch =
storeOneMatch(cur.regm, lastmatch, allmatches, &tmpData, data);
438 while (!
matchOnce(ltp->plain,tmpData, ltp->
regex, rp, ¤tRegMatch ) )
440 lastmatch =
storeOneMatch(currentRegMatch, lastmatch, allmatches, &tmpData, data);
444 if(index >= _KW_first && index <= _KW_last ) {
447 else if (cur.currentLicenceIndex > -1 ) {
450 g_array_free(allmatches, 1);
451 CALL_IF_DEBUG_MODE(printf(
"Bye!\n");)
454 if (!ltp->plain ) regfree(rp);
464 g_array_append_val(indexList, index);
475 return uncollapsePosition(posInDoctoredBuffer, cur.docBufferPositionsAndOffsets);
486 return & g_array_index(in, regmatch_t, index);
500 if (mode != 1 && mode != 2)
502 FOSSY_EXIT(
"This mode is not supported\n", 8);
507 int nmatches = regmatch_tArray->len;
508 int alreadyFound = highlight->len;
509 g_array_set_size(highlight, alreadyFound + nmatches);
511 for (i = 0; i < nmatches; ++i)
514 if (theRegmatch->rm_eo == -1 || theRegmatch->rm_so == -1)
516 FOSSY_EXIT(
"Found match at negative position... this should not happen\n", 9);
521 ourMatchv->
start = (mode == 1) ? theRegmatch->rm_so :
getOffset(theRegmatch->rm_so);
522 ourMatchv->
end = (mode == 1) ? theRegmatch->rm_eo :
getOffset(theRegmatch->rm_eo);
523 ourMatchv->
index = index;
525 CALL_IF_DEBUG_MODE(printf(
"here: %i - %i \n", ourMatchv->
start, ourMatchv->
end);)
527 CALL_IF_DEBUG_MODE(printf(
" We go and now we know %d ", highlight->len);)
530 #define _XC(q) ((char) xascii[q]) 552 static int firstFlag = 1;
553 static char xascii[128];
563 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG) 564 traceFunc(
"== strNbuf(%p, %p)\n", data, str);
574 for (i = 0; i <
sizeof(xascii); i++)
576 if ((i >= 65) && (i <= 90))
580 else if ((i >= 97) && (i <= 122))
587 xascii[i] = (char) 0;
594 for (i = 0; i <
sizeof (xascii); i++)
598 printf(
" %c%c ", (
unsigned) i, xascii[i]);
602 printf(
"\\%03d ", (
int) xascii[i]);
612 printf(
"DATA \"%s\"\nPATT \"%s\"\n", data, str);
618 alph = isalpha(*str);
621 firstx = xascii[(int) *str];
623 printf(
"NOTE: first char (%c) is Alphabetic - alternate is (%c)\n",
629 LOG_FATAL(
"Unexpected initialization")
634 for (bufp = data; *bufp; bufp = mark)
637 printf(
"\nDEBUG: start, buffer = \"%s\"\n", bufp);
646 printf(
"... findfirst, *bufp is '%c' == [%c%c]?\n",
647 *bufp, *str, alph ? firstx : *str);
653 if (alph && (*bufp == firstx))
666 printf(
"GOT IT, at offset %d (*mark now is '%c')\n",
667 bufp - data - 1, *mark);
670 for (++pattp; *bufp && *pattp; bufp++, pattp++)
673 printf(
"STRING-COMPARE: %c == %c ??\n", *bufp, *pattp);
680 printf(
"... or perhaps: %c == %c ??\n", *bufp,
683 if (((x = xascii[(
int) *pattp])) && (*bufp == x))
691 matchPos->rm_so = save;
692 matchPos->rm_eo = save + strlen(str);
regmatch_t * getRegmatch_t(GArray *in, int index)
From a given array, get regex match from a given index.
int optionIsSet(int val)
Check if an CLI option is set.
int idxGrep_base(int index, char *data, int flags, int mode)
compile a regex, and perform the search (on data?)
int idxGrep_recordPosition(int index, char *data, int flags)
compile a regex, perform the search and record findings
static char regexErrbuf[myBUFSIZ]
int s
The socket that the CLI will use to communicate.
static int getOffset(int posInDoctoredBuffer)
Get offset from doctored buffer.
GArray * matchPositions
Match positions.
void Bail(int exitval)
Close connections and exit.
int endsIn(char *s, char *suffix)
Check if a string ends with given suffix.
int textInFile(char *pathname, char *regex, int flags)
Check if a regex passes in a file.
int idxGrep_recordPositionDoctored(int index, char *data, int flags)
compile a regex, perform the search and record findings
int start
Start position of match.
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
int strNbuf(char *data, char *str)
Check if a string exists in buffer (case insensitive)
int lineInFile(char *pathname, char *regex)
Check if a line exists in a file.
void Msg(const char *fmt,...)
DO NOT automatically add to a string passed to Msg(); in parseDistro, we sometimes want to dump a p...
#define NULL_CHAR
NULL character.
void Assert(int fatalFlag, const char *fmt,...)
Raise an assert.
int idxGrep(int index, char *data, int flags)
compile a regex, and perform the search (on data?)
char * mmapFile(char *pathname)
Blarg. Files that are EXACTLY a multiple of the system pagesize do not get a NULL on the end of the b...
void recordIndex(GArray *indexList, int index)
Add a given index to index list.
int storeOneMatch(regmatch_t currentRegMatch, int lastmatch, GArray *allmatches, char **tmpData, char *data)
Store a single regex match to array.
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
int strNbuf_noGlobals(char *data, char *str, regmatch_t *matchPos, int doSave, char *saveData)
This is our own internal, case-insensitive version of strstr().
int index
Enums from index (Entrynumber) in STRINGS.in.
int end
End position of match.
#define NULL_STR
NULL string.
void rememberWhatWeFound(GArray *highlight, GArray *regmatch_tArray, int index, int mode)
Store regex matches in highlight array.
int strGrep(char *regex, char *data, int flags)
General-purpose grep function, used for one-time-only searches.
int matchOnce(int isPlain, char *data, char *regex, regex_t *rp, regmatch_t *regmatch)
Perform a regex match on a given data and return only first match.
void munmapFile(void *ptr)
int idxGrep_recordIndex(int index, char *data, int flags)
compile a regex, perform the search and record index
char * regex
License regex.
GArray * keywordPositions
void regexError(int ret, regex_t *regc, char *regex)
Log an error caused by regex.