1 /* $OpenBSD: write_entry.c,v 1.3 1999/03/11 21:03:57 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.46 1999/03/06 22:48:21 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 132 if (make_directory(destination) < 0) 133 _nc_err_abort("%s: permission denied (errno %d)", 134 destination, errno); 135 136 destination = home; 137 } 138 } 139 140 /* 141 * Note: because of this code, this logic should be exercised 142 * *once only* per run. 143 */ 144 if (chdir(_nc_tic_dir(destination)) < 0 145 || getcwd(actual, sizeof(actual)) == 0) 146 _nc_err_abort("%s: not a directory", destination); 147 _nc_keep_tic_dir(strdup(actual)); 148 } 149 150 /* 151 * check_writeable(char code) 152 * 153 * Miscellaneous initialisations 154 * 155 * Check for access rights to destination directories 156 * Create any directories which don't exist. 157 * Note: there's no reason to return the result of make_directory(), since 158 * this function is called only in instances where that has to succeed. 159 * 160 */ 161 162 static void check_writeable(int code) 163 { 164 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 165 static bool verified[sizeof(dirnames)]; 166 167 char dir[2]; 168 char *s; 169 170 if (code == 0 || (s = strchr(dirnames, code)) == 0) 171 _nc_err_abort("Illegal terminfo subdirectory \"%c\"", code); 172 173 if (verified[s-dirnames]) 174 return; 175 176 dir[0] = code; 177 dir[1] = '\0'; 178 if (make_directory(dir) < 0) { 179 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir); 180 } 181 182 verified[s-dirnames] = TRUE; 183 } 184 185 /* 186 * _nc_write_entry() 187 * 188 * Save the compiled version of a description in the filesystem. 189 * 190 * make a copy of the name-list 191 * break it up into first-name and all-but-last-name 192 * creat(first-name) 193 * write object information to first-name 194 * close(first-name) 195 * for each name in all-but-last-name 196 * link to first-name 197 * 198 * Using 'time()' to obtain a reference for file timestamps is unreliable, 199 * e.g., with NFS, because the filesystem may have a different time 200 * reference. We check for pre-existence of links by latching the first 201 * timestamp from a file that we create. 202 * 203 * The _nc_warning() calls will report a correct line number only if 204 * _nc_curr_line is properly set before the write_entry() call. 205 */ 206 207 void _nc_write_entry(TERMTYPE *const tp) 208 { 209 struct stat statbuf; 210 char name_list[MAX_TERMINFO_LENGTH]; 211 char *first_name, *other_names; 212 char *ptr; 213 char filename[PATH_MAX]; 214 char linkname[PATH_MAX]; 215 #if USE_SYMLINKS 216 char symlinkname[PATH_MAX]; 217 #endif /* USE_SYMLINKS */ 218 static int call_count; 219 static time_t start_time; /* time at start of writes */ 220 221 if (call_count++ == 0) { 222 start_time = 0; 223 } 224 225 (void) strcpy(name_list, tp->term_names); 226 DEBUG(7, ("Name list = '%s'", name_list)); 227 228 first_name = name_list; 229 230 ptr = &name_list[strlen(name_list) - 1]; 231 other_names = ptr + 1; 232 233 while (ptr > name_list && *ptr != '|') 234 ptr--; 235 236 if (ptr != name_list) { 237 *ptr = '\0'; 238 239 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) 240 continue; 241 242 if (*ptr == '\0') 243 other_names = ptr; 244 else { 245 *ptr = '\0'; 246 other_names = ptr + 1; 247 } 248 } 249 250 DEBUG(7, ("First name = '%s'", first_name)); 251 DEBUG(7, ("Other names = '%s'", other_names)); 252 253 _nc_set_type(first_name); 254 255 if (strlen(first_name) > sizeof(filename)-3) 256 _nc_warning("terminal name too long."); 257 258 sprintf(filename, "%c/%s", first_name[0], first_name); 259 260 /* 261 * Has this primary name been written since the first call to 262 * write_entry()? If so, the newer write will step on the older, 263 * so warn the user. 264 */ 265 if (start_time > 0 && 266 stat(filename, &statbuf) >= 0 267 && statbuf.st_mtime >= start_time) 268 { 269 _nc_warning("name multiply defined."); 270 } 271 272 check_writeable(first_name[0]); 273 write_file(filename, tp); 274 275 if (start_time == 0) { 276 if (stat(filename, &statbuf) < 0 277 || (start_time = statbuf.st_mtime) == 0) { 278 _nc_syserr_abort("error obtaining time from %s/%s", 279 _nc_tic_dir(0), filename); 280 } 281 } 282 while (*other_names != '\0') { 283 ptr = other_names++; 284 while (*other_names != '|' && *other_names != '\0') 285 other_names++; 286 287 if (*other_names != '\0') 288 *(other_names++) = '\0'; 289 290 if (strlen(ptr) > sizeof(linkname)-3) { 291 _nc_warning("terminal alias %s too long.", ptr); 292 continue; 293 } 294 if (strchr(ptr, '/') != 0) { 295 _nc_warning("cannot link alias %s.", ptr); 296 continue; 297 } 298 299 check_writeable(ptr[0]); 300 sprintf(linkname, "%c/%s", ptr[0], ptr); 301 302 if (strcmp(filename, linkname) == 0) { 303 _nc_warning("self-synonym ignored"); 304 } 305 else if (stat(linkname, &statbuf) >= 0 && 306 statbuf.st_mtime < start_time) 307 { 308 _nc_warning("alias %s multiply defined.", ptr); 309 } 310 else if (_nc_access(linkname, W_OK) == 0) 311 #if HAVE_LINK 312 { 313 int code; 314 #if USE_SYMLINKS 315 strcpy(symlinkname, "../"); 316 strncat(symlinkname, filename, sizeof(symlinkname) - 4); 317 symlinkname[sizeof(symlinkname) - 1] = '\0'; 318 #endif /* USE_SYMLINKS */ 319 #if HAVE_REMOVE 320 code = remove(linkname); 321 #else 322 code = unlink(linkname); 323 #endif 324 if (code != 0 && errno == ENOENT) 325 code = 0; 326 #if USE_SYMLINKS 327 if (symlink(symlinkname, linkname) < 0) 328 #else 329 if (link(filename, linkname) < 0) 330 #endif /* USE_SYMLINKS */ 331 { 332 /* 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. 337 */ 338 if (code == 0 && errno == EEXIST) 339 _nc_warning("can't link %s to %s", filename, linkname); 340 else if (code == 0 && errno == EPERM) 341 write_file(linkname, tp); 342 else 343 _nc_syserr_abort("can't link %s to %s", filename, linkname); 344 } 345 else 346 { 347 DEBUG(1, ("Linked %s", linkname)); 348 } 349 } 350 #else /* just make copies */ 351 write_file(linkname, tp); 352 #endif /* HAVE_LINK */ 353 } 354 } 355 356 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */ 357 #define HI(x) ((x) / 256) 358 #define LO(x) ((x) % 256) 359 #define LITTLE_ENDIAN(p, x) (p)[0] = LO(x), (p)[1] = HI(x) 360 361 #define WRITE_STRING(str) (fwrite(str, sizeof(char), strlen(str) + 1, fp) == strlen(str) + 1) 362 363 static int compute_offsets(char **Strings, int strmax, short *offsets) 364 { 365 size_t nextfree = 0; 366 int i; 367 368 for (i = 0; i < strmax; i++) { 369 if (Strings[i] == ABSENT_STRING) { 370 offsets[i] = -1; 371 } else if (Strings[i] == CANCELLED_STRING) { 372 offsets[i] = -2; 373 } else { 374 offsets[i] = nextfree; 375 nextfree += strlen(Strings[i]) + 1; 376 TRACE_OUT(("put Strings[%d]=%s(%d)", i, _nc_visbuf(Strings[i]), nextfree)); 377 } 378 } 379 return nextfree; 380 } 381 382 static void convert_shorts(unsigned char *buf, short *Numbers, int count) 383 { 384 int i; 385 for (i = 0; i < count; i++) { 386 if (Numbers[i] == -1) { /* HI/LO won't work */ 387 buf[2*i] = buf[2*i + 1] = 0377; 388 } else if (Numbers[i] == -2) { /* HI/LO won't work */ 389 buf[2*i] = 0376; 390 buf[2*i + 1] = 0377; 391 } else { 392 LITTLE_ENDIAN(buf + 2*i, Numbers[i]); 393 TRACE_OUT(("put Numbers[%d]=%d", i, Numbers[i])); 394 } 395 } 396 } 397 398 #define even_boundary(value) \ 399 ((value) % 2 != 0 && fwrite(&zero, sizeof(char), 1, fp) != 1) 400 401 static int write_object(FILE *fp, TERMTYPE *tp) 402 { 403 char *namelist; 404 size_t namelen, boolmax, nummax, strmax; 405 char zero = '\0'; 406 size_t i; 407 short nextfree; 408 short offsets[MAX_ENTRY_SIZE/2]; 409 unsigned char buf[MAX_ENTRY_SIZE]; 410 411 namelist = tp->term_names; 412 namelen = strlen(namelist) + 1; 413 414 /* 415 * BOOLWRITE, etc., are less than BOOLCOUNT because we store some 416 * values internally. 417 */ 418 boolmax = 0; 419 for (i = 0; i < BOOLWRITE; i++) { 420 if (tp->Booleans[i]) 421 boolmax = i+1; 422 } 423 424 nummax = 0; 425 for (i = 0; i < NUMWRITE; i++) { 426 if (tp->Numbers[i] != ABSENT_NUMERIC) 427 nummax = i+1; 428 } 429 430 strmax = 0; 431 for (i = 0; i < STRWRITE; i++) { 432 if (tp->Strings[i] != ABSENT_STRING) 433 strmax = i+1; 434 } 435 436 nextfree = compute_offsets(tp->Strings, strmax, offsets); 437 438 /* fill in the header */ 439 LITTLE_ENDIAN(buf, MAGIC); 440 LITTLE_ENDIAN(buf+2, min(namelen, MAX_NAME_SIZE + 1)); 441 LITTLE_ENDIAN(buf+4, boolmax); 442 LITTLE_ENDIAN(buf+6, nummax); 443 LITTLE_ENDIAN(buf+8, strmax); 444 LITTLE_ENDIAN(buf+10, nextfree); 445 446 /* write out the header */ 447 TRACE_OUT(("Header of %s @%ld", namelist, ftell(fp))); 448 if (fwrite(buf, 12, 1, fp) != 1 449 || fwrite(namelist, sizeof(char), namelen, fp) != namelen 450 || fwrite(tp->Booleans, sizeof(char), boolmax, fp) != boolmax) 451 return(ERR); 452 453 if (even_boundary(namelen+boolmax)) 454 return(ERR); 455 456 TRACE_OUT(("Numerics begin at %04lx", ftell(fp))); 457 458 /* the numerics */ 459 convert_shorts(buf, tp->Numbers, nummax); 460 if (fwrite(buf, 2, nummax, fp) != nummax) 461 return(ERR); 462 463 TRACE_OUT(("String offsets begin at %04lx", ftell(fp))); 464 465 /* the string offsets */ 466 convert_shorts(buf, offsets, strmax); 467 if (fwrite(buf, 2, strmax, fp) != strmax) 468 return(ERR); 469 470 TRACE_OUT(("String table begins at %04lx", ftell(fp))); 471 472 /* the strings */ 473 for (i = 0; i < strmax; i++) 474 if (VALID_STRING(tp->Strings[i])) 475 if (!WRITE_STRING(tp->Strings[i])) 476 return(ERR); 477 478 #if NCURSES_XNAMES 479 if (NUM_EXT_NAMES(tp)) { 480 unsigned extcnt = NUM_EXT_NAMES(tp); 481 482 if (even_boundary(nextfree)) 483 return(ERR); 484 485 nextfree = compute_offsets(tp->Strings + STRCOUNT, tp->ext_Strings, offsets); 486 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree)); 487 nextfree += compute_offsets(tp->ext_Names, extcnt, offsets + tp->ext_Strings); 488 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree)); 489 strmax = tp->ext_Strings + extcnt; 490 491 /* 492 * Write the extended header 493 */ 494 LITTLE_ENDIAN(buf+0, tp->ext_Booleans); 495 LITTLE_ENDIAN(buf+2, tp->ext_Numbers); 496 LITTLE_ENDIAN(buf+4, tp->ext_Strings); 497 LITTLE_ENDIAN(buf+6, strmax); 498 LITTLE_ENDIAN(buf+8, nextfree); 499 TRACE_OUT(("WRITE extended-header @%ld", ftell(fp))); 500 if (fwrite(buf, 10, 1, fp) != 1) 501 return(ERR); 502 503 TRACE_OUT(("WRITE %d booleans @%ld", tp->ext_Booleans, ftell(fp))); 504 if (tp->ext_Booleans 505 && fwrite(tp->Booleans + BOOLCOUNT, sizeof(char), tp->ext_Booleans, fp) != tp->ext_Booleans) 506 return(ERR); 507 508 if (even_boundary(tp->ext_Booleans)) 509 return(ERR); 510 511 TRACE_OUT(("WRITE %d numbers @%ld", tp->ext_Numbers, ftell(fp))); 512 if (tp->ext_Numbers) { 513 convert_shorts(buf, tp->Numbers + NUMCOUNT, tp->ext_Numbers); 514 if (fwrite(buf, 2, tp->ext_Numbers, fp) != tp->ext_Numbers) 515 return(ERR); 516 } 517 518 /* 519 * Convert the offsets for the ext_Strings and ext_Names tables, 520 * in that order. 521 */ 522 convert_shorts(buf, offsets, strmax); 523 TRACE_OUT(("WRITE offsets @%ld", ftell(fp))); 524 if (fwrite(buf, 2, strmax, fp) != strmax) 525 return(ERR); 526 527 /* 528 * Write the string table after the offset tables so we do not 529 * have to do anything about alignment. 530 */ 531 for (i = 0; i < tp->ext_Strings; i++) { 532 if (VALID_STRING(tp->Strings[i+STRCOUNT])) { 533 TRACE_OUT(("WRITE ext_Strings[%d]=%s", i, _nc_visbuf(tp->Strings[i+STRCOUNT]))); 534 if (!WRITE_STRING(tp->Strings[i+STRCOUNT])) 535 return(ERR); 536 } 537 } 538 539 /* 540 * Write the extended names 541 */ 542 for (i = 0; i < extcnt; i++) { 543 TRACE_OUT(("WRITE ext_Names[%d]=%s", i, tp->ext_Names[i])); 544 if (!WRITE_STRING(tp->ext_Names[i])) 545 return(ERR); 546 } 547 548 } 549 #endif /* NCURSES_XNAMES */ 550 551 total_written++; 552 return(OK); 553 } 554 555 /* 556 * Returns the total number of entries written by this process 557 */ 558 int _nc_tic_written(void) 559 { 560 return total_written; 561 } 562