2005-09-20 13:26:39 +00:00
|
|
|
/** \file wildcard.c
|
2005-11-29 14:33:52 +00:00
|
|
|
|
2006-01-04 12:51:02 +00:00
|
|
|
Fish needs it's own globbing implementation to support
|
|
|
|
tab-expansion of globbed parameters. Also provides recursive
|
|
|
|
wildcards using **.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2012-01-16 16:56:47 +00:00
|
|
|
#include <algorithm>
|
2005-09-20 13:26:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2007-02-25 09:05:24 +00:00
|
|
|
#include <string.h>
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2006-02-28 13:17:16 +00:00
|
|
|
|
|
|
|
#include "fallback.h"
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "util.h"
|
2006-02-28 13:17:16 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
#include "wutil.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "expand.h"
|
2007-02-25 09:05:24 +00:00
|
|
|
#include "exec.h"
|
|
|
|
#include "halloc_util.h"
|
2006-07-19 22:55:49 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-11-30 15:33:03 +00:00
|
|
|
/**
|
|
|
|
This flag is set in the flags parameter of wildcard_expand if the
|
|
|
|
call is part of a recursiv wildcard search. It is used to make sure
|
|
|
|
that the contents of subdirectories are only searched once.
|
|
|
|
*/
|
|
|
|
#define WILDCARD_RECURSIVE 64
|
|
|
|
|
2005-11-29 16:52:02 +00:00
|
|
|
/**
|
|
|
|
The maximum length of a filename token. This is a fallback value,
|
2011-12-27 03:18:46 +00:00
|
|
|
an attempt to find the true value using patchconf is always made.
|
2005-11-29 16:52:02 +00:00
|
|
|
*/
|
|
|
|
#define MAX_FILE_LENGTH 1024
|
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
/**
|
|
|
|
The command to run to get a description from a file suffix
|
|
|
|
*/
|
|
|
|
#define SUFFIX_CMD_STR L"mimedb 2>/dev/null -fd "
|
|
|
|
|
|
|
|
/**
|
|
|
|
Description for generic executable
|
|
|
|
*/
|
|
|
|
#define COMPLETE_EXEC_DESC _( L"Executable" )
|
|
|
|
/**
|
|
|
|
Description for link to executable
|
|
|
|
*/
|
|
|
|
#define COMPLETE_EXEC_LINK_DESC _( L"Executable link" )
|
|
|
|
|
|
|
|
/**
|
|
|
|
Description for regular file
|
|
|
|
*/
|
|
|
|
#define COMPLETE_FILE_DESC _( L"File" )
|
|
|
|
/**
|
|
|
|
Description for character device
|
|
|
|
*/
|
|
|
|
#define COMPLETE_CHAR_DESC _( L"Character device" )
|
|
|
|
/**
|
|
|
|
Description for block device
|
|
|
|
*/
|
|
|
|
#define COMPLETE_BLOCK_DESC _( L"Block device" )
|
|
|
|
/**
|
|
|
|
Description for fifo buffer
|
|
|
|
*/
|
|
|
|
#define COMPLETE_FIFO_DESC _( L"Fifo" )
|
|
|
|
/**
|
|
|
|
Description for symlink
|
|
|
|
*/
|
|
|
|
#define COMPLETE_SYMLINK_DESC _( L"Symbolic link" )
|
|
|
|
/**
|
|
|
|
Description for symlink
|
|
|
|
*/
|
|
|
|
#define COMPLETE_DIRECTORY_SYMLINK_DESC _( L"Symbolic link to directory" )
|
|
|
|
/**
|
|
|
|
Description for Rotten symlink
|
|
|
|
*/
|
|
|
|
#define COMPLETE_ROTTEN_SYMLINK_DESC _( L"Rotten symbolic link" )
|
|
|
|
/**
|
|
|
|
Description for symlink loop
|
|
|
|
*/
|
|
|
|
#define COMPLETE_LOOP_SYMLINK_DESC _( L"Symbolic link loop" )
|
|
|
|
/**
|
|
|
|
Description for socket files
|
|
|
|
*/
|
|
|
|
#define COMPLETE_SOCKET_DESC _( L"Socket" )
|
|
|
|
/**
|
|
|
|
Description for directories
|
|
|
|
*/
|
|
|
|
#define COMPLETE_DIRECTORY_DESC _( L"Directory" )
|
|
|
|
|
|
|
|
/** Hashtable containing all descriptions that describe an executable */
|
|
|
|
static hash_table_t *suffix_hash=0;
|
|
|
|
|
2006-02-19 01:54:38 +00:00
|
|
|
/**
|
|
|
|
Push the specified argument to the list if an identical string is
|
|
|
|
not already in the list. This function iterates over the list,
|
|
|
|
which is quite slow if the list is large. It might make sense to
|
|
|
|
use a hashtable for this.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static void al_push_check( array_list_t *l, const wchar_t *newv )
|
2006-02-19 01:54:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
for( i = 0; i < al_get_count(l); i++ )
|
2006-02-19 01:54:38 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
if( !wcscmp( (const wchar_t *)al_get(l, i), newv ) )
|
2006-02-19 01:54:38 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
free( (void *)newv );
|
2006-02-19 01:54:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
al_push( l, newv );
|
2006-02-19 01:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
/**
|
|
|
|
Free hash key and hash value
|
|
|
|
*/
|
|
|
|
static void clear_hash_entry( void *key, void *data )
|
|
|
|
{
|
|
|
|
free( (void *)key );
|
|
|
|
free( (void *)data );
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
int wildcard_has( const wchar_t *str, int internal )
|
|
|
|
{
|
|
|
|
wchar_t prev=0;
|
2006-06-20 21:20:16 +00:00
|
|
|
if( !str )
|
|
|
|
{
|
|
|
|
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
|
2011-12-27 03:18:46 +00:00
|
|
|
return 0;
|
2006-06-20 21:20:16 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( internal )
|
|
|
|
{
|
|
|
|
for( ; *str; str++ )
|
|
|
|
{
|
|
|
|
if( ( *str == ANY_CHAR ) || (*str == ANY_STRING) || (*str == ANY_STRING_RECURSIVE) )
|
|
|
|
return 1;
|
|
|
|
prev = *str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( ; *str; str++ )
|
|
|
|
{
|
|
|
|
if( ( (*str == L'*' ) || (*str == L'?' ) ) && (prev != L'\\') )
|
|
|
|
return 1;
|
|
|
|
prev = *str;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Check whether the string str matches the wildcard string wc.
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
\param str String to be matched.
|
|
|
|
\param wc The wildcard.
|
2011-12-27 03:18:46 +00:00
|
|
|
\param is_first Whether files beginning with dots should not be matched against wildcards.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static int wildcard_match2( const wchar_t *str,
|
|
|
|
const wchar_t *wc,
|
2005-09-20 13:26:39 +00:00
|
|
|
int is_first )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( *str == 0 && *wc==0 )
|
|
|
|
return 1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( *wc == ANY_STRING || *wc == ANY_STRING_RECURSIVE)
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Ignore hidden file */
|
|
|
|
if( is_first && *str == L'.' )
|
2005-11-30 15:33:03 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
return 0;
|
2005-11-30 15:33:03 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Try all submatches */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( wildcard_match2( str, wc+1, 0 ) )
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while( *(str++) != 0 );
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-28 18:09:16 +00:00
|
|
|
else if( *str == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
End of string, but not end of wildcard, and the next wildcard
|
|
|
|
element is not a '*', so this is not a match.
|
|
|
|
*/
|
|
|
|
return 0;
|
|
|
|
}
|
2005-11-30 15:33:03 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( *wc == ANY_CHAR )
|
2005-11-30 15:33:03 +00:00
|
|
|
{
|
|
|
|
if( is_first && *str == L'.' )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return wildcard_match2( str+1, wc+1, 0 );
|
2005-11-30 15:33:03 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( *wc == *str )
|
|
|
|
return wildcard_match2( str+1, wc+1, 0 );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Matches the string against the wildcard, and if the wildcard is a
|
|
|
|
possible completion of the string, the remainder of the string is
|
|
|
|
inserted into the array_list_t.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static int wildcard_complete_internal( const wchar_t *orig,
|
|
|
|
const wchar_t *str,
|
|
|
|
const wchar_t *wc,
|
2005-09-20 13:26:39 +00:00
|
|
|
int is_first,
|
|
|
|
const wchar_t *desc,
|
|
|
|
const wchar_t *(*desc_func)(const wchar_t *),
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> &out,
|
2007-02-25 09:05:24 +00:00
|
|
|
int flags )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-06-20 21:20:16 +00:00
|
|
|
if( !wc || !str || !orig)
|
2006-02-22 15:41:52 +00:00
|
|
|
{
|
|
|
|
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
|
2011-12-27 03:18:46 +00:00
|
|
|
return 0;
|
2006-02-22 15:41:52 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 02:56:56 +00:00
|
|
|
if( *wc == 0 &&
|
2005-09-20 13:26:39 +00:00
|
|
|
( ( *str != L'.') || (!is_first)) )
|
|
|
|
{
|
2007-02-24 08:11:31 +00:00
|
|
|
wchar_t *out_completion = 0;
|
|
|
|
const wchar_t *out_desc = desc;
|
2012-01-20 16:50:31 +00:00
|
|
|
|
2007-04-20 19:34:30 +00:00
|
|
|
if( flags & COMPLETE_NO_CASE )
|
|
|
|
{
|
|
|
|
out_completion = wcsdup( orig );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out_completion = wcsdup( str );
|
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( wcschr( str, PROG_COMPLETE_SEP ) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This completion has an embedded description, du not use the generic description
|
|
|
|
*/
|
|
|
|
wchar_t *sep;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-24 08:11:31 +00:00
|
|
|
sep = wcschr(out_completion, PROG_COMPLETE_SEP );
|
|
|
|
*sep = 0;
|
|
|
|
out_desc = sep + 1;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-12 14:49:03 +00:00
|
|
|
if( desc_func )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
A descripton generating function is specified, call
|
|
|
|
it. If it returns something, use that as the
|
|
|
|
description.
|
|
|
|
*/
|
2006-01-16 13:42:44 +00:00
|
|
|
const wchar_t *func_desc = desc_func( orig );
|
2006-01-12 14:49:03 +00:00
|
|
|
if( func_desc )
|
2007-02-24 08:11:31 +00:00
|
|
|
out_desc = func_desc;
|
2006-01-12 14:49:03 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-28 21:43:27 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-24 08:11:31 +00:00
|
|
|
if( out_completion )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
completion_allocate( out,
|
2007-02-24 08:11:31 +00:00
|
|
|
out_completion,
|
|
|
|
out_desc,
|
2007-02-25 09:05:24 +00:00
|
|
|
flags );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-24 08:11:31 +00:00
|
|
|
free ( out_completion );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( *wc == ANY_STRING )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
int res=0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Ignore hidden file */
|
|
|
|
if( is_first && str[0] == L'.' )
|
|
|
|
return 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/* Try all submatches */
|
|
|
|
do
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
res |= wildcard_complete_internal( orig, str, wc+1, 0, desc, desc_func, out, flags );
|
2012-01-20 16:50:31 +00:00
|
|
|
if( res )
|
2005-09-20 13:26:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
while( *str++ != 0 );
|
|
|
|
return res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else if( *wc == ANY_CHAR )
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
else if( *wc == *str )
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-02-28 21:43:27 +00:00
|
|
|
else if( towlower(*wc) == towlower(*str) )
|
|
|
|
{
|
|
|
|
return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags | COMPLETE_NO_CASE );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
return 0;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int wildcard_complete( const wchar_t *str,
|
|
|
|
const wchar_t *wc,
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *desc,
|
2005-09-20 13:26:39 +00:00
|
|
|
const wchar_t *(*desc_func)(const wchar_t *),
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> &out,
|
2007-02-25 09:05:24 +00:00
|
|
|
int flags )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-02-28 21:43:27 +00:00
|
|
|
int res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
res = wildcard_complete_internal( str, str, wc, 1, desc, desc_func, out, flags );
|
2007-02-28 21:43:27 +00:00
|
|
|
|
|
|
|
return res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int wildcard_match( const wchar_t *str, const wchar_t *wc )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
return wildcard_match2( str, wc, 1 );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
Creates a path from the specified directory and filename.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
static wchar_t *make_path( const wchar_t *base_dir, const wchar_t *name )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *long_name;
|
|
|
|
int base_len = wcslen( base_dir );
|
2011-12-27 03:18:46 +00:00
|
|
|
if( !(long_name= (wchar_t *)malloc( sizeof(wchar_t)*(base_len+wcslen(name)+1) )))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
wcscpy( long_name, base_dir );
|
|
|
|
wcscpy(&long_name[base_len], name );
|
|
|
|
return long_name;
|
|
|
|
}
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
Return a description of a file based on its suffix. This function
|
|
|
|
does not perform any caching, it directly calls the mimedb command
|
|
|
|
to do a lookup.
|
|
|
|
*/
|
2007-02-25 09:05:24 +00:00
|
|
|
static wchar_t *complete_get_desc_suffix_internal( const wchar_t *suff_orig )
|
|
|
|
{
|
|
|
|
|
|
|
|
wchar_t *suff = wcsdup( suff_orig );
|
|
|
|
wchar_t *cmd = wcsdupcat( SUFFIX_CMD_STR, suff );
|
|
|
|
|
|
|
|
if( !suff || !cmd )
|
|
|
|
DIE_MEM();
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
wcstring_list_t lst;
|
|
|
|
wcstring desc;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
if( exec_subshell2( cmd, lst ) != -1 )
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
if( lst.size()>0 )
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
const wcstring & ln = lst.at(0);
|
|
|
|
if( ln.size() > 0 && ln != L"unknown" )
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
desc = ln;
|
2007-02-25 09:05:24 +00:00
|
|
|
/*
|
|
|
|
I have decided I prefer to have the description
|
|
|
|
begin in uppercase and the whole universe will just
|
|
|
|
have to accept it. Hah!
|
|
|
|
*/
|
|
|
|
desc[0]=towupper(desc[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
free(cmd);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
if( ! desc.size() )
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2011-12-28 20:36:47 +00:00
|
|
|
desc = COMPLETE_FILE_DESC;
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
wchar_t *tmp = wcsdup(desc.c_str());
|
|
|
|
hash_put( suffix_hash, suff, tmp );
|
2007-02-25 09:05:24 +00:00
|
|
|
|
2011-12-28 20:36:47 +00:00
|
|
|
return tmp;
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
Free the suffix_hash hash table and all memory used by it.
|
|
|
|
*/
|
2007-02-25 09:05:24 +00:00
|
|
|
static void complete_get_desc_destroy_suffix_hash()
|
|
|
|
{
|
2008-01-13 16:47:47 +00:00
|
|
|
if( suffix_hash )
|
|
|
|
{
|
|
|
|
hash_foreach( suffix_hash, &clear_hash_entry );
|
|
|
|
hash_destroy( suffix_hash );
|
|
|
|
free( suffix_hash );
|
|
|
|
}
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
|
2006-06-20 00:50:10 +00:00
|
|
|
/**
|
2007-02-25 09:05:24 +00:00
|
|
|
Use the mimedb command to look up a description for a given suffix
|
2006-06-20 00:50:10 +00:00
|
|
|
*/
|
2007-02-25 09:05:24 +00:00
|
|
|
static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
int len;
|
|
|
|
wchar_t *suff;
|
|
|
|
wchar_t *pos;
|
|
|
|
wchar_t *tmp;
|
|
|
|
wchar_t *desc;
|
|
|
|
|
|
|
|
len = wcslen(suff_orig );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
if( len == 0 )
|
|
|
|
return COMPLETE_FILE_DESC;
|
|
|
|
|
|
|
|
if( !suffix_hash )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
suffix_hash = (hash_table_t *)malloc( sizeof( hash_table_t) );
|
2007-02-25 09:05:24 +00:00
|
|
|
if( !suffix_hash )
|
|
|
|
DIE_MEM();
|
|
|
|
hash_init( suffix_hash, &hash_wcs_func, &hash_wcs_cmp );
|
|
|
|
halloc_register_function_void( global_context, &complete_get_desc_destroy_suffix_hash );
|
|
|
|
}
|
|
|
|
|
|
|
|
suff = wcsdup(suff_orig);
|
2006-11-09 17:58:04 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
/*
|
|
|
|
Drop characters that are commonly used as backup suffixes from the suffix
|
|
|
|
*/
|
|
|
|
for( pos=suff; *pos; pos++ )
|
|
|
|
{
|
|
|
|
if( wcschr( L"?;#~@&", *pos ) )
|
|
|
|
{
|
|
|
|
*pos=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = escape( suff, 1 );
|
|
|
|
free(suff);
|
|
|
|
suff = tmp;
|
|
|
|
desc = (wchar_t *)hash_get( suffix_hash, suff );
|
|
|
|
|
|
|
|
if( !desc )
|
|
|
|
{
|
|
|
|
desc = complete_get_desc_suffix_internal( suff );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
free( suff );
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Obtain a description string for the file specified by the filename.
|
|
|
|
|
|
|
|
The returned value is a string constant and should not be free'd.
|
|
|
|
|
|
|
|
\param filename The file for which to find a description string
|
|
|
|
\param lstat_res The result of calling lstat on the file
|
|
|
|
\param lbuf The struct buf output of calling lstat on the file
|
|
|
|
\param stat_res The result of calling stat on the file
|
|
|
|
\param buf The struct buf output of calling stat on the file
|
2011-12-27 03:18:46 +00:00
|
|
|
\param err The errno value after a failed stat call on the file.
|
2007-02-25 09:05:24 +00:00
|
|
|
*/
|
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
static const wchar_t *file_get_desc( const wchar_t *filename,
|
2007-02-25 09:05:24 +00:00
|
|
|
int lstat_res,
|
2011-12-27 03:18:46 +00:00
|
|
|
struct stat lbuf,
|
|
|
|
int stat_res,
|
|
|
|
struct stat buf,
|
2007-02-25 09:05:24 +00:00
|
|
|
int err )
|
|
|
|
{
|
2012-01-05 21:58:48 +00:00
|
|
|
const wchar_t *suffix;
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
CHECK( filename, 0 );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
if( !lstat_res )
|
|
|
|
{
|
|
|
|
if( S_ISLNK(lbuf.st_mode))
|
|
|
|
{
|
|
|
|
if( !stat_res )
|
|
|
|
{
|
|
|
|
if( S_ISDIR(buf.st_mode) )
|
|
|
|
{
|
|
|
|
return COMPLETE_DIRECTORY_SYMLINK_DESC;
|
|
|
|
}
|
2007-02-25 11:17:38 +00:00
|
|
|
else
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2007-02-25 11:17:38 +00:00
|
|
|
|
|
|
|
if( ( buf.st_mode & S_IXUSR ) ||
|
|
|
|
( buf.st_mode & S_IXGRP ) ||
|
|
|
|
( buf.st_mode & S_IXOTH ) )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 11:17:38 +00:00
|
|
|
if( waccess( filename, X_OK ) == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Weird group permissions and other such
|
|
|
|
issues make it non-trivial to find out
|
|
|
|
if we can actually execute a file using
|
|
|
|
the result from stat. It is much safer
|
|
|
|
to use the access function, since it
|
|
|
|
tells us exactly what we want to know.
|
|
|
|
*/
|
|
|
|
return COMPLETE_EXEC_LINK_DESC;
|
|
|
|
}
|
|
|
|
}
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
return COMPLETE_SYMLINK_DESC;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch( err )
|
|
|
|
{
|
|
|
|
case ENOENT:
|
|
|
|
{
|
|
|
|
return COMPLETE_ROTTEN_SYMLINK_DESC;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
case ELOOP:
|
|
|
|
{
|
|
|
|
return COMPLETE_LOOP_SYMLINK_DESC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
On unknown errors we do nothing. The file will be
|
|
|
|
given the default 'File' description or one based on the suffix.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if( S_ISCHR(buf.st_mode) )
|
|
|
|
{
|
|
|
|
return COMPLETE_CHAR_DESC;
|
|
|
|
}
|
|
|
|
else if( S_ISBLK(buf.st_mode) )
|
|
|
|
{
|
|
|
|
return COMPLETE_BLOCK_DESC;
|
|
|
|
}
|
|
|
|
else if( S_ISFIFO(buf.st_mode) )
|
|
|
|
{
|
|
|
|
return COMPLETE_FIFO_DESC;
|
|
|
|
}
|
|
|
|
else if( S_ISSOCK(buf.st_mode))
|
|
|
|
{
|
|
|
|
return COMPLETE_SOCKET_DESC;
|
|
|
|
}
|
|
|
|
else if( S_ISDIR(buf.st_mode) )
|
|
|
|
{
|
|
|
|
return COMPLETE_DIRECTORY_DESC;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
else
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
2007-02-25 11:17:38 +00:00
|
|
|
if( ( buf.st_mode & S_IXUSR ) ||
|
|
|
|
( buf.st_mode & S_IXGRP ) ||
|
|
|
|
( buf.st_mode & S_IXOTH ) )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 11:17:38 +00:00
|
|
|
if( waccess( filename, X_OK ) == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Weird group permissions and other such issues
|
|
|
|
make it non-trivial to find out if we can
|
|
|
|
actually execute a file using the result from
|
|
|
|
stat. It is much safer to use the access
|
|
|
|
function, since it tells us exactly what we want
|
|
|
|
to know.
|
|
|
|
*/
|
|
|
|
return COMPLETE_EXEC_DESC;
|
|
|
|
}
|
|
|
|
}
|
2007-02-25 09:05:24 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
suffix = wcsrchr( filename, L'.' );
|
|
|
|
if( suffix != 0 && !wcsrchr( suffix, L'/' ) )
|
|
|
|
{
|
|
|
|
return complete_get_desc_suffix( suffix );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
return COMPLETE_FILE_DESC ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-12-27 03:18:46 +00:00
|
|
|
Add the specified filename if it matches the specified wildcard.
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
If the filename matches, first get the description of the specified
|
|
|
|
filename. If this is a regular file, append the filesize to the
|
|
|
|
description.
|
|
|
|
|
|
|
|
\param list the list to add he completion to
|
|
|
|
\param fullname the full filename of the file
|
|
|
|
\param completion the completion part of the file name
|
|
|
|
\param wc the wildcard to match against
|
|
|
|
\param is_cmd whether we are performing command completion
|
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
static void wildcard_completion_allocate( std::vector<completion_t> &list,
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *fullname,
|
2007-10-15 09:51:08 +00:00
|
|
|
const wchar_t *completion,
|
|
|
|
const wchar_t *wc,
|
|
|
|
int is_cmd )
|
2007-02-25 09:05:24 +00:00
|
|
|
{
|
|
|
|
const wchar_t *desc;
|
|
|
|
struct stat buf, lbuf;
|
|
|
|
static string_buffer_t *sb = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
int free_completion = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
int flags = 0;
|
|
|
|
int stat_res, lstat_res;
|
|
|
|
int stat_errno=0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
long long sz;
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
if( !sb )
|
2006-06-20 21:20:16 +00:00
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
sb = sb_halloc( global_context );
|
2006-06-20 21:20:16 +00:00
|
|
|
}
|
2007-02-25 09:05:24 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sb_clear( sb );
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK( fullname, );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
sb_clear( sb );
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If the file is a symlink, we need to stat both the file itself
|
|
|
|
_and_ the destination file. But we try to avoid this with
|
|
|
|
non-symlinks by first doing an lstat, and if the file is not a
|
|
|
|
link we copy the results over to the regular stat buffer.
|
|
|
|
*/
|
|
|
|
if( ( lstat_res = lwstat( fullname, &lbuf ) ) )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
sz=-1;
|
2007-02-25 09:05:24 +00:00
|
|
|
stat_res = lstat_res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
if( S_ISLNK(lbuf.st_mode))
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
if( ( stat_res = wstat( fullname, &buf ) ) )
|
|
|
|
{
|
|
|
|
sz=-1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sz = (long long)buf.st_size;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
/*
|
|
|
|
In order to differentiate between e.g. rotten symlinks
|
|
|
|
and symlink loops, we also need to know the error status of wstat.
|
|
|
|
*/
|
|
|
|
stat_errno = errno;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stat_res = lstat_res;
|
|
|
|
memcpy( &buf, &lbuf, sizeof( struct stat ) );
|
|
|
|
sz = (long long)buf.st_size;
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-02-25 09:05:24 +00:00
|
|
|
desc = file_get_desc( fullname, lstat_res, lbuf, stat_res, buf, stat_errno );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( sz >= 0 && S_ISDIR(buf.st_mode) )
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
free_completion = 1;
|
|
|
|
flags = flags | COMPLETE_NO_SPACE;
|
|
|
|
completion = wcsdupcat( completion, L"/" );
|
2006-01-21 02:52:06 +00:00
|
|
|
sb_append( sb, desc );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
sb_append( sb, desc, L", ", NULL );
|
2007-10-15 09:51:08 +00:00
|
|
|
sb_format_size( sb, sz );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2007-02-25 09:05:24 +00:00
|
|
|
|
|
|
|
wildcard_complete( completion, wc, (wchar_t *)sb->buff, 0, list, flags );
|
|
|
|
if( free_completion )
|
2007-02-25 11:17:38 +00:00
|
|
|
free( (void *)completion );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2006-06-20 00:50:10 +00:00
|
|
|
/**
|
2005-11-29 14:33:52 +00:00
|
|
|
Test if the file specified by the given filename matches the
|
2005-12-07 15:57:17 +00:00
|
|
|
expansion flags specified. flags can be a combination of
|
2005-11-29 14:33:52 +00:00
|
|
|
EXECUTABLES_ONLY and DIRECTORIES_ONLY.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static int test_flags( const wchar_t *filename,
|
2005-09-20 13:26:39 +00:00
|
|
|
int flags )
|
|
|
|
{
|
2006-11-23 10:40:23 +00:00
|
|
|
if( flags & DIRECTORIES_ONLY )
|
2005-12-25 22:00:44 +00:00
|
|
|
{
|
2006-11-23 10:40:23 +00:00
|
|
|
struct stat buf;
|
|
|
|
if( wstat( filename, &buf ) == -1 )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !S_ISDIR( buf.st_mode ) )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-12-25 22:00:44 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( flags & EXECUTABLES_ONLY )
|
2006-11-23 10:40:23 +00:00
|
|
|
{
|
|
|
|
if ( waccess( filename, X_OK ) != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-11-23 10:40:23 +00:00
|
|
|
return 1;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
/**
|
|
|
|
The real implementation of wildcard expansion is in this
|
|
|
|
function. Other functions are just wrappers around this one.
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2008-01-13 16:47:47 +00:00
|
|
|
This function traverses the relevant directory tree looking for
|
|
|
|
matches, and recurses when needed to handle wildcrards spanning
|
2011-12-27 03:18:46 +00:00
|
|
|
multiple components and recursive wildcards.
|
2008-01-13 16:47:47 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
static int wildcard_expand_internal( const wchar_t *wc,
|
2007-03-24 19:07:38 +00:00
|
|
|
const wchar_t *base_dir,
|
|
|
|
int flags,
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> &out )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/* Points to the end of the current wildcard segment */
|
2012-01-05 21:58:48 +00:00
|
|
|
const wchar_t *wc_end;
|
2005-11-29 14:33:52 +00:00
|
|
|
|
|
|
|
/* Variables for traversing a directory */
|
|
|
|
DIR *dir;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/* The result returned */
|
|
|
|
int res = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/* Length of the directory to search in */
|
|
|
|
int base_len;
|
|
|
|
|
|
|
|
/* Variables for testing for presense of recursive wildcards */
|
2012-01-05 21:58:48 +00:00
|
|
|
const wchar_t *wc_recursive;
|
2005-11-29 14:33:52 +00:00
|
|
|
int is_recursive;
|
|
|
|
|
|
|
|
/* Sligtly mangled version of base_dir */
|
|
|
|
const wchar_t *dir_string;
|
|
|
|
|
|
|
|
/* Description for completions */
|
|
|
|
string_buffer_t sb_desc;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-12-07 16:06:47 +00:00
|
|
|
// debug( 3, L"WILDCARD_EXPAND %ls in %ls", wc, base_dir );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2007-01-09 16:47:05 +00:00
|
|
|
if( reader_interrupted() )
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-18 18:33:19 +00:00
|
|
|
if( !wc || !base_dir )
|
2006-06-20 21:20:16 +00:00
|
|
|
{
|
|
|
|
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
|
2011-12-27 03:18:46 +00:00
|
|
|
return 0;
|
2006-06-20 21:20:16 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Avoid excessive number of returned matches for wc ending with a *
|
2005-11-29 14:33:52 +00:00
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
int len = wcslen(wc);
|
|
|
|
if( len && (wc[len-1]==ANY_STRING) )
|
|
|
|
{
|
|
|
|
wchar_t * foo = wcsdup( wc );
|
|
|
|
foo[len-1]=0;
|
2007-03-24 19:07:38 +00:00
|
|
|
int res = wildcard_expand_internal( foo, base_dir, flags, out );
|
2005-09-20 13:26:39 +00:00
|
|
|
free( foo );
|
2011-12-27 03:18:46 +00:00
|
|
|
return res;
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/*
|
|
|
|
Initialize various variables
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
dir_string = base_dir[0]==L'\0'?L".":base_dir;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( !(dir = wopendir( dir_string )))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
wc_end = wcschr(wc,L'/');
|
|
|
|
base_len = wcslen( base_dir );
|
|
|
|
|
|
|
|
/*
|
|
|
|
Test for recursive match string in current segment
|
2011-12-27 03:18:46 +00:00
|
|
|
*/
|
2005-11-29 14:33:52 +00:00
|
|
|
wc_recursive = wcschr( wc, ANY_STRING_RECURSIVE );
|
|
|
|
is_recursive = ( wc_recursive && (!wc_end || wc_recursive < wc_end));
|
2005-11-30 15:33:03 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
|
|
|
sb_init( &sb_desc );
|
|
|
|
|
|
|
|
/*
|
|
|
|
Is this segment of the wildcard the last?
|
|
|
|
*/
|
2006-10-21 22:44:29 +00:00
|
|
|
if( !wc_end )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-11-29 14:33:52 +00:00
|
|
|
Wildcard segment is the last segment,
|
2005-09-20 13:26:39 +00:00
|
|
|
|
|
|
|
Insert all matching files/directories
|
|
|
|
*/
|
|
|
|
if( wc[0]=='\0' )
|
|
|
|
{
|
|
|
|
/*
|
2005-11-29 14:33:52 +00:00
|
|
|
The last wildcard segment is empty. Insert everything if
|
|
|
|
completing, the directory itself otherwise.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring next;
|
|
|
|
while(wreaddir(dir, next))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
if( next[0] != L'.' )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *name = next.c_str();
|
|
|
|
const wchar_t *long_name = make_path( base_dir, name );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( test_flags( long_name, flags ) )
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
wildcard_completion_allocate( out,
|
|
|
|
long_name,
|
|
|
|
name,
|
|
|
|
L"",
|
|
|
|
flags & EXECUTABLES_ONLY );
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
free( (void *)long_name );
|
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
res = 1;
|
2012-01-16 18:24:57 +00:00
|
|
|
completion_t data_to_push;
|
|
|
|
data_to_push.completion = base_dir;
|
2012-01-16 16:56:47 +00:00
|
|
|
if ( std::find( out.begin(), out.end(), data_to_push ) != out.end() ){
|
|
|
|
out.push_back( data_to_push);
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is the last wildcard segment, and it is not empty. Match files/directories.
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
wcstring next;
|
|
|
|
while(wreaddir(dir, next))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *name = next.c_str();
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *long_name = make_path( base_dir, name );
|
2005-11-29 14:33:52 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
/*
|
2011-12-27 03:18:46 +00:00
|
|
|
Test for matches before stating file, so as to minimize the number of calls to the much slower stat function
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> test;
|
2005-09-20 13:26:39 +00:00
|
|
|
if( wildcard_complete( name,
|
|
|
|
wc,
|
|
|
|
L"",
|
|
|
|
0,
|
2012-01-16 16:56:47 +00:00
|
|
|
test,
|
2005-09-20 13:26:39 +00:00
|
|
|
0 ) )
|
2005-11-30 15:33:03 +00:00
|
|
|
{
|
2005-09-20 13:26:39 +00:00
|
|
|
if( test_flags( long_name, flags ) )
|
|
|
|
{
|
2007-02-25 09:05:24 +00:00
|
|
|
wildcard_completion_allocate( out,
|
|
|
|
long_name,
|
|
|
|
name,
|
|
|
|
wc,
|
|
|
|
flags & EXECUTABLES_ONLY );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
free( long_name );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( wildcard_match2( name, wc, 1 ) )
|
|
|
|
{
|
|
|
|
wchar_t *long_name = make_path( base_dir, name );
|
2006-10-21 22:44:29 +00:00
|
|
|
int skip = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-21 22:44:29 +00:00
|
|
|
if( is_recursive )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
In recursive mode, we are only
|
|
|
|
interested in adding files -directories
|
|
|
|
will be added in the next pass.
|
|
|
|
*/
|
|
|
|
struct stat buf;
|
|
|
|
if( !wstat( long_name, &buf ) )
|
|
|
|
{
|
|
|
|
skip = S_ISDIR(buf.st_mode);
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2006-10-21 22:44:29 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2006-10-21 22:44:29 +00:00
|
|
|
if( skip )
|
|
|
|
{
|
|
|
|
free( long_name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-01-16 18:24:57 +00:00
|
|
|
completion_t data_to_push;
|
|
|
|
data_to_push.completion = long_name;
|
2012-01-16 16:56:47 +00:00
|
|
|
out.push_back( data_to_push );
|
2006-10-21 22:44:29 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-21 22:44:29 +00:00
|
|
|
|
|
|
|
if( wc_end || is_recursive )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
/*
|
2005-11-30 15:33:03 +00:00
|
|
|
Wilcard segment is not the last segment. Recursively call
|
|
|
|
wildcard_expand for all matching subdirectories.
|
2005-09-20 13:26:39 +00:00
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 16:52:02 +00:00
|
|
|
/*
|
|
|
|
wc_str is the part of the wildcarded string from the
|
|
|
|
beginning to the first slash
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *wc_str;
|
2005-11-29 16:52:02 +00:00
|
|
|
|
|
|
|
/*
|
2005-11-30 15:33:03 +00:00
|
|
|
new_dir is a scratch area containing the full path to a
|
|
|
|
file/directory we are iterating over
|
2005-11-29 16:52:02 +00:00
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
wchar_t *new_dir;
|
2005-11-29 16:52:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
The maximum length of a file element
|
|
|
|
*/
|
2006-03-29 00:14:50 +00:00
|
|
|
long ln=MAX_FILE_LENGTH;
|
2005-09-20 13:26:39 +00:00
|
|
|
char * narrow_dir_string = wcs2str( dir_string );
|
2006-10-21 22:44:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
In recursive mode, we look through the direcotry twice. If
|
|
|
|
so, this rewind is needed.
|
|
|
|
*/
|
|
|
|
rewinddir( dir );
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( narrow_dir_string )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
/*
|
2005-11-29 16:52:02 +00:00
|
|
|
Find out how long the filename can be in a worst case
|
|
|
|
scenario
|
|
|
|
*/
|
2011-12-27 03:18:46 +00:00
|
|
|
ln = pathconf( narrow_dir_string, _PC_NAME_MAX );
|
2005-11-29 16:52:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
If not specified, use som large number as fallback
|
|
|
|
*/
|
2005-09-20 13:26:39 +00:00
|
|
|
if( ln < 0 )
|
2011-12-27 03:18:46 +00:00
|
|
|
ln = MAX_FILE_LENGTH;
|
2005-09-20 13:26:39 +00:00
|
|
|
free( narrow_dir_string );
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
new_dir= (wchar_t *)malloc( sizeof(wchar_t)*(base_len+ln+2) );
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
wc_str = wc_end?wcsndup(wc, wc_end-wc):wcsdup(wc);
|
2005-11-29 16:52:02 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
if( (!new_dir) || (!wc_str) )
|
|
|
|
{
|
2006-07-03 10:39:57 +00:00
|
|
|
DIE_MEM();
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2005-11-23 15:35:03 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
wcscpy( new_dir, base_dir );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
wcstring next;
|
|
|
|
while (wreaddir(dir, next))
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
const wchar_t *name = next.c_str();
|
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/*
|
|
|
|
Test if the file/directory name matches the whole
|
|
|
|
wildcard element, i.e. regular matching.
|
|
|
|
*/
|
|
|
|
int whole_match = wildcard_match2( name, wc_str, 1 );
|
|
|
|
int partial_match = 0;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
/*
|
2005-11-29 14:33:52 +00:00
|
|
|
If we are doing recursive matching, also check if this
|
|
|
|
directory matches the part up to the recusrive
|
|
|
|
wildcard, if so, then we can search all subdirectories
|
|
|
|
for matches.
|
|
|
|
*/
|
|
|
|
if( is_recursive )
|
|
|
|
{
|
2012-01-05 21:58:48 +00:00
|
|
|
const wchar_t *end = wcschr( wc, ANY_STRING_RECURSIVE );
|
2012-01-19 16:53:25 +00:00
|
|
|
wchar_t *wc_sub = wcsndup( wc, end-wc+1);
|
2005-11-29 14:33:52 +00:00
|
|
|
partial_match = wildcard_match2( name, wc_sub, 1 );
|
|
|
|
free( wc_sub );
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-11-29 14:33:52 +00:00
|
|
|
|
|
|
|
if( whole_match || partial_match )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
|
|
|
int new_len;
|
2011-12-27 03:18:46 +00:00
|
|
|
struct stat buf;
|
2005-11-29 14:33:52 +00:00
|
|
|
char *dir_str;
|
2005-09-20 13:26:39 +00:00
|
|
|
int stat_res;
|
2007-01-09 16:47:05 +00:00
|
|
|
int new_res;
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
wcscpy(&new_dir[base_len], name );
|
|
|
|
dir_str = wcs2str( new_dir );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
if( dir_str )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2005-11-30 15:33:03 +00:00
|
|
|
stat_res = stat( dir_str, &buf );
|
2005-11-29 14:33:52 +00:00
|
|
|
free( dir_str );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
if( !stat_res )
|
2005-09-20 13:26:39 +00:00
|
|
|
{
|
2006-08-01 00:24:11 +00:00
|
|
|
if( S_ISDIR(buf.st_mode) )
|
2005-11-29 14:33:52 +00:00
|
|
|
{
|
|
|
|
new_len = wcslen( new_dir );
|
|
|
|
new_dir[new_len] = L'/';
|
|
|
|
new_dir[new_len+1] = L'\0';
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/*
|
|
|
|
Regular matching
|
|
|
|
*/
|
|
|
|
if( whole_match )
|
|
|
|
{
|
2012-01-15 06:48:53 +00:00
|
|
|
const wchar_t *new_wc = L"";
|
2006-02-21 15:47:38 +00:00
|
|
|
if( wc_end )
|
|
|
|
{
|
2012-01-15 06:48:53 +00:00
|
|
|
new_wc=wc_end+1;
|
2006-02-22 15:41:52 +00:00
|
|
|
/*
|
|
|
|
Accept multiple '/' as a single direcotry separator
|
|
|
|
*/
|
2006-02-21 15:47:38 +00:00
|
|
|
while(*new_wc==L'/')
|
2006-02-22 15:41:52 +00:00
|
|
|
{
|
2006-02-21 15:47:38 +00:00
|
|
|
new_wc++;
|
2006-02-22 15:41:52 +00:00
|
|
|
}
|
2006-02-21 15:47:38 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
new_res = wildcard_expand_internal( new_wc,
|
2011-12-27 03:18:46 +00:00
|
|
|
new_dir,
|
|
|
|
flags,
|
2007-03-24 19:07:38 +00:00
|
|
|
out );
|
2007-01-09 16:47:05 +00:00
|
|
|
|
|
|
|
if( new_res == -1 )
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2007-01-09 16:47:05 +00:00
|
|
|
res |= new_res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
/*
|
|
|
|
Recursive matching
|
|
|
|
*/
|
|
|
|
if( partial_match )
|
|
|
|
{
|
2011-12-27 03:18:46 +00:00
|
|
|
|
|
|
|
new_res = wildcard_expand_internal( wcschr( wc, ANY_STRING_RECURSIVE ),
|
2007-03-24 19:07:38 +00:00
|
|
|
new_dir,
|
2011-12-27 03:18:46 +00:00
|
|
|
flags | WILDCARD_RECURSIVE,
|
2007-03-24 19:07:38 +00:00
|
|
|
out );
|
|
|
|
|
2007-01-09 16:47:05 +00:00
|
|
|
if( new_res == -1 )
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
break;
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2007-01-09 16:47:05 +00:00
|
|
|
res |= new_res;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
2005-11-29 14:33:52 +00:00
|
|
|
}
|
2005-09-20 13:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
free( wc_str );
|
|
|
|
free( new_dir );
|
|
|
|
}
|
|
|
|
closedir( dir );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2005-11-29 14:33:52 +00:00
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
2006-02-22 15:41:52 +00:00
|
|
|
{
|
2005-11-29 14:33:52 +00:00
|
|
|
sb_destroy( &sb_desc );
|
2006-02-22 15:41:52 +00:00
|
|
|
}
|
|
|
|
|
2005-09-20 13:26:39 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
|
2011-12-27 03:18:46 +00:00
|
|
|
int wildcard_expand( const wchar_t *wc,
|
2007-03-24 19:07:38 +00:00
|
|
|
const wchar_t *base_dir,
|
|
|
|
int flags,
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> &out )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
int c = out.size();
|
2007-03-24 19:07:38 +00:00
|
|
|
int res = wildcard_expand_internal( wc, base_dir, flags, out );
|
|
|
|
int i;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
if( flags & ACCEPT_INCOMPLETE )
|
|
|
|
{
|
2007-04-16 21:06:05 +00:00
|
|
|
wchar_t *wc_base=L"";
|
2012-01-05 21:58:48 +00:00
|
|
|
wchar_t *wc_base_ptr = const_cast<wchar_t*>(wcsrchr( wc, L'/' ));
|
2007-04-16 21:06:05 +00:00
|
|
|
string_buffer_t sb;
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
|
|
|
|
if( wc_base_ptr )
|
|
|
|
{
|
|
|
|
wc_base = wcsndup( wc, (wc_base_ptr-wc)+1 );
|
2007-04-16 21:06:05 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:06:05 +00:00
|
|
|
sb_init( &sb );
|
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
for( i=c; i<out.size(); i++ )
|
2007-04-16 21:06:05 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
completion_t &c = out.at( i );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
if( c.flags & COMPLETE_NO_CASE )
|
2007-03-24 19:07:38 +00:00
|
|
|
{
|
2007-04-16 21:06:05 +00:00
|
|
|
sb_clear( &sb );
|
2012-01-20 16:50:31 +00:00
|
|
|
sb_printf( &sb, L"%ls%ls%ls", base_dir, wc_base, c.completion.c_str() );
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
c.completion = (wchar_t *)sb.buff;
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-04-16 21:06:05 +00:00
|
|
|
sb_destroy( &sb );
|
|
|
|
|
|
|
|
if( wc_base_ptr )
|
2007-04-20 19:34:30 +00:00
|
|
|
{
|
2007-04-16 21:06:05 +00:00
|
|
|
free( wc_base );
|
2007-04-20 19:34:30 +00:00
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2007-03-24 19:07:38 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, std::vector<completion_t> &outputs )
|
2011-12-27 03:18:46 +00:00
|
|
|
{
|
2012-01-16 16:56:47 +00:00
|
|
|
std::vector<completion_t> lst;
|
|
|
|
// al_init(&lst);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
int res = wildcard_expand(wc.c_str(), base_dir.c_str(), flags, lst);
|
2011-12-27 03:18:46 +00:00
|
|
|
|
2012-01-16 16:56:47 +00:00
|
|
|
int i, max = lst.size();
|
2011-12-27 03:18:46 +00:00
|
|
|
for (i=0; i < max; i++) {
|
2012-01-16 16:56:47 +00:00
|
|
|
outputs.push_back( lst.at(i));
|
2011-12-27 03:18:46 +00:00
|
|
|
}
|
2012-01-16 16:56:47 +00:00
|
|
|
// al_destroy(&lst);
|
2011-12-27 03:18:46 +00:00
|
|
|
return res;
|
|
|
|
}
|