#! /usr/bin/python

import sys, os, string, getopt, re


#some default definitions
colours = {	
			'none'		:	"",
			'black'		:	"\033[30m", 
			'red'		:	"\033[31m",
			'green'		:	"\033[32m",
			'yellow'	:	"\033[33m",
			'blue'		:	"\033[34m",
			'magenta'	:	"\033[35m",
			'purple'	:	"\033[35m",
			'cyan'		:	"\033[36m",
			'white'		:	"\033[37m",
			'darkgray'	:	"\033[30m",
			'default'	:	"\033[0m"
			}


conffilepath = [os.environ['HOME']+"/.grc/", "/usr/local/share/grc/", "/usr/share/grc/", ""]
for i in conffilepath:
	if os.path.isfile(i+sys.argv[1]):
		conffile = i+sys.argv[1]
		break
regexplist = []

f = open(conffile, "r")
while 1:
	l = f.readline()
	if l == "" :
		break
	if l[0] == "#" or l[0] == '\012':
		continue
	colour = f.readline()[:-1]
	cs = f.readline()[:-1]
	regexplist.append((re.compile(l[:-1]), colours[colour], cs))

f = sys.stdin
while 1:
	l = f.readline()
	if l == "" :
		break
	line = l[:-1]
	clist = []
	for pattern in regexplist:
		pos = 0
		while 1:
			m = pattern[0].search(line, pos)
			if m != None:
				clist.append((m.start(0), m.end(0), pattern[1]))
				if pattern[2] == "more": pos = m.end(0)
				else: pos = sys.maxint
			else: break
		if m != None and pattern[2] == "stop": break
	cline = (len(line)+1)*['']
	for i in clist:
		cline[i[0]] = i[2]
		if cline[i[1]] == '': cline[i[1]] = colours['default']
	nline = ""
	for i in range(len(line)):
		nline = nline + cline[i] + line[i]
	nline = nline + colours['default']
	print nline
		
