Something went wrong. Try again.
various tools I have been using throughout the years
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#define _POSIX_C_SOURCE 200809L
#include "mpd_common.h"
#include <limits.h>#include <string.h>#include <unistd.h>
static void usage(FILE *file, const char *program) { fprintf(file, "Usage: %s [--verbose] [FILE|-]\n", program);}
int main(int argc, char **argv) { bool verbose = false; const char *path = NULL; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(stdout, argv[0]); return 0; } if (strcmp(argv[i], "--verbose") == 0) { verbose = true; } else if ((argv[i][0] == '-' && strcmp(argv[i], "-") != 0) || path != NULL) { usage(stderr, argv[0]); return 2; } else { path = argv[i]; } }
bool close_input; FILE *input = mt_open_input(path, &close_input); if (input == NULL) return 1; if (path == NULL && isatty(STDIN_FILENO)) fprintf(stderr, "Reading tracks from stdin; press Ctrl-D to finish.\n");
struct mt_strvec tracks; mt_strvec_init(&tracks); bool ok = mt_read_lines(input, &tracks); if (close_input && fclose(input) != 0) { perror("mpd: close input"); ok = false; } if (!ok) { mt_strvec_clear(&tracks); return 1; }
struct mpd_connection *connection = mt_connect(); if (connection == NULL) { mt_strvec_clear(&tracks); return 1; } struct mt_player_state state; ok = mt_get_player_state(connection, &state); unsigned position = 0U; if (ok && state.song_pos != UINT_MAX) { if (state.song_pos == UINT_MAX - 1U) { fprintf(stderr, "mpd: current queue position is too large\n"); ok = false; } else { position = state.song_pos + 1U; } } if (ok) { enum mt_state_match match = mt_player_state_is(connection, state.queue_version, state.song_id); if (match == MT_STATE_CHANGED) { fprintf(stderr, "mpd: player or queue changed before insertion; retry the " "command\n"); ok = false; } else if (match == MT_STATE_ERROR) { ok = false; } } if (ok) ok = mt_insert_queue(connection, &tracks, position, verbose); if (ok) printf("Inserted %zu track%s at queue position %u.\n", tracks.len, tracks.len == 1U ? "" : "s", position);
mt_player_state_clear(&state); mpd_connection_free(connection); mt_strvec_clear(&tracks); return ok ? 0 : 1;}