2 * Copyright (c) 1997 Ben Harrison, and others
4 * This software may be copied and distributed for educational, research,
5 * and not for profit purposes provided that this copyright and statement
6 * are included in all such copies.
9 #include "core/asking-player.h"
10 #include "core/game-play.h"
11 #include "core/scores.h"
12 #include "game-option/runtime-arguments.h"
13 #include "io/files-util.h"
15 #include "io/record-play-movie.h"
16 #include "io/signal-handlers.h"
17 #include "io/uid-checker.h"
18 #include "main/angband-initializer.h"
19 #include "player/process-name.h"
20 #include "system/angband-version.h"
21 #include "system/angband.h"
22 #include "system/player-type-definition.h"
23 #include "system/system-variables.h"
24 #include "term/gameterm.h"
25 #include "term/term-color-types.h"
26 #include "util/angband-files.h"
27 #include "util/string-processor.h"
28 #include "view/display-scores.h"
29 #include "wizard/wizard-spoiler.h"
32 * Available graphic modes
34 #define GRAPHICS_NONE 0
35 #define GRAPHICS_ORIGINAL 1
36 #define GRAPHICS_ADAM_BOLT 2
37 #define GRAPHICS_HENGBAND 3
40 * Some machines have a "main()" function in their "main-xxx.c" file,
41 * all the others use this file for their "main()" function.
46 * A hook for "quit()".
48 * Close down, then fall back into "quit()".
50 static void quit_hook(concptr s)
58 for (j = 8 - 1; j >= 0; j--) {
64 term_nuke(angband_term[j]);
69 * Set the stack size and overlay buffer (see main-286.c")
71 #ifdef PRIVATE_USER_PATH
74 * Create an ".angband/" directory in the users home directory.
76 * ToDo: Add error handling.
77 * ToDo: Only create the directories when actually writing files.
79 static void create_user_dir(void)
82 char subdirpath[1024];
84 /* Get an absolute path from the filename */
85 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
87 /* Create the ~/.angband/ directory */
90 /* Build the path to the variant-specific sub-directory */
91 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
93 /* Create the directory */
94 mkdir(subdirpath, 0700);
97 #endif /* PRIVATE_USER_PATH */
100 * Initialize and verify the file paths, and the score file.
102 * Use the ANGBAND_PATH environment var if possible, else use
103 * DEFAULT_(LIB|VAR)_PATH, and in either case, branch off
106 * First, we'll look for the ANGBAND_PATH environment variable,
107 * and then look for the files in there. If that doesn't work,
108 * we'll try the DEFAULT_(LIB|VAR)_PATH constants. So be sure
109 * that one of these two things works...
111 * We must ensure that the path ends with "PATH_SEP" if needed,
112 * since the "init_file_paths()" function will simply append the
113 * relevant "sub-directory names" to the given path.
115 * Make sure that the path doesn't overflow the buffer. We have
116 * to leave enough space for the path separator, directory, and
119 static void init_stuff(void)
121 char libpath[1024], varpath[1024];
125 /* Get the environment variable */
126 tail = getenv("ANGBAND_PATH");
128 /* Use the angband_path, or a default */
129 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
130 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
132 /* Make sure they're terminated */
136 /* Hack -- Add a path separator (only if needed) */
137 if (!suffix(libpath, PATH_SEP))
138 strcat(libpath, PATH_SEP);
139 if (!suffix(varpath, PATH_SEP))
140 strcat(varpath, PATH_SEP);
143 init_file_paths(libpath, varpath);
147 * Handle a "-d<what>=<path>" option
149 * The "<what>" can be any string starting with the same letter as the
150 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
152 * The "<path>" can be any legal path for the given system, and should
153 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
155 static void change_path(concptr info)
159 /* Find equal sign */
160 s = angband_strchr(info, '=');
162 /* Verify equal sign */
164 quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
167 switch (tolower(info[0])) {
169 string_free(ANGBAND_DIR_APEX);
170 ANGBAND_DIR_APEX = string_make(s + 1);
175 string_free(ANGBAND_DIR_FILE);
176 ANGBAND_DIR_FILE = string_make(s + 1);
181 string_free(ANGBAND_DIR_HELP);
182 ANGBAND_DIR_HELP = string_make(s + 1);
187 string_free(ANGBAND_DIR_INFO);
188 ANGBAND_DIR_INFO = string_make(s + 1);
193 string_free(ANGBAND_DIR_USER);
194 ANGBAND_DIR_USER = string_make(s + 1);
199 string_free(ANGBAND_DIR_XTRA);
200 ANGBAND_DIR_XTRA = string_make(s + 1);
205 string_free(ANGBAND_DIR_BONE);
206 ANGBAND_DIR_BONE = string_make(s + 1);
211 string_free(ANGBAND_DIR_DATA);
212 ANGBAND_DIR_DATA = string_make(s + 1);
217 string_free(ANGBAND_DIR_EDIT);
218 ANGBAND_DIR_EDIT = string_make(s + 1);
223 string_free(ANGBAND_DIR_SAVE);
224 ANGBAND_DIR_SAVE = string_make(s + 1);
229 string_free(ANGBAND_DIR_SCRIPT);
230 ANGBAND_DIR_SCRIPT = string_make(s + 1);
235 quit_fmt("Bad semantics in '-d%s'", info);
240 static void display_usage(const char *program)
242 /* Dump usage information */
243 printf("Usage: %s [options] [-- subopts]\n", program);
244 puts(" -n Start a new character");
245 puts(" -f Request fiddle mode");
246 puts(" -w Request wizard mode");
247 puts(" -b Request BGM mode");
248 puts(" -v Request sound mode");
249 puts(" -g Request graphics mode");
250 puts(" -o Request original keyset");
251 puts(" -r Request rogue-like keyset");
252 puts(" -M Request monochrome mode");
253 puts(" -s<num> Show <num> high scores");
254 puts(" -u<who> Use your <who> savefile");
255 puts(" -m<sys> Force 'main-<sys>.c' usage");
256 puts(" -d<def> Define a 'lib' dir sub-path");
257 puts(" --output-spoilers");
258 puts(" Output auto generated spoilers and exit");
262 puts(" -mx11 To use X11");
263 puts(" -- Sub options");
264 puts(" -- -d Set display name");
265 puts(" -- -o Request old 8x8 tile graphics");
266 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
267 puts(" -- -b Request Bigtile graphics mode");
268 puts(" -- -s Turn off smoothscaling graphics");
269 puts(" -- -n# Number of terms to use");
274 puts(" -mgcu To use GCU (GNU Curses)");
275 puts(" -- Sub options");
276 puts(" -- -o old subwindow layout (no bigscreen)");
280 puts(" -mcap To use CAP (\"Termcap\" calls)");
283 /* Actually abort the process */
288 * @brief 2文字以上のコマンドライン引数 (オプション)を実行する
289 * @param opt コマンドライン引数
290 * @return Usageを表示する必要があるか否か
291 * @details v3.0.0 Alpha21時点では、スポイラー出力モードの判定及び実行を行う
293 static bool parse_long_opt(const char *opt)
295 if (strcmp(opt + 2, "output-spoilers") != 0) {
300 init_angband(p_ptr, true);
301 switch (output_all_spoilers()) {
302 case SpoilerOutputResultType::SPOILER_OUTPUT_SUCCESS:
303 puts("Successfully created a spoiler file.");
306 case SpoilerOutputResultType::SPOILER_OUTPUT_FAIL_FOPEN:
307 quit("Cannot create spoiler file.");
309 case SpoilerOutputResultType::SPOILER_OUTPUT_FAIL_FCLOSE:
310 quit("Cannot close spoiler file.");
320 * Simple "main" function for multiple platforms.
322 * Note the special "--" option which terminates the processing of
323 * standard options. All non-standard options (if any) are passed
324 * directly to the "init_xxx()" function.
326 int main(int argc, char *argv[])
331 bool new_game = false;
333 concptr mstr = nullptr;
336 /* Save the "program name" XXX XXX XXX */
341 /* Default permissions on files */
346 /* Get the file paths */
351 /* Get the user id (?) */
352 p_ptr->player_uid = getuid();
355 /* Mega-Hack -- Factor group id */
356 p_ptr->player_uid += (getgid() * 1000);
361 #ifdef _POSIX_SAVED_IDS
363 /* Save some info for later */
364 p_ptr->player_euid = geteuid();
365 p_ptr->player_egid = getegid();
373 /* Drop permissions */
378 /* Acquire the "user name" as a default player name */
379 user_name(p_ptr->name, p_ptr->player_uid);
381 #ifdef PRIVATE_USER_PATH
383 /* Create a directory for the users files. */
386 #endif /* PRIVATE_USER_PATH */
390 /* Process the command line arguments */
391 bool browsing_movie = false;
392 for (i = 1; args && (i < argc); i++) {
393 /* Require proper options */
394 if (argv[i][0] != '-') {
395 display_usage(argv[0]);
400 bool is_usage_needed = false;
401 switch (argv[i][1]) {
419 /* HACK - Graphics mode switches on the original tiles */
420 arg_graphics = GRAPHICS_ORIGINAL;
425 arg_force_roguelike = true;
430 arg_force_original = true;
435 show_score = atoi(&argv[i][2]);
443 is_usage_needed = true;
447 strcpy(p_ptr->name, &argv[i][2]);
452 is_usage_needed = true;
460 arg_monochrome = true;
465 change_path(&argv[i][2]);
470 is_usage_needed = true;
474 prepare_browse_movie_with_path_build(&argv[i][2]);
475 browsing_movie = true;
479 if (argv[i][2] == '\0') {
485 is_usage_needed = parse_long_opt(argv[i]);
490 is_usage_needed = true;
495 if (!is_usage_needed)
498 display_usage(argv[0]);
501 /* Hack -- Forget standard args */
507 /* Process the player name */
508 process_player_name(p_ptr, true);
510 /* Install "quit" hook */
511 quit_aux = quit_hook;
514 /* Attempt to use the "main-xaw.c" support */
515 if (!done && (!mstr || (streq(mstr, "xaw")))) {
516 extern errr init_xaw(int, char **);
517 if (0 == init_xaw(argc, argv)) {
525 /* Attempt to use the "main-x11.c" support */
526 if (!done && (!mstr || (streq(mstr, "x11")))) {
527 extern errr init_x11(int, char **);
528 if (0 == init_x11(argc, argv)) {
536 /* Attempt to use the "main-gcu.c" support */
537 if (!done && (!mstr || (streq(mstr, "gcu")))) {
538 extern errr init_gcu(int, char **);
539 if (0 == init_gcu(argc, argv)) {
547 /* Attempt to use the "main-cap.c" support */
548 if (!done && (!mstr || (streq(mstr, "cap")))) {
549 extern errr init_cap(int, char **);
550 if (0 == init_cap(argc, argv)) {
557 /* Make sure we have a display! */
559 quit("Unable to prepare any 'display module'!");
561 /* Hack -- If requested, display scores and quit */
563 display_scores(0, show_score);
565 /* Catch nasty signals */
569 init_angband(p_ptr, false);
571 /* Wait for response */
575 play_game(p_ptr, new_game, browsing_movie);