run_input.cxx 3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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 "ScopeIOThread.hxx"
28
#include "util/Error.hxx"
29
#include "thread/Cond.hxx"
30
#include "Log.hxx"
31 32
#include "fs/io/BufferedOutputStream.hxx"
#include "fs/io/StdioOutputStream.hxx"
33

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

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
	is->Lock();
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 71 72
		Error error;
		char buffer[4096];
		size_t num_read = is->Read(buffer, sizeof(buffer), error);
73
		if (num_read == 0) {
74
			if (error.IsDefined())
75
				LogError(error);
76

77
			break;
78
		}
79

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

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

92
	is->Unlock();
93

94 95 96 97 98 99
	return 0;
}

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

	/* initialize MPD */

	config_global_init();

108
	const ScopeIOThread io_thread;
109

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

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

	/* open the stream and dump it */

122 123
	Mutex mutex;
	Cond cond;
124

125 126
	InputStream *is = InputStream::OpenReady(argv[1], mutex, cond, error);
	int ret;
127 128
	if (is != NULL) {
		ret = dump_input_stream(is);
129
		delete is;
130
	} else {
131
		if (error.IsDefined())
132
			LogError(error);
133
		else
134 135
			fprintf(stderr, "input_stream::Open() failed\n");
		ret = EXIT_FAILURE;
136 137
	}

138 139 140
	/* deinitialize everything */

	input_stream_global_finish();
141 142 143 144 145

#ifdef ENABLE_ARCHIVE
	archive_plugin_deinit_all();
#endif

146 147
	config_global_finish();

148
	return ret;
149
}