mirror of
https://github.com/troglobit/editline.git
synced 2025-06-24 08:21:12 +08:00
Whitespace + coding style cleanup
- Use Emacs K&R style - Add missing braces to for() loops - Fix coding style issues in cli.c example - Add empty lines to separate sections - Simplify and de-indent code, check error case first + continue Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
parent
6021e54dbc
commit
b613db2afb
121
examples/cli.c
121
examples/cli.c
@ -25,59 +25,55 @@
|
|||||||
#define HISTORY "/tmp/.cli-history"
|
#define HISTORY "/tmp/.cli-history"
|
||||||
|
|
||||||
static char *list[] = {
|
static char *list[] = {
|
||||||
"foo ", "bar ", "bsd ", "cli ", "ls ", "cd ", "malloc ", "tee ", NULL
|
"foo ", "bar ", "bsd ", "cli ", "ls ", "cd ", "malloc ", "tee ", NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Attempt to complete the pathname, returning an allocated copy.
|
/* Attempt to complete the pathname, returning an allocated copy.
|
||||||
* Fill in *unique if we completed it, or set it to 0 if ambiguous. */
|
* Fill in *unique if we completed it, or set it to 0 if ambiguous. */
|
||||||
static char *my_rl_complete(char *token, int *match)
|
static char *my_rl_complete(char *token, int *match)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int index = -1;
|
int index = -1;
|
||||||
int matchlen = 0;
|
int matchlen = 0;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
for (i = 0; list[i]; i++)
|
for (i = 0; list[i]; i++) {
|
||||||
{
|
int partlen = strlen (token); /* Part of token */
|
||||||
int partlen = strlen (token); /* Part of token */
|
|
||||||
|
|
||||||
if (!strncmp (list[i], token, partlen))
|
if (!strncmp (list[i], token, partlen)) {
|
||||||
{
|
index = i;
|
||||||
index = i;
|
matchlen = partlen;
|
||||||
matchlen = partlen;
|
count ++;
|
||||||
count ++;
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (count == 1)
|
if (count == 1) {
|
||||||
{
|
*match = 1;
|
||||||
*match = 1;
|
return strdup (list[index] + matchlen);
|
||||||
return strdup (list[index] + matchlen);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return all possible completions. */
|
/* Return all possible completions. */
|
||||||
static int my_rl_list_possib(char *token, char ***av)
|
static int my_rl_list_possib(char *token, char ***av)
|
||||||
{
|
{
|
||||||
int i, num, total = 0;
|
int i, num, total = 0;
|
||||||
char **copy;
|
char **copy;
|
||||||
|
|
||||||
for (num = 0; list[num]; num++)
|
for (num = 0; list[num]; num++)
|
||||||
;
|
;
|
||||||
copy = (char **) malloc (num * sizeof(char *));
|
|
||||||
for (i = 0; i < num; i++)
|
|
||||||
{
|
|
||||||
if (!strncmp (list[i], token, strlen (token)))
|
|
||||||
{
|
|
||||||
copy[total] = strdup (list[i]);
|
|
||||||
total ++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*av = copy;
|
|
||||||
|
|
||||||
return total;
|
copy = (char **) malloc (num * sizeof(char *));
|
||||||
|
for (i = 0; i < num; i++) {
|
||||||
|
if (!strncmp (list[i], token, strlen (token))) {
|
||||||
|
copy[total] = strdup (list[i]);
|
||||||
|
total ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*av = copy;
|
||||||
|
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
el_status_t list_possible(void)
|
el_status_t list_possible(void)
|
||||||
@ -104,42 +100,49 @@ el_status_t list_possible(void)
|
|||||||
|
|
||||||
el_status_t do_break(void)
|
el_status_t do_break(void)
|
||||||
{
|
{
|
||||||
puts("Breakout!");
|
puts("Breakout!");
|
||||||
return CSeof;
|
return CSeof;
|
||||||
}
|
}
|
||||||
|
|
||||||
el_status_t do_exit(void)
|
el_status_t do_exit(void)
|
||||||
{
|
{
|
||||||
puts("Bye bye!");
|
puts("Bye bye!");
|
||||||
return CSeof;
|
return CSeof;
|
||||||
}
|
}
|
||||||
|
|
||||||
el_status_t do_suspend(void)
|
el_status_t do_suspend(void)
|
||||||
{
|
{
|
||||||
puts("Abort!");
|
puts("Abort!");
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac __attribute__ ((unused)), char *av[] __attribute__ ((unused)))
|
int main(void)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
char *prompt = "cli> ";
|
char *prompt = "cli> ";
|
||||||
|
|
||||||
/* Setup callbacks */
|
/* Setup callbacks */
|
||||||
rl_set_complete_func(&my_rl_complete);
|
rl_set_complete_func(&my_rl_complete);
|
||||||
rl_set_list_possib_func(&my_rl_list_possib);
|
rl_set_list_possib_func(&my_rl_list_possib);
|
||||||
el_bind_key('?', list_possible);
|
el_bind_key('?', list_possible);
|
||||||
el_bind_key(CTL('C'), do_break);
|
el_bind_key(CTL('C'), do_break);
|
||||||
el_bind_key(CTL('D'), do_exit);
|
el_bind_key(CTL('D'), do_exit);
|
||||||
el_bind_key(CTL('Z'), do_suspend);
|
el_bind_key(CTL('Z'), do_suspend);
|
||||||
read_history(HISTORY);
|
read_history(HISTORY);
|
||||||
|
|
||||||
while ((line = readline(prompt)) != NULL) {
|
while ((line = readline(prompt)) != NULL) {
|
||||||
printf("\t\t\t|%s|\n", line);
|
printf("\t\t\t|%s|\n", line);
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
write_history(HISTORY);
|
write_history(HISTORY);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* c-file-style: "k&r"
|
||||||
|
* c-basic-offset: 4
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
|
@ -73,9 +73,7 @@ int main(int argc, char *argv[] __attribute__ ((unused)))
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* version-control: t
|
* c-file-style: "k&r"
|
||||||
* indent-tabs-mode: t
|
|
||||||
* c-file-style: "ellemtel"
|
|
||||||
* c-basic-offset: 4
|
* c-basic-offset: 4
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
|
@ -74,6 +74,7 @@ static int FindMatches(char *dir, char *file, char ***avp)
|
|||||||
total = 0;
|
total = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ac) {
|
if (ac) {
|
||||||
memcpy(word, av, ac * sizeof(char *));
|
memcpy(word, av, ac * sizeof(char *));
|
||||||
free(av);
|
free(av);
|
||||||
@ -94,15 +95,22 @@ static int FindMatches(char *dir, char *file, char ***avp)
|
|||||||
closedir(dp);
|
closedir(dp);
|
||||||
if (total > MAX_TOTAL_MATCHES) {
|
if (total > MAX_TOTAL_MATCHES) {
|
||||||
char many[sizeof(total) * 3];
|
char many[sizeof(total) * 3];
|
||||||
|
|
||||||
p = many + sizeof(many);
|
p = many + sizeof(many);
|
||||||
*--p = '\0';
|
*--p = '\0';
|
||||||
while (choices > 0) {
|
while (choices > 0) {
|
||||||
*--p = '0' + choices % 10;
|
*--p = '0' + choices % 10;
|
||||||
choices /= 10;
|
choices /= 10;
|
||||||
}
|
}
|
||||||
while (p > many + sizeof(many) - 8) *--p = ' ';
|
|
||||||
if ((p = strdup(p)) != NULL) av[ac++] = p;
|
while (p > many + sizeof(many) - 8)
|
||||||
if ((p = strdup("choices")) != NULL) av[ac++] = p;
|
*--p = ' ';
|
||||||
|
|
||||||
|
if ((p = strdup(p)) != NULL)
|
||||||
|
av[ac++] = p;
|
||||||
|
|
||||||
|
if ((p = strdup("choices")) != NULL)
|
||||||
|
av[ac++] = p;
|
||||||
} else {
|
} else {
|
||||||
if (ac)
|
if (ac)
|
||||||
qsort(av, ac, sizeof(char *), compare);
|
qsort(av, ac, sizeof(char *), compare);
|
||||||
@ -121,6 +129,7 @@ static int SplitPath(char *path, char **dirpart, char **filepart)
|
|||||||
if ((fpart = strrchr(path, '/')) == NULL) {
|
if ((fpart = strrchr(path, '/')) == NULL) {
|
||||||
if ((dpart = strdup(DOT)) == NULL)
|
if ((dpart = strdup(DOT)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if ((fpart = strdup(path)) == NULL) {
|
if ((fpart = strdup(path)) == NULL) {
|
||||||
free(dpart);
|
free(dpart);
|
||||||
return -1;
|
return -1;
|
||||||
@ -128,6 +137,7 @@ static int SplitPath(char *path, char **dirpart, char **filepart)
|
|||||||
} else {
|
} else {
|
||||||
if ((dpart = strdup(path)) == NULL)
|
if ((dpart = strdup(path)) == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
dpart[fpart - path + 1] = '\0';
|
dpart[fpart - path + 1] = '\0';
|
||||||
if ((fpart = strdup(fpart + 1)) == NULL) {
|
if ((fpart = strdup(fpart + 1)) == NULL) {
|
||||||
free(dpart);
|
free(dpart);
|
||||||
@ -147,6 +157,7 @@ rl_complete_func_t *rl_set_complete_func(rl_complete_func_t *func)
|
|||||||
{
|
{
|
||||||
rl_complete_func_t *old = el_complete_func;
|
rl_complete_func_t *old = el_complete_func;
|
||||||
el_complete_func = func;
|
el_complete_func = func;
|
||||||
|
|
||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,11 +207,13 @@ char *el_filename_complete(char *pathname, int *match)
|
|||||||
*match = 0;
|
*match = 0;
|
||||||
if (len) {
|
if (len) {
|
||||||
/* Find largest matching substring. */
|
/* Find largest matching substring. */
|
||||||
for (i = len, end = strlen(av[0]); i < end; i++)
|
for (i = len, end = strlen(av[0]); i < end; i++) {
|
||||||
for (j = 1; j < ac; j++)
|
for (j = 1; j < ac; j++) {
|
||||||
if (av[0][i] != av[j][i])
|
if (av[0][i] != av[j][i])
|
||||||
goto breakout;
|
goto breakout;
|
||||||
breakout:
|
}
|
||||||
|
}
|
||||||
|
breakout:
|
||||||
if (i > len) {
|
if (i > len) {
|
||||||
j = i - len + 1;
|
j = i - len + 1;
|
||||||
p = malloc(sizeof(char) * j);
|
p = malloc(sizeof(char) * j);
|
||||||
@ -277,8 +290,7 @@ int rl_list_possib(char *token, char ***av)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* indent-tabs-mode: t
|
* c-file-style: "k&r"
|
||||||
* c-file-style: "ellemtel"
|
|
||||||
* c-basic-offset: 4
|
* c-basic-offset: 4
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
|
218
src/editline.c
218
src/editline.c
@ -161,7 +161,6 @@ static void tty_flush(void)
|
|||||||
|
|
||||||
if (!el_no_echo) {
|
if (!el_no_echo) {
|
||||||
res = write(el_outfd, Screen, ScreenCount);
|
res = write(el_outfd, Screen, ScreenCount);
|
||||||
|
|
||||||
if (res > 0)
|
if (res > 0)
|
||||||
ScreenCount = 0;
|
ScreenCount = 0;
|
||||||
}
|
}
|
||||||
@ -229,10 +228,12 @@ int rl_getc(void)
|
|||||||
static int tty_get(void)
|
static int tty_get(void)
|
||||||
{
|
{
|
||||||
tty_flush();
|
tty_flush();
|
||||||
|
|
||||||
if (el_pushed) {
|
if (el_pushed) {
|
||||||
el_pushed = 0;
|
el_pushed = 0;
|
||||||
return el_push_back;
|
return el_push_back;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*el_input)
|
if (*el_input)
|
||||||
return *el_input++;
|
return *el_input++;
|
||||||
|
|
||||||
@ -282,9 +283,10 @@ void el_print_columns(int ac, char **av)
|
|||||||
int cols;
|
int cols;
|
||||||
|
|
||||||
/* Find longest name, determine column count from that. */
|
/* Find longest name, determine column count from that. */
|
||||||
for (longest = 0, i = 0; i < ac; i++)
|
for (longest = 0, i = 0; i < ac; i++) {
|
||||||
if ((j = strlen((char *)av[i])) > longest)
|
if ((j = strlen((char *)av[i])) > longest)
|
||||||
longest = j;
|
longest = j;
|
||||||
|
}
|
||||||
cols = tty_cols / (longest + 3);
|
cols = tty_cols / (longest + 3);
|
||||||
|
|
||||||
tty_puts(NEWLINE);
|
tty_puts(NEWLINE);
|
||||||
@ -292,10 +294,13 @@ void el_print_columns(int ac, char **av)
|
|||||||
for (j = i; j < ac; j += skip) {
|
for (j = i; j < ac; j += skip) {
|
||||||
for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++)
|
for (p = av[j], len = strlen((char *)p), k = len; --k >= 0; p++)
|
||||||
tty_put(*p);
|
tty_put(*p);
|
||||||
if (j + skip < ac)
|
|
||||||
|
if (j + skip < ac) {
|
||||||
while (++len < longest + 3)
|
while (++len < longest + 3)
|
||||||
tty_put(' ');
|
tty_put(' ');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tty_puts(NEWLINE);
|
tty_puts(NEWLINE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -323,6 +328,7 @@ static void left(el_status_t Change)
|
|||||||
tty_back();
|
tty_back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Change == CSmove)
|
if (Change == CSmove)
|
||||||
rl_point--;
|
rl_point--;
|
||||||
}
|
}
|
||||||
@ -339,6 +345,7 @@ el_status_t el_ring_bell(void)
|
|||||||
{
|
{
|
||||||
tty_put('\07');
|
tty_put('\07');
|
||||||
tty_flush();
|
tty_flush();
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -355,6 +362,7 @@ static el_status_t do_macro(int c)
|
|||||||
el_input = NILSTR;
|
el_input = NILSTR;
|
||||||
return el_ring_bell();
|
return el_ring_bell();
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,14 +377,16 @@ static el_status_t do_forward(el_status_t move)
|
|||||||
p = &rl_line_buffer[rl_point];
|
p = &rl_line_buffer[rl_point];
|
||||||
|
|
||||||
/* Skip to end of word, if inside a word. */
|
/* Skip to end of word, if inside a word. */
|
||||||
for (; rl_point < rl_end && is_alpha_num(p[0]); rl_point++, p++)
|
for (; rl_point < rl_end && is_alpha_num(p[0]); rl_point++, p++) {
|
||||||
if (move == CSmove)
|
if (move == CSmove)
|
||||||
right(CSstay);
|
right(CSstay);
|
||||||
|
}
|
||||||
|
|
||||||
/* Skip to next word, or skip leading white space if outside a word. */
|
/* Skip to next word, or skip leading white space if outside a word. */
|
||||||
for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++)
|
for ( ; rl_point < rl_end && (p[0] == ' ' || !is_alpha_num(p[0])); rl_point++, p++) {
|
||||||
if (move == CSmove)
|
if (move == CSmove)
|
||||||
right(CSstay);
|
right(CSstay);
|
||||||
|
}
|
||||||
|
|
||||||
if (rl_point == rl_end)
|
if (rl_point == rl_end)
|
||||||
break;
|
break;
|
||||||
@ -506,7 +516,8 @@ static el_status_t insert_string(const char *p)
|
|||||||
|
|
||||||
static el_status_t redisplay(void)
|
static el_status_t redisplay(void)
|
||||||
{
|
{
|
||||||
tty_puts(NEWLINE); /* XXX: Use "\r\e[K" to get really neat effect on ANSI capable terminals. */
|
/* XXX: Use "\r\e[K" to get really neat effect on ANSI capable terminals. */
|
||||||
|
tty_puts(NEWLINE);
|
||||||
tty_puts(rl_prompt);
|
tty_puts(rl_prompt);
|
||||||
tty_string(rl_line_buffer);
|
tty_string(rl_line_buffer);
|
||||||
|
|
||||||
@ -550,13 +561,13 @@ static el_status_t do_insert_hist(const char *p)
|
|||||||
static el_status_t do_hist(const char *(*move)(void))
|
static el_status_t do_hist(const char *(*move)(void))
|
||||||
{
|
{
|
||||||
const char *p;
|
const char *p;
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
do {
|
do {
|
||||||
if ((p = move()) == NULL)
|
if ((p = move()) == NULL)
|
||||||
return el_ring_bell();
|
return el_ring_bell();
|
||||||
} while (++i < Repeat);
|
} while (++i < Repeat);
|
||||||
|
|
||||||
return do_insert_hist(p);
|
return do_insert_hist(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -595,9 +606,12 @@ static int substrcmp(const char *text, const char *pat, size_t len)
|
|||||||
|
|
||||||
if ((c = *pat) == '\0')
|
if ((c = *pat) == '\0')
|
||||||
return *text == '\0';
|
return *text == '\0';
|
||||||
for ( ; *text; text++)
|
|
||||||
|
for ( ; *text; text++) {
|
||||||
if (*text == c && strncmp(text, pat, len) == 0)
|
if (*text == c && strncmp(text, pat, len) == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -680,9 +694,8 @@ static el_status_t h_search(void)
|
|||||||
|
|
||||||
static el_status_t fd_char(void)
|
static el_status_t fd_char(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
do {
|
do {
|
||||||
if (rl_point >= rl_end)
|
if (rl_point >= rl_end)
|
||||||
break;
|
break;
|
||||||
@ -710,8 +723,8 @@ static void save_yank(int begin, int i)
|
|||||||
|
|
||||||
static el_status_t delete_string(int count)
|
static el_status_t delete_string(int count)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *p;
|
char *p;
|
||||||
|
|
||||||
if (count <= 0 || rl_end == rl_point)
|
if (count <= 0 || rl_end == rl_point)
|
||||||
return el_ring_bell();
|
return el_ring_bell();
|
||||||
@ -734,6 +747,7 @@ static el_status_t delete_string(int count)
|
|||||||
*p = '\0';
|
*p = '\0';
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rl_point + count > rl_end && (count = rl_end - rl_point) <= 0)
|
if (rl_point + count > rl_end && (count = rl_end - rl_point) <= 0)
|
||||||
return CSstay;
|
return CSstay;
|
||||||
|
|
||||||
@ -745,14 +759,14 @@ static el_status_t delete_string(int count)
|
|||||||
ceol();
|
ceol();
|
||||||
rl_end -= count;
|
rl_end -= count;
|
||||||
tty_string(&rl_line_buffer[rl_point]);
|
tty_string(&rl_line_buffer[rl_point]);
|
||||||
|
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
static el_status_t bk_char(void)
|
static el_status_t bk_char(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
do {
|
do {
|
||||||
if (rl_point == 0)
|
if (rl_point == 0)
|
||||||
break;
|
break;
|
||||||
@ -764,9 +778,8 @@ static el_status_t bk_char(void)
|
|||||||
|
|
||||||
static el_status_t bk_del_char(void)
|
static el_status_t bk_del_char(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
i = 0;
|
|
||||||
do {
|
do {
|
||||||
if (rl_point == 0)
|
if (rl_point == 0)
|
||||||
break;
|
break;
|
||||||
@ -778,7 +791,7 @@ static el_status_t bk_del_char(void)
|
|||||||
|
|
||||||
static el_status_t kill_line(void)
|
static el_status_t kill_line(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (Repeat != NO_ARG) {
|
if (Repeat != NO_ARG) {
|
||||||
if (Repeat < rl_point) {
|
if (Repeat < rl_point) {
|
||||||
@ -790,6 +803,7 @@ static el_status_t kill_line(void)
|
|||||||
right(CSmove);
|
right(CSmove);
|
||||||
delete_string(Repeat - rl_point - 1);
|
delete_string(Repeat - rl_point - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -797,6 +811,7 @@ static el_status_t kill_line(void)
|
|||||||
rl_line_buffer[rl_point] = '\0';
|
rl_line_buffer[rl_point] = '\0';
|
||||||
ceol();
|
ceol();
|
||||||
rl_end = rl_point;
|
rl_end = rl_point;
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -811,6 +826,7 @@ static el_status_t insert_char(int c)
|
|||||||
if (Repeat == NO_ARG || Repeat < 2) {
|
if (Repeat == NO_ARG || Repeat < 2) {
|
||||||
buff[0] = c;
|
buff[0] = c;
|
||||||
buff[1] = '\0';
|
buff[1] = '\0';
|
||||||
|
|
||||||
return insert_string(buff);
|
return insert_string(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -834,6 +850,7 @@ static el_status_t beg_line(void)
|
|||||||
rl_point = 0;
|
rl_point = 0;
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -843,6 +860,7 @@ static el_status_t end_line(void)
|
|||||||
rl_point = rl_end;
|
rl_point = rl_end;
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,7 +877,7 @@ el_status_t el_del_char(void)
|
|||||||
static el_status_t meta(void)
|
static el_status_t meta(void)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
el_keymap_t *kp;
|
el_keymap_t *kp;
|
||||||
|
|
||||||
if ((c = tty_get()) == EOF)
|
if ((c = tty_get()) == EOF)
|
||||||
return CSeof;
|
return CSeof;
|
||||||
@ -868,19 +886,19 @@ static el_status_t meta(void)
|
|||||||
/* Also include VT-100 arrows. */
|
/* Also include VT-100 arrows. */
|
||||||
if (c == '[' || c == 'O') {
|
if (c == '[' || c == 'O') {
|
||||||
switch (tty_get()) {
|
switch (tty_get()) {
|
||||||
case EOF: return CSeof;
|
case EOF: return CSeof;
|
||||||
case '2': tty_get(); return CSstay; /* Insert */
|
case '2': tty_get(); return CSstay; /* Insert */
|
||||||
case '3': tty_get(); return del_char(); /* Delete */
|
case '3': tty_get(); return del_char(); /* Delete */
|
||||||
case '5': tty_get(); return CSstay; /* PgUp */
|
case '5': tty_get(); return CSstay; /* PgUp */
|
||||||
case '6': tty_get(); return CSstay; /* PgDn */
|
case '6': tty_get(); return CSstay; /* PgDn */
|
||||||
case 'A': return h_prev(); /* Up */
|
case 'A': return h_prev(); /* Up */
|
||||||
case 'B': return h_next(); /* Down */
|
case 'B': return h_next(); /* Down */
|
||||||
case 'C': return fd_char(); /* Left */
|
case 'C': return fd_char(); /* Left */
|
||||||
case 'D': return bk_char(); /* Right */
|
case 'D': return bk_char(); /* Right */
|
||||||
case 'F': return end_line(); /* End */
|
case 'F': return end_line(); /* End */
|
||||||
case 'H': return beg_line(); /* Home */
|
case 'H': return beg_line(); /* Home */
|
||||||
default: /* Fall through */
|
default: /* Fall through */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return el_ring_bell();
|
return el_ring_bell();
|
||||||
@ -891,11 +909,13 @@ static el_status_t meta(void)
|
|||||||
for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); )
|
for (Repeat = c - '0'; (c = tty_get()) != EOF && isdigit(c); )
|
||||||
Repeat = Repeat * 10 + c - '0';
|
Repeat = Repeat * 10 + c - '0';
|
||||||
tty_push(c);
|
tty_push(c);
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isupper(c))
|
if (isupper(c))
|
||||||
return do_macro(c);
|
return do_macro(c);
|
||||||
|
|
||||||
for (kp = MetaMap; kp->Function; kp++) {
|
for (kp = MetaMap; kp->Function; kp++) {
|
||||||
if (kp->Key == c)
|
if (kp->Key == c)
|
||||||
return kp->Function();
|
return kp->Function();
|
||||||
@ -957,18 +977,20 @@ static el_status_t tty_special(int c)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (rl_meta_chars && ISMETA(c))
|
if (rl_meta_chars && ISMETA(c))
|
||||||
return CSdispatch;
|
return CSdispatch;
|
||||||
|
|
||||||
if (c == rl_erase || c == DEL)
|
if (c == rl_erase || c == DEL)
|
||||||
return bk_del_char();
|
return bk_del_char();
|
||||||
|
|
||||||
if (c == rl_kill) {
|
if (c == rl_kill) {
|
||||||
if (rl_point != 0) {
|
if (rl_point != 0) {
|
||||||
rl_point = 0;
|
rl_point = 0;
|
||||||
reposition();
|
reposition();
|
||||||
}
|
}
|
||||||
Repeat = NO_ARG;
|
Repeat = NO_ARG;
|
||||||
return kill_line();
|
|
||||||
|
return kill_line();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_EOF
|
#ifdef CONFIG_EOF
|
||||||
@ -990,6 +1012,21 @@ static char *editinput(void)
|
|||||||
el_intr_pending = -1;
|
el_intr_pending = -1;
|
||||||
while ((c = tty_get()) != EOF) {
|
while ((c = tty_get()) != EOF) {
|
||||||
switch (tty_special(c)) {
|
switch (tty_special(c)) {
|
||||||
|
case CSdone:
|
||||||
|
return rl_line_buffer;
|
||||||
|
|
||||||
|
case CSeof:
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
case CSsignal:
|
||||||
|
return (char *)"";
|
||||||
|
|
||||||
|
case CSmove:
|
||||||
|
reposition();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CSdispatch:
|
||||||
|
switch (emacs(c)) {
|
||||||
case CSdone:
|
case CSdone:
|
||||||
return rl_line_buffer;
|
return rl_line_buffer;
|
||||||
|
|
||||||
@ -1004,28 +1041,13 @@ static char *editinput(void)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case CSdispatch:
|
case CSdispatch:
|
||||||
switch (emacs(c)) {
|
|
||||||
case CSdone:
|
|
||||||
return rl_line_buffer;
|
|
||||||
|
|
||||||
case CSeof:
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
case CSsignal:
|
|
||||||
return (char *)"";
|
|
||||||
|
|
||||||
case CSmove:
|
|
||||||
reposition();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CSdispatch:
|
|
||||||
case CSstay:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CSstay:
|
case CSstay:
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CSstay:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1086,12 +1108,14 @@ static char *read_redirected(void)
|
|||||||
|
|
||||||
p += oldpos; /* Continue where we left off... */
|
p += oldpos; /* Continue where we left off... */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (read(el_infd, p, 1) <= 0) {
|
if (read(el_infd, p, 1) <= 0) {
|
||||||
/* Ignore "incomplete" lines at EOF, just like we do for a tty. */
|
/* Ignore "incomplete" lines at EOF, just like we do for a tty. */
|
||||||
free(line);
|
free(line);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (*p == '\n')
|
|
||||||
|
if (*p == '\n')
|
||||||
break;
|
break;
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
@ -1133,14 +1157,14 @@ void rl_reset_terminal(const char *terminal_name)
|
|||||||
|
|
||||||
if (tty_cols <= 0 || tty_rows <= 0) {
|
if (tty_cols <= 0 || tty_rows <= 0) {
|
||||||
#ifdef TIOCGWINSZ
|
#ifdef TIOCGWINSZ
|
||||||
if (ioctl(el_outfd, TIOCGWINSZ, &W) >= 0 && W.ws_col > 0 && W.ws_row > 0) {
|
if (ioctl(el_outfd, TIOCGWINSZ, &W) >= 0 && W.ws_col > 0 && W.ws_row > 0) {
|
||||||
tty_cols = (int)W.ws_col;
|
tty_cols = (int)W.ws_col;
|
||||||
tty_rows = (int)W.ws_row;
|
tty_rows = (int)W.ws_row;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
tty_cols = SCREEN_COLS;
|
tty_cols = SCREEN_COLS;
|
||||||
tty_rows = SCREEN_ROWS;
|
tty_rows = SCREEN_ROWS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1225,6 +1249,7 @@ char *readline(const char *prompt)
|
|||||||
|
|
||||||
if (!isatty(0)) {
|
if (!isatty(0)) {
|
||||||
tty_flush();
|
tty_flush();
|
||||||
|
|
||||||
return read_redirected();
|
return read_redirected();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1246,6 +1271,7 @@ char *readline(const char *prompt)
|
|||||||
rl_prompt = prompt ? prompt : NILSTR;
|
rl_prompt = prompt ? prompt : NILSTR;
|
||||||
if (el_no_echo) {
|
if (el_no_echo) {
|
||||||
int old = el_no_echo;
|
int old = el_no_echo;
|
||||||
|
|
||||||
el_no_echo = 0;
|
el_no_echo = 0;
|
||||||
tty_puts(rl_prompt);
|
tty_puts(rl_prompt);
|
||||||
tty_flush();
|
tty_flush();
|
||||||
@ -1273,9 +1299,10 @@ char *readline(const char *prompt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (el_intr_pending > 0) {
|
if (el_intr_pending > 0) {
|
||||||
int s = el_intr_pending;
|
int signo = el_intr_pending;
|
||||||
el_intr_pending = 0;
|
|
||||||
kill(getpid(), s);
|
el_intr_pending = 0;
|
||||||
|
kill(getpid(), signo);
|
||||||
}
|
}
|
||||||
|
|
||||||
return line;
|
return line;
|
||||||
@ -1306,6 +1333,7 @@ int read_history(const char *filename)
|
|||||||
while (H.Size < el_hist_size) {
|
while (H.Size < el_hist_size) {
|
||||||
if (!fgets(buf, SCREEN_INC, fp))
|
if (!fgets(buf, SCREEN_INC, fp))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
buf[strlen(buf) - 1] = 0; /* Remove '\n' */
|
buf[strlen(buf) - 1] = 0; /* Remove '\n' */
|
||||||
add_history(buf);
|
add_history(buf);
|
||||||
}
|
}
|
||||||
@ -1428,7 +1456,7 @@ static el_status_t c_complete(void)
|
|||||||
|
|
||||||
while (*p) {
|
while (*p) {
|
||||||
if ((*p < ' ' || strchr(SEPS, *p) != NULL)
|
if ((*p < ' ' || strchr(SEPS, *p) != NULL)
|
||||||
&& (!unique || p[1] != 0)) {
|
&& (!unique || p[1] != 0)) {
|
||||||
*q++ = '\\';
|
*q++ = '\\';
|
||||||
}
|
}
|
||||||
*q++ = *p++;
|
*q++ = *p++;
|
||||||
@ -1480,6 +1508,7 @@ static el_status_t transpose(void)
|
|||||||
rl_line_buffer[rl_point++] = c;
|
rl_line_buffer[rl_point++] = c;
|
||||||
tty_show(c);
|
tty_show(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1508,6 +1537,7 @@ static el_status_t exchange(void)
|
|||||||
rl_point = c;
|
rl_point = c;
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1515,6 +1545,7 @@ static el_status_t yank(void)
|
|||||||
{
|
{
|
||||||
if (Yanked && *Yanked)
|
if (Yanked && *Yanked)
|
||||||
return insert_string(Yanked);
|
return insert_string(Yanked);
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1533,17 +1564,19 @@ static el_status_t copy_region(void)
|
|||||||
|
|
||||||
static el_status_t move_to_char(void)
|
static el_status_t move_to_char(void)
|
||||||
{
|
{
|
||||||
int c;
|
int i, c;
|
||||||
int i;
|
char *p;
|
||||||
char *p;
|
|
||||||
|
|
||||||
if ((c = tty_get()) == EOF)
|
if ((c = tty_get()) == EOF)
|
||||||
return CSeof;
|
return CSeof;
|
||||||
for (i = rl_point + 1, p = &rl_line_buffer[i]; i < rl_end; i++, p++)
|
|
||||||
|
for (i = rl_point + 1, p = &rl_line_buffer[i]; i < rl_end; i++, p++) {
|
||||||
if (*p == c) {
|
if (*p == c) {
|
||||||
rl_point = i;
|
rl_point = i;
|
||||||
return CSmove;
|
return CSmove;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1554,7 +1587,7 @@ static el_status_t fd_word(void)
|
|||||||
|
|
||||||
static el_status_t fd_kill_word(void)
|
static el_status_t fd_kill_word(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
do_forward(CSstay);
|
do_forward(CSstay);
|
||||||
if (old_point != rl_point) {
|
if (old_point != rl_point) {
|
||||||
@ -1562,6 +1595,7 @@ static el_status_t fd_kill_word(void)
|
|||||||
rl_point = old_point;
|
rl_point = old_point;
|
||||||
return delete_string(i);
|
return delete_string(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CSstay;
|
return CSstay;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1605,34 +1639,35 @@ static int argify(char *line, char ***avp)
|
|||||||
i = MEM_INC;
|
i = MEM_INC;
|
||||||
*avp = p = malloc(sizeof(char *) * i);
|
*avp = p = malloc(sizeof(char *) * i);
|
||||||
if (!p)
|
if (!p)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (c = line; isspace(*c); c++)
|
for (c = line; isspace(*c); c++)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (*c == '\n' || *c == '\0')
|
if (*c == '\n' || *c == '\0')
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
|
for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
|
||||||
if (isspace(*c)) {
|
if (!isspace(*c)) {
|
||||||
*c++ = '\0';
|
c++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (*c && *c != '\n') {
|
*c++ = '\0';
|
||||||
if (ac + 1 == i) {
|
if (*c && *c != '\n') {
|
||||||
arg = malloc(sizeof(char *) * (i + MEM_INC));
|
if (ac + 1 == i) {
|
||||||
if (!arg) {
|
arg = malloc(sizeof(char *) * (i + MEM_INC));
|
||||||
p[ac] = NULL;
|
if (!arg) {
|
||||||
return ac;
|
p[ac] = NULL;
|
||||||
}
|
return ac;
|
||||||
|
}
|
||||||
|
|
||||||
memcpy(arg, p, i * sizeof(char *));
|
memcpy(arg, p, i * sizeof(char *));
|
||||||
i += MEM_INC;
|
i += MEM_INC;
|
||||||
free(p);
|
free(p);
|
||||||
*avp = p = arg;
|
*avp = p = arg;
|
||||||
}
|
}
|
||||||
p[ac++] = c;
|
p[ac++] = c;
|
||||||
}
|
|
||||||
} else {
|
|
||||||
c++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1781,8 +1816,7 @@ el_status_t el_bind_key_in_metamap(int key, el_keymap_func_t function)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* indent-tabs-mode: t
|
* c-file-style: "k&r"
|
||||||
* c-file-style: "ellemtel"
|
|
||||||
* c-basic-offset: 4
|
* c-basic-offset: 4
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
|
@ -57,8 +57,7 @@ void rl_add_slash(char *path, char *p)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* indent-tabs-mode: t
|
* c-file-style: "k&r"
|
||||||
* c-file-style: "ellemtel"
|
|
||||||
* c-basic-offset: 4
|
* c-basic-offset: 4
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
|
@ -87,6 +87,7 @@ void rl_ttyset(int Reset)
|
|||||||
if (!Reset) {
|
if (!Reset) {
|
||||||
if (-1 == getattr(0, &old))
|
if (-1 == getattr(0, &old))
|
||||||
perror("Failed tcgetattr()");
|
perror("Failed tcgetattr()");
|
||||||
|
|
||||||
rl_erase = old.c_cc[VERASE];
|
rl_erase = old.c_cc[VERASE];
|
||||||
rl_kill = old.c_cc[VKILL];
|
rl_kill = old.c_cc[VKILL];
|
||||||
rl_eof = old.c_cc[VEOF];
|
rl_eof = old.c_cc[VEOF];
|
||||||
@ -124,6 +125,7 @@ void rl_ttyset(int Reset)
|
|||||||
if (!Reset) {
|
if (!Reset) {
|
||||||
if (-1 == ioctl_wrap(0, TCGETA, &old))
|
if (-1 == ioctl_wrap(0, TCGETA, &old))
|
||||||
perror("Failed ioctl(TCGETA)");
|
perror("Failed ioctl(TCGETA)");
|
||||||
|
|
||||||
rl_erase = old.c_cc[VERASE];
|
rl_erase = old.c_cc[VERASE];
|
||||||
rl_kill = old.c_cc[VKILL];
|
rl_kill = old.c_cc[VKILL];
|
||||||
rl_eof = old.c_cc[VEOF];
|
rl_eof = old.c_cc[VEOF];
|
||||||
@ -167,19 +169,22 @@ void rl_ttyset(int Reset)
|
|||||||
if (!Reset) {
|
if (!Reset) {
|
||||||
if (-1 == ioctl_wrap(0, TIOCGETP, &old_sgttyb))
|
if (-1 == ioctl_wrap(0, TIOCGETP, &old_sgttyb))
|
||||||
perror("Failed TIOCGETP");
|
perror("Failed TIOCGETP");
|
||||||
|
|
||||||
rl_erase = old_sgttyb.sg_erase;
|
rl_erase = old_sgttyb.sg_erase;
|
||||||
rl_kill = old_sgttyb.sg_kill;
|
rl_kill = old_sgttyb.sg_kill;
|
||||||
|
|
||||||
if (-1 == ioctl_wrap(0, TIOCGETC, &old_tchars))
|
if (-1 == ioctl_wrap(0, TIOCGETC, &old_tchars))
|
||||||
perror("Failed TIOCGETC");
|
perror("Failed TIOCGETC");
|
||||||
rl_eof = old_tchars.t_eofc;
|
|
||||||
|
rl_eof = old_tchars.t_eofc;
|
||||||
rl_intr = old_tchars.t_intrc;
|
rl_intr = old_tchars.t_intrc;
|
||||||
rl_quit = old_tchars.t_quitc;
|
rl_quit = old_tchars.t_quitc;
|
||||||
|
|
||||||
#ifdef CONFIG_SIGSTOP
|
#ifdef CONFIG_SIGSTOP
|
||||||
if (-1 == ioctl_wrap(0, TIOCGLTC, &old_ltchars))
|
if (-1 == ioctl_wrap(0, TIOCGLTC, &old_ltchars))
|
||||||
perror("Failed TIOCGLTC");
|
perror("Failed TIOCGLTC");
|
||||||
rl_susp = old_ltchars.t_suspc;
|
|
||||||
|
rl_susp = old_ltchars.t_suspc;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
new_sgttyb = old_sgttyb;
|
new_sgttyb = old_sgttyb;
|
||||||
@ -189,9 +194,11 @@ void rl_ttyset(int Reset)
|
|||||||
new_sgttyb.sg_flags &= ~PASS8;
|
new_sgttyb.sg_flags &= ~PASS8;
|
||||||
else
|
else
|
||||||
new_sgttyb.sg_flags |= PASS8;
|
new_sgttyb.sg_flags |= PASS8;
|
||||||
if (-1 == ioctl_wrap(0, TIOCSETP, &new_sgttyb))
|
|
||||||
|
if (-1 == ioctl_wrap(0, TIOCSETP, &new_sgttyb))
|
||||||
perror("Failed TIOCSETP");
|
perror("Failed TIOCSETP");
|
||||||
new_tchars = old_tchars;
|
|
||||||
|
new_tchars = old_tchars;
|
||||||
new_tchars.t_intrc = -1;
|
new_tchars.t_intrc = -1;
|
||||||
new_tchars.t_quitc = -1;
|
new_tchars.t_quitc = -1;
|
||||||
if (-1 == ioctl_wrap(0, TIOCSETC, &new_tchars))
|
if (-1 == ioctl_wrap(0, TIOCSETC, &new_tchars))
|
||||||
@ -199,7 +206,8 @@ void rl_ttyset(int Reset)
|
|||||||
} else {
|
} else {
|
||||||
if (-1 == ioctl_wrap(0, TIOCSETP, &old_sgttyb))
|
if (-1 == ioctl_wrap(0, TIOCSETP, &old_sgttyb))
|
||||||
perror("Failed TIOCSETP");
|
perror("Failed TIOCSETP");
|
||||||
if (-1 == ioctl_wrap(0, TIOCSETC, &old_tchars))
|
|
||||||
|
if (-1 == ioctl_wrap(0, TIOCSETC, &old_tchars))
|
||||||
perror("Failed TIOCSETC");
|
perror("Failed TIOCSETC");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -232,8 +240,7 @@ void rl_add_slash(char *path, char *p)
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Variables:
|
* Local Variables:
|
||||||
* indent-tabs-mode: t
|
* c-file-style: "k&r"
|
||||||
* c-file-style: "ellemtel"
|
|
||||||
* c-basic-offset: 4
|
* c-basic-offset: 4
|
||||||
* End:
|
* End:
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user