This repository has been archived by the owner on Dec 31, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsole.c
1481 lines (1319 loc) · 42.1 KB
/
Console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Console Plugin
Copyright (C) 2003-2004 Artur Dorochowicz
All Rights Reserved.
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "resources.h"
#include "version.h"
//---MACROS---------------------------------------------------------------------
#define MAX_VAR_LENGTH 63
#define MAX_CMD_LENGTH 531
#define PREVIOUS_COMMANDS_COUNT 11
#define WATCHES_COUNT 5
#define WATCH_WND_HEIGHT 150
#define TITLE_BAR_HEIGHT 20
#define SPACE_BETWEEN_WNDS 20 // space between watch window and edit control
#define ID_EDITBOX 200
#define ID_WATCHBOX 210
#define ID_EXITBTN 220
#define ID_MINIMIZEBTN 230
#define IDT_TIMER 300
#define ACTION_EXECUTE 1
#define ACTION_PRINT 2
#define ACTION_APPEND 3
#define MAIN_WND_BK_COLOR (RGB(236, 234, 220))
//------------------------------------------------------------------------------
typedef struct tagPProServices
{
void (* ErrMessage) (LPSTR, LPSTR);
BOOL (* MatchCaption) (HWND, LPSTR);
HWND (* FindMatchingWindow) (LPSTR,BOOL);
BOOL (* IsRolled) (HWND hw);
BOOL (* IsTrayMinned) (HWND hw);
void (* GetExeFullPath) (HWND hw, LPSTR szt);
void (* RollUp) (HWND hw);
void (* TrayMin) (HWND hw);
void (* SendKeys) (LPSTR sz);
BOOL (* EvalExpr) (LPSTR sz, LPSTR szo);
void (* Debug) (LPSTR sz1, LPSTR sz2,LPSTR sz3, LPSTR sz4, LPSTR sz5, LPSTR sz6);
LPSTR (* AllocTemp) (UINT leng);
void (* ReturnString) (LPSTR sz, LPSTR* szargs);
LPSTR (* GetVarAddr) (LPSTR var);
LPSTR (* SetVar) (LPSTR var, LPSTR val);
void (* IgnoreNextClip) ();
void (* Show) (HWND h);
void (* RunCmd) (LPSTR szCmd, LPSTR szParam, LPSTR szWork);
BOOL (* InsertStringForBar) ( LPSTR szStr, LPSTR szCmd);
void (* ResetFocus) ();
} PPROSERVICES;
//---GLOBAL VARIABLES-----------------------------------------------------------
const char wnd_class_name [] = "Console Plugin " CONSOLE_VERSION; // the main window class name
const char wnd_title [] = "Console Plugin"; // The title bar text
const unsigned char default_font [] = "Courier New"; // default font name
const unsigned char default_prompt [] = "?>"; // default prompt
PPROSERVICES * PProServices;
BOOL awaiting_input; // true if waiting for user input
BOOL watches_enabled; // true if watches are enabled
HBITMAP hbTitleBar = NULL; // bitmap for title bar
HBITMAP hbClose = NULL; // handle of bitmap on close button
HBITMAP hbMinimize = NULL; // handle of bitmap on minimize button
HBRUSH title_bar_brush = NULL; // brush for filling the title bar
HFONT hFont = NULL; // font handle for edit control
HINSTANCE instance = NULL; // current instance
HWND hEdit = NULL; // edit control window handle
HWND hExitBtn = NULL; // exit button handle
HWND hMainWnd = NULL; // main window handle
HWND hMinimizeBtn = NULL; // minimize button handle
HWND hWatch = NULL; // watch window handle
int buffer_size; // size of buffer storing contents of edit box
int cmd_line_start; // index of first character of current command line
int line_num; // stores line number of currently shown line
int line_num_for_next; // stores line number for saving next line
unsigned char * edit_ctrl_text = NULL; // buffer for edit control text
unsigned char ** previous_commands = NULL; // stores previous command lines
unsigned char ** watches = NULL; // stores variables to check
WNDPROC OrigEditWndProc; // original window procedure of edit control
// settings
unsigned char * prompt = NULL;
unsigned char * font_name = NULL;
int font_size;
int wnd_height;
int wnd_width;
int wnd_pos_left;
int wnd_pos_top;
BOOL font_bold;
BOOL copyright_note;
BOOL wnd_resizable;
//---EXTERN---------------------------------------------------------------------
extern int yyparse( void );
extern void yyrestart( FILE * );
//---DECLARATIONS---------------------------------------------------------------
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK EditWndProc (HWND, UINT, WPARAM, LPARAM);
BOOL AllocateMemory (void);
BOOL IsKeyword (const unsigned char *);
BOOL IsConsoleService (const unsigned char *);
BOOL SetDefaultSettings (void);
int DecreaseLineNum (int number);
int GetCaretPositionBySelection( void );
int GetCaretPositionByGetCaretPos( void );
int IncreaseLineNum (int number);
LRESULT OnKeyDown (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnKeyLeft (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnKeyUp (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnKeyEnter(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT OnKeyHome (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
unsigned char * GetNextCommand (void);
unsigned char * GetPreviousCommand (void);
void AddCommandToList (const unsigned char *);
void DeleteCmdLine( void );
void DisableWatches (void);
void EnableWatches (int interval);
void FreeResources (void);
void GetCmdLine( unsigned char * );
void MakeAction (int action, LPSTR, LPSTR, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD *, UINT, LPSTR *, PPROSERVICES *);
void MoveCaretToEnd( void );
void SetWatch( int watch_no, unsigned char * value );
void WriteCopyrightNote( void );
void WritePrompt( void );
void WriteText( HWND, const unsigned char * );
void ErrorMessage( const unsigned char * );
//---DEFINITIONS----------------------------------------------------------------
BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
instance = hinstDLL;
}
else
{
if (dwReason == DLL_PROCESS_DETACH)
{
HWND oldhWnd;
oldhWnd = FindWindow(wnd_class_name,NULL);
if (oldhWnd)
DestroyWindow(oldhWnd);
UnregisterClass(wnd_class_name, hinstDLL);
}
}
return TRUE;
}
//------------------------------------------------------------------------------
_declspec(dllexport) void show (LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
WNDCLASS wc;
HDC hdc;
HWND oldhWnd;
// return nothing
**szargs = '\0';
PProServices = ppsv;
// need 1 or 0 arguments
if (nargs >= 2)
{
ErrorMessage( "The service needs at most one argument." );
return;
}
// if window exists - exit
oldhWnd = FindWindow( wnd_class_name, NULL );
if (oldhWnd != NULL)
return;
// set initial values
awaiting_input = FALSE;
watches_enabled = FALSE;
line_num = 0;
line_num_for_next = 0;
buffer_size = 100000;
// load bitmaps
hbClose = LoadBitmap (instance, MAKEINTRESOURCE (IDB_CLOSE));
hbMinimize = LoadBitmap (instance, MAKEINTRESOURCE (IDB_MINIMIZE));
hbTitleBar = LoadBitmap (instance, MAKEINTRESOURCE (IDB_TITLE_BAR));
if (hbClose == NULL || hbMinimize == NULL || hbTitleBar == NULL)
{
ErrorMessage( "Cannot load bitmaps." );
FreeResources ();
return;
}
// create brush
title_bar_brush = CreatePatternBrush (hbTitleBar);
if (title_bar_brush == NULL)
{
ErrorMessage( "Cannot create brush." );
FreeResources ();
return;
}
// load default settings
if (FALSE == SetDefaultSettings ())
{
ErrorMessage( "Cannot allocate memory for default settings." );
FreeResources ();
return;
}
// if we have one argument then load settings from file
if (nargs == 1)
{
FILE * file;
file = fopen (szargs[1], "r");
if (file == NULL)
{
ErrorMessage( "Cannot open specified file." );
FreeResources ();
return;
}
yyrestart (file);
if (1 == yyparse ())
{
fclose (file);
// if got here because of allocation error
if (prompt == NULL || font_name == NULL)
{
ErrorMessage( "Cannot allocate memory for loaded settings." );
}
// it's really a syntax error
else
{
ErrorMessage( "Specified file has wrong format." );
}
FreeResources ();
return;
}
fclose (file);
}
// allocate memory
if ( FALSE == AllocateMemory( ) )
{
ErrorMessage( "Cannot allocate memory." );
FreeResources();
return;
}
// register class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = ( WNDPROC ) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = CreateSolidBrush( MAIN_WND_BK_COLOR );
wc.lpszMenuName = NULL;
wc.lpszClassName = wnd_class_name;
if ( !RegisterClass( &wc ) )
{
ErrorMessage( "Cannot register window class." );
FreeResources();
return;
}
// create font
hdc = GetDC( NULL );
hFont = CreateFont( -MulDiv(font_size, GetDeviceCaps(hdc, LOGPIXELSY), 72), // logical height of font
0, // logical average character width
0, // angle of escapement
0, // base-line orientation angle
(font_bold == FALSE) ? (FW_NORMAL) : (FW_BOLD), // font weight
FALSE, // italic attribute flag
FALSE, // underline attribute flag
FALSE, // strikeout attribute flag
DEFAULT_CHARSET, // character set identifier
OUT_DEFAULT_PRECIS, // output precision
CLIP_DEFAULT_PRECIS, // clipping precision
DEFAULT_QUALITY, // output quality
DEFAULT_PITCH | FF_DONTCARE, // pitch and family
font_name); // pointer to typeface name string
ReleaseDC (NULL, hdc);
if ( hFont == NULL )
{
ErrorMessage( "Cannot create requested font." );
FreeResources();
return;
}
// create main window
hMainWnd = CreateWindow (wnd_class_name, wnd_title,
(wnd_resizable == FALSE) ? (WS_POPUPWINDOW) : (WS_POPUPWINDOW | WS_SIZEBOX),
wnd_pos_left, wnd_pos_top,
wnd_width, wnd_height,
NULL, NULL, instance, NULL);
if (NULL == hMainWnd)
{
ErrorMessage( "Cannot create main window." );
FreeResources();
return;
}
ShowWindow(hMainWnd, SW_SHOW);
UpdateWindow(hMainWnd);
}
//------------------------------------------------------------------------------
_declspec(dllexport) void close (LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
HWND oldhWnd;
**szargs = '\0';
oldhWnd = FindWindow(wnd_class_name, NULL);
if (oldhWnd)
{
DestroyWindow(oldhWnd);
}
UnregisterClass(wnd_class_name, instance);
}
//------------------------------------------------------------------------------
_declspec(dllexport) void execute (LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
MakeAction (ACTION_EXECUTE, szv, szx, GetVar, SetVar, pFlags, nargs, szargs, ppsv);
}
//------------------------------------------------------------------------------
_declspec(dllexport) void print (LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
MakeAction (ACTION_PRINT, szv, szx, GetVar, SetVar, pFlags, nargs, szargs, ppsv);
}
//------------------------------------------------------------------------------
_declspec(dllexport) void append (LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
MakeAction (ACTION_APPEND, szv, szx, GetVar, SetVar, pFlags, nargs, szargs, ppsv);
}
//------------------------------------------------------------------------------
void MakeAction (int action, LPSTR szv, LPSTR szx, BOOL (*GetVar)(LPSTR, LPSTR), void (*SetVar)(LPSTR, LPSTR), DWORD * pFlags, UINT nargs, LPSTR * szargs, PPROSERVICES * ppsv)
{
HWND oldhWnd;
// return nothing
**szargs = '\0';
if (nargs != 1)
{
ErrorMessage( "The service needs exactly one argument" );
return;
}
oldhWnd = FindWindow (wnd_class_name, NULL);
if (oldhWnd == NULL)
{
// make show() believe that there are no arguments
// but pass szargs because show() writes to it anyway
show (szv, szx, GetVar, SetVar, pFlags, 0, szargs, ppsv);
}
SetForegroundWindow (hMainWnd);
switch (action)
{
case ACTION_EXECUTE:
awaiting_input = FALSE;
DeleteCmdLine( );
WriteText (hEdit, szargs[1]);
awaiting_input = TRUE;
SendMessage (hEdit, WM_CHAR, '\r', 0);
break;
case ACTION_PRINT:
awaiting_input = FALSE;
DeleteCmdLine( );
WriteText (hEdit, szargs[1]);
awaiting_input = TRUE;
break;
case ACTION_APPEND:
MoveCaretToEnd( );
awaiting_input = FALSE;
WriteText (hEdit, szargs[1]);
awaiting_input = TRUE;
break;
}
}
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
// exit button
hExitBtn = CreateWindow ("BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP | BS_FLAT,
0, 0, 0, 0, hWnd, ( HMENU ) ID_EXITBTN, instance, NULL );
SendMessage (hExitBtn, BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) (HANDLE) hbClose);
// minimize button
hMinimizeBtn = CreateWindow ("BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BITMAP | BS_FLAT,
0, 0, 0, 0, hWnd, ( HMENU ) ID_MINIMIZEBTN, instance, NULL );
SendMessage (hMinimizeBtn, BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) (HANDLE) hbMinimize);
// edit control
hEdit = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL| WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN,
0, 0, 0, 0, hWnd, (HMENU) ID_EDITBOX, (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE), NULL);
SendMessage (hEdit, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0));
// edit control for watches
hWatch = CreateWindow ("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN,
0, 0, 0, 0, hWnd, (HMENU) ID_WATCHBOX, instance, NULL);
SendMessage (hWatch, WM_SETFONT, (WPARAM) hFont, MAKELPARAM(TRUE, 0));
if (copyright_note == TRUE)
{
WriteCopyrightNote( );
}
WritePrompt( );
cmd_line_start = GetCaretPositionBySelection( );
OrigEditWndProc = (WNDPROC) SetWindowLong (hEdit, GWL_WNDPROC, (LONG) EditWndProc);
SetForegroundWindow (hWnd);
awaiting_input = TRUE;
break;
case WM_SETFOCUS:
SetFocus (hEdit);
break;
case WM_SIZE:
{
RECT wnd_rect;
GetWindowRect( hWnd, &wnd_rect );
wnd_width = wnd_rect.right - wnd_rect.left;
wnd_height = wnd_rect.bottom - wnd_rect.top;
// watches are enabled
if ( watches_enabled == TRUE )
{
wnd_height -= WATCH_WND_HEIGHT + SPACE_BETWEEN_WNDS;
MoveWindow( hEdit, 0, TITLE_BAR_HEIGHT, LOWORD(lParam), HIWORD(lParam) - WATCH_WND_HEIGHT - TITLE_BAR_HEIGHT - SPACE_BETWEEN_WNDS, TRUE );
MoveWindow( hWatch, 0, HIWORD(lParam) - WATCH_WND_HEIGHT, LOWORD(lParam), WATCH_WND_HEIGHT, TRUE );
}
// watches are disabled
else
{
MoveWindow( hEdit, 0, TITLE_BAR_HEIGHT, LOWORD(lParam), HIWORD(lParam) - TITLE_BAR_HEIGHT, TRUE );
MoveWindow( hWatch, 0, HIWORD(lParam) + SPACE_BETWEEN_WNDS, LOWORD(lParam), WATCH_WND_HEIGHT, TRUE );
}
MoveWindow( hExitBtn, LOWORD(lParam) - 2 - 16, 2, 16, 16, TRUE );
MoveWindow( hMinimizeBtn, LOWORD(lParam) - 2 - 16 - 1 - 16, 2, 16, 16, TRUE );
return 0;
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case ID_EXITBTN:
DestroyWindow(hWnd);
UnregisterClass(wnd_class_name, instance);
break;
case ID_MINIMIZEBTN:
CloseWindow (hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
{
HGDIOBJ old_obj;
RECT client_rect;
GetClientRect (hWnd, &client_rect);
hdc = BeginPaint(hWnd, &ps);
old_obj = SelectObject (hdc, title_bar_brush);
PatBlt (hdc, 0, 0, client_rect.right - client_rect.left - 2 - 16 - 1 - 16 - 2, TITLE_BAR_HEIGHT, PATCOPY);
SelectObject (hdc, old_obj);
EndPaint(hWnd, &ps);
return 0;
}
case WM_CLOSE:
DestroyWindow (hWnd);
UnregisterClass (wnd_class_name, instance);
return 0;
case WM_DESTROY:
SetWindowLong(hEdit, GWL_WNDPROC, (LONG) OrigEditWndProc);
KillTimer (hMainWnd, IDT_TIMER);
FreeResources ();
return 0;
case WM_NCHITTEST:
{
LRESULT result;
RECT WndRect;
GetWindowRect (hWnd, &WndRect);
result = DefWindowProc(hWnd, message, wParam, lParam);
if (result == HTCLIENT && (HIWORD(lParam) - WndRect.top) < TITLE_BAR_HEIGHT && (LOWORD(lParam) - WndRect.left) < (wnd_width - 2 - 16 - 1 - 16 - 2))
result = HTCAPTION;
return result;
}
case WM_TIMER:
switch (wParam)
{
case IDT_TIMER:
{
unsigned char cmd_result [MAX_CMD_LENGTH + 1];
unsigned char * value;
unsigned char num [20];
int i;
SendMessage (hWatch, WM_SETTEXT, 0, (LPARAM) NULL);
for (i = 0; i < WATCHES_COUNT; i++)
{
sprintf (num, "%d", i + 1);
WriteText (hWatch, num);
WriteText (hWatch, ". ");
if (_stricmp (watches[i], "") != 0)
{
WriteText (hWatch, watches[i]);
WriteText (hWatch, " = ");
value = (PProServices->GetVarAddr(watches[i]));
if (value != NULL)
{
WriteText (hWatch, value);
WriteText (hWatch, "\n");
}
else
{
WriteText (hWatch, "Error! No such variable.\n");
}
}
else
{
WriteText (hWatch, "No watch defined.\n");
}
}
for (i = 0; i < 32; i++)
{
sprintf (num, "pproflag(%d)", i);
PProServices->EvalExpr (num, cmd_result);
sprintf (num, "%2d(%c) ", i, cmd_result[0]);
WriteText (hWatch, num);
}
return 0;
} // case IDT_TIMER
}
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//------------------------------------------------------------------------------
LRESULT CALLBACK EditWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_RBUTTONDOWN:
SetFocus(hWnd);
return 0;
case WM_LBUTTONDOWN:
SetFocus(hWnd);
return 0;
case WM_LBUTTONDBLCLK:
SetFocus(hWnd);
return 0;
case WM_UNDO:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_HOME:
return OnKeyHome (hWnd, message, wParam, lParam);
case VK_LEFT:
return OnKeyLeft (hWnd, message, wParam, lParam);
case VK_UP:
return OnKeyUp (hWnd, message, wParam, lParam);
case VK_DOWN:
return OnKeyDown (hWnd, message, wParam, lParam);
default:
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
}
case WM_CHAR:
switch (wParam)
{
case '\r':
if (awaiting_input == TRUE)
{
return OnKeyEnter (hWnd, message, wParam, lParam);
}
else
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
case '\b':
if (awaiting_input == TRUE)
{
if ( cmd_line_start == GetCaretPositionByGetCaretPos( ) )
{
return 0;
}
else
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
}
else
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
default:
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
}
default:
return CallWindowProc(OrigEditWndProc, hWnd, message, wParam, lParam);
}
}
//------------------------------------------------------------------------------
void WriteText (HWND hwnd, const unsigned char * text)
{
int i;
int length = strlen (text);
for (i = 0; i < length; i++)
{
SendMessage (hwnd, WM_CHAR, text[i], 0);
}
}
//------------------------------------------------------------------------------
void WritePrompt( )
{
WriteText( hEdit, prompt );
}
//------------------------------------------------------------------------------
int IncreaseLineNum (int number)
{
if (number == PREVIOUS_COMMANDS_COUNT - 1)
{
return 0;
}
else
return number + 1;
}
//------------------------------------------------------------------------------
int DecreaseLineNum (int number)
{
if (number == 0)
{
return PREVIOUS_COMMANDS_COUNT - 1;
}
else
return number - 1;
}
//------------------------------------------------------------------------------
BOOL AllocateMemory()
{
int i;
edit_ctrl_text = (unsigned char *) malloc (buffer_size);
if (edit_ctrl_text == NULL) return FALSE;
watches = (unsigned char **) malloc (WATCHES_COUNT * sizeof(unsigned char *));
if (watches == NULL) return FALSE;
for (i = 0; i < WATCHES_COUNT; i++)
{
// calloc - because we need it be clear
watches[i] = (unsigned char *) calloc (MAX_VAR_LENGTH + 1, sizeof (unsigned char));
if (watches[i] == NULL) return FALSE;
}
previous_commands = (unsigned char **) malloc (PREVIOUS_COMMANDS_COUNT * sizeof(unsigned char *));
if (previous_commands == NULL) return FALSE;
for (i = 0; i < PREVIOUS_COMMANDS_COUNT; i++)
{
// calloc - because we need it be clear
previous_commands[i] = (unsigned char *) calloc (MAX_CMD_LENGTH + 1, sizeof (unsigned char));
if (previous_commands[i] == NULL) return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------
BOOL IsKeyword (const unsigned char * name)
// Not needed for PowerPro 3.8.02 and above
{
if ( _stricmp ("acdc", name) == 0 ) return TRUE;
if ( _stricmp ("allglobals", name) == 0 ) return TRUE;
if ( _stricmp ("alt", name) == 0 ) return TRUE;
if ( _stricmp ("batterypercent", name) == 0 ) return TRUE;
if ( _stricmp ("browserDomain", name) == 0 ) return TRUE;
if ( _stricmp ("browserSubdomain", name) == 0 ) return TRUE;
if ( _stricmp ("browserURL", name) == 0 ) return TRUE;
if ( _stricmp ("caption", name) == 0 ) return TRUE;
if ( _stricmp ("captionunder", name) == 0 ) return TRUE;
if ( _stricmp ("cdcurtrack", name) == 0 ) return TRUE;
if ( _stricmp ("cdlasttrack", name) == 0 ) return TRUE;
if ( _stricmp ("clip", name) == 0 ) return TRUE;
if ( _stricmp ("cliptrackon", name) == 0 ) return TRUE;
if ( _stricmp ("cpu", name) == 0 ) return TRUE;
if ( _stricmp ("ctrl", name) == 0 ) return TRUE;
if ( _stricmp ("currentdir", name) == 0 ) return TRUE;
if ( _stricmp ("date", name) == 0) return TRUE;
if ( _stricmp ("dayofweek", name) == 0 ) return TRUE;
if ( _stricmp ("dayofyear", name) == 0 ) return TRUE;
if ( _stricmp ("defaultprinter", name) == 0 ) return TRUE;
if ( _stricmp ("deskempty", name) == 0 ) return TRUE;
if ( _stricmp ("deskname", name) == 0 ) return TRUE;
if ( _stricmp ("desknum", name) == 0 ) return TRUE;
if ( _stricmp ("dialupname", name) == 0 ) return TRUE;
if ( _stricmp ("disk", name) == 0 ) return TRUE;
if ( _stricmp ("dunidle", name) == 0 ) return TRUE;
if ( _stricmp ("dunrate", name) == 0 ) return TRUE;
if ( _stricmp ("exefilename", name) == 0 ) return TRUE;
if ( _stricmp ("exefullpath", name) == 0 ) return TRUE;
if ( _stricmp ("gdi", name) == 0 ) return TRUE;
if ( _stricmp ("inputcolor", name) == 0 ) return TRUE;
if ( _stricmp ("inputdate", name) == 0 ) return TRUE;
if ( _stricmp ("inputdatetime", name) == 0 ) return TRUE;
if ( _stricmp ("inputfolder", name) == 0 ) return TRUE;
if ( _stricmp ("inputpath", name) == 0 ) return TRUE;
if ( _stricmp ("inputpathmulti", name) == 0 ) return TRUE;
if ( _stricmp ("inputtext", name) == 0 ) return TRUE;
if ( _stricmp ("keylog", name) == 0 ) return TRUE;
if ( _stricmp ("keylogfile", name) == 0 ) return TRUE;
if ( _stricmp ("lastactivehandle", name) == 0 ) return TRUE;
if ( _stricmp ("lastautorunhandle", name) == 0 ) return TRUE;
if ( _stricmp ("lastclipname", name) == 0 ) return TRUE;
if ( _stricmp ("lastclippath", name) == 0 ) return TRUE;
if ( _stricmp ("lastidletime", name) == 0 ) return TRUE;
if ( _stricmp ("longdate", name) == 0 ) return TRUE;
if ( _stricmp ("modem", name) == 0 ) return TRUE;
if ( _stricmp ("mouseleft", name) == 0 ) return TRUE;
if ( _stricmp ("mousemiddle", name) == 0 ) return TRUE;
if ( _stricmp ("mouseright", name) == 0 ) return TRUE;
if ( _stricmp ("muted", name) == 0 ) return TRUE;
if ( _stricmp ("paper", name) == 0 ) return TRUE;
if ( _stricmp ("pmem", name) == 0 ) return TRUE;
if ( _stricmp ("pprofolder", name) == 0 ) return TRUE;
if ( _stricmp ("pproversion", name) == 0 ) return TRUE;
if ( _stricmp ("processcount", name) == 0 ) return TRUE;
if ( _stricmp ("recycleItems", name) == 0 ) return TRUE;
if ( _stricmp ("recycleSize", name) == 0 ) return TRUE;
if ( _stricmp ("saver", name) == 0 ) return TRUE;
if ( _stricmp ("saveractive", name) == 0 ) return TRUE;
if ( _stricmp ("saverenabled", name) == 0 ) return TRUE;
if ( _stricmp ("savertime", name) == 0 ) return TRUE;
if ( _stricmp ("scriptfolder", name) == 0 ) return TRUE;
if ( _stricmp ("scriptname", name) == 0 ) return TRUE;
if ( _stricmp ("scriptpath", name) == 0 ) return TRUE;
if ( _stricmp ("shift", name) == 0 ) return TRUE;
if ( _stricmp ("shortdate", name) == 0) return TRUE;
if ( _stricmp ("Subbarname", name) == 0 ) return TRUE;
if ( _stricmp ("threadcount", name) == 0 ) return TRUE;
if ( _stricmp ("time", name) == 0 ) return TRUE;
if ( _stricmp ("timesec", name) == 0 ) return TRUE;
if ( _stricmp ("timezone", name) == 0 ) return TRUE;
if ( _stricmp ("unixdate", name) == 0 ) return TRUE;
if ( _stricmp ("uptime", name) == 0 ) return TRUE;
if ( _stricmp ("user", name) == 0 ) return TRUE;
if ( _stricmp ("volume", name) == 0 ) return TRUE;
if ( _stricmp ("win", name) == 0 ) return TRUE;
if ( _stricmp ("xmouse", name) == 0 ) return TRUE;
if ( _stricmp ("xscreen", name) == 0 ) return TRUE;
if ( _stricmp ("xtime", name) == 0 ) return TRUE;
if ( _stricmp ("ymouse", name) == 0 ) return TRUE;
if ( _stricmp ("yscreen", name) == 0 ) return TRUE;
return FALSE;
}
//------------------------------------------------------------------------------
unsigned char * GetNextCommand ()
{
int tmp;
if (line_num == line_num_for_next)
{
return NULL;
}
tmp = IncreaseLineNum (line_num);
if (tmp == line_num_for_next || strcmp(previous_commands[tmp], "") == 0)
{
return NULL;
}
else
{
line_num = tmp;
return previous_commands [line_num];
}
}
//------------------------------------------------------------------------------
unsigned char * GetPreviousCommand ()
{
int tmp = DecreaseLineNum (line_num);
if (tmp == line_num_for_next || strcmp(previous_commands[tmp], "") == 0)
{
return NULL;
}
else
{
line_num = tmp;
return previous_commands[line_num];
}
}
//------------------------------------------------------------------------------
void AddCommandToList (const unsigned char * cmd)
{
if (strcmp (cmd, "") != 0)
{
strcpy (previous_commands[line_num_for_next], cmd);
line_num_for_next = IncreaseLineNum (line_num_for_next);
}
line_num = line_num_for_next;
}
//------------------------------------------------------------------------------
int GetCaretPositionBySelection( )
{
int sel_start;
SendMessage( hEdit, EM_GETSEL, (WPARAM) &sel_start, 0 );
return sel_start;
}
//------------------------------------------------------------------------------
int GetCaretPositionByGetCaretPos( )
{
POINT point;
// if error getting caret's position then move caret to the end and return that new position
if (!GetCaretPos (&point))
{
int sel_start;
MoveCaretToEnd( );
SendMessage( hEdit, EM_GETSEL, (WPARAM) &sel_start, 0 );
return sel_start;
}
// if caret is not visible then move it to the end and return that new position
if (point.x < 0 || point.y < 0)
{
int sel_start;
MoveCaretToEnd( );
SendMessage( hEdit, EM_GETSEL, (WPARAM) &sel_start, 0 );
return sel_start;
}
return LOWORD( SendMessage( hEdit, EM_CHARFROMPOS, 0, MAKELPARAM( point.x, point.y ) ) );
}
//------------------------------------------------------------------------------
void MoveCaretToEnd( )
{
LRESULT length;
length = SendMessage( hEdit, WM_GETTEXTLENGTH, 0, 0 );
SendMessage( hEdit, EM_SETSEL, length, length );
}
//------------------------------------------------------------------------------
void GetCmdLine( unsigned char * target )
{
/* OLD WAY, LocalLock always returns NULL on Windows 98
LRESULT memhandle;
char * text;
int length;
memhandle = SendMessage (hwnd, EM_GETHANDLE, 0, 0);
text = LocalLock ( (HLOCAL) memhandle);
length = strlen (text) - start_position;
if (length > 531) length = 531;
memcpy (target, &(text[start_position]), length);
target[length] = '\0';
LocalUnlock ((HLOCAL) memhandle);
*/
LRESULT length;
length = SendMessage( hEdit, WM_GETTEXTLENGTH, 0, 0 );
length++; //for terminating NULL character;
if (length > buffer_size)
{
buffer_size = 2 * length;
free (edit_ctrl_text);
edit_ctrl_text = (unsigned char *) malloc (buffer_size);
if (edit_ctrl_text == NULL)
{
ErrorMessage( "Cannot increase size of the text buffer.\nThe Console Plugin will close now." );
strcpy (target, "exit");
return;
}
}
GetWindowText( hEdit, edit_ctrl_text, length );
length -= cmd_line_start;
if (length > 532)
length = 532;
memcpy (target, &(edit_ctrl_text[cmd_line_start]), length);
}
//------------------------------------------------------------------------------
void DeleteCmdLine( )
{
LRESULT length;
length = SendMessage( hEdit, WM_GETTEXTLENGTH, 0, 0);
MoveCaretToEnd( );
length -= cmd_line_start;
while (length-- > 0)
{
WriteText (hEdit, "\b");
}
}
//------------------------------------------------------------------------------
LRESULT OnKeyHome (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int current_position = GetCaretPositionByGetCaretPos( );
SHORT ctrl_pressed = GetAsyncKeyState (VK_CONTROL) & 0x8000;
SHORT shift_pressed = GetAsyncKeyState (VK_SHIFT) & 0x8000;
if ( ctrl_pressed && shift_pressed )
{
int sel_start;
int sel_end;
SendMessage (hWnd, EM_GETSEL, (WPARAM) &sel_start, (LPARAM)&sel_end);
if (sel_start == current_position)
{
SendMessage (hWnd, EM_SETSEL, (WPARAM) sel_end, (LPARAM) cmd_line_start);
}
else
SendMessage (hWnd, EM_SETSEL, (WPARAM) sel_start, (LPARAM) cmd_line_start);
return 0;
}
else if ( ctrl_pressed )
{
SendMessage (hWnd, EM_SETSEL, (WPARAM) cmd_line_start, (LPARAM) cmd_line_start);
return 0;
}
else if ( shift_pressed)
{
POINT caret_pos;
if (cmd_line_start == current_position)
return 0;
GetCaretPos (&caret_pos);
if (caret_pos.y == HIWORD(SendMessage (hWnd, EM_POSFROMCHAR, (WPARAM) cmd_line_start, 0)))
{
int sel_start, sel_end;
SendMessage (hWnd, EM_GETSEL, (WPARAM) &sel_start, (LPARAM)&sel_end);