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

Fixes issue #84
This commit is contained in:
Bert Münnich 2013-03-19 21:11:29 +01:00
parent 447bc1c784
commit 6f05e77728
6 changed files with 60 additions and 38 deletions

View File

@ -75,8 +75,10 @@ of small previews is displayed, making it easy to choose an image to open.
-f Start in fullscreen mode -f Start in fullscreen mode
-g GEOMETRY Set window position and size -g GEOMETRY Set window position and size
(see section GEOMETRY SPECIFICATIONS of X(7)) (see section GEOMETRY SPECIFICATIONS of X(7))
-i Read file list from stdin
-n NUM Start at picture NUM -n NUM Start at picture NUM
-N NAME Set X window resource name to NAME -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 -p Pixelize, i.e. turn off image anti-aliasing
-q Be quiet, disable warnings -q Be quiet, disable warnings
-r Search given directories recursively for images -r Search given directories recursively for images

View File

@ -26,6 +26,7 @@
#include "commands.h" #include "commands.h"
#include "image.h" #include "image.h"
#include "options.h"
#include "thumbs.h" #include "thumbs.h"
#include "util.h" #include "util.h"
#include "config.h" #include "config.h"
@ -57,6 +58,12 @@ const int ss_delays[] = {
bool it_quit(arg_t a) 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(); cleanup();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }

51
main.c
View File

@ -622,7 +622,7 @@ int main(int argc, char **argv)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
if (options->filecnt == 0) { if (options->filecnt == 0 && !options->from_stdin) {
print_usage(); print_usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -635,7 +635,6 @@ int main(int argc, char **argv)
files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
fileidx = 0; fileidx = 0;
/* build file list: */
if (options->from_stdin) { if (options->from_stdin) {
filename = NULL; filename = NULL;
while ((len = get_line(&filename, &n, stdin)) > 0) { while ((len = get_line(&filename, &n, stdin)) > 0) {
@ -645,34 +644,34 @@ int main(int argc, char **argv)
} }
if (filename != NULL) if (filename != NULL)
free(filename); free(filename);
} else { }
for (i = 0; i < options->filecnt; i++) {
filename = options->filenames[i];
if (stat(filename, &fstats) < 0) { for (i = 0; i < options->filecnt; i++) {
warn("could not stat file: %s", filename); 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; continue;
} }
if (!S_ISDIR(fstats.st_mode)) { if (r_opendir(&dir, filename) < 0) {
check_add_file(filename); warn("could not open directory: %s", filename);
} else { continue;
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);
} }
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);
} }
} }

View File

@ -33,7 +33,7 @@ const options_t *options = (const options_t*) &_options;
void print_usage(void) 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"); "[-N name] [-z ZOOM] FILES...\n");
} }
@ -46,6 +46,8 @@ void parse_options(int argc, char **argv)
{ {
int opt, t; int opt, t;
_options.from_stdin = false;
_options.to_stdout = false;
_options.recursive = false; _options.recursive = false;
_options.startnum = 0; _options.startnum = 0;
@ -63,7 +65,7 @@ void parse_options(int argc, char **argv)
_options.thumb_mode = false; _options.thumb_mode = false;
_options.clean_cache = 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) { switch (opt) {
case '?': case '?':
print_usage(); print_usage();
@ -89,6 +91,9 @@ void parse_options(int argc, char **argv)
case 'h': case 'h':
print_usage(); print_usage();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 'i':
_options.from_stdin = true;
break;
case 'n': case 'n':
if (sscanf(optarg, "%d", &t) <= 0 || t < 1) { if (sscanf(optarg, "%d", &t) <= 0 || t < 1) {
fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", fprintf(stderr, "sxiv: invalid argument for option -n: %s\n",
@ -101,6 +106,9 @@ void parse_options(int argc, char **argv)
case 'N': case 'N':
_options.res_name = optarg; _options.res_name = optarg;
break; break;
case 'o':
_options.to_stdout = true;
break;
case 'p': case 'p':
_options.aa = false; _options.aa = false;
break; break;
@ -137,6 +145,10 @@ void parse_options(int argc, char **argv)
_options.filenames = argv + optind; _options.filenames = argv + optind;
_options.filecnt = argc - 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;
}
} }

View File

@ -26,6 +26,7 @@ typedef struct {
/* file list: */ /* file list: */
char **filenames; char **filenames;
bool from_stdin; bool from_stdin;
bool to_stdout;
bool recursive; bool recursive;
int filecnt; int filecnt;
int startnum; int startnum;

17
sxiv.1
View File

@ -3,7 +3,7 @@
sxiv \- Simple X Image Viewer sxiv \- Simple X Image Viewer
.SH SYNOPSIS .SH SYNOPSIS
.B sxiv .B sxiv
.RB [ \-bcdFfhpqrstvZ ] .RB [ \-bcdFfhiopqrstvZ ]
.RB [ \-g .RB [ \-g
.IR GEOMETRY ] .IR GEOMETRY ]
.RB [ \-n .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 sxiv is a simple image viewer for X. It only has the most basic features
required for fast image viewing. required for fast image viewing.
.P .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 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 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. 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 .B \-h
Print brief usage information to standard output and exit. Print brief usage information to standard output and exit.
.TP .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 .B \-p
Pixelize images, i.e. turn off anti-aliasing. Pixelize images, i.e. turn off anti-aliasing.
.TP .TP