From 6f05e777280cd36a8ccaf20182c4e8a0554bb563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bert=20M=C3=BCnnich?= Date: Tue, 19 Mar 2013 21:11:29 +0100 Subject: [PATCH] New options: -[io], read/write files from/to stdin/out Fixes issue #84 --- README.md | 2 ++ commands.c | 7 +++++++ main.c | 51 +++++++++++++++++++++++++-------------------------- options.c | 20 ++++++++++++++++---- options.h | 1 + sxiv.1 | 17 +++++++++-------- 6 files changed, 60 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index d8c1c7a..5c4dd96 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,10 @@ of small previews is displayed, making it easy to choose an image to open. -f Start in fullscreen mode -g GEOMETRY Set window position and size (see section GEOMETRY SPECIFICATIONS of X(7)) + -i Read file list from stdin -n NUM Start at picture NUM -N NAME Set X window resource name to NAME + -o Write file list to stdout when quitting -p Pixelize, i.e. turn off image anti-aliasing -q Be quiet, disable warnings -r Search given directories recursively for images diff --git a/commands.c b/commands.c index 3e108a4..0ab1944 100644 --- a/commands.c +++ b/commands.c @@ -26,6 +26,7 @@ #include "commands.h" #include "image.h" +#include "options.h" #include "thumbs.h" #include "util.h" #include "config.h" @@ -57,6 +58,12 @@ const int ss_delays[] = { bool it_quit(arg_t a) { + unsigned int i; + + if (options->to_stdout) { + for (i = 0; i < filecnt; i++) + printf("%s\n", files[i].name); + } cleanup(); exit(EXIT_SUCCESS); } diff --git a/main.c b/main.c index e6d6700..cda0c4d 100644 --- a/main.c +++ b/main.c @@ -622,7 +622,7 @@ int main(int argc, char **argv) exit(EXIT_SUCCESS); } - if (options->filecnt == 0) { + if (options->filecnt == 0 && !options->from_stdin) { print_usage(); exit(EXIT_FAILURE); } @@ -635,7 +635,6 @@ int main(int argc, char **argv) files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); fileidx = 0; - /* build file list: */ if (options->from_stdin) { filename = NULL; while ((len = get_line(&filename, &n, stdin)) > 0) { @@ -645,34 +644,34 @@ int main(int argc, char **argv) } if (filename != NULL) free(filename); - } else { - for (i = 0; i < options->filecnt; i++) { - filename = options->filenames[i]; + } - if (stat(filename, &fstats) < 0) { - warn("could not stat file: %s", filename); + for (i = 0; i < options->filecnt; i++) { + filename = options->filenames[i]; + + if (stat(filename, &fstats) < 0) { + warn("could not stat file: %s", filename); + continue; + } + if (!S_ISDIR(fstats.st_mode)) { + check_add_file(filename); + } else { + if (!options->recursive) { + warn("ignoring directory: %s", filename); continue; } - if (!S_ISDIR(fstats.st_mode)) { - check_add_file(filename); - } else { - if (!options->recursive) { - warn("ignoring directory: %s", filename); - continue; - } - if (r_opendir(&dir, filename) < 0) { - warn("could not open directory: %s", filename); - continue; - } - start = fileidx; - while ((filename = r_readdir(&dir)) != NULL) { - check_add_file(filename); - free((void*) filename); - } - r_closedir(&dir); - if (fileidx - start > 1) - qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); + if (r_opendir(&dir, filename) < 0) { + warn("could not open directory: %s", filename); + continue; } + start = fileidx; + while ((filename = r_readdir(&dir)) != NULL) { + check_add_file(filename); + free((void*) filename); + } + r_closedir(&dir); + if (fileidx - start > 1) + qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); } } diff --git a/options.c b/options.c index 0354616..2d35992 100644 --- a/options.c +++ b/options.c @@ -33,7 +33,7 @@ const options_t *options = (const options_t*) &_options; void print_usage(void) { - printf("usage: sxiv [-bcdFfhpqrstvZ] [-g GEOMETRY] [-n NUM] " + printf("usage: sxiv [-bcdFfhiopqrstvZ] [-g GEOMETRY] [-n NUM] " "[-N name] [-z ZOOM] FILES...\n"); } @@ -46,6 +46,8 @@ void parse_options(int argc, char **argv) { int opt, t; + _options.from_stdin = false; + _options.to_stdout = false; _options.recursive = false; _options.startnum = 0; @@ -63,7 +65,7 @@ void parse_options(int argc, char **argv) _options.thumb_mode = false; _options.clean_cache = false; - while ((opt = getopt(argc, argv, "bcdFfg:hn:N:pqrstvZz:")) != -1) { + while ((opt = getopt(argc, argv, "bcdFfg:hin:N:opqrstvZz:")) != -1) { switch (opt) { case '?': print_usage(); @@ -89,6 +91,9 @@ void parse_options(int argc, char **argv) case 'h': print_usage(); exit(EXIT_SUCCESS); + case 'i': + _options.from_stdin = true; + break; case 'n': if (sscanf(optarg, "%d", &t) <= 0 || t < 1) { fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", @@ -101,6 +106,9 @@ void parse_options(int argc, char **argv) case 'N': _options.res_name = optarg; break; + case 'o': + _options.to_stdout = true; + break; case 'p': _options.aa = false; break; @@ -137,6 +145,10 @@ void parse_options(int argc, char **argv) _options.filenames = argv + optind; _options.filecnt = argc - optind; - _options.from_stdin = _options.filecnt == 1 && - STREQ(_options.filenames[0], "-"); + + if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) { + _options.filenames++; + _options.filecnt--; + _options.from_stdin = true; + } } diff --git a/options.h b/options.h index f21410e..d60bc9a 100644 --- a/options.h +++ b/options.h @@ -26,6 +26,7 @@ typedef struct { /* file list: */ char **filenames; bool from_stdin; + bool to_stdout; bool recursive; int filecnt; int startnum; diff --git a/sxiv.1 b/sxiv.1 index 6f61561..85872e3 100644 --- a/sxiv.1 +++ b/sxiv.1 @@ -3,7 +3,7 @@ sxiv \- Simple X Image Viewer .SH SYNOPSIS .B sxiv -.RB [ \-bcdFfhpqrstvZ ] +.RB [ \-bcdFfhiopqrstvZ ] .RB [ \-g .IR GEOMETRY ] .RB [ \-n @@ -17,13 +17,6 @@ sxiv \- Simple X Image Viewer sxiv is a simple image viewer for X. It only has the most basic features required for fast image viewing. .P -sxiv opens all named -.IR FILE s, -or reads the names of the files to open from standard input, if only a single -hyphen\-minus -.RB ( \- ) -is given. -.P sxiv has two modes of operation: image and thumbnail mode. The default is image mode, in which only the current image is shown. In thumbnail mode a grid of small previews is displayed, making it easy to choose an image to open. @@ -65,6 +58,14 @@ Set the resource name of sxiv's X window to NAME. .B \-h Print brief usage information to standard output and exit. .TP +.B \-i +Read names of files to open from standard input. +.TP +.B \-o +Write list of opened files to standard output when quitting. If combined with +.IR \-i , +then sxiv acts as a visual filter/pipe. +.TP .B \-p Pixelize images, i.e. turn off anti-aliasing. .TP