summaryrefslogtreecommitdiff
path: root/bin/bbackupquery/CommandCompletion.cpp
blob: 3bc79f3ab2cc07947ce76ec1d8196b2096d0862b (plain)
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
// --------------------------------------------------------------------------
//
// File
//		Name:    CommandCompletion.cpp
//		Purpose: Parts of BackupQueries that depend on readline
//		Created: 2011/01/21
//
// --------------------------------------------------------------------------

#include "Box.h"

#ifdef HAVE_LIBREADLINE
	#ifdef HAVE_READLINE_READLINE_H
		#include <readline/readline.h>
	#elif defined(HAVE_EDITLINE_READLINE_H)
		#include <editline/readline.h>
	#elif defined(HAVE_READLINE_H)
		#include <readline.h>
	#endif
#endif

#ifdef HAVE_READLINE_HISTORY
	#ifdef HAVE_READLINE_HISTORY_H
		#include <readline/history.h>
	#elif defined(HAVE_HISTORY_H)
		#include <history.h>
	#endif
#endif

#include <cstring>

#include "BackupQueries.h"
#include "Configuration.h"

#include "autogen_BackupProtocol.h"

#include "MemLeakFindOn.h"

#define COMPARE_RETURN_SAME		1
#define COMPARE_RETURN_DIFFERENT	2
#define COMPARE_RETURN_ERROR		3
#define COMMAND_RETURN_ERROR		4

#define COMPLETION_FUNCTION(name, code) \
std::vector<std::string> Complete ## name( \
	BackupQueries::ParsedCommand& rCommand, \
	const std::string& prefix, \
	BackupProtocolClient& rProtocol, const Configuration& rConfig, \
	BackupQueries& rQueries) \
{ \
	std::vector<std::string> completions; \
	\
	try \
	{ \
		code \
	} \
	catch(std::exception &e) \
	{ \
		BOX_TRACE("Failed to complete " << prefix << ": " << e.what()); \
	} \
	catch(...) \
	{ \
		BOX_TRACE("Failed to complete " << prefix << ": " \
			"unknown error"); \
	} \
	\
	return completions; \
}

#define DELEGATE_COMPLETION(name) \
	completions = Complete ## name(rCommand, prefix, rProtocol, rConfig, \
	rQueries);

COMPLETION_FUNCTION(None,)

#ifdef HAVE_RL_FILENAME_COMPLETION_FUNCTION
	#define RL_FILENAME_COMPLETION_FUNCTION rl_filename_completion_function
	#define HAVE_A_FILENAME_COMPLETION_FUNCTION 1
#elif defined HAVE_FILENAME_COMPLETION_FUNCTION
	#define RL_FILENAME_COMPLETION_FUNCTION filename_completion_function
	#define HAVE_A_FILENAME_COMPLETION_FUNCTION 1
#endif

#ifdef HAVE_A_FILENAME_COMPLETION_FUNCTION
COMPLETION_FUNCTION(Default,
	int i = 0;
	
	while (const char *match = RL_FILENAME_COMPLETION_FUNCTION(prefix.c_str(), i))
	{
		completions.push_back(match);
		++i;
	}
)
#else // !HAVE_A_FILENAME_COMPLETION_FUNCTION
COMPLETION_FUNCTION(Default,)
#endif // HAVE_A_FILENAME_COMPLETION_FUNCTION

COMPLETION_FUNCTION(Command,
	int len = prefix.length();

	for(int i = 0; commands[i].name != NULL; i++)
	{
		if(::strncmp(commands[i].name, prefix.c_str(), len) == 0)
		{
			completions.push_back(commands[i].name);
		}
	}
)

void CompleteOptionsInternal(const std::string& prefix,
	BackupQueries::ParsedCommand& rCommand,
	std::vector<std::string>& completions)
{
	std::string availableOptions = rCommand.pSpec->opts;

	for(std::string::iterator
		opt =  availableOptions.begin();
		opt != availableOptions.end(); opt++)
	{
		if(rCommand.mOptions.find(*opt) == std::string::npos)
		{
			if(prefix == "")
			{
				// complete with possible option strings
				completions.push_back(std::string("-") + *opt);
			}
			else
			{
				// complete with possible additional options
				completions.push_back(prefix + *opt);
			}
		}
	}
}

COMPLETION_FUNCTION(Options,
	CompleteOptionsInternal(prefix, rCommand, completions);
)

std::string EncodeFileName(const std::string &rUnEncodedName)
{
#ifdef WIN32
	std::string encodedName;
	if(!ConvertConsoleToUtf8(rUnEncodedName, encodedName))
	{
		return std::string();
	}
	return encodedName;
#else
	return rUnEncodedName;
#endif
}

int16_t GetExcludeFlags(BackupQueries::ParsedCommand& rCommand)
{
	int16_t excludeFlags = 0;

	if (rCommand.mOptions.find(LIST_OPTION_ALLOWOLD) == std::string::npos)
	{
		excludeFlags |= BackupProtocolListDirectory::Flags_OldVersion;
	}

	if (rCommand.mOptions.find(LIST_OPTION_ALLOWDELETED) == std::string::npos)
	{
		excludeFlags |= BackupProtocolListDirectory::Flags_Deleted;
	}

	return excludeFlags;
}

std::vector<std::string> CompleteRemoteFileOrDirectory(
	BackupQueries::ParsedCommand& rCommand,
	const std::string& prefix, BackupProtocolClient& rProtocol,
	BackupQueries& rQueries, int16_t includeFlags)
{
	std::vector<std::string> completions;
	
	// default to using the current directory
	int64_t listDirId = rQueries.GetCurrentDirectoryID();
	std::string searchPrefix;
	std::string listDir = prefix;

	if(rCommand.mCompleteArgCount == rCommand.mCmdElements.size())
	{
		// completing an empty name, from the current directory
		// nothing to change
	}
	else
	{
		// completing a partially-completed subdirectory name
		searchPrefix = prefix;
		listDir = "";

		// do we need to list a subdirectory to complete?
		size_t lastSlash = searchPrefix.rfind('/');
		if(lastSlash == std::string::npos)
		{
			// no slashes, so the whole name is the prefix
			// nothing to change
		}
		else
		{
			// listing a partially-completed subdirectory name
			listDir = searchPrefix.substr(0, lastSlash);

			listDirId = rQueries.FindDirectoryObjectID(listDir,
				rCommand.mOptions.find(LIST_OPTION_ALLOWOLD)
					!= std::string::npos,
				rCommand.mOptions.find(LIST_OPTION_ALLOWDELETED)
					!= std::string::npos);

			if(listDirId == 0)
			{
				// no matches for subdir to list,
				// return empty-handed.
				return completions;
			}

			// matched, and updated listDir and listDirId already
			searchPrefix = searchPrefix.substr(lastSlash + 1);
		}
	}

	// Always include directories, because they contain files.
	// We will append a slash later for each directory if we're
	// actually looking for files.
	//
	// If we're looking for directories, then only list directories.

	bool completeFiles = includeFlags &
		BackupProtocolListDirectory::Flags_File;
	bool completeDirs = includeFlags &
		BackupProtocolListDirectory::Flags_Dir;
	int16_t listFlags = 0;

	if(completeFiles)
	{
		listFlags = BackupProtocolListDirectory::Flags_INCLUDE_EVERYTHING;
	}
	else if(completeDirs)
	{
		listFlags = BackupProtocolListDirectory::Flags_Dir;
	}

	rProtocol.QueryListDirectory(listDirId,
		listFlags, GetExcludeFlags(rCommand),
		false /* no attributes */);

	// Retrieve the directory from the stream following
	BackupStoreDirectory dir;
	std::auto_ptr<IOStream> dirstream(rProtocol.ReceiveStream());
	dir.ReadFromStream(*dirstream, rProtocol.GetTimeout());

	// Then... display everything
	BackupStoreDirectory::Iterator i(dir);
	BackupStoreDirectory::Entry *en = 0;
	while((en = i.Next()) != 0)
	{
		BackupStoreFilenameClear clear(en->GetName());
		std::string name = clear.GetClearFilename().c_str();
		if(name.compare(0, searchPrefix.length(), searchPrefix) == 0)
		{
			bool dir_added = false;

			if(en->IsDir() &&
				(includeFlags & BackupProtocolListDirectory::Flags_Dir) == 0)
			{
				// Was looking for a file, but this is a 
				// directory, so append a slash to the name
				name += "/";
			}

			#ifdef HAVE_LIBREADLINE
			if(strchr(name.c_str(), ' '))
			{
				int n_quote = 0;

				for(int k = strlen(rl_line_buffer); k >= 0; k--)
				{
					if (rl_line_buffer[k] == '\"') {
						++n_quote;
					}
				}

				dir_added = false;

				if (!(n_quote % 2))
				{
					name = "\"" + (listDir == "" ? name : listDir + "/" + name);
					dir_added = true;
				}

				name = name + "\"";
			}
			#endif

			if(listDir == "" || dir_added)
			{
				completions.push_back(name);
			}
			else
			{
				completions.push_back(listDir + "/" + name);
			}
		}
	}

	return completions;
}

COMPLETION_FUNCTION(RemoteDir,
	completions = CompleteRemoteFileOrDirectory(rCommand, prefix,
		rProtocol, rQueries,
		BackupProtocolListDirectory::Flags_Dir);
)

COMPLETION_FUNCTION(RemoteFile,
	completions = CompleteRemoteFileOrDirectory(rCommand, prefix,
		rProtocol, rQueries,
		BackupProtocolListDirectory::Flags_File);
)

COMPLETION_FUNCTION(LocalDir,
	DELEGATE_COMPLETION(Default);
)

COMPLETION_FUNCTION(LocalFile,
	DELEGATE_COMPLETION(Default);
)

COMPLETION_FUNCTION(LocationName,
	const Configuration &locations(rConfig.GetSubConfiguration(
		"BackupLocations"));

	std::vector<std::string> locNames =
		locations.GetSubConfigurationNames();

	for(std::vector<std::string>::iterator
		pLocName  = locNames.begin();
		pLocName != locNames.end();
		pLocName++)
	{
		if(pLocName->compare(0, pLocName->length(), prefix) == 0)
		{
			completions.push_back(*pLocName);
		}
	}
)

COMPLETION_FUNCTION(RemoteFileIdInCurrentDir,
	int64_t listDirId = rQueries.GetCurrentDirectoryID();
	int16_t excludeFlags = GetExcludeFlags(rCommand);

	rProtocol.QueryListDirectory(
		listDirId,
		BackupProtocolListDirectory::Flags_File,
		excludeFlags, false /* no attributes */);

	// Retrieve the directory from the stream following
	BackupStoreDirectory dir;
	std::auto_ptr<IOStream> dirstream(rProtocol.ReceiveStream());
	dir.ReadFromStream(*dirstream, rProtocol.GetTimeout());

	// Then... compare each item
	BackupStoreDirectory::Iterator i(dir);
	BackupStoreDirectory::Entry *en = 0;
	while((en = i.Next()) != 0)
	{
		std::ostringstream hexId;
		hexId << std::hex << en->GetObjectID();
		if(hexId.str().compare(0, prefix.length(), prefix) == 0)
		{
			completions.push_back(hexId.str());
		}
	}
)

// TODO implement completion of hex IDs up to the maximum according to Usage
COMPLETION_FUNCTION(RemoteId,)

COMPLETION_FUNCTION(GetFileOrId,
	if(rCommand.mOptions.find('i') != std::string::npos)
	{
		DELEGATE_COMPLETION(RemoteFileIdInCurrentDir);
	}
	else
	{
		DELEGATE_COMPLETION(RemoteFile);
	}
)

COMPLETION_FUNCTION(CompareLocationOrRemoteDir,
	if(rCommand.mOptions.find('l') != std::string::npos)
	{
		DELEGATE_COMPLETION(LocationName);
	}
	else
	{
		DELEGATE_COMPLETION(RemoteDir);
	}
)

COMPLETION_FUNCTION(CompareNoneOrLocalDir,
	if(rCommand.mOptions.find('l') != std::string::npos)
	{
		// no completions
		DELEGATE_COMPLETION(None);
	}
	else
	{
		DELEGATE_COMPLETION(LocalDir);
	}
)

COMPLETION_FUNCTION(RestoreRemoteDirOrId,
	if(rCommand.mOptions.find('i') != std::string::npos)
	{
		DELEGATE_COMPLETION(RemoteId);
	}
	else
	{
		DELEGATE_COMPLETION(RemoteDir);
	}
)

// Data about commands
QueryCommandSpecification commands[] = 
{
	{ "quit",	"",		Command_Quit, 	{} },
	{ "exit",	"",		Command_Quit,	{} },
	{ "list",	"rodIFtTash",	Command_List,	{CompleteRemoteDir} },
	{ "pwd",	"",		Command_pwd,	{} },
	{ "cd",		"od",		Command_cd,	{CompleteRemoteDir} },
	{ "lcd",	"",		Command_lcd,	{CompleteLocalDir} },
	{ "sh", 	"",		Command_sh,	{CompleteDefault} },
	{ "getobject",	"",		Command_GetObject,
		{CompleteRemoteId, CompleteLocalDir} },
	{ "get",	"i",		Command_Get,
		{CompleteGetFileOrId, CompleteLocalDir} },
	{ "compare",	"alcqAEQ",	Command_Compare,
		{CompleteCompareLocationOrRemoteDir, CompleteCompareNoneOrLocalDir} },
	{ "restore",	"drif",		Command_Restore,
		{CompleteRestoreRemoteDirOrId, CompleteLocalDir} },
	{ "help",	"",		Command_Help,	{} },
	{ "usage",	"m",		Command_Usage,	{} },
	{ "undelete",	"i",		Command_Undelete,
		{CompleteGetFileOrId} },
	{ "delete",	"i",		Command_Delete,	{CompleteGetFileOrId} },
	{ NULL, 	NULL,		Command_Unknown, {} } 
};

const char *alias[] = {"ls", 0};
const int aliasIs[] = {Command_List, 0};

BackupQueries::ParsedCommand::ParsedCommand(const std::string& Command,
	bool isFromCommandLine)
: mInOptions(false),
  mFailed(false),
  pSpec(NULL),
  mCompleteArgCount(0)
{
	mCompleteCommand = Command;
	
	// is the command a shell command?
	if(Command[0] == 's' && Command[1] == 'h' && Command[2] == ' ' && Command[3] != '\0')
	{
		// Yes, run shell command
		for(int i = 0; commands[i].type != Command_Unknown; i++)
		{
			if(commands[i].type == Command_sh)
			{
				pSpec = &(commands[i]);
				break;
			}
		}

		mCmdElements[0] = "sh";
		mCmdElements[1] = Command.c_str() + 3;
		return;
	}

	// split command into components
	bool inQuoted = false;
	mInOptions = false;
	
	std::string currentArg;
	for (std::string::const_iterator c = Command.begin();
		c != Command.end(); c++)
	{
		// Terminating char?
		if(*c == ((inQuoted)?'"':' '))
		{
			if(!currentArg.empty())
			{
				mCmdElements.push_back(currentArg);

				// Because we just found a space, and the last
				// word was not options (otherwise currentArg
				// would be empty), we've received a complete
				// command or non-option argument.
				mCompleteArgCount++;
			}

			currentArg.resize(0);
			inQuoted = false;
			mInOptions = false;
		}
		// Start of quoted parameter?
		else if(currentArg.empty() && *c == '"')
		{
			inQuoted = true;
		}
		// Start of options? You can't have options if there's no
		// command before them, so treat the options as a command (which
		// doesn't exist, so it will fail to parse) in that case.
		else if(currentArg.empty() && *c == '-' && !mCmdElements.empty())
		{
			mInOptions = true;
		}
		else if(mInOptions)
		{
			// Option char
			mOptions += *c;
		}
		else
		{
			// Normal string char, part of current arg
			currentArg += *c;
		}
	}
	
	if(!currentArg.empty())
	{
		mCmdElements.push_back(currentArg);
	}

	// If there are no commands then there's nothing to do except return
	if(mCmdElements.empty())
	{
		return;
	}

	// Work out which command it is...
	int cmd = 0;
	while(commands[cmd].name != 0 && 
		mCmdElements[0] != commands[cmd].name)
	{
		cmd++;
	}
	
	if(commands[cmd].name == 0)
	{
		// Check for aliases
		int a;
		for(a = 0; alias[a] != 0; ++a)
		{
			if(mCmdElements[0] == alias[a])
			{
				// Found an alias
				cmd = aliasIs[a];
				break;
			}
		}
	}

	if(commands[cmd].name == 0)
	{
		mFailed = true;
		return;
	}

	pSpec = &(commands[cmd]);
	
	#ifdef WIN32
	if(isFromCommandLine)
	{
		std::string converted;
		
		if(!ConvertEncoding(mCompleteCommand, CP_ACP, converted, 
			GetConsoleCP()))
		{
			BOX_ERROR("Failed to convert encoding");
			mFailed = true;
		}
		
		mCompleteCommand = converted;
		
		for(std::vector<std::string>::iterator 
			i  = mCmdElements.begin();
			i != mCmdElements.end(); i++)
		{
			if(!ConvertEncoding(*i, CP_ACP, converted, 
				GetConsoleCP()))
			{
				BOX_ERROR("Failed to convert encoding");
				mFailed = true;
			}
			
			*i = converted;
		}
	}
	#endif
}