run_input.cxx 2.67 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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
#include "TagSave.hxx"
22
#include "tag/Tag.hxx"
23
#include "config/ConfigGlobal.hxx"
Max Kellermann's avatar
Max Kellermann committed
24 25
#include "input/InputStream.hxx"
#include "input/Init.hxx"
26
#include "ScopeIOThread.hxx"
27
#include "thread/Cond.hxx"
28
#include "Log.hxx"
29 30
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
31

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

36 37
#include <stdexcept>

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

41 42 43 44 45 46 47 48 49
static void
tag_save(FILE *file, const Tag &tag)
{
	StdioOutputStream sos(file);
	BufferedOutputStream bos(sos);
	tag_save(bos, tag);
	bos.Flush();
}

50
static int
51
dump_input_stream(InputStream *is)
52
{
53
	const ScopeLock protect(is->mutex);
54

55 56
	/* print meta data */

57 58
	if (is->HasMimeType())
		fprintf(stderr, "MIME type: %s\n", is->GetMimeType());
59 60 61

	/* read data and tags from the stream */

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

70
		char buffer[4096];
71 72
		size_t num_read = is->Read(buffer, sizeof(buffer));
		if (num_read == 0)
73 74
			break;

75
		ssize_t num_written = write(1, buffer, num_read);
76 77 78 79
		if (num_written <= 0)
			break;
	}

80
	is->Check();
81

82 83 84 85
	return 0;
}

int main(int argc, char **argv)
86
try {
87
	if (argc != 2) {
88 89
		fprintf(stderr, "Usage: run_input URI\n");
		return EXIT_FAILURE;
90 91 92 93 94 95
	}

	/* initialize MPD */

	config_global_init();

96
	const ScopeIOThread io_thread;
97

98 99 100 101
#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

102
	input_stream_global_init();
103 104 105

	/* open the stream and dump it */

106
	int ret;
107 108 109
	{
		Mutex mutex;
		Cond cond;
110 111
		auto is = InputStream::OpenReady(argv[1], mutex, cond);
		ret = dump_input_stream(is.get());
112 113
	}

114 115 116
	/* deinitialize everything */

	input_stream_global_finish();
117 118 119 120 121

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

122 123
	config_global_finish();

124
	return ret;
125 126 127
} catch (const std::exception &e) {
	LogError(e);
	return EXIT_FAILURE;
128
}