9 #define CALL_IF_DEBUG_MODE(x)
11 #define CALL_IF_DEBUG_MODE(x) x
14 #include "nomos_regex.h"
15 #include "nomos_gap.h"
16 #include "nomos_utils.h"
27 regex_t idx_regc[NFOOTPRINTS];
28 regex_t regc[NFOOTPRINTS];
41 traceFunc(
"== regexError(%d, %p, %s)\n", ret, regc, regex);
45 Msg(
"regex = \"%s\"\n", regex);
58 int slen = (int) strlen(
s);
59 int sufflen = (int) strlen(suffix);
65 traceFunc(
"== endsIn(%s, %s)\n",
s, suffix);
68 if (strncasecmp(
s + slen - sufflen, suffix, (
size_t) sufflen) == 0)
86 traceFunc(
"== lineInFile(%s, \"%s\")\n", pathname, regex);
89 (void) sprintf(buf,
"^%s$", regex);
90 return (
textInFile(pathname, buf, REG_NEWLINE));
106 traceFunc(
"== textInFile(%s, \"%s\", 0x%x)\n", pathname, regex, flags);
114 Assert(
NO,
"textInFile: NULL pathname");
118 Assert(
NO,
"textInFile: NULL regex");
127 ret =
strGrep(regex, textp, flags);
139 int strGrep(
char *regex,
char *data,
int flags)
148 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
149 traceFunc(
"== strGrep(\"%s\", %p, 0x%x)\n", regex, data, flags);
157 if ((ret = regcomp(®c, regex, flags)) != 0)
168 ret = regexec(®c, data, 1, &cur.regm, 0);
175 if (cur.regm.rm_so == cur.regm.rm_eo)
177 Assert(
NO,
"start/end offsets are identical in strGrep()");
181 printf(
"strGrep MATCH(%s) @ %d! = {", regex, cur.regm.rm_so);
182 for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
184 printf(
"%c", data[i]);
190 cur.matchBase = data;
282 int matchOnce(
int isPlain,
char *data,
char* regex, regex_t *rp,
283 regmatch_t* regmatch)
289 return regexec(rp, data, 1, regmatch, 0);
301 int storeOneMatch(regmatch_t currentRegMatch,
int lastmatch, GArray* allmatches,
302 char** tmpData,
char* data)
304 regmatch_t storeRegMatch;
305 storeRegMatch.rm_so = currentRegMatch.rm_so + lastmatch;
306 storeRegMatch.rm_eo = currentRegMatch.rm_eo + lastmatch;
307 g_array_append_val(allmatches, storeRegMatch);
308 lastmatch += currentRegMatch.rm_eo;
309 *tmpData = data + lastmatch;
329 int show = flags & FL_SHOWMATCH;
335 regex_t *rp = idx_regc + index;
337 CALL_IF_DEBUG_MODE(printf(
" %i %i \"", index, ltp->plain);)
339 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
340 traceFunc(
"== idxGrep(%d, %p, 0x%x)\n... regex \"%s\"\n", index, data,
344 if (index > NFOOTPRINTS)
346 LOG_FATAL(
"idxGrep: index %d out of range", index)
352 printf(
"idxGrep: NULL pointer to file data!\n");
360 if(ret == 0)
return (ret);
363 if ((ret = regcomp(rp, ltp->
regex, flags)))
365 fprintf(stderr,
"Compile failed, regex #%d\n", index);
368 printf(
"Compile error \n");
372 if (regexec(rp, data, 1, &cur.regm, 0))
380 if (cur.regm.rm_so == cur.regm.rm_eo)
383 Assert(
NO,
"start/end offsets are identical in idxGrep(%d)",
391 printf(
"REGEX(%d) \"%s\"\n", index, ltp->
regex);
393 printf(
"MATCH @ %d! = {", cur.regm.rm_so);
394 for (i = cur.regm.rm_so; i < cur.regm.rm_eo; i++)
396 printf(
"%c", data[i]);
402 cur.matchBase = data;
411 else if (mode==1 || mode == 2)
413 CALL_IF_DEBUG_MODE(printf(
"MATCH!\n");)
416 CALL_IF_DEBUG_MODE(printf(
"%s", data);)
419 GArray* allmatches = g_array_new(FALSE, FALSE,
sizeof(regmatch_t));
420 regmatch_t currentRegMatch;
423 char* tmpData = data;
425 lastmatch =
storeOneMatch(cur.regm, lastmatch, allmatches, &tmpData, data);
427 while (!
matchOnce(ltp->plain,tmpData, ltp->
regex, rp, ¤tRegMatch ) )
429 lastmatch =
storeOneMatch(currentRegMatch, lastmatch, allmatches, &tmpData, data);
433 if(index >= _KW_first && index <= _KW_last ) {
436 else if (cur.currentLicenceIndex > -1 ) {
439 g_array_free(allmatches, 1);
440 CALL_IF_DEBUG_MODE(printf(
"Bye!\n");)
443 if (!ltp->plain ) regfree(rp);
453 g_array_append_val(indexList, index);
464 return uncollapsePosition(posInDoctoredBuffer, cur.docBufferPositionsAndOffsets);
475 return & g_array_index(in, regmatch_t, index);
489 if (mode != 1 && mode != 2)
491 FOSSY_EXIT(
"This mode is not supported\n", 8);
496 int nmatches = regmatch_tArray->len;
497 int alreadyFound = highlight->len;
498 g_array_set_size(highlight, alreadyFound + nmatches);
500 for (i = 0; i < nmatches; ++i)
503 if (theRegmatch->rm_eo == -1 || theRegmatch->rm_so == -1)
505 FOSSY_EXIT(
"Found match at negative position... this should not happen\n", 9);
510 ourMatchv->
start = (mode == 1) ? theRegmatch->rm_so :
getOffset(theRegmatch->rm_so);
511 ourMatchv->
end = (mode == 1) ? theRegmatch->rm_eo :
getOffset(theRegmatch->rm_eo);
512 ourMatchv->
index = index;
514 CALL_IF_DEBUG_MODE(printf(
"here: %i - %i \n", ourMatchv->
start, ourMatchv->
end);)
516 CALL_IF_DEBUG_MODE(printf(
" We go and now we know %d ", highlight->len);)
519 #define _XC(q) ((char) xascii[q])
541 static int firstFlag = 1;
542 static char xascii[128];
552 #if defined(PROC_TRACE) || defined(PHRASE_DEBUG)
553 traceFunc(
"== strNbuf(%p, %p)\n", data, str);
563 for (i = 0; i <
sizeof(xascii); i++)
565 if ((i >= 65) && (i <= 90))
569 else if ((i >= 97) && (i <= 122))
576 xascii[i] = (char) 0;
583 for (i = 0; i <
sizeof (xascii); i++)
587 printf(
" %c%c ", (
unsigned) i, xascii[i]);
591 printf(
"\\%03d ", (
int) xascii[i]);
601 printf(
"DATA \"%s\"\nPATT \"%s\"\n", data, str);
607 alph = isalpha(*str);
610 firstx = xascii[(int) *str];
612 printf(
"NOTE: first char (%c) is Alphabetic - alternate is (%c)\n",
618 LOG_FATAL(
"Unexpected initialization")
623 for (bufp = data; *bufp; bufp = mark)
626 printf(
"\nDEBUG: start, buffer = \"%s\"\n", bufp);
635 printf(
"... findfirst, *bufp is '%c' == [%c%c]?\n",
636 *bufp, *str, alph ? firstx : *str);
642 if (alph && (*bufp == firstx))
655 printf(
"GOT IT, at offset %d (*mark now is '%c')\n",
656 bufp - data - 1, *mark);
659 for (++pattp; *bufp && *pattp; bufp++, pattp++)
662 printf(
"STRING-COMPARE: %c == %c ??\n", *bufp, *pattp);
669 printf(
"... or perhaps: %c == %c ??\n", *bufp,
672 if (((x = xascii[(
int) *pattp])) && (*bufp == x))
680 matchPos->rm_so = save;
681 matchPos->rm_eo = save + strlen(str);
int s
The socket that the CLI will use to communicate.
void munmapFile(void *ptr)
void Assert(int fatalFlag, const char *fmt,...)
Raise an assert.
void Msg(const char *fmt,...)
DO NOT automatically add to a string passed to Msg(); in parseDistro, we sometimes want to dump a p...
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...
#define NULL_STR
NULL string.
void Bail(int exitval)
Close connections and exit.
#define NULL_CHAR
NULL character.
int optionIsSet(int val)
Check if an CLI option is set.
int storeOneMatch(regmatch_t currentRegMatch, int lastmatch, GArray *allmatches, char **tmpData, char *data)
Store a single regex match to array.
int idxGrep_base(int index, char *data, int flags, int mode)
compile a regex, and perform the search (on data?)
regmatch_t * getRegmatch_t(GArray *in, int index)
From a given array, get regex match from a given index.
int idxGrep_recordPosition(int index, char *data, int flags)
compile a regex, perform the search and record findings
int idxGrep(int index, char *data, int flags)
compile a regex, and perform the search (on data?)
int lineInFile(char *pathname, char *regex)
Check if a line exists in a file.
static char regexErrbuf[myBUFSIZ]
void recordIndex(GArray *indexList, int index)
Add a given index to index list.
int strGrep(char *regex, char *data, int flags)
General-purpose grep function, used for one-time-only searches.
int idxGrep_recordPositionDoctored(int index, char *data, int flags)
compile a regex, perform the search and record findings
int strNbuf(char *data, char *str)
Check if a string exists in buffer (case insensitive)
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 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.
int endsIn(char *s, char *suffix)
Check if a string ends with given suffix.
void regexError(int ret, regex_t *regc, char *regex)
Log an error caused by regex.
int idxGrep_recordIndex(int index, char *data, int flags)
compile a regex, perform the search and record index
int textInFile(char *pathname, char *regex, int flags)
Check if a regex passes in a file.
void rememberWhatWeFound(GArray *highlight, GArray *regmatch_tArray, int index, int mode)
Store regex matches in highlight array.
static int getOffset(int posInDoctoredBuffer)
Get offset from doctored buffer.
FUNCTION MatchPositionAndType * getMatchfromHighlightInfo(GArray *in, int index)
Get the MatchPositionAndType for a given index in highlight array.
FUNCTION LicenceAndMatchPositions * getLicenceAndMatchPositions(GArray *in, int index)
Get the LicenceAndMatchPositions for a given index in match array.
GArray * matchPositions
Match positions.
int start
Start position of match.
int index
Enums from index (Entrynumber) in STRINGS.in.
int end
End position of match.
GArray * keywordPositions
char * regex
License regex.