-
Notifications
You must be signed in to change notification settings - Fork 52
/
options.c
625 lines (558 loc) · 19.4 KB
/
options.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/* options.c
Copyright 1999-2000 Tom Gilbert <tom@linuxbrit.co.uk,
gilbertt@linuxbrit.co.uk,
scrot_sucks@linuxbrit.co.uk>
Copyright 2008 William Vera <billy@billy.com.mx>
Copyright 2009 George Danchev <danchev@spnet.net>
Copyright 2009 James Cameron <quozl@us.netrek.org>
Copyright 2010 Ibragimov Rinat <ibragimovrinat@mail.ru>
Copyright 2017 Stoney Sauce <stoneysauce@gmail.com>
Copyright 2019 Daniel Lublin <daniel@lublin.se>
Copyright 2019-2023 Daniel T. Borelli <danieltborelli@gmail.com>
Copyright 2019 Jade Auer <jade@trashwitch.dev>
Copyright 2020 Sean Brennan <zettix1@gmail.com>
Copyright 2021 Christopher R. Nelson <christopher.nelson@languidnights.com>
Copyright 2021-2023 Guilherme Janczak <guilherme.janczak@yandex.com>
Copyright 2021 IFo Hancroft <contact@ifohancroft.com>
Copyright 2021 Peter Wu <peterwu@hotmail.com>
Copyright 2021 Wilson Smith <01wsmith+gh@gmail.com>
Copyright 2022 Zev Weiss <zev@bewilderbeest.net>
Copyright 2023 NRK <nrk@disroot.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "note.h"
#include "options.h"
#include "scrot.h"
#include "scrot_selection.h"
#include "util.h"
#define STR_LEN_MAX_FILENAME(msg, fileName) do { \
if (strlen((fileName)) > MAX_FILENAME) { \
errx(EXIT_FAILURE, #msg " filename too long, must be " \
"less than %d characters", MAX_FILENAME); \
} \
} while(0)
#define checkMaxInputFileName(fileName) \
STR_LEN_MAX_FILENAME(input, (fileName))
enum {
MAX_FILENAME = 256, // characters
};
struct ScrotOptions opt = {
.quality = 75,
.lineStyle = LineSolid,
.lineWidth = 1,
.lineOpacity = SELECTION_OPACITY_DEFAULT,
.lineMode = LINE_MODE_S_CLASSIC,
.stackDirection = HORIZONTAL,
.monitor = -1,
.windowId = None,
.outputFile = "%Y-%m-%d-%H%M%S_$wx$h_scrot.png",
};
static void showUsage(void);
static void showVersion(void);
static long long optionsParseNumBase(const char *, long long, long long,
const char *[static 1], int);
static void optionsParseThumbnail(char *);
static char *optionsNameThumbnail(const char *);
long long optionsParseNum(const char *str, long long min, long long max,
const char *errmsg[static 1])
{
return optionsParseNumBase(str, min, max, errmsg, 10);
}
/* optionsParseNumBase: "string to number" function.
*
* Parses the string representation of an integer in str, and simultaneously
* ensures that it is >= min and <= max. The string representation must be of
* base `base`.
*
* Returns the integer and sets *errmsg to NULL on success.
* Returns 0 and sets *errmsg to a pointer to a string containing the
* reason why the number can't be parsed on error.
*/
static long long optionsParseNumBase(const char *str, long long min,
long long max, const char *errmsg[static 1], int base)
{
char *end = NULL;
long long rval;
int saved_errno = errno;
if (str == NULL) {
*errmsg = "missing";
return 0;
}
*errmsg = NULL;
errno = 0;
rval = strtoll(str, &end, base);
if (errno == ERANGE) {
*errmsg = "not representable";
} else if (*str == '\0') {
*errmsg = "the null string";
} else if (*end != '\0') {
*errmsg = "not an integer";
} else if (rval < min) {
/*
* rval could be set to 0 due to strtoll() returning error and this
* could be smaller than min or larger than max. To make sure we don't
* return the wrong error message, put min/max checks after everything
* else.
*/
*errmsg = min == 0 ? "negative" : "too small";
} else if (rval > max) {
*errmsg = "too large";
}
errno = saved_errno;
return (*errmsg ? 0 : rval);
}
static bool optionsParseIsString(const char *const str)
{
return (str && (str[0] != '\0'));
}
static void optionsParseStack(const char *optarg)
{
// the suboption it's optional
if (!optarg) {
opt.stackDirection = HORIZONTAL;
return;
}
const char *value = strchr(optarg, '=');
if (value)
++value;
else
value = optarg;
if (*value == 'v')
opt.stackDirection = VERTICAL;
else if (*value == 'h')
opt.stackDirection = HORIZONTAL;
else {
errx(EXIT_FAILURE, "option --stack: Unknown value for suboption '%s'",
value);
}
}
static void optionsParseSelection(const char *optarg)
{
// the suboption it's optional
if (!optarg) {
opt.selection.mode = SELECTION_MODE_CAPTURE;
return;
}
const char *value = strchr(optarg, '=');
if (value)
++value;
else
value = optarg;
if (!strncmp(value, SELECTION_MODE_S_CAPTURE, SELECTION_MODE_L_CAPTURE)) {
opt.selection.mode = SELECTION_MODE_CAPTURE;
return; /* it has no parameter */
}
else if (!strncmp(value, SELECTION_MODE_S_HIDE, SELECTION_MODE_L_HIDE)) {
opt.selection.mode = SELECTION_MODE_HIDE;
value += SELECTION_MODE_L_HIDE;
}
else if (!strncmp(value, SELECTION_MODE_S_HOLE, SELECTION_MODE_L_HOLE)) {
opt.selection.mode = SELECTION_MODE_HOLE;
}
else if (!strncmp(value, SELECTION_MODE_S_BLUR, SELECTION_MODE_L_BLUR)) {
opt.selection.mode = SELECTION_MODE_BLUR;
opt.selection.paramNum = SELECTION_MODE_BLUR_DEFAULT;
value += SELECTION_MODE_L_BLUR;
}
else {
errx(EXIT_FAILURE, "option --select: Unknown value for suboption '%s'",
value);
}
if (opt.selection.mode & SELECTION_MODE_NOT_NEED_PARAM)
return;
if (*value != SELECTION_MODE_SEPARATOR)
return;
if (*(++value) == '\0')
errx(EXIT_FAILURE, "option --select: Invalid parameter.");
if (opt.selection.mode == SELECTION_MODE_BLUR) {
const char *errmsg;
opt.selection.paramNum = optionsParseNum(value,
SELECTION_MODE_BLUR_MIN, SELECTION_MODE_BLUR_MAX, &errmsg);
if (errmsg)
errx(EXIT_FAILURE, "option --select: '%s' is %s", value, errmsg);
} else { // SELECTION_MODE_HIDE
checkMaxInputFileName(value);
opt.selection.paramStr = estrdup(value);
}
}
static void optionsParseLine(char *optarg)
{
enum {
Style = 0,
Width,
Color,
Opacity,
Mode
};
char *const token[] = {
[Style] = "style",
[Width] = "width",
[Color] = "color",
[Opacity] = "opacity",
[Mode] = "mode",
NULL
};
char *subopts = optarg;
char *value = NULL;
const char *errmsg;
while (*subopts != '\0') {
switch (getsubopt(&subopts, token, &value)) {
case Style:
if (!optionsParseIsString(value)) {
errx(EXIT_FAILURE, "Missing value for suboption '%s'",
token[Style]);
}
if (!strncmp(value, "dash", 4))
opt.lineStyle = LineOnOffDash;
else if (!strncmp(value, "solid", 5))
opt.lineStyle = LineSolid;
else {
errx(EXIT_FAILURE, "Unknown value for suboption '%s': %s",
token[Style], value);
}
break;
case Width:
opt.lineWidth = optionsParseNum(value, 1, 8, &errmsg);
if (errmsg) {
if (value == NULL)
value = "(null)";
errx(EXIT_FAILURE, "option --line: suboption '%s': '%s' is %s",
token[Width], value, errmsg);
}
break;
case Color:
if (!optionsParseIsString(value)) {
errx(EXIT_FAILURE, "Missing value for suboption '%s'",
token[Color]);
}
opt.lineColor = estrdup(value);
break;
case Mode:
if (!optionsParseIsString(value)) {
errx(EXIT_FAILURE, "Missing value for suboption '%s'",
token[Mode]);
}
bool isValidMode = !strncmp(value, LINE_MODE_S_CLASSIC, LINE_MODE_L_CLASSIC);
isValidMode = isValidMode || !strncmp(value, LINE_MODE_S_EDGE, LINE_MODE_L_EDGE);
if (!isValidMode) {
errx(EXIT_FAILURE, "Unknown value for suboption '%s': %s",
token[Mode], value);
}
opt.lineMode = estrdup(value);
break;
case Opacity:
opt.lineOpacity = optionsParseNum(value,
SELECTION_OPACITY_MIN, SELECTION_OPACITY_MAX, &errmsg);
if (errmsg) {
if (value == NULL)
value = "(null)";
errx(EXIT_FAILURE, "option --line: suboption %s: '%s' is %s",
token[Opacity], value, errmsg);
}
break;
default:
errx(EXIT_FAILURE, "No match found for token: '%s'", value);
break;
}
} /* while */
}
static bool accessFileOk(const char *const pathName)
{
errno = 0;
return (0 == access(pathName, W_OK));
}
static const char *getPathOfStdout(void)
{
const char *paths[] = { "/dev/stdout", "/dev/fd/1", "/proc/self/fd/1" };
for (size_t i = 0; i < ARRAY_COUNT(paths); ++i) {
if (accessFileOk(paths[i]))
return paths[i];
}
err(EXIT_FAILURE, "access to stdout failed");
}
void optionsParse(int argc, char *argv[])
{
enum { /* long opt only */
/* ensure these don't collude with single byte opts. */
OPT_FORMAT = UCHAR_MAX + 1,
};
static char stropts[] = "a:bC:cD:d:e:F:fhik::l:M:mn:opq:S:s::t:uvw:z";
static struct option lopts[] = {
{"autoselect", required_argument, NULL, 'a'},
{"border", no_argument, NULL, 'b'},
{"class", required_argument, NULL, 'C'},
{"count", no_argument, NULL, 'c'},
{"display", required_argument, NULL, 'D'},
{"delay", required_argument, NULL, 'd'},
{"exec", required_argument, NULL, 'e'},
{"file", required_argument, NULL, 'F'},
{"freeze", no_argument, NULL, 'f'},
{"help", no_argument, NULL, 'h'},
{"ignorekeyboard", no_argument, NULL, 'i'},
{"stack", optional_argument, NULL, 'k'},
{"line", required_argument, NULL, 'l'},
{"monitor", required_argument, NULL, 'M'},
{"multidisp", no_argument, NULL, 'm'},
{"note", required_argument, NULL, 'n'},
{"overwrite", no_argument, NULL, 'o'},
{"pointer", no_argument, NULL, 'p'},
{"quality", required_argument, NULL, 'q'},
{"script", required_argument, NULL, 'S'},
{"select", optional_argument, NULL, 's'},
{"thumb", required_argument, NULL, 't'},
{"focused", no_argument, NULL, 'u'},
/* macquarie dictionary has both spellings */
{"focussed", no_argument, NULL, 'u'},
{"version", no_argument, NULL, 'v'},
{"window", required_argument, NULL, 'w'},
{"silent", no_argument, NULL, 'z'},
{"format", required_argument, NULL, OPT_FORMAT},
{0}
};
int optch;
const char *errmsg;
bool FFlagSet = false;
/* Now to pass some optionarinos */
while ((optch = getopt_long(argc, argv, stropts, lopts, NULL)) != -1) {
switch (optch) {
case 'a':
optionsParseAutoselect(optarg);
break;
case 'b':
opt.border = 1;
break;
case 'C':
opt.windowClassName = optarg;
break;
case 'c':
opt.countdown = 1;
break;
case 'D':
opt.display = optarg;
break;
case 'd':
if (*optarg == 'b') {
opt.delay_selection = true;
++optarg;
}
opt.delay = optionsParseNum(optarg, 0, INT_MAX, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --delay: '%s' is %s", optarg,
errmsg);
}
break;
case 'e':
opt.exec = estrdup(optarg);
break;
case 'F':
FFlagSet = true;
opt.outputFile = optarg;
break;
case 'f':
opt.freeze = 1;
break;
case 'h':
showUsage();
break;
case 'i':
opt.ignoreKeyboard = 1;
break;
case 'k':
opt.stack = 1;
optionsParseStack(optarg);
break;
case 'l':
optionsParseLine(optarg);
break;
case 'M':
opt.monitor = optionsParseNum(optarg, 0, INT_MAX, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --monitor: '%s' is %s", optarg,
errmsg);
}
break;
case 'm':
opt.multidisp = 1;
break;
case 'n':
optionsParseNote(optarg);
break;
case 'o':
opt.overwrite = 1;
break;
case 'p':
opt.pointer = 1;
break;
case 'q':
opt.quality = optionsParseNum(optarg, 1, 100, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --quality: '%s' is %s", optarg,
errmsg);
}
break;
case 'S':
opt.script = estrdup(optarg);
break;
case 's':
optionsParseSelection(optarg);
break;
case 't':
optionsParseThumbnail(optarg);
break;
case 'u':
opt.focused = 1;
break;
case 'v':
showVersion();
break;
case 'w':
opt.windowId = optionsParseNumBase(optarg, None/*0L*/, LONG_MAX, &errmsg, 0);
if (errmsg) {
errx(EXIT_FAILURE, "option --window: '%s' is %s", optarg,
errmsg);
}
break;
case 'z':
opt.silent = 1;
break;
case OPT_FORMAT:
opt.format = optarg;
break;
default:
exit(EXIT_FAILURE);
}
}
argv += optind;
if (!FFlagSet && *argv) {
opt.outputFile = *argv;
argv++;
}
for (; *argv; ++argv)
warnx("ignoring extraneous non-option argument: %s", *argv);
if (strcmp(opt.outputFile, "-") == 0) {
opt.overwrite = 1;
opt.thumb = THUMB_DISABLED;
opt.outputFile = getPathOfStdout();
}
if (opt.thumb != THUMB_DISABLED)
opt.thumbFile = optionsNameThumbnail(opt.outputFile);
}
static void showUsage(void)
{
fputs(/* Check that everything lines up after any changes. */
"usage: " PACKAGE " [-bcfhimopuvz] [-a X,Y,W,H] [-C NAME] [-D DISPLAY]\n"
" [-d SEC] [-e CMD] [-k OPT] [-l STYLE] [-M NUM] [-n OPTS]\n"
" [-q NUM] [-S CMD] [-s OPTS] [-t % | WxH] [[-F] FILE]\n",
stdout);
exit(0);
}
static void showVersion(void)
{
printf(PACKAGE " version " VERSION "\n");
exit(0);
}
static char *optionsNameThumbnail(const char *name)
{
const ptrdiff_t nameLength = strlen(name);
const char thumbSuffix[] = "-thumb";
Stream ret = {0};
const char *const extension = strrchr(name, '.');
const ptrdiff_t baseNameLength = extension ? extension-name : nameLength;
streamMem(&ret, name, baseNameLength);
streamMem(&ret, thumbSuffix, sizeof(thumbSuffix)-1);
if (extension)
streamMem(&ret, extension, nameLength - baseNameLength);
streamChar(&ret, '\0');
return ret.buf;
}
void optionsParseAutoselect(char *optarg)
{
char *token;
int *dimensions[] = {&opt.autoselectX, &opt.autoselectY, &opt.autoselectW,
&opt.autoselectH, NULL /* Sentinel. */};
int i = 0;
int min;
const char *errmsg;
/* Geometry dimensions must be in format x,y,w,h */
token = strtok(optarg, ",");
for (; token != NULL; token = strtok(NULL, ",")) {
if (dimensions[i] == NULL)
errx(EXIT_FAILURE, "option --autoselect: too many dimensions");
min = i >= 2; /* X,Y offsets may be 0. Width and height may not. */
*dimensions[i] = optionsParseNum(token, min, INT_MAX, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --autoselect: '%s' is %s", token,
errmsg);
}
i++;
opt.autoselect = 1;
}
if (i < 4)
errx(EXIT_FAILURE, "option --autoselect: too few dimensions");
}
static void optionsParseThumbnail(char *optarg)
{
char *height;
const char *errmsg;
if ((height = strchr(optarg, 'x')) != NULL) { /* optarg is a resolution. */
/* optarg holds the width, height holds the height. */
*height++ = '\0';
opt.thumb = THUMB_RES;
opt.thumbW = optionsParseNum(optarg, 0, INT_MAX, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --thumb: resolution width '%s' is %s",
optarg, errmsg);
}
opt.thumbH = optionsParseNum(height, 0, INT_MAX, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --thumb: resolution height '%s' is %s",
height, errmsg);
}
if (opt.thumbW == 0 && opt.thumbH == 0)
errx(EXIT_FAILURE, "option --thumb: both width and height are 0");
} else { /* optarg is a percentage. */
opt.thumb = THUMB_PERCENT;
opt.thumbPercent = optionsParseNum(optarg, 1, 100, &errmsg);
if (errmsg) {
errx(EXIT_FAILURE, "option --thumb: percentage '%s' is %s", optarg,
errmsg);
}
}
}
void optionsParseNote(char *optarg)
{
if (opt.note)
free(opt.note);
opt.note = estrdup(optarg);
if (!opt.note)
return;
if (opt.note[0] == '\0')
errx(EXIT_FAILURE, "Required arguments for --note.");
scrotNoteNew(opt.note);
}