diff options
author | Nathan Sketch <sketchn98@gmail.com> | 2021-02-07 07:52:14 -0500 |
---|---|---|
committer | Michael Homer <mwh@mwh.geek.nz> | 2021-03-08 10:11:50 +1300 |
commit | d94c9290a0a4752c8d318b49d86eb66dfde090c3 (patch) | |
tree | 246e01dc6a9ab3e4976c39be4412429580a3ef9f | |
parent | Use --on-top, -T for always-on-top switch (diff) | |
download | dragon-d94c9290a0a4752c8d318b49d86eb66dfde090c3.tar.gz dragon-d94c9290a0a4752c8d318b49d86eb66dfde090c3.tar.bz2 dragon-d94c9290a0a4752c8d318b49d86eb66dfde090c3.zip |
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.
-rw-r--r-- | dragon.c | 75 |
1 files changed, 54 insertions, 21 deletions
@@ -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<argc; i++) { if (strcmp(argv[i], "--help") == 0) { mode = MODE_HELP; @@ -363,6 +400,7 @@ int main (int argc, char **argv) { " instead of URIs\n"); printf(" --all, -a drag all files at once\n"); printf(" --on-top, -T make window always-on-top\n"); + printf(" --stdin, -I read input from stdin\n"); printf(" --verbose, -v be verbose\n"); printf(" --help show help\n"); printf(" --version show version details\n"); @@ -398,6 +436,9 @@ int main (int argc, char **argv) { } else if (strcmp(argv[i], "-T") == 0 || strcmp(argv[i], "--on-top") == 0) { always_on_top = true; + } else if (strcmp(argv[i], "-I") == 0 + || strcmp(argv[i], "--stdin") == 0) { + from_stdin = true; } else if (argv[i][0] == '-') { fprintf(stderr, "%s: error: unknown option `%s'.\n", progname, argv[i]); @@ -438,27 +479,19 @@ int main (int argc, char **argv) { exit(0); } - if (drag_all) { - uri_collection = malloc(sizeof(char*) * ((argc > 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<argc; i++) { - if (argv[i][0] != '-') { - filename = argv[i]; - 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); - } - had_filename = true; - } + if (argv[i][0] != '-') + make_btn(argv[i]); } - if (!had_filename) { + if (from_stdin) + readstdin(); + + if (!uri_count) { printf("Usage: %s [OPTIONS] FILENAME\n", progname); exit(0); } |