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"
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/system-variables.h"
23 #include "term/gameterm.h"
24 #include "term/term-color-types.h"
25 #include "util/angband-files.h"
26 #include "util/string-processor.h"
27 #include "wizard/wizard-spoiler.h"
30 * Available graphic modes
32 #define GRAPHICS_NONE 0
33 #define GRAPHICS_ORIGINAL 1
34 #define GRAPHICS_ADAM_BOLT 2
35 #define GRAPHICS_HENGBAND 3
38 * Some machines have a "main()" function in their "main-xxx.c" file,
39 * all the others use this file for their "main()" function.
44 * A hook for "quit()".
46 * Close down, then fall back into "quit()".
48 static void quit_hook(concptr s)
56 for (j = 8 - 1; j >= 0; j--) {
62 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 */
98 * Initialize and verify the file paths, and the score file.
100 * Use the ANGBAND_PATH environment var if possible, else use
101 * DEFAULT_(LIB|VAR)_PATH, and in either case, branch off
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_(LIB|VAR)_PATH constants. So be sure
107 * that one of 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)
119 char libpath[1024], varpath[1024];
123 /* Get the environment variable */
124 tail = getenv("ANGBAND_PATH");
126 /* Use the angband_path, or a default */
127 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
128 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
130 /* Make sure they're terminated */
134 /* Hack -- Add a path separator (only if needed) */
135 if (!suffix(libpath, PATH_SEP))
136 strcat(libpath, PATH_SEP);
137 if (!suffix(varpath, PATH_SEP))
138 strcat(varpath, PATH_SEP);
141 init_file_paths(libpath, varpath);
145 * Handle a "-d<what>=<path>" option
147 * The "<what>" can be any string starting with the same letter as the
148 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
150 * The "<path>" can be any legal path for the given system, and should
151 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
153 static void change_path(concptr info)
157 /* Find equal sign */
158 s = angband_strchr(info, '=');
160 /* Verify equal sign */
162 quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
165 switch (tolower(info[0])) {
167 string_free(ANGBAND_DIR_APEX);
168 ANGBAND_DIR_APEX = string_make(s + 1);
173 string_free(ANGBAND_DIR_FILE);
174 ANGBAND_DIR_FILE = string_make(s + 1);
179 string_free(ANGBAND_DIR_HELP);
180 ANGBAND_DIR_HELP = string_make(s + 1);
185 string_free(ANGBAND_DIR_INFO);
186 ANGBAND_DIR_INFO = string_make(s + 1);
191 string_free(ANGBAND_DIR_USER);
192 ANGBAND_DIR_USER = string_make(s + 1);
197 string_free(ANGBAND_DIR_XTRA);
198 ANGBAND_DIR_XTRA = string_make(s + 1);
203 string_free(ANGBAND_DIR_BONE);
204 ANGBAND_DIR_BONE = string_make(s + 1);
209 string_free(ANGBAND_DIR_DATA);
210 ANGBAND_DIR_DATA = string_make(s + 1);
215 string_free(ANGBAND_DIR_EDIT);
216 ANGBAND_DIR_EDIT = string_make(s + 1);
221 string_free(ANGBAND_DIR_SAVE);
222 ANGBAND_DIR_SAVE = string_make(s + 1);
227 string_free(ANGBAND_DIR_SCRIPT);
228 ANGBAND_DIR_SCRIPT = string_make(s + 1);
233 quit_fmt("Bad semantics in '-d%s'", info);
238 static void display_usage(const char *program)
240 /* Dump usage information */
241 printf("Usage: %s [options] [-- subopts]\n", program);
242 puts(" -n Start a new character");
243 puts(" -f Request fiddle mode");
244 puts(" -w Request wizard mode");
245 puts(" -b Request BGM mode");
246 puts(" -v Request sound mode");
247 puts(" -g Request graphics mode");
248 puts(" -o Request original keyset");
249 puts(" -r Request rogue-like keyset");
250 puts(" -M Request monochrome mode");
251 puts(" -s<num> Show <num> high scores");
252 puts(" -u<who> Use your <who> savefile");
253 puts(" -m<sys> Force 'main-<sys>.c' usage");
254 puts(" -d<def> Define a 'lib' dir sub-path");
255 puts(" --output-spoilers");
256 puts(" Output auto generated spoilers and exit");
260 puts(" -mx11 To use X11");
261 puts(" -- Sub options");
262 puts(" -- -d Set display name");
263 puts(" -- -o Request old 8x8 tile graphics");
264 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
265 puts(" -- -b Request Bigtile graphics mode");
266 puts(" -- -s Turn off smoothscaling graphics");
267 puts(" -- -n# Number of terms to use");
272 puts(" -mgcu To use GCU (GNU Curses)");
276 puts(" -mcap To use CAP (\"Termcap\" calls)");
279 /* Actually abort the process */
283 static bool parse_long_opt(const char *opt)
285 bool is_usage_needed = TRUE;
287 if (strcmp(opt + 2, "output-spoilers") == 0) {
289 init_angband(p_ptr, TRUE);
290 switch (output_all_spoilers()) {
291 case SPOILER_OUTPUT_SUCCESS:
292 puts("Successfully created a spoiler file.");
295 case SPOILER_OUTPUT_FAIL_FOPEN:
296 quit("Cannot create spoiler file.");
298 case SPOILER_OUTPUT_FAIL_FCLOSE:
299 quit("Cannot close spoiler file.");
304 is_usage_needed = FALSE;
307 return is_usage_needed;
311 * Simple "main" function for multiple platforms.
313 * Note the special "--" option which terminates the processing of
314 * standard options. All non-standard options (if any) are passed
315 * directly to the "init_xxx()" function.
317 int main(int argc, char *argv[])
322 bool new_game = FALSE;
327 /* Save the "program name" XXX XXX XXX */
332 /* Default permissions on files */
337 /* Get the file paths */
342 /* Get the user id (?) */
343 p_ptr->player_uid = getuid();
346 /* Mega-Hack -- Factor group id */
347 p_ptr->player_uid += (getgid() * 1000);
352 #ifdef _POSIX_SAVED_IDS
354 /* Save some info for later */
355 p_ptr->player_euid = geteuid();
356 p_ptr->player_egid = getegid();
364 /* Drop permissions */
369 /* Acquire the "user name" as a default player name */
370 user_name(p_ptr->name, p_ptr->player_uid);
372 #ifdef PRIVATE_USER_PATH
374 /* Create a directory for the users files. */
377 #endif /* PRIVATE_USER_PATH */
381 /* Process the command line arguments */
382 bool browsing_movie = FALSE;
383 for (i = 1; args && (i < argc); i++) {
384 /* Require proper options */
385 if (argv[i][0] != '-') {
386 display_usage(argv[0]);
391 bool is_usage_needed = FALSE;
392 switch (argv[i][1]) {
420 /* HACK - Graphics mode switches on the original tiles */
421 arg_graphics = GRAPHICS_ORIGINAL;
426 arg_force_roguelike = TRUE;
431 arg_force_original = TRUE;
436 show_score = atoi(&argv[i][2]);
444 is_usage_needed = TRUE;
448 strcpy(p_ptr->name, &argv[i][2]);
453 is_usage_needed = TRUE;
461 arg_monochrome = TRUE;
466 change_path(&argv[i][2]);
471 is_usage_needed = TRUE;
475 prepare_browse_movie_with_path_build(&argv[i][2]);
476 browsing_movie = TRUE;
480 if (argv[i][2] == '\0') {
486 is_usage_needed = parse_long_opt(argv[i]);
491 is_usage_needed = TRUE;
496 if (!is_usage_needed)
499 display_usage(argv[0]);
502 /* Hack -- Forget standard args */
508 /* Process the player name */
509 process_player_name(p_ptr, TRUE);
511 /* Install "quit" hook */
512 quit_aux = quit_hook;
515 /* Attempt to use the "main-xaw.c" support */
516 if (!done && (!mstr || (streq(mstr, "xaw")))) {
517 extern errr init_xaw(int, char **);
518 if (0 == init_xaw(argc, argv)) {
526 /* Attempt to use the "main-x11.c" support */
527 if (!done && (!mstr || (streq(mstr, "x11")))) {
528 extern errr init_x11(int, char **);
529 if (0 == init_x11(argc, argv)) {
537 /* Attempt to use the "main-gcu.c" support */
538 if (!done && (!mstr || (streq(mstr, "gcu")))) {
539 extern errr init_gcu(int, char **);
540 if (0 == init_gcu(argc, argv)) {
548 /* Attempt to use the "main-cap.c" support */
549 if (!done && (!mstr || (streq(mstr, "cap")))) {
550 extern errr init_cap(int, char **);
551 if (0 == init_cap(argc, argv)) {
558 /* Make sure we have a display! */
560 quit("Unable to prepare any 'display module'!");
562 /* Hack -- If requested, display scores and quit */
564 display_scores(0, show_score);
566 /* Catch nasty signals */
570 init_angband(p_ptr, FALSE);
572 /* Wait for response */
576 play_game(p_ptr, new_game, browsing_movie);