Debian Codegraph @

test/terminfo.c

raw
1 /* netToe Version 1.5.1
2 *
3 * Copyright 2011, 2012, 2013, 2014 Mats Erik Andersson
4 * <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #if defined USE_TERMINFO || defined USE_TERMCAP
29 # if (defined HAVE_NCURSES_H || defined HAVE_NCURSES_NCURSES_H) \
30 && (defined USE_TINFO || defined USE_NCURSES)
31 # if defined HAVE_NCURSES_NCURSES_H
32 # include <ncurses/ncurses.h>
33 # else
34 # include <ncurses.h>
35 # endif
36 # endif
37 # if defined HAVE_CURSES_H && defined USE_CURSES
38 # include <curses.h>
39 # endif
40 # if defined HAVE_NCURSES_TERM_H && USE_NCURSES
41 # include <ncurses/term.h>
42 # elif defined HAVE_TERM_H && !USE_TERMCAP
43 # include <term.h>
44 # elif defined HAVE_TERMCAP_H && USE_TERMCAP
45 # include <termcap.h>
46 # endif
47
48 # if !HAVE_PUTP
49 # define putp(x) tputs((x), 1, putchar)
50 # endif
51
52 const char *clear_str = NULL;
53 const char *bell_str = NULL;
54 const char *bold_str = NULL;
55 const char *rmso_str = NULL;
56 const char *smso_str = NULL;
57 const char *sgr0_str = NULL;
58 const char *setab_str = NULL;
59 const char *setaf_str = NULL;
60 #endif /* Use terminal capabilities */
61
62 int main(void)
63 {
64 #if defined USE_TERMINFO || defined USE_TERMCAP
65 char *sp;
66 # if HAVE_SETUPTERM || HAVE_TGETENT
67 int err;
68 # endif /* HAVE_SETUPTERM || HAVE_TGETENT */
69 # if HAVE_TGETENT
70 static char buf[1024];
71 char *p = buf;
72 # endif /* HAVE_TGETENT */
73
74 if ((sp = getenv("TERM")) == NULL) {
75 sp = "dumb";
76 printf("No environment value set for TERM!\n"
77 "Using \"%s\" instead.\n", sp);
78 }
79
80 # if HAVE_SETUPTERM
81 if (setupterm(sp, fileno(stdout), &err) == ERR) {
82 switch (err) {
83 case 1: puts("Hardcopy terminal!");
84 return EXIT_SUCCESS;
85 break;
86 case 0: printf("Terminal \"%s\" not found.\n", sp);
87 return EXIT_SUCCESS;
88 break;
89 default: puts("No terminfo database accessible.");
90 return EXIT_SUCCESS;
91 break;
92 }
93 }
94 # elif HAVE_TGETENT
95 err = tgetent(buf, sp);
96 switch (err) {
97 case 1: /* Success */
98 break;
99 case 0: printf("Terminal \"%s\" not found.\n", sp);
100 return EXIT_SUCCESS;
101 break;
102 default: puts("No terminfo database accessible.");
103 return EXIT_SUCCESS;
104 break;
105 }
106 # endif /* !HAVE_SETUPTERM && HAVE_TGETENT */
107
108 printf("Terminal: TERM = %s.\n", sp);
109
110 # if HAVE_SETUPTERM
111 clear_str = tigetstr("clear");
112 bell_str = tigetstr("bel");
113 bold_str = tigetstr("bold");
114 rmso_str = tigetstr("rmso");
115 smso_str = tigetstr("smso");
116 sgr0_str = tigetstr("sgr0");
117 setab_str = tigetstr("setab");
118 setaf_str = tigetstr("setaf");
119 # elif HAVE_TGETENT
120 clear_str = tgetstr("cl", &p);
121 bell_str = tgetstr("bl", &p);
122 bold_str = tgetstr("md", &p);
123 rmso_str = tgetstr("se", &p);
124 smso_str = tgetstr("so", &p);
125 sgr0_str = tgetstr("me", &p);
126 setab_str = tgetstr("AB", &p);
127 setaf_str = tgetstr("AF", &p);
128 # endif /* HAVE_TGETENT */
129 printf("clear: %s\n", clear_str ? "yes" : "no, using ANSI");
130 printf("bell: %s\n", bell_str ? "yes" : "no, using ANSI");
131 printf("bold: %s\n", bold_str ? "yes" : "no, using ANSI");
132 printf("rmso: %s\n", rmso_str ? "yes" : "no, using ANSI");
133 printf("smso: %s\n", smso_str ? "yes" : "no, using ANSI");
134 printf("sgr0: %s\n", sgr0_str ? "yes" : "no, using ANSI");
135 printf("setab: %s\n", setab_str ? "yes" : "no, using ANSI");
136 printf("setaf: %s\n", setaf_str ? "yes" : "no, using ANSI");
137
138 return 0;
139 #else /* No terminal capabilities. */
140 printf("No terminal capabilities are in use.\n");
141
142 return 0;
143 #endif
144 }
145