From b4730561a1dc02727a2e7b4c04be234cbc5418af Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 30 Jun 2011 03:12:58 +0000 Subject: [PATCH] add new helpers for command completion that take arrays of strings git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1932 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 6 ++++++ urjtag/src/cmd/cmd.h | 22 ++++++++++++++++++++++ urjtag/src/cmd/cmd_bfin.c | 11 ++++++++--- urjtag/src/cmd/cmd_cmd.c | 12 ++++++++++++ urjtag/src/cmd/cmd_debug.c | 21 +++++++++++++-------- urjtag/src/cmd/cmd_print.c | 13 +++++++++---- 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index d65009ad..dc748c02 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -28,6 +28,12 @@ * src/cmd/cmd_endian.c (cmd_endian_complete): Add matches based on existing local array of strings. + * src/cmd/cmd.h (urj_completion_mayben_add_matches_num, + urj_completion_mayben_add_matches): Helpers for adding arrays of matches. + * src/cmd/cmd_cmd.c (urj_completion_mayben_add_matches_num): New helper. + * src/cmd/cmd_bfin.c, src/cmd/cmd_cmd.c, src/cmd/cmd_debug.c, + src/cmd/cmd_print.c: Convert to new array helpers. + 2011-06-27 Jie Zhang * include/urjtag/Makefile.am (nodist_pkginclude_HEADERS): New and diff --git a/urjtag/src/cmd/cmd.h b/urjtag/src/cmd/cmd.h index 4eb722ab..d41cf365 100644 --- a/urjtag/src/cmd/cmd.h +++ b/urjtag/src/cmd/cmd.h @@ -112,6 +112,28 @@ void urj_completion_mayben_add_match (char ***matches, size_t *cnt, const char *text, size_t text_len, const char *match); +/** + * This is just like urj_completion_mayben_add_match, except you pass in + * an array of strings rather than just one single one. + * + * @param text the string to compare to match (e.g. user input) + * @param text_len the length of text + * @param matchs the array of strings to possibly add to the set of matches + * @param num the number of elements in the @matchs array + */ +void urj_completion_mayben_add_matches_num (char ***matches, size_t *cnt, + const char *text, size_t text_len, + const char * const *matchs, + size_t num); + +/** + * This is just like urj_completion_mayben_add_matches_num, except the @num + * arg is automatically computed. + */ +#define urj_completion_mayben_add_matches(matches, cnt, text, text_len, matchs) \ + urj_completion_mayben_add_matches_num (matches, cnt, text, text_len, \ + matchs, ARRAY_SIZE (matchs)) + /** * 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_bfin.c b/urjtag/src/cmd/cmd_bfin.c index c33b00ec..d3db0076 100644 --- a/urjtag/src/cmd/cmd_bfin.c +++ b/urjtag/src/cmd/cmd_bfin.c @@ -515,12 +515,17 @@ cmd_bfin_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) { + static const char * const main_cmds[] = { + "execute", + "emulation", + "reset", + }; + if (token_point != 1) return; - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "execute"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "emulation"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "reset"); + urj_completion_mayben_add_matches (matches, match_cnt, text, text_len, + main_cmds); } const urj_cmd_t urj_cmd_bfin = { diff --git a/urjtag/src/cmd/cmd_cmd.c b/urjtag/src/cmd/cmd_cmd.c index 57fe06bd..f12d03db 100644 --- a/urjtag/src/cmd/cmd_cmd.c +++ b/urjtag/src/cmd/cmd_cmd.c @@ -87,6 +87,18 @@ urj_completion_mayben_add_match (char ***matches, size_t *cnt, const char *text, urj_completion_add_match_dupe (matches, cnt, match); } +void +urj_completion_mayben_add_matches_num (char ***matches, size_t *cnt, + const char *text, size_t text_len, + const char * const *matchs, size_t num) +{ + size_t n; + + for (n = 0; n < num; ++n) + urj_completion_mayben_add_match (matches, cnt, text, text_len, + matchs[n]); +} + void urj_completion_maybe_add_match (char ***matches, size_t *cnt, const char *text, const char *match) diff --git a/urjtag/src/cmd/cmd_debug.c b/urjtag/src/cmd/cmd_debug.c index 883165d1..3234b463 100644 --- a/urjtag/src/cmd/cmd_debug.c +++ b/urjtag/src/cmd/cmd_debug.c @@ -136,17 +136,22 @@ cmd_debug_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) { + static const char * const levels[] = { + "all", + "comm", + "debug", + "detail", + "normal", + "warning", + "error", + "silent", + }; + if (token_point != 1) return; - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "all"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "comm"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "debug"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "detail"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "normal"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "warning"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "error"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "silent"); + urj_completion_mayben_add_matches (matches, match_cnt, text, text_len, + levels); } const urj_cmd_t urj_cmd_debug = { diff --git a/urjtag/src/cmd/cmd_print.c b/urjtag/src/cmd/cmd_print.c index 986777cc..ec423c69 100644 --- a/urjtag/src/cmd/cmd_print.c +++ b/urjtag/src/cmd/cmd_print.c @@ -284,13 +284,18 @@ cmd_print_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) { + static const char * const status[] = { + "chain", + "bus", + "signals", + "instructions", + }; + if (token_point != 1) return; - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "chain"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "bus"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "signals"); - urj_completion_mayben_add_match (matches, match_cnt, text, text_len, "instructions"); + urj_completion_mayben_add_matches (matches, match_cnt, text, text_len, + status); } const urj_cmd_t urj_cmd_print = {