varnish-cache/lib/libvarnish/vte.c
0
/*-
1
 * Copyright (c) 2019 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 */
30
31
#include "config.h"
32
33
#include <stdlib.h>
34
#include <string.h>
35
#include <sys/types.h> /* for MUSL (ssize_t) */
36
37
#include "vdef.h"
38
#include "vqueue.h"
39
40
#include "vas.h"
41
#include "vcli_serve.h"
42
#include "vsb.h"
43
44
#define MAXCOL 10
45
46
void
47 26264
VCLI_VTE(struct cli *cli, struct vsb **src, int width)
48
{
49
        int w_col[MAXCOL];
50 26264
        int n_col = 0;
51 26264
        int w_ln = 0;
52 26264
        int cc = 0;
53 26264
        int wc = 0;
54 26264
        int wl = 0;
55
        int nsp;
56
        const char *p;
57
        char *s;
58
59 26264
        AN(cli);
60 26264
        AN(src);
61 26264
        AN(*src);
62 26264
        AZ(VSB_finish(*src));
63 26264
        if (VSB_len(*src) == 0) {
64 56
                VSB_destroy(src);
65 56
                return;
66
        }
67 26208
        s = VSB_data(*src);
68 26208
        AN(s);
69 26208
        memset(w_col, 0, sizeof w_col);
70 3206437
        for (p = s; *p ; p++) {
71 3180229
                if (wl == 0 && *p == ' ') {
72 134288
                        while (p[1] != '\0' && *p != '\n')
73 132272
                                p++;
74 2016
                        continue;
75
                }
76 3178213
                if (*p == '\t' || *p == '\n') {
77 318190
                        if (wc > w_col[cc])
78 199917
                                w_col[cc] = wc;
79 318190
                        cc++;
80 318190
                        assert(cc < MAXCOL);
81 318190
                        wc = 0;
82 318190
                }
83 3178213
                if (*p == '\n') {
84 63862
                        n_col = vmax(n_col, cc);
85 63862
                        w_ln = vmax(w_ln, wl);
86 63862
                        cc = 0;
87 63862
                        wc = 0;
88 63862
                        wl = 0;
89 63862
                } else {
90 3114351
                        wc++;
91 3114351
                        wl++;
92
                }
93 3178213
        }
94
95 26208
        if (n_col == 0)
96 0
                return;
97 26208
        AN(n_col);
98
99 26208
        nsp = vlimit_t(int, (width - (w_ln)) / n_col, 1, 3);
100
101 26208
        cc = 0;
102 26208
        wc = 0;
103 3206437
        for (p = s; *p ; p++) {
104 3180229
                if (wc == 0 && cc == 0 && *p == ' ') {
105 134288
                        while (p[1] != '\0') {
106 134288
                                VCLI_Out(cli, "%c", *p);
107 134288
                                if (*p == '\n')
108 2016
                                        break;
109 132272
                                p++;
110
                        }
111 2016
                        continue;
112
                }
113 3178213
                if (*p == '\t') {
114 1524797
                        while (wc++ < w_col[cc] + nsp)
115 1270469
                                VCLI_Out(cli, " ");
116 254328
                        cc++;
117 254328
                        wc = 0;
118 3178213
                } else if (*p == '\n') {
119 63862
                        VCLI_Out(cli, "%c", *p);
120 63862
                        cc = 0;
121 63862
                        wc = 0;
122 63862
                } else {
123 2860023
                        VCLI_Out(cli, "%c", *p);
124 2860023
                        wc++;
125
                }
126 3178213
        }
127 26208
        VSB_destroy(src);
128 26264
}
129