X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=progs%2Fdump_entry.c;h=e21e05e03516227b3beb4b383b7a73185a865500;hp=87e3ce332fa0c9d51fefa56a4032e45f2c705560;hb=fb6c095b1b63de26712d67de11dc6e35ea223f16;hpb=7af63696972b12659832a1c3413d9ace9641c8f6 diff --git a/progs/dump_entry.c b/progs/dump_entry.c index 87e3ce33..e21e05e0 100644 --- a/progs/dump_entry.c +++ b/progs/dump_entry.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc. * + * Copyright (c) 1998-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 * @@ -39,10 +39,11 @@ #include "termsort.c" /* this C file is generated */ #include /* so is this */ -MODULE_ID("$Id: dump_entry.c,v 1.143 2016/10/09 01:30:14 tom Exp $") +MODULE_ID("$Id: dump_entry.c,v 1.155 2017/07/08 18:34:53 tom Exp $") #define DISCARD(string) string = ABSENT_STRING #define PRINTF (void) printf +#define WRAPPED 32 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array)) #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR) @@ -62,6 +63,7 @@ static int column; /* current column, limited by 'width' */ static int oldcol; /* last value of column before wrap */ static bool pretty; /* true if we format if-then-else strings */ static bool wrapped; /* true if we wrap too-long strings */ +static bool did_wrap; /* true if last wrap_concat did wrapping */ static bool checking; /* true if we are checking for tic */ static int quickdump; /* true if we are dumping compiled data */ @@ -208,6 +210,8 @@ dump_init(const char *version, checking = check; quickdump = (quick & 3); + did_wrap = (width <= 0); + /* versions */ if (version == 0) tversion = V_ALLCAPS; @@ -295,7 +299,7 @@ dump_init(const char *version, _nc_progname, width, tversion, outform); } -static TERMTYPE *cur_type; +static TERMTYPE2 *cur_type; static int dump_predicate(PredType type, PredIdx idx) @@ -318,7 +322,7 @@ dump_predicate(PredType type, PredIdx idx) return (FALSE); /* pacify compiler */ } -static void set_obsolete_termcaps(TERMTYPE *tp); +static void set_obsolete_termcaps(TERMTYPE2 *tp); /* is this the index of a function key string? */ #define FNKEY(i) \ @@ -556,6 +560,7 @@ wrap_concat(const char *src) int gaps = (int) strlen(separator); int want = gaps + need; + did_wrap = (width <= 0); if (column > indent && column + want > width) { force_wrap(); @@ -565,14 +570,15 @@ wrap_concat(const char *src) (column + want) > width && (!TcOutput() || strncmp(src, "..", 2))) { int step = 0; - int used = width > 32 ? width : 32; - int size = used; + int used = width > WRAPPED ? width : WRAPPED; + int size; int base = 0; char *p, align[9]; const char *my_t = trailer; char *fill = fill_spaces(src); + int last = (int) strlen(fill); - need = (int) strlen(fill); + need = last; if (TcOutput()) trailer = "\\\n\t "; @@ -585,21 +591,26 @@ wrap_concat(const char *src) } else { align[base] = '\0'; } - while ((column + (need + gaps)) > used) { - size = used; - if (size > ((int) strlen(fill) - step)) { - size = ((int) strlen(fill) - step); - } - if (step) { - strcpy_DYN(&outbuf, align); - size -= base; + /* "pretty" overrides wrapping if it already split the line */ + if (!pretty || strchr(fill, '\n') == 0) { + while ((column + (need + gaps)) > used) { + size = used; + if (step) { + strcpy_DYN(&outbuf, align); + size -= base; + } + if (size > (last - step)) { + size = (last - step); + } + size = find_split(fill, step, size); + strncpy_DYN(&outbuf, fill + step, (size_t) size); + step += size; + need -= size; + if (need > 0) { + force_wrap(); + did_wrap = TRUE; + } } - size = find_split(fill, step, size); - strncpy_DYN(&outbuf, fill + step, (size_t) size); - step += size; - need -= size; - if (need > 0) - force_wrap(); } if (need > 0) { if (step) @@ -686,7 +697,7 @@ has_params(const char *src) } static char * -fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level) +fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level) { bool percent = FALSE; bool params = has_params(src); @@ -785,11 +796,35 @@ fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level) return src; } +/* + * Make "large" numbers a little easier to read by showing them in hexadecimal + * if they are "close" to a power of two. + */ +static const char * +number_format(int value) +{ + const char *result = "%d"; + if ((outform != F_TERMCAP) && (value > 255)) { + unsigned long lv = (unsigned long) value; + unsigned long mm; + int bits = sizeof(unsigned long) * 8; + int nn; + for (nn = 8; nn < bits; ++nn) { + mm = 1UL << nn; + if ((mm - 16) <= lv && (mm + 16) > lv) { + result = "%#x"; + break; + } + } + } + return result; +} + #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap) #define EXTRA_CAP 20 int -fmt_entry(TERMTYPE *tterm, +fmt_entry(TERMTYPE2 *tterm, PredFunc pred, int content_only, int suppress_untranslatable, @@ -806,9 +841,10 @@ fmt_entry(TERMTYPE *tterm, PredIdx num_strings = 0; bool outcount = 0; -#define WRAP_CONCAT \ - wrap_concat(buffer); \ - outcount = TRUE +#define WRAP_CONCAT1(s) wrap_concat(s); outcount = TRUE +#define WRAP_CONCAT2(a,b) wrap_concat(a); WRAP_CONCAT1(b) +#define WRAP_CONCAT3(a,b,c) wrap_concat(a); WRAP_CONCAT2(b,c) +#define WRAP_CONCAT WRAP_CONCAT1(buffer) len = 12; /* terminfo file-header */ @@ -881,8 +917,13 @@ fmt_entry(TERMTYPE *tterm, _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%s@", name); } else { + size_t nn; _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) - "%s#%d", name, tterm->Numbers[i]); + "%s#", name); + nn = strlen(buffer); + _nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn) + number_format(tterm->Numbers[i]), + tterm->Numbers[i]); if (i + 1 > num_values) num_values = i + 1; } @@ -967,9 +1008,9 @@ fmt_entry(TERMTYPE *tterm, set_attributes = save_sgr; trimmed_sgr0 = _nc_trim_sgr0(tterm); - if (strcmp(capability, trimmed_sgr0)) + if (strcmp(capability, trimmed_sgr0)) { capability = trimmed_sgr0; - else { + } else { if (trimmed_sgr0 != exit_attribute_mode) free(trimmed_sgr0); } @@ -1006,13 +1047,21 @@ fmt_entry(TERMTYPE *tterm, _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "%s=!!! %s WILL NOT CONVERT !!!", name, srccap); + WRAP_CONCAT; } else if (suppress_untranslatable) { continue; } else { char *s = srccap, *d = buffer; - _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name); - d += strlen(d); + WRAP_CONCAT3("..", name, "="); while ((*d = *s++) != 0) { + if ((d - buffer + 1) >= (int) sizeof(buffer)) { + fprintf(stderr, + "%s: value for %s is too long\n", + _nc_progname, + name); + *d = '\0'; + break; + } if (*d == ':') { *d++ = '\\'; *d = ':'; @@ -1021,13 +1070,12 @@ fmt_entry(TERMTYPE *tterm, } d++; } + WRAP_CONCAT; } } else { - _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) - "%s=%s", name, cv); + WRAP_CONCAT3(name, "=", cv); } len += (int) strlen(capability) + 1; - WRAP_CONCAT; } else { char *src = _nc_tic_expand(capability, outform == F_TERMINFO, numbers); @@ -1043,8 +1091,7 @@ fmt_entry(TERMTYPE *tterm, strcpy_DYN(&tmpbuf, src); } len += (int) strlen(capability) + 1; - wrap_concat(tmpbuf.text); - outcount = TRUE; + WRAP_CONCAT1(tmpbuf.text); } } /* e.g., trimmed_sgr0 */ @@ -1138,9 +1185,11 @@ fmt_entry(TERMTYPE *tterm, if (outcount) { bool trimmed = FALSE; j = (PredIdx) outbuf.used; - if (j >= 2 - && outbuf.text[j - 1] == '\t' - && outbuf.text[j - 2] == '\n') { + if (wrapped && did_wrap) { + /* EMPTY */ ; + } else if (j >= 2 + && outbuf.text[j - 1] == '\t' + && outbuf.text[j - 2] == '\n') { outbuf.used -= 2; trimmed = TRUE; } else if (j >= 4 @@ -1175,7 +1224,7 @@ fmt_entry(TERMTYPE *tterm, } static bool -kill_string(TERMTYPE *tterm, char *cap) +kill_string(TERMTYPE2 *tterm, char *cap) { unsigned n; for (n = 0; n < NUM_STRINGS(tterm); ++n) { @@ -1188,7 +1237,7 @@ kill_string(TERMTYPE *tterm, char *cap) } static char * -find_string(TERMTYPE *tterm, char *name) +find_string(TERMTYPE2 *tterm, char *name) { PredIdx n; for (n = 0; n < NUM_STRINGS(tterm); ++n) { @@ -1209,7 +1258,7 @@ find_string(TERMTYPE *tterm, char *name) * make it smaller. */ static int -kill_labels(TERMTYPE *tterm, int target) +kill_labels(TERMTYPE2 *tterm, int target) { int n; int result = 0; @@ -1234,7 +1283,7 @@ kill_labels(TERMTYPE *tterm, int target) * make it smaller. */ static int -kill_fkeys(TERMTYPE *tterm, int target) +kill_fkeys(TERMTYPE2 *tterm, int target) { int n; int result = 0; @@ -1288,7 +1337,7 @@ one_one_mapping(const char *mapping) #define SHOW_WHY PRINTF static bool -purged_acs(TERMTYPE *tterm) +purged_acs(TERMTYPE2 *tterm) { bool result = FALSE; @@ -1315,16 +1364,16 @@ encode_b64(char *target, char *source, unsigned state, int *saved) switch (state % 3) { case 0: - *target++ = data[ch & 077]; - *saved = (ch >> 6) & 3; + *target++ = data[(ch >> 2) & 077]; + *saved = (ch << 4); break; case 1: - *target++ = data[((ch << 2) | *saved) & 077]; - *saved = (ch >> 4) & 017; + *target++ = data[((ch >> 4) | *saved) & 077]; + *saved = (ch << 2); break; case 2: - *target++ = data[((ch << 4) | *saved) & 077]; - *target++ = data[(ch >> 2) & 077]; + *target++ = data[((ch >> 6) | *saved) & 077]; + *target++ = data[ch & 077]; *saved = 0; break; } @@ -1335,13 +1384,13 @@ encode_b64(char *target, char *source, unsigned state, int *saved) * Dump a single entry. */ void -dump_entry(TERMTYPE *tterm, +dump_entry(TERMTYPE2 *tterm, int suppress_untranslatable, int limited, int numbers, PredFunc pred) { - TERMTYPE save_tterm; + TERMTYPE2 save_tterm; int len, critlen; const char *legend; bool infodump; @@ -1366,6 +1415,8 @@ dump_entry(TERMTYPE *tterm, } } if (quickdump & 2) { + static char padding[] = + {0, 0}; int value = 0; if (outbuf.used) wrap_concat("\n"); @@ -1378,10 +1429,14 @@ dump_entry(TERMTYPE *tterm, case 0: break; case 1: - wrap_concat("==="); + encode_b64(numbuf, padding, 1, &value); + wrap_concat(numbuf); + wrap_concat("=="); break; case 2: - wrap_concat("=="); + encode_b64(numbuf, padding, 1, &value); + wrap_concat(numbuf); + wrap_concat("="); break; } } @@ -1478,7 +1533,8 @@ dump_entry(TERMTYPE *tterm, } if (len > critlen) { (void) fprintf(stderr, - "warning: %s entry is %d bytes long\n", + "%s: %s entry is %d bytes long\n", + _nc_progname, _nc_first_name(tterm->term_names), len); SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n", @@ -1547,7 +1603,7 @@ show_entry(void) void compare_entry(PredHook hook, - TERMTYPE *tp GCC_UNUSED, + TERMTYPE2 *tp GCC_UNUSED, bool quiet) /* compare two entries */ { @@ -1606,7 +1662,7 @@ compare_entry(PredHook hook, #define CUR tp-> static void -set_obsolete_termcaps(TERMTYPE *tp) +set_obsolete_termcaps(TERMTYPE2 *tp) { #include "capdefaults.c" } @@ -1616,7 +1672,7 @@ set_obsolete_termcaps(TERMTYPE *tp) * unique. */ void -repair_acsc(TERMTYPE *tp) +repair_acsc(TERMTYPE2 *tp) { if (VALID_STRING(acs_chars)) { size_t n, m;