webperimental: killstack decides stack protects.
[freeciv.git] / tools / ruleup.c
blob2d4b5cffda180151ec2e71f66b2c651add3a5360
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #ifdef FREECIV_MSWINDOWS
19 #include <windows.h>
20 #endif
22 /* utility */
23 #include "fc_cmdline.h"
24 #include "fciconv.h"
25 #include "registry.h"
26 #include "string_vector.h"
28 /* common */
29 #include "fc_cmdhelp.h"
30 #include "fc_interface.h"
32 /* server */
33 #include "ruleset.h"
34 #include "sernet.h"
35 #include "settings.h"
37 /* tools/shared */
38 #include "tools_fc_interface.h"
40 /* tools/ruleutil */
41 #include "comments.h"
42 #include "rulesave.h"
44 static char *rs_selected = NULL;
45 static char *od_selected = NULL;
47 /**************************************************************************
48 Parse freeciv-ruleup commandline parameters.
49 **************************************************************************/
50 static void rup_parse_cmdline(int argc, char *argv[])
52 int i = 1;
54 while (i < argc) {
55 char *option = NULL;
57 if (is_option("--help", argv[i])) {
58 struct cmdhelp *help = cmdhelp_new(argv[0]);
60 cmdhelp_add(help, "h", "help",
61 _("Print a summary of the options"));
62 cmdhelp_add(help, "r",
63 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
64 _("ruleset RULESET"),
65 _("Update RULESET"));
66 cmdhelp_add(help, "o",
67 /* TRANS: "output" is exactly what user must type, do not translate. */
68 _("output DIRECTORY"),
69 _("Create directory DIRECTORY for output"));
71 /* The function below prints a header and footer for the options.
72 * Furthermore, the options are sorted. */
73 cmdhelp_display(help, TRUE, FALSE, TRUE);
74 cmdhelp_destroy(help);
76 cmdline_option_values_free();
78 exit(EXIT_SUCCESS);
79 } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, TRUE))) {
80 if (rs_selected != NULL) {
81 fc_fprintf(stderr,
82 _("Multiple rulesets requested. Only one ruleset at time supported.\n"));
83 } else {
84 rs_selected = option;
86 } else if ((option = get_option_malloc("--output", argv, &i, argc, TRUE))) {
87 if (od_selected != NULL) {
88 fc_fprintf(stderr,
89 _("Multiple output directories given.\n"));
90 } else {
91 od_selected = option;
93 } else {
94 fc_fprintf(stderr, _("Unrecognized option: \"%s\"\n"), argv[i]);
95 cmdline_option_values_free();
96 exit(EXIT_FAILURE);
99 i++;
103 /**************************************************************************
104 Main entry point for freeciv-ruleup
105 **************************************************************************/
106 int main(int argc, char **argv)
108 enum log_level loglevel = LOG_NORMAL;
110 /* Load win32 post-crash debugger */
111 #ifdef FREECIV_MSWINDOWS
112 # ifndef FREECIV_NDEBUG
113 if (LoadLibrary("exchndl.dll") == NULL) {
114 # ifdef FREECIV_DEBUG
115 fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
116 # endif /* FREECIV_DEBUG */
118 # endif /* FREECIV_NDEBUG */
119 #endif /* FREECIV_MSWINDOWS */
121 init_nls();
123 registry_module_init();
124 init_character_encodings(FC_DEFAULT_DATA_ENCODING, FALSE);
126 log_init(NULL, loglevel, NULL, NULL, -1);
128 init_connections();
130 settings_init(FALSE);
132 game_init(FALSE);
133 i_am_tool();
135 /* Initialize the fc_interface functions needed to understand rules. */
136 fc_interface_init_tool();
138 rup_parse_cmdline(argc, argv);
140 /* Set ruleset user requested to use */
141 if (rs_selected == NULL) {
142 rs_selected = GAME_DEFAULT_RULESETDIR;
144 sz_strlcpy(game.server.rulesetdir, rs_selected);
146 /* Reset aifill to zero */
147 game.info.aifill = 0;
149 if (load_rulesets(NULL, TRUE, FALSE, TRUE)) {
150 struct rule_data data;
151 char tgt_dir[2048];
153 data.nationlist = game.server.ruledit.nationlist;
155 if (od_selected != NULL) {
156 fc_strlcpy(tgt_dir, od_selected, sizeof(tgt_dir));
157 } else {
158 fc_snprintf(tgt_dir, sizeof(tgt_dir), "%s.ruleup", rs_selected);
161 comments_load();
162 save_ruleset(tgt_dir, rs_selected, &data);
163 log_normal("Saved %s", tgt_dir);
164 comments_free();
165 } else {
166 log_error(_("Can't load ruleset %s"), rs_selected);
169 registry_module_close();
170 log_close();
171 free_libfreeciv();
172 free_nls();
173 cmdline_option_values_free();
175 return EXIT_SUCCESS;