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
/*
 * regexpl - Console Registry Explorer
 *
 * Copyright (C) 2000-2005 Nedko Arnaudov <nedko@users.sourceforge.net>
 *
 * 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 2 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

// Completion.cpp : Defines the completion related functions.
#include "ph.h"
#include "RegistryKey.h"
#include "RegistryTree.h"

#define COMPLETION_BUFFER_SIZE	4096

extern CRegistryTree Tree;

BOOL g_blnCompletionCycle = TRUE;

class CCompletionMatch
{
public:
  CCompletionMatch()
  {
    m_pNext = m_pPrev = NULL;
    m_pszText = NULL;
  };

  BOOL Init(const TCHAR *pszText)
  {
    // find the offset to "unique" part
    const TCHAR *pszUnique = _tcsrchr(pszText,_T('\\'));
    pszUnique = pszUnique?(pszUnique+1):pszText;
    BOOL b = _tcschr(pszUnique,_T(' ')) != NULL; // has it spaces in it ???
    size_t s = _tcslen(pszText);

    if (m_pszText)
      delete[] m_pszText;

    m_pszText = new (std::nothrow) TCHAR [s+(b?3:1)]; // if we have spaces in unique part, we need 2 addtional chars for "

    if (!m_pszText)
      return FALSE;

    ASSERT(pszText <= pszUnique);
    s = pszUnique - pszText; // calculate offset of the unique part
    if (s)
      _tcsncpy(m_pszText,pszText,s);

    if (b)
      m_pszText[s++] = _T('\"');

    _tcscpy(m_pszText+s,pszUnique);

    if (b)
      _tcscat(m_pszText,_T("\""));

    return TRUE;
  }

  ~CCompletionMatch()
  {
    if (m_pszText)
      delete[] m_pszText;
  }

private:
  TCHAR *m_pszText;
  BOOL m_blnIsKey;
  CCompletionMatch *m_pNext;
  CCompletionMatch *m_pPrev;
  friend class CCompletionList;
};

class CCompletionList
{
public:
  CCompletionList();
  ~CCompletionList();

  BOOL IsNewCompletion(const TCHAR *pszContext, const TCHAR *pszBegin, BOOL& rblnNew);
  BOOL Add(const TCHAR *pszText, BOOL blnIsKey);
  const TCHAR *Get(unsigned __int64 nIndex, BOOL& rblnIsKey);
  unsigned __int64 GetCount();
  const TCHAR * GetContext();
  const TCHAR * GetBegin();
  void DeleteList();
  void Invalidate();

private:
  CCompletionMatch *m_pHead; // head of completions linked list
  CCompletionMatch *m_pTail; // tail of completions linked list
  CCompletionMatch *m_pLastSearched;
  unsigned int m_nLastSearched;
  TCHAR *m_pszContext;
  TCHAR *m_pszBegin;
  TCHAR *m_pszCurrentKey;
  unsigned __int64 m_nCount;
} g_Completion;

// --- begin of CCompletionList implementation ---
CCompletionList::CCompletionList()<--- Member variable 'CCompletionList::m_nLastSearched' is not initialized in the constructor.
{
  m_pHead = NULL;
  m_pTail = NULL;
  m_nCount = 0;
  m_pLastSearched = NULL;
  m_pszContext = NULL;
  m_pszBegin = NULL;
  m_pszCurrentKey = NULL;
}

CCompletionList::~CCompletionList()
{
  DeleteList();

  if (m_pszContext)
    delete[] m_pszContext;

  if (m_pszBegin)
    delete[] m_pszBegin;

  if (m_pszCurrentKey)
    delete[] m_pszCurrentKey;
}

void CCompletionList::Invalidate()
{
  if (m_pszCurrentKey)
  {
    delete[] m_pszCurrentKey;
    m_pszCurrentKey = NULL;
  }
}

BOOL CCompletionList::IsNewCompletion(const TCHAR *pszContext, const TCHAR *pszBegin, BOOL& rblnNew)
{
  const TCHAR *pszCurrentKey = Tree.GetCurrentPath();
  if (!m_pszContext ||
      !m_pszBegin ||
      !m_pszCurrentKey ||
      (_tcscmp(m_pszContext,pszContext) != 0) ||
      (_tcscmp(m_pszBegin,pszBegin) != 0) ||
      (_tcscmp(m_pszCurrentKey,pszCurrentKey)))
  { // new completion
    DeleteList();
    rblnNew = TRUE;
    if (m_pszContext)
    {
      delete[] m_pszContext;
      m_pszContext = NULL;
    }

    if (m_pszBegin)
    {
      delete[] m_pszBegin;
      m_pszBegin = NULL;
    }

    if (m_pszCurrentKey)
    {
      delete[] m_pszCurrentKey;
      m_pszCurrentKey = NULL;
    }

    size_t s = _tcslen(pszContext);
    m_pszContext = new (std::nothrow) TCHAR[s+1];
    if (!m_pszContext)
      return FALSE;
    _tcscpy(m_pszContext,pszContext);

    s = _tcslen(pszBegin);
    m_pszBegin = new (std::nothrow) TCHAR[s+1];
    if (!m_pszBegin)
    {
      delete[] m_pszContext;
      m_pszContext = NULL;
      return FALSE;
    }
    _tcscpy(m_pszBegin,pszBegin);

    s = _tcslen(pszCurrentKey);
    m_pszCurrentKey = new (std::nothrow) TCHAR[s+1];
    if (!m_pszCurrentKey)
    {
      delete[] m_pszContext;
      delete[] m_pszBegin;
      m_pszContext = NULL;
      m_pszBegin = NULL;
      return FALSE;
    }
    _tcscpy(m_pszCurrentKey,pszCurrentKey);

    return TRUE;
  }

  rblnNew = FALSE;
  return TRUE;
}

BOOL CCompletionList::Add(const TCHAR *pszText, BOOL blnIsKey)
{
  if (_tcsnicmp(pszText,m_pszBegin,_tcslen(m_pszBegin)) != 0)
    return TRUE;
  CCompletionMatch *pNode = new (std::nothrow) CCompletionMatch;
  if (!pNode)
    return FALSE;
  if (!pNode->Init(pszText))
    return FALSE;

  ASSERT(pNode->m_pszText);
  ASSERT(pNode->m_pNext == NULL);

  // add new node to tail
  pNode->m_blnIsKey = blnIsKey;
  if (m_pTail)
  {
    pNode->m_pPrev = m_pTail;
    ASSERT(m_pTail->m_pNext == NULL);
    m_pTail->m_pNext = pNode;
    m_pTail = pNode;
  }
  else
  {
    ASSERT(m_pHead == NULL);
    m_pHead = m_pTail = pNode;
  }

  m_nCount++;

  m_pLastSearched = NULL;

  return TRUE;
}

const TCHAR * CCompletionList::Get(unsigned __int64 nIndex, BOOL& rblnIsKey)
{
  ASSERT(nIndex < m_nCount);
  BOOL blnForward = FALSE;
  CCompletionMatch *pNode = NULL;

  unsigned __int64 nRelativeIndex = 0;

  if (m_pLastSearched)
  {
    pNode = m_pLastSearched;
    blnForward = nIndex > m_nLastSearched;
    nRelativeIndex = blnForward?(nIndex-m_nLastSearched):(m_nLastSearched-nIndex);
    if ((nRelativeIndex > nIndex)||(nRelativeIndex > m_nCount-nIndex-1))
      pNode = NULL; // seraching from tail or from head is more effective
  }

  if (!pNode && (nIndex <= m_nCount/2))
  { // search from head
    pNode = m_pHead;
    blnForward = TRUE;
    nRelativeIndex = nIndex;
  }

  if (!pNode)
  { // search from tail
    pNode = m_pTail;
    blnForward = FALSE;
    nRelativeIndex = m_nCount-nIndex-1;
  }

  while(pNode)
  {
    if (nRelativeIndex == 0)
    {
      m_nLastSearched = nIndex;
      m_pLastSearched = pNode;
      rblnIsKey = pNode->m_blnIsKey;
      if (!pNode->m_pszText)
        ASSERT(FALSE);
      return pNode->m_pszText;
    }

    nRelativeIndex--;

    pNode = blnForward?(pNode->m_pNext):(pNode->m_pPrev);
  }

  ASSERT(FALSE);
  return NULL;
}

unsigned __int64 CCompletionList::GetCount()
{
  return m_nCount;
}

const TCHAR * CCompletionList::GetContext()
{
  return m_pszContext;
}

const TCHAR * CCompletionList::GetBegin()
{
  return m_pszBegin;
}

void CCompletionList::DeleteList()
{
  CCompletionMatch *pNode;
  while(m_pHead)
  {
    pNode = m_pHead;
    m_pHead = m_pHead->m_pNext;
    delete pNode;
  }
  m_pTail = NULL;
  ASSERT(m_pHead == NULL);
  m_nCount = 0;
}

// --- end of CCompletionList implementation ---

BOOL FillCompletion(const TCHAR *pszKey)
{
  g_Completion.DeleteList();
  LONG nError;
  CRegistryKey Key;
  TCHAR *pszSubkeyName = NULL;
  DWORD dwMaxSubkeyNameLength;
  TCHAR *pszValueName = NULL;
  DWORD dwMaxValueNameSize;

  if (!Tree.GetKey(pszKey?pszKey:_T("."),KEY_ENUMERATE_SUB_KEYS|KEY_QUERY_VALUE,Key))
    return FALSE;

  BOOL blnCompletionOnKeys = TRUE;
  BOOL blnCompletionOnValues = TRUE;

/*	if ((_tcsnicmp(pchContext,DIR_CMD,DIR_CMD_LENGTH) == 0)||
		(_tcsnicmp(pchContext,CD_CMD,CD_CMD_LENGTH) == 0)||
		(_tcsnicmp(pchContext,OWNER_CMD,OWNER_CMD_LENGTH) == 0)||
		(_tcsnicmp(pchContext,DACL_CMD,DACL_CMD_LENGTH) == 0)||
		(_tcsnicmp(pchContext,SACL_CMD,SACL_CMD_LENGTH) == 0))
	{
		blnCompletionOnValues = FALSE;
	}*/
//	else if (_tcsnicmp(pchContext,VALUE_CMD,VALUE_CMD_LENGTH) == 0)
//	{
//		blnCompletionOnKeys = FALSE;
//	}

  size_t nKeyNameSize = 0;
  if (pszKey)
  {
    nKeyNameSize = _tcslen(pszKey);
    if (_tcscmp(pszKey,_T("\\")))
      nKeyNameSize++;
  }

  if (blnCompletionOnKeys)
  {
    nError = Key.GetSubkeyNameMaxLength(dwMaxSubkeyNameLength);
    if (nError != ERROR_SUCCESS)
      return FALSE;

    pszSubkeyName = new (std::nothrow) TCHAR[nKeyNameSize+dwMaxSubkeyNameLength+1];
    if (!pszSubkeyName)
      goto Abort;

    if (pszKey)
      _stprintf(pszSubkeyName,_tcscmp(pszKey,_T("\\"))?_T("%s\\"):_T("%s"),pszKey);

    Key.InitSubkeyEnumeration(pszSubkeyName+nKeyNameSize,dwMaxSubkeyNameLength);
    while ((nError = Key.GetNextSubkeyName()) == ERROR_SUCCESS)
      if (!g_Completion.Add(pszSubkeyName,TRUE))
      {
        ASSERT(FALSE);
        goto Abort;
      }
    if ((nError != ERROR_SUCCESS)&&(nError != ERROR_NO_MORE_ITEMS))
    {
      ASSERT(FALSE);
      goto Abort;
    }
  }

  if (blnCompletionOnValues)
  {
    nError = Key.GetMaxValueNameLength(dwMaxValueNameSize);
    if (nError != ERROR_SUCCESS)
    {
      ASSERT(FALSE);
      goto Abort;
    }

    pszValueName = new (std::nothrow) TCHAR[nKeyNameSize+dwMaxValueNameSize+1];
    if (!pszValueName)
      goto Abort;

    if (pszKey)
      _stprintf(pszValueName,_tcscmp(pszKey,_T("\\"))?_T("%s\\"):_T("%s"),pszKey);

    Key.InitValueEnumeration(pszValueName+nKeyNameSize,dwMaxValueNameSize,NULL,0,NULL);
    while((nError = Key.GetNextValue()) == ERROR_SUCCESS)
      if (!g_Completion.Add(pszValueName,FALSE))
      {
        ASSERT(FALSE);
        goto Abort;
      }
    if ((nError != ERROR_SUCCESS)&&(nError != ERROR_NO_MORE_ITEMS))
    {
      ASSERT(FALSE);
      goto Abort;
    }
  }

  if (pszValueName)
    delete[] pszValueName;
  if (pszSubkeyName)
    delete[] pszSubkeyName;
  return TRUE;
Abort:
  if (pszValueName)
    delete[] pszValueName;
  if (pszSubkeyName)
    delete[] pszSubkeyName;
  return FALSE;
}

const TCHAR * CompletionCallback(unsigned __int64 & rnIndex,
                                 const BOOL *pblnForward,
                                 const TCHAR *pszContext,
                                 const TCHAR *pszBegin)
{
  static TCHAR pszBuffer[COMPLETION_BUFFER_SIZE];

  // Find first non-white space in context
  while(*pszContext && _istspace(*pszContext))
    pszContext++;

  BOOL blnNewCompletion = TRUE;
  if (!g_Completion.IsNewCompletion(pszContext,pszBegin,blnNewCompletion))
  {
    ASSERT(FALSE);
    return NULL;
  }

  if (blnNewCompletion)
  {
    _tcsncpy(pszBuffer,pszBegin,COMPLETION_BUFFER_SIZE-1);
    pszBuffer[COMPLETION_BUFFER_SIZE-1] = 0;
    TCHAR *pszSeparator = pszBuffer; // set it to aby non null value
    if (_tcscmp(pszBuffer,_T("\\")))
    {
      pszSeparator = _tcsrchr(pszBuffer,_T('\\'));
      if (pszSeparator)
        *pszSeparator = 0;
    }

    if (!FillCompletion(pszSeparator?pszBuffer:NULL))
      return NULL;
  }

  unsigned __int64 nTotalItems = g_Completion.GetCount();
  if (nTotalItems == 0)
    return NULL;

  if (rnIndex >= nTotalItems)
    rnIndex = nTotalItems-1;

  if (pblnForward)
  {
    if (*pblnForward)
    {
      rnIndex++;
      if (rnIndex >= nTotalItems)
      {
        if (g_blnCompletionCycle)
          rnIndex = 0;
        else
          rnIndex--;
      }
    }
    else
    {
      if (rnIndex)
        rnIndex--;
      else if (g_blnCompletionCycle)
        rnIndex = nTotalItems-1;
    }
  }
  BOOL blnIsKey = FALSE;
  const TCHAR *pszName = g_Completion.Get(rnIndex,blnIsKey);

  ASSERT(pszName);

  _sntprintf(pszBuffer,COMPLETION_BUFFER_SIZE-1,_T("%s%s"),pszName,(blnIsKey?_T("\\"):_T("")));
  pszBuffer[COMPLETION_BUFFER_SIZE-1] = 0;
  return pszBuffer;
}

void InvalidateCompletion()
{
  g_Completion.Invalidate();
}