hist.c revision 1573
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Christos Zoulas of Cornell University.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
161573Srgrimes * 3. All advertising materials mentioning features or use of this software
171573Srgrimes *    must display the following acknowledgement:
181573Srgrimes *	This product includes software developed by the University of
191573Srgrimes *	California, Berkeley and its contributors.
201573Srgrimes * 4. Neither the name of the University nor the names of its contributors
211573Srgrimes *    may be used to endorse or promote products derived from this software
221573Srgrimes *    without specific prior written permission.
231573Srgrimes *
241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341573Srgrimes * SUCH DAMAGE.
351573Srgrimes */
361573Srgrimes
371573Srgrimes#if !defined(lint) && !defined(SCCSID)
381573Srgrimesstatic char sccsid[] = "@(#)hist.c	8.1 (Berkeley) 6/4/93";
391573Srgrimes#endif /* not lint && not SCCSID */
401573Srgrimes
411573Srgrimes/*
421573Srgrimes * hist.c: History access functions
431573Srgrimes */
441573Srgrimes#include "sys.h"
451573Srgrimes#include <stdlib.h>
461573Srgrimes#include "el.h"
471573Srgrimes
481573Srgrimes/* hist_init():
491573Srgrimes *	Initialization function.
501573Srgrimes */
511573Srgrimesprotected int
521573Srgrimeshist_init(el)
531573Srgrimes    EditLine *el;
541573Srgrimes{
551573Srgrimes    el->el_history.fun  = NULL;
561573Srgrimes    el->el_history.ref  = NULL;
571573Srgrimes    el->el_history.buf   = (char *) el_malloc(EL_BUFSIZ);
581573Srgrimes    el->el_history.last  = el->el_history.buf;
591573Srgrimes    return 0;
601573Srgrimes}
611573Srgrimes
621573Srgrimes
631573Srgrimes/* hist_end():
641573Srgrimes *	clean up history;
651573Srgrimes */
661573Srgrimesprotected void
671573Srgrimeshist_end(el)
681573Srgrimes    EditLine *el;
691573Srgrimes{
701573Srgrimes    el_free((ptr_t) el->el_history.buf);
711573Srgrimes    el->el_history.buf   = NULL;
721573Srgrimes}
731573Srgrimes
741573Srgrimes
751573Srgrimes/* hist_set():
761573Srgrimes *	Set new history interface
771573Srgrimes */
781573Srgrimesprotected int
791573Srgrimeshist_set(el, fun, ptr)
801573Srgrimes    EditLine *el;
811573Srgrimes    hist_fun_t fun;
821573Srgrimes    ptr_t ptr;
831573Srgrimes
841573Srgrimes{
851573Srgrimes    el->el_history.ref = ptr;
861573Srgrimes    el->el_history.fun = fun;
871573Srgrimes    return 0;
881573Srgrimes}
891573Srgrimes
901573Srgrimes
911573Srgrimes/* hist_get():
921573Srgrimes *	Get a history line and update it in the buffer.
931573Srgrimes *	eventno tells us the event to get.
941573Srgrimes */
951573Srgrimesprotected el_action_t
961573Srgrimeshist_get(el)
971573Srgrimes    EditLine *el;
981573Srgrimes{
991573Srgrimes    const char    *hp;
1001573Srgrimes    int     h;
1011573Srgrimes
1021573Srgrimes    if (el->el_history.eventno == 0) {	/* if really the current line */
1031573Srgrimes	(void) strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ);
1041573Srgrimes	el->el_line.lastchar = el->el_line.buffer +
1051573Srgrimes		(el->el_history.last - el->el_history.buf);
1061573Srgrimes
1071573Srgrimes#ifdef KSHVI
1081573Srgrimes    if (el->el_map.type == MAP_VI)
1091573Srgrimes	el->el_line.cursor = el->el_line.buffer;
1101573Srgrimes    else
1111573Srgrimes#endif /* KSHVI */
1121573Srgrimes	el->el_line.cursor = el->el_line.lastchar;
1131573Srgrimes
1141573Srgrimes	return CC_REFRESH;
1151573Srgrimes    }
1161573Srgrimes
1171573Srgrimes    if (el->el_history.ref == NULL)
1181573Srgrimes	return CC_ERROR;
1191573Srgrimes
1201573Srgrimes    hp = HIST_FIRST(el);
1211573Srgrimes
1221573Srgrimes    if (hp == NULL)
1231573Srgrimes	return CC_ERROR;
1241573Srgrimes
1251573Srgrimes    for (h = 1; h < el->el_history.eventno; h++)
1261573Srgrimes	if ((hp = HIST_NEXT(el)) == NULL) {
1271573Srgrimes	    el->el_history.eventno = h;
1281573Srgrimes	    return CC_ERROR;
1291573Srgrimes	}
1301573Srgrimes
1311573Srgrimes    (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ);
1321573Srgrimes    el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer);
1331573Srgrimes
1341573Srgrimes    if (el->el_line.lastchar > el->el_line.buffer) {
1351573Srgrimes	if (el->el_line.lastchar[-1] == '\n')
1361573Srgrimes	    el->el_line.lastchar--;
1371573Srgrimes	if (el->el_line.lastchar[-1] == ' ')
1381573Srgrimes	    el->el_line.lastchar--;
1391573Srgrimes	if (el->el_line.lastchar < el->el_line.buffer)
1401573Srgrimes	    el->el_line.lastchar = el->el_line.buffer;
1411573Srgrimes    }
1421573Srgrimes
1431573Srgrimes#ifdef KSHVI
1441573Srgrimes    if (el->el_map.type == MAP_VI)
1451573Srgrimes	el->el_line.cursor = el->el_line.buffer;
1461573Srgrimes    else
1471573Srgrimes#endif /* KSHVI */
1481573Srgrimes	el->el_line.cursor = el->el_line.lastchar;
1491573Srgrimes
1501573Srgrimes    return CC_REFRESH;
1511573Srgrimes}
1521573Srgrimes
1531573Srgrimes/* hist_list()
1541573Srgrimes *	List history entries
1551573Srgrimes */
1561573Srgrimesprotected int
1571573Srgrimes/*ARGSUSED*/
1581573Srgrimeshist_list(el, argc, argv)
1591573Srgrimes    EditLine *el;
1601573Srgrimes    int argc;
1611573Srgrimes    char **argv;
1621573Srgrimes{
1631573Srgrimes    const char *str;
1641573Srgrimes
1651573Srgrimes    if (el->el_history.ref == NULL)
1661573Srgrimes	return -1;
1671573Srgrimes    for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el))
1681573Srgrimes	(void) fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str);
1691573Srgrimes    return 0;
1701573Srgrimes}
171