Freeciv-3.4
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/* utility */
21#include "executable.h"
22#include "fc_cmdline.h"
23#include "fciconv.h"
24#include "registry.h"
25#include "string_vector.h"
26
27/* common */
28#include "fc_cmdhelp.h"
29#include "fc_interface.h"
30
31/* server */
32#include "ruleload.h"
33#include "sernet.h"
34#include "settings.h"
35
36/* tools/shared */
37#include "tools_fc_interface.h"
38
39/* tools/ruleutil */
40#include "comments.h"
41#include "rulesave.h"
42
43static char *rs_selected = NULL;
44static char *od_selected = NULL;
45static int fatal_assertions = -1;
46static bool dirty = TRUE;
47
48/**********************************************************************/
51static void rup_parse_cmdline(int argc, char *argv[])
52{
53 int i = 1;
54
55 while (i < argc) {
56 char *option = NULL;
57
58 if (is_option("--help", argv[i])) {
59 struct cmdhelp *help = cmdhelp_new(argv[0]);
60
61 cmdhelp_add(help, "h", "help",
62 _("Print a summary of the options"));
63#ifndef FREECIV_NDEBUG
64 cmdhelp_add(help, "F",
65 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
66 _("Fatal [SIGNAL]"),
67 _("Raise a signal on failed assertion or broken data"));
68#endif /* FREECIV_NDEBUG */
69 cmdhelp_add(help, "r",
70 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
71 _("ruleset RULESET"),
72 _("Update RULESET"));
73 cmdhelp_add(help, "o",
74 /* TRANS: "output" is exactly what user must type, do not translate. */
75 _("output DIRECTORY"),
76 _("Create directory DIRECTORY for output"));
77 cmdhelp_add(help, "c", "clean",
78 _("Clean up the ruleset before saving it."));
79
80 /* The function below prints a header and footer for the options.
81 * Furthermore, the options are sorted. */
83 cmdhelp_destroy(help);
84
86
88 } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, TRUE))) {
89 if (rs_selected != NULL) {
91 _("Multiple rulesets requested. Only one ruleset at time supported.\n"));
92 } else {
94 }
95 } else if ((option = get_option_malloc("--output", argv, &i, argc, TRUE))) {
96 if (od_selected != NULL) {
98 _("Multiple output directories given.\n"));
99 } else {
101 }
102#ifndef FREECIV_NDEBUG
103 } else if (is_option("--Fatal", argv[i])) {
104 if (i + 1 >= argc || '-' == argv[i + 1][0]) {
106 } else if (str_to_int(argv[i + 1], &fatal_assertions)) {
107 i++;
108 } else {
109 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
110 argv[i + 1]);
111 fc_fprintf(stderr, _("Try using --help.\n"));
113 }
114#endif /* FREECIV_NDEBUG */
115 } else if (is_option("--clean", argv[i])) {
116 dirty = FALSE;
117 } else {
118 fc_fprintf(stderr, _("Unrecognized option: \"%s\"\n"), argv[i]);
121 }
122
123 i++;
124 }
125}
126
127/**********************************************************************/
130static void conv_log(const char *msg)
131{
132 log_normal("%s", msg);
133}
134
135/**********************************************************************/
138int main(int argc, char **argv)
139{
140 enum log_level loglevel = LOG_NORMAL;
142
144
145 /* Initialize the fc_interface functions needed to understand rules.
146 * fc_interface_init_tool() includes low level support like
147 * guaranteeing that fc_vsnprintf() will work after it,
148 * so this need to be early. */
150
153
155
156 log_init(NULL, loglevel, NULL, NULL, fatal_assertions);
157
159
161
163 i_am_tool();
164
165 /* Set ruleset user requested to use */
166 if (rs_selected == NULL) {
168 }
170
171 /* Reset aifill to zero */
172 game.info.aifill = 0;
173
175 struct rule_data data;
176 char tgt_dir[2048];
177
179
180 if (od_selected != NULL) {
182 } else {
183 fc_snprintf(tgt_dir, sizeof(tgt_dir), "%s.ruleup", rs_selected);
184 }
185
186 if (!comments_load()) {
187 /* TRANS: 'Failed to load comments-x.y.txt' where x.y is
188 * freeciv version */
189 log_error(R__("Failed to load %s."), COMMENTS_FILE_NAME);
190
191 /* Reuse fatal_assertions for failed comment loading. */
192 if (0 <= fatal_assertions) {
193 /* Emit a signal. */
195 }
196 }
197
198 /* Clean up unused entities added during the ruleset upgrade. */
199 if (!dirty) {
201
202 if (purged > 0) {
203 log_normal("Purged %d unused entities after the ruleset upgrade",
204 purged);
205 }
206
208 if (purged > 0) {
209 log_normal("Purged %d redundant requirements after the ruleset"
210 " upgrade", purged);
211 }
212 }
213
215 log_normal("Saved %s", tgt_dir);
217 } else {
218 log_error(_("Can't load ruleset %s"), rs_selected);
219
220 /* Failed to upgrade the ruleset */
222 }
223
225 log_close();
228
229 return exit_status;
230}
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 executable_init(void)
Definition executable.c:34
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:62
void game_init(bool keep_ruleset_value)
Definition game.c:446
#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:323
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:9413
int ruleset_purge_redundant_reqs(void)
Definition ruleload.c:500
bool save_ruleset(const char *path, const char *name, struct rule_data *data)
Definition rulesave.c:3469
int main(int argc, char **argv)
Definition ruleup.c:138
static char * od_selected
Definition ruleup.c:44
static void rup_parse_cmdline(int argc, char *argv[])
Definition ruleup.c:51
static void conv_log(const char *msg)
Definition ruleup.c:130
static bool dirty
Definition ruleup.c:46
static int fatal_assertions
Definition ruleup.c:45
static char * rs_selected
Definition ruleup.c:43
void init_connections(void)
Definition sernet.c:1362
void settings_init(bool act)
Definition settings.c:5257
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)