1 /* $OpenBSD: write_entry.c,v 1.14 2023/10/17 09:52:09 nicm Exp $ */
2
3 /****************************************************************************
4 * Copyright 2018-2022,2023 Thomas E. Dickey *
5 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
6 * *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
29 * authorization. *
30 ****************************************************************************/
31
32 /****************************************************************************
33 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
34 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
35 * and: Thomas E. Dickey 1996-on *
36 ****************************************************************************/
37
38 /*
39 * write_entry.c -- write a terminfo structure onto the file system
40 */
41
42 #include <curses.priv.h>
43 #include <hashed_db.h>
44
45 #include <tic.h>
46
47 MODULE_ID("$Id: write_entry.c,v 1.14 2023/10/17 09:52:09 nicm Exp $")
48
49 #if 1
50 #define TRACE_OUT(p) DEBUG(2, p)
51 #define TRACE_NUM(n) if (VALID_NUMERIC(Numbers[n])) { \
52 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) (n), Numbers[n])); }
53 #else
54 #define TRACE_OUT(p) /*nothing */
55 #define TRACE_NUM(n) /* nothing */
56 #endif
57
58 /*
59 * FIXME: special case to work around Cygwin bug in link(), which updates
60 * the target file's timestamp.
61 */
62 #if HAVE_LINK && !USE_SYMLINKS && !MIXEDCASE_FILENAMES && defined(__CYGWIN__)
63 #define LINK_TOUCHES 1
64 #else
65 #define LINK_TOUCHES 0
66 #endif
67
68 static int total_written;
69 static int total_parts;
70 static int total_size;
71
72 static int make_db_root(const char *);
73
74 #if !USE_HASHED_DB
75 static void
write_file(char * filename,TERMTYPE2 * tp)76 write_file(char *filename, TERMTYPE2 *tp)
77 {
78 char buffer[MAX_ENTRY_SIZE];
79 unsigned limit = sizeof(buffer);
80 unsigned offset = 0;
81
82 if (_nc_write_object(tp, buffer, &offset, limit) == ERR) {
83 _nc_warning("entry is larger than %u bytes", limit);
84 } else {
85 FILE *fp = ((_nc_access(filename, W_OK) == 0)
86 ? safe_fopen(filename, BIN_W)
87 : 0);
88 size_t actual;
89
90 if (fp == 0) {
91 perror(filename);
92 _nc_syserr_abort("cannot open %s/%s", _nc_tic_dir(0), filename);
93 }
94
95 actual = fwrite(buffer, sizeof(char), (size_t) offset, fp);
96 if (actual != offset) {
97 int myerr = ferror(fp) ? errno : 0;
98 if (myerr) {
99 _nc_syserr_abort("error writing %s/%s: %s",
100 _nc_tic_dir(NULL),
101 filename,
102 strerror(myerr));
103 } else {
104 _nc_syserr_abort("error writing %s/%s: %u bytes vs actual %lu",
105 _nc_tic_dir(NULL),
106 filename,
107 offset,
108 (unsigned long) actual);
109 }
110 } else {
111 fclose(fp);
112 DEBUG(1, ("Created %s", filename));
113 }
114 }
115 }
116
117 /*
118 * Check for access rights to destination directories
119 * Create any directories which don't exist.
120 *
121 * Note: there's no reason to return the result of make_db_root(), since
122 * this function is called only in instances where that has to succeed.
123 */
124 static void
check_writeable(int code)125 check_writeable(int code)
126 {
127 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
128 static bool verified[sizeof(dirnames)];
129
130 char dir[sizeof(LEAF_FMT)];
131 char *s = 0;
132
133 if (code == 0 || (s = (strchr) (dirnames, code)) == 0) {
134 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
135 } else if (!verified[s - dirnames]) {
136 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
137 if (make_db_root(dir) < 0) {
138 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(NULL), dir);
139 } else {
140 verified[s - dirnames] = TRUE;
141 }
142 }
143 }
144 #endif /* !USE_HASHED_DB */
145
146 static int
make_db_path(char * dst,const char * src,size_t limit)147 make_db_path(char *dst, const char *src, size_t limit)
148 {
149 int rc = -1;
150 const char *top = _nc_tic_dir(NULL);
151
152 if (src == top || _nc_is_abs_path(src)) {
153 if (strlen(src) + 1 <= limit) {
154 _nc_STRCPY(dst, src, limit);
155 rc = 0;
156 }
157 } else {
158 if ((strlen(top) + strlen(src) + 6) <= limit) {
159 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
160 rc = 0;
161 }
162 }
163 #if USE_HASHED_DB
164 if (rc == 0) {
165 static const char suffix[] = DBM_SUFFIX;
166 size_t have = strlen(dst);
167 size_t need = strlen(suffix);
168 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
169 if (have + need <= limit) {
170 _nc_STRCAT(dst, suffix, limit);
171 } else {
172 rc = -1;
173 }
174 } else if (_nc_is_dir_path(dst)) {
175 rc = -1;
176 }
177 }
178 #endif
179 return rc;
180 }
181
182 /*
183 * Make a database-root if it doesn't exist.
184 */
185 static int
make_db_root(const char * path)186 make_db_root(const char *path)
187 {
188 int rc;
189 char fullpath[PATH_MAX];
190
191 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
192 #if USE_HASHED_DB
193 DB *capdbp;
194
195 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
196 rc = -1;
197 } else if (_nc_db_close(capdbp) < 0) {
198 rc = -1;
199 }
200 #else
201 struct stat statbuf;
202
203 if ((rc = stat(path, &statbuf)) == -1) {
204 rc = mkdir(path
205 #ifndef _NC_WINDOWS
206 ,0777
207 #endif
208 );
209 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
210 rc = -1; /* permission denied */
211 } else if (!(S_ISDIR(statbuf.st_mode))) {
212 rc = -1; /* not a directory */
213 }
214 #endif
215 }
216 return rc;
217 }
218
219 /*
220 * Set the write directory for compiled entries.
221 */
222 NCURSES_EXPORT(void)
_nc_set_writedir(const char * dir)223 _nc_set_writedir(const char *dir)
224 {
225 const char *destination;
226 char actual[PATH_MAX];
227 bool specific = (dir != NULL);
228
229 if (!specific && use_terminfo_vars())
230 dir = getenv("TERMINFO");
231
232 if (dir != NULL)
233 (void) _nc_tic_dir(dir);
234
235 destination = _nc_tic_dir(NULL);
236 if (make_db_root(destination) < 0) {
237 bool success = FALSE;
238
239 if (!specific) {
240 char *home = _nc_home_terminfo();
241
242 if (home != NULL) {
243 destination = home;
244 if (make_db_root(destination) == 0)
245 success = TRUE;
246 }
247 }
248 if (!success) {
249 _nc_err_abort("%s: permission denied (errno %d)",
250 destination, errno);
251 }
252 }
253
254 /*
255 * Note: because of this code, this logic should be exercised
256 * *once only* per run.
257 */
258 #if USE_HASHED_DB
259 make_db_path(actual, destination, sizeof(actual));
260 #else
261 if (chdir(_nc_tic_dir(destination)) < 0
262 || getcwd(actual, sizeof(actual)) == NULL)
263 _nc_err_abort("%s: not a directory", destination);
264 #endif
265 _nc_keep_tic_dir(actual);
266 }
267
268 /*
269 * Save the compiled version of a description in the filesystem.
270 *
271 * make a copy of the name-list
272 * break it up into first-name and all-but-last-name
273 * creat(first-name)
274 * write object information to first-name
275 * close(first-name)
276 * for each name in all-but-last-name
277 * link to first-name
278 *
279 * Using 'time()' to obtain a reference for file timestamps is unreliable,
280 * e.g., with NFS, because the filesystem may have a different time
281 * reference. We check for pre-existence of links by latching the first
282 * timestamp from a file that we create.
283 *
284 * The _nc_warning() calls will report a correct line number only if
285 * _nc_curr_line is properly set before the write_entry() call.
286 */
287
288 NCURSES_EXPORT(void)
_nc_write_entry(TERMTYPE2 * const tp)289 _nc_write_entry(TERMTYPE2 *const tp)
290 {
291 #if USE_HASHED_DB
292
293 char buffer[MAX_ENTRY_SIZE + 1];
294 unsigned limit = sizeof(buffer);
295 unsigned offset = 0;
296
297 #else /* !USE_HASHED_DB */
298
299 struct stat statbuf;
300 char filename[PATH_MAX];
301 char linkname[PATH_MAX];
302 #if USE_SYMLINKS
303 char symlinkname[PATH_MAX];
304 #if !HAVE_LINK
305 #undef HAVE_LINK
306 #define HAVE_LINK 1
307 #endif
308 #endif /* USE_SYMLINKS */
309
310 unsigned limit2 = sizeof(filename) - (2 + LEAF_LEN);
311 char saved = '\0';
312
313 static int call_count;
314 static time_t start_time; /* time at start of writes */
315
316 #endif /* USE_HASHED_DB */
317
318 char name_list[MAX_TERMINFO_LENGTH];
319 char *first_name, *other_names;
320 char *ptr;
321 char *term_names = tp->term_names;
322 size_t name_size = strlen(term_names);
323
324 if (name_size == 0) {
325 _nc_syserr_abort("no terminal name found.");
326 } else if (name_size >= sizeof(name_list) - 1) {
327 _nc_syserr_abort("terminal name too long: %s", term_names);
328 }
329
330 _nc_STRCPY(name_list, term_names, sizeof(name_list));
331 DEBUG(7, ("Name list = '%s'", name_list));
332
333 first_name = name_list;
334
335 ptr = &name_list[name_size - 1];
336 other_names = ptr + 1;
337
338 while (ptr > name_list && *ptr != '|')
339 ptr--;
340
341 if (ptr != name_list) {
342 *ptr = '\0';
343
344 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++) {
345 /* EMPTY */ ;
346 }
347
348 if (*ptr == '\0')
349 other_names = ptr;
350 else {
351 *ptr = '\0';
352 other_names = ptr + 1;
353 }
354 }
355
356 DEBUG(7, ("First name = '%s'", first_name));
357 DEBUG(7, ("Other names = '%s'", other_names));
358
359 _nc_set_type(first_name);
360
361 #if USE_HASHED_DB
362 if (_nc_write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
363 DB *capdb = _nc_db_open(_nc_tic_dir(NULL), TRUE);
364 DBT key, data;
365
366 if (capdb != NULL) {
367 buffer[0] = 0;
368
369 memset(&key, 0, sizeof(key));
370 key.data = term_names;
371 key.size = name_size;
372
373 memset(&data, 0, sizeof(data));
374 data.data = buffer;
375 data.size = offset + 1;
376
377 _nc_db_put(capdb, &key, &data);
378
379 buffer[0] = 2;
380
381 key.data = name_list;
382 key.size = strlen(name_list);
383
384 _nc_STRCPY(buffer + 1,
385 term_names,
386 sizeof(buffer) - 1);
387 data.size = name_size + 1;
388
389 total_size += data.size;
390 total_parts++;
391 _nc_db_put(capdb, &key, &data);
392
393 while (*other_names != '\0') {
394 ptr = other_names++;
395 assert(ptr < buffer + sizeof(buffer) - 1);
396 while (*other_names != '|' && *other_names != '\0')
397 other_names++;
398
399 if (*other_names != '\0')
400 *(other_names++) = '\0';
401
402 key.data = ptr;
403 key.size = strlen(ptr);
404
405 total_size += data.size;
406 total_parts++;
407 _nc_db_put(capdb, &key, &data);
408 }
409 }
410 }
411 #else /* !USE_HASHED_DB */
412 if (call_count++ == 0) {
413 start_time = 0;
414 }
415
416 if (strlen(first_name) >= limit2) {
417 _nc_warning("terminal name too long.");
418 saved = first_name[limit2];
419 first_name[limit2] = '\0';
420 }
421
422 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
423 LEAF_FMT "/%.*s", UChar(first_name[0]),
424 (int) (sizeof(filename) - (LEAF_LEN + 2)),
425 first_name);
426
427 if (saved)
428 first_name[limit2] = saved;
429
430 /*
431 * Has this primary name been written since the first call to
432 * write_entry()? If so, the newer write will step on the older,
433 * so warn the user.
434 */
435 if (start_time > 0 &&
436 stat(filename, &statbuf) >= 0
437 && statbuf.st_mtime >= start_time) {
438 #if HAVE_LINK && !USE_SYMLINKS
439 /*
440 * If the file has more than one link, the reason for the previous
441 * write could be that the current primary name used to be an alias for
442 * the previous entry. In that case, unlink the file so that we will
443 * not modify the previous entry as we write this one.
444 */
445 if (statbuf.st_nlink > 1) {
446 _nc_warning("name redefined.");
447 unlink(filename);
448 } else {
449 _nc_warning("name multiply defined.");
450 }
451 #else
452 _nc_warning("name multiply defined.");
453 #endif
454 }
455
456 check_writeable(first_name[0]);
457 write_file(filename, tp);
458
459 if (start_time == 0) {
460 if (stat(filename, &statbuf) == -1
461 || (start_time = statbuf.st_mtime) == 0) {
462 _nc_syserr_abort("error obtaining time from %s/%s",
463 _nc_tic_dir(NULL), filename);
464 }
465 }
466 while (*other_names != '\0') {
467 ptr = other_names++;
468 while (*other_names != '|' && *other_names != '\0')
469 other_names++;
470
471 if (*other_names != '\0')
472 *(other_names++) = '\0';
473
474 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
475 _nc_warning("terminal alias %s too long.", ptr);
476 continue;
477 }
478 if (strchr(ptr, '/') != NULL) {
479 _nc_warning("cannot link alias %s.", ptr);
480 continue;
481 }
482
483 check_writeable(ptr[0]);
484 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
485 LEAF_FMT "/%.*s", ptr[0],
486 (int) sizeof(linkname) - (2 + LEAF_LEN), ptr);
487
488 if (strcmp(filename, linkname) == 0) {
489 _nc_warning("self-synonym ignored");
490 }
491 #if !LINK_TOUCHES
492 else if (stat(linkname, &statbuf) >= 0 &&
493 statbuf.st_mtime < start_time) {
494 _nc_warning("alias %s multiply defined.", ptr);
495 }
496 #endif
497 else if (_nc_access(linkname, W_OK) == 0)
498 #if HAVE_LINK
499 {
500 int code;
501 #if USE_SYMLINKS
502 #define MY_SIZE sizeof(symlinkname) - 1
503 if (first_name[0] == linkname[0]) {
504 _nc_STRNCPY(symlinkname, first_name, MY_SIZE);
505 } else {
506 _nc_STRCPY(symlinkname, "../", sizeof(symlinkname));
507 _nc_STRNCPY(symlinkname + 3, filename, MY_SIZE - 3);
508 }
509 symlinkname[MY_SIZE] = '\0';
510 #endif /* USE_SYMLINKS */
511 #if HAVE_REMOVE
512 code = remove(linkname);
513 #else
514 code = unlink(linkname);
515 #endif
516 if (code != 0 && errno == ENOENT)
517 code = 0;
518 #if USE_SYMLINKS
519 if (symlink(symlinkname, linkname) < 0)
520 #else
521 if (link(filename, linkname) < 0)
522 #endif /* USE_SYMLINKS */
523 {
524 /*
525 * If there wasn't anything there, and we cannot
526 * link to the target because it is the same as the
527 * target, then the source must be on a filesystem
528 * that uses caseless filenames, such as Win32, etc.
529 */
530 if (code == 0 && errno == EEXIST)
531 _nc_warning("can't link %s to %s", filename, linkname);
532 else if (code == 0 && (errno == EPERM || errno == ENOENT))
533 write_file(linkname, tp);
534 else {
535 #if MIXEDCASE_FILENAMES
536 _nc_syserr_abort("cannot link %s to %s", filename, linkname);
537 #else
538 _nc_warning("cannot link %s to %s (errno=%d)", filename,
539 linkname, errno);
540 #endif
541 }
542 } else {
543 DEBUG(1, ("Linked %s", linkname));
544 }
545 }
546 #else /* just make copies */
547 write_file(linkname, tp);
548 #endif /* HAVE_LINK */
549 }
550 #endif /* USE_HASHED_DB */
551 }
552
553 static size_t
fake_write(char * dst,unsigned * offset,size_t limit,char * src,size_t want,size_t size)554 fake_write(char *dst,
555 unsigned *offset,
556 size_t limit,
557 char *src,
558 size_t want,
559 size_t size)
560 {
561 size_t have = (limit - *offset);
562
563 want *= size;
564 if (have > 0) {
565 if (want > have)
566 want = have;
567 memcpy(dst + *offset, src, want);
568 *offset += (unsigned) want;
569 } else {
570 want = 0;
571 }
572 return (want / size);
573 }
574
575 #define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
576
577 #undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
578 #define HI(x) ((x) / 256)
579 #define LO(x) ((x) % 256)
580 #define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
581 (p)[1] = (unsigned char)HI(x)
582
583 #define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
584
585 static int
compute_offsets(char ** Strings,size_t strmax,short * offsets)586 compute_offsets(char **Strings, size_t strmax, short *offsets)
587 {
588 int nextfree = 0;
589 size_t i;
590
591 for (i = 0; i < strmax; i++) {
592 if (Strings[i] == ABSENT_STRING) {
593 offsets[i] = -1;
594 } else if (Strings[i] == CANCELLED_STRING) {
595 offsets[i] = -2;
596 } else {
597 offsets[i] = (short) nextfree;
598 nextfree += (int) strlen(Strings[i]) + 1;
599 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
600 _nc_visbuf(Strings[i]), (int) nextfree));
601 }
602 }
603 return nextfree;
604 }
605
606 static size_t
convert_shorts(unsigned char * buf,short * Numbers,size_t count)607 convert_shorts(unsigned char *buf, short *Numbers, size_t count)
608 {
609 size_t i;
610 for (i = 0; i < count; i++) {
611 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
612 buf[2 * i] = buf[2 * i + 1] = 0377;
613 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
614 buf[2 * i] = 0376;
615 buf[2 * i + 1] = 0377;
616 } else {
617 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
618 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
619 }
620 }
621 return SIZEOF_SHORT;
622 }
623
624 #if NCURSES_EXT_NUMBERS
625 static size_t
convert_16bit(unsigned char * buf,NCURSES_INT2 * Numbers,size_t count)626 convert_16bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
627 {
628 size_t i, j;
629 size_t size = SIZEOF_SHORT;
630 for (i = 0; i < count; i++) {
631 unsigned value = (unsigned) Numbers[i];
632 TRACE_NUM(i);
633 for (j = 0; j < size; ++j) {
634 *buf++ = value & 0xff;
635 value >>= 8;
636 }
637 }
638 return size;
639 }
640
641 static size_t
convert_32bit(unsigned char * buf,NCURSES_INT2 * Numbers,size_t count)642 convert_32bit(unsigned char *buf, NCURSES_INT2 *Numbers, size_t count)
643 {
644 size_t i, j;
645 size_t size = SIZEOF_INT2;
646 for (i = 0; i < count; i++) {
647 unsigned value = (unsigned) Numbers[i];
648 TRACE_NUM(i);
649 for (j = 0; j < size; ++j) {
650 *buf++ = value & 0xff;
651 value >>= 8;
652 }
653 }
654 return size;
655 }
656 #endif
657
658 #define even_boundary(value) \
659 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
660
661 #if NCURSES_XNAMES
662 static unsigned
extended_Booleans(TERMTYPE2 * tp)663 extended_Booleans(TERMTYPE2 *tp)
664 {
665 unsigned result = 0;
666 unsigned i;
667
668 for (i = 0; i < tp->ext_Booleans; ++i) {
669 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
670 result = (i + 1);
671 }
672 return result;
673 }
674
675 static unsigned
extended_Numbers(TERMTYPE2 * tp)676 extended_Numbers(TERMTYPE2 *tp)
677 {
678 unsigned result = 0;
679 unsigned i;
680
681 for (i = 0; i < tp->ext_Numbers; ++i) {
682 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
683 result = (i + 1);
684 }
685 return result;
686 }
687
688 static unsigned
extended_Strings(TERMTYPE2 * tp)689 extended_Strings(TERMTYPE2 *tp)
690 {
691 unsigned short result = 0;
692 unsigned short i;
693
694 for (i = 0; i < tp->ext_Strings; ++i) {
695 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
696 result = (unsigned short) (i + 1);
697 }
698 return result;
699 }
700
701 /*
702 * _nc_align_termtype() will extend entries that are referenced in a use=
703 * clause - discard the unneeded data.
704 */
705 static bool
extended_object(TERMTYPE2 * tp)706 extended_object(TERMTYPE2 *tp)
707 {
708 bool result = FALSE;
709
710 if (_nc_user_definable) {
711 result = ((extended_Booleans(tp)
712 + extended_Numbers(tp)
713 + extended_Strings(tp)) != 0);
714 }
715 return result;
716 }
717 #endif
718
719 NCURSES_EXPORT(int)
_nc_write_object(TERMTYPE2 * tp,char * buffer,unsigned * offset,unsigned limit)720 _nc_write_object(TERMTYPE2 *tp, char *buffer, unsigned *offset, unsigned limit)
721 {
722 char *namelist;
723 size_t namelen, boolmax, nummax, strmax, numlen;
724 char zero = '\0';
725 size_t i;
726 int nextfree;
727 short offsets[MAX_ENTRY_SIZE / 2];
728 unsigned char buf[MAX_ENTRY_SIZE];
729 unsigned last_bool = BOOLWRITE;
730 unsigned last_num = NUMWRITE;
731 unsigned last_str = STRWRITE;
732 #if NCURSES_EXT_NUMBERS
733 bool need_ints = FALSE;
734 size_t (*convert_numbers) (unsigned char *, NCURSES_INT2 *, size_t);
735 #else
736 #define convert_numbers convert_shorts
737 #endif
738
739 #if NCURSES_XNAMES
740 /*
741 * Normally we limit the list of values to exclude the "obsolete"
742 * capabilities. However, if we are accepting extended names, add
743 * these as well, since they are used for supporting translation
744 * to/from termcap.
745 */
746 if (_nc_user_definable) {
747 last_bool = BOOLCOUNT;
748 last_num = NUMCOUNT;
749 last_str = STRCOUNT;
750 }
751 #endif
752
753 namelist = tp->term_names;
754 namelen = strlen(namelist) + 1;
755
756 boolmax = 0;
757 for (i = 0; i < last_bool; i++) {
758 if (tp->Booleans[i] == TRUE) {
759 boolmax = i + 1;
760 }
761 }
762
763 nummax = 0;
764 for (i = 0; i < last_num; i++) {
765 if (tp->Numbers[i] != ABSENT_NUMERIC) {
766 nummax = i + 1;
767 #if NCURSES_EXT_NUMBERS
768 if (tp->Numbers[i] > MAX_OF_TYPE(NCURSES_COLOR_T)) {
769 need_ints = TRUE;
770 }
771 #endif
772 }
773 }
774
775 strmax = 0;
776 for (i = 0; i < last_str; i++) {
777 if (tp->Strings[i] != ABSENT_STRING)
778 strmax = i + 1;
779 }
780
781 nextfree = compute_offsets(tp->Strings, strmax, offsets);
782
783 /* fill in the header */
784 #if NCURSES_EXT_NUMBERS
785 if (need_ints) {
786 convert_numbers = convert_32bit;
787 LITTLE_ENDIAN(buf, MAGIC2);
788 } else {
789 convert_numbers = convert_16bit;
790 LITTLE_ENDIAN(buf, MAGIC);
791 }
792 #else
793 LITTLE_ENDIAN(buf, MAGIC);
794 #endif
795 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
796 LITTLE_ENDIAN(buf + 4, boolmax);
797 LITTLE_ENDIAN(buf + 6, nummax);
798 LITTLE_ENDIAN(buf + 8, strmax);
799 LITTLE_ENDIAN(buf + 10, nextfree);
800
801 /* write out the header */
802 TRACE_OUT(("Header of %s @%d", namelist, *offset));
803 if (Write(buf, 12, 1) != 1
804 || Write(namelist, sizeof(char), namelen) != namelen) {
805 return (ERR);
806 }
807
808 for (i = 0; i < boolmax; i++) {
809 if (tp->Booleans[i] == TRUE) {
810 buf[i] = TRUE;
811 } else {
812 buf[i] = FALSE;
813 }
814 }
815 if (Write(buf, sizeof(char), boolmax) != boolmax) {
816 return (ERR);
817 }
818
819 if (even_boundary(namelen + boolmax)) {
820 return (ERR);
821 }
822
823 TRACE_OUT(("Numerics begin at %04x", *offset));
824
825 /* the numerics */
826 numlen = convert_numbers(buf, tp->Numbers, nummax);
827 if (Write(buf, numlen, nummax) != nummax) {
828 return (ERR);
829 }
830
831 TRACE_OUT(("String offsets begin at %04x", *offset));
832
833 /* the string offsets */
834 convert_shorts(buf, offsets, strmax);
835 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
836 return (ERR);
837 }
838
839 TRACE_OUT(("String table begins at %04x", *offset));
840
841 /* the strings */
842 for (i = 0; i < strmax; i++) {
843 if (VALID_STRING(tp->Strings[i])) {
844 if (!WRITE_STRING(tp->Strings[i])) {
845 return (ERR);
846 }
847 }
848 }
849
850 #if NCURSES_XNAMES
851 if (extended_object(tp)) {
852 unsigned ext_total = (unsigned) NUM_EXT_NAMES(tp);
853 unsigned ext_usage = ext_total;
854
855 if (even_boundary(nextfree)) {
856 return (ERR);
857 }
858
859 nextfree = compute_offsets(tp->Strings + STRCOUNT,
860 (size_t) tp->ext_Strings,
861 offsets);
862 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
863
864 if (tp->ext_Strings >= SIZEOF(offsets)) {
865 return (ERR);
866 }
867
868 nextfree += compute_offsets(tp->ext_Names,
869 (size_t) ext_total,
870 offsets + tp->ext_Strings);
871 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
872 strmax = tp->ext_Strings + ext_total;
873 for (i = 0; i < tp->ext_Strings; ++i) {
874 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
875 ext_usage++;
876 }
877 }
878 TRACE_OUT(("will write %u/%lu strings", ext_usage, (unsigned long) strmax));
879
880 /*
881 * Write the extended header
882 */
883 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
884 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
885 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
886 LITTLE_ENDIAN(buf + 6, ext_usage);
887 LITTLE_ENDIAN(buf + 8, nextfree);
888 TRACE_OUT(("WRITE extended-header @%d", *offset));
889 if (Write(buf, 10, 1) != 1) {
890 return (ERR);
891 }
892
893 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
894 if (tp->ext_Booleans
895 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
896 tp->ext_Booleans) != tp->ext_Booleans) {
897 return (ERR);
898 }
899
900 if (even_boundary(tp->ext_Booleans)) {
901 return (ERR);
902 }
903
904 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
905 if (tp->ext_Numbers) {
906 numlen = convert_numbers(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
907 if (Write(buf, numlen, tp->ext_Numbers) != tp->ext_Numbers) {
908 return (ERR);
909 }
910 }
911
912 /*
913 * Convert the offsets for the ext_Strings and ext_Names tables,
914 * in that order.
915 */
916 convert_shorts(buf, offsets, strmax);
917 TRACE_OUT(("WRITE offsets @%d", *offset));
918 if (Write(buf, SIZEOF_SHORT, strmax) != strmax) {
919 return (ERR);
920 }
921
922 /*
923 * Write the string table after the offset tables so we do not
924 * have to do anything about alignment.
925 */
926 for (i = 0; i < tp->ext_Strings; i++) {
927 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
928 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
929 _nc_visbuf(tp->Strings[i + STRCOUNT])));
930 if (!WRITE_STRING(tp->Strings[i + STRCOUNT])) {
931 return (ERR);
932 }
933 }
934 }
935
936 /*
937 * Write the extended names
938 */
939 for (i = 0; i < ext_total; i++) {
940 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
941 if (!WRITE_STRING(tp->ext_Names[i])) {
942 return (ERR);
943 }
944 }
945
946 }
947 #endif /* NCURSES_XNAMES */
948
949 total_written++;
950 total_parts++;
951 total_size = total_size + (int) (*offset + 1);
952 return (OK);
953 }
954
955 /*
956 * Returns the total number of entries written by this process
957 */
958 NCURSES_EXPORT(int)
_nc_tic_written(void)959 _nc_tic_written(void)
960 {
961 TR(TRACE_DATABASE, ("_nc_tic_written %d entries, %d parts, %d size",
962 total_written, total_parts, total_size));
963 return total_written;
964 }
965