1 /****************************************************************************
2 * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
36 * write_entry.c -- write a terminfo structure onto the file system
39 #include <curses.priv.h>
40 #include <hashed_db.h>
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])); }
49 #define TRACE_OUT(p) /*nothing */
50 #define TRACE_NUM(n) /* nothing */
53 MODULE_ID("$Id: write_entry.c,v 1.114 2019/06/29 23:07:18 tom Exp $")
55 static int total_written;
56 static int total_parts;
57 static int total_size;
59 static int make_db_root(const char *);
63 write_file(char *filename, TERMTYPE2 *tp)
65 char buffer[MAX_ENTRY_SIZE];
66 unsigned limit = sizeof(buffer);
69 if (_nc_write_object(tp, buffer, &offset, limit) == ERR) {
70 _nc_warning("entry is larger than %u bytes", limit);
72 FILE *fp = ((_nc_access(filename, W_OK) == 0)
73 ? fopen(filename, BIN_W)
79 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
82 actual = fwrite(buffer, sizeof(char), (size_t) offset, fp);
83 if (actual != offset) {
84 int myerr = ferror(fp) ? errno : 0;
86 _nc_syserr_abort("error writing %s/%s: %s",
91 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu",
95 (unsigned long) actual);
99 DEBUG(1, ("Created %s", filename));
105 * Check for access rights to destination directories
106 * Create any directories which don't exist.
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.
112 check_writeable(int code)
114 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
115 static bool verified[sizeof(dirnames)];
117 char dir[sizeof(LEAF_FMT)];
120 if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
121 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
123 if (verified[s - dirnames])
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);
131 verified[s - dirnames] = TRUE;
133 #endif /* !USE_HASHED_DB */
136 make_db_path(char *dst, const char *src, size_t limit)
139 const char *top = _nc_tic_dir(0);
141 if (src == top || _nc_is_abs_path(src)) {
142 if (strlen(src) + 1 <= limit) {
143 _nc_STRCPY(dst, src, limit);
147 if (strlen(top) + strlen(src) + 2 <= limit) {
148 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
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);
163 } else if (_nc_is_dir_path(dst)) {
172 * Make a database-root if it doesn't exist.
175 make_db_root(const char *path)
178 char fullpath[PATH_MAX];
180 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
184 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
186 } else if (_nc_db_close(capdbp) < 0) {
192 if ((rc = stat(path, &statbuf)) < 0) {
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 */
209 * Set the write directory for compiled entries.
212 _nc_set_writedir(const char *dir)
214 const char *destination;
215 char actual[PATH_MAX];
218 #ifndef USE_ROOT_ENVIRON
219 && use_terminfo_vars()
222 dir = getenv("TERMINFO");
225 (void) _nc_tic_dir(dir);
227 destination = _nc_tic_dir(0);
228 if (make_db_root(destination) < 0) {
229 char *home = _nc_home_terminfo();
233 if (make_db_root(destination) < 0)
234 _nc_err_abort("%s: permission denied (errno %d)",
240 * Note: because of this code, this logic should be exercised
241 * *once only* per run.
244 make_db_path(actual, destination, sizeof(actual));
246 if (chdir(_nc_tic_dir(destination)) < 0
247 || getcwd(actual, sizeof(actual)) == 0)
248 _nc_err_abort("%s: not a directory", destination);
250 _nc_keep_tic_dir(strdup(actual));
254 * Save the compiled version of a description in the filesystem.
256 * make a copy of the name-list
257 * break it up into first-name and all-but-last-name
259 * write object information to first-name
261 * for each name in all-but-last-name
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.
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.
274 _nc_write_entry(TERMTYPE2 *const tp)
278 char buffer[MAX_ENTRY_SIZE + 1];
279 unsigned limit = sizeof(buffer);
282 #else /* !USE_HASHED_DB */
285 char filename[PATH_MAX];
286 char linkname[PATH_MAX];
288 char symlinkname[PATH_MAX];
293 #endif /* USE_SYMLINKS */
295 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
298 static int call_count;
299 static time_t start_time; /* time at start of writes */
301 #endif /* USE_HASHED_DB */
303 char name_list[MAX_TERMINFO_LENGTH];
304 char *first_name, *other_names;
306 char *term_names = tp->term_names;
307 size_t name_size = strlen(term_names);
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);
315 _nc_STRCPY(name_list, term_names, sizeof(name_list));
316 DEBUG(7, ("Name list = '%s'", name_list));
318 first_name = name_list;
320 ptr = &name_list[name_size - 1];
321 other_names = ptr + 1;
323 while (ptr > name_list && *ptr != '|')
326 if (ptr != name_list) {
329 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
336 other_names = ptr + 1;
340 DEBUG(7, ("First name = '%s'", first_name));
341 DEBUG(7, ("Other names = '%s'", other_names));
343 _nc_set_type(first_name);
346 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
347 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
353 memset(&key, 0, sizeof(key));
354 key.data = term_names;
355 key.size = name_size;
357 memset(&data, 0, sizeof(data));
359 data.size = offset + 1;
361 _nc_db_put(capdb, &key, &data);
365 key.data = name_list;
366 key.size = strlen(name_list);
368 _nc_STRCPY(buffer + 1,
371 data.size = name_size + 1;
373 total_size += data.size;
375 _nc_db_put(capdb, &key, &data);
377 while (*other_names != '\0') {
379 assert(ptr < buffer + sizeof(buffer) - 1);
380 while (*other_names != '|' && *other_names != '\0')
383 if (*other_names != '\0')
384 *(other_names++) = '\0';
387 key.size = strlen(ptr);
389 total_size += data.size;
391 _nc_db_put(capdb, &key, &data);
395 #else /* !USE_HASHED_DB */
396 if (call_count++ == 0) {
400 if (strlen(first_name) >= limit2) {
401 _nc_warning("terminal name too long.");
402 saved = first_name[limit2];
403 first_name[limit2] = '\0';
406 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
407 LEAF_FMT "/%.*s", UChar(first_name[0]),
408 (int) (sizeof(filename) - (LEAF_LEN + 2)),
412 first_name[limit2] = saved;
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,
419 if (start_time > 0 &&
420 stat(filename, &statbuf) >= 0
421 && statbuf.st_mtime >= start_time) {
422 #if HAVE_LINK && !USE_SYMLINKS
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.
429 if (statbuf.st_nlink > 1) {
430 _nc_warning("name redefined.");
433 _nc_warning("name multiply defined.");
436 _nc_warning("name multiply defined.");
440 check_writeable(first_name[0]);
441 write_file(filename, tp);
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);
450 while (*other_names != '\0') {
452 while (*other_names != '|' && *other_names != '\0')
455 if (*other_names != '\0')
456 *(other_names++) = '\0';
458 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
459 _nc_warning("terminal alias %s too long.", ptr);
462 if (strchr(ptr, '/') != 0) {
463 _nc_warning("cannot link alias %s.", ptr);
467 check_writeable(ptr[0]);
468 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
469 LEAF_FMT "/%.*s", ptr[0],
470 (int) sizeof(linkname) - (2 + LEAF_LEN), ptr);
472 if (strcmp(filename, linkname) == 0) {
473 _nc_warning("self-synonym ignored");
474 } else if (stat(linkname, &statbuf) >= 0 &&
475 statbuf.st_mtime < start_time) {
476 _nc_warning("alias %s multiply defined.", ptr);
477 } else if (_nc_access(linkname, W_OK) == 0)
482 #define MY_SIZE sizeof(symlinkname) - 1
483 if (first_name[0] == linkname[0]) {
484 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
486 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
487 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
489 symlinkname[MY_SIZE] = '\0';
490 #endif /* USE_SYMLINKS */
492 code = remove(linkname);
494 code = unlink(linkname);
496 if (code != 0 && errno == ENOENT)
499 if (symlink(symlinkname, linkname) < 0)
501 if (link(filename, linkname) < 0)
502 #endif /* USE_SYMLINKS */
505 * If there wasn't anything there, and we cannot
506 * link to the target because it is the same as the
507 * target, then the source must be on a filesystem
508 * that uses caseless filenames, such as Win32, etc.
510 if (code == 0 && errno == EEXIST)
511 _nc_warning("can't link %s to %s", filename, linkname);
512 else if (code == 0 && (errno == EPERM || errno == ENOENT))
513 write_file(linkname, tp);
515 #if MIXEDCASE_FILENAMES
516 _nc_syserr_abort("can't link %s to %s", filename, linkname);
518 _nc_warning("can't link %s to %s (errno=%d)", filename,
523 DEBUG(1, ("Linked %s", linkname));
526 #else /* just make copies */
527 write_file(linkname, tp);
528 #endif /* HAVE_LINK */
530 #endif /* USE_HASHED_DB */
534 fake_write(char *dst,
541 size_t have = (limit - *offset);
547 memcpy(dst + *offset, src, want);
548 *offset += (unsigned) want;
552 return (want / size);
555 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
557 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
558 #define HI(x) ((x) / 256)
559 #define LO(x) ((x) % 256)
560 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
561 (p)[1] = (unsigned char)HI(x)
563 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
566 compute_offsets(char **Strings, size_t strmax, short *offsets)
571 for (i = 0; i < strmax; i++) {
572 if (Strings[i] == ABSENT_STRING) {
574 } else if (Strings[i] == CANCELLED_STRING) {
577 offsets[i] = (short) nextfree;
578 nextfree += (int) strlen(Strings[i]) + 1;
579 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
580 _nc_visbuf(Strings[i]), (int) nextfree));
587 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
590 for (i = 0; i < count; i++) {
591 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
592 buf[2 * i] = buf[2 * i + 1] = 0377;
593 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
595 buf[2 * i + 1] = 0377;
597 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
598 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
604 #if NCURSES_EXT_NUMBERS
606 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
609 size_t size = SIZEOF_SHORT;
610 for (i = 0; i < count; i++) {
611 unsigned value = (unsigned) Numbers[i];
613 for (j = 0; j < size; ++j) {
614 *buf++ = value & 0xff;
622 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
625 size_t size = SIZEOF_INT2;
626 for (i = 0; i < count; i++) {
627 unsigned value = (unsigned) Numbers[i];
629 for (j = 0; j < size; ++j) {
630 *buf++ = value & 0xff;
638 #define even_boundary(value) \
639 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
643 extended_Booleans(TERMTYPE2 *tp)
648 for (i = 0; i < tp->ext_Booleans; ++i) {
649 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
656 extended_Numbers(TERMTYPE2 *tp)
661 for (i = 0; i < tp->ext_Numbers; ++i) {
662 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
669 extended_Strings(TERMTYPE2 *tp)
671 unsigned short result = 0;
674 for (i = 0; i < tp->ext_Strings; ++i) {
675 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
676 result = (unsigned short) (i + 1);
682 * _nc_align_termtype() will extend entries that are referenced in a use=
683 * clause - discard the unneeded data.
686 extended_object(TERMTYPE2 *tp)
690 if (_nc_user_definable) {
691 result = ((extended_Booleans(tp)
692 + extended_Numbers(tp)
693 + extended_Strings(tp)) != 0);
700 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
703 size_t namelen, boolmax, nummax, strmax, numlen;
707 short offsets[MAX_ENTRY_SIZE / 2];
708 unsigned char buf[MAX_ENTRY_SIZE];
709 unsigned last_bool = BOOLWRITE;
710 unsigned last_num = NUMWRITE;
711 unsigned last_str = STRWRITE;
712 #if NCURSES_EXT_NUMBERS
713 bool need_ints = FALSE;
714 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t) = convert_32bit;
716 #define convert_numbers convert_shorts
721 * Normally we limit the list of values to exclude the "obsolete"
722 * capabilities. However, if we are accepting extended names, add
723 * these as well, since they are used for supporting translation
726 if (_nc_user_definable) {
727 last_bool = BOOLCOUNT;
733 namelist = tp->term_names;
734 namelen = strlen(namelist) + 1;
737 for (i = 0; i < last_bool; i++) {
738 if (tp->Booleans[i] == TRUE) {
744 for (i = 0; i < last_num; i++) {
745 if (tp->Numbers[i] != ABSENT_NUMERIC) {
747 #if NCURSES_EXT_NUMBERS
748 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
756 for (i = 0; i < last_str; i++) {
757 if (tp->Strings[i] != ABSENT_STRING)
761 nextfree = compute_offsets(tp->Strings, strmax, offsets);
763 /* fill in the header */
764 #if NCURSES_EXT_NUMBERS
766 convert_numbers = convert_32bit;
767 LITTLE_ENDIAN(buf, MAGIC2);
769 convert_numbers = convert_16bit;
770 LITTLE_ENDIAN(buf, MAGIC);
773 LITTLE_ENDIAN(buf, MAGIC);
775 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
776 LITTLE_ENDIAN(buf + 4, boolmax);
777 LITTLE_ENDIAN(buf + 6, nummax);
778 LITTLE_ENDIAN(buf + 8, strmax);
779 LITTLE_ENDIAN(buf + 10, nextfree);
781 /* write out the header */
782 TRACE_OUT(("Header of %s @%d", namelist, *offset));
783 if (Write(buf, 12, 1) != 1
784 || Write(namelist, sizeof(char), namelen) != namelen) {
788 for (i = 0; i < boolmax; i++) {
789 if (tp->Booleans[i] == TRUE) {
795 if (Write(buf, sizeof(char), boolmax) != boolmax) {
799 if (even_boundary(namelen + boolmax)) {
803 TRACE_OUT(("Numerics begin at %04x", *offset));
806 numlen = convert_numbers(buf, tp->Numbers, nummax);
807 if (Write(buf, numlen, nummax) != nummax) {
811 TRACE_OUT(("String offsets begin at %04x", *offset));
813 /* the string offsets */
814 convert_shorts(buf, offsets, strmax);
815 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
819 TRACE_OUT(("String table begins at %04x", *offset));
822 for (i = 0; i < strmax; i++) {
823 if (VALID_STRING(tp->Strings[i])) {
824 if (!WRITE_STRING(tp->Strings[i])) {
831 if (extended_object(tp)) {
832 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
833 unsigned ext_usage = ext_total;
835 if (even_boundary(nextfree)) {
839 nextfree = compute_offsets(tp->Strings + STRCOUNT,
840 (size_t) tp->ext_Strings,
842 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
844 if (tp->ext_Strings >= SIZEOF(offsets)) {
848 nextfree += compute_offsets(tp->ext_Names,
850 offsets + tp->ext_Strings);
851 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
852 strmax = tp->ext_Strings + ext_total;
853 for (i = 0; i < tp->ext_Strings; ++i) {
854 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
858 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
861 * Write the extended header
863 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
864 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
865 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
866 LITTLE_ENDIAN(buf + 6, ext_usage);
867 LITTLE_ENDIAN(buf + 8, nextfree);
868 TRACE_OUT(("WRITE extended-header @%d", *offset));
869 if (Write(buf, 10, 1) != 1) {
873 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
875 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
876 tp->ext_Booleans) != tp->ext_Booleans) {
880 if (even_boundary(tp->ext_Booleans)) {
884 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
885 if (tp->ext_Numbers) {
886 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
887 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) {
893 * Convert the offsets for the ext_Strings and ext_Names tables,
896 convert_shorts(buf, offsets, strmax);
897 TRACE_OUT(("WRITE offsets @%d", *offset));
898 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
903 * Write the string table after the offset tables so we do not
904 * have to do anything about alignment.
906 for (i = 0; i < tp->ext_Strings; i++) {
907 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
908 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
909 _nc_visbuf(tp->Strings[i + STRCOUNT])));
910 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) {
917 * Write the extended names
919 for (i = 0; i < ext_total; i++) {
920 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
921 if (!WRITE_STRING(tp->ext_Names[i])) {
927 #endif /* NCURSES_XNAMES */
931 total_size = total_size + (int) (*offset + 1);
936 * Returns the total number of entries written by this process
939 _nc_tic_written(void)
941 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
942 total_written, total_parts, total_size));
943 return total_written;