"Fossies" - the Fresh Open Source Software Archive

Member "tnftp-20200705/libedit/literal.c" (4 Jul 2020, 3870 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 "literal.c" see the Fossies "Dox" file reference documentation.

    1 /*  $NetBSD: literal.c,v 1.2 2020/07/04 14:34:28 lukem Exp $    */
    2 /*  from    NetBSD: literal.c,v 1.5 2019/07/23 13:10:11 christos Exp    */
    3 
    4 /*-
    5  * Copyright (c) 2017 The NetBSD Foundation, Inc.
    6  * All rights reserved.
    7  *
    8  * This code is derived from software contributed to The NetBSD Foundation
    9  * by Christos Zoulas.
   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  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 #include "config.h"
   34 
   35 #if 0 /* tnftp */
   36 #if !defined(lint) && !defined(SCCSID)
   37 __RCSID(" NetBSD: literal.c,v 1.5 2019/07/23 13:10:11 christos Exp  ");
   38 #endif /* not lint && not SCCSID */
   39 #endif /* tnftp */
   40 
   41 /*
   42  * literal.c: Literal sequences handling.
   43  */
   44 #if 0 /* tnftp */
   45 #include <assert.h>
   46 #include <stdio.h>
   47 #include <stdlib.h>
   48 #include <string.h>
   49 #endif /* tnftp */
   50 
   51 #include "el.h"
   52 
   53 libedit_private void
   54 literal_init(EditLine *el)
   55 {
   56     el_literal_t *l = &el->el_literal;
   57 
   58     memset(l, 0, sizeof(*l));
   59 }
   60 
   61 libedit_private void
   62 literal_end(EditLine *el)
   63 {
   64     literal_clear(el);
   65 }
   66 
   67 libedit_private void
   68 literal_clear(EditLine *el)
   69 {
   70     el_literal_t *l = &el->el_literal;
   71     size_t i;
   72 
   73     if (l->l_len == 0)
   74         return;
   75 
   76     for (i = 0; i < l->l_idx; i++)
   77         el_free(l->l_buf[i]);
   78     el_free(l->l_buf);
   79     l->l_buf = NULL;
   80     l->l_len = 0;
   81     l->l_idx = 0;
   82 }
   83 
   84 libedit_private wint_t
   85 literal_add(EditLine *el, const wchar_t *buf, const wchar_t *end, int *wp)
   86 {
   87     el_literal_t *l = &el->el_literal;
   88     size_t i, len;
   89     ssize_t w, n;
   90     char *b;
   91 
   92     w = wcwidth(end[1]);    /* column width of the visible char */
   93     *wp = (int)w;
   94 
   95     if (w <= 0)     /* we require something to be printed */
   96         return 0;
   97 
   98     len = (size_t)(end - buf);
   99     for (w = 0, i = 0; i < len; i++)
  100         w += ct_enc_width(buf[i]);
  101     w += ct_enc_width(end[1]);
  102 
  103     b = el_malloc((size_t)(w + 1));
  104     if (b == NULL)
  105         return 0;
  106 
  107     for (n = 0, i = 0; i < len; i++)
  108         n += ct_encode_char(b + n, (size_t)(w - n), buf[i]);
  109     n += ct_encode_char(b + n, (size_t)(w - n), end[1]);
  110     b[n] = '\0';
  111 
  112     /*
  113      * Then save this literal string in the list of such strings,
  114      * and return a "magic character" to put into the terminal buffer.
  115      * When that magic char is 'printed' the saved string (which includes
  116      * the char that belongs in that position) gets sent instead.
  117      */
  118     if (l->l_idx == l->l_len) {
  119         char **bp;
  120 
  121         l->l_len += 4;
  122         bp = el_realloc(l->l_buf, sizeof(*l->l_buf) * l->l_len);
  123         if (bp == NULL) {
  124             free(b);
  125             l->l_len -= 4;
  126             return 0;
  127         }
  128         l->l_buf = bp;
  129     }
  130     l->l_buf[l->l_idx++] = b;
  131     return EL_LITERAL | (wint_t)(l->l_idx - 1);
  132 }
  133 
  134 libedit_private const char *
  135 literal_get(EditLine *el, wint_t idx)
  136 {
  137     el_literal_t *l = &el->el_literal;
  138 
  139     assert(idx & EL_LITERAL);
  140     idx &= ~EL_LITERAL;
  141     assert(l->l_idx > (size_t)idx);
  142     return l->l_buf[idx];
  143 }