1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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.52 2000/03/11 12:23:42 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 (void) _nc_tic_dir(dir);
121 else if (getenv("TERMINFO") != NULL)
122 (void) _nc_tic_dir(getenv("TERMINFO"));
124 destination = _nc_tic_dir(0);
125 if (make_directory(destination) < 0) {
126 char *home = _nc_home_terminfo();
130 if (make_directory(destination) < 0)
131 _nc_err_abort("%s: permission denied (errno %d)",
137 * Note: because of this code, this logic should be exercised
138 * *once only* per run.
140 if (chdir(_nc_tic_dir(destination)) < 0
141 || getcwd(actual, sizeof(actual)) == 0)
142 _nc_err_abort("%s: not a directory", destination);
143 _nc_keep_tic_dir(strdup(actual));
147 * check_writeable(char code)
149 * Miscellaneous initialisations
151 * Check for access rights to destination directories
152 * Create any directories which don't exist.
153 * Note: there's no reason to return the result of make_directory(), since
154 * this function is called only in instances where that has to succeed.
159 check_writeable(int code)
161 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
162 static bool verified[sizeof(dirnames)];
167 if (code == 0 || (s = strchr(dirnames, code)) == 0)
168 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code);
170 if (verified[s - dirnames])
175 if (make_directory(dir) < 0) {
176 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
179 verified[s - dirnames] = TRUE;
185 * Save the compiled version of a description in the filesystem.
187 * make a copy of the name-list
188 * break it up into first-name and all-but-last-name
190 * write object information to first-name
192 * for each name in all-but-last-name
195 * Using 'time()' to obtain a reference for file timestamps is unreliable,
196 * e.g., with NFS, because the filesystem may have a different time
197 * reference. We check for pre-existence of links by latching the first
198 * timestamp from a file that we create.
200 * The _nc_warning() calls will report a correct line number only if
201 * _nc_curr_line is properly set before the write_entry() call.
205 _nc_write_entry(TERMTYPE * const tp)
208 char name_list[MAX_TERMINFO_LENGTH];
209 char *first_name, *other_names;
211 char filename[PATH_MAX];
212 char linkname[PATH_MAX];
214 char symlinkname[PATH_MAX];
215 #endif /* USE_SYMLINKS */
216 static int call_count;
217 static time_t start_time; /* time at start of writes */
219 if (call_count++ == 0) {
223 (void) strcpy(name_list, tp->term_names);
224 DEBUG(7, ("Name list = '%s'", name_list));
226 first_name = name_list;
228 ptr = &name_list[strlen(name_list) - 1];
229 other_names = ptr + 1;
231 while (ptr > name_list && *ptr != '|')
234 if (ptr != name_list) {
237 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
244 other_names = ptr + 1;
248 DEBUG(7, ("First name = '%s'", first_name));
249 DEBUG(7, ("Other names = '%s'", other_names));
251 _nc_set_type(first_name);
253 if (strlen(first_name) > sizeof(filename) - 3)
254 _nc_warning("terminal name too long.");
256 sprintf(filename, "%c/%s", first_name[0], first_name);
259 * Has this primary name been written since the first call to
260 * write_entry()? If so, the newer write will step on the older,
263 if (start_time > 0 &&
264 stat(filename, &statbuf) >= 0
265 && statbuf.st_mtime >= start_time) {
266 _nc_warning("name multiply defined.");
269 check_writeable(first_name[0]);
270 write_file(filename, tp);
272 if (start_time == 0) {
273 if (stat(filename, &statbuf) < 0
274 || (start_time = statbuf.st_mtime) == 0) {
275 _nc_syserr_abort("error obtaining time from %s/%s",
276 _nc_tic_dir(0), filename);
279 while (*other_names != '\0') {
281 while (*other_names != '|' && *other_names != '\0')
284 if (*other_names != '\0')
285 *(other_names++) = '\0';
287 if (strlen(ptr) > sizeof(linkname) - 3) {
288 _nc_warning("terminal alias %s too long.", ptr);
291 if (strchr(ptr, '/') != 0) {
292 _nc_warning("cannot link alias %s.", ptr);
296 check_writeable(ptr[0]);
297 sprintf(linkname, "%c/%s", ptr[0], ptr);
299 if (strcmp(filename, linkname) == 0) {
300 _nc_warning("self-synonym ignored");
301 } else if (stat(linkname, &statbuf) >= 0 &&
302 statbuf.st_mtime < start_time) {
303 _nc_warning("alias %s multiply defined.", ptr);
304 } else if (_nc_access(linkname, W_OK) == 0)
309 strcpy(symlinkname, "../");
310 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
311 symlinkname[sizeof(symlinkname) - 1] = '\0';
312 #endif /* USE_SYMLINKS */
314 code = remove(linkname);
316 code = unlink(linkname);
318 if (code != 0 && errno == ENOENT)
321 if (symlink(symlinkname, linkname) < 0)
323 if (link(filename, linkname) < 0)
324 #endif /* USE_SYMLINKS */
327 * If there wasn't anything there, and we cannot
328 * link to the target because it is the same as the
329 * target, then the source must be on a filesystem
330 * that uses caseless filenames, such as Win32, etc.
332 if (code == 0 && errno == EEXIST)
333 _nc_warning("can't link %s to %s", filename, linkname);
334 else if (code == 0 && errno == EPERM)
335 write_file(linkname, tp);
337 _nc_syserr_abort("can't link %s to %s", filename, linkname);
339 DEBUG(1, ("Linked %s", linkname));
342 #else /* just make copies */
343 write_file(linkname, tp);
344 #endif /* HAVE_LINK */
348 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
349 #define HI(x) ((x) / 256)
350 #define LO(x) ((x) % 256)
351 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x)
353 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1)
356 compute_offsets(char **Strings, int strmax, short *offsets)
361 for (i = 0; i < strmax; i++) {
362 if (Strings[i] == ABSENT_STRING) {
364 } else if (Strings[i] == CANCELLED_STRING) {
367 offsets[i] = nextfree;
368 nextfree += strlen(Strings[i]) + 1;
369 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree));
376 convert_shorts(unsigned char *buf, short *Numbers, int count)
379 for (i = 0; i < count; i++) {
380 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
381 buf[2 * i] = buf[2 * i + 1] = 0377;
382 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
384 buf[2 * i + 1] = 0377;
386 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
387 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
392 #define even_boundary(value) \
393 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1)
396 write_object(FILE * fp, TERMTYPE * tp)
399 size_t namelen, boolmax, nummax, strmax;
403 short offsets[MAX_ENTRY_SIZE / 2];
404 unsigned char buf[MAX_ENTRY_SIZE];
405 unsigned last_bool = BOOLWRITE;
406 unsigned last_num = NUMWRITE;
407 unsigned last_str = STRWRITE;
411 * Normally we limit the list of values to exclude the "obsolete"
412 * capabilities. However, if we are accepting extended names, add
413 * these as well, since they are used for supporting translation
416 if (_nc_user_definable) {
417 last_bool = BOOLCOUNT;
423 namelist = tp->term_names;
424 namelen = strlen(namelist) + 1;
427 for (i = 0; i < last_bool; i++) {
428 if (tp->Booleans[i] == TRUE)
433 for (i = 0; i < last_num; i++) {
434 if (tp->Numbers[i] != ABSENT_NUMERIC)
439 for (i = 0; i < last_str; i++) {
440 if (tp->Strings[i] != ABSENT_STRING)
444 nextfree = compute_offsets(tp->Strings, strmax, offsets);
446 /* fill in the header */
447 LITTLE_ENDIAN(buf, MAGIC);
448 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
449 LITTLE_ENDIAN(buf + 4, boolmax);
450 LITTLE_ENDIAN(buf + 6, nummax);
451 LITTLE_ENDIAN(buf + 8, strmax);
452 LITTLE_ENDIAN(buf + 10, nextfree);
454 /* write out the header */
455 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp)));
456 if (fwrite(buf, 12, 1, fp) != 1
457 || fwrite(namelist, sizeof(char), namelen, fp) != namelen)
460 for (i = 0; i < boolmax; i++)
461 if (tp->Booleans[i] == TRUE)
465 if (fwrite(buf, sizeof(char), boolmax, fp) != boolmax)
468 if (even_boundary(namelen + boolmax))
471 TRACE_OUT(("Numerics begin at %04lx", ftell(fp)));
474 convert_shorts(buf, tp->Numbers, nummax);
475 if (fwrite(buf, 2, nummax, fp) != nummax)
478 TRACE_OUT(("String offsets begin at %04lx", ftell(fp)));
480 /* the string offsets */
481 convert_shorts(buf, offsets, strmax);
482 if (fwrite(buf, 2, strmax, fp) != strmax)
485 TRACE_OUT(("String table begins at %04lx", ftell(fp)));
488 for (i = 0; i < strmax; i++)
489 if (VALID_STRING(tp->Strings[i]))
490 if (!WRITE_STRING(tp->Strings[i]))
494 if (NUM_EXT_NAMES(tp)) {
495 unsigned extcnt = NUM_EXT_NAMES(tp);
497 if (even_boundary(nextfree))
500 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets);
501 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
502 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings);
503 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
504 strmax = tp->ext_Strings + extcnt;
507 * Write the extended header
509 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
510 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
511 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
512 LITTLE_ENDIAN(buf + 6, strmax);
513 LITTLE_ENDIAN(buf + 8, nextfree);
514 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp)));
515 if (fwrite(buf, 10, 1, fp) != 1)
518 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp)));
520 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char),
521 tp->ext_Booleans, fp) != tp->ext_Booleans)
524 if (even_boundary(tp->ext_Booleans))
527 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp)));
528 if (tp->ext_Numbers) {
529 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
530 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers)
535 * Convert the offsets for the ext_Strings and ext_Names tables,
538 convert_shorts(buf, offsets, strmax);
539 TRACE_OUT(("WRITE offsets @%ld", ftell(fp)));
540 if (fwrite(buf, 2, strmax, fp) != strmax)
544 * Write the string table after the offset tables so we do not
545 * have to do anything about alignment.
547 for (i = 0; i < tp->ext_Strings; i++) {
548 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
549 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i,
550 _nc_visbuf(tp->Strings[i + STRCOUNT])));
551 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
557 * Write the extended names
559 for (i = 0; i < extcnt; i++) {
560 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i]));
561 if (!WRITE_STRING(tp->ext_Names[i]))
566 #endif /* NCURSES_XNAMES */
573 * Returns the total number of entries written by this process
576 _nc_tic_written(void)
578 return total_written;