Here is a snippet for a log coloring tool I sometimes use.
Note that is only works against stdin/stdout and in a terminal supporting ANSI colors.
#include <stdio.h>
#include <regex.h>
#define MAX_LINE 4096
#define RESET "33[0m"
#define BLACK "33[30m" /* Black */
#define RED "33[31m" /* Red */
#define GREEN "33[32m" /* Green */
#define YELLOW "33[33m" /* Yellow */
#define BLUE "33[34m" /* Blue */
#define MAGENTA "33[35m" /* Magenta */
#define CYAN "33[36m" /* Cyan */
#define WHITE "33[37m" /* White */
#define BOLDBLACK "33[1m33[30m" /* Bold Black */
#define BOLDRED "33[1m33[31m" /* Bold Red */
#define BOLDGREEN "33[1m33[32m" /* Bold Green */
#define BOLDYELLOW "33[1m33[33m" /* Bold Yellow */
#define BOLDBLUE "33[1m33[34m" /* Bold Blue */
#define BOLDMAGENTA "33[1m33[35m" /* Bold Magenta */
#define BOLDCYAN "33[1m33[36m" /* Bold Cyan */
#define BOLDWHITE "33[1m33[37m" /* Bold White */
static int selected_color = 0;
static char *colors[] = {
"-green", GREEN,
"-black", BLACK,
"-red", RED,
"-yellow", YELLOW,
"-blue", BLUE,
"-magenta", MAGENTA,
"-cyan", CYAN,
"-white", WHITE,
"-boldgreen", BOLDGREEN,
"-boldblack", BOLDBLACK,
"-boldred", BOLDRED,
"-boldyellow", BOLDYELLOW,
"-boldblue", BOLDBLUE,
"-boldmagenta", BOLDMAGENTA,
"-boldcyan", BOLDCYAN,
"-boldwhite", BOLDWHITE,
NULL
};
/*----------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
char buf[MAX_LINE];
int has_re = 0;
regex_t re;
if (argc > 1) {
if (argc > 2) {
int idx = 0;
while (colors[idx*2]) {
if (!strcmp(colors[idx*2], argv[1])) {
selected_color = idx;
break;
}
idx++;
}
if (regcomp(&re, argv[2], REG_EXTENDED | REG_NEWLINE)) {
printf("regcomp() failed!
");
return -1;
}
} else if (regcomp(&re, argv[1], REG_EXTENDED | REG_NEWLINE)) {
printf("regcomp() failed!
");
return -1;
}
has_re = 1;
} else {
printf("Usage: %s [ -red | -blue | -cyan | -white | -black | "
"-yellow | -magenta ] <regexp>
", argv[0]);
return -1;
}
while (fgets(buf, MAX_LINE, stdin) == buf) {
char *bbuf = buf;
while (1) {
if (has_re) {
regmatch_t match[10];
if (regexec(&re, bbuf, re.re_nsub + 1, match, 0)) {
printf("%s", bbuf);
break;
} else {
int i, idx;
for (i=idx=0; i<1; i++) {
if (match[0].rm_so < 0) {
break;
} else {
printf("%.*s",
(int)(match[i].rm_so-idx),
bbuf+idx);
printf( "%s%.*s" RESET,
colors[selected_color*2+1],
(int)(match[i].rm_eo-match[i].rm_so),
bbuf+(int)match[i].rm_so);
idx = match[i].rm_eo;
bbuf += idx;
}
}
}
}
fflush(stdout);
}
}
if (has_re) {
regfree(&re);
}
return 0;
}