]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/write_entry.c
ncurses 6.0 - patch 20171125
[ncurses.git] / ncurses / tinfo / write_entry.c
index 45f315977daad89f7be3534ba7d39d61c926cd5d..f480e1996a79d07d5cac245c594ffbc4760d01db 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2015,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            *
 #define TRACE_OUT(p)           /*nothing */
 #endif
 
-MODULE_ID("$Id: write_entry.c,v 1.87 2012/12/29 23:12:22 tom Exp $")
+MODULE_ID("$Id: write_entry.c,v 1.100 2017/11/25 02:06:43 tom Exp $")
 
 static int total_written;
+static int total_parts;
+static int total_size;
 
 static int make_db_root(const char *);
-static int write_object(TERMTYPE *, char *, unsigned *, unsigned);
 
 #if !USE_HASHED_DB
 static void
-write_file(char *filename, TERMTYPE *tp)
+write_file(char *filename, TERMTYPE2 *tp)
 {
     char buffer[MAX_ENTRY_SIZE];
     unsigned limit = sizeof(buffer);
@@ -69,7 +70,7 @@ write_file(char *filename, TERMTYPE *tp)
     }
     DEBUG(1, ("Created %s", filename));
 
-    if (write_object(tp, buffer, &offset, limit) == ERR
+    if (_nc_write_object(tp, buffer, &offset, limit) == ERR
        || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
        _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
     }
@@ -93,7 +94,7 @@ check_writeable(int code)
     char dir[sizeof(LEAF_FMT)];
     char *s = 0;
 
-    if (code == 0 || (s = strchr(dirnames, code)) == 0)
+    if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
        _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
 
     if (verified[s - dirnames])
@@ -185,13 +186,16 @@ make_db_root(const char *path)
  * Set the write directory for compiled entries.
  */
 NCURSES_EXPORT(void)
-_nc_set_writedir(char *dir)
+_nc_set_writedir(const char *dir)
 {
     const char *destination;
     char actual[PATH_MAX];
 
     if (dir == 0
-       && use_terminfo_vars())
+#ifndef USE_ROOT_ENVIRON
+       && use_terminfo_vars()
+#endif
+       )
        dir = getenv("TERMINFO");
 
     if (dir != 0)
@@ -244,7 +248,7 @@ _nc_set_writedir(char *dir)
  */
 
 NCURSES_EXPORT(void)
-_nc_write_entry(TERMTYPE *const tp)
+_nc_write_entry(TERMTYPE2 *const tp)
 {
 #if USE_HASHED_DB
 
@@ -265,6 +269,9 @@ _nc_write_entry(TERMTYPE *const tp)
 #endif
 #endif /* USE_SYMLINKS */
 
+    unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
+    char saved = '\0';
+
     static int call_count;
     static time_t start_time;  /* time at start of writes */
 
@@ -273,7 +280,7 @@ _nc_write_entry(TERMTYPE *const tp)
     char name_list[MAX_TERMINFO_LENGTH];
     char *first_name, *other_names;
     char *ptr;
-    const char *term_names = tp->term_names;
+    char *term_names = tp->term_names;
     size_t name_size = strlen(term_names);
 
     if (name_size == 0) {
@@ -313,7 +320,7 @@ _nc_write_entry(TERMTYPE *const tp)
     _nc_set_type(first_name);
 
 #if USE_HASHED_DB
-    if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
+    if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
        DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
        DBT key, data;
 
@@ -333,13 +340,15 @@ _nc_write_entry(TERMTYPE *const tp)
            buffer[0] = 2;
 
            key.data = name_list;
-           key.size = name_size;
+           key.size = strlen(name_list);
 
            _nc_STRCPY(buffer + 1,
                       term_names,
                       sizeof(buffer) - 1);
            data.size = name_size + 1;
 
+           total_size += data.size;
+           total_parts++;
            _nc_db_put(capdb, &key, &data);
 
            while (*other_names != '\0') {
@@ -354,6 +363,8 @@ _nc_write_entry(TERMTYPE *const tp)
                key.data = ptr;
                key.size = strlen(ptr);
 
+               total_size += data.size;
+               total_parts++;
                _nc_db_put(capdb, &key, &data);
            }
        }
@@ -363,12 +374,18 @@ _nc_write_entry(TERMTYPE *const tp)
        start_time = 0;
     }
 
-    if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
+    if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN)) {
        _nc_warning("terminal name too long.");
+       saved = first_name[limit2];
+       first_name[limit2] = '\0';
+    }
 
     _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
                LEAF_FMT "/%s", first_name[0], first_name);
 
+    if (saved)
+       first_name[limit2] = saved;
+
     /*
      * Has this primary name been written since the first call to
      * write_entry()?  If so, the newer write will step on the older,
@@ -436,13 +453,14 @@ _nc_write_entry(TERMTYPE *const tp)
        {
            int code;
 #if USE_SYMLINKS
-           if (first_name[0] == linkname[0])
-               strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
-           else {
-               _nc_STRCPY(symlinkname, "../", sizeof(suymlinkname));
-               strncat(symlinkname, filename, sizeof(symlinkname) - 4);
+#define MY_SIZE sizeof(symlinkname) - 1
+           if (first_name[0] == linkname[0]) {
+               _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
+           } else {
+               _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
+               _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
            }
-           symlinkname[sizeof(symlinkname) - 1] = '\0';
+           symlinkname[MY_SIZE] = '\0';
 #endif /* USE_SYMLINKS */
 #if HAVE_REMOVE
            code = remove(linkname);
@@ -556,12 +574,34 @@ convert_shorts(unsigned char *buf, short *Numbers, size_t count)
     }
 }
 
+#if NCURSES_EXT_NUMBERS
+static void
+convert_numbers(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
+{
+    size_t i;
+    for (i = 0; i < count; i++) {
+       if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
+           buf[2 * i] = buf[2 * i + 1] = 0377;
+       } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
+           buf[2 * i] = 0376;
+           buf[2 * i + 1] = 0377;
+       } else {
+           LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
+           TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
+       }
+    }
+}
+
+#else
+#define convert_numbers(buf,vec,len) convert_shorts(buf,vec,len)
+#endif
+
 #define even_boundary(value) \
            ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
 
 #if NCURSES_XNAMES
 static unsigned
-extended_Booleans(TERMTYPE *tp)
+extended_Booleans(TERMTYPE2 *tp)
 {
     unsigned result = 0;
     unsigned i;
@@ -574,7 +614,7 @@ extended_Booleans(TERMTYPE *tp)
 }
 
 static unsigned
-extended_Numbers(TERMTYPE *tp)
+extended_Numbers(TERMTYPE2 *tp)
 {
     unsigned result = 0;
     unsigned i;
@@ -587,7 +627,7 @@ extended_Numbers(TERMTYPE *tp)
 }
 
 static unsigned
-extended_Strings(TERMTYPE *tp)
+extended_Strings(TERMTYPE2 *tp)
 {
     unsigned short result = 0;
     unsigned short i;
@@ -604,7 +644,7 @@ extended_Strings(TERMTYPE *tp)
  * clause - discard the unneeded data.
  */
 static bool
-extended_object(TERMTYPE *tp)
+extended_object(TERMTYPE2 *tp)
 {
     bool result = FALSE;
 
@@ -617,8 +657,8 @@ extended_object(TERMTYPE *tp)
 }
 #endif
 
-static int
-write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
+NCURSES_EXPORT(int)
+_nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
 {
     char *namelist;
     size_t namelen, boolmax, nummax, strmax;
@@ -696,7 +736,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
     TRACE_OUT(("Numerics begin at %04x", *offset));
 
     /* the numerics */
-    convert_shorts(buf, tp->Numbers, nummax);
+    convert_numbers(buf, tp->Numbers, nummax);
     if (Write(buf, 2, nummax) != nummax)
        return (ERR);
 
@@ -759,7 +799,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
 
        TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
        if (tp->ext_Numbers) {
-           convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
+           convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
            if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
                return (ERR);
        }
@@ -799,6 +839,8 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
 #endif /* NCURSES_XNAMES */
 
     total_written++;
+    total_parts++;
+    total_size = total_size + (int) (*offset + 1);
     return (OK);
 }
 
@@ -808,5 +850,7 @@ write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
 NCURSES_EXPORT(int)
 _nc_tic_written(void)
 {
+    TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
+                       total_written, total_parts, total_size));
     return total_written;
 }