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.21 2017/06/17 21:19:25 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((NCURSES_CONST char *) 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);
103 #define TC_PARMS data->sp, (NCURSES_CONST char *)tc
104 #define TI_PARMS data->sp, (NCURSES_CONST char *)ti
107 show_cap_flag(MYDATA * data, const char *ti, const char *tc)
109 const char *name = (opt_t ? tc : ti);
110 show_flag(data, name, (opt_t
111 ? tgetflag_sp(TC_PARMS)
112 : tigetflag_sp(TI_PARMS)));
116 show_number(MYDATA * data, const char *name, int value)
119 fprintf(data->fp, " %s = (unknown)\n", name);
120 } else if (value <= -1) {
121 fprintf(data->fp, " %s = (missing)\n", name);
123 fprintf(data->fp, " %s = %d\n", name, value);
128 show_cap_number(MYDATA * data, const char *ti, const char *tc)
130 const char *name = (opt_t ? tc : ti);
131 show_number(data, name, (opt_t
132 ? tgetnum_sp(TC_PARMS)
133 : tigetnum_sp(TI_PARMS)));
137 show_string(MYDATA * data, const char *name, const char *value)
139 fprintf(data->fp, " %s = ", name);
141 fprintf(data->fp, "(missing)");
142 } else if (value == (char *) -1) {
143 fprintf(data->fp, "(canceled)");
146 while ((ch = UChar(*value++)) != '\0') {
148 fprintf(data->fp, "^%c", ch | '@');
149 } else if (ch == 127) {
150 fprintf(data->fp, "^?");
151 } else if (ch > 127) {
152 fprintf(data->fp, "\\%03o", ch);
154 fprintf(data->fp, "%c", ch);
158 fprintf(data->fp, "\n");
162 show_cap_string(MYDATA * data, const char *ti, const char *tc)
164 const char *name = (opt_t ? tc : ti);
166 char *tcap_ptr = tcapjunk;
167 show_string(data, name, (opt_t
168 ? tgetstr_sp(TC_PARMS, &tcap_ptr)
169 : tigetstr_sp(TI_PARMS)));
173 show_char(MYDATA * data, const char *name, int value)
176 show_string(data, name, "(missing)");
179 temp[0] = (char) value;
181 show_string(data, name, temp);
186 do_stuff(MYDATA * data)
189 SCREEN *sp = data->sp;
191 const char *my_text = "\033[?m";
193 set_curterm_sp(sp, data->term);
195 /* putp always goes to standard output */
196 putp_sp(sp, "Hello ");
197 putp_sp(sp, data->name);
200 fprintf(data->fp, "Term: %s\n", termname_sp(sp));
201 fprintf(data->fp, "Long: %s\n", longname_sp(sp));
202 show_cap_flag(data, "am", "am");
203 show_cap_number(data, "lines", "li");
204 show_cap_string(data, "clear", "cl");
205 show_cap_string(data, "tbc", "ct");
206 show_flag(data, "has_ic", has_ic_sp(sp));
207 show_flag(data, "has_il", has_il_sp(sp));
208 show_number(data, "baudrate", baudrate_sp(sp));
209 show_char(data, "erase ch", erasechar_sp(sp));
210 show_char(data, "kill ch", killchar_sp(sp));
211 show_string(data, "unctrl", unctrl_sp(sp, 033));
214 define_key_sp(sp, my_text, my_code);
216 key_defined_sp(sp, my_text);
217 if ((s = keybound_sp(sp, my_code, 0)) != 0)
220 keyok_sp(sp, my_code, FALSE);
221 keyok_sp(sp, my_code, TRUE);
225 def_shell_mode_sp(sp);
228 * These functions are low-level settings for ncurses.
230 set_tabsize_sp(sp, 5); /* waddch */
231 typeahead_sp(sp, FALSE); /* waddch */
232 use_env_sp(sp, FALSE); /* newterm */
233 use_tioctl_sp(sp, FALSE); /* newterm */
234 intrflush_sp(sp, 0, 0); /* wgetch */
235 flushinp_sp(sp); /* waddch */
236 halfdelay_sp(sp, 5); /* wgetch */
239 * These manipulate the terminal modes, mainly for wgetch.
243 def_prog_mode_sp(sp);
245 delay_output_sp(sp, 200);
256 tputs_sp(sp, "{reset-mode}\n", 0, data->outc);
258 reset_prog_mode_sp(sp);
261 tputs_sp(sp, "{prog-mode}\n", 0, data->outc);
263 reset_shell_mode_sp(sp);
265 tputs_sp(sp, "{shell-mode}\n", 0, data->outc);
269 cleanup(MYDATA * data)
271 set_curterm(data->term);
272 del_curterm(data->term);
274 free(data->sp); /* cannot use delscreen in tinfo */
282 static const char *tbl[] =
284 "Usage: sp_tinfo [output] [error]",
287 " -n suppress call to new_prescr()",
288 " -t use termcap functions rather than terminfo",
292 for (n = 0; n < SIZEOF(tbl); ++n) {
293 fprintf(stderr, "%s\n", tbl[n]);
295 ExitProgram(EXIT_FAILURE);
299 main(int argc, char *argv[])
305 while ((n = getopt(argc, argv, "nt")) != -1) {
318 argv += (optind - 1);
319 argc -= (optind - 1);
324 my_out = initialize((argc > 1) ? argv[1] : "vt100", stdout);
325 my_err = initialize((argc > 2) ? argv[2] : "ansi", stderr);
333 ExitProgram(EXIT_SUCCESS);
337 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
340 "This program requires the low-level ncurses sp-funcs tputs_sp\n");
341 ExitProgram(EXIT_FAILURE);