Freeciv-3.3: /fast/freeciv/nightly/main/tools/manual/fc_manual.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
fc_manual.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Project
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 <stdarg.h>
19#include <stdlib.h>
20#include <string.h>
21
22#ifdef HAVE_SIGNAL_H
23#include <signal.h>
24#endif
25
26/* utility */
27#include "capability.h"
28#include "fc_cmdline.h"
29#include "fciconv.h"
30#include "fcintl.h"
31#include "log.h"
32#include "mem.h"
33#include "registry.h"
34
35/* common */
36#include "capstr.h"
37#include "connection.h"
38#include "fc_cmdhelp.h"
39#include "fc_interface.h"
40#include "version.h"
41
42/* client */
43#include "client_main.h"
44#include "climisc.h"
45#include "helpdata.h"
46#include "music.h"
47#include "tilespec.h"
48
49/* server */
50#include "ruleload.h"
51#include "settings.h"
52#include "sernet.h"
53#include "srv_main.h"
54#include "stdinhand.h"
55
56/* tools/shared */
57#include "tools_fc_interface.h"
58
59/* tools/manual */
60#include "fc_manual.h"
61
62
63 struct tag_types html_tags = {
64 /* file extension */
65 "html",
66
67 /* header */
68 "<html><head><link rel=\"stylesheet\" type=\"text/css\" "
69 "href=\"manual.css\"/><meta http-equiv=\"Content-Type\" "
70 "content=\"text/html; charset=UTF-8\"/></head><body>\n\n",
71
72 /* title begin */
73 "<h1>",
74
75 /* title end */
76 "</h1>",
77
78 /* section title begin */
79 "<h3 class='section'>",
80
81 /* section title end */
82 "</h3>",
83
84 /* image begin */
85 "<img src=\"",
86
87 /* image end */
88 ".png\">",
89
90 /* item begin */
91 "<div class='item' id='%s%d'>\n",
92
93 /* item end */
94 "</div>\n",
95
96 /* subitem begin */
97 "<pre class='%s'>",
98
99 /* subitem end */
100 "</pre>\n",
101
102 /* tail */
103 "</body></html>",
104
105 /* horizontal line */
106 "<hr/>"
107};
108
109 struct tag_types wiki_tags = {
110 /* file extension */
111 "mediawiki",
112
113 /* header */
114 " ",
115
116 /* title begin */
117 "=",
118
119 /* title end */
120 "=",
121
122 /* section title begin */
123 "===",
124
125 /* section title end */
126 "===",
127
128 /* image begin */
129 "[[Image:",
130
131 /* image end */
132 ".png]]",
133
134 /* item begin */
135 "----\n<!-- %s %d -->\n",
136
137 /* item end */
138 "\n",
139
140 /* subitem begin */
141 "<!-- %s -->\n",
142
143 /* subitem end */
144 "\n",
145
146 /* tail */
147 " ",
148
149 /* horizontal line */
150 "----"
151};
152
153
154void insert_client_build_info(char *outbuf, size_t outlen);
155
156/* Needed for "About Freeciv" help */
157 const char *client_string = "freeciv-manual";
158
159 static char *ruleset = NULL;
160
161/**********************************************************************/
164 char *html_special_chars(char *str, size_t *len)
165{
166 char *buf;
167
168 buf = fc_strrep_resize(str, len, "&", "&amp;");
169 buf = fc_strrep_resize(buf, len, "<", "&lt;");
170 buf = fc_strrep_resize(buf, len, ">", "&gt;");
171
172 return buf;
173}
174
175
176/*******************************************
177 Useless stubs for compiling client code.
178*******************************************/
179
180/**********************************************************************/
183 void popup_help_dialog_string(const char *item)
184{
185 /* Empty stub. */
186}
187
188/**********************************************************************/
191 void popdown_help_dialog(void)
192{
193 /* Empty stub. */
194}
195
196 struct tileset *tileset;
197
198/**********************************************************************/
201 const char *tileset_name_get(struct tileset *t)
202{
203 return NULL;
204}
205
206/**********************************************************************/
209 const char *tileset_version(struct tileset *t)
210{
211 return NULL;
212}
213
214/**********************************************************************/
217 const char *tileset_summary(struct tileset *t)
218{
219 return NULL;
220}
221
222/**********************************************************************/
225 const char *tileset_description(struct tileset *t)
226{
227 return NULL;
228}
229
230/**********************************************************************/
233 const char *current_musicset_name(void)
234{
235 return NULL;
236}
237
238/**********************************************************************/
241 const char *current_musicset_version(void)
242{
243 return NULL;
244}
245
246/**********************************************************************/
249 const char *current_musicset_summary(void)
250{
251 return NULL;
252}
253
254/**********************************************************************/
257 const char *current_musicset_description(void)
258{
259 return NULL;
260}
261
262/**********************************************************************/
265 enum client_states client_state(void)
266{
267 return C_S_INITIAL;
268}
269
270/**********************************************************************/
273 bool client_nation_is_in_current_set(const struct nation_type *pnation)
274{
275 /* Currently, there is no way to select a nation set for freeciv-manual.
276 * Then, let's assume we want to print help for all nations. */
277 return TRUE;
278}
279
280/**********************************************************************/
287 FILE *manual_start(struct tag_types *tag_info, int manual_number)
288{
289 char filename[40];
290 FILE *doc;
291
292 fc_snprintf(filename, sizeof(filename), "%s%d.%s",
293 game.server.rulesetdir, manual_number + 1, tag_info->file_ext);
294
295 if (!is_reg_file_for_access(filename, TRUE)
296 || !(doc = fc_fopen(filename, "w"))) {
297 log_error(_("Could not write manual file %s."), filename);
298
299 return NULL;
300 }
301
302 fprintf(doc, "%s", tag_info->header);
303 fprintf(doc, "<!-- Generated by freeciv-manual version %s -->\n\n",
304 freeciv_datafile_version());
305
306 return doc;
307}
308
309/**********************************************************************/
316 void manual_finalize(struct tag_types *tag_info, FILE *doc,
317 enum manuals manual)
318{
319 fprintf(doc, "%s", tag_info->tail);
320 fclose(doc);
321 log_normal(_("%s (%d) manual successfully written."),
322 _(manuals_name(manual)), manual + 1);
323}
324
325/**********************************************************************/
328 static bool manual_command(struct tag_types *tag_info)
329{
330 /* Reset aifill to zero */
331 game.info.aifill = 0;
332
333 if (!load_rulesets(NULL, NULL, FALSE, NULL, FALSE, FALSE, FALSE)) {
334 /* Failed to load correct ruleset */
335 return FALSE;
336 }
337
338 if (!manual_settings(tag_info)
339 || !manual_commands(tag_info)
340 || !manual_terrain(tag_info)
341 || !manual_extras(tag_info)
342 || !manual_buildings(tag_info)
343 || !manual_governments(tag_info)
344 || !manual_units(tag_info)
345 || !manual_techs(tag_info)) {
346 return FALSE;
347 }
348
349 return TRUE;
350}
351
352/**********************************************************************/
355 int main(int argc, char **argv)
356{
357 int inx;
358 bool showhelp = FALSE;
359 bool showvers = FALSE;
360 char *option = NULL;
361 int retval = EXIT_SUCCESS;
362 struct tag_types *tag_info = &html_tags;
363
364 /* Initialize the fc_interface functions needed to generate the help
365 * text.
366 * fc_interface_init_tool() includes low level support like
367 * guaranteeing that fc_vsnprintf() will work after it,
368 * so this need to be early. */
369 fc_interface_init_tool();
370
371 registry_module_init();
372 init_character_encodings(FC_DEFAULT_DATA_ENCODING, FALSE);
373
374 /* Set the default log level. */
375 srvarg.loglevel = LOG_NORMAL;
376
377 /* parse command-line arguments... */
378 inx = 1;
379 while (inx < argc) {
380 if ((option = get_option_malloc("--ruleset", argv, &inx, argc, TRUE))) {
381 if (ruleset != NULL) {
382 fc_fprintf(stderr, _("Multiple rulesets requested. Only one "
383 "ruleset at a time is supported.\n"));
384 } else {
385 ruleset = option;
386 }
387 } else if (is_option("--help", argv[inx])) {
388 showhelp = TRUE;
389 break;
390 } else if (is_option("--version", argv[inx])) {
391 showvers = TRUE;
392 } else if ((option = get_option_malloc("--log", argv, &inx, argc, TRUE))) {
393 srvarg.log_filename = option;
394 } else if (is_option("--wiki", argv[inx])) {
395 tag_info = &wiki_tags;
396#ifndef FREECIV_NDEBUG
397 } else if (is_option("--Fatal", argv[inx])) {
398 if (inx + 1 >= argc || '-' == argv[inx + 1][0]) {
399 srvarg.fatal_assertions = SIGABRT;
400 } else if (str_to_int(argv[inx + 1], &srvarg.fatal_assertions)) {
401 inx++;
402 } else {
403 fc_fprintf(stderr, _("Invalid signal number \"%s\".\n"),
404 argv[inx + 1]);
405 inx++;
406 showhelp = TRUE;
407 }
408#endif /* FREECIV_NDEBUG */
409 } else if ((option = get_option_malloc("--debug", argv, &inx, argc, FALSE))) {
410 if (!log_parse_level_str(option, &srvarg.loglevel)) {
411 showhelp = TRUE;
412 break;
413 }
414 free(option);
415 } else {
416 fc_fprintf(stderr, _("Unrecognized option: \"%s\"\n"), argv[inx]);
417 exit(EXIT_FAILURE);
418 }
419 inx++;
420 }
421
422 init_our_capability();
423
424 /* must be before con_log_init() */
425 init_connections();
426 con_log_init(srvarg.log_filename, srvarg.loglevel,
427 srvarg.fatal_assertions);
428 /* logging available after this point */
429
430 /* Get common code to treat us as a tool. */
431 i_am_tool();
432
433 /* Initialize game with default values */
434 game_init(FALSE);
435
436 /* Set ruleset user requested in to use */
437 if (ruleset != NULL) {
438 sz_strlcpy(game.server.rulesetdir, ruleset);
439 }
440
441 settings_init(FALSE);
442
443 if (showvers && !showhelp) {
444 fc_fprintf(stderr, "%s \n", freeciv_name_version());
445 exit(EXIT_SUCCESS);
446 } else if (showhelp) {
447 struct cmdhelp *help = cmdhelp_new(argv[0]);
448
449#ifdef FREECIV_DEBUG
450 cmdhelp_add(help, "d",
451 /* TRANS: "debug" is exactly what user must type, do not translate. */
452 _("debug LEVEL"),
453 _("Set debug log level (one of f,e,w,n,v,d, or "
454 "d:file1,min,max:...)"));
455#else /* FREECIV_DEBUG */
456 cmdhelp_add(help, "d",
457 /* TRANS: "debug" is exactly what user must type, do not translate. */
458 _("debug LEVEL"),
459 _("Set debug log level (one of f,e,w,n,v)"));
460#endif /* FREECIV_DEBUG */
461#ifndef FREECIV_NDEBUG
462 cmdhelp_add(help, "F",
463 /* TRANS: "Fatal" is exactly what user must type, do not translate. */
464 _("Fatal [SIGNAL]"),
465 _("Raise a signal on failed assertion"));
466#endif /* FREECIV_NDEBUG */
467 cmdhelp_add(help, "h", "help",
468 _("Print a summary of the options"));
469 cmdhelp_add(help, "l",
470 /* TRANS: "log" is exactly what user must type, do not translate. */
471 _("log FILE"),
472 _("Use FILE as logfile"));
473 cmdhelp_add(help, "r",
474 /* TRANS: "ruleset" is exactly what user must type, do not translate. */
475 _("ruleset RULESET"),
476 _("Make manual for RULESET"));
477 cmdhelp_add(help, "v", "version",
478 _("Print the version number"));
479 cmdhelp_add(help, "w", "wiki",
480 _("Write manual in wiki format"));
481
482 /* The function below prints a header and footer for the options.
483 * Furthermore, the options are sorted. */
484 cmdhelp_display(help, TRUE, FALSE, TRUE);
485 cmdhelp_destroy(help);
486
487 exit(EXIT_SUCCESS);
488 }
489
490 if (!manual_command(tag_info)) {
491 retval = EXIT_FAILURE;
492 }
493
494 con_log_close();
495 registry_module_close();
496 libfreeciv_free();
497 cmdline_option_values_free();
498
499 return retval;
500}
501
502/**********************************************************************/
505 void insert_client_build_info(char *outbuf, size_t outlen)
506{
507 /* Nothing here */
508}
str
#define str
Definition astring.c:76
capability.h
init_our_capability
void init_our_capability(void)
Definition capstr.c:87
capstr.h
client_main.h
client_states
client_states
Definition client_main.h:43
C_S_INITIAL
@ C_S_INITIAL
Definition client_main.h:44
climisc.h
incite_cost
char * incite_cost
Definition comments.c:74
connection.h
con_log_init
void con_log_init(const char *log_filename, enum log_level level, int fatal_assertions)
Definition console.c:150
con_log_close
void con_log_close(void)
Definition console.c:167
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:102
fc_interface.h
i_am_tool
static void i_am_tool(void)
Definition fc_interface.h:80
client_string
const char * client_string
Definition fc_manual.c:157
tileset
struct tileset * tileset
Definition fc_manual.c:196
manual_finalize
void manual_finalize(struct tag_types *tag_info, FILE *doc, enum manuals manual)
Definition fc_manual.c:316
main
int main(int argc, char **argv)
Definition fc_manual.c:355
current_musicset_version
const char * current_musicset_version(void)
Definition fc_manual.c:241
current_musicset_name
const char * current_musicset_name(void)
Definition fc_manual.c:233
current_musicset_summary
const char * current_musicset_summary(void)
Definition fc_manual.c:249
tileset_description
const char * tileset_description(struct tileset *t)
Definition fc_manual.c:225
current_musicset_description
const char * current_musicset_description(void)
Definition fc_manual.c:257
client_nation_is_in_current_set
bool client_nation_is_in_current_set(const struct nation_type *pnation)
Definition fc_manual.c:273
popdown_help_dialog
void popdown_help_dialog(void)
Definition fc_manual.c:191
tileset_summary
const char * tileset_summary(struct tileset *t)
Definition fc_manual.c:217
ruleset
static char * ruleset
Definition fc_manual.c:159
popup_help_dialog_string
void popup_help_dialog_string(const char *item)
Definition fc_manual.c:183
html_special_chars
char * html_special_chars(char *str, size_t *len)
Definition fc_manual.c:164
manual_start
FILE * manual_start(struct tag_types *tag_info, int manual_number)
Definition fc_manual.c:287
insert_client_build_info
void insert_client_build_info(char *outbuf, size_t outlen)
Definition fc_manual.c:505
client_state
enum client_states client_state(void)
Definition fc_manual.c:265
manual_command
static bool manual_command(struct tag_types *tag_info)
Definition fc_manual.c:328
tileset_name_get
const char * tileset_name_get(struct tileset *t)
Definition fc_manual.c:201
tileset_version
const char * tileset_version(struct tileset *t)
Definition fc_manual.c:209
html_tags
struct tag_types html_tags
Definition fc_manual.c:63
wiki_tags
struct tag_types wiki_tags
Definition fc_manual.c:109
fc_manual.h
manual_terrain
bool manual_terrain(struct tag_types *tag_info)
Definition manual_terrain.c:33
manual_units
bool manual_units(struct tag_types *tag_info)
Definition manual_units.c:37
manual_buildings
bool manual_buildings(struct tag_types *tag_info)
Definition manual_buildings.c:136
manual_extras
bool manual_extras(struct tag_types *tag_info)
Definition manual_extras.c:37
manual_settings
bool manual_settings(struct tag_types *tag_info)
Definition manual_settings.c:33
manual_governments
bool manual_governments(struct tag_types *tag_info)
Definition manual_governments.c:37
manual_commands
bool manual_commands(struct tag_types *tag_info)
Definition manual_commands.c:33
manual_techs
bool manual_techs(struct tag_types *tag_info)
Definition manual_techs.c:37
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__
fcintl.h
_
#define _(String)
Definition fcintl.h:67
game
struct civ_game game
Definition game.c:62
game_init
void game_init(bool keep_ruleset_value)
Definition game.c:444
helpdata.h
log_parse_level_str
bool log_parse_level_str(const char *level_str, enum log_level *ret_level)
Definition log.c:86
log.h
log_normal
#define log_normal(message,...)
Definition log.h:107
LOG_NORMAL
@ LOG_NORMAL
Definition log.h:32
log_error
#define log_error(message,...)
Definition log.h:103
mem.h
music.h
len
int len
Definition packhand.c:127
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
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 ruleload.c:9185
ruleload.h
init_connections
void init_connections(void)
Definition sernet.c:1362
sernet.h
settings_init
void settings_init(bool act)
Definition settings.c:5205
settings.h
str_to_int
bool str_to_int(const char *str, int *pint)
Definition shared.c:517
srvarg
struct server_arguments srvarg
Definition srv_main.c:180
srv_main.h
stdinhand.h
civ_game::info
struct packet_game_info info
Definition game.h:89
civ_game::rulesetdir
char rulesetdir[MAX_LEN_NAME]
Definition game.h:239
civ_game::server
struct civ_game::@31::@35 server
cmdhelp
Definition fc_cmdhelp.c:43
item
Definition climisc.h:80
nation_type
Definition nation.h:98
option
Definition options.c:514
packet_game_info::aifill
int aifill
Definition packets_gen.h:122
server_arguments::loglevel
enum log_level loglevel
Definition srv_main.h:40
server_arguments::log_filename
char * log_filename
Definition srv_main.h:42
server_arguments::fatal_assertions
int fatal_assertions
Definition srv_main.h:61
tag_types
Definition fc_manual.h:28
tileset
Definition tilespec.c:481
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
fc_fopen
FILE * fc_fopen(const char *filename, const char *opentype)
Definition support.c:505
is_reg_file_for_access
bool is_reg_file_for_access(const char *name, bool write_access)
Definition support.c:1120
fc_strrep_resize
char * fc_strrep_resize(char *str, size_t *len, const char *search, const char *replace)
Definition support.c:681
sz_strlcpy
#define sz_strlcpy(dest, src)
Definition support.h:189
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
tilespec.h
fc_interface_init_tool
void fc_interface_init_tool(void)
Definition tools_fc_interface.c:60
tools_fc_interface.h
freeciv_name_version
const char * freeciv_name_version(void)
Definition version.c:35
freeciv_datafile_version
const char * freeciv_datafile_version(void)
Definition version.c:186
version.h