1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00
putty-source/windows/help.c

251 lines
6.1 KiB
C
Raw Permalink Normal View History

/*
* help.c: centralised functions to launch Windows HTML Help files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "putty.h"
#include "putty-rc.h"
#ifdef NO_HTMLHELP
/* If htmlhelp.h is not available, we can't do any of this at all */
bool has_help(void) { return false; }
void init_help(void) { }
void shutdown_help(void) { }
void launch_help(HWND hwnd, const char *topic) { }
void quit_help(HWND hwnd) { }
#else
#include <htmlhelp.h>
static char *chm_path = NULL;
static bool chm_created_by_us = false;
Convert a lot of 'int' variables to 'bool'. My normal habit these days, in new code, is to treat int and bool as _almost_ completely separate types. I'm still willing to use C's implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine, no need to spell it out as blob.len != 0), but generally, if a variable is going to be conceptually a boolean, I like to declare it bool and assign to it using 'true' or 'false' rather than 0 or 1. PuTTY is an exception, because it predates the C99 bool, and I've stuck to its existing coding style even when adding new code to it. But it's been annoying me more and more, so now that I've decided C99 bool is an acceptable thing to require from our toolchain in the first place, here's a quite thorough trawl through the source doing 'boolification'. Many variables and function parameters are now typed as bool rather than int; many assignments of 0 or 1 to those variables are now spelled 'true' or 'false'. I managed this thorough conversion with the help of a custom clang plugin that I wrote to trawl the AST and apply heuristics to point out where things might want changing. So I've even managed to do a decent job on parts of the code I haven't looked at in years! To make the plugin's work easier, I pushed platform front ends generally in the direction of using standard 'bool' in preference to platform-specific boolean types like Windows BOOL or GTK's gboolean; I've left the platform booleans in places they _have_ to be for the platform APIs to work right, but variables only used by my own code have been converted wherever I found them. In a few places there are int values that look very like booleans in _most_ of the places they're used, but have a rarely-used third value, or a distinction between different nonzero values that most users don't care about. In these cases, I've _removed_ uses of 'true' and 'false' for the return values, to emphasise that there's something more subtle going on than a simple boolean answer: - the 'multisel' field in dialog.h's list box structure, for which the GTK front end in particular recognises a difference between 1 and 2 but nearly everything else treats as boolean - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you something about the specific location of the urgent pointer, but most clients only care about 0 vs 'something nonzero' - the return value of wc_match, where -1 indicates a syntax error in the wildcard. - the return values from SSH-1 RSA-key loading functions, which use -1 for 'wrong passphrase' and 0 for all other failures (so any caller which already knows it's not loading an _encrypted private_ key can treat them as boolean) - term->esc_query, and the 'query' parameter in toggle_mode in terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h, but can also hold -1 for some other intervening character that we don't support. In a few places there's an integer that I haven't turned into a bool even though it really _can_ only take values 0 or 1 (and, as above, tried to make the call sites consistent in not calling those values true and false), on the grounds that I thought it would make it more confusing to imply that the 0 value was in some sense 'negative' or bad and the 1 positive or good: - the return value of plug_accepting uses the POSIXish convention of 0=success and nonzero=error; I think if I made it bool then I'd also want to reverse its sense, and that's a job for a separate piece of work. - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1 represent the default and alternate screens. There's no obvious reason why one of those should be considered 'true' or 'positive' or 'success' - they're just indices - so I've left it as int. ssh_scp_recv had particularly confusing semantics for its previous int return value: its call sites used '<= 0' to check for error, but it never actually returned a negative number, just 0 or 1. Now the function and its call sites agree that it's a bool. In a couple of places I've renamed variables called 'ret', because I don't like that name any more - it's unclear whether it means the return value (in preparation) for the _containing_ function or the return value received from a subroutine call, and occasionally I've accidentally used the same variable for both and introduced a bug. So where one of those got in my way, I've renamed it to 'toret' or 'retd' (the latter short for 'returned') in line with my usual modern practice, but I haven't done a thorough job of finding all of them. Finally, one amusing side effect of doing this is that I've had to separate quite a few chained assignments. It used to be perfectly fine to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a the 'true' defined by stdbool.h, that idiom provokes a warning from gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
static bool requested_help;
DECL_WINDOWS_FUNCTION(static, HWND, HtmlHelpA, (HWND, LPCSTR, UINT, DWORD_PTR));
static HRSRC chm_hrsrc;
static DWORD chm_resource_size = 0;
static const void *chm_resource = NULL;
int has_embedded_chm(void)
{
static bool checked = false;
if (!checked) {
checked = true;
chm_hrsrc = FindResource(
NULL, MAKEINTRESOURCE(ID_CUSTOM_CHMFILE),
MAKEINTRESOURCE(TYPE_CUSTOM_CHMFILE));
}
return chm_hrsrc != NULL ? 1 : 0;
}
static bool find_chm_resource(void)
{
static bool checked = false;
if (checked) /* we've been here already */
goto out;
checked = true;
/*
* Look for a CHM file embedded in this executable as a custom
* resource.
*/
if (!has_embedded_chm()) /* set up chm_hrsrc and check if it's NULL */
goto out;
chm_resource_size = SizeofResource(NULL, chm_hrsrc);
if (chm_resource_size == 0)
goto out;
HGLOBAL chm_hglobal = LoadResource(NULL, chm_hrsrc);
if (chm_hglobal == NULL)
goto out;
chm_resource = (const uint8_t *)LockResource(chm_hglobal);
out:
return chm_resource != NULL;
}
static bool load_chm_resource(void)
{
bool toret = false;
char *filename = NULL;
HANDLE filehandle = INVALID_HANDLE_VALUE;
bool created = false;
static bool tried_to_load = false;
if (tried_to_load)
goto out;
tried_to_load = true;
/*
* We've found it! Now write it out into a separate file, so that
* htmlhelp.exe can handle it.
*/
/* GetTempPath is documented as returning a size of up to
* MAX_PATH+1 which does not count the NUL */
char tempdir[MAX_PATH + 2];
if (GetTempPath(sizeof(tempdir), tempdir) == 0)
goto out;
unsigned long pid = GetCurrentProcessId();
for (uint64_t counter = 0;; counter++) {
filename = dupprintf(
"%s\\putty_%lu_%"PRIu64".chm", tempdir, pid, counter);
filehandle = CreateFile(
filename, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (filehandle != INVALID_HANDLE_VALUE)
break; /* success! */
if (GetLastError() != ERROR_FILE_EXISTS)
goto out; /* failed for some other reason! */
sfree(filename);
filename = NULL;
}
created = true;
const uint8_t *p = (const uint8_t *)chm_resource;
for (DWORD pos = 0; pos < chm_resource_size ;) {
DWORD to_write = chm_resource_size - pos;
DWORD written = 0;
if (!WriteFile(filehandle, p + pos, to_write, &written, NULL))
goto out;
pos += written;
}
chm_path = filename;
filename = NULL;
chm_created_by_us = true;
toret = true;
out:
if (created && !toret)
DeleteFile(filename);
sfree(filename);
if (filehandle != INVALID_HANDLE_VALUE)
CloseHandle(filehandle);
return toret;
}
static bool find_chm_from_installation(void)
{
static const char *const reg_paths[] = {
"Software\\SimonTatham\\PuTTY64\\CHMPath",
"Software\\SimonTatham\\PuTTY\\CHMPath",
};
for (size_t i = 0; i < lenof(reg_paths); i++) {
char *filename = get_reg_sz_simple(
HKEY_LOCAL_MACHINE, reg_paths[i], NULL);
if (filename) {
chm_path = filename;
chm_created_by_us = false;
return true;
}
}
return false;
}
void init_help(void)
{
/* Just in case of multiple calls */
static bool already_called = false;
if (already_called)
return;
already_called = true;
/*
* Don't even try looking for the CHM file if we can't even find
* the HtmlHelp() API function.
*/
HINSTANCE dllHH = load_system32_dll("hhctrl.ocx");
GET_WINDOWS_FUNCTION(dllHH, HtmlHelpA);
if (!p_HtmlHelpA) {
FreeLibrary(dllHH);
return;
}
/*
* If there's a CHM file embedded in this executable, we should
* use that as the first choice.
*/
if (find_chm_resource())
return;
/*
* Otherwise, try looking for the CHM in the location that the
* installer marked in the registry.
*/
if (find_chm_from_installation())
return;
}
void shutdown_help(void)
{
if (chm_path && chm_created_by_us) {
p_HtmlHelpA(NULL, NULL, HH_CLOSE_ALL, 0);
DeleteFile(chm_path);
}
sfree(chm_path);
chm_path = NULL;
chm_created_by_us = false;
}
Convert a lot of 'int' variables to 'bool'. My normal habit these days, in new code, is to treat int and bool as _almost_ completely separate types. I'm still willing to use C's implicit test for zero on an integer (e.g. 'if (!blob.len)' is fine, no need to spell it out as blob.len != 0), but generally, if a variable is going to be conceptually a boolean, I like to declare it bool and assign to it using 'true' or 'false' rather than 0 or 1. PuTTY is an exception, because it predates the C99 bool, and I've stuck to its existing coding style even when adding new code to it. But it's been annoying me more and more, so now that I've decided C99 bool is an acceptable thing to require from our toolchain in the first place, here's a quite thorough trawl through the source doing 'boolification'. Many variables and function parameters are now typed as bool rather than int; many assignments of 0 or 1 to those variables are now spelled 'true' or 'false'. I managed this thorough conversion with the help of a custom clang plugin that I wrote to trawl the AST and apply heuristics to point out where things might want changing. So I've even managed to do a decent job on parts of the code I haven't looked at in years! To make the plugin's work easier, I pushed platform front ends generally in the direction of using standard 'bool' in preference to platform-specific boolean types like Windows BOOL or GTK's gboolean; I've left the platform booleans in places they _have_ to be for the platform APIs to work right, but variables only used by my own code have been converted wherever I found them. In a few places there are int values that look very like booleans in _most_ of the places they're used, but have a rarely-used third value, or a distinction between different nonzero values that most users don't care about. In these cases, I've _removed_ uses of 'true' and 'false' for the return values, to emphasise that there's something more subtle going on than a simple boolean answer: - the 'multisel' field in dialog.h's list box structure, for which the GTK front end in particular recognises a difference between 1 and 2 but nearly everything else treats as boolean - the 'urgent' parameter to plug_receive, where 1 vs 2 tells you something about the specific location of the urgent pointer, but most clients only care about 0 vs 'something nonzero' - the return value of wc_match, where -1 indicates a syntax error in the wildcard. - the return values from SSH-1 RSA-key loading functions, which use -1 for 'wrong passphrase' and 0 for all other failures (so any caller which already knows it's not loading an _encrypted private_ key can treat them as boolean) - term->esc_query, and the 'query' parameter in toggle_mode in terminal.c, which _usually_ hold 0 for ESC[123h or 1 for ESC[?123h, but can also hold -1 for some other intervening character that we don't support. In a few places there's an integer that I haven't turned into a bool even though it really _can_ only take values 0 or 1 (and, as above, tried to make the call sites consistent in not calling those values true and false), on the grounds that I thought it would make it more confusing to imply that the 0 value was in some sense 'negative' or bad and the 1 positive or good: - the return value of plug_accepting uses the POSIXish convention of 0=success and nonzero=error; I think if I made it bool then I'd also want to reverse its sense, and that's a job for a separate piece of work. - the 'screen' parameter to lineptr() in terminal.c, where 0 and 1 represent the default and alternate screens. There's no obvious reason why one of those should be considered 'true' or 'positive' or 'success' - they're just indices - so I've left it as int. ssh_scp_recv had particularly confusing semantics for its previous int return value: its call sites used '<= 0' to check for error, but it never actually returned a negative number, just 0 or 1. Now the function and its call sites agree that it's a bool. In a couple of places I've renamed variables called 'ret', because I don't like that name any more - it's unclear whether it means the return value (in preparation) for the _containing_ function or the return value received from a subroutine call, and occasionally I've accidentally used the same variable for both and introduced a bug. So where one of those got in my way, I've renamed it to 'toret' or 'retd' (the latter short for 'returned') in line with my usual modern practice, but I haven't done a thorough job of finding all of them. Finally, one amusing side effect of doing this is that I've had to separate quite a few chained assignments. It used to be perfectly fine to write 'a = b = c = TRUE' when a,b,c were int and TRUE was just a the 'true' defined by stdbool.h, that idiom provokes a warning from gcc: 'suggest parentheses around assignment used as truth value'!
2018-11-02 19:23:19 +00:00
bool has_help(void)
{
return chm_path != NULL || chm_resource != NULL;
}
void launch_help(HWND hwnd, const char *topic)
{
if (!chm_path && chm_resource) {
/*
* If we've been called without already having a file name for
* the CHM file, that might be because we've located it in our
* resource section but not written it to a temp file yet. Do
* so now, on first use.
*/
load_chm_resource();
}
/* If we _still_ don't have a CHM pathname, we just can't display help. */
if (!chm_path)
return;
if (topic) {
char *fname = dupprintf(
"%s::/%s.html>main", chm_path, topic);
p_HtmlHelpA(hwnd, fname, HH_DISPLAY_TOPIC, 0);
sfree(fname);
} else {
p_HtmlHelpA(hwnd, chm_path, HH_DISPLAY_TOPIC, 0);
}
requested_help = true;
}
void quit_help(HWND hwnd)
{
if (requested_help)
p_HtmlHelpA(NULL, NULL, HH_CLOSE_ALL, 0);
if (chm_path && chm_created_by_us)
DeleteFile(chm_path);
}
#endif /* NO_HTMLHELP */