Changeset 2641


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.

Location:
box/trunk/bin/bbackupquery
Files:
3 edited

Legend:

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

    r2636 r2641  
    6464#define COMMAND_RETURN_ERROR            4 
    6565 
     66// Data about commands 
     67QueryCommandSpecification commands[] =  
     68{ 
     69        { "quit", "" }, 
     70        { "exit", "" }, 
     71        { "list", "rodIFtTash", }, 
     72        { "pwd",  "" }, 
     73        { "cd",   "od" }, 
     74        { "lcd",  "" }, 
     75        { "sh",   "" }, 
     76        { "getobject", "" }, 
     77        { "get",  "i" }, 
     78        { "compare", "alcqAEQ" }, 
     79        { "restore", "drif" }, 
     80        { "help", "" }, 
     81        { "usage", "m" }, 
     82        { "undelete", "" }, 
     83        { "delete", "" }, 
     84        { NULL, NULL }  
     85}; 
     86 
     87const char *alias[] = {"ls", 0}; 
     88const int aliasIs[] = {Command_List, 0}; 
     89 
    6690// -------------------------------------------------------------------------- 
    6791// 
     
    100124{ 
    101125} 
    102  
    103 typedef struct 
    104 { 
    105         const char* name; 
    106         const char* opts; 
    107 } QueryCommandSpecification; 
    108126 
    109127// -------------------------------------------------------------------------- 
     
    207225                return; 
    208226        } 
    209          
    210         // Data about commands 
    211         static QueryCommandSpecification commands[] =  
    212         { 
    213                 { "quit", "" }, 
    214                 { "exit", "" }, 
    215                 { "list", "rodIFtTash", }, 
    216                 { "pwd",  "" }, 
    217                 { "cd",   "od" }, 
    218                 { "lcd",  "" }, 
    219                 { "sh",   "" }, 
    220                 { "getobject", "" }, 
    221                 { "get",  "i" }, 
    222                 { "compare", "alcqAEQ" }, 
    223                 { "restore", "drif" }, 
    224                 { "help", "" }, 
    225                 { "usage", "m" }, 
    226                 { "undelete", "" }, 
    227                 { "delete", "" }, 
    228                 { NULL, NULL }  
    229         }; 
    230  
    231         typedef enum 
    232         { 
    233                 Command_Quit = 0, 
    234                 Command_Exit, 
    235                 Command_List, 
    236                 Command_pwd, 
    237                 Command_cd, 
    238                 Command_lcd, 
    239                 Command_sh, 
    240                 Command_GetObject, 
    241                 Command_Get, 
    242                 Command_Compare, 
    243                 Command_Restore, 
    244                 Command_Help, 
    245                 Command_Usage, 
    246                 Command_Undelete, 
    247                 Command_Delete, 
    248         } 
    249         CommandType; 
    250  
    251         static const char *alias[] = {"ls", 0}; 
    252         static const int aliasIs[] = {Command_List, 0}; 
    253          
     227 
    254228        // Work out which command it is... 
    255229        int cmd = 0; 
  • box/trunk/bin/bbackupquery/BackupQueries.h

    r2430 r2641  
    2020class Configuration; 
    2121class ExcludeList; 
     22 
     23typedef struct 
     24{ 
     25        const char* name; 
     26        const char* opts; 
     27} 
     28QueryCommandSpecification; 
     29 
     30// Data about commands 
     31extern QueryCommandSpecification commands[]; 
     32 
     33typedef enum 
     34{ 
     35        Command_Quit = 0, 
     36        Command_Exit, 
     37        Command_List, 
     38        Command_pwd, 
     39        Command_cd, 
     40        Command_lcd, 
     41        Command_sh, 
     42        Command_GetObject, 
     43        Command_Get, 
     44        Command_Compare, 
     45        Command_Restore, 
     46        Command_Help, 
     47        Command_Usage, 
     48        Command_Undelete, 
     49        Command_Delete, 
     50} 
     51CommandType; 
     52 
     53extern const char *alias[]; 
     54extern const int aliasIs[]; 
    2255 
    2356// -------------------------------------------------------------------------- 
  • 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.