defs.h (7179B)
1 /* This program is licensed under the terms of GNU GPL v3 or (at your option) 2 * any later version. Copyright (C) 2020-2026 Страхиња Радић. 3 * See the file LICENSE for exact copyright and license details. */ 4 5 #include "utf8.h" 6 7 #define BUF_DEFAULT 4096 8 #define BUF_DELTA 512 9 #define FORMAT_DEFAULT 256 10 11 // #define ANSI_SGR_RESET "\033[0m\033[?25h" 12 #define ANSI_SGR_BOLD_ON "\033[1m" 13 #define ANSI_SGR_BOLD_OFF "\033[0m" 14 15 #define CUSTOM_ERROR_START 200 16 17 #define HASH(a, b) ((unsigned int)((a) << 8) | (b)) 18 #define LEN(x) (sizeof(x) / sizeof(x[0])) 19 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 20 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 21 #define U32(c) ((u32)(0x00000000 | (c))) 22 #define UNUSED(x) ((void)(x)) 23 24 #define TAB ((u32)0x00000009) 25 26 #define ENSURE_SIZE(to, temp, tosize, testsize, fromsize, type) \ 27 do \ 28 { \ 29 if (!to || tosize < testsize) \ 30 { \ 31 tosize = fromsize; \ 32 temp = realloc(to, tosize * sizeof(type)); \ 33 if (!temp) \ 34 { \ 35 perror(PROGRAMNAME ": realloc"); \ 36 exit(errno); \ 37 } \ 38 to = temp; \ 39 } \ 40 } while (0) 41 42 #define CALLOC(ptr, ptrtype, nmemb) \ 43 do \ 44 { \ 45 ptr = calloc(nmemb, sizeof(ptrtype)); \ 46 if (!ptr) \ 47 { \ 48 perror(PROGRAMNAME ": calloc"); \ 49 exit(errno); \ 50 } \ 51 } while (0) 52 53 #define MALLOC(ptr, size) \ 54 do \ 55 { \ 56 ptr = malloc(size); \ 57 if (!ptr) \ 58 { \ 59 perror(PROGRAMNAME ": malloc"); \ 60 exit(errno); \ 61 } \ 62 } while (0) 63 64 #define REALLOC(ptr, ptrtype, newsize) \ 65 do \ 66 { \ 67 ptrtype* newptr = realloc(ptr, newsize); \ 68 if (!newptr) \ 69 { \ 70 perror(PROGRAMNAME ": realloc"); \ 71 exit(errno); \ 72 } \ 73 ptr = newptr; \ 74 } while (0) 75 76 /* Copy *parg to *ptoken and increase ptoken */ 77 #define CHECKCOPY(token, ptoken, token_size, parg) \ 78 do \ 79 { \ 80 if (ptoken + 2 > token + token_size) \ 81 { \ 82 size_t old_size = token_size; \ 83 token_size += BUF_DELTA; \ 84 REALLOC(token, char, token_size); \ 85 ptoken = token + old_size - 1; \ 86 } \ 87 *ptoken++ = *parg++; \ 88 } while (0) 89 90 /* Set *pformat to num and increase pformat */ 91 #define CHECKSET(format, pformat, format_size, num) \ 92 do \ 93 { \ 94 if (pformat + 2 > format + format_size) \ 95 { \ 96 size_t old_size = format_size; \ 97 format_size += BUF_DELTA; \ 98 REALLOC(format, int, format_size); \ 99 pformat = format + old_size - 1; \ 100 } \ 101 *pformat++ = num; \ 102 } while (0) 103 104 enum { 105 ERR_NONE, 106 ERR_NO_CL_ARG_PARAM, 107 ERR_BAD_CL_ARG, 108 ERR_BAD_ALIGN, 109 ERR_BAD_NUMBER, 110 ERR_BAD_FORMAT_SPEC, 111 ERR_BAD_QUOTES_ARG, 112 ERR_BAD_SET_SPEC, 113 ERR_ISDIR, 114 ERR_NOTREG_FILE, 115 ERR_UNMATCHED_QUOTE, 116 ERR_STREAM_ERROR, 117 ERR_BINARY_INPUT, 118 }; 119 120 enum { Q_PROCESS, Q_IGNORE }; 121 122 enum { 123 TABLE_SYMBOLS_ASCII, 124 TABLE_SYMBOLS_SINGLE, 125 TABLE_SYMBOLS_DOUBLE, 126 TABLE_SYMBOLS_EMPTY 127 }; 128 129 enum { 130 TABLE_INNER_ASCII_ASCII, 131 TABLE_INNER_SINGLE_SINGLE, 132 TABLE_INNER_SINGLE_DOUBLE, 133 TABLE_INNER_DOUBLE_SINGLE, 134 TABLE_INNER_DOUBLE_DOUBLE, 135 TABLE_INNER_EMPTY_EMPTY, 136 TABLE_INNER_ASCII_EMPTY, 137 TABLE_INNER_SINGLE_EMPTY, 138 TABLE_INNER_DOUBLE_EMPTY, 139 TABLE_INNER_EMPTY_ASCII, 140 TABLE_INNER_EMPTY_SINGLE, 141 TABLE_INNER_EMPTY_DOUBLE, 142 }; 143 144 typedef enum { 145 CMD_NONE, 146 CMD_ALIGN, 147 CMD_COLUMNS, 148 CMD_DELIMITER, 149 CMD_FORMAT, 150 CMD_QUOTES, 151 CMD_SYMBOLS, 152 CMD_VERSION, 153 CMD_FULL_VERSION 154 } Command; 155 156 typedef enum { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT } Alignment; 157 158 static const u32 table_symbols[][9] = 159 { 160 [TABLE_SYMBOLS_ASCII] = { 161 // ascii 162 U32('+'), U32('-'), U32('+'), 163 U32('|'), U32(' '), U32('|'), 164 U32('+'), U32('-'), U32('+'), 165 }, 166 [TABLE_SYMBOLS_SINGLE] = { 167 // single 168 U32(0x250c), U32(0x2500), U32(0x2510), 169 U32(0x2502), U32(' '), U32(0x2502), 170 U32(0x2514), U32(0x2500), U32(0x2518), 171 }, 172 [TABLE_SYMBOLS_DOUBLE] = { 173 // double 174 U32(0x2554), U32(0x2550), U32(0x2557), 175 U32(0x2551), U32(' '), U32(0x2551), 176 U32(0x255a), U32(0x2550), U32(0x255d), 177 }, 178 [TABLE_SYMBOLS_EMPTY] = { 179 // empty 180 U32(' '), U32(' '), U32(' '), 181 U32(' '), U32(' '), U32(' '), 182 U32(' '), U32(' '), U32(' '), 183 } 184 }; 185 186 static const u32 table_inner_symbols[][3] = 187 { 188 [TABLE_INNER_ASCII_ASCII] = { 189 // ascii -> ascii 190 U32('+'), 191 U32('|'), 192 U32('+'), 193 }, 194 195 [TABLE_INNER_SINGLE_SINGLE] = { 196 // single -> single 197 U32(0x252c), 198 U32(0x2502), 199 U32(0x2534), 200 }, 201 202 [TABLE_INNER_SINGLE_DOUBLE] = { 203 // single -> double 204 U32(0x2565), 205 U32(0x2551), 206 U32(0x2568), 207 }, 208 209 [TABLE_INNER_DOUBLE_SINGLE] = { 210 // double -> single 211 U32(0x2564), 212 U32(0x2502), 213 U32(0x2567), 214 }, 215 216 [TABLE_INNER_DOUBLE_DOUBLE] = { 217 // double -> double 218 U32(0x2566), 219 U32(0x2551), 220 U32(0x2569), 221 }, 222 223 [TABLE_INNER_EMPTY_EMPTY] = { 224 // empty -> empty 225 U32(' '), 226 U32(' '), 227 U32(' '), 228 }, 229 230 [TABLE_INNER_ASCII_EMPTY] = { 231 // ascii -> empty 232 U32('-'), 233 U32(' '), 234 U32('-'), 235 }, 236 237 [TABLE_INNER_SINGLE_EMPTY] = { 238 // single -> empty 239 U32(0x2500), 240 U32(' '), 241 U32(0x2500), 242 }, 243 244 [TABLE_INNER_DOUBLE_EMPTY] = { 245 // double -> empty 246 U32(0x2550), 247 U32(' '), 248 U32(0x2550), 249 }, 250 251 [TABLE_INNER_EMPTY_ASCII] = { 252 // empty -> ascii 253 U32(' '), 254 U32('|'), 255 U32(' '), 256 }, 257 258 [TABLE_INNER_EMPTY_SINGLE] = { 259 // empty -> single 260 U32(' '), 261 U32(0x2502), 262 U32(' '), 263 }, 264 265 [TABLE_INNER_EMPTY_DOUBLE] = { 266 // empty -> double 267 U32(' '), 268 U32(0x2551), 269 U32(' '), 270 } 271 272 };