Krolyxon
Commit 6f05e77

Branches

Refs →

nsxiv6f05e77

New options: -[io], read/write files from/to stdin/out

Fixes issue #84
Author
Bert Münnich
Date
2013-03-19 21:11 +0100
Committer
Bert Münnich · 2013-03-19 21:14 +0100
Parent
447bc1c
Commit
6f05e777280cd36a8ccaf20182c4e8a0554bb563

+59 −37

6 files changed

  1. M README.md +2 −0
  2. M commands.c +7 −0
  3. M main.c +24 −25
  4. M options.c +16 −4
  5. M options.h +1 −0
  6. M sxiv.1 +9 −8
M README.md View
@@ -75,8 +75,10 @@ of small previews is displayed, making it easy to choose an image to open.
7575 -f Start in fullscreen mode
7676 -g GEOMETRY Set window position and size
7777 (see section GEOMETRY SPECIFICATIONS of X(7))
78+ -i Read file list from stdin
7879 -n NUM Start at picture NUM
7980 -N NAME Set X window resource name to NAME
81+ -o Write file list to stdout when quitting
8082 -p Pixelize, i.e. turn off image anti-aliasing
8183 -q Be quiet, disable warnings
8284 -r Search given directories recursively for images
M commands.c View
@@ -26,6 +26,7 @@
2626
2727 #include "commands.h"
2828 #include "image.h"
29+#include "options.h"
2930 #include "thumbs.h"
3031 #include "util.h"
3132 #include "config.h"
@@ -57,6 +58,12 @@ const int ss_delays[] = {
5758
5859 bool it_quit(arg_t a)
5960 {
61+ unsigned int i;
62+
63+ if (options->to_stdout) {
64+ for (i = 0; i < filecnt; i++)
65+ printf("%s\n", files[i].name);
66+ }
6067 cleanup();
6168 exit(EXIT_SUCCESS);
6269 }
M main.c View
@@ -622,7 +622,7 @@ int main(int argc, char **argv)
622622 exit(EXIT_SUCCESS);
623623 }
624624
625− if (options->filecnt == 0) {
625+ if (options->filecnt == 0 && !options->from_stdin) {
626626 print_usage();
627627 exit(EXIT_FAILURE);
628628 }
@@ -635,7 +635,6 @@ int main(int argc, char **argv)
635635 files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
636636 fileidx = 0;
637637
638− /* build file list: */
639638 if (options->from_stdin) {
640639 filename = NULL;
641640 while ((len = get_line(&filename, &n, stdin)) > 0) {
@@ -645,34 +644,34 @@ int main(int argc, char **argv)
645644 }
646645 if (filename != NULL)
647646 free(filename);
648− } else {
649− for (i = 0; i < options->filecnt; i++) {
650− filename = options->filenames[i];
647+ }
648+
649+ for (i = 0; i < options->filecnt; i++) {
650+ filename = options->filenames[i];
651651
652− if (stat(filename, &fstats) < 0) {
653− warn("could not stat file: %s", filename);
652+ if (stat(filename, &fstats) < 0) {
653+ warn("could not stat file: %s", filename);
654+ continue;
655+ }
656+ if (!S_ISDIR(fstats.st_mode)) {
657+ check_add_file(filename);
658+ } else {
659+ if (!options->recursive) {
660+ warn("ignoring directory: %s", filename);
654661 continue;
655662 }
656− if (!S_ISDIR(fstats.st_mode)) {
663+ if (r_opendir(&dir, filename) < 0) {
664+ warn("could not open directory: %s", filename);
665+ continue;
666+ }
667+ start = fileidx;
668+ while ((filename = r_readdir(&dir)) != NULL) {
657669 check_add_file(filename);
658− } else {
659− if (!options->recursive) {
660− warn("ignoring directory: %s", filename);
661− continue;
662− }
663− if (r_opendir(&dir, filename) < 0) {
664− warn("could not open directory: %s", filename);
665− continue;
666− }
667− start = fileidx;
668− while ((filename = r_readdir(&dir)) != NULL) {
669− check_add_file(filename);
670− free((void*) filename);
671− }
672− r_closedir(&dir);
673− if (fileidx - start > 1)
674− qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
670+ free((void*) filename);
675671 }
672+ r_closedir(&dir);
673+ if (fileidx - start > 1)
674+ qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
676675 }
677676 }
678677
M options.c View
@@ -33,7 +33,7 @@ const options_t *options = (const options_t*) &_options;
3333
3434 void print_usage(void)
3535 {
36− printf("usage: sxiv [-bcdFfhpqrstvZ] [-g GEOMETRY] [-n NUM] "
36+ printf("usage: sxiv [-bcdFfhiopqrstvZ] [-g GEOMETRY] [-n NUM] "
3737 "[-N name] [-z ZOOM] FILES...\n");
3838 }
3939
@@ -46,6 +46,8 @@ void parse_options(int argc, char **argv)
4646 {
4747 int opt, t;
4848
49+ _options.from_stdin = false;
50+ _options.to_stdout = false;
4951 _options.recursive = false;
5052 _options.startnum = 0;
5153
@@ -63,7 +65,7 @@ void parse_options(int argc, char **argv)
6365 _options.thumb_mode = false;
6466 _options.clean_cache = false;
6567
66− while ((opt = getopt(argc, argv, "bcdFfg:hn:N:pqrstvZz:")) != -1) {
68+ while ((opt = getopt(argc, argv, "bcdFfg:hin:N:opqrstvZz:")) != -1) {
6769 switch (opt) {
6870 case '?':
6971 print_usage();
@@ -89,6 +91,9 @@ void parse_options(int argc, char **argv)
8991 case 'h':
9092 print_usage();
9193 exit(EXIT_SUCCESS);
94+ case 'i':
95+ _options.from_stdin = true;
96+ break;
9297 case 'n':
9398 if (sscanf(optarg, "%d", &t) <= 0 || t < 1) {
9499 fprintf(stderr, "sxiv: invalid argument for option -n: %s\n",
@@ -101,6 +106,9 @@ void parse_options(int argc, char **argv)
101106 case 'N':
102107 _options.res_name = optarg;
103108 break;
109+ case 'o':
110+ _options.to_stdout = true;
111+ break;
104112 case 'p':
105113 _options.aa = false;
106114 break;
@@ -137,6 +145,10 @@ void parse_options(int argc, char **argv)
137145
138146 _options.filenames = argv + optind;
139147 _options.filecnt = argc - optind;
140− _options.from_stdin = _options.filecnt == 1 &&
141− STREQ(_options.filenames[0], "-");
148+
149+ if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
150+ _options.filenames++;
151+ _options.filecnt--;
152+ _options.from_stdin = true;
153+ }
142154 }
M options.h View
@@ -26,6 +26,7 @@ typedef struct {
2626 /* file list: */
2727 char **filenames;
2828 bool from_stdin;
29+ bool to_stdout;
2930 bool recursive;
3031 int filecnt;
3132 int startnum;
M sxiv.1 View
@@ -3,7 +3,7 @@
33 sxiv \- Simple X Image Viewer
44 .SH SYNOPSIS
55 .B sxiv
6−.RB [ \-bcdFfhpqrstvZ ]
6+.RB [ \-bcdFfhiopqrstvZ ]
77 .RB [ \-g
88 .IR GEOMETRY ]
99 .RB [ \-n
@@ -17,13 +17,6 @@ sxiv \- Simple X Image Viewer
1717 sxiv is a simple image viewer for X. It only has the most basic features
1818 required for fast image viewing.
1919 .P
20−sxiv opens all named
21−.IR FILE s,
22−or reads the names of the files to open from standard input, if only a single
23−hyphen\-minus
24−.RB ( \- )
25−is given.
26−.P
2720 sxiv has two modes of operation: image and thumbnail mode. The default is image
2821 mode, in which only the current image is shown. In thumbnail mode a grid of
2922 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.
6558 .B \-h
6659 Print brief usage information to standard output and exit.
6760 .TP
61+.B \-i
62+Read names of files to open from standard input.
63+.TP
64+.B \-o
65+Write list of opened files to standard output when quitting. If combined with
66+.IR \-i ,
67+then sxiv acts as a visual filter/pipe.
68+.TP
6869 .B \-p
6970 Pixelize images, i.e. turn off anti-aliasing.
7071 .TP