aboutsummaryrefslogtreecommitdiff
path: root/tap/readline/ring.c
blob: 474cc2731fcae7c7005ce17510081b5f9fdc97f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* 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;
}