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.
15 * Some machines have a "main()" function in their "main-xxx.c" file,
16 * all the others use this file for their "main()" function.
20 #if !defined(MACINTOSH) && !defined(WINDOWS) && !defined(ACORN) && !defined(MACH_O_CARBON) && !defined(MACH_O_COCOA)
24 * A hook for "quit()".
26 * Close down, then fall back into "quit()".
28 static void quit_hook(concptr s)
36 for (j = 8 - 1; j >= 0; j--)
39 if (!angband_term[j]) continue;
42 term_nuke(angband_term[j]);
48 * Initialize and verify the file paths, and the score file.
50 * Use the ANGBAND_PATH environment var if possible, else use
51 * DEFAULT_PATH, and in either case, branch off appropriately.
53 * First, we'll look for the ANGBAND_PATH environment variable,
54 * and then look for the files in there. If that doesn't work,
55 * we'll try the DEFAULT_PATH constant. So be sure that one of
56 * these two things works...
58 * We must ensure that the path ends with "PATH_SEP" if needed,
59 * since the "init_file_paths()" function will simply append the
60 * relevant "sub-directory names" to the given path.
62 * Make sure that the path doesn't overflow the buffer. We have
63 * to leave enough space for the path separator, directory, and
66 static void init_stuff(void)
72 /* Get the environment variable */
73 tail = getenv("ANGBAND_PATH");
75 /* Use the angband_path, or a default */
76 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
78 /* Make sure it's terminated */
81 /* Hack -- Add a path separator (only if needed) */
82 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
85 init_file_paths(path, path, path);
91 * Handle a "-d<what>=<path>" option
93 * The "<what>" can be any string starting with the same letter as the
94 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
96 * The "<path>" can be any legal path for the given system, and should
97 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
99 static void change_path(concptr info)
103 /* Find equal sign */
104 s = my_strchr(info, '=');
106 /* Verify equal sign */
107 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
110 switch (tolower(info[0]))
114 string_free(ANGBAND_DIR_APEX);
115 ANGBAND_DIR_APEX = string_make(s+1);
121 string_free(ANGBAND_DIR_FILE);
122 ANGBAND_DIR_FILE = string_make(s+1);
128 string_free(ANGBAND_DIR_HELP);
129 ANGBAND_DIR_HELP = string_make(s+1);
135 string_free(ANGBAND_DIR_INFO);
136 ANGBAND_DIR_INFO = string_make(s+1);
142 string_free(ANGBAND_DIR_USER);
143 ANGBAND_DIR_USER = string_make(s+1);
149 string_free(ANGBAND_DIR_XTRA);
150 ANGBAND_DIR_XTRA = string_make(s+1);
154 #ifdef VERIFY_SAVEFILE
161 quit_fmt("Restricted option '-d%s'", info);
164 #else /* VERIFY_SAVEFILE */
168 string_free(ANGBAND_DIR_BONE);
169 ANGBAND_DIR_BONE = string_make(s+1);
175 string_free(ANGBAND_DIR_DATA);
176 ANGBAND_DIR_DATA = string_make(s+1);
182 string_free(ANGBAND_DIR_EDIT);
183 ANGBAND_DIR_EDIT = string_make(s+1);
189 string_free(ANGBAND_DIR_SAVE);
190 ANGBAND_DIR_SAVE = string_make(s+1);
196 string_free(ANGBAND_DIR_SCRIPT);
197 ANGBAND_DIR_SCRIPT = string_make(s+1);
201 #endif /* VERIFY_SAVEFILE */
205 quit_fmt("Bad semantics in '-d%s'", info);
212 * Simple "main" function for multiple platforms.
214 * Note the special "--" option which terminates the processing of
215 * standard options. All non-standard options (if any) are passed
216 * directly to the "init_xxx()" function.
218 int main(int argc, char *argv[])
223 bool new_game = FALSE;
228 /* Save the "program name" XXX XXX XXX */
233 /* Default permissions on files */
244 /* Get the file paths */
250 /* Get the user id (?) */
251 player_uid = getuid();
254 /* Mega-Hack -- Factor group id */
255 player_uid += (getgid() * 1000);
260 # ifdef _POSIX_SAVED_IDS
262 /* Save some info for later */
263 player_euid = geteuid();
264 player_egid = getegid();
268 # if 0 /* XXX XXX XXX */
270 /* Redundant setting necessary in case root is running the game */
271 /* If not root or game not setuid the following two calls do nothing */
273 if (setgid(getegid()) != 0)
275 quit("setgid(): cannot set permissions correctly!");
278 if (setuid(geteuid()) != 0)
280 quit("setuid(): cannot set permissions correctly!");
290 /* Drop permissions */
296 /* Initialize the "time" checker */
297 if (check_time_init() || check_time())
299 quit("The gates to Angband are closed (bad time).");
302 /* Initialize the "load" checker */
303 if (check_load_init() || check_load())
305 quit("The gates to Angband are closed (bad load).");
308 /* Acquire the "user name" as a default player name */
310 user_name(p_ptr->name, player_uid);
311 #else /* ANGBAND_2_8_1 */
312 user_name(op_ptr->full_name, player_uid);
313 #endif /* ANGBAND_2_8_1 */
315 #ifdef PRIVATE_USER_PATH
317 /* Create directories for the user's files; handled by init2.c. */
318 create_needed_dirs();
320 #endif /* PRIVATE_USER_PATH */
325 /* Process the command line arguments */
326 for (i = 1; args && (i < argc); i++)
328 /* Require proper options */
329 if (argv[i][0] != '-') goto usage;
372 /* HACK - Graphics mode switches on the original tiles */
373 arg_graphics = GRAPHICS_ORIGINAL;
380 arg_force_roguelike = TRUE;
387 arg_force_original = TRUE;
394 show_score = atoi(&argv[i][2]);
395 if (show_score <= 0) show_score = 10;
402 if (!argv[i][2]) goto usage;
404 strcpy(p_ptr->name, &argv[i][2]);
405 #else /* ANGBAND_2_8_1 */
407 /* Get the savefile name */
408 strncpy(op_ptr->full_name, &argv[i][2], 32);
410 /* Make sure it's terminated */
411 op_ptr->full_name[31] = '\0';
413 #endif /* ANGBAND_2_8_1 */
419 if (!argv[i][2]) goto usage;
426 arg_monochrome = TRUE;
433 change_path(&argv[i][2]);
441 if (!argv[i][2]) goto usage;
442 chuukei_server = TRUE;
443 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
450 if (!argv[i][2]) goto usage;
451 chuukei_client = TRUE;
452 connect_chuukei_server(&argv[i][2]);
459 if (!argv[i][2]) goto usage;
460 prepare_browse_movie(&argv[i][2]);
476 /* Dump usage information */
477 puts("Usage: angband [options] [-- subopts]");
478 puts(" -n Start a new character");
479 puts(" -f Request fiddle mode");
480 puts(" -w Request wizard mode");
481 puts(" -b Request BGM mode");
482 puts(" -v Request sound mode");
483 puts(" -g Request graphics mode");
484 puts(" -o Request original keyset");
485 puts(" -r Request rogue-like keyset");
486 puts(" -M Request monochrome mode");
487 puts(" -s<num> Show <num> high scores");
488 puts(" -u<who> Use your <who> savefile");
489 puts(" -m<sys> Force 'main-<sys>.c' usage");
490 puts(" -d<def> Define a 'lib' dir sub-path");
494 puts(" -mx11 To use X11");
495 puts(" -- Sub options");
496 puts(" -- -d Set display name");
497 puts(" -- -o Request old 8x8 tile graphics");
498 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
499 puts(" -- -b Request Bigtile graphics mode");
500 puts(" -- -s Turn off smoothscaling graphics");
501 puts(" -- -n# Number of terms to use");
506 puts(" -mgcu To use GCU (GNU Curses)");
510 puts(" -mcap To use CAP (\"Termcap\" calls)");
514 puts(" -mdos To use DOS (Graphics)");
518 puts(" -mibm To use IBM (BIOS text mode)");
521 /* Actually abort the process */
527 /* Hack -- Forget standard args */
535 /* Process the player name */
536 process_player_name(TRUE);
540 /* Install "quit" hook */
541 quit_aux = quit_hook;
546 /* Attempt to use the "main-xaw.c" support */
547 if (!done && (!mstr || (streq(mstr, "xaw"))))
549 extern errr init_xaw(int, char**);
550 if (0 == init_xaw(argc, argv))
559 /* Attempt to use the "main-x11.c" support */
560 if (!done && (!mstr || (streq(mstr, "x11"))))
562 extern errr init_x11(int, char**);
563 if (0 == init_x11(argc, argv))
572 /* Attempt to use the "main-gcu.c" support */
573 if (!done && (!mstr || (streq(mstr, "gcu"))))
575 extern errr init_gcu(int, char**);
576 if (0 == init_gcu(argc, argv))
585 /* Attempt to use the "main-cap.c" support */
586 if (!done && (!mstr || (streq(mstr, "cap"))))
588 extern errr init_cap(int, char**);
589 if (0 == init_cap(argc, argv))
599 /* Attempt to use the "main-dos.c" support */
600 if (!done && (!mstr || (streq(mstr, "dos"))))
602 extern errr init_dos(void);
612 /* Attempt to use the "main-ibm.c" support */
613 if (!done && (!mstr || (streq(mstr, "ibm"))))
615 extern errr init_ibm(void);
625 /* Make sure we have a display! */
626 if (!done) quit("Unable to prepare any 'display module'!");
629 /* Hack -- If requested, display scores and quit */
630 if (show_score > 0) display_scores(0, show_score);
633 /* Catch nasty signals */
639 /* Wait for response */