termbox.diff (5063B)
1 --- /home/strajder/src/termbox2/termbox2.h Sat Jan 24 00:00:44 2026 2 +++ termbox.h Sat Jan 24 11:34:49 2026 3 @@ -1,4 +1,13 @@ 4 +/* This program is licensed under the terms of GNU GPL v3 or (at your option) 5 + * any later version. Copyright (C) 2022-2026 Страхиња Радић. 6 + * See the file LICENSE for exact copyright and license details. */ 7 + 8 /* 9 +This file is based on the termbox2 TUI library, (c) 2021 termbox developers. 10 + 11 +License notice from the original termbox2.h from the termbox2 repository 12 +follows: 13 + 14 MIT License 15 16 Copyright (c) 2010-2020 nsf <no.smile.face@gmail.com> 17 @@ -22,6 +31,7 @@ 18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 SOFTWARE. 20 */ 21 + 22 #ifndef TERMBOX_H_INCL 23 #define TERMBOX_H_INCL 24 25 @@ -468,7 +478,7 @@ 26 int tb_init_file(const char *path); 27 int tb_init_fd(int ttyfd); 28 int tb_init_rwfd(int rfd, int wfd); 29 -int tb_shutdown(void); 30 +int tb_shutdown(int deallocate); 31 32 /* Return the size of the internal back buffer (which is the same as terminal's 33 * window size in rows and columns). The internal buffer can be resized after 34 @@ -2289,9 +2299,10 @@ 35 static int update_term_size(void); 36 static int update_term_size_via_esc(void); 37 static int init_cellbuf(void); 38 -static int tb_deinit(void); 39 +static int tb_deinit(int deallocate); 40 static int load_terminfo(void); 41 static int load_terminfo_from_path(const char *path, const char *term); 42 +static int unveil_terminfo_path(void); 43 static int read_terminfo_path(const char *path); 44 static int parse_terminfo_caps(void); 45 static int load_builtin_caps(void); 46 @@ -2373,14 +2384,15 @@ 47 global.initialized = 1; 48 } while (0); 49 50 - if (rv != TB_OK) tb_deinit(); 51 + if (rv != TB_OK) tb_deinit(1); 52 53 return rv; 54 } 55 56 -int tb_shutdown(void) { 57 +/* Change to the original: add deallocate parameter. [SR] */ 58 +int tb_shutdown(int deallocate) { 59 if_not_init_return(); 60 - tb_deinit(); 61 + tb_deinit(deallocate); 62 return TB_OK; 63 } 64 65 @@ -3169,7 +3181,8 @@ 66 return TB_OK; 67 } 68 69 -static int tb_deinit(void) { 70 +/* Change to the original: add deallocate parameter. [SR] */ 71 +static int tb_deinit(int deallocate) { 72 if (global.caps[0] != NULL && global.wfd >= 0) { 73 bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]); 74 bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]); 75 @@ -3196,12 +3209,15 @@ 76 if (global.resize_pipefd[0] >= 0) close(global.resize_pipefd[0]); 77 if (global.resize_pipefd[1] >= 0) close(global.resize_pipefd[1]); 78 79 - cellbuf_free(&global.back); 80 - cellbuf_free(&global.front); 81 - bytebuf_free(&global.in); 82 - bytebuf_free(&global.out); 83 + if (deallocate) 84 + { 85 + cellbuf_free(&global.back); 86 + cellbuf_free(&global.front); 87 + bytebuf_free(&global.in); 88 + bytebuf_free(&global.out); 89 90 - if (global.terminfo) tb_free(global.terminfo); 91 + if (global.terminfo) tb_free(global.terminfo); 92 + } 93 94 cap_trie_deinit(&global.cap_trie); 95 96 @@ -3281,6 +3297,74 @@ 97 98 return TB_ERR; 99 } 100 + 101 +#ifdef __OpenBSD__ 102 +static int unveil_single_path(const char* term, const char* path) { 103 + int rv; 104 + char tmp[TB_PATH_MAX]; 105 + snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term); 106 + return unveil(tmp, "r"); 107 +} 108 + 109 +static int unveil_terminfo_path(void) { 110 + int rv; 111 + char tmp[TB_PATH_MAX]; 112 + const char *term = NULL; 113 + const char *terminfo = NULL; 114 + const char *home = NULL; 115 + const char *dirs = NULL; 116 + 117 + term = getenv("TERM"); 118 + if (!term) return TB_ERR; 119 + terminfo = getenv("TERMINFO"); 120 + if (terminfo) { 121 + rv = unveil_single_path(term, terminfo); 122 + if (!rv) return rv; 123 + } 124 + home = getenv("HOME"); 125 + if (home) { 126 + snprintf_or_return(rv, tmp, sizeof(tmp), "%s/.terminfo", home); 127 + rv = unveil_single_path(term, tmp); 128 + if (!rv) return rv; 129 + } 130 + dirs = getenv("TERMINFO_DIRS"); 131 + if (dirs) { 132 + char *dir = NULL; 133 + snprintf_or_return(rv, tmp, sizeof(tmp), "%s", dirs); 134 + dir = strtok(tmp, ":"); 135 + while (dir) { 136 + const char *cdir = dir; 137 + if (*cdir != '\0') { 138 + rv = unveil_single_path(term, cdir); 139 + if (!rv) return rv; 140 + } 141 + dir = strtok(NULL, ":"); 142 + } 143 + } 144 +#ifdef TB_TERMINFO_DIR 145 + rv = unveil_single_path(term, TB_TERMINFO_DIR); 146 + if (!rv) return rv; 147 +#endif 148 + rv = unveil_single_path(term, "/usr/local/etc/terminfo"); 149 + if (!rv) return rv; 150 + rv = unveil_single_path(term, "/usr/local/share/terminfo"); 151 + if (!rv) return rv; 152 + rv = unveil_single_path(term, "/usr/local/lib/terminfo"); 153 + if (!rv) return rv; 154 + rv = unveil_single_path(term, "/etc/terminfo"); 155 + if (!rv) return rv; 156 + rv = unveil_single_path(term, "/usr/share/terminfo"); 157 + if (!rv) return rv; 158 + rv = unveil_single_path(term, "/usr/lib/terminfo"); 159 + if (!rv) return rv; 160 + rv = unveil_single_path(term, "/usr/share/lib/terminfo"); 161 + if (!rv) return rv; 162 + rv = unveil_single_path(term, "/lib/terminfo"); 163 + if (!rv) return rv; 164 + 165 + return -1; 166 +} 167 +#endif 168 169 static int read_terminfo_path(const char *path) { 170 FILE *fp = fopen(path, "rb");