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 "autopick/autopick-pref-processor.h"
10 #include "core/asking-player.h"
11 #include "core/game-play.h"
12 #include "core/scores.h"
13 #include "game-option/runtime-arguments.h"
14 #include "io/files-util.h"
16 #include "io/record-play-movie.h"
17 #include "io/signal-handlers.h"
18 #include "io/uid-checker.h"
19 #include "main/angband-initializer.h"
20 #include "player/process-name.h"
21 #include "system/angband-version.h"
22 #include "system/angband.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 "wizard/wizard-spoiler.h"
31 * Available graphic modes
33 #define GRAPHICS_NONE 0
34 #define GRAPHICS_ORIGINAL 1
35 #define GRAPHICS_ADAM_BOLT 2
36 #define GRAPHICS_HENGBAND 3
39 * Some machines have a "main()" function in their "main-xxx.c" file,
40 * all the others use this file for their "main()" function.
45 * A hook for "quit()".
47 * Close down, then fall back into "quit()".
49 static void quit_hook(concptr s)
57 for (j = 8 - 1; j >= 0; j--) {
63 term_nuke(angband_term[j]);
68 * Set the stack size and overlay buffer (see main-286.c")
70 #ifdef PRIVATE_USER_PATH
73 * Create an ".angband/" directory in the users home directory.
75 * ToDo: Add error handling.
76 * ToDo: Only create the directories when actually writing files.
78 static void create_user_dir(void)
81 char subdirpath[1024];
83 /* Get an absolute path from the filename */
84 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
86 /* Create the ~/.angband/ directory */
89 /* Build the path to the variant-specific sub-directory */
90 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
92 /* Create the directory */
93 mkdir(subdirpath, 0700);
96 #endif /* PRIVATE_USER_PATH */
99 * Initialize and verify the file paths, and the score file.
101 * Use the ANGBAND_PATH environment var if possible, else use
102 * DEFAULT_(LIB|VAR)_PATH, and in either case, branch off
105 * First, we'll look for the ANGBAND_PATH environment variable,
106 * and then look for the files in there. If that doesn't work,
107 * we'll try the DEFAULT_(LIB|VAR)_PATH constants. So be sure
108 * that one of these two things works...
110 * We must ensure that the path ends with "PATH_SEP" if needed,
111 * since the "init_file_paths()" function will simply append the
112 * relevant "sub-directory names" to the given path.
114 * Make sure that the path doesn't overflow the buffer. We have
115 * to leave enough space for the path separator, directory, and
118 static void init_stuff(void)
120 char libpath[1024], varpath[1024];
124 /* Get the environment variable */
125 tail = getenv("ANGBAND_PATH");
127 /* Use the angband_path, or a default */
128 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
129 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
131 /* Make sure they're terminated */
135 /* Hack -- Add a path separator (only if needed) */
136 if (!suffix(libpath, PATH_SEP))
137 strcat(libpath, PATH_SEP);
138 if (!suffix(varpath, PATH_SEP))
139 strcat(varpath, PATH_SEP);
142 init_file_paths(libpath, varpath);
146 * Handle a "-d<what>=<path>" option
148 * The "<what>" can be any string starting with the same letter as the
149 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
151 * The "<path>" can be any legal path for the given system, and should
152 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
154 static void change_path(concptr info)
158 /* Find equal sign */
159 s = angband_strchr(info, '=');
161 /* Verify equal sign */
163 quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
166 switch (tolower(info[0])) {
168 string_free(ANGBAND_DIR_APEX);
169 ANGBAND_DIR_APEX = string_make(s + 1);
174 string_free(ANGBAND_DIR_FILE);
175 ANGBAND_DIR_FILE = string_make(s + 1);
180 string_free(ANGBAND_DIR_HELP);
181 ANGBAND_DIR_HELP = string_make(s + 1);
186 string_free(ANGBAND_DIR_INFO);
187 ANGBAND_DIR_INFO = string_make(s + 1);
192 string_free(ANGBAND_DIR_USER);
193 ANGBAND_DIR_USER = string_make(s + 1);
198 string_free(ANGBAND_DIR_XTRA);
199 ANGBAND_DIR_XTRA = string_make(s + 1);
204 string_free(ANGBAND_DIR_BONE);
205 ANGBAND_DIR_BONE = string_make(s + 1);
210 string_free(ANGBAND_DIR_DATA);
211 ANGBAND_DIR_DATA = string_make(s + 1);
216 string_free(ANGBAND_DIR_EDIT);
217 ANGBAND_DIR_EDIT = string_make(s + 1);
222 string_free(ANGBAND_DIR_SAVE);
223 ANGBAND_DIR_SAVE = string_make(s + 1);
228 string_free(ANGBAND_DIR_SCRIPT);
229 ANGBAND_DIR_SCRIPT = string_make(s + 1);
234 quit_fmt("Bad semantics in '-d%s'", info);
239 static void display_usage(const char* program)
241 /* Dump usage information */
242 printf("Usage: %s [options] [-- subopts]\n", program);
243 puts(" -n Start a new character");
244 puts(" -f Request fiddle mode");
245 puts(" -w Request wizard mode");
246 puts(" -b Request BGM mode");
247 puts(" -v Request sound mode");
248 puts(" -g Request graphics mode");
249 puts(" -o Request original keyset");
250 puts(" -r Request rogue-like keyset");
251 puts(" -M Request monochrome mode");
252 puts(" -s<num> Show <num> high scores");
253 puts(" -u<who> Use your <who> savefile");
254 puts(" -m<sys> Force 'main-<sys>.c' usage");
255 puts(" -d<def> Define a 'lib' dir sub-path");
256 puts(" --output-spoilers");
257 puts(" Output auto generated spoilers and exit");
261 puts(" -mx11 To use X11");
262 puts(" -- Sub options");
263 puts(" -- -d Set display name");
264 puts(" -- -o Request old 8x8 tile graphics");
265 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
266 puts(" -- -b Request Bigtile graphics mode");
267 puts(" -- -s Turn off smoothscaling graphics");
268 puts(" -- -n# Number of terms to use");
273 puts(" -mgcu To use GCU (GNU Curses)");
277 puts(" -mcap To use CAP (\"Termcap\" calls)");
280 /* Actually abort the process */
284 static bool parse_long_opt(const char *opt)
286 bool is_usage_needed = TRUE;
288 if (strcmp(opt + 2, "output-spoilers") == 0) {
290 init_angband(p_ptr, process_autopick_file_command, TRUE);
291 switch (output_all_spoilers()) {
292 case SPOILER_OUTPUT_SUCCESS:
293 puts("Successfully created a spiler file.");
296 case SPOILER_OUTPUT_FAIL_FOPEN:
297 quit("Cannot create spoiler file.");
299 case SPOILER_OUTPUT_FAIL_FCLOSE:
300 quit("Cannot close spoiler file.");
305 is_usage_needed = FALSE;
308 return is_usage_needed;
312 * Simple "main" function for multiple platforms.
314 * Note the special "--" option which terminates the processing of
315 * standard options. All non-standard options (if any) are passed
316 * directly to the "init_xxx()" function.
318 int main(int argc, char *argv[])
323 bool new_game = FALSE;
328 /* Save the "program name" XXX XXX XXX */
333 /* Default permissions on files */
338 /* Get the file paths */
343 /* Get the user id (?) */
344 p_ptr->player_uid = getuid();
347 /* Mega-Hack -- Factor group id */
348 p_ptr->player_uid += (getgid() * 1000);
353 #ifdef _POSIX_SAVED_IDS
355 /* Save some info for later */
356 p_ptr->player_euid = geteuid();
357 p_ptr->player_egid = getegid();
365 /* Drop permissions */
370 /* Acquire the "user name" as a default player name */
371 user_name(p_ptr->name, p_ptr->player_uid);
373 #ifdef PRIVATE_USER_PATH
375 /* Create a directory for the users files. */
378 #endif /* PRIVATE_USER_PATH */
382 /* Process the command line arguments */
383 bool browsing_movie = FALSE;
384 for (i = 1; args && (i < argc); i++) {
385 /* Require proper options */
386 if (argv[i][0] != '-') {
387 display_usage(argv[0]);
392 bool is_usage_needed = FALSE;
393 switch (argv[i][1]) {
421 /* HACK - Graphics mode switches on the original tiles */
422 arg_graphics = GRAPHICS_ORIGINAL;
427 arg_force_roguelike = TRUE;
432 arg_force_original = TRUE;
437 show_score = atoi(&argv[i][2]);
445 is_usage_needed = TRUE;
449 strcpy(p_ptr->name, &argv[i][2]);
454 is_usage_needed = TRUE;
462 arg_monochrome = TRUE;
467 change_path(&argv[i][2]);
472 is_usage_needed = TRUE;
476 prepare_browse_movie_with_path_build(&argv[i][2]);
477 browsing_movie = TRUE;
481 if (argv[i][2] == '\0') {
487 is_usage_needed = parse_long_opt(argv[i]);
492 is_usage_needed = TRUE;
497 if (!is_usage_needed)
500 display_usage(argv[0]);
503 /* Hack -- Forget standard args */
509 /* Process the player name */
510 process_player_name(p_ptr, TRUE);
512 /* Install "quit" hook */
513 quit_aux = quit_hook;
516 /* Attempt to use the "main-xaw.c" support */
517 if (!done && (!mstr || (streq(mstr, "xaw")))) {
518 extern errr init_xaw(int, char **);
519 if (0 == init_xaw(argc, argv)) {
527 /* Attempt to use the "main-x11.c" support */
528 if (!done && (!mstr || (streq(mstr, "x11")))) {
529 extern errr init_x11(int, char **);
530 if (0 == init_x11(argc, argv)) {
538 /* Attempt to use the "main-gcu.c" support */
539 if (!done && (!mstr || (streq(mstr, "gcu")))) {
540 extern errr init_gcu(int, char **);
541 if (0 == init_gcu(argc, argv)) {
549 /* Attempt to use the "main-cap.c" support */
550 if (!done && (!mstr || (streq(mstr, "cap")))) {
551 extern errr init_cap(int, char **);
552 if (0 == init_cap(argc, argv)) {
559 /* Make sure we have a display! */
561 quit("Unable to prepare any 'display module'!");
563 /* Hack -- If requested, display scores and quit */
565 display_scores(0, show_score);
567 /* Catch nasty signals */
571 init_angband(p_ptr, process_autopick_file_command, FALSE);
573 /* Wait for response */
577 play_game(p_ptr, new_game, browsing_movie);