forked from Warzone2100/warzone2100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclparse.cpp
671 lines (592 loc) · 18.2 KB
/
clparse.cpp
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2019 Warzone 2100 Project
Warzone 2100 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.
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* clParse.c
*
* Parse command line arguments
*
*/
#include "lib/framework/frame.h"
#include "lib/framework/opengl.h"
#include "lib/ivis_opengl/screen.h"
#include "lib/netplay/netplay.h"
#include "lib/ivis_opengl/pieclip.h"
#include "levels.h"
#include "clparse.h"
#include "display3d.h"
#include "frontend.h"
#include "keybind.h"
#include "loadsave.h"
#include "main.h"
#include "modding.h"
#include "multiplay.h"
#include "version.h"
#include "warzoneconfig.h"
#include "wrappers.h"
//////
// Our fine replacement for the popt abomination follows
#define POPT_ARG_STRING true
#define POPT_ARG_NONE false
#define POPT_ERROR_BADOPT -1
#define POPT_SKIP_MAC_PSN 666
struct poptOption
{
const char *string;
bool argument;
int enumeration;
const char *descrip;
const char *argDescrip;
};
typedef struct _poptContext
{
int argc = 0;
int current = 0;
int size = 0;
const char * const *argv = nullptr;
const char *parameter = nullptr;
const char *bad = nullptr;
const struct poptOption *table = nullptr;
} *poptContext;
/// TODO: Find a way to use the real qFatal from Qt
#undef qFatal
#define qFatal(...) { fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); exit(1); }
/// Enable automatic test games
static bool wz_autogame = false;
static std::string wz_saveandquit;
static std::string wz_test;
static void poptPrintHelp(poptContext ctx, FILE *output)
{
// TRANSLATORS: Summary of commandline option syntax
fprintf(output, _("Usage: %s [OPTION...]\n"), ctx->argv[0]);
for (int i = 0; i < ctx->size; i++)
{
char txt[128];
ssprintf(txt, " --%s", ctx->table[i].string);
if (ctx->table[i].argument)
{
sstrcat(txt, "=");
sstrcat(txt, ctx->table[i].argDescrip);
}
// calculate number of terminal columns required to print
// for languages with multibyte characters
const size_t txtSize = strlen(txt) + 1;
int txtOffset = (int) mbstowcs(nullptr, txt, txtSize) + 1 - txtSize;
// CJK characters take up two columns
char language[3]; // stores ISO 639-1 code
strlcpy(language, setlocale(LC_MESSAGES, nullptr), sizeof(language));
if (strcmp(language, "zh") == 0 || strcmp(language, "ko") == 0)
{
txtOffset /= 2;
}
fprintf(output, "%-*s", 40 - txtOffset, txt);
if (ctx->table[i].descrip)
{
fprintf(output, "%s", ctx->table[i].descrip);
}
fprintf(output, "\n");
}
}
static const char *poptBadOption(poptContext ctx, WZ_DECL_UNUSED int unused)
{
return ctx->bad;
}
static const char *poptGetOptArg(poptContext ctx)
{
return ctx->parameter;
}
static int poptGetNextOpt(poptContext ctx)
{
static char match[PATH_MAX]; // static for bad function
static char parameter[PATH_MAX]; // static for arg function
char *pparam;
int i;
ctx->bad = nullptr;
ctx->parameter = nullptr;
parameter[0] = '\0';
match[0] = '\0';
if (ctx->current >= ctx->argc) // counts from 1
{
return 0;
}
if (strstr(ctx->argv[ctx->current], "-psn_"))
{
ctx->current++; // skip mac -psn_* Yum!
return POPT_SKIP_MAC_PSN;
}
sstrcpy(match, ctx->argv[ctx->current]);
ctx->current++;
pparam = strrchr(match, '=');
if (pparam) // option's got a parameter
{
*pparam++ = '\0'; // split option from parameter and increment past '='
if (pparam[0] == '"') // found scary quotes
{
pparam++; // skip start quote
sstrcpy(parameter, pparam); // copy first parameter
if (!strrchr(pparam, '"')) // if no end quote, then find it
{
while (!strrchr(parameter, '"') && ctx->current < ctx->argc)
{
sstrcat(parameter, " "); // insert space
sstrcat(parameter, ctx->argv[ctx->current]);
ctx->current++; // next part, please!
}
}
if (strrchr(parameter, '"')) // its not an else for above!
{
*strrchr(parameter, '"') = '\0'; // remove end qoute
}
}
else
{
sstrcpy(parameter, pparam); // copy parameter
}
}
for (i = 0; i < ctx->size; i++)
{
char slong[64];
ssprintf(slong, "--%s", ctx->table[i].string);
if (strcmp(slong, match) == 0)
{
if (ctx->table[i].argument && pparam)
{
ctx->parameter = parameter;
}
return ctx->table[i].enumeration;
}
}
ctx->bad = match;
ctx->current++;
return POPT_ERROR_BADOPT;
}
static poptContext poptGetContext(WZ_DECL_UNUSED void *unused, int argc, const char * const *argv, const struct poptOption *table, WZ_DECL_UNUSED int none)
{
static struct _poptContext ctx;
ctx.argc = argc;
ctx.argv = argv;
ctx.table = table;
ctx.current = 1;
ctx.parameter = nullptr;
for (ctx.size = 0; table[ctx.size].string; ctx.size++) ; // count table size
return &ctx;
}
typedef enum
{
// We don't want to use zero, so start at one (1)
CLI_CONFIGDIR = 1,
CLI_DATADIR,
CLI_DEBUG,
CLI_DEBUGFILE,
CLI_FLUSHDEBUGSTDERR,
CLI_FULLSCREEN,
CLI_GAME,
CLI_HELP,
CLI_MOD_GLOB,
CLI_MOD_CA,
CLI_MOD_MP,
CLI_LOADSKIRMISH,
CLI_LOADCAMPAIGN,
CLI_WINDOW,
CLI_VERSION,
CLI_RESOLUTION,
CLI_SHADOWS,
CLI_NOSHADOWS,
CLI_SOUND,
CLI_NOSOUND,
CLI_CONNECTTOIP,
CLI_HOSTLAUNCH,
CLI_NOASSERT,
CLI_CRASH,
CLI_TEXTURECOMPRESSION,
CLI_NOTEXTURECOMPRESSION,
CLI_AUTOGAME,
CLI_SAVEANDQUIT,
CLI_SKIRMISH,
} CLI_OPTIONS;
static const struct poptOption *getOptionsTable()
{
static const struct poptOption optionsTable[] =
{
{ "configdir", POPT_ARG_STRING, CLI_CONFIGDIR, N_("Set configuration directory"), N_("configuration directory") },
{ "datadir", POPT_ARG_STRING, CLI_DATADIR, N_("Add data directory"), N_("data directory") },
{ "debug", POPT_ARG_STRING, CLI_DEBUG, N_("Show debug for given level"), N_("debug level") },
{ "debugfile", POPT_ARG_STRING, CLI_DEBUGFILE, N_("Log debug output to file"), N_("file") },
{ "flush-debug-stderr", POPT_ARG_NONE, CLI_FLUSHDEBUGSTDERR, N_("Flush all debug output written to stderr"), nullptr },
{ "fullscreen", POPT_ARG_NONE, CLI_FULLSCREEN, N_("Play in fullscreen mode"), nullptr },
{ "game", POPT_ARG_STRING, CLI_GAME, N_("Load a specific game mode"), N_("level name") },
{ "help", POPT_ARG_NONE, CLI_HELP, N_("Show options and exit"), nullptr },
{ "mod", POPT_ARG_STRING, CLI_MOD_GLOB, N_("Enable a global mod"), N_("mod") },
{ "mod_ca", POPT_ARG_STRING, CLI_MOD_CA, N_("Enable a campaign only mod"), N_("mod") },
{ "mod_mp", POPT_ARG_STRING, CLI_MOD_MP, N_("Enable a multiplay only mod"), N_("mod") },
{ "noassert", POPT_ARG_NONE, CLI_NOASSERT, N_("Disable asserts"), nullptr },
{ "crash", POPT_ARG_NONE, CLI_CRASH, N_("Causes a crash to test the crash handler"), nullptr },
{ "loadskirmish", POPT_ARG_STRING, CLI_LOADSKIRMISH, N_("Load a saved skirmish game"), N_("savegame") },
{ "loadcampaign", POPT_ARG_STRING, CLI_LOADCAMPAIGN, N_("Load a saved campaign game"), N_("savegame") },
{ "window", POPT_ARG_NONE, CLI_WINDOW, N_("Play in windowed mode"), nullptr },
{ "version", POPT_ARG_NONE, CLI_VERSION, N_("Show version information and exit"), nullptr },
{ "resolution", POPT_ARG_STRING, CLI_RESOLUTION, N_("Set the resolution to use"), N_("WIDTHxHEIGHT") },
{ "shadows", POPT_ARG_NONE, CLI_SHADOWS, N_("Enable shadows"), nullptr },
{ "noshadows", POPT_ARG_NONE, CLI_NOSHADOWS, N_("Disable shadows"), nullptr },
{ "sound", POPT_ARG_NONE, CLI_SOUND, N_("Enable sound"), nullptr },
{ "nosound", POPT_ARG_NONE, CLI_NOSOUND, N_("Disable sound"), nullptr },
{ "join", POPT_ARG_STRING, CLI_CONNECTTOIP, N_("Connect directly to IP/hostname"), N_("host") },
{ "host", POPT_ARG_NONE, CLI_HOSTLAUNCH, N_("Go directly to host screen"), nullptr },
{ "texturecompression", POPT_ARG_NONE, CLI_TEXTURECOMPRESSION, N_("Enable texture compression"), nullptr },
{ "notexturecompression", POPT_ARG_NONE, CLI_NOTEXTURECOMPRESSION, N_("Disable texture compression"), nullptr },
{ "autogame", POPT_ARG_NONE, CLI_AUTOGAME, N_("Run games automatically for testing"), nullptr },
{ "saveandquit", POPT_ARG_STRING, CLI_SAVEANDQUIT, N_("Immediately save game and quit"), N_("save name") },
{ "skirmish", POPT_ARG_STRING, CLI_SKIRMISH, N_("Start skirmish game with given settings file"), N_("test") },
// Terminating entry
{ nullptr, 0, 0, nullptr, nullptr },
};
static struct poptOption TranslatedOptionsTable[sizeof(optionsTable) / sizeof(struct poptOption)];
static bool translated = false;
if (translated == false)
{
unsigned int table_size = sizeof(optionsTable) / sizeof(struct poptOption) - 1;
unsigned int i;
for (i = 0; i < table_size; ++i)
{
TranslatedOptionsTable[i] = optionsTable[i];
// If there is a description, make sure to translate it with gettext
if (TranslatedOptionsTable[i].descrip != nullptr)
{
TranslatedOptionsTable[i].descrip = gettext(TranslatedOptionsTable[i].descrip);
}
if (TranslatedOptionsTable[i].argDescrip != nullptr)
{
TranslatedOptionsTable[i].argDescrip = gettext(TranslatedOptionsTable[i].argDescrip);
}
}
translated = true;
}
return TranslatedOptionsTable;
}
//! Early parsing of the commandline
/**
* First half of the command line parsing. Also see ParseCommandLine()
* below. The parameters here are needed early in the boot process,
* while the ones in ParseCommandLine can benefit from debugging being
* set up first.
* \param argc number of arguments given
* \param argv string array of the arguments
* \return Returns true on success, false on error */
bool ParseCommandLineEarly(int argc, const char * const *argv)
{
poptContext poptCon = poptGetContext(nullptr, argc, argv, getOptionsTable(), 0);
int iOption;
#if defined(WZ_OS_MAC) && defined(DEBUG)
debug_enable_switch("all");
#endif /* WZ_OS_MAC && DEBUG */
/* loop through command line */
while ((iOption = poptGetNextOpt(poptCon)) > 0 || iOption == POPT_ERROR_BADOPT)
{
CLI_OPTIONS option = (CLI_OPTIONS)iOption;
const char *token;
if (iOption == POPT_ERROR_BADOPT)
{
qFatal("Unrecognized option: %s", poptBadOption(poptCon, 0));
}
switch (option)
{
case CLI_DEBUG:
// retrieve the debug section name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Usage: --debug=<flag>");
}
// Attempt to enable the given debug section
if (!debug_enable_switch(token))
{
qFatal("Debug flag \"%s\" not found!", token);
}
break;
case CLI_DEBUGFILE:
{
// find the file name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Missing debugfile filename?");
}
WzString debug_filename = token;
debug_register_callback(debug_callback_file, debug_callback_file_init, debug_callback_file_exit, &debug_filename); // note: by the time this function returns, all use of debug_filename has completed
customDebugfile = true;
break;
}
case CLI_FLUSHDEBUGSTDERR:
// Tell the debug stderr output callback to always flush its output
debugFlushStderr();
break;
case CLI_CONFIGDIR:
// retrieve the configuration directory
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Unrecognised configuration directory");
}
if (strlen(token) >= (sizeof(configdir) / sizeof(configdir[0])))
{
qFatal("Configuration directory exceeds maximum supported length on this platform");
}
sstrcpy(configdir, token);
break;
case CLI_HELP:
poptPrintHelp(poptCon, stdout);
return false;
case CLI_VERSION:
printf("Warzone 2100 - %s\n", version_getFormattedVersionString());
return false;
default:
break;
};
}
return true;
}
//! second half of parsing the commandline
/**
* Second half of command line parsing. See ParseCommandLineEarly() for
* the first half. Note that render mode must come before resolution flag.
* \param argc number of arguments given
* \param argv string array of the arguments
* \return Returns true on success, false on error */
bool ParseCommandLine(int argc, const char * const *argv)
{
poptContext poptCon = poptGetContext(nullptr, argc, argv, getOptionsTable(), 0);
int iOption;
/* loop through command line */
while ((iOption = poptGetNextOpt(poptCon)) > 0)
{
const char *token;
CLI_OPTIONS option = (CLI_OPTIONS)iOption;
switch (option)
{
case CLI_DEBUG:
case CLI_DEBUGFILE:
case CLI_FLUSHDEBUGSTDERR:
case CLI_CONFIGDIR:
case CLI_HELP:
case CLI_VERSION:
// These options are parsed in ParseCommandLineEarly() already, so ignore them
break;
case CLI_NOASSERT:
kf_NoAssert();
break;
// NOTE: The sole purpose of this is to test the crash handler.
case CLI_CRASH:
CauseCrash = true;
NetPlay.bComms = false;
sstrcpy(aLevelName, "CAM_3A");
SetGameMode(GS_NORMAL);
break;
case CLI_DATADIR:
// retrieve the quoted path name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Unrecognised datadir");
}
sstrcpy(datadir, token);
break;
case CLI_FULLSCREEN:
war_setFullscreen(true);
break;
case CLI_CONNECTTOIP:
//get the ip we want to connect with, and go directly to join screen.
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("No IP/hostname given");
}
sstrcpy(iptoconnect, token);
break;
case CLI_HOSTLAUNCH:
// go directly to host screen, bypass all others.
hostlaunch = 1;
break;
case CLI_GAME:
// retrieve the game name
token = poptGetOptArg(poptCon);
if (token == nullptr
|| (strcmp(token, "CAM_1A") && strcmp(token, "CAM_2A") && strcmp(token, "CAM_3A")
&& strcmp(token, "TUTORIAL3") && strcmp(token, "FASTPLAY")))
{
qFatal("The game parameter requires one of the following keywords:"
"CAM_1A, CAM_2A, CAM_3A, TUTORIAL3, or FASTPLAY.");
}
NetPlay.bComms = false;
bMultiPlayer = false;
bMultiMessages = false;
for (int i = 0; i < MAX_PLAYERS; i++)
{
NET_InitPlayer(i, true, false);
}
//NET_InitPlayer deallocates Player 0, who must be allocated so that a later invocation of processDebugMappings does not trigger DEBUG mode
NetPlay.players[0].allocated = true;
if (!strcmp(token, "CAM_1A") || !strcmp(token, "CAM_2A") || !strcmp(token, "CAM_3A"))
{
game.type = CAMPAIGN;
}
else
{
game.type = SKIRMISH; // tutorial is skirmish for some reason
}
sstrcpy(aLevelName, token);
SetGameMode(GS_NORMAL);
break;
case CLI_MOD_GLOB:
{
// retrieve the file name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Missing mod name?");
}
global_mods.push_back(token);
break;
}
case CLI_MOD_CA:
{
// retrieve the file name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Missing mod name?");
}
campaign_mods.push_back(token);
break;
}
case CLI_MOD_MP:
{
// retrieve the file name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Missing mod name?");
}
multiplay_mods.push_back(token);
break;
}
case CLI_RESOLUTION:
{
unsigned int width, height;
token = poptGetOptArg(poptCon);
if (sscanf(token, "%ux%u", &width, &height) != 2)
{
qFatal("Invalid parameter specified (format is WIDTHxHEIGHT, e.g. 800x600)");
}
if (width < 640)
{
debug(LOG_ERROR, "Screen width < 640 unsupported, using 640");
width = 640;
}
if (height < 480)
{
debug(LOG_ERROR, "Screen height < 480 unsupported, using 480");
height = 480;
}
// tell the display system of the desired resolution
pie_SetVideoBufferWidth(width);
pie_SetVideoBufferHeight(height);
// and update the configuration
war_SetWidth(width);
war_SetHeight(height);
break;
}
case CLI_LOADSKIRMISH:
// retrieve the game name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Unrecognised skirmish savegame name");
}
snprintf(saveGameName, sizeof(saveGameName), "%s/skirmish/%s.gam", SaveGamePath, token);
sstrcpy(sRequestResult, saveGameName); // hack to avoid crashes
SPinit();
bMultiPlayer = true;
game.type = SKIRMISH; // tutorial is skirmish for some reason
SetGameMode(GS_SAVEGAMELOAD);
break;
case CLI_LOADCAMPAIGN:
// retrieve the game name
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Unrecognised campaign savegame name");
}
snprintf(saveGameName, sizeof(saveGameName), "%s/campaign/%s.gam", SaveGamePath, token);
SPinit();
SetGameMode(GS_SAVEGAMELOAD);
break;
case CLI_WINDOW:
war_setFullscreen(false);
break;
case CLI_SHADOWS:
setDrawShadows(true);
break;
case CLI_NOSHADOWS:
setDrawShadows(false);
break;
case CLI_SOUND:
war_setSoundEnabled(true);
break;
case CLI_NOSOUND:
war_setSoundEnabled(false);
break;
case CLI_TEXTURECOMPRESSION:
wz_texture_compression = true;
break;
case CLI_NOTEXTURECOMPRESSION:
wz_texture_compression = false;
break;
case CLI_AUTOGAME:
wz_autogame = true;
break;
case CLI_SAVEANDQUIT:
token = poptGetOptArg(poptCon);
if (token == nullptr || !strchr(token, '/'))
{
qFatal("Bad savegame name (needs to be a full path)");
}
wz_saveandquit = token;
break;
case CLI_SKIRMISH:
hostlaunch = 2;
token = poptGetOptArg(poptCon);
if (token == nullptr)
{
qFatal("Bad test key");
}
wz_test = token;
break;
};
}
return true;
}
bool autogame_enabled()
{
return wz_autogame;
}
const std::string &saveandquit_enabled()
{
return wz_saveandquit;
}
const std::string &wz_skirmish_test()
{
return wz_test;
}