summaryrefslogtreecommitdiff
path: root/test/demo_termcap.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/demo_termcap.c')
-rw-r--r--test/demo_termcap.c198
1 files changed, 135 insertions, 63 deletions
diff --git a/test/demo_termcap.c b/test/demo_termcap.c
index 2a29266..adc9eee 100644
--- a/test/demo_termcap.c
+++ b/test/demo_termcap.c
@@ -1,5 +1,6 @@
/****************************************************************************
- * Copyright (c) 2005-2013,2014 Free Software Foundation, Inc. *
+ * Copyright 2019,2020 Thomas E. Dickey *
+ * Copyright 2005-2016,2017 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
@@ -29,7 +30,7 @@
/*
* Author: Thomas E. Dickey
*
- * $Id: demo_termcap.c,v 1.46 2014/10/10 00:35:43 tom Exp $
+ * $Id: demo_termcap.c,v 1.59 2020/02/02 23:34:34 tom Exp $
*
* A simple demo of the termcap interface.
*/
@@ -46,6 +47,16 @@
#endif
#endif
+#if defined(NCURSES_VERSION)
+#if HAVE_NCURSES_TERMCAP_H
+#include <ncurses/termcap.h>
+#elif HAVE_TERMCAP_H
+#include <termcap.h>
+#endif
+#endif
+
+static void failed(const char *) GCC_NORETURN;
+
static void
failed(const char *msg)
{
@@ -68,8 +79,10 @@ static bool b_opt = FALSE;
static bool n_opt = FALSE;
static bool s_opt = FALSE;
static bool q_opt = FALSE;
+#ifdef NCURSES_VERSION
static bool x_opt = FALSE;
static bool y_opt = FALSE;
+#endif
static char *d_opt;
static char *e_opt;
@@ -94,8 +107,9 @@ static long total_s_values;
static char *
make_dbitem(char *p, char *q)
{
- char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
- sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
+ size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
+ char *result = malloc(need);
+ _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
return result;
}
@@ -146,10 +160,12 @@ next_dbitem(void)
db_item++;
}
}
- printf("** %s\n", result);
+ if (result != 0)
+ printf("** %s\n", result);
return result;
}
+#if NO_LEAKS
static void
free_dblist(void)
{
@@ -161,6 +177,73 @@ free_dblist(void)
db_list = 0;
}
}
+#endif /* NO_LEAKS */
+
+static void
+show_string(const char *name, const char *value)
+{
+ printf(FNAME(str), name);
+ if (value == ((char *) -1)) {
+ printf("CANCELLED");
+ } else if (value == ((char *) 0)) {
+ printf("ABSENT");
+ } else {
+ while (*value != 0) {
+ int ch = UChar(*value++);
+ switch (ch) {
+ case '\177':
+ fputs("^?", stdout);
+ break;
+ case '\033':
+ fputs("\\E", stdout);
+ break;
+ case '\b':
+ fputs("\\b", stdout);
+ break;
+ case '\f':
+ fputs("\\f", stdout);
+ break;
+ case '\n':
+ fputs("\\n", stdout);
+ break;
+ case '\r':
+ fputs("\\r", stdout);
+ break;
+ case ' ':
+ fputs("\\s", stdout);
+ break;
+ case '\t':
+ fputs("\\t", stdout);
+ break;
+ case '^':
+ fputs("\\^", stdout);
+ break;
+ case ':':
+ fputs("\\072", stdout);
+ break;
+ case '\\':
+ fputs("\\\\", stdout);
+ break;
+ default:
+ if (isgraph(ch))
+ fputc(ch, stdout);
+ else if (ch < 32)
+ printf("^%c", ch + '@');
+ else
+ printf("\\%03o", ch);
+ break;
+ }
+ }
+ }
+ printf("\n");
+}
+
+static void
+show_number(const char *name, int value)
+{
+ printf(FNAME(num), name);
+ printf(" %d\n", value);
+}
static void
dumpit(NCURSES_CONST char *cap)
@@ -184,61 +267,13 @@ dumpit(NCURSES_CONST char *cap)
* Note that the strings returned are mostly terminfo format, since
* ncurses does not convert except for a handful of special cases.
*/
- printf(FNAME(str), cap);
- while (*str != 0) {
- int ch = UChar(*str++);
- switch (ch) {
- case '\177':
- fputs("^?", stdout);
- break;
- case '\033':
- fputs("\\E", stdout);
- break;
- case '\b':
- fputs("\\b", stdout);
- break;
- case '\f':
- fputs("\\f", stdout);
- break;
- case '\n':
- fputs("\\n", stdout);
- break;
- case '\r':
- fputs("\\r", stdout);
- break;
- case ' ':
- fputs("\\s", stdout);
- break;
- case '\t':
- fputs("\\t", stdout);
- break;
- case '^':
- fputs("\\^", stdout);
- break;
- case ':':
- fputs("\\072", stdout);
- break;
- case '\\':
- fputs("\\\\", stdout);
- break;
- default:
- if (isgraph(ch))
- fputc(ch, stdout);
- else if (ch < 32)
- printf("^%c", ch + '@');
- else
- printf("\\%03o", ch);
- break;
- }
- }
- printf("\n");
+ show_string(cap, str);
}
} else if ((num = tgetnum(cap)) >= 0) {
total_values++;
total_n_values++;
if (!q_opt) {
- printf(FNAME(num), cap);
- printf(" %d\n", num);
+ show_number(cap, num);
}
} else if (tgetflag(cap) > 0) {
total_values++;
@@ -294,8 +329,6 @@ dump_xname(NCURSES_CONST char *cap)
static void
demo_termcap(NCURSES_CONST char *name)
{
- unsigned n;
- NCURSES_CONST char *cap;
char buffer[1024];
if (db_list) {
@@ -304,6 +337,8 @@ demo_termcap(NCURSES_CONST char *name)
if (!q_opt)
printf("Terminal type \"%s\"\n", name);
if (tgetent(buffer, name) >= 0) {
+ NCURSES_CONST char *cap;
+ unsigned n;
if (b_opt) {
for (n = 0;; ++n) {
@@ -334,7 +369,7 @@ demo_termcap(NCURSES_CONST char *name)
#ifdef NCURSES_VERSION
if (x_opt && (my_blob == 0) && y_opt) {
#if NCURSES_XNAMES
- TERMTYPE *term = &(cur_term->type);
+ TERMTYPE *term = (TERMTYPE *) cur_term;
if (term != 0
&& ((NUM_BOOLEANS(term) != BOOLCOUNT)
|| (NUM_NUMBERS(term) != NUMCOUNT)
@@ -665,7 +700,6 @@ copy_code_list(NCURSES_CONST char *const *list)
size_t count;
size_t length = 1;
char **result = 0;
- char *blob = 0;
char *unused = 0;
for (pass = 0; pass < 2; ++pass) {
@@ -675,12 +709,12 @@ copy_code_list(NCURSES_CONST char *const *list)
length += chunk;
} else {
result[count] = unused;
- strcpy(unused, list[count]);
+ _nc_STRCPY(unused, list[count], length);
unused += chunk;
}
}
if (pass == 0) {
- blob = malloc(length);
+ char *blob = malloc(length);
result = typeCalloc(char *, count + 1);
unused = blob;
if (blob == 0 || result == 0)
@@ -690,7 +724,18 @@ copy_code_list(NCURSES_CONST char *const *list)
return result;
}
-#endif
+
+#if NO_LEAKS
+static void
+free_code_list(char **list)
+{
+ if (list) {
+ free(list[0]);
+ free(list);
+ }
+}
+#endif /* NO_LEAKS */
+#endif /* USE_CODE_LISTS */
static void
usage(void)
@@ -712,6 +757,7 @@ usage(void)
" -q quiet (prints only counts)",
" -r COUNT repeat for given count",
" -s print string-capabilities",
+ " -v print termcap-variables",
#ifdef NCURSES_VERSION
" -x print extended capabilities",
#endif
@@ -729,12 +775,15 @@ main(int argc, char *argv[])
int n;
char *name;
bool a_opt = FALSE;
+#if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
+ bool v_opt = FALSE;
+#endif
char *input_name = 0;
int repeat;
int r_opt = 1;
- while ((n = getopt(argc, argv, "abd:e:i:nqr:sxy")) != -1) {
+ while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
switch (n) {
case 'a':
a_opt = TRUE;
@@ -764,6 +813,12 @@ main(int argc, char *argv[])
case 's':
s_opt = TRUE;
break;
+#if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
+ case 'v':
+ v_opt = TRUE;
+ break;
+#endif
+#ifdef NCURSES_VERSION
#if NCURSES_XNAMES
case 'x':
x_opt = TRUE;
@@ -773,6 +828,7 @@ main(int argc, char *argv[])
x_opt = TRUE;
break;
#endif
+#endif
default:
usage();
break;
@@ -836,7 +892,23 @@ main(int argc, char *argv[])
printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
total_values, total_b_values, total_n_values, total_s_values);
+#if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
+ if (v_opt) {
+ show_number("PC", PC);
+ show_string("UP", UP);
+ show_string("BC", BC);
+ show_number("ospeed", (int) ospeed);
+ }
+#endif
+
+#if NO_LEAKS
free_dblist();
+#if USE_CODE_LISTS
+ free_code_list(my_boolcodes);
+ free_code_list(my_numcodes);
+ free_code_list(my_strcodes);
+#endif
+#endif /* NO_LEAKS */
ExitProgram(EXIT_SUCCESS);
}