1 /* $NetBSD: tcap.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
5 ** tcap.c -- termcap color capabilities
7 ** (c) Jan 1991 - Feb 2010 by hoz
9 ** Feb 2002 max line size increased to 512 byte
10 ** default terminal "html" added
11 ** Feb 2010 color capabilities added
13 *****************************************************************/
22 # include "config_zkt.h"
24 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
37 /*****************************************************************
39 *****************************************************************/
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];
54 /* function declaration */
55 static int tc_printattr (FILE *fp
, const char *attstr
);
56 static int tc_color (FILE *fp
, int color
);
62 /*****************************************************************
64 *****************************************************************/
65 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
66 int tc_init (FILE *fp
, const char *term
)
68 static char area
[1024];
71 char *af
= ""; /* AF */ /* ansi foreground */
74 /* clear all color strings */
75 for ( i
= 0; i
< 8; i
++ )
76 colortab
[i
][0] = '\0';
78 if ( term
== NULL
|| *term
== '\0' ||
79 strcmp (term
, "none") == 0 || strcmp (term
, "dumb") == 0 )
82 if ( strcmp (term
, "html") == 0 || strcmp (term
, "HTML") == 0 )
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>");
105 switch ( tgetent (buf
, term
) )
107 case -1: perror ("termcap file");
109 case 0: fprintf (stderr
, "unknown terminal %s\n", term
);
113 if ( !(is1
= tgetstr ("is1", &ap
)) )
115 if ( !(is2
= tgetstr ("is2", &ap
)) )
117 if ( !(r1
= tgetstr ("r1", &ap
)) )
119 if ( !(r2
= tgetstr ("r2", &ap
)) )
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
= "";
128 bold_off
= tgetstr ("se", &ap
);
130 bold_off
= tgetstr ("me", &ap
);
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
= "";
138 italic_off
= tgetstr ("ue", &ap
);
140 italic_off
= tgetstr ("ZR", &ap
);
142 maxcolor
= tgetnum ("Co");
143 if ( maxcolor
< 0 ) /* no colors ? */
148 if ( (af
= tgetstr ("AF", &ap
)) ) /* set ansi color foreground */
150 for ( i
= 0; i
< maxcolor
; i
++ )
151 snprintf (colortab
[i
], sizeof colortab
[0], "%s", tparm (af
, i
));
153 else if ( (af
= tgetstr ("Sf", &ap
)) ) /* or set color foreground */
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));
167 tc_printattr (fp
, is1
);
169 tc_printattr (fp
, is2
);
175 int tc_init (FILE *fp
, const char *term
)
187 for ( i
= 0; i
< 8; i
++ )
188 colortab
[i
][0] = '\0';
196 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
197 int tc_end (FILE *fp
, const char *term
)
202 // if ( r1 && *r1 ) tc_printattr (fp, r1);
204 tc_printattr (fp
, r2
);
210 int tc_end (FILE *fp
, const char *term
)
216 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
217 int tc_attr (FILE *fp
, tc_att_t attr
, int on
)
222 if ( on
) /* turn attributes on ? */
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
);
230 len
+= tc_color (fp
, attr
& 0xFF);
232 else /* turn attributes off */
235 len
+= fprintf (fp
, "</font>");
237 len
+= tc_color (fp
, TC_BLACK
);
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
);
248 int tc_attr (FILE *fp
, tc_att_t attr
, int on
)
254 /*****************************************************************
255 ** internal functions
256 *****************************************************************/
257 static FILE *tc_outfp
;
258 static int put (int c
)
260 return putc (c
, tc_outfp
);
263 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
264 static int tc_printattr (FILE *fp
, const char *attstr
)
267 return tputs (attstr
, 0, put
);
270 static int tc_printattr (FILE *fp
, const char *attstr
)
276 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
277 static int tc_color (FILE *fp
, int color
)
281 if ( color
< 0 || color
>= maxcolor
)
283 return tputs (colortab
[color
], 0, put
);
286 static int tc_color (FILE *fp
, int color
)
294 static const char *progname
;
295 /*****************************************************************
297 *****************************************************************/
298 main (int argc
, const char *argv
[])
300 extern char *getenv ();
301 char *term
= getenv ("TERM");
307 tc_init (stdout
, term
);
309 // printattr (is); /* Initialisierungsstring ausgeben */
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
++ )
320 tc_attr (stdout
, i
, 1);
322 tc_attr (stdout
, i
, 0);
325 tc_attr (stdout
, (i
| TC_BOLD
), 1);
326 printf ("\t%s", text
);
327 tc_attr (stdout
, (i
| TC_BOLD
), 0);
329 tc_attr (stdout
, (i
| TC_ITALIC
), 1);
330 printf ("\t%s", text
);
331 tc_attr (stdout
, (i
| TC_ITALIC
), 0);
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);
339 printf ("now back to black\n");
341 // printattr (r2); /* Zuruecksetzen */