diff --git a/src/music/extmidi.cpp b/src/music/extmidi.cpp index f4a35360c0..12b3689e2a 100644 --- a/src/music/extmidi.cpp +++ b/src/music/extmidi.cpp @@ -12,6 +12,7 @@ #include "../stdafx.h" #include "../debug.h" #include "../string_func.h" +#include "../core/alloc_func.hpp" #include "../sound/sound_driver.hpp" #include "../video/video_driver.hpp" #include "../gfx_func.h" @@ -42,9 +43,33 @@ const char *MusicDriver_ExtMidi::Start(const char * const * parm) } const char *command = GetDriverParam(parm, "cmd"); +#ifndef MIDI_ARG if (StrEmpty(command)) command = EXTERNAL_PLAYER; +#else + if (StrEmpty(command)) command = EXTERNAL_PLAYER " " MIDI_ARG; +#endif + + /* Count number of arguments, but include 3 extra slots: 1st for command, 2nd for song title, and 3rd for terminating NULL. */ + uint num_args = 3; + for (const char *t = command; *t != '\0'; t++) if (*t == ' ') num_args++; + + this->params = CallocT(num_args); + this->params[0] = stredup(command); + + /* Replace space with \0 and add next arg to params */ + uint p = 1; + while (true) { + this->params[p] = strchr(this->params[p - 1], ' '); + if (this->params[p] == NULL) break; + + this->params[p][0] = '\0'; + this->params[p]++; + p++; + } + + /* Last parameter is the song file. */ + this->params[p] = this->song; - this->command = stredup(command); this->song[0] = '\0'; this->pid = -1; return NULL; @@ -52,7 +77,8 @@ const char *MusicDriver_ExtMidi::Start(const char * const * parm) void MusicDriver_ExtMidi::Stop() { - free(command); + free(params[0]); + free(params); this->song[0] = '\0'; this->DoStop(); } @@ -91,11 +117,7 @@ void MusicDriver_ExtMidi::DoPlay() close(0); int d = open("/dev/null", O_RDONLY); if (d != -1 && dup2(d, 1) != -1 && dup2(d, 2) != -1) { - #if defined(MIDI_ARG) - execlp(this->command, "extmidi", MIDI_ARG, this->song, (char*)0); - #else - execlp(this->command, "extmidi", this->song, (char*)0); - #endif + execvp(this->params[0], this->params); } _exit(1); } diff --git a/src/music/extmidi.h b/src/music/extmidi.h index c6a9e08f8d..cfbd894596 100644 --- a/src/music/extmidi.h +++ b/src/music/extmidi.h @@ -16,7 +16,7 @@ class MusicDriver_ExtMidi : public MusicDriver { private: - char *command; + char **params; char song[MAX_PATH]; pid_t pid;