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