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_(LIB|VAR)_PATH, and in either case, branch off
54 * First, we'll look for the ANGBAND_PATH environment variable,
55 * and then look for the files in there. If that doesn't work,
56 * we'll try the DEFAULT_(LIB|VAR)_PATH constants. So be sure
57 * that one of these two things works...
59 * We must ensure that the path ends with "PATH_SEP" if needed,
60 * since the "init_file_paths()" function will simply append the
61 * relevant "sub-directory names" to the given path.
63 * Make sure that the path doesn't overflow the buffer. We have
64 * to leave enough space for the path separator, directory, and
67 static void init_stuff(void)
69 char libpath[1024], varpath[1024];
73 /* Get the environment variable */
74 tail = getenv("ANGBAND_PATH");
76 /* Use the angband_path, or a default */
77 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
78 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
80 /* Make sure they're terminated */
84 /* Hack -- Add a path separator (only if needed) */
85 if (!suffix(libpath, PATH_SEP)) strcat(libpath, PATH_SEP);
86 if (!suffix(varpath, PATH_SEP)) strcat(varpath, PATH_SEP);
89 init_file_paths(libpath, varpath);
95 * Handle a "-d<what>=<path>" option
97 * The "<what>" can be any string starting with the same letter as the
98 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
100 * The "<path>" can be any legal path for the given system, and should
101 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
103 static void change_path(concptr info)
107 /* Find equal sign */
108 s = my_strchr(info, '=');
110 /* Verify equal sign */
111 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
114 switch (tolower(info[0]))
118 string_free(ANGBAND_DIR_APEX);
119 ANGBAND_DIR_APEX = string_make(s+1);
125 string_free(ANGBAND_DIR_FILE);
126 ANGBAND_DIR_FILE = string_make(s+1);
132 string_free(ANGBAND_DIR_HELP);
133 ANGBAND_DIR_HELP = string_make(s+1);
139 string_free(ANGBAND_DIR_INFO);
140 ANGBAND_DIR_INFO = string_make(s+1);
146 string_free(ANGBAND_DIR_USER);
147 ANGBAND_DIR_USER = string_make(s+1);
153 string_free(ANGBAND_DIR_XTRA);
154 ANGBAND_DIR_XTRA = string_make(s+1);
158 #ifdef VERIFY_SAVEFILE
165 quit_fmt("Restricted option '-d%s'", info);
168 #else /* VERIFY_SAVEFILE */
172 string_free(ANGBAND_DIR_BONE);
173 ANGBAND_DIR_BONE = string_make(s+1);
179 string_free(ANGBAND_DIR_DATA);
180 ANGBAND_DIR_DATA = string_make(s+1);
186 string_free(ANGBAND_DIR_EDIT);
187 ANGBAND_DIR_EDIT = string_make(s+1);
193 string_free(ANGBAND_DIR_SAVE);
194 ANGBAND_DIR_SAVE = string_make(s+1);
200 string_free(ANGBAND_DIR_SCRIPT);
201 ANGBAND_DIR_SCRIPT = string_make(s+1);
205 #endif /* VERIFY_SAVEFILE */
209 quit_fmt("Bad semantics in '-d%s'", info);
216 * Simple "main" function for multiple platforms.
218 * Note the special "--" option which terminates the processing of
219 * standard options. All non-standard options (if any) are passed
220 * directly to the "init_xxx()" function.
222 int main(int argc, char *argv[])
227 bool new_game = FALSE;
232 /* Save the "program name" XXX XXX XXX */
237 /* Default permissions on files */
248 /* Get the file paths */
254 /* Get the user id (?) */
255 player_uid = getuid();
258 /* Mega-Hack -- Factor group id */
259 player_uid += (getgid() * 1000);
264 # ifdef _POSIX_SAVED_IDS
266 /* Save some info for later */
267 player_euid = geteuid();
268 player_egid = getegid();
272 # if 0 /* XXX XXX XXX */
274 /* Redundant setting necessary in case root is running the game */
275 /* If not root or game not setuid the following two calls do nothing */
277 if (setgid(getegid()) != 0)
279 quit("setgid(): cannot set permissions correctly!");
282 if (setuid(geteuid()) != 0)
284 quit("setuid(): cannot set permissions correctly!");
294 /* Drop permissions */
300 /* Initialize the "time" checker */
301 if (check_time_init() || check_time())
303 quit("The gates to Angband are closed (bad time).");
306 /* Initialize the "load" checker */
307 if (check_load_init() || check_load())
309 quit("The gates to Angband are closed (bad load).");
312 /* Acquire the "user name" as a default player name */
314 user_name(p_ptr->name, player_uid);
315 #else /* ANGBAND_2_8_1 */
316 user_name(op_ptr->full_name, player_uid);
317 #endif /* ANGBAND_2_8_1 */
319 #ifdef PRIVATE_USER_PATH
321 /* Create directories for the user's files; handled by init2.c. */
322 create_needed_dirs();
324 #endif /* PRIVATE_USER_PATH */
329 /* Process the command line arguments */
330 for (i = 1; args && (i < argc); i++)
332 /* Require proper options */
333 if (argv[i][0] != '-') goto usage;
376 /* HACK - Graphics mode switches on the original tiles */
377 arg_graphics = GRAPHICS_ORIGINAL;
384 arg_force_roguelike = TRUE;
391 arg_force_original = TRUE;
398 show_score = atoi(&argv[i][2]);
399 if (show_score <= 0) show_score = 10;
406 if (!argv[i][2]) goto usage;
408 strcpy(p_ptr->name, &argv[i][2]);
409 #else /* ANGBAND_2_8_1 */
411 /* Get the savefile name */
412 strncpy(op_ptr->full_name, &argv[i][2], 32);
414 /* Make sure it's terminated */
415 op_ptr->full_name[31] = '\0';
417 #endif /* ANGBAND_2_8_1 */
423 if (!argv[i][2]) goto usage;
430 arg_monochrome = TRUE;
437 change_path(&argv[i][2]);
445 if (!argv[i][2]) goto usage;
446 chuukei_server = TRUE;
447 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
454 if (!argv[i][2]) goto usage;
455 chuukei_client = TRUE;
456 connect_chuukei_server(&argv[i][2]);
463 if (!argv[i][2]) goto usage;
464 prepare_browse_movie(&argv[i][2]);
480 /* Dump usage information */
481 puts("Usage: angband [options] [-- subopts]");
482 puts(" -n Start a new character");
483 puts(" -f Request fiddle mode");
484 puts(" -w Request wizard mode");
485 puts(" -b Request BGM mode");
486 puts(" -v Request sound mode");
487 puts(" -g Request graphics mode");
488 puts(" -o Request original keyset");
489 puts(" -r Request rogue-like keyset");
490 puts(" -M Request monochrome mode");
491 puts(" -s<num> Show <num> high scores");
492 puts(" -u<who> Use your <who> savefile");
493 puts(" -m<sys> Force 'main-<sys>.c' usage");
494 puts(" -d<def> Define a 'lib' dir sub-path");
498 puts(" -mx11 To use X11");
499 puts(" -- Sub options");
500 puts(" -- -d Set display name");
501 puts(" -- -o Request old 8x8 tile graphics");
502 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
503 puts(" -- -b Request Bigtile graphics mode");
504 puts(" -- -s Turn off smoothscaling graphics");
505 puts(" -- -n# Number of terms to use");
510 puts(" -mgcu To use GCU (GNU Curses)");
514 puts(" -mcap To use CAP (\"Termcap\" calls)");
518 puts(" -mdos To use DOS (Graphics)");
522 puts(" -mibm To use IBM (BIOS text mode)");
525 /* Actually abort the process */
531 /* Hack -- Forget standard args */
539 /* Process the player name */
540 process_player_name(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))
603 /* Attempt to use the "main-dos.c" support */
604 if (!done && (!mstr || (streq(mstr, "dos"))))
606 extern errr init_dos(void);
616 /* Attempt to use the "main-ibm.c" support */
617 if (!done && (!mstr || (streq(mstr, "ibm"))))
619 extern errr init_ibm(void);
629 /* Make sure we have a display! */
630 if (!done) quit("Unable to prepare any 'display module'!");
633 /* Hack -- If requested, display scores and quit */
634 if (show_score > 0) display_scores(0, show_score);
637 /* Catch nasty signals */
643 /* Wait for response */