colour != Col || spc->attrib != Att) {
*b++ = 0x1B;
*b++ = '[';
if (spc->attrib != Att) {
*b++ = Att = spc->attrib ;
if (spc->colour != Col)
*b++ = ';';
}
if (spc->colour != Col) {
*b++ = '3';
*b++ = Col = spc->colour ;
}
*b++ = 'm';
}
*b++ = *p++;
}
*b++ = 0x1B;
*b++ = '[';
*b++ = Spc->attrib;
*b++ = ';';
*b++ = '3';
*b++ = Spc->colour;
*b++ = 'm';
*b++ = 0;
if (opt_line_number) printf ("%5d: ", inp_lineno);
puts (output);
}
void
create_rex (FILE *fp)
{
char buffer [BUFFER], *p;
int n;
spc_t *spc;
memset (Spc, 0, sizeof (Spc));
spc = Spc;
spc->aryidx = 0;
spc->colour = WHI;
spc->attrib = '0';
spc->htmrgb = (opt_reverse) ? "Black" : "White" ;
spc->htmlen = strlen (spc->htmrgb);
spc++;
/*
* This loop reads each line of the config file and
* processes it according to its pattern type field.
* If it is a RE ('r' or 't') it is compiled. If it
* is a simple character set ('c') or string ('s')
* the strings are just stored.
*/
while (fgets (buffer, sizeof (buffer), fp)) {
n = strlen (buffer);
if (n == BUFFER)
err_quit ("line too long: %s", buffer);
inp_lineno++;
buffer [n-1] = 0;
buffer [COLUMN_COLOUR+3] = 0;
if (*buffer == '#') continue; /* comment */
if (*buffer == 0 ) continue; /* blank line */
if (buffer [COLUMN_HTMRGB] == ' ')
strcpy (&buffer [COLUMN_HTMRGB], (opt_reverse) ? "Black" : "White");
else {
p = &buffer [COLUMN_HTMRGB];
while (*p != ' ') p++ ;
*p = 0;
}
spc->aryidx = Spn;
spc->colour = colour_lookup (&buffer [COLUMN_COLOUR]);
spc->attrib = attrib_lookup ( buffer [COLUMN_ATTRIB]);
spc->number = number_lookup ( buffer [COLUMN_NUMBER]);
spc->patype = patype_lookup ( buffer [COLUMN_PATYPE]);
spc->htmrgb = malloc (strlen (&buffer [COLUMN_HTMRGB]) + 1);
strcpy (spc->htmrgb,&buffer [COLUMN_HTMRGB]);
spc->htmlen = strlen (spc->htmrgb);
if (spc->patype == PATYPE_REGEXP ||
spc->patype == PATYPE_UNIXTM) {
/*
* Compile the regular expression
*/
if ((n = regcomp (&spc->buffer, buffer+COLUMN_CREGEX, REG_EXTENDED))) {
fprintf (stderr, "regcomp error: %s\n", buffer+COLUMN_CREGEX);
regerror (n, &spc->buffer, buffer, sizeof (buffer));
err_quit ("%s", buffer);
}
} else if (spc->patype == PATYPE_CHARS) {
/*
* Allocate a 256 byte array, one for each ASCII
* character and set each location to 1 that has
* a corresponding character in the input string.
*/
spc->string = malloc (256);
memset (spc->string, 0, 256);
n=strlen (buffer+COLUMN_CREGEX-1);
for (p=buffer+COLUMN_CREGEX; *p; p++)
spc->string [*p] = 1;
} else {
/*
* just save the string expression
*/
spc->string = malloc (strlen (buffer+COLUMN_CREGEX-1));
strcpy (spc->string, buffer+COLUMN_CREGEX);
}
Spn++;
spc++;
}
if (Spn == 1)
err_quit ("config file %s is invalid", opt_config);
}
void
initialize_con ()
{
putchar (0x1B);
putchar ('[');
putchar (Att = Spc->attrib);
putchar (';');
putchar ('3');
putchar (Col = Spc->colour);
putchar ('m');
}
void
finalize_con ()
{
putchar (0x1B);
putchar ('[');
putchar ('1');
putchar (';');
putchar ('3');
putchar ('7');
putchar ('m');
}
void
initialize_htm ()
{
puts ("");
puts ("");
puts ("");
printf ("Colorized by supercat %s\n", VERSION);
puts ("");
printf ("\n", (opt_reverse) ? "White" : "Black" );
puts ("");
}
void
finalize_htm ()
{
puts ("");
puts ("");
puts ("");
}
FILE *
input_open ()
{
FILE *in = stdin;
struct stat status_buffer [1];
if (inp_file) {
if (stat (inp_file, status_buffer) == -1)
err_sys ("can't stat input file %s", inp_file);
if (!S_ISREG (status_buffer->st_mode))
err_quit ("input file %s is not a regular file", inp_file);
if (!(in = fopen (inp_file, "r")))
err_sys ("can't open input file %s", inp_file);
}
return (in);
}
int
main (int ac, char *av[])
{
char string [BUFFER];
char aryidx [BUFFER];
FILE *in;
int rtn, n;
options (ac, av);
create_rex (configure ());
in = input_open ();
if (opt_webpage)
initialize_htm ();
else
initialize_con ();
inp_lineno = 0;
while (fgets (string, sizeof (string), in)) {
string [n=strlen (string)-1] = 0;
inp_lineno++;
rtn = colour_sca (string, aryidx, n);
if (opt_webpage) {
if (!opt_matching || rtn)
printf_htm (string, aryidx, n);
} else {
if (!opt_matching || rtn)
printf_con (string, aryidx, n);
}
}
if (opt_webpage)
finalize_htm ();
else
finalize_con ();
return (0);
}