mpd_update_queue.c (1664B)
1 #define _POSIX_C_SOURCE 200809L 2 3 #include "mpd_common.h" 4 5 #include <limits.h> 6 #include <string.h> 7 #include <unistd.h> 8 9 static void usage(FILE *file, const char *program) { 10 fprintf(file, "Usage: %s [--force] [FILE|-]\n", program); 11 } 12 13 int main(int argc, char **argv) { 14 bool force = false; 15 const char *path = NULL; 16 for (int i = 1; i < argc; i++) { 17 if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { 18 usage(stdout, argv[0]); 19 return 0; 20 } 21 if (strcmp(argv[i], "--force") == 0) { 22 force = true; 23 } else if (argv[i][0] == '-' && strcmp(argv[i], "-") != 0) { 24 usage(stderr, argv[0]); 25 return 2; 26 } else if (path == NULL) { 27 path = argv[i]; 28 } else { 29 usage(stderr, argv[0]); 30 return 2; 31 } 32 } 33 34 bool close_input; 35 FILE *input = mt_open_input(path, &close_input); 36 if (input == NULL) 37 return 1; 38 if (path == NULL && isatty(STDIN_FILENO)) 39 fprintf(stderr, 40 "Reading the new queue from stdin; press Ctrl-D to finish.\n"); 41 42 struct mt_strvec tracks; 43 mt_strvec_init(&tracks); 44 bool ok = mt_read_lines(input, &tracks); 45 if (close_input && fclose(input) != 0) { 46 perror("mpd: close input"); 47 ok = false; 48 } 49 if (!ok) { 50 mt_strvec_clear(&tracks); 51 return 1; 52 } 53 54 struct mpd_connection *connection = mt_connect(); 55 if (connection == NULL) { 56 mt_strvec_clear(&tracks); 57 return 1; 58 } 59 ok = mt_replace_queue(connection, &tracks, UINT_MAX, force); 60 if (ok) 61 printf("Queue replaced with %zu track%s.\n", tracks.len, 62 tracks.len == 1U ? "" : "s"); 63 mpd_connection_free(connection); 64 mt_strvec_clear(&tracks); 65 return ok ? 0 : 1; 66 }