4 * Copyright (c) 1997 Ben Harrison, and others
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies.
11 #include "system/angband.h"
12 #include "autopick/autopick-pref-processor.h"
13 #include "core/game-play.h"
14 #include "core/scores.h"
15 #include "game-option/runtime-arguments.h"
16 #include "io/chuukei.h"
17 #include "io/files-util.h"
19 #include "io/signal-handlers.h"
20 #include "io/uid-checker.h"
21 #include "main/init.h"
22 #include "player/process-name.h"
23 #include "system/angband-version.h"
24 #include "system/system-variables.h"
25 #include "term/gameterm.h"
26 #include "term/term-color-types.h"
29 * Available graphic modes
31 #define GRAPHICS_NONE 0
32 #define GRAPHICS_ORIGINAL 1
33 #define GRAPHICS_ADAM_BOLT 2
34 #define GRAPHICS_HENGBAND 3
37 * Some machines have a "main()" function in their "main-xxx.c" file,
38 * 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--)
60 if (!angband_term[j]) continue;
63 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 */
101 * Initialize and verify the file paths, and the score file.
103 * Use the ANGBAND_PATH environment var if possible, else use
104 * DEFAULT_PATH, and in either case, branch off appropriately.
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_PATH constant. So be sure that one of
109 * 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)
125 /* Get the environment variable */
126 tail = getenv("ANGBAND_PATH");
128 /* Use the angband_path, or a default */
129 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
131 /* Make sure it's terminated */
134 /* Hack -- Add a path separator (only if needed) */
135 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
138 init_file_paths(path);
144 * Handle a "-d<what>=<path>" option
146 * The "<what>" can be any string starting with the same letter as the
147 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
149 * The "<path>" can be any legal path for the given system, and should
150 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
152 static void change_path(concptr info)
156 /* Find equal sign */
157 s = angband_strchr(info, '=');
159 /* Verify equal sign */
160 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
163 switch (tolower(info[0]))
167 string_free(ANGBAND_DIR_APEX);
168 ANGBAND_DIR_APEX = string_make(s+1);
174 string_free(ANGBAND_DIR_FILE);
175 ANGBAND_DIR_FILE = string_make(s+1);
181 string_free(ANGBAND_DIR_HELP);
182 ANGBAND_DIR_HELP = string_make(s+1);
188 string_free(ANGBAND_DIR_INFO);
189 ANGBAND_DIR_INFO = string_make(s+1);
195 string_free(ANGBAND_DIR_USER);
196 ANGBAND_DIR_USER = string_make(s+1);
202 string_free(ANGBAND_DIR_XTRA);
203 ANGBAND_DIR_XTRA = string_make(s+1);
209 string_free(ANGBAND_DIR_BONE);
210 ANGBAND_DIR_BONE = string_make(s+1);
216 string_free(ANGBAND_DIR_DATA);
217 ANGBAND_DIR_DATA = string_make(s+1);
223 string_free(ANGBAND_DIR_EDIT);
224 ANGBAND_DIR_EDIT = string_make(s+1);
230 string_free(ANGBAND_DIR_SAVE);
231 ANGBAND_DIR_SAVE = string_make(s+1);
237 string_free(ANGBAND_DIR_SCRIPT);
238 ANGBAND_DIR_SCRIPT = string_make(s+1);
244 quit_fmt("Bad semantics in '-d%s'", info);
249 static void display_usage(void)
251 /* Dump usage information */
252 puts("Usage: angband [options] [-- subopts]");
253 puts(" -n Start a new character");
254 puts(" -f Request fiddle mode");
255 puts(" -w Request wizard mode");
256 puts(" -b Request BGM mode");
257 puts(" -v Request sound mode");
258 puts(" -g Request graphics mode");
259 puts(" -o Request original keyset");
260 puts(" -r Request rogue-like keyset");
261 puts(" -M Request monochrome mode");
262 puts(" -s<num> Show <num> high scores");
263 puts(" -u<who> Use your <who> savefile");
264 puts(" -m<sys> Force 'main-<sys>.c' usage");
265 puts(" -d<def> Define a 'lib' dir sub-path");
269 puts(" -mx11 To use X11");
270 puts(" -- Sub options");
271 puts(" -- -d Set display name");
272 puts(" -- -o Request old 8x8 tile graphics");
273 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
274 puts(" -- -b Request Bigtile graphics mode");
275 puts(" -- -s Turn off smoothscaling graphics");
276 puts(" -- -n# Number of terms to use");
281 puts(" -mgcu To use GCU (GNU Curses)");
285 puts(" -mcap To use CAP (\"Termcap\" calls)");
288 /* Actually abort the process */
294 * Simple "main" function for multiple platforms.
296 * Note the special "--" option which terminates the processing of
297 * standard options. All non-standard options (if any) are passed
298 * directly to the "init_xxx()" function.
300 int main(int argc, char *argv[])
305 bool new_game = FALSE;
310 /* Save the "program name" XXX XXX XXX */
315 /* Default permissions on files */
321 /* Get the file paths */
327 /* Get the user id (?) */
328 p_ptr->player_uid = getuid();
331 /* Mega-Hack -- Factor group id */
332 p_ptr->player_uid += (getgid() * 1000);
337 # ifdef _POSIX_SAVED_IDS
339 /* Save some info for later */
340 p_ptr->player_euid = geteuid();
341 p_ptr->player_egid = getegid();
350 /* Drop permissions */
356 /* Acquire the "user name" as a default player name */
357 user_name(p_ptr->name, p_ptr->player_uid);
359 #ifdef PRIVATE_USER_PATH
361 /* Create a directory for the users files. */
364 #endif /* PRIVATE_USER_PATH */
369 /* Process the command line arguments */
370 for (i = 1; args && (i < argc); i++)
372 /* Require proper options */
373 if (argv[i][0] != '-')
380 bool is_usage_needed = FALSE;
416 /* HACK - Graphics mode switches on the original tiles */
417 arg_graphics = GRAPHICS_ORIGINAL;
423 arg_force_roguelike = TRUE;
429 arg_force_original = TRUE;
435 show_score = atoi(&argv[i][2]);
436 if (show_score <= 0) show_score = 10;
444 is_usage_needed = TRUE;
448 strcpy(p_ptr->name, &argv[i][2]);
455 is_usage_needed = TRUE;
464 arg_monochrome = TRUE;
470 change_path(&argv[i][2]);
479 is_usage_needed = TRUE;
483 chuukei_server = TRUE;
484 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
493 is_usage_needed = TRUE;
497 chuukei_client = TRUE;
498 connect_chuukei_server(&argv[i][2]);
506 is_usage_needed = TRUE;
510 prepare_browse_movie(&argv[i][2]);
523 is_usage_needed = TRUE;
528 if (!is_usage_needed) continue;
533 /* Hack -- Forget standard args */
541 /* Process the player name */
542 process_player_name(p_ptr,TRUE);
546 /* Install "quit" hook */
547 quit_aux = quit_hook;
552 /* Attempt to use the "main-xaw.c" support */
553 if (!done && (!mstr || (streq(mstr, "xaw"))))
555 extern errr init_xaw(int, char**);
556 if (0 == init_xaw(argc, argv))
565 /* Attempt to use the "main-x11.c" support */
566 if (!done && (!mstr || (streq(mstr, "x11"))))
568 extern errr init_x11(int, char**);
569 if (0 == init_x11(argc, argv))
578 /* Attempt to use the "main-gcu.c" support */
579 if (!done && (!mstr || (streq(mstr, "gcu"))))
581 extern errr init_gcu(int, char**);
582 if (0 == init_gcu(argc, argv))
591 /* Attempt to use the "main-cap.c" support */
592 if (!done && (!mstr || (streq(mstr, "cap"))))
594 extern errr init_cap(int, char**);
595 if (0 == init_cap(argc, argv))
603 /* Make sure we have a display! */
604 if (!done) quit("Unable to prepare any 'display module'!");
607 /* Hack -- If requested, display scores and quit */
608 if (show_score > 0) display_scores(0, show_score);
611 /* Catch nasty signals */
615 init_angband(p_ptr, process_autopick_file_command);
617 /* Wait for response */
621 play_game(p_ptr, new_game);