2006-01-23 20:40:14 +00:00
|
|
|
/** \file builtin_complete.c Functions defining the complete builtin
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
Functions used for implementing the complete builtin.
|
2006-01-22 21:10:55 +00:00
|
|
|
|
|
|
|
*/
|
2006-08-11 01:18:35 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <signal.h>
|
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
#include "fallback.h"
|
2006-01-22 21:10:55 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "wgetopt.h"
|
|
|
|
#include "parser.h"
|
2006-01-30 16:51:50 +00:00
|
|
|
#include "reader.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
Internal storage for the builtin_complete_get_temporary_buffer() function.
|
2006-06-17 13:07:08 +00:00
|
|
|
*/
|
2010-10-08 00:43:57 +00:00
|
|
|
static const wchar_t *temporary_buffer;
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-01-22 23:40:03 +00:00
|
|
|
/*
|
|
|
|
builtin_complete_* are a set of rather silly looping functions that
|
|
|
|
make sure that all the proper combinations of complete_add or
|
2006-06-02 02:15:17 +00:00
|
|
|
complete_remove get called. This is needed since complete allows you
|
|
|
|
to specify multiple switches on a single commandline, like 'complete
|
|
|
|
-s a -s b -s c', but the complete_add function only accepts one
|
|
|
|
short switch and one long switch.
|
2006-01-22 23:40:03 +00:00
|
|
|
*/
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Silly function
|
|
|
|
*/
|
2006-01-22 21:10:55 +00:00
|
|
|
static void builtin_complete_add2( const wchar_t *cmd,
|
|
|
|
int cmd_type,
|
|
|
|
const wchar_t *short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
const wcstring_list_t &gnu_opt,
|
|
|
|
const wcstring_list_t &old_opt,
|
2011-12-27 03:18:46 +00:00
|
|
|
int result_mode,
|
2006-01-22 21:10:55 +00:00
|
|
|
const wchar_t *condition,
|
|
|
|
const wchar_t *comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
const wchar_t *desc,
|
|
|
|
int flags )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
size_t i;
|
2006-01-22 21:10:55 +00:00
|
|
|
const wchar_t *s;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
for( s=short_opt; *s; s++ )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
complete_add( cmd,
|
2006-01-22 23:30:00 +00:00
|
|
|
cmd_type,
|
|
|
|
*s,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
for( i=0; i<gnu_opt.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
complete_add( cmd,
|
2006-01-22 23:30:00 +00:00
|
|
|
cmd_type,
|
|
|
|
0,
|
2012-02-08 08:45:07 +00:00
|
|
|
gnu_opt.at(i).c_str(),
|
2006-01-22 23:30:00 +00:00
|
|
|
0,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
for( i=0; i<old_opt.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
complete_add( cmd,
|
2006-01-22 23:30:00 +00:00
|
|
|
cmd_type,
|
|
|
|
0,
|
2012-02-08 08:45:07 +00:00
|
|
|
old_opt.at(i).c_str(),
|
2006-01-22 23:30:00 +00:00
|
|
|
1,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-01-22 23:30:00 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
if( old_opt.size() == 0 && gnu_opt.size() == 0 && wcslen(short_opt) == 0 )
|
2006-01-22 23:30:00 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
complete_add( cmd,
|
2006-01-22 23:30:00 +00:00
|
|
|
cmd_type,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Silly function
|
|
|
|
*/
|
2012-02-08 08:45:07 +00:00
|
|
|
static void builtin_complete_add( const wcstring_list_t &cmd,
|
|
|
|
const wcstring_list_t &path,
|
2006-01-22 21:10:55 +00:00
|
|
|
const wchar_t *short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
wcstring_list_t &gnu_opt,
|
|
|
|
wcstring_list_t &old_opt,
|
2011-12-27 03:18:46 +00:00
|
|
|
int result_mode,
|
2007-08-01 17:35:24 +00:00
|
|
|
int authoritative,
|
2006-01-22 21:10:55 +00:00
|
|
|
const wchar_t *condition,
|
|
|
|
const wchar_t *comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
const wchar_t *desc,
|
|
|
|
int flags )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
for( size_t i=0; i<cmd.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_add2( cmd.at(i).c_str(),
|
2007-01-28 13:40:59 +00:00
|
|
|
COMMAND,
|
2011-12-27 03:18:46 +00:00
|
|
|
short_opt,
|
2007-01-28 13:40:59 +00:00
|
|
|
gnu_opt,
|
2011-12-27 03:18:46 +00:00
|
|
|
old_opt,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2007-01-28 13:40:59 +00:00
|
|
|
|
2007-08-01 17:35:24 +00:00
|
|
|
if( authoritative != -1 )
|
2007-01-28 03:24:16 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
complete_set_authoritative( cmd.at(i).c_str(),
|
2007-01-28 13:40:59 +00:00
|
|
|
COMMAND,
|
2007-08-01 17:35:24 +00:00
|
|
|
authoritative );
|
2007-01-28 03:24:16 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
for( size_t i=0; i<path.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_add2( path.at(i).c_str(),
|
2007-01-28 13:40:59 +00:00
|
|
|
PATH,
|
2011-12-27 03:18:46 +00:00
|
|
|
short_opt,
|
2007-01-28 13:40:59 +00:00
|
|
|
gnu_opt,
|
2011-12-27 03:18:46 +00:00
|
|
|
old_opt,
|
|
|
|
result_mode,
|
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
|
|
|
flags );
|
2007-01-28 13:40:59 +00:00
|
|
|
|
2007-08-01 17:35:24 +00:00
|
|
|
if( authoritative != -1 )
|
2007-01-28 03:24:16 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
complete_set_authoritative( path.at(i).c_str(),
|
2007-01-28 13:40:59 +00:00
|
|
|
PATH,
|
2007-08-01 17:35:24 +00:00
|
|
|
authoritative );
|
2007-01-28 03:24:16 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Silly function
|
|
|
|
*/
|
2012-02-08 08:45:07 +00:00
|
|
|
static void builtin_complete_remove3( const wchar_t *cmd,
|
2006-01-22 21:10:55 +00:00
|
|
|
int cmd_type,
|
|
|
|
wchar_t short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
const wcstring_list_t &long_opt )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
for( size_t i=0; i<long_opt.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
|
|
|
complete_remove( cmd,
|
|
|
|
cmd_type,
|
|
|
|
short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
long_opt.at(i).c_str());
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Silly function
|
|
|
|
*/
|
2012-02-08 08:45:07 +00:00
|
|
|
static void builtin_complete_remove2( const wchar_t *cmd,
|
2006-01-22 21:10:55 +00:00
|
|
|
int cmd_type,
|
|
|
|
const wchar_t *short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
const wcstring_list_t &gnu_opt,
|
|
|
|
const wcstring_list_t &old_opt )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
|
|
|
const wchar_t *s = (wchar_t *)short_opt;
|
|
|
|
if( *s )
|
|
|
|
{
|
|
|
|
for( ; *s; s++ )
|
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
if( old_opt.size() == 0 && gnu_opt.size() == 0 )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
|
|
|
complete_remove(cmd,
|
|
|
|
cmd_type,
|
|
|
|
*s,
|
|
|
|
0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builtin_complete_remove3( cmd,
|
|
|
|
cmd_type,
|
|
|
|
*s,
|
|
|
|
gnu_opt );
|
|
|
|
builtin_complete_remove3( cmd,
|
|
|
|
cmd_type,
|
|
|
|
*s,
|
|
|
|
old_opt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
builtin_complete_remove3( cmd,
|
|
|
|
cmd_type,
|
|
|
|
0,
|
|
|
|
gnu_opt );
|
|
|
|
builtin_complete_remove3( cmd,
|
|
|
|
cmd_type,
|
|
|
|
0,
|
|
|
|
old_opt );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
|
2006-06-17 13:07:08 +00:00
|
|
|
/**
|
|
|
|
Silly function
|
|
|
|
*/
|
2012-02-08 08:45:07 +00:00
|
|
|
static void builtin_complete_remove( const wcstring_list_t &cmd,
|
|
|
|
const wcstring_list_t &path,
|
2006-01-22 21:10:55 +00:00
|
|
|
const wchar_t *short_opt,
|
2012-02-08 08:45:07 +00:00
|
|
|
const wcstring_list_t &gnu_opt,
|
|
|
|
const wcstring_list_t &old_opt )
|
|
|
|
{
|
|
|
|
for( size_t i=0; i<cmd.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_remove2( cmd.at(i).c_str(),
|
2006-01-22 21:10:55 +00:00
|
|
|
COMMAND,
|
2011-12-27 03:18:46 +00:00
|
|
|
short_opt,
|
2006-01-22 21:10:55 +00:00
|
|
|
gnu_opt,
|
|
|
|
old_opt );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
for( size_t i=0; i<path.size(); i++ )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_remove2( path.at(i).c_str(),
|
2006-01-22 21:10:55 +00:00
|
|
|
PATH,
|
2011-12-27 03:18:46 +00:00
|
|
|
short_opt,
|
2006-01-22 21:10:55 +00:00
|
|
|
gnu_opt,
|
|
|
|
old_opt );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
const wchar_t *builtin_complete_get_temporary_buffer()
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2006-01-30 16:51:50 +00:00
|
|
|
return temporary_buffer;
|
|
|
|
}
|
|
|
|
|
2006-06-13 13:43:28 +00:00
|
|
|
/**
|
|
|
|
The complete builtin. Used for specifying programmable
|
|
|
|
tab-completions. Calls the functions in complete.c for any heavy
|
|
|
|
lifting. Defined in builtin_complete.c
|
|
|
|
*/
|
2012-01-16 20:10:08 +00:00
|
|
|
static int builtin_complete( parser_t &parser, wchar_t **argv )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-02-26 22:32:06 +00:00
|
|
|
bool res=false;
|
2006-01-22 21:10:55 +00:00
|
|
|
int argc=0;
|
|
|
|
int result_mode=SHARED;
|
|
|
|
int remove = 0;
|
2007-08-01 17:35:24 +00:00
|
|
|
int authoritative = -1;
|
2007-02-28 21:43:27 +00:00
|
|
|
int flags = COMPLETE_AUTO_SPACE;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 03:33:11 +00:00
|
|
|
wcstring short_opt;
|
2012-02-08 08:45:07 +00:00
|
|
|
wcstring_list_t gnu_opt, old_opt;
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *comp=L"", *desc=L"", *condition=L"";
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2012-02-26 21:46:21 +00:00
|
|
|
bool do_complete = false;
|
|
|
|
wcstring do_complete_param;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-08 08:45:07 +00:00
|
|
|
wcstring_list_t cmd;
|
|
|
|
wcstring_list_t path;
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
static int recursion_level=0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
argc = builtin_count_args( argv );
|
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
woptind=0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
while( ! res )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2010-10-08 00:43:57 +00:00
|
|
|
static const struct woption
|
2006-01-22 21:10:55 +00:00
|
|
|
long_options[] =
|
|
|
|
{
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"exclusive", no_argument, 0, 'x'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"no-files", no_argument, 0, 'f'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"require-parameter", no_argument, 0, 'r'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"path", required_argument, 0, 'p'
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
,
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"command", required_argument, 0, 'c'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
,
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"short-option", required_argument, 0, 's'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2006-02-15 02:49:00 +00:00
|
|
|
L"long-option", required_argument, 0, 'l'
|
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
,
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
L"old-option", required_argument, 0, 'o'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"description", required_argument, 0, 'd'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"arguments", required_argument, 0, 'a'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
|
|
|
L"erase", no_argument, 0, 'e'
|
|
|
|
}
|
|
|
|
,
|
|
|
|
{
|
2007-08-01 17:35:24 +00:00
|
|
|
L"unauthoritative", no_argument, 0, 'u'
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
,
|
2007-01-28 13:40:59 +00:00
|
|
|
{
|
2007-08-01 17:35:24 +00:00
|
|
|
L"authoritative", no_argument, 0, 'A'
|
2007-01-28 13:40:59 +00:00
|
|
|
}
|
|
|
|
,
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
|
|
|
L"condition", required_argument, 0, 'n'
|
|
|
|
}
|
|
|
|
,
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
2006-08-24 00:53:15 +00:00
|
|
|
L"do-complete", optional_argument, 0, 'C'
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
|
|
|
,
|
2006-05-26 11:24:02 +00:00
|
|
|
{
|
|
|
|
L"help", no_argument, 0, 'h'
|
|
|
|
}
|
|
|
|
,
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
0, 0, 0, 0
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
;
|
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
int opt_index = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
int opt = wgetopt_long( argc,
|
2011-12-27 03:18:46 +00:00
|
|
|
argv,
|
|
|
|
L"a:c:p:s:l:o:d:frxeuAn:C::h",
|
|
|
|
long_options,
|
2006-01-22 21:10:55 +00:00
|
|
|
&opt_index );
|
|
|
|
if( opt == -1 )
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
switch( opt )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if(long_options[opt_index].flag != 0)
|
|
|
|
break;
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2006-01-22 21:10:55 +00:00
|
|
|
BUILTIN_ERR_UNKNOWN,
|
|
|
|
argv[0],
|
|
|
|
long_options[opt_index].name );
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2006-01-22 23:40:03 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
case 'x':
|
2006-01-22 21:10:55 +00:00
|
|
|
result_mode |= EXCLUSIVE;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
case 'f':
|
2006-01-22 21:10:55 +00:00
|
|
|
result_mode |= NO_FILES;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
case 'r':
|
2006-01-22 21:10:55 +00:00
|
|
|
result_mode |= NO_COMMON;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
case 'p':
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'c':
|
2006-06-15 01:11:54 +00:00
|
|
|
{
|
2012-05-09 09:55:36 +00:00
|
|
|
wcstring tmp = woptarg;
|
|
|
|
if (unescape_string(tmp, 1))
|
2006-06-15 01:11:54 +00:00
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
if (opt=='p')
|
2012-05-09 09:55:36 +00:00
|
|
|
path.push_back(tmp);
|
2012-02-08 08:45:07 +00:00
|
|
|
else
|
2012-05-09 09:55:36 +00:00
|
|
|
cmd.push_back(tmp);
|
2006-06-15 01:11:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer, L"%ls: Invalid token '%ls'\n", argv[0], woptarg );
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-01-22 21:10:55 +00:00
|
|
|
break;
|
2006-06-15 01:11:54 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'd':
|
|
|
|
desc = woptarg;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'u':
|
2007-08-01 17:35:24 +00:00
|
|
|
authoritative=0;
|
2006-01-22 21:10:55 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-01-28 13:40:59 +00:00
|
|
|
case 'A':
|
2007-08-01 17:35:24 +00:00
|
|
|
authoritative=1;
|
2007-01-28 13:40:59 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 's':
|
2012-02-22 03:33:11 +00:00
|
|
|
short_opt.append(woptarg);
|
2006-01-22 21:10:55 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'l':
|
2012-02-08 08:45:07 +00:00
|
|
|
gnu_opt.push_back(woptarg);
|
2006-01-22 21:10:55 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'o':
|
2012-02-08 08:45:07 +00:00
|
|
|
old_opt.push_back(woptarg);
|
2006-01-22 21:10:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
comp = woptarg;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case 'e':
|
|
|
|
remove = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
condition = woptarg;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
case 'C':
|
2012-02-26 21:46:21 +00:00
|
|
|
do_complete = true;
|
|
|
|
do_complete_param = woptarg ? woptarg : reader_get_buffer();
|
2006-01-30 16:51:50 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-26 11:24:02 +00:00
|
|
|
case 'h':
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stdout_buffer );
|
2006-05-26 11:24:02 +00:00
|
|
|
return 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
case '?':
|
2012-01-16 20:10:08 +00:00
|
|
|
builtin_unknown_option( parser, argv[0], argv[woptind-1] );
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2006-01-22 23:40:03 +00:00
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
2006-01-22 23:40:03 +00:00
|
|
|
|
2006-05-21 19:26:30 +00:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
if( condition && wcslen( condition ) )
|
|
|
|
{
|
2012-01-16 20:10:08 +00:00
|
|
|
if( parser.test( condition, 0, 0, 0 ) )
|
2006-05-21 19:26:30 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2011-12-27 03:18:46 +00:00
|
|
|
L"%ls: Condition '%ls' contained a syntax error\n",
|
2006-05-21 19:26:30 +00:00
|
|
|
argv[0],
|
|
|
|
condition );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
parser.test( condition, 0, &stderr_buffer, argv[0] );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2006-05-21 19:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-05-21 19:26:30 +00:00
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
if( comp && wcslen( comp ) )
|
|
|
|
{
|
2012-01-16 20:10:08 +00:00
|
|
|
if( parser.test_args( comp, 0, 0 ) )
|
2006-05-21 19:26:30 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2011-12-27 03:18:46 +00:00
|
|
|
L"%ls: Completion '%ls' contained a syntax error\n",
|
2006-05-21 19:26:30 +00:00
|
|
|
argv[0],
|
|
|
|
comp );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-22 18:51:06 +00:00
|
|
|
parser.test_args( comp, &stderr_buffer, argv[0] );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2006-05-21 19:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-15 02:49:00 +00:00
|
|
|
if( !res )
|
2006-01-30 16:51:50 +00:00
|
|
|
{
|
2006-02-15 02:49:00 +00:00
|
|
|
if( do_complete )
|
|
|
|
{
|
2012-02-06 08:57:43 +00:00
|
|
|
const wchar_t *token;
|
2007-04-20 19:55:06 +00:00
|
|
|
|
2012-02-26 21:46:21 +00:00
|
|
|
parse_util_token_extent( do_complete_param.c_str(), do_complete_param.size(), &token, 0, 0, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 21:46:21 +00:00
|
|
|
const wchar_t *prev_temporary_buffer = temporary_buffer;
|
|
|
|
temporary_buffer = do_complete_param.c_str();
|
2006-01-30 16:51:50 +00:00
|
|
|
|
2006-02-15 02:49:00 +00:00
|
|
|
if( recursion_level < 1 )
|
|
|
|
{
|
|
|
|
recursion_level++;
|
2012-02-26 22:32:06 +00:00
|
|
|
|
|
|
|
std::vector<completion_t> comp;
|
2012-02-26 21:46:21 +00:00
|
|
|
complete( do_complete_param, comp, COMPLETE_DEFAULT );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-30 06:06:58 +00:00
|
|
|
for( size_t i=0; i< comp.size() ; i++ )
|
2006-02-15 02:49:00 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
const completion_t &next = comp.at( i );
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *prepend;
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( next.flags & COMPLETE_NO_CASE )
|
2007-04-20 19:55:06 +00:00
|
|
|
{
|
|
|
|
prepend = L"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prepend = token;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-20 19:55:06 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( !(next.description).empty() )
|
2007-03-26 06:13:07 +00:00
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stdout_buffer, L"%ls%ls\t%ls\n", prepend, next.completion.c_str(), next.description.c_str() );
|
2007-03-26 06:13:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stdout_buffer, L"%ls%ls\n", prepend, next.completion.c_str() );
|
2007-03-26 06:13:07 +00:00
|
|
|
}
|
2006-02-15 02:49:00 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-02-15 02:49:00 +00:00
|
|
|
recursion_level--;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
temporary_buffer = prev_temporary_buffer;
|
|
|
|
|
2006-01-30 16:51:50 +00:00
|
|
|
}
|
2006-02-15 02:49:00 +00:00
|
|
|
else if( woptind != argc )
|
|
|
|
{
|
2012-02-22 18:51:06 +00:00
|
|
|
append_format(stderr_buffer,
|
2006-02-15 02:49:00 +00:00
|
|
|
_( L"%ls: Too many arguments\n" ),
|
|
|
|
argv[0] );
|
2012-02-22 18:51:06 +00:00
|
|
|
builtin_print_help( parser, argv[0], stderr_buffer );
|
2006-01-22 21:10:55 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
res = true;
|
2006-02-15 02:49:00 +00:00
|
|
|
}
|
2012-02-08 08:45:07 +00:00
|
|
|
else if( cmd.empty() && path.empty() )
|
2006-01-22 21:10:55 +00:00
|
|
|
{
|
2006-02-15 02:49:00 +00:00
|
|
|
/* No arguments specified, meaning we print the definitions of
|
|
|
|
* all specified completions to stdout.*/
|
2012-02-22 18:51:06 +00:00
|
|
|
complete_print( stdout_buffer );
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-02-15 02:49:00 +00:00
|
|
|
if( remove )
|
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_remove( cmd,
|
|
|
|
path,
|
2012-02-22 03:33:11 +00:00
|
|
|
short_opt.c_str(),
|
2012-02-08 08:45:07 +00:00
|
|
|
gnu_opt,
|
|
|
|
old_opt );
|
2006-02-15 02:49:00 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-08 08:45:07 +00:00
|
|
|
builtin_complete_add( cmd,
|
|
|
|
path,
|
2012-02-22 03:33:11 +00:00
|
|
|
short_opt.c_str(),
|
2012-02-08 08:45:07 +00:00
|
|
|
gnu_opt,
|
|
|
|
old_opt,
|
2011-12-27 03:18:46 +00:00
|
|
|
result_mode,
|
2007-08-01 17:35:24 +00:00
|
|
|
authoritative,
|
2006-02-15 02:49:00 +00:00
|
|
|
condition,
|
|
|
|
comp,
|
2007-02-28 21:43:27 +00:00
|
|
|
desc,
|
2011-12-27 03:18:46 +00:00
|
|
|
flags );
|
2006-02-15 02:49:00 +00:00
|
|
|
}
|
2006-01-22 23:40:03 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-02-15 02:49:00 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-02-26 22:32:06 +00:00
|
|
|
return res ? 1 : 0;
|
2006-01-22 21:10:55 +00:00
|
|
|
}
|