1 /****************************************************************************
2 * Copyright (c) 1998-2015,2017 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.101 2017/11/25 19:56:06 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 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
72 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
74 DEBUG(1, ("Created %s", filename));
76 if (_nc_write_object(tp, buffer, &offset, limit) == ERR
77 || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
78 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
85 * Check for access rights to destination directories
86 * Create any directories which don't exist.
88 * Note: there's no reason to return the result of make_db_root(), since
89 * this function is called only in instances where that has to succeed.
92 check_writeable(int code)
94 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
95 static bool verified[sizeof(dirnames)];
97 char dir[sizeof(LEAF_FMT)];
100 if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
101 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
103 if (verified[s - dirnames])
106 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
107 if (make_db_root(dir) < 0) {
108 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
111 verified[s - dirnames] = TRUE;
113 #endif /* !USE_HASHED_DB */
116 make_db_path(char *dst, const char *src, size_t limit)
119 const char *top = _nc_tic_dir(0);
121 if (src == top || _nc_is_abs_path(src)) {
122 if (strlen(src) + 1 <= limit) {
123 _nc_STRCPY(dst, src, limit);
127 if (strlen(top) + strlen(src) + 2 <= limit) {
128 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
134 static const char suffix[] = DBM_SUFFIX;
135 size_t have = strlen(dst);
136 size_t need = strlen(suffix);
137 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
138 if (have + need <= limit) {
139 _nc_STRCAT(dst, suffix, limit);
143 } else if (_nc_is_dir_path(dst)) {
152 * Make a database-root if it doesn't exist.
155 make_db_root(const char *path)
158 char fullpath[PATH_MAX];
160 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
164 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
166 } else if (_nc_db_close(capdbp) < 0) {
172 if ((rc = stat(path, &statbuf)) < 0) {
174 #if !defined(__MINGW32__)
178 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
179 rc = -1; /* permission denied */
180 } else if (!(S_ISDIR(statbuf.st_mode))) {
181 rc = -1; /* not a directory */
189 * Set the write directory for compiled entries.
192 _nc_set_writedir(const char *dir)
194 const char *destination;
195 char actual[PATH_MAX];
198 #ifndef USE_ROOT_ENVIRON
199 && use_terminfo_vars()
202 dir = getenv("TERMINFO");
205 (void) _nc_tic_dir(dir);
207 destination = _nc_tic_dir(0);
208 if (make_db_root(destination) < 0) {
209 char *home = _nc_home_terminfo();
213 if (make_db_root(destination) < 0)
214 _nc_err_abort("%s: permission denied (errno %d)",
220 * Note: because of this code, this logic should be exercised
221 * *once only* per run.
224 make_db_path(actual, destination, sizeof(actual));
226 if (chdir(_nc_tic_dir(destination)) < 0
227 || getcwd(actual, sizeof(actual)) == 0)
228 _nc_err_abort("%s: not a directory", destination);
230 _nc_keep_tic_dir(strdup(actual));
234 * Save the compiled version of a description in the filesystem.
236 * make a copy of the name-list
237 * break it up into first-name and all-but-last-name
239 * write object information to first-name
241 * for each name in all-but-last-name
244 * Using 'time()' to obtain a reference for file timestamps is unreliable,
245 * e.g., with NFS, because the filesystem may have a different time
246 * reference. We check for pre-existence of links by latching the first
247 * timestamp from a file that we create.
249 * The _nc_warning() calls will report a correct line number only if
250 * _nc_curr_line is properly set before the write_entry() call.
254 _nc_write_entry(TERMTYPE2 *const tp)
258 char buffer[MAX_ENTRY_SIZE + 1];
259 unsigned limit = sizeof(buffer);
262 #else /* !USE_HASHED_DB */
265 char filename[PATH_MAX];
266 char linkname[PATH_MAX];
268 char symlinkname[PATH_MAX];
273 #endif /* USE_SYMLINKS */
275 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
278 static int call_count;
279 static time_t start_time; /* time at start of writes */
281 #endif /* USE_HASHED_DB */
283 char name_list[MAX_TERMINFO_LENGTH];
284 char *first_name, *other_names;
286 char *term_names = tp->term_names;
287 size_t name_size = strlen(term_names);
289 if (name_size == 0) {
290 _nc_syserr_abort("no terminal name found.");
291 } else if (name_size >= sizeof(name_list) - 1) {
292 _nc_syserr_abort("terminal name too long: %s", term_names);
295 _nc_STRCPY(name_list, term_names, sizeof(name_list));
296 DEBUG(7, ("Name list = '%s'", name_list));
298 first_name = name_list;
300 ptr = &name_list[name_size - 1];
301 other_names = ptr + 1;
303 while (ptr > name_list && *ptr != '|')
306 if (ptr != name_list) {
309 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
316 other_names = ptr + 1;
320 DEBUG(7, ("First name = '%s'", first_name));
321 DEBUG(7, ("Other names = '%s'", other_names));
323 _nc_set_type(first_name);
326 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
327 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
333 memset(&key, 0, sizeof(key));
334 key.data = term_names;
335 key.size = name_size;
337 memset(&data, 0, sizeof(data));
339 data.size = offset + 1;
341 _nc_db_put(capdb, &key, &data);
345 key.data = name_list;
346 key.size = strlen(name_list);
348 _nc_STRCPY(buffer + 1,
351 data.size = name_size + 1;
353 total_size += data.size;
355 _nc_db_put(capdb, &key, &data);
357 while (*other_names != '\0') {
359 assert(ptr < buffer + sizeof(buffer) - 1);
360 while (*other_names != '|' && *other_names != '\0')
363 if (*other_names != '\0')
364 *(other_names++) = '\0';
367 key.size = strlen(ptr);
369 total_size += data.size;
371 _nc_db_put(capdb, &key, &data);
375 #else /* !USE_HASHED_DB */
376 if (call_count++ == 0) {
380 if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN)) {
381 _nc_warning("terminal name too long.");
382 saved = first_name[limit2];
383 first_name[limit2] = '\0';
386 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
387 LEAF_FMT "/%s", first_name[0], first_name);
390 first_name[limit2] = saved;
393 * Has this primary name been written since the first call to
394 * write_entry()? If so, the newer write will step on the older,
397 if (start_time > 0 &&
398 stat(filename, &statbuf) >= 0
399 && statbuf.st_mtime >= start_time) {
400 #if HAVE_LINK && !USE_SYMLINKS
402 * If the file has more than one link, the reason for the previous
403 * write could be that the current primary name used to be an alias for
404 * the previous entry. In that case, unlink the file so that we will
405 * not modify the previous entry as we write this one.
407 if (statbuf.st_nlink > 1) {
408 _nc_warning("name redefined.");
411 _nc_warning("name multiply defined.");
414 _nc_warning("name multiply defined.");
418 check_writeable(first_name[0]);
419 write_file(filename, tp);
421 if (start_time == 0) {
422 if (stat(filename, &statbuf) < 0
423 || (start_time = statbuf.st_mtime) == 0) {
424 _nc_syserr_abort("error obtaining time from %s/%s",
425 _nc_tic_dir(0), filename);
428 while (*other_names != '\0') {
430 while (*other_names != '|' && *other_names != '\0')
433 if (*other_names != '\0')
434 *(other_names++) = '\0';
436 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
437 _nc_warning("terminal alias %s too long.", ptr);
440 if (strchr(ptr, '/') != 0) {
441 _nc_warning("cannot link alias %s.", ptr);
445 check_writeable(ptr[0]);
446 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
447 LEAF_FMT "/%s", ptr[0], ptr);
449 if (strcmp(filename, linkname) == 0) {
450 _nc_warning("self-synonym ignored");
451 } else if (stat(linkname, &statbuf) >= 0 &&
452 statbuf.st_mtime < start_time) {
453 _nc_warning("alias %s multiply defined.", ptr);
454 } else if (_nc_access(linkname, W_OK) == 0)
459 #define MY_SIZE sizeof(symlinkname) - 1
460 if (first_name[0] == linkname[0]) {
461 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
463 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
464 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
466 symlinkname[MY_SIZE] = '\0';
467 #endif /* USE_SYMLINKS */
469 code = remove(linkname);
471 code = unlink(linkname);
473 if (code != 0 && errno == ENOENT)
476 if (symlink(symlinkname, linkname) < 0)
478 if (link(filename, linkname) < 0)
479 #endif /* USE_SYMLINKS */
482 * If there wasn't anything there, and we cannot
483 * link to the target because it is the same as the
484 * target, then the source must be on a filesystem
485 * that uses caseless filenames, such as Win32, etc.
487 if (code == 0 && errno == EEXIST)
488 _nc_warning("can't link %s to %s", filename, linkname);
489 else if (code == 0 && (errno == EPERM || errno == ENOENT))
490 write_file(linkname, tp);
492 #if MIXEDCASE_FILENAMES
493 _nc_syserr_abort("can't link %s to %s", filename, linkname);
495 _nc_warning("can't link %s to %s (errno=%d)", filename,
500 DEBUG(1, ("Linked %s", linkname));
503 #else /* just make copies */
504 write_file(linkname, tp);
505 #endif /* HAVE_LINK */
507 #endif /* USE_HASHED_DB */
511 fake_write(char *dst,
518 size_t have = (limit - *offset);
524 memcpy(dst + *offset, src, want);
525 *offset += (unsigned) want;
529 return (want / size);
532 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
534 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
535 #define HI(x) ((x) / 256)
536 #define LO(x) ((x) % 256)
537 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
538 (p)[1] = (unsigned char)HI(x)
540 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
543 compute_offsets(char **Strings, size_t strmax, short *offsets)
548 for (i = 0; i < strmax; i++) {
549 if (Strings[i] == ABSENT_STRING) {
551 } else if (Strings[i] == CANCELLED_STRING) {
554 offsets[i] = (short) nextfree;
555 nextfree += (int) strlen(Strings[i]) + 1;
556 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
557 _nc_visbuf(Strings[i]), (int) nextfree));
564 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
567 for (i = 0; i < count; i++) {
568 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
569 buf[2 * i] = buf[2 * i + 1] = 0377;
570 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
572 buf[2 * i + 1] = 0377;
574 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
575 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
581 #if NCURSES_EXT_NUMBERS
583 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
586 size_t size = SIZEOF_SHORT;
587 for (i = 0; i < count; i++) {
588 unsigned value = (unsigned) Numbers[i];
590 for (j = 0; j < size; ++j) {
591 *buf++ = value & 0xff;
599 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
602 size_t size = SIZEOF_INT2;
603 for (i = 0; i < count; i++) {
604 unsigned value = (unsigned) Numbers[i];
606 for (j = 0; j < size; ++j) {
607 *buf++ = value & 0xff;
615 #define even_boundary(value) \
616 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
620 extended_Booleans(TERMTYPE2 *tp)
625 for (i = 0; i < tp->ext_Booleans; ++i) {
626 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
633 extended_Numbers(TERMTYPE2 *tp)
638 for (i = 0; i < tp->ext_Numbers; ++i) {
639 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
646 extended_Strings(TERMTYPE2 *tp)
648 unsigned short result = 0;
651 for (i = 0; i < tp->ext_Strings; ++i) {
652 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
653 result = (unsigned short) (i + 1);
659 * _nc_align_termtype() will extend entries that are referenced in a use=
660 * clause - discard the unneeded data.
663 extended_object(TERMTYPE2 *tp)
667 if (_nc_user_definable) {
668 result = ((extended_Booleans(tp)
669 + extended_Numbers(tp)
670 + extended_Strings(tp)) != 0);
677 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
680 size_t namelen, boolmax, nummax, strmax, numlen;
684 short offsets[MAX_ENTRY_SIZE / 2];
685 unsigned char buf[MAX_ENTRY_SIZE];
686 unsigned last_bool = BOOLWRITE;
687 unsigned last_num = NUMWRITE;
688 unsigned last_str = STRWRITE;
689 #if NCURSES_EXT_NUMBERS
690 bool need_ints = FALSE;
691 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t) = convert_32bit;
693 #define convert_numbers convert_shorts
698 * Normally we limit the list of values to exclude the "obsolete"
699 * capabilities. However, if we are accepting extended names, add
700 * these as well, since they are used for supporting translation
703 if (_nc_user_definable) {
704 last_bool = BOOLCOUNT;
710 namelist = tp->term_names;
711 namelen = strlen(namelist) + 1;
714 for (i = 0; i < last_bool; i++) {
715 if (tp->Booleans[i] == TRUE) {
721 for (i = 0; i < last_num; i++) {
722 if (tp->Numbers[i] != ABSENT_NUMERIC) {
724 #if NCURSES_EXT_NUMBERS
725 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
733 for (i = 0; i < last_str; i++) {
734 if (tp->Strings[i] != ABSENT_STRING)
738 nextfree = compute_offsets(tp->Strings, strmax, offsets);
740 /* fill in the header */
741 #if NCURSES_EXT_NUMBERS
743 convert_numbers = convert_32bit;
744 LITTLE_ENDIAN(buf, MAGIC2);
746 convert_numbers = convert_16bit;
747 LITTLE_ENDIAN(buf, MAGIC);
750 LITTLE_ENDIAN(buf, MAGIC);
752 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
753 LITTLE_ENDIAN(buf + 4, boolmax);
754 LITTLE_ENDIAN(buf + 6, nummax);
755 LITTLE_ENDIAN(buf + 8, strmax);
756 LITTLE_ENDIAN(buf + 10, nextfree);
758 /* write out the header */
759 TRACE_OUT(("Header of %s @%d", namelist, *offset));
760 if (Write(buf, 12, 1) != 1
761 || Write(namelist, sizeof(char), namelen) != namelen)
764 for (i = 0; i < boolmax; i++)
765 if (tp->Booleans[i] == TRUE)
769 if (Write(buf, sizeof(char), boolmax) != boolmax)
772 if (even_boundary(namelen + boolmax))
775 TRACE_OUT(("Numerics begin at %04x", *offset));
778 numlen = convert_numbers(buf, tp->Numbers, nummax);
779 if (Write(buf, numlen, nummax) != nummax)
782 TRACE_OUT(("String offsets begin at %04x", *offset));
784 /* the string offsets */
785 convert_shorts(buf, offsets, strmax);
786 if (Write(buf, SIZEOF_SHORT, strmax) != strmax)
789 TRACE_OUT(("String table begins at %04x", *offset));
792 for (i = 0; i < strmax; i++)
793 if (VALID_STRING(tp->Strings[i]))
794 if (!WRITE_STRING(tp->Strings[i]))
798 if (extended_object(tp)) {
799 unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
801 if (even_boundary(nextfree))
804 nextfree = compute_offsets(tp->Strings + STRCOUNT,
805 (size_t) tp->ext_Strings,
807 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
809 if (tp->ext_Strings >= SIZEOF(offsets))
812 nextfree += compute_offsets(tp->ext_Names,
814 offsets + tp->ext_Strings);
815 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
816 strmax = tp->ext_Strings + extcnt;
819 * Write the extended header
821 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
822 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
823 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
824 LITTLE_ENDIAN(buf + 6, strmax);
825 LITTLE_ENDIAN(buf + 8, nextfree);
826 TRACE_OUT(("WRITE extended-header @%d", *offset));
827 if (Write(buf, 10, 1) != 1)
830 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
832 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
833 tp->ext_Booleans) != tp->ext_Booleans)
836 if (even_boundary(tp->ext_Booleans))
839 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
840 if (tp->ext_Numbers) {
841 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
842 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers)
847 * Convert the offsets for the ext_Strings and ext_Names tables,
850 convert_shorts(buf, offsets, strmax);
851 TRACE_OUT(("WRITE offsets @%d", *offset));
852 if (Write(buf, SIZEOF_SHORT, strmax) != strmax)
856 * Write the string table after the offset tables so we do not
857 * have to do anything about alignment.
859 for (i = 0; i < tp->ext_Strings; i++) {
860 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
861 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
862 _nc_visbuf(tp->Strings[i + STRCOUNT])));
863 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
869 * Write the extended names
871 for (i = 0; i < extcnt; i++) {
872 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
873 if (!WRITE_STRING(tp->ext_Names[i]))
878 #endif /* NCURSES_XNAMES */
882 total_size = total_size + (int) (*offset + 1);
887 * Returns the total number of entries written by this process
890 _nc_tic_written(void)
892 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
893 total_written, total_parts, total_size));
894 return total_written;