hist.cxx

Go to the documentation of this file.
00001 // @(#)root/editline:$Id: hist.cxx 30194 2009-09-16 12:19:46Z axel $
00002 // Author: Mary-Louise Gill, 2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 /*      $NetBSD: hist.c,v 1.9 2001/05/17 01:02:17 christos Exp $        */
00013 
00014 /*-
00015  * Copyright (c) 1992, 1993
00016  *      The Regents of the University of California.  All rights reserved.
00017  *
00018  * This code is derived from software contributed to Berkeley by
00019  * Christos Zoulas of Cornell University.
00020  *
00021  * Redistribution and use in source and binary forms, with or without
00022  * modification, are permitted provided that the following conditions
00023  * are met:
00024  * 1. Redistributions of source code must retain the above copyright
00025  *    notice, this list of conditions and the following disclaimer.
00026  * 2. Redistributions in binary form must reproduce the above copyright
00027  *    notice, this list of conditions and the following disclaimer in the
00028  *    documentation and/or other materials provided with the distribution.
00029  * 3. Neither the name of the University nor the names of its contributors
00030  *    may be used to endorse or promote products derived from this software
00031  *    without specific prior written permission.
00032  *
00033  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00034  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00035  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00036  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00037  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00038  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00039  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00040  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00041  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00042  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00043  * SUCH DAMAGE.
00044  */
00045 
00046 #include "compat.h"
00047 
00048 /*
00049  * hist.c: History_t access functions
00050  */
00051 #include "sys.h"
00052 #include <stdlib.h>
00053 #include "el.h"
00054 
00055 /* hist_init():
00056  *      Initialization function.
00057  */
00058 el_protected int
00059 hist_init(EditLine_t* el) {
00060    el->fHistory.fFun = NULL;
00061    el->fHistory.fRef = NULL;
00062    el->fHistory.fBuf = (char*) el_malloc(EL_BUFSIZ);
00063    el->fHistory.fSz = EL_BUFSIZ;
00064 
00065    if (el->fHistory.fBuf == NULL) {
00066       return -1;
00067    }
00068    el->fHistory.fLast = el->fHistory.fBuf;
00069    return 0;
00070 }
00071 
00072 
00073 /* hist_end():
00074  *      clean up history;
00075  */
00076 el_protected void
00077 hist_end(EditLine_t* el) {
00078    el_free((ptr_t) el->fHistory.fBuf);
00079    el->fHistory.fBuf = NULL;
00080 }
00081 
00082 
00083 /* hist_set():
00084  *      Set new history interface
00085  */
00086 el_protected int
00087 hist_set(EditLine_t* el, HistFun_t fun, ptr_t ptr) {
00088    el->fHistory.fRef = ptr;
00089    el->fHistory.fFun = fun;
00090    return 0;
00091 }
00092 
00093 
00094 /* hist_get():
00095  *      Get a history line and update it in the buffer.
00096  *      eventno tells us the event to get.
00097  */
00098 el_protected ElAction_t
00099 hist_get(EditLine_t* el) {
00100    const char* hp;
00101    int h;
00102 
00103    if (el->fHistory.fEventNo == 0) {           /* if really the current line */
00104       (void) strncpy(el->fLine.fBuffer, el->fHistory.fBuf,
00105                      el->fHistory.fSz);
00106       ElColor_t* col = el->fLine.fBufColor;
00107 
00108       for (size_t i = 0; i < (size_t) el->fHistory.fSz; ++i) {
00109          col[i] = -1;
00110       }
00111       el->fLine.fLastChar = el->fLine.fBuffer +
00112                              (el->fHistory.fLast - el->fHistory.fBuf);
00113 
00114 #ifdef KSHVI
00115 
00116       if (el->fMap.fType == MAP_VI) {
00117          el->fLine.fCursor = el->fLine.fBuffer;
00118       } else
00119 #endif /* KSHVI */
00120       el->fLine.fCursor = el->fLine.fLastChar;
00121 
00122       return CC_REFRESH;
00123    }
00124 
00125    if (el->fHistory.fRef == NULL) {
00126       return CC_ERROR;
00127    }
00128 
00129    hp = HIST_FIRST(el);
00130 
00131    if (hp == NULL) {
00132       return CC_ERROR;
00133    }
00134 
00135    for (h = 1; h < el->fHistory.fEventNo; h++) {
00136       if ((hp = HIST_NEXT(el)) == NULL) {
00137          el->fHistory.fEventNo = h;
00138          return CC_ERROR;
00139       }
00140    }
00141    (void) strncpy(el->fLine.fBuffer, hp,
00142                   (size_t) (el->fLine.fLimit - el->fLine.fBuffer));
00143    ElColor_t* col = el->fLine.fBufColor;
00144 
00145    for (size_t i = 0; i < (size_t) (el->fLine.fLimit - el->fLine.fBuffer); ++i) {
00146       col[i] = -1;
00147    }
00148    el->fLine.fLastChar = el->fLine.fBuffer + strlen(el->fLine.fBuffer);
00149 
00150    if (el->fLine.fLastChar > el->fLine.fBuffer) {
00151       if (el->fLine.fLastChar[-1] == '\n') {
00152          el->fLine.fLastChar--;
00153       }
00154 
00155       if (el->fLine.fLastChar[-1] == ' ') {
00156          el->fLine.fLastChar--;
00157       }
00158 
00159       if (el->fLine.fLastChar < el->fLine.fBuffer) {
00160          el->fLine.fLastChar = el->fLine.fBuffer;
00161       }
00162    }
00163 #ifdef KSHVI
00164 
00165    if (el->fMap.fType == MAP_VI) {
00166       el->fLine.fCursor = el->fLine.fBuffer;
00167    } else
00168 #endif /* KSHVI */
00169    el->fLine.fCursor = el->fLine.fLastChar;
00170 
00171    return CC_REFRESH;
00172 } // hist_get
00173 
00174 
00175 /* hist_list()
00176  *      List history entries
00177  */
00178 el_protected int
00179 /*ARGSUSED*/
00180 hist_list(EditLine_t* el, int /*argc*/, const char** /*argv*/) {
00181    const char* str;
00182 
00183    if (el->fHistory.fRef == NULL) {
00184       return -1;
00185    }
00186 
00187    for (str = HIST_LAST(el); str != NULL; str = HIST_PREV(el)) {
00188       (void) fprintf(el->fOutFile, "%d %s%s",
00189                      el->fHistory.fEv.fNum, str,
00190                      (NULL == strstr(str, "\n")) ? "\n" : ""
00191 
00192                      /* ^^^
00193                         added by stephan@s11n.net to fix
00194                         when used with GNU RL compat mode,
00195                         which strips the newline (which is
00196                         more sane, IMO).
00197                       */
00198       );
00199    }
00200 
00201    return 0;
00202 } // hist_list
00203 
00204 
00205 /* hist_enlargebuf()
00206  *      Enlarge history buffer to specified value. Called from el_enlargebufs().
00207  *      Return 0 for failure, 1 for success.
00208  */
00209 el_protected int
00210 /*ARGSUSED*/
00211 hist_enlargebuf(EditLine_t* el, size_t oldsz, size_t newsz) {
00212    char* newbuf;
00213 
00214    newbuf = (char*) realloc(el->fHistory.fBuf, newsz);
00215 
00216    if (!newbuf) {
00217       return 0;
00218    }
00219 
00220    (void) memset(&newbuf[oldsz], '\0', newsz - oldsz);
00221 
00222    el->fHistory.fLast = newbuf +
00223                          (el->fHistory.fLast - el->fHistory.fBuf);
00224    el->fHistory.fBuf = newbuf;
00225    el->fHistory.fSz = newsz;
00226 
00227    return 1;
00228 } // hist_enlargebuf

Generated on Tue Jul 5 14:11:38 2011 for ROOT_528-00b_version by  doxygen 1.5.1