1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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.80 2011/08/13 20:52:40 tom Exp $")
52 static int total_written;
54 static int make_db_root(const char *);
55 static int write_object(TERMTYPE *, char *, unsigned *, unsigned);
59 write_file(char *filename, TERMTYPE *tp)
61 char buffer[MAX_ENTRY_SIZE];
62 unsigned limit = sizeof(buffer);
65 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
68 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
70 DEBUG(1, ("Created %s", filename));
72 if (write_object(tp, buffer, &offset, limit) == ERR
73 || fwrite(buffer, sizeof(char), offset, fp) != offset) {
74 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
81 * Check for access rights to destination directories
82 * Create any directories which don't exist.
84 * Note: there's no reason to return the result of make_db_root(), since
85 * this function is called only in instances where that has to succeed.
88 check_writeable(int code)
90 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
91 static bool verified[sizeof(dirnames)];
93 char dir[sizeof(LEAF_FMT)];
96 if (code == 0 || (s = strchr(dirnames, code)) == 0)
97 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
99 if (verified[s - dirnames])
102 sprintf(dir, LEAF_FMT, code);
103 if (make_db_root(dir) < 0) {
104 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
107 verified[s - dirnames] = TRUE;
109 #endif /* !USE_HASHED_DB */
112 make_db_path(char *dst, const char *src, unsigned limit)
115 const char *top = _nc_tic_dir(0);
117 if (src == top || _nc_is_abs_path(src)) {
118 if (strlen(src) + 1 <= limit) {
119 (void) strcpy(dst, src);
123 if (strlen(top) + strlen(src) + 2 <= limit) {
124 (void) sprintf(dst, "%s/%s", top, src);
130 if (_nc_is_dir_path(dst)) {
133 static const char suffix[] = DBM_SUFFIX;
134 unsigned have = strlen(dst);
135 unsigned need = strlen(suffix);
136 if (have > need && strcmp(dst + have - need, suffix)) {
137 if (have + need <= limit)
149 * Make a database-root if it doesn't exist.
152 make_db_root(const char *path)
155 char fullpath[PATH_MAX];
157 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
161 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL)
163 else if (_nc_db_close(capdbp) < 0)
168 if ((rc = stat(path, &statbuf)) < 0) {
170 #if !defined(__MINGW32__)
174 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
175 rc = -1; /* permission denied */
176 } else if (!(S_ISDIR(statbuf.st_mode))) {
177 rc = -1; /* not a directory */
185 * Set the write directory for compiled entries.
188 _nc_set_writedir(char *dir)
190 const char *destination;
191 char actual[PATH_MAX];
194 && use_terminfo_vars())
195 dir = getenv("TERMINFO");
198 (void) _nc_tic_dir(dir);
200 destination = _nc_tic_dir(0);
201 if (make_db_root(destination) < 0) {
202 char *home = _nc_home_terminfo();
206 if (make_db_root(destination) < 0)
207 _nc_err_abort("%s: permission denied (errno %d)",
213 * Note: because of this code, this logic should be exercised
214 * *once only* per run.
217 make_db_path(actual, destination, sizeof(actual));
219 if (chdir(_nc_tic_dir(destination)) < 0
220 || getcwd(actual, sizeof(actual)) == 0)
221 _nc_err_abort("%s: not a directory", destination);
223 _nc_keep_tic_dir(strdup(actual));
227 * Save the compiled version of a description in the filesystem.
229 * make a copy of the name-list
230 * break it up into first-name and all-but-last-name
232 * write object information to first-name
234 * for each name in all-but-last-name
237 * Using 'time()' to obtain a reference for file timestamps is unreliable,
238 * e.g., with NFS, because the filesystem may have a different time
239 * reference. We check for pre-existence of links by latching the first
240 * timestamp from a file that we create.
242 * The _nc_warning() calls will report a correct line number only if
243 * _nc_curr_line is properly set before the write_entry() call.
247 _nc_write_entry(TERMTYPE *const tp)
251 char buffer[MAX_ENTRY_SIZE + 1];
252 unsigned limit = sizeof(buffer);
255 #else /* !USE_HASHED_DB */
258 char filename[PATH_MAX];
259 char linkname[PATH_MAX];
261 char symlinkname[PATH_MAX];
266 #endif /* USE_SYMLINKS */
268 static int call_count;
269 static time_t start_time; /* time at start of writes */
271 #endif /* USE_HASHED_DB */
273 char name_list[MAX_TERMINFO_LENGTH];
274 char *first_name, *other_names;
277 assert(strlen(tp->term_names) != 0);
278 assert(strlen(tp->term_names) < sizeof(name_list));
280 (void) strcpy(name_list, tp->term_names);
281 DEBUG(7, ("Name list = '%s'", name_list));
283 first_name = name_list;
285 ptr = &name_list[strlen(name_list) - 1];
286 other_names = ptr + 1;
288 while (ptr > name_list && *ptr != '|')
291 if (ptr != name_list) {
294 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
301 other_names = ptr + 1;
305 DEBUG(7, ("First name = '%s'", first_name));
306 DEBUG(7, ("Other names = '%s'", other_names));
308 _nc_set_type(first_name);
311 if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
312 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
318 memset(&key, 0, sizeof(key));
319 key.data = tp->term_names;
320 key.size = strlen(tp->term_names);
322 memset(&data, 0, sizeof(data));
324 data.size = offset + 1;
326 _nc_db_put(capdb, &key, &data);
330 key.data = name_list;
331 key.size = strlen(name_list);
333 strcpy(buffer + 1, tp->term_names);
334 data.size = strlen(tp->term_names) + 1;
336 _nc_db_put(capdb, &key, &data);
338 while (*other_names != '\0') {
340 assert(ptr < buffer + sizeof(buffer) - 1);
341 while (*other_names != '|' && *other_names != '\0')
344 if (*other_names != '\0')
345 *(other_names++) = '\0';
348 key.size = strlen(ptr);
350 _nc_db_put(capdb, &key, &data);
354 #else /* !USE_HASHED_DB */
355 if (call_count++ == 0) {
359 if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
360 _nc_warning("terminal name too long.");
362 sprintf(filename, LEAF_FMT "/%s", first_name[0], first_name);
365 * Has this primary name been written since the first call to
366 * write_entry()? If so, the newer write will step on the older,
369 if (start_time > 0 &&
370 stat(filename, &statbuf) >= 0
371 && statbuf.st_mtime >= start_time) {
372 _nc_warning("name multiply defined.");
375 check_writeable(first_name[0]);
376 write_file(filename, tp);
378 if (start_time == 0) {
379 if (stat(filename, &statbuf) < 0
380 || (start_time = statbuf.st_mtime) == 0) {
381 _nc_syserr_abort("error obtaining time from %s/%s",
382 _nc_tic_dir(0), filename);
385 while (*other_names != '\0') {
387 while (*other_names != '|' && *other_names != '\0')
390 if (*other_names != '\0')
391 *(other_names++) = '\0';
393 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
394 _nc_warning("terminal alias %s too long.", ptr);
397 if (strchr(ptr, '/') != 0) {
398 _nc_warning("cannot link alias %s.", ptr);
402 check_writeable(ptr[0]);
403 sprintf(linkname, LEAF_FMT "/%s", ptr[0], ptr);
405 if (strcmp(filename, linkname) == 0) {
406 _nc_warning("self-synonym ignored");
407 } else if (stat(linkname, &statbuf) >= 0 &&
408 statbuf.st_mtime < start_time) {
409 _nc_warning("alias %s multiply defined.", ptr);
410 } else if (_nc_access(linkname, W_OK) == 0)
415 if (first_name[0] == linkname[0])
416 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
418 strcpy(symlinkname, "../");
419 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
421 symlinkname[sizeof(symlinkname) - 1] = '\0';
422 #endif /* USE_SYMLINKS */
424 code = remove(linkname);
426 code = unlink(linkname);
428 if (code != 0 && errno == ENOENT)
431 if (symlink(symlinkname, linkname) < 0)
433 if (link(filename, linkname) < 0)
434 #endif /* USE_SYMLINKS */
437 * If there wasn't anything there, and we cannot
438 * link to the target because it is the same as the
439 * target, then the source must be on a filesystem
440 * that uses caseless filenames, such as Win32, etc.
442 if (code == 0 && errno == EEXIST)
443 _nc_warning("can't link %s to %s", filename, linkname);
444 else if (code == 0 && (errno == EPERM || errno == ENOENT))
445 write_file(linkname, tp);
447 #if MIXEDCASE_FILENAMES
448 _nc_syserr_abort("can't link %s to %s", filename, linkname);
450 _nc_warning("can't link %s to %s (errno=%d)", filename,
455 DEBUG(1, ("Linked %s", linkname));
458 #else /* just make copies */
459 write_file(linkname, tp);
460 #endif /* HAVE_LINK */
462 #endif /* USE_HASHED_DB */
466 fake_write(char *dst,
473 size_t have = (limit - *offset);
479 memcpy(dst + *offset, src, want);
480 *offset += (unsigned) want;
484 return (want / size);
487 #define Write(buf, size, count) fake_write(buffer, offset, limit, (char *) buf, count, size)
489 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
490 #define HI(x) ((x) / 256)
491 #define LO(x) ((x) % 256)
492 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
493 (p)[1] = (unsigned char)HI(x)
495 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
498 compute_offsets(char **Strings, size_t strmax, short *offsets)
503 for (i = 0; i < strmax; i++) {
504 if (Strings[i] == ABSENT_STRING) {
506 } else if (Strings[i] == CANCELLED_STRING) {
509 offsets[i] = (short) nextfree;
510 nextfree += (int) strlen(Strings[i]) + 1;
511 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
512 _nc_visbuf(Strings[i]), (int) nextfree));
519 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
522 for (i = 0; i < count; i++) {
523 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
524 buf[2 * i] = buf[2 * i + 1] = 0377;
525 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
527 buf[2 * i + 1] = 0377;
529 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
530 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
535 #define even_boundary(value) \
536 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
540 extended_Booleans(TERMTYPE *tp)
545 for (i = 0; i < tp->ext_Booleans; ++i) {
546 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
553 extended_Numbers(TERMTYPE *tp)
558 for (i = 0; i < tp->ext_Numbers; ++i) {
559 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
566 extended_Strings(TERMTYPE *tp)
568 unsigned short result = 0;
571 for (i = 0; i < tp->ext_Strings; ++i) {
572 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
573 result = (unsigned short) (i + 1);
579 * _nc_align_termtype() will extend entries that are referenced in a use=
580 * clause - discard the unneeded data.
583 extended_object(TERMTYPE *tp)
587 if (_nc_user_definable) {
588 result = ((extended_Booleans(tp)
589 + extended_Numbers(tp)
590 + extended_Strings(tp)) != 0);
597 write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
600 size_t namelen, boolmax, nummax, strmax;
604 short offsets[MAX_ENTRY_SIZE / 2];
605 unsigned char buf[MAX_ENTRY_SIZE];
606 unsigned last_bool = BOOLWRITE;
607 unsigned last_num = NUMWRITE;
608 unsigned last_str = STRWRITE;
612 * Normally we limit the list of values to exclude the "obsolete"
613 * capabilities. However, if we are accepting extended names, add
614 * these as well, since they are used for supporting translation
617 if (_nc_user_definable) {
618 last_bool = BOOLCOUNT;
624 namelist = tp->term_names;
625 namelen = strlen(namelist) + 1;
628 for (i = 0; i < last_bool; i++) {
629 if (tp->Booleans[i] == TRUE)
634 for (i = 0; i < last_num; i++) {
635 if (tp->Numbers[i] != ABSENT_NUMERIC)
640 for (i = 0; i < last_str; i++) {
641 if (tp->Strings[i] != ABSENT_STRING)
645 nextfree = compute_offsets(tp->Strings, strmax, offsets);
647 /* fill in the header */
648 LITTLE_ENDIAN(buf, MAGIC);
649 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
650 LITTLE_ENDIAN(buf + 4, boolmax);
651 LITTLE_ENDIAN(buf + 6, nummax);
652 LITTLE_ENDIAN(buf + 8, strmax);
653 LITTLE_ENDIAN(buf + 10, nextfree);
655 /* write out the header */
656 TRACE_OUT(("Header of %s @%d", namelist, *offset));
657 if (Write(buf, 12, 1) != 1
658 || Write(namelist, sizeof(char), namelen) != namelen)
661 for (i = 0; i < boolmax; i++)
662 if (tp->Booleans[i] == TRUE)
666 if (Write(buf, sizeof(char), boolmax) != boolmax)
669 if (even_boundary(namelen + boolmax))
672 TRACE_OUT(("Numerics begin at %04x", *offset));
675 convert_shorts(buf, tp->Numbers, nummax);
676 if (Write(buf, 2, nummax) != nummax)
679 TRACE_OUT(("String offsets begin at %04x", *offset));
681 /* the string offsets */
682 convert_shorts(buf, offsets, strmax);
683 if (Write(buf, 2, strmax) != strmax)
686 TRACE_OUT(("String table begins at %04x", *offset));
689 for (i = 0; i < strmax; i++)
690 if (VALID_STRING(tp->Strings[i]))
691 if (!WRITE_STRING(tp->Strings[i]))
695 if (extended_object(tp)) {
696 unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
698 if (even_boundary(nextfree))
701 nextfree = compute_offsets(tp->Strings + STRCOUNT,
704 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
706 if (tp->ext_Strings >= SIZEOF(offsets))
709 nextfree += compute_offsets(tp->ext_Names,
711 offsets + tp->ext_Strings);
712 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
713 strmax = tp->ext_Strings + extcnt;
716 * Write the extended header
718 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
719 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
720 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
721 LITTLE_ENDIAN(buf + 6, strmax);
722 LITTLE_ENDIAN(buf + 8, nextfree);
723 TRACE_OUT(("WRITE extended-header @%d", *offset));
724 if (Write(buf, 10, 1) != 1)
727 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
729 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
730 tp->ext_Booleans) != tp->ext_Booleans)
733 if (even_boundary(tp->ext_Booleans))
736 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
737 if (tp->ext_Numbers) {
738 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
739 if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
744 * Convert the offsets for the ext_Strings and ext_Names tables,
747 convert_shorts(buf, offsets, strmax);
748 TRACE_OUT(("WRITE offsets @%d", *offset));
749 if (Write(buf, 2, strmax) != strmax)
753 * Write the string table after the offset tables so we do not
754 * have to do anything about alignment.
756 for (i = 0; i < tp->ext_Strings; i++) {
757 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
758 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
759 _nc_visbuf(tp->Strings[i + STRCOUNT])));
760 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
766 * Write the extended names
768 for (i = 0; i < extcnt; i++) {
769 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
770 if (!WRITE_STRING(tp->ext_Names[i]))
775 #endif /* NCURSES_XNAMES */
782 * Returns the total number of entries written by this process
785 _nc_tic_written(void)
787 return total_written;