1 /* HERCLIN.C    (c) Copyright Ivan Warren, 2005-2009                 */
2 /*              Hercules Line Mode Console                           */
3 
4 /************************************************/
5 /* (C) Copyright 2005-2009 Roger Bowler & Others*/
6 /* Initial author : Ivan Warren                 */
7 /*                                              */
8 /* HERCLIN.C                                    */
9 /*                                              */
10 /* THIS IS SAMPLE CODE DEMONSTRATING            */
11 /* THE USE OF THE INITIAL HARDWARE PANEL        */
12 /* INTERFACE FEATURE.                           */
13 /************************************************/
14 
15 #ifdef _MSVC_
16 #include <windows.h>
17 #include <conio.h>
18 #endif
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "hextapi.h"
23 
24 #if defined(HDL_USE_LIBTOOL)
25 
26 /* This must be included if HDL uses the   */
27 /* libtool ltdl convenience library        */
28 
29 #include "ltdl.h"
30 #endif
31 
32 
33 /**********************************************/
34 /* The following function is the LOG callback */
35 /* function. It gets called by the engine     */
36 /* whenever a log message needs to be         */
37 /* displayed. This function may therefore be  */
38 /* invoked from a separate thread             */
39 /*                                            */
40 /* This simple version simply writes to STDOUT*/
41 /**********************************************/
42 
43 #ifdef _MSVC_
44 static HANDLE hLogCallbackThread = NULL;
45 #endif
46 
mywrite(const char * a,size_t b)47 void mywrite(const char *a,size_t b)
48 {
49 #ifdef _MSVC_
50     if (!hLogCallbackThread)
51         hLogCallbackThread = OpenThread(
52             SYNCHRONIZE, FALSE,
53             GetCurrentThreadId());
54 #endif
55 
56     fflush(stdout);
57     fwrite(a,b,1,stdout);
58     fflush(stdout);
59 }
60 
61 
main(int ac,char ** av)62 int main(int ac,char **av)
63 {
64     /*****************************************/
65     /* COMMANDHANDLER is the function type   */
66     /* of the engine's panel command handler */
67     /* this MUST be resolved at run time     */
68     /* since some HDL module might have      */
69     /* redirected the initial engine function*/
70     /*****************************************/
71 
72     COMMANDHANDLER  ch;
73     char *str,*bfr;
74 
75 #if defined( OPTION_DYNAMIC_LOAD ) && defined( HDL_USE_LIBTOOL )
76     /* LTDL Preloaded symbols for HDL using libtool */
77     LTDL_SET_PRELOADED_SYMBOLS();
78 #endif
79 
80     /******************************************/
81     /* Register the 'mywrite' function as the */
82     /* log callback routine                   */
83     /******************************************/
84     registerLogCallback(mywrite);
85 
86     /******************************************/
87     /* Initialize the HERCULE Engine          */
88     /******************************************/
89     impl(ac,av);
90 
91     /******************************************/
92     /* Get the command handler function       */
93     /* This MUST be done after IML            */
94     /******************************************/
95     ch=getCommandHandler();
96 
97     /******************************************/
98     /* Read STDIN and pass to Command Handler */
99     /******************************************/
100     bfr=(char *)malloc(1024);
101     while
102     (
103 #ifdef _MSVC_
104         !hLogCallbackThread ||
105         WaitForSingleObject(hLogCallbackThread,0)
106             != WAIT_OBJECT_0
107 #else
108         1
109 #endif
110     )
111     {
112 #ifdef _MSVC_
113         if (!kbhit()) Sleep(50); else
114 #endif
115         if ((str=fgets(bfr,1024,stdin)))
116         {
117             str[strlen(str)-1]=0;
118             ch(str);
119         }
120     }
121 #ifdef _MSVC_
122     CloseHandle(hLogCallbackThread);
123 #endif
124     return 0;
125 }
126 
127