11 * For the terminal info
13 static char term_buffer[8192];
16 * For the capabilities
41 * Initialize the terminal info
43 void init_terminal_data ()
45 char *termtype = getenv ("TERM");
49 fprintf(stderr,"Specify a terminal type with `setenv TERM <yourtype>'.\n");
53 success = tgetent (term_buffer, termtype);
55 fprintf(stderr,"Could not access the termcap data base.\n");
59 fprintf(stderr,"Terminal type `%s' is not defined.\n", termtype);
65 * Print a band with background of all available colors
67 void colormap( int do_boldstand )
70 for( back = 0; back < maxcolors; back++ ) {
72 strB = strdup(tparm(setback,back));
73 printf("%s %s",strB,resetcolor);
78 for( back = 0; back < maxcolors; back++ ) {
80 strB = strdup(tparm(setfore,back));
81 printf("%s%s%s %s%s%s",bold,standstr,strB,resetcolor,
82 exit_stand,exit_attr);
87 for ( back = 0; back < maxcolors; back++ ) {
89 strB = strdup(tparm(setfore,back));
90 printf("%sXXXXXXXX%s",strB,resetcolor);
95 for ( back = 0; back < maxcolors; back++ ) {
97 strB = strdup(tparm(setfore,back));
98 printf("%s%sXXXXXXXX%s%s",bold,strB,resetcolor,exit_attr);
106 /*********************
107 * Print a table with all the fore and background
108 * color combinations, with bold and optionally standout
110 void combinations_of_colors(int do_standout, int do_boldstand)
113 for( fore = 0; fore < maxcolors; fore ++ )
114 for( back = 0; back < maxcolors; back ++ ) {
117 strB = strdup(tparm(setback,back));
118 strF = strdup(tparm(setfore,fore));
119 printf( "%s%s%s [%3d|%3d]%s%s", "",strF,strB,fore,back,resetcolor,"");
121 printf( "%s%s%sB[%3d|%3d]%s%s", bold,strF,strB,fore,back,resetcolor,exit_attr);
122 /* Standout usually does not work good... do only if asked for */
123 /* Since standout usually reverses, we'll reverse the fore and back too */
124 sstrB = strdup(tparm(setback,fore));
125 sstrF = strdup(tparm(setfore,back));
127 printf( "%s%s%sS[%3d|%3d]%s%s",standstr,sstrF,sstrB,fore,back,resetcolor,exit_stand);
129 printf( "%s%s%s%sS[%3d|%3d]%s%s%s",bold,standstr,sstrF,sstrB,fore,back,resetcolor,exit_stand,exit_attr);
137 /** Query and assign a capability
139 void capability(char**buf,char*code,char**var,char*message)
141 char *val = tgetstr(code,buf);
144 printf("%s\n",message);
151 /****************************
152 * Retrieve the capabilities
154 void get_capabilities()
156 char buffer[2500]="", *buf=buffer;
159 * Get the capabilities
161 maxcolors = tgetnum("Co");
162 if ( maxcolors <= 0 ) {
163 printf("I am sorry, your terminal does not show color in the capabilities\n");
165 maxpairs = tgetnum("pa");
166 if (maxpairs <= 0 ) {
167 printf("I am sorry, your terminal does not have the max color pairs information\n");
170 capability(&buf,"AF",&setfore,
171 "I am sorry, your terminal does not have the set foreground color string in the capabilities");
172 capability(&buf,"AB",&setback,
173 "I am sorry, your terminal does not have the set background color string in the capabilities");
174 capability(&buf,"op",&resetcolor,
175 "I am sorry, your terminal does not have the reset current color string in the capabilities");
176 capability(&buf,"md",&bold,
177 "I am sorry, your terminal does not have the bold string in the capabilities");
178 capability(&buf,"mh",&dim,
179 "I am sorry, your terminal does not have the dim string in the capabilities");
180 capability(&buf,"mr",&reverse,
181 "I am sorry, your terminal does not have the reverse string in the capabilities");
182 capability(&buf,"mb",&blinking,
183 "I am sorry, your terminal does not have the blinking string in the capabilities");
184 capability(&buf,"so",&standstr,
185 "I am sorry, your terminal does not have the standout string in the capabilities");
186 capability(&buf,"se",&exit_stand,
187 "I am sorry, your terminal does not have the exit standout string in the capabilities");
188 capability(&buf,"me",&exit_attr,
189 "I am sorry, your terminal does not have the exit attribute string in the capabilities");
190 capability(&buf,"Ic",&inicolor_str,
191 "I am sorry, your terminal does not have the init color string in the capabilities");
192 capability(&buf,"Yw",&clrnames,
193 "I am sorry, your terminal does not have the color names string in the capabilities");
194 capability(&buf,"op",&opair,
195 "I am sorry, your terminal does not have the original pairs string in the capabilities");
199 /********************
204 int i, fd=fileno(stdin);
208 if ( ! isatty(fd) || ! isatty(fileno(stdout)) )
209 printf("Sorry, but stdin and/or stdout is not the terminal tty so I cannot query it\n");
211 if ( ! inicolor_str )
212 printf("According to your terminal info this terminal rgb query will hang. Press ^C to abort the query.\n");
215 * We need to do the equivalent of cbreak()
218 mode=ts.c_lflag; /* Save mode so we can restore afterwards */
219 ts.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
220 tcsetattr(fd, TCSANOW,&ts);
223 /* Ask for the colors */
224 for ( i = 0 ; 1 ; i ++ ) {
227 *end=buffer+sizeof(buffer)-1;
230 /* This is a string retrieved from Noah
231 * Friedmans xterm-frobs.el. I could not find
232 * any termcap entry that verifies it
234 printf("\e]4;%d;?;\e\\",i);
236 memset(buffer,0,sizeof(buffer));
238 fd_set fdsr, fdse,fdsw;
243 t.tv_usec = 100000; /* 100ms */
250 fds = select(fd+1,&fdsr,&fdsw,&fdse,&t);
252 tcsetattr(fd, TCSANOW,&ts);
255 if ( ! FD_ISSET(fd, &fdsr) ) {
256 tcsetattr(fd, TCSANOW,&ts);
261 /* The response sequence ends with the only "\"
266 if ( *buf == '
\ 3' ) {
268 If the terminal did not reply, chances are
269 this is not supported and the user used ^C
279 /* Extract the rgb values */
280 buf=strstr(buffer,"rgb:");
283 char *end = strstr(buf,"\e\\");
287 buf="Did not find info";
289 printf("Color %d: %s\n",i,buf);
293 /* Restore terminal settings */
294 tcsetattr(fd, TCSANOW,&ts);
300 * Fake the capabilities the ansi terminal info
305 if ( ! (maxcolors > 0) ) {
307 printf("WARNING: Forcing ANSI testing, 8 colors\n");
309 if ( maxcolors == 8 && do_force_16 ) {
311 printf("WARNING: Forcing 16 color testing\n");
314 setfore="\e[3%p1%dm";
315 printf("WARNING: Forcing ANSI set foreground: ^[%s\n", setfore+1);
318 setback="\e[4%p1%dm";
319 printf("WARNING: Forcing ANSI set background: ^[%s\n", setback+1);
321 if ( ! resetcolor ) {
322 resetcolor="\e[39;49m";
323 printf("WARNING: Forcing ANSI reset colour : ^[%s\n", resetcolor+1);
330 main( int argc, char *argv[] )
338 { /* Process arguments */
341 for( i = 1; i < argc; i++ ) {
342 if( ! strcmp(argv[i],"-m") )
344 if( ! strcmp(argv[i],"-c") )
346 if ( ! strcmp(argv[i], "-f" ) )
348 if( ! strcmp(argv[i],"-s") )
350 if( ! strcmp(argv[i],"-bs") )
352 if( ! strcmp(argv[i],"-q") )
357 init_terminal_data();
362 * Report the capabilities
364 printf("\n\nTerminal: %s\n",getenv("TERM"));
366 printf("You've got %d colours\n",maxcolors);
368 printf("You've got %d pairs of colors possible\n",maxpairs);
370 if ( setfore ) printf("To set foreground: ^[%s\n", setfore+1);
371 if ( setback ) printf("To set background: ^[%s\n", setback+1);
372 if ( resetcolor ) printf("To reset colours : ^[%s\n", resetcolor+1);
373 if ( standstr ) printf("To standout : ^[%s\n", standstr+1);
374 if ( bold ) printf("To bold : ^[%s\n", bold+1);
375 if ( dim ) printf("To dim : ^[%s\n", dim+1);
376 if ( reverse ) printf("To reverse : ^[%s\n", reverse+1);
377 if ( blinking ) printf("To blinking : ^[%s\n", blinking+1);
378 if ( exit_stand ) printf("To exit standout : ^[%s\n", exit_stand+1);
379 if ( exit_attr ) printf("To exit attribute: ^[%s\n", exit_attr+1);
380 if ( inicolor_str ) printf("To init color : ^[%s\n", inicolor_str+1);
381 if ( clrnames ) printf("To color names : ^[%s\n", clrnames+1);
382 if ( opair ) printf("To original pairs: ^[%s\n", opair+1);
387 * Let's try the ANSI defaults
389 if ( do_colormap || do_combinations ) {
394 printf("\nQuery the RGB from the terminal:\n");
399 printf("\nColormap:\n");
400 colormap( do_boldstand );
403 if ( do_combinations ) {
404 printf("\nColor combinations:\n");
405 combinations_of_colors( do_standout,