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