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 #ifdef WIN32_NATIVE
24 #include <windows.h>
25 #endif
26 
27 /* utility */
28 #include "fc_cmdline.h"
29 #include "fciconv.h"
30 #include "fcintl.h"
31 #include "log.h"
32 #include "registry.h"
33 
34 /* common */
35 #include "fc_cmdhelp.h"
36 #include "fc_interface.h"
37 #include "version.h"
38 
39 /* server */
40 #include "sernet.h"
41 #include "settings.h"
42 
43 /* ruledit */
44 #include "ruledit_qt.h"
45 
46 #include "ruledit.h"
47 
48 static int re_parse_cmdline(int argc, char *argv[]);
49 
50 struct ruledit_arguments reargs;
51 
52 /**************************************************************************
53   Main entry point for freeciv-ruledit
54 **************************************************************************/
main(int argc,char ** argv)55 int main(int argc, char **argv)
56 {
57   enum log_level loglevel = LOG_NORMAL;
58   int ui_options;
59 
60   /* Load win32 post-crash debugger */
61 #ifdef WIN32_NATIVE
62 # ifndef FREECIV_NDEBUG
63   if (LoadLibrary("exchndl.dll") == NULL) {
64 #  ifdef FREECIV_DEBUG
65     fprintf(stderr, "exchndl.dll could not be loaded, no crash debugger\n");
66 #  endif /* FREECIV_DEBUG */
67   }
68 # endif /* FREECIV_NDEBUG */
69 #endif /* WIN32_NATIVE */
70 
71   init_nls();
72 
73 #ifdef ENABLE_NLS
74   (void) bindtextdomain("freeciv-ruledit", get_locale_dir());
75 #endif
76 
77   init_character_encodings(FC_DEFAULT_DATA_ENCODING, FALSE);
78 #ifdef ENABLE_NLS
79   bind_textdomain_codeset("freeciv-ruledit", get_internal_encoding());
80 #endif
81 
82   registry_module_init();
83 
84   log_init(NULL, loglevel, NULL, NULL, -1);
85 
86   /* Initialize command line arguments. */
87   reargs.ruleset = NULL;
88 
89   ui_options = re_parse_cmdline(argc, argv);
90 
91   if (ui_options != -1) {
92     init_connections();
93 
94     settings_init(FALSE);
95 
96     /* Reset aifill to zero */
97     game.info.aifill = 0;
98 
99     game_init();
100     i_am_server();
101 
102     ruledit_qt_run(ui_options, argv);
103   }
104 
105   registry_module_close();
106   log_close();
107   free_libfreeciv();
108   free_nls();
109 
110   /* Clean up command line arguments. */
111   cmdline_option_values_free();
112 
113   return EXIT_SUCCESS;
114 }
115 
116 /**************************************************************************
117   Parse freeciv-ruledit commandline.
118 **************************************************************************/
re_parse_cmdline(int argc,char * argv[])119 static int re_parse_cmdline(int argc, char *argv[])
120 {
121   int i = 1;
122   bool ui_separator = FALSE;
123   int ui_options = 0;
124 
125   while (i < argc) {
126     char *option;
127 
128     if (ui_separator) {
129       argv[1 + ui_options] = argv[i];
130       ui_options++;
131     } else if (is_option("--help", argv[i])) {
132       struct cmdhelp *help = cmdhelp_new(argv[0]);
133 
134       cmdhelp_add(help, "h", "help",
135                   R__("Print a summary of the options"));
136       cmdhelp_add(help, "v", "version",
137                   R__("Print the version number"));
138       cmdhelp_add(help, "r",
139                   /* TRANS: argument (don't translate) VALUE (translate) */
140                   R__("ruleset RULESET"),
141                   R__("Ruleset to use as the starting point."));
142       /* The function below prints a header and footer for the options.
143        * Furthermore, the options are sorted. */
144       cmdhelp_display(help, TRUE, TRUE, TRUE);
145       cmdhelp_destroy(help);
146 
147       exit(EXIT_SUCCESS);
148     } else if (is_option("--version", argv[i])) {
149       fc_fprintf(stderr, "%s \n", freeciv_name_version());
150 
151       exit(EXIT_SUCCESS);
152     } else if ((option = get_option_malloc("--ruleset", argv, &i, argc, true))) {
153       if (reargs.ruleset) {
154         fc_fprintf(stderr, R__("Can only edit one ruleset at a time.\n"));
155       } else {
156         reargs.ruleset = option;
157       }
158     } else if (is_option("--", argv[i])) {
159       ui_separator = TRUE;
160     } else {
161       fc_fprintf(stderr, R__("Unrecognized option: \"%s\"\n"), argv[i]);
162       exit(EXIT_FAILURE);
163     }
164 
165     i++;
166   }
167 
168   return ui_options;
169 }
170