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_(LIB|VAR)_PATH, and in either case, branch off
102 * First, we'll look for the ANGBAND_PATH environment variable,
103 * and then look for the files in there. If that doesn't work,
104 * we'll try the DEFAULT_(LIB|VAR)_PATH constants. So be sure
105 * that one of these two things works...
107 * We must ensure that the path ends with "PATH_SEP" if needed,
108 * since the "init_file_paths()" function will simply append the
109 * relevant "sub-directory names" to the given path.
111 * Note that the "path" must be "Angband:" for the Amiga, and it
112 * is ignored for "VM/ESA", so I just combined the two.
114 * Make sure that the path doesn't overflow the buffer. We have
115 * to leave enough space for the path separator, directory, and
118 static void init_stuff(void)
120 char libpath[1024], varpath[1024];
122 #if defined(AMIGA) || defined(VM)
124 /* Hack -- prepare "path" */
125 strcpy(path, "Angband:");
127 #else /* AMIGA / VM */
131 /* Get the environment variable */
132 tail = getenv("ANGBAND_PATH");
134 /* Use the angband_path, or a default */
135 strncpy(libpath, tail ? tail : DEFAULT_LIB_PATH, 511);
136 strncpy(varpath, tail ? tail : DEFAULT_VAR_PATH, 511);
138 /* Make sure they're terminated */
142 /* Hack -- Add a path separator (only if needed) */
143 if (!suffix(libpath, PATH_SEP)) strcat(libpath, PATH_SEP);
144 if (!suffix(varpath, PATH_SEP)) strcat(varpath, PATH_SEP);
146 #endif /* AMIGA / VM */
149 init_file_paths(libpath, varpath);
155 * Handle a "-d<what>=<path>" option
157 * The "<what>" can be any string starting with the same letter as the
158 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
160 * The "<path>" can be any legal path for the given system, and should
161 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
163 static void change_path(cptr info)
167 /* Find equal sign */
168 s = strchr(info, '=');
170 /* Verify equal sign */
171 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
174 switch (tolower(info[0]))
178 string_free(ANGBAND_DIR_APEX);
179 ANGBAND_DIR_APEX = string_make(s+1);
185 string_free(ANGBAND_DIR_FILE);
186 ANGBAND_DIR_FILE = string_make(s+1);
192 string_free(ANGBAND_DIR_HELP);
193 ANGBAND_DIR_HELP = string_make(s+1);
199 string_free(ANGBAND_DIR_INFO);
200 ANGBAND_DIR_INFO = string_make(s+1);
206 string_free(ANGBAND_DIR_USER);
207 ANGBAND_DIR_USER = string_make(s+1);
213 string_free(ANGBAND_DIR_XTRA);
214 ANGBAND_DIR_XTRA = string_make(s+1);
218 #ifdef VERIFY_SAVEFILE
225 quit_fmt("Restricted option '-d%s'", info);
228 #else /* VERIFY_SAVEFILE */
232 string_free(ANGBAND_DIR_BONE);
233 ANGBAND_DIR_BONE = string_make(s+1);
239 string_free(ANGBAND_DIR_DATA);
240 ANGBAND_DIR_DATA = string_make(s+1);
246 string_free(ANGBAND_DIR_EDIT);
247 ANGBAND_DIR_EDIT = string_make(s+1);
253 string_free(ANGBAND_DIR_SAVE);
254 ANGBAND_DIR_SAVE = string_make(s+1);
260 string_free(ANGBAND_DIR_SCRIPT);
261 ANGBAND_DIR_SCRIPT = string_make(s+1);
265 #endif /* VERIFY_SAVEFILE */
269 quit_fmt("Bad semantics in '-d%s'", info);
276 * Simple "main" function for multiple platforms.
278 * Note the special "--" option which terminates the processing of
279 * standard options. All non-standard options (if any) are passed
280 * directly to the "init_xxx()" function.
282 int main(int argc, char *argv[])
288 bool new_game = FALSE;
297 /* Save the "program name" XXX XXX XXX */
301 /* Attempt to use XMS (or EMS) memory for swap space */
302 if (_OvrInitExt(0L, 0L))
304 _OvrInitEms(0, 0, 64);
311 /* Default permissions on files */
322 /* Get the file paths */
328 /* Get the user id (?) */
329 player_uid = getuid();
332 /* Mega-Hack -- Factor group id */
333 player_uid += (getgid() * 1000);
338 # ifdef _POSIX_SAVED_IDS
340 /* Save some info for later */
341 player_euid = geteuid();
342 player_egid = getegid();
346 # if 0 /* XXX XXX XXX */
348 /* Redundant setting necessary in case root is running the game */
349 /* If not root or game not setuid the following two calls do nothing */
351 if (setgid(getegid()) != 0)
353 quit("setgid(): cannot set permissions correctly!");
356 if (setuid(geteuid()) != 0)
358 quit("setuid(): cannot set permissions correctly!");
368 /* Drop permissions */
374 /* Initialize the "time" checker */
375 if (check_time_init() || check_time())
377 quit("The gates to Angband are closed (bad time).");
380 /* Initialize the "load" checker */
381 if (check_load_init() || check_load())
383 quit("The gates to Angband are closed (bad load).");
386 /* Acquire the "user name" as a default player name */
388 user_name(player_name, player_uid);
389 #else /* ANGBAND_2_8_1 */
390 user_name(op_ptr->full_name, player_uid);
391 #endif /* ANGBAND_2_8_1 */
393 #ifdef PRIVATE_USER_PATH
395 /* Create a directory for the users files. */
398 #endif /* PRIVATE_USER_PATH */
403 /* Process the command line arguments */
404 for (i = 1; args && (i < argc); i++)
406 /* Require proper options */
407 if (argv[i][0] != '-') goto usage;
443 /* HACK - Graphics mode switches on the original tiles */
444 arg_graphics = GRAPHICS_ORIGINAL;
451 arg_force_roguelike = TRUE;
458 arg_force_original = TRUE;
465 show_score = atoi(&argv[i][2]);
466 if (show_score <= 0) show_score = 10;
473 if (!argv[i][2]) goto usage;
475 strcpy(player_name, &argv[i][2]);
476 #else /* ANGBAND_2_8_1 */
478 /* Get the savefile name */
479 strncpy(op_ptr->full_name, &argv[i][2], 32);
481 /* Make sure it's terminated */
482 op_ptr->full_name[31] = '\0';
484 #endif /* ANGBAND_2_8_1 */
490 if (!argv[i][2]) goto usage;
497 arg_monochrome = TRUE;
504 change_path(&argv[i][2]);
512 if (!argv[i][2]) goto usage;
513 chuukei_server = TRUE;
514 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
521 if (!argv[i][2]) goto usage;
522 chuukei_client = TRUE;
523 connect_chuukei_server(&argv[i][2]);
540 /* Dump usage information */
541 puts("Usage: angband [options] [-- subopts]");
542 puts(" -n Start a new character");
543 puts(" -f Request fiddle mode");
544 puts(" -w Request wizard mode");
545 puts(" -v Request sound mode");
546 puts(" -g Request graphics mode");
547 puts(" -o Request original keyset");
548 puts(" -r Request rogue-like keyset");
549 puts(" -M Request monochrome mode");
550 puts(" -s<num> Show <num> high scores");
551 puts(" -u<who> Use your <who> savefile");
552 puts(" -m<sys> Force 'main-<sys>.c' usage");
553 puts(" -d<def> Define a 'lib' dir sub-path");
557 puts(" -mx11 To use X11");
558 puts(" -- Sub options");
559 puts(" -- -d Set display name");
560 puts(" -- -o Request old 8x8 tile graphics");
561 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
562 puts(" -- -b Request Bigtile graphics mode");
563 puts(" -- -s Turn off smoothscaling graphics");
564 puts(" -- -n# Number of terms to use");
569 puts(" -mgcu To use GCU (GNU Curses)");
573 puts(" -mcap To use CAP (\"Termcap\" calls)");
577 puts(" -mdos To use DOS (Graphics)");
581 puts(" -mibm To use IBM (BIOS text mode)");
585 puts(" -msla To use SLA (SLANG)");
589 puts(" -mlsl To use LSL (Linux-SVGALIB)");
593 puts(" -mami To use AMI (Amiga)");
597 puts(" -mvme To use VME (VAX/ESA)");
600 /* Actually abort the process */
606 /* Hack -- Forget standard args */
614 /* Process the player name */
615 process_player_name(TRUE);
619 /* Install "quit" hook */
620 quit_aux = quit_hook;
625 /* Attempt to use the "main-xaw.c" support */
626 if (!done && (!mstr || (streq(mstr, "xaw"))))
628 extern errr init_xaw(int, char**);
629 if (0 == init_xaw(argc, argv))
638 /* Attempt to use the "main-x11.c" support */
639 if (!done && (!mstr || (streq(mstr, "x11"))))
641 extern errr init_x11(int, char**);
642 if (0 == init_x11(argc, argv))
651 /* Attempt to use the "main-gcu.c" support */
652 if (!done && (!mstr || (streq(mstr, "gcu"))))
654 extern errr init_gcu(int, char**);
655 if (0 == init_gcu(argc, argv))
664 /* Attempt to use the "main-cap.c" support */
665 if (!done && (!mstr || (streq(mstr, "cap"))))
667 extern errr init_cap(int, char**);
668 if (0 == init_cap(argc, argv))
678 /* Attempt to use the "main-dos.c" support */
679 if (!done && (!mstr || (streq(mstr, "dos"))))
681 extern errr init_dos(void);
691 /* Attempt to use the "main-ibm.c" support */
692 if (!done && (!mstr || (streq(mstr, "ibm"))))
694 extern errr init_ibm(void);
705 /* Attempt to use the "main-emx.c" support */
706 if (!done && (!mstr || (streq(mstr, "emx"))))
708 extern errr init_emx(void);
719 /* Attempt to use the "main-sla.c" support */
720 if (!done && (!mstr || (streq(mstr, "sla"))))
722 extern errr init_sla(void);
733 /* Attempt to use the "main-lsl.c" support */
734 if (!done && (!mstr || (streq(mstr, "lsl"))))
736 extern errr init_lsl(void);
747 /* Attempt to use the "main-ami.c" support */
748 if (!done && (!mstr || (streq(mstr, "ami"))))
750 extern errr init_ami(void);
761 /* Attempt to use the "main-vme.c" support */
762 if (!done && (!mstr || (streq(mstr, "vme"))))
764 extern errr init_vme(void);
774 /* Make sure we have a display! */
775 if (!done) quit("Unable to prepare any 'display module'!");
778 /* Hack -- If requested, display scores and quit */
779 if (show_score > 0) display_scores(0, show_score);
782 /* Catch nasty signals */
788 /* Wait for response */