bool iswword(wstring _String) { for (wstring::const_iterator iter = _String.begin(); iter != _String.end(); iter++) { if (!iswword(*iter)) { return false; } } return true; }
/* * Processes a line comparing it with the specified patterns. Each pattern * is looped to be compared along with the full string, saving each and every * match, which is necessary to colorize the output and to count the * matches. The matching lines are passed to printline() to display the * appropriate output. */ static int procline(struct str *l, int nottext) { regmatch_t matches[MAX_LINE_MATCHES]; regmatch_t pmatch; size_t st = 0; unsigned int i; int c = 0, m = 0, r = 0; /* Loop to process the whole line */ while (st <= l->len) { pmatch.rm_so = st; pmatch.rm_eo = l->len; /* Loop to compare with all the patterns */ for (i = 0; i < patterns; i++) { if (fg_pattern[i].pattern) r = fastexec(&fg_pattern[i], l->dat, 1, &pmatch, eflags); else r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags); r = (r == 0) ? 0 : REG_NOMATCH; st = (cflags & REG_NOSUB) ? (size_t)l->len : (size_t)pmatch.rm_eo; if (r == REG_NOMATCH) continue; /* Check for full match */ if (r == 0 && xflag) if (pmatch.rm_so != 0 || (size_t)pmatch.rm_eo != l->len) r = REG_NOMATCH; /* Check for whole word match */ if (r == 0 && (wflag || fg_pattern[i].word)) { wchar_t wbegin, wend; wbegin = wend = L' '; if (pmatch.rm_so != 0 && sscanf(&l->dat[pmatch.rm_so - 1], "%lc", &wbegin) != 1) r = REG_NOMATCH; else if ((size_t)pmatch.rm_eo != l->len && sscanf(&l->dat[pmatch.rm_eo], "%lc", &wend) != 1) r = REG_NOMATCH; else if (iswword(wbegin) || iswword(wend)) r = REG_NOMATCH; } if (r == 0) { if (m == 0) c++; if (m < MAX_LINE_MATCHES) matches[m++] = pmatch; /* matches - skip further patterns */ if ((color == NULL && !oflag) || qflag || lflag) break; } } if (vflag) { c = !c; break; } /* One pass if we are not recording matches */ if (!wflag && ((color == NULL && !oflag) || qflag || lflag || Lflag)) break; if (st == (size_t)pmatch.rm_so) break; /* No matches */ } /* Count the matches if we have a match limit */ if (mflag) mcount -= c; if (c && binbehave == BINFILE_BIN && nottext) return (c); /* Binary file */ /* Dealing with the context */ if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) { if (c) { if (!first && !prev && !tail && Aflag) printf("--\n"); tail = Aflag; if (Bflag > 0) { if (!first && !prev) printf("--\n"); printqueue(); } linesqueued = 0; printline(l, ':', matches, m); } else { printline(l, '-', matches, m); tail--; } } if (c) { prev = true; first = false; } else prev = false; return (c); }