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.
13 #include "uid-checker.h"
14 #include "signal-handlers.h"
23 * Available graphic modes
25 #define GRAPHICS_NONE 0
26 #define GRAPHICS_ORIGINAL 1
27 #define GRAPHICS_ADAM_BOLT 2
28 #define GRAPHICS_HENGBAND 3
31 * Some machines have a "main()" function in their "main-xxx.c" file,
32 * all the others use this file for their "main()" function.
39 * A hook for "quit()".
41 * Close down, then fall back into "quit()".
43 static void quit_hook(concptr s)
51 for (j = 8 - 1; j >= 0; j--)
54 if (!angband_term[j]) continue;
57 term_nuke(angband_term[j]);
63 * Set the stack size and overlay buffer (see main-286.c")
65 #ifdef PRIVATE_USER_PATH
68 * Create an ".angband/" directory in the users home directory.
70 * ToDo: Add error handling.
71 * ToDo: Only create the directories when actually writing files.
73 static void create_user_dir(void)
76 char subdirpath[1024];
78 /* Get an absolute path from the filename */
79 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
81 /* Create the ~/.angband/ directory */
84 /* Build the path to the variant-specific sub-directory */
85 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
87 /* Create the directory */
88 mkdir(subdirpath, 0700);
91 #endif /* PRIVATE_USER_PATH */
95 * Initialize and verify the file paths, and the score file.
97 * Use the ANGBAND_PATH environment var if possible, else use
98 * DEFAULT_PATH, and in either case, branch off appropriately.
100 * First, we'll look for the ANGBAND_PATH environment variable,
101 * and then look for the files in there. If that doesn't work,
102 * we'll try the DEFAULT_PATH constant. So be sure that one of
103 * these two things works...
105 * We must ensure that the path ends with "PATH_SEP" if needed,
106 * since the "init_file_paths()" function will simply append the
107 * relevant "sub-directory names" to the given path.
109 * Make sure that the path doesn't overflow the buffer. We have
110 * to leave enough space for the path separator, directory, and
113 static void init_stuff(void)
119 /* Get the environment variable */
120 tail = getenv("ANGBAND_PATH");
122 /* Use the angband_path, or a default */
123 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
125 /* Make sure it's terminated */
128 /* Hack -- Add a path separator (only if needed) */
129 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
132 init_file_paths(path);
138 * Handle a "-d<what>=<path>" option
140 * The "<what>" can be any string starting with the same letter as the
141 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
143 * The "<path>" can be any legal path for the given system, and should
144 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
146 static void change_path(concptr info)
150 /* Find equal sign */
151 s = my_strchr(info, '=');
153 /* Verify equal sign */
154 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
157 switch (tolower(info[0]))
161 string_free(ANGBAND_DIR_APEX);
162 ANGBAND_DIR_APEX = string_make(s+1);
168 string_free(ANGBAND_DIR_FILE);
169 ANGBAND_DIR_FILE = string_make(s+1);
175 string_free(ANGBAND_DIR_HELP);
176 ANGBAND_DIR_HELP = string_make(s+1);
182 string_free(ANGBAND_DIR_INFO);
183 ANGBAND_DIR_INFO = string_make(s+1);
189 string_free(ANGBAND_DIR_USER);
190 ANGBAND_DIR_USER = string_make(s+1);
196 string_free(ANGBAND_DIR_XTRA);
197 ANGBAND_DIR_XTRA = string_make(s+1);
203 string_free(ANGBAND_DIR_BONE);
204 ANGBAND_DIR_BONE = string_make(s+1);
210 string_free(ANGBAND_DIR_DATA);
211 ANGBAND_DIR_DATA = string_make(s+1);
217 string_free(ANGBAND_DIR_EDIT);
218 ANGBAND_DIR_EDIT = string_make(s+1);
224 string_free(ANGBAND_DIR_SAVE);
225 ANGBAND_DIR_SAVE = string_make(s+1);
231 string_free(ANGBAND_DIR_SCRIPT);
232 ANGBAND_DIR_SCRIPT = string_make(s+1);
238 quit_fmt("Bad semantics in '-d%s'", info);
243 static void display_usage(void)
245 /* Dump usage information */
246 puts("Usage: angband [options] [-- subopts]");
247 puts(" -n Start a new character");
248 puts(" -f Request fiddle mode");
249 puts(" -w Request wizard mode");
250 puts(" -b Request BGM mode");
251 puts(" -v Request sound mode");
252 puts(" -g Request graphics mode");
253 puts(" -o Request original keyset");
254 puts(" -r Request rogue-like keyset");
255 puts(" -M Request monochrome mode");
256 puts(" -s<num> Show <num> high scores");
257 puts(" -u<who> Use your <who> savefile");
258 puts(" -m<sys> Force 'main-<sys>.c' usage");
259 puts(" -d<def> Define a 'lib' dir sub-path");
263 puts(" -mx11 To use X11");
264 puts(" -- Sub options");
265 puts(" -- -d Set display name");
266 puts(" -- -o Request old 8x8 tile graphics");
267 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
268 puts(" -- -b Request Bigtile graphics mode");
269 puts(" -- -s Turn off smoothscaling graphics");
270 puts(" -- -n# Number of terms to use");
275 puts(" -mgcu To use GCU (GNU Curses)");
279 puts(" -mcap To use CAP (\"Termcap\" calls)");
282 /* Actually abort the process */
288 * Simple "main" function for multiple platforms.
290 * Note the special "--" option which terminates the processing of
291 * standard options. All non-standard options (if any) are passed
292 * directly to the "init_xxx()" function.
294 int main(int argc, char *argv[])
299 bool new_game = FALSE;
304 /* Save the "program name" XXX XXX XXX */
309 /* Default permissions on files */
315 /* Get the file paths */
321 /* Get the user id (?) */
322 p_ptr->player_uid = getuid();
325 /* Mega-Hack -- Factor group id */
326 p_ptr->player_uid += (getgid() * 1000);
331 # ifdef _POSIX_SAVED_IDS
333 /* Save some info for later */
334 p_ptr->player_euid = geteuid();
335 p_ptr->player_egid = getegid();
344 /* Drop permissions */
350 /* Acquire the "user name" as a default player name */
351 user_name(p_ptr->name, p_ptr->player_uid);
353 #ifdef PRIVATE_USER_PATH
355 /* Create a directory for the users files. */
358 #endif /* PRIVATE_USER_PATH */
363 /* Process the command line arguments */
364 for (i = 1; args && (i < argc); i++)
366 /* Require proper options */
367 if (argv[i][0] != '-')
374 bool is_usage_needed = FALSE;
410 /* HACK - Graphics mode switches on the original tiles */
411 arg_graphics = GRAPHICS_ORIGINAL;
417 arg_force_roguelike = TRUE;
423 arg_force_original = TRUE;
429 show_score = atoi(&argv[i][2]);
430 if (show_score <= 0) show_score = 10;
438 is_usage_needed = TRUE;
442 strcpy(p_ptr->name, &argv[i][2]);
449 is_usage_needed = TRUE;
458 arg_monochrome = TRUE;
464 change_path(&argv[i][2]);
473 is_usage_needed = TRUE;
477 chuukei_server = TRUE;
478 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
487 is_usage_needed = TRUE;
491 chuukei_client = TRUE;
492 connect_chuukei_server(&argv[i][2]);
500 is_usage_needed = TRUE;
504 prepare_browse_movie(&argv[i][2]);
517 is_usage_needed = TRUE;
522 if (!is_usage_needed) continue;
527 /* Hack -- Forget standard args */
535 /* Process the player name */
536 process_player_name(p_ptr,TRUE);
540 /* Install "quit" hook */
541 quit_aux = quit_hook;
546 /* Attempt to use the "main-xaw.c" support */
547 if (!done && (!mstr || (streq(mstr, "xaw"))))
549 extern errr init_xaw(int, char**);
550 if (0 == init_xaw(argc, argv))
559 /* Attempt to use the "main-x11.c" support */
560 if (!done && (!mstr || (streq(mstr, "x11"))))
562 extern errr init_x11(int, char**);
563 if (0 == init_x11(argc, argv))
572 /* Attempt to use the "main-gcu.c" support */
573 if (!done && (!mstr || (streq(mstr, "gcu"))))
575 extern errr init_gcu(int, char**);
576 if (0 == init_gcu(argc, argv))
585 /* Attempt to use the "main-cap.c" support */
586 if (!done && (!mstr || (streq(mstr, "cap"))))
588 extern errr init_cap(int, char**);
589 if (0 == init_cap(argc, argv))
597 /* Make sure we have a display! */
598 if (!done) quit("Unable to prepare any 'display module'!");
601 /* Hack -- If requested, display scores and quit */
602 if (show_score > 0) display_scores(0, show_score);
605 /* Catch nasty signals */
611 /* Wait for response */
615 play_game(p_ptr, new_game);