2021-10-28 12:41:16 +02:00
|
|
|
/* Copyright 2011-2020 Bert Muennich
|
2023-01-15 10:26:46 +01:00
|
|
|
* Copyright 2021-2023 nsxiv contributors
|
2011-01-17 14:57:59 +01:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* This file is a part of nsxiv.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is free software; you can redistribute it and/or modify
|
2013-02-08 21:52:41 +01:00
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2021-09-16 11:32:59 +02:00
|
|
|
* nsxiv is distributed in the hope that it will be useful,
|
2013-02-08 21:52:41 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-09-16 11:32:59 +02:00
|
|
|
* along with nsxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 14:57:59 +01:00
|
|
|
*/
|
|
|
|
|
2021-09-16 11:32:59 +02:00
|
|
|
#include "nsxiv.h"
|
2018-06-09 11:33:30 +02:00
|
|
|
#include "version.h"
|
2022-08-17 16:54:14 +02:00
|
|
|
#define INCLUDE_OPTIONS_CONFIG
|
|
|
|
#include "config.h"
|
2017-10-16 21:10:35 +02:00
|
|
|
|
2022-08-16 10:43:50 +02:00
|
|
|
#include <assert.h>
|
2022-08-17 16:54:14 +02:00
|
|
|
#include <limits.h>
|
2022-05-31 13:25:14 +02:00
|
|
|
#include <stdio.h>
|
2022-06-16 09:39:03 +02:00
|
|
|
#include <stdlib.h>
|
2011-02-14 17:51:04 +01:00
|
|
|
#include <string.h>
|
2011-09-08 20:54:24 +02:00
|
|
|
#include <unistd.h>
|
2011-01-17 16:40:54 +01:00
|
|
|
|
2022-08-16 10:43:50 +02:00
|
|
|
#define OPTPARSE_IMPLEMENTATION
|
|
|
|
#define OPTPARSE_API static
|
|
|
|
#pragma GCC diagnostic push /* also works on clang */
|
|
|
|
#pragma GCC diagnostic ignored "-Wshadow"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-function"
|
|
|
|
#include "optparse.h"
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
2021-11-20 04:51:49 +01:00
|
|
|
const opt_t *options;
|
2011-01-17 16:40:54 +01:00
|
|
|
|
2024-08-02 13:05:05 +02:00
|
|
|
void print_usage(FILE *stream)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2024-08-02 13:05:05 +02:00
|
|
|
fprintf(stream,
|
|
|
|
"usage: %s [-abcfhiopqrtvZ0] [-A FRAMERATE] [-e WID] [-G GAMMA] "
|
|
|
|
"[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] "
|
|
|
|
"[-z ZOOM] FILES...\n",
|
|
|
|
progname);
|
2011-01-17 16:40:54 +01:00
|
|
|
}
|
|
|
|
|
2021-11-20 04:51:49 +01:00
|
|
|
static void print_version(void)
|
2013-02-08 22:05:31 +01:00
|
|
|
{
|
2022-07-15 07:56:45 +02:00
|
|
|
printf("%s %s\n", progname, VERSION);
|
2023-07-08 08:53:57 +02:00
|
|
|
fputs("features: "
|
|
|
|
#if HAVE_INOTIFY
|
|
|
|
"+inotify "
|
|
|
|
#endif
|
|
|
|
#if HAVE_LIBFONTS
|
|
|
|
"+statusbar "
|
|
|
|
#endif
|
|
|
|
#if HAVE_LIBEXIF
|
|
|
|
"+exif "
|
|
|
|
#endif
|
|
|
|
#if HAVE_IMLIB2_MULTI_FRAME
|
|
|
|
"+multiframe "
|
|
|
|
#endif
|
|
|
|
"\n", stdout);
|
2011-01-17 16:40:54 +01:00
|
|
|
}
|
2011-01-18 17:20:41 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void parse_options(int argc, char **argv)
|
|
|
|
{
|
2023-01-17 10:48:59 +01:00
|
|
|
enum {
|
|
|
|
/* ensure these can't be represented in a single byte in order
|
|
|
|
* to avoid conflicts with short opts
|
|
|
|
*/
|
|
|
|
OPT_START = UCHAR_MAX,
|
|
|
|
OPT_AA,
|
2023-10-01 13:06:04 +02:00
|
|
|
OPT_AL,
|
2024-06-20 12:50:19 +02:00
|
|
|
OPT_BG,
|
|
|
|
OPT_CA,
|
|
|
|
OPT_CD
|
2022-08-17 16:54:14 +02:00
|
|
|
};
|
2022-08-16 10:43:50 +02:00
|
|
|
static const struct optparse_long longopts[] = {
|
2022-08-17 16:54:14 +02:00
|
|
|
{ "framerate", 'A', OPTPARSE_REQUIRED },
|
|
|
|
{ "animate", 'a', OPTPARSE_NONE },
|
|
|
|
{ "no-bar", 'b', OPTPARSE_NONE },
|
|
|
|
{ "clean-cache", 'c', OPTPARSE_NONE },
|
|
|
|
{ "embed", 'e', OPTPARSE_REQUIRED },
|
|
|
|
{ "fullscreen", 'f', OPTPARSE_NONE },
|
|
|
|
{ "gamma", 'G', OPTPARSE_REQUIRED },
|
|
|
|
{ "geometry", 'g', OPTPARSE_REQUIRED },
|
|
|
|
{ "help", 'h', OPTPARSE_NONE },
|
|
|
|
{ "stdin", 'i', OPTPARSE_NONE },
|
|
|
|
{ "class", 'N', OPTPARSE_REQUIRED },
|
|
|
|
{ "start-at", 'n', OPTPARSE_REQUIRED },
|
|
|
|
{ "stdout", 'o', OPTPARSE_NONE },
|
|
|
|
{ "private", 'p', OPTPARSE_NONE },
|
|
|
|
{ "quiet", 'q', OPTPARSE_NONE },
|
|
|
|
{ "recursive", 'r', OPTPARSE_NONE },
|
|
|
|
{ "ss-delay", 'S', OPTPARSE_REQUIRED },
|
|
|
|
{ "scale-mode", 's', OPTPARSE_REQUIRED },
|
|
|
|
{ "thumbnail", 't', OPTPARSE_NONE },
|
|
|
|
{ "version", 'v', OPTPARSE_NONE },
|
|
|
|
{ "zoom-100", 'Z', OPTPARSE_NONE },
|
|
|
|
{ "zoom", 'z', OPTPARSE_REQUIRED },
|
|
|
|
{ "null", '0', OPTPARSE_NONE },
|
|
|
|
{ "anti-alias", OPT_AA, OPTPARSE_OPTIONAL },
|
2023-01-17 10:48:59 +01:00
|
|
|
{ "alpha-layer", OPT_AL, OPTPARSE_OPTIONAL },
|
2023-10-01 13:06:04 +02:00
|
|
|
/* TODO: document this when it's stable */
|
|
|
|
{ "bg-cache", OPT_BG, OPTPARSE_OPTIONAL },
|
2024-06-20 12:50:19 +02:00
|
|
|
{ "cache-allow", OPT_CA, OPTPARSE_REQUIRED },
|
|
|
|
{ "cache-deny", OPT_CD, OPTPARSE_REQUIRED },
|
2022-08-16 10:43:50 +02:00
|
|
|
{ 0 }, /* end */
|
|
|
|
};
|
|
|
|
|
2022-09-14 06:58:41 +02:00
|
|
|
long n, opt;
|
|
|
|
float f;
|
2014-02-04 23:05:29 +01:00
|
|
|
char *end, *s;
|
2022-08-16 10:43:50 +02:00
|
|
|
struct optparse op;
|
|
|
|
const char scalemodes[] = "dfFwh"; /* must be sorted according to scalemode_t */
|
2021-11-20 04:51:49 +01:00
|
|
|
static opt_t _options;
|
2011-05-25 09:23:23 +02:00
|
|
|
|
2022-08-16 10:43:50 +02:00
|
|
|
options = &_options;
|
2013-03-19 21:11:29 +01:00
|
|
|
_options.from_stdin = false;
|
|
|
|
_options.to_stdout = false;
|
2021-10-28 07:43:36 +02:00
|
|
|
_options.using_null = false;
|
2011-09-11 21:01:24 +02:00
|
|
|
_options.recursive = false;
|
2011-05-25 09:23:23 +02:00
|
|
|
_options.startnum = 0;
|
2011-01-19 14:07:45 +01:00
|
|
|
|
2014-02-04 23:03:53 +01:00
|
|
|
_options.scalemode = SCALE_DOWN;
|
2011-01-28 13:34:16 +01:00
|
|
|
_options.zoom = 1.0;
|
2022-09-10 15:30:40 +02:00
|
|
|
_options.anti_alias = ANTI_ALIAS;
|
2023-01-17 10:48:59 +01:00
|
|
|
_options.alpha_layer = ALPHA_LAYER;
|
2014-07-25 22:52:31 +02:00
|
|
|
_options.animate = false;
|
2013-11-13 20:54:09 +01:00
|
|
|
_options.gamma = 0;
|
2014-01-04 18:38:40 +01:00
|
|
|
_options.slideshow = 0;
|
2016-11-27 08:54:15 +01:00
|
|
|
_options.framerate = 0;
|
2011-01-28 13:34:16 +01:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
_options.fullscreen = false;
|
2016-10-29 04:09:26 +02:00
|
|
|
_options.embed = 0;
|
2012-02-16 23:20:27 +01:00
|
|
|
_options.hide_bar = false;
|
2011-01-30 22:00:58 +01:00
|
|
|
_options.geometry = NULL;
|
2012-10-29 00:53:50 +01:00
|
|
|
_options.res_name = NULL;
|
2011-01-27 16:15:43 +01:00
|
|
|
|
2024-06-20 12:50:19 +02:00
|
|
|
_options.tns_filters = TNS_FILTERS;
|
|
|
|
_options.tns_filters_is_blacklist = TNS_FILTERS_IS_BLACKLIST;
|
2011-09-11 21:01:24 +02:00
|
|
|
_options.quiet = false;
|
|
|
|
_options.thumb_mode = false;
|
|
|
|
_options.clean_cache = false;
|
2017-07-13 02:15:51 +02:00
|
|
|
_options.private_mode = false;
|
2023-10-01 13:06:04 +02:00
|
|
|
_options.background_cache = false;
|
2011-01-30 16:39:16 +01:00
|
|
|
|
2022-06-16 15:33:32 +02:00
|
|
|
if (argc > 0) {
|
|
|
|
s = strrchr(argv[0], '/');
|
|
|
|
progname = s != NULL && s[1] != '\0' ? s + 1 : argv[0];
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:43:50 +02:00
|
|
|
optparse_init(&op, argv);
|
|
|
|
while ((opt = optparse_long(&op, longopts, NULL)) != -1) {
|
|
|
|
for (n = 0; n < (int)ARRLEN(longopts); ++n) { /* clang-tidy finds some non-sensical branch and thinks optarg == NULL is possible */
|
|
|
|
if (opt == longopts[n].shortname && longopts[n].argtype == OPTPARSE_REQUIRED)
|
|
|
|
assert(op.optarg != NULL);
|
|
|
|
}
|
2011-01-19 14:07:45 +01:00
|
|
|
switch (opt) {
|
2022-08-16 10:54:31 +02:00
|
|
|
case '?':
|
|
|
|
fprintf(stderr, "%s\n", op.errmsg);
|
2024-08-02 13:05:05 +02:00
|
|
|
print_usage(stderr);
|
2022-08-16 10:54:31 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
case 'A':
|
|
|
|
n = strtol(op.optarg, &end, 0);
|
2022-09-14 06:58:41 +02:00
|
|
|
if (*end != '\0' || n <= 0 || n > INT_MAX)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid framerate: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.framerate = n;
|
|
|
|
/* fall through */
|
|
|
|
case 'a':
|
|
|
|
_options.animate = true;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
_options.hide_bar = true;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
_options.clean_cache = true;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
n = strtol(op.optarg, &end, 0);
|
|
|
|
if (*end != '\0')
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid window id: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.embed = n;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
_options.fullscreen = true;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
n = strtol(op.optarg, &end, 0);
|
2022-09-14 06:58:41 +02:00
|
|
|
if (*end != '\0' || n < INT_MIN || n > INT_MAX)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid gamma: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.gamma = n;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
_options.geometry = op.optarg;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2024-08-02 13:05:05 +02:00
|
|
|
print_usage(stdout);
|
2022-08-16 10:54:31 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'i':
|
|
|
|
_options.from_stdin = true;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
n = strtol(op.optarg, &end, 0);
|
2022-09-14 06:58:41 +02:00
|
|
|
if (*end != '\0' || n <= 0 || n > INT_MAX)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid starting number: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.startnum = n - 1;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
_options.res_name = op.optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
_options.to_stdout = true;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
_options.private_mode = true;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
_options.quiet = true;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
_options.recursive = true;
|
|
|
|
break;
|
|
|
|
case 'S':
|
2022-09-14 06:58:41 +02:00
|
|
|
f = strtof(op.optarg, &end) * 10.0f;
|
|
|
|
if (*end != '\0' || f <= 0 || f >= (float)UINT_MAX)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid slideshow delay: %s", op.optarg);
|
2022-09-14 06:58:41 +02:00
|
|
|
_options.slideshow = (unsigned int)f;
|
2022-08-16 10:54:31 +02:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
s = strchr(scalemodes, op.optarg[0]);
|
|
|
|
if (s == NULL || *s == '\0' || strlen(op.optarg) != 1)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid scale mode: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.scalemode = s - scalemodes;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
_options.thumb_mode = true;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
print_version();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'Z':
|
|
|
|
_options.scalemode = SCALE_ZOOM;
|
|
|
|
_options.zoom = 1.0f;
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
n = strtol(op.optarg, &end, 0);
|
|
|
|
if (*end != '\0' || n <= 0)
|
2022-09-28 18:15:09 +02:00
|
|
|
error(EXIT_FAILURE, 0, "Invalid zoom level: %s", op.optarg);
|
2022-08-16 10:54:31 +02:00
|
|
|
_options.scalemode = SCALE_ZOOM;
|
2023-02-10 06:35:53 +01:00
|
|
|
_options.zoom = (float)n / 100.0f;
|
2022-08-16 10:54:31 +02:00
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
_options.using_null = true;
|
|
|
|
break;
|
2022-08-17 16:54:14 +02:00
|
|
|
case OPT_AA:
|
|
|
|
if (op.optarg != NULL && !STREQ(op.optarg, "no"))
|
|
|
|
error(EXIT_FAILURE, 0, "Invalid argument for option --anti-alias: %s", op.optarg);
|
2022-09-10 15:30:40 +02:00
|
|
|
_options.anti_alias = op.optarg == NULL;
|
2022-08-17 16:54:14 +02:00
|
|
|
break;
|
2023-01-17 10:48:59 +01:00
|
|
|
case OPT_AL:
|
|
|
|
if (op.optarg != NULL && !STREQ(op.optarg, "no"))
|
|
|
|
error(EXIT_FAILURE, 0, "Invalid argument for option --alpha-layer: %s", op.optarg);
|
|
|
|
_options.alpha_layer = op.optarg == NULL;
|
|
|
|
break;
|
2023-10-01 13:06:04 +02:00
|
|
|
case OPT_BG:
|
|
|
|
if (op.optarg != NULL && !STREQ(op.optarg, "no"))
|
|
|
|
error(EXIT_FAILURE, 0, "Invalid argument for option --bg-cache: %s", op.optarg);
|
|
|
|
_options.background_cache = op.optarg == NULL;
|
|
|
|
break;
|
2024-06-20 12:50:19 +02:00
|
|
|
case OPT_CA: case OPT_CD:
|
|
|
|
_options.tns_filters = op.optarg;
|
|
|
|
_options.tns_filters_is_blacklist = (opt == OPT_CD);
|
|
|
|
break;
|
2011-01-19 14:07:45 +01:00
|
|
|
}
|
|
|
|
}
|
2011-01-20 16:03:59 +01:00
|
|
|
|
2022-08-16 10:43:50 +02:00
|
|
|
_options.filenames = argv + op.optind;
|
|
|
|
_options.filecnt = argc - op.optind;
|
2013-03-19 21:11:29 +01:00
|
|
|
|
|
|
|
if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
|
|
|
|
_options.filenames++;
|
|
|
|
_options.filecnt--;
|
|
|
|
_options.from_stdin = true;
|
|
|
|
}
|
2011-01-18 17:20:41 +01:00
|
|
|
}
|