1 #include <stdio.h>
2 #include <string.h>
3 #define WIN32_LEAN_AND_MEAN
4 #include <windows.h>
5 #undef WIN32_LEAN_AND_MEAN
6 #include <htmlhelp.h>
7 #include <io.h>
8 #include <tcl.h>
9 
10 /*---------- WinHtml -----------------------------------------------
11  * run windows HTML help
12  *------------------------------------------------------------------*/
13 
WinHtml(ClientData data,Tcl_Interp * interp,int argc,char ** argv)14 static int WinHtml (ClientData data, Tcl_Interp *interp,
15     int argc, char **argv)
16 {
17     int n;
18     char *p;
19     static char winname[65] = "";
20     static char hlpfile[256] = "";
21     static char usg_msg[] =
22         "WinHtml file CHMfile ?window?\n"
23         "WinHtml topic HTMLfile ?tag?\n"
24         "WinHtml index\n"
25         "WinHtml close";
26 
27     if (argc < 2) {
28         Tcl_SetResult (interp, usg_msg, TCL_STATIC);
29         return TCL_ERROR;
30     }
31     Tcl_ResetResult (interp);
32 
33     if (0 == strcmp ("close", argv[1])) {
34         HtmlHelp (NULL, NULL, HH_CLOSE_ALL, 0);
35         return TCL_OK;
36     }
37 
38     if (0 == strcmp ("file", argv[1])) {
39         if (argc < 3 || argc > 4) {
40             Tcl_SetResult (interp,
41                 "usage: WinHtml file CHMfile [window]", TCL_STATIC);
42             return TCL_ERROR;
43         }
44         p = strrchr (argv[2], '.');
45         if (NULL == p || _stricmp (p, ".chm") || _access (argv[2], 0)) {
46             *hlpfile = 0;
47             Tcl_AppendResult (interp, "CHM file \"", argv[2],
48                 "\" does not exist", NULL);
49             return TCL_ERROR;
50         }
51         if (strlen (argv[2]) >= sizeof(hlpfile)) {
52             Tcl_SetResult (interp, "CHM file pathname too long", TCL_STATIC);
53             return TCL_ERROR;
54         }
55         strcpy (hlpfile, argv[2]);
56         if (argc == 4)
57             strcpy (winname, argv[3]);
58         else
59             *winname = 0;
60         return TCL_OK;
61     }
62 
63     n = strlen (hlpfile);
64     if (!n) {
65         Tcl_SetResult (interp, "CHM help file not specified", TCL_STATIC);
66         return TCL_ERROR;
67     }
68 
69     if (0 == strcmp ("index", argv[1])) {
70         if (*winname)
71             sprintf (&hlpfile[n], ">%s", winname);
72         HtmlHelp (NULL, hlpfile, HH_DISPLAY_TOPIC, 0);
73         hlpfile[n] = 0;
74         return TCL_OK;
75     }
76 
77     if (strcmp ("topic", argv[1])) {
78         Tcl_SetResult (interp, usg_msg, TCL_STATIC);
79         return TCL_ERROR;
80     }
81 
82     if (argc < 3 || argc > 4) {
83         Tcl_SetResult (interp,
84            "usage: WinHtml topic HTMLfile [tag]", TCL_STATIC);
85         return TCL_ERROR;
86     }
87     sprintf (&hlpfile[n], "::/%s", argv[2]);
88     p = hlpfile + strlen (hlpfile);
89     if (argc == 4) {
90         sprintf (p, "#%s", argv[3]);
91         p = hlpfile + strlen (hlpfile);
92     }
93     if (*winname)
94         sprintf (p, ">%s", winname);
95     HtmlHelp (NULL, hlpfile, HH_DISPLAY_TOPIC, 0);
96     hlpfile[n] = 0;
97     return TCL_OK;
98 }
99 
100 /*----------------------------------------------------------------------*/
101 
102 #if defined(_WIN32) && defined(BUILD_DLL)
103 __declspec(dllexport)
104 #endif
WinHtml_Init(Tcl_Interp * interp)105 int WinHtml_Init(Tcl_Interp *interp)
106 {
107     Tcl_CreateCommand (interp, "WinHtml", (Tcl_CmdProc *)WinHtml,
108         (ClientData)0, (Tcl_CmdDeleteProc *)0);
109     return TCL_OK;
110 }
111 
112