Freeciv-3.3
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 "ruleload.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
46static char *rs_selected = NULL;
47static char *od_selected = NULL;
48static int fatal_assertions = -1;
49static bool dirty = TRUE;
50
51/**********************************************************************/
54static 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. */
86 cmdhelp_destroy(help);
87
89
91 } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, TRUE))) {
92 if (rs_selected != NULL) {
94 _("Multiple rulesets requested. Only one ruleset at time supported.\n"));
95 } else {
97 }
98 } else if ((option = get_option_malloc("--output", argv, &i, argc, TRUE))) {
99 if (od_selected != NULL) {
101 _("Multiple output directories given.\n"));
102 } else {
104 }
105#ifndef FREECIV_NDEBUG
106 } else if (is_option("--Fatal", argv[i])) {
107 if (i + 1 >= argc || '-' == argv[i + 1][0]) {
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"));
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]);
124 }
125
126 i++;
127 }
128}
129
130/**********************************************************************/
133static void conv_log(const char *msg)
134{
135 log_normal("%s", msg);
136}
137
138/**********************************************************************/
141int main(int argc, char **argv)
142{
143 enum log_level loglevel = LOG_NORMAL;
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. */
162
165
167
168 log_init(NULL, loglevel, NULL, NULL, fatal_assertions);
169
171
173
175 i_am_tool();
176
177 /* Set ruleset user requested to use */
178 if (rs_selected == NULL) {
180 }
182
183 /* Reset aifill to zero */
184 game.info.aifill = 0;
185
187 struct rule_data data;
188 char tgt_dir[2048];
189
191
192 if (od_selected != NULL) {
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. */
207 }
208 }
209
210 /* Clean up unused entities added during the ruleset upgrade. */
211 if (!dirty) {
213
214 if (purged > 0) {
215 log_normal("Purged %d unused entities after the ruleset upgrade",
216 purged);
217 }
218
220 if (purged > 0) {
221 log_normal("Purged %d redundant requirements after the ruleset"
222 " upgrade", purged);
223 }
224 }
225
227 log_normal("Saved %s", tgt_dir);
229 } else {
230 log_error(_("Can't load ruleset %s"), rs_selected);
231
232 /* Failed to upgrade the ruleset */
234 }
235
237 log_close();
240
241 return exit_status;
242}
void comments_free(void)
Definition comments.c:276
bool comments_load(void)
Definition comments.c:109
char * incite_cost
Definition comments.c:76
#define COMMENTS_FILE_NAME
Definition comments.h:20
void cmdhelp_destroy(struct cmdhelp *pcmdhelp)
Definition fc_cmdhelp.c:70
void cmdhelp_display(struct cmdhelp *pcmdhelp, bool sort, bool gui_options, bool report_bugs)
Definition fc_cmdhelp.c:104
struct cmdhelp * cmdhelp_new(const char *cmdname)
Definition fc_cmdhelp.c:57
void cmdhelp_add(struct cmdhelp *pcmdhelp, const char *shortarg, const char *longarg, const char *helpstr,...)
Definition fc_cmdhelp.c:86
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
char * get_option_malloc(const char *option_name, char **argv, int *i, int argc, bool gc)
Definition fc_cmdline.c:50
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
void libfreeciv_free(void)
static void i_am_tool(void)
static char void init_character_encodings(const char *my_internal_encoding, bool my_use_transliteration)
Definition fciconv.c:70
#define FC_DEFAULT_DATA_ENCODING
Definition fciconv.h:89
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define _(String)
Definition fcintl.h:67
#define R__(String)
Definition fcintl.h:75
struct civ_game game
Definition game.c:61
void game_init(bool keep_ruleset_value)
Definition game.c:445
#define GAME_DEFAULT_RULESETDIR
Definition game.h:681
void log_close(void)
Definition log.c:270
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
#define log_normal(message,...)
Definition log.h:108
log_level
Definition log.h:29
@ LOG_NORMAL
Definition log.h:33
#define log_error(message,...)
Definition log.h:104
void registry_module_init(void)
Definition registry.c:31
void registry_module_close(void)
Definition registry.c:41
int ruleset_purge_unused_entities(void)
Definition ruleload.c:315
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 ruleload.c:9188
int ruleset_purge_redundant_reqs(void)
Definition ruleload.c:492
bool save_ruleset(const char *path, const char *name, struct rule_data *data)
Definition rulesave.c:3397
int main(int argc, char **argv)
Definition ruleup.c:141
static char * od_selected
Definition ruleup.c:47
static void rup_parse_cmdline(int argc, char *argv[])
Definition ruleup.c:54
static void conv_log(const char *msg)
Definition ruleup.c:133
static bool dirty
Definition ruleup.c:49
static int fatal_assertions
Definition ruleup.c:48
static char * rs_selected
Definition ruleup.c:46
void init_connections(void)
Definition sernet.c:1362
void settings_init(bool act)
Definition settings.c:5212
bool str_to_int(const char *str, int *pint)
Definition shared.c:515
char * nationlist
Definition game.h:289
struct civ_game::@32::@36::@42 ruledit
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
char rulesetdir[MAX_LEN_NAME]
Definition game.h:246
struct civ_game::@32::@36 server
char name[MAX_LEN_NAME]
char * nationlist
Definition rulesave.h:22
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
size_t fc_strlcpy(char *dest, const char *src, size_t n)
Definition support.c:777
#define sz_strlcpy(dest, src)
Definition support.h:195
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
void fc_interface_init_tool(void)