1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/* This file is part of GNU Dico
Copyright (C) 2008-2024 Sergey Poznyakoff
GNU Dico is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Dico is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Dico. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <dico.h>
#define ISWS(c) ((c) == ' ' || (c) == '\t')
#define ISQUOTE(c) ((c) == '"' || (c) == '\'')
static char quote_transtab[] = "\\\\\"\"a\ab\bf\fn\nr\rt\t";
int
dico_unquote_char(int c)
{
char *p;
for (p = quote_transtab; *p; p += 2) {
if (*p == c)
return p[1];
}
return 0;
}
int
dico_quote_char(int c)
{
char *p;
for (p = quote_transtab; *p; p += 2) {
if (p[1] == c)
return p[0];
}
return 0;
}
void
dico_tokenize_begin(struct dico_tokbuf *tb)
{
memset(tb, 0, sizeof(*tb));
}
void
dico_tokenize_end(struct dico_tokbuf *tb)
{
free (tb->tb_base);
free (tb->tb_tokv);
}
void
dico_tokenize_clear(struct dico_tokbuf *tb)
{
tb->tb_level = 0;
tb->tb_tokc = 0;
}
static int
_dico_tkn_grow(struct dico_tokbuf *tb, char *str, size_t len)
{
if (tb->tb_level + len > tb->tb_size) {
size_t newsize = ((tb->tb_level + len + TKNBLOCKSIZ - 1) /
TKNBLOCKSIZ) * TKNBLOCKSIZ;
char *newbase = realloc(tb->tb_base, newsize);
if (!newbase)
return ENOMEM;
tb->tb_base = newbase;
tb->tb_size = newsize;
}
memcpy(tb->tb_base + tb->tb_level, str, len);
tb->tb_level += len;
return 0;
}
static int
_dico_tkn_1grow(struct dico_tokbuf *tb, int ch)
{
char c = ch;
return _dico_tkn_grow(tb, &c, 1);
}
int
dico_tokenize_string(struct dico_tokbuf *tb, char *str)
{
struct utf8_iterator itr;
int i, argc = 0;
int rc;
size_t start_level;
char *p;
utf8_iter_first(&itr, str);
start_level = tb->tb_level;
for (rc = 0; rc == 0;) {
int quote;
for (; !utf8_iter_end_p(&itr)
&& utf8_iter_isascii(itr) && ISWS(*itr.curptr);
utf8_iter_next(&itr))
;
if (utf8_iter_end_p(&itr))
break;
if (utf8_iter_isascii(itr) && ISQUOTE(*itr.curptr)) {
quote = *itr.curptr;
utf8_iter_next(&itr);
} else
quote = 0;
for (; !utf8_iter_end_p(&itr)
&& !(utf8_iter_isascii(itr) && (quote ? 0 : ISWS(*itr.curptr)));
utf8_iter_next(&itr)) {
if (utf8_iter_isascii(itr)) {
if (*itr.curptr == quote) {
utf8_iter_next(&itr);
break;
} else if (*itr.curptr == '\\') {
utf8_iter_next(&itr);
if (utf8_iter_isascii(itr)) {
rc = _dico_tkn_1grow(tb, dico_quote_char(*itr.curptr));
} else {
rc = _dico_tkn_1grow(tb, '\\');
if (rc == 0)
rc = _dico_tkn_grow(tb, itr.curptr, itr.curwidth);
}
continue;
}
}
rc = _dico_tkn_grow(tb, itr.curptr, itr.curwidth);
}
if (rc == 0)
rc = _dico_tkn_1grow(tb, 0);
argc++;
}
if (rc)
return rc;
if (tb->tb_tokc + argc + 1 > tb->tb_tokm) {
size_t nmax = tb->tb_tokc + argc + 1;
char **nargv = realloc(tb->tb_tokv, sizeof(tb->tb_tokv[0]) * nmax);
if (!nargv)
return ENOMEM;
tb->tb_tokv = nargv;
tb->tb_tokm = nmax;
}
for (i = 0, p = tb->tb_base + start_level; i < argc; i++) {
tb->tb_tokv[tb->tb_tokc++] = p;
p += strlen(p) + 1;
}
tb->tb_tokv[tb->tb_tokc] = NULL;
return 0;
}
|