Freeciv-3.1: /fast/freeciv/nightly/S3_1/tools/ruleup.c Source File

Freeciv-3.1

Loading...
Searching...
No Matches
ruleup.c
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 <signal.h>
19
20#ifdef FREECIV_MSWINDOWS
21#include <windows.h>
22#endif
23
24/* utility */
25#include "fc_cmdline.h"
26#include "fciconv.h"
27#include "registry.h"
28#include "string_vector.h"
29
30/* common */
31#include "fc_cmdhelp.h"
32#include "fc_interface.h"
33
34/* server */
35#include "ruleset.h"
36#include "sernet.h"
37#include "settings.h"
38
39/* tools/shared */
40#include "tools_fc_interface.h"
41
42/* tools/ruleutil */
43#include "comments.h"
44#include "rulesave.h"
45
46 static char *rs_selected = NULL;
47 static char *od_selected = NULL;
48 static int fatal_assertions = -1;
49 static bool dirty = TRUE;
50
51/**********************************************************************/
54 static void rup_parse_cmdline(int argc, char *argv[])
55{
56 int i = 1;
57
58 while (i < argc) {
59 char *option = NULL;
60
61 if (is_option("--help", argv[i])) {
62 struct cmdhelp *help = cmdhelp_new(argv[0]);
63
64 cmdhelp_add(help, "h", "help",
65 _("Print a summary of the options"));
66#ifndef FREECIV_NDEBUG
67 cmdhelp_add(help, "F",
68 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
69 _("Fatal [SIGNAL]"),
70 _("Raise a signal on failed assertion or broken data"));
71#endif /* FREECIV_NDEBUG */
72 cmdhelp_add(help, "r",
73 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
74 _("ruleset RULESET"),
75 _("Update RULESET"));
76 cmdhelp_add(help, "o",
77 /* TRANS: "output" is exactly what user must type, do not translate. */
78 _("output DIRECTORY"),
79 _("Create directory DIRECTORY for output"));
80 cmdhelp_add(help, "c", "clean",
81 _("Clean up the ruleset before saving it."));
82
83 /* The function below prints a header and footer for the options.
84 * Furthermore, the options are sorted. */
85 cmdhelp_display(help, TRUE, FALSE, TRUE);
86 cmdhelp_destroy(help);
87
88 cmdline_option_values_free();
89
90 exit(EXIT_SUCCESS);
91 } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, TRUE))) {
92 if (rs_selected != NULL) {
93 fc_fprintf(stderr,
94 _("Multiple rulesets requested. Only one ruleset at time supported.\n"));
95 } else {
96 rs_selected = option;
97 }
98 } else if ((option = get_option_malloc("--output", argv, &i, argc, TRUE))) {
99 if (od_selected != NULL) {
100 fc_fprintf(stderr,
101 _("Multiple output directories given.\n"));
102 } else {
103 od_selected = option;
104 }
105#ifndef FREECIV_NDEBUG
106 } else if (is_option("--Fatal", argv[i])) {
107 if (i + 1 >= argc || '-' == argv[i + 1][0]) {
108 fatal_assertions = SIGABRT;
109 } else if (str_to_int(argv[i + 1], &fatal_assertions)) {
110 i++;
111 } else {
112 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
113 argv[i + 1]);
114 fc_fprintf(stderr, _("Try using --help.\n"));
115 exit(EXIT_FAILURE);
116 }
117#endif /* FREECIV_NDEBUG */
118 } else if (is_option("--clean", argv[i])) {
119 dirty = FALSE;
120 } else {
121 fc_fprintf(stderr, _("Unrecognized option: \"%s\"\n"), argv[i]);
122 cmdline_option_values_free();
123 exit(EXIT_FAILURE);
124 }
125
126 i++;
127 }
128}
129
130/**********************************************************************/
133 static void conv_log(const char *msg)
134{
135 log_normal("%s", msg);
136}
137
138/**********************************************************************/
141 int main(int argc, char **argv)
142{
143 enum log_level loglevel = LOG_NORMAL;
144 int exit_status = EXIT_SUCCESS;
145
146 /* Load Windows post-crash debugger */
147#ifdef FREECIV_MSWINDOWS
148# ifndef FREECIV_NDEBUG
149 if (LoadLibrary("exchndl.dll") == NULL) {
150# ifdef FREECIV_DEBUG
151 fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
152# endif /* FREECIV_DEBUG */
153 }
154# endif /* FREECIV_NDEBUG */
155#endif /* FREECIV_MSWINDOWS */
156
157 /* Initialize the fc_interface functions needed to understand rules.
158 * fc_interface_init_tool() includes low level support like
159 * guaranteeing that fc_vsnprintf() will work after it,
160 * so this need to be early. */
161 fc_interface_init_tool();
162
163 registry_module_init();
164 init_character_encodings(FC_DEFAULT_DATA_ENCODING, FALSE);
165
166 rup_parse_cmdline(argc, argv);
167
168 log_init(NULL, loglevel, NULL, NULL, fatal_assertions);
169
170 init_connections();
171
172 settings_init(FALSE);
173
174 game_init(FALSE);
175 i_am_tool();
176
177 /* Set ruleset user requested to use */
178 if (rs_selected == NULL) {
179 rs_selected = GAME_DEFAULT_RULESETDIR;
180 }
181 sz_strlcpy(game.server.rulesetdir, rs_selected);
182
183 /* Reset aifill to zero */
184 game.info.aifill = 0;
185
186 if (load_rulesets(NULL, NULL, TRUE, conv_log, FALSE, TRUE, TRUE)) {
187 struct rule_data data;
188 char tgt_dir[2048];
189
190 data.nationlist = game.server.ruledit.nationlist;
191
192 if (od_selected != NULL) {
193 fc_strlcpy(tgt_dir, od_selected, sizeof(tgt_dir));
194 } else {
195 fc_snprintf(tgt_dir, sizeof(tgt_dir), "%s.ruleup", rs_selected);
196 }
197
198 if (!comments_load()) {
199 /* TRANS: 'Failed to load comments-x.y.txt' where x.y is
200 * freeciv version */
201 log_error(R__("Failed to load %s."), COMMENTS_FILE_NAME);
202
203 /* Reuse fatal_assertions for failed comment loading. */
204 if (0 <= fatal_assertions) {
205 /* Emit a signal. */
206 raise(fatal_assertions);
207 }
208 }
209
210 /* Clean up unused entities added during the ruleset upgrade. */
211 if (!dirty) {
212 int purged = ruleset_purge_unused_entities();
213
214 if (purged > 0) {
215 log_normal("Purged %d unused entities after the ruleset upgrade",
216 purged);
217 }
218
219 purged = ruleset_purge_redundant_reqs();
220 if (purged > 0) {
221 log_normal("Purged %d redundant requirements after the ruleset"
222 " upgrade", purged);
223 }
224 }
225
226 save_ruleset(tgt_dir, game.control.name, &data);
227 log_normal("Saved %s", tgt_dir);
228 comments_free();
229 } else {
230 log_error(_("Can't load ruleset %s"), rs_selected);
231
232 /* Failed to upgrade the ruleset */
233 exit_status = EXIT_FAILURE;
234 }
235
236 registry_module_close();
237 log_close();
238 libfreeciv_free();
239 cmdline_option_values_free();
240
241 return exit_status;
242}
comments_free
void comments_free(void)
Definition comments.c:142
comments_load
bool comments_load(void)
Definition comments.c:61
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:103
fc_interface.h
i_am_tool
static void i_am_tool(void)
Definition fc_interface.h:80
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__
_
#define _(String)
Definition fcintl.h:67
R__
#define R__(String)
Definition fcintl.h:75
game
struct civ_game game
Definition game.c:57
game_init
void game_init(bool keep_ruleset_value)
Definition game.c:429
GAME_DEFAULT_RULESETDIR
#define GAME_DEFAULT_RULESETDIR
Definition game.h:654
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_normal
#define log_normal(message,...)
Definition log.h:107
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
save_ruleset
bool save_ruleset(const char *path, const char *name, struct rule_data *data)
Definition rulesave.c:3131
rulesave.h
ruleset_purge_unused_entities
int ruleset_purge_unused_entities(void)
Definition ruleset.c:304
load_rulesets
bool load_rulesets(const char *restore, const char *alt, bool compat_mode, rs_conversion_logger logger, bool act, bool buffer_script, bool load_luadata)
Definition ruleset.c:9246
ruleset_purge_redundant_reqs
int ruleset_purge_redundant_reqs(void)
Definition ruleset.c:481
ruleset.h
main
int main(int argc, char **argv)
Definition ruleup.c:141
od_selected
static char * od_selected
Definition ruleup.c:47
rup_parse_cmdline
static void rup_parse_cmdline(int argc, char *argv[])
Definition ruleup.c:54
conv_log
static void conv_log(const char *msg)
Definition ruleup.c:133
dirty
static bool dirty
Definition ruleup.c:49
fatal_assertions
static int fatal_assertions
Definition ruleup.c:48
rs_selected
static char * rs_selected
Definition ruleup.c:46
init_connections
void init_connections(void)
Definition sernet.c:1355
sernet.h
settings_init
void settings_init(bool act)
Definition settings.c:4952
settings.h
str_to_int
bool str_to_int(const char *str, int *pint)
Definition shared.c:512
string_vector.h
civ_game::nationlist
char * nationlist
Definition game.h:274
civ_game::server
struct civ_game::@30::@34 server
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
civ_game::info
struct packet_game_info info
Definition game.h:89
civ_game::rulesetdir
char rulesetdir[MAX_LEN_NAME]
Definition game.h:236
civ_game::ruledit
struct civ_game::@30::@34::@39 ruledit
cmdhelp
Definition fc_cmdhelp.c:43
option
Definition options.c:454
packet_game_info::aifill
int aifill
Definition packets_gen.h:121
packet_ruleset_control::name
char name[MAX_LEN_NAME]
Definition packets_gen.h:1481
rule_data
Definition rulesave.h:21
rule_data::nationlist
char * nationlist
Definition rulesave.h:22
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:969
fc_strlcpy
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:787
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:161
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