[BACK]Return to readline.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libedit

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/lib/libedit/readline.c between version 1.91 and 1.92

version 1.91, 2010/08/28 15:44:59 version 1.92, 2010/09/16 20:08:51
Line 121  VFunction *rl_prep_term_function = (VFun
Line 121  VFunction *rl_prep_term_function = (VFun
 VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;  VFunction *rl_deprep_term_function = (VFunction *)rl_deprep_terminal;
 KEYMAP_ENTRY_ARRAY emacs_meta_keymap;  KEYMAP_ENTRY_ARRAY emacs_meta_keymap;
   
 #ifdef WIDECHAR  
 static ct_buffer_t conv;  
 #endif  
   
 /*  /*
  * The current prompt string.   * The current prompt string.
  */   */
Line 157  int rl_completion_append_character = ' '
Line 153  int rl_completion_append_character = ' '
   
 /* stuff below is used internally by libedit for readline emulation */  /* stuff below is used internally by libedit for readline emulation */
   
 static TYPE(History) *h = NULL;  static History *h = NULL;
 static EditLine *e = NULL;  static EditLine *e = NULL;
 static Function *map[256];  static Function *map[256];
 static jmp_buf topbuf;  static jmp_buf topbuf;
Line 191  _get_prompt(EditLine *el __attribute__((
Line 187  _get_prompt(EditLine *el __attribute__((
 static HIST_ENTRY *  static HIST_ENTRY *
 _move_history(int op)  _move_history(int op)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         static HIST_ENTRY rl_he;          static HIST_ENTRY rl_he;
   
         if (FUNW(history)(h, &ev, op) != 0)          if (history(h, &ev, op) != 0)
                 return (HIST_ENTRY *) NULL;                  return (HIST_ENTRY *) NULL;
   
         rl_he.line = ct_encode_string(ev.str, &conv);          rl_he.line = ev.str;
         rl_he.data = NULL;          rl_he.data = NULL;
   
         return (&rl_he);          return (&rl_he);
Line 282  rl_set_prompt(const char *prompt)
Line 278  rl_set_prompt(const char *prompt)
 int  int
 rl_initialize(void)  rl_initialize(void)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int editmode = 1;          int editmode = 1;
         struct termios t;          struct termios t;
   
         if (e != NULL)          if (e != NULL)
                 el_end(e);                  el_end(e);
         if (h != NULL)          if (h != NULL)
                 FUN(history,end)(h);                  history_end(h);
   
         if (!rl_instream)          if (!rl_instream)
                 rl_instream = stdin;                  rl_instream = stdin;
Line 305  rl_initialize(void)
Line 301  rl_initialize(void)
         e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);          e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr);
   
         if (!editmode)          if (!editmode)
                 FUN(el,set)(e, EL_EDITMODE, 0);                  el_set(e, EL_EDITMODE, 0);
   
         h = FUN(history,init)();          h = history_init();
         if (!e || !h)          if (!e || !h)
                 return (-1);                  return (-1);
   
         FUNW(history)(h, &ev, H_SETSIZE, INT_MAX);      /* unlimited */          history(h, &ev, H_SETSIZE, INT_MAX);    /* unlimited */
         history_length = 0;          history_length = 0;
         max_input_history = INT_MAX;          max_input_history = INT_MAX;
         el_set(e, EL_HIST, history, h);          el_set(e, EL_HIST, history, h);
Line 325  rl_initialize(void)
Line 321  rl_initialize(void)
   
         /* for proper prompt printing in readline() */          /* for proper prompt printing in readline() */
         if (rl_set_prompt("") == -1) {          if (rl_set_prompt("") == -1) {
                 FUN(history,end)(h);                  history_end(h);
                 el_end(e);                  el_end(e);
                 return -1;                  return -1;
         }          }
Line 381  rl_initialize(void)
Line 377  rl_initialize(void)
 char *  char *
 readline(const char *p)  readline(const char *p)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         const char * volatile prompt = p;          const char * volatile prompt = p;
         int count;          int count;
         const char *ret;          const char *ret;
Line 429  readline(const char *p)
Line 425  readline(const char *p)
         } else          } else
                 buf = NULL;                  buf = NULL;
   
         FUNW(history)(h, &ev, H_GETSIZE);          history(h, &ev, H_GETSIZE);
         history_length = ev.num;          history_length = ev.num;
   
         return buf;          return buf;
Line 509  get_history_event(const char *cmd, int *
Line 505  get_history_event(const char *cmd, int *
         size_t len;          size_t len;
         char    *pat;          char    *pat;
         const char *rptr;          const char *rptr;
         TYPE(HistEvent) ev;          HistEvent ev;
   
         idx = *cindex;          idx = *cindex;
         if (cmd[idx++] != history_expansion_char)          if (cmd[idx++] != history_expansion_char)
Line 517  get_history_event(const char *cmd, int *
Line 513  get_history_event(const char *cmd, int *
   
         /* find out which event to take */          /* find out which event to take */
         if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {          if (cmd[idx] == history_expansion_char || cmd[idx] == '\0') {
                 if (FUNW(history)(h, &ev, H_FIRST) != 0)                  if (history(h, &ev, H_FIRST) != 0)
                         return(NULL);                          return(NULL);
                 *cindex = cmd[idx]? (idx + 1):idx;                  *cindex = cmd[idx]? (idx + 1):idx;
                 return ct_encode_string(ev.str, &conv);                  return ev.str;
         }          }
         sign = 0;          sign = 0;
         if (cmd[idx] == '-') {          if (cmd[idx] == '-') {
Line 575  get_history_event(const char *cmd, int *
Line 571  get_history_event(const char *cmd, int *
                 pat[len] = '\0';                  pat[len] = '\0';
         }          }
   
         if (FUNW(history)(h, &ev, H_CURR) != 0) {          if (history(h, &ev, H_CURR) != 0) {
                 if (pat != last_search_pat)                  if (pat != last_search_pat)
                         free(pat);                          free(pat);
                 return (NULL);                  return (NULL);
Line 594  get_history_event(const char *cmd, int *
Line 590  get_history_event(const char *cmd, int *
   
         if (ret == -1) {          if (ret == -1) {
                 /* restore to end of list on failed search */                  /* restore to end of list on failed search */
                 FUNW(history)(h, &ev, H_FIRST);                  history(h, &ev, H_FIRST);
                 (void)fprintf(rl_outstream, "%s: Event not found\n", pat);                  (void)fprintf(rl_outstream, "%s: Event not found\n", pat);
                 if (pat != last_search_pat)                  if (pat != last_search_pat)
                         free(pat);                          free(pat);
Line 610  get_history_event(const char *cmd, int *
Line 606  get_history_event(const char *cmd, int *
         if (pat != last_search_pat)          if (pat != last_search_pat)
                 free(pat);                  free(pat);
   
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return(NULL);                  return(NULL);
         *cindex = idx;          *cindex = idx;
         rptr = ct_encode_string(ev.str, &conv);          rptr = ev.str;
   
         /* roll back to original position */          /* roll back to original position */
         (void)FUNW(history)(h, &ev, H_SET, num);          (void)history(h, &ev, H_SET, num);
   
         return rptr;          return rptr;
 }  }
Line 1126  history_tokenize(const char *str)
Line 1122  history_tokenize(const char *str)
 void  void
 stifle_history(int max)  stifle_history(int max)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
   
         if (FUNW(history)(h, &ev, H_SETSIZE, max) == 0)          if (history(h, &ev, H_SETSIZE, max) == 0)
                 max_input_history = max;                  max_input_history = max;
 }  }
   
Line 1142  stifle_history(int max)
Line 1138  stifle_history(int max)
 int  int
 unstifle_history(void)  unstifle_history(void)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int omax;          int omax;
   
         FUNW(history)(h, &ev, H_SETSIZE, INT_MAX);          history(h, &ev, H_SETSIZE, INT_MAX);
         omax = max_input_history;          omax = max_input_history;
         max_input_history = INT_MAX;          max_input_history = INT_MAX;
         return (omax);          /* some value _must_ be returned */          return (omax);          /* some value _must_ be returned */
Line 1303  out1:
Line 1299  out1:
 int  int
 read_history(const char *filename)  read_history(const char *filename)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
         if (filename == NULL && (filename = _default_history_file()) == NULL)          if (filename == NULL && (filename = _default_history_file()) == NULL)
                 return errno;                  return errno;
         return (FUNW(history)(h, &ev, H_LOAD, filename) == -1 ?          return (history(h, &ev, H_LOAD, filename) == -1 ?
             (errno ? errno : EINVAL) : 0);              (errno ? errno : EINVAL) : 0);
 }  }
   
Line 1320  read_history(const char *filename)
Line 1316  read_history(const char *filename)
 int  int
 write_history(const char *filename)  write_history(const char *filename)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
         if (filename == NULL && (filename = _default_history_file()) == NULL)          if (filename == NULL && (filename = _default_history_file()) == NULL)
                 return errno;                  return errno;
         return (FUNW(history)(h, &ev, H_SAVE, filename) == -1 ?          return (history(h, &ev, H_SAVE, filename) == -1 ?
             (errno ? errno : EINVAL) : 0);              (errno ? errno : EINVAL) : 0);
 }  }
   
Line 1340  HIST_ENTRY *
Line 1336  HIST_ENTRY *
 history_get(int num)  history_get(int num)
 {  {
         static HIST_ENTRY she;          static HIST_ENTRY she;
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num;          int curr_num;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
   
         /* save current position */          /* save current position */
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return (NULL);                  return (NULL);
         curr_num = ev.num;          curr_num = ev.num;
   
         /* start from the oldest */          /* start from the oldest */
         if (FUNW(history)(h, &ev, H_LAST) != 0)          if (history(h, &ev, H_LAST) != 0)
                 return (NULL);  /* error */                  return (NULL);  /* error */
   
         /* look forwards for event matching specified offset */          /* look forwards for event matching specified offset */
         if (FUNW(history)(h, &ev, H_NEXT_EVDATA, num, &she.data))          if (history(h, &ev, H_NEXT_EVDATA, num, &she.data))
                 return (NULL);                  return (NULL);
   
         she.line = ct_encode_string(ev.str, &conv);          she.line = ev.str;
   
         /* restore pointer to where it was */          /* restore pointer to where it was */
         (void)FUNW(history)(h, &ev, H_SET, curr_num);          (void)history(h, &ev, H_SET, curr_num);
   
         return (&she);          return (&she);
 }  }
Line 1374  history_get(int num)
Line 1370  history_get(int num)
 int  int
 add_history(const char *line)  add_history(const char *line)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         const Char *wline;  
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
   
         wline = ct_decode_string(line, &conv);          (void)history(h, &ev, H_ENTER, line);
           if (history(h, &ev, H_GETSIZE) == 0)
         (void)FUNW(history)(h, &ev, H_ENTER, wline);  
         if (FUNW(history)(h, &ev, H_GETSIZE) == 0)  
                 history_length = ev.num;                  history_length = ev.num;
   
         return (!(history_length > 0)); /* return 0 if all is okay */          return (!(history_length > 0)); /* return 0 if all is okay */
Line 1397  HIST_ENTRY *
Line 1390  HIST_ENTRY *
 remove_history(int num)  remove_history(int num)
 {  {
         HIST_ENTRY *he;          HIST_ENTRY *he;
         TYPE(HistEvent) ev;          HistEvent ev;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
Line 1405  remove_history(int num)
Line 1398  remove_history(int num)
         if ((he = malloc(sizeof(*he))) == NULL)          if ((he = malloc(sizeof(*he))) == NULL)
                 return NULL;                  return NULL;
   
         if (FUNW(history)(h, &ev, H_DELDATA, num, &he->data) != 0) {          if (history(h, &ev, H_DELDATA, num, &he->data) != 0) {
                 free(he);                  free(he);
                 return NULL;                  return NULL;
         }          }
   
         he->line = ct_encode_string(ev.str, &conv);          he->line = ev.str;
         if (FUNW(history)(h, &ev, H_GETSIZE) == 0)          if (history(h, &ev, H_GETSIZE) == 0)
                 history_length = ev.num;                  history_length = ev.num;
   
         return he;          return he;
Line 1425  HIST_ENTRY *
Line 1418  HIST_ENTRY *
 replace_history_entry(int num, const char *line, histdata_t data)  replace_history_entry(int num, const char *line, histdata_t data)
 {  {
         HIST_ENTRY *he;          HIST_ENTRY *he;
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num;          int curr_num;
   
         if (h == NULL || e == NULL)          if (h == NULL || e == NULL)
                 rl_initialize();                  rl_initialize();
   
         /* save current position */          /* save current position */
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return NULL;                  return NULL;
         curr_num = ev.num;          curr_num = ev.num;
   
         /* start from the oldest */          /* start from the oldest */
         if (FUNW(history)(h, &ev, H_LAST) != 0)          if (history(h, &ev, H_LAST) != 0)
                 return NULL;    /* error */                  return NULL;    /* error */
   
         if ((he = malloc(sizeof(*he))) == NULL)          if ((he = malloc(sizeof(*he))) == NULL)
                 return NULL;                  return NULL;
   
         /* look forwards for event matching specified offset */          /* look forwards for event matching specified offset */
         if (FUNW(history)(h, &ev, H_NEXT_EVDATA, num, &he->data))          if (history(h, &ev, H_NEXT_EVDATA, num, &he->data))
                 goto out;                  goto out;
   
         he->line = strdup(ct_encode_string(ev.str, &e->el_scratch));          he->line = strdup(ev.str);
         if (he->line == NULL)          if (he->line == NULL)
                 goto out;                  goto out;
   
         if (FUNW(history)(h, &ev, H_REPLACE, line, data))          if (history(h, &ev, H_REPLACE, line, data))
                 goto out;                  goto out;
   
         /* restore pointer to where it was */          /* restore pointer to where it was */
         if (FUNW(history)(h, &ev, H_SET, curr_num))          if (history(h, &ev, H_SET, curr_num))
                 goto out;                  goto out;
   
         return he;          return he;
Line 1470  out:
Line 1463  out:
 void  void
 clear_history(void)  clear_history(void)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
   
         (void)FUNW(history)(h, &ev, H_CLEAR);          (void)history(h, &ev, H_CLEAR);
         history_length = 0;          history_length = 0;
 }  }
   
Line 1483  clear_history(void)
Line 1476  clear_history(void)
 int  int
 where_history(void)  where_history(void)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num, off;          int curr_num, off;
   
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return (0);                  return (0);
         curr_num = ev.num;          curr_num = ev.num;
   
         (void)FUNW(history)(h, &ev, H_FIRST);          (void)history(h, &ev, H_FIRST);
         off = 1;          off = 1;
         while (ev.num != curr_num && FUNW(history)(h, &ev, H_NEXT) == 0)          while (ev.num != curr_num && history(h, &ev, H_NEXT) == 0)
                 off++;                  off++;
   
         return (off);          return (off);
Line 1516  current_history(void)
Line 1509  current_history(void)
 int  int
 history_total_bytes(void)  history_total_bytes(void)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num;          int curr_num;
         size_t size;          size_t size;
   
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return (-1);                  return (-1);
         curr_num = ev.num;          curr_num = ev.num;
   
         (void)FUNW(history)(h, &ev, H_FIRST);          (void)history(h, &ev, H_FIRST);
         size = 0;          size = 0;
         do          do
                 size += Strlen(ev.str) * sizeof(*ev.str);                  size += strlen(ev.str) * sizeof(*ev.str);
         while (FUNW(history)(h, &ev, H_NEXT) == 0);          while (history(h, &ev, H_NEXT) == 0);
   
         /* get to the same position as before */          /* get to the same position as before */
         FUNW(history)(h, &ev, H_PREV_EVENT, curr_num);          history(h, &ev, H_PREV_EVENT, curr_num);
   
         return (int)(size);          return (int)(size);
 }  }
Line 1543  history_total_bytes(void)
Line 1536  history_total_bytes(void)
 int  int
 history_set_pos(int pos)  history_set_pos(int pos)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num;          int curr_num;
   
         if (pos >= history_length || pos < 0)          if (pos >= history_length || pos < 0)
                 return (-1);                  return (-1);
   
         (void)FUNW(history)(h, &ev, H_CURR);          (void)history(h, &ev, H_CURR);
         curr_num = ev.num;          curr_num = ev.num;
   
         /*          /*
          * use H_DELDATA to set to nth history (without delete) by passing           * use H_DELDATA to set to nth history (without delete) by passing
          * (void **)-1           * (void **)-1
          */           */
         if (FUNW(history)(h, &ev, H_DELDATA, pos, (void **)-1)) {          if (history(h, &ev, H_DELDATA, pos, (void **)-1)) {
                 (void)FUNW(history)(h, &ev, H_SET, curr_num);                  (void)history(h, &ev, H_SET, curr_num);
                 return(-1);                  return(-1);
         }          }
         return (0);          return (0);
Line 1592  next_history(void)
Line 1585  next_history(void)
 int  int
 history_search(const char *str, int direction)  history_search(const char *str, int direction)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         const Char *strp;          const char *strp;
         const Char *wstr;  
         int curr_num;          int curr_num;
   
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return (-1);                  return (-1);
         curr_num = ev.num;          curr_num = ev.num;
   
         wstr = ct_decode_string(str, &conv);  
         for (;;) {          for (;;) {
                 if ((strp = Strstr(ev.str, wstr)) != NULL)                  if ((strp = strstr(ev.str, str)) != NULL)
                         return (int) (strp - ev.str);                          return (int) (strp - ev.str);
                 if (FUNW(history)(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)                  if (history(h, &ev, direction < 0 ? H_NEXT:H_PREV) != 0)
                         break;                          break;
         }          }
         (void)FUNW(history)(h, &ev, H_SET, curr_num);          (void)history(h, &ev, H_SET, curr_num);
         return (-1);          return (-1);
 }  }
   
Line 1619  history_search(const char *str, int dire
Line 1610  history_search(const char *str, int dire
 int  int
 history_search_prefix(const char *str, int direction)  history_search_prefix(const char *str, int direction)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
   
         return (FUNW(history)(h, &ev, direction < 0 ?          return (history(h, &ev, direction < 0 ?
             H_PREV_STR : H_NEXT_STR, str));              H_PREV_STR : H_NEXT_STR, str));
 }  }
   
Line 1635  int
Line 1626  int
 history_search_pos(const char *str,  history_search_pos(const char *str,
                    int direction __attribute__((__unused__)), int pos)                     int direction __attribute__((__unused__)), int pos)
 {  {
         TYPE(HistEvent) ev;          HistEvent ev;
         int curr_num, off;          int curr_num, off;
         const Char *wstr;  
   
         off = (pos > 0) ? pos : -pos;          off = (pos > 0) ? pos : -pos;
         pos = (pos > 0) ? 1 : -1;          pos = (pos > 0) ? 1 : -1;
   
         if (FUNW(history)(h, &ev, H_CURR) != 0)          if (history(h, &ev, H_CURR) != 0)
                 return (-1);                  return (-1);
         curr_num = ev.num;          curr_num = ev.num;
   
         if (history_set_pos(off) != 0 || FUNW(history)(h, &ev, H_CURR) != 0)          if (history_set_pos(off) != 0 || history(h, &ev, H_CURR) != 0)
                 return (-1);                  return (-1);
   
         wstr = ct_decode_string(str, &conv);  
         for (;;) {          for (;;) {
                 if (Strstr(ev.str, wstr))                  if (strstr(ev.str, str))
                         return (off);                          return (off);
                 if (FUNW(history)(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)                  if (history(h, &ev, (pos < 0) ? H_PREV : H_NEXT) != 0)
                         break;                          break;
         }          }
   
         /* set "current" pointer back to previous state */          /* set "current" pointer back to previous state */
         (void)FUNW(history)(h, &ev,          (void)history(h, &ev,
             pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);              pos < 0 ? H_NEXT_EVENT : H_PREV_EVENT, curr_num);
   
         return (-1);          return (-1);
Line 1781  rl_complete(int ignore __attribute__((__
Line 1770  rl_complete(int ignore __attribute__((__
             (size_t)rl_completion_query_items,              (size_t)rl_completion_query_items,
             &rl_completion_type, &rl_attempted_completion_over,              &rl_completion_type, &rl_attempted_completion_over,
             &rl_point, &rl_end);              &rl_point, &rl_end);
   
   
 }  }
   
   

Legend:
Removed from v.1.91  
changed lines
  Added in v.1.92

CVSweb <webmaster@jp.NetBSD.org>