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)
33 for (j = 8 - 1; j >= 0; j--)
36 if (!angband_term[j]) continue;
39 term_nuke(angband_term[j]);
46 * Set the stack size (for the Amiga)
50 __near long __stack = 32768L;
55 * Set the stack size and overlay buffer (see main-286.c")
59 extern unsigned _stklen = 32768U;
60 extern unsigned _ovrbuffer = 0x1500;
63 #ifdef PRIVATE_USER_PATH
66 * Create an ".angband/" directory in the users home directory.
68 * ToDo: Add error handling.
69 * ToDo: Only create the directories when actually writing files.
71 static void create_user_dir(void)
74 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, 1024, dirpath, VERSION_NAME);
88 /* Create the directory */
89 mkdir(subdirpath, 0700);
95 #endif /* PRIVATE_USER_PATH */
99 * Initialize and verify the file paths, and the score file.
101 * Use the ANGBAND_PATH environment var if possible, else use
102 * DEFAULT_PATH, and in either case, branch off appropriately.
104 * First, we'll look for the ANGBAND_PATH environment variable,
105 * and then look for the files in there. If that doesn't work,
106 * we'll try the DEFAULT_PATH constant. So be sure that one of
107 * these two things works...
109 * We must ensure that the path ends with "PATH_SEP" if needed,
110 * since the "init_file_paths()" function will simply append the
111 * relevant "sub-directory names" to the given path.
113 * Note that the "path" must be "Angband:" for the Amiga, and it
114 * is ignored for "VM/ESA", so I just combined the two.
116 * Make sure that the path doesn't overflow the buffer. We have
117 * to leave enough space for the path separator, directory, and
120 static void init_stuff(void)
124 #if defined(AMIGA) || defined(VM)
126 /* Hack -- prepare "path" */
127 strcpy(path, "Angband:");
129 #else /* AMIGA / VM */
133 /* Get the environment variable */
134 tail = getenv("ANGBAND_PATH");
136 /* Use the angband_path, or a default */
137 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
139 /* Make sure it's terminated */
142 /* Hack -- Add a path separator (only if needed) */
143 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
145 #endif /* AMIGA / VM */
148 init_file_paths(path);
154 * Handle a "-d<what>=<path>" option
156 * The "<what>" can be any string starting with the same letter as the
157 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
159 * The "<path>" can be any legal path for the given system, and should
160 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
162 static void change_path(cptr info)
166 /* Find equal sign */
167 s = strchr(info, '=');
169 /* Verify equal sign */
170 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
173 switch (tolower(info[0]))
177 string_free(ANGBAND_DIR_APEX);
178 ANGBAND_DIR_APEX = string_make(s+1);
184 string_free(ANGBAND_DIR_FILE);
185 ANGBAND_DIR_FILE = string_make(s+1);
191 string_free(ANGBAND_DIR_HELP);
192 ANGBAND_DIR_HELP = string_make(s+1);
198 string_free(ANGBAND_DIR_INFO);
199 ANGBAND_DIR_INFO = string_make(s+1);
205 string_free(ANGBAND_DIR_USER);
206 ANGBAND_DIR_USER = string_make(s+1);
212 string_free(ANGBAND_DIR_XTRA);
213 ANGBAND_DIR_XTRA = string_make(s+1);
217 #ifdef VERIFY_SAVEFILE
224 quit_fmt("Restricted option '-d%s'", info);
227 #else /* VERIFY_SAVEFILE */
231 string_free(ANGBAND_DIR_BONE);
232 ANGBAND_DIR_BONE = string_make(s+1);
238 string_free(ANGBAND_DIR_DATA);
239 ANGBAND_DIR_DATA = string_make(s+1);
245 string_free(ANGBAND_DIR_EDIT);
246 ANGBAND_DIR_EDIT = string_make(s+1);
252 string_free(ANGBAND_DIR_SAVE);
253 ANGBAND_DIR_SAVE = string_make(s+1);
259 string_free(ANGBAND_DIR_SCRIPT);
260 ANGBAND_DIR_SCRIPT = string_make(s+1);
264 #endif /* VERIFY_SAVEFILE */
268 quit_fmt("Bad semantics in '-d%s'", info);
275 * Simple "main" function for multiple platforms.
277 * Note the special "--" option which terminates the processing of
278 * standard options. All non-standard options (if any) are passed
279 * directly to the "init_xxx()" function.
281 int main(int argc, char *argv[])
287 bool new_game = FALSE;
296 /* Save the "program name" XXX XXX XXX */
300 /* Attempt to use XMS (or EMS) memory for swap space */
301 if (_OvrInitExt(0L, 0L))
303 _OvrInitEms(0, 0, 64);
310 /* Default permissions on files */
321 /* Get the file paths */
327 /* Get the user id (?) */
328 player_uid = getuid();
331 /* Mega-Hack -- Factor group id */
332 player_uid += (getgid() * 1000);
337 # ifdef _POSIX_SAVED_IDS
339 /* Save some info for later */
340 player_euid = geteuid();
341 player_egid = getegid();
345 # if 0 /* XXX XXX XXX */
347 /* Redundant setting necessary in case root is running the game */
348 /* If not root or game not setuid the following two calls do nothing */
350 if (setgid(getegid()) != 0)
352 quit("setgid(): cannot set permissions correctly!");
355 if (setuid(geteuid()) != 0)
357 quit("setuid(): cannot set permissions correctly!");
369 /* Initialize the "time" checker */
370 if (check_time_init() || check_time())
372 quit("The gates to Angband are closed (bad time).");
375 /* Initialize the "load" checker */
376 if (check_load_init() || check_load())
378 quit("The gates to Angband are closed (bad load).");
381 /* Acquire the "user name" as a default player name */
383 user_name(player_name, player_uid);
384 #else /* ANGBAND_2_8_1 */
385 user_name(op_ptr->full_name, player_uid);
386 #endif /* ANGBAND_2_8_1 */
388 #ifdef PRIVATE_USER_PATH
390 /* Create a directory for the users files. */
393 #endif /* PRIVATE_USER_PATH */
398 /* Process the command line arguments */
399 for (i = 1; args && (i < argc); i++)
401 /* Require proper options */
402 if (argv[i][0] != '-') goto usage;
438 /* HACK - Graphics mode switches on the original tiles */
439 arg_graphics = GRAPHICS_ORIGINAL;
446 arg_force_roguelike = TRUE;
453 arg_force_original = TRUE;
460 show_score = atoi(&argv[i][2]);
461 if (show_score <= 0) show_score = 10;
468 if (!argv[i][2]) goto usage;
470 strcpy(player_name, &argv[i][2]);
471 #else /* ANGBAND_2_8_1 */
473 /* Get the savefile name */
474 strncpy(op_ptr->full_name, &argv[i][2], 32);
476 /* Make sure it's terminated */
477 op_ptr->full_name[31] = '\0';
479 #endif /* ANGBAND_2_8_1 */
485 if (!argv[i][2]) goto usage;
492 arg_monochrome = TRUE;
499 change_path(&argv[i][2]);
507 if (!argv[i][2]) goto usage;
508 chuukei_server = TRUE;
509 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
516 if (!argv[i][2]) goto usage;
517 chuukei_client = TRUE;
518 connect_chuukei_server(&argv[i][2]);
535 /* Dump usage information */
536 puts("Usage: angband [options] [-- subopts]");
537 puts(" -n Start a new character");
538 puts(" -f Request fiddle mode");
539 puts(" -w Request wizard mode");
540 puts(" -v Request sound mode");
541 puts(" -g Request graphics mode");
542 puts(" -o Request original keyset");
543 puts(" -r Request rogue-like keyset");
544 puts(" -M Request monochrome mode");
545 puts(" -s<num> Show <num> high scores");
546 puts(" -u<who> Use your <who> savefile");
547 puts(" -m<sys> Force 'main-<sys>.c' usage");
548 puts(" -d<def> Define a 'lib' dir sub-path");
552 puts(" -mx11 To use X11");
553 puts(" -- Sub options");
554 puts(" -- -d Set display name");
555 puts(" -- -o Request old 8x8 tile graphics");
556 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
557 puts(" -- -b Request Bigtile graphics mode");
558 puts(" -- -s Turn off smoothscaling graphics");
559 puts(" -- -n# Number of terms to use");
564 puts(" -mgcu To use GCU (GNU Curses)");
568 puts(" -mcap To use CAP (\"Termcap\" calls)");
572 puts(" -mdos To use DOS (Graphics)");
576 puts(" -mibm To use IBM (BIOS text mode)");
580 puts(" -msla To use SLA (SLANG)");
584 puts(" -mlsl To use LSL (Linux-SVGALIB)");
588 puts(" -mami To use AMI (Amiga)");
592 puts(" -mvme To use VME (VAX/ESA)");
595 /* Actually abort the process */
601 /* Hack -- Forget standard args */
609 /* Process the player name */
610 process_player_name(TRUE);
614 /* Install "quit" hook */
615 quit_aux = quit_hook;
618 /* Drop privs (so X11 will work correctly) */
623 /* Attempt to use the "main-xaw.c" support */
624 if (!done && (!mstr || (streq(mstr, "xaw"))))
626 extern errr init_xaw(int, char**);
627 if (0 == init_xaw(argc, argv))
636 /* Attempt to use the "main-x11.c" support */
637 if (!done && (!mstr || (streq(mstr, "x11"))))
639 extern errr init_x11(int, char**);
640 if (0 == init_x11(argc, argv))
649 /* Attempt to use the "main-gcu.c" support */
650 if (!done && (!mstr || (streq(mstr, "gcu"))))
652 extern errr init_gcu(int, char**);
653 if (0 == init_gcu(argc, argv))
662 /* Attempt to use the "main-cap.c" support */
663 if (!done && (!mstr || (streq(mstr, "cap"))))
665 extern errr init_cap(int, char**);
666 if (0 == init_cap(argc, argv))
676 /* Attempt to use the "main-dos.c" support */
677 if (!done && (!mstr || (streq(mstr, "dos"))))
679 extern errr init_dos(void);
689 /* Attempt to use the "main-ibm.c" support */
690 if (!done && (!mstr || (streq(mstr, "ibm"))))
692 extern errr init_ibm(void);
703 /* Attempt to use the "main-emx.c" support */
704 if (!done && (!mstr || (streq(mstr, "emx"))))
706 extern errr init_emx(void);
717 /* Attempt to use the "main-sla.c" support */
718 if (!done && (!mstr || (streq(mstr, "sla"))))
720 extern errr init_sla(void);
731 /* Attempt to use the "main-lsl.c" support */
732 if (!done && (!mstr || (streq(mstr, "lsl"))))
734 extern errr init_lsl(void);
745 /* Attempt to use the "main-ami.c" support */
746 if (!done && (!mstr || (streq(mstr, "ami"))))
748 extern errr init_ami(void);
759 /* Attempt to use the "main-vme.c" support */
760 if (!done && (!mstr || (streq(mstr, "vme"))))
762 extern errr init_vme(void);
772 /* Grab privs (dropped above for X11) */
776 /* Make sure we have a display! */
777 if (!done) quit("Unable to prepare any 'display module'!");
780 /* Hack -- If requested, display scores and quit */
781 if (show_score > 0) display_scores(0, show_score);
784 /* Catch nasty signals */
790 /* Wait for response */