1 /****************************************************************************
2 * Copyright (c) 1998-2014,2015 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)
47 #define TRACE_OUT(p) /*nothing */
50 MODULE_ID("$Id: write_entry.c,v 1.93 2015/09/05 21:24:29 tom Exp $")
52 static int total_written;
54 static int make_db_root(const char *);
58 write_file(char *filename, TERMTYPE *tp)
60 char buffer[MAX_ENTRY_SIZE];
61 unsigned limit = sizeof(buffer);
64 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
67 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
69 DEBUG(1, ("Created %s", filename));
71 if (_nc_write_object(tp, buffer, &offset, limit) == ERR
72 || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
73 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
80 * Check for access rights to destination directories
81 * Create any directories which don't exist.
83 * Note: there's no reason to return the result of make_db_root(), since
84 * this function is called only in instances where that has to succeed.
87 check_writeable(int code)
89 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
90 static bool verified[sizeof(dirnames)];
92 char dir[sizeof(LEAF_FMT)];
95 if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
96 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
98 if (verified[s - dirnames])
101 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
102 if (make_db_root(dir) < 0) {
103 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
106 verified[s - dirnames] = TRUE;
108 #endif /* !USE_HASHED_DB */
111 make_db_path(char *dst, const char *src, size_t limit)
114 const char *top = _nc_tic_dir(0);
116 if (src == top || _nc_is_abs_path(src)) {
117 if (strlen(src) + 1 <= limit) {
118 _nc_STRCPY(dst, src, limit);
122 if (strlen(top) + strlen(src) + 2 <= limit) {
123 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
129 static const char suffix[] = DBM_SUFFIX;
130 size_t have = strlen(dst);
131 size_t need = strlen(suffix);
132 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
133 if (have + need <= limit) {
134 _nc_STRCAT(dst, suffix, limit);
138 } else if (_nc_is_dir_path(dst)) {
147 * Make a database-root if it doesn't exist.
150 make_db_root(const char *path)
153 char fullpath[PATH_MAX];
155 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
159 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
161 } else if (_nc_db_close(capdbp) < 0) {
167 if ((rc = stat(path, &statbuf)) < 0) {
169 #if !defined(__MINGW32__)
173 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
174 rc = -1; /* permission denied */
175 } else if (!(S_ISDIR(statbuf.st_mode))) {
176 rc = -1; /* not a directory */
184 * Set the write directory for compiled entries.
187 _nc_set_writedir(const char *dir)
189 const char *destination;
190 char actual[PATH_MAX];
193 #ifndef USE_ROOT_ENVIRON
194 && use_terminfo_vars()
197 dir = getenv("TERMINFO");
200 (void) _nc_tic_dir(dir);
202 destination = _nc_tic_dir(0);
203 if (make_db_root(destination) < 0) {
204 char *home = _nc_home_terminfo();
208 if (make_db_root(destination) < 0)
209 _nc_err_abort("%s: permission denied (errno %d)",
215 * Note: because of this code, this logic should be exercised
216 * *once only* per run.
219 make_db_path(actual, destination, sizeof(actual));
221 if (chdir(_nc_tic_dir(destination)) < 0
222 || getcwd(actual, sizeof(actual)) == 0)
223 _nc_err_abort("%s: not a directory", destination);
225 _nc_keep_tic_dir(strdup(actual));
229 * Save the compiled version of a description in the filesystem.
231 * make a copy of the name-list
232 * break it up into first-name and all-but-last-name
234 * write object information to first-name
236 * for each name in all-but-last-name
239 * Using 'time()' to obtain a reference for file timestamps is unreliable,
240 * e.g., with NFS, because the filesystem may have a different time
241 * reference. We check for pre-existence of links by latching the first
242 * timestamp from a file that we create.
244 * The _nc_warning() calls will report a correct line number only if
245 * _nc_curr_line is properly set before the write_entry() call.
249 _nc_write_entry(TERMTYPE *const tp)
253 char buffer[MAX_ENTRY_SIZE + 1];
254 unsigned limit = sizeof(buffer);
257 #else /* !USE_HASHED_DB */
260 char filename[PATH_MAX];
261 char linkname[PATH_MAX];
263 char symlinkname[PATH_MAX];
268 #endif /* USE_SYMLINKS */
270 static int call_count;
271 static time_t start_time; /* time at start of writes */
273 #endif /* USE_HASHED_DB */
275 char name_list[MAX_TERMINFO_LENGTH];
276 char *first_name, *other_names;
278 char *term_names = tp->term_names;
279 size_t name_size = strlen(term_names);
281 if (name_size == 0) {
282 _nc_syserr_abort("no terminal name found.");
283 } else if (name_size >= sizeof(name_list) - 1) {
284 _nc_syserr_abort("terminal name too long: %s", term_names);
287 _nc_STRCPY(name_list, term_names, sizeof(name_list));
288 DEBUG(7, ("Name list = '%s'", name_list));
290 first_name = name_list;
292 ptr = &name_list[name_size - 1];
293 other_names = ptr + 1;
295 while (ptr > name_list && *ptr != '|')
298 if (ptr != name_list) {
301 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
308 other_names = ptr + 1;
312 DEBUG(7, ("First name = '%s'", first_name));
313 DEBUG(7, ("Other names = '%s'", other_names));
315 _nc_set_type(first_name);
318 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
319 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
325 memset(&key, 0, sizeof(key));
326 key.data = term_names;
327 key.size = name_size;
329 memset(&data, 0, sizeof(data));
331 data.size = offset + 1;
333 _nc_db_put(capdb, &key, &data);
337 key.data = name_list;
338 key.size = strlen(name_list);
340 _nc_STRCPY(buffer + 1,
343 data.size = name_size + 1;
345 _nc_db_put(capdb, &key, &data);
347 while (*other_names != '\0') {
349 assert(ptr < buffer + sizeof(buffer) - 1);
350 while (*other_names != '|' && *other_names != '\0')
353 if (*other_names != '\0')
354 *(other_names++) = '\0';
357 key.size = strlen(ptr);
359 _nc_db_put(capdb, &key, &data);
363 #else /* !USE_HASHED_DB */
364 if (call_count++ == 0) {
368 if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
369 _nc_warning("terminal name too long.");
371 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
372 LEAF_FMT "/%s", first_name[0], first_name);
375 * Has this primary name been written since the first call to
376 * write_entry()? If so, the newer write will step on the older,
379 if (start_time > 0 &&
380 stat(filename, &statbuf) >= 0
381 && statbuf.st_mtime >= start_time) {
382 #if HAVE_LINK && !USE_SYMLINKS
384 * If the file has more than one link, the reason for the previous
385 * write could be that the current primary name used to be an alias for
386 * the previous entry. In that case, unlink the file so that we will
387 * not modify the previous entry as we write this one.
389 if (statbuf.st_nlink > 1) {
390 _nc_warning("name redefined.");
393 _nc_warning("name multiply defined.");
396 _nc_warning("name multiply defined.");
400 check_writeable(first_name[0]);
401 write_file(filename, tp);
403 if (start_time == 0) {
404 if (stat(filename, &statbuf) < 0
405 || (start_time = statbuf.st_mtime) == 0) {
406 _nc_syserr_abort("error obtaining time from %s/%s",
407 _nc_tic_dir(0), filename);
410 while (*other_names != '\0') {
412 while (*other_names != '|' && *other_names != '\0')
415 if (*other_names != '\0')
416 *(other_names++) = '\0';
418 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
419 _nc_warning("terminal alias %s too long.", ptr);
422 if (strchr(ptr, '/') != 0) {
423 _nc_warning("cannot link alias %s.", ptr);
427 check_writeable(ptr[0]);
428 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
429 LEAF_FMT "/%s", ptr[0], ptr);
431 if (strcmp(filename, linkname) == 0) {
432 _nc_warning("self-synonym ignored");
433 } else if (stat(linkname, &statbuf) >= 0 &&
434 statbuf.st_mtime < start_time) {
435 _nc_warning("alias %s multiply defined.", ptr);
436 } else if (_nc_access(linkname, W_OK) == 0)
441 if (first_name[0] == linkname[0])
442 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
444 _nc_STRCPY(symlinkname, "../", sizeof(suymlinkname));
445 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
447 symlinkname[sizeof(symlinkname) - 1] = '\0';
448 #endif /* USE_SYMLINKS */
450 code = remove(linkname);
452 code = unlink(linkname);
454 if (code != 0 && errno == ENOENT)
457 if (symlink(symlinkname, linkname) < 0)
459 if (link(filename, linkname) < 0)
460 #endif /* USE_SYMLINKS */
463 * If there wasn't anything there, and we cannot
464 * link to the target because it is the same as the
465 * target, then the source must be on a filesystem
466 * that uses caseless filenames, such as Win32, etc.
468 if (code == 0 && errno == EEXIST)
469 _nc_warning("can't link %s to %s", filename, linkname);
470 else if (code == 0 && (errno == EPERM || errno == ENOENT))
471 write_file(linkname, tp);
473 #if MIXEDCASE_FILENAMES
474 _nc_syserr_abort("can't link %s to %s", filename, linkname);
476 _nc_warning("can't link %s to %s (errno=%d)", filename,
481 DEBUG(1, ("Linked %s", linkname));
484 #else /* just make copies */
485 write_file(linkname, tp);
486 #endif /* HAVE_LINK */
488 #endif /* USE_HASHED_DB */
492 fake_write(char *dst,
499 size_t have = (limit - *offset);
505 memcpy(dst + *offset, src, want);
506 *offset += (unsigned) want;
510 return (want / size);
513 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
515 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
516 #define HI(x) ((x) / 256)
517 #define LO(x) ((x) % 256)
518 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
519 (p)[1] = (unsigned char)HI(x)
521 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
524 compute_offsets(char **Strings, size_t strmax, short *offsets)
529 for (i = 0; i < strmax; i++) {
530 if (Strings[i] == ABSENT_STRING) {
532 } else if (Strings[i] == CANCELLED_STRING) {
535 offsets[i] = (short) nextfree;
536 nextfree += (int) strlen(Strings[i]) + 1;
537 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
538 _nc_visbuf(Strings[i]), (int) nextfree));
545 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
548 for (i = 0; i < count; i++) {
549 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
550 buf[2 * i] = buf[2 * i + 1] = 0377;
551 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
553 buf[2 * i + 1] = 0377;
555 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
556 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
561 #define even_boundary(value) \
562 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
566 extended_Booleans(TERMTYPE *tp)
571 for (i = 0; i < tp->ext_Booleans; ++i) {
572 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
579 extended_Numbers(TERMTYPE *tp)
584 for (i = 0; i < tp->ext_Numbers; ++i) {
585 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
592 extended_Strings(TERMTYPE *tp)
594 unsigned short result = 0;
597 for (i = 0; i < tp->ext_Strings; ++i) {
598 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
599 result = (unsigned short) (i + 1);
605 * _nc_align_termtype() will extend entries that are referenced in a use=
606 * clause - discard the unneeded data.
609 extended_object(TERMTYPE *tp)
613 if (_nc_user_definable) {
614 result = ((extended_Booleans(tp)
615 + extended_Numbers(tp)
616 + extended_Strings(tp)) != 0);
623 _nc_write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
626 size_t namelen, boolmax, nummax, strmax;
630 short offsets[MAX_ENTRY_SIZE / 2];
631 unsigned char buf[MAX_ENTRY_SIZE];
632 unsigned last_bool = BOOLWRITE;
633 unsigned last_num = NUMWRITE;
634 unsigned last_str = STRWRITE;
638 * Normally we limit the list of values to exclude the "obsolete"
639 * capabilities. However, if we are accepting extended names, add
640 * these as well, since they are used for supporting translation
643 if (_nc_user_definable) {
644 last_bool = BOOLCOUNT;
650 namelist = tp->term_names;
651 namelen = strlen(namelist) + 1;
654 for (i = 0; i < last_bool; i++) {
655 if (tp->Booleans[i] == TRUE)
660 for (i = 0; i < last_num; i++) {
661 if (tp->Numbers[i] != ABSENT_NUMERIC)
666 for (i = 0; i < last_str; i++) {
667 if (tp->Strings[i] != ABSENT_STRING)
671 nextfree = compute_offsets(tp->Strings, strmax, offsets);
673 /* fill in the header */
674 LITTLE_ENDIAN(buf, MAGIC);
675 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
676 LITTLE_ENDIAN(buf + 4, boolmax);
677 LITTLE_ENDIAN(buf + 6, nummax);
678 LITTLE_ENDIAN(buf + 8, strmax);
679 LITTLE_ENDIAN(buf + 10, nextfree);
681 /* write out the header */
682 TRACE_OUT(("Header of %s @%d", namelist, *offset));
683 if (Write(buf, 12, 1) != 1
684 || Write(namelist, sizeof(char), namelen) != namelen)
687 for (i = 0; i < boolmax; i++)
688 if (tp->Booleans[i] == TRUE)
692 if (Write(buf, sizeof(char), boolmax) != boolmax)
695 if (even_boundary(namelen + boolmax))
698 TRACE_OUT(("Numerics begin at %04x", *offset));
701 convert_shorts(buf, tp->Numbers, nummax);
702 if (Write(buf, 2, nummax) != nummax)
705 TRACE_OUT(("String offsets begin at %04x", *offset));
707 /* the string offsets */
708 convert_shorts(buf, offsets, strmax);
709 if (Write(buf, 2, strmax) != strmax)
712 TRACE_OUT(("String table begins at %04x", *offset));
715 for (i = 0; i < strmax; i++)
716 if (VALID_STRING(tp->Strings[i]))
717 if (!WRITE_STRING(tp->Strings[i]))
721 if (extended_object(tp)) {
722 unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
724 if (even_boundary(nextfree))
727 nextfree = compute_offsets(tp->Strings + STRCOUNT,
728 (size_t) tp->ext_Strings,
730 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
732 if (tp->ext_Strings >= SIZEOF(offsets))
735 nextfree += compute_offsets(tp->ext_Names,
737 offsets + tp->ext_Strings);
738 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
739 strmax = tp->ext_Strings + extcnt;
742 * Write the extended header
744 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
745 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
746 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
747 LITTLE_ENDIAN(buf + 6, strmax);
748 LITTLE_ENDIAN(buf + 8, nextfree);
749 TRACE_OUT(("WRITE extended-header @%d", *offset));
750 if (Write(buf, 10, 1) != 1)
753 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
755 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
756 tp->ext_Booleans) != tp->ext_Booleans)
759 if (even_boundary(tp->ext_Booleans))
762 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
763 if (tp->ext_Numbers) {
764 convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
765 if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
770 * Convert the offsets for the ext_Strings and ext_Names tables,
773 convert_shorts(buf, offsets, strmax);
774 TRACE_OUT(("WRITE offsets @%d", *offset));
775 if (Write(buf, 2, strmax) != strmax)
779 * Write the string table after the offset tables so we do not
780 * have to do anything about alignment.
782 for (i = 0; i < tp->ext_Strings; i++) {
783 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
784 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
785 _nc_visbuf(tp->Strings[i + STRCOUNT])));
786 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
792 * Write the extended names
794 for (i = 0; i < extcnt; i++) {
795 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
796 if (!WRITE_STRING(tp->ext_Names[i]))
801 #endif /* NCURSES_XNAMES */
808 * Returns the total number of entries written by this process
811 _nc_tic_written(void)
813 return total_written;