1 /* $OpenBSD: hist.c,v 1.3 1997/03/14 05:12:48 millert Exp $ */ 2 /* $NetBSD: hist.c,v 1.2 1997/01/11 06:47:55 lukem Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Christos Zoulas of Cornell University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #if !defined(lint) && !defined(SCCSID) 41 #if 0 42 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 static char rcsid[] = "$OpenBSD: hist.c,v 1.3 1997/03/14 05:12:48 millert 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(el) 60 EditLine *el; 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(el) 75 EditLine *el; 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(el, fun, ptr) 87 EditLine *el; 88 hist_fun_t fun; 89 ptr_t ptr; 90 91 { 92 el->el_history.ref = ptr; 93 el->el_history.fun = fun; 94 return 0; 95 } 96 97 98 /* hist_get(): 99 * Get a history line and update it in the buffer. 100 * eventno tells us the event to get. 101 */ 102 protected el_action_t 103 hist_get(el) 104 EditLine *el; 105 { 106 const char *hp; 107 int h; 108 109 if (el->el_history.eventno == 0) { /* if really the current line */ 110 (void)strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ - 1); 111 el->el_line.buffer[EL_BUFSIZ - 1] = '\0'; 112 el->el_line.lastchar = el->el_line.buffer + 113 (el->el_history.last - el->el_history.buf); 114 115 #ifdef KSHVI 116 if (el->el_map.type == MAP_VI) 117 el->el_line.cursor = el->el_line.buffer; 118 else 119 #endif /* KSHVI */ 120 el->el_line.cursor = el->el_line.lastchar; 121 122 return CC_REFRESH; 123 } 124 125 if (el->el_history.ref == NULL) 126 return CC_ERROR; 127 128 hp = HIST_FIRST(el); 129 130 if (hp == NULL) 131 return CC_ERROR; 132 133 for (h = 1; h < el->el_history.eventno; h++) 134 if ((hp = HIST_NEXT(el)) == NULL) { 135 el->el_history.eventno = h; 136 return CC_ERROR; 137 } 138 139 (void)strncpy(el->el_line.buffer, hp, EL_BUFSIZ - 1); 140 el->el_line.buffer[EL_BUFSIZ - 1] = '\0'; 141 el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer); 142 143 if (el->el_line.lastchar > el->el_line.buffer) { 144 if (el->el_line.lastchar[-1] == '\n') 145 el->el_line.lastchar--; 146 if (el->el_line.lastchar[-1] == ' ') 147 el->el_line.lastchar--; 148 if (el->el_line.lastchar < el->el_line.buffer) 149 el->el_line.lastchar = el->el_line.buffer; 150 } 151 152 #ifdef KSHVI 153 if (el->el_map.type == MAP_VI) 154 el->el_line.cursor = el->el_line.buffer; 155 else 156 #endif /* KSHVI */ 157 el->el_line.cursor = el->el_line.lastchar; 158 159 return CC_REFRESH; 160 } 161 162 /* hist_list() 163 * List history entries 164 */ 165 protected int 166 /*ARGSUSED*/ 167 hist_list(el, argc, argv) 168 EditLine *el; 169 int argc; 170 char **argv; 171 { 172 const char *str; 173 174 if (el->el_history.ref == NULL) 175 return -1; 176 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 177 (void)fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str); 178 return 0; 179 } 180