X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fdemo_termcap.c;h=468bd976da83e49503ca3fd6480b00da287b0a18;hp=110797e502e01db460ae80bc38d89a05355635a1;hb=HEAD;hpb=0266ec751090f9523ae676f357095176e293fb16 diff --git a/test/demo_termcap.c b/test/demo_termcap.c index 110797e5..6e2aba70 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-2022,2023 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.43 2014/08/31 23:11:39 tom Exp $ + * $Id: demo_termcap.c,v 1.65 2023/05/27 20:13:10 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 +#elif HAVE_TERMCAP_H +#include +#endif +#endif + +static GCC_NORETURN void failed(const char *); + 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; @@ -92,10 +105,11 @@ static long total_s_values; #define EachCapName(n) n = 33; n < 127; ++n static char * -make_dbitem(char *p, char *q) +make_dbitem(const char *const p, const char *const 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) @@ -369,6 +404,9 @@ typedef enum { static void parse_description(const char *input_name) { + static char empty[1] = + {0}; + FILE *fp; struct stat sb; size_t count_bools = 0; @@ -400,10 +438,13 @@ parse_description(const char *input_name) failed("cannot allocate memory for input-file"); } - if ((fp = fopen(input_name, "r")) == 0) + if ((fp = fopen(input_name, "r")) == 0) { failed("cannot open input-file"); - len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp); - fclose(fp); + } else { + len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp); + my_blob[sb.st_size] = '\0'; + fclose(fp); + } /* * First, get rid of comments and escaped newlines, as well as repeated @@ -585,10 +626,10 @@ parse_description(const char *input_name) * known order. */ if (count_strs) { - my_strvalues[count_strs] = ""; + my_strvalues[count_strs] = empty; my_strcodes[count_strs++] = &my_blob[j]; } else if (count_nums) { - my_numvalues[count_nums] = ""; + my_numvalues[count_nums] = empty; my_numcodes[count_nums++] = &my_blob[j]; } else { my_boolcodes[count_bools++] = &my_blob[j]; @@ -660,9 +701,8 @@ copy_code_list(NCURSES_CONST char *const *list) { int pass; size_t count; - size_t length = 0; + size_t length = 1; char **result = 0; - char *blob = 0; char *unused = 0; for (pass = 0; pass < 2; ++pass) { @@ -672,12 +712,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) @@ -687,52 +727,72 @@ copy_code_list(NCURSES_CONST char *const *list) return result; } -#endif +#if NO_LEAKS static void -usage(void) +free_code_list(char **list) +{ + if (list) { + free(list[0]); + free(list); + } +} +#endif /* NO_LEAKS */ +#endif /* USE_CODE_LISTS */ + +static void +usage(int ok) { static const char *msg[] = { - "Usage: demo_termcap [options] [terminal]", - "", - "If no options are given, print all (boolean, numeric, string)", - "capabilities for the given terminal, using short names.", - "", - "Options:", - " -a try all names, print capabilities found", - " -b print boolean-capabilities", - " -d LIST colon-separated list of databases to use", - " -e NAME environment variable to set with -d option", - " -i NAME terminal description to use as names for \"-a\" option, etc.", - " -n print numeric-capabilities", - " -q quiet (prints only counts)", - " -r COUNT repeat for given count", - " -s print string-capabilities", + "Usage: demo_termcap [options] [terminal]" + ,"" + ,"If no options are given, print all (boolean, numeric, string)" + ,"capabilities for the given terminal, using short names." + ,"" + ,USAGE_COMMON + ,"Options:" + ," -a try all names, print capabilities found" + ," -b print boolean-capabilities" + ," -d LIST colon-separated list of databases to use" + ," -e NAME environment variable to set with -d option" + ," -i NAME terminal description to use as names for \"-a\" option, etc." + ," -n print numeric-capabilities" + ," -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", + ," -x print extended capabilities" #endif }; unsigned n; for (n = 0; n < SIZEOF(msg); ++n) { fprintf(stderr, "%s\n", msg[n]); } - ExitProgram(EXIT_FAILURE); + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); } +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ int main(int argc, char *argv[]) { + int ch; 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) { - switch (n) { + while ((ch = getopt(argc, argv, OPTS_COMMON "abd:e:i:nqr:svxy")) != -1) { + switch (ch) { case 'a': a_opt = TRUE; break; @@ -756,11 +816,17 @@ main(int argc, char *argv[]) break; case 'r': if ((r_opt = atoi(optarg)) <= 0) - usage(); + usage(FALSE); break; 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; @@ -770,13 +836,17 @@ main(int argc, char *argv[]) x_opt = TRUE; break; #endif +#endif + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); - break; + usage(ch == OPTS_USAGE); + /* NOTREACHED */ } } -#if NCURSES_XNAMES +#if HAVE_USE_EXTENDED_NAMES use_extended_names(x_opt); #endif @@ -833,15 +903,30 @@ 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); } #else int -main(int argc GCC_UNUSED, - char *argv[]GCC_UNUSED) +main(void) { failed("This program requires termcap"); }