diff options
author | Bert Münnich <be.muennich@gmail.com> | 2013-03-19 21:11:29 +0100 |
---|---|---|
committer | Bert Münnich <be.muennich@gmail.com> | 2013-03-19 21:14:58 +0100 |
commit | 6f05e777280cd36a8ccaf20182c4e8a0554bb563 (patch) | |
tree | 07029d49c352d9a2fd26ff810a551ebfae9afe23 | |
parent | 447bc1c784cc358078f4ffbbac0c63ee74a069c8 (diff) |
New options: -[io], read/write files from/to stdin/out
Fixes issue #84
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | commands.c | 7 | ||||
-rw-r--r-- | main.c | 49 | ||||
-rw-r--r-- | options.c | 20 | ||||
-rw-r--r-- | options.h | 1 | ||||
-rw-r--r-- | sxiv.1 | 17 |
6 files changed, 59 insertions, 37 deletions
@@ -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 @@ -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); } @@ -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]; + } + + for (i = 0; i < options->filecnt; i++) { + filename = options->filenames[i]; - if (stat(filename, &fstats) < 0) { - warn("could not stat file: %s", filename); + 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)) { + 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); - } 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); + free((void*) filename); } + r_closedir(&dir); + if (fileidx - start > 1) + qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp); } } @@ -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; + } } @@ -26,6 +26,7 @@ typedef struct { /* file list: */ char **filenames; bool from_stdin; + bool to_stdout; bool recursive; int filecnt; int startnum; @@ -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 |