GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/commandline.cpp Lines: 1 137 0.7 %
Date: 2017-11-29 Branches: 0 43 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "commandline.h"
24
25
#include "main.h"
26
#include "settings.h"
27
28
#include "fs/paths.h"
29
30
#include "utils/cast.h"
31
#include "utils/gettext.h"
32
#include "utils/stringutils.h"
33
34
#include <getopt.h>
35
#include <iostream>
36
#include <unistd.h>
37
38
#include "debug.h"
39
40
static void printHelp()
41
{
42
    std::cout <<
43
        // TRANSLATORS: command line help
44
        _("manaplus [options] [manaplus-file]") <<
45
        std::endl << std::endl <<
46
        // TRANSLATORS: command line help
47
        _("[manaplus-file] : The manaplus file is an XML file (.manaplus)") <<
48
        std::endl <<
49
        // TRANSLATORS: command line help
50
        _("                  used to set custom parameters") <<
51
        std::endl <<
52
        // TRANSLATORS: command line help
53
        _("                  to the manaplus client.") <<
54
        std::endl << std::endl <<
55
        // TRANSLATORS: command line help
56
        _("Options:") <<
57
        std::endl <<
58
        // TRANSLATORS: command line help
59
        _("  -l --log-file       : Log file to use") << std::endl <<
60
        // TRANSLATORS: command line help
61
        _("  -a --chat-log-dir   : Chat log dir to use") << std::endl <<
62
        // TRANSLATORS: command line help
63
        _("  -v --version        : Display the version") << std::endl <<
64
        // TRANSLATORS: command line help
65
        _("  -h --help           : Display this help") << std::endl <<
66
        // TRANSLATORS: command line help
67
        _("  -C --config-dir     : Configuration directory to use") <<
68
        std::endl <<
69
        // TRANSLATORS: command line help
70
        _("  -U --username       : Login with this username") <<
71
        std::endl <<
72
        // TRANSLATORS: command line help
73
        _("  -P --password       : Login with this password") <<
74
        std::endl <<
75
        // TRANSLATORS: command line help
76
        _("  -c --character      : Login with this character") <<
77
        std::endl <<
78
        // TRANSLATORS: command line help
79
        _("  -s --server         : Login server name or IP") <<
80
        std::endl <<
81
        // TRANSLATORS: command line help
82
        _("  -y --server-type    : Login server type") <<
83
        std::endl <<
84
        // TRANSLATORS: command line help
85
        _("  -p --port           : Login server port") <<
86
        std::endl <<
87
        // TRANSLATORS: command line help
88
        _("  -H --update-host    : Use this update host") <<
89
        std::endl <<
90
        // TRANSLATORS: command line help
91
        _("  -D --default        : Choose default character server and "
92
            "character") <<
93
        std::endl <<
94
        // TRANSLATORS: command line help
95
        _("  -u --skip-update    : Skip the update downloads") <<
96
        std::endl <<
97
        // TRANSLATORS: command line help
98
        _("  -d --data           : Directory to load game data from") <<
99
        std::endl <<
100
        // TRANSLATORS: command line help
101
        _("  -L --localdata-dir  : Directory to use as local "
102
            "data directory") <<
103
        std::endl <<
104
        // TRANSLATORS: command line help
105
        _("     --screenshot-dir : Directory to store screenshots") <<
106
        std::endl <<
107
        // TRANSLATORS: command line help
108
        _("     --safemode       : Start game in safe mode") <<
109
        std::endl <<
110
        // TRANSLATORS: command line help
111
        _("     --renderer       : Set renderer type") <<
112
        std::endl <<
113
        // TRANSLATORS: command line help
114
        _("  -T --tests          : Start testing drivers and "
115
            "auto configuring") <<
116
        std::endl <<
117
        // TRANSLATORS: command line help
118
        _("  -V --validate       : Start validating client data") <<
119
        std::endl
120
#ifdef USE_OPENGL
121
        <<
122
        // TRANSLATORS: command line help
123
        _("  -O --no-opengl      : Disable OpenGL for this session") <<
124
        std::endl
125
#endif  // USE_OPENGL
126
        ;
127
}
128
129
static void printVersion()
130
{
131
    std::cout <<
132
        strprintf("ManaPlus client %s", FULL_VERSION) <<
133
        std::endl;
134
}
135
136
void parseOptions(const int argc, char *const argv[])
137
{
138
    const char *const optstring = "hvud:U:P:Dc:p:y:l:L:C:s:t:T:a:r:e:V";
139
140
    const struct option long_options[] =
141
    {
142
        { "config-dir",     required_argument, nullptr, 'C' },
143
        { "data",           required_argument, nullptr, 'd' },
144
        { "default",        no_argument,       nullptr, 'D' },
145
        { "password",       required_argument, nullptr, 'P' },
146
        { "character",      required_argument, nullptr, 'c' },
147
        { "help",           no_argument,       nullptr, 'h' },
148
        { "localdata-dir",  required_argument, nullptr, 'L' },
149
        { "update-host",    required_argument, nullptr, 'H' },
150
        { "port",           required_argument, nullptr, 'p' },
151
        { "server",         required_argument, nullptr, 's' },
152
        { "skip-update",    no_argument,       nullptr, 'u' },
153
        { "username",       required_argument, nullptr, 'U' },
154
        { "no-opengl",      no_argument,       nullptr, 'O' },
155
        { "chat-log-dir",   required_argument, nullptr, 'a' },
156
        { "version",        no_argument,       nullptr, 'v' },
157
        { "log-file",       required_argument, nullptr, 'l' },
158
        { "screenshot-dir", required_argument, nullptr, 'i' },
159
        { "safemode",       no_argument,       nullptr, 'm' },
160
        { "tests",          no_argument,       nullptr, 'T' },
161
        { "test",           required_argument, nullptr, 't' },
162
        { "renderer",       required_argument, nullptr, 'r' },
163
        { "server-type",    required_argument, nullptr, 'y' },
164
        { "enable-ipc",     no_argument,       nullptr, 'I' },
165
        { "hide-cursor",    no_argument,       nullptr, 'q' },
166
        { "error",          no_argument,       nullptr, 'e' },
167
        { "validate",       no_argument,       nullptr, 'V' },
168
        { nullptr,          0,                 nullptr, 0 }
169
    };
170
171
    Options &options = settings.options;
172
173
    while (optind < argc)
174
    {
175
        const int result = getopt_long(argc,
176
            argv,
177
            optstring,
178
            long_options,
179
            nullptr);
180
181
        if (result == -1)
182
            break;
183
184
        switch (result)
185
        {
186
            case 'C':
187
                options.configDir = optarg;
188
                break;
189
            case 'd':
190
                options.dataPath = optarg;
191
                break;
192
            case 'D':
193
                options.chooseDefault = true;
194
                break;
195
            case '?':  // Unknown option
196
            case ':':  // Missing argument
197
            case 'h':
198
                options.printHelp = true;
199
                break;
200
            case 'H':
201
                if (checkPath(optarg))
202
                    options.updateHost = optarg;
203
                else
204
                    options.updateHost.clear();
205
                break;
206
            case 'c':
207
                options.character = optarg;
208
                break;
209
            case 'P':
210
                options.password = optarg;
211
                break;
212
            case 's':
213
                options.serverName = optarg;
214
                break;
215
            case 'p':
216
                options.serverPort = CAST_U16(atoi(optarg));
217
                break;
218
            case 'u':
219
                options.skipUpdate = true;
220
                break;
221
            case 'U':
222
                options.username = optarg;
223
                break;
224
            case 'v':
225
                options.printVersion = true;
226
                break;
227
            case 'L':
228
                options.localDataDir = optarg;
229
                break;
230
            case 'O':
231
                options.noOpenGL = true;
232
                break;
233
            case 'l':
234
                options.logFileName = std::string(optarg);
235
                break;
236
            case 'a':
237
                options.chatLogDir = std::string(optarg);
238
                break;
239
            case 'i':
240
                options.screenshotDir = optarg;
241
                break;
242
            case 'm':
243
                options.safeMode = true;
244
                break;
245
            case 'T':
246
                options.testMode = true;
247
                options.test.clear();
248
                break;
249
            case 't':
250
                options.testMode = true;
251
                options.test = std::string(optarg);
252
                break;
253
            case 'r':
254
                options.renderer = CAST_S32(atoi(optarg));
255
                break;
256
            case 'y':
257
                options.serverType = optarg;
258
                break;
259
            case 'I':
260
                options.ipc = true;
261
                break;
262
            case 'q':
263
                options.hideCursor = true;
264
                break;
265
            case 'e':
266
                options.error = true;
267
                break;
268
            case 'V':
269
                options.validate = true;
270
                break;
271
            default:
272
                break;
273
        }
274
    }
275
276
    // when there are still options left use the last
277
    // one as branding file
278
    if (optind < argc)
279
    {
280
        options.brandingPath = argv[optind];
281
    }
282
283
    if (settings.options.printHelp)
284
    {
285
        printHelp();
286
        _exit(0);
287
    }
288
    else if (settings.options.printVersion)
289
    {
290
        printVersion();
291
        _exit(0);
292
    }
293
4
}