summaryrefslogtreecommitdiff
path: root/indra/appearance_utility/appearance_utility.cpp
blob: 130bca84a89b265d93f4e0b731092e5385e3209d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/**
 * @file appearance_utility.cpp
 * @author Don Kjer <don@lindenlab.com>, Nyx Linden
 * @brief Utility for processing avatar appearance without a full viewer implementation.
 *
 * $LicenseInfo:firstyear=2012&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2012, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include <iostream>
#include <sstream>
#include <string>

#include "linden_common.h"

#include "llapp.h"
#include "llapr.h"
#include "llerrorcontrol.h"
#include "llsd.h"
#include "llsdserialize.h"
#include "llsdutil.h"

enum EResult
{
	RV_SUCCESS = 0,
	RV_UNKNOWN_ERROR,
	RV_BAD_ARGUMENTS,
	RV_UNABLE_OPEN,
	RV_UNABLE_TO_PARSE,
};

static const std::string MESSAGE_RV_UNKNOWN("Unknown error.");
static const std::string MESSAGE_RV_ARGUMENTS 
("Invalid arguments: ");
static const std::string MESSAGE_RV_UNABLE_OPEN("Unable to open file: ");
static const std::string MESSAGE_RV_UNABLE_TO_PARSE("Unable to parse input LLSD.");

static const apr_getopt_option_t APPEARANCE_UTILITY_OPTIONS[] =
{
	{"tbd", 't', 0, "Extract dump information from a mesh asset."},
	{"output", 'o', 1, "The output file to write to.  Default is stdout"},
	{"agent-id", 'a', 1, "The agent-id of the user."},
	{"grid", 'g', 1, "The grid."},
	{"help", 'h', 0, "Print the help message."},
	{0, 0, 0, 0}
};

const std::string NOTHING_EXTRA("");

/**
 * Helper to return the standard error based on the return value.
 */
LLSD spit_error(EResult rv, const std::string& extra = NOTHING_EXTRA);

/**
 * Helper to generate an error output based on code and message.
 */
LLSD spit_error(const std::string& key, const std::string& message);



LLSD spit_error(EResult value, const std::string& extra)
{
	LLSD rv;
	switch(value)
	{
	case RV_UNKNOWN_ERROR:
		rv = spit_error("unknown", MESSAGE_RV_UNKNOWN);
	case RV_BAD_ARGUMENTS:
		rv = spit_error("arguments", MESSAGE_RV_ARGUMENTS + extra);
		break;
	case RV_UNABLE_OPEN:
		rv = spit_error("file", MESSAGE_RV_UNABLE_OPEN + extra);
		break;
	case RV_UNABLE_TO_PARSE:
		rv = spit_error("input", MESSAGE_RV_UNABLE_TO_PARSE);
		break;
	default:
		rv = spit_error("arguments", "Invalid arguments to spit_error");
		break;
	}
	return rv;
}

LLSD spit_error(const std::string& key, const std::string& message)
{
	LLSD rv;
	rv["success"] = false;
	rv["error"]["key"] = key;
	rv["error"]["message"] = message;
	return rv;
}


std::string usage(const char* command)
{
	std::ostringstream ostr;
	ostr << "Utilities for processing agent appearance data."
		<< std::endl << std::endl
		<< "Usage:" << std::endl
		<< "\t" << command << " [options] filename" << std::endl << std::endl
		<< "Will read from stdin if filename is set to '-'." << std::endl << std::endl
		<< "Options:" << std::endl;
	const apr_getopt_option_t* option = &APPEARANCE_UTILITY_OPTIONS[0];
	while(option->name)
	{
		ostr << "\t--" << option->name << "\t\t"
			<< option->description << std::endl;
		++option;
	}
	ostr << std::endl << "Return Values:" << std::endl
		<< "\t0\t\tSuccess." << std::endl
		<< "\t1\t\tUnknown error." << std::endl
		<< "\t2\t\tBad arguments." << std::endl
		<< "\t3\t\tUnable to open file. Possibly wrong filename"
		<< " or bad permissions." << std::endl
		<< "\t4\t\tUnable to parse input LLSD." << std::endl
		<< std::endl
		<< "Output:" << std::endl
		<< "If a non-zero status code is returned, additional error information"
		<< " will be returned on stderr." << std::endl
		<< "* This will be in the form of an LLSD document." << std::endl
		<< "* Check ['error']['message'] to get a human readable message." << std::endl
		<< "If a zero status code is returned, processed output will be written"
		<< " to the file specified by --out (or stdout, if not specified)." << std::endl
		<< std::endl
		<< std::endl;
	return ostr.str();
}

EResult process_tbd(LLSD& input, std::ostream& output, LLSD& error_llsd)
{
	EResult rv = RV_SUCCESS;

	LLSD result;
	result["success"] = true;
	result["input"] = input;
	output << LLSDOStreamer<LLSDXMLFormatter>(result);

	return rv;
}

/**
 * @brief Called by main() to ensure proper cleanup. Basically main().
 */
EResult process_command(int argc, char** argv, LLSD& error_llsd)
{
	EResult rv = RV_SUCCESS;

	////// BEGIN OPTION PARSING //////
	// Check for '-' as last option, since apr doesn't seem to like that.
	bool read_stdin = false;
	bool write_stdout = true;
	if (std::string(argv[argc-1]) == "-")
	{
		read_stdin = true;
		argc--;
	}

	apr_status_t apr_err;
	const char* opt_arg = NULL;
	int opt_id = 0;
	apr_getopt_t* os = NULL;
	if(APR_SUCCESS != apr_getopt_init(&os, gAPRPoolp, argc, argv))
	{
		std::cerr << "Unable to initialize apr" << std::endl;
		rv = RV_UNKNOWN_ERROR;
		error_llsd = spit_error(rv);
		return rv;
	}

	bool tbd = false;
	LLUUID agent_id;
	std::string output_filename;
	std::string grid;
	while(true)
	{
		apr_err = apr_getopt_long(os, APPEARANCE_UTILITY_OPTIONS, &opt_id, &opt_arg);
		if(APR_STATUS_IS_EOF(apr_err)) break;
		if(apr_err)
		{
			char buf[MAX_STRING];		/* Flawfinder: ignore */
			std::cerr << "Error parsing options: "
				<< apr_strerror(apr_err, buf, MAX_STRING) << std::endl;
			std::cerr << usage(os->argv[0]) <<  std::endl;
			rv = RV_BAD_ARGUMENTS;
			error_llsd = spit_error(rv, buf);
			return rv;
		}
		switch (opt_id)
		{
		case 't':
			tbd = true;
			break;
		case 'o':
			output_filename.assign(opt_arg);
			write_stdout=false;
			break;
		case 'a':
			agent_id.set(opt_arg);
			if (agent_id.isNull())
			{
				const char* INVALID_AGENT_ID="agent-id must be a valid uuid.";
				std::cerr << "Incorrect arguments. " << INVALID_AGENT_ID
					<< std::endl << usage(argv[0]) <<  std::endl;
				rv = RV_BAD_ARGUMENTS;
				error_llsd = spit_error(rv, INVALID_AGENT_ID);
				return rv;
			}
			break;
		case 'g':
			grid = opt_arg;
			break;
		case 'h':
			std::cout << usage(os->argv[0]);
			return RV_SUCCESS;
		default:
			std::cerr << usage(os->argv[0]);
			rv = RV_BAD_ARGUMENTS;
			error_llsd = spit_error(rv, "Unknown option.");
			return rv;
		}
	}
	////// END OPTION PARSING //////

	/////  BEGIN ARGUMENT VALIDATION /////
	std::string input_filename;

	// Make sure we don't have more than one command specified.
	if (!tbd)
	{
		const char* INVALID_MODE="Must specify mode. (tbd)";
		std::cerr << "Incorrect arguments. " << INVALID_MODE
				  << std::endl;
		std::cerr << usage(os->argv[0]);
		rv = RV_BAD_ARGUMENTS;
		error_llsd = spit_error(rv, INVALID_MODE);
		return rv;
	}

	// *TODO: Add debug mode(s).  Skip this in debug mode.
	LLError::setDefaultLevel(LLError::LEVEL_WARN);

	if (!read_stdin)
	{
		bool valid_input_filename = false;
		// Try to grab the input filename.
		if (os->argv && os->argv[os->ind])
		{
			input_filename.assign(os->argv[os->ind]);
			if (! input_filename.empty() )
			{
				valid_input_filename = true;
			}
		}
		if (!valid_input_filename)
		{
			const char* INVALID_FILENAME="Must specify input file.";
			std::cerr << "Incorrect arguments. " << INVALID_FILENAME
				<< std::endl << usage(os->argv[0]) <<  std::endl;
			rv = RV_BAD_ARGUMENTS;
			error_llsd = spit_error(rv, INVALID_FILENAME);
			return rv;
		}
	}

	/////  END ARGUMENT VALIDATION /////

	/////  BEGIN OPEN INPUT FILE ////
	std::ifstream input_file;
	std::stringstream data;
	std::istream* input = NULL;

	if (read_stdin)
	{
		// Read unformated data from stdin in to memory.
		const S32 BUFFER_SIZE = BUFSIZ;
		char buffer[BUFFER_SIZE];
		while (true)
		{
			std::cin.read(buffer, BUFFER_SIZE);
			// Check if anything out of the ordinary happened.
			if (!std::cin)
			{
				// See if something 'really bad' happened, or if we just
				// used up all of our buffer.
				if (std::cin.bad())
				{
					std::cerr << "Problem reading standard input." << std::endl;
					rv = RV_UNKNOWN_ERROR;
					error_llsd = spit_error(rv);
					return rv;
				}
				else
				{
					// Output normally.
					data.write(buffer, std::cin.gcount());
					if (std::cin.eof()) break;

					// Clear this problem.  We have handled it.
					std::cin.clear();
				}
			}
			else
			{
				data.write(buffer, std::cin.gcount());
				if (std::cin.eof()) break;
			}
		}
		input = &data;
	}
	else
	{
		// Make sure we can open the input file.
		input_file.open( input_filename.c_str(), std::fstream::in );
		if ( input_file.fail())
		{
			std::cerr << "Couldn't open input file '" << input_filename << "'." << std::endl;
			rv = RV_UNABLE_OPEN;
			error_llsd = spit_error(rv, input_filename);
			return rv;
		}
		input = &input_file;
	}
	/////  END OPEN INPUT FILE ////

	/////  BEGIN OPEN OUTPUT FILE ////
	std::fstream output_file;
	std::ostream* output = NULL;

	// Make sure we can open the output file.
	if (write_stdout)
	{
		output = &std::cout;
	}
	else
	{
		output_file.open( output_filename.c_str(), std::fstream::out );
		if ( output_file.fail() )
		{
			std::cerr << "Couldn't open output file '" << output_filename << "'." << std::endl;
			rv = RV_UNABLE_OPEN;
			error_llsd = spit_error(rv, output_filename);
			if (!read_stdin) input_file.close();
			return rv;
		}
		output = &output_file;
	}
	/////  END OPEN OUTPUT FILE ////

	/////  BEGIN INPUT PARSING ////
	LLSD input_llsd;
	LLSDSerialize::fromXML( input_llsd, *input );
	if (input_llsd.isUndefined())
	{
		rv = RV_UNABLE_TO_PARSE;
		error_llsd = spit_error(rv);
		return rv;
	}

	/////  END INPUT PARSING ////

	if (tbd)
	{
		rv = process_tbd(input_llsd, *output, error_llsd);
	}

	if (!write_stdout) output_file.close();
	if (!read_stdin) input_file.close();

	return rv;
}

class LLAppAppearanceUtility : public LLApp
{
public:
	LLAppAppearanceUtility() {}
	virtual ~LLAppAppearanceUtility() {}
	/*virtual*/ bool init();
	/*virtual*/ bool cleanup();
	/*virtual*/ bool mainLoop() { return true;}
};

bool LLAppAppearanceUtility::init()
{
	return true;
}

bool LLAppAppearanceUtility::cleanup()
{
	return true;
}

int main(int argc, char** argv)
{
	ll_init_apr();
	LLAppAppearanceUtility* app = new LLAppAppearanceUtility();
	app->init();
	LLSD error_llsd;
	EResult rv = process_command(argc, argv, error_llsd);
	if (RV_SUCCESS != rv)
	{
		std::cerr << LLSDOStreamer<LLSDXMLFormatter>(error_llsd);
	}
	app->cleanup();
	delete app;
	ll_cleanup_apr();
	return (int) rv;
}