1 /* $NetBSD: tcap.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */ 2 3 /***************************************************************** 4 ** 5 ** tcap.c -- termcap color capabilities 6 ** 7 ** (c) Jan 1991 - Feb 2010 by hoz 8 ** 9 ** Feb 2002 max line size increased to 512 byte 10 ** default terminal "html" added 11 ** Feb 2010 color capabilities added 12 ** 13 *****************************************************************/ 14 15 #include <stdio.h> 16 #include <string.h> 17 18 #ifdef HAVE_CONFIG_H 19 # include <config.h> 20 #endif 21 22 # include "config_zkt.h" 23 24 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 25 # ifdef HAVE_TERM_H 26 # include <term.h> 27 # endif 28 # ifdef HAVE_CURSES_H 29 # include <curses.h> 30 # endif 31 #endif 32 33 #define extern 34 # include "tcap.h" 35 #undef extern 36 37 /***************************************************************** 38 ** global vars 39 *****************************************************************/ 40 /* termcap strings */ 41 static const char *is1 = ""; 42 static const char *is2 = ""; 43 static const char *r1 = ""; 44 static const char *r2 = ""; 45 static const char *bold_on = ""; 46 static const char *bold_off = ""; 47 static const char *italic_on = ""; 48 static const char *italic_off = ""; 49 static char colortab[8][31+1]; 50 51 /* termcap numbers */ 52 static int maxcolor; 53 54 /* function declaration */ 55 static int tc_printattr (FILE *fp, const char *attstr); 56 static int tc_color (FILE *fp, int color); 57 58 static int html = 0; 59 60 61 62 /***************************************************************** 63 ** global functions 64 *****************************************************************/ 65 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 66 int tc_init (FILE *fp, const char *term) 67 { 68 static char area[1024]; 69 char buf[1024]; 70 char *ap = area; 71 char *af = ""; /* AF */ /* ansi foreground */ 72 int i; 73 74 /* clear all color strings */ 75 for ( i = 0; i < 8; i++ ) 76 colortab[i][0] = '\0'; 77 78 if ( term == NULL || *term == '\0' || 79 strcmp (term, "none") == 0 || strcmp (term, "dumb") == 0 ) 80 return 0; 81 82 if ( strcmp (term, "html") == 0 || strcmp (term, "HTML") == 0 ) 83 { 84 bold_on = "<B>"; 85 bold_off = "</B>"; 86 italic_on = "<I>"; 87 italic_off = "</I>"; 88 af = ""; 89 maxcolor = 8; 90 snprintf (colortab[TC_BLACK], sizeof colortab[0], "<font color=black>"); 91 snprintf (colortab[TC_BLUE], sizeof colortab[0], "<font color=blue>"); 92 snprintf (colortab[TC_GREEN], sizeof colortab[0], "<font color=green>"); 93 snprintf (colortab[TC_CYAN], sizeof colortab[0], "<font color=cyan>"); 94 snprintf (colortab[TC_RED], sizeof colortab[0], "<font color=red>"); 95 snprintf (colortab[TC_MAGENTA], sizeof colortab[0], "<font color=magenta>"); 96 snprintf (colortab[TC_YELLOW], sizeof colortab[0], "<font color=yellow>"); 97 snprintf (colortab[TC_WHITE], sizeof colortab[0], "<font color=white>"); 98 html = 1; 99 return 0; 100 } 101 #if 0 102 if ( !istty (fp) ) 103 return 0; 104 #endif 105 switch ( tgetent (buf, term) ) 106 { 107 case -1: perror ("termcap file"); 108 return -1; 109 case 0: fprintf (stderr, "unknown terminal %s\n", term); 110 return -1; 111 } 112 113 if ( !(is1 = tgetstr ("is1", &ap)) ) 114 is1 = ""; 115 if ( !(is2 = tgetstr ("is2", &ap)) ) 116 is2 = ""; 117 if ( !(r1 = tgetstr ("r1", &ap)) ) 118 r1 = ""; 119 if ( !(r2 = tgetstr ("r2", &ap)) ) 120 r2 = ""; 121 122 /* if bold is not present */ 123 if ( !(bold_on = tgetstr ("md", &ap)) ) 124 /* use standout mode */ 125 if ( !(bold_on = tgetstr ("so", &ap)) ) 126 bold_on = bold_off = ""; 127 else 128 bold_off = tgetstr ("se", &ap); 129 else 130 bold_off = tgetstr ("me", &ap); 131 132 /* if italic not present */ 133 if ( !(italic_on = tgetstr ("ZH", &ap)) ) 134 /* use underline mode */ 135 if ( !(italic_on = tgetstr ("us", &ap)) ) 136 italic_on = italic_off = ""; 137 else 138 italic_off = tgetstr ("ue", &ap); 139 else 140 italic_off = tgetstr ("ZR", &ap); 141 142 maxcolor = tgetnum ("Co"); 143 if ( maxcolor < 0 ) /* no colors ? */ 144 return 0; 145 if ( maxcolor > 8 ) 146 maxcolor = 8; 147 148 if ( (af = tgetstr ("AF", &ap)) ) /* set ansi color foreground */ 149 { 150 for ( i = 0; i < maxcolor; i++ ) 151 snprintf (colortab[i], sizeof colortab[0], "%s", tparm (af, i)); 152 } 153 else if ( (af = tgetstr ("Sf", &ap)) ) /* or set color foreground */ 154 { 155 snprintf (colortab[TC_BLACK], sizeof colortab[0], "%s", tparm (af, 0)); 156 snprintf (colortab[TC_BLUE], sizeof colortab[0], "%s", tparm (af, 1)); 157 snprintf (colortab[TC_GREEN], sizeof colortab[0], "%s", tparm (af, 2)); 158 snprintf (colortab[TC_CYAN], sizeof colortab[0], "%s", tparm (af, 3)); 159 snprintf (colortab[TC_RED], sizeof colortab[0], "%s", tparm (af, 4)); 160 snprintf (colortab[TC_MAGENTA], sizeof colortab[0], "%s", tparm (af, 5)); 161 snprintf (colortab[TC_YELLOW], sizeof colortab[0], "%s", tparm (af, 6)); 162 snprintf (colortab[TC_WHITE], sizeof colortab[0], "%s", tparm (af, 7)); 163 } 164 165 #if 0 166 if ( is1 && *is1 ) 167 tc_printattr (fp, is1); 168 if ( is2 && *is2 ) 169 tc_printattr (fp, is2); 170 #endif 171 172 return 0; 173 } 174 #else 175 int tc_init (FILE *fp, const char *term) 176 { 177 int i; 178 179 is1 = ""; 180 is2 = ""; 181 r1 = ""; 182 r2 = ""; 183 bold_on = ""; 184 bold_off = ""; 185 italic_on = ""; 186 italic_off = ""; 187 for ( i = 0; i < 8; i++ ) 188 colortab[i][0] = '\0'; 189 maxcolor = 0; 190 html = 0; 191 192 return 0; 193 } 194 #endif 195 196 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 197 int tc_end (FILE *fp, const char *term) 198 { 199 #if 0 200 if ( term ) 201 { 202 // if ( r1 && *r1 ) tc_printattr (fp, r1); 203 if ( r2 && *r2 ) 204 tc_printattr (fp, r2); 205 } 206 #endif 207 return 0; 208 } 209 #else 210 int tc_end (FILE *fp, const char *term) 211 { 212 return 0; 213 } 214 #endif 215 216 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 217 int tc_attr (FILE *fp, tc_att_t attr, int on) 218 { 219 int len; 220 221 len = 0; 222 if ( on ) /* turn attributes on ? */ 223 { 224 if ( (attr & TC_BOLD) == TC_BOLD ) 225 len += tc_printattr (fp, bold_on); 226 if ( (attr & TC_ITALIC) == TC_ITALIC ) 227 len += tc_printattr (fp, italic_on); 228 229 if ( attr & 0xFF ) 230 len += tc_color (fp, attr & 0xFF); 231 } 232 else /* turn attributes off */ 233 { 234 if ( html ) 235 len += fprintf (fp, "</font>"); 236 else 237 len += tc_color (fp, TC_BLACK); 238 239 if ( (attr & TC_ITALIC) == TC_ITALIC ) 240 len += tc_printattr (fp, italic_off); 241 if ( !html || (attr & TC_BOLD) == TC_BOLD ) 242 len += tc_printattr (fp, bold_off); 243 } 244 245 return len; 246 } 247 #else 248 int tc_attr (FILE *fp, tc_att_t attr, int on) 249 { 250 return 0; 251 } 252 #endif 253 254 /***************************************************************** 255 ** internal functions 256 *****************************************************************/ 257 static FILE *tc_outfp; 258 static int put (int c) 259 { 260 return putc (c, tc_outfp); 261 } 262 263 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 264 static int tc_printattr (FILE *fp, const char *attstr) 265 { 266 tc_outfp = fp; 267 return tputs (attstr, 0, put); 268 } 269 #else 270 static int tc_printattr (FILE *fp, const char *attstr) 271 { 272 return 0; 273 } 274 #endif 275 276 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES 277 static int tc_color (FILE *fp, int color) 278 { 279 tc_outfp = fp; 280 281 if ( color < 0 || color >= maxcolor ) 282 return 0; 283 return tputs (colortab[color], 0, put); 284 } 285 #else 286 static int tc_color (FILE *fp, int color) 287 { 288 return 0; 289 } 290 #endif 291 292 293 #ifdef TEST 294 static const char *progname; 295 /***************************************************************** 296 ** test main() 297 *****************************************************************/ 298 main (int argc, const char *argv[]) 299 { 300 extern char *getenv (); 301 char *term = getenv ("TERM"); 302 int i; 303 const char *text; 304 305 progname = *argv; 306 307 tc_init (stdout, term); 308 309 // printattr (is); /* Initialisierungsstring ausgeben */ 310 311 text = "Test"; 312 if ( argc > 1 ) 313 text = *++argv; 314 315 tc_attr (stdout, TC_BOLD, 1); 316 printf ("Bold Headline\n"); 317 tc_attr (stdout, TC_BOLD, 0); 318 for ( i = 0; i < 8; i++ ) 319 { 320 tc_attr (stdout, i, 1); 321 printf ("%s", text); 322 tc_attr (stdout, i, 0); 323 324 #if 0 325 tc_attr (stdout, (i | TC_BOLD), 1); 326 printf ("\t%s", text); 327 tc_attr (stdout, (i | TC_BOLD), 0); 328 329 tc_attr (stdout, (i | TC_ITALIC), 1); 330 printf ("\t%s", text); 331 tc_attr (stdout, (i | TC_ITALIC), 0); 332 333 tc_attr (stdout, (i | TC_BOLD | TC_ITALIC), 1); 334 printf ("\t%s", text); 335 tc_attr (stdout, (i | TC_BOLD | TC_ITALIC), 0); 336 #endif 337 printf ("\n"); 338 } 339 printf ("now back to black\n"); 340 341 // printattr (r2); /* Zuruecksetzen */ 342 343 return (0); 344 } 345 #endif 346