OSDN Git Service

[Refactor] #1524 arg_fiddleを常時真として削除した
[hengbandforosx/hengbandosx.git] / src / main.cpp
1 /*
2  * Copyright (c) 1997 Ben Harrison, and others
3  *
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.
7  */
8
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/inet.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"
30
31 /*
32  * Available graphic modes
33  */
34 #define GRAPHICS_NONE 0
35 #define GRAPHICS_ORIGINAL 1
36 #define GRAPHICS_ADAM_BOLT 2
37 #define GRAPHICS_HENGBAND 3
38
39 /*
40  * Some machines have a "main()" function in their "main-xxx.c" file,
41  * all the others use this file for their "main()" function.
42  */
43
44 #ifndef WINDOWS
45 /*
46  * A hook for "quit()".
47  *
48  * Close down, then fall back into "quit()".
49  */
50 static void quit_hook(concptr s)
51 {
52     int j;
53
54     /* Unused */
55     (void)s;
56
57     /* Scan windows */
58     for (j = 8 - 1; j >= 0; j--) {
59         /* Unused */
60         if (!angband_term[j])
61             continue;
62
63         /* Nuke it */
64         term_nuke(angband_term[j]);
65     }
66 }
67
68 /*
69  * Set the stack size and overlay buffer (see main-286.c")
70  */
71 #ifdef PRIVATE_USER_PATH
72
73 /*
74  * Create an ".angband/" directory in the users home directory.
75  *
76  * ToDo: Add error handling.
77  * ToDo: Only create the directories when actually writing files.
78  */
79 static void create_user_dir(void)
80 {
81     char dirpath[1024];
82     char subdirpath[1024];
83
84     /* Get an absolute path from the filename */
85     path_parse(dirpath, 1024, PRIVATE_USER_PATH);
86
87     /* Create the ~/.angband/ directory */
88     mkdir(dirpath, 0700);
89
90     /* Build the path to the variant-specific sub-directory */
91     path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
92
93     /* Create the directory */
94     mkdir(subdirpath, 0700);
95 }
96
97 #endif /* PRIVATE_USER_PATH */
98
99 /*
100  * Initialize and verify the file paths, and the score file.
101  *
102  * Use the ANGBAND_PATH environment var if possible, else use
103  * DEFAULT_(LIB|VAR)_PATH, and in either case, branch off
104  * appropriately.
105  *
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...
110  *
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.
114  *
115  * Make sure that the path doesn't overflow the buffer.  We have
116  * to leave enough space for the path separator, directory, and
117  * filenames.
118  */
119 static void init_stuff(void)
120 {
121     char libpath[1024], varpath[1024];
122
123     concptr tail;
124
125     /* Get the environment variable */
126     tail = getenv("ANGBAND_PATH");
127
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);
131
132     /* Make sure they're terminated */
133     libpath[511] = '\0';
134     varpath[511] = '\0';
135
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);
141
142     /* Initialize */
143     init_file_paths(libpath, varpath);
144 }
145
146 /*
147  * Handle a "-d<what>=<path>" option
148  *
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").
151  *
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").
154  */
155 static void change_path(concptr info)
156 {
157     concptr s;
158
159     /* Find equal sign */
160     s = angband_strchr(info, '=');
161
162     /* Verify equal sign */
163     if (!s)
164         quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
165
166     /* Analyze */
167     switch (tolower(info[0])) {
168     case 'a': {
169         string_free(ANGBAND_DIR_APEX);
170         ANGBAND_DIR_APEX = string_make(s + 1);
171         break;
172     }
173
174     case 'f': {
175         string_free(ANGBAND_DIR_FILE);
176         ANGBAND_DIR_FILE = string_make(s + 1);
177         break;
178     }
179
180     case 'h': {
181         string_free(ANGBAND_DIR_HELP);
182         ANGBAND_DIR_HELP = string_make(s + 1);
183         break;
184     }
185
186     case 'i': {
187         string_free(ANGBAND_DIR_INFO);
188         ANGBAND_DIR_INFO = string_make(s + 1);
189         break;
190     }
191
192     case 'u': {
193         string_free(ANGBAND_DIR_USER);
194         ANGBAND_DIR_USER = string_make(s + 1);
195         break;
196     }
197
198     case 'x': {
199         string_free(ANGBAND_DIR_XTRA);
200         ANGBAND_DIR_XTRA = string_make(s + 1);
201         break;
202     }
203
204     case 'b': {
205         string_free(ANGBAND_DIR_BONE);
206         ANGBAND_DIR_BONE = string_make(s + 1);
207         break;
208     }
209
210     case 'd': {
211         string_free(ANGBAND_DIR_DATA);
212         ANGBAND_DIR_DATA = string_make(s + 1);
213         break;
214     }
215
216     case 'e': {
217         string_free(ANGBAND_DIR_EDIT);
218         ANGBAND_DIR_EDIT = string_make(s + 1);
219         break;
220     }
221
222     case 's': {
223         string_free(ANGBAND_DIR_SAVE);
224         ANGBAND_DIR_SAVE = string_make(s + 1);
225         break;
226     }
227
228     case 'z': {
229         string_free(ANGBAND_DIR_SCRIPT);
230         ANGBAND_DIR_SCRIPT = string_make(s + 1);
231         break;
232     }
233
234     default: {
235         quit_fmt("Bad semantics in '-d%s'", info);
236     }
237     }
238 }
239
240 static void display_usage(const char *program)
241 {
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");
259     puts("");
260
261 #ifdef USE_X11
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");
270     puts("");
271 #endif /* USE_X11 */
272
273 #ifdef USE_GCU
274     puts("  -mgcu    To use GCU (GNU Curses)");
275 #endif /* USE_GCU */
276
277 #ifdef USE_CAP
278     puts("  -mcap    To use CAP (\"Termcap\" calls)");
279 #endif /* USE_CAP */
280
281     /* Actually abort the process */
282     quit(nullptr);
283 }
284
285 /*
286  * @brief 2文字以上のコマンドライン引数 (オプション)を実行する
287  * @param opt コマンドライン引数
288  * @return Usageを表示する必要があるか否か
289  * @details v3.0.0 Alpha21時点では、スポイラー出力モードの判定及び実行を行う
290  */
291 static bool parse_long_opt(const char *opt)
292 {
293     if (strcmp(opt + 2, "output-spoilers") != 0) {
294         return true;
295     }
296
297     init_stuff();
298     init_angband(p_ptr, true);
299     switch (output_all_spoilers()) {
300     case spoiler_output_status::SPOILER_OUTPUT_SUCCESS:
301         puts("Successfully created a spoiler file.");
302         quit(nullptr);
303         break;
304     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FOPEN:
305         quit("Cannot create spoiler file.");
306         break;
307     case spoiler_output_status::SPOILER_OUTPUT_FAIL_FCLOSE:
308         quit("Cannot close spoiler file.");
309         break;
310     default:
311         break;
312     }
313
314     return false;
315 }
316
317 /*
318  * Simple "main" function for multiple platforms.
319  *
320  * Note the special "--" option which terminates the processing of
321  * standard options.  All non-standard options (if any) are passed
322  * directly to the "init_xxx()" function.
323  */
324 int main(int argc, char *argv[])
325 {
326     int i;
327
328     bool done = false;
329     bool new_game = false;
330     int show_score = 0;
331     concptr mstr = nullptr;
332     bool args = true;
333
334     /* Save the "program name" XXX XXX XXX */
335     argv0 = argv[0];
336
337 #ifdef SET_UID
338
339     /* Default permissions on files */
340     (void)umask(022);
341
342 #endif
343
344     /* Get the file paths */
345     init_stuff();
346
347 #ifdef SET_UID
348
349     /* Get the user id (?) */
350     p_ptr->player_uid = getuid();
351
352 #ifdef VMS
353     /* Mega-Hack -- Factor group id */
354     p_ptr->player_uid += (getgid() * 1000);
355 #endif
356
357 #ifdef SAFE_SETUID
358
359 #ifdef _POSIX_SAVED_IDS
360
361     /* Save some info for later */
362     p_ptr->player_euid = geteuid();
363     p_ptr->player_egid = getegid();
364
365 #endif
366
367 #endif
368
369 #endif
370
371     /* Drop permissions */
372     safe_setuid_drop();
373
374 #ifdef SET_UID
375
376     /* Acquire the "user name" as a default player name */
377     user_name(p_ptr->name, p_ptr->player_uid);
378
379 #ifdef PRIVATE_USER_PATH
380
381     /* Create a directory for the users files. */
382     create_user_dir();
383
384 #endif /* PRIVATE_USER_PATH */
385
386 #endif /* SET_UID */
387
388     /* Process the command line arguments */
389     bool browsing_movie = false;
390     for (i = 1; args && (i < argc); i++) {
391         /* Require proper options */
392         if (argv[i][0] != '-') {
393             display_usage(argv[0]);
394             continue;
395         }
396
397         /* Analyze option */
398         bool is_usage_needed = false;
399         switch (argv[i][1]) {
400         case 'N':
401         case 'n': {
402             new_game = true;
403             break;
404         }
405         case 'W':
406         case 'w': {
407             arg_wizard = true;
408             break;
409         }
410         case 'B':
411         case 'b': {
412             arg_music = true;
413             break;
414         }
415         case 'V':
416         case 'v': {
417             arg_sound = true;
418             break;
419         }
420         case 'G':
421         case 'g': {
422             /* HACK - Graphics mode switches on the original tiles */
423             arg_graphics = GRAPHICS_ORIGINAL;
424             break;
425         }
426         case 'R':
427         case 'r': {
428             arg_force_roguelike = true;
429             break;
430         }
431         case 'O':
432         case 'o': {
433             arg_force_original = true;
434             break;
435         }
436         case 'S':
437         case 's': {
438             show_score = atoi(&argv[i][2]);
439             if (show_score <= 0)
440                 show_score = 10;
441             break;
442         }
443         case 'u':
444         case 'U': {
445             if (!argv[i][2]) {
446                 is_usage_needed = true;
447                 break;
448             }
449
450             strcpy(p_ptr->name, &argv[i][2]);
451             break;
452         }
453         case 'm': {
454             if (!argv[i][2]) {
455                 is_usage_needed = true;
456                 break;
457             }
458
459             mstr = &argv[i][2];
460             break;
461         }
462         case 'M': {
463             arg_monochrome = true;
464             break;
465         }
466         case 'd':
467         case 'D': {
468             change_path(&argv[i][2]);
469             break;
470         }
471         case 'x': {
472             if (!argv[i][2]) {
473                 is_usage_needed = true;
474                 break;
475             }
476
477             prepare_browse_movie_with_path_build(&argv[i][2]);
478             browsing_movie = true;
479             break;
480         }
481         case '-': {
482             if (argv[i][2] == '\0') {
483                 argv[i] = argv[0];
484                 argc = argc - i;
485                 argv = argv + i;
486                 args = false;
487             } else {
488                 is_usage_needed = parse_long_opt(argv[i]);
489             }
490             break;
491         }
492         default: {
493             is_usage_needed = true;
494             break;
495         }
496         }
497
498         if (!is_usage_needed)
499             continue;
500
501         display_usage(argv[0]);
502     }
503
504     /* Hack -- Forget standard args */
505     if (args) {
506         argc = 1;
507         argv[1] = nullptr;
508     }
509
510     /* Process the player name */
511     process_player_name(p_ptr, true);
512
513     /* Install "quit" hook */
514     quit_aux = quit_hook;
515
516 #ifdef USE_XAW
517     /* Attempt to use the "main-xaw.c" support */
518     if (!done && (!mstr || (streq(mstr, "xaw")))) {
519         extern errr init_xaw(int, char **);
520         if (0 == init_xaw(argc, argv)) {
521             ANGBAND_SYS = "xaw";
522             done = true;
523         }
524     }
525 #endif
526
527 #ifdef USE_X11
528     /* Attempt to use the "main-x11.c" support */
529     if (!done && (!mstr || (streq(mstr, "x11")))) {
530         extern errr init_x11(int, char **);
531         if (0 == init_x11(argc, argv)) {
532             ANGBAND_SYS = "x11";
533             done = true;
534         }
535     }
536 #endif
537
538 #ifdef USE_GCU
539     /* Attempt to use the "main-gcu.c" support */
540     if (!done && (!mstr || (streq(mstr, "gcu")))) {
541         extern errr init_gcu(int, char **);
542         if (0 == init_gcu(argc, argv)) {
543             ANGBAND_SYS = "gcu";
544             done = true;
545         }
546     }
547 #endif
548
549 #ifdef USE_CAP
550     /* Attempt to use the "main-cap.c" support */
551     if (!done && (!mstr || (streq(mstr, "cap")))) {
552         extern errr init_cap(int, char **);
553         if (0 == init_cap(argc, argv)) {
554             ANGBAND_SYS = "cap";
555             done = true;
556         }
557     }
558 #endif
559
560     /* Make sure we have a display! */
561     if (!done)
562         quit("Unable to prepare any 'display module'!");
563
564     /* Hack -- If requested, display scores and quit */
565     if (show_score > 0)
566         display_scores(0, show_score);
567
568     /* Catch nasty signals */
569     signals_init();
570
571     /* Initialize */
572     init_angband(p_ptr, false);
573
574     /* Wait for response */
575     pause_line(23);
576
577     /* Play the game */
578     play_game(p_ptr, new_game, browsing_movie);
579
580     /* Quit */
581     quit(nullptr);
582
583     /* Exit */
584     return (0);
585 }
586
587 #endif