modified: n.fq
[GalaxyCodeBases.git] / funny / ac.c
blobd5e47b0630f73947ced472a179e9b864d0732071
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdarg.h>
4 #include <stdlib.h>
5 #include <string.h>
7 /*
8 0 Reset
9 1 FG BRIGHT
10 5 BG BRIGHT under sh/putty
11 7 REVERSE FG and BG
12 4 UNDERLINE under gnome-terminal and putty
13 8 HIDDENT (sh => fg black, gnome-terminal => fg==bg)
14 9 Strikethrough under gnome-terminal
17 enum ANSIColors {
18 Rest=0,
19 FgBright=1,
20 BgBright=5,
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).
46 Also,
47 16 printf("[%k]\n");
48 17 printf("[%k]\n",1);
49 18 printf("[%%k]\n");
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 );
59 const char *old_pos;
60 char *des_pos, *ksub_pos;
61 old_pos=format;
62 des_pos=newformat;
63 register char c;
64 while ( (c = *old_pos++) ) {
65 if (c != '%') {
66 *des_pos++ = c;
67 } else {
68 if ( (c = *old_pos++) != '\0' ) {
69 if (c != 'k') {
70 *des_pos++ = '%';
71 *des_pos++ = c; // "%%" will be OK since c == '%' here.
72 } else {
73 if ( (c = *old_pos++) != '\0' ) {
74 if (c == '(') {
75 ksub_pos=ksubstr;
76 char flag=0;
77 char last_num_count=0;
78 size_t sublen = 0;
79 while ( (c = *old_pos++) && c != ')' ) {
80 ++sublen;
81 if (c>='0' && c<='9') {
82 *ksub_pos++ = c;
83 ++last_num_count;
84 } else {
85 if ( c==',' ) {
86 if (last_num_count) {
87 *ksub_pos++ = ';';
89 } else {
90 flag=1;
92 last_num_count=0;
95 if (last_num_count) {
96 *ksub_pos = '\0';
97 } else {
98 *--ksub_pos = '\0';
100 if ( c == ')' ) {
101 if ( flag==0 && sublen ) { // is %k\([^)]+\)
102 *des_pos++ = cESC;
103 *des_pos++ = '[';
104 ksub_pos=ksubstr;
105 while ( (*des_pos++ = *ksub_pos++) )
107 --des_pos;
108 *des_pos++ = 'm';
110 } else { // c == '\0', EOL
111 break;
113 } else {
114 *des_pos++ = '%';
115 *des_pos++ = 'k';
116 //*des_pos++ = c; // what if c == '%' ?
117 --old_pos; // make another jump is slower than DEC.
119 } else {
120 *des_pos++ = '%';
121 *des_pos++ = 'k';
122 break;
125 } else {
126 *des_pos++ = '%'; // the printf will ignore the tailing '%';
127 break;
131 *des_pos = '\0'; // when c == '\0', copy not done.
132 free(ksubstr);
133 //printf("[%s]->[%s]\n",format,newformat);
134 puts(format);
135 puts("->");
136 puts(newformat);
138 va_list arg;
139 int done;
140 va_start (arg, format);
141 done = vfprintf (stdout, newformat, arg);
142 va_end (arg);
144 free(newformat);
145 return done;
148 int main(int argc, char *argv[]) {
150 printf("isatty = %d\n", isatty(0));
151 if ( isatty(STDOUT_FILENO) ) {
152 char e='\033';
153 puts("[\033[1;32mWith color.\033[0m]");
154 } else {
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]);
162 return 0;