blob: b2edd5d9e63993b13c110d3593021f9d9c1b5e5a [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
34
35/*
36 * write_entry.c -- write a terminfo structure onto the file system
37 */
38
39#include <curses.priv.h>
40#include <hashed_db.h>
41
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053042#include <tic.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053043
44#if 1
45#define TRACE_OUT(p) DEBUG(2, p)
46#else
47#define TRACE_OUT(p) /*nothing */
48#endif
49
Steve Kondikae271bc2015-11-15 02:50:53 +010050MODULE_ID("$Id: write_entry.c,v 1.92 2014/11/01 14:47:00 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053051
52static int total_written;
53
54static int make_db_root(const char *);
55static int write_object(TERMTYPE *, char *, unsigned *, unsigned);
56
57#if !USE_HASHED_DB
58static void
59write_file(char *filename, TERMTYPE *tp)
60{
61 char buffer[MAX_ENTRY_SIZE];
62 unsigned limit = sizeof(buffer);
63 unsigned offset = 0;
64
65 FILE *fp = (_nc_access(filename, W_OK) == 0) ? fopen(filename, "wb") : 0;
66 if (fp == 0) {
67 perror(filename);
68 _nc_syserr_abort("can't open %s/%s", _nc_tic_dir(0), filename);
69 }
70 DEBUG(1, ("Created %s", filename));
71
72 if (write_object(tp, buffer, &offset, limit) == ERR
Steve Kondikae271bc2015-11-15 02:50:53 +010073 || fwrite(buffer, sizeof(char), (size_t) offset, fp) != offset) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053074 _nc_syserr_abort("error writing %s/%s", _nc_tic_dir(0), filename);
75 }
76
77 fclose(fp);
78}
79
80/*
81 * Check for access rights to destination directories
82 * Create any directories which don't exist.
83 *
84 * Note: there's no reason to return the result of make_db_root(), since
85 * this function is called only in instances where that has to succeed.
86 */
87static void
88check_writeable(int code)
89{
90 static const char dirnames[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
91 static bool verified[sizeof(dirnames)];
92
93 char dir[sizeof(LEAF_FMT)];
94 char *s = 0;
95
Steve Kondikae271bc2015-11-15 02:50:53 +010096 if (code == 0 || (s = (strchr) (dirnames, code)) == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053097 _nc_err_abort("Illegal terminfo subdirectory \"" LEAF_FMT "\"", code);
98
99 if (verified[s - dirnames])
100 return;
101
Steve Kondikae271bc2015-11-15 02:50:53 +0100102 _nc_SPRINTF(dir, _nc_SLIMIT(sizeof(dir)) LEAF_FMT, code);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530103 if (make_db_root(dir) < 0) {
104 _nc_err_abort("%s/%s: permission denied", _nc_tic_dir(0), dir);
105 }
106
107 verified[s - dirnames] = TRUE;
108}
109#endif /* !USE_HASHED_DB */
110
111static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100112make_db_path(char *dst, const char *src, size_t limit)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530113{
114 int rc = -1;
115 const char *top = _nc_tic_dir(0);
116
117 if (src == top || _nc_is_abs_path(src)) {
118 if (strlen(src) + 1 <= limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100119 _nc_STRCPY(dst, src, limit);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530120 rc = 0;
121 }
122 } else {
123 if (strlen(top) + strlen(src) + 2 <= limit) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 _nc_SPRINTF(dst, _nc_SLIMIT(limit) "%s/%s", top, src);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530125 rc = 0;
126 }
127 }
128#if USE_HASHED_DB
129 if (rc == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100130 static const char suffix[] = DBM_SUFFIX;
131 size_t have = strlen(dst);
132 size_t need = strlen(suffix);
133 if (have > need && strcmp(dst + (int) (have - need), suffix)) {
134 if (have + need <= limit) {
135 _nc_STRCAT(dst, suffix, limit);
136 } else {
137 rc = -1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530138 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100139 } else if (_nc_is_dir_path(dst)) {
140 rc = -1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530141 }
142 }
143#endif
144 return rc;
145}
146
147/*
148 * Make a database-root if it doesn't exist.
149 */
150static int
151make_db_root(const char *path)
152{
153 int rc;
154 char fullpath[PATH_MAX];
155
156 if ((rc = make_db_path(fullpath, path, sizeof(fullpath))) == 0) {
157#if USE_HASHED_DB
158 DB *capdbp;
159
Steve Kondikae271bc2015-11-15 02:50:53 +0100160 if ((capdbp = _nc_db_open(fullpath, TRUE)) == NULL) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530161 rc = -1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100162 } else if (_nc_db_close(capdbp) < 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530163 rc = -1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100164 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530165#else
166 struct stat statbuf;
167
168 if ((rc = stat(path, &statbuf)) < 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100169 rc = mkdir(path
170#if !defined(__MINGW32__)
171 ,0777
172#endif
173 );
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530174 } else if (_nc_access(path, R_OK | W_OK | X_OK) < 0) {
175 rc = -1; /* permission denied */
176 } else if (!(S_ISDIR(statbuf.st_mode))) {
177 rc = -1; /* not a directory */
178 }
179#endif
180 }
181 return rc;
182}
183
184/*
185 * Set the write directory for compiled entries.
186 */
187NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100188_nc_set_writedir(const char *dir)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530189{
190 const char *destination;
191 char actual[PATH_MAX];
192
193 if (dir == 0
Steve Kondikae271bc2015-11-15 02:50:53 +0100194#ifndef USE_ROOT_ENVIRON
195 && use_terminfo_vars()
196#endif
197 )
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530198 dir = getenv("TERMINFO");
199
200 if (dir != 0)
201 (void) _nc_tic_dir(dir);
202
203 destination = _nc_tic_dir(0);
204 if (make_db_root(destination) < 0) {
205 char *home = _nc_home_terminfo();
206
207 if (home != 0) {
208 destination = home;
209 if (make_db_root(destination) < 0)
210 _nc_err_abort("%s: permission denied (errno %d)",
211 destination, errno);
212 }
213 }
214
215 /*
216 * Note: because of this code, this logic should be exercised
217 * *once only* per run.
218 */
219#if USE_HASHED_DB
220 make_db_path(actual, destination, sizeof(actual));
221#else
222 if (chdir(_nc_tic_dir(destination)) < 0
223 || getcwd(actual, sizeof(actual)) == 0)
224 _nc_err_abort("%s: not a directory", destination);
225#endif
226 _nc_keep_tic_dir(strdup(actual));
227}
228
229/*
230 * Save the compiled version of a description in the filesystem.
231 *
232 * make a copy of the name-list
233 * break it up into first-name and all-but-last-name
234 * creat(first-name)
235 * write object information to first-name
236 * close(first-name)
237 * for each name in all-but-last-name
238 * link to first-name
239 *
240 * Using 'time()' to obtain a reference for file timestamps is unreliable,
241 * e.g., with NFS, because the filesystem may have a different time
242 * reference. We check for pre-existence of links by latching the first
243 * timestamp from a file that we create.
244 *
245 * The _nc_warning() calls will report a correct line number only if
246 * _nc_curr_line is properly set before the write_entry() call.
247 */
248
249NCURSES_EXPORT(void)
250_nc_write_entry(TERMTYPE *const tp)
251{
252#if USE_HASHED_DB
253
254 char buffer[MAX_ENTRY_SIZE + 1];
255 unsigned limit = sizeof(buffer);
256 unsigned offset = 0;
257
258#else /* !USE_HASHED_DB */
259
260 struct stat statbuf;
261 char filename[PATH_MAX];
262 char linkname[PATH_MAX];
263#if USE_SYMLINKS
264 char symlinkname[PATH_MAX];
265#if !HAVE_LINK
266#undef HAVE_LINK
267#define HAVE_LINK 1
268#endif
269#endif /* USE_SYMLINKS */
270
271 static int call_count;
272 static time_t start_time; /* time at start of writes */
273
274#endif /* USE_HASHED_DB */
275
276 char name_list[MAX_TERMINFO_LENGTH];
277 char *first_name, *other_names;
278 char *ptr;
Steve Kondikae271bc2015-11-15 02:50:53 +0100279 char *term_names = tp->term_names;
280 size_t name_size = strlen(term_names);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530281
Steve Kondikae271bc2015-11-15 02:50:53 +0100282 if (name_size == 0) {
283 _nc_syserr_abort("no terminal name found.");
284 } else if (name_size >= sizeof(name_list) - 1) {
285 _nc_syserr_abort("terminal name too long: %s", term_names);
286 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530287
Steve Kondikae271bc2015-11-15 02:50:53 +0100288 _nc_STRCPY(name_list, term_names, sizeof(name_list));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530289 DEBUG(7, ("Name list = '%s'", name_list));
290
291 first_name = name_list;
292
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 ptr = &name_list[name_size - 1];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530294 other_names = ptr + 1;
295
296 while (ptr > name_list && *ptr != '|')
297 ptr--;
298
299 if (ptr != name_list) {
300 *ptr = '\0';
301
302 for (ptr = name_list; *ptr != '\0' && *ptr != '|'; ptr++)
303 continue;
304
305 if (*ptr == '\0')
306 other_names = ptr;
307 else {
308 *ptr = '\0';
309 other_names = ptr + 1;
310 }
311 }
312
313 DEBUG(7, ("First name = '%s'", first_name));
314 DEBUG(7, ("Other names = '%s'", other_names));
315
316 _nc_set_type(first_name);
317
318#if USE_HASHED_DB
319 if (write_object(tp, buffer + 1, &offset, limit - 1) != ERR) {
320 DB *capdb = _nc_db_open(_nc_tic_dir(0), TRUE);
321 DBT key, data;
322
323 if (capdb != 0) {
324 buffer[0] = 0;
325
326 memset(&key, 0, sizeof(key));
Steve Kondikae271bc2015-11-15 02:50:53 +0100327 key.data = term_names;
328 key.size = name_size;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530329
330 memset(&data, 0, sizeof(data));
331 data.data = buffer;
332 data.size = offset + 1;
333
334 _nc_db_put(capdb, &key, &data);
335
336 buffer[0] = 2;
337
338 key.data = name_list;
339 key.size = strlen(name_list);
340
Steve Kondikae271bc2015-11-15 02:50:53 +0100341 _nc_STRCPY(buffer + 1,
342 term_names,
343 sizeof(buffer) - 1);
344 data.size = name_size + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530345
346 _nc_db_put(capdb, &key, &data);
347
348 while (*other_names != '\0') {
349 ptr = other_names++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100350 assert(ptr < buffer + sizeof(buffer) - 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530351 while (*other_names != '|' && *other_names != '\0')
352 other_names++;
353
354 if (*other_names != '\0')
355 *(other_names++) = '\0';
356
357 key.data = ptr;
358 key.size = strlen(ptr);
359
360 _nc_db_put(capdb, &key, &data);
361 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530362 }
363 }
364#else /* !USE_HASHED_DB */
365 if (call_count++ == 0) {
366 start_time = 0;
367 }
368
Steve Kondikae271bc2015-11-15 02:50:53 +0100369 if (strlen(first_name) >= sizeof(filename) - (2 + LEAF_LEN))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530370 _nc_warning("terminal name too long.");
371
Steve Kondikae271bc2015-11-15 02:50:53 +0100372 _nc_SPRINTF(filename, _nc_SLIMIT(sizeof(filename))
373 LEAF_FMT "/%s", first_name[0], first_name);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530374
375 /*
376 * Has this primary name been written since the first call to
377 * write_entry()? If so, the newer write will step on the older,
378 * so warn the user.
379 */
380 if (start_time > 0 &&
381 stat(filename, &statbuf) >= 0
382 && statbuf.st_mtime >= start_time) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100383#if HAVE_LINK && !USE_SYMLINKS
384 /*
385 * If the file has more than one link, the reason for the previous
386 * write could be that the current primary name used to be an alias for
387 * the previous entry. In that case, unlink the file so that we will
388 * not modify the previous entry as we write this one.
389 */
390 if (statbuf.st_nlink > 1) {
391 _nc_warning("name redefined.");
392 unlink(filename);
393 } else {
394 _nc_warning("name multiply defined.");
395 }
396#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530397 _nc_warning("name multiply defined.");
Steve Kondikae271bc2015-11-15 02:50:53 +0100398#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530399 }
400
401 check_writeable(first_name[0]);
402 write_file(filename, tp);
403
404 if (start_time == 0) {
405 if (stat(filename, &statbuf) < 0
406 || (start_time = statbuf.st_mtime) == 0) {
407 _nc_syserr_abort("error obtaining time from %s/%s",
408 _nc_tic_dir(0), filename);
409 }
410 }
411 while (*other_names != '\0') {
412 ptr = other_names++;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530413 while (*other_names != '|' && *other_names != '\0')
414 other_names++;
415
416 if (*other_names != '\0')
417 *(other_names++) = '\0';
418
Steve Kondikae271bc2015-11-15 02:50:53 +0100419 if (strlen(ptr) > sizeof(linkname) - (2 + LEAF_LEN)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530420 _nc_warning("terminal alias %s too long.", ptr);
421 continue;
422 }
423 if (strchr(ptr, '/') != 0) {
424 _nc_warning("cannot link alias %s.", ptr);
425 continue;
426 }
427
428 check_writeable(ptr[0]);
Steve Kondikae271bc2015-11-15 02:50:53 +0100429 _nc_SPRINTF(linkname, _nc_SLIMIT(sizeof(linkname))
430 LEAF_FMT "/%s", ptr[0], ptr);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530431
432 if (strcmp(filename, linkname) == 0) {
433 _nc_warning("self-synonym ignored");
434 } else if (stat(linkname, &statbuf) >= 0 &&
435 statbuf.st_mtime < start_time) {
436 _nc_warning("alias %s multiply defined.", ptr);
437 } else if (_nc_access(linkname, W_OK) == 0)
438#if HAVE_LINK
439 {
440 int code;
441#if USE_SYMLINKS
Steve Kondikae271bc2015-11-15 02:50:53 +0100442 if (first_name[0] == linkname[0])
443 strncpy(symlinkname, first_name, sizeof(symlinkname) - 1);
444 else {
445 _nc_STRCPY(symlinkname, "../", sizeof(suymlinkname));
446 strncat(symlinkname, filename, sizeof(symlinkname) - 4);
447 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530448 symlinkname[sizeof(symlinkname) - 1] = '\0';
449#endif /* USE_SYMLINKS */
450#if HAVE_REMOVE
451 code = remove(linkname);
452#else
453 code = unlink(linkname);
454#endif
455 if (code != 0 && errno == ENOENT)
456 code = 0;
457#if USE_SYMLINKS
458 if (symlink(symlinkname, linkname) < 0)
459#else
460 if (link(filename, linkname) < 0)
461#endif /* USE_SYMLINKS */
462 {
463 /*
464 * If there wasn't anything there, and we cannot
465 * link to the target because it is the same as the
466 * target, then the source must be on a filesystem
467 * that uses caseless filenames, such as Win32, etc.
468 */
469 if (code == 0 && errno == EEXIST)
470 _nc_warning("can't link %s to %s", filename, linkname);
471 else if (code == 0 && (errno == EPERM || errno == ENOENT))
472 write_file(linkname, tp);
473 else {
474#if MIXEDCASE_FILENAMES
475 _nc_syserr_abort("can't link %s to %s", filename, linkname);
476#else
477 _nc_warning("can't link %s to %s (errno=%d)", filename,
478 linkname, errno);
479#endif
480 }
481 } else {
482 DEBUG(1, ("Linked %s", linkname));
483 }
484 }
485#else /* just make copies */
486 write_file(linkname, tp);
487#endif /* HAVE_LINK */
488 }
489#endif /* USE_HASHED_DB */
490}
491
Steve Kondikae271bc2015-11-15 02:50:53 +0100492static size_t
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530493fake_write(char *dst,
494 unsigned *offset,
Steve Kondikae271bc2015-11-15 02:50:53 +0100495 size_t limit,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530496 char *src,
Steve Kondikae271bc2015-11-15 02:50:53 +0100497 size_t want,
498 size_t size)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530499{
Steve Kondikae271bc2015-11-15 02:50:53 +0100500 size_t have = (limit - *offset);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530501
502 want *= size;
503 if (have > 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100504 if (want > have)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530505 want = have;
506 memcpy(dst + *offset, src, want);
Steve Kondikae271bc2015-11-15 02:50:53 +0100507 *offset += (unsigned) want;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530508 } else {
509 want = 0;
510 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100511 return (want / size);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530512}
513
Steve Kondikae271bc2015-11-15 02:50:53 +0100514#define Write(buf, size, count) fake_write(buffer, offset, (size_t) limit, (char *) buf, (size_t) count, (size_t) size)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530515
516#undef LITTLE_ENDIAN /* BSD/OS defines this as a feature macro */
517#define HI(x) ((x) / 256)
518#define LO(x) ((x) % 256)
Steve Kondikae271bc2015-11-15 02:50:53 +0100519#define LITTLE_ENDIAN(p, x) (p)[0] = (unsigned char)LO(x), \
520 (p)[1] = (unsigned char)HI(x)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530521
522#define WRITE_STRING(str) (Write(str, sizeof(char), strlen(str) + 1) == strlen(str) + 1)
523
524static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100525compute_offsets(char **Strings, size_t strmax, short *offsets)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530526{
Steve Kondikae271bc2015-11-15 02:50:53 +0100527 int nextfree = 0;
528 size_t i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530529
530 for (i = 0; i < strmax; i++) {
531 if (Strings[i] == ABSENT_STRING) {
532 offsets[i] = -1;
533 } else if (Strings[i] == CANCELLED_STRING) {
534 offsets[i] = -2;
535 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100536 offsets[i] = (short) nextfree;
537 nextfree += (int) strlen(Strings[i]) + 1;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530538 TRACE_OUT(("put Strings[%d]=%s(%d)", (int) i,
539 _nc_visbuf(Strings[i]), (int) nextfree));
540 }
541 }
542 return nextfree;
543}
544
545static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100546convert_shorts(unsigned char *buf, short *Numbers, size_t count)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530547{
Steve Kondikae271bc2015-11-15 02:50:53 +0100548 size_t i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530549 for (i = 0; i < count; i++) {
550 if (Numbers[i] == ABSENT_NUMERIC) { /* HI/LO won't work */
551 buf[2 * i] = buf[2 * i + 1] = 0377;
552 } else if (Numbers[i] == CANCELLED_NUMERIC) { /* HI/LO won't work */
553 buf[2 * i] = 0376;
554 buf[2 * i + 1] = 0377;
555 } else {
556 LITTLE_ENDIAN(buf + 2 * i, Numbers[i]);
Steve Kondikae271bc2015-11-15 02:50:53 +0100557 TRACE_OUT(("put Numbers[%u]=%d", (unsigned) i, Numbers[i]));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530558 }
559 }
560}
561
562#define even_boundary(value) \
563 ((value) % 2 != 0 && Write(&zero, sizeof(char), 1) != 1)
564
565#if NCURSES_XNAMES
566static unsigned
567extended_Booleans(TERMTYPE *tp)
568{
Steve Kondikae271bc2015-11-15 02:50:53 +0100569 unsigned result = 0;
570 unsigned i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530571
572 for (i = 0; i < tp->ext_Booleans; ++i) {
573 if (tp->Booleans[BOOLCOUNT + i] == TRUE)
574 result = (i + 1);
575 }
576 return result;
577}
578
579static unsigned
580extended_Numbers(TERMTYPE *tp)
581{
Steve Kondikae271bc2015-11-15 02:50:53 +0100582 unsigned result = 0;
583 unsigned i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530584
585 for (i = 0; i < tp->ext_Numbers; ++i) {
586 if (tp->Numbers[NUMCOUNT + i] != ABSENT_NUMERIC)
587 result = (i + 1);
588 }
589 return result;
590}
591
592static unsigned
593extended_Strings(TERMTYPE *tp)
594{
595 unsigned short result = 0;
596 unsigned short i;
597
598 for (i = 0; i < tp->ext_Strings; ++i) {
599 if (tp->Strings[STRCOUNT + i] != ABSENT_STRING)
Steve Kondikae271bc2015-11-15 02:50:53 +0100600 result = (unsigned short) (i + 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530601 }
602 return result;
603}
604
605/*
606 * _nc_align_termtype() will extend entries that are referenced in a use=
607 * clause - discard the unneeded data.
608 */
609static bool
610extended_object(TERMTYPE *tp)
611{
612 bool result = FALSE;
613
614 if (_nc_user_definable) {
615 result = ((extended_Booleans(tp)
616 + extended_Numbers(tp)
617 + extended_Strings(tp)) != 0);
618 }
619 return result;
620}
621#endif
622
623static int
624write_object(TERMTYPE *tp, char *buffer, unsigned *offset, unsigned limit)
625{
626 char *namelist;
627 size_t namelen, boolmax, nummax, strmax;
628 char zero = '\0';
629 size_t i;
Steve Kondikae271bc2015-11-15 02:50:53 +0100630 int nextfree;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530631 short offsets[MAX_ENTRY_SIZE / 2];
632 unsigned char buf[MAX_ENTRY_SIZE];
633 unsigned last_bool = BOOLWRITE;
634 unsigned last_num = NUMWRITE;
635 unsigned last_str = STRWRITE;
636
637#if NCURSES_XNAMES
638 /*
639 * Normally we limit the list of values to exclude the "obsolete"
640 * capabilities. However, if we are accepting extended names, add
641 * these as well, since they are used for supporting translation
642 * to/from termcap.
643 */
644 if (_nc_user_definable) {
645 last_bool = BOOLCOUNT;
646 last_num = NUMCOUNT;
647 last_str = STRCOUNT;
648 }
649#endif
650
651 namelist = tp->term_names;
652 namelen = strlen(namelist) + 1;
653
654 boolmax = 0;
655 for (i = 0; i < last_bool; i++) {
656 if (tp->Booleans[i] == TRUE)
657 boolmax = i + 1;
658 }
659
660 nummax = 0;
661 for (i = 0; i < last_num; i++) {
662 if (tp->Numbers[i] != ABSENT_NUMERIC)
663 nummax = i + 1;
664 }
665
666 strmax = 0;
667 for (i = 0; i < last_str; i++) {
668 if (tp->Strings[i] != ABSENT_STRING)
669 strmax = i + 1;
670 }
671
672 nextfree = compute_offsets(tp->Strings, strmax, offsets);
673
674 /* fill in the header */
675 LITTLE_ENDIAN(buf, MAGIC);
676 LITTLE_ENDIAN(buf + 2, min(namelen, MAX_NAME_SIZE + 1));
677 LITTLE_ENDIAN(buf + 4, boolmax);
678 LITTLE_ENDIAN(buf + 6, nummax);
679 LITTLE_ENDIAN(buf + 8, strmax);
680 LITTLE_ENDIAN(buf + 10, nextfree);
681
682 /* write out the header */
683 TRACE_OUT(("Header of %s @%d", namelist, *offset));
684 if (Write(buf, 12, 1) != 1
685 || Write(namelist, sizeof(char), namelen) != namelen)
686 return (ERR);
687
688 for (i = 0; i < boolmax; i++)
689 if (tp->Booleans[i] == TRUE)
690 buf[i] = TRUE;
691 else
692 buf[i] = FALSE;
693 if (Write(buf, sizeof(char), boolmax) != boolmax)
694 return (ERR);
695
696 if (even_boundary(namelen + boolmax))
697 return (ERR);
698
699 TRACE_OUT(("Numerics begin at %04x", *offset));
700
701 /* the numerics */
702 convert_shorts(buf, tp->Numbers, nummax);
703 if (Write(buf, 2, nummax) != nummax)
704 return (ERR);
705
706 TRACE_OUT(("String offsets begin at %04x", *offset));
707
708 /* the string offsets */
709 convert_shorts(buf, offsets, strmax);
710 if (Write(buf, 2, strmax) != strmax)
711 return (ERR);
712
713 TRACE_OUT(("String table begins at %04x", *offset));
714
715 /* the strings */
716 for (i = 0; i < strmax; i++)
717 if (VALID_STRING(tp->Strings[i]))
718 if (!WRITE_STRING(tp->Strings[i]))
719 return (ERR);
720
721#if NCURSES_XNAMES
722 if (extended_object(tp)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100723 unsigned extcnt = (unsigned) NUM_EXT_NAMES(tp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530724
725 if (even_boundary(nextfree))
726 return (ERR);
727
728 nextfree = compute_offsets(tp->Strings + STRCOUNT,
Steve Kondikae271bc2015-11-15 02:50:53 +0100729 (size_t) tp->ext_Strings,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530730 offsets);
731 TRACE_OUT(("after extended string capabilities, nextfree=%d", nextfree));
732
733 if (tp->ext_Strings >= SIZEOF(offsets))
734 return (ERR);
735
736 nextfree += compute_offsets(tp->ext_Names,
Steve Kondikae271bc2015-11-15 02:50:53 +0100737 (size_t) extcnt,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530738 offsets + tp->ext_Strings);
739 TRACE_OUT(("after extended capnames, nextfree=%d", nextfree));
740 strmax = tp->ext_Strings + extcnt;
741
742 /*
743 * Write the extended header
744 */
745 LITTLE_ENDIAN(buf + 0, tp->ext_Booleans);
746 LITTLE_ENDIAN(buf + 2, tp->ext_Numbers);
747 LITTLE_ENDIAN(buf + 4, tp->ext_Strings);
748 LITTLE_ENDIAN(buf + 6, strmax);
749 LITTLE_ENDIAN(buf + 8, nextfree);
750 TRACE_OUT(("WRITE extended-header @%d", *offset));
751 if (Write(buf, 10, 1) != 1)
752 return (ERR);
753
754 TRACE_OUT(("WRITE %d booleans @%d", tp->ext_Booleans, *offset));
755 if (tp->ext_Booleans
756 && Write(tp->Booleans + BOOLCOUNT, sizeof(char),
757 tp->ext_Booleans) != tp->ext_Booleans)
758 return (ERR);
759
760 if (even_boundary(tp->ext_Booleans))
761 return (ERR);
762
763 TRACE_OUT(("WRITE %d numbers @%d", tp->ext_Numbers, *offset));
764 if (tp->ext_Numbers) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100765 convert_shorts(buf, tp->Numbers + NUMCOUNT, (size_t) tp->ext_Numbers);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530766 if (Write(buf, 2, tp->ext_Numbers) != tp->ext_Numbers)
767 return (ERR);
768 }
769
770 /*
771 * Convert the offsets for the ext_Strings and ext_Names tables,
772 * in that order.
773 */
774 convert_shorts(buf, offsets, strmax);
775 TRACE_OUT(("WRITE offsets @%d", *offset));
776 if (Write(buf, 2, strmax) != strmax)
777 return (ERR);
778
779 /*
780 * Write the string table after the offset tables so we do not
781 * have to do anything about alignment.
782 */
783 for (i = 0; i < tp->ext_Strings; i++) {
784 if (VALID_STRING(tp->Strings[i + STRCOUNT])) {
785 TRACE_OUT(("WRITE ext_Strings[%d]=%s", (int) i,
786 _nc_visbuf(tp->Strings[i + STRCOUNT])));
787 if (!WRITE_STRING(tp->Strings[i + STRCOUNT]))
788 return (ERR);
789 }
790 }
791
792 /*
793 * Write the extended names
794 */
795 for (i = 0; i < extcnt; i++) {
796 TRACE_OUT(("WRITE ext_Names[%d]=%s", (int) i, tp->ext_Names[i]));
797 if (!WRITE_STRING(tp->ext_Names[i]))
798 return (ERR);
799 }
800
801 }
802#endif /* NCURSES_XNAMES */
803
804 total_written++;
805 return (OK);
806}
807
808/*
809 * Returns the total number of entries written by this process
810 */
811NCURSES_EXPORT(int)
812_nc_tic_written(void)
813{
814 return total_written;
815}