1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * write_entry.c -- write a terminfo structure onto the file system
40 #include <curses.priv.h>
41 #include <hashed_db.h>
46 #define TRACE_OUT(p) DEBUG(2, p)
47 #define TRACE_NUM(n) if (VALID_NUMERIC(Numbers[n])) { \
48 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) (n), Numbers[n])); }
50 #define TRACE_OUT(p) /*nothing */
51 #define TRACE_NUM(n) /* nothing */
54 MODULE_ID("$Id: write_entry.c,v 1.118 2021/08/15 20:07:11 tom Exp $")
56 static int total_written;
57 static int total_parts;
58 static int total_size;
60 static int make_db_root(const char *);
64 write_file(char *filename, TERMTYPE2 *tp)
66 char buffer[MAX_ENTRY_SIZE];
67 unsigned limit = sizeof(buffer);
70 if (_nc_write_object(tp, buffer, &offset, limit) == ERR) {
71 _nc_warning("entry is larger than %u bytes", limit);
73 FILE *fp = ((_nc_access(filename, W_OK) == 0)
74 ? safe_fopen(filename, BIN_W)
80 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
83 actual = fwrite(buffer, sizeof(char), (size_t) offset, fp);
84 if (actual != offset) {
85 int myerr = ferror(fp) ? errno : 0;
87 _nc_syserr_abort("error writing %s/%s: %s",
92 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu",
96 (unsigned long) actual);
100 DEBUG(1, ("Created %s", filename));
106 * Check for access rights to destination directories
107 * Create any directories which don't exist.
109 * Note: there's no reason to return the result of make_db_root(), since
110 * this function is called only in instances where that has to succeed.
113 check_writeable(int code)
115 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
116 static bool verified[sizeof(dirnames)];
118 char dir[sizeof(LEAF_FMT)];
121 if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
122 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
124 if (verified[s - dirnames])
127 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
128 if (make_db_root(dir) < 0) {
129 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
132 verified[s - dirnames] = TRUE;
134 #endif /* !USE_HASHED_DB */
137 make_db_path(char *dst, const char *src, size_t limit)
140 const char *top = _nc_tic_dir(0);
142 if (src == top || _nc_is_abs_path(src)) {
143 if (strlen(src) + 1 <= limit) {
144 _nc_STRCPY(dst, src, limit);
148 if (strlen(top) + strlen(src) + 2 <= limit) {
149 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
155 static const char suffix[] = DBM_SUFFIX;
156 size_t have = strlen(dst);
157 size_t need = strlen(suffix);
158 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
159 if (have + need <= limit) {
160 _nc_STRCAT(dst, suffix, limit);
164 } else if (_nc_is_dir_path(dst)) {
173 * Make a database-root if it doesn't exist.
176 make_db_root(const char *path)
179 char fullpath[PATH_MAX];
181 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
185 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
187 } else if (_nc_db_close(capdbp) < 0) {
193 if ((rc = stat(path, &statbuf)) == -1) {
199 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
200 rc = -1; /* permission denied */
201 } else if (!(S_ISDIR(statbuf.st_mode))) {
202 rc = -1; /* not a directory */
210 * Set the write directory for compiled entries.
213 _nc_set_writedir(const char *dir)
215 const char *destination;
216 char actual[PATH_MAX];
219 #ifndef USE_ROOT_ENVIRON
220 && use_terminfo_vars()
223 dir = getenv("TERMINFO");
226 (void) _nc_tic_dir(dir);
228 destination = _nc_tic_dir(0);
229 if (make_db_root(destination) < 0) {
230 char *home = _nc_home_terminfo();
234 if (make_db_root(destination) < 0)
235 _nc_err_abort("%s: permission denied (errno %d)",
241 * Note: because of this code, this logic should be exercised
242 * *once only* per run.
245 make_db_path(actual, destination, sizeof(actual));
247 if (chdir(_nc_tic_dir(destination)) < 0
248 || getcwd(actual, sizeof(actual)) == 0)
249 _nc_err_abort("%s: not a directory", destination);
251 _nc_keep_tic_dir(strdup(actual));
255 * Save the compiled version of a description in the filesystem.
257 * make a copy of the name-list
258 * break it up into first-name and all-but-last-name
260 * write object information to first-name
262 * for each name in all-but-last-name
265 * Using 'time()' to obtain a reference for file timestamps is unreliable,
266 * e.g., with NFS, because the filesystem may have a different time
267 * reference. We check for pre-existence of links by latching the first
268 * timestamp from a file that we create.
270 * The _nc_warning() calls will report a correct line number only if
271 * _nc_curr_line is properly set before the write_entry() call.
275 _nc_write_entry(TERMTYPE2 *const tp)
279 char buffer[MAX_ENTRY_SIZE + 1];
280 unsigned limit = sizeof(buffer);
283 #else /* !USE_HASHED_DB */
286 char filename[PATH_MAX];
287 char linkname[PATH_MAX];
289 char symlinkname[PATH_MAX];
294 #endif /* USE_SYMLINKS */
296 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
299 static int call_count;
300 static time_t start_time; /* time at start of writes */
302 #endif /* USE_HASHED_DB */
304 char name_list[MAX_TERMINFO_LENGTH];
305 char *first_name, *other_names;
307 char *term_names = tp->term_names;
308 size_t name_size = strlen(term_names);
310 if (name_size == 0) {
311 _nc_syserr_abort("no terminal name found.");
312 } else if (name_size >= sizeof(name_list) - 1) {
313 _nc_syserr_abort("terminal name too long: %s", term_names);
316 _nc_STRCPY(name_list, term_names, sizeof(name_list));
317 DEBUG(7, ("Name list = '%s'", name_list));
319 first_name = name_list;
321 ptr = &name_list[name_size - 1];
322 other_names = ptr + 1;
324 while (ptr > name_list && *ptr != '|')
327 if (ptr != name_list) {
330 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
337 other_names = ptr + 1;
341 DEBUG(7, ("First name = '%s'", first_name));
342 DEBUG(7, ("Other names = '%s'", other_names));
344 _nc_set_type(first_name);
347 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
348 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
354 memset(&key, 0, sizeof(key));
355 key.data = term_names;
356 key.size = name_size;
358 memset(&data, 0, sizeof(data));
360 data.size = offset + 1;
362 _nc_db_put(capdb, &key, &data);
366 key.data = name_list;
367 key.size = strlen(name_list);
369 _nc_STRCPY(buffer + 1,
372 data.size = name_size + 1;
374 total_size += data.size;
376 _nc_db_put(capdb, &key, &data);
378 while (*other_names != '\0') {
380 assert(ptr < buffer + sizeof(buffer) - 1);
381 while (*other_names != '|' && *other_names != '\0')
384 if (*other_names != '\0')
385 *(other_names++) = '\0';
388 key.size = strlen(ptr);
390 total_size += data.size;
392 _nc_db_put(capdb, &key, &data);
396 #else /* !USE_HASHED_DB */
397 if (call_count++ == 0) {
401 if (strlen(first_name) >= limit2) {
402 _nc_warning("terminal name too long.");
403 saved = first_name[limit2];
404 first_name[limit2] = '\0';
407 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
408 LEAF_FMT "/%.*s", UChar(first_name[0]),
409 (int) (sizeof(filename) - (LEAF_LEN + 2)),
413 first_name[limit2] = saved;
416 * Has this primary name been written since the first call to
417 * write_entry()? If so, the newer write will step on the older,
420 if (start_time > 0 &&
421 stat(filename, &statbuf) >= 0
422 && statbuf.st_mtime >= start_time) {
423 #if HAVE_LINK && !USE_SYMLINKS
425 * If the file has more than one link, the reason for the previous
426 * write could be that the current primary name used to be an alias for
427 * the previous entry. In that case, unlink the file so that we will
428 * not modify the previous entry as we write this one.
430 if (statbuf.st_nlink > 1) {
431 _nc_warning("name redefined.");
434 _nc_warning("name multiply defined.");
437 _nc_warning("name multiply defined.");
441 check_writeable(first_name[0]);
442 write_file(filename, tp);
444 if (start_time == 0) {
445 if (stat(filename, &statbuf) == -1
446 || (start_time = statbuf.st_mtime) == 0) {
447 _nc_syserr_abort("error obtaining time from %s/%s",
448 _nc_tic_dir(0), filename);
451 while (*other_names != '\0') {
453 while (*other_names != '|' && *other_names != '\0')
456 if (*other_names != '\0')
457 *(other_names++) = '\0';
459 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
460 _nc_warning("terminal alias %s too long.", ptr);
463 if (strchr(ptr, '/') != 0) {
464 _nc_warning("cannot link alias %s.", ptr);
468 check_writeable(ptr[0]);
469 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
470 LEAF_FMT "/%.*s", ptr[0],
471 (int) sizeof(linkname) - (2 + LEAF_LEN), ptr);
473 if (strcmp(filename, linkname) == 0) {
474 _nc_warning("self-synonym ignored");
475 } else if (stat(linkname, &statbuf) >= 0 &&
476 statbuf.st_mtime < start_time) {
477 _nc_warning("alias %s multiply defined.", ptr);
478 } else if (_nc_access(linkname, W_OK) == 0)
483 #define MY_SIZE sizeof(symlinkname) - 1
484 if (first_name[0] == linkname[0]) {
485 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
487 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
488 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
490 symlinkname[MY_SIZE] = '\0';
491 #endif /* USE_SYMLINKS */
493 code = remove(linkname);
495 code = unlink(linkname);
497 if (code != 0 && errno == ENOENT)
500 if (symlink(symlinkname, linkname) < 0)
502 if (link(filename, linkname) < 0)
503 #endif /* USE_SYMLINKS */
506 * If there wasn't anything there, and we cannot
507 * link to the target because it is the same as the
508 * target, then the source must be on a filesystem
509 * that uses caseless filenames, such as Win32, etc.
511 if (code == 0 && errno == EEXIST)
512 _nc_warning("can't link %s to %s", filename, linkname);
513 else if (code == 0 && (errno == EPERM || errno == ENOENT))
514 write_file(linkname, tp);
516 #if MIXEDCASE_FILENAMES
517 _nc_syserr_abort("can't link %s to %s", filename, linkname);
519 _nc_warning("can't link %s to %s (errno=%d)", filename,
524 DEBUG(1, ("Linked %s", linkname));
527 #else /* just make copies */
528 write_file(linkname, tp);
529 #endif /* HAVE_LINK */
531 #endif /* USE_HASHED_DB */
535 fake_write(char *dst,
542 size_t have = (limit - *offset);
548 memcpy(dst + *offset, src, want);
549 *offset += (unsigned) want;
553 return (want / size);
556 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
558 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
559 #define HI(x) ((x) / 256)
560 #define LO(x) ((x) % 256)
561 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
562 (p)[1] = (unsigned char)HI(x)
564 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
567 compute_offsets(char **Strings, size_t strmax, short *offsets)
572 for (i = 0; i < strmax; i++) {
573 if (Strings[i] == ABSENT_STRING) {
575 } else if (Strings[i] == CANCELLED_STRING) {
578 offsets[i] = (short) nextfree;
579 nextfree += (int) strlen(Strings[i]) + 1;
580 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
581 _nc_visbuf(Strings[i]), (int) nextfree));
588 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
591 for (i = 0; i < count; i++) {
592 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
593 buf[2 * i] = buf[2 * i + 1] = 0377;
594 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
596 buf[2 * i + 1] = 0377;
598 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
599 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
605 #if NCURSES_EXT_NUMBERS
607 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
610 size_t size = SIZEOF_SHORT;
611 for (i = 0; i < count; i++) {
612 unsigned value = (unsigned) Numbers[i];
614 for (j = 0; j < size; ++j) {
615 *buf++ = value & 0xff;
623 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
626 size_t size = SIZEOF_INT2;
627 for (i = 0; i < count; i++) {
628 unsigned value = (unsigned) Numbers[i];
630 for (j = 0; j < size; ++j) {
631 *buf++ = value & 0xff;
639 #define even_boundary(value) \
640 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
644 extended_Booleans(TERMTYPE2 *tp)
649 for (i = 0; i < tp->ext_Booleans; ++i) {
650 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
657 extended_Numbers(TERMTYPE2 *tp)
662 for (i = 0; i < tp->ext_Numbers; ++i) {
663 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
670 extended_Strings(TERMTYPE2 *tp)
672 unsigned short result = 0;
675 for (i = 0; i < tp->ext_Strings; ++i) {
676 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
677 result = (unsigned short) (i + 1);
683 * _nc_align_termtype() will extend entries that are referenced in a use=
684 * clause - discard the unneeded data.
687 extended_object(TERMTYPE2 *tp)
691 if (_nc_user_definable) {
692 result = ((extended_Booleans(tp)
693 + extended_Numbers(tp)
694 + extended_Strings(tp)) != 0);
701 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
704 size_t namelen, boolmax, nummax, strmax, numlen;
708 short offsets[MAX_ENTRY_SIZE / 2];
709 unsigned char buf[MAX_ENTRY_SIZE];
710 unsigned last_bool = BOOLWRITE;
711 unsigned last_num = NUMWRITE;
712 unsigned last_str = STRWRITE;
713 #if NCURSES_EXT_NUMBERS
714 bool need_ints = FALSE;
715 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t) = convert_32bit;
717 #define convert_numbers convert_shorts
722 * Normally we limit the list of values to exclude the "obsolete"
723 * capabilities. However, if we are accepting extended names, add
724 * these as well, since they are used for supporting translation
727 if (_nc_user_definable) {
728 last_bool = BOOLCOUNT;
734 namelist = tp->term_names;
735 namelen = strlen(namelist) + 1;
738 for (i = 0; i < last_bool; i++) {
739 if (tp->Booleans[i] == TRUE) {
745 for (i = 0; i < last_num; i++) {
746 if (tp->Numbers[i] != ABSENT_NUMERIC) {
748 #if NCURSES_EXT_NUMBERS
749 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
757 for (i = 0; i < last_str; i++) {
758 if (tp->Strings[i] != ABSENT_STRING)
762 nextfree = compute_offsets(tp->Strings, strmax, offsets);
764 /* fill in the header */
765 #if NCURSES_EXT_NUMBERS
767 convert_numbers = convert_32bit;
768 LITTLE_ENDIAN(buf, MAGIC2);
770 convert_numbers = convert_16bit;
771 LITTLE_ENDIAN(buf, MAGIC);
774 LITTLE_ENDIAN(buf, MAGIC);
776 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
777 LITTLE_ENDIAN(buf + 4, boolmax);
778 LITTLE_ENDIAN(buf + 6, nummax);
779 LITTLE_ENDIAN(buf + 8, strmax);
780 LITTLE_ENDIAN(buf + 10, nextfree);
782 /* write out the header */
783 TRACE_OUT(("Header of %s @%d", namelist, *offset));
784 if (Write(buf, 12, 1) != 1
785 || Write(namelist, sizeof(char), namelen) != namelen) {
789 for (i = 0; i < boolmax; i++) {
790 if (tp->Booleans[i] == TRUE) {
796 if (Write(buf, sizeof(char), boolmax) != boolmax) {
800 if (even_boundary(namelen + boolmax)) {
804 TRACE_OUT(("Numerics begin at %04x", *offset));
807 numlen = convert_numbers(buf, tp->Numbers, nummax);
808 if (Write(buf, numlen, nummax) != nummax) {
812 TRACE_OUT(("String offsets begin at %04x", *offset));
814 /* the string offsets */
815 convert_shorts(buf, offsets, strmax);
816 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
820 TRACE_OUT(("String table begins at %04x", *offset));
823 for (i = 0; i < strmax; i++) {
824 if (VALID_STRING(tp->Strings[i])) {
825 if (!WRITE_STRING(tp->Strings[i])) {
832 if (extended_object(tp)) {
833 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
834 unsigned ext_usage = ext_total;
836 if (even_boundary(nextfree)) {
840 nextfree = compute_offsets(tp->Strings + STRCOUNT,
841 (size_t) tp->ext_Strings,
843 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
845 if (tp->ext_Strings >= SIZEOF(offsets)) {
849 nextfree += compute_offsets(tp->ext_Names,
851 offsets + tp->ext_Strings);
852 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
853 strmax = tp->ext_Strings + ext_total;
854 for (i = 0; i < tp->ext_Strings; ++i) {
855 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
859 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
862 * Write the extended header
864 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
865 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
866 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
867 LITTLE_ENDIAN(buf + 6, ext_usage);
868 LITTLE_ENDIAN(buf + 8, nextfree);
869 TRACE_OUT(("WRITE extended-header @%d", *offset));
870 if (Write(buf, 10, 1) != 1) {
874 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
876 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
877 tp->ext_Booleans) != tp->ext_Booleans) {
881 if (even_boundary(tp->ext_Booleans)) {
885 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
886 if (tp->ext_Numbers) {
887 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
888 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) {
894 * Convert the offsets for the ext_Strings and ext_Names tables,
897 convert_shorts(buf, offsets, strmax);
898 TRACE_OUT(("WRITE offsets @%d", *offset));
899 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
904 * Write the string table after the offset tables so we do not
905 * have to do anything about alignment.
907 for (i = 0; i < tp->ext_Strings; i++) {
908 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
909 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
910 _nc_visbuf(tp->Strings[i + STRCOUNT])));
911 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) {
918 * Write the extended names
920 for (i = 0; i < ext_total; i++) {
921 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
922 if (!WRITE_STRING(tp->ext_Names[i])) {
928 #endif /* NCURSES_XNAMES */
932 total_size = total_size + (int) (*offset + 1);
937 * Returns the total number of entries written by this process
940 _nc_tic_written(void)
942 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
943 total_written, total_parts, total_size));
944 return total_written;