Sebastian Kuschel reports that pfd_closing can be called for a socket
[u/mdw/putty] / windows / winhelp.c
1 /*
2 * winhelp.c: centralised functions to launch Windows help files,
3 * and to decide whether to use .HLP or .CHM help in any given
4 * situation.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #include "putty.h"
13
14 #ifndef NO_HTMLHELP
15 #include <htmlhelp.h>
16 #endif /* NO_HTMLHELP */
17
18 static int requested_help;
19 static char *help_path;
20 static int help_has_contents;
21 #ifndef NO_HTMLHELP
22 DECL_WINDOWS_FUNCTION(static, HWND, HtmlHelpA, (HWND, LPCSTR, UINT, DWORD));
23 static char *chm_path;
24 #endif /* NO_HTMLHELP */
25
26 void init_help(void)
27 {
28 char b[2048], *p, *q, *r;
29 FILE *fp;
30
31 GetModuleFileName(NULL, b, sizeof(b) - 1);
32 r = b;
33 p = strrchr(b, '\\');
34 if (p && p >= r) r = p+1;
35 q = strrchr(b, ':');
36 if (q && q >= r) r = q+1;
37 strcpy(r, PUTTY_HELP_FILE);
38 if ( (fp = fopen(b, "r")) != NULL) {
39 help_path = dupstr(b);
40 fclose(fp);
41 } else
42 help_path = NULL;
43 strcpy(r, PUTTY_HELP_CONTENTS);
44 if ( (fp = fopen(b, "r")) != NULL) {
45 help_has_contents = TRUE;
46 fclose(fp);
47 } else
48 help_has_contents = FALSE;
49
50 #ifndef NO_HTMLHELP
51 strcpy(r, PUTTY_CHM_FILE);
52 if ( (fp = fopen(b, "r")) != NULL) {
53 chm_path = dupstr(b);
54 fclose(fp);
55 } else
56 chm_path = NULL;
57 if (chm_path) {
58 HINSTANCE dllHH = load_system32_dll("hhctrl.ocx");
59 GET_WINDOWS_FUNCTION(dllHH, HtmlHelpA);
60 if (!p_HtmlHelpA) {
61 chm_path = NULL;
62 if (dllHH)
63 FreeLibrary(dllHH);
64 }
65 }
66 #endif /* NO_HTMLHELP */
67 }
68
69 void shutdown_help(void)
70 {
71 /* Nothing to do currently.
72 * (If we were running HTML Help single-threaded, this is where we'd
73 * call HH_UNINITIALIZE.) */
74 }
75
76 int has_help(void)
77 {
78 /*
79 * FIXME: it would be nice here to disregard help_path on
80 * platforms that didn't have WINHLP32. But that's probably
81 * unrealistic, since even Vista will have it if the user
82 * specifically downloads it.
83 */
84 return (help_path != NULL
85 #ifndef NO_HTMLHELP
86 || chm_path
87 #endif /* NO_HTMLHELP */
88 );
89 }
90
91 void launch_help(HWND hwnd, const char *topic)
92 {
93 if (topic) {
94 int colonpos = strcspn(topic, ":");
95
96 #ifndef NO_HTMLHELP
97 if (chm_path) {
98 char *fname;
99 assert(topic[colonpos] != '\0');
100 fname = dupprintf("%s::/%s.html>main", chm_path,
101 topic + colonpos + 1);
102 p_HtmlHelpA(hwnd, fname, HH_DISPLAY_TOPIC, 0);
103 sfree(fname);
104 } else
105 #endif /* NO_HTMLHELP */
106 if (help_path) {
107 char *cmd = dupprintf("JI(`',`%.*s')", colonpos, topic);
108 WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
109 sfree(cmd);
110 }
111 } else {
112 #ifndef NO_HTMLHELP
113 if (chm_path) {
114 p_HtmlHelpA(hwnd, chm_path, HH_DISPLAY_TOPIC, 0);
115 } else
116 #endif /* NO_HTMLHELP */
117 if (help_path) {
118 WinHelp(hwnd, help_path,
119 help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
120 }
121 }
122 requested_help = TRUE;
123 }
124
125 void quit_help(HWND hwnd)
126 {
127 if (requested_help) {
128 #ifndef NO_HTMLHELP
129 if (chm_path) {
130 p_HtmlHelpA(NULL, NULL, HH_CLOSE_ALL, 0);
131 } else
132 #endif /* NO_HTMLHELP */
133 if (help_path) {
134 WinHelp(hwnd, help_path, HELP_QUIT, 0);
135 }
136 requested_help = FALSE;
137 }
138 }