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)
24 * A hook for "quit()".
26 * Close down, then fall back into "quit()".
28 static void quit_hook(cptr s)
36 for (j = 8 - 1; j >= 0; j--)
39 if (!angband_term[j]) continue;
42 term_nuke(angband_term[j]);
48 * Set the stack size and overlay buffer (see main-286.c")
50 #ifdef PRIVATE_USER_PATH
53 * Create an ".angband/" directory in the users home directory.
55 * ToDo: Add error handling.
56 * ToDo: Only create the directories when actually writing files.
58 static void create_user_dir(void)
61 char subdirpath[1024];
63 /* Get an absolute path from the filename */
64 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
66 /* Create the ~/.angband/ directory */
69 /* Build the path to the variant-specific sub-directory */
70 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
72 /* Create the directory */
73 mkdir(subdirpath, 0700);
76 #endif /* PRIVATE_USER_PATH */
80 * Initialize and verify the file paths, and the score file.
82 * Use the ANGBAND_PATH environment var if possible, else use
83 * DEFAULT_PATH, and in either case, branch off appropriately.
85 * First, we'll look for the ANGBAND_PATH environment variable,
86 * and then look for the files in there. If that doesn't work,
87 * we'll try the DEFAULT_PATH constant. So be sure that one of
88 * these two things works...
90 * We must ensure that the path ends with "PATH_SEP" if needed,
91 * since the "init_file_paths()" function will simply append the
92 * relevant "sub-directory names" to the given path.
94 * Make sure that the path doesn't overflow the buffer. We have
95 * to leave enough space for the path separator, directory, and
98 static void init_stuff(void)
104 /* Get the environment variable */
105 tail = getenv("ANGBAND_PATH");
107 /* Use the angband_path, or a default */
108 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
110 /* Make sure it's terminated */
113 /* Hack -- Add a path separator (only if needed) */
114 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
117 init_file_paths(path);
123 * Handle a "-d<what>=<path>" option
125 * The "<what>" can be any string starting with the same letter as the
126 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
128 * The "<path>" can be any legal path for the given system, and should
129 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
131 static void change_path(cptr info)
135 /* Find equal sign */
136 s = my_strchr(info, '=');
138 /* Verify equal sign */
139 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
142 switch (tolower(info[0]))
146 string_free(ANGBAND_DIR_APEX);
147 ANGBAND_DIR_APEX = string_make(s+1);
153 string_free(ANGBAND_DIR_FILE);
154 ANGBAND_DIR_FILE = string_make(s+1);
160 string_free(ANGBAND_DIR_HELP);
161 ANGBAND_DIR_HELP = string_make(s+1);
167 string_free(ANGBAND_DIR_INFO);
168 ANGBAND_DIR_INFO = string_make(s+1);
174 string_free(ANGBAND_DIR_USER);
175 ANGBAND_DIR_USER = string_make(s+1);
181 string_free(ANGBAND_DIR_XTRA);
182 ANGBAND_DIR_XTRA = string_make(s+1);
186 #ifdef VERIFY_SAVEFILE
193 quit_fmt("Restricted option '-d%s'", info);
196 #else /* VERIFY_SAVEFILE */
200 string_free(ANGBAND_DIR_BONE);
201 ANGBAND_DIR_BONE = string_make(s+1);
207 string_free(ANGBAND_DIR_DATA);
208 ANGBAND_DIR_DATA = string_make(s+1);
214 string_free(ANGBAND_DIR_EDIT);
215 ANGBAND_DIR_EDIT = string_make(s+1);
221 string_free(ANGBAND_DIR_SAVE);
222 ANGBAND_DIR_SAVE = string_make(s+1);
228 string_free(ANGBAND_DIR_SCRIPT);
229 ANGBAND_DIR_SCRIPT = string_make(s+1);
233 #endif /* VERIFY_SAVEFILE */
237 quit_fmt("Bad semantics in '-d%s'", info);
244 * Simple "main" function for multiple platforms.
246 * Note the special "--" option which terminates the processing of
247 * standard options. All non-standard options (if any) are passed
248 * directly to the "init_xxx()" function.
250 int main(int argc, char *argv[])
255 bool new_game = FALSE;
260 /* Save the "program name" XXX XXX XXX */
265 /* Default permissions on files */
276 /* Get the file paths */
282 /* Get the user id (?) */
283 player_uid = getuid();
286 /* Mega-Hack -- Factor group id */
287 player_uid += (getgid() * 1000);
292 # ifdef _POSIX_SAVED_IDS
294 /* Save some info for later */
295 player_euid = geteuid();
296 player_egid = getegid();
300 # if 0 /* XXX XXX XXX */
302 /* Redundant setting necessary in case root is running the game */
303 /* If not root or game not setuid the following two calls do nothing */
305 if (setgid(getegid()) != 0)
307 quit("setgid(): cannot set permissions correctly!");
310 if (setuid(geteuid()) != 0)
312 quit("setuid(): cannot set permissions correctly!");
322 /* Drop permissions */
328 /* Initialize the "time" checker */
329 if (check_time_init() || check_time())
331 quit("The gates to Angband are closed (bad time).");
334 /* Initialize the "load" checker */
335 if (check_load_init() || check_load())
337 quit("The gates to Angband are closed (bad load).");
340 /* Acquire the "user name" as a default player name */
342 user_name(p_ptr->name, player_uid);
343 #else /* ANGBAND_2_8_1 */
344 user_name(op_ptr->full_name, player_uid);
345 #endif /* ANGBAND_2_8_1 */
347 #ifdef PRIVATE_USER_PATH
349 /* Create a directory for the users files. */
352 #endif /* PRIVATE_USER_PATH */
357 /* Process the command line arguments */
358 for (i = 1; args && (i < argc); i++)
360 /* Require proper options */
361 if (argv[i][0] != '-') goto usage;
404 /* HACK - Graphics mode switches on the original tiles */
405 arg_graphics = GRAPHICS_ORIGINAL;
412 arg_force_roguelike = TRUE;
419 arg_force_original = TRUE;
426 show_score = atoi(&argv[i][2]);
427 if (show_score <= 0) show_score = 10;
434 if (!argv[i][2]) goto usage;
436 strcpy(p_ptr->name, &argv[i][2]);
437 #else /* ANGBAND_2_8_1 */
439 /* Get the savefile name */
440 strncpy(op_ptr->full_name, &argv[i][2], 32);
442 /* Make sure it's terminated */
443 op_ptr->full_name[31] = '\0';
445 #endif /* ANGBAND_2_8_1 */
451 if (!argv[i][2]) goto usage;
458 arg_monochrome = TRUE;
465 change_path(&argv[i][2]);
473 if (!argv[i][2]) goto usage;
474 chuukei_server = TRUE;
475 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
482 if (!argv[i][2]) goto usage;
483 chuukei_client = TRUE;
484 connect_chuukei_server(&argv[i][2]);
491 if (!argv[i][2]) goto usage;
492 prepare_browse_movie(&argv[i][2]);
508 /* Dump usage information */
509 puts("Usage: angband [options] [-- subopts]");
510 puts(" -n Start a new character");
511 puts(" -f Request fiddle mode");
512 puts(" -w Request wizard mode");
513 puts(" -b Request BGM mode");
514 puts(" -v Request sound mode");
515 puts(" -g Request graphics mode");
516 puts(" -o Request original keyset");
517 puts(" -r Request rogue-like keyset");
518 puts(" -M Request monochrome mode");
519 puts(" -s<num> Show <num> high scores");
520 puts(" -u<who> Use your <who> savefile");
521 puts(" -m<sys> Force 'main-<sys>.c' usage");
522 puts(" -d<def> Define a 'lib' dir sub-path");
526 puts(" -mx11 To use X11");
527 puts(" -- Sub options");
528 puts(" -- -d Set display name");
529 puts(" -- -o Request old 8x8 tile graphics");
530 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
531 puts(" -- -b Request Bigtile graphics mode");
532 puts(" -- -s Turn off smoothscaling graphics");
533 puts(" -- -n# Number of terms to use");
538 puts(" -mgcu To use GCU (GNU Curses)");
542 puts(" -mcap To use CAP (\"Termcap\" calls)");
546 puts(" -mdos To use DOS (Graphics)");
550 puts(" -mibm To use IBM (BIOS text mode)");
553 /* Actually abort the process */
559 /* Hack -- Forget standard args */
567 /* Process the player name */
568 process_player_name(TRUE);
572 /* Install "quit" hook */
573 quit_aux = quit_hook;
578 /* Attempt to use the "main-xaw.c" support */
579 if (!done && (!mstr || (streq(mstr, "xaw"))))
581 extern errr init_xaw(int, char**);
582 if (0 == init_xaw(argc, argv))
591 /* Attempt to use the "main-x11.c" support */
592 if (!done && (!mstr || (streq(mstr, "x11"))))
594 extern errr init_x11(int, char**);
595 if (0 == init_x11(argc, argv))
604 /* Attempt to use the "main-gcu.c" support */
605 if (!done && (!mstr || (streq(mstr, "gcu"))))
607 extern errr init_gcu(int, char**);
608 if (0 == init_gcu(argc, argv))
617 /* Attempt to use the "main-cap.c" support */
618 if (!done && (!mstr || (streq(mstr, "cap"))))
620 extern errr init_cap(int, char**);
621 if (0 == init_cap(argc, argv))
631 /* Attempt to use the "main-dos.c" support */
632 if (!done && (!mstr || (streq(mstr, "dos"))))
634 extern errr init_dos(void);
644 /* Attempt to use the "main-ibm.c" support */
645 if (!done && (!mstr || (streq(mstr, "ibm"))))
647 extern errr init_ibm(void);
657 /* Make sure we have a display! */
658 if (!done) quit("Unable to prepare any 'display module'!");
661 /* Hack -- If requested, display scores and quit */
662 if (show_score > 0) display_scores(0, show_score);
665 /* Catch nasty signals */
671 /* Wait for response */