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/spoiler-util.h"
30 #include "wizard/wizard-spoiler.h"
34 * Available graphic modes
36 #define GRAPHICS_NONE 0
37 #define GRAPHICS_ORIGINAL 1
38 #define GRAPHICS_ADAM_BOLT 2
39 #define GRAPHICS_HENGBAND 3
42 * Some machines have a "main()" function in their "main-xxx.c" file,
43 * all the others use this file for their "main()" function.
48 * A hook for "quit()".
50 * Close down, then fall back into "quit()".
52 static void quit_hook(concptr s)
58 for (auto i = static_cast<int>(angband_terms.size()); i >= 0; i--) {
60 if (!angband_terms[i]) {
65 term_nuke(angband_terms[i]);
70 * Set the stack size and overlay buffer (see main-286.c")
72 #ifdef PRIVATE_USER_PATH
75 * Create an ".angband/" directory in the users home directory.
77 * ToDo: Add error handling.
78 * ToDo: Only create the directories when actually writing files.
80 static void create_user_dir(void)
83 char subdirpath[1024];
85 /* Get an absolute path from the filename */
86 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
88 /* Create the ~/.angband/ directory */
91 /* Build the path to the variant-specific sub-directory */
92 path_build(subdirpath, sizeof(subdirpath), dirpath, VARIANT_NAME.data());
94 /* Create the directory */
95 mkdir(subdirpath, 0700);
98 #endif /* PRIVATE_USER_PATH */
101 * Initialize and verify the file paths, and the score file.
103 * Use the ANGBAND_PATH environment var if possible, else use
104 * DEFAULT_(LIB|VAR)_PATH, and in either case, branch off
107 * First, we'll look for the ANGBAND_PATH environment variable,
108 * and then look for the files in there. If that doesn't work,
109 * we'll try the DEFAULT_(LIB|VAR)_PATH constants. So be sure
110 * that one of these two things works...
112 * We must ensure that the path ends with "PATH_SEP" if needed,
113 * since the "init_file_paths()" function will simply append the
114 * relevant "sub-directory names" to the given path.
116 * Make sure that the path doesn't overflow the buffer. We have
117 * to leave enough space for the path separator, directory, and
120 static void init_stuff(void)
122 char libpath[1024], varpath[1024];
126 /* Get the environment variable */
127 tail = getenv("ANGBAND_PATH");
129 /* Use the angband_path, or a default */
130 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
131 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
133 /* Make sure they're terminated */
137 /* Hack -- Add a path separator (only if needed) */
138 if (!suffix(libpath, PATH_SEP)) {
139 strcat(libpath, PATH_SEP);
141 if (!suffix(varpath, PATH_SEP)) {
142 strcat(varpath, PATH_SEP);
146 init_file_paths(libpath, varpath);
150 * Handle a "-d<what>=<path>" option
152 * The "<what>" can be any string starting with the same letter as the
153 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
155 * The "<path>" can be any legal path for the given system, and should
156 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
158 static void change_path(concptr info)
162 /* Find equal sign */
163 s = angband_strchr(info, '=');
165 /* Verify equal sign */
167 quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
171 switch (tolower(info[0])) {
173 string_free(ANGBAND_DIR_APEX);
174 ANGBAND_DIR_APEX = string_make(s + 1);
179 string_free(ANGBAND_DIR_FILE);
180 ANGBAND_DIR_FILE = string_make(s + 1);
185 string_free(ANGBAND_DIR_HELP);
186 ANGBAND_DIR_HELP = string_make(s + 1);
191 string_free(ANGBAND_DIR_INFO);
192 ANGBAND_DIR_INFO = string_make(s + 1);
197 string_free(ANGBAND_DIR_USER);
198 ANGBAND_DIR_USER = string_make(s + 1);
203 string_free(ANGBAND_DIR_XTRA);
204 ANGBAND_DIR_XTRA = string_make(s + 1);
209 string_free(ANGBAND_DIR_BONE);
210 ANGBAND_DIR_BONE = string_make(s + 1);
215 string_free(ANGBAND_DIR_DATA);
216 ANGBAND_DIR_DATA = string_make(s + 1);
221 string_free(ANGBAND_DIR_EDIT);
222 ANGBAND_DIR_EDIT = string_make(s + 1);
227 string_free(ANGBAND_DIR_SAVE);
228 ANGBAND_DIR_SAVE = string_make(s + 1);
233 string_free(ANGBAND_DIR_SCRIPT);
234 ANGBAND_DIR_SCRIPT = string_make(s + 1);
239 quit_fmt("Bad semantics in '-d%s'", info);
244 static void display_usage(const char *program)
246 /* Dump usage information */
247 printf("Usage: %s [options] [-- subopts]\n", program);
248 puts(" -n Start a new character");
249 puts(" -f Request fiddle mode");
250 puts(" -w Request wizard mode");
251 puts(" -b Request BGM mode");
252 puts(" -v Request sound mode");
253 puts(" -g Request graphics mode");
254 puts(" -o Request original keyset");
255 puts(" -r Request rogue-like keyset");
256 puts(" -M Request monochrome mode");
257 puts(" -s<num> Show <num> high scores");
258 puts(" -u<who> Use your <who> savefile");
259 puts(" -m<sys> Force 'main-<sys>.c' usage");
260 puts(" -d<def> Define a 'lib' dir sub-path");
261 puts(" --output-spoilers");
262 puts(" Output auto generated spoilers and exit");
266 puts(" -mx11 To use X11");
267 puts(" -- Sub options");
268 puts(" -- -d Set display name");
269 puts(" -- -o Request old 8x8 tile graphics");
270 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
271 puts(" -- -b Request Bigtile graphics mode");
272 puts(" -- -s Turn off smoothscaling graphics");
273 puts(" -- -n# Number of terms to use");
278 puts(" -mgcu To use GCU (GNU Curses)");
279 puts(" -- Sub options");
280 puts(" -- -o old subwindow layout (no bigscreen)");
284 puts(" -mcap To use CAP (\"Termcap\" calls)");
287 /* Actually abort the process */
292 * @brief 2文字以上のコマンドライン引数 (オプション)を実行する
293 * @param opt コマンドライン引数
294 * @return Usageを表示する必要があるか否か
295 * @details v3.0.0 Alpha21時点では、スポイラー出力モードの判定及び実行を行う
297 static bool parse_long_opt(const char *opt)
299 if (strcmp(opt + 2, "output-spoilers") != 0) {
304 init_angband(p_ptr, true);
305 switch (output_all_spoilers()) {
306 case SpoilerOutputResultType::SUCCESSFUL:
307 puts("Successfully created a spoiler file.");
310 case SpoilerOutputResultType::FILE_OPEN_FAILED:
311 quit("Cannot create spoiler file.");
313 case SpoilerOutputResultType::FILE_CLOSE_FAILED:
314 quit("Cannot close spoiler file.");
324 * Simple "main" function for multiple platforms.
326 * Note the special "--" option which terminates the processing of
327 * standard options. All non-standard options (if any) are passed
328 * directly to the "init_xxx()" function.
330 int main(int argc, char *argv[])
335 bool new_game = false;
337 concptr mstr = nullptr;
340 /* Save the "program name" XXX XXX XXX */
345 /* Default permissions on files */
350 /* Get the file paths */
355 /* Get the user id (?) */
356 p_ptr->player_uid = getuid();
359 /* Mega-Hack -- Factor group id */
360 p_ptr->player_uid += (getgid() * 1000);
365 #ifdef _POSIX_SAVED_IDS
367 /* Save some info for later */
368 p_ptr->player_euid = geteuid();
369 p_ptr->player_egid = getegid();
377 /* Drop permissions */
382 /* Acquire the "user name" as a default player name */
383 user_name(p_ptr->name, p_ptr->player_uid);
385 #ifdef PRIVATE_USER_PATH
387 /* Create a directory for the users files. */
390 #endif /* PRIVATE_USER_PATH */
394 /* Process the command line arguments */
395 bool browsing_movie = false;
396 for (i = 1; args && (i < argc); i++) {
397 /* Require proper options */
398 if (argv[i][0] != '-') {
399 display_usage(argv[0]);
404 bool is_usage_needed = false;
405 switch (argv[i][1]) {
423 /* HACK - Graphics mode switches on the original tiles */
424 arg_graphics = GRAPHICS_ORIGINAL;
429 arg_force_roguelike = true;
434 arg_force_original = true;
439 show_score = atoi(&argv[i][2]);
440 if (show_score <= 0) {
448 is_usage_needed = true;
452 strcpy(p_ptr->name, &argv[i][2]);
457 is_usage_needed = true;
465 arg_monochrome = true;
470 change_path(&argv[i][2]);
475 is_usage_needed = true;
479 prepare_browse_movie_with_path_build(&argv[i][2]);
480 browsing_movie = true;
484 if (argv[i][2] == '\0') {
490 is_usage_needed = parse_long_opt(argv[i]);
495 is_usage_needed = true;
500 if (!is_usage_needed) {
504 display_usage(argv[0]);
507 /* Hack -- Forget standard args */
513 /* Process the player name */
514 process_player_name(p_ptr, true);
516 /* Install "quit" hook */
517 quit_aux = quit_hook;
520 /* Attempt to use the "main-x11.c" support */
521 if (!done && (!mstr || (streq(mstr, "x11")))) {
522 extern errr init_x11(int, char **);
523 if (0 == init_x11(argc, argv)) {
531 /* Attempt to use the "main-gcu.c" support */
532 if (!done && (!mstr || (streq(mstr, "gcu")))) {
533 extern errr init_gcu(int, char **);
534 if (0 == init_gcu(argc, argv)) {
542 /* Attempt to use the "main-cap.c" support */
543 if (!done && (!mstr || (streq(mstr, "cap")))) {
544 extern errr init_cap(int, char **);
545 if (0 == init_cap(argc, argv)) {
552 /* Make sure we have a display! */
554 quit("Unable to prepare any 'display module'!");
557 /* Hack -- If requested, display scores and quit */
558 if (show_score > 0) {
559 display_scores(0, show_score);
562 /* Catch nasty signals */
566 init_angband(p_ptr, false);
568 /* Wait for response */
572 play_game(p_ptr, new_game, browsing_movie);