1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
37 * write_entry.c -- write a terminfo structure onto the file system
40 #include <curses.priv.h>
45 #include <term_entry.h>
48 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR)
52 #define TRACE_OUT(p) DEBUG(2, p)
54 #define TRACE_OUT(p) /*nothing*/
57 MODULE_ID("$Id: write_entry.c,v 1.47 1999/07/10 20:29:22 tom Exp $")
59 static int total_written;
61 static int write_object(FILE *, TERMTYPE *);
63 static void write_file(char *filename, TERMTYPE *tp)
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(fp, tp) == ERR) {
73 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
79 * make_directory(char *path)
81 * Make a directory if it doesn't exist.
83 static int 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 */
112 void _nc_set_writedir(char *dir)
113 /* set the write directory for compiled entries */
115 const char *destination;
116 char actual[PATH_MAX];
119 (void) _nc_tic_dir(dir);
120 else if (getenv("TERMINFO") != NULL)
121 (void) _nc_tic_dir(getenv("TERMINFO"));
123 destination = _nc_tic_dir(0);
124 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.
158 static void check_writeable(int code)
160 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
161 static bool verified[sizeof(dirnames)];
166 if (code == 0 || (s = strchr(dirnames, code)) == 0)
167 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code);
169 if (verified[s-dirnames])
174 if (make_directory(dir) < 0) {
175 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
178 verified[s-dirnames] = TRUE;
184 * Save the compiled version of a description in the filesystem.
186 * make a copy of the name-list
187 * break it up into first-name and all-but-last-name
189 * write object information to first-name
191 * for each name in all-but-last-name
194 * Using 'time()' to obtain a reference for file timestamps is unreliable,
195 * e.g., with NFS, because the filesystem may have a different time
196 * reference. We check for pre-existence of links by latching the first
197 * timestamp from a file that we create.
199 * The _nc_warning() calls will report a correct line number only if
200 * _nc_curr_line is properly set before the write_entry() call.
203 void _nc_write_entry(TERMTYPE *const tp)
206 char name_list[MAX_TERMINFO_LENGTH];
207 char *first_name, *other_names;
209 char filename[PATH_MAX];
210 char linkname[PATH_MAX];
212 char symlinkname[PATH_MAX];
213 #endif /* USE_SYMLINKS */
214 static int call_count;
215 static time_t start_time; /* time at start of writes */
217 if (call_count++ == 0) {
221 (void) strcpy(name_list, tp->term_names);
222 DEBUG(7, ("Name list = '%s'", name_list));
224 first_name = name_list;
226 ptr = &name_list[strlen(name_list) - 1];
227 other_names = ptr + 1;
229 while (ptr > name_list && *ptr != '|')
232 if (ptr != name_list) {
235 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
242 other_names = ptr + 1;
246 DEBUG(7, ("First name = '%s'", first_name));
247 DEBUG(7, ("Other names = '%s'", other_names));
249 _nc_set_type(first_name);
251 if (strlen(first_name) > sizeof(filename)-3)
252 _nc_warning("terminal name too long.");
254 sprintf(filename, "%c/%s", first_name[0], first_name);
257 * Has this primary name been written since the first call to
258 * write_entry()? If so, the newer write will step on the older,
261 if (start_time > 0 &&
262 stat(filename, &statbuf) >= 0
263 && statbuf.st_mtime >= start_time)
265 _nc_warning("name multiply defined.");
268 check_writeable(first_name[0]);
269 write_file(filename, tp);
271 if (start_time == 0) {
272 if (stat(filename, &statbuf) < 0
273 || (start_time = statbuf.st_mtime) == 0) {
274 _nc_syserr_abort("error obtaining time from %s/%s",
275 _nc_tic_dir(0), filename);
278 while (*other_names != '\0') {
280 while (*other_names != '|' && *other_names != '\0')
283 if (*other_names != '\0')
284 *(other_names++) = '\0';
286 if (strlen(ptr) > sizeof(linkname)-3) {
287 _nc_warning("terminal alias %s too long.", ptr);
290 if (strchr(ptr, '/') != 0) {
291 _nc_warning("cannot link alias %s.", ptr);
295 check_writeable(ptr[0]);
296 sprintf(linkname, "%c/%s", ptr[0], ptr);
298 if (strcmp(filename, linkname) == 0) {
299 _nc_warning("self-synonym ignored");
301 else if (stat(linkname, &statbuf) >= 0 &&
302 statbuf.st_mtime < start_time)
304 _nc_warning("alias %s multiply defined.", ptr);
306 else if (_nc_access(linkname, W_OK) == 0)
311 strcpy(symlinkname, "../");
312 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
313 symlinkname[sizeof(symlinkname) - 1] = '\0';
314 #endif /* USE_SYMLINKS */
316 code = remove(linkname);
318 code = unlink(linkname);
320 if (code != 0 && errno == ENOENT)
323 if (symlink(symlinkname, linkname) < 0)
325 if (link(filename, linkname) < 0)
326 #endif /* USE_SYMLINKS */
329 * If there wasn't anything there, and we cannot
330 * link to the target because it is the same as the
331 * target, then the source must be on a filesystem
332 * that uses caseless filenames, such as Win32, etc.
334 if (code == 0 && errno == EEXIST)
335 _nc_warning("can't link %s to %s", filename, linkname);
336 else if (code == 0 && errno == EPERM)
337 write_file(linkname, tp);
339 _nc_syserr_abort("can't link %s to %s", filename, linkname);
343 DEBUG(1, ("Linked %s", linkname));
346 #else /* just make copies */
347 write_file(linkname, tp);
348 #endif /* HAVE_LINK */
352 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
353 #define HI(x) ((x) / 256)
354 #define LO(x) ((x) % 256)
355 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x)
357 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1)
359 static int compute_offsets(char **Strings, int strmax, short *offsets)
364 for (i = 0; i < strmax; i++) {
365 if (Strings[i] == ABSENT_STRING) {
367 } else if (Strings[i] == CANCELLED_STRING) {
370 offsets[i] = nextfree;
371 nextfree += strlen(Strings[i]) + 1;
372 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree));
378 static void convert_shorts(unsigned char *buf, short *Numbers, int count)
381 for (i = 0; i < count; i++) {
382 if (Numbers[i] == -1) { /* HI/LO won't work */
383 buf[2*i] = buf[2*i + 1] = 0377;
384 } else if (Numbers[i] == -2) { /* HI/LO won't work */
388 LITTLE_ENDIAN(buf + 2*i, Numbers[i]);
389 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i]));
394 #define even_boundary(value) \
395 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1)
397 static int write_object(FILE *fp, TERMTYPE *tp)
400 size_t namelen, boolmax, nummax, strmax;
404 short offsets[MAX_ENTRY_SIZE/2];
405 unsigned char buf[MAX_ENTRY_SIZE];
407 namelist = tp->term_names;
408 namelen = strlen(namelist) + 1;
411 * BOOLWRITE, etc., are less than BOOLCOUNT because we store some
415 for (i = 0; i < BOOLWRITE; i++) {
421 for (i = 0; i < NUMWRITE; i++) {
422 if (tp->Numbers[i] != ABSENT_NUMERIC)
427 for (i = 0; i < STRWRITE; i++) {
428 if (tp->Strings[i] != ABSENT_STRING)
432 nextfree = compute_offsets(tp->Strings, strmax, offsets);
434 /* fill in the header */
435 LITTLE_ENDIAN(buf, MAGIC);
436 LITTLE_ENDIAN(buf+2, min(namelen, MAX_NAME_SIZE + 1));
437 LITTLE_ENDIAN(buf+4, boolmax);
438 LITTLE_ENDIAN(buf+6, nummax);
439 LITTLE_ENDIAN(buf+8, strmax);
440 LITTLE_ENDIAN(buf+10, nextfree);
442 /* write out the header */
443 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp)));
444 if (fwrite(buf, 12, 1, fp) != 1
445 || fwrite(namelist, sizeof(char), namelen, fp) != namelen
446 || fwrite(tp->Booleans, sizeof(char), boolmax, fp) != boolmax)
449 if (even_boundary(namelen+boolmax))
452 TRACE_OUT(("Numerics begin at %04lx", ftell(fp)));
455 convert_shorts(buf, tp->Numbers, nummax);
456 if (fwrite(buf, 2, nummax, fp) != nummax)
459 TRACE_OUT(("String offsets begin at %04lx", ftell(fp)));
461 /* the string offsets */
462 convert_shorts(buf, offsets, strmax);
463 if (fwrite(buf, 2, strmax, fp) != strmax)
466 TRACE_OUT(("String table begins at %04lx", ftell(fp)));
469 for (i = 0; i < strmax; i++)
470 if (VALID_STRING(tp->Strings[i]))
471 if (!WRITE_STRING(tp->Strings[i]))
475 if (NUM_EXT_NAMES(tp)) {
476 unsigned extcnt = NUM_EXT_NAMES(tp);
478 if (even_boundary(nextfree))
481 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets);
482 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
483 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings);
484 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
485 strmax = tp->ext_Strings + extcnt;
488 * Write the extended header
490 LITTLE_ENDIAN(buf+0, tp->ext_Booleans);
491 LITTLE_ENDIAN(buf+2, tp->ext_Numbers);
492 LITTLE_ENDIAN(buf+4, tp->ext_Strings);
493 LITTLE_ENDIAN(buf+6, strmax);
494 LITTLE_ENDIAN(buf+8, nextfree);
495 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp)));
496 if (fwrite(buf, 10, 1, fp) != 1)
499 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp)));
501 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char), tp->ext_Booleans, fp) != tp->ext_Booleans)
504 if (even_boundary(tp->ext_Booleans))
507 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp)));
508 if (tp->ext_Numbers) {
509 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers);
510 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers)
515 * Convert the offsets for the ext_Strings and ext_Names tables,
518 convert_shorts(buf, offsets, strmax);
519 TRACE_OUT(("WRITE offsets @%ld", ftell(fp)));
520 if (fwrite(buf, 2, strmax, fp) != strmax)
524 * Write the string table after the offset tables so we do not
525 * have to do anything about alignment.
527 for (i = 0; i < tp->ext_Strings; i++) {
528 if (VALID_STRING(tp->Strings[i+STRCOUNT])) {
529 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i, _nc_visbuf(tp->Strings[i+STRCOUNT])));
530 if (!WRITE_STRING(tp->Strings[i+STRCOUNT]))
536 * Write the extended names
538 for (i = 0; i < extcnt; i++) {
539 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i]));
540 if (!WRITE_STRING(tp->ext_Names[i]))
545 #endif /* NCURSES_XNAMES */
552 * Returns the total number of entries written by this process
554 int _nc_tic_written(void)
556 return total_written;