Ignore:
Timestamp:
25/02/2010 23:20:27 (2 years ago)
Author:
chris
Message:

First attempt at tab completion for readline/libedit in bbackupquery,
with commands and local file names, because it's easy and will help to
find compatibility problems.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/bin/bbackupquery/bbackupquery.cpp

    r2604 r2641  
    7575} 
    7676 
     77#ifdef HAVE_LIBREADLINE 
     78// copied from: http://tiswww.case.edu/php/chet/readline/readline.html#SEC44 
     79 
     80char * command_generator(const char *text, int state) 
     81{ 
     82        static int list_index, len; 
     83        const char *name; 
     84 
     85        /*  
     86         * If this is a new word to complete, initialize now.  This includes 
     87         * saving the length of TEXT for efficiency, and initializing the index 
     88         * variable to 0. 
     89         */ 
     90        if(!state) 
     91        { 
     92                list_index = 0; 
     93                len = strlen(text); 
     94        } 
     95 
     96        /* Return the next name which partially matches from the command list. */ 
     97        while((name = commands[list_index].name)) 
     98        { 
     99                list_index++; 
     100 
     101                if(::strncmp(name, text, len) == 0 && !(state--)) 
     102                { 
     103                        return ::strdup(name); 
     104                } 
     105        } 
     106 
     107        list_index = 0; 
     108 
     109        while((name = alias[list_index])) 
     110        { 
     111                list_index++; 
     112 
     113                if(::strncmp(name, text, len) == 0 && !(state--)) 
     114                { 
     115                        return ::strdup(name); 
     116                } 
     117        } 
     118 
     119        /* If no names matched, then return NULL. */ 
     120        return (char *) NULL; 
     121} 
     122 
     123char ** bbackupquery_completion(const char *text, int start, int end) 
     124{ 
     125        char **matches; 
     126 
     127        matches = (char **)NULL; 
     128 
     129        /* If this word is at the start of the line, then it is a command 
     130         * to complete.  Otherwise it is the name of a file in the current 
     131         * directory. 
     132         */ 
     133        if (start == 0) 
     134        { 
     135                matches = rl_completion_matches(text, command_generator); 
     136        } 
     137 
     138        return matches; 
     139} 
     140 
     141#endif // HAVE_LIBREADLINE 
     142 
    77143int main(int argc, const char *argv[]) 
    78144{ 
     
    370436        using_history(); 
    371437#endif 
     438        /* Allow conditional parsing of the ~/.inputrc file. */ 
     439        rl_readline_name = "bbackupquery"; 
     440 
     441        /* Tell the completer that we want a crack first. */ 
     442        rl_attempted_completion_function = bbackupquery_completion; 
     443 
    372444        char *last_cmd = 0; 
    373445        while(!context.Stop()) 
Note: See TracChangeset for help on using the changeset viewer.