Commit f1ed108b authored by Jan Moskyto Matejka's avatar Jan Moskyto Matejka
Browse files

Bash: Complete also in the middle of the line. (And some cleanup, too.)

parent 5b8eb44c
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -26,8 +26,6 @@ function _birdc_complete {
  NOW=$2
  PREV=$3

  echo "bagr" >>xxb

  case $PREV in
    -*([lvr])s)
      COMPREPLY=( $(compgen -W "$(find -maxdepth 1 -type s)" -- $NOW) )
@@ -46,7 +44,7 @@ function _birdc_complete {
      ;;
  esac

  COMPREPLY=( $($CMD -C "$NOW" "$COMP_TYPE" "$COMP_CWORD" "$COMP_POINT" "${COMP_WORDS[@]}") )
  COMPREPLY=( $($CMD -C "$NOW" "$COMP_TYPE" "$COMP_CWORD" "${COMP_WORDS[@]}") )
}

complete -F _bird_complete bird
+10 −3
Original line number Diff line number Diff line
@@ -72,8 +72,8 @@ parse_args(int argc, char **argv)

  if ((argc > 1) && !strcmp(argv[1], "-C")) {
    complete_init(argc-2, argv+2);
    argv += 6;
    argc -= 6;
    argv += COMPLETE_ARGC + 2;
    argc -= COMPLETE_ARGC + 2;
    complete = 1;
  }

@@ -113,10 +113,17 @@ parse_args(int argc, char **argv)
      tmp = init_cmd = malloc(len);
      for (i = optind; i < argc; i++)
	{
	  if (complete && (argv[i] == comp_last))
	    break;
	  strcpy(tmp, argv[i]);
	  tmp += strlen(tmp);
	  *tmp++ = ' ';
	}

      if (complete) {
	strcpy(tmp, comp_now);
	tmp += strlen(comp_now);
      } else
	tmp[-1] = 0;

      once = 1;
+2 −0
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@ char *cmd_expand(char *cmd);

void complete_init(int argc, char **argv);
int do_complete(char *cmd);
#define COMPLETE_ARGC	3
extern const char *comp_now, *comp_last;

/* die() with system error messages */
#define DIE(x, y...) die(x ": %s", ##y, strerror(errno))
+8 −7
Original line number Diff line number Diff line
@@ -14,8 +14,8 @@
#include "nest/bird.h"
#include "client/client.h"

static const char *c_now;
static int comp_type, comp_cword, comp_point;
static int comp_type, comp_cword;
const char *comp_now, *comp_last;

void complete_init(int argc, char **argv) {
  /* In argv, there are:
@@ -26,9 +26,9 @@ void complete_init(int argc, char **argv) {
   * ${COMP_WORDS[@]}
   */

  c_now = argv[0];
  comp_now = argv[0];

  if (argc < 4)
  if (argc < COMPLETE_ARGC)
    die("Not enough args.");

  if (sscanf(argv[1], "%d", &comp_type) != 1)
@@ -37,9 +37,10 @@ void complete_init(int argc, char **argv) {
  if (sscanf(argv[2], "%d", &comp_cword) != 1)
    die("Strange COMP_CWORD=\"%s\".", argv[2]);

  if (sscanf(argv[3], "%d", &comp_point) != 1)
    die("Strange COMP_POINT=\"%s\".", argv[3]);
  if (comp_cword + COMPLETE_ARGC >= argc)
    die("COMP_CWORD=%d points after end of arg list.", comp_cword);

  comp_last = argv[COMPLETE_ARGC + comp_cword];
  return;
}

@@ -50,7 +51,7 @@ int do_complete(char *cmd) {
  char buf[256];
  int res = cmd_complete(cmd, strlen(cmd), buf, (comp_type == 63));
  if (res == 1)
    printf("%s%s\n", c_now, buf);
    printf("%s%s\n", comp_now, buf);

    
  return 0;