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"
21 * Available graphic modes
23 #define GRAPHICS_NONE 0
24 #define GRAPHICS_ORIGINAL 1
25 #define GRAPHICS_ADAM_BOLT 2
26 #define GRAPHICS_HENGBAND 3
29 * Some machines have a "main()" function in their "main-xxx.c" file,
30 * all the others use this file for their "main()" function.
37 * A hook for "quit()".
39 * Close down, then fall back into "quit()".
41 static void quit_hook(concptr s)
49 for (j = 8 - 1; j >= 0; j--)
52 if (!angband_term[j]) continue;
55 term_nuke(angband_term[j]);
61 * Set the stack size and overlay buffer (see main-286.c")
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];
76 /* Get an absolute path from the filename */
77 path_parse(dirpath, 1024, PRIVATE_USER_PATH);
79 /* Create the ~/.angband/ directory */
82 /* Build the path to the variant-specific sub-directory */
83 path_build(subdirpath, sizeof(subdirpath), dirpath, VERSION_NAME);
85 /* Create the directory */
86 mkdir(subdirpath, 0700);
89 #endif /* PRIVATE_USER_PATH */
93 * Initialize and verify the file paths, and the score file.
95 * Use the ANGBAND_PATH environment var if possible, else use
96 * DEFAULT_PATH, and in either case, branch off appropriately.
98 * First, we'll look for the ANGBAND_PATH environment variable,
99 * and then look for the files in there. If that doesn't work,
100 * we'll try the DEFAULT_PATH constant. So be sure that one of
101 * these two things works...
103 * We must ensure that the path ends with "PATH_SEP" if needed,
104 * since the "init_file_paths()" function will simply append the
105 * relevant "sub-directory names" to the given path.
107 * Make sure that the path doesn't overflow the buffer. We have
108 * to leave enough space for the path separator, directory, and
111 static void init_stuff(void)
117 /* Get the environment variable */
118 tail = getenv("ANGBAND_PATH");
120 /* Use the angband_path, or a default */
121 strncpy(path, tail ? tail : DEFAULT_PATH, 511);
123 /* Make sure it's terminated */
126 /* Hack -- Add a path separator (only if needed) */
127 if (!suffix(path, PATH_SEP)) strcat(path, PATH_SEP);
130 init_file_paths(path);
136 * Handle a "-d<what>=<path>" option
138 * The "<what>" can be any string starting with the same letter as the
139 * name of a subdirectory of the "lib" folder (i.e. "i" or "info").
141 * The "<path>" can be any legal path for the given system, and should
142 * not end in any special path separator (i.e. "/tmp" or "~/.ang-info").
144 static void change_path(concptr info)
148 /* Find equal sign */
149 s = my_strchr(info, '=');
151 /* Verify equal sign */
152 if (!s) quit_fmt("Try '-d<what>=<path>' not '-d%s'", info);
155 switch (tolower(info[0]))
159 string_free(ANGBAND_DIR_APEX);
160 ANGBAND_DIR_APEX = string_make(s+1);
166 string_free(ANGBAND_DIR_FILE);
167 ANGBAND_DIR_FILE = string_make(s+1);
173 string_free(ANGBAND_DIR_HELP);
174 ANGBAND_DIR_HELP = string_make(s+1);
180 string_free(ANGBAND_DIR_INFO);
181 ANGBAND_DIR_INFO = string_make(s+1);
187 string_free(ANGBAND_DIR_USER);
188 ANGBAND_DIR_USER = string_make(s+1);
194 string_free(ANGBAND_DIR_XTRA);
195 ANGBAND_DIR_XTRA = string_make(s+1);
201 string_free(ANGBAND_DIR_BONE);
202 ANGBAND_DIR_BONE = string_make(s+1);
208 string_free(ANGBAND_DIR_DATA);
209 ANGBAND_DIR_DATA = string_make(s+1);
215 string_free(ANGBAND_DIR_EDIT);
216 ANGBAND_DIR_EDIT = string_make(s+1);
222 string_free(ANGBAND_DIR_SAVE);
223 ANGBAND_DIR_SAVE = string_make(s+1);
229 string_free(ANGBAND_DIR_SCRIPT);
230 ANGBAND_DIR_SCRIPT = string_make(s+1);
236 quit_fmt("Bad semantics in '-d%s'", info);
241 static void display_usage(void)
243 /* Dump usage information */
244 puts("Usage: angband [options] [-- subopts]");
245 puts(" -n Start a new character");
246 puts(" -f Request fiddle mode");
247 puts(" -w Request wizard mode");
248 puts(" -b Request BGM mode");
249 puts(" -v Request sound mode");
250 puts(" -g Request graphics mode");
251 puts(" -o Request original keyset");
252 puts(" -r Request rogue-like keyset");
253 puts(" -M Request monochrome mode");
254 puts(" -s<num> Show <num> high scores");
255 puts(" -u<who> Use your <who> savefile");
256 puts(" -m<sys> Force 'main-<sys>.c' usage");
257 puts(" -d<def> Define a 'lib' dir sub-path");
261 puts(" -mx11 To use X11");
262 puts(" -- Sub options");
263 puts(" -- -d Set display name");
264 puts(" -- -o Request old 8x8 tile graphics");
265 puts(" -- -a Request Adam Bolt 16x16 tile graphics");
266 puts(" -- -b Request Bigtile graphics mode");
267 puts(" -- -s Turn off smoothscaling graphics");
268 puts(" -- -n# Number of terms to use");
273 puts(" -mgcu To use GCU (GNU Curses)");
277 puts(" -mcap To use CAP (\"Termcap\" calls)");
280 /* Actually abort the process */
286 * Simple "main" function for multiple platforms.
288 * Note the special "--" option which terminates the processing of
289 * standard options. All non-standard options (if any) are passed
290 * directly to the "init_xxx()" function.
292 int main(int argc, char *argv[])
297 bool new_game = FALSE;
302 /* Save the "program name" XXX XXX XXX */
307 /* Default permissions on files */
313 /* Get the file paths */
319 /* Get the user id (?) */
320 p_ptr->player_uid = getuid();
323 /* Mega-Hack -- Factor group id */
324 p_ptr->player_uid += (getgid() * 1000);
329 # ifdef _POSIX_SAVED_IDS
331 /* Save some info for later */
332 p_ptr->player_euid = geteuid();
333 p_ptr->player_egid = getegid();
342 /* Drop permissions */
348 /* Acquire the "user name" as a default player name */
349 user_name(p_ptr->name, p_ptr->player_uid);
351 #ifdef PRIVATE_USER_PATH
353 /* Create a directory for the users files. */
356 #endif /* PRIVATE_USER_PATH */
361 /* Process the command line arguments */
362 for (i = 1; args && (i < argc); i++)
364 /* Require proper options */
365 if (argv[i][0] != '-')
372 bool is_usage_needed = FALSE;
408 /* HACK - Graphics mode switches on the original tiles */
409 arg_graphics = GRAPHICS_ORIGINAL;
415 arg_force_roguelike = TRUE;
421 arg_force_original = TRUE;
427 show_score = atoi(&argv[i][2]);
428 if (show_score <= 0) show_score = 10;
436 is_usage_needed = TRUE;
440 strcpy(p_ptr->name, &argv[i][2]);
447 is_usage_needed = TRUE;
456 arg_monochrome = TRUE;
462 change_path(&argv[i][2]);
471 is_usage_needed = TRUE;
475 chuukei_server = TRUE;
476 if (connect_chuukei_server(&argv[i][2]) < 0) chuukei_server = FALSE;
485 is_usage_needed = TRUE;
489 chuukei_client = TRUE;
490 connect_chuukei_server(&argv[i][2]);
498 is_usage_needed = TRUE;
502 prepare_browse_movie(&argv[i][2]);
515 is_usage_needed = TRUE;
520 if (!is_usage_needed) continue;
525 /* Hack -- Forget standard args */
533 /* Process the player name */
534 process_player_name(p_ptr,TRUE);
538 /* Install "quit" hook */
539 quit_aux = quit_hook;
544 /* Attempt to use the "main-xaw.c" support */
545 if (!done && (!mstr || (streq(mstr, "xaw"))))
547 extern errr init_xaw(int, char**);
548 if (0 == init_xaw(argc, argv))
557 /* Attempt to use the "main-x11.c" support */
558 if (!done && (!mstr || (streq(mstr, "x11"))))
560 extern errr init_x11(int, char**);
561 if (0 == init_x11(argc, argv))
570 /* Attempt to use the "main-gcu.c" support */
571 if (!done && (!mstr || (streq(mstr, "gcu"))))
573 extern errr init_gcu(int, char**);
574 if (0 == init_gcu(argc, argv))
583 /* Attempt to use the "main-cap.c" support */
584 if (!done && (!mstr || (streq(mstr, "cap"))))
586 extern errr init_cap(int, char**);
587 if (0 == init_cap(argc, argv))
595 /* Make sure we have a display! */
596 if (!done) quit("Unable to prepare any 'display module'!");
599 /* Hack -- If requested, display scores and quit */
600 if (show_score > 0) display_scores(0, show_score);
603 /* Catch nasty signals */
609 /* Wait for response */
613 play_game(p_ptr, new_game);