From d94c9290a0a4752c8d318b49d86eb66dfde090c3 Mon Sep 17 00:00:00 2001 From: Nathan Sketch Date: Sun, 7 Feb 2021 07:52:14 -0500 Subject: Add -I/--stdin option to read filenames from stdin This option expects one path per line (and so doesn't accept paths containing newlines). Files are treated as though passed on the command line, and the window will not appear until stdin is closed. --- dragon.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 21 deletions(-) (limited to 'dragon.c') diff --git a/dragon.c b/dragon.c index 75a294a..f81a39c 100644 --- a/dragon.c +++ b/dragon.c @@ -40,6 +40,8 @@ bool print_path = false; bool icons_only = false; bool always_on_top = false; +static char *stdin_files; + #define MODE_HELP 1 #define MODE_TARGET 2 #define MODE_VERSION 4 @@ -55,7 +57,7 @@ struct draggable_thing { // MODE_ALL #define MAX_SIZE 100 char** uri_collection; -int uri_count; +int uri_count = 0; bool drag_all = false; // --- @@ -167,11 +169,12 @@ GtkButton *add_button(char *label, struct draggable_thing *dragdata, int type) { if (drag_all) { if (uri_count < MAX_SIZE) { - uri_collection[uri_count++] = dragdata->uri; + uri_collection[uri_count] = dragdata->uri; } else { fprintf(stderr, "Exceeded maximum number of files for drag_all (%d)\n", MAX_SIZE); } } + uri_count++; return (GtkButton *)button; } @@ -348,9 +351,43 @@ void target_mode() { gtk_main(); } +void make_btn(char *filename) { + if (!is_uri(filename)) { + add_filename_button(filename); + } else if (is_file_uri(filename)) { + GFile *file = g_file_new_for_uri(filename); + add_file_button(file); + } else { + add_uri_button(filename); + } +} + +static void readstdin(void) { + char *write_pos = stdin_files, *newline; + size_t max_size = BUFSIZ * 2, cur_size = 0; + // read each line from stdin and add it to the item list + while (fgets(write_pos, BUFSIZ, stdin)) { + if (write_pos[0] == '-') + continue; + if ((newline = strchr(write_pos, '\n'))) + *newline = '\0'; + else + break; + make_btn(write_pos); + cur_size = newline - stdin_files + 1; + if (max_size < cur_size + BUFSIZ) { + if (!(stdin_files = realloc(stdin_files, (max_size += BUFSIZ)))) + fprintf(stderr, "%s: cannot realloc %lu bytes.\n", progname, max_size); + newline = stdin_files + cur_size - 1; + } + write_pos = newline + 1; + } +} + int main (int argc, char **argv) { + bool from_stdin = false; + stdin_files = malloc(BUFSIZ * 2); progname = argv[0]; - char *filename = NULL; for (int i=1; i MAX_SIZE ? argc : MAX_SIZE) + 1)); - uri_count = 0; - } + if (from_stdin) + uri_collection = malloc(sizeof(char*) * (MAX_SIZE + 1)); + else if (drag_all) + uri_collection = malloc(sizeof(char*) * ((argc > MAX_SIZE ? argc : MAX_SIZE) + 1)); - bool had_filename = false; for (int i=1; i