-
Notifications
You must be signed in to change notification settings - Fork 2
/
exec.c
463 lines (371 loc) · 9.56 KB
/
exec.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
/*
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 "rule.h"
#include "Scintilla.h"
#include "exec.h"
#include "exec_def.h"
#include "Notepad_plus_msgs.h"
#include "nppexec_msgs.h"
#include "mem.h"
#include "plugin.h"
#include "queue_dlg.h"
#include "resource.h"
#include "util.h"
/** TODO */
#define UPDATE_INTERVAL_IN_MS 100
typedef struct _Exec
{
const Rule *rule;
ExecState state;
struct _Exec *next;
wchar_t *path;
wchar_t args[];
} Exec;
static Exec* allocExec(size_t pathLen, uptr_t bufIdDigitCnt, size_t *argsLen);
static bool initArgs(Exec *exec,
size_t argsLen,
size_t pathLen,
const wchar_t *path,
uptr_t bufIdDigitCnt,
uptr_t bufId);
static uptr_t countBufIdDigits(uptr_t bufId);
static void CALLBACK timerProc(HWND wnd,
UINT msg,
UINT_PTR timerId,
DWORD sysTime);
static Exec* getExecAt(int pos);
static struct
{
Exec *first;
Exec *last;
unsigned int size;
unsigned int foregroundCnt;
UINT_PTR timerId;
} queue;
int execRule(uptr_t bufId, const wchar_t *path, const Rule *rule)
{
Exec *exec;
Exec *prevExec;
uptr_t bufIdDigitCnt;
size_t pathLen;
size_t argsLen;
assert(path);
assert(rule);
if (queue.size == INT_MAX)
{
/* TODO error */
goto fail_too_many_execs;
}
queue.size++;
queue.foregroundCnt += !rule->background;
pathLen = wcslen(path);
bufIdDigitCnt = countBufIdDigits(bufId);
if (!(exec = allocExec(pathLen, bufIdDigitCnt, &argsLen)))
{
/* TODO error */
goto fail_alloc;
}
if (!initArgs(exec, argsLen, pathLen, path, bufIdDigitCnt, bufId))
{
/* TODO error */
goto fail_args;
}
exec->rule = rule;
exec->state = STATE_QUEUED;
exec->next = NULL;
if (!queue.first)
{
if (!(queue.timerId
= SetTimer(NULL, 0, UPDATE_INTERVAL_IN_MS, timerProc)))
{
/* TODO error */
goto fail_timer;
}
/* Get rid of a 'maybe uninitialized' warning. */
prevExec = NULL;
queue.first = exec;
queue.last = exec;
/* If we're adding a background rule, try to pass it to Notepad++ for
** execution immediately. However do NOT schedule a background rule at
** this point, because the blocking dialog must be opened and this
** could fail. Instead, the block dialog's initialization code is
** responsible for updating the queue.
*/
if (rule->background)
updateQueue();
}
else
{
prevExec = queue.last;
queue.last->next = exec;
queue.last = exec;
}
if (isQueueDlgVisible())
{
processQueueEvent(
rule->background ? QUEUE_ADD_BACKGROUND : QUEUE_ADD_FOREGROUND);
}
else if (!rule->background
&& openQueueDlg(getNppWnd(), QDLR_FOREGROUND_RULE) == -1)
{
/* TODO launch dialog */
goto fail_dlg;
}
return 0;
fail_dlg:
if (!prevExec)
{
stopQueue();
queue.first = NULL;
queue.last = NULL;
}
else
{
queue.last = prevExec;
queue.last->next = NULL;
}
fail_timer:
fail_args:
freeMem(exec);
fail_alloc:
queue.foregroundCnt -= !rule->background;
queue.size--;
fail_too_many_execs:
return 1;
}
void emptyQueue(void)
{
Exec *exec;
Exec *next;
assert(queue.size);
assert(!queue.foregroundCnt);
exec = queue.first;
do
{
next = exec->next;
freeMem(exec);
exec = next;
}
while (exec);
stopQueue();
queue.first = NULL;
queue.last = NULL;
queue.size = 0;
}
int isQueueEmpty(void)
{
return !queue.size;
}
void updateQueue(void)
{
Exec *next;
NpeNppExecParam npep;
DWORD state;
assert(queue.size);
if (queue.first->state == STATE_EXECUTING)
{
sendNppExecMsg(NPEM_GETSTATE, &state);
if (state != NPE_STATEREADY)
return;
queue.size--;
queue.foregroundCnt -= !queue.first->rule->background;
next = queue.first->next;
freeMem(queue.first);
queue.first = next;
if (!next)
{
stopQueue();
queue.last = NULL;
return;
}
/* The state is updated below. */
}
npep.szScriptName = queue.first->rule->cmd;
npep.szScriptArguments = queue.first->args;
npep.dwResult = 0;
sendNppExecMsg(NPEM_NPPEXEC, &npep);
queue.first->state = npep.dwResult ==
NPE_EXECUTE_OK ? STATE_EXECUTING : STATE_WAITING;
}
void stopQueue(void)
{
KillTimer(NULL, queue.timerId);
}
unsigned int getQueueSize(unsigned int *foregroundCnt)
{
*foregroundCnt = queue.foregroundCnt;
return queue.size;
}
void abortExecs(int *positions)
{
Exec *prevExec;
Exec *currExec;
Exec *nextExec;
int adjustedPos;
int ii;
assert(positions);
prevExec = NULL;
currExec = queue.first;
adjustedPos = 0;
for (ii = 0; positions[ii] != -1; ii++)
{
for (; adjustedPos < positions[ii] - ii; adjustedPos++)
{
prevExec = currExec;
currExec = currExec->next;
}
queue.foregroundCnt -= !currExec->rule->background;
nextExec = currExec->next;
freeMem(currExec);
currExec = nextExec;
if (prevExec)
prevExec->next = currExec;
else
queue.first = currExec;
}
if (!currExec && !(queue.last = prevExec))
stopQueue();
queue.size -= ii;
}
const wchar_t* getExecRule(unsigned int pos)
{
assert(pos < queue.size);
return getExecAt(pos)->rule->name;
}
ExecState getExecState(unsigned int pos)
{
assert(pos < queue.size);
return getExecAt(pos)->state;
}
const wchar_t* getExecPath(unsigned int pos)
{
assert(pos < queue.size);
return getExecAt(pos)->path;
}
int isExecForeground(unsigned int pos)
{
assert(pos < queue.size);
return !getExecAt(pos)->rule->background;
}
bool initArgs(Exec *exec,
size_t argsLen,
size_t pathLen,
const wchar_t *path,
uptr_t bufIdDigitCnt,
uptr_t bufId)
{
assert(argsLen);
assert(pathLen);
assert(path);
assert(bufIdDigitCnt);
if (bufIdDigitCnt > INT_MAX)
{
/* TODO error */
return false;
}
StringCchPrintfW(exec->args,
argsLen,
L"\"%*s\" \"%s\"%c%s",
(int) bufIdDigitCnt,
L"",
path,
L'\0',
path);
/* Copy the buffer ID the hard way, because its type is not supported by
** *PrintfW or we don't know which specifier to use.
*/
do
{
exec->args[bufIdDigitCnt--] = L'0' + (bufId % 10);
}
while (bufId /= 10, bufIdDigitCnt);
exec->path = exec->args + wcslen(exec->args);
return true;
}
Exec* allocExec(size_t pathLen, uptr_t bufIdDigitCnt, size_t *argsLen)
{
Exec *exec;
size_t len;
assert(pathLen);
assert(bufIdDigitCnt);
assert(argsLen);
/* Check if we can fit all the string data we need to store. The magic
** number 7 is the extra space required to store 4 double quotes ("),
** 1 space and 2 null characters (\0).
*/
if (pathLen > (SIZE_MAX - 7) / 2
|| bufIdDigitCnt > SIZE_MAX - 7 - 2 * pathLen)
{
/* TODO error */
return NULL;
}
len = 7 + 2 * pathLen + bufIdDigitCnt;
if (len > (SIZE_MAX - sizeof *exec) / sizeof exec->args[0])
{
/* TODO error */
return NULL;
}
/* Allocate memory for the struct and the trailing string buffer. */
if (!(exec = allocMem(sizeof *exec + len * sizeof exec->args[0])))
{
/* TODO error */
return NULL;
}
*argsLen = len;
return exec;
}
uptr_t countBufIdDigits(uptr_t bufId)
{
uptr_t cnt;
cnt = 0;
do
{
cnt++;
}
while (bufId /= 10);
return cnt;
}
void CALLBACK timerProc(HWND wnd, UINT msg, UINT_PTR timerId, DWORD sysTime)
{
unsigned int prevCnt;
ExecState prevState;
bool background;
prevCnt = queue.size;
prevState = queue.first->state;
background = queue.first->rule->background;
updateQueue();
if (prevCnt != queue.size)
{
if (isQueueDlgVisible())
{
processQueueEvent(background ? QUEUE_REMOVE_BACKGROUND
: QUEUE_REMOVE_FOREGROUND);
}
}
else if (prevState != queue.first->state)
{
if (isQueueDlgVisible())
processQueueEvent(QUEUE_STATUS_UPDATE);
}
}
Exec* getExecAt(int pos)
{
Exec *exec;
exec = queue.first;
for (; pos; pos--)
exec = exec->next;
return exec;
}