]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/write_entry.c
ncurses 6.1 - patch 20190317
[ncurses.git] / ncurses / tinfo / write_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  ****************************************************************************/
34
35 /*
36  *      write_entry.c -- write a terminfo structure onto the file system
37  */
38
39 #include <curses.priv.h>
40 #include <hashed_db.h>
41
42 #include <tic.h>
43
44 #if 1
45 #define TRACE_OUT(p) DEBUG(2, p)
46 #define TRACE_NUM(n) if (VALID_NUMERIC(Numbers[n])) { \
47         TRACE_OUT(("put Numbers[%u]=%d", (unsigned) (n), Numbers[n])); }
48 #else
49 #define TRACE_OUT(p)            /*nothing */
50 #define TRACE_NUM(n)            /* nothing */
51 #endif
52
53 MODULE_ID("$Id: write_entry.c,v 1.111 2019/01/20 02:54:14 tom Exp $")
54
55 static int total_written;
56 static int total_parts;
57 static int total_size;
58
59 static int make_db_root(const char *);
60
61 #if !USE_HASHED_DB
62 static void
63 write_file(char *filename, TERMTYPE2 *tp)
64 {
65     char buffer[MAX_ENTRY_SIZE];
66     unsigned limit = sizeof(buffer);
67     unsigned offset = 0;
68
69     if (_nc_write_object(tp, buffer, &offset, limit) == ERR) {
70         _nc_warning("entry is larger than %u bytes", limit);
71     } else {
72         FILE *fp = ((_nc_access(filename, W_OK) == 0)
73                     ? fopen(filename, "wb")
74                     : 0);
75         size_t actual;
76
77         if (fp == 0) {
78             perror(filename);
79             _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
80         }
81
82         actual = fwrite(buffer, sizeof(char), (size_t) offset, fp);
83         if (actual != offset) {
84             int myerr = ferror(fp) ? errno : 0;
85             if (myerr) {
86                 _nc_syserr_abort("error writing %s/%s: %s",
87                                  _nc_tic_dir(0),
88                                  filename,
89                                  strerror(myerr));
90             } else {
91                 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu",
92                                  _nc_tic_dir(0),
93                                  filename,
94                                  offset,
95                                  (unsigned long) actual);
96             }
97         } else {
98             fclose(fp);
99             DEBUG(1, ("Created %s", filename));
100         }
101     }
102 }
103
104 /*
105  * Check for access rights to destination directories
106  * Create any directories which don't exist.
107  *
108  * Note:  there's no reason to return the result of make_db_root(), since
109  * this function is called only in instances where that has to succeed.
110  */
111 static void
112 check_writeable(int code)
113 {
114     static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
115     static bool verified[sizeof(dirnames)];
116
117     char dir[sizeof(LEAF_FMT)];
118     char *s = 0;
119
120     if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
121         _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
122
123     if (verified[s - dirnames])
124         return;
125
126     _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
127     if (make_db_root(dir) < 0) {
128         _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
129     }
130
131     verified[s - dirnames] = TRUE;
132 }
133 #endif /* !USE_HASHED_DB */
134
135 static int
136 make_db_path(char *dst, const char *src, size_t limit)
137 {
138     int rc = -1;
139     const char *top = _nc_tic_dir(0);
140
141     if (src == top || _nc_is_abs_path(src)) {
142         if (strlen(src) + 1 <= limit) {
143             _nc_STRCPY(dst, src, limit);
144             rc = 0;
145         }
146     } else {
147         if (strlen(top) + strlen(src) + 2 <= limit) {
148             _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
149             rc = 0;
150         }
151     }
152 #if USE_HASHED_DB
153     if (rc == 0) {
154         static const char suffix[] = DBM_SUFFIX;
155         size_t have = strlen(dst);
156         size_t need = strlen(suffix);
157         if (have > need && strcmp(dst + (int) (have - need), suffix)) {
158             if (have + need <= limit) {
159                 _nc_STRCAT(dst, suffix, limit);
160             } else {
161                 rc = -1;
162             }
163         } else if (_nc_is_dir_path(dst)) {
164             rc = -1;
165         }
166     }
167 #endif
168     return rc;
169 }
170
171 /*
172  * Make a database-root if it doesn't exist.
173  */
174 static int
175 make_db_root(const char *path)
176 {
177     int rc;
178     char fullpath[PATH_MAX];
179
180     if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
181 #if USE_HASHED_DB
182         DB *capdbp;
183
184         if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
185             rc = -1;
186         } else if (_nc_db_close(capdbp) < 0) {
187             rc = -1;
188         }
189 #else
190         struct stat statbuf;
191
192         if ((rc = stat(path, &statbuf)) < 0) {
193             rc = mkdir(path
194 #if !defined(_WIN32)
195                        ,0777
196 #endif
197                 );
198         } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
199             rc = -1;            /* permission denied */
200         } else if (!(S_ISDIR(statbuf.st_mode))) {
201             rc = -1;            /* not a directory */
202         }
203 #endif
204     }
205     return rc;
206 }
207
208 /*
209  * Set the write directory for compiled entries.
210  */
211 NCURSES_EXPORT(void)
212 _nc_set_writedir(const char *dir)
213 {
214     const char *destination;
215     char actual[PATH_MAX];
216
217     if (dir == 0
218 #ifndef USE_ROOT_ENVIRON
219         && use_terminfo_vars()
220 #endif
221         )
222         dir = getenv("TERMINFO");
223
224     if (dir != 0)
225         (void) _nc_tic_dir(dir);
226
227     destination = _nc_tic_dir(0);
228     if (make_db_root(destination) < 0) {
229         char *home = _nc_home_terminfo();
230
231         if (home != 0) {
232             destination = home;
233             if (make_db_root(destination) < 0)
234                 _nc_err_abort("%s: permission denied (errno %d)",
235                               destination, errno);
236         }
237     }
238
239     /*
240      * Note: because of this code, this logic should be exercised
241      * *once only* per run.
242      */
243 #if USE_HASHED_DB
244     make_db_path(actual, destination, sizeof(actual));
245 #else
246     if (chdir(_nc_tic_dir(destination)) < 0
247         || getcwd(actual, sizeof(actual)) == 0)
248         _nc_err_abort("%s: not a directory", destination);
249 #endif
250     _nc_keep_tic_dir(strdup(actual));
251 }
252
253 /*
254  *      Save the compiled version of a description in the filesystem.
255  *
256  *      make a copy of the name-list
257  *      break it up into first-name and all-but-last-name
258  *      creat(first-name)
259  *      write object information to first-name
260  *      close(first-name)
261  *      for each name in all-but-last-name
262  *          link to first-name
263  *
264  *      Using 'time()' to obtain a reference for file timestamps is unreliable,
265  *      e.g., with NFS, because the filesystem may have a different time
266  *      reference.  We check for pre-existence of links by latching the first
267  *      timestamp from a file that we create.
268  *
269  *      The _nc_warning() calls will report a correct line number only if
270  *      _nc_curr_line is properly set before the write_entry() call.
271  */
272
273 NCURSES_EXPORT(void)
274 _nc_write_entry(TERMTYPE2 *const tp)
275 {
276 #if USE_HASHED_DB
277
278     char buffer[MAX_ENTRY_SIZE + 1];
279     unsigned limit = sizeof(buffer);
280     unsigned offset = 0;
281
282 #else /* !USE_HASHED_DB */
283
284     struct stat statbuf;
285     char filename[PATH_MAX];
286     char linkname[PATH_MAX];
287 #if USE_SYMLINKS
288     char symlinkname[PATH_MAX];
289 #if !HAVE_LINK
290 #undef HAVE_LINK
291 #define HAVE_LINK 1
292 #endif
293 #endif /* USE_SYMLINKS */
294
295     unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
296     char saved = '\0';
297
298     static int call_count;
299     static time_t start_time;   /* time at start of writes */
300
301 #endif /* USE_HASHED_DB */
302
303     char name_list[MAX_TERMINFO_LENGTH];
304     char *first_name, *other_names;
305     char *ptr;
306     char *term_names = tp->term_names;
307     size_t name_size = strlen(term_names);
308
309     if (name_size == 0) {
310         _nc_syserr_abort("no terminal name found.");
311     } else if (name_size >= sizeof(name_list) - 1) {
312         _nc_syserr_abort("terminal name too long: %s", term_names);
313     }
314
315     _nc_STRCPY(name_list, term_names, sizeof(name_list));
316     DEBUG(7, ("Name list = '%s'", name_list));
317
318     first_name = name_list;
319
320     ptr = &name_list[name_size - 1];
321     other_names = ptr + 1;
322
323     while (ptr > name_list && *ptr != '|')
324         ptr--;
325
326     if (ptr != name_list) {
327         *ptr = '\0';
328
329         for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
330             continue;
331
332         if (*ptr == '\0')
333             other_names = ptr;
334         else {
335             *ptr = '\0';
336             other_names = ptr + 1;
337         }
338     }
339
340     DEBUG(7, ("First name = '%s'", first_name));
341     DEBUG(7, ("Other names = '%s'", other_names));
342
343     _nc_set_type(first_name);
344
345 #if USE_HASHED_DB
346     if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
347         DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
348         DBT key, data;
349
350         if (capdb != 0) {
351             buffer[0] = 0;
352
353             memset(&key, 0, sizeof(key));
354             key.data = term_names;
355             key.size = name_size;
356
357             memset(&data, 0, sizeof(data));
358             data.data = buffer;
359             data.size = offset + 1;
360
361             _nc_db_put(capdb, &key, &data);
362
363             buffer[0] = 2;
364
365             key.data = name_list;
366             key.size = strlen(name_list);
367
368             _nc_STRCPY(buffer + 1,
369                        term_names,
370                        sizeof(buffer) - 1);
371             data.size = name_size + 1;
372
373             total_size += data.size;
374             total_parts++;
375             _nc_db_put(capdb, &key, &data);
376
377             while (*other_names != '\0') {
378                 ptr = other_names++;
379                 assert(ptr < buffer + sizeof(buffer) - 1);
380                 while (*other_names != '|' && *other_names != '\0')
381                     other_names++;
382
383                 if (*other_names != '\0')
384                     *(other_names++) = '\0';
385
386                 key.data = ptr;
387                 key.size = strlen(ptr);
388
389                 total_size += data.size;
390                 total_parts++;
391                 _nc_db_put(capdb, &key, &data);
392             }
393         }
394     }
395 #else /* !USE_HASHED_DB */
396     if (call_count++ == 0) {
397         start_time = 0;
398     }
399
400     if (strlen(first_name) >= limit2) {
401         _nc_warning("terminal name too long.");
402         saved = first_name[limit2];
403         first_name[limit2] = '\0';
404     }
405
406     _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
407                 LEAF_FMT "/%.*s", UChar(first_name[0]),
408                 (int) (sizeof(filename) - LEAF_LEN - 2),
409                 first_name);
410
411     if (saved)
412         first_name[limit2] = saved;
413
414     /*
415      * Has this primary name been written since the first call to
416      * write_entry()?  If so, the newer write will step on the older,
417      * so warn the user.
418      */
419     if (start_time > 0 &&
420         stat(filename, &statbuf) >= 0
421         && statbuf.st_mtime >= start_time) {
422 #if HAVE_LINK && !USE_SYMLINKS
423         /*
424          * If the file has more than one link, the reason for the previous
425          * write could be that the current primary name used to be an alias for
426          * the previous entry.  In that case, unlink the file so that we will
427          * not modify the previous entry as we write this one.
428          */
429         if (statbuf.st_nlink > 1) {
430             _nc_warning("name redefined.");
431             unlink(filename);
432         } else {
433             _nc_warning("name multiply defined.");
434         }
435 #else
436         _nc_warning("name multiply defined.");
437 #endif
438     }
439
440     check_writeable(first_name[0]);
441     write_file(filename, tp);
442
443     if (start_time == 0) {
444         if (stat(filename, &statbuf) < 0
445             || (start_time = statbuf.st_mtime) == 0) {
446             _nc_syserr_abort("error obtaining time from %s/%s",
447                              _nc_tic_dir(0), filename);
448         }
449     }
450     while (*other_names != '\0') {
451         ptr = other_names++;
452         while (*other_names != '|' && *other_names != '\0')
453             other_names++;
454
455         if (*other_names != '\0')
456             *(other_names++) = '\0';
457
458         if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
459             _nc_warning("terminal alias %s too long.", ptr);
460             continue;
461         }
462         if (strchr(ptr, '/') != 0) {
463             _nc_warning("cannot link alias %s.", ptr);
464             continue;
465         }
466
467         check_writeable(ptr[0]);
468         _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
469                     LEAF_FMT "/%s", ptr[0], ptr);
470
471         if (strcmp(filename, linkname) == 0) {
472             _nc_warning("self-synonym ignored");
473         } else if (stat(linkname, &statbuf) >= 0 &&
474                    statbuf.st_mtime < start_time) {
475             _nc_warning("alias %s multiply defined.", ptr);
476         } else if (_nc_access(linkname, W_OK) == 0)
477 #if HAVE_LINK
478         {
479             int code;
480 #if USE_SYMLINKS
481 #define MY_SIZE sizeof(symlinkname) - 1
482             if (first_name[0] == linkname[0]) {
483                 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
484             } else {
485                 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
486                 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
487             }
488             symlinkname[MY_SIZE] = '\0';
489 #endif /* USE_SYMLINKS */
490 #if HAVE_REMOVE
491             code = remove(linkname);
492 #else
493             code = unlink(linkname);
494 #endif
495             if (code != 0 && errno == ENOENT)
496                 code = 0;
497 #if USE_SYMLINKS
498             if (symlink(symlinkname, linkname) < 0)
499 #else
500             if (link(filename, linkname) < 0)
501 #endif /* USE_SYMLINKS */
502             {
503                 /*
504                  * If there wasn't anything there, and we cannot
505                  * link to the target because it is the same as the
506                  * target, then the source must be on a filesystem
507                  * that uses caseless filenames, such as Win32, etc.
508                  */
509                 if (code == 0 && errno == EEXIST)
510                     _nc_warning("can't link %s to %s", filename, linkname);
511                 else if (code == 0 && (errno == EPERM || errno == ENOENT))
512                     write_file(linkname, tp);
513                 else {
514 #if MIXEDCASE_FILENAMES
515                     _nc_syserr_abort("can't link %s to %s", filename, linkname);
516 #else
517                     _nc_warning("can't link %s to %s (errno=%d)", filename,
518                                 linkname, errno);
519 #endif
520                 }
521             } else {
522                 DEBUG(1, ("Linked %s", linkname));
523             }
524         }
525 #else /* just make copies */
526             write_file(linkname, tp);
527 #endif /* HAVE_LINK */
528     }
529 #endif /* USE_HASHED_DB */
530 }
531
532 static size_t
533 fake_write(char *dst,
534            unsigned *offset,
535            size_t limit,
536            char *src,
537            size_t want,
538            size_t size)
539 {
540     size_t have = (limit - *offset);
541
542     want *= size;
543     if (have > 0) {
544         if (want > have)
545             want = have;
546         memcpy(dst + *offset, src, want);
547         *offset += (unsigned) want;
548     } else {
549         want = 0;
550     }
551     return (want / size);
552 }
553
554 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
555
556 #undef LITTLE_ENDIAN            /* BSD/OS defines this as a feature macro */
557 #define HI(x)                   ((x) / 256)
558 #define LO(x)                   ((x) % 256)
559 #define LITTLE_ENDIAN(p, x)     (p)[0] = (unsigned char)LO(x),  \
560                                 (p)[1] = (unsigned char)HI(x)
561
562 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
563
564 static int
565 compute_offsets(char **Strings, size_t strmax, short *offsets)
566 {
567     int nextfree = 0;
568     size_t i;
569
570     for (i = 0; i < strmax; i++) {
571         if (Strings[i] == ABSENT_STRING) {
572             offsets[i] = -1;
573         } else if (Strings[i] == CANCELLED_STRING) {
574             offsets[i] = -2;
575         } else {
576             offsets[i] = (short) nextfree;
577             nextfree += (int) strlen(Strings[i]) + 1;
578             TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
579                        _nc_visbuf(Strings[i]), (int) nextfree));
580         }
581     }
582     return nextfree;
583 }
584
585 static size_t
586 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
587 {
588     size_t i;
589     for (i = 0; i < count; i++) {
590         if (Numbers[i] == ABSENT_NUMERIC) {     /* HI/LO won't work */
591             buf[2 * i] = buf[2 * i + 1] = 0377;
592         } else if (Numbers[i] == CANCELLED_NUMERIC) {   /* HI/LO won't work */
593             buf[2 * i] = 0376;
594             buf[2 * i + 1] = 0377;
595         } else {
596             LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
597             TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
598         }
599     }
600     return SIZEOF_SHORT;
601 }
602
603 #if NCURSES_EXT_NUMBERS
604 static size_t
605 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
606 {
607     size_t i, j;
608     size_t size = SIZEOF_SHORT;
609     for (i = 0; i < count; i++) {
610         unsigned value = (unsigned) Numbers[i];
611         TRACE_NUM(i);
612         for (j = 0; j < size; ++j) {
613             *buf++ = value & 0xff;
614             value >>= 8;
615         }
616     }
617     return size;
618 }
619
620 static size_t
621 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
622 {
623     size_t i, j;
624     size_t size = SIZEOF_INT2;
625     for (i = 0; i < count; i++) {
626         unsigned value = (unsigned) Numbers[i];
627         TRACE_NUM(i);
628         for (j = 0; j < size; ++j) {
629             *buf++ = value & 0xff;
630             value >>= 8;
631         }
632     }
633     return size;
634 }
635 #endif
636
637 #define even_boundary(value) \
638             ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
639
640 #if NCURSES_XNAMES
641 static unsigned
642 extended_Booleans(TERMTYPE2 *tp)
643 {
644     unsigned result = 0;
645     unsigned i;
646
647     for (i = 0; i < tp->ext_Booleans; ++i) {
648         if (tp->Booleans[BOOLCOUNT + i] == TRUE)
649             result = (i + 1);
650     }
651     return result;
652 }
653
654 static unsigned
655 extended_Numbers(TERMTYPE2 *tp)
656 {
657     unsigned result = 0;
658     unsigned i;
659
660     for (i = 0; i < tp->ext_Numbers; ++i) {
661         if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
662             result = (i + 1);
663     }
664     return result;
665 }
666
667 static unsigned
668 extended_Strings(TERMTYPE2 *tp)
669 {
670     unsigned short result = 0;
671     unsigned short i;
672
673     for (i = 0; i < tp->ext_Strings; ++i) {
674         if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
675             result = (unsigned short) (i + 1);
676     }
677     return result;
678 }
679
680 /*
681  * _nc_align_termtype() will extend entries that are referenced in a use=
682  * clause - discard the unneeded data.
683  */
684 static bool
685 extended_object(TERMTYPE2 *tp)
686 {
687     bool result = FALSE;
688
689     if (_nc_user_definable) {
690         result = ((extended_Booleans(tp)
691                    + extended_Numbers(tp)
692                    + extended_Strings(tp)) != 0);
693     }
694     return result;
695 }
696 #endif
697
698 NCURSES_EXPORT(int)
699 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
700 {
701     char *namelist;
702     size_t namelen, boolmax, nummax, strmax, numlen;
703     char zero = '\0';
704     size_t i;
705     int nextfree;
706     short offsets[MAX_ENTRY_SIZE / 2];
707     unsigned char buf[MAX_ENTRY_SIZE];
708     unsigned last_bool = BOOLWRITE;
709     unsigned last_num = NUMWRITE;
710     unsigned last_str = STRWRITE;
711 #if NCURSES_EXT_NUMBERS
712     bool need_ints = FALSE;
713     size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t) = convert_32bit;
714 #else
715 #define convert_numbers convert_shorts
716 #endif
717
718 #if NCURSES_XNAMES
719     /*
720      * Normally we limit the list of values to exclude the "obsolete"
721      * capabilities.  However, if we are accepting extended names, add
722      * these as well, since they are used for supporting translation
723      * to/from termcap.
724      */
725     if (_nc_user_definable) {
726         last_bool = BOOLCOUNT;
727         last_num = NUMCOUNT;
728         last_str = STRCOUNT;
729     }
730 #endif
731
732     namelist = tp->term_names;
733     namelen = strlen(namelist) + 1;
734
735     boolmax = 0;
736     for (i = 0; i < last_bool; i++) {
737         if (tp->Booleans[i] == TRUE) {
738             boolmax = i + 1;
739         }
740     }
741
742     nummax = 0;
743     for (i = 0; i < last_num; i++) {
744         if (tp->Numbers[i] != ABSENT_NUMERIC) {
745             nummax = i + 1;
746 #if NCURSES_EXT_NUMBERS
747             if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
748                 need_ints = TRUE;
749             }
750 #endif
751         }
752     }
753
754     strmax = 0;
755     for (i = 0; i < last_str; i++) {
756         if (tp->Strings[i] != ABSENT_STRING)
757             strmax = i + 1;
758     }
759
760     nextfree = compute_offsets(tp->Strings, strmax, offsets);
761
762     /* fill in the header */
763 #if NCURSES_EXT_NUMBERS
764     if (need_ints) {
765         convert_numbers = convert_32bit;
766         LITTLE_ENDIAN(buf, MAGIC2);
767     } else {
768         convert_numbers = convert_16bit;
769         LITTLE_ENDIAN(buf, MAGIC);
770     }
771 #else
772     LITTLE_ENDIAN(buf, MAGIC);
773 #endif
774     LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
775     LITTLE_ENDIAN(buf + 4, boolmax);
776     LITTLE_ENDIAN(buf + 6, nummax);
777     LITTLE_ENDIAN(buf + 8, strmax);
778     LITTLE_ENDIAN(buf + 10, nextfree);
779
780     /* write out the header */
781     TRACE_OUT(("Header of %s @%d", namelist, *offset));
782     if (Write(buf, 12, 1) != 1
783         || Write(namelist, sizeof(char), namelen) != namelen) {
784         return (ERR);
785     }
786
787     for (i = 0; i < boolmax; i++) {
788         if (tp->Booleans[i] == TRUE) {
789             buf[i] = TRUE;
790         } else {
791             buf[i] = FALSE;
792         }
793     }
794     if (Write(buf, sizeof(char), boolmax) != boolmax) {
795         return (ERR);
796     }
797
798     if (even_boundary(namelen + boolmax)) {
799         return (ERR);
800     }
801
802     TRACE_OUT(("Numerics begin at %04x", *offset));
803
804     /* the numerics */
805     numlen = convert_numbers(buf, tp->Numbers, nummax);
806     if (Write(buf, numlen, nummax) != nummax) {
807         return (ERR);
808     }
809
810     TRACE_OUT(("String offsets begin at %04x", *offset));
811
812     /* the string offsets */
813     convert_shorts(buf, offsets, strmax);
814     if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
815         return (ERR);
816     }
817
818     TRACE_OUT(("String table begins at %04x", *offset));
819
820     /* the strings */
821     for (i = 0; i < strmax; i++) {
822         if (VALID_STRING(tp->Strings[i])) {
823             if (!WRITE_STRING(tp->Strings[i])) {
824                 return (ERR);
825             }
826         }
827     }
828
829 #if NCURSES_XNAMES
830     if (extended_object(tp)) {
831         unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
832         unsigned ext_usage = ext_total;
833
834         if (even_boundary(nextfree)) {
835             return (ERR);
836         }
837
838         nextfree = compute_offsets(tp->Strings + STRCOUNT,
839                                    (size_t) tp->ext_Strings,
840                                    offsets);
841         TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
842
843         if (tp->ext_Strings >= SIZEOF(offsets)) {
844             return (ERR);
845         }
846
847         nextfree += compute_offsets(tp->ext_Names,
848                                     (size_t) ext_total,
849                                     offsets + tp->ext_Strings);
850         TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
851         strmax = tp->ext_Strings + ext_total;
852         for (i = 0; i < tp->ext_Strings; ++i) {
853             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
854                 ext_usage++;
855             }
856         }
857         TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
858
859         /*
860          * Write the extended header
861          */
862         LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
863         LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
864         LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
865         LITTLE_ENDIAN(buf + 6, ext_usage);
866         LITTLE_ENDIAN(buf + 8, nextfree);
867         TRACE_OUT(("WRITE extended-header @%d", *offset));
868         if (Write(buf, 10, 1) != 1) {
869             return (ERR);
870         }
871
872         TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
873         if (tp->ext_Booleans
874             && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
875                      tp->ext_Booleans) != tp->ext_Booleans) {
876             return (ERR);
877         }
878
879         if (even_boundary(tp->ext_Booleans)) {
880             return (ERR);
881         }
882
883         TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
884         if (tp->ext_Numbers) {
885             numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
886             if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) {
887                 return (ERR);
888             }
889         }
890
891         /*
892          * Convert the offsets for the ext_Strings and ext_Names tables,
893          * in that order.
894          */
895         convert_shorts(buf, offsets, strmax);
896         TRACE_OUT(("WRITE offsets @%d", *offset));
897         if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
898             return (ERR);
899         }
900
901         /*
902          * Write the string table after the offset tables so we do not
903          * have to do anything about alignment.
904          */
905         for (i = 0; i < tp->ext_Strings; i++) {
906             if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
907                 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
908                            _nc_visbuf(tp->Strings[i + STRCOUNT])));
909                 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) {
910                     return (ERR);
911                 }
912             }
913         }
914
915         /*
916          * Write the extended names
917          */
918         for (i = 0; i < ext_total; i++) {
919             TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
920             if (!WRITE_STRING(tp->ext_Names[i])) {
921                 return (ERR);
922             }
923         }
924
925     }
926 #endif /* NCURSES_XNAMES */
927
928     total_written++;
929     total_parts++;
930     total_size = total_size + (int) (*offset + 1);
931     return (OK);
932 }
933
934 /*
935  * Returns the total number of entries written by this process
936  */
937 NCURSES_EXPORT(int)
938 _nc_tic_written(void)
939 {
940     TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
941                         total_written, total_parts, total_size));
942     return total_written;
943 }