"Fossies" - the Fresh Open Source Software Archive

Member "tnftp-20200705/libedit/hist.c" (4 Jul 2020, 6577 Bytes) of package /linux/privat/tnftp-20200705.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "hist.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 20151004_vs_20200705.

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