[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.21 and 1.29

version 1.21, 2002/03/18 16:20:36 version 1.29, 2003/03/29 22:48:38
Line 51  __RCSID("$NetBSD$");
Line 51  __RCSID("$NetBSD$");
 #include <stdlib.h>  #include <stdlib.h>
 #include <unistd.h>  #include <unistd.h>
 #include <limits.h>  #include <limits.h>
   #ifdef HAVE_ALLOCA_H
   #include <alloca.h>
   #endif
 #include "histedit.h"  #include "histedit.h"
 #include "readline/readline.h"  #include "readline/readline.h"
 #include "el.h"  #include "el.h"
Line 219  rl_initialize(void)
Line 222  rl_initialize(void)
   
         /* for proper prompt printing in readline() */          /* for proper prompt printing in readline() */
         el_rl_prompt = strdup("");          el_rl_prompt = strdup("");
           if (el_rl_prompt == NULL) {
                   history_end(h);
                   el_end(e);
                   return -1;
           }
         el_set(e, EL_PROMPT, _get_prompt);          el_set(e, EL_PROMPT, _get_prompt);
         el_set(e, EL_SIGNAL, 1);          el_set(e, EL_SIGNAL, 1);
   
Line 283  readline(const char *prompt)
Line 291  readline(const char *prompt)
         if (strcmp(el_rl_prompt, prompt) != 0) {          if (strcmp(el_rl_prompt, prompt) != 0) {
                 free(el_rl_prompt);                  free(el_rl_prompt);
                 el_rl_prompt = strdup(prompt);                  el_rl_prompt = strdup(prompt);
                   if (el_rl_prompt == NULL)
                           return NULL;
         }          }
         /* get one line from input stream */          /* get one line from input stream */
         ret = el_gets(e, &count);          ret = el_gets(e, &count);
Line 291  readline(const char *prompt)
Line 301  readline(const char *prompt)
                 int lastidx;                  int lastidx;
   
                 buf = strdup(ret);                  buf = strdup(ret);
                   if (buf == NULL)
                           return NULL;
                 lastidx = count - 1;                  lastidx = count - 1;
                 if (buf[lastidx] == '\n')                  if (buf[lastidx] == '\n')
                         buf[lastidx] = '\0';                          buf[lastidx] = '\0';
Line 321  using_history(void)
Line 333  using_history(void)
   
 /*  /*
  * substitute ``what'' with ``with'', returning resulting string; if   * substitute ``what'' with ``with'', returning resulting string; if
  * globally == 1, substitutes all occurences of what, otherwise only the   * globally == 1, substitutes all occurrences of what, otherwise only the
  * first one   * first one
  */   */
 static char *  static char *
Line 334  _rl_compat_sub(const char *str, const ch
Line 346  _rl_compat_sub(const char *str, const ch
         size_t size, i;          size_t size, i;
   
         result = malloc((size = 16));          result = malloc((size = 16));
           if (result == NULL)
                   return NULL;
         temp = str;          temp = str;
         with_len = strlen(with);          with_len = strlen(with);
         what_len = strlen(what);          what_len = strlen(what);
Line 344  _rl_compat_sub(const char *str, const ch
Line 358  _rl_compat_sub(const char *str, const ch
                         i = new - temp;                          i = new - temp;
                         add = i + with_len;                          add = i + with_len;
                         if (i + add + 1 >= size) {                          if (i + add + 1 >= size) {
                                   char *nresult;
                                 size += add + 1;                                  size += add + 1;
                                 result = realloc(result, size);                                  nresult = realloc(result, size);
                                   if (nresult == NULL) {
                                           free(result);
                                           return NULL;
                                   }
                                   result = nresult;
                         }                          }
                         (void) strncpy(&result[len], temp, i);                          (void) strncpy(&result[len], temp, i);
                         len += i;                          len += i;
Line 355  _rl_compat_sub(const char *str, const ch
Line 375  _rl_compat_sub(const char *str, const ch
                 } else {                  } else {
                         add = strlen(temp);                          add = strlen(temp);
                         if (len + add + 1 >= size) {                          if (len + add + 1 >= size) {
                                   char *nresult;
                                 size += add + 1;                                  size += add + 1;
                                 result = realloc(result, size);                                  nresult = realloc(result, size);
                                   if (nresult == NULL) {
                                           free(result);
                                           return NULL;
                                   }
                                   result = nresult;
                         }                          }
                         (void) strcpy(&result[len], temp);      /* safe */                          (void) strcpy(&result[len], temp);      /* safe */
                         len += add;                          len += add;
Line 499  _history_expand_command(const char *comm
Line 525  _history_expand_command(const char *comm
                 cmd++;                  cmd++;
   
         line = strdup(event_data);          line = strdup(event_data);
           if (line == NULL)
                   return 0;
         for (; *cmd; cmd++) {          for (; *cmd; cmd++) {
                 if (*cmd == ':')                  if (*cmd == ':')
                         continue;                          continue;
Line 525  _history_expand_command(const char *comm
Line 553  _history_expand_command(const char *comm
                                 delim = *(++cmd), cmd++;                                  delim = *(++cmd), cmd++;
                                 size = 16;                                  size = 16;
                                 what = realloc(from, size);                                  what = realloc(from, size);
                                   if (what == NULL) {
                                           free(from);
                                           return 0;
                                   }
                                 len = 0;                                  len = 0;
                                 for (; *cmd && *cmd != delim; cmd++) {                                  for (; *cmd && *cmd != delim; cmd++) {
                                         if (*cmd == '\\'                                          if (*cmd == '\\'
                                             && *(cmd + 1) == delim)                                              && *(cmd + 1) == delim)
                                                 cmd++;                                                  cmd++;
                                         if (len >= size)                                          if (len >= size) {
                                                 what = realloc(what,                                                  char *nwhat;
                                                   nwhat = realloc(what,
                                                     (size <<= 1));                                                      (size <<= 1));
                                                   if (nwhat == NULL) {
                                                           free(what);
                                                           return 0;
                                                   }
                                                   what = nwhat;
                                           }
                                         what[len++] = *cmd;                                          what[len++] = *cmd;
                                 }                                  }
                                 what[len] = '\0';                                  what[len] = '\0';
                                 from = what;                                  from = what;
                                 if (*what == '\0') {                                  if (*what == '\0') {
                                         free(what);                                          free(what);
                                         if (search)                                          if (search) {
                                                 from = strdup(search);                                                  from = strdup(search);
                                         else {                                                  if (from == NULL)
                                                           return 0;
                                           } else {
                                                 from = NULL;                                                  from = NULL;
                                                 return (-1);                                                  return (-1);
                                         }                                          }
Line 552  _history_expand_command(const char *comm
Line 593  _history_expand_command(const char *comm
   
                                 size = 16;                                  size = 16;
                                 with = realloc(to, size);                                  with = realloc(to, size);
                                   if (with == NULL) {
                                           free(to);
                                           return -1;
                                   }
                                 len = 0;                                  len = 0;
                                 from_len = strlen(from);                                  from_len = strlen(from);
                                 for (; *cmd && *cmd != delim; cmd++) {                                  for (; *cmd && *cmd != delim; cmd++) {
                                         if (len + from_len + 1 >= size) {                                          if (len + from_len + 1 >= size) {
                                                   char *nwith;
                                                 size += from_len + 1;                                                  size += from_len + 1;
                                                 with = realloc(with, size);                                                  nwith = realloc(with, size);
                                                   if (nwith == NULL) {
                                                           free(with);
                                                           return -1;
                                                   }
                                                   with = nwith;
                                         }                                          }
                                         if (*cmd == '&') {                                          if (*cmd == '&') {
                                                 /* safe */                                                  /* safe */
Line 576  _history_expand_command(const char *comm
Line 627  _history_expand_command(const char *comm
   
                                 tempcmd = _rl_compat_sub(line, from, to,                                  tempcmd = _rl_compat_sub(line, from, to,
                                     (g_on) ? 1 : 0);                                      (g_on) ? 1 : 0);
                                 free(line);                                  if (tempcmd) {
                                 line = tempcmd;                                          free(line);
                                           line = tempcmd;
                                   }
                                 g_on = 0;                                  g_on = 0;
                         }                          }
                 }                  }
Line 623  _history_expand_command(const char *comm
Line 676  _history_expand_command(const char *comm
         }          }
   
         cmdsize = 1, cmdlen = 0;          cmdsize = 1, cmdlen = 0;
         tempcmd = malloc(cmdsize);          if ((tempcmd = malloc(cmdsize)) == NULL)
                   return 0;
         for (i = start; start <= i && i <= end; i++) {          for (i = start; start <= i && i <= end; i++) {
                 int arr_len;                  int arr_len;
   
                 arr_len = strlen(arr[i]);                  arr_len = strlen(arr[i]);
                 if (cmdlen + arr_len + 1 >= cmdsize) {                  if (cmdlen + arr_len + 1 >= cmdsize) {
                           char *ntempcmd;
                         cmdsize += arr_len + 1;                          cmdsize += arr_len + 1;
                         tempcmd = realloc(tempcmd, cmdsize);                          ntempcmd = realloc(tempcmd, cmdsize);
                           if (ntempcmd == NULL) {
                                   free(tempcmd);
                                   return 0;
                           }
                           tempcmd = ntempcmd;
                 }                  }
                 (void) strcpy(&tempcmd[cmdlen], arr[i]);        /* safe */                  (void) strcpy(&tempcmd[cmdlen], arr[i]);        /* safe */
                 cmdlen += arr_len;                  cmdlen += arr_len;
Line 663  history_expand(char *str, char **output)
Line 723  history_expand(char *str, char **output)
                 rl_initialize();                  rl_initialize();
   
         *output = strdup(str);  /* do it early */          *output = strdup(str);  /* do it early */
           if (*output == NULL)
                   return 0;
   
         if (str[0] == history_subst_char) {          if (str[0] == history_subst_char) {
                 /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */                  /* ^foo^foo2^ is equivalent to !!:s^foo^foo2^ */
Line 675  history_expand(char *str, char **output)
Line 737  history_expand(char *str, char **output)
         }          }
 #define ADD_STRING(what, len)                                           \  #define ADD_STRING(what, len)                                           \
         {                                                               \          {                                                               \
                 if (idx + len + 1 > size)                               \                  if (idx + len + 1 > size) {                             \
                         result = realloc(result, (size += len + 1));    \                          char *nresult = realloc(result, (size += len + 1));\
                           if (nresult == NULL) {                          \
                                   free(*output);                          \
                                   return 0;                               \
                           }                                               \
                           result = nresult;                               \
                   }                                                       \
                 (void)strncpy(&result[idx], what, len);                 \                  (void)strncpy(&result[idx], what, len);                 \
                 idx += len;                                             \                  idx += len;                                             \
                 result[idx] = '\0';                                     \                  result[idx] = '\0';                                     \
Line 790  history_tokenize(const char *str)
Line 858  history_tokenize(const char *str)
                 }                  }
   
                 if (result_idx + 2 >= size) {                  if (result_idx + 2 >= size) {
                           char **nresult;
                         size <<= 1;                          size <<= 1;
                         result = realloc(result, size * sizeof(char *));                          nresult = realloc(result, size * sizeof(char *));
                           if (nresult == NULL) {
                                   free(result);
                                   return NULL;
                           }
                           result = nresult;
                 }                  }
                 len = i - start;                  len = i - start;
                 temp = malloc(len + 1);                  temp = malloc(len + 1);
                   if (temp == NULL) {
                           free(result);
                           return NULL;
                   }
                 (void) strncpy(temp, &str[start], len);                  (void) strncpy(temp, &str[start], len);
                 temp[len] = '\0';                  temp[len] = '\0';
                 result[result_idx++] = temp;                  result[result_idx++] = temp;
Line 1158  tilde_expand(char *txt)
Line 1236  tilde_expand(char *txt)
                 return (strdup(txt));                  return (strdup(txt));
   
         temp = strchr(txt + 1, '/');          temp = strchr(txt + 1, '/');
         if (temp == NULL)          if (temp == NULL) {
                 temp = strdup(txt + 1);                  temp = strdup(txt + 1);
         else {                  if (temp == NULL)
                           return NULL;
           } else {
                 len = temp - txt + 1;   /* text until string after slash */                  len = temp - txt + 1;   /* text until string after slash */
                 temp = malloc(len);                  temp = malloc(len);
                   if (temp == NULL)
                           return NULL;
                 (void) strncpy(temp, txt + 1, len - 2);                  (void) strncpy(temp, txt + 1, len - 2);
                 temp[len - 2] = '\0';                  temp[len - 2] = '\0';
         }          }
Line 1176  tilde_expand(char *txt)
Line 1258  tilde_expand(char *txt)
         txt += len;          txt += len;
   
         temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);          temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
           if (temp == NULL)
                   return NULL;
         (void) sprintf(temp, "%s/%s", pass->pw_dir, txt);          (void) sprintf(temp, "%s/%s", pass->pw_dir, txt);
   
         return (temp);          return (temp);
Line 1200  filename_completion_function(const char 
Line 1284  filename_completion_function(const char 
         size_t len;          size_t len;
   
         if (state == 0 || dir == NULL) {          if (state == 0 || dir == NULL) {
                 if (dir != NULL) {  
                         closedir(dir);  
                         dir = NULL;  
                 }  
                 temp = strrchr(text, '/');                  temp = strrchr(text, '/');
                 if (temp) {                  if (temp) {
                           char *nptr;
                         temp++;                          temp++;
                         filename = realloc(filename, strlen(temp) + 1);                          nptr = realloc(filename, strlen(temp) + 1);
                           if (nptr == NULL) {
                                   free(filename);
                                   return NULL;
                           }
                           filename = nptr;
                         (void) strcpy(filename, temp);                          (void) strcpy(filename, temp);
                         len = temp - text;      /* including last slash */                          len = temp - text;      /* including last slash */
                         dirname = realloc(dirname, len + 1);                          nptr = realloc(dirname, len + 1);
                           if (nptr == NULL) {
                                   free(filename);
                                   return NULL;
                           }
                           dirname = nptr;
                         (void) strncpy(dirname, text, len);                          (void) strncpy(dirname, text, len);
                         dirname[len] = '\0';                          dirname[len] = '\0';
                 } else {                  } else {
                         filename = strdup(text);                          filename = strdup(text);
                           if (filename == NULL)
                                   return NULL;
                         dirname = NULL;                          dirname = NULL;
                 }                  }
   
                 /* support for ``~user'' syntax */                  /* support for ``~user'' syntax */
                 if (dirname && *dirname == '~') {                  if (dirname && *dirname == '~') {
                           char *nptr;
                         temp = tilde_expand(dirname);                          temp = tilde_expand(dirname);
                         dirname = realloc(dirname, strlen(temp) + 1);                          if (temp == NULL)
                                   return NULL;
                           nptr = realloc(dirname, strlen(temp) + 1);
                           if (nptr == NULL) {
                                   free(dirname);
                                   return NULL;
                           }
                           dirname = nptr;
                         (void) strcpy(dirname, temp);   /* safe */                          (void) strcpy(dirname, temp);   /* safe */
                         free(temp);     /* no longer needed */                          free(temp);     /* no longer needed */
                 }                  }
Line 1230  filename_completion_function(const char 
Line 1331  filename_completion_function(const char 
                 if (filename_len == 0)                  if (filename_len == 0)
                         return (NULL);  /* no expansion possible */                          return (NULL);  /* no expansion possible */
   
                   if (dir != NULL) {
                           (void)closedir(dir);
                           dir = NULL;
                   }
                 dir = opendir(dirname ? dirname : ".");                  dir = opendir(dirname ? dirname : ".");
                 if (!dir)                  if (!dir)
                         return (NULL);  /* cannot open the directory */                          return (NULL);  /* cannot open the directory */
Line 1259  filename_completion_function(const char 
Line 1364  filename_completion_function(const char 
 #endif  #endif
                     ((dirname) ? strlen(dirname) : 0) + 1 + 1;                      ((dirname) ? strlen(dirname) : 0) + 1 + 1;
                 temp = malloc(len);                  temp = malloc(len);
                   if (temp == NULL)
                           return NULL;
                 (void) sprintf(temp, "%s%s",                  (void) sprintf(temp, "%s%s",
                     dirname ? dirname : "", entry->d_name);     /* safe */                      dirname ? dirname : "", entry->d_name);     /* safe */
   
                 /* test, if it's directory */                  /* test, if it's directory */
                 if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))                  if (stat(temp, &stbuf) == 0 && S_ISDIR(stbuf.st_mode))
                         strcat(temp, "/");      /* safe */                          strcat(temp, "/");      /* safe */
         } else          } else {
                   (void)closedir(dir);
                   dir = NULL;
                 temp = NULL;                  temp = NULL;
           }
   
         return (temp);          return (temp);
 }  }
Line 1331  completion_matches(const char *text, CPF
Line 1441  completion_matches(const char *text, CPF
         matches = 0;          matches = 0;
         match_list_len = 1;          match_list_len = 1;
         while ((retstr = (*genfunc) (text, matches)) != NULL) {          while ((retstr = (*genfunc) (text, matches)) != NULL) {
                 if (matches + 1 >= match_list_len) {                  /* allow for list terminator here */
                   if (matches + 2 >= match_list_len) {
                           char **nmatch_list;
                         match_list_len <<= 1;                          match_list_len <<= 1;
                         match_list = realloc(match_list,                          nmatch_list = realloc(match_list,
                             match_list_len * sizeof(char *));                              match_list_len * sizeof(char *));
                           if (nmatch_list == NULL) {
                                   free(match_list);
                                   return NULL;
                           }
                           match_list = nmatch_list;
   
                 }                  }
                 match_list[++matches] = retstr;                  match_list[++matches] = retstr;
         }          }
   
         if (!match_list)          if (!match_list)
                 return (char **) NULL;  /* nothing found */                  return NULL;    /* nothing found */
   
         /* find least denominator and insert it to match_list[0] */          /* find least denominator and insert it to match_list[0] */
         which = 2;          which = 2;
Line 1354  completion_matches(const char *text, CPF
Line 1472  completion_matches(const char *text, CPF
         }          }
   
         retstr = malloc(max_equal + 1);          retstr = malloc(max_equal + 1);
           if (retstr == NULL) {
                   free(match_list);
                   return NULL;
           }
         (void) strncpy(retstr, match_list[1], max_equal);          (void) strncpy(retstr, match_list[1], max_equal);
         retstr[max_equal] = '\0';          retstr[max_equal] = '\0';
         match_list[0] = retstr;          match_list[0] = retstr;
   
         /* add NULL as last pointer to the array */          /* add NULL as last pointer to the array */
         if (matches + 1 >= match_list_len)  
                 match_list = realloc(match_list,  
                     (match_list_len + 1) * sizeof(char *));  
         match_list[matches + 1] = (char *) NULL;          match_list[matches + 1] = (char *) NULL;
   
         return (match_list);          return (match_list);
Line 1374  static int
Line 1493  static int
 _rl_qsort_string_compare(i1, i2)  _rl_qsort_string_compare(i1, i2)
         const void *i1, *i2;          const void *i1, *i2;
 {  {
         /* LINTED const castaway */          const char *s1 = ((const char * const *)i1)[0];
         const char *s1 = ((const char **)i1)[0];          const char *s2 = ((const char * const *)i2)[0];
         /* LINTED const castaway */  
         const char *s2 = ((const char **)i2)[0];  
   
         return strcasecmp(s1, s2);          return strcasecmp(s1, s2);
 }  }

Legend:
Removed from v.1.21  
changed lines
  Added in v.1.29

CVSweb <webmaster@jp.NetBSD.org>