1 1.34 christos /* $NetBSD: hist.c,v 1.34 2019/07/23 10:19:35 christos Exp $ */ 2 1.2 lukem 3 1.1 cgd /*- 4 1.1 cgd * Copyright (c) 1992, 1993 5 1.1 cgd * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * This code is derived from software contributed to Berkeley by 8 1.1 cgd * Christos Zoulas of Cornell University. 9 1.1 cgd * 10 1.1 cgd * Redistribution and use in source and binary forms, with or without 11 1.1 cgd * modification, are permitted provided that the following conditions 12 1.1 cgd * are met: 13 1.1 cgd * 1. Redistributions of source code must retain the above copyright 14 1.1 cgd * notice, this list of conditions and the following disclaimer. 15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 cgd * notice, this list of conditions and the following disclaimer in the 17 1.1 cgd * documentation and/or other materials provided with the distribution. 18 1.14 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 cgd * may be used to endorse or promote products derived from this software 20 1.1 cgd * without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 cgd * SUCH DAMAGE. 33 1.1 cgd */ 34 1.1 cgd 35 1.10 christos #include "config.h" 36 1.1 cgd #if !defined(lint) && !defined(SCCSID) 37 1.2 lukem #if 0 38 1.1 cgd static char sccsid[] = "@(#)hist.c 8.1 (Berkeley) 6/4/93"; 39 1.2 lukem #else 40 1.34 christos __RCSID("$NetBSD: hist.c,v 1.34 2019/07/23 10:19:35 christos Exp $"); 41 1.2 lukem #endif 42 1.1 cgd #endif /* not lint && not SCCSID */ 43 1.1 cgd 44 1.1 cgd /* 45 1.1 cgd * hist.c: History access functions 46 1.1 cgd */ 47 1.1 cgd #include <stdlib.h> 48 1.24 christos #include <string.h> 49 1.30 christos #include <vis.h> 50 1.24 christos 51 1.1 cgd #include "el.h" 52 1.1 cgd 53 1.1 cgd /* hist_init(): 54 1.1 cgd * Initialization function. 55 1.1 cgd */ 56 1.29 christos libedit_private int 57 1.6 lukem hist_init(EditLine *el) 58 1.1 cgd { 59 1.6 lukem 60 1.6 lukem el->el_history.fun = NULL; 61 1.6 lukem el->el_history.ref = NULL; 62 1.33 christos el->el_history.buf = el_calloc(EL_BUFSIZ, sizeof(*el->el_history.buf)); 63 1.7 christos if (el->el_history.buf == NULL) 64 1.20 christos return -1; 65 1.34 christos el->el_history.sz = EL_BUFSIZ; 66 1.6 lukem el->el_history.last = el->el_history.buf; 67 1.20 christos return 0; 68 1.1 cgd } 69 1.1 cgd 70 1.1 cgd 71 1.1 cgd /* hist_end(): 72 1.1 cgd * clean up history; 73 1.1 cgd */ 74 1.29 christos libedit_private void 75 1.6 lukem hist_end(EditLine *el) 76 1.1 cgd { 77 1.6 lukem 78 1.19 christos el_free(el->el_history.buf); 79 1.6 lukem el->el_history.buf = NULL; 80 1.1 cgd } 81 1.1 cgd 82 1.1 cgd 83 1.1 cgd /* hist_set(): 84 1.1 cgd * Set new history interface 85 1.1 cgd */ 86 1.29 christos libedit_private int 87 1.19 christos hist_set(EditLine *el, hist_fun_t fun, void *ptr) 88 1.6 lukem { 89 1.1 cgd 90 1.6 lukem el->el_history.ref = ptr; 91 1.6 lukem el->el_history.fun = fun; 92 1.20 christos return 0; 93 1.1 cgd } 94 1.1 cgd 95 1.1 cgd 96 1.1 cgd /* hist_get(): 97 1.1 cgd * Get a history line and update it in the buffer. 98 1.1 cgd * eventno tells us the event to get. 99 1.1 cgd */ 100 1.29 christos libedit_private el_action_t 101 1.6 lukem hist_get(EditLine *el) 102 1.1 cgd { 103 1.28 christos const wchar_t *hp; 104 1.6 lukem int h; 105 1.31 christos size_t blen, hlen; 106 1.1 cgd 107 1.6 lukem if (el->el_history.eventno == 0) { /* if really the current line */ 108 1.27 christos (void) wcsncpy(el->el_line.buffer, el->el_history.buf, 109 1.8 jdolecek el->el_history.sz); 110 1.6 lukem el->el_line.lastchar = el->el_line.buffer + 111 1.6 lukem (el->el_history.last - el->el_history.buf); 112 1.1 cgd 113 1.1 cgd #ifdef KSHVI 114 1.6 lukem if (el->el_map.type == MAP_VI) 115 1.6 lukem el->el_line.cursor = el->el_line.buffer; 116 1.6 lukem else 117 1.1 cgd #endif /* KSHVI */ 118 1.6 lukem el->el_line.cursor = el->el_line.lastchar; 119 1.1 cgd 120 1.20 christos return CC_REFRESH; 121 1.6 lukem } 122 1.6 lukem if (el->el_history.ref == NULL) 123 1.20 christos return CC_ERROR; 124 1.1 cgd 125 1.6 lukem hp = HIST_FIRST(el); 126 1.1 cgd 127 1.6 lukem if (hp == NULL) 128 1.20 christos return CC_ERROR; 129 1.1 cgd 130 1.6 lukem for (h = 1; h < el->el_history.eventno; h++) 131 1.31 christos if ((hp = HIST_NEXT(el)) == NULL) 132 1.31 christos goto out; 133 1.31 christos 134 1.32 christos hlen = wcslen(hp) + 1; 135 1.31 christos blen = (size_t)(el->el_line.limit - el->el_line.buffer); 136 1.31 christos if (hlen > blen && !ch_enlargebufs(el, hlen)) 137 1.31 christos goto out; 138 1.31 christos 139 1.31 christos memcpy(el->el_line.buffer, hp, hlen * sizeof(*hp)); 140 1.31 christos el->el_line.lastchar = el->el_line.buffer + hlen - 1; 141 1.6 lukem 142 1.11 christos if (el->el_line.lastchar > el->el_line.buffer 143 1.11 christos && el->el_line.lastchar[-1] == '\n') 144 1.11 christos el->el_line.lastchar--; 145 1.11 christos if (el->el_line.lastchar > el->el_line.buffer 146 1.11 christos && el->el_line.lastchar[-1] == ' ') 147 1.11 christos el->el_line.lastchar--; 148 1.1 cgd #ifdef KSHVI 149 1.6 lukem if (el->el_map.type == MAP_VI) 150 1.6 lukem el->el_line.cursor = el->el_line.buffer; 151 1.6 lukem else 152 1.1 cgd #endif /* KSHVI */ 153 1.6 lukem el->el_line.cursor = el->el_line.lastchar; 154 1.1 cgd 155 1.20 christos return CC_REFRESH; 156 1.31 christos out: 157 1.31 christos el->el_history.eventno = h; 158 1.31 christos return CC_ERROR; 159 1.31 christos 160 1.1 cgd } 161 1.1 cgd 162 1.6 lukem 163 1.12 christos /* hist_command() 164 1.12 christos * process a history command 165 1.1 cgd */ 166 1.29 christos libedit_private int 167 1.28 christos hist_command(EditLine *el, int argc, const wchar_t **argv) 168 1.1 cgd { 169 1.28 christos const wchar_t *str; 170 1.12 christos int num; 171 1.27 christos HistEventW ev; 172 1.1 cgd 173 1.6 lukem if (el->el_history.ref == NULL) 174 1.20 christos return -1; 175 1.12 christos 176 1.27 christos if (argc == 1 || wcscmp(argv[1], L"list") == 0) { 177 1.30 christos size_t maxlen = 0; 178 1.30 christos char *buf = NULL; 179 1.30 christos int hno = 1; 180 1.12 christos /* List history entries */ 181 1.12 christos 182 1.30 christos for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) { 183 1.30 christos char *ptr = 184 1.30 christos ct_encode_string(str, &el->el_scratch); 185 1.30 christos size_t len = strlen(ptr); 186 1.30 christos if (len > 0 && ptr[len - 1] == '\n') 187 1.30 christos ptr[--len] = '\0'; 188 1.30 christos len = len * 4 + 1; 189 1.30 christos if (len >= maxlen) { 190 1.30 christos maxlen = len + 1024; 191 1.30 christos char *nbuf = el_realloc(buf, maxlen); 192 1.30 christos if (nbuf == NULL) { 193 1.30 christos el_free(buf); 194 1.30 christos return -1; 195 1.30 christos } 196 1.30 christos buf = nbuf; 197 1.30 christos } 198 1.30 christos strvis(buf, ptr, VIS_NL); 199 1.30 christos (void) fprintf(el->el_outfile, "%d\t%s\n", 200 1.30 christos hno++, buf); 201 1.30 christos } 202 1.30 christos el_free(buf); 203 1.20 christos return 0; 204 1.12 christos } 205 1.12 christos 206 1.15 christos if (argc != 3) 207 1.20 christos return -1; 208 1.12 christos 209 1.26 christos num = (int)wcstol(argv[2], NULL, 0); 210 1.12 christos 211 1.27 christos if (wcscmp(argv[1], L"size") == 0) 212 1.27 christos return history_w(el->el_history.ref, &ev, H_SETSIZE, num); 213 1.12 christos 214 1.27 christos if (wcscmp(argv[1], L"unique") == 0) 215 1.27 christos return history_w(el->el_history.ref, &ev, H_SETUNIQUE, num); 216 1.12 christos 217 1.12 christos return -1; 218 1.8 jdolecek } 219 1.8 jdolecek 220 1.8 jdolecek /* hist_enlargebuf() 221 1.8 jdolecek * Enlarge history buffer to specified value. Called from el_enlargebufs(). 222 1.8 jdolecek * Return 0 for failure, 1 for success. 223 1.8 jdolecek */ 224 1.29 christos libedit_private int 225 1.8 jdolecek /*ARGSUSED*/ 226 1.8 jdolecek hist_enlargebuf(EditLine *el, size_t oldsz, size_t newsz) 227 1.8 jdolecek { 228 1.28 christos wchar_t *newbuf; 229 1.8 jdolecek 230 1.16 christos newbuf = el_realloc(el->el_history.buf, newsz * sizeof(*newbuf)); 231 1.8 jdolecek if (!newbuf) 232 1.8 jdolecek return 0; 233 1.8 jdolecek 234 1.16 christos (void) memset(&newbuf[oldsz], '\0', (newsz - oldsz) * sizeof(*newbuf)); 235 1.8 jdolecek 236 1.8 jdolecek el->el_history.last = newbuf + 237 1.8 jdolecek (el->el_history.last - el->el_history.buf); 238 1.8 jdolecek el->el_history.buf = newbuf; 239 1.8 jdolecek el->el_history.sz = newsz; 240 1.8 jdolecek 241 1.8 jdolecek return 1; 242 1.1 cgd } 243 1.17 christos 244 1.29 christos libedit_private wchar_t * 245 1.19 christos hist_convert(EditLine *el, int fn, void *arg) 246 1.17 christos { 247 1.17 christos HistEventW ev; 248 1.17 christos if ((*(el)->el_history.fun)((el)->el_history.ref, &ev, fn, arg) == -1) 249 1.17 christos return NULL; 250 1.17 christos return ct_decode_string((const char *)(const void *)ev.str, 251 1.17 christos &el->el_scratch); 252 1.17 christos } 253