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.
12 #include "core/system-variables.h"
13 #include "uid-checker.h"
14 #include "signal-handlers.h"
19 #include "term/gameterm.h"
20 #include "player/process-name.h"
22 #include "autopick/autopick-pref-processor.h"
23 #include "core/angband-version.h"
24 #include "core/game-play.h"
27 * Available graphic modes
29 #define GRAPHICS_NONE 0
30 #define GRAPHICS_ORIGINAL 1
31 #define GRAPHICS_ADAM_BOLT 2
32 #define GRAPHICS_HENGBAND 3
35 * Some machines have a "main()" function in their "main-xxx.c" file,
36 * all the others use this file for their "main()" function.
43 * A hook for "quit()".
45 * Close down, then fall back into "quit()".
47 static void quit_hook(concptr s)
55 for (j = 8 - 1; j >= 0; j--)
58 if (!angband_term[j]) continue;
61 term_nuke(angband_term[j]);
67 * Set the stack size and overlay buffer (see main-286.c")
69 #ifdef PRIVATE_USER_PATH
72 * Create an ".angband/" directory in the users home directory.
74 * ToDo: Add error handling.
75 * ToDo: Only create the directories when actually writing files.
77 static void create_user_dir(void)
80 char subdirpath[1024];
82 /* Get an absolute path from the filename */
83 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
85 /* Create the ~/.angband/ directory */
88 /* Build the path to the variant-specific sub-directory */
89 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
91 /* Create the directory */
92 mkdir(subdirpath, 0700);
95 #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_PATH, and in either case, branch off appropriately.
104 * First, we'll look for the ANGBAND_PATH environment variable,
105 * and then look for the files in there. If that doesn't work,
106 * we'll try the DEFAULT_PATH constant. So be sure that one of
107 * these two things works...
109 * We must ensure that the path ends with "PATH_SEP" if needed,
110 * since the "init_file_paths()" function will simply append the
111 * relevant "sub-directory names" to the given path.
113 * Make sure that the path doesn't overflow the buffer. We have
114 * to leave enough space for the path separator, directory, and
117 static void init_stuff(void)
123 /* Get the environment variable */
124 tail = getenv("ANGBAND_PATH");
126 /* Use the angband_path, or a default */
127 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
129 /* Make sure it's terminated */
132 /* Hack -- Add a path separator (only if needed) */
133 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
136 init_file_paths(path);
142 * Handle a "-d<what>=<path>" option
144 * The "<what>" can be any string starting with the same letter as the
145 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
147 * The "<path>" can be any legal path for the given system, and should
148 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
150 static void change_path(concptr info)
154 /* Find equal sign */
155 s = my_strchr(info, '=');
157 /* Verify equal sign */
158 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
161 switch (tolower(info[0]))
165 string_free(ANGBAND_DIR_APEX);
166 ANGBAND_DIR_APEX = string_make(s+1);
172 string_free(ANGBAND_DIR_FILE);
173 ANGBAND_DIR_FILE = string_make(s+1);
179 string_free(ANGBAND_DIR_HELP);
180 ANGBAND_DIR_HELP = string_make(s+1);
186 string_free(ANGBAND_DIR_INFO);
187 ANGBAND_DIR_INFO = string_make(s+1);
193 string_free(ANGBAND_DIR_USER);
194 ANGBAND_DIR_USER = string_make(s+1);
200 string_free(ANGBAND_DIR_XTRA);
201 ANGBAND_DIR_XTRA = string_make(s+1);
207 string_free(ANGBAND_DIR_BONE);
208 ANGBAND_DIR_BONE = string_make(s+1);
214 string_free(ANGBAND_DIR_DATA);
215 ANGBAND_DIR_DATA = string_make(s+1);
221 string_free(ANGBAND_DIR_EDIT);
222 ANGBAND_DIR_EDIT = string_make(s+1);
228 string_free(ANGBAND_DIR_SAVE);
229 ANGBAND_DIR_SAVE = string_make(s+1);
235 string_free(ANGBAND_DIR_SCRIPT);
236 ANGBAND_DIR_SCRIPT = string_make(s+1);
242 quit_fmt("Bad semantics in '-d%s'", info);
247 static void display_usage(void)
249 /* Dump usage information */
250 puts("Usage: angband [options] [-- subopts]");
251 puts(" -n Start a new character");
252 puts(" -f Request fiddle mode");
253 puts(" -w Request wizard mode");
254 puts(" -b Request BGM mode");
255 puts(" -v Request sound mode");
256 puts(" -g Request graphics mode");
257 puts(" -o Request original keyset");
258 puts(" -r Request rogue-like keyset");
259 puts(" -M Request monochrome mode");
260 puts(" -s<num> Show <num> high scores");
261 puts(" -u<who> Use your <who> savefile");
262 puts(" -m<sys> Force 'main-<sys>.c' usage");
263 puts(" -d<def> Define a 'lib' dir sub-path");
267 puts(" -mx11 To use X11");
268 puts(" -- Sub options");
269 puts(" -- -d Set display name");
270 puts(" -- -o Request old 8x8 tile graphics");
271 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
272 puts(" -- -b Request Bigtile graphics mode");
273 puts(" -- -s Turn off smoothscaling graphics");
274 puts(" -- -n# Number of terms to use");
279 puts(" -mgcu To use GCU (GNU Curses)");
283 puts(" -mcap To use CAP (\"Termcap\" calls)");
286 /* Actually abort the process */
292 * Simple "main" function for multiple platforms.
294 * Note the special "--" option which terminates the processing of
295 * standard options. All non-standard options (if any) are passed
296 * directly to the "init_xxx()" function.
298 int main(int argc, char *argv[])
303 bool new_game = FALSE;
308 /* Save the "program name" XXX XXX XXX */
313 /* Default permissions on files */
319 /* Get the file paths */
325 /* Get the user id (?) */
326 p_ptr->player_uid = getuid();
329 /* Mega-Hack -- Factor group id */
330 p_ptr->player_uid += (getgid() * 1000);
335 # ifdef _POSIX_SAVED_IDS
337 /* Save some info for later */
338 p_ptr->player_euid = geteuid();
339 p_ptr->player_egid = getegid();
348 /* Drop permissions */
354 /* Acquire the "user name" as a default player name */
355 user_name(p_ptr->name, p_ptr->player_uid);
357 #ifdef PRIVATE_USER_PATH
359 /* Create a directory for the users files. */
362 #endif /* PRIVATE_USER_PATH */
367 /* Process the command line arguments */
368 for (i = 1; args && (i < argc); i++)
370 /* Require proper options */
371 if (argv[i][0] != '-')
378 bool is_usage_needed = FALSE;
414 /* HACK - Graphics mode switches on the original tiles */
415 arg_graphics = GRAPHICS_ORIGINAL;
421 arg_force_roguelike = TRUE;
427 arg_force_original = TRUE;
433 show_score = atoi(&argv[i][2]);
434 if (show_score <= 0) show_score = 10;
442 is_usage_needed = TRUE;
446 strcpy(p_ptr->name, &argv[i][2]);
453 is_usage_needed = TRUE;
462 arg_monochrome = TRUE;
468 change_path(&argv[i][2]);
477 is_usage_needed = TRUE;
481 chuukei_server = TRUE;
482 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
491 is_usage_needed = TRUE;
495 chuukei_client = TRUE;
496 connect_chuukei_server(&argv[i][2]);
504 is_usage_needed = TRUE;
508 prepare_browse_movie(&argv[i][2]);
521 is_usage_needed = TRUE;
526 if (!is_usage_needed) continue;
531 /* Hack -- Forget standard args */
539 /* Process the player name */
540 process_player_name(p_ptr,TRUE);
544 /* Install "quit" hook */
545 quit_aux = quit_hook;
550 /* Attempt to use the "main-xaw.c" support */
551 if (!done && (!mstr || (streq(mstr, "xaw"))))
553 extern errr init_xaw(int, char**);
554 if (0 == init_xaw(argc, argv))
563 /* Attempt to use the "main-x11.c" support */
564 if (!done && (!mstr || (streq(mstr, "x11"))))
566 extern errr init_x11(int, char**);
567 if (0 == init_x11(argc, argv))
576 /* Attempt to use the "main-gcu.c" support */
577 if (!done && (!mstr || (streq(mstr, "gcu"))))
579 extern errr init_gcu(int, char**);
580 if (0 == init_gcu(argc, argv))
589 /* Attempt to use the "main-cap.c" support */
590 if (!done && (!mstr || (streq(mstr, "cap"))))
592 extern errr init_cap(int, char**);
593 if (0 == init_cap(argc, argv))
601 /* Make sure we have a display! */
602 if (!done) quit("Unable to prepare any 'display module'!");
605 /* Hack -- If requested, display scores and quit */
606 if (show_score > 0) display_scores(0, show_score);
609 /* Catch nasty signals */
613 init_angband(p_ptr, process_autopick_file_command);
615 /* Wait for response */
619 play_game(p_ptr, new_game);