-
Notifications
You must be signed in to change notification settings - Fork 34
/
imgcat.c
493 lines (413 loc) · 14.7 KB
/
imgcat.c
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright (c) 2014–2018, Eddie Antonio Santos <easantos@ualberta.ca>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* Feature-test macro for fileno(3), fdopen(3), and mkstemp(3), snprintf(3). */
#define _XOPEN_SOURCE 600
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <limits.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#include <sysexits.h>
#include <term.h>
#include "print_image.h"
#include "config.h"
enum colors_t {
C_ANSI = 8,
C_256 = 256,
C_24BIT = 256 * 256 * 256,
};
_Static_assert(C_24BIT >= 0, "enum type overflows :(");
/* All the information I care about the terminal. */
struct terminal_t {
int width;
int height;
unsigned colors;
bool isatty;
Format optimum_format;
};
/**
* Global containing all relevant command line options.
*/
static struct {
Format format;
bool should_resize;
int width;
int height;
bool use_half_height;
bool use_fake_terminal;
bool should_preserve_aspect_ratio;
} options = {
.format = F_UNSET, /* Default: autodetect highest fidelity. */
.should_resize = true, /* Default: yes! */
.width = WIDTH_UNSET,
.height = HEIGHT_UNSET,
.use_half_height = false,
.use_fake_terminal = false,
.should_preserve_aspect_ratio = true
};
/**
* Info about the user's real terminal.
*/
static struct terminal_t real_terminal = {
.width = WIDTH_UNSET,
.height = HEIGHT_UNSET,
.colors = 0,
.isatty = false,
.optimum_format = F_8_COLOR
};
/**
* Fake terminal used in --x-terminal-override.
*/
static struct terminal_t fake_terminal = { 0 };
/* Global temporary filename for dumping stdin into. */
static char tempfile_name[PATH_MAX + NAME_MAX + 1];
static char tempfile_name_template[NAME_MAX + 1];
/* Long options */
static struct option long_options[] = {
/* Options affecting output colour depth. */
{ "depth", required_argument, NULL, 'd' },
/* Options affecting size. */
{ "no-resize", no_argument, NULL, 'R' },
{ "width", required_argument, NULL, 'w' },
{ "height", required_argument, NULL, 'r' },
{ "half-height", no_argument, NULL, 'H' },
{ "no-preserve-aspect-ratio", no_argument, NULL, 'P' },
/* Abbreviated options. */
{ "8", no_argument, (int*) &options.format, F_8_COLOR },
{ "ansi", no_argument, (int*) &options.format, F_8_COLOR },
{ "256", no_argument, (int*) &options.format, F_256_COLOR },
{ "true", no_argument, (int*) &options.format, F_TRUE_COLOR },
{ "24bit", no_argument, (int*) &options.format, F_TRUE_COLOR },
{ "iterm2", no_argument, (int*) &options.format, F_ITERM2 },
/* Common options. */
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' },
/* Options for internal use and debugging. */
/* These flags are EXPLICITLY undocumented, as they are for development
* use only, and can change or be removed at any time. */
{ "x-terminal-override", required_argument, NULL, 'x' },
{ NULL, 0, NULL, 0 }
};
/** Returns the filename of the image to open. */
static const char* parse_args(int argc, char **argv);
static void bad_usage(const char *msg, ...) __attribute__((noreturn));
static void fatal_error(int code, const char *msg, ...) __attribute__((noreturn));
static void determine_terminal_capabilities();
static void determine_optimum_color_format(struct terminal_t *);
static void set_fake_terminal(const char *);
static void usage(FILE *dest);
static const char *dump_stdin_into_tempfile();
/* Set first thing in main(). */
static char const* program_name;
int main(int argc, char **argv) {
int desired_width = WIDTH_UNSET;
int desired_height = HEIGHT_UNSET;
bool status;
const char *image_name;
Format color_format = F_UNSET;
struct terminal_t* terminal;
program_name = argv[0];
image_name = parse_args(argc, argv);
if (image_name == NULL) {
if (isatty(fileno(stdin))) {
/* No image is specified on the command line, and there's nothing
* redirected to stdin. */
bad_usage("Must specify an image file.");
} else {
/* There's an image redirected to stdin. */
image_name = dump_stdin_into_tempfile();
}
}
/* Determine whether to use the real termainal or the fake (overridden)
* terminal */
if (options.use_fake_terminal) {
/* For debugging and testing, use the overridden terminal. */
terminal = &fake_terminal;
assert(terminal->isatty);
fprintf(stderr, "Using overridden terminal: %dx%d at %d colors\n",
terminal->width, terminal->height, terminal->colors);
} else {
determine_terminal_capabilities();
terminal = &real_terminal;
}
/* Determine if the image should be resized. */
if (options.should_resize) {
/* Use explicitly provided dimensions. */
if (options.width != WIDTH_UNSET) {
desired_width = options.width;
}
if (options.height != HEIGHT_UNSET) {
desired_height = options.height;
}
}
/* Set color format either from options, or infered from the terminal. */
if (options.format != F_UNSET) {
color_format = options.format;
} else {
color_format = terminal->optimum_format;
}
PrintRequest request = (PrintRequest) {
.filename = image_name,
.desired_width = desired_width,
.desired_height = desired_height,
.max_width = terminal->width,
.max_height = terminal->height,
.half_height = options.use_half_height,
.format = color_format,
.preserve_aspect_ratio = options.should_preserve_aspect_ratio
};
status = print_image(&request);
if (!status) {
bad_usage("Failed to open image: %s", image_name);
}
return EXIT_SUCCESS;
}
/**
* Get the color capability from the terminfo database.
*/
static int get_terminal_colours() {
char tbuf[1024];
if (tgetent(tbuf, getenv("TERM")) != 1) {
return -1;
}
// Check if the COLORTERM variable is defined and advertizes support for
// true color. There doesn't seem to be a standard way to test for this.
const char *ct = getenv("COLORTERM");
if (ct && (strcmp(ct, "truecolor") == 0 || strcmp(ct, "24bit") == 0)) {
return C_24BIT;
}
return tigetnum("colors");
}
/**
* Determines the terminal's capabilities:
* its optimum colour depth and dimensions.
*/
static void determine_terminal_capabilities() {
int stdout_fd = fileno(stdout);
struct winsize ws;
/* If stdout is NOT a terminal (it's redirected to a file perhaps),
* just bail. */
if (!isatty(stdout_fd)) {
return;
}
real_terminal.isatty = true;
/* Get the current window size. */
assert(ioctl(stdout_fd, TIOCGWINSZ, &ws) != -1);
real_terminal.width = ws.ws_col;
real_terminal.height = ws.ws_row;
real_terminal.colors = get_terminal_colours();
/* ITERM_SESSION_ID is exported in iTerm2 sessions. */
if (getenv("ITERM_SESSION_ID") != NULL) {
real_terminal.optimum_format = F_ITERM2;
} else {
/* Otherwise, determine the capability from the reported colours. */
determine_optimum_color_format(&real_terminal);
}
}
/**
* Figures out the ideal color format for a terminal, by only considering its
* self-reported color depth.
*/
static void determine_optimum_color_format(struct terminal_t *terminal) {
if (terminal->colors >= C_24BIT) {
terminal->optimum_format = F_TRUE_COLOR;
} else if (terminal->colors >= C_256) {
terminal->optimum_format = F_256_COLOR;
} else if (terminal->colors >= C_ANSI) {
terminal->optimum_format = F_8_COLOR;
} else {
fprintf(stderr, "%s: Cannot determine optimum color depth for "
"reported %d terminal colors setting\n",
program_name, terminal->colors);
}
}
/**
* Removes the temporary file. Intended to be the atexit() callback.
*/
static void unlink_tempfile(void) {
remove(tempfile_name);
}
/**
* This is necessary because CImg likes to close and reopen the file it's
* reading, which discards header data when reading from /dev/stdin.
*/
static const char *dump_stdin_into_tempfile() {
int byte;
/* Set up the mutable buffer for mkstemp() to do its magic. */
strncpy(tempfile_name_template, "imgcat.XXXXXXXX", NAME_MAX);
snprintf(tempfile_name, PATH_MAX + NAME_MAX, "%s/%s", P_tmpdir, tempfile_name_template);
int output_fd = mkstemp(tempfile_name);
if (output_fd == -1) {
fatal_error(EX_TEMPFAIL, "could not create temporary file: %s",
strerror(errno));
}
FILE *output = fdopen(output_fd, "wb");
if (output == NULL) {
fatal_error(EX_IOERR, "could not open temporary file: %s",
strerror(errno));
}
/* TODO: do something better than a byte-for-byte file transfer. */
while ((byte = getchar()) != EOF) {
fputc(byte, output);
}
fclose(output);
/* Remove the file at ordinary exit. */
atexit(unlink_tempfile);
return tempfile_name;
}
static void usage(FILE *dest) {
const int field_width = strlen(program_name);
fprintf(dest, "Usage:\n");
fprintf(dest,
"\t%s" " [--width=<columns> --height=<rows>|--no-resize] [--no-preserve-aspect-ratio]\n"
"\t%*c" " [--half-height] [--depth=(8|256|24bit|iterm2)] IMAGE\n",
program_name, field_width, ' ');
fprintf(dest, "\t"
"%s --version\n", program_name);
fprintf(dest, "\t"
"%s --help\n", program_name);
}
static void bad_usage(const char *msg, ...) {
va_list args;
fprintf(stderr, "%s: ", program_name);
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
usage(stderr);
exit(EX_USAGE);
}
static void fatal_error(int code, const char *msg, ...) {
va_list args;
fprintf(stderr, "%s: ", program_name);
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, "\n");
exit(code);
}
static Format parse_format(const char *arg) {
# define argeq(b) (strncmp(arg, (b), (sizeof(b))) == 0)
if (argeq("true") || argeq("24bit")) {
return F_TRUE_COLOR;
} else if (argeq("256")) {
return F_256_COLOR;
} else if (argeq("8") || argeq("ansi")) {
return F_8_COLOR;
} else if (argeq("iterm2")) {
return F_ITERM2;
}
return F_UNSET;
# undef argeq
}
static const char* parse_args(int argc, char **argv) {
int c;
/* Disable getopt_long from printing to stderr. */
extern int opterr;
opterr = 0;
while (1) {
c = getopt_long(argc, argv, "w:r:d:PRHhv", long_options, NULL);
if (c == -1) {
break;
}
switch (c) {
case 'w': /* --width */
options.width = (int)strtol(optarg, NULL, 10);
if (options.width < 1) {
bad_usage("Width must be a positive integer, not '%s'",
optarg);
}
break;
case 'r': /* --rows */
options.height = (int)strtol(optarg, NULL, 10);
if (options.height < 1) {
bad_usage("Height must be a positive integer, not '%s'",
optarg);
}
break;
case 'd': /* --depth=(8|ansi|256|24bit|true|iterm2) */
options.format = parse_format(optarg);
if (options.format == F_UNSET) {
bad_usage("Unknown output format: %s", optarg);
}
break;
case 'P': /* --no-preserve-aspect-ratio */
options.should_preserve_aspect_ratio = false;
break;
case 'R': /* --no-resize */
options.should_resize = false;
break;
case 'H': /* --half-height */
options.use_half_height = true;
break;
case 'h': /* --help */
usage(stdout);
exit(EXIT_SUCCESS);
break;
case 'v': /* --version */
printf("%s %s\n", program_name, PACKAGE_VERSION);
exit(EXIT_SUCCESS);
break;
case 'x': /* --x-terminal-override */
options.use_fake_terminal = true;
set_fake_terminal(optarg);
break;
case 0:
/* Set an abbreviated option like --8, --ansi, --256. */
break;
/* Unknown option. */
case '?':
default:
bad_usage("Unknown option: %s", argv[optind - 1]);
}
}
if (argc == optind) {
/* This means getopt() has exhausted all of argv and has not found a
* valid image name argument. */
return NULL;
} else {
return argv[optind];
}
}
/**
* Parses the --x-terminal-override string.
*
* Options are: WIDTHxHEIGHTxCOLORS
*/
static void set_fake_terminal(const char *override_string) {
int width, height, colors;
int opts = sscanf(override_string, "%dx%d:%d", &width, &height, &colors);
if (opts != 3) {
bad_usage("invalid override string: %s", override_string);
}
if (width <= 0 || height <= 0 || colors <= 0) {
bad_usage("invalid settings: %s", override_string);
}
fake_terminal.width = width;
fake_terminal.height = height;
fake_terminal.colors = colors;
determine_optimum_color_format(&fake_terminal);
/* We're doing this --x-terminal-override stuff to *simulate* isatty
* without calling it, so ALWAYS set isatty to true. */
fake_terminal.isatty = true;
}