From cc06049046d8395f3ac70fd38fbbafcba2f9d4da Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 4 Jun 2014 13:51:33 +0800 Subject: [PATCH] $kill returns full copy of kill buffer. --- line.c | 45 +++++++++++++++++++++++++++++++++------------ tststr.cmd | 13 ++++++++++++- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/line.c b/line.c index e0fa6ed..9bfcc83 100644 --- a/line.c +++ b/line.c @@ -49,26 +49,38 @@ struct kill { static struct kill *kbufp = NULL ; /* current kill buffer chunk pointer */ static struct kill *kbufh = NULL ; /* kill buffer header pointer */ static int kused = KBLOCK ; /* # of bytes used in kill buffer */ +static int klen ; /* length of kill buffer content */ +static char *value = NULL ; /* temp buffer for value */ /* * return some of the contents of the kill buffer */ -char *getkill( void) -{ - int size; /* max number of chars to return */ - static char value[NSTRING]; /* temp buffer for value */ +char *getkill( void) { + struct kill *kp ; + char *cp ; if (kbufh == NULL) /* no kill buffer....just a null string */ - value[0] = 0; - else { - /* copy in the contents... */ - if( kbufh == kbufp && kused < NSTRING) - size = kused; + return "" ; + + if( value != NULL) + free( value) ; + + value = (char *) malloc( klen + 1) ; + cp = value ; + for( kp = kbufh ; kp != NULL ; kp = kp->d_next) { + int size ; + + if( kp->d_next != NULL) + size = KBLOCK ; else - size = NSTRING - 1; - strncpy(value, kbufh->d_chunk, size); + size = kused ; + + memcpy( cp, kp->d_chunk, size) ; + cp += size ; } + + *cp = 0 ; /* and return the constructed value */ return value; @@ -712,6 +724,11 @@ void kdelete(void) /* and reset all the kill buffer pointers */ kbufh = kbufp = NULL; kused = KBLOCK; + klen = 0 ; + if( value != NULL) { + free( value) ; + value = NULL ; + } } } @@ -729,8 +746,11 @@ int kinsert(int c) if (kused >= KBLOCK) { if ((nchunk = (struct kill *)malloc(sizeof(struct kill))) == NULL) return FALSE; - if (kbufh == NULL) /* set head ptr if first time */ + if( kbufh == NULL) { /* set head ptr if first time */ kbufh = nchunk; + klen = 0 ; + } + if (kbufp != NULL) /* point the current to this new one */ kbufp->d_next = nchunk; kbufp = nchunk; @@ -740,6 +760,7 @@ int kinsert(int c) /* and now insert the character */ kbufp->d_chunk[kused++] = c; + klen += 1 ; return TRUE; } diff --git a/tststr.cmd b/tststr.cmd index 9089987..1463032 100644 --- a/tststr.cmd +++ b/tststr.cmd @@ -3,6 +3,8 @@ insert-string &env PATH newline insert-string $PATH newline +insert-string &cat $PATH $PATH +newline set %mypath $PATH insert-string %mypath newline @@ -11,6 +13,12 @@ newline ; Insert string with escaped characters insert-string "hello, world~n" newline +; Insert 512 long token [will be truncated to sizeof istring buffer - 2 (510)] +insert-string 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF +newline +; Insert 512 long quoted string [will be truncated to sizeof istring buffer - 3 (509)] +insert-string "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF" +newline ; Insert long quoted string [will be truncated to NSTRING - 2 (126)] insert-string "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" next-line @@ -55,4 +63,7 @@ yank insert-string $kill save-file beginning-of-file - +set-mark +end-of-file +copy-region +insert-string $kill