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"
14 #include "io/record-play-movie.h"
15 #include "io/signal-handlers.h"
16 #include "io/uid-checker.h"
17 #include "main-unix/unix-user-ids.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.
46 #if !defined(WINDOWS) && !defined(MACH_O_COCOA)
48 * A hook for "quit()".
50 * Close down, then fall back into "quit()".
52 static void quit_hook(concptr s)
58 for (auto it = angband_terms.rbegin(); it != angband_terms.rend(); ++it) {
61 if (term == nullptr) {
70 static void init_stuff()
73 const auto tail = getenv("ANGBAND_PATH");
74 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
75 if (!suffix(libpath, PATH_SEP)) {
76 strcat(libpath, PATH_SEP);
79 init_file_paths(libpath, libpath);
83 * Handle a "-d<what>=<path>" option
85 * The "<what>" can be any string starting with the same letter as the
86 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
88 * The "<path>" can be any legal path for the given system, and should
89 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
91 static void change_path(concptr info)
93 const auto s = angband_strchr(info, '=');
95 /* Verify equal sign */
97 quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
100 switch (tolower(info[0])) {
102 ANGBAND_DIR_APEX = s + 1;
105 ANGBAND_DIR_FILE = s + 1;
108 ANGBAND_DIR_HELP = s + 1;
111 ANGBAND_DIR_INFO = s + 1;
114 ANGBAND_DIR_USER = s + 1;
117 ANGBAND_DIR_XTRA = s + 1;
120 ANGBAND_DIR_BONE = s + 1;
123 ANGBAND_DIR_DATA = s + 1;
126 ANGBAND_DIR_EDIT = s + 1;
129 ANGBAND_DIR_SAVE = s + 1;
132 ANGBAND_DIR_SCRIPT = s + 1;
135 quit_fmt("Bad semantics in '-d%s'", info);
139 static void display_usage(const char *program)
141 /* Dump usage information */
142 printf("Usage: %s [options] [-- subopts]\n", program);
143 puts(" -n Start a new character");
144 puts(" -f Request fiddle mode");
145 puts(" -w Request wizard mode");
146 puts(" -b Request BGM mode");
147 puts(" -v Request sound mode");
148 puts(" -g Request graphics mode");
149 puts(" -o Request original keyset");
150 puts(" -r Request rogue-like keyset");
151 puts(" -M Request monochrome mode");
152 puts(" -s<num> Show <num> high scores");
153 puts(" -u<who> Use your <who> savefile");
154 puts(" -m<sys> Force 'main-<sys>.c' usage");
155 puts(" -d<def> Define a 'lib' dir sub-path");
156 puts(" --output-spoilers");
157 puts(" Output auto generated spoilers and exit");
161 puts(" -mx11 To use X11");
162 puts(" -- Sub options");
163 puts(" -- -d Set display name");
164 puts(" -- -o Request old 8x8 tile graphics");
165 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
166 puts(" -- -b Request Bigtile graphics mode");
167 puts(" -- -s Turn off smoothscaling graphics");
168 puts(" -- -n# Number of terms to use");
173 puts(" -mgcu To use GCU (GNU Curses)");
174 puts(" -- Sub options");
175 puts(" -- -o old subwindow layout (no bigscreen)");
179 puts(" -mcap To use CAP (\"Termcap\" calls)");
182 /* Actually abort the process */
187 * @brief 2文字以上のコマンドライン引数 (オプション)を実行する
188 * @param opt コマンドライン引数
189 * @return Usageを表示する必要があるか否か
190 * @details v3.0.0 Alpha21時点では、スポイラー出力モードの判定及び実行を行う
192 static bool parse_long_opt(const char *opt)
194 if (strcmp(opt + 2, "output-spoilers") != 0) {
199 init_angband(p_ptr, true);
200 switch (output_all_spoilers()) {
201 case SpoilerOutputResultType::SUCCESSFUL:
202 puts("Successfully created a spoiler file.");
205 case SpoilerOutputResultType::FILE_OPEN_FAILED:
206 quit("Cannot create spoiler file.");
208 case SpoilerOutputResultType::FILE_CLOSE_FAILED:
209 quit("Cannot close spoiler file.");
219 * Simple "main" function for multiple platforms.
221 * Note the special "--" option which terminates the processing of
222 * standard options. All non-standard options (if any) are passed
223 * directly to the "init_xxx()" function.
225 int main(int argc, char *argv[])
228 auto new_game = false;
230 concptr mstr = nullptr;
235 /* Default permissions on files */
240 auto &ids = UnixUserIds::get_instance();
242 ids.set_user_id(getuid());
244 ids.mod_user_id(getgid() * 1000);
247 #if defined(SAFE_SETUID) && defined(_POSIX_SAVED_IDS)
248 ids.set_effective_user_id(geteuid());
249 ids.set_effective_group_id(getegid());
256 user_name(p_ptr->name, ids.get_user_id());
257 #ifdef PRIVATE_USER_PATH
258 /* Create a directory for the user's files; handled by init.c. */
259 create_needed_dirs();
260 #endif /* PRIVATE_USER_PATH */
263 auto browsing_movie = false;
264 for (auto i = 1; args && (i < argc); i++) {
265 if (argv[i][0] != '-') {
266 display_usage(argv[0]);
270 auto is_usage_needed = false;
271 switch (argv[i][1]) {
286 arg_graphics = GRAPHICS_ORIGINAL;
290 arg_force_roguelike = true;
294 arg_force_original = true;
298 show_score = atoi(&argv[i][2]);
299 if (show_score <= 0) {
307 is_usage_needed = true;
311 strcpy(p_ptr->name, &argv[i][2]);
315 is_usage_needed = true;
322 arg_monochrome = true;
326 change_path(&argv[i][2]);
330 is_usage_needed = true;
334 prepare_browse_movie_with_path_build(&argv[i][2]);
335 browsing_movie = true;
338 if (argv[i][2] == '\0') {
344 is_usage_needed = parse_long_opt(argv[i]);
349 is_usage_needed = true;
353 if (!is_usage_needed) {
357 display_usage(argv[0]);
365 process_player_name(p_ptr, true);
366 quit_aux = quit_hook;
369 if (!done && (!mstr || (streq(mstr, "x11")))) {
370 extern errr init_x11(int, char **);
371 if (0 == init_x11(argc, argv)) {
379 if (!done && (!mstr || (streq(mstr, "gcu")))) {
380 extern errr init_gcu(int, char **);
381 if (0 == init_gcu(argc, argv)) {
389 if (!done && (!mstr || (streq(mstr, "cap")))) {
390 extern errr init_cap(int, char **);
391 if (0 == init_cap(argc, argv)) {
399 quit("Unable to prepare any 'display module'!");
402 if (show_score > 0) {
403 display_scores(0, show_score);
409 TermCenteredOffsetSetter tcos(MAIN_TERM_MIN_COLS, MAIN_TERM_MIN_ROWS);
410 init_angband(p_ptr, false);
411 pause_line(MAIN_TERM_MIN_ROWS - 1);
414 play_game(p_ptr, new_game, browsing_movie);