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.111 2019/01/20 02:54:14 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, "wb")
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], ptr);
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)
481 #define MY_SIZE sizeof(symlinkname) - 1
482 if (first_name[0] == linkname[0]) {
483 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
485 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
486 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
488 symlinkname[MY_SIZE] = '\0';
489 #endif /* USE_SYMLINKS */
491 code = remove(linkname);
493 code = unlink(linkname);
495 if (code != 0 && errno == ENOENT)
498 if (symlink(symlinkname, linkname) < 0)
500 if (link(filename, linkname) < 0)
501 #endif /* USE_SYMLINKS */
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.
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);
514 #if MIXEDCASE_FILENAMES
515 _nc_syserr_abort("can't link %s to %s", filename, linkname);
517 _nc_warning("can't link %s to %s (errno=%d)", filename,
522 DEBUG(1, ("Linked %s", linkname));
525 #else /* just make copies */
526 write_file(linkname, tp);
527 #endif /* HAVE_LINK */
529 #endif /* USE_HASHED_DB */
533 fake_write(char *dst,
540 size_t have = (limit - *offset);
546 memcpy(dst + *offset, src, want);
547 *offset += (unsigned) want;
551 return (want / size);
554 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
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)
562 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
565 compute_offsets(char **Strings, size_t strmax, short *offsets)
570 for (i = 0; i < strmax; i++) {
571 if (Strings[i] == ABSENT_STRING) {
573 } else if (Strings[i] == CANCELLED_STRING) {
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));
586 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
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 */
594 buf[2 * i + 1] = 0377;
596 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
597 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
603 #if NCURSES_EXT_NUMBERS
605 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
608 size_t size = SIZEOF_SHORT;
609 for (i = 0; i < count; i++) {
610 unsigned value = (unsigned) Numbers[i];
612 for (j = 0; j < size; ++j) {
613 *buf++ = value & 0xff;
621 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
624 size_t size = SIZEOF_INT2;
625 for (i = 0; i < count; i++) {
626 unsigned value = (unsigned) Numbers[i];
628 for (j = 0; j < size; ++j) {
629 *buf++ = value & 0xff;
637 #define even_boundary(value) \
638 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
642 extended_Booleans(TERMTYPE2 *tp)
647 for (i = 0; i < tp->ext_Booleans; ++i) {
648 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
655 extended_Numbers(TERMTYPE2 *tp)
660 for (i = 0; i < tp->ext_Numbers; ++i) {
661 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
668 extended_Strings(TERMTYPE2 *tp)
670 unsigned short result = 0;
673 for (i = 0; i < tp->ext_Strings; ++i) {
674 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
675 result = (unsigned short) (i + 1);
681 * _nc_align_termtype() will extend entries that are referenced in a use=
682 * clause - discard the unneeded data.
685 extended_object(TERMTYPE2 *tp)
689 if (_nc_user_definable) {
690 result = ((extended_Booleans(tp)
691 + extended_Numbers(tp)
692 + extended_Strings(tp)) != 0);
699 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
702 size_t namelen, boolmax, nummax, strmax, numlen;
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;
715 #define convert_numbers convert_shorts
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
725 if (_nc_user_definable) {
726 last_bool = BOOLCOUNT;
732 namelist = tp->term_names;
733 namelen = strlen(namelist) + 1;
736 for (i = 0; i < last_bool; i++) {
737 if (tp->Booleans[i] == TRUE) {
743 for (i = 0; i < last_num; i++) {
744 if (tp->Numbers[i] != ABSENT_NUMERIC) {
746 #if NCURSES_EXT_NUMBERS
747 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
755 for (i = 0; i < last_str; i++) {
756 if (tp->Strings[i] != ABSENT_STRING)
760 nextfree = compute_offsets(tp->Strings, strmax, offsets);
762 /* fill in the header */
763 #if NCURSES_EXT_NUMBERS
765 convert_numbers = convert_32bit;
766 LITTLE_ENDIAN(buf, MAGIC2);
768 convert_numbers = convert_16bit;
769 LITTLE_ENDIAN(buf, MAGIC);
772 LITTLE_ENDIAN(buf, MAGIC);
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);
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) {
787 for (i = 0; i < boolmax; i++) {
788 if (tp->Booleans[i] == TRUE) {
794 if (Write(buf, sizeof(char), boolmax) != boolmax) {
798 if (even_boundary(namelen + boolmax)) {
802 TRACE_OUT(("Numerics begin at %04x", *offset));
805 numlen = convert_numbers(buf, tp->Numbers, nummax);
806 if (Write(buf, numlen, nummax) != nummax) {
810 TRACE_OUT(("String offsets begin at %04x", *offset));
812 /* the string offsets */
813 convert_shorts(buf, offsets, strmax);
814 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
818 TRACE_OUT(("String table begins at %04x", *offset));
821 for (i = 0; i < strmax; i++) {
822 if (VALID_STRING(tp->Strings[i])) {
823 if (!WRITE_STRING(tp->Strings[i])) {
830 if (extended_object(tp)) {
831 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
832 unsigned ext_usage = ext_total;
834 if (even_boundary(nextfree)) {
838 nextfree = compute_offsets(tp->Strings + STRCOUNT,
839 (size_t) tp->ext_Strings,
841 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
843 if (tp->ext_Strings >= SIZEOF(offsets)) {
847 nextfree += compute_offsets(tp->ext_Names,
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])) {
857 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
860 * Write the extended header
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) {
872 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
874 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
875 tp->ext_Booleans) != tp->ext_Booleans) {
879 if (even_boundary(tp->ext_Booleans)) {
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) {
892 * Convert the offsets for the ext_Strings and ext_Names tables,
895 convert_shorts(buf, offsets, strmax);
896 TRACE_OUT(("WRITE offsets @%d", *offset));
897 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
902 * Write the string table after the offset tables so we do not
903 * have to do anything about alignment.
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])) {
916 * Write the extended names
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])) {
926 #endif /* NCURSES_XNAMES */
930 total_size = total_size + (int) (*offset + 1);
935 * Returns the total number of entries written by this process
938 _nc_tic_written(void)
940 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
941 total_written, total_parts, total_size));
942 return total_written;