diff --git a/src/cache.c b/src/cache.c index 1938d1b..dfe79fc 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1,6 +1,6 @@ /* * cache.c - * Copyright (c) 2014-2017 Arkadiusz Bokowy + * Copyright (c) 2014-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -122,8 +122,8 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) { struct cmusfm_cache_record *record; size_t record_size; - debug("cache update: %ld", sb_tinf->timestamp); - debug("payload: %s - %s (%s) - %d. %s (%ds)", + debug("Cache update: %ld", sb_tinf->timestamp); + debug("Payload: %s - %s (%s) - %d. %s (%ds)", sb_tinf->artist, sb_tinf->album, sb_tinf->album_artist, sb_tinf->track_number, sb_tinf->track, sb_tinf->duration); @@ -160,7 +160,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { size_t rd_len, record_size; char *ptr; - debug("cache submit"); + debug("Cache submit"); if ((f = fopen(cmusfm_cache_file, "r")) == NULL) return; @@ -187,13 +187,13 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { /* validate record type and first-stage data integration */ if (record->signature != CMUSFM_CACHE_SIGNATURE || record->checksum1 != get_cache_record_checksum1(record)) { - fprintf(stderr, "error: invalid cache record signature\n"); - debug("signature: %x, checksum: %x", record->signature, record->checksum1); + fprintf(stderr, "ERROR: Invalid cache record signature\n"); + debug("Signature: %x, checksum: %x", record->signature, record->checksum1); goto return_failure; } record_size = get_cache_record_size(record); - debug("record size: %ld", record_size); + debug("Record size: %ld", record_size); /* break if current record is truncated */ if ((void *)record - (void *)rd_buff + record_size > rd_len) @@ -201,7 +201,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { /* check for second-stage data integration */ if (record->checksum2 != get_cache_record_checksum2(record)) { - fprintf(stderr, "error: cache record data corrupted\n"); + fprintf(stderr, "ERROR: Cache record data corrupted\n"); goto return_failure; } @@ -235,7 +235,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) { } #endif - debug("cache: %s - %s (%s) - %d. %s (%ds)", + debug("Cache: %s - %s (%s) - %d. %s (%ds)", sb_tinf.artist, sb_tinf.album, sb_tinf.album_artist, sb_tinf.track_number, sb_tinf.track, sb_tinf.duration); diff --git a/src/libscrobbler2.c b/src/libscrobbler2.c index 0a71839..e28ea66 100644 --- a/src/libscrobbler2.c +++ b/src/libscrobbler2.c @@ -1,6 +1,6 @@ /* * libscrobbler2.c - * Copyright (c) 2011-2017 Arkadiusz Bokowy + * Copyright (c) 2011-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -54,7 +54,7 @@ static size_t sb_curl_write_callback(char *ptr, size_t size, size_t nmemb, struct sb_response_data *rd = (struct sb_response_data *)data; size *= nmemb; - debug("read: size: %zu, body: %s", size, ptr); + debug("Read: size: %zu, body: %s", size, ptr); /* XXX: Passing a zero bytes data to this callback is not en error, * however memory allocation fail is. */ @@ -111,7 +111,7 @@ static void sb_curl_cleanup(CURL *curl, struct sb_response_data *response) { static scrobbler_status_t sb_check_response(struct sb_response_data *response, int curl_status, scrobbler_session_t *sbs) { - debug("check: status: %d, body: %s", curl_status, response->data); + debug("Check: status: %d, body: %s", curl_status, response->data); /* network transfer failure (curl error) */ if (curl_status != 0) { @@ -160,7 +160,7 @@ static void sb_generate_method_signature(struct sb_getpost_data *sb_data, offset += sprintf(tmp_str + offset, format, sb_data[x].name, sb_data[x].data); } - debug("signature data: %s", tmp_str); + debug("Signature data: %s", tmp_str); strcat(tmp_str, secret_hex); MD5((unsigned char *)tmp_str, strlen(tmp_str), sign); } @@ -195,7 +195,7 @@ static char *sb_make_curl_getpost_string(CURL *curl, char *str_buffer, /* strip '&' from the end of the string */ str_buffer[offset - 1] = 0; - debug("params: %s", str_buffer); + debug("Params: %s", str_buffer); return str_buffer; } @@ -229,8 +229,8 @@ scrobbler_status_t scrobbler_scrobble(scrobbler_session_t *sbs, {"api_sig", 's', sign_hex}, }; - debug("scrobble: %ld", sbt->timestamp); - debug("payload: %s - %s (%s) - %d. %s (%ds)", + debug("Scrobble: %ld", sbt->timestamp); + debug("Payload: %s - %s (%s) - %d. %s (%ds)", sbt->artist, sbt->album, sbt->album_artist, sbt->track_number, sbt->track, sbt->duration); @@ -252,7 +252,7 @@ scrobbler_status_t scrobbler_scrobble(scrobbler_session_t *sbs, curl_easy_setopt(curl, CURLOPT_URL, sbs->api_url); sb_check_response(&response, curl_easy_perform(curl), sbs); - debug("scrobble status: %d", sbs->status); + debug("Scrobble status: %d", sbs->status); sb_curl_cleanup(curl, &response); return sbs->status; @@ -286,8 +286,8 @@ static scrobbler_status_t sb_update_now_playing(scrobbler_session_t *sbs, {"api_sig", 's', sign_hex}, }; - debug("now playing: %ld", sbt->timestamp); - debug("payload: %s - %s (%s) - %d. %s (%ds)", + debug("Now playing: %ld", sbt->timestamp); + debug("Payload: %s - %s (%s) - %d. %s (%ds)", sbt->artist, sbt->album, sbt->album_artist, sbt->track_number, sbt->track, sbt->duration); @@ -306,7 +306,7 @@ static scrobbler_status_t sb_update_now_playing(scrobbler_session_t *sbs, curl_easy_setopt(curl, CURLOPT_URL, sbs->api_url); sb_check_response(&response, curl_easy_perform(curl), sbs); - debug("now playing status: %d", sbs->status); + debug("Now playing status: %d", sbs->status); sb_curl_cleanup(curl, &response); return sbs->status; @@ -315,7 +315,7 @@ static scrobbler_status_t sb_update_now_playing(scrobbler_session_t *sbs, /* Update "Now playing" notification. */ scrobbler_status_t scrobbler_update_now_playing(scrobbler_session_t *sbs, scrobbler_trackinfo_t *sbt) { - debug("now playing wrapper"); + debug("Now playing wrapper"); if (sbt->artist == NULL || sbt->track == NULL) return sbs->status = SCROBBLER_STATUS_ERR_TRACKINF; return sb_update_now_playing(sbs, sbt); @@ -324,7 +324,7 @@ scrobbler_status_t scrobbler_update_now_playing(scrobbler_session_t *sbs, /* Hard-codded method for validating session key. This approach uses the * updateNotify method call with invalid parameters as a test call. */ scrobbler_status_t scrobbler_test_session_key(scrobbler_session_t *sbs) { - debug("session validation wrapper"); + debug("Session validation wrapper"); scrobbler_trackinfo_t sbt = { .artist = "", .track = "" }; return sb_update_now_playing(sbs, &sbt); } @@ -408,7 +408,7 @@ scrobbler_status_t scrobbler_authentication(scrobbler_session_t *sbs, curl_easy_setopt(curl, CURLOPT_URL, get_url); status = sb_check_response(&response, curl_easy_perform(curl), sbs); - debug("authentication status: %d", sbs->status); + debug("Authentication status: %d", sbs->status); if (status != SCROBBLER_STATUS_OK) { sb_curl_cleanup(curl, &response); return status; diff --git a/src/main.c b/src/main.c index 4e5a9c3..d7e0b32 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ /* * main.c - * Copyright (c) 2010-2017 Arkadiusz Bokowy + * Copyright (c) 2010-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -64,7 +64,7 @@ static struct cmtrack_info *get_track_info(int argc, char *argv[]) { tinfo->status = CMSTATUS_UNDEFINED; for (i = 1; i + 1 < argc; i += 2) { - debug("cmus argv: %s %s", argv[i], argv[i + 1]); + debug("Cmus argv: %s %s", argv[i], argv[i + 1]); if (strcmp(argv[i], "status") == 0) { if (strcmp(argv[i + 1], "playing") == 0) tinfo->status = CMSTATUS_PLAYING; @@ -166,13 +166,13 @@ static void cmusfm_spawn_server_process(const char *cmusfm) { pid_t pid; if ((pid = fork()) == -1) { - perror("error: fork server"); + perror("ERROR: Fork server"); exit(EXIT_FAILURE); } if (pid == 0) { execlp(cmusfm, cmusfm, "server", NULL); - perror("error: exec server"); + perror("ERROR: Exec server"); exit(EXIT_FAILURE); } @@ -202,7 +202,7 @@ int main(int argc, char *argv[]) { } if (cmusfm_config_read(cmusfm_config_file, &config) == -1) { - perror("error: config read"); + perror("ERROR: Read config"); return EXIT_FAILURE; } @@ -211,7 +211,7 @@ int main(int argc, char *argv[]) { /* try to parse cmus status display program arguments */ if ((tinfo = get_track_info(argc, argv)) == NULL) { - perror("error: get track info"); + perror("ERROR: Get track info"); return EXIT_FAILURE; } @@ -220,8 +220,8 @@ int main(int argc, char *argv[]) { sleep(1); } - if (cmusfm_server_send_track(tinfo)) { - fprintf(stderr, "error: sending track to server failed\n"); + if (cmusfm_server_send_track(tinfo) != 0) { + perror("ERROR: Send track"); return EXIT_FAILURE; } diff --git a/src/notify.c b/src/notify.c index 0f5e3d6..d2c4f9f 100644 --- a/src/notify.c +++ b/src/notify.c @@ -1,6 +1,6 @@ /* * notify.c - * Copyright (c) 2014-2017 Arkadiusz Bokowy + * Copyright (c) 2014-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -58,7 +58,7 @@ void cmusfm_notify_show(const scrobbler_trackinfo_t *sb_tinf, const char *icon) cmus_notify = notify_notification_new(sb_tinf->track, body, icon); if (!notify_notification_show(cmus_notify, &error)) { - debug("desktop notify error: %s", error->message); + debug("Desktop notify error: %s", error->message); g_error_free(error); /* NOTE: Free the notification subsystem upon show failure. This action * allows us to recover from the D-Bus connection failure, which diff --git a/src/server.c b/src/server.c index f7f0801..1e1fa8d 100644 --- a/src/server.c +++ b/src/server.c @@ -1,6 +1,6 @@ /* * server.c - * Copyright (c) 2010-2017 Arkadiusz Bokowy + * Copyright (c) 2010-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -24,6 +24,7 @@ #include "server.h" +#include #include #include #include @@ -114,15 +115,15 @@ static void cmusfm_server_process_data(scrobbler_session_t *sbs, (checksum2 = make_record_checksum2(record)) != record->checksum2) return; - debug("payload: %s - %s - %d. %s (%ds)", + debug("Payload: %s - %s - %d. %s (%ds)", get_record_artist(record), get_record_album(record), record->track_number, get_record_track(record), record->duration); - debug("location: %s", get_record_location(record)); + debug("Location: %s", get_record_location(record)); #if DEBUG && !defined(DEBUG_SKIP_HICCUP) /* simulate server "hiccup" (e.g. internet connection issue) */ - debug("server hiccup test (5s)"); + debug("Server hiccup test (5s)"); sleep(5); #endif @@ -163,7 +164,7 @@ static void cmusfm_server_process_data(scrobbler_session_t *sbs, if ((saved_is_radio && !config.submit_shoutcast) || (!saved_is_radio && !config.submit_localfile)) { /* skip submission if we don't want it */ - debug("submission not enabled"); + debug("Submission not enabled"); goto action_submit_skip; } @@ -205,7 +206,7 @@ static void cmusfm_server_process_data(scrobbler_session_t *sbs, cmusfm_notify_show(&sb_tinf, get_album_cover_file( get_record_location(record), config.format_coverfile)); else - debug("notification not enabled"); + debug("Notification not enabled"); #endif /* update now-playing indicator */ @@ -216,7 +217,7 @@ static void cmusfm_server_process_data(scrobbler_session_t *sbs, scrobbler_fail_time = 1; } else - debug("now playing not enabled"); + debug("Now playing not enabled"); } } @@ -277,7 +278,7 @@ int cmusfm_server_check(void) { static bool server_on = true; static void cmusfm_server_stop(int sig) { (void)sig; - debug("stopping server"); + debug("Stopping server"); server_on = false; } @@ -293,7 +294,7 @@ int cmusfm_server_start(void) { size_t rd_len; int retval; - debug("starting server"); + debug("Starting server"); /* setup poll structure for data reading */ struct pollfd pfds[3] = { @@ -332,7 +333,7 @@ int cmusfm_server_start(void) { cmusfm_config_add_watch(pfds[2].fd); #endif - debug("entering server main loop"); + debug("Entering server main loop"); while (server_on) { if (poll(pfds, 3, -1) == -1) @@ -340,7 +341,7 @@ int cmusfm_server_start(void) { if (pfds[0].revents & POLLIN) { pfds[1].fd = accept(pfds[0].fd, NULL, NULL); - debug("new client accepted: %d", pfds[1].fd); + debug("New client accepted: %d", pfds[1].fd); } if (pfds[1].revents & POLLIN && pfds[1].fd != -1) { @@ -356,7 +357,7 @@ int cmusfm_server_start(void) { /* We're watching only one file, so the result is of no importance * to us, simply read out the inotify file descriptor. */ read(pfds[2].fd, &inot_even, sizeof(inot_even)); - debug("inotify event occurred: %x", inot_even.mask); + debug("Inotify event occurred: %x", inot_even.mask); cmusfm_config_read(cmusfm_config_file, &config); cmusfm_config_add_watch(pfds[2].fd); } @@ -390,7 +391,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { char buffer[CMSOCKET_BUFFER_SIZE]; struct cmusfm_data_record *record = (struct cmusfm_data_record *)buffer; struct format_match *match, *matches; - int sock; + int err, sock; /* helper accessors for dynamic fields */ char *artist = &buffer[sizeof(*record)]; @@ -398,7 +399,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { char *title = &buffer[sizeof(*record)]; char *location = &buffer[sizeof(*record)]; - debug("sending track to server"); + debug("Sending track to server"); memset(buffer, 0, sizeof(buffer)); @@ -414,15 +415,15 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { if ((tinfo->url != NULL && tinfo->artist == NULL && tinfo->title != NULL) || (tinfo->file != NULL && tinfo->artist == NULL && tinfo->title == NULL)) { - debug("regular expression matching mode"); + debug("Regular expression matching mode"); if (tinfo->url != NULL) { /* URL: try to fetch artist and track tile form the 'title' field */ matches = get_regexp_format_matches(tinfo->title, config.format_shoutcast); if (matches == NULL) { - fprintf(stderr, "error: shoutcast format match failed\n"); - return -1; + fprintf(stderr, "INFO: Title does not match format-shoutcast\n"); + return 0; } } else { @@ -431,8 +432,8 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { tinfo->file = basename(tinfo->file); matches = get_regexp_format_matches(tinfo->file, config.format_localfile); if (matches == NULL) { - fprintf(stderr, "error: localfile format match failed\n"); - return -1; + fprintf(stderr, "INFO: File name does not match format-localfile\n"); + return 0; } } @@ -477,18 +478,24 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) { /* connect to the communication socket */ struct sockaddr_un saddr = { .sun_family = AF_UNIX }; strcpy(saddr.sun_path, cmusfm_socket_file); - sock = socket(PF_UNIX, SOCK_STREAM, 0); - if (connect(sock, (struct sockaddr *)(&saddr), sizeof(saddr)) == -1) { - close(sock); - return -1; - } + if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) + goto fail; + if (connect(sock, (struct sockaddr *)(&saddr), sizeof(saddr)) == -1) + goto fail; - debug("record length: %ld", sizeof(struct cmusfm_data_record) + - record->off_title + record->off_location + strlen(location) + 1); - write(sock, buffer, sizeof(struct cmusfm_data_record) + - record->off_title + record->off_location + strlen(location) + 1); + ssize_t len = sizeof(struct cmusfm_data_record) + + record->off_title + record->off_location + strlen(location) + 1; + debug("Record length: %zd", len); + if (write(sock, buffer, len) != len) + goto fail; return close(sock); + +fail: + err = errno; + close(sock); + errno = err; + return -1; } /* Helper function for retrieving server socket file. */ diff --git a/src/utils.c b/src/utils.c index 3510934..1221c07 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,6 +1,6 @@ /* * utils.c - * Copyright (c) 2014-2017 Arkadiusz Bokowy + * Copyright (c) 2014-2018 Arkadiusz Bokowy * * This file is a part of cmusfm. * @@ -123,7 +123,7 @@ char *get_album_cover_file(const char *location, const char *format) { regex_t regex; char *tmp; - debug("get cover (case-insensitive): %s", format); + debug("Get cover (case-insensitive): %s", format); if (location == NULL) return NULL; @@ -148,7 +148,7 @@ char *get_album_cover_file(const char *location, const char *format) { /* scan given directory for cover file name pattern */ while ((dp = readdir(dir)) != NULL) { - debug("cover lookup: %s", dp->d_name); + debug("Cover lookup: %s", dp->d_name); if (!regexec(®ex, dp->d_name, 0, NULL, 0)) { strcat(strcat(fname, "/"), dp->d_name); break; @@ -161,7 +161,7 @@ char *get_album_cover_file(const char *location, const char *format) { if (dp == NULL) return NULL; - debug("cover: %s", fname); + debug("Cover: %s", fname); return fname; } #endif @@ -185,7 +185,7 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form regex_t regex; regmatch_t regmatch[MATCHES_SIZE]; - debug("matching: %s: %s", format, str); + debug("Matching: %s: %s", format, str); /* allocate memory for up to FORMAT_MATCH_TYPE_COUNT matches * with one extra always empty terminating structure */ @@ -198,7 +198,7 @@ struct format_match *get_regexp_format_matches(const char *str, const char *form strcpy(®exp[p - format - i * 2], p); } - debug("regexp: %s", regexp); + debug("Regexp: %s", regexp); status = regcomp(®ex, regexp, REG_EXTENDED | REG_ICASE); free(regexp);