1 /* $NetBSD: hist.c,v 1.2 1997/01/11 06:47:55 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 #if !defined(lint) && !defined(SCCSID) 40 #if 0 41 static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; 42 #else 43 static char rcsid[] = "$NetBSD: hist.c,v 1.2 1997/01/11 06:47:55 lukem Exp $"; 44 #endif 45 #endif /* not lint && not SCCSID */ 46 47 /* 48 * hist.c: History access functions 49 */ 50 #include "sys.h" 51 #include <stdlib.h> 52 #include "el.h" 53 54 /* hist_init(): 55 * Initialization function. 56 */ 57 protected int 58 hist_init(el) 59 EditLine *el; 60 { 61 el->el_history.fun = NULL; 62 el->el_history.ref = NULL; 63 el->el_history.buf = (char *) el_malloc(EL_BUFSIZ); 64 el->el_history.last = el->el_history.buf; 65 return 0; 66 } 67 68 69 /* hist_end(): 70 * clean up history; 71 */ 72 protected void 73 hist_end(el) 74 EditLine *el; 75 { 76 el_free((ptr_t) el->el_history.buf); 77 el->el_history.buf = NULL; 78 } 79 80 81 /* hist_set(): 82 * Set new history interface 83 */ 84 protected int 85 hist_set(el, fun, ptr) 86 EditLine *el; 87 hist_fun_t fun; 88 ptr_t ptr; 89 90 { 91 el->el_history.ref = ptr; 92 el->el_history.fun = fun; 93 return 0; 94 } 95 96 97 /* hist_get(): 98 * Get a history line and update it in the buffer. 99 * eventno tells us the event to get. 100 */ 101 protected el_action_t 102 hist_get(el) 103 EditLine *el; 104 { 105 const char *hp; 106 int h; 107 108 if (el->el_history.eventno == 0) { /* if really the current line */ 109 (void) strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ); 110 el->el_line.lastchar = el->el_line.buffer + 111 (el->el_history.last - el->el_history.buf); 112 113 #ifdef KSHVI 114 if (el->el_map.type == MAP_VI) 115 el->el_line.cursor = el->el_line.buffer; 116 else 117 #endif /* KSHVI */ 118 el->el_line.cursor = el->el_line.lastchar; 119 120 return CC_REFRESH; 121 } 122 123 if (el->el_history.ref == NULL) 124 return CC_ERROR; 125 126 hp = HIST_FIRST(el); 127 128 if (hp == NULL) 129 return CC_ERROR; 130 131 for (h = 1; h < el->el_history.eventno; h++) 132 if ((hp = HIST_NEXT(el)) == NULL) { 133 el->el_history.eventno = h; 134 return CC_ERROR; 135 } 136 137 (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ); 138 el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer); 139 140 if (el->el_line.lastchar > el->el_line.buffer) { 141 if (el->el_line.lastchar[-1] == '\n') 142 el->el_line.lastchar--; 143 if (el->el_line.lastchar[-1] == ' ') 144 el->el_line.lastchar--; 145 if (el->el_line.lastchar < el->el_line.buffer) 146 el->el_line.lastchar = el->el_line.buffer; 147 } 148 149 #ifdef KSHVI 150 if (el->el_map.type == MAP_VI) 151 el->el_line.cursor = el->el_line.buffer; 152 else 153 #endif /* KSHVI */ 154 el->el_line.cursor = el->el_line.lastchar; 155 156 return CC_REFRESH; 157 } 158 159 /* hist_list() 160 * List history entries 161 */ 162 protected int 163 /*ARGSUSED*/ 164 hist_list(el, argc, argv) 165 EditLine *el; 166 int argc; 167 char **argv; 168 { 169 const char *str; 170 171 if (el->el_history.ref == NULL) 172 return -1; 173 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 174 (void) fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str); 175 return 0; 176 } 177