Windows startup modifications

Changes to be committed:
	modified:   include/winprocs.h
	modified:   src/options.c
	modified:   sys/share/pcmain.c
	new file:   sys/share/safeproc.c
	modified:   sys/winnt/Makefile.msc
	modified:   sys/winnt/stubs.c
	new file:   sys/winnt/windmain.c
	modified:   sys/winnt/winnt.c
	modified:   win/win32/vs2017/NetHack.vcxproj
	modified:   win/win32/vs2017/NetHackW.vcxproj
	modified:   win/win32/winhack.c

Because multiple window ports are supported on Windows
now, even in the same executable and selectable via
config file in some cases, some adjustments became
necessary. There will likely be some further refining
of this over the next day or two.

List of changes:

Move Windows startup from sys/share/pcmain.c and
into its own sys/winnt/windmain.c so that it can
be modified to fix some current breakage, and
allow altering the order of some things.

There is startup processing code that is common to
all of the Windows WindowPorts, but that startup
processing code needs to have no dependency on
any one of those WindowPorts.

Yet, during startup processing, some of the initialization
routines can end up calling NetHack functions that
expect an active Window port underneath, and if there
isn't one, routines like pline, impossible, panic can
end up invoking null function pointers.

Place a new file sys/share/safeproc.c, in which a complete
window port is available for early startup processing
purposes. It's WindowPort name field is set to
"safe-startup" just for reference.  The prototypes in
include/winprocs.h require that SAFEPROCS be

Usage:

 windowprocs = get_safe_procs(0);
initializes a set of winprocs function pointers that ensure
none of the function pointers are left null, but that's all it does.

 windowprocs = get_safe_procs(1);
initializes a set of winprocs functions pointers that ensure
none of the function pointers are left null, but also
provides some basic output and input functionality using nothing
other than C stdio routines (no platform or OS specific code).

The conditional code related to WIN32 has been removed from
sys/share/pcmain.c

The code common to all of the Windows WindowPorts calls
get_safe_procs() almost immediately to ensure that
there is a set of WindowPort winprocs available.
This commit is contained in:
nhmall
2018-12-08 17:56:20 -05:00
parent 2255543800
commit dc1e2da359
11 changed files with 1447 additions and 284 deletions

View File

@@ -3,11 +3,7 @@
/*-Copyright (c) Derek S. Ray, 2015. */
/* NetHack may be freely redistributed. See license for details. */
/* main.c - MSDOS, OS/2, ST, Amiga, and Windows NetHack */
#ifdef WIN32
#include "win32api.h" /* for GetModuleFileName */
#endif
/* main.c - MSDOS, OS/2, ST, Amiga NetHack */
#include "hack.h"
#include "dlb.h"
@@ -47,29 +43,12 @@ void NDECL(preserve_icon);
STATIC_DCL void FDECL(process_options, (int argc, char **argv));
STATIC_DCL void NDECL(nhusage);
#if defined(MICRO) || defined(WIN32) || defined(OS2)
#if defined(MICRO) || defined(OS2)
extern void FDECL(nethack_exit, (int));
#else
#define nethack_exit exit
#endif
#ifdef WIN32
extern boolean getreturn_enabled; /* from sys/share/pcsys.c */
extern int redirect_stdout; /* from sys/share/pcsys.c */
extern int GUILaunched;
char *NDECL(exename);
char default_window_sys[] = "mswin";
#ifndef WIN32CON
HANDLE hStdOut;
boolean NDECL(fakeconsole);
void NDECL(freefakeconsole);
#endif
#endif
#if defined(MSWIN_GRAPHICS)
extern void NDECL(mswin_destroy_reg);
#endif
#ifdef EXEPATH
STATIC_DCL char *FDECL(exepath, (char *));
#endif
@@ -78,7 +57,7 @@ int FDECL(main, (int, char **));
extern boolean FDECL(pcmain, (int, char **));
#if defined(__BORLANDC__) && !defined(_WIN32)
#if defined(__BORLANDC__)
void NDECL(startup);
unsigned _stklen = STKSIZ;
#endif
@@ -98,21 +77,8 @@ char *argv[];
{
boolean resuming;
nethack_enter(argc, argv);
sys_early_init();
#if defined(WIN32) && defined(TTY_GRAPHICS)
Strcpy(default_window_sys, "tty");
#else
#if defined(CURSES_GRAPHICS)
Strcpy(default_window_sys, "curses");
#endif
#endif
resuming = pcmain(argc, argv);
#ifdef LAN_FEATURES
init_lan_features();
#endif
moveloop(resuming);
nethack_exit(EXIT_SUCCESS);
/*NOTREACHED*/
@@ -126,14 +92,10 @@ char *argv[];
{
register int fd;
register char *dir;
#if defined(WIN32) || defined(MSDOS)
#if defined(MSDOS)
char *envp = NULL;
char *sptr = NULL;
#endif
#if defined(WIN32)
char fnamebuf[BUFSZ], encodedfnamebuf[BUFSZ];
boolean save_getreturn_status = getreturn_enabled;
#endif
#ifdef NOCWD_ASSUMPTIONS
char failbuf[BUFSZ];
#endif
@@ -162,7 +124,7 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
# endif
#endif
#if defined(__BORLANDC__) && !defined(_WIN32)
#if defined(__BORLANDC__)
startup();
#endif
@@ -175,15 +137,7 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
#endif
hname = "NetHack"; /* used for syntax messages */
#ifndef WIN32
choose_windows(DEFAULT_WINDOW_SYS);
#else
choose_windows(default_window_sys);
if (argc >= 1
&& !strcmpi(default_window_sys, "mswin")
&& strstri(argv[0], "nethackw.exe"))
iflags.windowtype_locked = TRUE;
#endif
#if !defined(AMIGA) && !defined(GNUDOS)
/* Save current directory and make sure it gets restored when
@@ -243,7 +197,7 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
for (prefcnt = 1; prefcnt < PREFIX_COUNT; prefcnt++)
fqn_prefix[prefcnt] = fqn_prefix[0];
#if defined(WIN32) || defined(MSDOS)
#if defined(MSDOS)
/* sysconf should be searched for in this location */
envp = nh_getenv("COMMONPROGRAMFILES");
if (envp) {
@@ -317,14 +271,6 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
chdirx(HACKDIR, 1);
#endif
ami_wininit_data();
#endif
#ifdef WIN32
save_getreturn_status = getreturn_enabled;
#ifdef TTY_GRAPHICS
raw_clear_screen();
#endif
getreturn_enabled = TRUE;
check_recordfile((char *) 0);
#endif
initoptions();
@@ -355,13 +301,6 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
argv++;
}
#ifdef WIN32
if (argcheck(argc, argv, ARG_WINDOWS) == 1) {
argc--;
argv++;
}
#endif
if (argc > 1 && !strncmp(argv[1], "-d", 2) && argv[1][2] != 'e') {
/* avoid matching "-dec" for DECgraphics; since the man page
* says -d directory, hope nobody's using -desomething_else
@@ -381,36 +320,11 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
Strcpy(hackdir, dir);
}
if (argc > 1) {
#if defined(WIN32) && !defined(WIN32CON)
int sfd = 0;
boolean tmpconsole = FALSE;
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
/*
* Now we know the directory containing 'record' and
* may do a prscore().
*/
if (!strncmp(argv[1], "-s", 2)) {
#if defined(WIN32) && !defined(WIN32CON)
#if 0
if (!hStdOut) {
tmpconsole = fakeconsole();
}
#endif
/*
* Check to see if we're redirecting to a file.
*/
sfd = (int) _fileno(stdout);
redirect_stdout = (sfd >= 0) ? !isatty(sfd) : 0;
if (!redirect_stdout && !hStdOut) {
raw_printf(
"-s is not supported for the Graphical Interface\n");
nethack_exit(EXIT_SUCCESS);
}
#endif
#if defined(CHDIR) && !defined(NOCWD_ASSUMPTIONS)
chdirx(hackdir, 0);
#endif
@@ -418,47 +332,16 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
initoptions();
#endif
prscore(argc, argv);
#if defined(WIN32) && !defined(WIN32CON)
if (tmpconsole) {
getreturn("to exit");
freefakeconsole();
tmpconsole = FALSE;
}
#endif
nethack_exit(EXIT_SUCCESS);
}
#ifdef MSWIN_GRAPHICS
if (!strncmpi(argv[1], "-clearreg", 6)) { /* clear registry */
mswin_destroy_reg();
nethack_exit(EXIT_SUCCESS);
}
#endif
/* Don't initialize the window system just to print usage */
if (!strncmp(argv[1], "-?", 2) || !strncmp(argv[1], "/?", 2)) {
#if 0
if (!hStdOut) {
GUILaunched = 0;
tmpconsole = fakeconsole();
}
#endif
nhusage();
#if defined(WIN32) && !defined(WIN32CON)
if (tmpconsole) {
getreturn("to exit");
freefakeconsole();
tmpconsole = FALSE;
}
#endif
nethack_exit(EXIT_SUCCESS);
}
}
}
#ifdef WIN32
getreturn_enabled = save_getreturn_status;
#endif
/*
* It seems you really want to play.
*/
@@ -472,11 +355,7 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
"\"%s\".%s",
copyright_banner_line(1), copyright_banner_line(2),
copyright_banner_line(3), copyright_banner_line(4), DLBFILE,
#ifdef WIN32
"\nAre you perhaps trying to run NetHack within a zip utility?");
#else
"");
#endif
error("dlb_init failure.");
}
@@ -490,41 +369,22 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
chdirx(hackdir, 1);
#endif
#if defined(MSDOS) || defined(WIN32)
#if defined(MSDOS)
/* In 3.6.0, several ports process options before they init
* the window port. This allows settings that impact window
* ports to be specified or read from the sys or user config files.
*/
process_options(argc, argv);
#ifdef WIN32
/*
if (WINDOWPORT("mswin"))
NHWinMainInit();
else
*/
#ifdef TTY_GRAPHICS
if (WINDOWPORT("tty")) {
iflags.use_background_glyph = FALSE;
nttty_open(1);
} else {
iflags.use_background_glyph = TRUE;
}
#endif /* TTY_GRAPHICS */
#endif /* WIN32 */
#endif /* MSDOS || WIN32 */
#endif /* MSDOS */
#if defined(MSDOS) || defined(WIN32)
#if defined(MSDOS)
init_nhwindows(&argc, argv);
#else
init_nhwindows(&argc, argv);
process_options(argc, argv);
#endif
#if defined(WIN32) && defined(TTY_GRAPHICS)
toggle_mouse_support(); /* must come after process_options */
#endif
#ifdef MFLOPPY
set_lock_and_bones();
#ifndef AMIGA
@@ -561,19 +421,8 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
* overwritten without confirmation when a user starts up
* another game with the same player name.
*/
#if defined(WIN32)
/* Obtain the name of the logged on user and incorporate
* it into the name. */
Sprintf(fnamebuf, "%s-%s", get_username(0), plname);
(void) fname_encode(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-.", '%',
fnamebuf, encodedfnamebuf, BUFSZ);
Sprintf(lock, "%s", encodedfnamebuf);
/* regularize(lock); */ /* we encode now, rather than substitute */
#else
Strcpy(lock, plname);
regularize(lock);
#endif
getlock();
#else /* What follows is !PC_LOCKING */
#ifdef AMIGA /* We'll put the bones & levels in the user specified directory \
@@ -599,11 +448,7 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
if (fd < 0) {
raw_print("Cannot create lock file");
} else {
#ifdef WIN32
hackpid = GetCurrentProcessId();
#else
hackpid = 1;
#endif
write(fd, (genericptr_t) &hackpid, sizeof(hackpid));
nhclose(fd);
}
@@ -618,10 +463,6 @@ _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);*/
vision_init();
display_gamewindows();
#ifdef WIN32
getreturn_enabled = TRUE;
#endif
/*
* First, try to find and restore a save file for specified character.
* We'll return here if new game player_selection() renames the hero.
@@ -799,18 +640,6 @@ char *argv[];
case 'l':
bigscreen = -1;
break;
#endif
#ifdef WIN32
case 'w': /* windowtype */
#ifdef TTY_GRAPHICS
if (strncmpi(&argv[0][2], "tty", 3)) {
nttty_open(1);
}
#endif
config_error_init(FALSE, "command line", FALSE);
choose_windows(&argv[0][2]);
config_error_done();
break;
#endif
case '@':
flags.randomall = 1;
@@ -904,14 +733,14 @@ boolean wr;
#endif /* CHDIR */
#ifdef PORT_HELP
#if defined(MSDOS) || defined(WIN32)
#if defined(MSDOS)
void
port_help()
{
/* display port specific help file */
display_file(PORT_HELP, 1);
}
#endif /* MSDOS || WIN32 */
#endif /* MSDOS */
#endif /* PORT_HELP */
/* validate wizard mode if player has requested access to it */
@@ -930,71 +759,6 @@ authorize_wizard_mode()
#define PATH_SEPARATOR '\\'
#endif
#if defined(WIN32) && !defined(WIN32CON)
static char exenamebuf[PATHLEN];
extern HANDLE hConIn;
extern HANDLE hConOut;
boolean has_fakeconsole;
char *
exename()
{
int bsize = PATHLEN;
char *tmp = exenamebuf, *tmp2;
#ifdef UNICODE
{
TCHAR wbuf[PATHLEN * 4];
GetModuleFileName((HANDLE) 0, wbuf, PATHLEN * 4);
WideCharToMultiByte(CP_ACP, 0, wbuf, -1, tmp, bsize, NULL, NULL);
}
#else
*(tmp + GetModuleFileName((HANDLE) 0, tmp, bsize)) = '\0';
#endif
tmp2 = strrchr(tmp, PATH_SEPARATOR);
if (tmp2)
*tmp2 = '\0';
tmp2++;
return tmp2;
}
boolean
fakeconsole(void)
{
if (!hStdOut) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (!hStdOut && !hStdIn) {
/* Bool rval; */
AllocConsole();
AttachConsole(GetCurrentProcessId());
/* rval = SetStdHandle(STD_OUTPUT_HANDLE, hWrite); */
freopen("CON", "w", stdout);
freopen("CON", "r", stdin);
}
has_fakeconsole = TRUE;
}
/* Obtain handles for the standard Console I/O devices */
hConIn = GetStdHandle(STD_INPUT_HANDLE);
hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
#if 0
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlHandler, TRUE)) {
/* Unable to set control handler */
cmode = 0; /* just to have a statement to break on for debugger */
}
#endif
return has_fakeconsole;
}
void freefakeconsole()
{
if (has_fakeconsole) {
FreeConsole();
}
}
#endif
#define EXEPATHBUFSZ 256
char exepathbuf[EXEPATHBUFSZ];
@@ -1009,19 +773,7 @@ char *str;
return (char *) 0;
bsize = EXEPATHBUFSZ;
tmp = exepathbuf;
#ifndef WIN32
Strcpy(tmp, str);
#else
#ifdef UNICODE
{
TCHAR wbuf[BUFSZ];
GetModuleFileName((HANDLE) 0, wbuf, BUFSZ);
WideCharToMultiByte(CP_ACP, 0, wbuf, -1, tmp, bsize, NULL, NULL);
}
#else
*(tmp + GetModuleFileName((HANDLE) 0, tmp, bsize)) = '\0';
#endif
#endif
tmp2 = strrchr(tmp, PATH_SEPARATOR);
if (tmp2)
*tmp2 = '\0';