version 1.22, 2002/04/09 01:57:34 |
version 1.23, 2002/10/27 20:24:28 |
Line 219 rl_initialize(void) |
|
Line 219 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 288 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 298 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 334 _rl_compat_sub(const char *str, const ch |
|
Line 343 _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 355 _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 372 _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 522 _history_expand_command(const char *comm |
|
cmd++; |
cmd++; |
|
|
line = strdup(event_data); |
line = strdup(event_data); |
|
if (line == NULL) |
|
return NULL; |
for (; *cmd; cmd++) { |
for (; *cmd; cmd++) { |
if (*cmd == ':') |
if (*cmd == ':') |
continue; |
continue; |
Line 525 _history_expand_command(const char *comm |
|
Line 550 _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 NULL; |
|
} |
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 NULL; |
|
} |
|
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 NULL; |
|
} else { |
from = NULL; |
from = NULL; |
return (-1); |
return (-1); |
} |
} |
Line 552 _history_expand_command(const char *comm |
|
Line 590 _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 623 _history_expand_command(const char *comm |
|
Line 671 _history_expand_command(const char *comm |
|
} |
} |
|
|
cmdsize = 1, cmdlen = 0; |
cmdsize = 1, cmdlen = 0; |
tempcmd = malloc(cmdsize); |
if ((tempcmd = malloc(cmdsize)) == NULL) |
|
return NULL; |
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 NULL; |
|
} |
|
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 718 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 NULL; |
|
|
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 732 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 NULL; \ |
|
} \ |
|
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 853 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 1231 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 1253 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 1206 filename_completion_function(const char |
|
Line 1285 filename_completion_function(const char |
|
} |
} |
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 1259 filename_completion_function(const char |
|
Line 1359 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 */ |
|
|
Line 1332 completion_matches(const char *text, CPF |
|
Line 1434 completion_matches(const char *text, CPF |
|
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) { |
if (matches + 1 >= 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 1463 completion_matches(const char *text, CPF |
|
} |
} |
|
|
retstr = malloc(max_equal + 1); |
retstr = malloc(max_equal + 1); |
|
if (retstr == NULL) |
|
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) |
if (matches + 1 >= match_list_len) { |
match_list = realloc(match_list, |
char **nmatch_list; |
|
nmatch_list = realloc(match_list, |
(match_list_len + 1) * sizeof(char *)); |
(match_list_len + 1) * sizeof(char *)); |
|
if (nmatch_list == NULL) { |
|
free(match_list); |
|
return NULL; |
|
} |
|
match_list = nmatch_list; |
|
} |
|
|
match_list[matches + 1] = (char *) NULL; |
match_list[matches + 1] = (char *) NULL; |
|
|
return (match_list); |
return (match_list); |