mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2024-12-29 16:36:41 +01:00
parent
8f05b636f0
commit
b99f0ed0db
1 changed files with 23 additions and 9 deletions
32
zathura.c
32
zathura.c
|
@ -430,10 +430,24 @@ zathura_set_argv(zathura_t* zathura, char** argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
static gchar*
|
static gchar*
|
||||||
prepare_document_open_from_stdin(zathura_t* zathura)
|
prepare_document_open_from_stdin(zathura_t* zathura, const char* path)
|
||||||
{
|
{
|
||||||
g_return_val_if_fail(zathura, NULL);
|
g_return_val_if_fail(zathura, NULL);
|
||||||
|
|
||||||
|
int infileno = -1;
|
||||||
|
if (g_strcmp0(path, "-") == 0) {
|
||||||
|
infileno = fileno(stdin);
|
||||||
|
} else if (g_str_has_prefix(path, "/proc/self/fd/") == true) {
|
||||||
|
char* begin = g_strrstr(path, "/") + 1;
|
||||||
|
gint64 temp = g_ascii_strtoll(begin, NULL, 0);
|
||||||
|
if (temp > INT_MAX || temp < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
infileno = (int) temp;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
GError* error = NULL;
|
GError* error = NULL;
|
||||||
gchar* file = NULL;
|
gchar* file = NULL;
|
||||||
gint handle = g_file_open_tmp("zathura.stdin.XXXXXX", &file, &error);
|
gint handle = g_file_open_tmp("zathura.stdin.XXXXXX", &file, &error);
|
||||||
|
@ -445,10 +459,9 @@ prepare_document_open_from_stdin(zathura_t* zathura)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read from stdin and dump to temporary file
|
// read and dump to temporary file
|
||||||
int stdinfno = fileno(stdin);
|
if (infileno == -1) {
|
||||||
if (stdinfno == -1) {
|
girara_error("Can not read from file descriptor.");
|
||||||
girara_error("Can not read from stdin.");
|
|
||||||
close(handle);
|
close(handle);
|
||||||
g_unlink(file);
|
g_unlink(file);
|
||||||
g_free(file);
|
g_free(file);
|
||||||
|
@ -457,7 +470,7 @@ prepare_document_open_from_stdin(zathura_t* zathura)
|
||||||
|
|
||||||
char buffer[BUFSIZ];
|
char buffer[BUFSIZ];
|
||||||
ssize_t count = 0;
|
ssize_t count = 0;
|
||||||
while ((count = read(stdinfno, buffer, BUFSIZ)) > 0) {
|
while ((count = read(infileno, buffer, BUFSIZ)) > 0) {
|
||||||
if (write(handle, buffer, count) != count) {
|
if (write(handle, buffer, count) != count) {
|
||||||
girara_error("Can not write to temporary file: %s", file);
|
girara_error("Can not write to temporary file: %s", file);
|
||||||
close(handle);
|
close(handle);
|
||||||
|
@ -470,7 +483,7 @@ prepare_document_open_from_stdin(zathura_t* zathura)
|
||||||
close(handle);
|
close(handle);
|
||||||
|
|
||||||
if (count != 0) {
|
if (count != 0) {
|
||||||
girara_error("Can not read from stdin.");
|
girara_error("Can not read from file descriptor.");
|
||||||
g_unlink(file);
|
g_unlink(file);
|
||||||
g_free(file);
|
g_free(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -487,8 +500,9 @@ document_info_open(gpointer data)
|
||||||
|
|
||||||
if (document_info->zathura != NULL && document_info->path != NULL) {
|
if (document_info->zathura != NULL && document_info->path != NULL) {
|
||||||
char* file = NULL;
|
char* file = NULL;
|
||||||
if (g_strcmp0(document_info->path, "-") == 0) {
|
if (g_strcmp0(document_info->path, "-") == 0 ||
|
||||||
file = prepare_document_open_from_stdin(document_info->zathura);
|
g_str_has_prefix(document_info->path, "/proc/self/fd/") == true) {
|
||||||
|
file = prepare_document_open_from_stdin(document_info->zathura, document_info->path);
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
girara_notify(document_info->zathura->ui.session, GIRARA_ERROR,
|
girara_notify(document_info->zathura->ui.session, GIRARA_ERROR,
|
||||||
_("Could not read file from stdin and write it to a temporary file."));
|
_("Could not read file from stdin and write it to a temporary file."));
|
||||||
|
|
Loading…
Reference in a new issue