Freeciv-3.3: /fast/freeciv/nightly/main/tools/ruledit/ruledit.cpp Source File

Freeciv-3.3

Loading...
Searching...
No Matches
ruledit.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include "fc_prehdrs.h"
19
20// ANSI
21#include <stdlib.h>
22
23#include <signal.h>
24
25#ifdef FREECIV_MSWINDOWS
26#include <windows.h>
27#endif
28
29// utility
30#include "fc_cmdline.h"
31#include "fciconv.h"
32#include "fcintl.h"
33#include "log.h"
34#include "registry.h"
35
36// common
37#include "fc_cmdhelp.h"
38#include "fc_interface.h"
39#include "version.h"
40
41// server
42#include "sernet.h"
43#include "settings.h"
44
45// tools/shared
46#include "tools_fc_interface.h"
47
48// ruledit
49#include "comments.h"
50#include "ruledit_qt.h"
51
52#include "ruledit.h"
53
54static int re_parse_cmdline(int argc, char *argv[]);
55
56 struct ruledit_arguments reargs;
57
58 static int fatal_assertions = -1;
59
60/**********************************************************************/
63 int main(int argc, char **argv)
64{
65 int ui_options;
66 int exit_status = EXIT_SUCCESS;
67
68 // Load Windows post-crash debugger
69#ifdef FREECIV_MSWINDOWS
70# ifndef FREECIV_NDEBUG
71 if (LoadLibrary("exchndl.dll") == nullptr) {
72# ifdef FREECIV_DEBUG
73 fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
74# endif // FREECIV_DEBUG
75 }
76# endif // FREECIV_NDEBUG
77#endif // FREECIV_MSWINDOWS
78
79 /* Initialize the fc_interface functions needed to understand rules.
80 * fc_interface_init_tool() includes low level support like
81 * guaranteeing that fc_vsnprintf() will work after it,
82 * so this need to be early. */
83 fc_interface_init_tool();
84
85#ifdef ENABLE_NLS
86 (void) bindtextdomain("freeciv-ruledit", get_locale_dir());
87#endif
88
89 init_character_encodings(FC_DEFAULT_DATA_ENCODING, FALSE);
90#ifdef ENABLE_NLS
91 bind_textdomain_codeset("freeciv-ruledit", get_internal_encoding());
92#endif
93
94 registry_module_init();
95
96 // Initialize command line arguments.
97 reargs.ruleset = nullptr;
98
99 ui_options = re_parse_cmdline(argc, argv);
100
101 if (ui_options != -1) {
102 init_connections();
103
104 settings_init(FALSE);
105
106 // Reset aifill to zero
107 game.info.aifill = 0;
108
109 game_init(FALSE);
110
111 i_am_tool();
112
113 if (comments_load()) {
114 ruledit_qt_run(ui_options, argv);
115
116 comments_free();
117 } else {
118 /* TRANS: 'Failed to load comments-x.y.txt' where x.y is
119 * freeciv version */
120 log_error(R__("Failed to load %s."), COMMENTS_FILE_NAME);
121
122 exit_status = EXIT_FAILURE;
123 }
124 }
125
126 registry_module_close();
127 log_close();
128 libfreeciv_free();
129
130 // Clean up command line arguments.
131 cmdline_option_values_free();
132
133 return exit_status;
134}
135
136/**********************************************************************/
139 static int re_parse_cmdline(int argc, char *argv[])
140{
141 enum log_level loglevel = LOG_NORMAL;
142 int i = 1;
143 bool ui_separator = FALSE;
144 int ui_options = 0;
145
146 while (i < argc) {
147 char *option;
148
149 if (ui_separator) {
150 argv[1 + ui_options] = argv[i];
151 ui_options++;
152 } else if (is_option("--help", argv[i])) {
153 struct cmdhelp *help = cmdhelp_new(argv[0]);
154
155 cmdhelp_add(help, "h", "help",
156 R__("Print a summary of the options"));
157#ifdef FREECIV_DEBUG
158 cmdhelp_add(help, "d",
159 // TRANS: "debug" is exactly what user must type, do not translate.
160 R__("debug LEVEL"),
161 R__("Set debug log level (one of f,e,w,n,v,d, or "
162 "d:file1,min,max:...)"));
163#else /* FREECIV_DEBUG */
164 cmdhelp_add(help, "d",
165 // TRANS: "debug" is exactly what user must type, do not translate.
166 R__("debug LEVEL"),
167 R__("Set debug log level (one of f,e,w,n,v)"));
168#endif /* FREECIV_DEBUG */
169 cmdhelp_add(help, "v", "version",
170 R__("Print the version number"));
171 cmdhelp_add(help, "r",
172 // TRANS: argument (don't translate) VALUE (translate)
173 R__("ruleset RULESET"),
174 R__("Ruleset to use as the starting point."));
175#ifndef FREECIV_NDEBUG
176 cmdhelp_add(help, "F",
177 // TRANS: "Fatal" is exactly what user must type, do not translate.
178 R__("Fatal [SIGNAL]"),
179 R__("Raise a signal on failed assertion"));
180#endif // FREECIV_NDEBUG
181 /* The function below prints a header and footer for the options.
182 * Furthermore, the options are sorted. */
183 cmdhelp_display(help, TRUE, TRUE, TRUE);
184 cmdhelp_destroy(help);
185
186 exit(EXIT_SUCCESS);
187 } else if (is_option("--version", argv[i])) {
188 fc_fprintf(stderr, "%s \n", freeciv_name_version());
189
190 exit(EXIT_SUCCESS);
191 } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, true))) {
192 if (reargs.ruleset) {
193 fc_fprintf(stderr, R__("Can only edit one ruleset at a time.\n"));
194 } else {
195 reargs.ruleset = option;
196 }
197 } else if ((option = get_option_malloc("--debug", argv, &i, argc, FALSE))) {
198 if (!log_parse_level_str(option, &loglevel)) {
199 fc_fprintf(stderr,
200 R__("Invalid debug level \"%s\" specified with --debug "
201 "option.\n"), option);
202 fc_fprintf(stderr, R__("Try using --help.\n"));
203 exit(EXIT_FAILURE);
204 }
205 free(option);
206#ifndef FREECIV_NDEBUG
207 } else if (is_option("--Fatal", argv[i])) {
208 if (i + 1 >= argc || '-' == argv[i + 1][0]) {
209 fatal_assertions = SIGABRT;
210 } else if (str_to_int(argv[i + 1], &fatal_assertions)) {
211 i++;
212 } else {
213 fc_fprintf(stderr, R__("Invalid signal number \"%s\".\n"),
214 argv[i + 1]);
215 fc_fprintf(stderr, R__("Try using --help.\n"));
216 exit(EXIT_FAILURE);
217 }
218#endif // FREECIV_NDEBUG
219 } else if (is_option("--", argv[i])) {
220 ui_separator = TRUE;
221 } else {
222 fc_fprintf(stderr, R__("Unrecognized option: \"%s\"\n"), argv[i]);
223 exit(EXIT_FAILURE);
224 }
225
226 i++;
227 }
228
229 log_init(nullptr, loglevel, nullptr, nullptr, fatal_assertions);
230
231 return ui_options;
232}
233
234/**********************************************************************/
237 void show_experimental(QWidget *wdg)
238{
239#ifdef RULEDIT_EXPERIMENTAL
240 wdg->show();
241#else
242 wdg->hide();
243#endif
244}
comments_free
void comments_free(void)
Definition comments.c:272
comments_load
bool comments_load(void)
Definition comments.c:106
incite_cost
char * incite_cost
Definition comments.c:74
comments.h
COMMENTS_FILE_NAME
#define COMMENTS_FILE_NAME
Definition comments.h:20
cmdhelp_destroy
void cmdhelp_destroy(struct cmdhelp *pcmdhelp)
Definition fc_cmdhelp.c:70
cmdhelp_display
void cmdhelp_display(struct cmdhelp *pcmdhelp, bool sort, bool gui_options, bool report_bugs)
Definition fc_cmdhelp.c:104
cmdhelp_new
struct cmdhelp * cmdhelp_new(const char *cmdname)
Definition fc_cmdhelp.c:57
cmdhelp_add
void cmdhelp_add(struct cmdhelp *pcmdhelp, const char *shortarg, const char *longarg, const char *helpstr,...)
Definition fc_cmdhelp.c:86
fc_cmdhelp.h
is_option
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
get_option_malloc
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
cmdline_option_values_free
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
fc_cmdline.h
libfreeciv_free
void libfreeciv_free(void)
Definition fc_interface.c:102
fc_interface.h
i_am_tool
static void i_am_tool(void)
Definition fc_interface.h:80
fc_prehdrs.h
get_internal_encoding
const char * get_internal_encoding(void)
Definition fciconv.c:182
init_character_encodings
static char void init_character_encodings(const char *my_internal_encoding, bool my_use_transliteration)
Definition fciconv.c:70
fciconv.h
FC_DEFAULT_DATA_ENCODING
#define FC_DEFAULT_DATA_ENCODING
Definition fciconv.h:89
fc_fprintf
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
get_locale_dir
const char * get_locale_dir(void)
Definition fcintl.c:111
fcintl.h
bindtextdomain
#define bindtextdomain(Package, Directory)
Definition fcintl.h:82
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:62
game_init
void game_init(bool keep_ruleset_value)
Definition game.c:444
log_close
void log_close(void)
Definition log.c:270
log_init
void log_init(const char *filename, enum log_level initial_level, log_callback_fn callback, log_prefix_fn prefix, int fatal_assertions)
Definition log.c:245
log_parse_level_str
bool log_parse_level_str(const char *level_str, enum log_level *ret_level)
Definition log.c:86
log.h
log_level
log_level
Definition log.h:28
LOG_NORMAL
@ LOG_NORMAL
Definition log.h:32
log_error
#define log_error(message,...)
Definition log.h:103
registry_module_init
void registry_module_init(void)
Definition registry.c:30
registry_module_close
void registry_module_close(void)
Definition registry.c:40
registry.h
reargs
struct ruledit_arguments reargs
Definition ruledit.cpp:56
main
int main(int argc, char **argv)
Definition ruledit.cpp:63
re_parse_cmdline
static int re_parse_cmdline(int argc, char *argv[])
Definition ruledit.cpp:139
show_experimental
void show_experimental(QWidget *wdg)
Definition ruledit.cpp:237
fatal_assertions
static int fatal_assertions
Definition ruledit.cpp:58
ruledit.h
ruledit_qt_run
int ruledit_qt_run(int argc, char **argv)
Definition ruledit_qt.cpp:71
ruledit_qt.h
init_connections
void init_connections(void)
Definition sernet.c:1362
sernet.h
settings_init
void settings_init(bool act)
Definition settings.c:5205
settings.h
str_to_int
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
civ_game::info
struct packet_game_info info
Definition game.h:89
cmdhelp
Definition fc_cmdhelp.c:43
option
Definition options.c:514
packet_game_info::aifill
int aifill
Definition packets_gen.h:122
ruledit_arguments
Definition ruledit.h:18
ruledit_arguments::ruleset
char * ruleset
Definition ruledit.h:20
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
fc_interface_init_tool
void fc_interface_init_tool(void)
Definition tools_fc_interface.c:60
tools_fc_interface.h
freeciv_name_version
const char * freeciv_name_version(void)
Definition version.c:35
version.h