Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libedit/readline.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libedit/readline.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.3 retrieving revision 1.10 diff -u -p -r1.3 -r1.10 --- src/lib/libedit/readline.c 1997/11/12 21:56:05 1.3 +++ src/lib/libedit/readline.c 2000/03/10 13:06:43 1.10 @@ -1,4 +1,4 @@ -/* $NetBSD: readline.c,v 1.3 1997/11/12 21:56:05 thorpej Exp $ */ +/* $NetBSD: readline.c,v 1.10 2000/03/10 13:06:43 jdolecek Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -38,7 +38,7 @@ #include #if !defined(lint) && !defined(SCCSID) -__RCSID("$NetBSD: readline.c,v 1.3 1997/11/12 21:56:05 thorpej Exp $"); +__RCSID("$NetBSD: readline.c,v 1.10 2000/03/10 13:06:43 jdolecek Exp $"); #endif /* not lint && not SCCSID */ #include @@ -97,15 +97,17 @@ static unsigned char _el_rl_complete __P static char *_get_prompt __P((EditLine *)); static HIST_ENTRY *_move_history __P((int)); static int _history_search_gen __P((const char *, int, int)); -static int _history_expand_command __P((const char *, int, char **)); +static int _history_expand_command __P((const char *, size_t, char **)); static char *_rl_compat_sub __P((const char *, const char *, const char *, int)); static int rl_complete_internal __P((int)); /* - * needed for easy prompt switching + * needed for prompt switching in readline() */ static char *el_rl_prompt = NULL; + +/* ARGSUSED */ static char * _get_prompt(el) EditLine *el; @@ -133,8 +135,8 @@ _move_history(op) } -/* - * READLINE compatibility stuff +/* + * READLINE compatibility stuff */ /* @@ -155,13 +157,13 @@ rl_initialize() rl_instream = stdin; if (!rl_outstream) rl_outstream = stdout; - e = el_init(rl_readline_name, rl_instream, rl_outstream); + e = el_init(rl_readline_name, rl_instream, rl_outstream, stderr); h = history_init(); if (!e || !h) return -1; - history(h, &ev, H_SETMAXSIZE, INT_MAX); /* unlimited */ + history(h, &ev, H_SETSIZE, INT_MAX); /* unlimited */ history_length = 0; max_input_history = INT_MAX; el_set(e, EL_HIST, history, h); @@ -187,6 +189,7 @@ rl_initialize() /* some readline apps do use this */ li = el_line(e); + /* LINTED const cast */ rl_line_buffer = (char *) li->buffer; rl_point = rl_end = 0; @@ -207,17 +210,19 @@ readline(const char *prompt) if (e == NULL || h == NULL) rl_initialize(); - /* set the prompt */ + /* update prompt accordingly to what has been passed */ + if (!prompt) prompt = ""; if (strcmp(el_rl_prompt, prompt) != 0) { free(el_rl_prompt); el_rl_prompt = strdup(prompt); } + /* get one line from input stream */ ret = el_gets(e, &count); if (ret && count > 0) { char *foo; - off_t lastidx; + int lastidx; foo = strdup(ret); lastidx = count - 1; @@ -231,6 +236,7 @@ readline(const char *prompt) history(h, &ev, H_GETSIZE); history_length = ev.num; + /* LINTED const cast */ return (char *) ret; } @@ -261,7 +267,8 @@ _rl_compat_sub(str, what, with, globally { char *result; const char *temp, *new; - int size, len, i, with_len, what_len, add; + int len, with_len, what_len, add; + size_t size, i; result = malloc((size = 16)); temp = str; @@ -292,7 +299,7 @@ _rl_compat_sub(str, what, with, globally len += add; temp = NULL; } - } while (temp); + } while (temp && globally); result[len] = '\0'; return result; @@ -310,24 +317,25 @@ _rl_compat_sub(str, what, with, globally * it's callers responsibility to free() string returned in *result */ static int -_history_expand_command(command, len, result) +_history_expand_command(command, cmdlen, result) const char *command; - int len; + size_t cmdlen; char **result; { - char **arr, *temp, *line, *search = NULL, *cmd; + char **arr, *tempcmd, *line, *search = NULL, *cmd; const char *event_data = NULL; - static const char *from = NULL, *to = NULL; - int start = -1, end = -1, max, i, size, idx; + static char *from = NULL, *to = NULL; + int start = -1, end = -1, max, i, idx; int h_on = 0, t_on = 0, r_on = 0, e_on = 0, p_on = 0, g_on = 0; int event_num = 0, retval; + size_t cmdsize; *result = NULL; - cmd = alloca(len + 1); - (void)strncpy(cmd, command, len); - cmd[len] = 0; + cmd = alloca(cmdlen + 1); + (void)strncpy(cmd, command, cmdlen); + cmd[cmdlen] = 0; idx = 1; /* find out which event to take */ @@ -335,7 +343,8 @@ _history_expand_command(command, len, re event_num = history_length; idx++; } else { - int off, len, num; + int off, num; + size_t len; off = idx; while (cmd[off] && !strchr(":^$*-%", cmd[off])) off++; @@ -405,21 +414,21 @@ _history_expand_command(command, len, re start = end = -1, cmd++; else if (*cmd == '*') start = 1, end = -1, cmd++; - else if (isdigit(*cmd)) { + else if (isdigit((unsigned char) *cmd)) { const char *temp; int shifted = 0; start = atoi(cmd); temp = cmd; - for (; isdigit(*cmd); cmd++); + for (; isdigit((unsigned char) *cmd); cmd++); if (temp != cmd) shifted = 1; if (shifted && *cmd == '-') { - if (!isdigit(*(cmd + 1))) + if (!isdigit((unsigned char) *(cmd + 1))) end = -2; else { end = atoi(cmd + 1); - for (; isdigit(*cmd); cmd++); + for (; isdigit((unsigned char) *cmd); cmd++); } } else if (shifted && *cmd == '*') end = -1, cmd++; @@ -446,15 +455,16 @@ _history_expand_command(command, len, re else if (*cmd == 'g') g_on = 2; else if (*cmd == 's' || *cmd == '&') { - char *what, *with, delim; - int len, size, from_len; + char *what, *with, delim; + int len, from_len; + size_t size; if (*cmd == '&' && (from == NULL || to == NULL)) continue; else if (*cmd == 's') { delim = *(++cmd), cmd++; size = 16; - what = realloc((void *) from, size); + what = realloc(from, size); len = 0; for (; *cmd && *cmd != delim; cmd++) { if (*cmd == '\\' @@ -481,7 +491,7 @@ _history_expand_command(command, len, re continue; size = 16; - with = realloc((void *) to, size); + with = realloc(to, size); len = 0; from_len = strlen(from); for (; *cmd && *cmd != delim; cmd++) { @@ -504,10 +514,10 @@ _history_expand_command(command, len, re with[len] = '\0'; to = with; - temp = _rl_compat_sub(line, from, to, + tempcmd = _rl_compat_sub(line, from, to, (g_on) ? 1 : 0); free(line); - line = temp; + line = tempcmd; g_on = 0; } } @@ -552,25 +562,25 @@ _history_expand_command(command, len, re (void)strcpy(arr[i], temp); } - size = 1, len = 0; - temp = malloc(size); + cmdsize = 1, cmdlen = 0; + tempcmd = malloc(cmdsize); for (i = start; start <= i && i <= end; i++) { int arr_len; arr_len = strlen(arr[i]); - if (len + arr_len + 1 >= size) { - size += arr_len + 1; - temp = realloc(temp, size); + if (cmdlen + arr_len + 1 >= cmdsize) { + cmdsize += arr_len + 1; + tempcmd = realloc(tempcmd, cmdsize); } - (void)strcpy(&temp[len], arr[i]); /* safe */ - len += arr_len; - temp[len++] = ' '; /* add a space */ - } - while (len > 0 && isspace(temp[len - 1])) - len--; - temp[len] = '\0'; + (void)strcpy(&tempcmd[cmdlen], arr[i]); /* safe */ + cmdlen += arr_len; + tempcmd[cmdlen++] = ' '; /* add a space */ + } + while (cmdlen > 0 && isspace((unsigned char) tempcmd[cmdlen - 1])) + cmdlen--; + tempcmd[cmdlen] = '\0'; - *result = temp; + *result = tempcmd; for (i = 0; i <= max; i++) free(arr[i]); @@ -586,7 +596,8 @@ history_expand(str, output) char *str; char **output; { - int i, retval = 0, size, idx; + int i, retval = 0, idx; + size_t size; char *temp, *result; if (h == NULL || e == NULL) @@ -615,7 +626,8 @@ history_expand(str, output) result = NULL; size = idx = 0; for (i = 0; str[i];) { - int start, j, len, loop_again; + int start, j, loop_again; + size_t len; loop_again = 1; start = j = i; @@ -631,7 +643,7 @@ loop: while (str[j] && str[++j] != '?'); if (str[j] == '?') j++; - } else if (isspace(str[j])) + } else if (isspace((unsigned char) str[j])) break; } if (str[j] == history_expansion_char @@ -664,7 +676,8 @@ loop: retval = 1; break; } - retval = _history_expand_command(&str[i], j - i, &temp); + retval = _history_expand_command(&str[i], (size_t)(j - i), + &temp); if (retval != -1) { len = strlen(temp); ADD_STRING(temp, len); @@ -689,26 +702,29 @@ loop: } /* - * returns array of tokens parsed out of string, much as the shell might + * Parse the string into individual tokens, similarily to how shell would do it. */ char ** history_tokenize(str) const char *str; { - int size = 1, result_idx = 0, i, start, len; + int size = 1, result_idx = 0, i, start; + size_t len; char **result = NULL, *temp, delim = '\0'; for (i = 0; str[i]; i++) { - while (isspace(str[i])) + while (isspace((unsigned char) str[i])) i++; start = i; for (; str[i]; i++) { - if (str[i] == '\\') - i++; - else if (str[i] == delim) + if (str[i] == '\\') { + if (str[i] != '\0') + i++; + } else if (str[i] == delim) delim = '\0'; else if (!delim && - (isspace(str[i]) || strchr("()<>;&|$", str[i]))) + (isspace((unsigned char) str[i]) || + strchr("()<>;&|$", str[i]))) break; else if (!delim && strchr("'`\"", str[i])) delim = str[i]; @@ -741,7 +757,7 @@ stifle_history(max) if (h == NULL || e == NULL) rl_initialize(); - if (history(h, &ev, H_SETMAXSIZE, max) == 0) + if (history(h, &ev, H_SETSIZE, max) == 0) max_input_history = max; } @@ -754,7 +770,7 @@ unstifle_history() HistEvent ev; int omax; - history(h, &ev, H_SETMAXSIZE, INT_MAX); + history(h, &ev, H_SETSIZE, INT_MAX); omax = max_input_history; max_input_history = INT_MAX; return omax; /* some value _must_ be returned */ @@ -1017,6 +1033,7 @@ history_search_prefix(str, direction) * search for event in history containing str, starting at offset * abs(pos); continue backward, if pos<0, forward otherwise */ +/* ARGSUSED */ int history_search_pos(str, direction, pos) const char *str; @@ -1150,11 +1167,15 @@ filename_completion_function(text, state return NULL; /* cannot open the directory */ } /* find the match */ - while ((entry = readdir(dir))) { + while ((entry = readdir(dir)) != NULL) { /* otherwise, get first entry where first */ /* filename_len characters are equal */ if (entry->d_name[0] == filename[0] +#if defined(__SVR4) || defined(__linux__) + && strlen(entry->d_name) >= filename_len +#else && entry->d_namlen >= filename_len +#endif && strncmp(entry->d_name, filename, filename_len) == 0) break; @@ -1163,7 +1184,11 @@ filename_completion_function(text, state if (entry) { /* match found */ struct stat stbuf; +#if defined(__SVR4) || defined(__linux__) + len = strlen(entry->d_name) + +#else len = entry->d_namlen + +#endif ((dirname) ? strlen(dirname) : 0) + 1 + 1; temp = malloc(len); (void)sprintf(temp, "%s%s", @@ -1214,6 +1239,7 @@ username_completion_function(text, state /* * el-compatible wrapper around rl_complete; needed for key binding */ +/* ARGSUSED */ static unsigned char _el_rl_complete(el, ch) EditLine *el; @@ -1231,15 +1257,15 @@ completion_matches(text, genfunc) CPFunction *genfunc; { char **match_list = NULL, *retstr, *prevstr; - size_t matches, math_list_len, max_equal, len, which, - i; + size_t math_list_len, max_equal, which, i; + int matches; if (h == NULL || e == NULL) rl_initialize(); matches = 0; math_list_len = 1; - while ((retstr = (*genfunc) (text, matches))) { + while ((retstr = (*genfunc) (text, matches)) != NULL) { if (matches + 1 >= math_list_len) { math_list_len <<= 1; match_list = realloc(match_list, @@ -1254,7 +1280,7 @@ completion_matches(text, genfunc) /* find least denominator and insert it to match_list[0] */ which = 2; prevstr = match_list[1]; - len = max_equal = strlen(prevstr); + max_equal = strlen(prevstr); for (; which < matches; which++) { for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++) @@ -1279,6 +1305,7 @@ completion_matches(text, genfunc) /* * called by rl_complete() */ +/* ARGSUSED */ static int rl_complete_internal(what_to_do) int what_to_do; @@ -1286,7 +1313,7 @@ rl_complete_internal(what_to_do) CPFunction *complet_func; const LineInfo *li; char *temp, *temp2, **arr; - int len; + size_t len; if (h == NULL || e == NULL) rl_initialize(); @@ -1296,6 +1323,7 @@ rl_complete_internal(what_to_do) complet_func = filename_completion_function; li = el_line(e); + /* LINTED const cast */ temp = (char *) li->cursor; while (temp > li->buffer && !strchr(rl_basic_word_break_characters, *(temp - 1))) @@ -1316,28 +1344,27 @@ rl_complete_internal(what_to_do) arr = completion_matches(temp, complet_func); else { int end = li->cursor - li->buffer; - arr = (*rl_attempted_completion_function) (temp, - end - len, end); + arr = (*rl_attempted_completion_function) (temp, (int) + (end - len), end); } - free(temp); /* no more needed */ if (arr) { int i; - el_deletestr(e, len); + el_deletestr(e, (int)len); el_insertstr(e, arr[0]); if (strcmp(arr[0], arr[1]) == 0) { /* lcd is valid object, so add a space to mark it */ /* in case of filename completition, add a space */ /* only if object found is not directory */ - int len = strlen(arr[0]); + size_t alen = strlen(arr[0]); if (complet_func != filename_completion_function - || (len > 0 && (arr[0])[len - 1] != '/')) + || (alen > 0 && (arr[0])[alen - 1] != '/')) el_insertstr(e, " "); } else /* lcd is not a valid object - further specification */ /* is needed */ - term_beep(e); + el_beep(e); /* free elements of array and the array itself */ for (i = 0; arr[i]; i++) @@ -1364,12 +1391,10 @@ rl_complete(ignore, invoking_key) return CC_REFRESH; } else return rl_complete_internal(invoking_key); - - return CC_REFRESH; } /* - * misc other functions + * misc other functions */ /* @@ -1411,6 +1436,7 @@ rl_read_key() /* * reset the terminal */ +/* ARGSUSED */ void rl_reset_terminal(p) const char *p;