run_input.cxx 2.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "config/ConfigGlobal.hxx"
Max Kellermann's avatar
Max Kellermann committed
25 26
#include "input/InputStream.hxx"
#include "input/Init.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 "archive/ArchiveList.hxx"
34 35
#endif

36
#ifdef HAVE_GLIB
37
#include <glib.h>
38
#endif
39 40

#include <unistd.h>
41
#include <stdlib.h>
42

43
static int
44
dump_input_stream(InputStream *is)
45
{
46
	Error error;
47 48 49 50
	char buffer[4096];
	size_t num_read;
	ssize_t num_written;

51
	is->Lock();
52

53 54
	/* print meta data */

55
	if (!is->mime.empty())
56
		fprintf(stderr, "MIME type: %s\n", is->mime.c_str());
57 58 59

	/* read data and tags from the stream */

60 61
	while (!is->IsEOF()) {
		Tag *tag = is->ReadTag();
62
		if (tag != NULL) {
63
			fprintf(stderr, "Received a tag:\n");
Max Kellermann's avatar
Max Kellermann committed
64 65
			tag_save(stderr, *tag);
			delete tag;
66 67
		}

68
		num_read = is->Read(buffer, sizeof(buffer), error);
69
		if (num_read == 0) {
70
			if (error.IsDefined())
71
				LogError(error);
72

73
			break;
74
		}
75 76 77 78 79 80

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

81
	if (!is->Check(error)) {
82
		LogError(error);
83
		is->Unlock();
84 85 86
		return EXIT_FAILURE;
	}

87
	is->Unlock();
88

89 90 91 92 93
	return 0;
}

int main(int argc, char **argv)
{
94
	Error error;
95
	InputStream *is;
96 97 98
	int ret;

	if (argc != 2) {
99 100
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
101 102 103 104
	}

	/* initialize GLib */

105
#ifdef HAVE_GLIB
106
#if !GLIB_CHECK_VERSION(2,32,0)
107
	g_thread_init(NULL);
108
#endif
109 110
#endif

111 112 113 114
	/* initialize MPD */

	config_global_init();

115
	io_thread_init();
116
	io_thread_start();
117

118 119 120 121
#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

122
	if (!input_stream_global_init(error)) {
123
		LogError(error);
Max Kellermann's avatar
Max Kellermann committed
124 125
		return 2;
	}
126 127 128

	/* open the stream and dump it */

129 130
	Mutex mutex;
	Cond cond;
131

132
	is = InputStream::OpenReady(argv[1], mutex, cond, error);
133 134
	if (is != NULL) {
		ret = dump_input_stream(is);
135
		is->Close();
136
	} else {
137
		if (error.IsDefined())
138
			LogError(error);
139
		else
140 141
			fprintf(stderr, "input_stream::Open() failed\n");
		ret = EXIT_FAILURE;
142 143
	}

144 145 146
	/* deinitialize everything */

	input_stream_global_finish();
147 148 149 150 151

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

152 153
	io_thread_deinit();

154 155
	config_global_finish();

156
	return ret;
157
}