removed %m formatstring, works now with openbsd, fixes #68

This commit is contained in:
Andrew Karpow 2015-04-25 00:35:01 +02:00
parent 3701a8510b
commit 4ba3e0b70f
1 changed files with 85 additions and 45 deletions

View File

@ -46,7 +46,7 @@ int callback_mpd(struct mg_connection *c)
size_t n = 0; size_t n = 0;
unsigned int uint_buf, uint_buf_2; unsigned int uint_buf, uint_buf_2;
int int_buf; int int_buf;
char *p_charbuf = NULL; char *p_charbuf = NULL, *token;
if(cmd_id == -1) if(cmd_id == -1)
return MG_TRUE; return MG_TRUE;
@ -119,55 +119,89 @@ int callback_mpd(struct mg_connection *c)
n = mpd_put_queue(mpd.buf, uint_buf); n = mpd_put_queue(mpd.buf, uint_buf);
break; break;
case MPD_API_GET_BROWSE: case MPD_API_GET_BROWSE:
if(sscanf(c->content, "MPD_API_GET_BROWSE,%u,%m[^\t\n]", &uint_buf, &p_charbuf) && p_charbuf != NULL) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_BROWSE"))
n = mpd_put_browse(mpd.buf, p_charbuf, uint_buf); goto out_browse;
free(p_charbuf);
} uint_buf = strtoul(strtok(NULL, ","), NULL, 10);
if((token = strtok(NULL, ",")) == NULL)
goto out_browse;
n = mpd_put_browse(mpd.buf, token, uint_buf);
out_browse:
free(p_charbuf);
break; break;
case MPD_API_ADD_TRACK: case MPD_API_ADD_TRACK:
if(sscanf(c->content, "MPD_API_ADD_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_TRACK"))
mpd_run_add(mpd.conn, p_charbuf); goto out_add_track;
free(p_charbuf);
} if((token = strtok(NULL, ",")) == NULL)
goto out_add_track;
mpd_run_add(mpd.conn, token);
out_add_track:
free(p_charbuf);
break; break;
case MPD_API_ADD_PLAY_TRACK: case MPD_API_ADD_PLAY_TRACK:
if(sscanf(c->content, "MPD_API_ADD_PLAY_TRACK,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAY_TRACK"))
int_buf = mpd_run_add_id(mpd.conn, p_charbuf); goto out_play_track;
if(int_buf != -1)
mpd_run_play_id(mpd.conn, int_buf); if((token = strtok(NULL, ",")) == NULL)
free(p_charbuf); goto out_play_track;
}
int_buf = mpd_run_add_id(mpd.conn, token);
if(int_buf != -1)
mpd_run_play_id(mpd.conn, int_buf);
out_play_track:
free(p_charbuf);
break; break;
case MPD_API_ADD_PLAYLIST: case MPD_API_ADD_PLAYLIST:
if(sscanf(c->content, "MPD_API_ADD_PLAYLIST,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAYLIST"))
mpd_run_load(mpd.conn, p_charbuf); goto out_playlist;
free(p_charbuf);
} if((token = strtok(NULL, ",")) == NULL)
goto out_playlist;
mpd_run_load(mpd.conn, token);
out_playlist:
free(p_charbuf);
break; break;
case MPD_API_SEARCH: case MPD_API_SEARCH:
if(sscanf(c->content, "MPD_API_SEARCH,%m[^\t\n]", &p_charbuf) && p_charbuf != NULL) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH"))
n = mpd_search(mpd.buf, p_charbuf); goto out_search;
free(p_charbuf);
} if((token = strtok(NULL, ",")) == NULL)
goto out_search;
n = mpd_search(mpd.buf, token);
out_search:
free(p_charbuf);
break; break;
#ifdef WITH_MPD_HOST_CHANGE #ifdef WITH_MPD_HOST_CHANGE
/* Commands allowed when disconnected from MPD server */ /* Commands allowed when disconnected from MPD server */
case MPD_API_SET_MPDHOST: case MPD_API_SET_MPDHOST:
int_buf = 0; int_buf = 0;
if(sscanf(c->content, "MPD_API_SET_MPDHOST,%d,%m[^\t\n ]", &int_buf, &p_charbuf) && p_charbuf = strdup(c->content);
p_charbuf != NULL && int_buf > 0) if(strcmp(strtok(p_charbuf, ","), "MPD_API_SET_MPDHOST"))
{ goto out_host_change;
strncpy(mpd.host, p_charbuf, sizeof(mpd.host));
free(p_charbuf); if((int_buf = strtol(strtok(NULL, ","), NULL, 10)) <= 0)
mpd.port = int_buf; goto out_host_change;
mpd.conn_state = MPD_RECONNECT;
return MG_TRUE; if((token = strtok(NULL, ",")) == NULL)
} goto out_host_change;
strncpy(mpd.host, token, sizeof(mpd.host));
mpd.port = int_buf;
mpd.conn_state = MPD_RECONNECT;
free(p_charbuf);
return MG_TRUE;
out_host_change:
free(p_charbuf);
break; break;
case MPD_API_GET_MPDHOST: case MPD_API_GET_MPDHOST:
n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": " n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"mpdhost\", \"data\": "
@ -175,15 +209,21 @@ int callback_mpd(struct mg_connection *c)
"}", mpd.host, mpd.port, mpd.password ? "true" : "false"); "}", mpd.host, mpd.port, mpd.password ? "true" : "false");
break; break;
case MPD_API_SET_MPDPASS: case MPD_API_SET_MPDPASS:
if(sscanf(c->content, "MPD_API_SET_MPDPASS,%m[^\t\n ]", &p_charbuf)) p_charbuf = strdup(c->content);
{ if(strcmp(strtok(p_charbuf, ","), "MPD_API_SET_MPDPASS"))
if(mpd.password) goto out_set_pass;
free(mpd.password);
mpd.password = p_charbuf; if((token = strtok(NULL, ",")) == NULL)
mpd.conn_state = MPD_RECONNECT; goto out_set_pass;
return MG_TRUE;
} if(mpd.password)
free(mpd.password);
mpd.password = token;
mpd.conn_state = MPD_RECONNECT;
return MG_TRUE;
out_set_pass:
free(p_charbuf);
break; break;
#endif #endif
} }