10 5 BG BRIGHT under sh/putty
12 4 UNDERLINE under gnome-terminal and putty
13 8 HIDDENT (sh => fg black, gnome-terminal => fg==bg)
14 9 Strikethrough under gnome-terminal
24 #define ANSIColor_RESET 0
25 #define ANSIColor_FGBRIGHT 1
26 #define ANSIColor_BGBRIGHT 5
27 #define ANSIColor_REVERSE 7
29 #define ANSIColor_UNDERLINE 4
30 #define ANSIColor_HIDDENT 8
31 #define ANSIColor_DELETELINE 9
33 #define ANSIColor_BLACK 0
34 #define ANSIColor_RED 1
35 #define ANSIColor_GREEN 2
36 #define ANSIColor_YELLOW 3
37 #define ANSIColor_BLUE 4
38 #define ANSIColor_MAGENTA 5 // 品红
39 #define ANSIColor_CYAN 6
40 #define ANSIColor_WHITE 7
42 /* Accroding to line 261 of [glibc.git]/stdio-common/vfprintf.c
43 ( http://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c ),
44 'k' is not used. Thus, I choose '%k' to hacking on. (another reason has sth. to do with the ass)
45 So, %k(int), %k(int,int), %k(int,int,int).
48 17 printf("[%k]\n",1);
50 t.c:16: warning: unknown conversion type character ‘k’ in format
51 t.c:17: warning: unknown conversion type character ‘k’ in format
52 t.c:17: warning: too many arguments for format
54 int colorprintf(const char *format
, ...) { // ANSI color is only useful to STDOUT, thus no FILE *stream here.
55 const char cESC
='\033'; // all that not conversion specifications is ordinary characters, which are simply copied to the output stream.
56 size_t inlen
= strlen(format
);
57 char *newformat
= (char *) malloc( inlen
);
58 char *ksubstr
= (char *) malloc( inlen
);
60 char *des_pos
, *ksub_pos
;
64 while ( (c
= *old_pos
++) ) {
68 if ( (c
= *old_pos
++) != '\0' ) {
71 *des_pos
++ = c
; // "%%" will be OK since c == '%' here.
73 if ( (c
= *old_pos
++) != '\0' ) {
77 char last_num_count
=0;
79 while ( (c
= *old_pos
++) && c
!= ')' ) {
81 if (c
>='0' && c
<='9') {
101 if ( flag
==0 && sublen
) { // is %k\([^)]+\)
105 while ( (*des_pos
++ = *ksub_pos
++) )
110 } else { // c == '\0', EOL
116 //*des_pos++ = c; // what if c == '%' ?
117 --old_pos
; // make another jump is slower than DEC.
126 *des_pos
++ = '%'; // the printf will ignore the tailing '%';
131 *des_pos
= '\0'; // when c == '\0', copy not done.
133 //printf("[%s]->[%s]\n",format,newformat);
140 va_start (arg
, format
);
141 done
= vfprintf (stdout
, newformat
, arg
);
148 int main(int argc
, char *argv
[]) {
150 printf("isatty = %d\n", isatty(0));
151 if ( isatty(STDOUT_FILENO) ) {
153 puts("[\033[1;32mWith color.\033[0m]");
155 puts("Without Color.");
156 fputs("STDERR Without Color.", stderr);
158 colorprintf("[%k(1,32)With color.[%s] [%s]%k(0)\n","t1","t2 t2");
160 //colorprintf("%k(1");
161 colorprintf(argv
[1],argv
[2]);