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;
439 /* HACK - Graphics mode switches on the original tiles */
440 arg_graphics = GRAPHICS_ORIGINAL;
447 arg_force_roguelike = TRUE;
454 arg_force_original = TRUE;
461 show_score = atoi(&argv[i][2]);
462 if (show_score <= 0) show_score = 10;
469 if (!argv[i][2]) goto usage;
471 strcpy(player_name, &argv[i][2]);
472 #else /* ANGBAND_2_8_1 */
474 /* Get the savefile name */
475 strncpy(op_ptr->full_name, &argv[i][2], 32);
477 /* Make sure it's terminated */
478 op_ptr->full_name[31] = '\0';
480 #endif /* ANGBAND_2_8_1 */
486 if (!argv[i][2]) goto usage;
493 arg_monochrome = TRUE;
500 change_path(&argv[i][2]);
508 if (!argv[i][2]) goto usage;
509 chuukei_server = TRUE;
510 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
517 if (!argv[i][2]) goto usage;
518 chuukei_client = TRUE;
519 connect_chuukei_server(&argv[i][2]);
526 if (!argv[i][2]) goto usage;
527 prepare_browse_movie(&argv[i][2]);
543 /* Dump usage information */
544 puts("Usage: angband [options] [-- subopts]");
545 puts(" -n Start a new character");
546 puts(" -f Request fiddle mode");
547 puts(" -w Request wizard mode");
548 puts(" -v Request sound mode");
549 puts(" -g Request graphics mode");
550 puts(" -o Request original keyset");
551 puts(" -r Request rogue-like keyset");
552 puts(" -M Request monochrome mode");
553 puts(" -s<num> Show <num> high scores");
554 puts(" -u<who> Use your <who> savefile");
555 puts(" -m<sys> Force 'main-<sys>.c' usage");
556 puts(" -d<def> Define a 'lib' dir sub-path");
560 puts(" -mx11 To use X11");
561 puts(" -- Sub options");
562 puts(" -- -d Set display name");
563 puts(" -- -o Request old 8x8 tile graphics");
564 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
565 puts(" -- -b Request Bigtile graphics mode");
566 puts(" -- -s Turn off smoothscaling graphics");
567 puts(" -- -n# Number of terms to use");
572 puts(" -mgcu To use GCU (GNU Curses)");
576 puts(" -mcap To use CAP (\"Termcap\" calls)");
580 puts(" -mdos To use DOS (Graphics)");
584 puts(" -mibm To use IBM (BIOS text mode)");
588 puts(" -msla To use SLA (SLANG)");
592 puts(" -mlsl To use LSL (Linux-SVGALIB)");
596 puts(" -mami To use AMI (Amiga)");
600 puts(" -mvme To use VME (VAX/ESA)");
603 /* Actually abort the process */
609 /* Hack -- Forget standard args */
617 /* Process the player name */
618 process_player_name(TRUE);
622 /* Install "quit" hook */
623 quit_aux = quit_hook;
628 /* Attempt to use the "main-xaw.c" support */
629 if (!done && (!mstr || (streq(mstr, "xaw"))))
631 extern errr init_xaw(int, char**);
632 if (0 == init_xaw(argc, argv))
641 /* Attempt to use the "main-x11.c" support */
642 if (!done && (!mstr || (streq(mstr, "x11"))))
644 extern errr init_x11(int, char**);
645 if (0 == init_x11(argc, argv))
654 /* Attempt to use the "main-gcu.c" support */
655 if (!done && (!mstr || (streq(mstr, "gcu"))))
657 extern errr init_gcu(int, char**);
658 if (0 == init_gcu(argc, argv))
667 /* Attempt to use the "main-cap.c" support */
668 if (!done && (!mstr || (streq(mstr, "cap"))))
670 extern errr init_cap(int, char**);
671 if (0 == init_cap(argc, argv))
681 /* Attempt to use the "main-dos.c" support */
682 if (!done && (!mstr || (streq(mstr, "dos"))))
684 extern errr init_dos(void);
694 /* Attempt to use the "main-ibm.c" support */
695 if (!done && (!mstr || (streq(mstr, "ibm"))))
697 extern errr init_ibm(void);
708 /* Attempt to use the "main-emx.c" support */
709 if (!done && (!mstr || (streq(mstr, "emx"))))
711 extern errr init_emx(void);
722 /* Attempt to use the "main-sla.c" support */
723 if (!done && (!mstr || (streq(mstr, "sla"))))
725 extern errr init_sla(void);
736 /* Attempt to use the "main-lsl.c" support */
737 if (!done && (!mstr || (streq(mstr, "lsl"))))
739 extern errr init_lsl(void);
750 /* Attempt to use the "main-ami.c" support */
751 if (!done && (!mstr || (streq(mstr, "ami"))))
753 extern errr init_ami(void);
764 /* Attempt to use the "main-vme.c" support */
765 if (!done && (!mstr || (streq(mstr, "vme"))))
767 extern errr init_vme(void);
777 /* Make sure we have a display! */
778 if (!done) quit("Unable to prepare any 'display module'!");
781 /* Hack -- If requested, display scores and quit */
782 if (show_score > 0) display_scores(0, show_score);
785 /* Catch nasty signals */
791 /* Wait for response */