#include<fstream>
#include<iostream>
#include<stdio.h>
#define BRACES
using namespace std;

const char grey[] = "\e[1;30m";
const char grey_bg[] = "\e[7;30m";
const char red[] = "\e[0;31m";
const char green[] = "\e[0;32m";
const char blue[] = "\e[0;34m";
const char pink[] = "\e[1;35m";
const char purple[] = "\e[0;35m";
const char cyan[] = "\e[0;36m";
const char nocolor[] = "\e[0m";
#ifdef BRACES
const char brown[] = "\e[0;33m";
#endif

/* Copyleft Andreas Nordal
 * all rights reversed
 */

int main(int argc, char *argv[]){

  for(int i=1; i < argc; ++i){
    ifstream straum(argv[i]);
    if(straum.fail()){
      perror(argv[i]);
      return 1;
    }else{
      char A=0, B=0, C;
      char modus=0;
#ifdef BRACES
      char togglebox=0x02;
#endif
      while(straum.get(C)){
	if(modus){
	  if(modus==1 && B=='"') modus=0;
	  else if(modus==2 && B=='\'') modus=0;
	  else if(modus>=5 && modus<=6 && B=='\n') modus=0;
	  else if(modus==7 && A=='*' && B=='/') modus=0;
	  if(!modus){
	    putchar(B);
            cout << nocolor;
	    goto end_of_while;
	  }else if(modus==11 && ((B>0 && B<'0') ||
	          (B>'9' && B<'A') || (B>'Z' && B<'_') ||
	          B>'z' || B=='`')){
	    modus=0;
	  }else if(modus==15 && (A=='x' || A=='X')){
	    modus=16;
	  }else if(modus==9 ||
	  (modus==10 && ((B<'0' || B>'9') && B!='.')) ||
	  (modus==16 && (B<'0' || (B>'9' && B<'a') || B>'f'))
	  || (modus==8 && (B<'0' || B>'8'))){
	    cout << nocolor;
	    modus=0;
	  }else if(modus==3){
	    putchar(B);
	    cout << red;
	    modus=1;
	    goto end_of_while;
	  }else if(modus==4){
	    modus=2;
	  }else if(B=='\\'){
	    if(modus==1){
	      cout << purple;
	      modus=3;
	    }else if(modus==2) modus=4;
	  }
#ifdef BRACES
	}if(!modus){
#else
	}else{
#endif
          if((B>='a' && B<='z') || B=='_' ||
             (B>='A' && B<='Z') || B<0){
            modus=11;	//identifier mode
          }else if(B=='"'){
            cout << red;
            modus=1;
          }else if(B=='\''){
            cout << pink;
            modus=2;
          }else if((B>='0' && B<='9') || B=='.'){
            if(B=='0' && (C>='0' && C<='9')){
	      cout << cyan;
	      modus=8;
	    }else if(B=='0' && (C=='x' || C=='X')){
	      cout << cyan;
	      modus=15;
	    }else{
	      if((B!='.' || (C>='0' && C<='9'))){
	        cout << blue;
	      }
	      modus=10;
            }
          }
#ifdef BRACES
          else if(B=='{' || B=='}' || B=='(' || B==')'){
	    if(B=='{'){
	      if(togglebox & 0x01) cout << green;
	      togglebox ^= 0x01;
	    }else if(B=='}'){
	      togglebox ^= 0x01;
	      if(togglebox & 0x01) cout << green;
	    }else if(B=='('){
	      if(togglebox & 0x02) cout << brown;
	      togglebox ^= 0x02;
	    }else if(B==')'){
	      togglebox ^= 0x02;
	      if(togglebox & 0x02) cout << brown;
	    }
	    putchar(B);
	    cout << nocolor;
	    goto end_of_while;
          }
#endif
          else if(B=='#'){
            cout << green;
            modus=5;
          }else if(B=='/' && C=='/'){
            cout << grey; //grey this
            modus=6;
          }else if(B=='/' && C=='*'){
            cout << grey; /*grey this*/
            modus=7;
          }else if(C=='\n' && (B==' ' || B=='\t')){	      
            cout << grey_bg; //grey whitespace at end of line^ 
            modus=9;
          }
        }
	if(B) putchar(B);
	end_of_while:
	A = B;
	B = C;
      }
      straum.close();
      putchar(C);
      if(modus) cout << nocolor;
      if(C!='\n') putchar('\n');
    }
  }
  return 0;
}

