-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugutil.cpp
More file actions
397 lines (306 loc) · 12.4 KB
/
Copy pathplugutil.cpp
File metadata and controls
397 lines (306 loc) · 12.4 KB
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
/*****************************************************************************
Project: FarVCS plugin
Purpose: Far plugin utilities not specific to FarVCS plugin
*****************************************************************************/
#include <strstream>
#include <algorithm>
#include <windows.h>
#include "plugin.hpp"
#include "farcolor.hpp"
#include "regwrap.h"
#include "plugutil.h"
using namespace std;
/// <summary>
/// Primitive parsing of the contents of an .lng-file into a vector of strings.
/// The contents is read from the '1 FARLANG' resource.
/// </summary>
vector<std::string> ParseLngResource()
{
vector<std::string> vsMsgs;
// Get the FARLANG resource
HRSRC hLangRes = ::FindResource(hResInst, MAKEINTRESOURCE(1), "FARLANG");
if (hLangRes == 0)
return vsMsg;
HGLOBAL hGlobal = ::LoadResource(hResInst, hLangRes);
if (hGlobal == 0)
return vsMsg;
const char *pLangRes = (const char*)::LockResource(hGlobal);
if (pLangRes == nullptr)
return vsMsg;
unsigned long dwSize = ::SizeofResource(hResInst, hLangRes);
if (dwSize == 0)
return vsMsg;
istrstream istr(pLangRes, dwSize);
char buf[4096];
while (istr)
{
// Get the next line
istr.getline(buf, sizeof buf);
if (!istr)
break;
buf[_countof(buf) - 1] = '\0';
// Ignore the line if it doesn't start with quote
if (buf[0] != '"')
continue;
// Get rid of the trailing quote followed by CRLFs
char *p = buf + strlen(buf) - 1;
while ( *p == '\n' || *p == '\r' )
--p;
if ( *p == '"' )
--p;
*(p+1) = '\0';
// Hanling escape sequences is easier in the std::string
string s( buf + 1 );
for ( ; ; )
{
size_t i = s.find_first_of( '\\' );
if ( i == string::npos || i >= s.size()-1 )
break;
char c = s[i+1];
char cReplacement = c == 'r' ? '\r' :
c == 'n' ? '\n' :
c == 'b' ? '\b' :
c == 't' ? '\t' :
c == '\\' ? '\\' :
'\0';
if ( cReplacement ) {
s.erase( i, 1 );
s[i] = cReplacement;
}
}
vsMsgs.push_back( s );
}
return vsMsgs;
}
/// <summary>
/// Wrapper around FAR's GetMsg function. Simplifies the call
/// and improves robustness: we survive absence of the lng-file.
/// </summary>
/// <remarks>
/// WARNING! The messages in the lng-file must have contiguous zero-based
/// numbering.
/// </remarks>
const TCHAR *GetMsg(unsigned ing nMsgId)
{
const TCHAR *szLngMsg = StartupInfo.GetMsg(StartupInfo.ModuleNumber, nMsgId);
if (*szLngMsg != _T('\0'))
return szLngMsg;
// If a message cannot be read from the lng-file, try and extract it
// from the backup lng file in the resources
static vector<tstring> vsMsgs;
static bool bInitialized = false; // Empty vector might also mean unsuccessful parsing, so we have to use a separate flag
if (!bInitialized)
{
vsMsgs = ParseLngResource();
bInitialized = true;
}
return nMsgId < vsMsgs.size() ? vsMsgs[nMsgId].c_str() : szLngMsg;
}
//==========================================================================>>
// Returns the hotkey assigned to the plugin with the given dll name or 0 if
// no hotkeys defined. The dll name must be specified in lower case,
// e.g. "farvcs.dll".
//==========================================================================>>
unsigned char GetPluginHotkey( const char *szDllName )
{
vector<string> vsHotKeyedPlugins;
RegWrap( HKEY_CURRENT_USER, "Software\\FAR\\PluginHotkeys" ).EnumKeys( vsHotKeyedPlugins );
for ( vector<string>::iterator p = vsHotKeyedPlugins.begin(); p != vsHotKeyedPlugins.end(); ++p ) {
transform( p->begin(), p->end(), p->begin(), tolower );
if ( p->find( szDllName ) != string::npos ) {
string sHotkey = RegWrap( HKEY_CURRENT_USER, "Software\\FAR\\PluginHotkeys\\" + *p ).ReadString( "Hotkey" );
if ( !sHotkey.empty() )
return (unsigned char)toupper( sHotkey[0] );
}
}
return 0;
}
//==========================================================================>>
// Dialogs preparation
//==========================================================================>>
void InitDialogItems( const InitDialogItem *pInitItems, FarDialogItem *pItems, int nItems )
{
const InitDialogItem *p = pInitItems;
FarDialogItem *q = pItems;
for ( int i = 0; i < nItems; ++i, ++p, ++q )
{
q->Type = p->Type;
q->X1 = p->X1;
q->Y1 = p->Y1;
q->X2 = p->X2;
q->Y2 = p->Y2;
q->Focus = p->Focus;
q->Selected = p->Selected;
q->Flags = p->Flags;
q->DefaultButton = p->DefaultButton;
strncpy( q->Data, p->Data, sizeof q->Data );
q->Data[sizeof q->Data] = '\0';
}
}
//==========================================================================>>
// Check if Esc is pressed in the console
//==========================================================================>>
bool CheckForEsc()
{
HANDLE hConsoleInput = ::GetStdHandle( STD_INPUT_HANDLE );
if ( !hConsoleInput )
return false;
INPUT_RECORD rec;
DWORD nEventsRead;
return ::PeekConsoleInput( hConsoleInput, &rec, 1, &nEventsRead ) && nEventsRead > 0 &&
::ReadConsoleInput( hConsoleInput, &rec, 1, &nEventsRead ) && nEventsRead > 0 &&
rec.EventType == KEY_EVENT && rec.Event.KeyEvent.bKeyDown && rec.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE;
}
//==========================================================================>>
// Run another application from within plugin
//==========================================================================>>
DWORD Execute( const char *szCurDir, const char *szCmdLine, bool bHideOutput, bool bSilent, bool bShowTitle, bool bBackground, const char *szOutputFile )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset( &si, 0, sizeof si );
si.cb = sizeof si;
HANDLE hConsoleInput = ::GetStdHandle( STD_INPUT_HANDLE );
HANDLE hConsoleOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
HANDLE hConsoleError = ::GetStdHandle( STD_ERROR_HANDLE );
HANDLE hReadPipe = 0;
HANDLE hWritePipe = 0;
HANDLE hScreen = 0;
CONSOLE_SCREEN_BUFFER_INFO csbi;
memset( &csbi, 0, sizeof csbi ); // Just to get rid of an annoying compiler warning
WORD outputColor = (WORD)StartupInfo.AdvControl( StartupInfo.ModuleNumber, ACTL_GETCOLOR, (void*)COL_COMMANDLINE );
if ( bHideOutput )
{
SECURITY_ATTRIBUTES sa = { sizeof sa, 0, TRUE };
const unsigned long cdwPipeBufferSize = 32768;
if ( ::CreatePipe( &hReadPipe, &hWritePipe, &sa, cdwPipeBufferSize ) )
{
::SetStdHandle( STD_OUTPUT_HANDLE, hWritePipe );
::SetStdHandle( STD_ERROR_HANDLE, hReadPipe );
if ( !bSilent )
hScreen = StartupInfo.SaveScreen( 0, 0, -1, -1 );
/* !!!
else
hScreen=Info.SaveScreen(0,0,-1,0);
Info.Text(2,0,LIGHTGRAY,GetMsg(MWaitForExternalProgram));
*/
}
else
bHideOutput = false;
}
else
{
// Clear screen
::GetConsoleScreenBufferInfo( hConsoleOutput, &csbi );
char szBlank[nMaxConsoleWidth+1];
memset( szBlank, ' ', array_size(szBlank) );
szBlank[min((unsigned int)csbi.dwSize.X,array_size(szBlank)-1)] = 0;
for ( int i = 0; i < csbi.dwSize.Y; ++i )
StartupInfo.Text( 0, i, outputColor, szBlank );
StartupInfo.Text( 0, 0, 0, 0 );
COORD C = { 0, csbi.dwCursorPosition.Y };
::SetConsoleCursorPosition( hConsoleOutput, C );
}
DWORD BackupConsoleMode;
::GetConsoleMode( hConsoleInput, &BackupConsoleMode );
::SetConsoleMode( hConsoleInput, ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT );
char szBackupTitle[nMaxConsoleWidth+1];
if ( bShowTitle )
::GetConsoleTitle( szBackupTitle, array_size(szBackupTitle) ),
SetConsoleTitle( szCmdLine );
/* $ 14.02.2001 raVen
äåëàòü îêîøêó minimize, åñëè â ôîíå */
/* !!! if (Opt.Background)
{
si.dwFlags=si.dwFlags | STARTF_USESHOWWINDOW;
si.wShowWindow=SW_MINIMIZE;
}
*/
/* raVen $ */
DWORD dwExitCode = 0;
char szVolatileCmdLine[2048]; // ::CreateProcess wants to modify the command line so we create a read-write copy
array_strcpy( szVolatileCmdLine, szCmdLine );
BOOL bResult = ::CreateProcess( 0, szVolatileCmdLine, 0, 0, bHideOutput ? TRUE : FALSE,
(bBackground ? CREATE_NEW_CONSOLE : 0), 0, szCurDir, &si, &pi );
DWORD dwLastError = bResult ? 0 : ::GetLastError();
if ( bHideOutput )
{
::SetStdHandle( STD_OUTPUT_HANDLE, hConsoleOutput );
::SetStdHandle( STD_ERROR_HANDLE, hConsoleError );
::CloseHandle( hWritePipe );
}
::SetLastError( dwLastError );
if ( bResult )
{
if ( bHideOutput )
{
::WaitForSingleObject( pi.hProcess, 1000 );
HANDLE hFile = szOutputFile ? ::CreateFile( szOutputFile, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0 ) : 0;
char buf[32768];
DWORD dwRead;
DWORD dwWritten;
while ( ::ReadFile( hReadPipe, buf, sizeof buf, &dwRead, 0 ) )
if ( hFile != INVALID_HANDLE_VALUE )
::WriteFile( hFile, buf, dwRead, &dwWritten, 0 );
::CloseHandle( hReadPipe );
if ( hFile != INVALID_HANDLE_VALUE )
::CloseHandle( hFile );
}
/* $ 13.09.2000 tran
ôîíîâîé âûïîëíåíèå */
if ( !bBackground )
{
::WaitForSingleObject( pi.hProcess, INFINITE );
::GetExitCodeProcess( pi.hProcess, (LPDWORD)&dwExitCode );
::CloseHandle( pi.hThread );
::CloseHandle( pi.hProcess );
}
else
{
/* !!!
StartThreadForKillListFile(&pi,ListFileName); // íåõàé çà ïðîöåññîì òðåä ñëåäèò, è ôàéë áúåò òàïêîì
dwExitCode=0;
*/
}
/* tran 13.09.2000 $ */
}
if ( bShowTitle )
::SetConsoleTitle( szBackupTitle );
::SetConsoleMode( hConsoleInput, BackupConsoleMode );
if ( !bHideOutput )
{
SMALL_RECT src = { 0, 2, csbi.dwSize.X, csbi.dwSize.Y };
COORD dest = { 0, 0 };
CHAR_INFO fill = { ' ', outputColor };
::ScrollConsoleScreenBuffer( hConsoleOutput, &src, 0, dest, &fill );
StartupInfo.Control( INVALID_HANDLE_VALUE, FCTL_SETUSERSCREEN, 0 );
}
if ( hScreen )
{
StartupInfo.RestoreScreen( 0 );
StartupInfo.RestoreScreen( hScreen );
}
return dwExitCode;
}
HANDLE ExecuteConsoleNoWait( const char *szCurDir, const char *szCmdLine, HANDLE hOutPipe, HANDLE hErrPipe )
{
HANDLE hConsoleOutput = ::GetStdHandle( STD_OUTPUT_HANDLE );
HANDLE hConsoleError = ::GetStdHandle( STD_ERROR_HANDLE );
::SetStdHandle( STD_OUTPUT_HANDLE, hOutPipe );
::SetStdHandle( STD_ERROR_HANDLE, hErrPipe );
char szVolatileCmdLine[2048]; // ::CreateProcess wants to modify the command line so we create a read-write copy
array_strcpy( szVolatileCmdLine, szCmdLine );
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset( &si, 0, sizeof si );
si.cb = sizeof si;
BOOL bResult = ::CreateProcess( 0, szVolatileCmdLine, 0, 0, TRUE, 0, 0, szCurDir, &si, &pi );
DWORD dwLastError = bResult ? 0 : ::GetLastError();
::SetStdHandle( STD_OUTPUT_HANDLE, hConsoleOutput );
::SetStdHandle( STD_ERROR_HANDLE, hConsoleError );
::SetLastError( dwLastError );
if ( !bResult )
return INVALID_HANDLE_VALUE;
::CloseHandle( pi.hThread );
return pi.hProcess;
}