1 /* $OpenBSD: write_entry.c,v 1.4 1999/07/11 14:10:11 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 37 38 /* 39 * write_entry.c -- write a terminfo structure onto the file system 40 */ 41 42 #include <curses.priv.h> 43 44 #include <sys/stat.h> 45 46 #include <tic.h> 47 #include <term_entry.h> 48 49 #ifndef S_ISDIR 50 #define S_ISDIR(mode) ((mode & S_IFMT) == S_IFDIR) 51 #endif 52 53 #if 0 54 #define TRACE_OUT(p) DEBUG(2, p) 55 #else 56 #define TRACE_OUT(p) /*nothing*/ 57 #endif 58 59 MODULE_ID("$From: write_entry.c,v 1.47 1999/07/10 20:29:22 tom Exp $") 60 61 static int total_written; 62 63 static int write_object(FILE *, TERMTYPE *); 64 65 static void write_file(char *filename, TERMTYPE *tp) 66 { 67 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0; 68 if (fp == 0) { 69 perror(filename); 70 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename); 71 } 72 DEBUG(1, ("Created %s", filename)); 73 74 if (write_object(fp, tp) == ERR) { 75 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename); 76 } 77 fclose(fp); 78 } 79 80 /* 81 * make_directory(char *path) 82 * 83 * Make a directory if it doesn't exist. 84 */ 85 static int make_directory(const char *path) 86 { 87 int rc; 88 struct stat statbuf; 89 char fullpath[PATH_MAX]; 90 const char *destination = _nc_tic_dir(0); 91 92 if (path == destination || *path == '/') { 93 if (strlen(path) + 1 > sizeof(fullpath)) 94 return(-1); 95 (void)strcpy(fullpath, path); 96 } else { 97 if (strlen(destination) + strlen(path) + 2 > sizeof(fullpath)) 98 return(-1); 99 (void)sprintf(fullpath, "%s/%s", destination, path); 100 } 101 102 if ((rc = stat(path, &statbuf)) < 0) { 103 rc = mkdir(path, 0777); 104 } else { 105 if (_nc_access(path, R_OK|W_OK|X_OK) < 0) { 106 rc = -1; /* permission denied */ 107 } else if (!(S_ISDIR(statbuf.st_mode))) { 108 rc = -1; /* not a directory */ 109 } 110 } 111 return rc; 112 } 113 114 void _nc_set_writedir(char *dir) 115 /* set the write directory for compiled entries */ 116 { 117 const char *destination; 118 char actual[PATH_MAX]; 119 120 if (dir != 0) 121 (void) _nc_tic_dir(dir); 122 else if (getenv("TERMINFO") != NULL) 123 (void) _nc_tic_dir(getenv("TERMINFO")); 124 125 destination = _nc_tic_dir(0); 126 if (make_directory(destination) < 0) 127 { 128 char *home = _nc_home_terminfo(); 129 130 if (home != 0) { 131 destination = home; 132 if (make_directory(destination) < 0) 133 _nc_err_abort("%s: permission denied (errno %d)", 134 destination, errno); 135 } 136 } 137 138 /* 139 * Note: because of this code, this logic should be exercised 140 * *once only* per run. 141 */ 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)); 146 } 147 148 /* 149 * check_writeable(char code) 150 * 151 * Miscellaneous initialisations 152 * 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. 157 * 158 */ 159 160 static void check_writeable(int code) 161 { 162 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 163 static bool verified[sizeof(dirnames)]; 164 165 char dir[2]; 166 char *s; 167 168 if (code == 0 || (s = strchr(dirnames, code)) == 0) 169 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code); 170 171 if (verified[s-dirnames]) 172 return; 173 174 dir[0] = code; 175 dir[1] = '\0'; 176 if (make_directory(dir) < 0) { 177 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir); 178 } 179 180 verified[s-dirnames] = TRUE; 181 } 182 183 /* 184 * _nc_write_entry() 185 * 186 * Save the compiled version of a description in the filesystem. 187 * 188 * make a copy of the name-list 189 * break it up into first-name and all-but-last-name 190 * creat(first-name) 191 * write object information to first-name 192 * close(first-name) 193 * for each name in all-but-last-name 194 * link to first-name 195 * 196 * Using 'time()' to obtain a reference for file timestamps is unreliable, 197 * e.g., with NFS, because the filesystem may have a different time 198 * reference. We check for pre-existence of links by latching the first 199 * timestamp from a file that we create. 200 * 201 * The _nc_warning() calls will report a correct line number only if 202 * _nc_curr_line is properly set before the write_entry() call. 203 */ 204 205 void _nc_write_entry(TERMTYPE *const tp) 206 { 207 struct stat statbuf; 208 char name_list[MAX_TERMINFO_LENGTH]; 209 char *first_name, *other_names; 210 char *ptr; 211 char filename[PATH_MAX]; 212 char linkname[PATH_MAX]; 213 #if USE_SYMLINKS 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 */ 218 219 if (call_count++ == 0) { 220 start_time = 0; 221 } 222 223 (void) strcpy(name_list, tp->term_names); 224 DEBUG(7, ("Name list = '%s'", name_list)); 225 226 first_name = name_list; 227 228 ptr = &name_list[strlen(name_list) - 1]; 229 other_names = ptr + 1; 230 231 while (ptr > name_list && *ptr != '|') 232 ptr--; 233 234 if (ptr != name_list) { 235 *ptr = '\0'; 236 237 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) 238 continue; 239 240 if (*ptr == '\0') 241 other_names = ptr; 242 else { 243 *ptr = '\0'; 244 other_names = ptr + 1; 245 } 246 } 247 248 DEBUG(7, ("First name = '%s'", first_name)); 249 DEBUG(7, ("Other names = '%s'", other_names)); 250 251 _nc_set_type(first_name); 252 253 if (strlen(first_name) > sizeof(filename)-3) 254 _nc_warning("terminal name too long."); 255 256 sprintf(filename, "%c/%s", first_name[0], first_name); 257 258 /* 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, 261 * so warn the user. 262 */ 263 if (start_time > 0 && 264 stat(filename, &statbuf) >= 0 265 && statbuf.st_mtime >= start_time) 266 { 267 _nc_warning("name multiply defined."); 268 } 269 270 check_writeable(first_name[0]); 271 write_file(filename, tp); 272 273 if (start_time == 0) { 274 if (stat(filename, &statbuf) < 0 275 || (start_time = statbuf.st_mtime) == 0) { 276 _nc_syserr_abort("error obtaining time from %s/%s", 277 _nc_tic_dir(0), filename); 278 } 279 } 280 while (*other_names != '\0') { 281 ptr = other_names++; 282 while (*other_names != '|' && *other_names != '\0') 283 other_names++; 284 285 if (*other_names != '\0') 286 *(other_names++) = '\0'; 287 288 if (strlen(ptr) > sizeof(linkname)-3) { 289 _nc_warning("terminal alias %s too long.", ptr); 290 continue; 291 } 292 if (strchr(ptr, '/') != 0) { 293 _nc_warning("cannot link alias %s.", ptr); 294 continue; 295 } 296 297 check_writeable(ptr[0]); 298 sprintf(linkname, "%c/%s", ptr[0], ptr); 299 300 if (strcmp(filename, linkname) == 0) { 301 _nc_warning("self-synonym ignored"); 302 } 303 else if (stat(linkname, &statbuf) >= 0 && 304 statbuf.st_mtime < start_time) 305 { 306 _nc_warning("alias %s multiply defined.", ptr); 307 } 308 else if (_nc_access(linkname, W_OK) == 0) 309 #if HAVE_LINK 310 { 311 int code; 312 #if USE_SYMLINKS 313 strcpy(symlinkname, "../"); 314 strncat(symlinkname, filename, sizeof(symlinkname) - 4); 315 symlinkname[sizeof(symlinkname) - 1] = '\0'; 316 #endif /* USE_SYMLINKS */ 317 #if HAVE_REMOVE 318 code = remove(linkname); 319 #else 320 code = unlink(linkname); 321 #endif 322 if (code != 0 && errno == ENOENT) 323 code = 0; 324 #if USE_SYMLINKS 325 if (symlink(symlinkname, linkname) < 0) 326 #else 327 if (link(filename, linkname) < 0) 328 #endif /* USE_SYMLINKS */ 329 { 330 /* 331 * If there wasn't anything there, and we cannot 332 * link to the target because it is the same as the 333 * target, then the source must be on a filesystem 334 * that uses caseless filenames, such as Win32, etc. 335 */ 336 if (code == 0 && errno == EEXIST) 337 _nc_warning("can't link %s to %s", filename, linkname); 338 else if (code == 0 && errno == EPERM) 339 write_file(linkname, tp); 340 else 341 _nc_syserr_abort("can't link %s to %s", filename, linkname); 342 } 343 else 344 { 345 DEBUG(1, ("Linked %s", linkname)); 346 } 347 } 348 #else /* just make copies */ 349 write_file(linkname, tp); 350 #endif /* HAVE_LINK */ 351 } 352 } 353 354 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */ 355 #define HI(x) ((x) / 256) 356 #define LO(x) ((x) % 256) 357 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x) 358 359 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1) 360 361 static int compute_offsets(char **Strings, int strmax, short *offsets) 362 { 363 size_t nextfree = 0; 364 int i; 365 366 for (i = 0; i < strmax; i++) { 367 if (Strings[i] == ABSENT_STRING) { 368 offsets[i] = -1; 369 } else if (Strings[i] == CANCELLED_STRING) { 370 offsets[i] = -2; 371 } else { 372 offsets[i] = nextfree; 373 nextfree += strlen(Strings[i]) + 1; 374 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree)); 375 } 376 } 377 return nextfree; 378 } 379 380 static void convert_shorts(unsigned char *buf, short *Numbers, int count) 381 { 382 int i; 383 for (i = 0; i < count; i++) { 384 if (Numbers[i] == -1) { /* HI/LO won't work */ 385 buf[2*i] = buf[2*i + 1] = 0377; 386 } else if (Numbers[i] == -2) { /* HI/LO won't work */ 387 buf[2*i] = 0376; 388 buf[2*i + 1] = 0377; 389 } else { 390 LITTLE_ENDIAN(buf + 2*i, Numbers[i]); 391 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i])); 392 } 393 } 394 } 395 396 #define even_boundary(value) \ 397 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1) 398 399 static int write_object(FILE *fp, TERMTYPE *tp) 400 { 401 char *namelist; 402 size_t namelen, boolmax, nummax, strmax; 403 char zero = '\0'; 404 size_t i; 405 short nextfree; 406 short offsets[MAX_ENTRY_SIZE/2]; 407 unsigned char buf[MAX_ENTRY_SIZE]; 408 409 namelist = tp->term_names; 410 namelen = strlen(namelist) + 1; 411 412 /* 413 * BOOLWRITE, etc., are less than BOOLCOUNT because we store some 414 * values internally. 415 */ 416 boolmax = 0; 417 for (i = 0; i < BOOLWRITE; i++) { 418 if (tp->Booleans[i]) 419 boolmax = i+1; 420 } 421 422 nummax = 0; 423 for (i = 0; i < NUMWRITE; i++) { 424 if (tp->Numbers[i] != ABSENT_NUMERIC) 425 nummax = i+1; 426 } 427 428 strmax = 0; 429 for (i = 0; i < STRWRITE; i++) { 430 if (tp->Strings[i] != ABSENT_STRING) 431 strmax = i+1; 432 } 433 434 nextfree = compute_offsets(tp->Strings, strmax, offsets); 435 436 /* fill in the header */ 437 LITTLE_ENDIAN(buf, MAGIC); 438 LITTLE_ENDIAN(buf+2, min(namelen, MAX_NAME_SIZE + 1)); 439 LITTLE_ENDIAN(buf+4, boolmax); 440 LITTLE_ENDIAN(buf+6, nummax); 441 LITTLE_ENDIAN(buf+8, strmax); 442 LITTLE_ENDIAN(buf+10, nextfree); 443 444 /* write out the header */ 445 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp))); 446 if (fwrite(buf, 12, 1, fp) != 1 447 || fwrite(namelist, sizeof(char), namelen, fp) != namelen 448 || fwrite(tp->Booleans, sizeof(char), boolmax, fp) != boolmax) 449 return(ERR); 450 451 if (even_boundary(namelen+boolmax)) 452 return(ERR); 453 454 TRACE_OUT(("Numerics begin at %04lx", ftell(fp))); 455 456 /* the numerics */ 457 convert_shorts(buf, tp->Numbers, nummax); 458 if (fwrite(buf, 2, nummax, fp) != nummax) 459 return(ERR); 460 461 TRACE_OUT(("String offsets begin at %04lx", ftell(fp))); 462 463 /* the string offsets */ 464 convert_shorts(buf, offsets, strmax); 465 if (fwrite(buf, 2, strmax, fp) != strmax) 466 return(ERR); 467 468 TRACE_OUT(("String table begins at %04lx", ftell(fp))); 469 470 /* the strings */ 471 for (i = 0; i < strmax; i++) 472 if (VALID_STRING(tp->Strings[i])) 473 if (!WRITE_STRING(tp->Strings[i])) 474 return(ERR); 475 476 #if NCURSES_XNAMES 477 if (NUM_EXT_NAMES(tp)) { 478 unsigned extcnt = NUM_EXT_NAMES(tp); 479 480 if (even_boundary(nextfree)) 481 return(ERR); 482 483 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets); 484 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree)); 485 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings); 486 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree)); 487 strmax = tp->ext_Strings + extcnt; 488 489 /* 490 * Write the extended header 491 */ 492 LITTLE_ENDIAN(buf+0, tp->ext_Booleans); 493 LITTLE_ENDIAN(buf+2, tp->ext_Numbers); 494 LITTLE_ENDIAN(buf+4, tp->ext_Strings); 495 LITTLE_ENDIAN(buf+6, strmax); 496 LITTLE_ENDIAN(buf+8, nextfree); 497 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp))); 498 if (fwrite(buf, 10, 1, fp) != 1) 499 return(ERR); 500 501 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp))); 502 if (tp->ext_Booleans 503 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char), tp->ext_Booleans, fp) != tp->ext_Booleans) 504 return(ERR); 505 506 if (even_boundary(tp->ext_Booleans)) 507 return(ERR); 508 509 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp))); 510 if (tp->ext_Numbers) { 511 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers); 512 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers) 513 return(ERR); 514 } 515 516 /* 517 * Convert the offsets for the ext_Strings and ext_Names tables, 518 * in that order. 519 */ 520 convert_shorts(buf, offsets, strmax); 521 TRACE_OUT(("WRITE offsets @%ld", ftell(fp))); 522 if (fwrite(buf, 2, strmax, fp) != strmax) 523 return(ERR); 524 525 /* 526 * Write the string table after the offset tables so we do not 527 * have to do anything about alignment. 528 */ 529 for (i = 0; i < tp->ext_Strings; i++) { 530 if (VALID_STRING(tp->Strings[i+STRCOUNT])) { 531 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i, _nc_visbuf(tp->Strings[i+STRCOUNT]))); 532 if (!WRITE_STRING(tp->Strings[i+STRCOUNT])) 533 return(ERR); 534 } 535 } 536 537 /* 538 * Write the extended names 539 */ 540 for (i = 0; i < extcnt; i++) { 541 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i])); 542 if (!WRITE_STRING(tp->ext_Names[i])) 543 return(ERR); 544 } 545 546 } 547 #endif /* NCURSES_XNAMES */ 548 549 total_written++; 550 return(OK); 551 } 552 553 /* 554 * Returns the total number of entries written by this process 555 */ 556 int _nc_tic_written(void) 557 { 558 return total_written; 559 } 560