-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplugin.cpp
648 lines (544 loc) · 15.5 KB
/
plugin.cpp
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
/*
This file is part of NppEventExec
Copyright (C) 2016-2017 Mihail Ivanchev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "base.h"
#include "plugin.h"
#include "csv.h"
#include "mem.h"
#include "match.h"
#include "rule.h"
#include "edit_dlg.h"
#include "rules_dlg.h"
#include "util.h"
#include "Scintilla.h"
#include "exec.h"
#include "resource.h"
#include "about_dlg.h"
#include "queue_dlg.h"
#include "PluginInterface.h"
#include "nppexec_msgs.h"
#ifdef DEBUG
#include <time.h>
#endif
static void initPlugin(NppData data);
static void deinitPlugin(void);
static bool validateModuleName(wchar_t **dir);
static wchar_t* queryConfigDir(void);
static void onEditRules(void);
static void onExecQueue(void);
static void onAbout(void);
static wchar_t *pluginDir;
static wchar_t *configDir;
static HINSTANCE pluginInst;
static HWND nppWnd;
static const TCHAR NPP_PLUGIN_NAME[] = PLUGIN_NAME;
static FuncItem menuItems[] = {
{ L"Edit rules...", onEditRules },
{ L"Execution queue...", onExecQueue },
{ L"", NULL }, // Separator
{ L"About...", onAbout }
};
static bool nppExecLoaded;
static bool initFailed;
static Rule *rules;
void initPlugin(NppData data)
{
nppWnd = data._nppHandle;
if (!validateModuleName(&pluginDir))
{
/* TODO error */
errorMsgBox(NULL,
L"Failed to validate the plugin's module path. The plugin "
L"will not function until the issues are resolved.");
goto fail_mod;
}
if (!(configDir = queryConfigDir()))
{
/* TODO error */
errorMsgBox(NULL,
L"Failed to query Notepad++'s configuration directory. The "
L"plugin will not function until the issues are resolved.");
goto fail_config;
}
if (readRules(&rules))
{
/* TODO error */
errorMsgBox(NULL,
L"Failed to read the rules list. The plugin will not "
L"function until the issues are resolved.");
goto fail_rules;
}
initFailed = false;
return;
fail_rules:
freeStr(configDir);
fail_config:
freeStr(pluginDir);
fail_mod:
initFailed = true;
}
void deinitPlugin(void)
{
freeRules(rules);
freeStr(configDir);
freeStr(pluginDir);
}
bool validateModuleName(wchar_t **dir)
{
wchar_t *path;
wchar_t *tmp;
DWORD len;
DWORD maxLen;
wchar_t *fname;
assert(MAX_PATH < SIZE_MAX);
assert(MAX_PATH < (DWORD) -1);
assert(dir);
len = MAX_PATH + 1;
maxLen = MIN(SIZE_MAX, (DWORD) -1);
if (!(path = allocStr(len)))
{
/* TODO error */
goto fail_alloc;
}
while (!GetModuleFileNameW(pluginInst, path, len))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
if (len == maxLen)
{
/* TODO error */
goto fail_too_long;
}
else if (len > maxLen - MAX_PATH)
len = maxLen;
else
len += MAX_PATH;
if (!(tmp = reallocStr(path, len)))
{
/* TODO error */
goto fail_realloc;
}
path = tmp;
}
else
{
/* TODO error */
goto fail_read;
}
}
fname = getFilename(path);
if (wcscmp(fname, PLUGIN_MODULE))
{
/* TODO error */
goto fail_invalid;
}
*(fname - 1) = L'\0';
if (!(*dir = reallocStr(path, wcslen(path) + 1)))
{
/* TODO error */
goto fail_resize;
}
return true;
fail_resize:
fail_invalid:
fail_read:
fail_realloc:
fail_too_long:
freeStr(path);
fail_alloc:
return false;
}
wchar_t* queryConfigDir(void)
{
wchar_t *dir;
wchar_t *tmp;
WPARAM len;
WPARAM maxLen;
assert(MAX_PATH < SIZE_MAX);
assert(MAX_PATH < (WPARAM) -1);
assert(MAX_PATH >= 4);
len = MAX_PATH + 1;
maxLen = MIN(SIZE_MAX, (WPARAM) -1);
if (!(dir = allocStr(len)))
{
/* TODO error */
goto fail_alloc;
}
StringCchCopyW(dir, len, L"\\\\?\\");
while (!sendNppMsg(NPPM_GETPLUGINSCONFIGDIR, len - 5,
reinterpret_cast<LPARAM>(&dir[4])))
{
/* Just assume the problem is the insufficient length although it could
** be something else. No way to know.
*/
if (len == maxLen)
{
/* TODO error */
goto fail_too_long;
}
else if (len > maxLen - MAX_PATH)
len = maxLen;
else
len += MAX_PATH;
if (!(tmp = reallocStr(dir, len)))
{
/* TODO error */
goto fail_realloc;
}
dir = tmp;
}
if (!(tmp = reallocStr(dir, wcslen(dir) + 1)))
{
/* TODO error */
goto fail_resize;
}
return tmp;
fail_resize:
fail_realloc:
fail_too_long:
freeStr(dir);
fail_alloc:
return NULL;
}
bool queryNppExecLoaded(void)
{
DWORD ver;
return sendNppExecMsg(NPEM_GETVERDWORD,
reinterpret_cast<void*>(&ver)) == TRUE;
}
void execRules(uptr_t bufId, unsigned int code)
{
LRESULT unitCnt;
size_t unitCntSizeT;
wchar_t *path;
unitCnt = sendNppMsg(NPPM_GETFULLPATHFROMBUFFERID,
static_cast<WPARAM>(bufId),
static_cast<LPARAM>(NULL));
/* Let's not assume that Notepad++ keeps the path string in a contiguous
** region of memory in a zero-terminated fashion. The conversion to size_t
** for the validation is based on the fact that conversion to an unsigned
** integer is always defined behaviour.
*/
unitCntSizeT = static_cast<size_t>(unitCnt);
if (static_cast<LRESULT>(unitCntSizeT) != unitCnt
|| unitCntSizeT == SIZE_MAX)
{
/* TODO error */
goto fail_path_too_long;
}
if (!(path = allocStr(unitCntSizeT + 1)))
{
/* TODO error */
goto fail_alloc;
}
sendNppMsg(NPPM_GETFULLPATHFROMBUFFERID,
static_cast<WPARAM>(bufId),
reinterpret_cast<LPARAM>(path));
for (Rule *rule = rules; rule; rule = rule->next)
{
if (rule->event == code
&& rule->enabled
&& isRegexMatch(rule->regex, path))
{
if (execRule(bufId, path, rule))
{
/* TODO error */
}
}
}
freeStr(path);
return;
fail_alloc:
fail_path_too_long:
return;
}
void onEditRules(void)
{
if (openRulesDlg(&rules))
{
/* TODO error */
errorMsgBox(nppWnd, L"Failed to open the rule management dialog.");
}
}
void onExecQueue(void)
{
if (openQueueDlg(getNppWnd(), QDLR_PLUGIN_MENU) == -1)
{
/* TODO error */
errorMsgBox(nppWnd, L"Failed to open the execution queue dialog.");
}
}
void onAbout(void)
{
openAboutDlg();
}
int isPluginInit(void)
{
return !initFailed;
}
LRESULT sendNppMsg(UINT msg, WPARAM wp, LPARAM lp)
{
return SendMessage(nppWnd, msg, wp, lp);
}
LRESULT sendNppExecMsg(long msg, void *info)
{
CommunicationInfo ci = { msg, PLUGIN_MODULE, info };
return sendNppMsg(NPPM_MSGTOPLUGIN,
reinterpret_cast<WPARAM>(NPPEXEC_PLUGIN_MODULE),
reinterpret_cast<LPARAM>(&ci));
}
wchar_t* getPluginDir(void)
{
return pluginDir;
}
wchar_t* getPluginConfigDir(void)
{
return configDir;
}
HINSTANCE getPluginInstance(void)
{
return pluginInst;
}
HWND getNppWnd(void)
{
return nppWnd;
}
BOOL APIENTRY DllMain(HINSTANCE inst,
DWORD reason,
LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
pluginInst = inst;
/* The initialization occurs once Notepad++ informs us about its
** environment through the setInfo method.
*/
break;
case DLL_PROCESS_DETACH:
/* The deinitialization occurs in the shutdown event handler because of
** complications when synchronizing threads here.
**
** https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx
*/
break;
}
return TRUE;
}
#ifdef DEBUG
LRESULT CALLBACK WndProc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
wchar_t *path;
size_t len;
CommunicationInfo *ci;
DWORD *state;
Rule *rule;
switch(msg)
{
case WM_CLOSE:
DestroyWindow(wnd);
break;
case WM_DESTROY:
if (!isQueueEmpty())
emptyQueue();
deinitPlugin();
if (allocatedBytes)
printf("Unallocated memory: %lu bytes\n", allocatedBytes);
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if (wp == VK_ESCAPE)
DestroyWindow(wnd);
else
{
for (rule = rules; rule; rule = rule->next)
execRule(0, L"C:\\foo.txt", rule);
// openQueueDlg(getNppWnd(), QDLR_PLUGIN_MENU);
// openAboutDlg();
// openRulesDlg(&rules);
}
break;
case NPPM_MSGTOPLUGIN:
ci = (CommunicationInfo*) lp;
if (ci->internalMsg == NPEM_NPPEXEC)
((NpeNppExecParam*) ci->info)->dwResult = NPE_EXECUTE_FAILED;
else if (ci->internalMsg == NPEM_GETSTATE)
{
state = static_cast<DWORD*>(ci->info);
// if (rand() < RAND_MAX / 5)
// *state = NPE_EXECUTE_OK;
// else
*state = NPE_EXECUTE_FAILED;
}
break;
case NPPM_GETFULLPATHFROMBUFFERID:
switch (wp)
{
case 0:
path = copyStr(L"C:\\Users\\BAREMETAL\\Desktop\\recipes");
break;
case 1:
path = copyStr(L"E:\\Develop");
break;
case 2:
path = copyStr(L"E:\\Software");
break;
case 3:
path = copyStr(L"E:\\Servers\\xampp");
break;
default:
path = copyStr(L"D:\\notes\\on-rare-plants.docx");
}
len = wcslen(path);
if ((wchar_t*) lp)
{
StringCchCopyW(reinterpret_cast<wchar_t*>(lp), len + 1, path);
}
freeStr(path);
return static_cast<LRESULT>(len);
default:
return DefWindowProc(wnd, msg, wp, lp);
}
return 0;
}
int main(int argc, char *argv[])
{
MSG msg;
srand(static_cast<UINT>(time(NULL)));
pluginInst = GetModuleHandle(NULL);
configDir = copyStr(L"config");
pluginDir = copyStr(L".");
readRules(&rules);
printRules(rules);
WNDCLASSEXW wcex;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = pluginInst;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"myWindowClass";
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassExW(&wcex);
nppWnd = CreateWindowExW(WS_EX_CLIENTEDGE,
L"myWindowClass",
L"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
240,
120,
NULL,
NULL,
pluginInst,
NULL);
ShowWindow(nppWnd, SW_SHOW);
UpdateWindow(nppWnd);
SetWindowPos(nppWnd, NULL, 400, 400, 0, 0, SWP_NOSIZE);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
#endif /* DEBUG */
/******************************************************************************\
* *
* Notepad++ plugin interface *
* *
\******************************************************************************/
void setInfo(NppData data)
{
initPlugin(data);
}
const TCHAR* getName()
{
return NPP_PLUGIN_NAME;
}
FuncItem* getFuncsArray(int *nbF)
{
*nbF = sizeof(menuItems) / sizeof(menuItems[0]);
return menuItems;
}
void beNotified(SCNotification *notification)
{
Sci_NotifyHeader *hdr;
HMENU menu;
size_t ii;
hdr = ¬ification->nmhdr;
if (reinterpret_cast<HWND>(hdr->hwndFrom) != nppWnd)
return;
if (hdr->code == NPPN_READY)
{
if (!isPluginInit())
{
menu = reinterpret_cast<HMENU>(sendNppMsg(NPPM_GETMENUHANDLE,
NPPPLUGINMENU,
0));
for (ii = 0; ii < BUFLEN(menuItems); ii++)
{
EnableMenuItem(menu, static_cast<UINT>(menuItems[ii]._cmdID),
MF_BYCOMMAND | MF_GRAYED);
}
return;
}
if (!(nppExecLoaded = queryNppExecLoaded()))
{
errorMsgBox(nppWnd,
L"NppExec was not loaded. No scripts will be executed "
L"until the issues are resolved.");
return;
}
}
else if (!isPluginInit())
return;
if (nppExecLoaded)
execRules(hdr->idFrom, hdr->code);
if (hdr->code == NPPN_SHUTDOWN)
{
if (!isQueueEmpty()
&& openQueueDlg(getNppWnd(), QDLR_NPP_CLOSING) == -1)
{
/* TODO error */
emptyQueue();
errorMsgBox(nppWnd,
L"The remaining rules were forcefully terminated, "
L"because Notepad++ is shutting down and the wait "
L"dialog failed to launch.");
}
deinitPlugin();
#ifdef DEBUG
if (allocatedBytes)
{
errorMsgBox(nppWnd,
L"Unallocated memory: %lu bytes",
allocatedBytes);
}
#endif
}
}
LRESULT messageProc(UINT msg, WPARAM wp, LPARAM lp)
{
return TRUE;
}
BOOL isUnicode()
{
return TRUE;
}