1 /****************************************************************************
2 * Copyright (c) 1998-2000,2002 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 ****************************************************************************/
35 * write_entry.c -- write a terminfo structure onto the file system
38 #include <curses.priv.h>
43 #include <term_entry.h>
46 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
50 #define TRACE_OUT(p) DEBUG(2, p)
52 #define TRACE_OUT(p) /*nothing */
55 MODULE_ID("$Id: write_entry.c,v 1.58 2002/04/21 20:35:08 tom Exp $")
57 static int total_written;
59 static int write_object(FILE *, TERMTYPE *);
62 write_file(char *filename, TERMTYPE * tp)
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 (write_object(fp, tp) == ERR) {
72 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
78 * make_directory(char *path)
80 * Make a directory if it doesn't exist.
83 make_directory(const char *path)
87 char fullpath[PATH_MAX];
88 const char *destination = _nc_tic_dir(0);
90 if (path == destination || *path == '/') {
91 if (strlen(path) + 1 > sizeof(fullpath))
93 (void) strcpy(fullpath, path);
95 if (strlen(destination) + strlen(path) + 2 > sizeof(fullpath))
97 (void) sprintf(fullpath, "%s/%s", destination, path);
100 if ((rc = stat(path, &statbuf)) < 0) {
101 rc = mkdir(path, 0777);
103 if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
104 rc = -1; /* permission denied */
105 } else if (!(S_ISDIR(statbuf.st_mode))) {
106 rc = -1; /* not a directory */
113 _nc_set_writedir(char *dir)
114 /* set the write directory for compiled entries */
116 const char *destination;
117 char actual[PATH_MAX];
120 && use_terminfo_vars())
121 dir = getenv("TERMINFO");
124 (void) _nc_tic_dir(dir);
126 destination = _nc_tic_dir(0);
127 if (make_directory(destination) < 0) {
128 char *home = _nc_home_terminfo();
132 if (make_directory(destination) < 0)
133 _nc_err_abort("%s: permission denied (errno %d)",
139 * Note: because of this code, this logic should be exercised
140 * *once only* per run.
142 if (chdir(_nc_tic_dir(destination)) < 0
143 || getcwd(actual, sizeof(actual)) == 0)
144 _nc_err_abort("%s: not a directory", destination);
145 _nc_keep_tic_dir(strdup(actual));
149 * check_writeable(char code)
151 * Miscellaneous initialisations
153 * Check for access rights to destination directories
154 * Create any directories which don't exist.
155 * Note: there's no reason to return the result of make_directory(), since
156 * this function is called only in instances where that has to succeed.
161 check_writeable(int code)
163 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
164 static bool verified[sizeof(dirnames)];
169 if (code == 0 || (s = strchr(dirnames, code)) == 0)
170 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code);
172 if (verified[s - dirnames])
177 if (make_directory(dir) < 0) {
178 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
181 verified[s - dirnames] = TRUE;
187 * Save the compiled version of a description in the filesystem.
189 * make a copy of the name-list
190 * break it up into first-name and all-but-last-name
192 * write object information to first-name
194 * for each name in all-but-last-name
197 * Using 'time()' to obtain a reference for file timestamps is unreliable,
198 * e.g., with NFS, because the filesystem may have a different time
199 * reference. We check for pre-existence of links by latching the first
200 * timestamp from a file that we create.
202 * The _nc_warning() calls will report a correct line number only if
203 * _nc_curr_line is properly set before the write_entry() call.
207 _nc_write_entry(TERMTYPE * const tp)
210 char name_list[MAX_TERMINFO_LENGTH];
211 char *first_name, *other_names;
213 char filename[PATH_MAX];
214 char linkname[PATH_MAX];
216 char symlinkname[PATH_MAX];
221 #endif /* USE_SYMLINKS */
222 static int call_count;
223 static time_t start_time; /* time at start of writes */
225 if (call_count++ == 0) {
229 (void) strcpy(name_list, tp->term_names);
230 DEBUG(7, ("Name list = '%s'", name_list));
232 first_name = name_list;
234 ptr = &name_list[strlen(name_list) - 1];
235 other_names = ptr + 1;
237 while (ptr > name_list && *ptr != '|')
240 if (ptr != name_list) {
243 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
250 other_names = ptr + 1;
254 DEBUG(7, ("First name = '%s'", first_name));
255 DEBUG(7, ("Other names = '%s'", other_names));
257 _nc_set_type(first_name);
259 if (strlen(first_name) > sizeof(filename) - 3)
260 _nc_warning("terminal name too long.");
262 sprintf(filename, "%c/%s", first_name[0], first_name);
265 * Has this primary name been written since the first call to
266 * write_entry()? If so, the newer write will step on the older,
269 if (start_time > 0 &&
270 stat(filename, &statbuf) >= 0
271 && statbuf.st_mtime >= start_time) {
272 _nc_warning("name multiply defined.");
275 check_writeable(first_name[0]);
276 write_file(filename, tp);
278 if (start_time == 0) {
279 if (stat(filename, &statbuf) < 0
280 || (start_time = statbuf.st_mtime) == 0) {
281 _nc_syserr_abort("error obtaining time from %s/%s",
282 _nc_tic_dir(0), filename);
285 while (*other_names != '\0') {
287 while (*other_names != '|' && *other_names != '\0')
290 if (*other_names != '\0')
291 *(other_names++) = '\0';
293 if (strlen(ptr) > sizeof(linkname) - 3) {
294 _nc_warning("terminal alias %s too long.", ptr);
297 if (strchr(ptr, '/') != 0) {
298 _nc_warning("cannot link alias %s.", ptr);
302 check_writeable(ptr[0]);
303 sprintf(linkname, "%c/%s", ptr[0], ptr);
305 if (strcmp(filename, linkname) == 0) {
306 _nc_warning("self-synonym ignored");
307 } else if (stat(linkname, &statbuf) >= 0 &&
308 statbuf.st_mtime < start_time) {
309 _nc_warning("alias %s multiply defined.", ptr);
310 } else if (_nc_access(linkname, W_OK) == 0)
315 strcpy(symlinkname, "../");
316 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
317 symlinkname[sizeof(symlinkname) - 1] = '\0';
318 #endif /* USE_SYMLINKS */
320 code = remove(linkname);
322 code = unlink(linkname);
324 if (code != 0 && errno == ENOENT)
327 if (symlink(symlinkname, linkname) < 0)
329 if (link(filename, linkname) < 0)
330 #endif /* USE_SYMLINKS */
333 * If there wasn't anything there, and we cannot
334 * link to the target because it is the same as the
335 * target, then the source must be on a filesystem
336 * that uses caseless filenames, such as Win32, etc.
338 if (code == 0 && errno == EEXIST)
339 _nc_warning("can't link %s to %s", filename, linkname);
340 else if (code == 0 && (errno == EPERM || errno == ENOENT))
341 write_file(linkname, tp);
343 #if MIXEDCASE_FILENAMES
344 _nc_syserr_abort("can't link %s to %s", filename, linkname);
346 _nc_warning("can't link %s to %s (errno=%d)", filename,
351 DEBUG(1, ("Linked %s", linkname));
354 #else /* just make copies */
355 write_file(linkname, tp);
356 #endif /* HAVE_LINK */
360 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
361 #define HI(x) ((x) / 256)
362 #define LO(x) ((x) % 256)
363 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x)
365 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1)
368 compute_offsets(char **Strings, unsigned strmax, short *offsets)
373 for (i = 0; i < strmax; i++) {
374 if (Strings[i] == ABSENT_STRING) {
376 } else if (Strings[i] == CANCELLED_STRING) {
379 offsets[i] = nextfree;
380 nextfree += strlen(Strings[i]) + 1;
381 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree));
388 convert_shorts(unsigned char *buf, short *Numbers, unsigned count)
391 for (i = 0; i < count; i++) {
392 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
393 buf[2 * i] = buf[2 * i + 1] = 0377;
394 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
396 buf[2 * i + 1] = 0377;
398 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
399 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
404 #define even_boundary(value) \
405 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1)
408 write_object(FILE * fp, TERMTYPE * tp)
411 size_t namelen, boolmax, nummax, strmax;
415 short offsets[MAX_ENTRY_SIZE / 2];
416 unsigned char buf[MAX_ENTRY_SIZE];
417 unsigned last_bool = BOOLWRITE;
418 unsigned last_num = NUMWRITE;
419 unsigned last_str = STRWRITE;
423 * Normally we limit the list of values to exclude the "obsolete"
424 * capabilities. However, if we are accepting extended names, add
425 * these as well, since they are used for supporting translation
428 if (_nc_user_definable) {
429 last_bool = BOOLCOUNT;
435 namelist = tp->term_names;
436 namelen = strlen(namelist) + 1;
439 for (i = 0; i < last_bool; i++) {
440 if (tp->Booleans[i] == TRUE)
445 for (i = 0; i < last_num; i++) {
446 if (tp->Numbers[i] != ABSENT_NUMERIC)
451 for (i = 0; i < last_str; i++) {
452 if (tp->Strings[i] != ABSENT_STRING)
456 nextfree = compute_offsets(tp->Strings, strmax, offsets);
458 /* fill in the header */
459 LITTLE_ENDIAN(buf, MAGIC);
460 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
461 LITTLE_ENDIAN(buf + 4, boolmax);
462 LITTLE_ENDIAN(buf + 6, nummax);
463 LITTLE_ENDIAN(buf + 8, strmax);
464 LITTLE_ENDIAN(buf + 10, nextfree);
466 /* write out the header */
467 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp)));
468 if (fwrite(buf, 12, 1, fp) != 1
469 || fwrite(namelist, sizeof(char), namelen, fp) != namelen)
472 for (i = 0; i < boolmax; i++)
473 if (tp->Booleans[i] == TRUE)
477 if (fwrite(buf, sizeof(char), boolmax, fp) != boolmax)
480 if (even_boundary(namelen + boolmax))
483 TRACE_OUT(("Numerics begin at %04lx", ftell(fp)));
486 convert_shorts(buf, tp->Numbers, nummax);
487 if (fwrite(buf, 2, nummax, fp) != nummax)
490 TRACE_OUT(("String offsets begin at %04lx", ftell(fp)));
492 /* the string offsets */
493 convert_shorts(buf, offsets, strmax);
494 if (fwrite(buf, 2, strmax, fp) != strmax)
497 TRACE_OUT(("String table begins at %04lx", ftell(fp)));
500 for (i = 0; i < strmax; i++)
501 if (VALID_STRING(tp->Strings[i]))
502 if (!WRITE_STRING(tp->Strings[i]))
506 if (NUM_EXT_NAMES(tp)) {
507 unsigned extcnt = NUM_EXT_NAMES(tp);
509 if (even_boundary(nextfree))
512 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets);
513 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
514 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings);
515 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
516 strmax = tp->ext_Strings + extcnt;
519 * Write the extended header
521 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
522 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
523 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
524 LITTLE_ENDIAN(buf + 6, strmax);
525 LITTLE_ENDIAN(buf + 8, nextfree);
526 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp)));
527 if (fwrite(buf, 10, 1, fp) != 1)
530 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp)));
532 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char),
533 tp->ext_Booleans, fp) != tp->ext_Booleans)
536 if (even_boundary(tp->ext_Booleans))
539 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp)));
540 if (tp->ext_Numbers) {
541 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
542 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers)
547 * Convert the offsets for the ext_Strings and ext_Names tables,
550 convert_shorts(buf, offsets, strmax);
551 TRACE_OUT(("WRITE offsets @%ld", ftell(fp)));
552 if (fwrite(buf, 2, strmax, fp) != strmax)
556 * Write the string table after the offset tables so we do not
557 * have to do anything about alignment.
559 for (i = 0; i < tp->ext_Strings; i++) {
560 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
561 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i,
562 _nc_visbuf(tp->Strings[i + STRCOUNT])));
563 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
569 * Write the extended names
571 for (i = 0; i < extcnt; i++) {
572 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i]));
573 if (!WRITE_STRING(tp->ext_Names[i]))
578 #endif /* NCURSES_XNAMES */
585 * Returns the total number of entries written by this process
588 _nc_tic_written(void)
590 return total_written;