unify readline filename completion code into one helper

git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1961 b68d4a1b-bc3d-0410-92ed-d4ac073336b7
master
Mike Frysinger 14 years ago
parent f85dd5676f
commit c5cb775947

@ -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 <jie.zhang@analog.com>
* src/tap/chain.c (urj_tap_chain_connect): Correct checking of the

@ -35,6 +35,7 @@
#define URJ_SRC_CMD_H
#include <stdlib.h> /* qsort */
#include <stdbool.h>
#include <sysdep.h>
#include <urjtag/params.h>
@ -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

@ -28,10 +28,15 @@
#include <string.h>
#include <ctype.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
#include <urjtag/error.h>
#include <urjtag/chain.h>
#include <urjtag/parse.h>
#include <urjtag/cmd.h>
#include <urjtag/jtag.h>
#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)

@ -29,10 +29,6 @@
#include <string.h>
#include <errno.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
#include <urjtag/error.h>
#include <urjtag/bus.h>
#include <urjtag/flash.h>
@ -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");

@ -29,16 +29,10 @@
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
#include <urjtag/error.h>
#include <urjtag/parse.h>
#include <urjtag/jtag.h>
#include <urjtag/cmd.h>
#include <urjtag/bsdl.h>
#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 = {

@ -29,10 +29,6 @@
#include <string.h>
#include <errno.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
#include <urjtag/error.h>
#include <urjtag/bus.h>
@ -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 = {

Loading…
Cancel
Save