diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 89455198..f33ba09c 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -2,6 +2,10 @@ * src/flash/intel.c: Add support for 28F256P33 flashes by Jonathan Stroud. + * src/cmd/cmd.h, src/cmd/cmd_cmd.c: Add new completion func for files. + * src/cmd/cmd_flashmem.c, src/cmd/cmd_include.c, src/cmd/cmd_readmem.c: + Use new completion helper rather than poking readline directly. + 2011-07-06 Jie Zhang * src/tap/chain.c (urj_tap_chain_connect): Correct checking of the diff --git a/urjtag/src/cmd/cmd.h b/urjtag/src/cmd/cmd.h index 7fed4523..719cde03 100644 --- a/urjtag/src/cmd/cmd.h +++ b/urjtag/src/cmd/cmd.h @@ -35,6 +35,7 @@ #define URJ_SRC_CMD_H #include /* qsort */ +#include #include #include @@ -147,6 +148,18 @@ void urj_completion_mayben_add_param_list (char ***matches, size_t *cnt, const char *text, size_t text_len, urj_param_list_t param_list); +/** + * This is just like urj_completion_mayben_add_match, but the matching is + * done against file names (and optionally searches the UrJTAG data dir). + * + * @param text the string to compare to match (e.g. user input) + * @param text_len the length of text + * @param search should relative paths search the UrJTAG data dir + */ +void urj_completion_mayben_add_file (char ***matches, size_t *cnt, + const char *text, size_t text_len, + bool search); + /** * Internal completion helper for matching against the signal list. * Since many functions involve signals as an option, unify the code diff --git a/urjtag/src/cmd/cmd_cmd.c b/urjtag/src/cmd/cmd_cmd.c index 943d6cf6..b2f237f4 100644 --- a/urjtag/src/cmd/cmd_cmd.c +++ b/urjtag/src/cmd/cmd_cmd.c @@ -28,10 +28,15 @@ #include #include +#ifdef HAVE_LIBREADLINE +#include +#endif + #include #include #include #include +#include #include "cmd.h" @@ -111,6 +116,49 @@ urj_completion_mayben_add_param_list (char ***matches, size_t *cnt, param_list.list[i].string); } +void urj_completion_mayben_add_file (char ***matches, size_t *cnt, + const char *text, size_t text_len, + bool search) +{ +#ifdef HAVE_LIBREADLINE + int state; + size_t implicit_len; + char *match, *search_text; + + /* Use the search path if path isn't explicitly relative/absolute */ + if (search && text[0] != '/' && text[0] != '.') + { + const char *jtag_data_dir = urj_get_data_dir (); + implicit_len = strlen (jtag_data_dir) + 1; + + search_text = malloc (implicit_len + text_len + 1); + if (!search_text) + return; + + sprintf (search_text, "%s/%s", jtag_data_dir, text); + text = search_text; + text_len += implicit_len; + } + else + { + implicit_len = 0; + search_text = NULL; + } + + state = 0; + while (1) + { + match = rl_filename_completion_function (text, state++); + if (!match) + break; + urj_completion_add_match_dupe (matches, cnt, match + implicit_len); + free (match); + } + + free (search_text); +#endif +} + void urj_completion_maybe_add_match (char ***matches, size_t *cnt, const char *text, const char *match) diff --git a/urjtag/src/cmd/cmd_flashmem.c b/urjtag/src/cmd/cmd_flashmem.c index 623e49e9..c22ae7bb 100644 --- a/urjtag/src/cmd/cmd_flashmem.c +++ b/urjtag/src/cmd/cmd_flashmem.c @@ -29,10 +29,6 @@ #include #include -#ifdef HAVE_LIBREADLINE -#include -#endif - #include #include #include @@ -123,23 +119,9 @@ cmd_flashmem_complete (urj_chain_t *chain, char ***matches, size_t *match_cnt, break; case 2: /* filename */ - { -#ifdef HAVE_LIBREADLINE - int state; - char *match; - - state = 0; - while (1) - { - match = rl_filename_completion_function (text, state++); - if (!match) - break; - urj_completion_add_match_dupe (matches, match_cnt, match); - free (match); - } -#endif + urj_completion_mayben_add_file (matches, match_cnt, text, + text_len, false); break; - } case 3: /* [noverify] */ urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "noverify"); diff --git a/urjtag/src/cmd/cmd_include.c b/urjtag/src/cmd/cmd_include.c index b17faaa6..21583abc 100644 --- a/urjtag/src/cmd/cmd_include.c +++ b/urjtag/src/cmd/cmd_include.c @@ -29,16 +29,10 @@ #include #include -#ifdef HAVE_LIBREADLINE -#include -#endif - #include #include -#include #include -#include #include "cmd.h" @@ -106,43 +100,7 @@ cmd_include_complete (urj_chain_t *chain, char ***matches, size_t *match_cnt, char * const *tokens, const char *text, size_t text_len, size_t token_point) { -#ifdef HAVE_LIBREADLINE - int state; - size_t implicit_len; - char *match, *search_text; - - /* Use the search path if path isn't explicitly relative/absolute */ - if (text[0] != '/' && text[0] != '.') - { - const char *jtag_data_dir = urj_get_data_dir (); - implicit_len = strlen (jtag_data_dir) + 1; - - search_text = malloc (implicit_len + text_len + 1); - if (!search_text) - return; - - sprintf (search_text, "%s/%s", jtag_data_dir, text); - text = search_text; - text_len += implicit_len; - } - else - { - implicit_len = 0; - search_text = NULL; - } - - state = 0; - while (1) - { - match = rl_filename_completion_function (text, state++); - if (!match) - break; - urj_completion_add_match_dupe (matches, match_cnt, match + implicit_len); - free (match); - } - - free (search_text); -#endif + urj_completion_mayben_add_file (matches, match_cnt, text, text_len, true); } const urj_cmd_t urj_cmd_include = { diff --git a/urjtag/src/cmd/cmd_readmem.c b/urjtag/src/cmd/cmd_readmem.c index 7b7601c2..a371f27f 100644 --- a/urjtag/src/cmd/cmd_readmem.c +++ b/urjtag/src/cmd/cmd_readmem.c @@ -29,10 +29,6 @@ #include #include -#ifdef HAVE_LIBREADLINE -#include -#endif - #include #include @@ -107,24 +103,10 @@ cmd_readmem_complete (urj_chain_t *chain, char ***matches, size_t *match_cnt, break; case 3: /* filename */ - { -#ifdef HAVE_LIBREADLINE - int state; - char *match; - - state = 0; - while (1) - { - match = rl_filename_completion_function (text, state++); - if (!match) - break; - urj_completion_add_match_dupe (matches, match_cnt, match); - free (match); - } -#endif + urj_completion_mayben_add_file (matches, match_cnt, text, + text_len, false); break; } - } } const urj_cmd_t urj_cmd_readmem = {