1 /****************************************************************************
2 * Copyright (c) 2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
30 * $Id: sp_tinfo.c,v 1.19 2017/05/13 23:19:04 tom Exp $
32 * TOTO: add option for non-sp-funcs interface
36 #include <test.priv.h>
40 * The higher-level curses library stores a TERMINAL* inside SCREEN, but the
41 * latter is opaque. This structure helps us keep the two associated.
48 int (*outc) (SCREEN *, int);
51 static bool opt_n = FALSE; /* true to suppress new_prescr */
52 static bool opt_t = FALSE; /* true to use termcap */
55 my_outc(SCREEN *sp, int ch)
58 return fputc(ch, stdout);
62 my_errc(SCREEN *sp, int ch)
65 return fputc(ch, stderr);
69 initialize(const char *name, FILE *output)
71 MYDATA *result = typeCalloc(MYDATA, 1);
76 result->outc = (fileno(output) == 1) ? my_outc : my_errc;
77 result->sp = opt_n ? NULL : new_prescr();
80 char *temp = strdup(name);
81 tgetent_sp(result->sp, temp, name);
84 setupterm(name, fileno(output), &error);
86 result->term = cur_term;
92 show_flag(MYDATA * data, const char *name, int value)
95 fprintf(data->fp, " %s = (unknown)\n", name);
96 } else if (value == 0) {
97 fprintf(data->fp, " %s = false\n", name);
99 fprintf(data->fp, " %s = true\n", name);
104 show_cap_flag(MYDATA * data, const char *ti, const char *tc)
106 const char *name = (opt_t ? tc : ti);
107 show_flag(data, name, (opt_t
108 ? tgetflag_sp(data->sp, tc)
109 : tigetflag_sp(data->sp, ti)));
113 show_number(MYDATA * data, const char *name, int value)
116 fprintf(data->fp, " %s = (unknown)\n", name);
117 } else if (value <= -1) {
118 fprintf(data->fp, " %s = (missing)\n", name);
120 fprintf(data->fp, " %s = %d\n", name, value);
125 show_cap_number(MYDATA * data, const char *ti, const char *tc)
127 const char *name = (opt_t ? tc : ti);
128 show_number(data, name, (opt_t
129 ? tgetnum_sp(data->sp, tc)
130 : tigetnum_sp(data->sp, ti)));
134 show_string(MYDATA * data, const char *name, const char *value)
136 fprintf(data->fp, " %s = ", name);
138 fprintf(data->fp, "(missing)");
139 } else if (value == (char *) -1) {
140 fprintf(data->fp, "(canceled)");
143 while ((ch = UChar(*value++)) != '\0') {
145 fprintf(data->fp, "^%c", ch | '@');
146 } else if (ch == 127) {
147 fprintf(data->fp, "^?");
148 } else if (ch > 127) {
149 fprintf(data->fp, "\\%03o", ch);
151 fprintf(data->fp, "%c", ch);
155 fprintf(data->fp, "\n");
159 show_cap_string(MYDATA * data, const char *ti, const char *tc)
161 const char *name = (opt_t ? tc : ti);
163 char *tcap_ptr = tcapjunk;
164 show_string(data, name, (opt_t
165 ? tgetstr_sp(data->sp, tc, &tcap_ptr)
166 : tigetstr_sp(data->sp, ti)));
170 show_char(MYDATA * data, const char *name, int value)
173 show_string(data, name, "(missing)");
176 temp[0] = (char) value;
178 show_string(data, name, temp);
183 do_stuff(MYDATA * data)
186 SCREEN *sp = data->sp;
188 const char *my_text = "\033[?m";
190 set_curterm_sp(sp, data->term);
192 /* putp always goes to standard output */
193 putp_sp(sp, "Hello ");
194 putp_sp(sp, data->name);
197 fprintf(data->fp, "Term: %s\n", termname_sp(sp));
198 fprintf(data->fp, "Long: %s\n", longname_sp(sp));
199 show_cap_flag(data, "am", "am");
200 show_cap_number(data, "lines", "li");
201 show_cap_string(data, "clear", "cl");
202 show_cap_string(data, "tbc", "ct");
203 show_flag(data, "has_ic", has_ic_sp(sp));
204 show_flag(data, "has_il", has_il_sp(sp));
205 show_number(data, "baudrate", baudrate_sp(sp));
206 show_char(data, "erase ch", erasechar_sp(sp));
207 show_char(data, "kill ch", killchar_sp(sp));
208 show_string(data, "unctrl", unctrl_sp(sp, 033));
211 define_key_sp(sp, my_text, my_code);
213 key_defined_sp(sp, my_text);
214 if ((s = keybound_sp(sp, my_code, 0)) != 0)
217 keyok_sp(sp, my_code, FALSE);
218 keyok_sp(sp, my_code, TRUE);
222 def_shell_mode_sp(sp);
225 * These functions are low-level settings for ncurses.
227 set_tabsize_sp(sp, 5); /* waddch */
228 typeahead_sp(sp, FALSE); /* waddch */
229 use_env_sp(sp, FALSE); /* newterm */
230 use_tioctl_sp(sp, FALSE); /* newterm */
231 intrflush_sp(sp, 0, 0); /* wgetch */
232 flushinp_sp(sp); /* waddch */
233 halfdelay_sp(sp, 5); /* wgetch */
236 * These manipulate the terminal modes, mainly for wgetch.
240 def_prog_mode_sp(sp);
242 delay_output_sp(sp, 200);
253 tputs_sp(sp, "{reset-mode}\n", 0, data->outc);
255 reset_prog_mode_sp(sp);
258 tputs_sp(sp, "{prog-mode}\n", 0, data->outc);
260 reset_shell_mode_sp(sp);
262 tputs_sp(sp, "{shell-mode}\n", 0, data->outc);
266 cleanup(MYDATA * data)
268 set_curterm(data->term);
269 del_curterm(data->term);
271 free(data->sp); /* cannot use delscreen in tinfo */
279 static const char *tbl[] =
281 "Usage: sp_tinfo [output] [error]",
284 " -n suppress call to new_prescr()",
285 " -t use termcap functions rather than terminfo",
289 for (n = 0; n < SIZEOF(tbl); ++n) {
290 fprintf(stderr, "%s\n", tbl[n]);
292 ExitProgram(EXIT_FAILURE);
296 main(int argc, char *argv[])
302 while ((n = getopt(argc, argv, "nt")) != -1) {
315 argv += (optind - 1);
316 argc -= (optind - 1);
321 my_out = initialize((argc > 1) ? argv[1] : "vt100", stdout);
322 my_err = initialize((argc > 2) ? argv[2] : "ansi", stderr);
330 ExitProgram(EXIT_SUCCESS);
334 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
337 "This program requires the low-level ncurses sp-funcs tputs_sp\n");
338 ExitProgram(EXIT_FAILURE);