run_input.cxx 2.97 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 "util/Error.hxx"
28
#include "thread/Cond.hxx"
29
#include "Log.hxx"
30 31
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
32

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

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

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

49
static int
50
dump_input_stream(InputStream *is)
51
{
52
	is->Lock();
53

54 55
	/* print meta data */

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

	/* read data and tags from the stream */

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

69 70 71
		Error error;
		char buffer[4096];
		size_t num_read = is->Read(buffer, sizeof(buffer), error);
72
		if (num_read == 0) {
73
			if (error.IsDefined())
74
				LogError(error);
75

76
			break;
77
		}
78

79
		ssize_t num_written = write(1, buffer, num_read);
80 81 82 83
		if (num_written <= 0)
			break;
	}

84
	Error error;
85
	if (!is->Check(error)) {
86
		LogError(error);
87
		is->Unlock();
88 89 90
		return EXIT_FAILURE;
	}

91
	is->Unlock();
92

93 94 95 96 97 98
	return 0;
}

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

	/* initialize MPD */

	config_global_init();

107
	const ScopeIOThread io_thread;
108

109 110 111 112
#ifdef ENABLE_ARCHIVE
	archive_plugin_init_all();
#endif

113
	Error error;
114
	if (!input_stream_global_init(error)) {
115
		LogError(error);
Max Kellermann's avatar
Max Kellermann committed
116 117
		return 2;
	}
118 119 120

	/* open the stream and dump it */

121
	int ret;
122 123 124 125 126 127 128 129 130 131 132 133 134
	{
		Mutex mutex;
		Cond cond;
		auto is = InputStream::OpenReady(argv[1], mutex, cond, error);
		if (is) {
			ret = dump_input_stream(is.get());
		} else {
			if (error.IsDefined())
				LogError(error);
			else
				fprintf(stderr, "input_stream::Open() failed\n");
			ret = EXIT_FAILURE;
		}
135 136
	}

137 138 139
	/* deinitialize everything */

	input_stream_global_finish();
140 141 142 143 144

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

145 146
	config_global_finish();

147
	return ret;
148
}