1*54217Sbostic /*- 2*54217Sbostic * Copyright (c) 1992 The Regents of the University of California. 3*54217Sbostic * All rights reserved. 4*54217Sbostic * 5*54217Sbostic * This code is derived from software contributed to Berkeley by 6*54217Sbostic * Christos Zoulas of Cornell University. 7*54217Sbostic * 8*54217Sbostic * %sccs.include.redist.c% 9*54217Sbostic */ 10*54217Sbostic 11*54217Sbostic #ifndef lint 12*54217Sbostic static char sccsid[] = "@(#)hist.c 5.1 (Berkeley) 06/22/92"; 13*54217Sbostic #endif /* not lint */ 14*54217Sbostic 15*54217Sbostic /* 16*54217Sbostic * el.hist.c: History access functions 17*54217Sbostic */ 18*54217Sbostic #include "sys.h" 19*54217Sbostic #include <stdlib.h> 20*54217Sbostic #include "el.h" 21*54217Sbostic 22*54217Sbostic /* hist_init(): 23*54217Sbostic * Initialization function. 24*54217Sbostic */ 25*54217Sbostic protected int 26*54217Sbostic hist_init(el) 27*54217Sbostic EditLine *el; 28*54217Sbostic { 29*54217Sbostic el->el_history.fun = NULL; 30*54217Sbostic el->el_history.ref = NULL; 31*54217Sbostic el->el_history.buf = (char *) el_malloc(EL_BUFSIZ); 32*54217Sbostic el->el_history.last = el->el_history.buf; 33*54217Sbostic return 0; 34*54217Sbostic } 35*54217Sbostic 36*54217Sbostic 37*54217Sbostic /* hist_end(): 38*54217Sbostic * clean up history; 39*54217Sbostic */ 40*54217Sbostic protected void 41*54217Sbostic hist_end(el) 42*54217Sbostic EditLine *el; 43*54217Sbostic { 44*54217Sbostic el_free((ptr_t) el->el_history.buf); 45*54217Sbostic el->el_history.buf = NULL; 46*54217Sbostic } 47*54217Sbostic 48*54217Sbostic 49*54217Sbostic /* hist_set(): 50*54217Sbostic * Set new history interface 51*54217Sbostic */ 52*54217Sbostic protected int 53*54217Sbostic hist_set(el, fun, ptr) 54*54217Sbostic EditLine *el; 55*54217Sbostic hist_fun_t fun; 56*54217Sbostic ptr_t ptr; 57*54217Sbostic 58*54217Sbostic { 59*54217Sbostic el->el_history.ref = ptr; 60*54217Sbostic el->el_history.fun = fun; 61*54217Sbostic return 0; 62*54217Sbostic } 63*54217Sbostic 64*54217Sbostic 65*54217Sbostic /* hist_get(): 66*54217Sbostic * Get a history line and update it in the buffer. 67*54217Sbostic * eventno tells us the event to get. 68*54217Sbostic */ 69*54217Sbostic protected el_action_t 70*54217Sbostic hist_get(el) 71*54217Sbostic EditLine *el; 72*54217Sbostic { 73*54217Sbostic const char *hp; 74*54217Sbostic int h; 75*54217Sbostic 76*54217Sbostic if (el->el_history.eventno == 0) { /* if really the current line */ 77*54217Sbostic (void) strncpy(el->el_line.buffer, el->el_history.buf, EL_BUFSIZ); 78*54217Sbostic el->el_line.lastchar = el->el_line.buffer + 79*54217Sbostic (el->el_history.last - el->el_history.buf); 80*54217Sbostic 81*54217Sbostic #ifdef KSHVI 82*54217Sbostic if (el->el_map.type == MAP_VI) 83*54217Sbostic el->el_line.cursor = el->el_line.buffer; 84*54217Sbostic else 85*54217Sbostic #endif /* KSHVI */ 86*54217Sbostic el->el_line.cursor = el->el_line.lastchar; 87*54217Sbostic 88*54217Sbostic return CC_REFRESH; 89*54217Sbostic } 90*54217Sbostic 91*54217Sbostic if (el->el_history.ref == NULL) 92*54217Sbostic return CC_ERROR; 93*54217Sbostic 94*54217Sbostic hp = HIST_FIRST(el); 95*54217Sbostic 96*54217Sbostic if (hp == NULL) 97*54217Sbostic return CC_ERROR; 98*54217Sbostic 99*54217Sbostic for (h = 1; h < el->el_history.eventno; h++) 100*54217Sbostic if ((hp = HIST_NEXT(el)) == NULL) { 101*54217Sbostic el->el_history.eventno = h; 102*54217Sbostic return CC_ERROR; 103*54217Sbostic } 104*54217Sbostic 105*54217Sbostic (void) strncpy(el->el_line.buffer, hp, EL_BUFSIZ); 106*54217Sbostic el->el_line.lastchar = el->el_line.buffer + strlen(el->el_line.buffer); 107*54217Sbostic 108*54217Sbostic if (el->el_line.lastchar > el->el_line.buffer) { 109*54217Sbostic if (el->el_line.lastchar[-1] == '\n') 110*54217Sbostic el->el_line.lastchar--; 111*54217Sbostic if (el->el_line.lastchar[-1] == ' ') 112*54217Sbostic el->el_line.lastchar--; 113*54217Sbostic if (el->el_line.lastchar < el->el_line.buffer) 114*54217Sbostic el->el_line.lastchar = el->el_line.buffer; 115*54217Sbostic } 116*54217Sbostic 117*54217Sbostic #ifdef KSHVI 118*54217Sbostic if (el->el_map.type == MAP_VI) 119*54217Sbostic el->el_line.cursor = el->el_line.buffer; 120*54217Sbostic else 121*54217Sbostic #endif /* KSHVI */ 122*54217Sbostic el->el_line.cursor = el->el_line.lastchar; 123*54217Sbostic 124*54217Sbostic return CC_REFRESH; 125*54217Sbostic } 126*54217Sbostic 127*54217Sbostic /* hist_list() 128*54217Sbostic * List history entries 129*54217Sbostic */ 130*54217Sbostic protected int 131*54217Sbostic /*ARGSUSED*/ 132*54217Sbostic hist_list(el, argc, argv) 133*54217Sbostic EditLine *el; 134*54217Sbostic int argc; 135*54217Sbostic char **argv; 136*54217Sbostic { 137*54217Sbostic const char *str; 138*54217Sbostic 139*54217Sbostic if (el->el_history.ref == NULL) 140*54217Sbostic return -1; 141*54217Sbostic for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) 142*54217Sbostic (void) fprintf(el->el_outfile, "%d %s", el->el_history.ev->num, str); 143*54217Sbostic return 0; 144*54217Sbostic } 145