~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to cmd-line-utils/libedit/hist.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: hist.c,v 1.15 2003/11/01 23:36:39 christos Exp $       */
 
2
 
 
3
/*-
 
4
 * Copyright (c) 1992, 1993
 
5
 *      The Regents of the University of California.  All rights reserved.
 
6
 *
 
7
 * This code is derived from software contributed to Berkeley by
 
8
 * Christos Zoulas of Cornell University.
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions
 
12
 * are met:
 
13
 * 1. Redistributions of source code must retain the above copyright
 
14
 *    notice, this list of conditions and the following disclaimer.
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *    notice, this list of conditions and the following disclaimer in the
 
17
 *    documentation and/or other materials provided with the distribution.
 
18
 * 3. Neither the name of the University nor the names of its contributors
 
19
 *    may be used to endorse or promote products derived from this software
 
20
 *    without specific prior written permission.
 
21
 *
 
22
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
25
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
28
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
29
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
31
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
32
 * SUCH DAMAGE.
 
33
 */
 
34
 
 
35
#include "config.h"
 
36
#if !defined(lint) && !defined(SCCSID)
 
37
#if 0
 
38
static char sccsid[] = "@(#)hist.c      8.1 (Berkeley) 6/4/93";
 
39
#else
 
40
#endif
 
41
#endif /* not lint && not SCCSID */
 
42
 
 
43
/*
 
44
 * hist.c: History access functions
 
45
 */
 
46
#include <stdlib.h>
 
47
#include "el.h"
 
48
 
 
49
/* hist_init():
 
50
 *      Initialization function.
 
51
 */
 
52
protected int
 
53
hist_init(EditLine *el)
 
54
{
 
55
 
 
56
        el->el_history.fun = NULL;
 
57
        el->el_history.ref = NULL;
 
58
        el->el_history.buf = (char *) el_malloc(EL_BUFSIZ);
 
59
        el->el_history.sz  = EL_BUFSIZ;
 
60
        if (el->el_history.buf == NULL)
 
61
                return (-1);
 
62
        el->el_history.last = el->el_history.buf;
 
63
        return (0);
 
64
}
 
65
 
 
66
 
 
67
/* hist_end():
 
68
 *      clean up history;
 
69
 */
 
70
protected void
 
71
hist_end(EditLine *el)
 
72
{
 
73
 
 
74
        el_free((ptr_t) el->el_history.buf);
 
75
        el->el_history.buf = NULL;
 
76
}
 
77
 
 
78
 
 
79
/* hist_set():
 
80
 *      Set new history interface
 
81
 */
 
82
protected int
 
83
hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr)
 
84
{
 
85
 
 
86
        el->el_history.ref = ptr;
 
87
        el->el_history.fun = fun;
 
88
        return (0);
 
89
}
 
90
 
 
91
 
 
92
/* hist_get():
 
93
 *      Get a history line and update it in the buffer.
 
94
 *      eventno tells us the event to get.
 
95
 */
 
96
protected el_action_t
 
97
hist_get(EditLine *el)
 
98
{
 
99
        const char *hp;
 
100
        int h;
 
101
 
 
102
        if (el->el_history.eventno == 0) {      /* if really the current line */
 
103
                (void) strncpy(el->el_line.buffer, el->el_history.buf,
 
104
                    el->el_history.sz);
 
105
                el->el_line.lastchar = el->el_line.buffer +
 
106
                    (el->el_history.last - el->el_history.buf);
 
107
 
 
108
#ifdef KSHVI
 
109
                if (el->el_map.type == MAP_VI)
 
110
                        el->el_line.cursor = el->el_line.buffer;
 
111
                else
 
112
#endif /* KSHVI */
 
113
                        el->el_line.cursor = el->el_line.lastchar;
 
114
 
 
115
                return (CC_REFRESH);
 
116
        }
 
117
        if (el->el_history.ref == NULL)
 
118
                return (CC_ERROR);
 
119
 
 
120
        hp = HIST_FIRST(el);
 
121
 
 
122
        if (hp == NULL)
 
123
                return (CC_ERROR);
 
124
 
 
125
        for (h = 1; h < el->el_history.eventno; h++)
 
126
                if ((hp = HIST_NEXT(el)) == NULL) {
 
127
                        el->el_history.eventno = h;
 
128
                        return (CC_ERROR);
 
129
                }
 
130
        (void) strlcpy(el->el_line.buffer, hp,
 
131
                        (size_t)(el->el_line.limit - el->el_line.buffer));
 
132
        el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
 
133
 
 
134
        if (el->el_line.lastchar > el->el_line.buffer
 
135
            && el->el_line.lastchar[-1] == '\n')
 
136
                el->el_line.lastchar--;
 
137
        if (el->el_line.lastchar > el->el_line.buffer
 
138
            && el->el_line.lastchar[-1] == ' ')
 
139
                el->el_line.lastchar--;
 
140
#ifdef KSHVI
 
141
        if (el->el_map.type == MAP_VI)
 
142
                el->el_line.cursor = el->el_line.buffer;
 
143
        else
 
144
#endif /* KSHVI */
 
145
                el->el_line.cursor = el->el_line.lastchar;
 
146
 
 
147
        return (CC_REFRESH);
 
148
}
 
149
 
 
150
 
 
151
/* hist_command()
 
152
 *      process a history command
 
153
 */
 
154
protected int
 
155
hist_command(EditLine *el, int argc, const char **argv)
 
156
{
 
157
        const char *str;
 
158
        int num;
 
159
        HistEvent ev;
 
160
 
 
161
        if (el->el_history.ref == NULL)
 
162
                return (-1);
 
163
 
 
164
        if (argc == 1 || strcmp(argv[1], "list") == 0) {
 
165
                 /* List history entries */
 
166
 
 
167
                for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
 
168
                        (void) fprintf(el->el_outfile, "%d %s",
 
169
                            el->el_history.ev.num, str);
 
170
                return (0);
 
171
        }
 
172
 
 
173
        if (argc != 3)
 
174
                return (-1);
 
175
 
 
176
        num = (int)strtol(argv[2], NULL, 0);
 
177
 
 
178
        if (strcmp(argv[1], "size") == 0)
 
179
                return history(el->el_history.ref, &ev, H_SETSIZE, num);
 
180
 
 
181
        if (strcmp(argv[1], "unique") == 0)
 
182
                return history(el->el_history.ref, &ev, H_SETUNIQUE, num);
 
183
 
 
184
        return -1;
 
185
}
 
186
 
 
187
/* hist_enlargebuf()
 
188
 *      Enlarge history buffer to specified value. Called from el_enlargebufs().
 
189
 *      Return 0 for failure, 1 for success.
 
190
 */
 
191
protected int
 
192
/*ARGSUSED*/
 
193
hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
 
194
{
 
195
        char *newbuf;
 
196
 
 
197
        newbuf = realloc(el->el_history.buf, newsz);
 
198
        if (!newbuf)
 
199
                return 0;
 
200
 
 
201
        (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
 
202
 
 
203
        el->el_history.last = newbuf +
 
204
                                (el->el_history.last - el->el_history.buf);
 
205
        el->el_history.buf = newbuf;
 
206
        el->el_history.sz  = newsz;
 
207
 
 
208
        return 1;
 
209
}