| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* str_process
|
| 4 |
* - common string processing features with UTF-8 support
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#ifdef HAVE_CONFIG_H
|
| 10 |
#include "config.h"
|
| 11 |
#endif
|
| 12 |
|
| 13 |
#include "common.h"
|
| 14 |
#include "log.h"
|
| 15 |
#include "str_process.h"
|
| 16 |
#include <ctype.h>
|
| 17 |
#include <stdio.h>
|
| 18 |
#include <stdlib.h>
|
| 19 |
#include <string.h>
|
| 20 |
#include <wchar.h>
|
| 21 |
|
| 22 |
int UTF8_fixed_width = 1;
|
| 23 |
|
| 24 |
int str_length(const char *str, int skip_ctrl_seq)
|
| 25 |
{
|
| 26 |
int str_len;
|
| 27 |
char input_str[5];
|
| 28 |
wchar_t wcs[2];
|
| 29 |
int wc_len;
|
| 30 |
int i;
|
| 31 |
char c;
|
| 32 |
int ret = 0;
|
| 33 |
|
| 34 |
for (i = 0; str[i] != '\0'; i++)
|
| 35 |
{
|
| 36 |
c = str[i];
|
| 37 |
|
| 38 |
if (c == '\r' || c == '\7') // skip
|
| 39 |
{
|
| 40 |
continue;
|
| 41 |
}
|
| 42 |
|
| 43 |
if (skip_ctrl_seq && c == '\033' && str[i + 1] == '[') // Skip control sequence
|
| 44 |
{
|
| 45 |
for (i = i + 2; isdigit((int)str[i]) || str[i] == ';' || str[i] == '?'; i++)
|
| 46 |
;
|
| 47 |
|
| 48 |
if (str[i] == 'm') // valid
|
| 49 |
{
|
| 50 |
// skip
|
| 51 |
}
|
| 52 |
else if (isalpha((int)str[i]))
|
| 53 |
{
|
| 54 |
// unsupported ANSI CSI command
|
| 55 |
}
|
| 56 |
else
|
| 57 |
{
|
| 58 |
i--;
|
| 59 |
}
|
| 60 |
|
| 61 |
continue;
|
| 62 |
}
|
| 63 |
|
| 64 |
// Process UTF-8 Chinese characters
|
| 65 |
if (c & 0x80) // head of multi-byte character
|
| 66 |
{
|
| 67 |
str_len = 0;
|
| 68 |
c = (char)(c & 0xf0);
|
| 69 |
while (c & 0x80)
|
| 70 |
{
|
| 71 |
input_str[str_len] = str[i + str_len];
|
| 72 |
str_len++;
|
| 73 |
c = (c & 0x7f) << 1;
|
| 74 |
}
|
| 75 |
input_str[str_len] = '\0';
|
| 76 |
|
| 77 |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
|
| 78 |
{
|
| 79 |
log_debug("mbstowcs(%s) error", input_str);
|
| 80 |
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
|
| 81 |
}
|
| 82 |
else
|
| 83 |
{
|
| 84 |
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
|
| 85 |
}
|
| 86 |
|
| 87 |
i += (str_len - 1);
|
| 88 |
ret += wc_len;
|
| 89 |
}
|
| 90 |
else
|
| 91 |
{
|
| 92 |
ret++;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
return ret;
|
| 97 |
}
|
| 98 |
|
| 99 |
int split_line(const char *buffer, int max_display_len, int *p_eol, int *p_display_len, int skip_ctrl_seq)
|
| 100 |
{
|
| 101 |
int i;
|
| 102 |
*p_eol = 0;
|
| 103 |
*p_display_len = 0;
|
| 104 |
char c;
|
| 105 |
int str_len;
|
| 106 |
char input_str[5];
|
| 107 |
wchar_t wcs[2];
|
| 108 |
int wc_len;
|
| 109 |
|
| 110 |
for (i = 0; buffer[i] != '\0'; i++)
|
| 111 |
{
|
| 112 |
c = buffer[i];
|
| 113 |
|
| 114 |
if (c == '\r' || c == '\7') // skip
|
| 115 |
{
|
| 116 |
continue;
|
| 117 |
}
|
| 118 |
|
| 119 |
if (skip_ctrl_seq && c == '\033' && buffer[i + 1] == '[') // Skip control sequence
|
| 120 |
{
|
| 121 |
i += 2;
|
| 122 |
while (buffer[i] != '\0' && buffer[i] != 'm')
|
| 123 |
{
|
| 124 |
i++;
|
| 125 |
}
|
| 126 |
continue;
|
| 127 |
}
|
| 128 |
|
| 129 |
if (c & 0x80) // head of multi-byte character
|
| 130 |
{
|
| 131 |
str_len = 0;
|
| 132 |
c = (char)(c & 0xf0);
|
| 133 |
while (c & 0x80)
|
| 134 |
{
|
| 135 |
input_str[str_len] = buffer[i + str_len];
|
| 136 |
str_len++;
|
| 137 |
c = (c & 0x7f) << 1;
|
| 138 |
}
|
| 139 |
input_str[str_len] = '\0';
|
| 140 |
|
| 141 |
if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
|
| 142 |
{
|
| 143 |
log_debug("mbstowcs(%s) error", input_str);
|
| 144 |
wc_len = (UTF8_fixed_width ? 2 : 1); // Fallback
|
| 145 |
}
|
| 146 |
else
|
| 147 |
{
|
| 148 |
wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
|
| 149 |
}
|
| 150 |
|
| 151 |
if (*p_display_len + wc_len > max_display_len)
|
| 152 |
{
|
| 153 |
break;
|
| 154 |
}
|
| 155 |
|
| 156 |
i += (str_len - 1);
|
| 157 |
(*p_display_len) += wc_len;
|
| 158 |
}
|
| 159 |
else
|
| 160 |
{
|
| 161 |
if (*p_display_len + 1 > max_display_len)
|
| 162 |
{
|
| 163 |
break;
|
| 164 |
}
|
| 165 |
(*p_display_len)++;
|
| 166 |
|
| 167 |
// \n is regarded as 1 character wide in terminal editor, which is different from Web version
|
| 168 |
if (c == '\n')
|
| 169 |
{
|
| 170 |
i++;
|
| 171 |
*p_eol = 1;
|
| 172 |
break;
|
| 173 |
}
|
| 174 |
}
|
| 175 |
}
|
| 176 |
|
| 177 |
return i;
|
| 178 |
}
|
| 179 |
|
| 180 |
long split_data_lines(const char *p_buf, int max_display_len, long *p_line_offsets, long line_offsets_count,
|
| 181 |
int skip_ctrl_seq, int *p_line_widths)
|
| 182 |
{
|
| 183 |
int line_cnt = 0;
|
| 184 |
int len;
|
| 185 |
int end_of_line = 0;
|
| 186 |
int display_len = 0;
|
| 187 |
|
| 188 |
p_line_offsets[line_cnt] = 0L;
|
| 189 |
|
| 190 |
do
|
| 191 |
{
|
| 192 |
len = split_line(p_buf, max_display_len, &end_of_line, &display_len, skip_ctrl_seq);
|
| 193 |
|
| 194 |
if (p_line_widths)
|
| 195 |
{
|
| 196 |
p_line_widths[line_cnt] = display_len;
|
| 197 |
}
|
| 198 |
|
| 199 |
// Exceed max_line_cnt
|
| 200 |
if (line_cnt + 1 >= line_offsets_count)
|
| 201 |
{
|
| 202 |
log_debug("Line count %d reaches limit %d", line_cnt + 1, line_offsets_count);
|
| 203 |
return line_cnt;
|
| 204 |
}
|
| 205 |
|
| 206 |
p_line_offsets[line_cnt + 1] = p_line_offsets[line_cnt] + len;
|
| 207 |
line_cnt++;
|
| 208 |
p_buf += len;
|
| 209 |
} while (p_buf[0] != '\0' || end_of_line);
|
| 210 |
|
| 211 |
return line_cnt;
|
| 212 |
}
|
| 213 |
|
| 214 |
int str_filter(char *buffer, int skip_ctrl_seq)
|
| 215 |
{
|
| 216 |
int i;
|
| 217 |
int j;
|
| 218 |
|
| 219 |
for (i = 0, j = 0; buffer[i] != '\0'; i++)
|
| 220 |
{
|
| 221 |
if (buffer[i] == '\r' || buffer[i] == '\7') // skip
|
| 222 |
{
|
| 223 |
continue;
|
| 224 |
}
|
| 225 |
|
| 226 |
if (skip_ctrl_seq && buffer[i] == '\033' && buffer[i + 1] == '[') // Skip control sequence
|
| 227 |
{
|
| 228 |
i += 2;
|
| 229 |
while (buffer[i] != '\0' && buffer[i] != 'm')
|
| 230 |
{
|
| 231 |
i++;
|
| 232 |
}
|
| 233 |
continue;
|
| 234 |
}
|
| 235 |
|
| 236 |
buffer[j] = buffer[i];
|
| 237 |
j++;
|
| 238 |
}
|
| 239 |
|
| 240 |
buffer[j] = '\0';
|
| 241 |
|
| 242 |
return j;
|
| 243 |
}
|