1 /* $NetBSD: hist.c,v 1.13 2003/06/19 15:55:06 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. 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 "config.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.13 2003/06/19 15:55:06 christos Exp $"); 45 #endif 46 #endif /* not lint && not SCCSID */ 47 48 /* 49 * hist.c: History access functions 50 */ 51 #include <stdlib.h> 52 #include "el.h" 53 54 /* hist_init(): 55 * Initialization function. 56 */ 57 protected int 58 hist_init(EditLine *el) 59 { 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.sz = EL_BUFSIZ; 65 if (el->el_history.buf == NULL) 66 return (-1); 67 el->el_history.last = el->el_history.buf; 68 return (0); 69 } 70 71 72 /* hist_end(): 73 * clean up history; 74 */ 75 protected void 76 hist_end(EditLine *el) 77 { 78 79 el_free((ptr_t) el->el_history.buf); 80 el->el_history.buf = NULL; 81 } 82 83 84 /* hist_set(): 85 * Set new history interface 86 */ 87 protected int 88 hist_set(EditLine *el, hist_fun_t fun, 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(EditLine *el) 103 { 104 const char *hp; 105 int h; 106 107 if (el->el_history.eventno == 0) { /* if really the current line */ 108 (void) strncpy(el->el_line.buffer, el->el_history.buf, 109 el->el_history.sz); 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 if (el->el_history.ref == NULL) 123 return (CC_ERROR); 124 125 hp = HIST_FIRST(el); 126 127 if (hp == NULL) 128 return (CC_ERROR); 129 130 for (h = 1; h < el->el_history.eventno; h++) 131 if ((hp = HIST_NEXT(el)) == NULL) { 132 el->el_history.eventno = h; 133 return (CC_ERROR); 134 } 135 (void) strlcpy(el->el_line.buffer, hp, 136 (size_t)(el->el_line.limit - el->el_line.buffer)); 137 el->el_line.lastchar = el->el_line.buffer + strlen(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 protected int 160 hist_command(EditLine *el, int argc, const char **argv) 161 { 162 const char *str; 163 int num; 164 HistEvent ev; 165 166 if (el->el_history.ref == NULL) 167 return (-1); 168 169 if (argc == 0 || strcmp(argv[0], "list") == 1) { 170 /* List history entries */ 171 172 for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 173 (void) fprintf(el->el_outfile, "%d %s", 174 el->el_history.ev.num, str); 175 return (0); 176 } 177 178 if (argc != 2) 179 return (-1); 180 181 num = (int)strtol(argv[1], NULL, 0); 182 183 if (strcmp(argv[0], "size") == 0) 184 return history(el->el_history.ref, &ev, H_SETSIZE, num); 185 186 if (strcmp(argv[0], "unique") == 0) 187 return history(el->el_history.ref, &ev, H_SETUNIQUE, num); 188 189 return -1; 190 } 191 192 /* hist_enlargebuf() 193 * Enlarge history buffer to specified value. Called from el_enlargebufs(). 194 * Return 0 for failure, 1 for success. 195 */ 196 protected int 197 /*ARGSUSED*/ 198 hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz) 199 { 200 char *newbuf; 201 202 newbuf = realloc(el->el_history.buf, newsz); 203 if (!newbuf) 204 return 0; 205 206 (void) memset(&newbuf[oldsz], '\0', newsz - oldsz); 207 208 el->el_history.last = newbuf + 209 (el->el_history.last - el->el_history.buf); 210 el->el_history.buf = newbuf; 211 el->el_history.sz = newsz; 212 213 return 1; 214 } 215