/* ring.c * Part of the Readline module for Tap. This is the implementation * of the kill ring. */ /* Copyright (C) 1999 Jonathan duSaint * * This file is part of Tap, an interactive program for reading and * writing data to the serial port to facilitate the reverse * engineering of communication protocols. * * Tap is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Tap is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public * License for more details. * * The GNU General Public License is generally kept in a file called * COPYING or LICENSE. If you do not have a copy of the license, write * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "readline.h" /* If the requested entry in the kill ring is NULL, * this is returned instead. */ char zero_length_string[] = ""; /* Returns text previously yanked, with the most recent * having an index of 1. If the entry is NULL, returns * a pointer to a zero length string. Memory returned by * this call should never be freed */ char * get_ring_entry (int which) { struct kill_ring *current; int k; which--; current = the_kill_ring; for (k = 0; k < which; k++) current = current->prev; if (current->text == NULL) return (zero_length_string); return (current->text); } /* Add TEXT to the kill ring and advance the ring. The * memory passed to this function should never be freed. */ void add_ring_entry (char *text) { the_kill_ring = the_kill_ring->next; if (the_kill_ring->text != NULL) xfree (the_kill_ring->text); the_kill_ring->text = text; the_kill_ring->length = strlen (text); } /* Copy text from START to FINISH to a new block * and add block to kill ring */ void kill_to_ring (int start, int finish) { char *text; text = xmalloc (finish - start + 1); strncpy (text, current_line + start, finish - start); text[finish - start] = 0; add_ring_entry (text); } /* Initialize the kill ring. */ void ring_init (void) { int k; struct kill_ring *current; /* allocate first separately */ the_kill_ring = xmalloc (sizeof (struct kill_ring)); the_kill_ring->length = 0; the_kill_ring->text = NULL; current = the_kill_ring; for (k = 0; k < DEFAULT_KILL_RING_SIZE - 2; k++) { current->next = xmalloc (sizeof (struct kill_ring)); current->next->length = 0; current->next->text = NULL; current->next->prev = current; current = current->next; } /* complete the ring */ current->next = the_kill_ring; the_kill_ring->prev = current; }