2019-07-23 13:18:52 +03:00
|
|
|
/* $NetBSD: chared.c,v 1.59 2019/07/23 10:18:52 christos Exp $ */
|
1997-01-11 09:47:47 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christos Zoulas of Cornell University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:42:00 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1994-05-06 10:01:42 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-03-18 19:00:50 +03:00
|
|
|
#include "config.h"
|
1994-05-06 10:01:42 +04:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
1997-01-11 09:47:47 +03:00
|
|
|
#if 0
|
1994-05-06 10:01:42 +04:00
|
|
|
static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
|
1997-01-11 09:47:47 +03:00
|
|
|
#else
|
2019-07-23 13:18:52 +03:00
|
|
|
__RCSID("$NetBSD: chared.c,v 1.59 2019/07/23 10:18:52 christos Exp $");
|
1997-01-11 09:47:47 +03:00
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* not lint && not SCCSID */
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
/*
|
1994-05-06 10:01:42 +04:00
|
|
|
* chared.c: Character editor utilities
|
|
|
|
*/
|
2016-02-17 01:53:14 +03:00
|
|
|
#include <ctype.h>
|
1994-05-06 10:01:42 +04:00
|
|
|
#include <stdlib.h>
|
2016-02-17 01:53:14 +03:00
|
|
|
#include <string.h>
|
2016-02-16 22:08:41 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
#include "el.h"
|
2016-02-16 22:08:41 +03:00
|
|
|
#include "common.h"
|
From Ingo Schwarze:
* Replace fcns.c by a shorter and simpler func.h
and include it only in the one file needing it, map.c.
* Combine help.h and help.c into a simplified help.h
and include it only in the one file needing it, map.c.
* Check the very simple, static files editline.c, historyn.c, and
tokenizern.c into CVS rather than needlessly generating them.
* So we no longer autogenerate any C files. :-)
* Shorten and simplify makelist by deleting the options -n, -e, -bc,
and -m; the latter was unused and useless in the first place.
* Move the declaration of el_func_t from fcns.h to the header
actually needing it, map.h. Since that header is already
included by el.h for unrelated reasons, that makes el_func_t
just as globally available as before.
* No longer include the simplified fcns.h into el.h,
include it directly into the *.c files needing it.
2016-04-18 20:01:19 +03:00
|
|
|
#include "fcns.h"
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2001-01-10 10:45:41 +03:00
|
|
|
/* value to leave unused in line buffer */
|
|
|
|
#define EL_LEAVE 2
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* cv_undo():
|
|
|
|
* Handle state for the vi undo command
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2002-11-15 17:32:32 +03:00
|
|
|
cv_undo(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
c_undo_t *vu = &el->el_chared.c_undo;
|
2002-11-15 17:32:32 +03:00
|
|
|
c_redo_t *r = &el->el_chared.c_redo;
|
2009-02-16 00:55:23 +03:00
|
|
|
size_t size;
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
/* Save entire line for undo */
|
2011-08-16 20:25:15 +04:00
|
|
|
size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
|
|
|
|
vu->len = (ssize_t)size;
|
2009-02-16 00:55:23 +03:00
|
|
|
vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
|
2009-12-31 01:37:40 +03:00
|
|
|
(void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
|
2002-11-15 17:32:32 +03:00
|
|
|
|
|
|
|
/* save command info for redo */
|
|
|
|
r->count = el->el_state.doingarg ? el->el_state.argument : 0;
|
|
|
|
r->action = el->el_chared.c_vcmd.action;
|
|
|
|
r->pos = r->buf;
|
|
|
|
r->cmd = el->el_state.thiscmd;
|
|
|
|
r->ch = el->el_state.thisch;
|
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
/* cv_yank():
|
|
|
|
* Save yank/delete data for paste
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2016-04-11 03:50:13 +03:00
|
|
|
cv_yank(EditLine *el, const wchar_t *ptr, int size)
|
2002-11-15 17:32:32 +03:00
|
|
|
{
|
|
|
|
c_kill_t *k = &el->el_chared.c_kill;
|
|
|
|
|
2011-08-16 20:25:15 +04:00
|
|
|
(void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
|
2002-11-15 17:32:32 +03:00
|
|
|
k->last = k->buf + size;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
/* c_insert():
|
1994-05-06 10:01:42 +04:00
|
|
|
* Insert num characters
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_insert(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
if (el->el_line.lastchar + num >= el->el_line.limit) {
|
2009-02-16 00:55:23 +03:00
|
|
|
if (!ch_enlargebufs(el, (size_t)num))
|
2002-11-15 17:32:32 +03:00
|
|
|
return; /* can't go past end of buffer */
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor < el->el_line.lastchar) {
|
|
|
|
/* if I must move chars */
|
|
|
|
for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
|
|
|
|
cp[num] = *cp;
|
|
|
|
}
|
|
|
|
el->el_line.lastchar += num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* c_delafter():
|
|
|
|
* Delete num characters after the cursor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delafter(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor + num > el->el_line.lastchar)
|
2009-02-16 00:55:23 +03:00
|
|
|
num = (int)(el->el_line.lastchar - el->el_line.cursor);
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
if (el->el_map.current != el->el_map.emacs) {
|
|
|
|
cv_undo(el);
|
|
|
|
cv_yank(el, el->el_line.cursor, num);
|
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (num > 0) {
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
|
|
|
|
*cp = cp[num];
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.lastchar -= num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-13 16:10:38 +04:00
|
|
|
/* c_delafter1():
|
|
|
|
* Delete the character after the cursor, do not yank
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2004-08-13 16:10:38 +04:00
|
|
|
c_delafter1(EditLine *el)
|
|
|
|
{
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp;
|
2004-08-13 16:10:38 +04:00
|
|
|
|
|
|
|
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
|
|
|
|
*cp = cp[1];
|
|
|
|
|
|
|
|
el->el_line.lastchar--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* c_delbefore():
|
|
|
|
* Delete num characters before the cursor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delbefore(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor - num < el->el_line.buffer)
|
2009-02-16 00:55:23 +03:00
|
|
|
num = (int)(el->el_line.cursor - el->el_line.buffer);
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
if (el->el_map.current != el->el_map.emacs) {
|
|
|
|
cv_undo(el);
|
|
|
|
cv_yank(el, el->el_line.cursor - num, num);
|
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (num > 0) {
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (cp = el->el_line.cursor - num;
|
2019-07-23 12:47:16 +03:00
|
|
|
&cp[num] <= el->el_line.lastchar;
|
2000-09-05 02:06:28 +04:00
|
|
|
cp++)
|
|
|
|
*cp = cp[num];
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.lastchar -= num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-13 16:10:38 +04:00
|
|
|
/* c_delbefore1():
|
|
|
|
* Delete the character before the cursor, do not yank
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2004-08-13 16:10:38 +04:00
|
|
|
c_delbefore1(EditLine *el)
|
|
|
|
{
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp;
|
2004-08-13 16:10:38 +04:00
|
|
|
|
|
|
|
for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
|
|
|
|
*cp = cp[1];
|
|
|
|
|
|
|
|
el->el_line.lastchar--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* ce__isword():
|
|
|
|
* Return if p is part of a word according to emacs
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
From Ingo Schwarze:
As we have seen before, "histedit.h" can never get rid of including
the <wchar.h> header because using the data types defined there is
deeply ingrained in the public interfaces of libedit.
Now POSIX unconditionally requires that <wchar.h> defines the type
wint_t. Consequently, it can be used unconditionally, no matter
whether WIDECHAR is active or not. Consequently, the #define Int
is pointless.
Note that removing it is not gratuitious churn. Auditing for
integer signedness problems is already hard when only fundamental
types like "int" and "unsigned" are involved. It gets very hard
when types come into the picture that have platform-dependent
signedness, like "char" and "wint_t". Adding yet another layer
on top, changing both the signedness and the width in a platform-
dependent way, makes auditing yet harder, which IMHO is really
dangerous. Note that while removing the #define, i already found
one bug caused by this excessive complication - in the function
re_putc() in refresh.c. If WIDECHAR was defined, it printed an
Int = wint_t value with %c. Fortunately, that bug only affects
debugging, not production. The fix is contained in the patch.
With WIDECHAR, this doesn't change anything. For the case without
WIDECHAR, i checked that none of the places wants to store values
that might not fit in wint_t.
This only changes internal interfaces; public ones remain unchanged.
2016-02-14 17:49:34 +03:00
|
|
|
ce__isword(wint_t p)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2016-04-11 03:22:48 +03:00
|
|
|
return iswalnum(p) || wcschr(L"*?_-.[]~=", p) != NULL;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__isword():
|
|
|
|
* Return if p is part of a word according to vi
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
From Ingo Schwarze:
As we have seen before, "histedit.h" can never get rid of including
the <wchar.h> header because using the data types defined there is
deeply ingrained in the public interfaces of libedit.
Now POSIX unconditionally requires that <wchar.h> defines the type
wint_t. Consequently, it can be used unconditionally, no matter
whether WIDECHAR is active or not. Consequently, the #define Int
is pointless.
Note that removing it is not gratuitious churn. Auditing for
integer signedness problems is already hard when only fundamental
types like "int" and "unsigned" are involved. It gets very hard
when types come into the picture that have platform-dependent
signedness, like "char" and "wint_t". Adding yet another layer
on top, changing both the signedness and the width in a platform-
dependent way, makes auditing yet harder, which IMHO is really
dangerous. Note that while removing the #define, i already found
one bug caused by this excessive complication - in the function
re_putc() in refresh.c. If WIDECHAR was defined, it printed an
Int = wint_t value with %c. Fortunately, that bug only affects
debugging, not production. The fix is contained in the patch.
With WIDECHAR, this doesn't change anything. For the case without
WIDECHAR, i checked that none of the places wants to store values
that might not fit in wint_t.
This only changes internal interfaces; public ones remain unchanged.
2016-02-14 17:49:34 +03:00
|
|
|
cv__isword(wint_t p)
|
2002-10-28 00:41:50 +03:00
|
|
|
{
|
2016-04-09 21:43:17 +03:00
|
|
|
if (iswalnum(p) || p == L'_')
|
2002-11-15 17:32:32 +03:00
|
|
|
return 1;
|
2016-04-09 21:43:17 +03:00
|
|
|
if (iswgraph(p))
|
2002-11-15 17:32:32 +03:00
|
|
|
return 2;
|
|
|
|
return 0;
|
2002-10-28 00:41:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__isWord():
|
|
|
|
* Return if p is part of a big word according to vi
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
From Ingo Schwarze:
As we have seen before, "histedit.h" can never get rid of including
the <wchar.h> header because using the data types defined there is
deeply ingrained in the public interfaces of libedit.
Now POSIX unconditionally requires that <wchar.h> defines the type
wint_t. Consequently, it can be used unconditionally, no matter
whether WIDECHAR is active or not. Consequently, the #define Int
is pointless.
Note that removing it is not gratuitious churn. Auditing for
integer signedness problems is already hard when only fundamental
types like "int" and "unsigned" are involved. It gets very hard
when types come into the picture that have platform-dependent
signedness, like "char" and "wint_t". Adding yet another layer
on top, changing both the signedness and the width in a platform-
dependent way, makes auditing yet harder, which IMHO is really
dangerous. Note that while removing the #define, i already found
one bug caused by this excessive complication - in the function
re_putc() in refresh.c. If WIDECHAR was defined, it printed an
Int = wint_t value with %c. Fortunately, that bug only affects
debugging, not production. The fix is contained in the patch.
With WIDECHAR, this doesn't change anything. For the case without
WIDECHAR, i checked that none of the places wants to store values
that might not fit in wint_t.
This only changes internal interfaces; public ones remain unchanged.
2016-02-14 17:49:34 +03:00
|
|
|
cv__isWord(wint_t p)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2016-04-09 21:43:17 +03:00
|
|
|
return !iswspace(p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c__prev_word():
|
|
|
|
* Find the previous word
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private wchar_t *
|
2016-04-11 03:50:13 +03:00
|
|
|
c__prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
|
|
|
|
|
|
|
while (n--) {
|
2009-12-31 01:37:40 +03:00
|
|
|
while ((p >= low) && !(*wtest)(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
2009-12-31 01:37:40 +03:00
|
|
|
while ((p >= low) && (*wtest)(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cp now points to one character before the word */
|
|
|
|
p++;
|
|
|
|
if (p < low)
|
|
|
|
p = low;
|
|
|
|
/* cp now points where we want it */
|
2011-07-29 19:16:33 +04:00
|
|
|
return p;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c__next_word():
|
|
|
|
* Find the next word
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private wchar_t *
|
2016-04-11 03:50:13 +03:00
|
|
|
c__next_word(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
2009-12-31 01:37:40 +03:00
|
|
|
while ((p < high) && !(*wtest)(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
2009-12-31 01:37:40 +03:00
|
|
|
while ((p < high) && (*wtest)(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p > high)
|
|
|
|
p = high;
|
|
|
|
/* p now points where we want it */
|
2011-07-29 19:16:33 +04:00
|
|
|
return p;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cv_next_word():
|
|
|
|
* Find the next word vi style
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private wchar_t *
|
2016-04-11 03:50:13 +03:00
|
|
|
cv_next_word(EditLine *el, wchar_t *p, wchar_t *high, int n,
|
|
|
|
int (*wtest)(wint_t))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int test;
|
|
|
|
|
|
|
|
while (n--) {
|
2009-12-31 01:37:40 +03:00
|
|
|
test = (*wtest)(*p);
|
|
|
|
while ((p < high) && (*wtest)(*p) == test)
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
|
|
|
/*
|
|
|
|
* vi historically deletes with cw only the word preserving the
|
|
|
|
* trailing whitespace! This is not what 'w' does..
|
|
|
|
*/
|
2002-10-28 00:41:50 +03:00
|
|
|
if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
|
2016-04-09 21:43:17 +03:00
|
|
|
while ((p < high) && iswspace(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
/* p now points where we want it */
|
|
|
|
if (p > high)
|
2011-07-29 19:16:33 +04:00
|
|
|
return high;
|
2000-09-05 02:06:28 +04:00
|
|
|
else
|
2011-07-29 19:16:33 +04:00
|
|
|
return p;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv_prev_word():
|
|
|
|
* Find the previous word vi style
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private wchar_t *
|
2016-04-11 03:50:13 +03:00
|
|
|
cv_prev_word(wchar_t *p, wchar_t *low, int n, int (*wtest)(wint_t))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int test;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
p--;
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
2016-04-09 21:43:17 +03:00
|
|
|
while ((p > low) && iswspace(*p))
|
2002-10-28 00:41:50 +03:00
|
|
|
p--;
|
2009-12-31 01:37:40 +03:00
|
|
|
test = (*wtest)(*p);
|
|
|
|
while ((p >= low) && (*wtest)(*p) == test)
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
p++;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
/* p now points where we want it */
|
|
|
|
if (p < low)
|
2011-07-29 19:16:33 +04:00
|
|
|
return low;
|
2000-09-05 02:06:28 +04:00
|
|
|
else
|
2011-07-29 19:16:33 +04:00
|
|
|
return p;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv_delfini():
|
|
|
|
* Finish vi delete action
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2000-09-05 02:06:28 +04:00
|
|
|
cv_delfini(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int size;
|
2002-11-15 17:32:32 +03:00
|
|
|
int action = el->el_chared.c_vcmd.action;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2002-11-15 17:32:32 +03:00
|
|
|
if (action & INSERT)
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_map.current = el->el_map.key;
|
|
|
|
|
|
|
|
if (el->el_chared.c_vcmd.pos == 0)
|
2002-11-15 17:32:32 +03:00
|
|
|
/* sanity */
|
2000-09-05 02:06:28 +04:00
|
|
|
return;
|
|
|
|
|
2009-02-16 00:55:23 +03:00
|
|
|
size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
|
2002-11-15 17:32:32 +03:00
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_line.cursor = el->el_chared.c_vcmd.pos;
|
2002-11-15 17:32:32 +03:00
|
|
|
if (action & YANK) {
|
|
|
|
if (size > 0)
|
|
|
|
cv_yank(el, el->el_line.cursor, size);
|
|
|
|
else
|
|
|
|
cv_yank(el, el->el_line.cursor + size, -size);
|
2000-09-05 02:06:28 +04:00
|
|
|
} else {
|
2002-11-15 17:32:32 +03:00
|
|
|
if (size > 0) {
|
|
|
|
c_delafter(el, size);
|
|
|
|
re_refresh_cursor(el);
|
|
|
|
} else {
|
|
|
|
c_delbefore(el, -size);
|
|
|
|
el->el_line.cursor += size;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__endword():
|
|
|
|
* Go to the end of this word according to vi
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private wchar_t *
|
2016-04-11 03:50:13 +03:00
|
|
|
cv__endword(wchar_t *p, wchar_t *high, int n, int (*wtest)(wint_t))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-10-28 00:41:50 +03:00
|
|
|
int test;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
2016-04-09 21:43:17 +03:00
|
|
|
while ((p < high) && iswspace(*p))
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
test = (*wtest)(*p);
|
|
|
|
while ((p < high) && (*wtest)(*p) == test)
|
2002-10-28 00:41:50 +03:00
|
|
|
p++;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
|
|
|
p--;
|
2011-07-29 19:16:33 +04:00
|
|
|
return p;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ch_init():
|
|
|
|
* Initialize the character editor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2000-09-05 02:06:28 +04:00
|
|
|
ch_init(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2019-07-23 13:18:52 +03:00
|
|
|
el->el_line.buffer = el_calloc(EL_BUFSIZ,
|
2009-12-31 01:37:40 +03:00
|
|
|
sizeof(*el->el_line.buffer));
|
2001-01-04 18:56:31 +03:00
|
|
|
if (el->el_line.buffer == NULL)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2001-01-04 18:56:31 +03:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2019-07-23 13:18:52 +03:00
|
|
|
el->el_chared.c_undo.buf = el_calloc(EL_BUFSIZ,
|
2009-12-31 01:37:40 +03:00
|
|
|
sizeof(*el->el_chared.c_undo.buf));
|
2001-01-04 18:56:31 +03:00
|
|
|
if (el->el_chared.c_undo.buf == NULL)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_chared.c_undo.len = -1;
|
|
|
|
el->el_chared.c_undo.cursor = 0;
|
2019-07-23 13:18:52 +03:00
|
|
|
el->el_chared.c_redo.buf = el_calloc(EL_BUFSIZ,
|
2009-12-31 01:37:40 +03:00
|
|
|
sizeof(*el->el_chared.c_redo.buf));
|
2002-11-15 17:32:32 +03:00
|
|
|
if (el->el_chared.c_redo.buf == NULL)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
|
|
|
|
el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
|
|
|
|
el->el_chared.c_redo.cmd = ED_UNASSIGNED;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
|
|
|
el->el_chared.c_vcmd.pos = el->el_line.buffer;
|
|
|
|
|
2019-07-23 13:18:52 +03:00
|
|
|
el->el_chared.c_kill.buf = el_calloc(EL_BUFSIZ,
|
2009-12-31 01:37:40 +03:00
|
|
|
sizeof(*el->el_chared.c_kill.buf));
|
2001-01-04 18:56:31 +03:00
|
|
|
if (el->el_chared.c_kill.buf == NULL)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer;
|
|
|
|
el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
|
2010-08-28 19:44:59 +04:00
|
|
|
el->el_chared.c_resizefun = NULL;
|
|
|
|
el->el_chared.c_resizearg = NULL;
|
2014-06-18 22:12:28 +04:00
|
|
|
el->el_chared.c_aliasfun = NULL;
|
|
|
|
el->el_chared.c_aliasarg = NULL;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
el->el_map.current = el->el_map.key;
|
|
|
|
|
|
|
|
el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
|
|
|
|
el->el_state.doingarg = 0;
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
el->el_state.argument = 1;
|
|
|
|
el->el_state.lastcmd = ED_UNASSIGNED;
|
|
|
|
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ch_reset():
|
|
|
|
* Reset the character editor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
Stop the read module from poking the el_chared.c_macro data structure
currently belonging to the chared module. The read module does so
from three of its functions, while no other module uses the macro
data, not even the chared module itself. That's quite logical
because macros are a feature of input handling, all of which is
done by the read module, and none by the chared module. So move
the data into the read modules's own opaque data structure, struct
el_read_t.
That simplifies internal interfaces in several respects: The
semi-public chared.h has one fewer struct, one fewer #define, and
one fewer member in struct el_chared_t; all three move to one single
C file, read.c, and are now module-local. And the internal interface
function ch_reset() needs one fewer argument, making the code of many
functions in various modules more readable.
The price is one additional internal interface function, read_end(),
10 lines long including comments, called publicly from exactly one
place: el_end() in el.c. That's hardly an increase in complexity
since most other modules already have their *_end() function, read.c
was the odd one out not having one.
From Ingo Schwarze
2016-05-22 22:44:26 +03:00
|
|
|
ch_reset(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_chared.c_undo.len = -1;
|
|
|
|
el->el_chared.c_undo.cursor = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
|
|
|
el->el_chared.c_vcmd.pos = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_map.current = el->el_map.key;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
|
|
|
|
el->el_state.doingarg = 0;
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
el->el_state.argument = 1;
|
|
|
|
el->el_state.lastcmd = ED_UNASSIGNED;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2005-08-08 05:41:30 +04:00
|
|
|
el->el_history.eventno = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2001-01-10 10:45:41 +03:00
|
|
|
/* ch_enlargebufs():
|
|
|
|
* Enlarge line buffer to be able to hold twice as much characters.
|
|
|
|
* Returns 1 if successful, 0 if not.
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2009-02-16 00:55:23 +03:00
|
|
|
ch_enlargebufs(EditLine *el, size_t addlen)
|
2001-01-10 10:45:41 +03:00
|
|
|
{
|
2001-04-13 05:04:19 +04:00
|
|
|
size_t sz, newsz;
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *newbuffer, *oldbuf, *oldkbuf;
|
2001-04-13 05:04:19 +04:00
|
|
|
|
2011-08-16 20:25:15 +04:00
|
|
|
sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
|
2001-04-13 05:04:19 +04:00
|
|
|
newsz = sz * 2;
|
|
|
|
/*
|
|
|
|
* If newly required length is longer than current buffer, we need
|
|
|
|
* to make the buffer big enough to hold both old and new stuff.
|
|
|
|
*/
|
|
|
|
if (addlen > sz) {
|
|
|
|
while(newsz - sz < addlen)
|
|
|
|
newsz *= 2;
|
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/*
|
|
|
|
* Reallocate line buffer.
|
|
|
|
*/
|
2009-12-31 01:37:40 +03:00
|
|
|
newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
|
2001-04-13 05:04:19 +04:00
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* zero the newly added memory, leave old data in */
|
2009-12-31 01:37:40 +03:00
|
|
|
(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
|
2016-02-17 22:47:49 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
oldbuf = el->el_line.buffer;
|
|
|
|
|
|
|
|
el->el_line.buffer = newbuffer;
|
|
|
|
el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
|
|
|
|
el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
|
2002-10-28 00:41:50 +03:00
|
|
|
/* don't set new size until all buffers are enlarged */
|
|
|
|
el->el_line.limit = &newbuffer[sz - EL_LEAVE];
|
2001-04-13 05:04:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reallocate kill buffer.
|
|
|
|
*/
|
2011-07-29 00:50:55 +04:00
|
|
|
newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
|
|
|
|
sizeof(*newbuffer));
|
2001-04-13 05:04:19 +04:00
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* zero the newly added memory, leave old data in */
|
2009-12-31 01:37:40 +03:00
|
|
|
(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
|
2001-04-13 05:04:19 +04:00
|
|
|
|
|
|
|
oldkbuf = el->el_chared.c_kill.buf;
|
|
|
|
|
|
|
|
el->el_chared.c_kill.buf = newbuffer;
|
|
|
|
el->el_chared.c_kill.last = newbuffer +
|
2001-01-10 10:45:41 +03:00
|
|
|
(el->el_chared.c_kill.last - oldkbuf);
|
2001-04-13 05:04:19 +04:00
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer +
|
2001-01-10 10:45:41 +03:00
|
|
|
(el->el_chared.c_kill.mark - oldbuf);
|
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/*
|
|
|
|
* Reallocate undo buffer.
|
|
|
|
*/
|
2009-12-31 01:37:40 +03:00
|
|
|
newbuffer = el_realloc(el->el_chared.c_undo.buf,
|
|
|
|
newsz * sizeof(*newbuffer));
|
2001-04-13 05:04:19 +04:00
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/* zero the newly added memory, leave old data in */
|
2009-12-31 01:37:40 +03:00
|
|
|
(void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
|
2001-04-13 05:04:19 +04:00
|
|
|
el->el_chared.c_undo.buf = newbuffer;
|
2002-10-28 00:41:50 +03:00
|
|
|
|
2009-12-31 01:37:40 +03:00
|
|
|
newbuffer = el_realloc(el->el_chared.c_redo.buf,
|
|
|
|
newsz * sizeof(*newbuffer));
|
2002-10-28 00:41:50 +03:00
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_chared.c_redo.pos = newbuffer +
|
|
|
|
(el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
|
|
|
|
el->el_chared.c_redo.lim = newbuffer +
|
|
|
|
(el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
|
|
|
|
el->el_chared.c_redo.buf = newbuffer;
|
2016-02-17 22:47:49 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
if (!hist_enlargebuf(el, sz, newsz))
|
|
|
|
return 0;
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
/* Safe to set enlarged buffer size */
|
2003-11-02 23:08:41 +03:00
|
|
|
el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
|
2010-08-28 19:44:59 +04:00
|
|
|
if (el->el_chared.c_resizefun)
|
|
|
|
(*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
|
2001-04-13 05:04:19 +04:00
|
|
|
return 1;
|
2001-01-10 10:45:41 +03:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
/* ch_end():
|
|
|
|
* Free the data structures used by the editor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private void
|
2000-09-05 02:06:28 +04:00
|
|
|
ch_end(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2011-07-29 00:50:55 +04:00
|
|
|
el_free(el->el_line.buffer);
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.buffer = NULL;
|
|
|
|
el->el_line.limit = NULL;
|
2011-07-29 00:50:55 +04:00
|
|
|
el_free(el->el_chared.c_undo.buf);
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_undo.buf = NULL;
|
2011-07-29 00:50:55 +04:00
|
|
|
el_free(el->el_chared.c_redo.buf);
|
2002-11-15 17:32:32 +03:00
|
|
|
el->el_chared.c_redo.buf = NULL;
|
|
|
|
el->el_chared.c_redo.pos = NULL;
|
|
|
|
el->el_chared.c_redo.lim = NULL;
|
|
|
|
el->el_chared.c_redo.cmd = ED_UNASSIGNED;
|
2011-07-29 00:50:55 +04:00
|
|
|
el_free(el->el_chared.c_kill.buf);
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_kill.buf = NULL;
|
Stop the read module from poking the el_chared.c_macro data structure
currently belonging to the chared module. The read module does so
from three of its functions, while no other module uses the macro
data, not even the chared module itself. That's quite logical
because macros are a feature of input handling, all of which is
done by the read module, and none by the chared module. So move
the data into the read modules's own opaque data structure, struct
el_read_t.
That simplifies internal interfaces in several respects: The
semi-public chared.h has one fewer struct, one fewer #define, and
one fewer member in struct el_chared_t; all three move to one single
C file, read.c, and are now module-local. And the internal interface
function ch_reset() needs one fewer argument, making the code of many
functions in various modules more readable.
The price is one additional internal interface function, read_end(),
10 lines long including comments, called publicly from exactly one
place: el_end() in el.c. That's hardly an increase in complexity
since most other modules already have their *_end() function, read.c
was the odd one out not having one.
From Ingo Schwarze
2016-05-22 22:44:26 +03:00
|
|
|
ch_reset(el);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_insertstr():
|
2017-10-11 09:49:03 +03:00
|
|
|
* Insert string at cursor
|
1994-05-06 10:01:42 +04:00
|
|
|
*/
|
2016-04-11 21:56:31 +03:00
|
|
|
int
|
2016-04-11 03:50:13 +03:00
|
|
|
el_winsertstr(EditLine *el, const wchar_t *s)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2001-05-17 05:02:17 +04:00
|
|
|
size_t len;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2016-04-11 03:22:48 +03:00
|
|
|
if (s == NULL || (len = wcslen(s)) == 0)
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2001-01-10 10:45:41 +03:00
|
|
|
if (el->el_line.lastchar + len >= el->el_line.limit) {
|
|
|
|
if (!ch_enlargebufs(el, len))
|
2011-07-29 19:16:33 +04:00
|
|
|
return -1;
|
2001-01-10 10:45:41 +03:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2001-05-17 05:02:17 +04:00
|
|
|
c_insert(el, (int)len);
|
2000-09-05 02:06:28 +04:00
|
|
|
while (*s)
|
|
|
|
*el->el_line.cursor++ = *s++;
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_deletestr():
|
|
|
|
* Delete num characters before the cursor
|
|
|
|
*/
|
2016-04-11 21:56:31 +03:00
|
|
|
void
|
2000-09-05 02:06:28 +04:00
|
|
|
el_deletestr(EditLine *el, int n)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
if (n <= 0)
|
|
|
|
return;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor < &el->el_line.buffer[n])
|
|
|
|
return;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delbefore(el, n); /* delete before dot */
|
|
|
|
el->el_line.cursor -= n;
|
|
|
|
if (el->el_line.cursor < el->el_line.buffer)
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 21:48:29 +04:00
|
|
|
/* el_cursor():
|
|
|
|
* Move the cursor to the left or the right of the current position
|
|
|
|
*/
|
2016-04-11 21:56:31 +03:00
|
|
|
int
|
2013-07-12 21:48:29 +04:00
|
|
|
el_cursor(EditLine *el, int n)
|
|
|
|
{
|
|
|
|
if (n == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
el->el_line.cursor += n;
|
|
|
|
|
|
|
|
if (el->el_line.cursor < el->el_line.buffer)
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
if (el->el_line.cursor > el->el_line.lastchar)
|
|
|
|
el->el_line.cursor = el->el_line.lastchar;
|
|
|
|
out:
|
2013-07-13 02:39:50 +04:00
|
|
|
return (int)(el->el_line.cursor - el->el_line.buffer);
|
2013-07-12 21:48:29 +04:00
|
|
|
}
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* c_gets():
|
|
|
|
* Get a string
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2016-04-11 03:50:13 +03:00
|
|
|
c_gets(EditLine *el, wchar_t *buf, const wchar_t *prompt)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2009-02-16 00:55:23 +03:00
|
|
|
ssize_t len;
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *cp = el->el_line.buffer, ch;
|
2002-11-20 19:50:08 +03:00
|
|
|
|
|
|
|
if (prompt) {
|
2016-04-11 03:22:48 +03:00
|
|
|
len = (ssize_t)wcslen(prompt);
|
2011-08-16 20:25:15 +04:00
|
|
|
(void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
|
2002-11-20 19:50:08 +03:00
|
|
|
cp += len;
|
|
|
|
}
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
el->el_line.cursor = cp;
|
|
|
|
*cp = ' ';
|
|
|
|
el->el_line.lastchar = cp + 1;
|
|
|
|
re_refresh(el);
|
|
|
|
|
2016-04-11 03:50:13 +03:00
|
|
|
if (el_wgetc(el, &ch) != 1) {
|
2002-11-20 19:50:08 +03:00
|
|
|
ed_end_of_file(el, 0);
|
|
|
|
len = -1;
|
|
|
|
break;
|
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
switch (ch) {
|
2002-11-20 19:50:08 +03:00
|
|
|
|
2016-02-24 17:25:38 +03:00
|
|
|
case L'\b': /* Delete and backspace */
|
2000-09-05 02:06:28 +04:00
|
|
|
case 0177:
|
2009-02-16 00:55:23 +03:00
|
|
|
if (len == 0) {
|
2002-11-20 19:50:08 +03:00
|
|
|
len = -1;
|
|
|
|
break;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
A very simple, non-intrusive patch to fix a segfault (and a
functional error) in c_gets(), file chared.c.
Run any program using libedit in the default way. At the el_[w]gets()
prompt, invoke ed-command (for example, in emacs mode, press the
escape key, then type the letter 'x'). You should see a ": " prompt.
Type the letter 'x' again. Now press the backspace key a few times,
looking at the screen after each key press:
- The 1st BS deletes the 'x'.
- The 2nd BS deletes the blank after the prompt.
- The 3rd BS deletes the colon of the prompt.
- The 4th BS moves the cursor up one line.
- The 5th BS gives me "Segmentation fault (core dumped)".
Depending on your platform, it might take a few more or a few less
backspaces for the buffer underrun to trigger the segfault, but
you should be able to hit it sooner or later no matter what.
Run the same program again, connect again and invoke ed-command again.
Now type: 'b', backspace, 'i', backspace, 'n', backspace, 'd', enter.
The "bind" command gets executed, even though you deleted what you
typed before hitting enter.
From Ingo Schwatze.
2016-02-24 17:29:21 +03:00
|
|
|
len--;
|
2002-11-20 19:50:08 +03:00
|
|
|
cp--;
|
|
|
|
continue;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
case 0033: /* ESC */
|
2016-02-24 17:25:38 +03:00
|
|
|
case L'\r': /* Newline */
|
|
|
|
case L'\n':
|
2002-11-20 19:50:08 +03:00
|
|
|
buf[len] = ch;
|
2000-09-05 02:06:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-07-30 03:44:44 +04:00
|
|
|
if (len >= (ssize_t)(EL_BUFSIZ - 16))
|
2011-07-28 05:05:20 +04:00
|
|
|
terminal_beep(el);
|
2000-09-05 02:06:28 +04:00
|
|
|
else {
|
|
|
|
buf[len++] = ch;
|
2002-11-20 19:50:08 +03:00
|
|
|
*cp++ = ch;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2002-11-20 19:50:08 +03:00
|
|
|
continue;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
2002-11-20 19:50:08 +03:00
|
|
|
break;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2002-11-20 19:50:08 +03:00
|
|
|
|
|
|
|
el->el_line.buffer[0] = '\0';
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
2009-02-16 00:55:23 +03:00
|
|
|
return (int)len;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c_hpos():
|
|
|
|
* Return the current horizontal position of the cursor
|
|
|
|
*/
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2000-09-05 02:06:28 +04:00
|
|
|
c_hpos(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2016-04-11 03:50:13 +03:00
|
|
|
wchar_t *ptr;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find how many characters till the beginning of this line.
|
|
|
|
*/
|
|
|
|
if (el->el_line.cursor == el->el_line.buffer)
|
2011-07-29 19:16:33 +04:00
|
|
|
return 0;
|
2000-09-05 02:06:28 +04:00
|
|
|
else {
|
|
|
|
for (ptr = el->el_line.cursor - 1;
|
|
|
|
ptr >= el->el_line.buffer && *ptr != '\n';
|
|
|
|
ptr--)
|
|
|
|
continue;
|
2009-02-16 00:55:23 +03:00
|
|
|
return (int)(el->el_line.cursor - ptr - 1);
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2010-08-28 19:44:59 +04:00
|
|
|
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2010-08-28 19:44:59 +04:00
|
|
|
ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
|
|
|
|
{
|
|
|
|
el->el_chared.c_resizefun = f;
|
|
|
|
el->el_chared.c_resizearg = a;
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-18 22:12:28 +04:00
|
|
|
|
2016-05-10 00:46:56 +03:00
|
|
|
libedit_private int
|
2014-06-18 22:12:28 +04:00
|
|
|
ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
|
|
|
|
{
|
|
|
|
el->el_chared.c_aliasfun = f;
|
|
|
|
el->el_chared.c_aliasarg = a;
|
|
|
|
return 0;
|
|
|
|
}
|