Fix: formating

This commit is contained in:
jcorporation 2018-08-06 22:29:20 +01:00
parent f42bba550d
commit e46c0b200f
3 changed files with 134 additions and 120 deletions

View File

@ -43,17 +43,15 @@ const char * mpd_cmd_strs[] = {
MPD_CMDS(GEN_STR)
};
static inline enum mpd_cmd_ids get_cmd_id(const char *cmd)
{
for(int i = 0; i < sizeof(mpd_cmd_strs)/sizeof(mpd_cmd_strs[0]); i++)
static inline enum mpd_cmd_ids get_cmd_id(const char *cmd) {
for (int i = 0; i < sizeof(mpd_cmd_strs) / sizeof(mpd_cmd_strs[0]); i++)
if (!strncmp(cmd, mpd_cmd_strs[i], strlen(mpd_cmd_strs[i])))
return i;
return -1;
}
void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
{
void callback_mympd(struct mg_connection *nc, const struct mg_str msg) {
size_t n = 0;
char *cmd;
unsigned int uint_buf1, uint_buf2, uint_rc;
@ -81,41 +79,41 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"Unknown request\"}");
break;
case MPD_API_SET_SETTINGS:
json_scanf(msg.p, msg.len, "{ data: { notificationWeb: %d, notificationPage: %d} }", &state.a, &state.b);
json_scanf(msg.p, msg.len, "{data: { notificationWeb: %d, notificationPage: %d}}", &state.a, &state.b);
char tmpfile[200];
snprintf(tmpfile,200,"%s.tmp", config.statefile);
json_fprintf(tmpfile, "{ notificationWeb: %d, notificationPage: %d}", state.a, state.b);
json_fprintf(tmpfile, "{notificationWeb: %d, notificationPage: %d}", state.a, state.b);
rename(tmpfile,config.statefile);
je = json_scanf(msg.p, msg.len, "{ data: { random:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {random:%u}}", &uint_buf1);
if (je == 1)
mpd_run_random(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { repeat:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {repeat:%u}}", &uint_buf1);
if (je == 1)
mpd_run_repeat(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { consume:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {consume:%u}}", &uint_buf1);
if (je == 1)
mpd_run_consume(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { single:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {single:%u}}", &uint_buf1);
if (je == 1)
mpd_run_single(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { crossfade:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {crossfade:%u}}", &uint_buf1);
if (je == 1)
mpd_run_crossfade(mpd.conn, uint_buf1);
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdb:%f } }", &float_buf);
je = json_scanf(msg.p, msg.len, "{data: {mixrampdb:%f}}", &float_buf);
if (je == 1)
mpd_run_mixrampdb(mpd.conn, float_buf);
je = json_scanf(msg.p, msg.len, "{ data: { mixrampdelay:%f } }", &float_buf);
je = json_scanf(msg.p, msg.len, "{data: {mixrampdelay:%f}}", &float_buf);
if (je == 1)
mpd_run_mixrampdelay(mpd.conn, float_buf);
je = json_scanf(msg.p, msg.len, "{ data: { replaygain:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {replaygain:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_send_command(mpd.conn, "replay_gain_mode", p_charbuf1, NULL);
struct mpd_pair *pair;
@ -127,7 +125,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break;
case MPD_API_GET_ARTISTALBUMTITLES:
je = json_scanf(msg.p, msg.len, "{ data: { albumartist:%Q, album:%Q } }", &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {albumartist:%Q, album:%Q}}", &p_charbuf1, &p_charbuf2);
if (je == 2) {
n = mympd_put_songs_in_album(mpd.buf, p_charbuf1, p_charbuf2);
free(p_charbuf1);
@ -170,21 +168,21 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = mympd_queue_crop(mpd.buf);
break;
case MPD_API_RM_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {track:%u}}", &uint_buf1);
if (je == 1) {
mpd_run_delete_id(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break;
case MPD_API_RM_RANGE:
je = json_scanf(msg.p, msg.len, "{ data: { start:%u, end:%u } }", &uint_buf1, &uint_buf2);
je = json_scanf(msg.p, msg.len, "{data: {start:%u, end:%u}}", &uint_buf1, &uint_buf2);
if (je == 2) {
mpd_run_delete_range(mpd.conn, uint_buf1, uint_buf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break;
case MPD_API_MOVE_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { from:%u, to:%u } }", &uint_buf1, &uint_buf2);
je = json_scanf(msg.p, msg.len, "{data: {from:%u, to:%u}}", &uint_buf1, &uint_buf2);
if (je == 2) {
uint_buf1 --;
uint_buf2 --;
@ -195,7 +193,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_PLAYLIST_MOVE_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { plist: %Q, from:%u, to:%u } }", &p_charbuf1, &uint_buf1, &uint_buf2);
je = json_scanf(msg.p, msg.len, "{data: {plist: %Q, from:%u, to:%u }}", &p_charbuf1, &uint_buf1, &uint_buf2);
if (je == 3) {
uint_buf1 --;
uint_buf2 --;
@ -208,7 +206,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_PLAY_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: { track:%u}}", &uint_buf1);
if (je == 1) {
mpd_run_play_id(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
@ -218,7 +216,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = mympd_put_outputs(mpd.buf);
break;
case MPD_API_TOGGLE_OUTPUT:
je = json_scanf(msg.p, msg.len, "{ data: { output:%u, state:%u } }", &uint_buf1, &uint_buf2);
je = json_scanf(msg.p, msg.len, "{data: {output:%u, state:%u}}", &uint_buf1, &uint_buf2);
if (je == 2) {
if (uint_buf2)
mpd_run_enable_output(mpd.conn, uint_buf1);
@ -228,21 +226,21 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SET_VOLUME:
je = json_scanf(msg.p, msg.len, "{ data: { volume:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {volume:%u}}", &uint_buf1);
if (je == 1) {
mpd_run_set_volume(mpd.conn, uint_buf1);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break;
case MPD_API_SET_SEEK:
je = json_scanf(msg.p, msg.len, "{ data: { songid:%u, seek:%u } }", &uint_buf1, &uint_buf2);
je = json_scanf(msg.p, msg.len, "{data: {songid:%u, seek:%u}}", &uint_buf1, &uint_buf2);
if (je == 2) {
mpd_run_seek_id(mpd.conn, uint_buf1, uint_buf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
}
break;
case MPD_API_GET_QUEUE:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u } }", &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u }}", &uint_buf1);
if (je == 1) {
n = mympd_put_queue(mpd.buf, uint_buf1);
}
@ -251,21 +249,21 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = mympd_put_current_song(mpd.buf);
break;
case MPD_API_GET_SONGDETAILS:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: { uri:%Q }}", &p_charbuf1);
if (je == 1) {
n = mympd_put_songdetails(mpd.buf, p_charbuf1);
free(p_charbuf1);
}
break;
case MPD_API_GET_ARTISTS:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q } }", &uint_buf1, &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q}}", &uint_buf1, &p_charbuf1);
if (je == 2) {
n = mympd_put_db_tag(mpd.buf, uint_buf1, "AlbumArtist", "", "", p_charbuf1);
free(p_charbuf1);
}
break;
case MPD_API_GET_ARTISTALBUMS:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q, albumartist:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q, albumartist:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
if (je == 3) {
n = mympd_put_db_tag(mpd.buf, uint_buf1, "Album", "AlbumArtist", p_charbuf2, p_charbuf1);
free(p_charbuf1);
@ -273,7 +271,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_PLAYLIST_RENAME:
je = json_scanf(msg.p, msg.len, "{ data: { from:%Q, to:%Q } }", &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {from:%Q, to:%Q}}", &p_charbuf1, &p_charbuf2);
if (je == 2) {
mpd_run_rename(mpd.conn, p_charbuf1, p_charbuf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Renamed playlist %s to %s\"}", p_charbuf1, p_charbuf2);
@ -282,14 +280,14 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_GET_PLAYLISTS:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q } }", &uint_buf1, &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q}}", &uint_buf1, &p_charbuf1);
if (je == 2) {
n = mympd_put_playlists(mpd.buf, uint_buf1, p_charbuf1);
free(p_charbuf1);
}
break;
case MPD_API_GET_PLAYLIST_LIST:
je = json_scanf(msg.p, msg.len, "{ data: { uri: %Q, offset:%u, filter:%Q } }", &p_charbuf1, &uint_buf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {uri: %Q, offset:%u, filter:%Q}}", &p_charbuf1, &uint_buf1, &p_charbuf2);
if (je == 3) {
n = mympd_put_playlist_list(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
free(p_charbuf1);
@ -297,7 +295,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_ADD_TO_PLAYLIST:
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q, uri:%Q } }", &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q, uri:%Q}}", &p_charbuf1, &p_charbuf2);
if (je == 2) {
mpd_run_playlist_add(mpd.conn, p_charbuf1, p_charbuf2);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"Added %s to playlist %s\"}", p_charbuf2, p_charbuf1);
@ -306,7 +304,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_PLAYLIST_CLEAR:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_run_playlist_clear(mpd.conn, p_charbuf1);
free(p_charbuf1);
@ -314,7 +312,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_RM_PLAYLIST_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, track:%u } }", &p_charbuf1, &uint_buf1);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q, track:%u}}", &p_charbuf1, &uint_buf1);
if (je == 2) {
mpd_run_playlist_delete(mpd.conn, p_charbuf1, uint_buf1);
free(p_charbuf1);
@ -322,7 +320,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_GET_FILESYSTEM:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q, path:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, filter:%Q, path:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
if (je == 3) {
n = mympd_put_browse(mpd.buf, p_charbuf2, uint_buf1, p_charbuf1);
free(p_charbuf1);
@ -330,7 +328,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_ADD_TRACK_AFTER:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q, to:%d } }", &p_charbuf1, &int_buf);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q, to:%d}}", &p_charbuf1, &int_buf);
if (je == 2) {
int_rc = mpd_run_add_id_to(mpd.conn, p_charbuf1, int_buf);
if (int_rc > -1 )
@ -339,7 +337,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_REPLACE_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q }}", &p_charbuf1);
if (je == 1) {
mpd_run_clear(mpd.conn);
mpd_run_add(mpd.conn, p_charbuf1);
@ -349,7 +347,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_ADD_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_run_add(mpd.conn, p_charbuf1);
free(p_charbuf1);
@ -357,7 +355,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_ADD_PLAY_TRACK:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {uri:%Q}}", &p_charbuf1);
if (je == 1) {
int_buf = mpd_run_add_id(mpd.conn, p_charbuf1);
if (int_buf != -1)
@ -367,7 +365,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_REPLACE_PLAYLIST:
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_run_clear(mpd.conn);
mpd_run_load(mpd.conn, p_charbuf1);
@ -377,7 +375,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_ADD_PLAYLIST:
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_run_load(mpd.conn, p_charbuf1);
free(p_charbuf1);
@ -385,7 +383,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SAVE_QUEUE:
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{ data: {plist:%Q}}", &p_charbuf1);
if (je == 1) {
mpd_run_save(mpd.conn, p_charbuf1);
free(p_charbuf1);
@ -393,7 +391,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SEARCH_QUEUE:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%Q, searchstr:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, mpdtag:%Q, searchstr:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
if (je == 3) {
n = mympd_search_queue(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
free(p_charbuf1);
@ -401,7 +399,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SEARCH_ADD:
je = json_scanf(msg.p, msg.len, "{ data: { filter:%Q, searchstr:%Q } }", &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {filter:%Q, searchstr:%Q}}", &p_charbuf1, &p_charbuf2);
if (je == 2) {
n = mympd_search_add(mpd.buf, p_charbuf1, p_charbuf2);
free(p_charbuf1);
@ -411,7 +409,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SEARCH_ADD_PLIST:
je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q, filter:%Q, searchstr:%Q } }", &p_charbuf1, &p_charbuf2, &p_charbuf3);
je = json_scanf(msg.p, msg.len, "{data: {plist:%Q, filter:%Q, searchstr:%Q}}", &p_charbuf1, &p_charbuf2, &p_charbuf3);
if (je == 3) {
n = mympd_search_add_plist(p_charbuf1, p_charbuf2, p_charbuf3);
free(p_charbuf1);
@ -422,7 +420,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_SEARCH:
je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%Q, searchstr:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: {offset:%u, mpdtag:%Q, searchstr:%Q}}", &uint_buf1, &p_charbuf1, &p_charbuf2);
if (je == 3) {
n = mympd_search(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2);
free(p_charbuf1);
@ -434,7 +432,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"result\", \"data\": \"ok\"}");
break;
case MPD_API_SEND_MESSAGE:
je = json_scanf(msg.p, msg.len, "{ data: { channel:%Q, text:%Q } }", &p_charbuf1, &p_charbuf2);
je = json_scanf(msg.p, msg.len, "{data: { channel:%Q, text:%Q}}", &p_charbuf1, &p_charbuf2);
if (je == 2) {
mpd_run_send_message(mpd.conn, p_charbuf1, p_charbuf2);
free(p_charbuf1);
@ -443,7 +441,7 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
}
break;
case MPD_API_RM_PLAYLIST:
je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1);
je = json_scanf(msg.p, msg.len, "{data: { uri:%Q }}", &p_charbuf1);
if (je == 1) {
mpd_run_rm(mpd.conn, p_charbuf1);
free(p_charbuf1);
@ -454,13 +452,13 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
n = mympd_put_settings(mpd.buf);
break;
case MPD_API_GET_STATS:
n = mympd_get_stats(mpd.buf);
n = mympd_put_stats(mpd.buf);
break;
}
if (mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) {
#ifdef DEBUG
fprintf(stderr,"Error: %s\n", mpd_connection_get_error_message(mpd.conn));
fprintf(stderr, "Error: %s\n", mpd_connection_get_error_message(mpd.conn));
#endif
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}",
mpd_connection_get_error_message(mpd.conn));
@ -475,13 +473,13 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg)
if (is_websocket(nc)) {
#ifdef DEBUG
fprintf(stdout,"Send websocket response:\n %s\n",mpd.buf);
fprintf(stdout, "Send websocket response:\n %s\n",mpd.buf);
#endif
mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n);
}
else {
#ifdef DEBUG
fprintf(stdout,"Send http response:\n %s\n",mpd.buf);
fprintf(stdout, "Send http response:\n %s\n",mpd.buf);
#endif
mg_send_http_chunk(nc, mpd.buf, n);
}
@ -502,9 +500,9 @@ static int mympd_notify_callback(struct mg_connection *c, const char *param) {
if (param) {
/* error message? */
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\",\"data\":\"%s\"}",param);
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"error\", \"data\": \"%s\"}",param);
#ifdef DEBUG
fprintf(stdout,"Error in mpd_notify_callback: %s\n",param);
fprintf(stderr, "Error in mpd_notify_callback: %s\n",param);
#endif
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
return 0;
@ -516,31 +514,31 @@ static int mympd_notify_callback(struct mg_connection *c, const char *param) {
struct t_mpd_client_session *s = (struct t_mpd_client_session *)c->user_data;
if (mpd.conn_state != MPD_CONNECTED) {
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"disconnected\"}");
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"disconnected\"}");
#ifdef DEBUG
fprintf(stdout,"Notify: disconnected\n");
fprintf(stdout, "Notify: disconnected\n");
#endif
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
}
else {
#ifdef DEBUG
fprintf(stdout,"Notify: %s\n",mpd.buf);
fprintf(stdout, "Notify: %s\n",mpd.buf);
#endif
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, strlen(mpd.buf));
if (s->song_id != mpd.song_id) {
n=mympd_put_current_song(mpd.buf);
n = mympd_put_current_song(mpd.buf);
#ifdef DEBUG
fprintf(stdout,"Notify: %s\n",mpd.buf);
fprintf(stdout, "Notify: %s\n",mpd.buf);
#endif
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
s->song_id = mpd.song_id;
}
if (s->queue_version != mpd.queue_version) {
n=snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"update_queue\"}");
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\": \"update_queue\"}");
#ifdef DEBUG
fprintf(stdout,"Notify: update_queue\n");
fprintf(stdout, "Notify: update_queue\n");
#endif
mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, mpd.buf, n);
s->queue_version = mpd.queue_version;
@ -553,7 +551,7 @@ void mympd_poll(struct mg_mgr *s) {
switch (mpd.conn_state) {
case MPD_DISCONNECTED:
/* Try to connect */
fprintf(stdout, "MPD Connecting to %s:%d\n", config.mpdhost, config.mpdport);
fprintf(stdout, "MPD Connecting to %s: %d\n", config.mpdhost, config.mpdport);
mpd.conn = mpd_connection_new(config.mpdhost, config.mpdport, 3000);
if (mpd.conn == NULL) {
fprintf(stderr, "Out of memory.");
@ -661,7 +659,8 @@ int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsi
*queue_version = mpd_status_get_queue_version(status);
mpd_status_free(status);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
return len;
}
@ -671,7 +670,8 @@ int mympd_put_welcome(char *buffer) {
len = json_printf(&out, "{type: welcome, data: { version: %Q}}", MYMPD_VERSION);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
return len;
}
@ -734,7 +734,8 @@ int mympd_put_settings(char *buffer) {
);
mpd_status_free(status);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -745,7 +746,7 @@ int mympd_put_outputs(char *buffer) {
int nr;
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
len = json_printf(&out,"{type: outputs, data: { outputs: [");
len = json_printf(&out,"{type: outputs, data: {outputs: [");
mpd_send_outputs(mpd.conn);
nr=0;
@ -765,7 +766,8 @@ int mympd_put_outputs(char *buffer) {
len += json_printf(&out,"]}}");
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -803,7 +805,7 @@ int mympd_put_current_song(char *buffer) {
len = json_printf(&out,"{type: song_change, data: { pos: %d, title: %Q, "
"artist: %Q, album: %Q, uri: %Q, currentsongid: %d, albumartist: %Q, "
"duration: %d, cover: %Q }}",
"duration: %d, cover: %Q}}",
mpd_song_get_pos(song),
mympd_get_tag(song, MPD_TAG_TITLE),
mympd_get_tag(song, MPD_TAG_ARTIST),
@ -818,7 +820,8 @@ int mympd_put_current_song(char *buffer) {
mpd_song_free(song);
mpd_response_finish(mpd.conn);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -850,7 +853,8 @@ int mympd_put_songdetails(char *buffer, char *uri) {
}
len += json_printf(&out, "}}");
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -877,7 +881,7 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
entity_count ++;
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out,",");
len += json_printf(&out, "{ id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q }",
len += json_printf(&out, "{id: %d, pos: %d, duration: %d, artist: %Q, album: %Q, title: %Q, uri: %Q }",
mpd_song_get_id(song),
mpd_song_get_pos(song),
mpd_song_get_duration(song),
@ -899,7 +903,8 @@ int mympd_put_queue(char *buffer, unsigned int offset) {
mpd.queue_version
);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -915,7 +920,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
if (!mpd_send_list_meta(mpd.conn, path))
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
len = json_printf(&out, "{ type: browse, data: [ ");
len = json_printf(&out, "{type: browse, data: [ ");
while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
const struct mpd_song *song;
@ -981,7 +986,7 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
( strncmp(filter,"0",1) == 0 && isalpha(*plName) == 0 )
) {
if (entities_returned ++) len += json_printf(&out,",");
len += json_printf(&out, "{ type: plist, uri: %Q, name: %Q }",
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q }",
entityName,
plName
);
@ -1007,7 +1012,8 @@ int mympd_put_browse(char *buffer, char *path, unsigned int offset, char *filter
filter
);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1059,7 +1065,8 @@ int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *
filter
);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1071,9 +1078,8 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
char cover[500];
struct json_out out = JSON_OUT_BUF(buffer, MAX_SIZE);
if (mpd_search_db_songs(mpd.conn, true) == false) {
if (mpd_search_db_songs(mpd.conn, true) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_db_songs");
}
if (mpd_search_add_tag_constraint(mpd.conn, MPD_OPERATOR_DEFAULT, MPD_TAG_ALBUM_ARTIST, albumartist) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_add_tag_constraint");
@ -1091,7 +1097,7 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
if (entity_count <= MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out, ", ");
else mympd_get_cover(mpd_song_get_uri(song),cover,500);
len += json_printf(&out, "{ type: song, uri: %Q, duration: %d, title: %Q, track: %Q }",
len += json_printf(&out, "{ type: song, uri: %Q, duration: %d, title: %Q, track: %Q}",
mpd_song_get_uri(song),
mpd_song_get_duration(song),
mympd_get_tag(song, MPD_TAG_TITLE),
@ -1101,7 +1107,7 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
mpd_song_free(song);
}
len += json_printf(&out, "], totalEntities: %d, returnedEntities: %d, albumartist: %Q, album: %Q, cover: %Q }",
len += json_printf(&out, "], totalEntities: %d, returnedEntities: %d, albumartist: %Q, album: %Q, cover: %Q}",
entity_count,
entities_returned,
albumartist,
@ -1110,7 +1116,8 @@ int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album) {
);
}
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1125,7 +1132,7 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
if (!mpd_send_list_playlists(mpd.conn))
RETURN_ERROR_AND_RECOVER("mpd_send_lists_playlists");
len = json_printf(&out, "{ type: playlists, data: [ ");
len = json_printf(&out, "{type: playlists, data: [ ");
while((pl = mpd_recv_playlist(mpd.conn)) != NULL) {
entity_count ++;
@ -1135,7 +1142,7 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
( strncmp(filter,"0",1) == 0 && isalpha(*plpath) == 0 )
) {
if (entities_returned ++) len += json_printf(&out, ", ");
len += json_printf(&out, "{ type: plist, uri: %Q, name: %Q, last_modified: %d }",
len += json_printf(&out, "{type: plist, uri: %Q, name: %Q, last_modified: %d}",
plpath,
plpath,
mpd_playlist_get_last_modified(pl)
@ -1150,13 +1157,14 @@ int mympd_put_playlists(char *buffer, unsigned int offset, char *filter) {
if (mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS || !mpd_response_finish(mpd.conn))
RETURN_ERROR_AND_RECOVER("mpd_send_list_playlists");
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d }",
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d}",
entity_count,
offset,
entities_returned
);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1171,7 +1179,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
if (!mpd_send_list_playlist_meta(mpd.conn, uri))
RETURN_ERROR_AND_RECOVER("mpd_send_list_meta");
len = json_printf(&out, "{ type: playlist_detail, data: [ ");
len = json_printf(&out, "{type: playlist_detail, data: [ ");
while((entity = mpd_recv_entity(mpd.conn)) != NULL) {
const struct mpd_song *song;
@ -1183,7 +1191,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
( strncmp(filter,"0",1) == 0 && isalpha(*entityName) == 0 )
) {
if (entities_returned ++) len += json_printf(&out,",");
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }",
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
mpd_song_get_uri(song),
mympd_get_tag(song, MPD_TAG_ALBUM),
mympd_get_tag(song, MPD_TAG_ARTIST),
@ -1198,7 +1206,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
mpd_entity_free(entity);
}
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q, uri: %Q }",
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, filter: %Q, uri: %Q}",
entity_count,
offset,
entities_returned,
@ -1207,7 +1215,7 @@ int mympd_put_playlist_list(char *buffer, char *uri, unsigned int offset, char *
);
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1233,13 +1241,13 @@ int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *sear
if (mpd_search_commit(mpd.conn) == false)
RETURN_ERROR_AND_RECOVER("mpd_search_commit");
else {
len = json_printf(&out, "{ type: search, data: [ ");
len = json_printf(&out, "{type: search, data: [ ");
while((song = mpd_recv_song(mpd.conn)) != NULL) {
entity_count ++;
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out, ", ");
len += json_printf(&out, "{ type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q }",
len += json_printf(&out, "{type: song, uri: %Q, album: %Q, artist: %Q, duration: %d, title: %Q, name: %Q}",
mpd_song_get_uri(song),
mympd_get_tag(song, MPD_TAG_ALBUM),
mympd_get_tag(song, MPD_TAG_ARTIST),
@ -1251,7 +1259,7 @@ int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *sear
mpd_song_free(song);
}
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q }",
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q}",
entity_count,
offset,
entities_returned,
@ -1260,7 +1268,7 @@ int mympd_search(char *buffer, char *mpdtagtype, unsigned int offset, char *sear
}
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1290,7 +1298,7 @@ int mympd_search_add(char *buffer, char *mpdtagtype, char *searchstr) {
}
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1305,7 +1313,7 @@ int mympd_search_add_plist(char *plist, char *mpdtagtype, char *searchstr) {
}
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1319,7 +1327,7 @@ int mympd_queue_crop(char *buffer) {
mpd_status_free(status);
if (length < 0) {
len = json_printf(&out, "{ type: error, data: %Q }", "A playlist longer than 1 song in length is required to crop.");
len = json_printf(&out, "{type: error, data: %Q }", "A playlist longer than 1 song in length is required to crop.");
}
else if (mpd_status_get_state(status) == MPD_STATE_PLAY || mpd_status_get_state(status) == MPD_STATE_PAUSE) {
playing_song_pos++;
@ -1328,13 +1336,13 @@ int mympd_queue_crop(char *buffer) {
playing_song_pos--;
if (playing_song_pos > 0 )
mpd_run_delete_range(mpd.conn, 0, playing_song_pos--);
len = json_printf(&out, "{ type: result, data: ok }");
len = json_printf(&out, "{type: result, data: ok}");
} else {
len = json_printf(&out, "{ type: error, data: %Q }", "You need to be playing to crop the playlist");
len = json_printf(&out, "{type: error, data: %Q}", "You need to be playing to crop the playlist");
}
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
fprintf(stderr, "Buffer truncated\n");
return len;
}
@ -1367,7 +1375,7 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
entity_count ++;
if (entity_count > offset && entity_count <= offset+MAX_ELEMENTS_PER_PAGE) {
if (entities_returned ++) len += json_printf(&out, ", ");
len += json_printf(&out, "{ type: song, id: %d, pos: %d, album: %Q, artist: %Q, duration: %d, title: %Q }",
len += json_printf(&out, "{type: song, id: %d, pos: %d, album: %Q, artist: %Q, duration: %d, title: %Q}",
mpd_song_get_id(song),
mpd_song_get_pos(song),
mympd_get_tag(song, MPD_TAG_ALBUM),
@ -1379,7 +1387,7 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
}
}
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q }",
len += json_printf(&out, "], totalEntities: %d, offset: %d, returnedEntities: %d, mpdtagtype: %Q}",
entity_count,
offset,
entities_returned,
@ -1387,11 +1395,12 @@ int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char
);
}
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr, "Buffer truncated\n");
return len;
}
int mympd_get_stats(char *buffer) {
int mympd_put_stats(char *buffer) {
struct mpd_stats *stats = mpd_run_stats(mpd.conn);
const unsigned *version = mpd_connection_get_server_version(mpd.conn);
char mpd_version[20];
@ -1401,9 +1410,9 @@ int mympd_get_stats(char *buffer) {
snprintf(mpd_version,20,"%i.%i.%i", version[0], version[1], version[2]);
if (stats == NULL)
RETURN_ERROR_AND_RECOVER("mympd_get_stats");
len = json_printf(&out, "{ type: mpdstats, data: { artists: %d, albums: %d, songs: %d, "
"playtime: %d, uptime: %d, dbupdated: %d, dbplaytime: %d, mympd_version: %Q, mpd_version: %Q }}",
RETURN_ERROR_AND_RECOVER("mympd_put_stats");
len = json_printf(&out, "{type: mpdstats, data: { artists: %d, albums: %d, songs: %d, "
"playtime: %d, uptime: %d, dbupdated: %d, dbplaytime: %d, mympd_version: %Q, mpd_version: %Q}}",
mpd_stats_get_number_of_artists(stats),
mpd_stats_get_number_of_albums(stats),
mpd_stats_get_number_of_songs(stats),
@ -1416,7 +1425,8 @@ int mympd_get_stats(char *buffer) {
);
mpd_stats_free(stats);
if (len > MAX_SIZE) fprintf(stderr,"Buffer truncated\n");
if (len > MAX_SIZE)
fprintf(stderr,"Buffer truncated\n");
return len;
}

View File

@ -35,7 +35,7 @@
if (!mpd_connection_clear_error(mpd.conn)) \
mpd.conn_state = MPD_FAILURE; \
return len; \
} while(0)
} while (0)
#define MAX_SIZE 1024 * 100
@ -159,7 +159,7 @@ int mympd_search_add(char *buffer, char *mpdtagtype, char *searchstr);
int mympd_search_add_plist(char *plist, char *mpdtagtype, char *searchstr);
int mympd_search_queue(char *buffer, char *mpdtagtype, unsigned int offset, char *searchstr);
int mympd_put_welcome(char *buffer);
int mympd_get_stats(char *buffer);
int mympd_put_stats(char *buffer);
int mympd_put_settings(char *buffer);
int mympd_put_db_tag(char *buffer, unsigned int offset, char *mpdtagtype, char *mpdsearchtagtype, char *searchstr, char *filter);
int mympd_put_songs_in_album(char *buffer, char *albumartist, char *album);

View File

@ -39,21 +39,21 @@ static struct mg_serve_http_opts s_http_server_opts;
char s_redirect[250];
static void signal_handler(int sig_num) {
signal(sig_num, signal_handler); // Reinstantiate signal handler
s_signal_received = sig_num;
signal(sig_num, signal_handler); // Reinstantiate signal handler
s_signal_received = sig_num;
}
static void handle_api(struct mg_connection *nc, struct http_message *hm) {
if (!is_websocket(nc)) {
if (!is_websocket(nc))
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n");
}
char buf[1000] = {0};
memcpy(buf, hm->body.p,sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len);
struct mg_str d = {buf, strlen(buf)};
callback_mympd(nc, d);
if (!is_websocket(nc)) {
if (!is_websocket(nc))
mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
}
}
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
@ -62,14 +62,14 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
#ifdef DEBUG
fprintf(stdout,"New Websocket connection\n");
#endif
struct mg_str d = {(char *) "{\"cmd\":\"MPD_API_WELCOME\"}", 25 };
struct mg_str d = {(char *) "{\"cmd\": \"MPD_API_WELCOME\"}", 25 };
callback_mympd(nc, d);
break;
}
case MG_EV_HTTP_REQUEST: {
struct http_message *hm = (struct http_message *) ev_data;
#ifdef DEBUG
printf("HTTP request: %.*s\n",hm->uri.len,hm->uri.p);
printf("HTTP request: %.*s\n", hm->uri.len, hm->uri.p);
#endif
if (mg_vcmp(&hm->uri, "/api") == 0) {
handle_api(nc, hm);
@ -166,7 +166,7 @@ int main(int argc, char **argv) {
config.sslport = "443";
config.sslcert = "/etc/mympd/ssl/server.pem";
config.sslkey = "/etc/mympd/ssl/server.key";
config.user = NULL;
config.user = "nobody";
config.streamport = 8000;
config.coverimage = "folder.jpg";
config.statefile = "/var/lib/mympd/mympd.state";
@ -178,10 +178,11 @@ int main(int argc, char **argv) {
}
}
else {
fprintf(stdout, "myMPD %d.%d.%d\n"
fprintf(stdout, "myMPD %s\n"
"Copyright (C) 2018 Juergen Mang <mail@jcgames.de>\n"
"https://github.com/jcorporation/myMPD\n"
"Built " __DATE__ " "__TIME__"\n\n",
MYMPD_VERSION_MAJOR, MYMPD_VERSION_MINOR, MYMPD_VERSION_PATCH);
MYMPD_VERSION);
printf("Usage: %s /path/to/mympd.conf\n", argv[0]);
return EXIT_FAILURE;
}
@ -223,12 +224,15 @@ int main(int argc, char **argv) {
struct passwd *pw;
if ((pw = getpwnam(config.user)) == NULL) {
printf("Unknown user\n");
mg_mgr_free(&mgr);
return EXIT_FAILURE;
} else if (setgid(pw->pw_gid) != 0) {
printf("setgid() failed\n");
mg_mgr_free(&mgr);
return EXIT_FAILURE;
} else if (setuid(pw->pw_uid) != 0) {
printf("setuid() failed\n");
mg_mgr_free(&mgr);
return EXIT_FAILURE;
}
}