diff --git a/bootstrap.conf b/bootstrap.conf index 70ae68e..9b8a609 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -65,6 +65,7 @@ ssize_t stdckdint-h streq strerror +strnlen strtoimax strverscmp unlocked-io diff --git a/lib/.gitignore b/lib/.gitignore index ff4e416..56abef9 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -249,6 +249,7 @@ /string.in.h /stripslash.c /strncpy.c +/strnlen.c /strnul.c /strtoimax.c /strtol.c diff --git a/m4/.gitignore b/m4/.gitignore index 70cca65..ddf2d0b 100644 --- a/m4/.gitignore +++ b/m4/.gitignore @@ -1,4 +1,3 @@ -/lock.m4 /00gnulib.m4 /__inline.m4 /absolute-header.m4 @@ -222,6 +221,7 @@ /strerror_r.m4 /string_h.m4 /strncpy.m4 +/strnlen.m4 /strtoimax.m4 /strtoll.m4 /strverscmp.m4 diff --git a/sed/compile.c b/sed/compile.c index 144233f..29162ed 100644 --- a/sed/compile.c +++ b/sed/compile.c @@ -1195,9 +1195,9 @@ compile_program (struct vector *vector) /* trans_pairs = {src(0), dest(0), src(1), dest(1), ... } src(i) : i-th source character. dest(i) : i-th destination character. */ - int *trans_pairs = xnmalloc (src_char_num, - 2 * sizeof *trans_pairs); - cur_cmd->x.translate.a.pair = trans_pairs; + struct trans_pair *trans_pair = xnmalloc (src_char_num, + sizeof *trans_pair); + cur_cmd->x.translate.a.pair = trans_pair; for (idx_t i = 0; i < src_char_num; i++) { if (idx >= dest_len) @@ -1205,7 +1205,7 @@ compile_program (struct vector *vector) /* Set the i-th source character. */ mcel_t s = mcel_scan (src_buf, src_buf + len); - trans_pairs[2 * i] = s.err ? -s.err : s.ch; + trans_pair[i].from = s.err ? -s.err : s.ch; src_buf += s.len; /* Forward to next character. */ len -= s.len; @@ -1213,7 +1213,9 @@ compile_program (struct vector *vector) mcel_t d = mcel_scan (dest_buf + idx, dest_buf + dest_len); /* Set the i-th destination character. */ - trans_pairs[2 * i + 1] = d.err ? -d.err : d.ch; + memcpy (trans_pair[i].to, dest_buf + idx, d.len); + if (d.len < sizeof trans_pair[i].to) + trans_pair[i].to[d.len] = '\0'; idx += d.len; /* Forward to next character. */ } if (idx != dest_len) diff --git a/sed/debug.c b/sed/debug.c index dc80866..2f4fe4c 100644 --- a/sed/debug.c +++ b/sed/debug.c @@ -249,10 +249,13 @@ debug_print_translation (const struct sed_cmd *sc) /* multibyte translation */ putchar ('/'); for (idx_t i = 0; i < npairs; i++) - debug_print_mb (sc->x.translate.a.pair[2 * i]); + debug_print_mb (sc->x.translate.a.pair[i].from); putchar ('/'); for (idx_t i = 0; i < npairs; i++) - debug_print_mb (sc->x.translate.a.pair[2 * i + 1]); + fwrite (sc->x.translate.a.pair[i].to, 1, + 1 + strnlen (sc->x.translate.a.pair[i].to + 1, + sizeof sc->x.translate.a.pair[i].to - 1), + stdout); putchar ('/'); } else diff --git a/sed/execute.c b/sed/execute.c index f9b1ac2..352cd84 100644 --- a/sed/execute.c +++ b/sed/execute.c @@ -1102,7 +1102,7 @@ do_subst (struct subst *sub) /* Translate the global input LINE via TRANS. This function handles the multi-byte case. */ static void -translate_mb (idx_t npairs, int *pair) +translate_mb (idx_t npairs, struct trans_pair *pair) { mcel_t g; for (idx_t idx = 0; idx < line.length; idx += g.len) @@ -1113,19 +1113,11 @@ translate_mb (idx_t npairs, int *pair) /* 'i' indicate i-th translate pair. */ for (idx_t i = 0; i < npairs; i++) { - if (pair[2 * i] == ch) + if (pair[i].from == ch) { bool move_remain_buffer = false; - int tr = pair[2 * i + 1]; - char mbuf[MCEL_LEN_MAX]; - idx_t trans_len; - if (tr < 0) - { - mbuf[0] = -tr; - trans_len = 1; - } - else - trans_len = c32rtomb1 (mbuf, tr); + char *tr = pair[i].to; + idx_t trans_len = 1 + strnlen (tr + 1, sizeof pair[i].to - 1); if (g.len < trans_len) { @@ -1151,7 +1143,7 @@ translate_mb (idx_t npairs, int *pair) line.length += move_offset; idx += move_offset; } - memcpy (line.active + prev_idx, mbuf, trans_len); + memcpy (line.active + prev_idx, tr, trans_len); break; } } diff --git a/sed/sed.h b/sed/sed.h index e44655d..e76a108 100644 --- a/sed/sed.h +++ b/sed/sed.h @@ -197,11 +197,21 @@ struct sed_cmd { sb[(unsigned char) {I}] is the translation of I. */ char *sb; - /* An array used for multibyte translation. For 0 <= I < 2 * npairs, - the translation of pair[I] is pair[I + 1] when I is even, - and a negative pair[I] stands for the encoding error byte -pair[I] - instead of for the usual char32_t character pair[I]. */ - int *pair; + /* An array used for multibyte translation. For 0 <= I < npairs, + the translation of pair[I].from is pair[I].to. */ + struct trans_pair + { + /* The character or encding-error byte translated from. + A negative value represents a negated encoding error byte + instead of the usual char32_t character value. */ + int from; + + /* The multibyte representation of the translation. + It is always at least one byte long; after that is null + terminated unless it is exactly MCEL_LEN_MAX bytes. + A null byte is therefore represented by !to[0] && !to[1]. */ + char to[MCEL_LEN_MAX]; + } *pair; } a; } translate;