etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / tcap.c
blob203f17fabfd4359db4434cafda251a66b7c2f102
1 /* $NetBSD: tcap.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
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
13 *****************************************************************/
15 #include <stdio.h>
16 #include <string.h>
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 # include "config_zkt.h"
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
33 #define extern
34 # include "tcap.h"
35 #undef extern
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];
51 /* termcap numbers */
52 static int maxcolor;
54 /* function declaration */
55 static int tc_printattr (FILE *fp, const char *attstr);
56 static int tc_color (FILE *fp, int color);
58 static int html = 0;
62 /*****************************************************************
63 ** global functions
64 *****************************************************************/
65 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
66 int tc_init (FILE *fp, const char *term)
68 static char area[1024];
69 char buf[1024];
70 char *ap = area;
71 char *af = ""; /* AF */ /* ansi foreground */
72 int i;
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 )
80 return 0;
82 if ( strcmp (term, "html") == 0 || strcmp (term, "HTML") == 0 )
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;
101 #if 0
102 if ( !istty (fp) )
103 return 0;
104 #endif
105 switch ( tgetent (buf, term) )
107 case -1: perror ("termcap file");
108 return -1;
109 case 0: fprintf (stderr, "unknown terminal %s\n", term);
110 return -1;
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 = "";
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);
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);
142 maxcolor = tgetnum ("Co");
143 if ( maxcolor < 0 ) /* no colors ? */
144 return 0;
145 if ( maxcolor > 8 )
146 maxcolor = 8;
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));
165 #if 0
166 if ( is1 && *is1 )
167 tc_printattr (fp, is1);
168 if ( is2 && *is2 )
169 tc_printattr (fp, is2);
170 #endif
172 return 0;
174 #else
175 int tc_init (FILE *fp, const char *term)
177 int i;
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;
192 return 0;
194 #endif
196 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
197 int tc_end (FILE *fp, const char *term)
199 #if 0
200 if ( term )
202 // if ( r1 && *r1 ) tc_printattr (fp, r1);
203 if ( r2 && *r2 )
204 tc_printattr (fp, r2);
206 #endif
207 return 0;
209 #else
210 int tc_end (FILE *fp, const char *term)
212 return 0;
214 #endif
216 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
217 int tc_attr (FILE *fp, tc_att_t attr, int on)
219 int len;
221 len = 0;
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);
229 if ( attr & 0xFF )
230 len += tc_color (fp, attr & 0xFF);
232 else /* turn attributes off */
234 if ( html )
235 len += fprintf (fp, "</font>");
236 else
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);
245 return len;
247 #else
248 int tc_attr (FILE *fp, tc_att_t attr, int on)
250 return 0;
252 #endif
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)
266 tc_outfp = fp;
267 return tputs (attstr, 0, put);
269 #else
270 static int tc_printattr (FILE *fp, const char *attstr)
272 return 0;
274 #endif
276 #if defined(COLOR_MODE) && COLOR_MODE && HAVE_LIBNCURSES
277 static int tc_color (FILE *fp, int color)
279 tc_outfp = fp;
281 if ( color < 0 || color >= maxcolor )
282 return 0;
283 return tputs (colortab[color], 0, put);
285 #else
286 static int tc_color (FILE *fp, int color)
288 return 0;
290 #endif
293 #ifdef TEST
294 static const char *progname;
295 /*****************************************************************
296 ** test main()
297 *****************************************************************/
298 main (int argc, const char *argv[])
300 extern char *getenv ();
301 char *term = getenv ("TERM");
302 int i;
303 const char *text;
305 progname = *argv;
307 tc_init (stdout, term);
309 // printattr (is); /* Initialisierungsstring ausgeben */
311 text = "Test";
312 if ( argc > 1 )
313 text = *++argv;
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);
321 printf ("%s", text);
322 tc_attr (stdout, i, 0);
324 #if 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);
336 #endif
337 printf ("\n");
339 printf ("now back to black\n");
341 // printattr (r2); /* Zuruecksetzen */
343 return (0);
345 #endif