run_input.cxx 3.32 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21 22
#include "TagSave.hxx"
#include "stdbin.h"
23
#include "tag/Tag.hxx"
24
#include "ConfigGlobal.hxx"
25
#include "InputStream.hxx"
26
#include "InputInit.hxx"
27
#include "IOThread.hxx"
28
#include "util/Error.hxx"
29
#include "thread/Cond.hxx"
30
#include "Log.hxx"
31

32
#ifdef ENABLE_ARCHIVE
33
#include "ArchiveList.hxx"
34 35
#endif

36 37 38
#include <glib.h>

#include <unistd.h>
39
#include <stdlib.h>
40 41

static void
42 43
my_log_func(const gchar *log_domain, gcc_unused GLogLevelFlags log_level,
	    const gchar *message, gcc_unused gpointer user_data)
44 45 46 47 48 49 50
{
	if (log_domain != NULL)
		g_printerr("%s: %s\n", log_domain, message);
	else
		g_printerr("%s\n", message);
}

51 52
static int
dump_input_stream(struct input_stream *is)
53
{
54
	Error error;
55 56 57 58
	char buffer[4096];
	size_t num_read;
	ssize_t num_written;

59
	is->Lock();
60

61
	/* wait until the stream becomes ready */
62

63
	is->WaitReady();
64

65
	if (!is->Check(error)) {
66
		LogError(error);
67
		is->Unlock();
68
		return EXIT_FAILURE;
69 70 71 72
	}

	/* print meta data */

73 74
	if (!is->mime.empty())
		g_printerr("MIME type: %s\n", is->mime.c_str());
75 76 77

	/* read data and tags from the stream */

78 79
	while (!is->IsEOF()) {
		Tag *tag = is->ReadTag();
80 81
		if (tag != NULL) {
			g_printerr("Received a tag:\n");
Max Kellermann's avatar
Max Kellermann committed
82 83
			tag_save(stderr, *tag);
			delete tag;
84 85
		}

86
		num_read = is->Read(buffer, sizeof(buffer), error);
87
		if (num_read == 0) {
88
			if (error.IsDefined())
89
				LogError(error);
90

91
			break;
92
		}
93 94 95 96 97 98

		num_written = write(1, buffer, num_read);
		if (num_written <= 0)
			break;
	}

99
	if (!is->Check(error)) {
100
		LogError(error);
101
		is->Unlock();
102 103 104
		return EXIT_FAILURE;
	}

105
	is->Unlock();
106

107 108 109 110 111
	return 0;
}

int main(int argc, char **argv)
{
112
	Error error;
113
	struct input_stream *is;
114 115 116 117 118 119 120 121 122
	int ret;

	if (argc != 2) {
		g_printerr("Usage: run_input URI\n");
		return 1;
	}

	/* initialize GLib */

123
#if !GLIB_CHECK_VERSION(2,32,0)
124
	g_thread_init(NULL);
125 126
#endif

127 128 129 130 131 132
	g_log_set_default_handler(my_log_func, NULL);

	/* initialize MPD */

	config_global_init();

133
	io_thread_init();
134
	io_thread_start();
135

136 137 138 139
#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

140
	if (!input_stream_global_init(error)) {
141
		LogError(error);
Max Kellermann's avatar
Max Kellermann committed
142 143
		return 2;
	}
144 145 146

	/* open the stream and dump it */

147 148
	Mutex mutex;
	Cond cond;
149

150
	is = input_stream::Open(argv[1], mutex, cond, error);
151 152
	if (is != NULL) {
		ret = dump_input_stream(is);
153
		is->Close();
154
	} else {
155
		if (error.IsDefined())
156
			LogError(error);
157
		else
158
			g_printerr("input_stream::Open() failed\n");
159 160 161
		ret = 2;
	}

162 163 164
	/* deinitialize everything */

	input_stream_global_finish();
165 166 167 168 169

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

170 171
	io_thread_deinit();

172 173
	config_global_finish();

174
	return ret;
175
}