1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
USAGE("[-f font-family] [-s [font-size][/[[min]-[max]]]] [-B | -S] [-bi]");
#define DEFAULT_LISTING_TYPE "script"
#define LIST_LISTINGS(X, D)\
X(BY_SCRIPT, "By _script", DEFAULT_LISTING_TYPE, by_script_selected, populate_scripts, GDK_s) D\
X(BY_BLOCK, "By Unicode _block", "block", by_block_selected, populate_blocks, GDK_b)
enum listing {
#define X(ENUM, TITLE, TYPE, SELFUN, POPFUN, ACCEL) ENUM
LIST_LISTINGS(X, COMMA)
#undef X
};
#define NLISTINGS_X(...) (size_t)1
#define NLISTINGS (LIST_LISTINGS(NLISTINGS_X, +))
static const struct libcmap_block all_block = {.name = "All", .range = LIBCMAP_UNIVERSE_RANGE};
static unsigned int min_font_size = 4;
static unsigned int default_font_size = 22;
static unsigned int max_font_size = 500;
static unsigned int small_font_size_increment = 1;
static unsigned int big_font_size_increment = 8;
static enum listing listing = (enum listing)0;
static int use_bold = 0;
static int use_italic = 0;
static size_t term_width;
static size_t term_height;
static size_t left_pane_width;
static int automatic_left_pane_width = 1;
static volatile sig_atomic_t term_resized = 0;
static struct termios term_attributes_stdin;
static int term_attributes_stdin_saved = 0;
static int term_entered_submode_stdout = 0;
static int interrupt_deferred = 0;
static int exiting = 0;
static void
enter_subterminal(void)
{
printf("\033[?1049h" /* enter subterminal */
"\033[?25l" /* hide cursor */
"\033[H\033[2J" /* clear terminal */
);
fflush(stdout);
term_entered_submode_stdout = 1;
}
static void
exit_subterminal(void)
{
if (term_entered_submode_stdout) {
term_entered_submode_stdout = 0;
printf("\033[H\033[2J" /* clear terminal */
"\033[?25h" /* show cursor */
"\033[?1049l" /* exit subterminal */
);
fflush(stdout);
}
}
static void
set_term_attributes(void)
{
struct termios new_term_attributes_stdin;
if (tcgetattr(STDIN_FILENO, &term_attributes_stdin))
eprintf("tcgetattr <stdin>:");
term_attributes_stdin_saved = 1;
memcpy(&new_term_attributes_stdin, &term_attributes_stdin, sizeof(term_attributes_stdin));
new_term_attributes_stdin.c_iflag &= (tcflag_t)~(IXON | IXANY | IXOFF);
new_term_attributes_stdin.c_lflag &= (tcflag_t)~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
if (tcsetattr(STDIN_FILENO, TCSANOW, &new_term_attributes_stdin))
weprintf("tcsetattr <stdin> TCSANOW:");
}
static void
restore_term_attributes(void)
{
if (term_attributes_stdin_saved) {
term_attributes_stdin_saved = 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attributes_stdin))
weprintf("tcsetattr <stdin> TCSAFLUSH:");
}
}
static void
configure_terminal(void)
{
set_term_attributes();
enter_subterminal();
}
static void
restore_terminal(void)
{
restore_term_attributes();
exit_subterminal();
}
static void
cleanup(void)
{
restore_terminal();
}
static void
term_resized_callback(int signo)
{
(void) signo;
term_resized = 1;
}
static void
subscribe_term_resize(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &term_resized_callback;
if (sigaction(SIGWINCH, &sa, NULL))
eprintf("sigaction SIGWINCH:");
}
static void
update_term_size(void)
{
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws))
eprintf("ioclt <stdout> TIOCGWINSZ:");
term_width = ws.ws_col;
term_height = ws.ws_row;
}
static const char *
maybe_sat_parse_int_as_uint(const char *s, unsigned int *out, int *setp)
{
unsigned int digit;
if (!isdigit(*s))
return s;
*out = 0;
*setp = 1;
for (; isdigit(*s); s++) {
digit = (unsigned int)(*s & 15);
if (*out > ((unsigned int)INT_MAX - digit) / 10U)
*out = (unsigned int)INT_MAX;
else
*out = *out * 10U + digit;
}
return s;
}
static void
parse_fontsize(const char *s, int *default_font_size_setp, int *min_font_size_setp, int *max_font_size_setp)
{
s = maybe_sat_parse_int_as_uint(s, &default_font_size, default_font_size_setp);
if (!*s)
return;
if (*s++ != '/')
usage();
if (!*s)
return;
s = maybe_sat_parse_int_as_uint(s, &min_font_size, min_font_size_setp);
if (*s++ != '-')
usage();
if (!*s)
return;
s = maybe_sat_parse_int_as_uint(s, &max_font_size, max_font_size_setp);
if (*s)
usage();
if (!default_font_size)
eprintf("default font size cannot be zero");
if (!min_font_size)
eprintf("minimum font size cannot be zero");
if (!max_font_size)
eprintf("maximum font size cannot be zero");
}
static void
handle_keyboard_input(union libterminput_input *input)
{
if (input->type == LIBTERMINPUT_KEYPRESS) {
switch ((int)input->keypress.mods) {
case 0:
if (input->keypress.key == LIBTERMINPUT_TAB) {
} else if (input->keypress.key == LIBTERMINPUT_BACKTAB) backtab: {
}
break;
case LIBTERMINPUT_SHIFT:
if (input->keypress.key == LIBTERMINPUT_TAB)
goto backtab;
break;
case LIBTERMINPUT_CTRL:
case LIBTERMINPUT_CTRL | LIBTERMINPUT_SHIFT:
if (IS_KEY(input, 'Z')) {
restore_terminal();
raise(SIGTSTP);
configure_terminal();
redraw:
interrupt_deferred = 1;
term_resized = 1;
} else if (IS_KEY(input, 'L')) {
goto redraw;
} else if (IS_KEY(input, 'Q') || IS_KEY(input, 'C')) {
exiting = 1;
}
break;
case LIBTERMINPUT_META:
if (input->keypress.key == LIBTERMINPUT_LEFT) {
automatic_left_pane_width = 0;
if (left_pane_width) {
left_pane_width -= 1U;
goto redraw;
}
} else if (input->keypress.key == LIBTERMINPUT_RIGHT) {
automatic_left_pane_width = 0;
if (term_width && left_pane_width < term_width - 1U) {
left_pane_width += 1U;
goto redraw;
}
}
break;
case LIBTERMINPUT_META | LIBTERMINPUT_SHIFT:
case LIBTERMINPUT_META | LIBTERMINPUT_CTRL:
case LIBTERMINPUT_META | LIBTERMINPUT_CTRL | LIBTERMINPUT_SHIFT:
default:
break;
}
}
}
int
main(int argc, char *argv[])
{
int default_font_size_set = 0;
int min_font_size_set = 0;
int max_font_size_set = 0;
const char *font_family = "sans";
size_t i, len;
struct libterminput_state input_ctx;
union libterminput_input input;
ARGBEGIN {
case 'B':
listing = 1;
break;
case 'S':
listing = 0;
break;
case 'b':
use_bold = 1;
break;
case 'i':
use_italic = 1;
break;
case 'f':
font_family = ARG();
break;
case 's':
parse_fontsize(ARG(), &default_font_size_set, &min_font_size_set, &max_font_size_set);
break;
default:
usage();
} ARGEND;
if (argc)
usage();
if (min_font_size_set && max_font_size_set) {
if (min_font_size > max_font_size)
eprintf("minimum font size cannot be greater than maximum font size");
} else if (min_font_size_set) {
if (max_font_size < min_font_size)
max_font_size = min_font_size;
} else if (max_font_size_set) {
if (min_font_size > max_font_size)
min_font_size = max_font_size;
}
if (default_font_size_set) {
if (default_font_size < min_font_size) {
if (min_font_size_set)
eprintf("default font size cannot be less than the minimum font size");
min_font_size = default_font_size;
}
if (default_font_size > max_font_size) {
if (max_font_size_set)
eprintf("default font size cannot be greater than the minimum font size");
max_font_size = default_font_size;
}
} else {
if (default_font_size < min_font_size)
default_font_size = min_font_size;
else if (default_font_size > max_font_size)
default_font_size = max_font_size;
}
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO))
eprintf("<stdin> and <stdout> are expected to be terminal devices");
left_pane_width = strlen("No Block"); /* wider than "All" */
for (i = 0; i < libcmap_block_list_size; i++) {
len = strlen(libcmap_block_list[i].name);
if (len > left_pane_width)
left_pane_width = len;
}
for (i = 0; i < libcmap_script_list_size; i++) {
len = strlen(libcmap_script_list[i].name);
if (len > left_pane_width)
left_pane_width = len;
}
atexit(&cleanup);
libsimple_eprintf_preprint = &cleanup;
setlocale(LC_ALL, "");
configure_terminal();
subscribe_term_resize();
memset(&input_ctx, 0, sizeof(input_ctx));
libterminput_init(&input_ctx, STDIN_FILENO);
goto beginning;
while (!exiting) {
if (interrupt_deferred) {
interrupt_deferred = 0;
goto interrupted;
}
switch (libterminput_read(STDIN_FILENO, &input, &input_ctx)) {
case 1:
handle_keyboard_input(&input);
break;
case 0:
exiting = 1;
break;
default:
if (errno != EINTR)
eprintf("libterminput <stdin>:");
interrupted:
if (term_resized) {
beginning:
term_resized = 0;
update_term_size();
/* TODO redraw */
}
break;
}
}
restore_terminal();
libterminput_destroy(&input_ctx);
return 0;
}
|