Home | History | Annotate | Line # | Download | only in libedit
hist.c revision 1.30
      1 /*	$NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 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 __RCSID("$NetBSD: hist.c,v 1.30 2016/11/07 15:30:18 christos Exp $");
     41 #endif
     42 #endif /* not lint && not SCCSID */
     43 
     44 /*
     45  * hist.c: History access functions
     46  */
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <vis.h>
     50 
     51 #include "el.h"
     52 
     53 /* hist_init():
     54  *	Initialization function.
     55  */
     56 libedit_private int
     57 hist_init(EditLine *el)
     58 {
     59 
     60 	el->el_history.fun = NULL;
     61 	el->el_history.ref = NULL;
     62 	el->el_history.buf = el_malloc(EL_BUFSIZ * sizeof(*el->el_history.buf));
     63 	el->el_history.sz  = EL_BUFSIZ;
     64 	if (el->el_history.buf == NULL)
     65 		return -1;
     66 	el->el_history.last = el->el_history.buf;
     67 	return 0;
     68 }
     69 
     70 
     71 /* hist_end():
     72  *	clean up history;
     73  */
     74 libedit_private void
     75 hist_end(EditLine *el)
     76 {
     77 
     78 	el_free(el->el_history.buf);
     79 	el->el_history.buf = NULL;
     80 }
     81 
     82 
     83 /* hist_set():
     84  *	Set new history interface
     85  */
     86 libedit_private int
     87 hist_set(EditLine *el, hist_fun_t fun, void *ptr)
     88 {
     89 
     90 	el->el_history.ref = ptr;
     91 	el->el_history.fun = fun;
     92 	return 0;
     93 }
     94 
     95 
     96 /* hist_get():
     97  *	Get a history line and update it in the buffer.
     98  *	eventno tells us the event to get.
     99  */
    100 libedit_private el_action_t
    101 hist_get(EditLine *el)
    102 {
    103 	const wchar_t *hp;
    104 	int h;
    105 
    106 	if (el->el_history.eventno == 0) {	/* if really the current line */
    107 		(void) wcsncpy(el->el_line.buffer, el->el_history.buf,
    108 		    el->el_history.sz);
    109 		el->el_line.lastchar = el->el_line.buffer +
    110 		    (el->el_history.last - el->el_history.buf);
    111 
    112 #ifdef KSHVI
    113 		if (el->el_map.type == MAP_VI)
    114 			el->el_line.cursor = el->el_line.buffer;
    115 		else
    116 #endif /* KSHVI */
    117 			el->el_line.cursor = el->el_line.lastchar;
    118 
    119 		return CC_REFRESH;
    120 	}
    121 	if (el->el_history.ref == NULL)
    122 		return CC_ERROR;
    123 
    124 	hp = HIST_FIRST(el);
    125 
    126 	if (hp == NULL)
    127 		return CC_ERROR;
    128 
    129 	for (h = 1; h < el->el_history.eventno; h++)
    130 		if ((hp = HIST_NEXT(el)) == NULL) {
    131 			el->el_history.eventno = h;
    132 			return CC_ERROR;
    133 		}
    134 	(void) wcsncpy(el->el_line.buffer, hp,
    135 			(size_t)(el->el_line.limit - el->el_line.buffer));
    136 	el->el_line.buffer[el->el_line.limit - el->el_line.buffer - 1] = '\0';
    137 	el->el_line.lastchar = el->el_line.buffer + wcslen(el->el_line.buffer);
    138 
    139 	if (el->el_line.lastchar > el->el_line.buffer
    140 	    && el->el_line.lastchar[-1] == '\n')
    141 		el->el_line.lastchar--;
    142 	if (el->el_line.lastchar > el->el_line.buffer
    143 	    && el->el_line.lastchar[-1] == ' ')
    144 		el->el_line.lastchar--;
    145 #ifdef KSHVI
    146 	if (el->el_map.type == MAP_VI)
    147 		el->el_line.cursor = el->el_line.buffer;
    148 	else
    149 #endif /* KSHVI */
    150 		el->el_line.cursor = el->el_line.lastchar;
    151 
    152 	return CC_REFRESH;
    153 }
    154 
    155 
    156 /* hist_command()
    157  *	process a history command
    158  */
    159 libedit_private int
    160 hist_command(EditLine *el, int argc, const wchar_t **argv)
    161 {
    162 	const wchar_t *str;
    163 	int num;
    164 	HistEventW ev;
    165 
    166 	if (el->el_history.ref == NULL)
    167 		return -1;
    168 
    169 	if (argc == 1 || wcscmp(argv[1], L"list") == 0) {
    170 		size_t maxlen = 0;
    171 		char *buf = NULL;
    172 		int hno = 1;
    173 		 /* List history entries */
    174 
    175 		for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) {
    176 			char *ptr =
    177 			    ct_encode_string(str, &el->el_scratch);
    178 			size_t len = strlen(ptr);
    179 			if (len > 0 && ptr[len - 1] == '\n')
    180 				ptr[--len] = '\0';
    181 			len = len * 4 + 1;
    182 			if (len >= maxlen) {
    183 				maxlen = len + 1024;
    184 				char *nbuf = el_realloc(buf, maxlen);
    185 				if (nbuf == NULL) {
    186 					el_free(buf);
    187 					return -1;
    188 				}
    189 				buf = nbuf;
    190 			}
    191 			strvis(buf, ptr, VIS_NL);
    192 			(void) fprintf(el->el_outfile, "%d\t%s\n",
    193 			    hno++, buf);
    194 		}
    195 		el_free(buf);
    196 		return 0;
    197 	}
    198 
    199 	if (argc != 3)
    200 		return -1;
    201 
    202 	num = (int)wcstol(argv[2], NULL, 0);
    203 
    204 	if (wcscmp(argv[1], L"size") == 0)
    205 		return history_w(el->el_history.ref, &ev, H_SETSIZE, num);
    206 
    207 	if (wcscmp(argv[1], L"unique") == 0)
    208 		return history_w(el->el_history.ref, &ev, H_SETUNIQUE, num);
    209 
    210 	return -1;
    211 }
    212 
    213 /* hist_enlargebuf()
    214  *	Enlarge history buffer to specified value. Called from el_enlargebufs().
    215  *	Return 0 for failure, 1 for success.
    216  */
    217 libedit_private int
    218 /*ARGSUSED*/
    219 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz)
    220 {
    221 	wchar_t *newbuf;
    222 
    223 	newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf));
    224 	if (!newbuf)
    225 		return 0;
    226 
    227 	(void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf));
    228 
    229 	el->el_history.last = newbuf +
    230 				(el->el_history.last - el->el_history.buf);
    231 	el->el_history.buf = newbuf;
    232 	el->el_history.sz  = newsz;
    233 
    234 	return 1;
    235 }
    236 
    237 libedit_private wchar_t *
    238 hist_convert(EditLine *el, int fn, void *arg)
    239 {
    240 	HistEventW ev;
    241 	if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1)
    242 		return NULL;
    243 	return ct_decode_string((const char *)(const void *)ev.str,
    244 	    &el->el_scratch);
    245 }
    246