summaryrefslogtreecommitdiff
path: root/indra/newview/llcommandlineparser.cpp
blob: f06a8abfe0f6da0f4dcfe4c90c8f6fa6cb424ed3 (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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/**
 * @file llcommandlineparser.cpp
 * @brief The LLCommandLineParser class definitions
 *
 * $LicenseInfo:firstyear=2007&license=viewergpl$
 * 
 * Copyright (c) 2007, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlife.com/developers/opensource/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at http://secondlife.com/developers/opensource/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */ 

#include "llviewerprecompiledheaders.h"
#include "llcommandlineparser.h"

// *NOTE: The boost::lexical_cast generates 
// the warning C4701(local used with out assignment) in VC7.1.
// Disable the warning for the boost includes.
#if _MSC_VER
#   pragma warning(push)
#   pragma warning( disable : 4701 )
#else
// NOTE: For the other platforms?
#endif

#include <boost/program_options.hpp>
#include <boost/bind.hpp>
#include<boost/tokenizer.hpp>

#if _MSC_VER
#   pragma warning(pop)
#endif

#include "llsdserialize.h"
#include <iostream>
#include <sstream>

#include "llcontrol.h"

namespace po = boost::program_options;

// *NTOE:MEP - Currently the boost object reside in file scope. 
// This has a couple of negatives, they are always around and 
// there can be only one instance of each. 
// The plus is that the boost-ly-ness of this implementation is 
// hidden from the rest of the world. 
// Its importatnt to realize that multiple LLCommandLineParser objects 
// will all have this single repository of option escs and parsed options.
// This could be good or bad, and probably won't matter for most use cases.
namespace 
{
    po::options_description gOptionsDesc;
    po::positional_options_description gPositionalOptions;
	po::variables_map gVariableMap;
    
    const LLCommandLineParser::token_vector_t gEmptyValue;

    void read_file_into_string(std::string& str, const std::basic_istream < char >& file)
    {
	    std::ostringstream oss;
	    oss << file.rdbuf();
	    str = oss.str();
    }

    bool gPastLastOption = false;
}

class LLCLPError : public std::logic_error {
public:
    LLCLPError(const std::string& what) : std::logic_error(what) {}
};

class LLCLPLastOption : public std::logic_error {
public:
    LLCLPLastOption(const std::string& what) : std::logic_error(what) {}
};

class LLCLPValue : public po::value_semantic_codecvt_helper<char> 
{
    unsigned mMinTokens;
    unsigned mMaxTokens;
    bool mIsComposing;
    typedef boost::function1<void, const LLCommandLineParser::token_vector_t&> notify_callback_t;
    notify_callback_t mNotifyCallback;
    bool mLastOption;

public:
    LLCLPValue() :
        mMinTokens(0),
        mMaxTokens(0),
        mIsComposing(false),
        mLastOption(false)
        {}
      
    virtual ~LLCLPValue() {};

    void setMinTokens(unsigned c) 
    {
        mMinTokens = c;
    }

    void setMaxTokens(unsigned c) 
    {
        mMaxTokens = c;
    }

    void setComposing(bool c)
    {
        mIsComposing = c;
    }

    void setLastOption(bool c)
    {
        mLastOption = c;
    }

    void setNotifyCallback(notify_callback_t f)
    {
        mNotifyCallback = f;
    }

    // Overrides to support the value_semantic interface.
    virtual std::string name() const 
    { 
        const std::string arg("arg");
        const std::string args("args");
        return (max_tokens() > 1) ? args : arg; 
    }

    virtual unsigned min_tokens() const
    {
        return mMinTokens;
    }

    virtual unsigned max_tokens() const 
    {
        return mMaxTokens;
    }

    virtual bool is_composing() const 
    {
        return mIsComposing;
    }

    virtual bool apply_default(boost::any& value_store) const
    {
        return false; // No defaults.
    }

    virtual void notify(const boost::any& value_store) const
    {
        const LLCommandLineParser::token_vector_t* value =
            boost::any_cast<const LLCommandLineParser::token_vector_t>(&value_store);
        if(mNotifyCallback) 
        {
           mNotifyCallback(*value);
        }

    }

protected:
    void xparse(boost::any& value_store,
         const std::vector<std::string>& new_tokens) const
    {
        if(gPastLastOption)
        {
            throw(LLCLPLastOption("Don't parse no more!"));
        }

        // Error checks. Needed?
        if (!value_store.empty() && !is_composing()) 
        {
            throw(LLCLPError("Non composing value with multiple occurences."));
        }
        if (new_tokens.size() < min_tokens() || new_tokens.size() > max_tokens())
        {
            throw(LLCLPError("Illegal number of tokens specified."));
        }
        
        if(value_store.empty())
        {
            value_store = boost::any(LLCommandLineParser::token_vector_t());
        }
        LLCommandLineParser::token_vector_t* tv = 
            boost::any_cast<LLCommandLineParser::token_vector_t>(&value_store); 
       
        for(unsigned i = 0; i < new_tokens.size() && i < mMaxTokens; ++i)
        {
            tv->push_back(new_tokens[i]);
        }

        if(mLastOption)
        {
            gPastLastOption = true;
        }
    }
};

//----------------------------------------------------------------------------
// LLCommandLineParser defintions
//----------------------------------------------------------------------------
void LLCommandLineParser::addOptionDesc(const LLString& option_name, 
                                        boost::function1<void, const token_vector_t&> notify_callback,
                                        unsigned int token_count,
                                        const LLString& description,
                                        const LLString& short_name,
                                        bool composing,
                                        bool positional,
                                        bool last_option)
{
    // Compose the name for boost::po. 
    // It takes the format "long_name, short name"
    const LLString comma(",");
    LLString boost_option_name = option_name;
    if(short_name != LLString::null)
    {
        boost_option_name += comma;
        boost_option_name += short_name;
    }
   
    LLCLPValue* value_desc = new LLCLPValue();
    value_desc->setMinTokens(token_count);
    value_desc->setMaxTokens(token_count);
    value_desc->setComposing(composing);
    value_desc->setLastOption(last_option);

    boost::shared_ptr<po::option_description> d(
            new po::option_description(boost_option_name.c_str(), 
                                    value_desc, 
                                    description.c_str()));

    if(!notify_callback.empty())
    {
        value_desc->setNotifyCallback(notify_callback);
    }

    gOptionsDesc.add(d);

    if(positional)
    {
        gPositionalOptions.add(boost_option_name.c_str(), token_count);
    }
}

bool LLCommandLineParser::parseAndStoreResults(po::command_line_parser& clp)
{
    try
    {
        clp.options(gOptionsDesc);
        clp.positional(gPositionalOptions);
        clp.style(po::command_line_style::default_style 
                  | po::command_line_style::allow_long_disguise);
		if(mExtraParser)
		{
			clp.extra_parser(mExtraParser);
		}
			
        po::basic_parsed_options<char> opts = clp.run();
        po::store(opts, gVariableMap);
    }
    catch(po::error& e)
    {
        llwarns << "Caught Error:" << e.what() << llendl;
		mErrorMsg = e.what();
        return false;
    }
    catch(LLCLPError& e)
    {
        llwarns << "Caught Error:" << e.what() << llendl;
		mErrorMsg = e.what();
        return false;
    }
    catch(LLCLPLastOption&) 
    {
		// This exception means a token was read after an option 
		// that must be the last option was reached (see url and slurl options)

        // boost::po will have stored a malformed option. 
        // All such options will be removed below.
		// The last option read, the last_option option, and its value
		// are put into the error message.
		std::string last_option;
		std::string last_value;
        for(po::variables_map::iterator i = gVariableMap.begin(); i != gVariableMap.end();)
        {
            po::variables_map::iterator tempI = i++;
            if(tempI->second.empty())
            {
                gVariableMap.erase(tempI);
            }
			else
			{
				last_option = tempI->first;
		        LLCommandLineParser::token_vector_t* tv = 
				    boost::any_cast<LLCommandLineParser::token_vector_t>(&(tempI->second.value())); 
				if(!tv->empty())
				{
					last_value = (*tv)[tv->size()-1];
				}
			}
        }

		// Continue without parsing.
		std::ostringstream msg;
		msg << "Caught Error: Found options after last option: " 
			<< last_option << " "
			<< last_value;

        llwarns << msg.str() << llendl;
		mErrorMsg = msg.str();
        return false;
    } 
    return true;
}

bool LLCommandLineParser::parseCommandLine(int argc, char **argv)
{
    po::command_line_parser clp(argc, argv);
    return parseAndStoreResults(clp);
}

bool LLCommandLineParser::parseCommandLineString(const std::string& str)
{
    // Split the string content into tokens
    boost::escaped_list_separator<char> sep("\\", "\r\n ", "\"'");
    boost::tokenizer< boost::escaped_list_separator<char> > tok(str, sep);
    std::vector<std::string> tokens;
    // std::copy(tok.begin(), tok.end(), std::back_inserter(tokens));
    for(boost::tokenizer< boost::escaped_list_separator<char> >::iterator i = tok.begin();
        i != tok.end();
        ++i)
    {
        if(0 != i->size())
        {
            tokens.push_back(*i);
        }
    }

    po::command_line_parser clp(tokens);
    return parseAndStoreResults(clp);
        
}

bool LLCommandLineParser::parseCommandLineFile(const std::basic_istream < char >& file)
{
    std::string args;
    read_file_into_string(args, file);

    return parseCommandLineString(args);
}

void LLCommandLineParser::notify()
{
    po::notify(gVariableMap);    
}

void LLCommandLineParser::printOptions() const
{
    for(po::variables_map::iterator i = gVariableMap.begin(); i != gVariableMap.end(); ++i)
    {
        std::string name = i->first;
        token_vector_t values = i->second.as<token_vector_t>();
        std::ostringstream oss;
        oss << name << ": ";
        for(token_vector_t::iterator t_itr = values.begin(); t_itr != values.end(); ++t_itr)
        {
            oss << t_itr->c_str() << " ";
        }
        llinfos << oss.str() << llendl;
    }
}

std::ostream& LLCommandLineParser::printOptionsDesc(std::ostream& os) const
{
    return os << gOptionsDesc;
}

bool LLCommandLineParser::hasOption(const std::string& name) const
{
    return gVariableMap.count(name) > 0;
}

const LLCommandLineParser::token_vector_t& LLCommandLineParser::getOption(const std::string& name) const
{
    if(hasOption(name))
    {
        return gVariableMap[name].as<token_vector_t>();
    }

    return gEmptyValue;
}

//----------------------------------------------------------------------------
// LLControlGroupCLP defintions
//----------------------------------------------------------------------------
void setControlValueCB(const LLCommandLineParser::token_vector_t& value, 
                       const LLString& opt_name, 
                       LLControlGroup* ctrlGroup)
{
    if(value.size() > 1)
    {
        llwarns << "Ignoring extra tokens mapped to the setting: " << opt_name << "." << llendl; 
    }
    
    // *FIX: Do sematic conversion here.
    // LLSD (ImplString) Is no good for doing string to type conversion for...
    // booleans
    // compound types
    // ?...

    LLControlVariable* ctrl = ctrlGroup->getControl(opt_name);
    if(NULL != ctrl)
    {
        switch(ctrl->type())
        {
        case TYPE_BOOLEAN:
            if(value.size() > 1)
            {
                llwarns << "Ignoring extra tokens." << llendl; 
            }
              
            if(value.size() > 0)
            {
                // There's a token. check the string for true/false/1/0 etc.
                BOOL result = false;
                BOOL gotSet = LLString::convertToBOOL(value[0], result);
                if(gotSet)
                {
                    ctrl->setValue(LLSD(result), false);
                }
            }
            else
            {
                ctrl->setValue(LLSD(true), false);
            }
            break;

        default:
            {
                // For the default types, let llsd do the conversion.
                if(value.size() > 1)
                {
                    // Assume its an array...
                    LLSD llsdArray;
                    for(unsigned int i = 0; i < value.size(); ++i)
                    {
                        LLSD llsdValue;
                        llsdValue.assign(LLSD::String(value[i]));
                        llsdArray.set(i, llsdValue);
                    }

                    ctrl->setValue(llsdArray, false);
                }
                else if(value.size() > 0)
                {
                    LLSD llsdValue;
                    llsdValue.assign(LLSD::String(value[0]));
                    ctrl->setValue(llsdValue, false);
                }
            }
            break;
        }
    }
    else
    {
        llwarns << "Command Line option mapping '" 
            << opt_name 
            << "' not found! Ignoring." 
            << llendl;
    }
}

void LLControlGroupCLP::configure(const LLString& config_filename, LLControlGroup* controlGroup)
{
    // This method reads the llsd based config file, and uses it to set 
    // members of a control group.
    LLSD clpConfigLLSD;
    
    llifstream input_stream;
    input_stream.open(config_filename.c_str(), std::ios::in | std::ios::binary);

    if(input_stream.is_open())
    {
        LLSDSerialize::fromXML(clpConfigLLSD, input_stream);
        for(LLSD::map_iterator option_itr = clpConfigLLSD.beginMap(); 
            option_itr != clpConfigLLSD.endMap(); 
            ++option_itr)
        {
            LLSD::String long_name = option_itr->first;
            LLSD option_params = option_itr->second;
            
            LLString desc("n/a");
            if(option_params.has("desc"))
            {
                desc = option_params["desc"].asString();
            }
            
            LLString short_name = LLString::null;
            if(option_params.has("short"))
            {
                short_name = option_params["short"].asString();
            }

            unsigned int token_count = 0;
            if(option_params.has("count"))
            {
                token_count = option_params["count"].asInteger();
            }

            bool composing = false;
            if(option_params.has("compose"))
            {
                composing = option_params["compose"].asBoolean();
            }

            bool positional = false;
            if(option_params.has("positional"))
            {
                positional = option_params["positional"].asBoolean();
            }

            bool last_option = false;
            if(option_params.has("last_option"))
            {
                last_option = option_params["last_option"].asBoolean();
            }

            boost::function1<void, const token_vector_t&> callback;
            if(option_params.has("map-to") && (NULL != controlGroup))
            {
                LLString controlName = option_params["map-to"].asString();
                callback = boost::bind(setControlValueCB, _1, 
                                       controlName, controlGroup);
            }

            this->addOptionDesc(
                long_name, 
                callback,
                token_count, 
                desc, 
                short_name, 
                composing,
                positional,
                last_option);
        }
    }
}