1 /* $NetBSD: hist.c,v 1.6 2000/09/04 22:06:29 lukem 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if !defined(lint) && !defined(SCCSID) 41 #if 0 42 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 __RCSID("$NetBSD: hist.c,v 1.6 2000/09/04 22:06:29 lukem Exp $"); 45 #endif 46 #endif /* not lint && not SCCSID */ 47 48 /* 49 * hist.c: History access functions 50 */ 51 #include "sys.h" 52 #include <stdlib.h> 53 #include "el.h" 54 55 /* hist_init(): 56 * Initialization function. 57 */ 58 protected int 59 hist_init(EditLine *el) 60 { 61 62 el->el_history.fun = NULL; 63 el->el_history.ref = NULL; 64 el->el_history.buf = (char *) el_malloc(EL_BUFSIZ); 65 el->el_history.last = el->el_history.buf; 66 return (0); 67 } 68 69 70 /* hist_end(): 71 * clean up history; 72 */ 73 protected void 74 hist_end(EditLine *el) 75 { 76 77 el_free((ptr_t) el->el_history.buf); 78 el->el_history.buf = NULL; 79 } 80 81 82 /* hist_set(): 83 * Set new history interface 84 */ 85 protected int 86 hist_set(EditLine *el, hist_fun_t fun, ptr_t ptr) 87 { 88 89 el->el_history.ref = ptr; 90 el->el_history.fun = fun; 91 return (0); 92 } 93 94 95 /* hist_get(): 96 * Get a history line and update it in the buffer. 97 * eventno tells us the event to get. 98 */ 99 protected el_action_t 100 hist_get(EditLine *el) 101 { 102 const char *hp; 103 int h; 104 105 if (el->el_history.eventno == 0) { /* if really the current line */ 106 (void) strncpy(el->el_line.buffer, el->el_history.buf, 107 EL_BUFSIZ); 108 el->el_line.lastchar = el->el_line.buffer + 109 (el->el_history.last - el->el_history.buf); 110 111 #ifdef KSHVI 112 if (el->el_map.type == MAP_VI) 113 el->el_line.cursor = el->el_line.buffer; 114 else 115 #endif /* KSHVI */ 116 el->el_line.cursor = el->el_line.lastchar; 117 118 return (CC_REFRESH); 119 } 120 if (el->el_history.ref == NULL) 121 return (CC_ERROR); 122 123 hp = HIST_FIRST(el); 124 125 if (hp == NULL) 126 return (CC_ERROR); 127 128 for (h = 1; h < el->el_history.eventno; h++) 129 if ((hp = HIST_NEXT(el)) == NULL) { 130 el->el_history.eventno = h; 131 return (CC_ERROR); 132 } 133 (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ); 134 el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer); 135 136 if (el->el_line.lastchar > el->el_line.buffer) { 137 if (el->el_line.lastchar[-1] == '\n') 138 el->el_line.lastchar--; 139 if (el->el_line.lastchar[-1] == ' ') 140 el->el_line.lastchar--; 141 if (el->el_line.lastchar < el->el_line.buffer) 142 el->el_line.lastchar = el->el_line.buffer; 143 } 144 #ifdef KSHVI 145 if (el->el_map.type == MAP_VI) 146 el->el_line.cursor = el->el_line.buffer; 147 else 148 #endif /* KSHVI */ 149 el->el_line.cursor = el->el_line.lastchar; 150 151 return (CC_REFRESH); 152 } 153 154 155 /* hist_list() 156 * List history entries 157 */ 158 protected int 159 /*ARGSUSED*/ 160 hist_list(EditLine *el, int argc, char **argv) 161 { 162 const char *str; 163 164 if (el->el_history.ref == NULL) 165 return (-1); 166 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 167 (void) fprintf(el->el_outfile, "%d %s", 168 el->el_history.ev.num, str); 169 return (0); 170 } 171