summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2017-03-29 17:36:46 +0000
committerpeter1138 <peter1138@openttd.org>2017-03-29 17:36:46 +0000
commita9a7abf26c6556c874521deaf27956d84875cd03 (patch)
tree0a95992fb3e4cf8ae1b7ab41a5c247b7923d3881
parent06785c9df2065183a4890c15fdbb5a325ee8ccbf (diff)
downloadopenttd-a9a7abf26c6556c874521deaf27956d84875cd03.tar.xz
(svn r27834) -Change: Parse extmidi command string for parameters to pass on.
-rw-r--r--src/music/extmidi.cpp36
-rw-r--r--src/music/extmidi.h2
2 files changed, 30 insertions, 8 deletions
diff --git a/src/music/extmidi.cpp b/src/music/extmidi.cpp
index f4a35360c..12b3689e2 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<char *>(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 c6a9e08f8..cfbd89459 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;