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]);
49 * Set the stack size (for the Amiga)
53 __near long __stack = 32768L;
58 * Set the stack size and overlay buffer (see main-286.c")
62 extern unsigned _stklen = 32768U;
63 extern unsigned _ovrbuffer = 0x1500;
66 #ifdef PRIVATE_USER_PATH
69 * Create an ".angband/" directory in the users home directory.
71 * ToDo: Add error handling.
72 * ToDo: Only create the directories when actually writing files.
74 static void create_user_dir(void)
77 char subdirpath[1024];
79 /* Get an absolute path from the filename */
80 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
82 /* Create the ~/.angband/ directory */
85 /* Build the path to the variant-specific sub-directory */
86 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
88 /* Create the directory */
89 mkdir(subdirpath, 0700);
92 #endif /* PRIVATE_USER_PATH */
96 * Initialize and verify the file paths, and the score file.
98 * Use the ANGBAND_PATH environment var if possible, else use
99 * DEFAULT_PATH, and in either case, branch off appropriately.
101 * First, we'll look for the ANGBAND_PATH environment variable,
102 * and then look for the files in there. If that doesn't work,
103 * we'll try the DEFAULT_PATH constant. So be sure that one of
104 * these two things works...
106 * We must ensure that the path ends with "PATH_SEP" if needed,
107 * since the "init_file_paths()" function will simply append the
108 * relevant "sub-directory names" to the given path.
110 * Note that the "path" must be "Angband:" for the Amiga, and it
111 * is ignored for "VM/ESA", so I just combined the two.
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)
121 #if defined(AMIGA) || defined(VM)
123 /* Hack -- prepare "path" */
124 strcpy(path, "Angband:");
126 #else /* AMIGA / VM */
130 /* Get the environment variable */
131 tail = getenv("ANGBAND_PATH");
133 /* Use the angband_path, or a default */
134 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
136 /* Make sure it's terminated */
139 /* Hack -- Add a path separator (only if needed) */
140 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
142 #endif /* AMIGA / VM */
145 init_file_paths(path);
151 * Handle a "-d<what>=<path>" option
153 * The "<what>" can be any string starting with the same letter as the
154 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
156 * The "<path>" can be any legal path for the given system, and should
157 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
159 static void change_path(cptr info)
163 /* Find equal sign */
164 s = my_strchr(info, '=');
166 /* Verify equal sign */
167 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
170 switch (tolower(info[0]))
174 string_free(ANGBAND_DIR_APEX);
175 ANGBAND_DIR_APEX = string_make(s+1);
181 string_free(ANGBAND_DIR_FILE);
182 ANGBAND_DIR_FILE = string_make(s+1);
188 string_free(ANGBAND_DIR_HELP);
189 ANGBAND_DIR_HELP = string_make(s+1);
195 string_free(ANGBAND_DIR_INFO);
196 ANGBAND_DIR_INFO = string_make(s+1);
202 string_free(ANGBAND_DIR_USER);
203 ANGBAND_DIR_USER = string_make(s+1);
209 string_free(ANGBAND_DIR_XTRA);
210 ANGBAND_DIR_XTRA = string_make(s+1);
214 #ifdef VERIFY_SAVEFILE
221 quit_fmt("Restricted option '-d%s'", info);
224 #else /* VERIFY_SAVEFILE */
228 string_free(ANGBAND_DIR_BONE);
229 ANGBAND_DIR_BONE = string_make(s+1);
235 string_free(ANGBAND_DIR_DATA);
236 ANGBAND_DIR_DATA = string_make(s+1);
242 string_free(ANGBAND_DIR_EDIT);
243 ANGBAND_DIR_EDIT = string_make(s+1);
249 string_free(ANGBAND_DIR_SAVE);
250 ANGBAND_DIR_SAVE = string_make(s+1);
256 string_free(ANGBAND_DIR_SCRIPT);
257 ANGBAND_DIR_SCRIPT = string_make(s+1);
261 #endif /* VERIFY_SAVEFILE */
265 quit_fmt("Bad semantics in '-d%s'", info);
272 * Simple "main" function for multiple platforms.
274 * Note the special "--" option which terminates the processing of
275 * standard options. All non-standard options (if any) are passed
276 * directly to the "init_xxx()" function.
278 int main(int argc, char *argv[])
284 bool new_game = FALSE;
293 /* Save the "program name" XXX XXX XXX */
297 /* Attempt to use XMS (or EMS) memory for swap space */
298 if (_OvrInitExt(0L, 0L))
300 _OvrInitEms(0, 0, 64);
307 /* Default permissions on files */
318 /* Get the file paths */
324 /* Get the user id (?) */
325 player_uid = getuid();
328 /* Mega-Hack -- Factor group id */
329 player_uid += (getgid() * 1000);
334 # ifdef _POSIX_SAVED_IDS
336 /* Save some info for later */
337 player_euid = geteuid();
338 player_egid = getegid();
342 # if 0 /* XXX XXX XXX */
344 /* Redundant setting necessary in case root is running the game */
345 /* If not root or game not setuid the following two calls do nothing */
347 if (setgid(getegid()) != 0)
349 quit("setgid(): cannot set permissions correctly!");
352 if (setuid(geteuid()) != 0)
354 quit("setuid(): cannot set permissions correctly!");
364 /* Drop permissions */
370 /* Initialize the "time" checker */
371 if (check_time_init() || check_time())
373 quit("The gates to Angband are closed (bad time).");
376 /* Initialize the "load" checker */
377 if (check_load_init() || check_load())
379 quit("The gates to Angband are closed (bad load).");
382 /* Acquire the "user name" as a default player name */
384 user_name(player_name, player_uid);
385 #else /* ANGBAND_2_8_1 */
386 user_name(op_ptr->full_name, player_uid);
387 #endif /* ANGBAND_2_8_1 */
389 #ifdef PRIVATE_USER_PATH
391 /* Create a directory for the users files. */
394 #endif /* PRIVATE_USER_PATH */
399 /* Process the command line arguments */
400 for (i = 1; args && (i < argc); i++)
402 /* Require proper options */
403 if (argv[i][0] != '-') goto usage;
446 /* HACK - Graphics mode switches on the original tiles */
447 arg_graphics = GRAPHICS_ORIGINAL;
454 arg_force_roguelike = TRUE;
461 arg_force_original = TRUE;
468 show_score = atoi(&argv[i][2]);
469 if (show_score <= 0) show_score = 10;
476 if (!argv[i][2]) goto usage;
478 strcpy(player_name, &argv[i][2]);
479 #else /* ANGBAND_2_8_1 */
481 /* Get the savefile name */
482 strncpy(op_ptr->full_name, &argv[i][2], 32);
484 /* Make sure it's terminated */
485 op_ptr->full_name[31] = '\0';
487 #endif /* ANGBAND_2_8_1 */
493 if (!argv[i][2]) goto usage;
500 arg_monochrome = TRUE;
507 change_path(&argv[i][2]);
515 if (!argv[i][2]) goto usage;
516 chuukei_server = TRUE;
517 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
524 if (!argv[i][2]) goto usage;
525 chuukei_client = TRUE;
526 connect_chuukei_server(&argv[i][2]);
533 if (!argv[i][2]) goto usage;
534 prepare_browse_movie(&argv[i][2]);
550 /* Dump usage information */
551 puts("Usage: angband [options] [-- subopts]");
552 puts(" -n Start a new character");
553 puts(" -f Request fiddle mode");
554 puts(" -w Request wizard mode");
555 puts(" -b Request BGM mode");
556 puts(" -v Request sound mode");
557 puts(" -g Request graphics mode");
558 puts(" -o Request original keyset");
559 puts(" -r Request rogue-like keyset");
560 puts(" -M Request monochrome mode");
561 puts(" -s<num> Show <num> high scores");
562 puts(" -u<who> Use your <who> savefile");
563 puts(" -m<sys> Force 'main-<sys>.c' usage");
564 puts(" -d<def> Define a 'lib' dir sub-path");
568 puts(" -mx11 To use X11");
569 puts(" -- Sub options");
570 puts(" -- -d Set display name");
571 puts(" -- -o Request old 8x8 tile graphics");
572 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
573 puts(" -- -b Request Bigtile graphics mode");
574 puts(" -- -s Turn off smoothscaling graphics");
575 puts(" -- -n# Number of terms to use");
580 puts(" -mgcu To use GCU (GNU Curses)");
584 puts(" -mcap To use CAP (\"Termcap\" calls)");
588 puts(" -mdos To use DOS (Graphics)");
592 puts(" -mibm To use IBM (BIOS text mode)");
596 puts(" -msla To use SLA (SLANG)");
600 puts(" -mlsl To use LSL (Linux-SVGALIB)");
604 puts(" -mami To use AMI (Amiga)");
608 puts(" -mvme To use VME (VAX/ESA)");
611 /* Actually abort the process */
617 /* Hack -- Forget standard args */
625 /* Process the player name */
626 process_player_name(TRUE);
630 /* Install "quit" hook */
631 quit_aux = quit_hook;
636 /* Attempt to use the "main-xaw.c" support */
637 if (!done && (!mstr || (streq(mstr, "xaw"))))
639 extern errr init_xaw(int, char**);
640 if (0 == init_xaw(argc, argv))
649 /* Attempt to use the "main-x11.c" support */
650 if (!done && (!mstr || (streq(mstr, "x11"))))
652 extern errr init_x11(int, char**);
653 if (0 == init_x11(argc, argv))
662 /* Attempt to use the "main-gcu.c" support */
663 if (!done && (!mstr || (streq(mstr, "gcu"))))
665 extern errr init_gcu(int, char**);
666 if (0 == init_gcu(argc, argv))
675 /* Attempt to use the "main-cap.c" support */
676 if (!done && (!mstr || (streq(mstr, "cap"))))
678 extern errr init_cap(int, char**);
679 if (0 == init_cap(argc, argv))
689 /* Attempt to use the "main-dos.c" support */
690 if (!done && (!mstr || (streq(mstr, "dos"))))
692 extern errr init_dos(void);
702 /* Attempt to use the "main-ibm.c" support */
703 if (!done && (!mstr || (streq(mstr, "ibm"))))
705 extern errr init_ibm(void);
716 /* Attempt to use the "main-emx.c" support */
717 if (!done && (!mstr || (streq(mstr, "emx"))))
719 extern errr init_emx(void);
730 /* Attempt to use the "main-sla.c" support */
731 if (!done && (!mstr || (streq(mstr, "sla"))))
733 extern errr init_sla(void);
744 /* Attempt to use the "main-lsl.c" support */
745 if (!done && (!mstr || (streq(mstr, "lsl"))))
747 extern errr init_lsl(void);
758 /* Attempt to use the "main-ami.c" support */
759 if (!done && (!mstr || (streq(mstr, "ami"))))
761 extern errr init_ami(void);
772 /* Attempt to use the "main-vme.c" support */
773 if (!done && (!mstr || (streq(mstr, "vme"))))
775 extern errr init_vme(void);
785 /* Make sure we have a display! */
786 if (!done) quit("Unable to prepare any 'display module'!");
789 /* Hack -- If requested, display scores and quit */
790 if (show_score > 0) display_scores(0, show_score);
793 /* Catch nasty signals */
799 /* Wait for response */