1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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>
47 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
51 #define TRACE_OUT(p) DEBUG(2, p)
53 #define TRACE_OUT(p) /*nothing */
56 MODULE_ID("$Id: write_entry.c,v 1.78 2010/12/25 23:23:08 tom Exp $")
58 static int total_written;
60 static int make_db_root(const char *);
61 static int write_object(TERMTYPE *, char *, unsigned *, unsigned);
65 write_file(char *filename, TERMTYPE *tp)
67 char buffer[MAX_ENTRY_SIZE];
68 unsigned limit = sizeof(buffer);
71 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
74 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
76 DEBUG(1, ("Created %s", filename));
78 if (write_object(tp, buffer, &offset, limit) == ERR
79 || fwrite(buffer, sizeof(char), offset, fp) != offset) {
80 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
87 * Check for access rights to destination directories
88 * Create any directories which don't exist.
90 * Note: there's no reason to return the result of make_db_root(), since
91 * this function is called only in instances where that has to succeed.
94 check_writeable(int code)
96 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
97 static bool verified[sizeof(dirnames)];
99 char dir[sizeof(LEAF_FMT)];
102 if (code == 0 || (s = strchr(dirnames, code)) == 0)
103 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
105 if (verified[s - dirnames])
108 sprintf(dir, LEAF_FMT, code);
109 if (make_db_root(dir) < 0) {
110 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
113 verified[s - dirnames] = TRUE;
115 #endif /* !USE_HASHED_DB */
118 make_db_path(char *dst, const char *src, unsigned limit)
121 const char *top = _nc_tic_dir(0);
123 if (src == top || _nc_is_abs_path(src)) {
124 if (strlen(src) + 1 <= limit) {
125 (void) strcpy(dst, src);
129 if (strlen(top) + strlen(src) + 2 <= limit) {
130 (void) sprintf(dst, "%s/%s", top, src);
136 if (_nc_is_dir_path(dst)) {
139 static const char suffix[] = DBM_SUFFIX;
140 unsigned have = strlen(dst);
141 unsigned need = strlen(suffix);
142 if (have > need && strcmp(dst + have - need, suffix)) {
143 if (have + need <= limit)
155 * Make a database-root if it doesn't exist.
158 make_db_root(const char *path)
161 char fullpath[PATH_MAX];
163 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
167 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL)
169 else if (_nc_db_close(capdbp) < 0)
174 if ((rc = stat(path, &statbuf)) < 0) {
176 #if !defined(__MINGW32__)
180 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
181 rc = -1; /* permission denied */
182 } else if (!(S_ISDIR(statbuf.st_mode))) {
183 rc = -1; /* not a directory */
191 * Set the write directory for compiled entries.
194 _nc_set_writedir(char *dir)
196 const char *destination;
197 char actual[PATH_MAX];
200 && use_terminfo_vars())
201 dir = getenv("TERMINFO");
204 (void) _nc_tic_dir(dir);
206 destination = _nc_tic_dir(0);
207 if (make_db_root(destination) < 0) {
208 char *home = _nc_home_terminfo();
212 if (make_db_root(destination) < 0)
213 _nc_err_abort("%s: permission denied (errno %d)",
219 * Note: because of this code, this logic should be exercised
220 * *once only* per run.
223 make_db_path(actual, destination, sizeof(actual));
225 if (chdir(_nc_tic_dir(destination)) < 0
226 || getcwd(actual, sizeof(actual)) == 0)
227 _nc_err_abort("%s: not a directory", destination);
229 _nc_keep_tic_dir(strdup(actual));
233 * Save the compiled version of a description in the filesystem.
235 * make a copy of the name-list
236 * break it up into first-name and all-but-last-name
238 * write object information to first-name
240 * for each name in all-but-last-name
243 * Using 'time()' to obtain a reference for file timestamps is unreliable,
244 * e.g., with NFS, because the filesystem may have a different time
245 * reference. We check for pre-existence of links by latching the first
246 * timestamp from a file that we create.
248 * The _nc_warning() calls will report a correct line number only if
249 * _nc_curr_line is properly set before the write_entry() call.
253 _nc_write_entry(TERMTYPE *const tp)
257 char buffer[MAX_ENTRY_SIZE + 1];
258 unsigned limit = sizeof(buffer);
261 #else /* !USE_HASHED_DB */
264 char filename[PATH_MAX];
265 char linkname[PATH_MAX];
267 char symlinkname[PATH_MAX];
272 #endif /* USE_SYMLINKS */
274 static int call_count;
275 static time_t start_time; /* time at start of writes */
277 #endif /* USE_HASHED_DB */
279 char name_list[MAX_TERMINFO_LENGTH];
280 char *first_name, *other_names;
283 assert(strlen(tp->term_names) != 0);
284 assert(strlen(tp->term_names) < sizeof(name_list));
286 (void) strcpy(name_list, tp->term_names);
287 DEBUG(7, ("Name list = '%s'", name_list));
289 first_name = name_list;
291 ptr = &name_list[strlen(name_list) - 1];
292 other_names = ptr + 1;
294 while (ptr > name_list && *ptr != '|')
297 if (ptr != name_list) {
300 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
307 other_names = ptr + 1;
311 DEBUG(7, ("First name = '%s'", first_name));
312 DEBUG(7, ("Other names = '%s'", other_names));
314 _nc_set_type(first_name);
317 if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
318 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
324 memset(&key, 0, sizeof(key));
325 key.data = tp->term_names;
326 key.size = strlen(tp->term_names);
328 memset(&data, 0, sizeof(data));
330 data.size = offset + 1;
332 _nc_db_put(capdb, &key, &data);
336 key.data = name_list;
337 key.size = strlen(name_list);
339 strcpy(buffer + 1, tp->term_names);
340 data.size = strlen(tp->term_names) + 1;
342 _nc_db_put(capdb, &key, &data);
344 while (*other_names != '\0') {
346 assert(ptr < buffer + sizeof(buffer) - 1);
347 while (*other_names != '|' && *other_names != '\0')
350 if (*other_names != '\0')
351 *(other_names++) = '\0';
354 key.size = strlen(ptr);
356 _nc_db_put(capdb, &key, &data);
361 #else /* !USE_HASHED_DB */
362 if (call_count++ == 0) {
366 if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
367 _nc_warning("terminal name too long.");
369 sprintf(filename, LEAF_FMT "/%s", first_name[0], first_name);
372 * Has this primary name been written since the first call to
373 * write_entry()? If so, the newer write will step on the older,
376 if (start_time > 0 &&
377 stat(filename, &statbuf) >= 0
378 && statbuf.st_mtime >= start_time) {
379 _nc_warning("name multiply defined.");
382 check_writeable(first_name[0]);
383 write_file(filename, tp);
385 if (start_time == 0) {
386 if (stat(filename, &statbuf) < 0
387 || (start_time = statbuf.st_mtime) == 0) {
388 _nc_syserr_abort("error obtaining time from %s/%s",
389 _nc_tic_dir(0), filename);
392 while (*other_names != '\0') {
394 while (*other_names != '|' && *other_names != '\0')
397 if (*other_names != '\0')
398 *(other_names++) = '\0';
400 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
401 _nc_warning("terminal alias %s too long.", ptr);
404 if (strchr(ptr, '/') != 0) {
405 _nc_warning("cannot link alias %s.", ptr);
409 check_writeable(ptr[0]);
410 sprintf(linkname, LEAF_FMT "/%s", ptr[0], ptr);
412 if (strcmp(filename, linkname) == 0) {
413 _nc_warning("self-synonym ignored");
414 } else if (stat(linkname, &statbuf) >= 0 &&
415 statbuf.st_mtime < start_time) {
416 _nc_warning("alias %s multiply defined.", ptr);
417 } else if (_nc_access(linkname, W_OK) == 0)
422 if (first_name[0] == linkname[0])
423 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
425 strcpy(symlinkname, "../");
426 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
428 symlinkname[sizeof(symlinkname) - 1] = '\0';
429 #endif /* USE_SYMLINKS */
431 code = remove(linkname);
433 code = unlink(linkname);
435 if (code != 0 && errno == ENOENT)
438 if (symlink(symlinkname, linkname) < 0)
440 if (link(filename, linkname) < 0)
441 #endif /* USE_SYMLINKS */
444 * If there wasn't anything there, and we cannot
445 * link to the target because it is the same as the
446 * target, then the source must be on a filesystem
447 * that uses caseless filenames, such as Win32, etc.
449 if (code == 0 && errno == EEXIST)
450 _nc_warning("can't link %s to %s", filename, linkname);
451 else if (code == 0 && (errno == EPERM || errno == ENOENT))
452 write_file(linkname, tp);
454 #if MIXEDCASE_FILENAMES
455 _nc_syserr_abort("can't link %s to %s", filename, linkname);
457 _nc_warning("can't link %s to %s (errno=%d)", filename,
462 DEBUG(1, ("Linked %s", linkname));
465 #else /* just make copies */
466 write_file(linkname, tp);
467 #endif /* HAVE_LINK */
469 #endif /* USE_HASHED_DB */
473 fake_write(char *dst,
480 size_t have = (limit - *offset);
486 memcpy(dst + *offset, src, want);
487 *offset += (unsigned) want;
491 return (want / size);
494 #define Write(buf, size, count) fake_write(buffer, offset, limit, (char *) buf, count, size)
496 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
497 #define HI(x) ((x) / 256)
498 #define LO(x) ((x) % 256)
499 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
500 (p)[1] = (unsigned char)HI(x)
502 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
505 compute_offsets(char **Strings, size_t strmax, short *offsets)
510 for (i = 0; i < strmax; i++) {
511 if (Strings[i] == ABSENT_STRING) {
513 } else if (Strings[i] == CANCELLED_STRING) {
516 offsets[i] = (short) nextfree;
517 nextfree += (int) strlen(Strings[i]) + 1;
518 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
519 _nc_visbuf(Strings[i]), (int) nextfree));
526 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
529 for (i = 0; i < count; i++) {
530 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
531 buf[2 * i] = buf[2 * i + 1] = 0377;
532 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
534 buf[2 * i + 1] = 0377;
536 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
537 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
542 #define even_boundary(value) \
543 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
547 extended_Booleans(TERMTYPE *tp)
552 for (i = 0; i < tp->ext_Booleans; ++i) {
553 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
560 extended_Numbers(TERMTYPE *tp)
565 for (i = 0; i < tp->ext_Numbers; ++i) {
566 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
573 extended_Strings(TERMTYPE *tp)
575 unsigned short result = 0;
578 for (i = 0; i < tp->ext_Strings; ++i) {
579 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
580 result = (unsigned short) (i + 1);
586 * _nc_align_termtype() will extend entries that are referenced in a use=
587 * clause - discard the unneeded data.
590 extended_object(TERMTYPE *tp)
594 if (_nc_user_definable) {
595 result = ((extended_Booleans(tp)
596 + extended_Numbers(tp)
597 + extended_Strings(tp)) != 0);
604 write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
607 size_t namelen, boolmax, nummax, strmax;
611 short offsets[MAX_ENTRY_SIZE / 2];
612 unsigned char buf[MAX_ENTRY_SIZE];
613 unsigned last_bool = BOOLWRITE;
614 unsigned last_num = NUMWRITE;
615 unsigned last_str = STRWRITE;
619 * Normally we limit the list of values to exclude the "obsolete"
620 * capabilities. However, if we are accepting extended names, add
621 * these as well, since they are used for supporting translation
624 if (_nc_user_definable) {
625 last_bool = BOOLCOUNT;
631 namelist = tp->term_names;
632 namelen = strlen(namelist) + 1;
635 for (i = 0; i < last_bool; i++) {
636 if (tp->Booleans[i] == TRUE)
641 for (i = 0; i < last_num; i++) {
642 if (tp->Numbers[i] != ABSENT_NUMERIC)
647 for (i = 0; i < last_str; i++) {
648 if (tp->Strings[i] != ABSENT_STRING)
652 nextfree = compute_offsets(tp->Strings, strmax, offsets);
654 /* fill in the header */
655 LITTLE_ENDIAN(buf, MAGIC);
656 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
657 LITTLE_ENDIAN(buf + 4, boolmax);
658 LITTLE_ENDIAN(buf + 6, nummax);
659 LITTLE_ENDIAN(buf + 8, strmax);
660 LITTLE_ENDIAN(buf + 10, nextfree);
662 /* write out the header */
663 TRACE_OUT(("Header of %s @%d", namelist, *offset));
664 if (Write(buf, 12, 1) != 1
665 || Write(namelist, sizeof(char), namelen) != namelen)
668 for (i = 0; i < boolmax; i++)
669 if (tp->Booleans[i] == TRUE)
673 if (Write(buf, sizeof(char), boolmax) != boolmax)
676 if (even_boundary(namelen + boolmax))
679 TRACE_OUT(("Numerics begin at %04x", *offset));
682 convert_shorts(buf, tp->Numbers, nummax);
683 if (Write(buf, 2, nummax) != nummax)
686 TRACE_OUT(("String offsets begin at %04x", *offset));
688 /* the string offsets */
689 convert_shorts(buf, offsets, strmax);
690 if (Write(buf, 2, strmax) != strmax)
693 TRACE_OUT(("String table begins at %04x", *offset));
696 for (i = 0; i < strmax; i++)
697 if (VALID_STRING(tp->Strings[i]))
698 if (!WRITE_STRING(tp->Strings[i]))
702 if (extended_object(tp)) {
703 unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
705 if (even_boundary(nextfree))
708 nextfree = compute_offsets(tp->Strings + STRCOUNT,
711 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
713 if (tp->ext_Strings >= SIZEOF(offsets))
716 nextfree += compute_offsets(tp->ext_Names,
718 offsets + tp->ext_Strings);
719 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
720 strmax = tp->ext_Strings + extcnt;
723 * Write the extended header
725 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
726 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
727 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
728 LITTLE_ENDIAN(buf + 6, strmax);
729 LITTLE_ENDIAN(buf + 8, nextfree);
730 TRACE_OUT(("WRITE extended-header @%d", *offset));
731 if (Write(buf, 10, 1) != 1)
734 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
736 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
737 tp->ext_Booleans) != tp->ext_Booleans)
740 if (even_boundary(tp->ext_Booleans))
743 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
744 if (tp->ext_Numbers) {
745 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
746 if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
751 * Convert the offsets for the ext_Strings and ext_Names tables,
754 convert_shorts(buf, offsets, strmax);
755 TRACE_OUT(("WRITE offsets @%d", *offset));
756 if (Write(buf, 2, strmax) != strmax)
760 * Write the string table after the offset tables so we do not
761 * have to do anything about alignment.
763 for (i = 0; i < tp->ext_Strings; i++) {
764 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
765 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
766 _nc_visbuf(tp->Strings[i + STRCOUNT])));
767 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
773 * Write the extended names
775 for (i = 0; i < extcnt; i++) {
776 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
777 if (!WRITE_STRING(tp->ext_Names[i]))
782 #endif /* NCURSES_XNAMES */
789 * Returns the total number of entries written by this process
792 _nc_tic_written(void)
794 return total_written;